REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestGeant4ParticleSourceCry.cxx
1 #include "TRestGeant4ParticleSourceCry.h"
2 
3 using namespace std;
4 
6 
7 TRestGeant4ParticleSourceCry::TRestGeant4ParticleSourceCry() {}
8 
14 
15  RESTMetadata << "Return Neutrons : " << fReturnNeutrons << RESTendl;
16  RESTMetadata << "Return Protons : " << fReturnProtons << RESTendl;
17  RESTMetadata << "Return Gammas : " << fReturnGammas << RESTendl;
18  RESTMetadata << "Return Electrons : " << fReturnElectrons << RESTendl;
19  RESTMetadata << "Return Pions : " << fReturnPions << RESTendl;
20  RESTMetadata << "Return Kaons : " << fReturnKaons << RESTendl;
21  RESTMetadata << "Return Muons : " << fReturnMuons << RESTendl;
22  RESTMetadata << " ======= " << RESTendl;
23 
24  RESTMetadata << "N particles min : " << fNParticlesMin << RESTendl;
25  RESTMetadata << "N particles max : " << fNParticlesMax << RESTendl;
26  RESTMetadata << " ======= " << RESTendl;
27 
28  RESTMetadata << "X-offset : " << fXOffset << "m" << RESTendl;
29  RESTMetadata << "Y-offset : " << fYOffset << "m" << RESTendl;
30  RESTMetadata << "Z-offset : " << fZOffset << "m" << RESTendl;
31  RESTMetadata << "SubBoxLength : " << fSubBoxLength << "m" << RESTendl;
32  RESTMetadata << " ======= " << RESTendl;
33 
34  RESTMetadata << "Date : " << fDate << RESTendl;
35  RESTMetadata << "Latitude : " << fLatitude << RESTendl;
36  RESTMetadata << "Altitude : " << fAltitude << RESTendl;
37  RESTMetadata << "----------------------" << RESTendl;
38 }
39 
44  fReturnNeutrons = StringToInteger(GetParameter("returnNeutrons", "1"));
45  fReturnProtons = StringToInteger(GetParameter("returnProtons", "1"));
46  fReturnGammas = StringToInteger(GetParameter("returnGammas", "1"));
47  fReturnElectrons = StringToInteger(GetParameter("returnElectrons", "1"));
48  fReturnPions = StringToInteger(GetParameter("returnPions", "1"));
49  fReturnKaons = StringToInteger(GetParameter("returnKaons", "1"));
50  fReturnMuons = StringToInteger(GetParameter("returnMuons", "1"));
51 
52  fNParticlesMin = StringToInteger(GetParameter("nParticlesMin", "1"));
53  fNParticlesMax = StringToInteger(GetParameter("nParticlesMax", "1000000"));
54 
55  fXOffset = StringToDouble(GetParameter("xoffset", "0.0"));
56  fYOffset = StringToDouble(GetParameter("yoffset", "0.0"));
57  fZOffset = StringToDouble(GetParameter("zoffset", "0.0"));
58  fSubBoxLength = StringToDouble(GetParameter("subBoxLength", "100.0"));
59 
60  fDate = GetParameter("date", "7\\1\\2012");
61  fDate = REST_StringHelper::Replace(fDate, "\\", "-");
62  fLatitude = StringToDouble(GetParameter("latitude", "90.0"));
63  fAltitude = StringToDouble(GetParameter("altitude", "0.0"));
64 
65  PrintMetadata();
66 
67  std::string setupString = "";
68  setupString += "returnNeutrons " + IntegerToString(fReturnNeutrons);
69  setupString += " returnProtons " + IntegerToString(fReturnProtons);
70  setupString += " returnGammas " + IntegerToString(fReturnGammas);
71  setupString += " returnElectrons " + IntegerToString(fReturnElectrons);
72  setupString += " returnPions " + IntegerToString(fReturnPions);
73  setupString += " returnKaons " + IntegerToString(fReturnKaons);
74  setupString += " returnMuons " + IntegerToString(fReturnMuons);
75 
76  setupString += " xoffset " + DoubleToString(fXOffset);
77  setupString += " yoffset " + DoubleToString(fYOffset);
78  setupString += " zoffset " + DoubleToString(fZOffset);
79  setupString += " subboxLength " + DoubleToString(fSubBoxLength);
80 
81  setupString += " date " + fDate;
82  setupString += " latitude " + DoubleToString(fLatitude);
83  setupString += " altitude " + DoubleToString(fAltitude);
84 
85  setupString += " nParticlesMin " + IntegerToString(fNParticlesMin);
86  setupString += " nParticlesMax " + IntegerToString(fNParticlesMax);
87 
88 #ifdef USE_CRY
89  CRYSetup* setup = new CRYSetup(setupString, CRY_DATA_PATH);
90  fCRYGenerator = new CRYGenerator(setup);
91 #endif
92 
93  Update();
94 }
95 
100  RemoveParticles();
101 
102 #ifdef USE_CRY
103  std::vector<CRYParticle*>* ev = new std::vector<CRYParticle*>;
104  ev->clear();
105  fCRYGenerator->genEvent(ev);
106 
107  // std::cout << "CRY particles : " << ev->size() << std::endl;
108  // std::cout << "-----" << std::endl;
109 
110  for (const auto& cryParticle : *ev) {
111  // std::cout << "id: " << cryParticle->id() << std::endl;
112  // std::cout << "x: " << cryParticle->x() << " y: " << cryParticle->y() << " z: " << cryParticle->z()
113  // << std::endl; std::cout << "u: " << cryParticle->u() << " v: " << cryParticle->v() << " w: " <<
114  // cryParticle->w() << std::endl; std::cout << "charge: " << cryParticle->charge() << " energy: " <<
115  // cryParticle->ke() << std::endl;
116 
117  TRestGeant4Particle particle;
118 
120  particle.SetParticleCharge(cryParticle->charge());
121  particle.SetExcitationLevel(0);
122 
124  TVector3 position(cryParticle->x(), cryParticle->y(), cryParticle->z());
125  particle.SetOrigin(1000. * position); // In mm (default REST units)
126 
128  TVector3 momDirection(cryParticle->u(), cryParticle->v(), cryParticle->w());
129  momDirection = momDirection.Unit();
130  particle.SetDirection(momDirection);
131 
133  particle.SetEnergy(1000. * cryParticle->ke()); // In keV (default REST units)
134 
135  /*
136  * 0 : Neutron
137  * 1 : Proton
138  * 2 : Pion
139  * 3 : Kaon
140  * 4 : Muon
141  * 5 : Electron
142  * 6 : Gamma
143  */
144 
145  int id = cryParticle->id();
146  if (id == 0) particle.SetParticleName("neutron");
147  if (id == 1) particle.SetParticleName("proton");
148  if (id == 2) {
149  if (cryParticle->charge() > 0)
150  particle.SetParticleName("pi+");
151  else if (cryParticle->charge() == 0)
152  particle.SetParticleName("pi0");
153  else if (cryParticle->charge() < 0)
154  particle.SetParticleName("pi-");
155  }
156  if (id == 3) {
157  if (cryParticle->charge() > 0)
158  particle.SetParticleName("kaon+");
159  else if (cryParticle->charge() == 0)
160  particle.SetParticleName("kaon0");
161  else if (cryParticle->charge() < 0)
162  particle.SetParticleName("kaon-");
163  }
164  if (id == 4) {
165  if (cryParticle->charge() > 0)
166  particle.SetParticleName("mu+");
167  else if (cryParticle->charge() < 0)
168  particle.SetParticleName("mu-");
169  }
170  if (id == 5) {
171  if (cryParticle->charge() > 0)
172  particle.SetParticleName("e+");
173  else if (cryParticle->charge() < 0)
174  particle.SetParticleName("e-");
175  }
176  if (id == 6) particle.SetParticleName("gamma");
177 
178  AddParticle(particle);
179  }
180  // std::cout << "-----" << std::endl;
181 #else
182  cout << "TRestGeant4ParticleSourceCry - ERROR: Geant4lib was not linked to CRY libraries" << endl;
183  cout << " " << endl;
184  cout << "Please, compile REST using `cmake -DREST_CRY_PATH=/path/to/cry/installation/directory" << endl;
185  cout << " " << endl;
186  cout
187  << "By default CRY libraries will generate just the static library, but REST needs the shared library"
188  << endl;
189  cout << "In order to generate the SHARED object from the STATIC libCRY.a object, execute the following "
190  "command"
191  << endl;
192  cout << "inside the CRY lib directory" << endl;
193  cout << " " << endl;
194  cout << "```" << endl;
195  cout << "gcc -shared -o libCRY.so -Wl,--whole-archive libCRY.a -Wl,--no-whole-archive" << endl;
196  cout << "```" << endl;
197 
198  exit(1);
199 #endif
200 }
void PrintMetadata() override
It will print on screen the settings used for the CRY generator setup.
void Update() override
It is used by restG4 PrimaryGeneratorAction to update the particle source.
void InitFromConfigFile() override
Initialization of TRestGeant4ParticleSourceCry members through a RML file.
virtual void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
A class used to store particle properties.
std::string IntegerToString(Int_t n, std::string format="%d")
Gets a string from an integer.
Double_t StringToDouble(std::string in)
Gets a double from a string.
Int_t StringToInteger(std::string in)
Gets an integer from a string.
std::string DoubleToString(Double_t d, std::string format="%8.6e")
Gets a string from a double.
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.