FlatG4Tree.cxx
Go to the documentation of this file.
1 /*
2  * FlatG4Tree.cxx
3  *
4  * Created on: Feb 9, 2021
5  * Author: chilgenb
6  */
7 
9 
10 using std::vector;
11 using namespace garana;
12 
13 // assumes read-only mode
15 {
16  SetupRead(tree);
17 
18 }
19 
21 {
22  CheckOpt(opt);//sets TreeReader::fOpt
23 
24  if(fOpt=='r'){
25  std::cout << "constructed FlatG4Tree object in read-only mode" << std::endl;
26  SetupRead(tree);
27  }
28  else {
29  std::cout << "constructed FlatG4Tree object in write mode" << std::endl;
30  fTreeIn = tree;
32  SetVecs();
33  }
34 
35 }
36 
37 
38 void FlatG4Tree::GetEntry(const UInt_t& ientry) {
39 
40  FillIndexMap();
41  if(ientry != fCurrentEntry) {
42  fCurrentEntry = ientry;
43  fTreeIn->GetEntry(ientry);
44  }
45 }
46 
48 
49 std::cout << "FlatG4Tree SetBranchAddresses()" << std::endl;
50 
51  if(fOpt=='r'){
52  fTreeIn->SetBranchAddress("Event", &fEvent, &b_Event );
53  fTreeIn->SetBranchAddress("TruthIndex", &fG4TruthIndex, &b_G4TruthIndex );
54  //fTreeIn->SetBranchAddress("FSIndex", &fG4FSIndex, &b_G4FSIndex);
55  fTreeIn->SetBranchAddress("NSim", &fNSim, &b_NSim );
56  fTreeIn->SetBranchAddress("NPts", &fNPts, &b_NPts );
57  fTreeIn->SetBranchAddress("NRegions", &fNRegions, &b_NRegions );
58  fTreeIn->SetBranchAddress("Region", &fRegions, &b_Regions );
59  fTreeIn->SetBranchAddress("TrkID", &fTrkID, &b_TrkID );
60  fTreeIn->SetBranchAddress("Pdg", &fPDG, &b_PDG );
61  fTreeIn->SetBranchAddress("ParentPdg", &fParentPdg, &b_ParentPdg );
62  fTreeIn->SetBranchAddress("ProgenitorPdg", &fProgenitorPdg, &b_ProgenitorPdg );
63  fTreeIn->SetBranchAddress("ParentTrackId", &fParentTrackId, &b_ParentTrackId );
64  fTreeIn->SetBranchAddress("ProgenitorTrackId", &fProgenitorTrackId, &b_ProgenitorTrackId);
65  fTreeIn->SetBranchAddress("ProcessI", &fProcessI, &b_ProcessI );
66  fTreeIn->SetBranchAddress("ProcessF", &fProcessF, &b_ProcessF );
67  fTreeIn->SetBranchAddress("X", &fX, &b_X );
68  fTreeIn->SetBranchAddress("Y", &fY, &b_Y );
69  fTreeIn->SetBranchAddress("Z", &fZ, &b_Z );
70  fTreeIn->SetBranchAddress("T", &fT, &b_T );
71  fTreeIn->SetBranchAddress("Px", &fPx, &b_Px );
72  fTreeIn->SetBranchAddress("Py", &fPy, &b_Py );
73  fTreeIn->SetBranchAddress("Pz", &fPz, &b_Pz );
74  fTreeIn->SetBranchAddress("E", &fE, &b_E );
75 
76  }
77 
78 
79  if(fOpt=='w'){
80 
81  fTreeIn->Branch("Event", &fEvent, "Event/I");
82  fTreeIn->Branch("NSim", &fNSim, "NSim/I" );
83  fTreeIn->Branch("NPts", &fNPts );
84  fTreeIn->Branch("NRegions", &fNRegions );
85  fTreeIn->Branch("Region", &fRegions );
86  fTreeIn->Branch("TrkID", &fTrkID );
87  fTreeIn->Branch("Pdg", &fPDG );
88  fTreeIn->Branch("ParentPdg", &fParentPdg );
89  fTreeIn->Branch("ProgenitorPdg", &fProgenitorPdg );
90  fTreeIn->Branch("ParentTrackId", &fParentTrackId );
91  fTreeIn->Branch("ProgenitorTrackId", &fProgenitorTrackId );
92  fTreeIn->Branch("ProcessI", &fProcessI );
93  fTreeIn->Branch("ProcessF", &fProcessF );
94  fTreeIn->Branch("X", &fX );
95  fTreeIn->Branch("Y", &fY );
96  fTreeIn->Branch("Z", &fZ );
97  fTreeIn->Branch("T", &fT );
98  fTreeIn->Branch("Px", &fPx );
99  fTreeIn->Branch("Py", &fPy );
100  fTreeIn->Branch("Pz", &fPz );
101  fTreeIn->Branch("E", &fE );
102  }
103 
104  return true;
105 }
106 
107 //implement accessors inherited from G4Tree
109 
110  fNPts = new vector<UInt_t>();
111  fTrkID = new vector<Int_t>();
112  fPDG = new vector<Int_t>();
113  fParentPdg = new vector<Int_t>();
114  fProgenitorPdg = new vector<Int_t>();
115  fParentTrackId = new vector<Int_t>();
116  fProgenitorTrackId = new vector<Int_t>();
117  fProcessI = new vector<Int_t>();
118  fProcessF = new vector<Int_t>();
119  fX = new vector<Float_t>();
120  fY = new vector<Float_t>();
121  fZ = new vector<Float_t>();
122  fT = new vector<Float_t>();
123  fPx = new vector<Float_t>();
124  fPy = new vector<Float_t>();
125  fPz = new vector<Float_t>();
126  fE = new vector<Float_t>();
127 
128  }
129 
131 
132  fNPts->clear();
133  fTrkID->clear();
134  fPDG->clear();
135  fParentPdg->clear();
136  fProgenitorPdg->clear();
137  fParentTrackId->clear();
138  fProgenitorTrackId->clear();
139  fProcessI->clear();
140  fProcessF->clear();
141  fX->clear();
142  fY->clear();
143  fZ->clear();
144  fT->clear();
145  fPx->clear();
146  fPy->clear();
147  fPz->clear();
148  fE->clear();
149 
150  }
151 
152  const UInt_t FlatG4Tree::NSim() const {
153  return fNSim;
154  }//
155 
156  const UInt_t FlatG4Tree::NPoints(const UInt_t& iparticle) const {
157  return fNPts->at(iparticle);
158  }//
159 
160  const bool FlatG4Tree::IsPrimary(const UInt_t& iparticle) const {
161  if(fParentPdg->at(iparticle)==INT_MAX) return true;
162  else return false;
163  }
164 
165  const Int_t FlatG4Tree::PDG(const UInt_t& iparticle) const {
166  return fPDG->at(iparticle);
167  }
168 
169 const UInt_t FlatG4Tree::NRegions(const UInt_t& iparticle) const {
170  return fNRegions->at(iparticle);
171 }
172 
173 const Int_t FlatG4Tree::Region(const UInt_t& iparticle, const UInt_t& iregion) const {
174  return fRegions->at(LocalToGlobalIndex(iparticle)+iregion);
175 }
176 
177 const vector<const TLorentzVector*>* FlatG4Tree::SimMomEnter(const UInt_t& iparticle) const {
178 
179  //output vector
180  auto v = new vector<const TLorentzVector*>();
181 
182  //get index range
183  size_t ireg = LocalToGlobalIndex(iparticle);
184  size_t ireg_max = ireg+2*fNRegions->at(iparticle);
185 
186  //fill for all regions
187  //entry points are every other value and precede exit points
188  for(; ireg<ireg_max; ireg+=2){
189  v->push_back(new TLorentzVector(fPx->at(ireg), fPy->at(ireg),
190  fPz->at(ireg), fE->at(ireg)));
191  }
192  return v;
193 }
194 
195 const vector<const TLorentzVector*>* FlatG4Tree::SimMomExit(const UInt_t& iparticle) const {
196 
197  //output vector
198  auto v = new vector<const TLorentzVector*>();
199 
200  //get index range
201  size_t ireg = LocalToGlobalIndex(iparticle)+1;
202  const size_t ireg_max = ireg-1 + 2*fNRegions->at(iparticle);
203 
204  //fill for all regions
205  //exit points are every other value and follow entry points
206  for(; ireg<ireg_max; ireg+=2){
207  v->push_back(new TLorentzVector(fPx->at(ireg), fPy->at(ireg),
208  fPz->at(ireg), fE->at(ireg)));
209  }
210  return v;
211 }
212 
213 const vector<const TLorentzVector*>* FlatG4Tree::SimPosEnter(const UInt_t& iparticle) const {
214 
215  //output vector
216  auto v = new vector<const TLorentzVector*>();
217 
218  //get index range
219  size_t ireg = LocalToGlobalIndex(iparticle);
220  size_t ireg_max = ireg+2*fNRegions->at(iparticle);
221 
222  //fill for all regions
223  //exit points are every other value and follow entry points
224  for(; ireg<ireg_max; ireg+=2){
225  v->push_back(new TLorentzVector(fX->at(ireg), fY->at(ireg),
226  fZ->at(ireg), fT->at(ireg)));
227  }
228  return v;
229 }
230 
231 const vector<const TLorentzVector*>* FlatG4Tree::SimPosExit(const UInt_t& iparticle) const {
232 
233  //output vector
234  auto v = new vector<const TLorentzVector*>();
235 
236  //get index range
237  size_t ireg = LocalToGlobalIndex(iparticle)+1;
238  size_t ireg_max = ireg-1 + 2*fNRegions->at(iparticle);
239 
240  //fill for all regions
241  //exit points are every other value and follow entry points
242  for(; ireg<ireg_max; ireg+=2){
243  v->push_back(new TLorentzVector(fX->at(ireg), fY->at(ireg),
244  fZ->at(ireg), fT->at(ireg)));
245  }
246  return v;
247 }
248 
249 const TLorentzVector* FlatG4Tree::SimMomEnter(const UInt_t& iparticle, const UInt_t& iregion) const {
250  UInt_t index = LocalToGlobalIndex(iparticle) + iregion;
251  return new TLorentzVector(fPx->at(index), fPy->at(index), fPz->at(index), fE->at(index));
252 }
253 
254 const TLorentzVector* FlatG4Tree::SimMomExit(const UInt_t& iparticle, const UInt_t& iregion) const {
255  UInt_t index = LocalToGlobalIndex(iparticle) + 1 + iregion;
256  return new TLorentzVector(fPx->at(index), fPy->at(index), fPz->at(index), fE->at(index));
257 }
258 
259 const TLorentzVector* FlatG4Tree::SimPosEnter(const UInt_t& iparticle, const UInt_t& iregion) const {
260  UInt_t index = LocalToGlobalIndex(iparticle) + iregion;
261  return new TLorentzVector(fX->at(index), fY->at(index), fZ->at(index), fT->at(index));
262 }
263 const TLorentzVector* FlatG4Tree::SimPosExit(const UInt_t& iparticle, const UInt_t& iregion) const {
264  UInt_t index = LocalToGlobalIndex(iparticle) + 1 + iregion;
265  return new TLorentzVector(fX->at(index), fY->at(index), fZ->at(index), fT->at(index));
266 }
267 
268 const int FlatG4Tree::ParentPDG(const UInt_t& iparticle) const {
269  return fParentPdg->at(LocalToGlobalIndex(iparticle));
270 }
271 
272 const int FlatG4Tree::ProgenitorPDG(const UInt_t& iparticle) const {
273  return fProgenitorPdg->at(LocalToGlobalIndex(iparticle));
274 }
275 
276 const int FlatG4Tree::TrackID(const UInt_t& iparticle) const {
277  return fTrkID->at(LocalToGlobalIndex(iparticle));
278 }
279 
280 const int FlatG4Tree::ParentTrackID(const UInt_t& iparticle) const {
281  return fParentTrackId->at(LocalToGlobalIndex(iparticle));
282 }
283 
284 const int FlatG4Tree::ProgenitorTrackID(const UInt_t& iparticle) const {
285  return fProgenitorTrackId->at(LocalToGlobalIndex(iparticle));
286 }
287 
288 const Int_t FlatG4Tree::ProcessI(const UInt_t& iparticle) const {
289  return fProcessI->at(LocalToGlobalIndex(iparticle));
290 }
291 
292 const Int_t FlatG4Tree::ProcessF(const UInt_t& iparticle) const {
293  return fProcessF->at(LocalToGlobalIndex(iparticle));
294 }
295 
296 const UInt_t FlatG4Tree::NSubEntries() const {
297  return fTrkID->size();
298 }
299 
301  fLocalToGlobalIndex.clear();
302  UInt_t iparticle=0;
303  int trkid = -1; fTrkID->at(0);
304  for(UInt_t i=0; i<NSubEntries(); i++){
305  if(trkid!=fTrkID->at(i)){
306  fLocalToGlobalIndex[iparticle] = i;
307  trkid = fTrkID->at(i);
308  iparticle++;
309  }
310  }
311 }
312 
313 const UInt_t FlatG4Tree::LocalToGlobalIndex(const UInt_t& iparticle) const {
314 
315  return fLocalToGlobalIndex.at(iparticle);
316 }
vector< Int_t > * fTrkID
particle&#39;s G4 trackID
Definition: FlatG4Tree.h:76
UInt_t fCurrentEntry
Definition: TreeReader.h:52
const UInt_t LocalToGlobalIndex(const UInt_t &iparticle) const
Definition: FlatG4Tree.cxx:313
const Int_t ProcessI(const UInt_t &iparticle) const override
code for process that created this one
Definition: FlatG4Tree.cxx:288
TBranch * b_ProgenitorTrackId
Definition: FlatG4Tree.h:103
const UInt_t NSim() const override
number of particles
Definition: FlatG4Tree.cxx:152
vector< Int_t > * fParentPdg
particle parent&#39;s PDG code
Definition: FlatG4Tree.h:78
vector< Float_t > * fPy
particle&#39;s y-momentum in lab frame [GeV/c]
Definition: FlatG4Tree.h:89
vector< Int_t > * fProcessI
process that produced the particle
Definition: FlatG4Tree.h:82
const vector< const TLorentzVector * > * SimPosEnter(const UInt_t &iparticle) const override
particle 4-position at entry point, all regions
Definition: FlatG4Tree.cxx:213
opt
Definition: train.py:196
vector< Int_t > * fRegions
region IDs
Definition: FlatG4Tree.h:75
TBranch * b_ParentTrackId
Definition: FlatG4Tree.h:102
const Int_t ProcessF(const UInt_t &iparticle) const override
code for process that killed this one
Definition: FlatG4Tree.cxx:292
const UInt_t NRegions(const UInt_t &iparticle) const override
number of regions traversed by particle
Definition: FlatG4Tree.cxx:169
const int ParentTrackID(const UInt_t &iparticle) const override
G4 track ID of parent particle.
Definition: FlatG4Tree.cxx:280
struct vector vector
vector< UInt_t > * fNRegions
number of regions of interest cross by the particle
Definition: FlatG4Tree.h:74
TBranch * b_TrkID
Definition: FlatG4Tree.h:98
TTree * fTreeIn
pointer to the analyzed TTree or TChain
Definition: TreeReader.h:51
TBranch * b_Regions
Definition: FlatG4Tree.h:97
vector< UInt_t > * fNPts
number of 4-vector "snapshots" (G4 steps)
Definition: FlatG4Tree.h:73
TBranch * b_ProgenitorPdg
Definition: FlatG4Tree.h:101
const int TrackID(const UInt_t &iparticle) const override
G4 track ID (can be <0 if it fell below trking threshold)
Definition: FlatG4Tree.cxx:276
const UInt_t NSubEntries() const
Definition: FlatG4Tree.cxx:296
void CheckOpt(char opt)
Definition: TreeReader.cxx:67
const int ProgenitorPDG(const UInt_t &iparticle) const override
PDG of primary that led this one.
Definition: FlatG4Tree.cxx:272
TBranch * b_NRegions
Definition: FlatG4Tree.h:96
const vector< const TLorentzVector * > * SimMomExit(const UInt_t &iparticle) const override
particle 4-momentum at exit point, all regions
Definition: FlatG4Tree.cxx:195
const int ParentPDG(const UInt_t &iparticle) const override
parent particle&#39;s PDG code
Definition: FlatG4Tree.cxx:268
const vector< const TLorentzVector * > * SimMomEnter(const UInt_t &iparticle) const override
particle 4-momentum at entry point, all regions
Definition: FlatG4Tree.cxx:177
const Int_t Region(const UInt_t &iparticle, const UInt_t &iregion) const override
region number
Definition: FlatG4Tree.cxx:173
vector< UInt_t > * fG4TruthIndex
Definition: G4Tree.h:71
vector< Float_t > * fE
particle&#39;s total energy in lab frame [GeV]
Definition: FlatG4Tree.h:91
vector< Float_t > * fT
particle&#39;s time in lab frame [ns]
Definition: FlatG4Tree.h:87
const Int_t PDG(const UInt_t &iparticle) const override
particle PDG code
Definition: FlatG4Tree.cxx:165
TBranch * b_ProcessF
Definition: FlatG4Tree.h:105
TBranch * b_PDG
Definition: FlatG4Tree.h:99
vector< Int_t > * fProgenitorPdg
FS particle from gen stage that led to this one.
Definition: FlatG4Tree.h:79
vector< Int_t > * fParentTrackId
particle&#39;s parent&#39;s trackID
Definition: FlatG4Tree.h:80
TBranch * b_G4TruthIndex
Definition: G4Tree.h:73
const vector< const TLorentzVector * > * SimPosExit(const UInt_t &iparticle) const override
particle 4-position at exit point, all regions
Definition: FlatG4Tree.cxx:231
UInt_t fNSim
number of G4 particles per event
Definition: FlatG4Tree.h:72
TBranch * b_Event
Definition: TreeReader.h:54
void SetupRead(TTree *tree)
Definition: TreeReader.cxx:6
TBranch * b_ParentPdg
Definition: FlatG4Tree.h:100
vector< Float_t > * fY
particle&#39;s y-position in lab frame [cm]
Definition: FlatG4Tree.h:85
vector< Float_t > * fZ
particle&#39;s z-position in lab frame [cm]
Definition: FlatG4Tree.h:86
vector< Float_t > * fPz
particle&#39;s z-momentum in lab frame [GeV/c]
Definition: FlatG4Tree.h:90
void GetEntry(const UInt_t &ientry) override
Definition: FlatG4Tree.cxx:38
const int ProgenitorTrackID(const UInt_t &iparticle) const override
G4 track ID of primary that led this one.
Definition: FlatG4Tree.cxx:284
vector< Int_t > * fProcessF
process that killed the particle
Definition: FlatG4Tree.h:83
TBranch * b_NPts
Definition: FlatG4Tree.h:95
std::map< UInt_t, UInt_t > fLocalToGlobalIndex
Definition: FlatG4Tree.h:66
vector< Int_t > * fProgenitorTrackId
FS particle from gen stage that led to this one.
Definition: FlatG4Tree.h:81
const UInt_t NPoints(const UInt_t &iparticle) const override
number of G4 steps (i.e. trajectory points)
Definition: FlatG4Tree.cxx:156
vector< Int_t > * fPDG
particle&#39;s PDG code
Definition: FlatG4Tree.h:77
TBranch * b_ProcessI
Definition: FlatG4Tree.h:104
Int_t fEvent
event number for tree entry
Definition: TreeReader.h:55
const bool IsPrimary(const UInt_t &iparticle) const override
did particle come from generator?
Definition: FlatG4Tree.cxx:160
TBranch * b_NSim
Definition: FlatG4Tree.h:94
vector< Float_t > * fPx
particle&#39;s x-momentum in lab frame [GeV/c]
Definition: FlatG4Tree.h:88
QTextStream & endl(QTextStream &s)
vector< Float_t > * fX
particle&#39;s x-position in lab frame [cm]
Definition: FlatG4Tree.h:84
bool SetBranchAddresses() override
Definition: FlatG4Tree.cxx:47