ATLAS Offline Software
Loading...
Searching...
No Matches
TruthParticle.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// TruthParticle.cxx
7// Implementation file for class TruthParticle
8// Author: S.Binet<binet@cern.ch>
10
11
12// STL includes
13#include <sstream>
14#include <string>
15
16// HepMC includes
18#include "AtlasHepMC/GenEvent.h"
20
21#include "VxVertex/RecVertex.h"
22
23// McParticleEvent includes
26
28// Public methods:
30
31// Constructors
34 INavigable ( ),
35 I4Momentum ( ),
38 m_container ( 0 ),
39 m_mothers ( 0 ),
40 m_children ( 0 ),
41 m_nGenEventIdx ( 0 )
42{}
43
45 IAthenaBarCode ( rhs ),
46 INavigable ( rhs ),
47 I4Momentum ( rhs ),
48 INavigable4Momentum ( rhs ),
49 TruthParticleImpl_t ( rhs ),
51 m_mothers ( rhs.m_mothers ),
52 m_children ( rhs.m_children ),
54{}
55
58 INavigable ( ),
59 I4Momentum ( ),
63 m_mothers ( 0 ),
64 m_children ( 0 ),
65 m_nGenEventIdx ( 0 )
66{
67 setGenParticle( std::move(particle) );
68 m_nGenEventIdx = container ? container->genEventIdx() : 0;
69}
70
72{
73 if ( this != &rhs ) {
74 INavigable::operator=(rhs);
75 I4Momentum::operator=(rhs);
76 INavigable4Momentum::operator=(rhs);
79 m_mothers = rhs.m_mothers;
82 }
83 return *this;
84}
85
87// Const methods:
89
90const TruthParticle * TruthParticle::mother(const std::size_t i) const
91{
92 if ( m_mothers.empty() ||
93 i >= m_mothers.size() ||
94 nullptr == m_container ) return nullptr;
95 return m_container->truthParticle(m_mothers[i], m_nGenEventIdx);
96}
97
98const TruthParticle * TruthParticle::child(const std::size_t i) const
99{
100 if ( m_children.empty() ||
101 i >= m_children.size() ||
102 nullptr == m_container ) return nullptr;
103 return m_container->truthParticle(m_children[i], m_nGenEventIdx);
104}
105
106
108{
109 if ( i < m_mothers.size() ) {
110 auto mother = this->mother(i);
111 return mother ? mother->genParticle() : nullptr;
112 } else {
113 std::string error = "WRONG index for TruthParticle::genMother(index)";
114 throw std::out_of_range(error);
115 }
116}
117
119{
120 if ( i < m_children.size() ) {
121 auto child = this->child(i);
122 return child ? child->genParticle() : nullptr;
123 } else {
124 std::string error = "WRONG index for TruthParticle::genChild(index)";
125 throw std::out_of_range(error);
126 }
127}
128
129bool TruthParticle::hasMother( const int pdgId ) const
130{
131 const std::size_t nMothers = m_mothers.size();
132 for ( std::size_t iMother = 0; iMother != nMothers; ++iMother ) {
133 auto mother = this->mother(iMother);
134 if ( mother && ( pdgId == mother->pdgId() ) ) {
135 return true;
136 }
137 }
138 return false;
139}
140
141bool TruthParticle::hasChild( const int pdgId ) const
142{
143 const std::size_t nChildren = m_children.size();
144 for ( std::size_t iChild = 0; iChild != nChildren; ++iChild ) {
145 auto child = this->child(iChild);
146 if ( child && ( pdgId == child->pdgId() ) ) {
147 return true;
148 }
149 }
150 return false;
151}
152
154 std::vector<unsigned int>& indices ) const
155{
156 bool found = false;
157 const std::size_t nMothers = m_mothers.size();
158 for ( std::size_t iMother = 0; iMother != nMothers; ++iMother ) {
159 auto mother = this->mother(iMother);
160 if ( mother && ( pdgId == mother->pdgId() ) ) {
161 found = true;
162 indices.push_back(iMother);
163 }
164 }
165 return found;
166}
167
169 std::vector<unsigned int>& indices ) const
170{
171 bool found = false;
172 const std::size_t nChildren = m_children.size();
173 for ( std::size_t iChild = 0; iChild != nChildren; ++iChild ) {
174 auto child = this->child(iChild);
175 if ( child && ( pdgId == child->pdgId() ) ) {
176 found = true;
177 indices.push_back(iChild);
178 }
179 }
180 return found;
181}
182
184{
185 return m_container->hasEtIsol( barcode() );
186}
187
189{
190 return m_container->etIsolations( barcode() );
191}
192
193double
195{
196 if ( coneIdx >= 0 && // just to be sure... as it is a signed integer...
198 return m_container->etIsol( barcode(), coneIdx );
199 } else {
200 std::string error = "WRONG index for TruthParticle::etIsol(coneIdx)";
201 throw std::out_of_range(error);
202 }
203}
204
205CLHEP::HepLorentzVector TruthParticle::pDecay( const std::size_t i ) const
206{
207 if ( i < m_children.size() ) {
208 auto child = this->child(i);
209 if ( child ) {
210 return child->hlv();
211 } else {
212 const std::string error = "Caught INVALID pointer to child in TruthParticle::pDecay call!";
213 throw std::out_of_range(error);
214 }
215 } else {
216 std::string error = "Caught INVALID pointer to child in TruthParticle::pDecay call!";
217 throw std::out_of_range(error);
218 }
219}
220
221int TruthParticle::pdgDecay( const std::size_t i ) const
222{
223 if ( i < m_children.size() ) {
224 auto child = this->child(i);
225 if ( child ) {
226 return child->pdgId();
227 } else {
228 const std::string error = "Caught INVALID pointer to child in TruthParticle::pdgDecay call!";
229 throw std::out_of_range(error);
230 }
231 } else {
232 const std::string error = "Caught INVALID pointer to child in TruthParticle::pdgDecay call!";
233 throw std::out_of_range(error);
234 }
235}
236
238// Non-const methods:
241{
242 this->particleBase().setGenParticle( particle );
243
244 if ( particle ) {
245 this->set4Mom(particle->momentum());
246
247 // children
248 auto dcyVtx = particle->end_vertex();
249#ifdef HEPMC3
250 m_children.reserve( dcyVtx ? dcyVtx->particles_out().size() : 0 );
251
252 if ( dcyVtx ) {
253 for ( const auto& itr: dcyVtx->particles_out()) {
254 m_children.push_back( HepMC::barcode(itr) );
255 }//> end loop over outgoing particles
256 }//> decay vertex exists
257
258 // parents
259 auto prodVtx = particle->production_vertex();
260 m_mothers.reserve( prodVtx ? prodVtx->particles_in().size() : 0 );
261 if ( prodVtx ) {
262 for (const auto& itr: prodVtx->particles_in()) {
263 m_mothers.push_back( HepMC::barcode(itr) );
264 }//> end loop over ingoing particles
265 }//> production vertex exists
266#else
267
268 m_children.reserve( dcyVtx ? dcyVtx->particles_out_size() : 0 );
269
270 if ( dcyVtx ) {
271 for ( HepMC::GenVertex::particles_out_const_iterator itr =
272 dcyVtx->particles_out_const_begin();
273 itr != dcyVtx->particles_out_const_end();
274 ++itr ) {
275 m_children.push_back( (*itr)->barcode() );
276 }//> end loop over outgoing particles
277 }//> decay vertex exists
278
279 // parents
280 auto prodVtx = particle->production_vertex();
281 m_mothers.reserve( prodVtx ? prodVtx->particles_in_size() : 0 );
282 if ( prodVtx ) {
283 for ( HepMC::GenVertex::particles_in_const_iterator itr =
284 prodVtx->particles_in_const_begin();
285 itr != prodVtx->particles_in_const_end();
286 ++itr ) {
287 m_mothers.push_back( (*itr)->barcode() );
288 }//> end loop over ingoing particles
289 }//> production vertex exists
290
291#endif
292 } else {
293 m_mothers.resize( 0 );
294 m_children.resize( 0 );
295 }
296}
297
I4Momentum is an abstract base class providing 4-momentum behavior.
Definition I4Momentum.h:31
McAod::EtIsolations EtIsol_t
An array of doubles of fixed size to modelize the Et isolations for different values of isolation rad...
void setGenParticle(HepMC::ConstGenParticlePtr particle)
Fill the data members of ParticleBase from the GenParticle.
std::vector< int > m_children
vector of barcodes of all the children of this particle.
int barcode() const
Forwards the HepMC::GenParticle::barcode() information.
int pdgDecay(const std::size_t i) const
Return the PDG-Id of the i-th child of this particle.
bool hasMother(const int pdgId) const
Return true if the given PDG::id to match can be found within the parents of this TruthParticle.
HepMC::ConstGenParticlePtr genChild(const std::size_t i) const
Retrieve the i-th child (GenParticle) of this TruthParticle.
std::vector< int > m_mothers
vector of barcodes of all the mothers of this particle.
ParticleImpl< TruthParticleNavigation, TruthParticleMomentum, TruthParticleBase > TruthParticleImpl_t
const TruthParticle * mother(const std::size_t i=0) const
Retrieve the i-th mother (TruthParticle) of this TruthParticle.
HepMC::ConstGenParticlePtr genMother(const std::size_t i=0) const
Retrieve the GenParticle mother of this TruthParticle.
const TruthParticleContainer * m_container
Pointer to the collection holding ourself.
void setGenParticle(HepMC::ConstGenParticlePtr particle)
Fill the data members of ParticleBase from the GenParticle.
evtIndex_t m_nGenEventIdx
the index of the GenEvent this particle is in
bool hasEtIsol() const
Return true if one has filled the Et isolation infos for this TruthParticle.
const TruthEtIsolations::EtIsol_t * etIsol() const
Return the transverse energies for all the isolation cones.
TruthParticle()
Default constructor.
const TruthParticle * child(const std::size_t i) const
Retrieve the i-th child (TruthParticle) of this TruthParticle.
TruthParticle & operator=(const TruthParticle &rhs)
Assignment operator.
CLHEP::HepLorentzVector pDecay(const std::size_t i) const
Return the 4-vec of i-th child of this particle.
bool hasChild(const int pdgId) const
Return true if the given PDG::id to match can be found within the children of this TruthParticle.
int barcode(const T *p)
Definition Barcode.h:16
const GenParticle * ConstGenParticlePtr
Definition GenParticle.h:38
ConeSize
Enum for Cone size indexes (for isolation)