MillGenTree.cxx
Go to the documentation of this file.
1 /*
2  * MillGenTree.cxx
3  *
4  * Created on: Feb 13, 2021
5  * Author: chilgenb
6  */
7 
8 //#include "include/garana/Accessors/StructuredGenTree.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 namespace { //helper functions
21  void FillEmpty(vector<double>* v);
22  void FillEmpty(vector<int>* v);
23  void FillEmpty(vector<bool>* v);
24 }//local namespace
25 
26 MillGenTree::MillGenTree(TTree* treeIn, TTree* treeOut)
27 {
28  fGenIn = new StructuredGenTree(treeIn);
29 
30  fTreeIn = treeOut;
31  //fTreeOut = new TTree("genTree", "generator level information");
32  //fGenOut= new FlatGenTree(treeOut,'w');
33 
34  fOpt='w';
35  this->VerifyBranches();
36  this->SetVecs();
37  this->SetBranchAddresses();
38  this->MillTrees();
39 
40 }//constructor()
41 
42 //Fill branches but do not fill
44 
45  if(!IsVerified()){
46  std::cerr << "WARNING(MillGenTree::MillTrees): "
47  << "trying to mill trees that have not been verified"
48  << std::endl;
49  return;
50  }
51 
52  std::cout << "MillGenTree: loop over " << fGenIn->NEntries() << " gen entries" << std::endl;
53 
54  //loop over input genTree events (entries)
55  for(size_t ientry=0; ientry<fGenIn->NEntries(); ientry++){
56 
57  //fGenOut->ClearVecs(); //clear previous entry's data (if any)
58  ClearVecs();
59  fGenIn->GetEntry(ientry);
60 
63 
65  fNGen = fGenIn->NGen();
66  std::cout << "MillGen: output NGen = " << fNGen << std::endl;
67  }
68 
69  //loop over gen entries (usually GTruths with 1 per event)
70  std::cout << "MillGenTree: loop over " << fGenIn->NGen() << " igen" <<std::endl;
71  for(size_t igen=0; igen<fGenIn->NGen(); igen++){
72 
73  if(branchToDrawOpt[kFSParticle]) {
74 
75  //loop over FSparticles associated with this GTruth
76  auto const& particles = fGenIn->GetParticles(igen);
77  fNFS->push_back(particles->size());
78  std::cout << "MillGenTree: igen->" << igen << " NFSP->"
79  << fNFS->back() << std::endl;
80  FillFSParticle(particles);
81 
82  }//if filling FSParticle branches
83 
85 
86  const GTruth* truth = fGenIn->GetGTruth(igen);
87 
88  if(!truth)
90  else
91  FillGTruth(truth);
92 
93  }//if filling GTruth branches
94  }//for gen entries
95 
96  fTreeIn->Fill(); //actually our fTreeOut, but it's called fTreeIn in FlatGenTree.h, fill once per event
97  }//for genTree entries
98 
99  fTreeIn->Write();
100 
101 }//Mill()
102 
104 
105  // get list of branches and check it matches what we expect
106  const TObjArray* branches = fGenIn->GetBranchList();
107  std::cout << "got ObjArray of branches from fTreeIn" << std::endl;
108  try {
109  if(!branches || branches->GetEntries()==0 )
110  throw branches;
111  }
112  catch(TObjArray* branches){
113  std::cerr << "ERROR(MillGenTree::VerifyBranches): no branches found in passed input tree"
114  << std::endl;
115  return false;
116  }
117 
118  if(branches->GetEntriesFast() != (Int_t)nameToGenBranch.size())
119  std::cout << "WARNING(MillGenTree::VerifyBranches): Mismatch in number of branches (expected "
120  << nameToGenBranch.size() << " but found " << branches->GetEntriesFast()
121  << ")" << std::endl;
122  else
123  std::cout << "found genTree with " << branches->GetEntriesFast() << " branches" << std::endl;
124 
125  // loop over branches
126  TIter next(branches);
127  TBranch* branch = nullptr;
128  while( (branch=(TBranch*)next())) {
129 
130  // check if branch is expected
131  if(nameToGenBranch.find(CharStarToString(branch->GetFullName()))!=nameToGenBranch.end()) {
132 
133  std::cout << " chopping branch '" << branch->GetFullName() << "'" << std::endl;
134 
135  if(nameToGenBranch[CharStarToString(branch->GetFullName())] == kEvent)
136  branchToDrawOpt[kEvent] = true;
137 
138  if(nameToGenBranch[CharStarToString(branch->GetFullName())] == kGIndex)
139  branchToDrawOpt[kGIndex] = true;
140 
141  if(nameToGenBranch[CharStarToString(branch->GetFullName())] == kGTruth)
142  branchToDrawOpt[kGTruth] = true;
143 
144  if(nameToGenBranch[CharStarToString(branch->GetFullName())] == kFSParticle)
146 
147  }//endif known branch
148 
149  else{
150  std::cout << "WARNING(MillGenTree): ignoring unknown branch '"
151  << branch->GetFullName() << "'" << std::endl;
152  }//else
153 
154  }//for branches
155 
156  fIsVerified = true;
157  return true;
158 
159 }//VerifyBranches()
160 
161 void MillGenTree::FillGTruth(const GTruth* truth){
162 
163  fNuX->push_back(truth->fVertex.X());
164  fNuY->push_back(truth->fVertex.Y());
165  fNuZ->push_back(truth->fVertex.Z());
166  fNuT->push_back(truth->fVertex.T());
167 
168  fWeight->push_back(truth->fweight);
169  fProbability->push_back(truth->fprobability);
170  fXsec->push_back(truth->fXsec);
171  fDiffXsec->push_back(truth->fDiffXsec);
172  fGPhaseSpace->push_back(truth->fGPhaseSpace);
173 
174  fProbePDG->push_back(truth->fProbePDG);
175  fProbePx->push_back(truth->fProbeP4.Px());
176  fProbePy->push_back(truth->fProbeP4.Py());
177  fProbePz->push_back(truth->fProbeP4.Pz());
178  fProbeE->push_back(truth->fProbeP4.E());
179 
180  fTgtPx->push_back(truth->fProbeP4.Px());
181  fTgtPy->push_back(truth->fProbeP4.Py());
182  fTgtPz->push_back(truth->fProbeP4.Pz());
183  fTgtE->push_back(truth->fProbeP4.E());
184  fTgtZ->push_back(truth->ftgtZ);
185  fTgtA->push_back(truth->ftgtA);
186  fTgtPDG->push_back(truth->ftgtPDG);
187  fHitNucPDG->push_back(truth->fHitNucPDG);
188  fHitQrkPDG->push_back(truth->fHitQrkPDG);
189  fIsSeaQuark->push_back(truth->fIsSeaQuark);
190 
191  fHitNucPx->push_back(truth->fHitNucP4.Px());
192  fHitNucPy->push_back(truth->fHitNucP4.Py());
193  fHitNucPz->push_back(truth->fHitNucP4.Pz());
194  fHitNucE->push_back(truth->fHitNucP4.E());
195  fHitNucPos->push_back(truth->fHitNucPos);
196  fGscatter->push_back(truth->fGscatter);
197  fGint->push_back(truth->fGint);
198  fGQ2->push_back(truth->fgQ2);
199  fGq2->push_back(truth->fgq2);
200  fGW->push_back(truth->fgW);
201  fGT->push_back(truth->fgT);
202  fGX->push_back(truth->fgX);
203  fGY->push_back(truth->fgY);
204 
205  fFSleptonPx->push_back(truth->fFSleptonP4.Px());
206  fFSleptonPy->push_back(truth->fFSleptonP4.Py());
207  fFSleptonPz->push_back(truth->fFSleptonP4.Pz());
208  fFSleptonE->push_back(truth->fFSleptonP4.E());
209 
210  fFShadSystPx->push_back(truth->fFShadSystP4.Px());
211  fFShadSystPy->push_back(truth->fFShadSystP4.Py());
212  fFShadSystPz->push_back(truth->fFShadSystP4.Pz());
213  fFShadSystE->push_back(truth->fFShadSystP4.E());
214 
215  fIsCharm->push_back(truth->fIsCharm);
216  fCharmHadronPDG->push_back(truth->fCharmHadronPdg);
217  fIsStrange->push_back(truth->fIsStrange);
218  fStrangeHadronPDG->push_back(truth->fStrangeHadronPdg);
219  fNumProton->push_back(truth->fNumProton);
220  fNumNeutron->push_back(truth->fNumNeutron);
221  fNumPi0->push_back(truth->fNumPi0);
222  fNumPiPlus->push_back(truth->fNumPiPlus);
223  fNumPiMinus->push_back(truth->fNumPiMinus);
224  fResNum->push_back(truth->fResNum);
225  fDecayMode->push_back(truth->fDecayMode);
226 }//FillGTruth()
227 
229 
230  FillEmpty(fNuX);
231  FillEmpty(fNuY);
232  FillEmpty(fNuZ);
233  FillEmpty(fNuT);
234  FillEmpty(fWeight);
235  FillEmpty(fProbability);
236  FillEmpty(fXsec);
237  FillEmpty(fDiffXsec);
238  FillEmpty(fGPhaseSpace);
239  FillEmpty(fProbePDG);
240  FillEmpty(fProbePx);
241  FillEmpty(fProbePy);
242  FillEmpty(fProbePz);
243  FillEmpty(fProbeE);
244  FillEmpty(fTgtPx);
245  FillEmpty(fTgtPy);
246  FillEmpty(fTgtPz);
247  FillEmpty(fTgtE);
248 
249  FillEmpty(fTgtZ);
250  FillEmpty(fTgtA);
251  FillEmpty(fTgtPDG);
252  FillEmpty(fHitNucPDG);
253  FillEmpty(fHitQrkPDG);
254  FillEmpty(fIsSeaQuark);
255  FillEmpty(fHitNucPx);
256  FillEmpty(fHitNucPy);
257  FillEmpty(fHitNucPz);
258  FillEmpty(fHitNucE);
259  FillEmpty(fHitNucPos);
260 
261  FillEmpty(fGscatter);
262  FillEmpty(fGint);
263  FillEmpty(fGQ2);
264  FillEmpty(fGq2);
265  FillEmpty(fGW);
266  FillEmpty(fGT);
267  FillEmpty(fGX);
268  FillEmpty(fGY);
269  FillEmpty(fFSleptonPx);
270  FillEmpty(fFSleptonPy);
271  FillEmpty(fFSleptonPz);
272  FillEmpty(fFSleptonE);
273  FillEmpty(fFShadSystPx);
274  FillEmpty(fFShadSystPy);
275  FillEmpty(fFShadSystPz);
276  FillEmpty(fFShadSystE);
277 
278  FillEmpty(fIsCharm);
279  FillEmpty(fCharmHadronPDG);
280  FillEmpty(fIsStrange);
281  FillEmpty(fStrangeHadronPDG);
282  FillEmpty(fNumProton);
283  FillEmpty(fNumNeutron);
284  FillEmpty(fNumPi0);
285  FillEmpty(fNumPiPlus);
286  FillEmpty(fNumPiMinus);
287  FillEmpty(fResNum);
288  FillEmpty(fDecayMode);
289 }//FillEmptyGTruth
290 
291 void MillGenTree::FillFSParticle(const vector<FSParticle>* fsps){
292 
293  for( auto const& fsp : *fsps ) {
294 
295  fFSPdg->push_back(fsp.PDG());
296  fFSPosX->push_back(fsp.X());
297  fFSPosY->push_back(fsp.Y());
298  fFSPosZ->push_back(fsp.Z());
299  fFST->push_back(fsp.T());
300  fFSMomX->push_back(fsp.Px());
301  fFSMomY->push_back(fsp.Py());
302  fFSMomZ->push_back(fsp.Pz());
303  fFSE->push_back(fsp.E());
304 
305  }//for FSParticles
306 }//FillFSParticle
307 
308 namespace {
309 
310  void FillEmpty(vector<double>* v){
311  v->push_back(DBL_MAX);
312  }
313 
314  void FillEmpty(vector<int>* v){
315  v->push_back(INT_MAX);
316  }
317 
318  void FillEmpty(vector<bool>* v){
319  v->push_back(false);
320  }
321 
322 }
323 
324 /* MillGenTree.cxx */
bool fIsCharm
did the interaction produce a charmed hadron?
Definition: GTruth.h:69
vector< double > * fFShadSystPz
Definition: FlatGenTree.h:240
vector< double > * fHitNucPy
Definition: FlatGenTree.h:219
const std::vector< Int_t > * GetGIndex() const
whether or not subentry is GENIE interaction, index of GENIE interaction
Definition: GenTree.cxx:7
double fgq2
Definition: GTruth.h:60
std::map< GenBranch, bool > branchToDrawOpt
Definition: MillGenTree.h:51
vector< double > * fTgtE
Definition: FlatGenTree.h:209
vector< int > * fNumPiPlus
Definition: FlatGenTree.h:250
const GTruth * GetGTruth(const UInt_t &igen) const
StructuredGenTree * fGenIn
Definition: MillGenTree.h:32
int fCharmHadronPdg
Definition: GTruth.h:70
vector< double > * fGq2
Definition: FlatGenTree.h:227
void MillTrees() override
Definition: MillGenTree.cxx:43
int fStrangeHadronPdg
Definition: GTruth.h:72
vector< double > * fFSleptonPz
Definition: FlatGenTree.h:235
vector< double > * fGY
Definition: FlatGenTree.h:231
int fNumPiMinus
number of pi minuses after reaction, before FSI
Definition: GTruth.h:77
bool VerifyBranches() override
const vector< Int_t > * fGIndex
Definition: FlatGenTree.h:90
double fweight
event interaction weight (genie internal)
Definition: GTruth.h:32
struct vector vector
vector< double > * fFSleptonPx
Definition: FlatGenTree.h:233
vector< Float_t > * fFSPosY
y-coordinate [cm]
Definition: FlatGenTree.h:179
vector< double > * fTgtPx
Definition: FlatGenTree.h:206
vector< double > * fGQ2
Definition: FlatGenTree.h:226
int fNumProton
number of protons after reaction, before FSI
Definition: GTruth.h:73
vector< int > * fTgtZ
Definition: FlatGenTree.h:211
vector< double > * fProbability
Definition: FlatGenTree.h:195
vector< double > * fHitNucPx
Definition: FlatGenTree.h:218
TTree * fTreeIn
pointer to the analyzed TTree or TChain
Definition: TreeReader.h:51
int fGscatter
neutrino scattering code
Definition: GTruth.h:54
vector< bool > * fIsStrange
Definition: FlatGenTree.h:245
double fgY
Definition: GTruth.h:64
TLorentzVector fFSleptonP4
generated final state primary lepton (LAB frame) // added version 13
Definition: GTruth.h:65
double fgT
Definition: GTruth.h:62
vector< double > * fFSleptonE
Definition: FlatGenTree.h:236
vector< Float_t > * fFST
time [ns]
Definition: FlatGenTree.h:181
vector< double > * fFShadSystE
Definition: FlatGenTree.h:241
int fNumNeutron
number of neutrons after reaction, before FSI
Definition: GTruth.h:74
int fResNum
resonance number
Definition: GTruth.h:78
vector< double > * fGT
Definition: FlatGenTree.h:229
vector< double > * fProbePz
Definition: FlatGenTree.h:203
TLorentzVector fVertex
Definition: GTruth.h:31
void FillFSParticle(const vector< FSParticle > *fsp)
vector< double > * fTgtPy
Definition: FlatGenTree.h:207
vector< Float_t > * fFSMomZ
momentum, z-component [GeV/c]
Definition: FlatGenTree.h:184
vector< double > * fDiffXsec
Definition: FlatGenTree.h:197
vector< double > * fGW
Definition: FlatGenTree.h:228
vector< int > * fResNum
Definition: FlatGenTree.h:252
vector< int > * fDecayMode
Definition: FlatGenTree.h:253
const vector< FSParticle > * GetParticles(const UInt_t &igen) const
vector< int > * fGscatter
Definition: FlatGenTree.h:224
std::map< std::string, GenBranch > nameToGenBranch
Definition: MillGenTree.h:44
double fprobability
interaction probability
Definition: GTruth.h:33
vector< Float_t > * fFSMomY
momentum, y-component [GeV/c]
Definition: FlatGenTree.h:183
vector< int > * fGint
Definition: FlatGenTree.h:225
double fgX
Definition: GTruth.h:63
vector< int > * fGPhaseSpace
Definition: FlatGenTree.h:198
vector< int > * fNumPi0
Definition: FlatGenTree.h:249
std::string CharStarToString(const char *cstr)
Definition: Mill.cxx:12
int ftgtPDG
PDG of Target Nucleus, nucleon only if free.
Definition: GTruth.h:46
vector< UInt_t > * fNFS
number of FSParticles for igen^th GTruth len=NGen
Definition: FlatGenTree.h:174
double fXsec
cross section of interaction
Definition: GTruth.h:34
vector< bool > * fIsCharm
Definition: FlatGenTree.h:243
TLorentzVector fProbeP4
Definition: GTruth.h:40
vector< Float_t > * fFSMomX
momentum, x-component [GeV/c]
Definition: FlatGenTree.h:182
vector< double > * fFSleptonPy
Definition: FlatGenTree.h:234
TLorentzVector fHitNucP4
Definition: GTruth.h:50
vector< double > * fNuZ
Definition: FlatGenTree.h:190
int fNumPi0
number of pi0 after reaction, before FSI
Definition: GTruth.h:75
int fDecayMode
Definition: GTruth.h:79
vector< double > * fNuX
GTruth (one or more per genTree entry) //.
Definition: FlatGenTree.h:188
bool fIsVerified
Definition: Mill.h:33
vector< int > * fHitQrkPDG
Definition: FlatGenTree.h:215
double fHitNucPos
Definition: GTruth.h:51
int fGPhaseSpace
phase space system of DiffXSec
Definition: GTruth.h:36
vector< int > * fTgtPDG
Definition: FlatGenTree.h:213
int fProbePDG
Definition: GTruth.h:39
vector< int > * fHitNucPDG
Definition: FlatGenTree.h:214
double fgW
Definition: GTruth.h:61
vector< Float_t > * fFSPosX
x-coordinate [cm]
Definition: FlatGenTree.h:178
double fDiffXsec
differential cross section of interaction
Definition: GTruth.h:35
vector< double > * fTgtPz
Definition: FlatGenTree.h:208
bool IsVerified() const
Definition: Mill.h:28
vector< int > * fStrangeHadronPDG
Definition: FlatGenTree.h:246
int fNumPiPlus
number of pi pluses after reaction, before FSI
Definition: GTruth.h:76
vector< double > * fFShadSystPx
Definition: FlatGenTree.h:238
vector< int > * fNumProton
Definition: FlatGenTree.h:247
vector< int > * fNumPiMinus
Definition: FlatGenTree.h:251
bool SetBranchAddresses() override
Definition: FlatGenTree.cxx:45
vector< bool > * fIsSeaQuark
Definition: FlatGenTree.h:216
vector< double > * fProbePx
Definition: FlatGenTree.h:201
int fHitNucPDG
hit nucleon PDG code
Definition: GTruth.h:47
vector< double > * fNuY
Definition: FlatGenTree.h:189
vector< int > * fProbePDG
Definition: FlatGenTree.h:199
const TObjArray * GetBranchList() const
Definition: TreeReader.cxx:47
int fGint
interaction code
Definition: GTruth.h:55
vector< double > * fXsec
Definition: FlatGenTree.h:196
vector< UInt_t > * fFSPdg
particle PDG code
Definition: FlatGenTree.h:177
vector< int > * fNumNeutron
Definition: FlatGenTree.h:248
vector< double > * fGX
Definition: FlatGenTree.h:230
vector< double > * fProbePy
Definition: FlatGenTree.h:202
bool fIsSeaQuark
Definition: GTruth.h:49
int fHitQrkPDG
hit quark PDG code
Definition: GTruth.h:48
bool fIsStrange
strange production // added version 13
Definition: GTruth.h:71
vector< Float_t > * fFSPosZ
z-coordinate [cm]
Definition: FlatGenTree.h:180
virtual void GetEntry(const UInt_t &ientry)
Definition: TreeReader.cxx:39
void FillGTruth(const GTruth *truth)
TLorentzVector fFShadSystP4
generated final state hadronic system (LAB frame)
Definition: GTruth.h:66
vector< Float_t > * fFSE
total energy [GeV]
Definition: FlatGenTree.h:185
vector< double > * fNuT
Definition: FlatGenTree.h:191
vector< double > * fHitNucPos
Definition: FlatGenTree.h:222
vector< double > * fFShadSystPy
Definition: FlatGenTree.h:239
double fgQ2
< these are for the internal (on shell) genie kinematics
Definition: GTruth.h:59
vector< double > * fWeight
Definition: FlatGenTree.h:194
vector< int > * fTgtA
Definition: FlatGenTree.h:212
vector< double > * fHitNucE
Definition: FlatGenTree.h:221
size_t NEntries() const
Definition: TreeReader.cxx:35
QTextStream & endl(QTextStream &s)
vector< double > * fHitNucPz
Definition: FlatGenTree.h:220
const UInt_t NGen() const override
vector< double > * fProbeE
Definition: FlatGenTree.h:204
vector< int > * fCharmHadronPDG
Definition: FlatGenTree.h:244