ATLAS Offline Software
TruthNavigationDecorator.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // TruthNavigationDecorator.cxx
7 // Add navigation information to small truth collections
8 
11 #include <algorithm> //for std::find
12 
13 // Constructor
15  const std::string& n,
16  const IInterface* p ) :
17  base_class(t,n,p)
18 {
19 }
20 
21 // Destructor
23 }
24 
25 // Initialise
27 
28  // Initialise input keys
29  ATH_CHECK( m_inputKeys.initialize() );
30  ATH_CHECK( m_truthEventKey.initialize() );
31 
32  // Decorations - dependent on the name of the input keys
33  // Loop over the container names provided by the user
34  for (const auto& key : m_inputKeys) {
35  m_parentLinksDecorKeys.emplace_back(key.key()+".parentLinks");
36  m_childLinksDecorKeys.emplace_back(key.key()+".childLinks");
37  }
38 
39  ATH_CHECK( m_parentLinksDecorKeys.initialize() );
40  ATH_CHECK( m_childLinksDecorKeys.initialize() );
41 
42  return StatusCode::SUCCESS;
43 
44 }
45 
46 // Function to do dressing, implements interface in IAugmentationTool
48 {
49  // Event context
50 
51  // Retrieve the truth collections
52  SG::ReadHandle<xAOD::TruthEventContainer> truthEvents(m_truthEventKey, ctx);
53  if (!truthEvents.isValid()) {
54  ATH_MSG_ERROR("Couldn't retrieve TruthEvent collection with name " << m_truthEventKey);
55  return StatusCode::FAILURE;
56  }
57 
58  // Retrieve all the individual particle collections
59  std::vector<SG::ReadHandle<xAOD::TruthParticleContainer> > inputParticles;
60  inputParticles.reserve(m_inputKeys.size());
62  inputParticles.push_back(SG::ReadHandle<xAOD::TruthParticleContainer>(inputKey, ctx));
63  }
64 
65  // Build a dictionary of uniqueIDs and element links
66  std::map<int,ElementLink<xAOD::TruthParticleContainer> > linkMap;
67  for (auto& coll : inputParticles){
68  for (size_t p=0;p<coll.ptr()->size();++p){
69  if (!coll.ptr()->at(p)) continue; // Protection against null ptrs
70  if (linkMap.find(HepMC::uniqueID(coll.ptr()->at(p))) != linkMap.end()) continue; // Particle in multiple collections
71  linkMap[HepMC::uniqueID(coll.ptr()->at(p))] = ElementLink<xAOD::TruthParticleContainer>(*coll,p);
72  } // Loop over particles in the collection
73  } // Loop over collections
74 
75  // Now loop over the collections and for each one decorate children and parents
76  // The list of particles we keep is small-ish, and the list of particles in the
77  // original truth record is large-ish, so I think it will be more efficient to
78  // do a loop (O(N)) over the big record and a search (O(Nlog(N))) over the small
79  // container. Future performance optimization is welcome...
80 
81  // Keep maps, do the decoration last. This ensures that duplicates all get decorated.
82  std::map< int , std::vector<ElementLink<xAOD::TruthParticleContainer> > > parentMap;
83  std::map< int , std::vector<ElementLink<xAOD::TruthParticleContainer> > > childMap;
84 
85  // Loop protection
86  std::vector<int> seen_particles(20);
87  // As usual, only consider the first truth event
88  const xAOD::TruthEvent * event = truthEvents->at(0);
89  for (size_t p=0;p<event->nTruthParticles();++p){
90  if (!event->truthParticle(p)) continue; // Protection against null ptrs
91  if (linkMap.find(HepMC::uniqueID(event->truthParticle(p))) == linkMap.end()) continue; // Not a particle we are interested in
92  // Make parent and child lists
93  std::vector<ElementLink<xAOD::TruthParticleContainer> > parents;
94  std::vector<ElementLink<xAOD::TruthParticleContainer> > children;
95  // Populate the lists - include loop protection
96  seen_particles.clear();
97  find_parents( event->truthParticle(p) , parents , linkMap , seen_particles );
98  seen_particles.clear();
99  find_children( event->truthParticle(p) , children , linkMap , seen_particles );
100  // Set the maps, so that we can decorate later
101  parentMap[HepMC::uniqueID(event->truthParticle(p))] = std::move(parents);
102  childMap[HepMC::uniqueID(event->truthParticle(p))] = std::move(children);
103  } // Loop over truth particles in the big truth collection
104 
105  // Now final loop over the collections and setting all the decorators
106  auto parent_decorator = m_parentLinksDecorKeys.makeHandles (ctx);
107  auto child_decorator = m_childLinksDecorKeys.makeHandles (ctx);
108  unsigned int pCntr{0};
109  for (auto coll : inputParticles){
110  if (parent_decorator.at(pCntr).isAvailable()) {
111  ++pCntr;
112  continue;
113  }
114  for (size_t p=0;p<coll.ptr()->size();++p){
115  if (!coll.ptr()->at(p)) continue; // Protection against null ptrs
116  parent_decorator.at(pCntr)(*coll.ptr()->at(p)) = parentMap[ HepMC::uniqueID(coll->at(p)) ];
117  child_decorator.at(pCntr)(*coll.ptr()->at(p)) = childMap[ HepMC::uniqueID(coll->at(p)) ];
118  } // Loop over the particles in each collection
119  ++pCntr;
120  } // Loop over the collections
121 
122  return StatusCode::SUCCESS;
123 }
124 
127  std::map<int,ElementLink<xAOD::TruthParticleContainer> >& linkMap ,
128  std::vector<int>& seen_particles ) const {
129  // Null pointer protection
130  if (!part) return;
131  // Check if we've seen the particle before, otherwise add it to our list
132  if (std::find(seen_particles.begin(), seen_particles.end(), HepMC::uniqueID(part)) != seen_particles.end()) return;
133  seen_particles.push_back(HepMC::uniqueID(part));
134  // Loop through the parents and see if we know about them; otherwise iterate through the list
135  for (size_t parent=0;parent<part->nParents();++parent){
136  if (!part->parent(parent)) continue; // Null pointer check
137  if (linkMap.find(HepMC::uniqueID(part->parent(parent))) != linkMap.end()){
138  // Hit! Add it to the list
139  parents.push_back( linkMap[HepMC::uniqueID(part->parent(parent))] );
140  } else {
141  // Not a hit yet, keep iterating
142  find_parents( part->parent(parent) , parents , linkMap , seen_particles );
143  }
144  } // Loop over parents
145 }
146 
149  std::map<int,ElementLink<xAOD::TruthParticleContainer> >& linkMap ,
150  std::vector<int>& seen_particles ) const {
151  // Null pointer protection
152  if (!part) return;
153  // Check if we've seen the particle before, otherwise add it to our list
154  if (std::find(seen_particles.begin(),seen_particles.end(),HepMC::uniqueID(part)) != seen_particles.end()) return;
155  seen_particles.push_back(HepMC::uniqueID(part));
156  // Look through the children and see if we know about them; otherwise iterate through the list
157  for (size_t child=0;child<part->nChildren();++child){
158  if (!part->child(child)) continue; // Null pointer check
159  if (linkMap.find(HepMC::uniqueID(part->child(child))) != linkMap.end()){
160  // Hit! Add it to the list
161  children.push_back( linkMap[HepMC::uniqueID(part->child(child))] );
162  } else {
163  // Not a hit yet, keep iterating
164  find_children( part->child(child) , children , linkMap , seen_particles );
165  }
166  } // Loop over parents
167 }
DerivationFramework::TruthNavigationDecorator::initialize
StatusCode initialize()
Definition: TruthNavigationDecorator.cxx:26
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
python.DecayParser.parents
parents
print ("==> buf:",buf)
Definition: DecayParser.py:30
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
SG::ReadHandleKey< xAOD::TruthParticleContainer >
DerivationFramework::TruthNavigationDecorator::addBranches
virtual StatusCode addBranches(const EventContext &ctx) const
Definition: TruthNavigationDecorator.cxx:47
TileDigitizationConfig.inputKey
inputKey
Definition: TileDigitizationConfig.py:107
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
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:727
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
DerivationFramework::TruthNavigationDecorator::TruthNavigationDecorator
TruthNavigationDecorator(const std::string &t, const std::string &n, const IInterface *p)
Definition: TruthNavigationDecorator.cxx:14
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:147
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
TruthNavigationDecorator.h
MagicNumbers.h
DerivationFramework::TruthNavigationDecorator::~TruthNavigationDecorator
~TruthNavigationDecorator()
Definition: TruthNavigationDecorator.cxx:22
python.DecayParser.children
children
Definition: DecayParser.py:31
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
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:125
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37