FairShip
Loading...
Searching...
No Matches
MuDISGenerator.cxx
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
5#include "MuDISGenerator.h"
6
7#include <cmath>
8
9#include "FairLogger.h"
10#include "FairPrimaryGenerator.h"
11#include "MeanMaterialBudget.h"
12#include "ShipUnit.h"
13#include "TDatabasePDG.h"
14#include "TFile.h"
15#include "TGeoCompositeShape.h"
16#include "TGeoEltu.h"
17#include "TGeoManager.h"
18#include "TGeoNode.h"
19#include "TGeoVolume.h"
20#include "TMath.h"
21#include "TROOT.h"
22#include "TRandom.h"
23#include "TSystem.h"
24#include "TVectorD.h"
25
26// MuDIS momentum GeV
27// Vertex in SI units, assume this means m
28
29using ShipUnit::c_light; // cm/ns
30
31// ----- Default constructor -------------------------------------------
33// -------------------------------------------------------------------------
34// ----- Default constructor -------------------------------------------
35Bool_t MuDISGenerator::Init(const char* fileName) { return Init(fileName, 0); }
36Bool_t MuDISGenerator::Init(const char* fileName, const int startEvent) {
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}
89
90// ----- Destructor ----------------------------------------------------
92 if (fInputFile) {
93 fInputFile->Close();
94 fInputFile->Delete();
95 delete fInputFile;
96 }
97}
98// ----- Passing the event ---------------------------------------------
99Bool_t MuDISGenerator::ReadEvent(FairPrimaryGenerator* cpg) {
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}
274// -------------------------------------------------------------------------
const Int_t debug
constexpr double c_light
Definition ShipUnit.h:44
~MuDISGenerator() override
Bool_t Init(const char *, int) override
Bool_t ReadEvent(FairPrimaryGenerator *) override
TClonesArray * dPartSoft
TFile * fInputFile
don't make it persistent, magic ROOT command
TClonesArray * iMuon
TClonesArray * dPart
void IncrementCounter(const std::string &name, Long64_t delta=1)
Definition Generator.h:65
constexpr double c_light
Definition ShipUnit.h:44
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)