2#include "TRestGeant4ParticleSourceCosmics.h"
9mutex TRestGeant4ParticleSourceCosmics::fMutex;
10unique_ptr<TRandom3> TRestGeant4ParticleSourceCosmics::fRandom =
nullptr;
12const map<string, string> geant4ParticleNames = {
13 {
"neutron",
"neutron"}, {
"proton",
"proton"}, {
"gamma",
"gamma"}, {
"electron_minus",
"e-"},
14 {
"electron_plus",
"e+"}, {
"muon_minus",
"mu-"}, {
"muon_plus",
"mu+"},
17TRestGeant4ParticleSourceCosmics::TRestGeant4ParticleSourceCosmics() =
default;
20 lock_guard<mutex> lock(fMutex);
22 cout <<
"TRestGeant4ParticleSourceCosmics::InitFromConfigFile" << endl;
24 const auto particles =
25 GetParameter(
"particles",
"neutron,proton,gamma,electron_minus,electron_plus,muon_minus,muon_plus");
27 fDirection = Get3DVectorParameterWithUnits(
"direction", {0, -1, 0});
29 std::istringstream iss(particles);
31 while (std::getline(iss, name,
',')) {
32 fParticleNames.insert(name);
35 for (
const auto& particle : fParticleNames) {
36 if (geant4ParticleNames.find(particle) == geant4ParticleNames.end()) {
37 cerr <<
"Particle name '" << particle <<
"' is not allowed." << endl;
42 auto file = TFile::Open(fFilename.c_str(),
"READ");
43 if (file ==
nullptr) {
44 cerr <<
"File '" << fFilename <<
"' not found." << endl;
47 for (
const auto& particle : fParticleNames) {
48 auto histogramFromFile = file->Get<TH2D>(string(particle +
"_energy_zenith").c_str());
49 if (histogramFromFile ==
nullptr) {
50 cerr <<
"Histogram '" << particle <<
"' not found in file '" << fFilename <<
"'." << endl;
53 fHistograms[particle] =
new TH2D(*histogramFromFile);
54 const auto& histogram = fHistograms.at(particle);
55 TH2D* hist =
new TH2D(*histogram);
58 for (
int i = 1; i <= hist->GetNbinsX(); i++) {
59 for (
int j = 1; j <= hist->GetNbinsY(); j++) {
60 const double zenith = hist->GetYaxis()->GetBinCenter(j);
61 const double value = hist->GetBinContent(i, j) / TMath::Cos(zenith * TMath::DegToRad());
62 hist->SetBinContent(i, j, value);
65 fHistogramsTransformed[particle] = hist;
69 for (
const auto& particle : fParticleNames) {
70 auto hist = fHistograms.at(particle);
71 fParticleWeights[particle] += hist->GetEntries();
72 sum += fParticleWeights.at(particle);
74 for (
auto& entry : fParticleWeights) {
76 cout <<
"TRestGeant4ParticleSourceCosmics::InitFromConfigFile: particle: " << entry.first
77 <<
" sampling weight: " << entry.second << endl;
83void TRestGeant4ParticleSourceCosmics::Update() {
84 lock_guard<mutex> lock(fMutex);
89 if (fParticleNames.size() == 1) {
90 particleName = *fParticleNames.begin();
92 const auto random = fRandom->Uniform(0, 1);
95 for (
const auto& entry : fParticleWeights) {
98 particleName = entry.first;
104 auto hist = fHistogramsTransformed.at(particleName);
105 double energy, zenith;
106 hist->GetRandom2(energy, zenith, fRandom.get());
108 particleName = geant4ParticleNames.at(particleName);
111 particle.SetParticleName(particleName);
113 particle.SetEnergy(energy * 1000);
115 double phi = fRandom->Uniform(0, 1) * TMath::TwoPi();
116 double zenithRad = zenith * TMath::DegToRad();
119 const TVector3 direction = {TMath::Sin(zenithRad) * TMath::Cos(phi), -TMath::Cos(zenithRad),
120 TMath::Sin(zenithRad) * TMath::Sin(phi)};
122 particle.SetDirection(direction);
124 AddParticle(particle);
127void TRestGeant4ParticleSourceCosmics::SetSeed(
unsigned int seed) {
128 cout <<
"TRestGeant4ParticleSourceCosmics::SetSeed: " << seed << endl;
129 fRandom = std::make_unique<TRandom3>(seed);
void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
A class used to store particle properties.