ATLAS Offline Software
Loading...
Searching...
No Matches
TruthDecayCollectionMaker.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// TruthDecayCollectionMaker.cxx
7// Removes all truth particles/vertices which do not pass a user-defined cut
8// Based on TruthCollectionMaker (but simpler)
9
20
22
23// STL includes
24#include <vector>
25#include <string>
26// For a find in the vector
27#include <algorithm>
28
29// Athena initialize
31{
32 ATH_MSG_VERBOSE("initialize() ...");
33
34 // Input truth particles
35 ATH_CHECK( m_particlesKey.initialize() );
36 ATH_MSG_INFO("Using " << m_particlesKey.key() << " as the input truth container key");
37
38 // Accessors (ReadDecorHandleKeys) TODO Convert these to SG::ConstAccessor?
39 ATH_CHECK(m_originAccessorKey.initialize());
40 ATH_CHECK(m_typeAccessorKey.initialize());
41 ATH_CHECK(m_outcomeAccessorKey.initialize());
43
44 // Output particle/vertex containers
45 ATH_CHECK(m_outputParticlesKey.initialize());
46 ATH_MSG_INFO("New truth particles container key: " << m_outputParticlesKey.key() );
47 ATH_CHECK(m_outputVerticesKey.initialize());
48 ATH_MSG_INFO("New truth vertices container key: " << m_outputVerticesKey.key() );
49
50 if (m_pdgIdsToKeep.empty() && !m_keepBHadrons && !m_keepCHadrons && !m_keepBSM) {
51 ATH_MSG_FATAL("No PDG IDs provided, not keeping b- or c-hadrons or BSM particles -- what do you want?");
52 return StatusCode::FAILURE;
53 }
54
55 // Decorators TODO Convert these to SG::Accessor?
56 ATH_CHECK(m_originDecoratorKey.initialize());
57 ATH_CHECK(m_typeDecoratorKey.initialize());
58 ATH_CHECK(m_outcomeDecoratorKey.initialize());
62
63 return StatusCode::SUCCESS;
64}
65
66
67// Selection and collection creation
68StatusCode DerivationFramework::TruthDecayCollectionMaker::addBranches(const EventContext& ctx) const
69{
70 // Event context for AthenaMT
71
72 // Retrieve truth collections
74 if (!truthParticles.isValid()) {
75 ATH_MSG_ERROR("Couldn't retrieve TruthParticle collection with name " << m_particlesKey);
76 return StatusCode::FAILURE;
77 }
78
79 // Create the new particle containers and WriteHandles
81 ATH_CHECK(newParticlesWriteHandle.record(std::make_unique<xAOD::TruthParticleContainer>(),
82 std::make_unique<xAOD::TruthParticleAuxContainer>()));
83 ATH_MSG_DEBUG( "Recorded new TruthParticleContainer with key: " << (m_outputParticlesKey.key()));
84 // Create the new vertex containers and WriteHandles
86 ATH_CHECK(newVerticesWriteHandle.record(std::make_unique<xAOD::TruthVertexContainer>(),
87 std::make_unique<xAOD::TruthVertexAuxContainer>()));
88 ATH_MSG_DEBUG( "Recorded new TruthVertexContainer with key: " << (m_outputVerticesKey.key()));
89
90 // List of unique IDs for particles in our collection already. Because of the way we recurse,
91 // adding more particles as we go, there should be no need to add (or help from adding) the
92 // unique IDs of particles that we are *not* going to keep
93 std::vector<int> seen_particles;
94 // Go through that list of particles!
95 for (const auto * part : *truthParticles){
96 // If this passes my cuts, keep it
97 if (id_ok(*part)){
98 addTruthParticle( ctx, *part, newParticlesWriteHandle.ptr(), newVerticesWriteHandle.ptr(), seen_particles , m_generations );
99 }
100 } // Loop over the initial truth particle collection
101 return StatusCode::SUCCESS;
102}
103
105 const xAOD::TruthParticle& old_part,
108 std::vector<int>& seen_particles,
109 const int generations) const {
110 // See if we've seen it - note, could also do this with a unary function on the container itself
111 if (std::find(seen_particles.begin(),seen_particles.end(),HepMC::uniqueID(&old_part))!=seen_particles.end()){
112 for (size_t p=0;p<part_cont->size();++p){
113 // Was it a hit?
114 const xAOD::TruthParticle *theParticle = (*part_cont)[p];
115 if (HepMC::is_same_particle(theParticle,&old_part)) return p;
116 } // Look through the old container
117 } // Found it in the old container
118 // Now we have seen it
119 seen_particles.push_back(HepMC::uniqueID(&old_part));
120 // Set up decorators
127 // Make a truth particle and add it to the container
128 xAOD::TruthParticle* xTruthParticle = new xAOD::TruthParticle();
129 part_cont->push_back( xTruthParticle );
130 // Fill with numerical content
131 xTruthParticle->setPdgId(old_part.pdgId());
132 xTruthParticle->setUid(HepMC::uniqueID(&old_part));
133 xTruthParticle->setStatus(old_part.status());
134 xTruthParticle->setM(old_part.m());
135 xTruthParticle->setPx(old_part.px());
136 xTruthParticle->setPy(old_part.py());
137 xTruthParticle->setPz(old_part.pz());
138 xTruthParticle->setE(old_part.e());
139 // Copy over the polarization information if it's there
140 if (old_part.polarization().valid()){
143 }
144 // Make a link to this particle
145 int my_index = part_cont->size()-1;
146 ElementLink<xAOD::TruthParticleContainer> eltp(*part_cont, my_index);
147 // Decay vertex information
148 if (old_part.hasDecayVtx()) {
149 int vert_index = addTruthVertex( ctx, *old_part.decayVtx(), part_cont, vert_cont, seen_particles, generations);
150 ElementLink<xAOD::TruthVertexContainer> eltv( *vert_cont, vert_index );
151 xTruthParticle->setDecayVtxLink( eltv );
152 (*vert_cont)[vert_index]->addIncomingParticleLink( eltp );
153 }
154 // Copy over the decorations if they are available
159
160 typeDecorator(*xTruthParticle) = classifierParticleTypeAcc(old_part);
161 originDecorator(*xTruthParticle) = classifierParticleOriginAcc(old_part);
162 outcomeDecorator(*xTruthParticle) = classifierParticleOutcomeAcc(old_part);
163 classificationDecorator(*xTruthParticle) = ClassificationAcc(old_part);
164
165 // Return a link to this particle
166 return my_index;
167}
168
170 const xAOD::TruthVertex& old_vert,
173 std::vector<int>& seen_particles,
174 const int generations) const {
175 // Make a new vertex and add it to the container
176 xAOD::TruthVertex* xTruthVertex = new xAOD::TruthVertex();
177 vert_cont->push_back( xTruthVertex );
178 // Get a link to this vertex -- will be used to set production vertices on all the next particles
179 int my_index = vert_cont->size()-1;
180 ElementLink<xAOD::TruthVertexContainer> eltv(*vert_cont, my_index);
181 // Set properties
182 xTruthVertex->setStatus(HepMC::status(old_vert));
183 xTruthVertex->setUid(HepMC::uniqueID(old_vert));
184 xTruthVertex->setX(old_vert.x());
185 xTruthVertex->setY(old_vert.y());
186 xTruthVertex->setZ(old_vert.z());
187 xTruthVertex->setT(old_vert.t());
188 // If we are done, then stop here
189 if (generations==0) return my_index;
190 // Add all the outgoing particles
191 for (size_t n=0;n<old_vert.nOutgoingParticles();++n){
192 if (!old_vert.outgoingParticle(n)) continue; // Just in case we removed some truth particles, e.g. G4 decays
193 if (m_rejectHadronChildren && old_vert.outgoingParticle(n)->isHadron()) { // Option to skip hadrons outright{
194 continue;
195 }
196 // Continue on the next generation; note that we only decrement the generation if this particle doesn't also pass our cuts
197 int part_index = addTruthParticle( ctx, *old_vert.outgoingParticle(n), part_cont, vert_cont, seen_particles,
198 generations-1+(id_ok(*old_vert.outgoingParticle(n))?1:0) );
199 ElementLink<xAOD::TruthParticleContainer> eltp( *part_cont, part_index);
200 xTruthVertex->addOutgoingParticleLink( eltp );
201 (*part_cont)[part_index]->setProdVtxLink( eltv );
202 }
203 // Return a link to this vertex
204 return my_index;
205}
206
208{
209 // Check list of PDG IDs to keep
210 for (int id : m_pdgIdsToKeep){
211 if (part.absPdgId()==id){
212 return true;
213 } // Found a particle of interest!
214 } // Loop over the PDG IDs we want to keep
215 // Also check functions for B/C/BSM
216 return (m_keepBHadrons && part.isBottomHadron()) ||
217
218 (m_keepCHadrons && part.isCharmHadron()) ||
219
220 (m_keepBSM && part.isBSM());
221}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
Helper class to provide constant type-safe access to aux data.
ATLAS-specific HepMC functions.
Handle class for reading a decoration on an object.
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
Handle class for recording to StoreGate.
value_type push_back(value_type pElem)
Add an element to the end of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_daughterIDDecoratorKey
SG::WriteHandleKey< xAOD::TruthVertexContainer > m_outputVerticesKey
virtual StatusCode addBranches(const EventContext &ctx) const override final
Gaudi::Property< bool > m_keepCHadrons
< Option to keep all c-hadrons (better than giving PDG IDs)
Gaudi::Property< std::vector< int > > m_pdgIdsToKeep
< List of PDG IDs to build this collection from
int addTruthVertex(const EventContext &, const xAOD::TruthVertex &old_vert, xAOD::TruthParticleContainer *part_cont, xAOD::TruthVertexContainer *vert_cont, std::vector< int > &seen_particles, const int generations=-1) const
Gaudi::Property< bool > m_rejectHadronChildren
< Option to reject hadron descendants
Gaudi::Property< int > m_generations
^^^^ These should be replaced by SG::Accessor.
SG::WriteHandleKey< xAOD::TruthParticleContainer > m_outputParticlesKey
int addTruthParticle(const EventContext &ctx, const xAOD::TruthParticle &old_part, xAOD::TruthParticleContainer *part_cont, xAOD::TruthVertexContainer *vert_cont, std::vector< int > &seen_particles, const int generations=-1) const
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_outcomeDecoratorKey
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer > m_outcomeAccessorKey
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_motherIDDecoratorKey
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer > m_classificationAccessorKey
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer > m_originAccessorKey
Gaudi::Property< bool > m_keepBHadrons
< Option to keep all b-hadrons (better than giving PDG IDs)
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_particlesKey
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer > m_typeAccessorKey
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_typeDecoratorKey
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_originDecoratorKey
FIXME Using WriteDecorHandles for decorations on a Container created in the current algorithm is unne...
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_classificationDecoratorKey
bool id_ok(const xAOD::TruthParticle &part) const
Gaudi::Property< bool > m_keepBSM
< Option to keep all BSM particles (better than giving PDG IDs)
Handle class for reading a decoration on an object.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Handle class for adding a decoration to an object.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
pointer_type ptr()
Dereference the pointer.
void setPy(float value)
Set the y component of the particle's momentum.
int status() const
Status code.
void setUid(int value)
Set unique ID.
bool isHadron() const
Whether the particle is a hadron.
void setM(float value)
Also store the mass.
virtual double m() const override final
The mass of the particle.
void setE(float value)
Set the energy of the particle.
int pdgId() const
PDG ID code.
const TruthVertex_v1 * decayVtx() const
The decay vertex of this particle.
Polarization polarization() const
Retrieve a full Polarization with a single call.
void setPz(float value)
Set the z component of the particle's momentum.
float px() const
The x component of the particle's momentum.
bool hasDecayVtx() const
Check for a decay vertex on this particle.
virtual double e() const override final
The total energy of the particle.
bool setPolarizationParameter(float value, PolParam parameter)
Set method for polarization parameter values.
void setDecayVtxLink(const ElementLink< TruthVertexContainer > &link)
Set the decay vertex of the particle.
float py() const
The y component of the particle's momentum.
void setStatus(int value)
Set status code.
void setPdgId(int pid)
Set PDG ID code.
void setPx(float value)
Set the x component of the particle's momentum.
bool polarizationParameter(float &value, PolParam parameter) const
Accessor for polarization parameters.
float pz() const
The z component of the particle's momentum.
@ polarizationPhi
Polarization in ( )
@ polarizationTheta
Polarization in ( )
const TruthParticle_v1 * outgoingParticle(size_t index) const
Get one of the outgoing particles.
void addOutgoingParticleLink(const TPLink_t &link)
Add one outgoing particle.
float z() const
Vertex longitudinal distance along the beam line form the origin.
void setStatus(int value)
Set the vertex status.
void setZ(float value)
Set the vertex's longitudinal distance from the origin.
float y() const
Vertex y displacement.
void setUid(int value)
Set the vertex unique ID.
float t() const
Vertex time.
size_t nOutgoingParticles() const
Get the number of outgoing particles.
void setT(float value)
Set the vertex time.
float x() const
Vertex x displacement.
void setX(float value)
Set the x displacement of the vertex.
void setY(float value)
Set the y displacement of the vertex.
int uniqueID(const T &p)
int status(const T &p)
bool is_same_particle(const T1 &p1, const T2 &p2)
Method to establish if two particles in the GenEvent actually represent the same particle.
TruthVertex_v1 TruthVertex
Typedef to implementation.
Definition TruthVertex.h:15
TruthVertexContainer_v1 TruthVertexContainer
Declare the latest version of the truth vertex container.
TruthParticle_v1 TruthParticle
Typedef to implementation.
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.
bool valid() const
Check if the stored values are valid.