ATLAS Offline Software
Loading...
Searching...
No Matches
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"
15
18
19// ==================================================================
20
28 recoElectronLinkAcc( "recoElectronLink" );
29
30const 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
53const 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
69bool 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 }
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
83bool 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//Lineage methods
89std::vector<const xAOD::TruthParticle*>
91 const bool allTheWayBack/*=true*/) {
92 std::vector<const xAOD::TruthParticle*> vec;
93 //Truth must exist and be an electron
94 if (!truthel || !MC::isElectron(truthel)){
95 return vec;
96 }
97 vec.push_back(truthel); //push its self back as first entry
98
99 // The first parent has to exist
100 if (!truthel->nParents()) {
101 return vec;
102 }
103
104 if (!HepMC::is_simulation_particle(truthel) && !allTheWayBack) {
105 return vec;
106 }
107
108 //And has to be a photon or electron
109 const xAOD::TruthParticle* parent = nullptr;
110 for (size_t p = 0; p < truthel->nParents(); ++p) {
111 if ( !MC::isPhoton(truthel->parent(p)) && !MC::isElectron(truthel->parent(p)) ) return vec;
112 parent = truthel->parent(p); //AV: note, here is an ambiguity for the case of multiple parents.
113 }
114
115 if (!parent) return vec;
116 vec.push_back(parent); //push in the parent as the second entry
117
118 //Loop over the generations
119 while (parent->nParents() &&
120 (HepMC::is_simulation_particle(parent) || allTheWayBack)) {
121 //Find the next parent
122 const xAOD::TruthParticle* tmp = nullptr;
123 //You want to see an electron or a photon
124 for (size_t p = 0; p < parent->nParents(); ++p) {
125 if (MC::isPhoton(parent->parent(p)) || MC::isElectron(parent->parent(p))) tmp = parent->parent(p); //AV: note some ambiguity for multiple parents passing the selection
126 }
127 if (tmp) {
128 parent = tmp;
129 } else { // if we do not see any more electron and photons we stop
130 break;
131 }
132 vec.push_back(parent); //push in the parent
133 }
134 return vec;
135}
138 const bool allTheWayBack/*=true*/){
139 const xAOD::TruthParticle *truthel =
141 return getBkgElectronMother(truthel,allTheWayBack);
142}
143
146 const bool allTheWayBack/*=true*/) {
147 std::vector<const xAOD::TruthParticle*> vec =
148 xAOD::EgammaHelpers::getBkgElectronLineage(truthel,allTheWayBack);
149 if (!vec.empty()) {
150 return vec.back();
151 }
152 return nullptr;
153}
154
155
156std::vector<const xAOD::TruthParticle*>
158 const bool allTheWayBack/*=true*/) {
160 return getBkgElectronLineage(truthel,allTheWayBack);
161}
std::vector< size_t > vec
static const SG::AuxElement::Accessor< ElementLink< xAOD::ElectronContainer > > recoElectronLinkAcc("recoElectronLink")
Accessor for the "recoElectronLink" dynamic variable.
static const SG::AuxElement::Accessor< ElementLink< xAOD::PhotonContainer > > recoPhotonLinkAcc("recoPhotonLink")
Accessor for the "recoPhotonLink" dynamic variable.
ATLAS-specific HepMC functions.
SG::Accessor< T, ALLOC > Accessor
Definition AuxElement.h:572
const TruthVertex_v1 * decayVtx() const
The decay vertex of this particle.
const TruthParticle_v1 * parent(size_t i) const
Retrieve the i-th mother (TruthParticle) of this TruthParticle.
bool hasDecayVtx() const
Check for a decay vertex on this particle.
size_t nParents() const
Number of parents of this particle.
float perp() const
Vertex transverse distance from the beam line.
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...
bool isPhoton(const T &p)
bool isElectron(const T &p)
std::vector< const xAOD::TruthParticle * > getBkgElectronLineage(const xAOD::TruthParticle *truthel, const bool allTheWayBack=true)
Helper function for getting the truth lineage of an electron that is arising from material interactio...
bool isTrueConvertedPhoton(const xAOD::Photon *ph, float maxRadius=800.)
is the object matched to a true converted photon with R < maxRadius
const xAOD::TruthParticle * getBkgElectronMother(const xAOD::Electron *el, const bool allTheWayBack=true)
Helper wrapper function for calling the function above extracting the truth from a reco electron.
const xAOD::Photon * getRecoPhoton(const xAOD::TruthParticle *particle)
return the reco photon associated to the given TruthParticle (if any)
const xAOD::Electron * getRecoElectron(const xAOD::TruthParticle *)
return the reco electron associated to the given TruthParticle (if any)
const xAOD::TruthParticle * getTruthParticle(const xAOD::IParticle &p)
Return the truthParticle associated to the given IParticle (if any)
TruthParticle_v1 TruthParticle
Typedef to implementation.
Photon_v1 Photon
Definition of the current "egamma version".
Electron_v1 Electron
Definition of the current "egamma version".