FairShip
Loading...
Searching...
No Matches
TTreeGenerator.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 "TTreeGenerator.h"
6
7#include "FairLogger.h"
8
9namespace SHiP {
10
12 : Generator(),
13 fInputFile(nullptr),
14 fInputTree(nullptr),
15 fTreeName("events"),
16 fNEvents(0),
18 fPx(0),
19 fPy(0),
20 fPz(0),
21 fX(0),
22 fY(0),
23 fZ(0),
24 fPdgId(0),
25 fWeight(1.0) {}
26
28 if (fInputFile) {
29 fInputFile->Close();
30 delete fInputFile;
31 }
32}
33
34Bool_t TTreeGenerator::Init(const char* fileName) { return Init(fileName, 0); }
35
36Bool_t TTreeGenerator::Init(const char* fileName, int startEvent) {
37 if (startEvent < 0) {
38 LOG(error) << "TTreeGenerator: startEvent must be >= 0, got " << startEvent;
39 return kFALSE;
40 }
41
42 if (fInputFile) {
43 fInputFile->Close();
44 delete fInputFile;
45 fInputFile = nullptr;
46 }
47 fInputTree = nullptr;
48 fNEvents = 0;
49 fCurrentEvent = 0;
50
51 fInputFile = TFile::Open(fileName, "READ");
52 if (!fInputFile) {
53 LOG(error) << "TTreeGenerator: Cannot open file " << fileName;
54 delete fInputFile;
55 fInputFile = nullptr;
56 return kFALSE;
57 }
58
59 fInputTree = dynamic_cast<TTree*>(fInputFile->Get(fTreeName.Data()));
60 if (!fInputTree) {
61 LOG(error) << "TTreeGenerator: Cannot find tree " << fTreeName.Data()
62 << " in file " << fileName;
63 fInputFile->Close();
64 delete fInputFile;
65 fInputFile = nullptr;
66 fInputTree = nullptr;
67 return kFALSE;
68 }
69
70 fNEvents = fInputTree->GetEntries();
71 if (startEvent >= fNEvents) {
72 LOG(error) << "TTreeGenerator: startEvent " << startEvent
73 << " is out of range for " << fNEvents << " entries";
74 fInputFile->Close();
75 delete fInputFile;
76 fInputFile = nullptr;
77 fInputTree = nullptr;
78 fNEvents = 0;
79 return kFALSE;
80 }
81 fCurrentEvent = startEvent;
82 LOG(info) << "TTreeGenerator: Found " << fNEvents
83 << " events in tree, starting at " << startEvent;
84
85 if (!InitBranches()) {
86 fInputFile->Close();
87 delete fInputFile;
88 fInputFile = nullptr;
89 fInputTree = nullptr;
90 fNEvents = 0;
91 fCurrentEvent = 0;
92 return kFALSE;
93 }
94
95 return kTRUE;
96}
97
98Bool_t TTreeGenerator::ReadEvent(FairPrimaryGenerator* primGen) {
100 return kFALSE;
101 }
102 if (fInputTree->GetEntry(fCurrentEvent) <= 0) {
103 LOG(error) << "TTreeGenerator: Failed to read entry " << fCurrentEvent;
104 return kFALSE;
105 }
106
107 // Position in cm, momentum in GeV/c. e < 0 makes FairPrimaryGenerator
108 // compute the energy from the momentum and PDG mass.
109 primGen->AddTrack(fPdgId, fPx, fPy, fPz, fX, fY, fZ, -1, true, -9e9, 0.,
110 fWeight);
111
113
114 return kTRUE;
115}
116
118 if (!fInputTree) return kFALSE;
119
120 bool ok = true;
121
122 ok &= (fInputTree->SetBranchAddress("px", &fPx) >= 0);
123 ok &= (fInputTree->SetBranchAddress("py", &fPy) >= 0);
124 ok &= (fInputTree->SetBranchAddress("pz", &fPz) >= 0);
125
126 ok &= (fInputTree->SetBranchAddress("x", &fX) >= 0);
127 ok &= (fInputTree->SetBranchAddress("y", &fY) >= 0);
128 ok &= (fInputTree->SetBranchAddress("z", &fZ) >= 0);
129
130 ok &= (fInputTree->SetBranchAddress("id", &fPdgId) >= 0);
131 ok &= (fInputTree->SetBranchAddress("w", &fWeight) >= 0);
132
133 if (!ok) {
134 LOG(error) << "TTreeGenerator: failed to bind one or more branches";
135 }
136
137 return ok ? kTRUE : kFALSE;
138}
139
140} // namespace SHiP
Generator()=default
TTree * fInputTree
Input TTree pointer.
TFile * fInputFile
Input ROOT file pointer.
Double_t fPz
Particle momentum z-component (GeV/c).
Long64_t fCurrentEvent
Current event index (0-based).
Long64_t fNEvents
Total number of events in the tree.
Bool_t ReadEvent(FairPrimaryGenerator *primGen) override
Int_t fPdgId
PDG particle identification code.
Double_t fPy
Particle momentum y-component (GeV/c).
Bool_t Init(const char *fileName, int startEvent) override
Double_t fZ
Particle position z-coordinate (cm).
TString fTreeName
Name of the TTree to read.
Double_t fPx
Particle momentum x-component (GeV/c).
Double_t fX
Particle position x-coordinate (cm).
Double_t fWeight
Event weight factor.
Double_t fY
Particle position y-coordinate (cm).