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