ATLAS Offline Software
TruthParticle.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 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
17 #include "AtlasHepMC/GenParticle.h"
18 #include "AtlasHepMC/GenEvent.h"
19 #include "AtlasHepMC/GenVertex.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 ),
50  m_container ( rhs.m_container ),
51  m_mothers ( rhs.m_mothers ),
52  m_children ( rhs.m_children ),
53  m_nGenEventIdx ( rhs.m_nGenEventIdx )
54 {}
55 
57  const TruthParticleContainer * container ) :
58  INavigable ( ),
59  I4Momentum ( ),
62  m_container ( container ),
63  m_mothers ( 0 ),
64  m_children ( 0 ),
65  m_nGenEventIdx ( 0 )
66 {
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;
80  m_children = rhs.m_children;
82  }
83  return *this;
84 }
85 
87 // Const methods:
89 
90 const 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;
96 }
97 
98 const 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;
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 
129 bool 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 
141 bool 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 
153 bool TruthParticle::hasMother( const int pdgId,
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 
168 bool TruthParticle::hasChild( const int pdgId,
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 
193 double
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 
205 CLHEP::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 
221 int 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 
RecVertex.h
I4Momentum
Definition: I4Momentum.h:31
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
TruthParticleParameters::NbrOfCones
@ NbrOfCones
Definition: TruthParticleParamDefs.h:31
GenEvent.h
ParticleImpl< TruthParticleNavigation, TruthParticleMomentum, TruthParticleBase >::pdgId
virtual int pdgId() const
Return enum indicating particle id the enum file is available in Event/EventKernel/PdtPdg....
Definition: ParticleImpl.h:738
TruthParticle::hasEtIsol
bool hasEtIsol() const
Return true if one has filled the Et isolation infos for this TruthParticle.
Definition: TruthParticle.cxx:183
TruthParticleContainer.h
TruthParticle::pDecay
CLHEP::HepLorentzVector pDecay(const std::size_t i) const
Return the 4-vec of i-th child of this particle.
Definition: TruthParticle.cxx:205
TruthParticle::barcode
int barcode() const
Forwards the HepMC::GenParticle::barcode() information.
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:310
TruthParticle::genChild
HepMC::ConstGenParticlePtr genChild(const std::size_t i) const
Retrieve the i-th child (GenParticle) of this TruthParticle.
Definition: TruthParticle.cxx:118
Trk::indices
std::pair< long int, long int > indices
Definition: AlSymMatBase.h:24
GenVertex.h
TruthParticle::mother
const TruthParticle * mother(const std::size_t i=0) const
Retrieve the i-th mother (TruthParticle) of this TruthParticle.
Definition: TruthParticle.cxx:90
TruthParticle
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:58
TruthEtIsolations::EtIsol_t
McAod::EtIsolations EtIsol_t
An array of doubles of fixed size to modelize the Et isolations for different values of isolation rad...
Definition: TruthEtIsolations.h:52
TruthParticleContainer::genEventIdx
std::size_t genEventIdx() const
Retrieve the index (within a McEventCollection) of the HepMC::GenEvent this TruthParticleContainer is...
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:243
TruthParticleParameters::ConeSize
ConeSize
Enum for Cone size indexes (for isolation)
Definition: TruthParticleParamDefs.h:20
TruthParticle::hasMother
bool hasMother(const int pdgId) const
Return true if the given PDG::id to match can be found within the parents of this TruthParticle.
Definition: TruthParticle.cxx:129
TruthParticle::genParticle
HepMC::ConstGenParticlePtr genParticle() const
Retrieve the GenParticle this TruthParticle has been made from (if any)
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:285
TruthParticleContainer
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:42
GenParticle.h
ParticleImpl< TruthParticleNavigation, TruthParticleMomentum, TruthParticleBase >::operator=
ParticleImpl & operator=(const ParticleImpl &rhs)
Assignment operator.
Definition: ParticleImpl.h:356
ParticleImpl< TruthParticleNavigation, TruthParticleMomentum, TruthParticleBase >
TruthParticle::setGenParticle
void setGenParticle(HepMC::ConstGenParticlePtr particle)
Fill the data members of ParticleBase from the GenParticle.
Definition: TruthParticle.cxx:240
lumiFormat.i
int i
Definition: lumiFormat.py:85
TruthParticle::m_children
std::vector< int > m_children
vector of barcodes of all the children of this particle.
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:266
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
TruthParticle::genMother
HepMC::ConstGenParticlePtr genMother(const std::size_t i=0) const
Retrieve the GenParticle mother of this TruthParticle.
Definition: TruthParticle.cxx:107
TruthParticleContainer::etIsol
double etIsol(const int barcode, const TruthParticleParameters::ConeSize coneIdx) const
Return the Et isolation for a given particle and a given cone size.
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:281
TruthParticle::hasChild
bool hasChild(const int pdgId) const
Return true if the given PDG::id to match can be found within the children of this TruthParticle.
Definition: TruthParticle.cxx:141
TruthParticle::etIsol
const TruthEtIsolations::EtIsol_t * etIsol() const
Return the transverse energies for all the isolation cones.
Definition: TruthParticle.cxx:188
INavigable
Definition: INavigable.h:18
IAthenaBarCode
Definition: AthenaKernel/AthenaKernel/IAthenaBarCode.h:48
ParticleImpl::hlv
virtual CLHEP::HepLorentzVector hlv() const
CLHEP HepLorentzVector.
Definition: ParticleImpl.h:635
TruthParticle.h
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
TruthParticle::operator=
TruthParticle & operator=(const TruthParticle &rhs)
Assignment operator.
Definition: TruthParticle.cxx:71
TruthParticleContainer::truthParticle
const TruthParticle * truthParticle(const int barcode, std::size_t genEventIdx=0) const
return a const pointer to an TruthParticle given the barcode of the HepMC::GenParticle it is wrapping
Definition: TruthParticleContainer.cxx:64
ParticleImpl< TruthParticleNavigation, TruthParticleMomentum, TruthParticleBase >::particleBase
const particle_type & particleBase() const
access to underlying base type (IParticle-like)
Definition: ParticleImpl.h:403
TruthParticle::m_container
const TruthParticleContainer * m_container
Pointer to the collection holding ourself.
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:254
TruthParticleBase::setGenParticle
void setGenParticle(HepMC::ConstGenParticlePtr particle)
Fill the data members of ParticleBase from the GenParticle.
Definition: TruthParticleBase.h:192
TruthParticle::pdgDecay
int pdgDecay(const std::size_t i) const
Return the PDG-Id of the i-th child of this particle.
Definition: TruthParticle.cxx:221
TruthParticle::m_mothers
std::vector< int > m_mothers
vector of barcodes of all the mothers of this particle.
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:260
TruthParticle::child
const TruthParticle * child(const std::size_t i) const
Retrieve the i-th child (TruthParticle) of this TruthParticle.
Definition: TruthParticle.cxx:98
TruthParticle::m_nGenEventIdx
evtIndex_t m_nGenEventIdx
the index of the GenEvent this particle is in
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:269
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
INavigable4Momentum
Definition: INavigable4Momentum.h:21
TruthParticleContainer::etIsolations
const TruthEtIsolations::EtIsol_t * etIsolations(const int barcode) const
return the container of Truth Et isolations for a given TruthParticle (or a HepMC::GenParticle)
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:267
TruthParticle::set4Mom
void set4Mom(const HepMC::FourVector &hlv)
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:228
error
Definition: IImpactPoint3dEstimator.h:70
TruthParticleContainer::hasEtIsol
bool hasEtIsol() const
tell if this TruthParticleContainer has been registered with a TruthEtIsolations container
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:255
TruthParticle::TruthParticle
TruthParticle()
Default constructor.
Definition: TruthParticle.cxx:33