Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KFParticle_truthAndDetTools.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file KFParticle_truthAndDetTools.cc
2 
3 #include "KFParticle_Tools.h" // for KFParticle_Tools
4 
5 #include <g4eval/SvtxEvalStack.h> // for SvtxEvalStack
6 #include <g4eval/SvtxTrackEval.h> // for SvtxTrackEval
7 #include <g4eval/SvtxTruthEval.h> // for SvtxTruthEval
8 #include <g4eval/SvtxVertexEval.h> // for SvtxVertexEval
9 
10 #include <globalvertex/SvtxVertex.h> // for SvtxVertex
11 #include <globalvertex/SvtxVertexMap.h> // for SvtxVertexMap, SvtxVer...
12 #include <trackbase/InttDefs.h> // for getLadderPhiId, getLad...
13 #include <trackbase/MvtxDefs.h> // for getChipId, getStaveId
14 #include <trackbase/TpcDefs.h> // for getSectorId, getSide
15 #include <trackbase/TrkrCluster.h> // for TrkrCluster
16 #include <trackbase/TrkrClusterContainer.h> // for TrkrClusterContainer
17 #include <trackbase/TrkrDefs.h> // for getLayer, getTrkrId
19 #include <trackbase_historic/SvtxTrack.h> // for SvtxTrack, SvtxTrack::...
20 #include <trackbase_historic/SvtxTrackMap.h> // for SvtxTrackMap, SvtxTrac...
21 
24 
25 #include <g4main/PHG4Particle.h> // for PHG4Particle
26 #include <g4main/PHG4TruthInfoContainer.h> // for PHG4TruthInfoContainer
27 #include <g4main/PHG4VtxPoint.h> // for PHG4VtxPoint
28 
29 #include <phhepmc/PHHepMCGenEvent.h> // for PHHepMCGenEvent
30 #include <phhepmc/PHHepMCGenEventMap.h> // for PHHepMCGenEventMap
31 
32 #include <phool/PHNodeIterator.h> // for PHNodeIterator
33 #include <phool/getClass.h> // for getClass
34 
35 #include <KFParticle.h> // for KFParticle
36 #include <TString.h> // for TString, operator+
37 #include <TTree.h> // for TTree
38 
39 #pragma GCC diagnostic push
40 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
41 #include <HepMC/GenEvent.h> // for GenEvent::particle_con...
42 #include <HepMC/GenVertex.h> // for GenVertex::particle_it...
43 #pragma GCC diagnostic pop
44 
45 #include <HepMC/GenParticle.h> // for GenParticle
46 #include <HepMC/IteratorRange.h> // for parents
47 #include <HepMC/SimpleVector.h> // for FourVector
48 
49 #include <algorithm> // for max, find
50 #include <cmath> // for pow, sqrt
51 #include <cstdlib> // for NULL, abs
52 #include <iostream> // for operator<<, endl, basi...
53 #include <iterator> // for end, begin
54 #include <map> // for _Rb_tree_iterator, map
55 #include <memory> // for allocator_traits<>::va...
56 #include <utility> // for pair
57 
58 class PHNode;
59 
60 std::map<std::string, int> Use =
61  {
62  {"MVTX", 1},
63  {"INTT", 1},
64  {"TPC", 1},
65  {"EMCAL", 0},
66  {"OHCAL", 0},
67  {"IHCAL", 0}};
68 
70  : m_svtx_evalstack(nullptr)
71 {
72 } // Constructor
73 
75 
77 {
78  SvtxTrack *matched_track = nullptr;
79 
80  for (auto &iter : *trackmap)
81  {
82  if (iter.first == track_id)
83  {
84  matched_track = iter.second;
85  }
86  }
87 
88  return matched_track;
89 }
90 
92 {
93  GlobalVertex *matched_vertex = vertexmap->get(vertex_id);
94 
95  return matched_vertex;
96 }
97 
99 {
100  /*
101  * There are two methods for getting the truth rack from the reco track
102  * 1. (recommended) Use the reco -> truth tables (requires SvtxPHG4ParticleMap). Introduced Summer of 2022
103  * 2. Get truth track via nClusters. Older method and will work with older DSTs
104  */
105 
106  PHG4Particle *particle = nullptr;
107 
108  PHNodeIterator nodeIter(topNode);
109  PHNode *findNode = dynamic_cast<PHNode *>(nodeIter.findFirst("SvtxPHG4ParticleMap"));
110  if (findNode)
111  {
112  findNode = dynamic_cast<PHNode *>(nodeIter.findFirst("G4TruthInfo"));
113  if (findNode)
114  {
115  m_truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
116  }
117  else
118  {
119  std::cout << "KFParticle truth matching: G4TruthInfo does not exist" << std::endl;
120  }
121 
122  SvtxPHG4ParticleMap_v1 *dst_reco_truth_map = findNode::getClass<SvtxPHG4ParticleMap_v1>(topNode, "SvtxPHG4ParticleMap");
123 
124  std::map<float, std::set<int>> truth_set = dst_reco_truth_map->get(thisTrack->get_id());
125  const auto &best_weight = truth_set.rbegin();
126  int best_truth_id = *best_weight->second.rbegin();
127  particle = m_truthinfo->GetParticle(best_truth_id);
128  }
129  else
130  {
131  std::cout << __FILE__ << ": SvtxPHG4ParticleMap not found, reverting to max_truth_particle_by_nclusters()" << std::endl;
132 
133  if (!m_svtx_evalstack)
134  {
135  m_svtx_evalstack = new SvtxEvalStack(topNode);
136  // clustereval = m_svtx_evalstack->get_cluster_eval();
137  // hiteval = m_svtx_evalstack->get_hit_eval();
141  }
142 
143  m_svtx_evalstack->next_event(topNode);
144 
145  particle = trackeval->max_truth_particle_by_nclusters(thisTrack);
146  }
147  return particle;
148 }
149 
150 void KFParticle_truthAndDetTools::initializeTruthBranches(TTree *m_tree, int daughter_id, const std::string &daughter_number, bool m_constrain_to_vertex_truthMatch)
151 {
152  m_tree->Branch(TString(daughter_number) + "_true_ID", &m_true_daughter_id[daughter_id], TString(daughter_number) + "_true_ID/I");
153  if (m_constrain_to_vertex_truthMatch)
154  {
155  m_tree->Branch(TString(daughter_number) + "_true_IP", &m_true_daughter_ip[daughter_id], TString(daughter_number) + "_true_IP/F");
156  m_tree->Branch(TString(daughter_number) + "_true_IP_xy", &m_true_daughter_ip_xy[daughter_id], TString(daughter_number) + "_true_IP_xy/F");
157  }
158  m_tree->Branch(TString(daughter_number) + "_true_px", &m_true_daughter_px[daughter_id], TString(daughter_number) + "_true_px/F");
159  m_tree->Branch(TString(daughter_number) + "_true_py", &m_true_daughter_py[daughter_id], TString(daughter_number) + "_true_py/F");
160  m_tree->Branch(TString(daughter_number) + "_true_pz", &m_true_daughter_pz[daughter_id], TString(daughter_number) + "_true_pz/F");
161  m_tree->Branch(TString(daughter_number) + "_true_p", &m_true_daughter_p[daughter_id], TString(daughter_number) + "_true_p/F");
162  m_tree->Branch(TString(daughter_number) + "_true_pT", &m_true_daughter_pt[daughter_id], TString(daughter_number) + "_true_pT/F");
163  m_tree->Branch(TString(daughter_number) + "_true_EV_x", &m_true_daughter_vertex_x[daughter_id], TString(daughter_number) + "_true_EV_x/F");
164  m_tree->Branch(TString(daughter_number) + "_true_EV_y", &m_true_daughter_vertex_y[daughter_id], TString(daughter_number) + "_true_EV_y/F");
165  m_tree->Branch(TString(daughter_number) + "_true_EV_z", &m_true_daughter_vertex_z[daughter_id], TString(daughter_number) + "_true_EV_z/F");
166  if (m_constrain_to_vertex_truthMatch)
167  {
168  m_tree->Branch(TString(daughter_number) + "_true_PV_x", &m_true_daughter_pv_x[daughter_id], TString(daughter_number) + "_true_PV_x/F");
169  m_tree->Branch(TString(daughter_number) + "_true_PV_y", &m_true_daughter_pv_y[daughter_id], TString(daughter_number) + "_true_PV_y/F");
170  m_tree->Branch(TString(daughter_number) + "_true_PV_z", &m_true_daughter_pv_z[daughter_id], TString(daughter_number) + "_true_PV_z/F");
171  }
172  m_tree->Branch(TString(daughter_number) + "_true_track_history_PDG_ID", &m_true_daughter_track_history_PDG_ID[daughter_id]);
173  m_tree->Branch(TString(daughter_number) + "_true_track_history_PDG_mass", &m_true_daughter_track_history_PDG_mass[daughter_id]);
174  m_tree->Branch(TString(daughter_number) + "_true_track_history_px", &m_true_daughter_track_history_px[daughter_id]);
175  m_tree->Branch(TString(daughter_number) + "_true_track_history_py", &m_true_daughter_track_history_py[daughter_id]);
176  m_tree->Branch(TString(daughter_number) + "_true_track_history_pz", &m_true_daughter_track_history_pz[daughter_id]);
177  m_tree->Branch(TString(daughter_number) + "_true_track_history_pE", &m_true_daughter_track_history_pE[daughter_id]);
178  m_tree->Branch(TString(daughter_number) + "_true_track_history_pT", &m_true_daughter_track_history_pT[daughter_id]);
179 }
180 
181 void KFParticle_truthAndDetTools::fillTruthBranch(PHCompositeNode *topNode, TTree * /*m_tree*/, const KFParticle &daughter, int daughter_id, const KFParticle &kfvertex, bool m_constrain_to_vertex_truthMatch)
182 {
183  float true_px, true_py, true_pz, true_p, true_pt;
184 
185  PHNodeIterator nodeIter(topNode);
186  PHNode *findNode = dynamic_cast<PHNode *>(nodeIter.findFirst(m_trk_map_node_name_nTuple));
187  if (findNode)
188  {
189  dst_trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_trk_map_node_name_nTuple);
190  }
191  else
192  {
193  std::cout << "KFParticle truth matching: " << m_trk_map_node_name_nTuple << " does not exist" << std::endl;
194  }
195  findNode = dynamic_cast<PHNode *>(nodeIter.findFirst(m_vtx_map_node_name_nTuple));
196  if (findNode)
197  {
198  dst_vertexmap = findNode::getClass<SvtxVertexMap>(topNode, m_vtx_map_node_name_nTuple);
199  }
200  else
201  {
202  std::cout << "KFParticle truth matching: " << m_vtx_map_node_name_nTuple << " does not exist" << std::endl;
203  }
204  auto globalvertexmap = findNode::getClass<GlobalVertexMap>(topNode, "GlobalVertexMap");
205  if (!globalvertexmap)
206  {
207  std::cout << "KFParticle truth matching: GlobalVertexMap does not exist" << std::endl;
208  }
209  track = getTrack(daughter.Id(), dst_trackmap);
210  g4particle = getTruthTrack(track, topNode);
211 
212  bool isParticleValid = g4particle == nullptr ? false : true;
213 
214  true_px = isParticleValid ? (Float_t) g4particle->get_px() : 0.;
215  true_py = isParticleValid ? (Float_t) g4particle->get_py() : 0.;
216  true_pz = isParticleValid ? (Float_t) g4particle->get_pz() : 0.;
217  true_p = sqrt(pow(true_px, 2) + pow(true_py, 2) + pow(true_pz, 2));
218  true_pt = sqrt(pow(true_px, 2) + pow(true_py, 2));
219 
220  m_true_daughter_px[daughter_id] = true_px;
221  m_true_daughter_py[daughter_id] = true_py;
222  m_true_daughter_pz[daughter_id] = true_pz;
223  m_true_daughter_p[daughter_id] = true_p;
224  m_true_daughter_pt[daughter_id] = true_pt;
225  m_true_daughter_id[daughter_id] = isParticleValid ? g4particle->get_pid() : 0;
226 
227  if (!m_svtx_evalstack)
228  {
229  m_svtx_evalstack = new SvtxEvalStack(topNode);
230  // clustereval = m_svtx_evalstack->get_cluster_eval();
231  // hiteval = m_svtx_evalstack->get_hit_eval();
235  }
236 
237  if (isParticleValid)
238  {
239  g4vertex_point = trutheval->get_vertex(g4particle);
240  }
241 
242  m_true_daughter_vertex_x[daughter_id] = isParticleValid ? g4vertex_point->get_x() : 0.;
243  m_true_daughter_vertex_y[daughter_id] = isParticleValid ? g4vertex_point->get_y() : 0.;
244  m_true_daughter_vertex_z[daughter_id] = isParticleValid ? g4vertex_point->get_z() : 0.;
245 
246  if (m_constrain_to_vertex_truthMatch)
247  {
248  // Calculate true DCA
249  GlobalVertex *recoVertex = getVertex(kfvertex.Id(), globalvertexmap);
250  auto svtxviter = recoVertex->find_vertexes(GlobalVertex::SVTX);
251  // check that it contains a track vertex
252  if (svtxviter == recoVertex->end_vertexes())
253  {
254  std::cout << "Have a global vertex with no track vertex... shouldn't happen in KFParticle_truthAndDetTools::fillTruthBranch..." << std::endl;
255  }
256 
257  auto svtxvertexvector = svtxviter->second;
258  SvtxVertex *svtxvertex = nullptr;
259 
260  for (auto &vertex : svtxvertexvector)
261  {
262  svtxvertex = dst_vertexmap->find(vertex->get_id())->second;
263  }
264 
265  PHG4VtxPoint *truePoint = vertexeval->max_truth_point_by_ntracks(svtxvertex);
266 
267  if (truePoint == nullptr)
268  {
269  PHG4Particle *g4mother = m_truthinfo->GetPrimaryParticle(g4particle->get_primary_id());
270  truePoint = m_truthinfo->GetVtx(g4mother->get_vtx_id()); // Note, this may not be the PV for a decay with tertiaries
271  }
272 
273  KFParticle trueKFParticleVertex;
274 
275  float f_vertexParameters[6] = {0};
276 
277  if (truePoint == nullptr)
278  {
279  std::cout << "KFParticle truth matching: This event has no PHG4VtxPoint information!\n";
280  std::cout << "Your truth track DCA will be measured wrt a reconstructed vertex!" << std::endl;
281 
282  f_vertexParameters[0] = recoVertex->get_x();
283  f_vertexParameters[1] = recoVertex->get_y();
284  f_vertexParameters[2] = recoVertex->get_z();
285  }
286  else
287  {
288  f_vertexParameters[0] = truePoint->get_x();
289  f_vertexParameters[1] = truePoint->get_y();
290  f_vertexParameters[2] = truePoint->get_z();
291  }
292 
293  float f_vertexCovariance[21] = {0};
294 
295  trueKFParticleVertex.Create(f_vertexParameters, f_vertexCovariance, 0, -1);
296 
297  KFParticle trueKFParticle;
298 
299  float f_trackParameters[6] = {m_true_daughter_vertex_x[daughter_id],
300  m_true_daughter_vertex_y[daughter_id],
301  m_true_daughter_vertex_z[daughter_id],
302  true_px,
303  true_py,
304  true_pz};
305 
306  float f_trackCovariance[21] = {0};
307 
308  trueKFParticle.Create(f_trackParameters, f_trackCovariance, 1, -1);
309 
310  m_true_daughter_ip[daughter_id] = trueKFParticle.GetDistanceFromVertex(trueKFParticleVertex);
311  m_true_daughter_ip_xy[daughter_id] = trueKFParticle.GetDistanceFromVertexXY(trueKFParticleVertex);
312 
313  m_true_daughter_pv_x[daughter_id] = truePoint == nullptr ? -99. : truePoint->get_x();
314  m_true_daughter_pv_y[daughter_id] = truePoint == nullptr ? -99. : truePoint->get_y();
315  m_true_daughter_pv_z[daughter_id] = truePoint == nullptr ? -99. : truePoint->get_z();
316  }
317 }
318 
320 {
321  Float_t pT = sqrt(pow(particle->get_px(), 2) + pow(particle->get_py(), 2));
322 
323  m_true_daughter_track_history_PDG_ID[daughter_id].push_back(particle->get_pid());
324  m_true_daughter_track_history_PDG_mass[daughter_id].push_back(0);
325  m_true_daughter_track_history_px[daughter_id].push_back((Float_t) particle->get_px());
326  m_true_daughter_track_history_py[daughter_id].push_back((Float_t) particle->get_py());
327  m_true_daughter_track_history_pz[daughter_id].push_back((Float_t) particle->get_pz());
328  m_true_daughter_track_history_pE[daughter_id].push_back((Float_t) particle->get_e());
329  m_true_daughter_track_history_pT[daughter_id].push_back((Float_t) pT);
330 }
331 
332 void KFParticle_truthAndDetTools::fillHepMCBranch(HepMC::GenParticle *particle, int daughter_id)
333 {
334  const HepMC::FourVector &myFourVector = particle->momentum();
335 
336  m_true_daughter_track_history_PDG_ID[daughter_id].push_back(particle->pdg_id());
337  m_true_daughter_track_history_PDG_mass[daughter_id].push_back((Float_t) particle->generatedMass());
338  m_true_daughter_track_history_px[daughter_id].push_back((Float_t) myFourVector.px());
339  m_true_daughter_track_history_py[daughter_id].push_back((Float_t) myFourVector.py());
340  m_true_daughter_track_history_pz[daughter_id].push_back((Float_t) myFourVector.pz());
341  m_true_daughter_track_history_pE[daughter_id].push_back((Float_t) myFourVector.e());
342  m_true_daughter_track_history_pT[daughter_id].push_back((Float_t) myFourVector.perp());
343 }
344 
345 int KFParticle_truthAndDetTools::getHepMCInfo(PHCompositeNode *topNode, TTree * /*m_tree*/, const KFParticle &daughter, int daughter_id)
346 {
347  // Make dummy particle for null pointers and missing nodes
348  HepMC::GenParticle *dummyParticle = new HepMC::GenParticle();
349  HepMC::FourVector dummyFourVector(0, 0, 0, 0);
350  dummyParticle->set_momentum(dummyFourVector);
351  dummyParticle->set_pdg_id(0);
352  dummyParticle->set_generated_mass(0.);
353 
354  PHNodeIterator nodeIter(topNode);
355  PHNode *findNode = dynamic_cast<PHNode *>(nodeIter.findFirst(m_trk_map_node_name_nTuple));
356  if (findNode)
357  {
358  dst_trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_trk_map_node_name_nTuple);
359  }
360  else
361  {
362  std::cout << "KFParticle truth matching: " << m_trk_map_node_name_nTuple << " does not exist" << std::endl;
363  }
364 
365  track = getTrack(daughter.Id(), dst_trackmap);
366  g4particle = getTruthTrack(track, topNode);
367 
368  bool isParticleValid = g4particle == nullptr ? false : true;
369 
370  if (!isParticleValid)
371  {
372  std::cout << "KFParticle truth matching: this track is a ghost" << std::endl;
373  fillHepMCBranch(dummyParticle, daughter_id);
374  return 0;
375  }
376 
377  m_geneventmap = findNode::getClass<PHHepMCGenEventMap>(topNode, "PHHepMCGenEventMap");
378  if (!m_geneventmap)
379  {
380  std::cout << "KFParticle truth matching: Missing node PHHepMCGenEventMap" << std::endl;
381  std::cout << "You will have no mother information" << std::endl;
382  fillHepMCBranch(dummyParticle, daughter_id);
383  return 0;
384  }
385 
386  m_genevt = m_geneventmap->get(1);
387  if (!m_genevt)
388  {
389  std::cout << "KFParticle truth matching: Missing node PHHepMCGenEvent" << std::endl;
390  std::cout << "You will have no mother information" << std::endl;
391  fillHepMCBranch(dummyParticle, daughter_id);
392  return 0;
393  }
394 
395  // Start by looking for our particle in the Geant record
396  // Any decay that Geant4 handles will not be in the HepMC record
397  // This can happen if you limit the decay volume in the generator
398  if (g4particle->get_parent_id() != 0)
399  {
400  findNode = dynamic_cast<PHNode *>(nodeIter.findFirst("G4TruthInfo"));
401  if (findNode)
402  {
403  m_truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
404  }
405  else
406  {
407  std::cout << "KFParticle truth matching: G4TruthInfo does not exist" << std::endl;
408  }
409  while (g4particle->get_parent_id() != 0)
410  {
411  g4particle = m_truthinfo->GetParticle(g4particle->get_parent_id());
412  fillGeant4Branch(g4particle, daughter_id);
413  }
414  }
415 
416  HepMC::GenEvent *theEvent = m_genevt->getEvent();
417  HepMC::GenParticle *prevParticle = nullptr;
418 
419  // int forbiddenPDGIDs[] = {21, 22}; //Stop tracing history when we reach quarks, gluons and photons
420  int forbiddenPDGIDs[] = {0}; // 20230921 - Request made to have gluon information to see about gluon-splitting
421 
422  for (HepMC::GenEvent::particle_const_iterator p = theEvent->particles_begin(); p != theEvent->particles_end(); ++p)
423  {
424  if (((*p)->barcode() == g4particle->get_barcode()))
425  {
426  prevParticle = *p;
427  while (!prevParticle->is_beam())
428  {
429  bool breakOut = false;
430  for (HepMC::GenVertex::particle_iterator mother = prevParticle->production_vertex()->particles_begin(HepMC::parents);
431  mother != prevParticle->production_vertex()->particles_end(HepMC::parents); ++mother)
432  {
433  if (std::find(std::begin(forbiddenPDGIDs), std::end(forbiddenPDGIDs),
434  abs((*mother)->pdg_id())) != std::end(forbiddenPDGIDs))
435  {
436  breakOut = true;
437  break;
438  }
439 
440  fillHepMCBranch((*mother), daughter_id);
441  prevParticle = *mother;
442  }
443  if (breakOut)
444  {
445  break;
446  }
447  }
448  }
449  }
450 
451  return 0;
452 } // End of function
453 
454 void KFParticle_truthAndDetTools::initializeCaloBranches(TTree *m_tree, int daughter_id, const std::string &daughter_number)
455 {
456  m_tree->Branch(TString(daughter_number) + "_EMCAL_DeltaPhi", &detector_emcal_deltaphi[daughter_id], TString(daughter_number) + "_EMCAL_DeltaPhi/F");
457  m_tree->Branch(TString(daughter_number) + "_EMCAL_DeltaEta", &detector_emcal_deltaeta[daughter_id], TString(daughter_number) + "_EMCAL_DeltaEta/F");
458  m_tree->Branch(TString(daughter_number) + "_EMCAL_energy_3x3", &detector_emcal_energy_3x3[daughter_id], TString(daughter_number) + "_EMCAL_energy_3x3/F");
459  m_tree->Branch(TString(daughter_number) + "_EMCAL_energy_5x5", &detector_emcal_energy_5x5[daughter_id], TString(daughter_number) + "_EMCAL_energy_5x5/F");
460  m_tree->Branch(TString(daughter_number) + "_EMCAL_energy_cluster", &detector_emcal_cluster_energy[daughter_id], TString(daughter_number) + "_EMCAL_energy_cluster/F");
461  m_tree->Branch(TString(daughter_number) + "_IHCAL_DeltaPhi", &detector_ihcal_deltaphi[daughter_id], TString(daughter_number) + "_IHCAL_DeltaPhi/F");
462  m_tree->Branch(TString(daughter_number) + "_IHCAL_DeltaEta", &detector_ihcal_deltaeta[daughter_id], TString(daughter_number) + "_IHCAL_DeltaEta/F");
463  m_tree->Branch(TString(daughter_number) + "_IHCAL_energy_3x3", &detector_ihcal_energy_3x3[daughter_id], TString(daughter_number) + "_IHCAL_energy_3x3/F");
464  m_tree->Branch(TString(daughter_number) + "_IHCAL_energy_5x5", &detector_ihcal_energy_5x5[daughter_id], TString(daughter_number) + "_IHCAL_energy_5x5/F");
465  m_tree->Branch(TString(daughter_number) + "_IHCAL_energy_cluster", &detector_ihcal_cluster_energy[daughter_id], TString(daughter_number) + "_IHCAL_energy_cluster/F");
466  m_tree->Branch(TString(daughter_number) + "_OHCAL_DeltaPhi", &detector_ohcal_deltaphi[daughter_id], TString(daughter_number) + "_OHCAL_DeltaEta/F");
467  m_tree->Branch(TString(daughter_number) + "_OHCAL_DeltaEta", &detector_ohcal_deltaeta[daughter_id], TString(daughter_number) + "_OHCAL_DeltaEta/F");
468  m_tree->Branch(TString(daughter_number) + "_OHCAL_energy_3x3", &detector_ohcal_energy_3x3[daughter_id], TString(daughter_number) + "_OHCAL_energy_3x3/F");
469  m_tree->Branch(TString(daughter_number) + "_OHCAL_energy_5x5", &detector_ohcal_energy_5x5[daughter_id], TString(daughter_number) + "_OHCAL_energy_5x5/F");
470  m_tree->Branch(TString(daughter_number) + "_OHCAL_energy_cluster", &detector_ohcal_cluster_energy[daughter_id], TString(daughter_number) + "_OHCAL_energy_cluster/F");
471 }
472 
474  TTree * /*m_tree*/, const KFParticle &daughter, int daughter_id)
475 {
476  PHNodeIterator nodeIter(topNode);
477  PHNode *findNode = dynamic_cast<PHNode *>(nodeIter.findFirst(m_trk_map_node_name_nTuple));
478  if (findNode)
479  {
480  dst_trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_trk_map_node_name_nTuple);
481  }
482  else
483  {
484  std::cout << "KFParticle truth matching: " << m_trk_map_node_name_nTuple << " does not exist" << std::endl;
485  }
486 
487  track = getTrack(daughter.Id(), dst_trackmap);
488 
494 
500 
506 }
507 
508 void KFParticle_truthAndDetTools::initializeDetectorBranches(TTree *m_tree, int daughter_id, const std::string &daughter_number)
509 {
510  m_tree->Branch(TString(daughter_number) + "_local_x", &detector_local_x[daughter_id]);
511  m_tree->Branch(TString(daughter_number) + "_local_y", &detector_local_y[daughter_id]);
512  m_tree->Branch(TString(daughter_number) + "_local_z", &detector_local_z[daughter_id]);
513  m_tree->Branch(TString(daughter_number) + "_layer", &detector_layer[daughter_id]);
514 
515  for (auto const &subdetector : Use)
516  {
517  if (subdetector.second)
518  {
519  initializeSubDetectorBranches(m_tree, subdetector.first, daughter_id, daughter_number);
520  }
521  }
522 }
523 
524 void KFParticle_truthAndDetTools::initializeSubDetectorBranches(TTree *m_tree, const std::string &detectorName, int daughter_id, const std::string &daughter_number)
525 {
526  if (detectorName == "MVTX")
527  {
528  m_tree->Branch(TString(daughter_number) + "_" + TString(detectorName) + "_staveID", &mvtx_staveID[daughter_id]);
529  m_tree->Branch(TString(daughter_number) + "_" + TString(detectorName) + "_chipID", &mvtx_chipID[daughter_id]);
530  }
531  if (detectorName == "INTT")
532  {
533  m_tree->Branch(TString(daughter_number) + "_" + TString(detectorName) + "_ladderZID", &intt_ladderZID[daughter_id]);
534  m_tree->Branch(TString(daughter_number) + "_" + TString(detectorName) + "_ladderPhiID", &intt_ladderPhiID[daughter_id]);
535  }
536  if (detectorName == "TPC")
537  {
538  m_tree->Branch(TString(daughter_number) + "_" + TString(detectorName) + "_sectorID", &tpc_sectorID[daughter_id]);
539  m_tree->Branch(TString(daughter_number) + "_" + TString(detectorName) + "_side", &tpc_side[daughter_id]);
540  }
541 }
542 
544  TTree * /*m_tree*/, const KFParticle &daughter, int daughter_id)
545 {
546  PHNodeIterator nodeIter(topNode);
547 
548  PHNode *findNode = dynamic_cast<PHNode *>(nodeIter.findFirst(m_trk_map_node_name_nTuple));
549  if (findNode)
550  {
551  dst_trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_trk_map_node_name_nTuple);
552  }
553  else
554  {
555  std::cout << "KFParticle truth matching: " << m_trk_map_node_name_nTuple << " does not exist" << std::endl;
556  }
557 
558  findNode = dynamic_cast<PHNode *>(nodeIter.findFirst("TRKR_CLUSTER"));
559  if (findNode)
560  {
561  dst_clustermap = findNode::getClass<TrkrClusterContainer>(topNode, "TRKR_CLUSTER");
562  }
563  else
564  {
565  std::cout << "KFParticle detector info: TRKR_CLUSTER does not exist" << std::endl;
566  }
567 
568  track = getTrack(daughter.Id(), dst_trackmap);
569 
571  iter != track->end_cluster_keys();
572  ++iter)
573  {
574  TrkrDefs::cluskey clusKey = *iter;
575  TrkrCluster *cluster = dst_clustermap->findCluster(clusKey);
576  const unsigned int trkrId = TrkrDefs::getTrkrId(clusKey);
577 
578  detector_local_x[daughter_id].push_back(cluster->getX());
579  detector_local_y[daughter_id].push_back(cluster->getY());
580  detector_local_z[daughter_id].push_back(cluster->getZ());
581  detector_layer[daughter_id].push_back(TrkrDefs::getLayer(clusKey));
582  unsigned int staveId, chipId, ladderZId, ladderPhiId, sectorId, side;
583  staveId = chipId = ladderZId = ladderPhiId = sectorId = side = -99;
584 
585  if (Use["MVTX"] && trkrId == TrkrDefs::mvtxId)
586  {
587  staveId = MvtxDefs::getStaveId(clusKey);
588  chipId = MvtxDefs::getChipId(clusKey);
589  }
590  else if (Use["INTT"] && trkrId == TrkrDefs::inttId)
591  {
592  ladderZId = InttDefs::getLadderZId(clusKey);
593  ladderPhiId = InttDefs::getLadderPhiId(clusKey);
594  }
595  else if (Use["TPC"] && trkrId == TrkrDefs::tpcId)
596  {
597  sectorId = TpcDefs::getSectorId(clusKey);
598  side = TpcDefs::getSide(clusKey);
599  }
600 
601  mvtx_staveID[daughter_id].push_back(staveId);
602  mvtx_chipID[daughter_id].push_back(chipId);
603  intt_ladderZID[daughter_id].push_back(ladderZId);
604  intt_ladderPhiID[daughter_id].push_back(ladderPhiId);
605  tpc_sectorID[daughter_id].push_back(sectorId);
606  tpc_side[daughter_id].push_back(side);
607  }
608 }
609 
611  TTree * /*m_tree*/,
612  const KFParticle &motherParticle,
613  std::vector<KFParticle> daughters,
614  std::vector<KFParticle> intermediates)
615 {
617  std::vector<KFParticle> primaryVertices = kfpTupleTools.makeAllPrimaryVertices(topNode, m_vtx_map_node_name_nTuple);
618 
619  for (auto &primaryVertice : primaryVertices)
620  {
621  allPV_x.push_back(primaryVertice.GetX());
622  allPV_y.push_back(primaryVertice.GetY());
623  allPV_z.push_back(primaryVertice.GetZ());
624 
625  allPV_mother_IP.push_back(motherParticle.GetDistanceFromVertex(primaryVertice));
626  allPV_mother_IPchi2.push_back(motherParticle.GetDeviationFromVertex(primaryVertice));
627 
628  for (unsigned int j = 0; j < daughters.size(); ++j)
629  {
630  allPV_daughter_IP[j].push_back(daughters[j].GetDistanceFromVertex(primaryVertice));
631  allPV_daughter_IPchi2[j].push_back(daughters[j].GetDeviationFromVertex(primaryVertice));
632  }
633 
634  for (unsigned int j = 0; j < intermediates.size(); ++j)
635  {
636  allPV_intermediates_IP[j].push_back(intermediates[j].GetDistanceFromVertex(primaryVertice));
637  allPV_intermediates_IPchi2[j].push_back(intermediates[j].GetDeviationFromVertex(primaryVertice));
638  }
639  }
640 }
641 
643 {
644  for (int i = 0; i < m_num_tracks_nTuple; ++i)
645  {
646  // Truth vectors
654 
655  // Detector vectors
656  detector_local_x[i].clear();
657  detector_local_y[i].clear();
658  detector_local_z[i].clear();
659  detector_layer[i].clear();
660  mvtx_staveID[i].clear();
661  mvtx_chipID[i].clear();
662  intt_ladderZID[i].clear();
663  intt_ladderPhiID[i].clear();
664  tpc_sectorID[i].clear();
665  tpc_side[i].clear();
666 
667  // PV vectors
668  allPV_daughter_IP[i].clear();
669  allPV_daughter_IPchi2[i].clear();
670  }
671 
672  allPV_x.clear();
673  allPV_y.clear();
674  allPV_z.clear();
675  allPV_z.clear();
676 
677  allPV_mother_IP.clear();
678  allPV_mother_IPchi2.clear();
679 
680  for (int i = 0; i < m_num_intermediate_states_nTuple; ++i)
681  {
682  allPV_intermediates_IP[i].clear();
683  allPV_intermediates_IPchi2[i].clear();
684  }
685 }