ATLAS Offline Software
EgammaTruthxAODHelpers.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "xAODEgamma/Egamma.h"
7 #include "xAODEgamma/Photon.h"
8 #include "xAODEgamma/Electron.h"
15 
18 
19 // ==================================================================
20 
28  recoElectronLinkAcc( "recoElectronLink" );
29 
30 const xAOD::Electron*
32 
33  if( ! recoElectronLinkAcc.isAvailable( *particle ) ) {
34  return nullptr;
35  }
37  recoElectronLinkAcc( *particle );
38  if( ! link.isValid() ) {
39  return nullptr;
40  }
41  return *link;
42 }
43 
51  recoPhotonLinkAcc( "recoPhotonLink" );
52 
53 const xAOD::Photon*
55 
56  if( ! recoPhotonLinkAcc.isAvailable( *particle ) ) {
57  return nullptr;
58  }
60  recoPhotonLinkAcc( *particle );
61  if( ! link.isValid() ) {
62  return nullptr;
63  }
64  return *link;
65 }
66 // ==================================================================
67 
68 //Is the object matched to a true converted photon with R < maxRadius
69 bool xAOD::EgammaHelpers::isTrueConvertedPhoton(const xAOD::Photon* ph, float maxRadius /* = 800. */){
71  if (!truthPh) {return false;}
72  // In older versions egammaTruthParticles did not have a decay vertex associated
73  // so we look for the original truth particle
74  if (truthPh->hasDecayVtx()){
75  return isTrueConvertedPhoton(truthPh, maxRadius);
76  }
77  const xAOD::TruthParticle *orgTruthPh = xAOD::TruthHelpers::getTruthParticle(*truthPh);
78  if (!orgTruthPh){ return false;}
79  return xAOD::EgammaHelpers::isTrueConvertedPhoton(orgTruthPh, maxRadius);
80 }
81 
82 //Is the true object a converted photon with R < maxRadius
83 bool xAOD::EgammaHelpers::isTrueConvertedPhoton(const xAOD::TruthParticle* truthPh, float maxRadius /*= 800.*/){
84  return (MC::isPhoton(truthPh) && truthPh->hasDecayVtx()
85  && truthPh->decayVtx()->perp() < maxRadius);
86 }
87 
88 // ==================================================================
89 
92  const bool allTheWayBack/*=true*/){
93  const xAOD::TruthParticle *truthel =
95  return getBkgElectronMother(truthel,allTheWayBack);
96 }
97 
100  const bool allTheWayBack/*=true*/) {
101  std::vector<const xAOD::TruthParticle*> vec =
102  xAOD::EgammaHelpers::getBkgElectronLineage(truthel,allTheWayBack);
103  if (!vec.empty()) {
104  return vec.back();
105  }
106  return nullptr;
107 }
108 
109 std::vector<const xAOD::TruthParticle*>
111  const bool allTheWayBack/*=true*/) {
113  return getBkgElectronLineage(truthel,allTheWayBack);
114 }
115 
116 //The actual implementation code
117 std::vector<const xAOD::TruthParticle*>
119  const bool allTheWayBack/*=true*/) {
120  std::vector<const xAOD::TruthParticle*> vec;
121  //Truth must exist and be an electron
122  if (!truthel || !MC::isElectron(truthel)){
123  return vec;
124  }
125  vec.push_back(truthel); //push its self back as first entry
126 
127  // The first parent has to exist
128  if (!truthel->nParents()) {
129  return vec;
130  }
131 
132  if (!HepMC::is_simulation_particle(truthel) && !allTheWayBack) {
133  return vec;
134  }
135 
136  //And has to be a photon or electron
137  const xAOD::TruthParticle* parent = truthel->parent();
138  if ( !MC::isPhoton(parent) && !MC::isElectron(parent) ) {
139  return vec;
140  }
141 
142  vec.push_back(parent); //push in the parent as the second entry
143 
144  //Loop over the generations
145  while (parent->nParents() &&
146  (HepMC::is_simulation_particle(parent) || allTheWayBack)) {
147  //Find the next parent
148  const xAOD::TruthParticle* tmp = parent->parent();
149  //You want to see an electron or a photon
150  if (MC::isPhoton(tmp) || MC::isElectron(tmp)) {
151  parent=tmp;
152  } else { // if we do not see any more electron and photons we stop
153  break;
154  }
155  vec.push_back(parent); //push in the parent
156  }
157  return vec;
158 }
159 
xAOD::TruthParticle_v1::parent
const TruthParticle_v1 * parent(size_t i=0) const
Retrieve the i-th mother (TruthParticle) of this TruthParticle.
Definition: TruthParticle_v1.cxx:131
xAOD::EgammaHelpers::getRecoPhoton
const xAOD::Photon * getRecoPhoton(const xAOD::TruthParticle *particle)
return the reco photon associated to the given TruthParticle (if any)
Definition: EgammaTruthxAODHelpers.cxx:54
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:66
TruthParticleContainer.h
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
xAODTruthHelpers.h
xAOD::EgammaHelpers::getBkgElectronMother
const xAOD::TruthParticle * getBkgElectronMother(const xAOD::Electron *el, const bool allTheWayBack=true)
Helper wrapper function for calling the function below that accepts truth input.
Definition: EgammaTruthxAODHelpers.cxx:91
CxxUtils::vec
typename vecDetail::vec_typedef< T, N >::type vec
Define a nice alias for the vectorized type.
Definition: vec.h:207
Egamma.h
xAOD::TruthParticle_v1::hasDecayVtx
bool hasDecayVtx() const
Check for a decay vertex on this particle.
xAOD::TruthParticle_v1::nParents
size_t nParents() const
Number of parents of this particle.
Definition: TruthParticle_v1.cxx:122
ElectronContainer.h
HepMC::is_simulation_particle
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
Definition: MagicNumbers.h:299
Photon.h
xAOD::EgammaHelpers::isElectron
bool isElectron(const xAOD::Egamma *eg)
is the object an electron (not Fwd)
Definition: EgammaxAODHelpers.cxx:13
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
xAOD::TruthVertex_v1::perp
float perp() const
Vertex transverse distance from the beam line.
Definition: TruthVertex_v1.cxx:165
test_pyathena.parent
parent
Definition: test_pyathena.py:15
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
TruthVertex.h
xAOD::TruthParticle_v1::decayVtx
const TruthVertex_v1 * decayVtx() const
The decay vertex of this particle.
xAOD::EgammaHelpers::isTrueConvertedPhoton
bool isTrueConvertedPhoton(const xAOD::Photon *ph, float maxRadius=800.)
is the object matched to a true converted photon with R < maxRadius
Definition: EgammaTruthxAODHelpers.cxx:69
MagicNumbers.h
xAOD::TruthHelpers::getTruthParticle
const xAOD::TruthParticle * getTruthParticle(const xAOD::IParticle &p)
Return the truthParticle associated to the given IParticle (if any)
Definition: xAODTruthHelpers.cxx:25
xAOD::EgammaHelpers::getRecoElectron
const xAOD::Electron * getRecoElectron(const xAOD::TruthParticle *)
return the reco electron associated to the given TruthParticle (if any)
Definition: EgammaTruthxAODHelpers.cxx:31
xAOD::Electron_v1
Definition: Electron_v1.h:34
xAOD::Photon_v1
Definition: Photon_v1.h:37
xAOD::EgammaHelpers::isPhoton
bool isPhoton(const xAOD::Egamma *eg)
is the object a photon
Definition: EgammaxAODHelpers.cxx:22
Electron.h
TruthParticle.h
PhotonContainer.h
EgammaTruthxAODHelpers.h
HepMCHelpers.h
xAOD::EgammaHelpers::getBkgElectronLineage
std::vector< const xAOD::TruthParticle * > getBkgElectronLineage(const xAOD::Electron *el, const bool allTheWayBack=true)
Helper wrapper function for calling the function below that accepts truth input.
Definition: EgammaTruthxAODHelpers.cxx:110