FdSimulation/PrimaryGeneratorKG/PrimaryGenerator.cc
Go to the documentation of this file.
1 #include "PrimaryGenerator.h"
2 
3 #include <fwk/CentralConfig.h>
4 #include <fwk/RandomEngineRegistry.h>
5 
6 #include <det/Detector.h>
7 
8 #include <evt/Event.h>
9 #include <evt/ShowerSimData.h>
10 #include <evt/DefaultShowerGeometryProducer.h>
11 
12 #include <utl/AugerUnits.h>
13 #include <utl/ErrorLogger.h>
14 #include <utl/Reader.h>
15 #include <utl/PhysicalConstants.h>
16 #include <utl/RandomEngine.h>
17 #include <utl/Particle.h>
18 #include <utl/NucleusProperties.h>
19 #include <utl/RandomSamplerFromPDF.h>
20 
21 #include <CLHEP/Random/RandFlat.h>
22 
23 using namespace std;
24 using namespace utl;
25 using namespace fwk;
26 using namespace PrimaryGeneratorKG;
27 
28 using CLHEP::RandFlat;
29 
30 
31 PrimaryGenerator::~PrimaryGenerator()
32 {
33  delete fRandomPDF;
34 }
35 
36 
39 {
40  CentralConfig* const cc = CentralConfig::GetInstance();
41  Branch topB = cc->GetTopBranch("PrimaryGeneratorKG");
42 
43  if (topB.GetChild("verbosity"))
44  topB.GetChild("verbosity").GetData(fVerbosity);
45 
46  Branch energyB = topB.GetChild("energy");
47  energyB.GetChild("minLogE").GetData(fMinLogE);
48  energyB.GetChild("maxLogE").GetData(fMaxLogE);
49  energyB.GetChild("index").GetData(fIndex);
50 
51  if (fMaxLogE < fMinLogE || fMinLogE <= 0 || fMaxLogE <= 0) {
52  stringstream msg;
53  msg << "Wrong energy assignement: " << fMaxLogE << " < " << fMinLogE;
54  ERROR(msg.str());
55  return eFailure;
56  }
57 
58  Branch primaryB = topB.GetChild("primaries");
59 
60  vector<double> primaryFractions;
61  vector<double> indices;
62  unsigned int index = 0;
63  double totFraction = 0;
64  for (Branch ptypeB = primaryB.GetFirstChild(); ptypeB; ptypeB = ptypeB.GetNextSibling()) {
65  if (ptypeB.GetName() == string("primary")) {
66  fPrimZ.push_back(ptypeB.GetChild("Z").Get<double>());
67  fPrimA.push_back(ptypeB.GetChild("A").Get<double>());
68  primaryFractions.push_back(ptypeB.GetChild("fraction").Get<double>());
69  indices.push_back(index);
70  totFraction += primaryFractions.back();
71  ++index;
72  }
73  }
74 
75  if (primaryFractions.size() < 1) {
76  ERROR("Particle type(s) not specified");
77  return eFailure;
78  }
79 
80  if (fabs(totFraction - 1) > 1e-6) {
81  stringstream msg;
82  msg << "Wrong particle fractions, totFraction = " << totFraction
83  << " ... please check!";
84  ERROR(msg.str());
85  return eFailure;
86  }
87 
88  fRandomEngine =
89  &RandomEngineRegistry::GetInstance().Get(RandomEngineRegistry::ePhysics);
90 
91  fRandomPDF =
92  new RandomSamplerFromPDF(indices, primaryFractions, RandomSamplerFromPDF::eDiscrete);
93 
94  ostringstream info;
95  info << " Version: " << GetVersionInfo(VModule::eRevisionNumber) << "\n"
96  " Parameters:\n"
97  " lg(Emin): " << fMinLogE << "\n"
98  " lg(Emax): " << fMaxLogE << "\n"
99  " index: " << fIndex << "\n"
100  " sampling from: dN/dE = E^index\n";
101  for (unsigned int i = 0; i < fPrimA.size(); ++i)
102  info << " primary " << setw(2) << i << ":"
103  " A=" << setw(2) << fPrimA[i]
104  << " Z=" << setw(2) << fPrimZ[i]
105  << " fraction=" << setw(4) << primaryFractions[i] << '\n';
106  INFO(info);
107 
108  return eSuccess;
109 }
110 
111 
113 PrimaryGenerator::Run(evt::Event& theEvent)
114 {
115  if (theEvent.HasSimShower()) {
116  ERROR("Event not cleared - has SimShower. Cannot produce primary.");
117  return eFailure;
118  }
120 
121  evt::ShowerSimData& theShower = theEvent.GetSimShower();
122 
123  stringstream info;
124 
125  theShower.SetEnergy(DiceEnergy());
126  info << "Shower energy log(E/eV)=" << log10(theShower.GetEnergy());
127 
128  const int prim = fRandomPDF->shoot(fRandomEngine->GetEngine());
129 
130  if (fPrimZ[prim] == 0 && fPrimA[prim] == 0)
132  else if (fPrimZ[prim] == 1 && fPrimA[prim] == 1)
134  else
135  theShower.SetPrimaryParticle(NucleusProperties::TypeCode(fPrimZ[prim],
136  fPrimA[prim]));
137 
138  info << ", Primary type=" << theShower.GetPrimaryParticle();
139  if (fVerbosity >= 1) {
140  INFO(info.str());
141  }
142 
143  return eSuccess;
144 }
145 
146 
148 PrimaryGenerator::Finish()
149 {
150  delete fRandomPDF;
151  fRandomPDF = nullptr;
152  return eSuccess;
153 }
154 
155 
156 double
157 PrimaryGenerator::DiceEnergy()
158 {
159  if (fMinLogE == fMaxLogE)
160  return pow(10, fMinLogE) * eV;
161 
162  const double minE = pow(10, fMinLogE);
163  const double maxE = pow(10, fMaxLogE);
164 
165  const double randNo = RandFlat::shoot(&fRandomEngine->GetEngine(), 0, 1);
166 
167  if (fIndex == -1)
168  return pow(10, fMinLogE + randNo * (fMaxLogE - fMinLogE)) * eV;
169 
170  return pow(pow(minE, fIndex + 1) + randNo * (pow(maxE, fIndex + 1) -
171  pow(minE, fIndex + 1)), 1/(fIndex + 1)) * eV;
172 }
const double eV
Definition: GalacticUnits.h:35
int GetPrimaryParticle() const
Get the type of the shower primary particle.
Definition: ShowerSimData.h:84
bool HasSimShower() const
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
void Init()
Initialise the registry.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
double pow(const double x, const unsigned int i)
Interface class to access Shower Simulated parameters.
Definition: ShowerSimData.h:49
Branch GetNextSibling() const
Get next sibling of this branch.
Definition: Branch.cc:284
Class representing a document branch.
Definition: Branch.h:107
ShowerSimData & GetSimShower()
void SetEnergy(const double theEnergy)
Set the energy of the shower primary particle.
Definition: ShowerSimData.h:91
double GetEnergy() const
Get the energy of the shower primary particle.
Definition: ShowerSimData.h:89
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
void SetPrimaryParticle(const int type)
Set the type of the shower primary particle.
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
void MakeSimShower(const evt::VShowerGeometryProducer &p)
Simple event generator to be used in conjunction with ConexShowerGeneratorKG or ProfileSimulatorOG.
v push_back(value)
Main configuration utility.
Definition: CentralConfig.h:51
Branch GetFirstChild() const
Get first child of this Branch.
Definition: Branch.cc:98
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)

, generated on Tue Sep 26 2023.