ATLAS Offline Software
Public Member Functions | List of all members
IDPVM::TrackTruthLookup Class Reference

Class for providing fast lookup of linked tracks and truth particles. More...

#include <TrackTruthLookup.h>

Collaboration diagram for IDPVM::TrackTruthLookup:

Public Member Functions

 TrackTruthLookup ()
 Constructor(s). More...
 
 TrackTruthLookup (const xAOD::TrackParticleContainer *trackParticles, const xAOD::TruthParticleContainer *truthParticles)
 
 TrackTruthLookup (const xAOD::TrackParticleContainer *trackParticles, const std::vector< const xAOD::TruthParticle * > *truthParticlesVec)
 
 ~TrackTruthLookup ()
 Destructor. More...
 

CacheMethods

Methods for initial cache generation of links between associated tracks and truth particles in the provided containers.

std::unordered_map< const xAOD::TrackParticle *, const xAOD::TruthParticle * > m_mapTrack
 Data member(s). More...
 
std::unordered_map< const xAOD::TruthParticle *, std::vector< const xAOD::TrackParticle * > > m_mapTruth
 
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::TruthEvent::truthParticleLinks() More...
 
void cache (const xAOD::TrackParticleContainer *trackParticles, const xAOD::TruthParticleContainer *truthParticles)
 Generate cache from usual evtStore() retrieved container pointers. More...
 
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 associated. More...
 
const xAOD::TruthParticlegetTruth (const xAOD::TrackParticle *track) const
 Accessor to get the unique xAOD::TruthParticle associated with 'track', or a nullptr is none is associated. More...
 
bool contains (const xAOD::TruthParticle *truth) const
 Returns true if the Lookup contains the pointer 'truth'. More...
 
bool contains (const xAOD::TrackParticle *track) const
 Returns true if the Lookup contains the pointer 'track'. More...
 
void clear ()
 Clears the contents of the unordered map data members. More...
 
void cacheTracks (const xAOD::TrackParticleContainer *trackParticles)
 
void cacheTruth (const xAOD::TruthParticleContainer *truthParticles)
 
void cacheTruth (const std::vector< const xAOD::TruthParticle * > *truthParticlesVec)
 

Detailed Description

Class for providing fast lookup of linked tracks and truth particles.

Intended to be used to avoid nested loops when matching tracks and truth particles, e.g. when computing efficiencies.

The links are stored in two unordered maps (single caching call per event, with O(N) complexity), emulating a bidirectional[1] map with O(1) look-up complexity. Tracks in the provided container are guaranteed to exist in the corresponding map, but may map to a nullptr if no link to a truth particle exists. Conversely, every truth particle is guaranteed to exist in the corresponding map, but may map to an empty vector if no link to a track exists.

Use, e.g. in the InDetPhysValLargeD0Tool, like:

[InDetPhysValMonitoring/InDetPhysValLargeD0Tool.h]
    #include "src/TrackTruthLookup.h"
    ...
    private:
        std::unique_ptr<IDPVM::TrackTruthLookup> m_lookup;
    ...

[src/InDetPhysValLargeD0Tool.cxx]
    InDetPhysValLargeD0Tool::initialize() {
        ...
        m_lookup = std::move(std::unique_ptr<IDPVM::TrackTruthLookup>(new IDPVM::TrackTruthLookup()));
        ...
    }
    ...
    InDetPhysValLargeD0Tool::fillHistograms() {
        ...
        m_lookup->cache(ptracks, truthParticles);
        ...
        for (const auto &thisTruth : *truthParticles) {
            for (const auto& track :  m_lookup->getTracks(thisTruth)) {
                // Only looping explicitly associated tracks, hence
                // O(N) complexity rather than O(N^2).
            }
        }
        ...
    }

[1] It is not exactly bidirectional, since the mapping track <-> truth is not exactly bijective: tracks are injective (one-to-one) on the set of truth particles but one truth particle may (although rarely) be associated to more than one track.

Definition at line 74 of file TrackTruthLookup.h.

Constructor & Destructor Documentation

◆ TrackTruthLookup() [1/3]

IDPVM::TrackTruthLookup::TrackTruthLookup ( )
inline

Constructor(s).

Definition at line 77 of file TrackTruthLookup.h.

77  {
78  }

◆ TrackTruthLookup() [2/3]

IDPVM::TrackTruthLookup::TrackTruthLookup ( const xAOD::TrackParticleContainer trackParticles,
const xAOD::TruthParticleContainer truthParticles 
)
inline

Definition at line 80 of file TrackTruthLookup.h.

81  {
82  cache(trackParticles, truthParticles);
83  }

◆ TrackTruthLookup() [3/3]

IDPVM::TrackTruthLookup::TrackTruthLookup ( const xAOD::TrackParticleContainer trackParticles,
const std::vector< const xAOD::TruthParticle * > *  truthParticlesVec 
)
inline

Definition at line 85 of file TrackTruthLookup.h.

86  {
87  cache(trackParticles, truthParticlesVec);
88  }

◆ ~TrackTruthLookup()

IDPVM::TrackTruthLookup::~TrackTruthLookup ( )
inline

Destructor.

Definition at line 91 of file TrackTruthLookup.h.

91  {
92  }

Member Function Documentation

◆ cache() [1/2]

void IDPVM::TrackTruthLookup::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::TruthEvent::truthParticleLinks()

Definition at line 107 of file TrackTruthLookup.cxx.

108  {
109  // Clear existing cache.
110  clear();
111  cacheTracks(trackParticles);
112  cacheTruth(truthParticlesVec);
113  }

◆ cache() [2/2]

void IDPVM::TrackTruthLookup::cache ( const xAOD::TrackParticleContainer trackParticles,
const xAOD::TruthParticleContainer truthParticles 
)

Generate cache from usual evtStore() retrieved container pointers.

Definition at line 98 of file TrackTruthLookup.cxx.

99  {
100  // Clear existing cache.
101  clear();
102  cacheTracks(trackParticles);
103  cacheTruth(truthParticles);
104  }

◆ cacheTracks()

void IDPVM::TrackTruthLookup::cacheTracks ( const xAOD::TrackParticleContainer trackParticles)
private

Definition at line 54 of file TrackTruthLookup.cxx.

54  {
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  }

◆ cacheTruth() [1/2]

void IDPVM::TrackTruthLookup::cacheTruth ( const std::vector< const xAOD::TruthParticle * > *  truthParticlesVec)
private

Definition at line 88 of file TrackTruthLookup.cxx.

88  {
89  // Cache remaining truth particles.
90  for (const xAOD::TruthParticle* truth : *truthParticlesVec) {
91  if (!contains(truth)) {
92  m_mapTruth[truth] = {};
93  }
94  }
95  }

◆ cacheTruth() [2/2]

void IDPVM::TrackTruthLookup::cacheTruth ( const xAOD::TruthParticleContainer truthParticles)
private

Definition at line 78 of file TrackTruthLookup.cxx.

78  {
79  // Cache remaining truth particles.
80  for (const xAOD::TruthParticle* truth : *truthParticles) {
81  if (!contains(truth)) {
82  m_mapTruth[truth] = {};
83  }
84  }
85  }

◆ clear()

void IDPVM::TrackTruthLookup::clear ( )
inline

Clears the contents of the unordered map data members.

Definition at line 136 of file TrackTruthLookup.h.

136  {
137  m_mapTrack.clear();
138  m_mapTruth.clear();
139  return;
140  }

◆ contains() [1/2]

bool IDPVM::TrackTruthLookup::contains ( const xAOD::TrackParticle track) const
inline

Returns true if the Lookup contains the pointer 'track'.

Definition at line 130 of file TrackTruthLookup.h.

130  {
131  return m_mapTrack.find(track) != m_mapTrack.end();
132  }

◆ contains() [2/2]

bool IDPVM::TrackTruthLookup::contains ( const xAOD::TruthParticle truth) const
inline

Returns true if the Lookup contains the pointer 'truth'.

Definition at line 124 of file TrackTruthLookup.h.

124  {
125  return m_mapTruth.find(truth) != m_mapTruth.end();
126  }

◆ getTracks()

std::vector< const xAOD::TrackParticle * > IDPVM::TrackTruthLookup::getTracks ( const xAOD::TruthParticle truth) const

Accessor to get the vector of xAOD::TrackParticles associated with 'truth', possibly empty if none is associated.

Throws out_of_range exception if truth particle does not exist in cache.

Definition at line 30 of file TrackTruthLookup.cxx.

30  {
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  }

◆ getTruth()

const xAOD::TruthParticle * IDPVM::TrackTruthLookup::getTruth ( const xAOD::TrackParticle track) const

Accessor to get the unique xAOD::TruthParticle associated with 'track', or a nullptr is none is associated.

Throws out_of_range exception if track does not exist in cache.

Definition at line 42 of file TrackTruthLookup.cxx.

42  {
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  }

Member Data Documentation

◆ m_mapTrack

std::unordered_map<const xAOD::TrackParticle*, const xAOD::TruthParticle*> IDPVM::TrackTruthLookup::m_mapTrack
private

Data member(s).

Definition at line 150 of file TrackTruthLookup.h.

◆ m_mapTruth

std::unordered_map<const xAOD::TruthParticle*, std::vector<const xAOD::TrackParticle*> > IDPVM::TrackTruthLookup::m_mapTruth
private

Definition at line 155 of file TrackTruthLookup.h.


The documentation for this class was generated from the following files:
IDPVM::TrackTruthLookup::m_mapTruth
std::unordered_map< const xAOD::TruthParticle *, std::vector< const xAOD::TrackParticle * > > m_mapTruth
Definition: TrackTruthLookup.h:155
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
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
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
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