Backtracker.cxx
Go to the documentation of this file.
1 /*
2  * Backtracker.cxx
3  *
4  * Created on: Mar 15, 2021
5  * Author: chilgenb
6  */
7 
9 
10 using namespace garana;
11 using namespace std;
12 
13 const vector<UInt_t>* Backtracker::GTruthToG4Particles(const UInt_t& itruth) const {
14  if(CheckRange(fGTruthToG4Particles, itruth)) {
15  return &fGTruthToG4Particles.at(itruth);
16  }
17  else {
18  return new vector<UInt_t>();
19  }
20 }
21 const vector<UInt_t>* Backtracker::GTruthToTracks(const UInt_t& itruth) const {
22  if(CheckRange(fGTruthToTracks, itruth)) {
23  return &fGTruthToTracks.at(itruth);
24  }
25  else {
26  return new vector<UInt_t>();
27  }
28 }
29 /*void Backtracker::FSParticleToG4Particles(const UInt_t& ifsp, vector<UInt_t>& ig4ps){
30  ig4ps.push_back(ifsp); //FIXME - this is just a dummy placeholder
31 }*/
32 UInt_t const Backtracker::G4ParticleToGTruth(const UInt_t& ig4p) const {
33 
34  if(CheckRange(fG4ParticleToGTruth, ig4p)) {
35  return fG4ParticleToGTruth.at(ig4p);
36  }
37  else {
38  return UINT_MAX;
39  }
40 }
41 
42 const vector<UInt_t>* Backtracker::G4ParticleToTracks(const UInt_t& ig4p) const {
43 
44  if(CheckRange(fG4ParticleToTracks, ig4p)) {
45  return &fG4ParticleToTracks.at(ig4p);
46  }
47  else {
48  return new vector<UInt_t>();
49  }
50 }
51 
52 /*UInt_t Backtracker::G4ParticleToFSParticle(const UInt_t& ig4p){
53  return ig4p; //FIXME - this is just a dummy placeholder
54 }*/
55 UInt_t const Backtracker::TrackToGTruth(const UInt_t& itrk) const {
56 
57  if(CheckRange(fTrackToGTruth, itrk)) {
58  return fTrackToGTruth.at(itrk);
59  }
60  else {
61  return UINT_MAX;
62  }
63 }
64 
65 const vector<UInt_t>* Backtracker::TrackToG4Particles(const UInt_t& itrk) const {
66  if(CheckRange(fTrackToG4Particles, itrk)) {
67  return &fTrackToG4Particles.at(itrk);
68  }
69  else {
70  return new vector<UInt_t>();
71  }
72 }
73 
74 const vector<pair<UInt_t,float>> Backtracker::TrackToG4ParticlesDeposits(const UInt_t& itrk) const {
75 
76  vector<pair<UInt_t,float>> outvec;
77  auto ig4ps = TrackToG4Particles(itrk);
78  auto iddeps = rec->TrackTrueDeposits(itrk);
79 
80  map<int,UInt_t> trackIdToig4; // Geant4 trackID to G4Particle index
81  for(auto const& ig4p : *ig4ps){
82  trackIdToig4[g4->TrackID(ig4p)] = ig4p;
83  }
84 
85 
86  for( auto const& iddep : *iddeps) {
87  outvec.push_back(make_pair(trackIdToig4[iddep.first],iddep.second));
88  }
89 
90  return outvec;
91 
92 }
93 
94 UInt_t const Backtracker::TrackToG4Particle(const UInt_t& itrk) const {
95  if(CheckRange(fTrackToG4Particle, itrk)) {
96  return fTrackToG4Particle.at(itrk);
97  }
98  else {
99  return UINT_MAX;
100  }
101 }
102 
103 const pair<UInt_t,float> Backtracker::TrackToG4ParticleDeposit(const UInt_t& itrk ) const {
104 
105  int trkidMax = rec->TrackTrkIdMaxDeposit(itrk);
106  auto ig4ps = TrackToG4Particles(itrk);
107  map<int,UInt_t> trackIdToig4; // Geant4 trackID to G4Particle index
108 
109  for(auto const& ig4p : *ig4ps){
110  trackIdToig4[g4->TrackID(ig4p)] = ig4p;
111  }
112 
113  const pair<UInt_t,float> outpair(trackIdToig4[trkidMax],rec->TrackMaxDeposit(itrk));
114  return outpair;
115 
116 }
117 
118 UInt_t const Backtracker::VertexToGTruth(const UInt_t& ivtx) const {
119  if(CheckRange(fVertexToGTruth, ivtx)) {
120  return fVertexToGTruth.at(ivtx);
121  }
122  else {
123  return UINT_MAX;
124  }
125 }
126 
127 UInt_t const Backtracker::GTruthToVertex(const UInt_t& itruth) const {
128  if(CheckRange(fGTruthToVertex, itruth)) {
129  return fGTruthToVertex.at(itruth);
130  }
131  else {
132  return UINT_MAX;
133  }
134 }
135 
136 const vector<UInt_t>* Backtracker::VertexToG4Particles(const UInt_t& ivtx) const {
137  if(CheckRange(fVertexToG4Particles, ivtx)) {
138  return &fVertexToG4Particles.at(ivtx);
139  }
140  else {
141  return new vector<UInt_t>();
142  }
143 }
144 
145 const vector<UInt_t>* Backtracker::G4ParticleToVertices(const UInt_t& ig4p) const {
146  if(CheckRange(fG4ParticleToVertices, ig4p)) {
147  return &fG4ParticleToVertices.at(ig4p);
148  }
149  else {
150  return new vector<UInt_t>();
151  }
152 }
153 
154 UInt_t const Backtracker::VeeToGTruth(const UInt_t& ivee) const {
155  return fVeeToGTruth.at(ivee);
156 }
157 UInt_t const Backtracker::GTruthToVee(const UInt_t& itruth) const {
158  return fGTruthToVee.at(itruth);
159 }
160 const vector<UInt_t>* Backtracker::VeeToG4Particles(const UInt_t& ivee) const {
161  if(CheckRange(fVeeToG4Particles, ivee)) {
162  return &fVeeToG4Particles.at(ivee);
163  }
164  else {
165  return new vector<UInt_t>();
166  }
167 }
168 UInt_t const Backtracker::G4ParticleToVee(const UInt_t& ig4p) const {
169  return fG4ParticleToVee.at(ig4p);
170 }
171 
172 const vector<UInt_t>* Backtracker::TrackToVertices(const UInt_t& itrk) const {
173  if(CheckRange(fTrackToVertices, itrk)) {
174  return &fTrackToVertices.at(itrk);
175  }
176  else {
177  return new vector<UInt_t>();
178  }
179 }
180 const vector<UInt_t>* Backtracker::VertexToTracks(const UInt_t& ivtx) const {
181  if(CheckRange(fVertexToTracks, ivtx)) {
182  return &fVertexToTracks.at(ivtx);
183  }
184  else {
185  return new vector<UInt_t>();
186  }
187 }
188 const vector<UInt_t>* Backtracker::TrackToVees(const UInt_t& itrk) const {
189  if(CheckRange(fTrackToVees, itrk)) {
190  return &fTrackToVees.at(itrk);
191  }
192  else {
193  return new vector<UInt_t>();
194  }
195 }
196 const vector<UInt_t>* Backtracker::VeeToTracks(const UInt_t& ivee) const {
197  if(CheckRange(fVeeToTracks, ivee)) {
198  return &fVeeToTracks.at(ivee);
199  }
200  else {
201  return new vector<UInt_t>();
202  }
203 }
204 
205 const vector<UInt_t>* Backtracker::TrackToCalClusters(const UInt_t& itrk) const {
206  if(CheckRange(fTrackToCaloClusters, itrk)) {
207  return &fTrackToCaloClusters.at(itrk);
208  }
209  else {
210  return new vector<UInt_t>();
211  }
212 }
213 
214 const vector<UInt_t>* Backtracker::CalClusterToTracks(const UInt_t& icluster) const {
215  if(CheckRange(fCaloClusterToTracks, icluster)) {
216  return &fCaloClusterToTracks.at(icluster);
217  }
218  else {
219  return new vector<UInt_t>();
220  }
221 }//
222 
223 const vector<UInt_t>* Backtracker::G4PToCalClusters(const UInt_t& ig4p) const {
224  if(CheckRange(fG4PToCaloClusters, ig4p)) {
225  return &fG4PToCaloClusters.at(ig4p);
226  }
227  else {
228  return new vector<UInt_t>();
229  }
230 }//
231 
232 const vector<UInt_t>* Backtracker::CalClusterToG4Ps(const UInt_t& icluster) const {
233  if(CheckRange(fCaloClusterToG4Ps, icluster)) {
234  return &fCaloClusterToG4Ps.at(icluster);
235  }
236  else {
237  return new vector<UInt_t>();
238  }
239 }//
240 
241 //=======================================
243 
244  //cout << "In FillMaps..." << endl;
245  Clear();
246  //GenTree* gen = nullptr;
247  //g4 = nullptr;
248  //DetTree* det = nullptr;
249  //rec = nullptr;
250 
251  //cout << "get trees" << endl;
252  //if(fTM->IsActiveGenTree()) gen = fTM->GetGenTree();
253  if(fTM->IsActiveG4Tree()) g4 = fTM->GetG4Tree();
254  //if(fTM->IsActiveDetTree()) det = fTM->GetDetTree();
255  if(fTM->IsActiveRecTree()) rec = fTM->GetRecoTree();
256 
257  //cout << "checking which are active..." << endl;
258  if(fTM->IsActiveG4Tree()){
259  //cout << "g4Tree is active" << endl;
260 
261  for(UInt_t ig4p=0; ig4p<g4->NSim(); ig4p++) {
262  fG4ParticleToTracks[ig4p] = {};
263  }
264 
265  if(fTM->IsActiveRecTree()){
266  //cout << "recoTree is active" << endl;
267  for(UInt_t itrk = 0; itrk<rec->NTrack(); itrk++ ) {
268 
269  //if(rec->TrackMaxDepositFrac(itrk)<ASSN_THRESHOLD) continue;
270 
271  //vector<UInt_t> tmpindices;
272  //cout << "call GetTrackG4PIndices" << endl;
273  rec->GetTrackG4PIndices(itrk, fTrackToG4Particles[itrk]);
274  //cout << "get match ID" << endl;
275  int matchid = rec->TrackTrkIdMaxDeposit(itrk);
276  //cout << "track matched to " << fTrackToG4Particles[itrk].size() << " G4 particle(s)" << endl;
277 
278  //cout << "point i" << endl;
279  if(rec->TrackMaxDepositFrac(itrk)<ASSN_THRESHOLD) {
280  //cout << "track deposit fraction too low (" << rec->TrackMaxDepositFrac(itrk)
281  // << ")...skipping to next track" << endl;
282  continue;
283  }//should this be considered an association?
284 
285  //cout << "point ii" << endl;
286  for(UInt_t ig4p=0; ig4p<fTrackToG4Particles[itrk].size(); ig4p++){
287  //for(UInt_t ig4p=0; ig4p<tmpindices.size(); ig4p++){
288 
289  if(fG4ParticleToTracks.find(fTrackToG4Particles[itrk][ig4p]) != fG4ParticleToTracks.end()) {
290 
291  //fTrackToG4Particles[itrk][ig4p] = tmpindices[ig4p];
292  //if(fG4ParticleToTracks.find(tmpindices[ig4p]) != fG4ParticleToTracks.end() && //){
293  //if(g4->TrackID(tmpindices[ig4p]) == matchid ) {
294  if(g4->TrackID(fTrackToG4Particles[itrk][ig4p]) == matchid ) {
295  //cout << "found match id" << endl;
296  fTrackToG4Particle[itrk] = ig4p;
297  fG4ParticleToTracks[ig4p].push_back(itrk);
298  break;
299  } //if match id matches g4particle ID
300  }//if G4Particle index is in map
301  }//for all matched particles to this track
302 
303  //cout << "point iii" << endl;
304  // for(UInt_t ig4p=0; ig4p<g4->NSim(); ig4p++) {
305  // if(fG4ParticleToTracks[ig4p].size() == 0) continue;
306  // cout << "G4particle " << ig4p << " matched to " << fG4ParticleToTracks[ig4p].size()
307  // << " reco track(s)" << endl;
308  // }//for all g4Particles
309  }//for tracks
310  }//if recoTree active
311  }//if g4Tree active
312 
313  //cout << "point 1" << endl;
314 
315  if(fTM->IsActiveGenTree()){
316 
317  // cout << "genTree is active" << endl;
318 
319  if(fTM->IsActiveG4Tree()){
320 
321  //G4 particles
322  for(UInt_t ig4 = 0; ig4<g4->NSim(); ig4++ ) {
323  fG4ParticleToGTruth[ig4] = g4->GetTruthIndex(ig4);
324  fGTruthToG4Particles[ fG4ParticleToGTruth[ig4] ].push_back(ig4);
325 
326  //fG4ParticleToFSParticle[ig4] //map< UInt_t, UInt_t >
327  //fFSParticleToG4Particles //map< UInt_t, vector<UInt_t> >
328 
329  }//endfor g4 particles
330  }//endif g4tree
331 
332  // cout << "point 2" << endl;
333 
334  if(fTM->IsActiveRecTree()){
335 
336  //tracks
337  for(UInt_t itrk=0; itrk<rec->NTrack(); itrk++ ) {
338  // cout << "point 2i" << endl;
339  //cout << "fTrackToG4Particles[itrk][0] = " << fTrackToG4Particles[itrk][0] << endl;
340  //cout << "fG4ParticleToGTruth[ fTrackToG4Particles[itrk][0] ] = " << fG4ParticleToGTruth[ fTrackToG4Particles[itrk][0] ] << endl;
341  if(fTrackToG4Particles[itrk].size()==0) continue;
342  fTrackToGTruth[itrk] = fG4ParticleToGTruth[ fTrackToG4Particles[itrk][0] ]; //TODO select truth with most associated energy
343  // cout << "point 2ii" << endl;
344  fGTruthToTracks[ fTrackToGTruth[itrk] ].push_back(itrk);
345  // cout << "point 2iii" << endl;
346  }//endfor tracks
347 
348  // cout << "point 2a" << endl;
349 
350  //vertices
351  for(UInt_t ivtx=0; ivtx<rec->NVertex(); ivtx++ ) {
352  rec->GetVertexTrackIndices(ivtx,fVertexToTracks[ivtx]);
353  for(UInt_t itrk=0; itrk<fVertexToTracks[ivtx].size(); itrk++) {
354  fTrackToVertices[ fVertexToTracks[ivtx][itrk] ].push_back(ivtx);
355 
356  for(UInt_t ig4p=0; ig4p<fTrackToG4Particles[fVertexToTracks[ivtx][itrk]].size(); ig4p++) {
357  fVertexToG4Particles[ivtx].push_back( fTrackToG4Particles[ fVertexToTracks[ivtx][itrk] ][ig4p] );
358  fG4ParticleToVertices[ fVertexToG4Particles[ivtx].back() ].push_back(ivtx);
359  }// for g4Particles
360  }// for tracks
361  // cout << "point 2b" << endl;
362  fVertexToGTruth[ivtx] = fTrackToGTruth[ fVertexToTracks[ivtx][0] ];//TODO you sure all tracks associated with vertex associated with same GTruth?
363  fGTruthToVertex[ fVertexToGTruth[ivtx] ] = ivtx;
364  }//endfor vertices
365 
366  // cout << "point 3" << endl;
367 
368  //vees
369  for(UInt_t ivee=0; ivee<rec->NVee(); ivee++){
370 
371  rec->GetVeeTrackIndices(ivee, fVeeToTracks[ivee]);
372 
373  for(UInt_t itrk=0; itrk<fVeeToTracks[ivee].size(); itrk++){
374 
375  fTrackToVees[ fVeeToTracks[ivee][itrk] ].push_back(ivee);
376 
377  for(UInt_t ig4p=0; ig4p<fTrackToG4Particles[ fVeeToTracks[ivee][itrk] ].size(); ig4p++) {
378  fVeeToG4Particles[ivee].push_back(fTrackToG4Particles[ fVeeToTracks[ivee][itrk] ][ig4p]);
379  fG4ParticleToVee[fVeeToG4Particles[ivee].back()] = ivee;
380  }
381  }// for tracks
382 
383  fVeeToGTruth[ivee] = fTrackToGTruth[ fVeeToTracks[ivee][0] ];//TODO you sure all tracks associated with vee associated with same GTruth?
384  fGTruthToVee[ fVeeToGTruth[ivee] ] = ivee;
385  }//endfor vees
386 
387  // cout << "point 4" << endl;
388 
389  //ECal clusters
390  for(UInt_t iclust=0; iclust<rec->NCalCluster(); iclust++){
391 
392  rec->GetCalClusterTrackIndices(iclust,fCaloClusterToTracks[iclust]);
393 
394  for(UInt_t itrk=0; itrk<fCaloClusterToTracks[iclust].size(); itrk++){
395  fTrackToCaloClusters[ fCaloClusterToTracks[iclust][itrk] ].push_back(iclust);
396  }
397 
398  rec->GetCalClusterG4Indices(iclust,fCaloClusterToG4Ps[iclust]);
399 
400  for(UInt_t ig4p=0; ig4p<fCaloClusterToG4Ps[iclust].size(); ig4p++){
401  fG4PToCaloClusters[ fCaloClusterToG4Ps[iclust][ig4p] ].push_back(iclust);
402  }
403  }//for clusters
404 
405  // cout << "point 5" << endl;
406 
407  }//endif recotree
408  }//if gentree
409 
410  //cout << "at end of FillMaps()" << endl;
411 
412 }//end FillMaps()
413 
414 template <class T>
415 bool Backtracker::CheckRange(const map<UInt_t,T>& m, const UInt_t& i) const {
416 
417  try {
418  if(m.find(i)==m.end())
419  throw(i);
420  return true;
421  }
422  catch(UInt_t i){
423  //cerr << "ERROR Backtracker: map index not found" << endl;
424  return false;
425  }
426 }
427 
429 
430  //gen = nullptr;
431  g4 = nullptr;
432  //det = nullptr;
433  rec = nullptr;
434 
435  fGTruthToG4Particles.clear();
436  fG4ParticleToGTruth.clear();
437  fGTruthToTracks.clear();
438  fTrackToGTruth.clear();
439  fTrackToG4Particles.clear();
440  fTrackToG4Particle.clear();
441  fG4ParticleToTracks.clear();
442  //fFSParticleToG4Particles.clear();
443  //fG4ParticleToFSParticle.clear();
444  fVertexToGTruth.clear();
445  fGTruthToVertex.clear();
446  fG4ParticleToVertices.clear();
447  fVertexToG4Particles.clear();
448  fVertexToGTruth.clear();
449  fGTruthToVertex.clear();
450  fVeeToG4Particles.clear();
451  fG4ParticleToVee.clear();
452  fVeeToGTruth.clear();
453  fGTruthToVee.clear();
454  fTrackToVertices.clear();
455  fVertexToTracks.clear();
456  fVeeToTracks.clear();
457  fTrackToVees.clear();
458  fCaloClusterToTracks.clear();
459  fTrackToCaloClusters.clear();
460  fCaloClusterToG4Ps.clear();
461  fG4PToCaloClusters.clear();
462 }
UInt_t const G4ParticleToVee(const UInt_t &ig4p) const
rec
Definition: tracks.py:88
const vector< UInt_t > * G4ParticleToVertices(const UInt_t &ig4p) const
const vector< pair< UInt_t, float > > TrackToG4ParticlesDeposits(const UInt_t &itrk) const
Definition: Backtracker.cxx:74
UInt_t const G4ParticleToGTruth(const UInt_t &ig4p) const
Definition: Backtracker.cxx:32
STL namespace.
UInt_t const TrackToG4Particle(const UInt_t &itrk) const
Definition: Backtracker.cxx:94
UInt_t const TrackToGTruth(const UInt_t &itrk) const
Definition: Backtracker.cxx:55
UInt_t const VeeToGTruth(const UInt_t &ivee) const
UInt_t const VertexToGTruth(const UInt_t &ivtx) const
const vector< UInt_t > * TrackToG4Particles(const UInt_t &itrk) const
Definition: Backtracker.cxx:65
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
const vector< UInt_t > * VertexToTracks(const UInt_t &ivtx) const
const pair< UInt_t, float > TrackToG4ParticleDeposit(const UInt_t &itrk) const
const vector< UInt_t > * TrackToVees(const UInt_t &itrk) const
const vector< UInt_t > * G4PToCalClusters(const UInt_t &itrk) const
UInt_t const GTruthToVee(const UInt_t &ivee) const
const vector< UInt_t > * TrackToVertices(const UInt_t &itrk) const
const vector< UInt_t > * G4ParticleToTracks(const UInt_t &ig4p) const
Definition: Backtracker.cxx:42
const vector< UInt_t > * CalClusterToTracks(const UInt_t &itrk) const
const vector< UInt_t > * VeeToTracks(const UInt_t &ivee) const
const vector< UInt_t > * VertexToG4Particles(const UInt_t &ivtx) const
ig4ps
Definition: tracks.py:192
UInt_t const GTruthToVertex(const UInt_t &ivtx) const
const vector< UInt_t > * CalClusterToG4Ps(const UInt_t &itrk) const
const vector< UInt_t > * GTruthToG4Particles(const UInt_t &itruth) const
Definition: Backtracker.cxx:13
const vector< UInt_t > * VeeToG4Particles(const UInt_t &ivee) const
const vector< UInt_t > * GTruthToTracks(const UInt_t &itruth) const
Definition: Backtracker.cxx:21
const vector< UInt_t > * TrackToCalClusters(const UInt_t &itrk) const
bool CheckRange(const map< UInt_t, T > &m, const UInt_t &i) const
g4
Definition: tracks.py:87