ATLAS Offline Software
TrackTruthLookup.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "TrackTruthLookup.h"
7 
8 namespace IDPVM {
9 // Utility function to get the linked truth particle from a track.
10 // Taken directly from InDetPhysValLargeD0Tool.cxx
11  const xAOD::TruthParticle*
13  using ElementTruthLink_t = ElementLink<xAOD::TruthParticleContainer>;
14  const xAOD::TruthParticle* result(nullptr);
15 
16  // Check whether truthParticleLink exists.
17  static const SG::ConstAccessor<ElementTruthLink_t> truthParticleLinkAcc("truthParticleLink");
18  if (truthParticleLinkAcc.isAvailable(track)) {
19  // If so, get linked xAOD::TruthParticle.
20  const ElementTruthLink_t ptruthContainer =
21  truthParticleLinkAcc(track);
22  if (ptruthContainer.isValid()) {
23  result = *ptruthContainer;
24  }
25  }
26  return result;
27  }
28 
29  std::vector<const xAOD::TrackParticle*>
31  // C++ try-catch blocks are zero-cost if no exception is thrown, so
32  // performance should be unaffected by this check.
33  try {
34  return m_mapTruth.at(truth);
35  } catch (const std::out_of_range& oor) {
36  throw std::out_of_range(
37  "Truth particle was not found in lookup map. Did you remember to call TrackTruthLookup::cache?");
38  }
39  }
40 
41  const xAOD::TruthParticle*
43  // C++ try-catch blocks are zero-cost if no exception is thrown, so
44  // performance should be unaffected by this check.
45  try {
46  return m_mapTrack.at(track);
47  } catch (const std::out_of_range& oor) {
48  throw std::out_of_range(
49  "Track particle was not found in lookup map. Did you remember to call TrackTruthLookup::cache?");
50  }
51  }
52 
53  void
55  // Cache all track particles.
56  for (const xAOD::TrackParticle* track : *trackParticles) {
57  const xAOD::TruthParticle* truth = getTruthPointer(*track);
58 
59  // Store pointer, even if null.
60  m_mapTrack[track] = truth;
61 
62  // Store link in reverse direction as well, to avoid O(N^2) complexity.
63  if (truth) {
64  if (!contains(truth)) {
65  // New truth particle; initialise vector.
66  m_mapTruth[truth] = {
67  track
68  };
69  } else {
70  // Existing truth particle; append vector.
71  m_mapTruth[truth].push_back(track);
72  }
73  }
74  }
75  }
76 
77  void
79  // Cache remaining truth particles.
80  for (const xAOD::TruthParticle* truth : *truthParticles) {
81  if (!contains(truth)) {
82  m_mapTruth[truth] = {};
83  }
84  }
85  }
86 
87  void
88  TrackTruthLookup::cacheTruth(const std::vector<const xAOD::TruthParticle*>* truthParticlesVec) {
89  // Cache remaining truth particles.
90  for (const xAOD::TruthParticle* truth : *truthParticlesVec) {
91  if (!contains(truth)) {
92  m_mapTruth[truth] = {};
93  }
94  }
95  }
96 
97  void
99  const xAOD::TruthParticleContainer* truthParticles) {
100  // Clear existing cache.
101  clear();
102  cacheTracks(trackParticles);
103  cacheTruth(truthParticles);
104  }
105 
106  void
108  const std::vector<const xAOD::TruthParticle*>* truthParticlesVec) {
109  // Clear existing cache.
110  clear();
111  cacheTracks(trackParticles);
112  cacheTruth(truthParticlesVec);
113  }
114 } // namespace
IDPVM::TrackTruthLookup::m_mapTruth
std::unordered_map< const xAOD::TruthParticle *, std::vector< const xAOD::TrackParticle * > > m_mapTruth
Definition: TrackTruthLookup.h:155
IDPVM
Class to retrieve associated truth from a track, implementing a cached response.
Definition: InDetPhysValMonitoringTool.h:55
IDPVM::TrackTruthLookup::getTruth
const xAOD::TruthParticle * getTruth(const xAOD::TrackParticle *track) const
Accessor to get the unique xAOD::TruthParticle associated with 'track', or a nullptr is none is assoc...
Definition: TrackTruthLookup.cxx:42
get_generator_info.result
result
Definition: get_generator_info.py:21
TrackTruthLookup.h
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
IDPVM::TrackTruthLookup::clear
void clear()
Clears the contents of the unordered map data members.
Definition: TrackTruthLookup.h:136
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
IDPVM::getTruthPointer
const xAOD::TruthParticle * getTruthPointer(const xAOD::TrackParticle &track)
Definition: TrackTruthLookup.cxx:12
DataVector< xAOD::TrackParticle_v1 >
IDPVM::TrackTruthLookup::getTracks
std::vector< const xAOD::TrackParticle * > getTracks(const xAOD::TruthParticle *truth) const
Accessor to get the vector of xAOD::TrackParticles associated with 'truth', possibly empty if none is...
Definition: TrackTruthLookup.cxx:30
IDPVM::TrackTruthLookup::cache
void cache(const xAOD::TrackParticleContainer *trackParticles, const std::vector< const xAOD::TruthParticle * > *truthParticlesVec)
Cache using a vector of TruthParticles, for compatibility with datatype returned from the xAOD::Truth...
Definition: TrackTruthLookup.cxx:107
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
IDPVM::TrackTruthLookup::m_mapTrack
std::unordered_map< const xAOD::TrackParticle *, const xAOD::TruthParticle * > m_mapTrack
Data member(s).
Definition: TrackTruthLookup.h:150
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
IDPVM::TrackTruthLookup::cacheTruth
void cacheTruth(const xAOD::TruthParticleContainer *truthParticles)
Definition: TrackTruthLookup.cxx:78
IDPVM::TrackTruthLookup::cacheTracks
void cacheTracks(const xAOD::TrackParticleContainer *trackParticles)
Definition: TrackTruthLookup.cxx:54
IDPVM::TrackTruthLookup::contains
bool contains(const xAOD::TruthParticle *truth) const
Returns true if the Lookup contains the pointer 'truth'.
Definition: TrackTruthLookup.h:124