ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
DerivationFramework::TruthNavigationDecorator Class Reference

#include <TruthNavigationDecorator.h>

Inheritance diagram for DerivationFramework::TruthNavigationDecorator:
Collaboration diagram for DerivationFramework::TruthNavigationDecorator:

Public Member Functions

 TruthNavigationDecorator (const std::string &t, const std::string &n, const IInterface *p)
 
 ~TruthNavigationDecorator ()
 
StatusCode initialize ()
 
virtual StatusCode addBranches () const
 

Private Member Functions

void find_parents (const xAOD::TruthParticle *part, std::vector< ElementLink< xAOD::TruthParticleContainer > > &parents, std::map< int, ElementLink< xAOD::TruthParticleContainer > > &linkMap, std::vector< int > &seen_particles) const
 Helper function for finding all the parents of a particle. More...
 
void find_children (const xAOD::TruthParticle *part, std::vector< ElementLink< xAOD::TruthParticleContainer > > &parents, std::map< int, ElementLink< xAOD::TruthParticleContainer > > &linkMap, std::vector< int > &seen_particles) const
 Helper function for finding all the children of a particle. More...
 

Private Attributes

SG::ReadHandleKeyArray< xAOD::TruthParticleContainerm_inputKeys {this, "InputCollections", {}, "Input truth particle collection keys"}
 Parameter: input particle collections. More...
 
SG::ReadHandleKey< xAOD::TruthEventContainerm_truthEventKey {this, "TruthEventKey", "TruthEvents", "SG key for the TruthEvent container"}
 
SG::WriteDecorHandleKeyArray< xAOD::TruthParticleContainer, std::vector< ElementLink< xAOD::TruthParticleContainer > > > m_parentLinksDecorKeys {this, "DoNotSet_parentDecorKeys", {}, "WriteHandleKeyArray - set internally but must be property"}
 Decor keys. More...
 
SG::WriteDecorHandleKeyArray< xAOD::TruthParticleContainer, std::vector< ElementLink< xAOD::TruthParticleContainer > > > m_childLinksDecorKeys {this, "DoNotSet_childDecorKeys", {}, "WriteHandleKeyArray - set internally but must be property"}
 

Detailed Description

Definition at line 27 of file TruthNavigationDecorator.h.

Constructor & Destructor Documentation

◆ TruthNavigationDecorator()

DerivationFramework::TruthNavigationDecorator::TruthNavigationDecorator ( const std::string &  t,
const std::string &  n,
const IInterface *  p 
)

Definition at line 18 of file TruthNavigationDecorator.cxx.

20  :
21  base_class(t,n,p)
22 {
23 }

◆ ~TruthNavigationDecorator()

DerivationFramework::TruthNavigationDecorator::~TruthNavigationDecorator ( )

Definition at line 26 of file TruthNavigationDecorator.cxx.

26  {
27 }

Member Function Documentation

◆ addBranches()

StatusCode DerivationFramework::TruthNavigationDecorator::addBranches ( ) const
virtual

Definition at line 51 of file TruthNavigationDecorator.cxx.

52 {
53  // Event context
54  const EventContext& ctx = Gaudi::Hive::currentContext();
55 
56  // Retrieve the truth collections
58  if (!truthEvents.isValid()) {
59  ATH_MSG_ERROR("Couldn't retrieve TruthEvent collection with name " << m_truthEventKey);
60  return StatusCode::FAILURE;
61  }
62 
63  // Retrieve all the individual particle collections
64  std::vector<SG::ReadHandle<xAOD::TruthParticleContainer> > inputParticles;
65  inputParticles.reserve(m_inputKeys.size());
67  inputParticles.push_back(SG::ReadHandle<xAOD::TruthParticleContainer>(inputKey, ctx));
68  }
69 
70  // Build a dictionary of uniqueIDs and element links
71  std::map<int,ElementLink<xAOD::TruthParticleContainer> > linkMap;
72  for (auto& coll : inputParticles){
73  for (size_t p=0;p<coll.ptr()->size();++p){
74  if (!coll.ptr()->at(p)) continue; // Protection against null ptrs
75  if (linkMap.find(HepMC::uniqueID(coll.ptr()->at(p))) != linkMap.end()) continue; // Particle in multiple collections
76  linkMap[HepMC::uniqueID(coll.ptr()->at(p))] = ElementLink<xAOD::TruthParticleContainer>(*coll,p);
77  } // Loop over particles in the collection
78  } // Loop over collections
79 
80  // Now loop over the collections and for each one decorate children and parents
81  // The list of particles we keep is small-ish, and the list of particles in the
82  // original truth record is large-ish, so I think it will be more efficient to
83  // do a loop (O(N)) over the big record and a search (O(Nlog(N))) over the small
84  // container. Future performance optimization is welcome...
85 
86  // Keep maps, do the decoration last. This ensures that duplicates all get decorated.
87  std::map< int , std::vector<ElementLink<xAOD::TruthParticleContainer> > > parentMap;
88  std::map< int , std::vector<ElementLink<xAOD::TruthParticleContainer> > > childMap;
89 
90  // Loop protection
91  std::vector<int> seen_particles(20);
92  // As usual, only consider the first truth event
93  const xAOD::TruthEvent * event = truthEvents->at(0);
94  for (size_t p=0;p<event->nTruthParticles();++p){
95  if (!event->truthParticle(p)) continue; // Protection against null ptrs
96  if (linkMap.find(HepMC::uniqueID(event->truthParticle(p))) == linkMap.end()) continue; // Not a particle we are interested in
97  // Make parent and child lists
98  std::vector<ElementLink<xAOD::TruthParticleContainer> > parents;
99  std::vector<ElementLink<xAOD::TruthParticleContainer> > children;
100  // Populate the lists - include loop protection
101  seen_particles.clear();
102  find_parents( event->truthParticle(p) , parents , linkMap , seen_particles );
103  seen_particles.clear();
104  find_children( event->truthParticle(p) , children , linkMap , seen_particles );
105  // Set the maps, so that we can decorate later
106  parentMap[HepMC::uniqueID(event->truthParticle(p))] = parents;
107  childMap[HepMC::uniqueID(event->truthParticle(p))] = children;
108  } // Loop over truth particles in the big truth collection
109 
110  // Now final loop over the collections and setting all the decorators
111  auto parent_decorator = m_parentLinksDecorKeys.makeHandles (ctx);
112  auto child_decorator = m_childLinksDecorKeys.makeHandles (ctx);
113  unsigned int pCntr{0};
114  for (auto coll : inputParticles){
115  if (parent_decorator.at(pCntr).isAvailable()) {
116  ++pCntr;
117  continue;
118  }
119  for (size_t p=0;p<coll.ptr()->size();++p){
120  if (!coll.ptr()->at(p)) continue; // Protection against null ptrs
121  parent_decorator.at(pCntr)(*coll.ptr()->at(p)) = parentMap[ HepMC::uniqueID(coll->at(p)) ];
122  child_decorator.at(pCntr)(*coll.ptr()->at(p)) = childMap[ HepMC::uniqueID(coll->at(p)) ];
123  } // Loop over the particles in each collection
124  ++pCntr;
125  } // Loop over the collections
126 
127  return StatusCode::SUCCESS;
128 }

◆ find_children()

void DerivationFramework::TruthNavigationDecorator::find_children ( const xAOD::TruthParticle part,
std::vector< ElementLink< xAOD::TruthParticleContainer > > &  parents,
std::map< int, ElementLink< xAOD::TruthParticleContainer > > &  linkMap,
std::vector< int > &  seen_particles 
) const
private

Helper function for finding all the children of a particle.

Definition at line 152 of file TruthNavigationDecorator.cxx.

155  {
156  // Null pointer protection
157  if (!part) return;
158  // Check if we've seen the particle before, otherwise add it to our list
159  if (std::find(seen_particles.begin(),seen_particles.end(),HepMC::uniqueID(part)) != seen_particles.end()) return;
160  seen_particles.push_back(HepMC::uniqueID(part));
161  // Look through the children and see if we know about them; otherwise iterate through the list
162  for (size_t child=0;child<part->nChildren();++child){
163  if (!part->child(child)) continue; // Null pointer check
164  if (linkMap.find(HepMC::uniqueID(part->child(child))) != linkMap.end()){
165  // Hit! Add it to the list
166  children.push_back( linkMap[HepMC::uniqueID(part->child(child))] );
167  } else {
168  // Not a hit yet, keep iterating
169  find_children( part->child(child) , children , linkMap , seen_particles );
170  }
171  } // Loop over parents
172 }

◆ find_parents()

void DerivationFramework::TruthNavigationDecorator::find_parents ( const xAOD::TruthParticle part,
std::vector< ElementLink< xAOD::TruthParticleContainer > > &  parents,
std::map< int, ElementLink< xAOD::TruthParticleContainer > > &  linkMap,
std::vector< int > &  seen_particles 
) const
private

Helper function for finding all the parents of a particle.

Definition at line 130 of file TruthNavigationDecorator.cxx.

133  {
134  // Null pointer protection
135  if (!part) return;
136  // Check if we've seen the particle before, otherwise add it to our list
137  if (std::find(seen_particles.begin(), seen_particles.end(), HepMC::uniqueID(part)) != seen_particles.end()) return;
138  seen_particles.push_back(HepMC::uniqueID(part));
139  // Loop through the parents and see if we know about them; otherwise iterate through the list
140  for (size_t parent=0;parent<part->nParents();++parent){
141  if (!part->parent(parent)) continue; // Null pointer check
142  if (linkMap.find(HepMC::uniqueID(part->parent(parent))) != linkMap.end()){
143  // Hit! Add it to the list
144  parents.push_back( linkMap[HepMC::uniqueID(part->parent(parent))] );
145  } else {
146  // Not a hit yet, keep iterating
147  find_parents( part->parent(parent) , parents , linkMap , seen_particles );
148  }
149  } // Loop over parents
150 }

◆ initialize()

StatusCode DerivationFramework::TruthNavigationDecorator::initialize ( )

Definition at line 30 of file TruthNavigationDecorator.cxx.

30  {
31 
32  // Initialise input keys
33  ATH_CHECK( m_inputKeys.initialize() );
34  ATH_CHECK( m_truthEventKey.initialize() );
35 
36  // Decorations - dependent on the name of the input keys
37  // Loop over the container names provided by the user
38  for (auto key : m_inputKeys) {
39  m_parentLinksDecorKeys.emplace_back(key.key()+".parentLinks");
40  m_childLinksDecorKeys.emplace_back(key.key()+".childLinks");
41  }
42 
43  ATH_CHECK( m_parentLinksDecorKeys.initialize() );
44  ATH_CHECK( m_childLinksDecorKeys.initialize() );
45 
46  return StatusCode::SUCCESS;
47 
48 }

Member Data Documentation

◆ m_childLinksDecorKeys

SG::WriteDecorHandleKeyArray<xAOD::TruthParticleContainer, std::vector<ElementLink<xAOD::TruthParticleContainer> > > DerivationFramework::TruthNavigationDecorator::m_childLinksDecorKeys {this, "DoNotSet_childDecorKeys", {}, "WriteHandleKeyArray - set internally but must be property"}
private

Definition at line 43 of file TruthNavigationDecorator.h.

◆ m_inputKeys

SG::ReadHandleKeyArray<xAOD::TruthParticleContainer> DerivationFramework::TruthNavigationDecorator::m_inputKeys {this, "InputCollections", {}, "Input truth particle collection keys"}
private

Parameter: input particle collections.

Definition at line 36 of file TruthNavigationDecorator.h.

◆ m_parentLinksDecorKeys

SG::WriteDecorHandleKeyArray<xAOD::TruthParticleContainer, std::vector<ElementLink<xAOD::TruthParticleContainer> > > DerivationFramework::TruthNavigationDecorator::m_parentLinksDecorKeys {this, "DoNotSet_parentDecorKeys", {}, "WriteHandleKeyArray - set internally but must be property"}
private

Decor keys.

Definition at line 41 of file TruthNavigationDecorator.h.

◆ m_truthEventKey

SG::ReadHandleKey<xAOD::TruthEventContainer> DerivationFramework::TruthNavigationDecorator::m_truthEventKey {this, "TruthEventKey", "TruthEvents", "SG key for the TruthEvent container"}
private

Definition at line 38 of file TruthNavigationDecorator.h.


The documentation for this class was generated from the following files:
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
DerivationFramework::TruthNavigationDecorator::m_truthEventKey
SG::ReadHandleKey< xAOD::TruthEventContainer > m_truthEventKey
Definition: TruthNavigationDecorator.h:39
python.DecayParser.parents
parents
print ("==> buf:",buf)
Definition: DecayParser.py:31
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
SG::ReadHandleKey< xAOD::TruthParticleContainer >
TileDigitizationConfig.inputKey
inputKey
Definition: TileDigitizationConfig.py:103
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
beamspotman.n
n
Definition: beamspotman.py:731
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:116
test_pyathena.parent
parent
Definition: test_pyathena.py:15
xAOD::TruthEvent_v1
Class describing a signal truth event in the MC record.
Definition: TruthEvent_v1.h:35
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework::TruthNavigationDecorator::find_children
void find_children(const xAOD::TruthParticle *part, std::vector< ElementLink< xAOD::TruthParticleContainer > > &parents, std::map< int, ElementLink< xAOD::TruthParticleContainer > > &linkMap, std::vector< int > &seen_particles) const
Helper function for finding all the children of a particle.
Definition: TruthNavigationDecorator.cxx:152
DerivationFramework::TruthNavigationDecorator::m_inputKeys
SG::ReadHandleKeyArray< xAOD::TruthParticleContainer > m_inputKeys
Parameter: input particle collections.
Definition: TruthNavigationDecorator.h:37
python.DecayParser.children
children
Definition: DecayParser.py:32
DerivationFramework::TruthNavigationDecorator::m_childLinksDecorKeys
SG::WriteDecorHandleKeyArray< xAOD::TruthParticleContainer, std::vector< ElementLink< xAOD::TruthParticleContainer > > > m_childLinksDecorKeys
Definition: TruthNavigationDecorator.h:44
DerivationFramework::TruthNavigationDecorator::m_parentLinksDecorKeys
SG::WriteDecorHandleKeyArray< xAOD::TruthParticleContainer, std::vector< ElementLink< xAOD::TruthParticleContainer > > > m_parentLinksDecorKeys
Decor keys.
Definition: TruthNavigationDecorator.h:42
DerivationFramework::TruthNavigationDecorator::find_parents
void find_parents(const xAOD::TruthParticle *part, std::vector< ElementLink< xAOD::TruthParticleContainer > > &parents, std::map< int, ElementLink< xAOD::TruthParticleContainer > > &linkMap, std::vector< int > &seen_particles) const
Helper function for finding all the parents of a particle.
Definition: TruthNavigationDecorator.cxx:130
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37