FairShip
Loading...
Searching...
No Matches
NewMuDISGenerator.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 "NewMuDISGenerator.h"
6
7#include <algorithm>
8#include <limits>
9
10#include "FairLogger.h"
11#include "FairPrimaryGenerator.h"
12#include "TFile.h"
13#include "TGeoCompositeShape.h"
14#include "TGeoEltu.h"
15#include "TGeoManager.h"
16#include "TGeoNode.h"
17#include "TGeoVolume.h"
18#include "TMath.h"
19#include "TROOT.h"
20#include "TRandom.h"
21#include "TSystem.h"
22#include "TVectorD.h"
23
24using namespace ShipMuDIS;
25
27 fTree = nullptr;
28 fNevents = -1;
29 fn = 0;
30 fnmu = 0;
31 fMat = 0;
32 fnmuDis = 0;
33 fnmuDisDau = 0;
35}
36
37std::vector<std::string> NewMuDISGenerator::GetMaterialNames() {
38 std::vector<std::string> names;
39 names.reserve(nMats);
40 for (const auto& label : MatTypeStr) {
41 names.emplace_back(label.Data());
42 }
43 return names;
44}
45
46Bool_t NewMuDISGenerator::Init(const char* fileName) {
47 return Init(fileName, 0);
48}
49
50Bool_t NewMuDISGenerator::Init(const std::vector<std::string>& fileNames) {
51 return Init(fileNames, 0);
52}
53
54Bool_t NewMuDISGenerator::Init(const std::vector<std::string>& fileNames,
55 const int startEvent) {
57 fNevents = -1;
58 fEntryLoaded = false;
59 if (startEvent < 0) {
60 LOG(error) << "NewMuDISGenerator: startEvent must be nonnegative";
61 return kFALSE;
62 }
63 if (fileNames.empty()) {
64 LOG(error) << "NewMuDISGenerator: no input files provided. "
65 << "Check the -f/--inputFile argument or input file glob.";
66 return kFALSE;
67 }
68 for (const auto& fileName : fileNames) {
69 if (fileName.empty()) {
70 LOG(error) << "NewMuDISGenerator: received an empty input file name. "
71 << "Check the -f/--inputFile argument.";
72 return kFALSE;
73 }
74 }
75
76 LOG(info) << "Opening input file to find keys " << fileNames.at(0);
77 TFile* testFile = TFile::Open(fileNames.at(0).c_str(), "READ");
78 auto testKeys = testFile ? testFile->GetListOfKeys() : nullptr;
79 if (testKeys == nullptr) {
80 delete testFile;
81 LOG(error) << "NewMuDISGenerator: Error opening input file "
82 << fileNames.at(0)
83 << ". Check that the path is correct and the file is a readable "
84 "ROOT file.";
85 return kFALSE;
86 }
87 const bool hasDIStree = testKeys->FindObject("MuonDIS") != nullptr;
88 testFile->Close();
89 delete testFile;
90
91 if (hasDIStree) {
92 delete fTree;
93 fTree = new TChain("MuonDIS");
94 for (auto& f : fileNames) {
95 LOG(info) << "Opening input file " << f;
96 fTree->Add(f.c_str());
97 }
98 const Long64_t treeEvts = fTree->GetEntries();
99 LOG(info) << "Reading " << treeEvts << " entries.";
100 if (startEvent > treeEvts) {
101 LOG(error) << "NewMuDISGenerator: startEvent " << startEvent
102 << " exceeds the number of input muon entries " << treeEvts;
103 return kFALSE;
104 }
105 fStartEvent = startEvent;
106 fn = 0;
107 fnmu = startEvent;
108 fMat = 0;
109 fnmuDis = 0;
110 fnmuDisDau = 0;
111
112 bool ok = finEv.Setup(fTree);
113
114 if (!ok) {
115 LOG(error)
116 << "NewMuDISGenerator: failed to bind one or more required branches";
117 return kFALSE;
118 }
120 if (fNevents < 0) return kFALSE;
121 LOG(info) << "NewMuDISGenerator: Initialization successful.";
122 return kTRUE;
123 }
124 return kFALSE;
125}
126
127// ----- Default constructor -------------------------------------------
128Bool_t NewMuDISGenerator::Init(const char* fileName, const int startEvent) {
129 std::vector<std::string> fileNames = {fileName};
130 return Init(fileNames, startEvent);
131}
132
133// ----- Passing the event ---------------------------------------------
134Bool_t NewMuDISGenerator::ReadEvent(FairPrimaryGenerator* cpg) {
136 if (fn >= fNevents) {
137 LOG(info) << " Reached total number of DIS events: counter " << fn
138 << " nTot=" << fNevents;
139 return kFALSE;
140 }
141 LOG(debug) << " - Processing input muon " << fnmu << " fMat " << fMat
142 << " fnmuDis " << fnmuDis << " fnmuDisDau " << fnmuDisDau;
143
144 if (!fEntryLoaded) {
145 if (!finEv.PrepareEntry(fTree, fnmu) || fTree->GetEntry(fnmu) <= 0) {
146 LOG(error) << " Error reading event " << fnmu;
147 fNevents = -1;
148 return kFALSE;
149 } else {
150 LOG(debug) << " Updated tree entry: " << fnmu;
151 }
152 if (!ValidateEntry(fnmu)) {
153 fNevents = -1;
154 return kFALSE;
155 }
156 fEntryLoaded = true;
157 }
158
159 // access the different materials in turn
160 // accessing by reference leads to sometimes vectors inside branch being reset
161 MuonDISInBranches* lBr = &finEv.br[fMat];
162 LOG(debug) << "Initial branch: " << lBr->Print(fn, MatTypeStr[fMat]).str();
163 int nDIS = lBr->nDISevts;
164 LOG(debug) << " nDIS " << nDIS << " DISparticles size "
165 << (*lBr->DISparticles).size() << " fMat " << fMat
166 << " local evtNumber " << fn;
167
168 // Counts describe the interactions actually stored, including after
169 // filtering. Advance only before producing an event: a successful final event
170 // must not fail just because there is no following input muon.
171 while (fnmuDis >= nDIS) {
172 fMat++;
173 fnmuDis = 0;
174 fnmuDisDau = 0;
175 LOG(debug) << " -- switching material" << fMat;
176 if (fMat >= nMats) {
177 fMat = 0;
178 fnmu++;
179 LOG(debug) << " -- switching input muon " << fnmu;
180 LOG(debug) << " - Processing input muon " << fnmu << " fMat " << fMat
181 << " fnmuDis " << fnmuDis << " fnmuDisDau " << fnmuDisDau;
182 if (fnmu >= fEndEvent || !finEv.PrepareEntry(fTree, fnmu) ||
183 fTree->GetEntry(fnmu) <= 0) {
184 LOG(error) << " Error reading event " << fnmu;
185 fNevents = -1;
186 return kFALSE;
187 } else {
188 LOG(debug) << " Updated tree entry: " << fnmu;
189 }
190 if (!ValidateEntry(fnmu)) {
191 fNevents = -1;
192 return kFALSE;
193 }
194 }
195 lBr = &finEv.br[fMat];
196 LOG(debug) << "Updating branch: " << lBr->Print(fn, MatTypeStr[fMat]).str();
197 nDIS = lBr->nDISevts;
198 }
199
200 if (fnmu % 10 == 0 && fnmuDis == 0) {
201 LOG(info) << "Info NewMuDISGenerator: NewMuDIS original muon event #"
202 << fnmu << " material " << fMat << " final event #" << fn;
203 }
204
205 if (nDIS > 0) { // if fMat branch has elements
206 if (fnmu > std::numeric_limits<Int_t>::max()) {
207 LOG(error) << "NewMuDISGenerator: input muon entry exceeds Int_t range";
208 fNevents = -1;
209 return kFALSE;
210 }
211 if (fnmuDis < 0 || !lBr->nDISdau || !lBr->DISparticles || !lBr->DISvx ||
212 !lBr->DISvy || !lBr->DISvz || !lBr->DISvt ||
213 static_cast<std::size_t>(fnmuDis) >= lBr->nDISdau->size() ||
214 static_cast<std::size_t>(fnmuDis) >= lBr->DISvx->size() ||
215 static_cast<std::size_t>(fnmuDis) >= lBr->DISvy->size() ||
216 static_cast<std::size_t>(fnmuDis) >= lBr->DISvz->size() ||
217 static_cast<std::size_t>(fnmuDis) >= lBr->DISvt->size()) {
218 LOG(error) << "NewMuDISGenerator: invalid DIS index " << fnmuDis
219 << " in input muon " << fnmu << " material " << fMat;
220 fNevents = -1;
221 return kFALSE;
222 }
223 const int nDaughters = (*lBr->nDISdau)[fnmuDis];
224 const auto nDISparts = lBr->DISparticles->size();
225 if (nDaughters < 0 || fnmuDisDau > nDISparts ||
226 static_cast<std::size_t>(nDaughters) > nDISparts - fnmuDisDau) {
227 LOG(error) << "NewMuDISGenerator: invalid daughter range at offset "
228 << fnmuDisDau << " with " << nDaughters << " daughters in "
229 << nDISparts << " DIS particles for input muon " << fnmu
230 << " material " << fMat << " DIS index " << fnmuDis;
231 fNevents = -1;
232 return kFALSE;
233 }
234 // add also soft tracks up to z_interaction
235 bool first = true;
236 int idxMum = -1;
237 for (auto&& mcTrk : *(finEv.mcTrks)) {
238 ShipMCTrack& softP = static_cast<ShipMCTrack&>(mcTrk);
239 // first particle is the original muon
240 if (first) {
241 cpg->AddTrack(softP.GetPdgCode(), softP.GetPx(), softP.GetPy(),
242 softP.GetPz(), softP.GetStartX(), softP.GetStartY(),
243 softP.GetStartZ(), idxMum, false, softP.GetEnergy(),
244 softP.GetStartT() / 1E9, softP.GetWeight());
245 // passed the first track, we want to track the soft particles and they
246 // all stem from first one.
247 first = false;
248 idxMum = 0;
249 } else if (softP.GetStartZ() <= (*lBr->DISvz)[fnmuDis]) {
250 bool wantTracking = softP.GetStartZ() < 12000;
251 cpg->AddTrack(softP.GetPdgCode(), softP.GetPx(), softP.GetPy(),
252 softP.GetPz(), softP.GetStartX(), softP.GetStartY(),
253 softP.GetStartZ(), idxMum, wantTracking,
254 softP.GetEnergy(), softP.GetStartT() / 1E9,
255 softP.GetWeight());
256 }
257 }
258 LOG(debug) << " --- Processing dis muon " << fnmuDis << " with "
259 << nDaughters << " daughters and " << nDISparts
260 << " total DIS particles";
261 LOG(debug) << " ---- index dau start " << fnmuDisDau;
262 // access the independent DIS events
263 for (int iD(0); iD < nDaughters; ++iD) {
264 DISparticle& lDau = (*lBr->DISparticles)[fnmuDisDau + iD];
265 cpg->AddTrack(lDau.pid, lDau.px, lDau.py, lDau.pz, (*lBr->DISvx)[fnmuDis],
266 (*lBr->DISvy)[fnmuDis], (*lBr->DISvz)[fnmuDis], 0, true,
267 lDau.E, (*lBr->DISvt)[fnmuDis] / 1E9, lBr->wDIS);
268 }
269 fDISMaterial = static_cast<Int_t>(fMat);
271 fMuonEntry = static_cast<Int_t>(fnmu);
273 // DISxsec is not reliable yet; retain the -1 placeholder.
274 fPythiaP = lBr->pPythia;
275 for (unsigned material = 0; material < nMats; ++material) {
276 fNGenerated[material] = finEv.br[material].nDISevtsGenerated;
277 }
278 fnmuDisDau += nDaughters;
279 LOG(debug) << " ---- index dau end " << fnmuDisDau;
280 fnmuDis++;
281 LOG(debug) << " --- increment DIS event " << fnmuDis;
282 fn++;
283 } // if fMat branch has elements
284 else {
285 LOG(error) << " Failed to process input muon " << fnmu << " fMat " << fMat
286 << " fnmuDis " << fnmuDis << " fnmuDisDau " << fnmuDisDau
287 << " local event " << fn;
288 return kFALSE;
289 }
290
291 if (fn == fNevents) {
292 LOG(info) << "-- Reached total number of DIS events: counter " << fn
293 << " nTot=" << fNevents;
294 }
295
296 return kTRUE;
297}
298
299// -------------------------------------------------------------------------
301
303 if (!tree || tree->GetEntries() != 0) {
304 LOG(error) << "NewMuDISGenerator: output branches require an empty tree";
305 return kFALSE;
306 }
307 for (const auto* name : {"muDIS_material", "muDIS_muEntry", "muDIS_disIndex",
308 "muDIS_xsec", "muDIS_pPythia", "muDIS_nGenerated"}) {
309 if (tree->GetBranch(name)) {
310 LOG(error) << "NewMuDISGenerator: output branch already exists: " << name;
311 return kFALSE;
312 }
313 }
314 return tree->Branch("muDIS_material", &fMaterialLabel) &&
315 tree->Branch("muDIS_muEntry", &fMuonEntry, "muDIS_muEntry/I") &&
316 tree->Branch("muDIS_disIndex", &fDISIndex, "muDIS_disIndex/I") &&
317 tree->Branch("muDIS_xsec", &fDISXsec, "muDIS_xsec/D") &&
318 tree->Branch("muDIS_pPythia", &fPythiaP, "muDIS_pPythia/D") &&
319 tree->Branch("muDIS_nGenerated", &fNGenerated);
320}
321
323 fDISMaterial = -1;
324 fMaterialLabel = "";
325 fMuonEntry = -1;
326 fDISIndex = -1;
327 fDISXsec = -1.;
328 fPythiaP = -1.;
329 fNGenerated.assign(nMats, -1);
330}
331
332bool NewMuDISGenerator::ValidateEntry(Long64_t entry) const {
333 if (!finEv.mcTrks || finEv.mcTrks->empty()) {
334 LOG(error) << "NewMuDISGenerator: missing muon tracks in entry " << entry;
335 return false;
336 }
337 for (unsigned material = 0; material < nMats; ++material) {
338 if (!finEv.br[material].IsValid()) {
339 LOG(error) << "NewMuDISGenerator: inconsistent DIS vectors in entry "
340 << entry << " material " << MatTypeStr[material];
341 return false;
342 }
343 }
344 return true;
345}
346
348 fMaxMuons = nMuons;
349 fNevents = -1;
350 // Counting reads the tree; reload the current muon before replaying it.
351 fEntryLoaded = false;
352 if (nMuons < -1) {
353 LOG(error)
354 << "NewMuDISGenerator: input muon limit must be -1 or nonnegative";
355 return;
356 }
357 if (!fTree) return;
358 fEndEvent = nMuons < 0 ? fTree->GetEntries()
359 : std::min(Long64_t(fStartEvent) + nMuons,
360 fTree->GetEntries());
361 int total = 0;
362 for (Long64_t iEv = fStartEvent; iEv < fEndEvent; ++iEv) {
363 if (!finEv.PrepareEntry(fTree, iEv) || fTree->GetEntry(iEv) <= 0 ||
364 !ValidateEntry(iEv)) {
365 LOG(error) << "NewMuDISGenerator: error counting input muon " << iEv;
366 return;
367 }
368 for (const auto& br : finEv.br) {
369 if (br.nDISevts > std::numeric_limits<int>::max() - total) {
370 LOG(error) << "NewMuDISGenerator: DIS event count exceeds Int_t range";
371 return;
372 }
373 total += br.nDISevts;
374 }
375 }
376 fNevents = total;
377}
const Int_t debug
bool ValidateEntry(Long64_t entry) const
Bool_t Init(const char *, int) override
Bool_t RegisterOutputBranches(TTree *tree)
static std::vector< std::string > GetMaterialNames()
std::vector< Int_t > fNGenerated
ShipMuDIS::MuonInBranches finEv
Bool_t ReadEvent(FairPrimaryGenerator *) override
void SetNevents(int nMuons=-1)
Generator()=default
Double_t GetWeight() const
Double_t GetStartZ() const
Definition ShipMCTrack.h:67
Double_t GetStartX() const
Definition ShipMCTrack.h:65
Double_t GetStartY() const
Definition ShipMCTrack.h:66
Double_t GetStartT() const
Definition ShipMCTrack.h:68
Double_t GetPy() const
Definition ShipMCTrack.h:63
Double_t GetEnergy() const
Double_t GetPx() const
Definition ShipMCTrack.h:62
Int_t GetPdgCode() const
Definition ShipMCTrack.h:60
Double_t GetPz() const
Definition ShipMCTrack.h:64
const std::array< TString, nMats > MatTypeStr
Definition MuDISDefs.h:45
std::vector< DISparticle > * DISparticles
Definition MuDISDefs.h:225
std::vector< double > * DISvt
Definition MuDISDefs.h:223
std::vector< double > * DISvx
Definition MuDISDefs.h:220
std::vector< double > * DISvy
Definition MuDISDefs.h:221
std::vector< double > * DISvz
Definition MuDISDefs.h:222
std::string Print(const std::vector< T > &aVec, const TString &aName)
Definition MuDISDefs.h:282
std::vector< int > * nDISdau
Definition MuDISDefs.h:224