REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestGeant4ParticleSource.h
1 
17 #ifndef RestCore_TRestGeant4ParticleSource
18 #define RestCore_TRestGeant4ParticleSource
19 
20 #include <TF1.h>
21 #include <TF2.h>
22 #include <TRestMetadata.h>
23 #include <TString.h>
24 #include <TVector2.h>
25 #include <TVector3.h>
26 
27 #include <iostream>
28 
29 #include "TRestGeant4Particle.h"
30 #include "TRestGeant4PrimaryGeneratorInfo.h"
31 
33  private:
34  void ReadEventDataFile(TString fName);
35  bool ReadNewDecay0File(TString fileName);
36  bool ReadOldDecay0File(TString fileName);
37 
38  protected:
39  TString fAngularDistributionType = "Flux";
40  TString fAngularDistributionFilename;
41  TString fAngularDistributionNameInFile;
42  size_t fAngularDistributionFormulaNPoints = 500;
43  TF1* fAngularDistributionFunction = nullptr;
44  TVector2 fAngularDistributionRange;
45 
46  TString fEnergyDistributionType = "Mono";
47  TString fEnergyDistributionFilename;
48  TString fEnergyDistributionNameInFile;
49  size_t fEnergyDistributionFormulaNPoints = 5000;
50  TF1* fEnergyDistributionFunction = nullptr;
51  TVector2 fEnergyDistributionRange;
52 
53  TF2* fEnergyAndAngularDistributionFunction = nullptr;
54 
55  TString fGenFilename;
56  // store a set of generated particles
57  std::vector<TRestGeant4Particle> fParticles;
58  // store a list of particle set templates that could be used to generate fParticles
59  std::vector<std::vector<TRestGeant4Particle>> fParticlesTemplate;
60  // the random method to generate 0~1 equally distributed numbers
61  double (*fRandomMethod)();
62 
63  public:
64  virtual void Update();
65  virtual void InitFromConfigFile() override;
66  static TRestGeant4ParticleSource* instantiate(std::string model = "");
67 
68  inline TVector3 GetDirection() const {
69  if (fDirection.Mag() <= 0) {
70  std::cout << "TRestGeant4ParticleSource::GetDirection: "
71  << "Direction cannot be the zero vector" << std::endl;
72  exit(1);
73  }
74  return fDirection;
75  }
76 
77  inline TString GetEnergyDistributionType() const { return fEnergyDistributionType; }
78  inline TVector2 GetEnergyDistributionRange() const { return fEnergyDistributionRange; }
79  inline Double_t GetEnergyDistributionRangeMin() const { return fEnergyDistributionRange.X(); }
80  inline Double_t GetEnergyDistributionRangeMax() const { return fEnergyDistributionRange.Y(); }
81  inline size_t GetEnergyDistributionFormulaNPoints() const { return fEnergyDistributionFormulaNPoints; }
82  inline TString GetEnergyDistributionFilename() const { return fEnergyDistributionFilename; }
83  inline TString GetEnergyDistributionNameInFile() const { return fEnergyDistributionNameInFile; }
84  inline const TF1* GetEnergyDistributionFunction() const { return fEnergyDistributionFunction; }
85 
86  inline TString GetAngularDistributionType() const { return fAngularDistributionType; }
87  inline TVector2 GetAngularDistributionRange() const { return fAngularDistributionRange; }
88  inline Double_t GetAngularDistributionRangeMin() const { return fAngularDistributionRange.X(); }
89  inline Double_t GetAngularDistributionRangeMax() const { return fAngularDistributionRange.Y(); }
90  inline size_t GetAngularDistributionFormulaNPoints() const { return fAngularDistributionFormulaNPoints; }
91  inline TString GetAngularDistributionFilename() const { return fAngularDistributionFilename; }
92  inline TString GetAngularDistributionNameInFile() const { return fAngularDistributionNameInFile; }
93  inline const TF1* GetAngularDistributionFunction() const { return fAngularDistributionFunction; }
94 
95  inline const TF2* GetEnergyAndAngularDistributionFunction() const {
96  return fEnergyAndAngularDistributionFunction;
97  }
98 
99  inline TString GetGenFilename() const { return fGenFilename; }
100 
101  inline std::vector<TRestGeant4Particle> GetParticles() const { return fParticles; }
102 
103  inline void SetAngularDistributionType(const TString& type) {
104  fAngularDistributionType = TRestGeant4PrimaryGeneratorTypes::AngularDistributionTypesToString(
105  TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionTypes(type.Data()));
106  }
107  inline void SetAngularDistributionRange(const TVector2& range) {
108  fAngularDistributionRange = range;
109  if (fAngularDistributionRange.X() < 0) {
110  fAngularDistributionRange.Set(0, fAngularDistributionRange.Y());
111  }
112  if (fAngularDistributionRange.Y() < 0) {
113  fAngularDistributionRange.Set(fAngularDistributionRange.X(), 0);
114  }
115  if (fAngularDistributionRange.Y() > TMath::Pi()) {
116  fAngularDistributionRange.Set(fAngularDistributionRange.X(), TMath::Pi());
117  }
118  if (fAngularDistributionRange.X() > fAngularDistributionRange.Y()) {
119  fAngularDistributionRange.Set(fAngularDistributionRange.Y(), fAngularDistributionRange.X());
120  }
121  }
122  inline void SetAngularDistributionFormulaNPoints(size_t nPoints) {
123  const auto nPointsMax = 10000;
124  if (nPoints > nPointsMax) {
125  nPoints = nPointsMax;
126  }
127  fAngularDistributionFormulaNPoints = nPoints;
128  }
129  inline void SetAngularDistributionFilename(const TString& filename) {
130  fAngularDistributionFilename = filename;
131  }
132  inline void SetAngularDistributionNameInFile(const TString& name) {
133  fAngularDistributionNameInFile = name;
134  }
135  inline void SetAngularDistributionFormula(const TString& formula) {
136  fAngularDistributionFunction =
137  (TF1*)TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToRootFormula(
138  TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionFormulas(formula.Data()))
139  .Clone();
140  }
141 
142  inline void SetEnergyDistributionType(const TString& type) {
143  fEnergyDistributionType = TRestGeant4PrimaryGeneratorTypes::EnergyDistributionTypesToString(
144  TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionTypes(type.Data()));
145  }
146  inline void SetEnergyDistributionRange(const TVector2& range) {
147  fEnergyDistributionRange = range;
148  if (fEnergyDistributionRange.X() < 0) {
149  fEnergyDistributionRange.Set(0, fEnergyDistributionRange.Y());
150  }
151  if (fEnergyDistributionRange.Y() < 0) {
152  fEnergyDistributionRange.Set(fEnergyDistributionRange.X(), 0);
153  }
154  if (fEnergyDistributionRange.X() > fEnergyDistributionRange.Y()) {
155  fEnergyDistributionRange.Set(fEnergyDistributionRange.Y(), fEnergyDistributionRange.X());
156  }
157  }
158  inline void SetEnergyDistributionFormulaNPoints(size_t nPoints) {
159  const auto nPointsMax = 10000;
160  if (nPoints > nPointsMax) {
161  nPoints = nPointsMax;
162  }
163  fEnergyDistributionFormulaNPoints = nPoints;
164  }
165  inline void SetEnergyDistributionFilename(const TString& filename) {
166  fEnergyDistributionFilename = filename;
167  }
168  inline void SetEnergyDistributionNameInFile(const TString& name) { fEnergyDistributionNameInFile = name; }
169  inline void SetEnergyDistributionFormula(const TString& formula) {
170  fEnergyDistributionFunction =
171  (TF1*)TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToRootFormula(
172  TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionFormulas(formula.Data()))
173  .Clone();
174  }
175 
176  inline void SetEnergyAndAngularDistributionFormula(const TString& formula) {
177  fEnergyAndAngularDistributionFunction =
178  (TF2*)TRestGeant4PrimaryGeneratorTypes::EnergyAndAngularDistributionFormulasToRootFormula(
179  TRestGeant4PrimaryGeneratorTypes::StringToEnergyAndAngularDistributionFormulas(
180  formula.Data()))
181  .Clone();
182  }
183 
184  inline void SetGenFilename(const TString& name) { fGenFilename = name; }
185 
186  inline void SetRandomMethod(double (*method)()) { fRandomMethod = method; }
187 
188  inline void AddParticle(const TRestGeant4Particle& particle) { fParticles.push_back(particle); }
189 
190  inline void RemoveParticles() { fParticles.clear(); }
191  inline void RemoveTemplates() { fParticlesTemplate.clear(); }
192  inline void FlushParticlesTemplate() {
193  fParticlesTemplate.push_back(fParticles);
194  fParticles.clear();
195  }
196 
197  virtual void PrintMetadata() override;
198 
199  // Constructor
201  // Destructor
202  virtual ~TRestGeant4ParticleSource();
203 
204  ClassDefOverride(TRestGeant4ParticleSource, 5);
205 };
206 #endif
void ReadEventDataFile(TString fName)
Reads an input file produced by Decay0.
virtual void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
virtual void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
bool ReadOldDecay0File(TString fileName)
Reads particle information using the file format from older Decay0 versions.
bool ReadNewDecay0File(TString fileName)
Reads particle information using the file format from newer Decay0 versions.
A class used to store particle properties.
A base class for any REST metadata class.
Definition: TRestMetadata.h:74