MillG4Tree.cxx
Go to the documentation of this file.
1 /*
2  * MillG4Tree.cxx
3  *
4  * Created on: Feb 13, 2021
5  * Author: chilgenb
6  */
7 
8 //#include "include/garana/Accessors/StructuredG4Tree.h"
10 
11 #include <TTree.h>
12 
13 #include <map>
14 #include <iostream>
15 
16 using std::vector;
17 
18 using namespace garana;
19 
20 MillG4Tree::MillG4Tree(TTree* treeIn, TTree* treeOut)
21 {
22  fG4In = new StructuredG4Tree(treeIn);
23 
24  fTreeIn = treeOut;
25  fOpt='w';
26  this->VerifyBranches();
27  this->SetVecs();
28  this->SetBranchAddresses();
29  this->MillTrees();
30 
31 }//constructor()
32 
33 //Fill branches but do not fill
35 
36  if(!IsVerified()){
37  std::cerr << "WARNING(MillG4Tree::MillTrees): "
38  << "trying to mill trees that have not been verified"
39  << std::endl;
40  return;
41  }
42 
43  std::cout << "MillG4Tree: loop over " << fG4In->NEntries() << " gen entries" << std::endl;
44 
45  //loop over input g4Tree events (entries)
46  for(size_t ientry=0; ientry<fG4In->NEntries(); ientry++){
47 
48  ClearVecs(); //clear previous entry's data (if any)
49  fG4In->GetEntry(ientry);
50 
51  if(branchToDrawOpt[kEvent]) {
52  fEvent = fG4In->Event();
53  }
54 
56  fNSim = fG4In->NSim();
57  std::cout << "MillG4: output NG4 = " << fNSim << std::endl;
58 
59  //loop over g4 entries
60  std::cout << "MillG4Tree: loop over " << fG4In->NSim() << " isim" <<std::endl;
61  for(size_t isim=0; isim<fG4In->NSim(); isim++){
62 
63  auto posenter = fG4In->SimPosEnter(isim); // 4-position for particle at entry, all regions
64  auto momenter = fG4In->SimMomEnter(isim); // 4-momentum for particle at entry, all regions
65  auto posexit = fG4In->SimPosExit(isim); // 4-position for particle at exit/end, all regions
66  auto momexit = fG4In->SimMomExit(isim); // 4-momentum for particle at exit, all regions
67 
68  for(size_t ireg=0; ireg<fG4In->NRegions(isim); ireg++){
69  for(size_t i=0; i<2; i++){
70  fNPts ->push_back(fG4In->NPoints(isim));
71  fTrkID ->push_back(fG4In->TrackID(isim));
72  fPDG ->push_back(fG4In->PDG(isim));
73  fParentPdg ->push_back(fG4In->ParentPDG(isim));
74  fProgenitorPdg ->push_back(fG4In->ProgenitorPDG(isim));
75  fParentTrackId ->push_back(fG4In->ParentTrackID(isim));
76  fProgenitorTrackId ->push_back(fG4In->ProgenitorTrackID(isim));
77  fProcessI ->push_back(fG4In->ProcessI(isim));
78  fProcessF ->push_back(fG4In->ProcessF(isim));
79  if(i==0) {
80  fX ->push_back(posenter->at(ireg)->X());
81  fY ->push_back(posenter->at(ireg)->Y());
82  fZ ->push_back(posenter->at(ireg)->Z());
83  fT ->push_back(posenter->at(ireg)->T());
84  fPx ->push_back(momenter->at(ireg)->Px());
85  fPy ->push_back(momenter->at(ireg)->Py());
86  fPz ->push_back(momenter->at(ireg)->Pz());
87  fE ->push_back(momenter->at(ireg)->E());
88  }
89  else {
90  fX ->push_back(posexit->at(ireg)->X());
91  fY ->push_back(posexit->at(ireg)->Y());
92  fZ ->push_back(posexit->at(ireg)->Z());
93  fT ->push_back(posexit->at(ireg)->T());
94  fPx ->push_back(momexit->at(ireg)->Px());
95  fPy ->push_back(momexit->at(ireg)->Py());
96  fPz ->push_back(momexit->at(ireg)->Pz());
97  fE ->push_back(momexit->at(ireg)->E());
98  }
99  }
100  }// for regions
101 
103  fG4TruthIndex->push_back(fG4In->GetTruthIndex(isim));
104  }
105 
106  }//for g4 entries
107  }//if branch present
108 
109  fTreeIn->Fill(); //actually our fTreeOut, but it's called fTreeIn in FlatG4Tree.h, fill once per event
110 
111  }//for genTree entries
112 
113  fTreeIn->Write();
114 
115 }//Mill()
116 
118 
119  // get list of branches and check it matches what we expect
120  const TObjArray* branches = fG4In->GetBranchList();
121  std::cout << "got ObjArray of branches from fTreeIn" << std::endl;
122  try {
123  if(!branches || branches->GetEntries()==0 )
124  throw branches;
125  }
126  catch(TObjArray* branches){
127  std::cerr << "ERROR(MillG4Tree::VerifyBranches): no branches found in passed input tree"
128  << std::endl;
129  return false;
130  }
131 
132  if(branches->GetEntriesFast() != (Int_t)nameToG4Branch.size())
133  std::cout << "WARNING(MillG4Tree::VerifyBranches): Mismatch in number of branches (expected "
134  << nameToG4Branch.size() << " but found " << branches->GetEntriesFast()
135  << ")" << std::endl;
136  else
137  std::cout << "found genTree with " << branches->GetEntriesFast() << " branches" << std::endl;
138 
139  // loop over branches
140  TIter next(branches);
141  TBranch* branch = nullptr;
142  while( (branch=(TBranch*)next())) {
143 
144  // check if branch is expected
145  if(nameToG4Branch.find(CharStarToString(branch->GetFullName()))!=nameToG4Branch.end()) {
146 
147  std::cout << " chopping branch '" << branch->GetFullName() << "'" << std::endl;
148 
149  if(nameToG4Branch[CharStarToString(branch->GetFullName())] == kEvent)
150  branchToDrawOpt[kEvent] = true;
151 
152  if(nameToG4Branch[CharStarToString(branch->GetFullName())] == kG4Particles)
154 
155  if(nameToG4Branch[CharStarToString(branch->GetFullName())] == kG4TruthIndex)
157 
158  }//endif known branch
159 
160  else{
161  std::cout << "WARNING(MillG4Tree): ignoring unknown branch '"
162  << branch->GetFullName() << "'" << std::endl;
163  }//else
164 
165  }//for branches
166 
167  fIsVerified = true;
168  return true;
169 
170 }//VerifyBranches()
171 
172 
173 /* MillG4Tree.cxx */
vector< Int_t > * fTrkID
particle&#39;s G4 trackID
Definition: FlatG4Tree.h:76
const int ParentPDG(const UInt_t &iparticle) const override
parent particle&#39;s PDG code
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 UInt_t NSim() const override
number of particles
const int ParentTrackID(const UInt_t &iparticle) const override
G4 track ID of parent particle.
struct vector vector
bool VerifyBranches() override
Definition: MillG4Tree.cxx:117
const UInt_t NPoints(const UInt_t &iparticle) const override
number of G4 steps (i.e. trajectory points)
TTree * fTreeIn
pointer to the analyzed TTree or TChain
Definition: TreeReader.h:51
vector< UInt_t > * fNPts
number of 4-vector "snapshots" (G4 steps)
Definition: FlatG4Tree.h:73
std::string CharStarToString(const char *cstr)
Definition: Mill.cxx:12
const vector< const TLorentzVector * > * SimMomEnter(const UInt_t &iparticle) const override
particle 4-momentum at entry point, all regions
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 ProcessF(const UInt_t &iparticle) const override
code for process that killed this one
void MillTrees() override
Definition: MillG4Tree.cxx:34
const vector< const TLorentzVector * > * SimMomExit(const UInt_t &iparticle) const override
particle 4-momentum at exit point, all regions
const UInt_t NRegions(const UInt_t &iparticle) const override
number of regions traversed by particle
vector< Int_t > * fProgenitorPdg
FS particle from gen stage that led to this one.
Definition: FlatG4Tree.h:79
bool fIsVerified
Definition: Mill.h:33
vector< Int_t > * fParentTrackId
particle&#39;s parent&#39;s trackID
Definition: FlatG4Tree.h:80
UInt_t fNSim
number of G4 particles per event
Definition: FlatG4Tree.h:72
bool IsVerified() const
Definition: Mill.h:28
Int_t Event() const
Definition: TreeReader.cxx:30
const Int_t ProcessI(const UInt_t &iparticle) const override
code for process that created this one
vector< Float_t > * fY
particle&#39;s y-position in lab frame [cm]
Definition: FlatG4Tree.h:85
const Int_t PDG(const UInt_t &iparticle) const override
particle PDG code
const vector< const TLorentzVector * > * SimPosEnter(const UInt_t &iparticle) const override
particle 4-position at entry point, all regions
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
vector< Int_t > * fProcessF
process that killed the particle
Definition: FlatG4Tree.h:83
const TObjArray * GetBranchList() const
Definition: TreeReader.cxx:47
std::map< std::string, G4Branch > nameToG4Branch
Definition: MillG4Tree.h:40
std::map< G4Branch, bool > branchToDrawOpt
Definition: MillG4Tree.h:47
vector< Int_t > * fProgenitorTrackId
FS particle from gen stage that led to this one.
Definition: FlatG4Tree.h:81
vector< Int_t > * fPDG
particle&#39;s PDG code
Definition: FlatG4Tree.h:77
const int TrackID(const UInt_t &iparticle) const override
G4 track ID (can be <0 if it fell below trking threshold)
UInt_t const GetTruthIndex(UInt_t iparticle) const
index in gen tree subentry to truth match to this
Definition: G4Tree.cxx:12
const vector< const TLorentzVector * > * SimPosExit(const UInt_t &iparticle) const override
particle 4-position at exit point, all regions
const int ProgenitorTrackID(const UInt_t &iparticle) const override
G4 track ID of primary that led this one.
const int ProgenitorPDG(const UInt_t &iparticle) const override
PDG of primary that led this one.
virtual void GetEntry(const UInt_t &ientry)
Definition: TreeReader.cxx:39
Int_t fEvent
event number for tree entry
Definition: TreeReader.h:55
StructuredG4Tree * fG4In
Definition: MillG4Tree.h:30
vector< Float_t > * fPx
particle&#39;s x-momentum in lab frame [GeV/c]
Definition: FlatG4Tree.h:88
size_t NEntries() const
Definition: TreeReader.cxx:35
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