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

#include <NewMuDISGenerator.h>

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

Public Member Functions

 NewMuDISGenerator ()
 ~NewMuDISGenerator () override
Bool_t ReadEvent (FairPrimaryGenerator *) override
Bool_t Init (const char *, int) override
Bool_t Init (const char *) override
Bool_t Init (const std::vector< std::string > &, int) override
Bool_t Init (const std::vector< std::string > &) override
Int_t GetNevents ()
void SetNevents (int nMuons=-1)
Bool_t RegisterOutputBranches (TTree *tree)
Int_t GetMaterial () const
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)

Static Public Member Functions

static std::vector< std::string > GetMaterialNames ()

Protected Member Functions

bool ValidateEntry (Long64_t entry) const
void ResetOutputBranches ()
Protected Member Functions inherited from SHiP::Generator
bool IsInVesselAcceptance (Double_t px, Double_t py, Double_t pz) const

Protected Attributes

FairLogger * fLogger
TChain * fTree
int fNevents
int fStartEvent = 0
int fMaxMuons = -1
Long64_t fEndEvent = 0
bool fEntryLoaded = false
ShipMuDIS::MuonInBranches finEv
int fn
Long64_t fnmu
unsigned fMat
Int_t fDISMaterial = -1
TString fMaterialLabel
Int_t fMuonEntry = -1
Int_t fDISIndex = -1
Double_t fDISXsec = -1.
Double_t fPythiaP
std::vector< Int_t > fNGenerated
int fnmuDis
std::size_t fnmuDisDau
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

Detailed Description

Definition at line 19 of file NewMuDISGenerator.h.

Constructor & Destructor Documentation

◆ NewMuDISGenerator()

NewMuDISGenerator::NewMuDISGenerator ( )

default constructor

Definition at line 26 of file NewMuDISGenerator.cxx.

26 : SHiP::Generator() {
27 fTree = nullptr;
28 fNevents = -1;
29 fn = 0;
30 fnmu = 0;
31 fMat = 0;
32 fnmuDis = 0;
33 fnmuDisDau = 0;
35}

◆ ~NewMuDISGenerator()

NewMuDISGenerator::~NewMuDISGenerator ( )
inlineoverride

destructor

Definition at line 25 of file NewMuDISGenerator.h.

25{ delete fTree; }

Member Function Documentation

◆ GetMaterial()

Int_t NewMuDISGenerator::GetMaterial ( ) const
inline

Definition at line 45 of file NewMuDISGenerator.h.

45{ return fDISMaterial; }

◆ GetMaterialNames()

std::vector< std::string > NewMuDISGenerator::GetMaterialNames ( )
static

Definition at line 37 of file NewMuDISGenerator.cxx.

37 {
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}
const std::array< TString, nMats > MatTypeStr
Definition MuDISDefs.h:45

◆ GetNevents()

Int_t NewMuDISGenerator::GetNevents ( )

Definition at line 300 of file NewMuDISGenerator.cxx.

300{ return fNevents; }

◆ Init() [1/4]

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

Implements SHiP::Generator.

Definition at line 46 of file NewMuDISGenerator.cxx.

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

◆ Init() [2/4]

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

Implements SHiP::Generator.

Definition at line 128 of file NewMuDISGenerator.cxx.

128 {
129 std::vector<std::string> fileNames = {fileName};
130 return Init(fileNames, startEvent);
131}

◆ Init() [3/4]

Bool_t NewMuDISGenerator::Init ( const std::vector< std::string > & fileNames)
overridevirtual

Reimplemented from SHiP::Generator.

Definition at line 50 of file NewMuDISGenerator.cxx.

50 {
51 return Init(fileNames, 0);
52}

◆ Init() [4/4]

Bool_t NewMuDISGenerator::Init ( const std::vector< std::string > & fileNames,
int startEvent )
overridevirtual

Reimplemented from SHiP::Generator.

Definition at line 54 of file NewMuDISGenerator.cxx.

55 {
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}
ShipMuDIS::MuonInBranches finEv
void SetNevents(int nMuons=-1)

◆ ReadEvent()

Bool_t NewMuDISGenerator::ReadEvent ( FairPrimaryGenerator * cpg)
override

Definition at line 134 of file NewMuDISGenerator.cxx.

134 {
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}
const Int_t debug
bool ValidateEntry(Long64_t entry) const
std::vector< Int_t > fNGenerated
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
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

◆ RegisterOutputBranches()

Bool_t NewMuDISGenerator::RegisterOutputBranches ( TTree * tree)

Definition at line 302 of file NewMuDISGenerator.cxx.

302 {
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}

◆ ResetOutputBranches()

void NewMuDISGenerator::ResetOutputBranches ( )
protected

Definition at line 322 of file NewMuDISGenerator.cxx.

322 {
323 fDISMaterial = -1;
324 fMaterialLabel = "";
325 fMuonEntry = -1;
326 fDISIndex = -1;
327 fDISXsec = -1.;
328 fPythiaP = -1.;
329 fNGenerated.assign(nMats, -1);
330}

◆ SetNevents()

void NewMuDISGenerator::SetNevents ( int nMuons = -1)

Definition at line 347 of file NewMuDISGenerator.cxx.

347 {
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}

◆ ValidateEntry()

bool NewMuDISGenerator::ValidateEntry ( Long64_t entry) const
protected

Definition at line 332 of file NewMuDISGenerator.cxx.

332 {
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}

Member Data Documentation

◆ fDISIndex

Int_t NewMuDISGenerator::fDISIndex = -1
protected

Definition at line 64 of file NewMuDISGenerator.h.

◆ fDISMaterial

Int_t NewMuDISGenerator::fDISMaterial = -1
protected

Definition at line 61 of file NewMuDISGenerator.h.

◆ fDISXsec

Double_t NewMuDISGenerator::fDISXsec = -1.
protected

Definition at line 65 of file NewMuDISGenerator.h.

◆ fEndEvent

Long64_t NewMuDISGenerator::fEndEvent = 0
protected

Definition at line 53 of file NewMuDISGenerator.h.

◆ fEntryLoaded

bool NewMuDISGenerator::fEntryLoaded = false
protected

Definition at line 54 of file NewMuDISGenerator.h.

◆ finEv

ShipMuDIS::MuonInBranches NewMuDISGenerator::finEv
protected

Definition at line 57 of file NewMuDISGenerator.h.

◆ fLogger

FairLogger* NewMuDISGenerator::fLogger
protected

Definition at line 48 of file NewMuDISGenerator.h.

◆ fMat

unsigned NewMuDISGenerator::fMat
protected

Definition at line 60 of file NewMuDISGenerator.h.

◆ fMaterialLabel

TString NewMuDISGenerator::fMaterialLabel
protected

Definition at line 62 of file NewMuDISGenerator.h.

◆ fMaxMuons

int NewMuDISGenerator::fMaxMuons = -1
protected

Definition at line 52 of file NewMuDISGenerator.h.

◆ fMuonEntry

Int_t NewMuDISGenerator::fMuonEntry = -1
protected

Definition at line 63 of file NewMuDISGenerator.h.

◆ fn

int NewMuDISGenerator::fn
protected

Definition at line 58 of file NewMuDISGenerator.h.

◆ fNevents

int NewMuDISGenerator::fNevents
protected

Definition at line 50 of file NewMuDISGenerator.h.

◆ fNGenerated

std::vector<Int_t> NewMuDISGenerator::fNGenerated
protected

Definition at line 68 of file NewMuDISGenerator.h.

◆ fnmu

Long64_t NewMuDISGenerator::fnmu
protected

Definition at line 59 of file NewMuDISGenerator.h.

◆ fnmuDis

int NewMuDISGenerator::fnmuDis
protected

Definition at line 69 of file NewMuDISGenerator.h.

◆ fnmuDisDau

std::size_t NewMuDISGenerator::fnmuDisDau
protected

Definition at line 70 of file NewMuDISGenerator.h.

◆ fPythiaP

Double_t NewMuDISGenerator::fPythiaP
protected
Initial value:
=
-1.

Definition at line 66 of file NewMuDISGenerator.h.

◆ fStartEvent

int NewMuDISGenerator::fStartEvent = 0
protected

Definition at line 51 of file NewMuDISGenerator.h.

◆ fTree

TChain* NewMuDISGenerator::fTree
protected

Definition at line 49 of file NewMuDISGenerator.h.


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