FairShip
Loading...
Searching...
No Matches
MuDISGenerator Class Reference

#include <MuDISGenerator.h>

Inheritance diagram for MuDISGenerator:
[legend]
Collaboration diagram for MuDISGenerator:
[legend]

Public Member Functions

 MuDISGenerator ()
 ~MuDISGenerator () override
Bool_t ReadEvent (FairPrimaryGenerator *) override
Bool_t Init (const char *, int) override
Bool_t Init (const char *) override
Int_t GetNevents ()
void SetPositions (Double_t z_start, Double_t z_end)
virtual Bool_t Init (const std::vector< std::string > &inFiles, int startNumber)
virtual Bool_t Init (const std::vector< std::string > &inFiles)
Public Member Functions inherited from SHiP::Generator
 Generator ()=default
 ~Generator () override
virtual void UseExternalFile (std::string x, Int_t i)
virtual void UseExternalFile (std::vector< std::string > &inFiles, Int_t i)
void SetMaxTheta (Double_t thetaX, Double_t thetaY)
Int_t nrOfGeoRejections () const
Long64_t GetCounter (const std::string &name) const
void SetCounter (const std::string &name, Long64_t value)
void IncrementCounter (const std::string &name, Long64_t delta=1)

Protected Attributes

Double_t startZ
Double_t endZ
TClonesArray * iMuon
TClonesArray * dPart
TClonesArray * dPartSoft
FairLogger * fLogger
TFile * fInputFile
 don't make it persistent, magic ROOT command
TTree * fTree
int fNevents
int fn
bool fFirst
Protected Attributes inherited from SHiP::Generator
std::optional< std::string > fextFile
Int_t firstEvent = 0
Double_t fMaxThetaX = 0
Double_t fMaxThetaY = 0
bool fUseVesselAcceptance = false
Int_t fnGeoRejects = 0
std::unordered_map< std::string, int64_t > fCounters

Additional Inherited Members

Protected Member Functions inherited from SHiP::Generator
bool IsInVesselAcceptance (Double_t px, Double_t py, Double_t pz) const

Detailed Description

Definition at line 19 of file MuDISGenerator.h.

Constructor & Destructor Documentation

◆ MuDISGenerator()

MuDISGenerator::MuDISGenerator ( )
default

default constructor

◆ ~MuDISGenerator()

MuDISGenerator::~MuDISGenerator ( )
override

destructor

Definition at line 91 of file MuDISGenerator.cxx.

91 {
92 if (fInputFile) {
93 fInputFile->Close();
94 fInputFile->Delete();
95 delete fInputFile;
96 }
97}
TFile * fInputFile
don't make it persistent, magic ROOT command

Member Function Documentation

◆ GetNevents()

Int_t MuDISGenerator::GetNevents ( )

Definition at line 275 of file MuDISGenerator.cxx.

275{ return fNevents; }

◆ Init() [1/4]

Bool_t MuDISGenerator::Init ( const char * fileName)
overridevirtual

Implements SHiP::Generator.

Definition at line 35 of file MuDISGenerator.cxx.

35{ return Init(fileName, 0); }
Bool_t Init(const char *, int) override

◆ Init() [2/4]

Bool_t MuDISGenerator::Init ( const char * fileName,
int startEvent )
overridevirtual

Implements SHiP::Generator.

Definition at line 36 of file MuDISGenerator.cxx.

36 {
37 LOGF(info, "Opening input file %s", fileName);
38 if (startEvent < 0) {
39 LOG(error) << "MuDISGenerator: startEvent must be >= 0, got " << startEvent;
40 return kFALSE;
41 }
42
43 iMuon = nullptr;
44 dPart = nullptr;
45 dPartSoft = nullptr;
46 fInputFile = TFile::Open(fileName, "READ");
47 if (!fInputFile) {
48 LOG(error) << "MuDISGenerator: error opening input file " << fileName;
49 delete fInputFile;
50 fInputFile = nullptr;
51 return kFALSE;
52 }
53 fTree = fInputFile->Get<TTree>("DIS");
54 if (!fTree) {
55 LOG(error) << "MuDISGenerator: cannot find tree DIS in file " << fileName;
56 fInputFile->Close();
57 delete fInputFile;
58 fInputFile = nullptr;
59 return kFALSE;
60 }
61 fNevents = fTree->GetEntries();
62 if (startEvent >= fNevents) {
63 LOG(error) << "MuDISGenerator: startEvent " << startEvent
64 << " is out of range for " << fNevents << " entries";
65 fInputFile->Close();
66 delete fInputFile;
67 fInputFile = nullptr;
68 fTree = nullptr;
69 return kFALSE;
70 }
71 fn = startEvent;
72
73 bool ok = true;
74 ok &= (fTree->SetBranchAddress("InMuon", &iMuon) >= 0);
75 ok &= (fTree->SetBranchAddress("DISParticles", &dPart) >= 0);
76 ok &= (fTree->SetBranchAddress("SoftParticles", &dPartSoft) >= 0);
77 if (!ok) {
78 LOG(error)
79 << "MuDISGenerator: failed to bind one or more required branches";
80 fInputFile->Close();
81 delete fInputFile;
82 fInputFile = nullptr;
83 fTree = nullptr;
84 return kFALSE;
85 }
86 LOG(info) << "MuDISGenerator: Initialization successful.";
87 return kTRUE;
88}
TClonesArray * dPartSoft
TClonesArray * iMuon
TClonesArray * dPart

◆ Init() [3/4]

virtual Bool_t SHiP::Generator::Init ( const std::vector< std::string > & inFiles)
inlinevirtual

public method ReadEvent

Reimplemented from SHiP::Generator.

Definition at line 33 of file Generator.h.

33 {
34 LOG(warning) << "Multiple input files not implemented for this generator "
35 "- only taking the first!";
36 return Init(inFiles.at(0).c_str(), 0);
37 };

◆ Init() [4/4]

virtual Bool_t SHiP::Generator::Init ( const std::vector< std::string > & inFiles,
int startNumber )
inlinevirtual

public method ReadEvent

Reimplemented from SHiP::Generator.

Definition at line 27 of file Generator.h.

28 {
29 LOG(warning) << "Multiple input files not implemented for this generator "
30 "- only taking the first!";
31 return Init(inFiles.at(0).c_str(), startNumber);
32 };

◆ ReadEvent()

Bool_t MuDISGenerator::ReadEvent ( FairPrimaryGenerator * cpg)
override

Definition at line 99 of file MuDISGenerator.cxx.

99 {
100 if (fn == fNevents) {
101 LOG(warning) << "End of input file. Rewind.";
102 }
103 fTree->GetEntry(fn % fNevents);
104 fn++;
105 if (fn % 10 == 0) {
106 LOG(info) << "Info MuDISGenerator: MuDIS event-nr " << fn;
107 }
108
109 int nf = dPart->GetEntries();
110 LOG(debug) << "*********************************************************";
111 LOG(debug) << "muon DIS Generator debug " << iMuon->GetEntries() << " "
112 << iMuon->AddrAt(0) << " nf " << nf << " fn=" << fn;
113
114 // some start/end positions in z (f.i. emulsion to Tracker 1)
115 Double_t start[3] = {0., 0., startZ};
116 Double_t end[3] = {0., 0., endZ};
117
118 // incoming muon
119 // array('d',[pid,px,py,pz,E,x,y,z,w,isProton,xsec,t,nDIS,nmuons]); entries
120 // 10-12 hold cross-section, time and nDIS (see below)
121 TVectorD* mu = dynamic_cast<TVectorD*>(iMuon->AddrAt(0));
122 LOG(debug) << "muon DIS Generator in muon " << int((*mu)[0]);
123 Double_t x = (*mu)[5] * 100.; // come in m -> cm
124 Double_t y = (*mu)[6] * 100.; // come in m -> cm
125 Double_t z = (*mu)[7] * 100.; // come in m -> cm
126 Double_t w =
127 (*mu)[8]; // weight of the original muon ( normalised to a spill)
128 Double_t cross_sec = (*mu)[10]; // in mbarns
129 Double_t t_muon = (*mu)[11]; // in ns
130 Double_t DIS_multiplicity = 1 / (*mu)[12]; // 1/nDIS
131
132 // calculate start/end positions along this muon, and amount of material in
133 // between
134
135 Double_t txmu = (*mu)[1] / (*mu)[3];
136 Double_t tymu = (*mu)[2] / (*mu)[3];
137 start[0] = x - (z - start[2]) * txmu;
138 start[1] = y - (z - start[2]) * tymu;
139 end[0] = x - (z - end[2]) * txmu;
140 end[1] = y - (z - end[2]) * tymu;
141 LOG(debug) << "MuDIS: mu xyz position " << x << ", " << y << ", " << z;
142 LOG(debug) << "MuDIS: mu pxyz position " << (*mu)[1] << ", " << (*mu)[2]
143 << ", " << (*mu)[3];
144 LOG(debug) << "MuDIS: mu weight*DISmultiplicity " << w;
145 LOG(debug) << "MuDIS: mu DIS cross section " << cross_sec;
146 LOG(debug) << "MuDIS: start position " << start[0] << ", " << start[1] << ", "
147 << start[2];
148 LOG(debug) << "MuDIS: end position " << end[0] << ", " << end[1] << ", "
149 << end[2];
150
151 Double_t bparam;
152 Double_t mparam[10];
153 bparam = shipgen::MeanMaterialBudget(start, end, mparam);
154 // loop over trajectory between start and end to pick an interaction point
155 Double_t prob2int = 0.;
156 Double_t xmu = 0.;
157 Double_t ymu = 0.;
158 Double_t zmu = 0.;
159 Int_t count = 0;
160 LOG(debug) << "Info MuDISGenerator Start prob2int while loop, bparam= "
161 << bparam << ", " << bparam * 1.e8;
162 LOG(debug) << "Info MuDISGenerator What was maximum density, mparam[7]= "
163 << mparam[7] << ", " << mparam[7] * 1.e8;
164
165 while (prob2int < gRandom->Uniform(0., 1.)) {
166 IncrementCounter("interaction_sampling_trials");
167 zmu = gRandom->Uniform(start[2], end[2]);
168 xmu = x - (z - zmu) * txmu;
169 ymu = y - (z - zmu) * tymu;
170 // get local material at this point
171 TGeoNode* node = gGeoManager->FindNode(xmu, ymu, zmu);
172 TGeoMaterial* mat = nullptr;
173 if (node && !gGeoManager->IsOutside()) {
174 mat = node->GetVolume()->GetMaterial();
175 LOG(debug) << "Info MuDISGenerator: mat " << count << ", "
176 << mat->GetName() << ", " << mat->GetDensity();
177 // density relative to Prob largest density along this trajectory, i.e.
178 // use rho(Pt)
179 prob2int = mat->GetDensity() / mparam[7];
180 if (prob2int > 1.) {
181 LOG(warning) << "MuDISGenerator: prob2int > Maximum density????";
182 LOG(warning) << "prob2int: " << prob2int;
183 LOG(warning) << "maxrho: " << mparam[7];
184 LOG(warning) << "material: " << mat->GetName();
185 }
186 } else {
187 // sampled point is outside the geometry: reject and resample
188 prob2int = 0.;
189 }
190 count += 1;
191 }
192
193 LOG(debug) << "Info MuDISGenerator: prob2int " << prob2int << ", " << count;
194 LOG(debug) << "MuDIS: put position " << xmu << ", " << ymu << ", " << zmu;
195
196 Double_t total_mom =
197 TMath::Sqrt(TMath::Power((*mu)[1], 2) + TMath::Power((*mu)[2], 2) +
198 TMath::Power((*mu)[3], 2)); // in GeV
199
200 Double_t distance =
201 TMath::Sqrt(TMath::Power(x - xmu, 2) + TMath::Power(y - ymu, 2) +
202 TMath::Power(z - zmu, 2)); // in cm
203
204 static const Double_t muon_mass =
205 TDatabasePDG::Instance()->GetParticle(13)->Mass();
206 Double_t v =
207 c_light * total_mom /
208 TMath::Sqrt(TMath::Power(total_mom, 2) + TMath::Power(muon_mass, 2));
209 Double_t t_rmu = distance / v;
210
211 // Adjust time based on the relative positions
212 if (zmu < z) {
213 t_rmu = -t_rmu;
214 }
215
216 Double_t t_DIS = (t_muon + t_rmu) /
217 ShipUnit::s; // time taken in seconds to reach [xmu,ymu,zmu]
218
219 cpg->AddTrack(static_cast<int>((*mu)[0]), // incoming muon track ()
220 (*mu)[1], (*mu)[2], (*mu)[3], xmu, ymu, zmu, -1,
221 false, // tracking disabled
222 (*mu)[4],
223 t_DIS, // shift time of the incoming muon track wrt t_muon from
224 // the input file.
225 w * DIS_multiplicity); // muon weight associated with a spill*
226 // DISmultiplicity
227
228 // outgoing DIS particles, [did,dpx,dpy,dpz,E], put density along trajectory
229 // as weight, g/cm^2
230
231 w = mparam[0] * mparam[4]; // modify weight, by multiplying with average
232 // density * track length
233 int index = 0;
234 for (auto&& particle : *dPart) {
235 TVectorD* Part = dynamic_cast<TVectorD*>(particle);
236 LOG(debug) << "muon DIS Generator out part " << int((*Part)[0]);
237 LOG(debug) << "muon DIS Generator out part mom " << (*Part)[1] << " "
238 << (*Part)[2] << " " << (*Part)[3] << " " << (*Part)[4];
239 LOG(debug) << "muon DIS Generator out part pos " << xmu << " " << ymu << ""
240 << zmu;
241 LOG(debug) << "muon DIS Generator out part w " << w;
242
243 if (index == 0) {
244 cpg->AddTrack(static_cast<int>((*Part)[0]), (*Part)[1], (*Part)[2],
245 (*Part)[3], xmu, ymu, zmu, 0, true, (*Part)[4], t_DIS,
246 cross_sec); // save DIS cross section in MCTrack[1]
247 } else {
248 cpg->AddTrack(static_cast<int>((*Part)[0]), (*Part)[1], (*Part)[2],
249 (*Part)[3], xmu, ymu, zmu, 0, true, (*Part)[4], t_DIS, w);
250 }
251 IncrementCounter("dis_particles_stored");
252 index += 1;
253 }
254
255 // Soft interaction tracks
256 for (auto&& softParticle : *dPartSoft) {
257 TVectorD* SoftPart = dynamic_cast<TVectorD*>(softParticle);
258 if ((*SoftPart)[7] > zmu) {
259 IncrementCounter("soft_particles_skipped");
260 continue;
261 } // Soft interactions after the DIS point are not saved
262 Double_t t_soft = (*SoftPart)[8] / ShipUnit::s; // Time in seconds
263 cpg->AddTrack(static_cast<int>((*SoftPart)[0]), (*SoftPart)[1],
264 (*SoftPart)[2], (*SoftPart)[3], (*SoftPart)[5],
265 (*SoftPart)[6], (*SoftPart)[7], 0, true, (*SoftPart)[4],
266 t_soft, w);
267 IncrementCounter("soft_particles_stored");
268 }
269
270 IncrementCounter("generated_events");
271
272 return kTRUE;
273}
const Int_t debug
constexpr double c_light
Definition ShipUnit.h:44
void IncrementCounter(const std::string &name, Long64_t delta=1)
Definition Generator.h:65
constexpr double s
Definition ShipUnit.h:31
double MeanMaterialBudget(std::span< const double, 3 > start, std::span< const double, 3 > end, std::span< double, 10 > mparam)

◆ SetPositions()

void MuDISGenerator::SetPositions ( Double_t z_start,
Double_t z_end )
inline

Definition at line 34 of file MuDISGenerator.h.

34 {
35 startZ = z_start;
36 endZ = z_end;
37 }

Member Data Documentation

◆ dPart

TClonesArray* MuDISGenerator::dPart
protected

Definition at line 42 of file MuDISGenerator.h.

◆ dPartSoft

TClonesArray* MuDISGenerator::dPartSoft
protected

Definition at line 43 of file MuDISGenerator.h.

◆ endZ

Double_t MuDISGenerator::endZ
protected

Definition at line 40 of file MuDISGenerator.h.

◆ fFirst

bool MuDISGenerator::fFirst
protected

Definition at line 49 of file MuDISGenerator.h.

◆ fInputFile

TFile* MuDISGenerator::fInputFile
protected

don't make it persistent, magic ROOT command

Definition at line 45 of file MuDISGenerator.h.

◆ fLogger

FairLogger* MuDISGenerator::fLogger
protected

Definition at line 44 of file MuDISGenerator.h.

◆ fn

int MuDISGenerator::fn
protected

Definition at line 48 of file MuDISGenerator.h.

◆ fNevents

int MuDISGenerator::fNevents
protected

Definition at line 47 of file MuDISGenerator.h.

◆ fTree

TTree* MuDISGenerator::fTree
protected

Definition at line 46 of file MuDISGenerator.h.

◆ iMuon

TClonesArray* MuDISGenerator::iMuon
protected

Definition at line 41 of file MuDISGenerator.h.

◆ startZ

Double_t MuDISGenerator::startZ
protected

Definition at line 40 of file MuDISGenerator.h.


The documentation for this class was generated from the following files: