StructuredRecoTree.cxx
Go to the documentation of this file.
1 /*
2  * StructuredRecoTree.cxx
3  *
4  * Created on: Feb 18, 2021
5  * Author: chilgenb
6  */
7 
9 
10 using namespace std;//::vector;
11 using namespace garana;
12 
13 StructuredRecoTree::StructuredRecoTree(TTree* tree) {
14  SetupRead(tree); //initialize tree pointer in TreeReader instance and set branch addresses
15 }
16 
17 bool StructuredRecoTree::SetBranchAddresses() {
18 
19  cout << "setting StructuredRecoTree branch addresses" << endl;
20 
21  fTreeIn->SetBranchAddress("Event", &fEvent, &b_Event );
22  cout << "set Event" << endl;
23  fTreeIn->SetBranchAddress("Tracks", &fTracks , &b_Tracks );
24  cout << "set Track" << endl;
25  fTreeIn->SetBranchAddress("Vees", &fVees , &b_Vees );
26  cout << "set Vees" << endl;
27  fTreeIn->SetBranchAddress("Vertices", &fVertices , &b_Vertices );
28  cout << "set Vertices" << endl;
29  fTreeIn->SetBranchAddress("CalClusters", &fCalClusters , &b_CalClusters );
30  cout << "set CalClusters" << endl;
31  fTreeIn->SetBranchAddress("TrackG4Indices", &fTrackG4PIndices , &b_TrackG4PIndices );
32  cout << "set TrackG4Indices" << endl;
33  fTreeIn->SetBranchAddress("VertTrackIndices", &fVertTrackIndices, &b_VertTrackIndices );
34  fTreeIn->SetBranchAddress("VertTrackEnds", &fVertTrackEnds , &b_VertTrackEnds );
35  fTreeIn->SetBranchAddress("VeeTrackIndices", &fVeeTrackIndices , &b_VeeTrackIndices );
36  fTreeIn->SetBranchAddress("VeeTrackEnds", &fVeeTrackEnds , &b_VeeTrackEnds );
37  fTreeIn->SetBranchAddress("CalTrackIndices", &fCalClusterTrackIndices , &b_CalClusterTrackIndices );
38  //fTreeIn->SetBranchAddress("CalTrackEnds", &fCalTrackEnds , &b_CalTrackEnds );
39  fTreeIn->SetBranchAddress("CalG4Indices", &fCalClusterG4Indices , &b_CalClusterG4Indices );
40  /*if(fGeo->HasMuonDetector()){
41  fTreeIn->SetBranchAddress("MuIDClusters", "vector<gar::rec::Cluster>", &fMuClusters);
42  }*/
43 
44  //TODO: add Assns for reco r&d products
45  /*if(fAnaMode == "reco"){
46  fRecoTree->Branch("TPCHits", "vector<gar::rec::Hit>", &fTPCHits);
47  fRecoTree->Branch("TPCClusters", "vector<gar::rec::TPCCluster>", &fTPCClusters);
48  fRecoTree->Branch("CalHits", "vector<gar::rec::CaloHit>", &fCalHits);
49  if(fGeo->HasMuonDetector()){
50  fRecoTree->Branch("MuIDHits", "vector<gar::rec::CaloHit>", &fMuHits);
51  }
52  }*/
53 
54  cout << "done." << endl;
55 
56  return true;
57 }// end const'or
58 
59 ////============ sizes ============================
60 const size_t StructuredRecoTree::NTrack() const {
61  return fTracks->size();
62 }
63 const size_t StructuredRecoTree::NVertex() const {
64  return fVertices->size();
65 }
66 const size_t StructuredRecoTree::NVee() const {
67  return fVees->size();
68 }
69 const size_t StructuredRecoTree::NCalCluster() const {
70  return fCalClusters->size();
71 }
72 
73 ////======================== track ===================================
74 const TLorentzVector* StructuredRecoTree::TrackVertex(const size_t& itrack) const {
75  return &(fTracks->at(itrack).fVtx);
76 }
77 
78 const TLorentzVector* StructuredRecoTree::TrackEnd(const size_t& itrack) const {
79  return &(fTracks->at(itrack).fEnd);
80 }
81 const size_t StructuredRecoTree::NTrackHit(const size_t& itrack) const {
82  return fTracks->at(itrack).fNHits;
83 }
84 const TVector3* StructuredRecoTree::TrackMomBeg(const size_t& itrack) const {
85  TVector3* v = new TVector3(fTracks->at(itrack).fVtxDir);
86  (*v) *= fTracks->at(itrack).fMomBeg;
87  return v;
88 }
89 
90 const TVector3* StructuredRecoTree::TrackMomEnd(const size_t& itrack) const {
91  TVector3* v = new TVector3(fTracks->at(itrack).fEndDir);
92  (*v) *= fTracks->at(itrack).fMomEnd;
93  return v;
94 }
95 
96 const float StructuredRecoTree::TrackVtxDirectionX(const size_t& itrack) const {
97  return fTracks->at(itrack).fVtxDir.X();
98 }
99 
100 const float StructuredRecoTree::TrackVtxDirectionY(const size_t& itrack) const {
101  return fTracks->at(itrack).fVtxDir.Y();
102 }
103 
104 const float StructuredRecoTree::TrackVtxDirectionZ(const size_t& itrack) const {
105  return fTracks->at(itrack).fVtxDir.Z();
106 }
107 
108 const float StructuredRecoTree::TrackEndDirectionX(const size_t& itrack) const {
109  return fTracks->at(itrack).fEndDir.X();
110 }
111 
112 const float StructuredRecoTree::TrackEndDirectionY(const size_t& itrack) const {
113  return fTracks->at(itrack).fEndDir.Y();
114 }
115 
116 const float StructuredRecoTree::TrackEndDirectionZ(const size_t& itrack) const {
117  return fTracks->at(itrack).fEndDir.Z();
118 }
119 
120 const float StructuredRecoTree::TrackLenFwd(const size_t& itrack) const {
121  return fTracks->at(itrack).fLenFwd;
122 }
123 
124 const float StructuredRecoTree::TrackLenBkd(const size_t& itrack) const {
125  return fTracks->at(itrack).fLenBac;
126 }
127 
128 const float StructuredRecoTree::TrackIonizFwd(const size_t& itrack) const {
129  return fTracks->at(itrack).fIonFwd;
130 }
131 
132 const float StructuredRecoTree::TrackIonizBkd(const size_t& itrack) const {
133  return fTracks->at(itrack).fLenBac;
134 }
135 
136 const int StructuredRecoTree::TrackChiSqrFwd(const size_t& itrack) const {
137  return fTracks->at(itrack).fChiFwd;
138 }
139 
140 const int StructuredRecoTree::TrackChiSqrBkd(const size_t& itrack) const {
141  return fTracks->at(itrack).fChiBac;
142 }
143 
144 const int StructuredRecoTree::TrackChgFwd(const size_t& itrack) const {
145  return fTracks->at(itrack).fChgFwd;
146 }
147 
148 const int StructuredRecoTree::TrackChgBkd(const size_t& itrack) const {
149  return fTracks->at(itrack).fChgBac;
150 }
151 
152 void StructuredRecoTree::TrackParBeg(const size_t& itrack, float pars[5]) const {
153  for(size_t i=0; i<5; i++)
154  pars[i] = fTracks->at(itrack).fTrackParBeg[i];
155 }
156 
157 void StructuredRecoTree::TrackParEnd(const size_t& itrack, float pars[5]) const {
158  for(size_t i=0; i<5; i++)
159  pars[i] = fTracks->at(itrack).fTrackParEnd[i];
160 }
161 
162 void StructuredRecoTree::TrackCovarBeg(const size_t& itrack, float covar[15]) const {
163  for(size_t i=0; i<15; i++)
164  covar[i] = fTracks->at(itrack).fCovMatBeg[i];
165 }
166 
167 void StructuredRecoTree::TrackCovarEnd(const size_t& itrack, float covar[15]) const {
168  for(size_t i=0; i<15; i++)
169  covar[i] = fTracks->at(itrack).fCovMatEnd[i];
170 }
171 
172 const TLorentzVector* StructuredRecoTree::TrackTruePosBeg(const size_t& itrack) const {
173  TLorentzVector outvec(0,0,0,0);
174  float ptot = 0.;
175  for(size_t imctrk=0; imctrk<fTracks->at(itrack).fTruePosVtx.size(); imctrk++ ) {
176  TLorentzVector tmpvec = fTracks->at(itrack).fTruePosVtx[imctrk].second;
177  tmpvec *= fTracks->at(itrack).fTrueMomVtx[imctrk].second.P();
178  outvec += tmpvec;
179  ptot += fTracks->at(itrack).fTrueMomVtx[imctrk].second.P();
180  }
181 
182  outvec *= 1.0/ptot;
183  return new TLorentzVector(outvec);
184 }
185 
186 const TLorentzVector* StructuredRecoTree::TrackTruePosEnd(const size_t& itrack) const {
187  TLorentzVector outvec(0,0,0,0);
188  float ptot = 0.;
189  std::cout << "loop over -- " << fTracks->at(itrack).fTruePosEnd.size() << " -- true positions" << std::endl;
190  for(size_t imctrk=0; imctrk<fTracks->at(itrack).fTruePosEnd.size(); imctrk++) {
191  TLorentzVector tmpvec = fTracks->at(itrack).fTruePosEnd[imctrk].second;
192  std::cout << " submomentum: " << fTracks->at(itrack).fTrueMomEnd[imctrk].second.P() << std::endl;
193  tmpvec *= fTracks->at(itrack).fTrueMomEnd[imctrk].second.P();
194  outvec += tmpvec;
195  ptot += fTracks->at(itrack).fTrueMomEnd[imctrk].second.P();
196  }
197  std::cout << "total true momentum: " << std::to_string(ptot) << std::endl;
198  outvec *= 1.0/ptot;
199  return new TLorentzVector(outvec);
200 }
201 
202 const TLorentzVector* StructuredRecoTree::TrackTrueMomBeg(const size_t& itrack) const {
203  TLorentzVector outvec(0,0,0,0);
204  float ptot = 0.;
205  for(size_t imctrk=0; imctrk<fTracks->at(itrack).fTrueMomVtx.size(); imctrk++) {
206  TLorentzVector tmpvec = fTracks->at(itrack).fTrueMomVtx[imctrk].second;
207  tmpvec *= fTracks->at(itrack).fTrueMomVtx[imctrk].second.P();
208  outvec += tmpvec;
209  ptot += fTracks->at(itrack).fTrueMomVtx[imctrk].second.P();
210  }
211 
212  outvec *= 1.0/ptot;
213  return new TLorentzVector(outvec);
214 }
215 
216 const TLorentzVector* StructuredRecoTree::TrackTrueMomEnd(const size_t& itrack) const {
217  TLorentzVector outvec(0,0,0,0);
218  float ptot = 0.;
219  for(size_t imctrk=0; imctrk<fTracks->at(itrack).fTrueMomEnd.size(); imctrk++) {
220  TLorentzVector tmpvec = fTracks->at(itrack).fTrueMomEnd[imctrk].second;
221  tmpvec *= fTracks->at(itrack).fTrueMomEnd[imctrk].second.P();
222  outvec += tmpvec;
223  ptot += fTracks->at(itrack).fTrueMomEnd[imctrk].second.P();
224  }
225 
226  outvec *= 1.0/ptot;
227  return new TLorentzVector(outvec);
228 }
229 
230 const float StructuredRecoTree::TrackTrueEnergy(const size_t& itrack) const {
231  float etrue = 0.;
232  for(auto const& e : fTracks->at(itrack).fTrueEnergy)
233  etrue += e.second;
234 
235  //return FLT_MAX-itrack*0;
236  return etrue;
237 }
238 
239 const size_t StructuredRecoTree::TrackNTrueTrack(const size_t& itrack) const {
240  return fTracks->at(itrack).fTrueEnergy.size();
241 }
242 
243 const int StructuredRecoTree::TrackTrkIdMaxDeposit(const size_t& itrack) const {
244  /*int imax=-1;
245  float emax = -1.;
246  for(size_t i=0; i<fTracks->at(itrack).fTrueEnergy.size(); i++) {
247 
248  if(fTracks->at(itrack).fTrueEnergy.at(i).second > emax){
249  emax = fTracks->at(itrack).fTrueEnergy.at(i).second;
250  imax = i;
251  }
252  }
253 
254  return fTracks->at(itrack).fTrueEnergy.at(imax).first;*/
255 
256  auto it = std::max_element(fTracks->at(itrack).fTrueEnergy.begin(),fTracks->at(itrack).fTrueEnergy.end(),
257  [](const std::pair<int,float>& lhs,const std::pair<int,float>& rhs) -> bool {return lhs.second < rhs.second; }
258  );
259  if(it==fTracks->at(itrack).fTrueEnergy.end()) {
260  cout << "max element not found for itrack = " << itrack << "! (this is just a warning; is it all noise hits?)" << endl;
261  //cout << "fTracks->at(itrack).fTrueEnergy.size() = " << fTracks->at(itrack).fTrueEnergy.size() << endl;
262  return -INT_MAX;
263  }
264  return (*it).first;
265 
266 }
267 
268 const float StructuredRecoTree::TrackMaxDeposit(const size_t& itrack) const {
269  //TODO check if we actually want total EDep rather than just from the leading contributor
270  auto it = std::max_element(fTracks->at(itrack).fTrueEnergy.begin(),fTracks->at(itrack).fTrueEnergy.end(),
271  [](const std::pair<int,float>& lhs,const std::pair<int,float>& rhs) -> bool {return lhs.second < rhs.second; }
272  );
273  if(it==fTracks->at(itrack).fTrueEnergy.end()) {
274  cout << "max element not found for itrack = " << itrack << "! (this is just a warning; is it all noise hits?)" << endl;
275  //cout << "fTracks->at(itrack).fTrueEnergy.size() = " << fTracks->at(itrack).fTrueEnergy.size() << endl;
276  return 0.;
277  }
278  return (*it).second;
279  //return fTracks->at(itrack).fTrueEnergy.at(TrackTrkIdMaxDeposit(itrack)).second;
280 }
281 
282 const pair<int,float>* StructuredRecoTree::TrackTrueDeposit(const size_t& itrack, size_t& itrue) const {
283 
284  return &(fTracks->at(itrack).fTrueEnergy.at(itrue));
285 
286 }
287 
288 const vector<pair<int,float>>* StructuredRecoTree::TrackTrueDeposits(const size_t& itrack) const {
289 
290  return &(fTracks->at(itrack).fTrueEnergy);
291 }
292 
293 //============== vertex ======================
294 const TLorentzVector* StructuredRecoTree::GetVertex(const size_t& ivertex) const {
295  return fVertices->at(ivertex).GetVertex();
296 }
297 
298 void StructuredRecoTree::VertexCovariance(const size_t& ivertex, float covar[][3]) const {
299  fVertices->at(ivertex).GetCovar(covar);
300 }
301 
302 /// =============== Vee =======================
303 
304 const TLorentzVector* StructuredRecoTree::VeeVertex(const size_t& ivee) const {
305  return fVees->at(ivee).GetVertex();
306 }
307 
308 void StructuredRecoTree::VeeCovariance(const size_t& ivee, float covar[][3]) const {
309  return fVees->at(ivee).GetCovar(covar);
310 }
311 
312 const vector<TLorentzVector>* StructuredRecoTree::VeeMomentumPerHypothesis(const size_t& ivee) const {
313  return fVees->at(ivee).GetMomentaPerHypothesis();
314 }
315 
316 const float StructuredRecoTree::VeeChiSquared(const size_t& ivee) const {
317  return fVees->at(ivee).GetChiSqr();
318 }
319 
320 /// ================ ECal cluster ======================
321 const CaloCluster* StructuredRecoTree::GetCalCluster(const size_t& icluster) const {
322  if(fCalClusters->size()==0)
323  return nullptr;
324 
325  return &(fCalClusters->at(icluster));
326 }
327 const TLorentzVector* StructuredRecoTree::CalClustPosition(const size_t& icluster) const {
328  return GetCalCluster(icluster)->Position();
329 }
330 
331 const float StructuredRecoTree::CalClustEnergy(const size_t& icluster) const {
332  return GetCalCluster(icluster)->Energy();
333 }
334 
335 const float StructuredRecoTree::CalClustEnergyError(const size_t& icluster) const {
336  return GetCalCluster(icluster)->EnergyError();
337 }
338 
339 const float StructuredRecoTree::CalClustTrueEnergy(const size_t& icluster) const{
340  return GetCalCluster(icluster)->TotalTrueEnergy();
341 }
342 const size_t StructuredRecoTree::CalClustNTrueTrack(const size_t& icluster) const{
343  return GetCalCluster(icluster)->NIdes();
344 }
345 const int StructuredRecoTree::CalClustTrkIdMaxDeposit(const size_t& icluster)const{
346  return GetCalCluster(icluster)->TrackIdMaxDep();
347 }
348 const float StructuredRecoTree::CalClustMaxDeposit(const size_t& icluster)const{
349  if(GetCalCluster(icluster))
350  return GetCalCluster(icluster)->MaxDeposit();
351  else
352  return 0.;
353 }
354 const std::pair<int,float>* StructuredRecoTree::CalClustTrueDeposit(const size_t& icluster, const size_t& itrack) const {
355  return GetCalCluster(icluster)->GetTrackIdEdep(itrack);
356 }
357 const float StructuredRecoTree::CalClustTimeDifference(const size_t& icluster) const {
358  return fCalClusters->at(icluster).TimeDifference();
359 }
360 
361 const float* StructuredRecoTree::CalClustShape(const size_t& icluster) const {
362  return fCalClusters->at(icluster).Shape();
363 }
364 
365 const float StructuredRecoTree::CalClustTheta(const size_t& icluster) const {
366  return fCalClusters->at(icluster).Theta();
367 }
368 
369 const float StructuredRecoTree::CalClustPhi(const size_t& icluster) const {
370  return fCalClusters->at(icluster).Phi();
371 }
372 
373 const vector<TVector3>* StructuredRecoTree::CalClustEigenVecs(const size_t& icluster) const{
374  return fCalClusters->at(icluster).EigenVecs();
375 }
376 
int TrackEnd
Definition: Track.h:32
STL namespace.
const double e
const TLorentzVector * Position() const
Definition: CaloCluster.cxx:50
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
QTextStream & endl(QTextStream &s)