ATLAS Offline Software
HepMCHelpers.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 #ifndef TRUTHUTILS_HEPMCHELPERS_H
5 #define TRUTHUTILS_HEPMCHELPERS_H
6 #include <vector>
7 #include <cmath>
8 #include <algorithm>
9 #include <array>
10 #include <cstdlib>
11 #include <set>
13 
17 
18 namespace MC
19 {
20  namespace Pythia8
21  {
23  template <class T> inline bool isConditionA(const T& p) { return p->status() == 62 || p->status() == 52 || p->status() == 21 || p->status() == 22;}
24 
25  template <class T> inline bool isConditionB(const T& p) { return p->status() == 23;}
26 
27  template <class T> inline bool isConditionC(const T& p) { return p->status() > 30 && p->status() < 40;}
28  }
29 
30 #include "AtlasPID.h"
31 
33  template <class T> inline bool isInteracting(const T& p) { return isStrongInteracting<T>(p) || isEMInteracting<T>(p) || isGeantino<T>(p); }
34 
36  template <class T> inline bool isChargedNonShowering(const T& p) { return (isMuon<T>(p) || isSUSY<T>(p)); }
37 
39  template <class T> inline bool isBeam(const T& p) { return HepMC::status(p)%HepMC::SIM_STATUS_THRESHOLD == 4;}
40 
42  template <class T> inline bool isDecayed(const T& p) { return HepMC::status(p)%HepMC::SIM_STATUS_THRESHOLD == 2;}
43 
45  template <class T> inline bool isStable(const T& p) { return HepMC::status(p)%HepMC::SIM_STATUS_THRESHOLD == 1;}
46 
48  template <class T> inline bool isFinalState(const T& p) { return HepMC::status(p)%HepMC::SIM_STATUS_THRESHOLD == 1 && !p->end_vertex();}
49 
51  template <class T> inline bool isPhysical(const T& p) { return isStable<T>(p) || isDecayed<T>(p); }
52 
54  template <class T> inline bool isGenStable(const T& p) { return isStable<T>(p) && !HepMC::is_simulation_particle<T>(p);}
55 
57  template <class T> inline bool isSimStable(const T& p) { return isStable<T>(p) && !p->end_vertex() && HepMC::is_simulation_particle<T>(p);}
58 
60  template <class T> inline bool isSimInteracting(const T& p) { return isGenStable<T>(p) && isInteracting<T>(p);}
61 
65  template <class T> inline bool isStableOrSimDecayed(const T& p) {
66  const auto vertex = p->end_vertex();
67  return ( isStable<T>(p) || (isDecayed<T>(p) && (!vertex || HepMC::is_simulation_vertex(vertex))));
68  }
69 
71  template <class T> inline bool isZeroEnergyPhoton(const T& p) { return isPhoton<T>(p) && p->e() == 0;}
72 
74  template <class T> inline bool isSingleParticle(const T& p) {
75 #if defined(HEPMC3)
76  return HepMC::barcode(p) == 3 || HepMC::barcode(p) == 10001;
77 #else
78  return HepMC::barcode(p) == 10001;
79 #endif
80  }
81 
83  template <class T> inline bool isSpecialNonInteracting(const T& p) {
84  const int apid = std::abs(p->pdg_id());
85  if (apid == NU_E || apid == NU_MU || apid == NU_TAU) return true; //< neutrinos
86  if (apid == 1000022 || apid == 1000024 || apid == 5100022) return true; // SUSY & KK photon and Z partners
87  if (apid == GRAVITON || apid == 1000039 || apid == 5000039) return true; //< gravitons: standard, SUSY and KK
88  if (apid == 9000001 || apid == 9000002 || apid == 9000003 || apid == 9000004 || apid == 9000005 || apid == 9000006) return true; //< exotic particles from monotop model
89  return false;
90  }
91 
93 
94  template <class T> T findMother(T thePart) {
95  auto partOriVert = thePart->production_vertex();
96  if (!partOriVert) return nullptr;
97 
98  long partPDG = thePart->pdg_id();
99  long MotherPDG(0);
100 
101  auto MothOriVert = partOriVert;
102  MothOriVert = nullptr;
103  T theMoth(nullptr);
104 
105  size_t itr = 0;
106  do {
107  if (itr != 0) partOriVert = MothOriVert;
108  auto incoming = partOriVert->particles_in();
109  for ( auto p: incoming) {
110  theMoth = p;
111  if (!theMoth) continue;
112  MotherPDG = theMoth->pdg_id();
113  MothOriVert = theMoth->production_vertex();
114  if (MotherPDG == partPDG) break;
115  }
116  itr++;
117  if (itr > 100) {
118  break;
119  }
120  } while (MothOriVert != nullptr && MotherPDG == partPDG && !HepMC::is_simulation_particle(thePart) &&
121  MothOriVert != partOriVert);
122  return theMoth;
123  }
124 
126 
127  template <class C, class T> T findMatching(C TruthContainer, T p) {
128  T ptrPart = nullptr;
129  if (!p) return ptrPart;
130  for (T truthParticle : *TruthContainer) {
131  if (HepMC::is_sim_descendant(p,truthParticle)) {
132  ptrPart = truthParticle;
133  break;
134  }
135  }
136  return ptrPart;
137  }
139 
140  template <class T> void findParticleAncestors(T thePart, std::set<T>& allancestors) {
141  auto prodVtx = thePart->production_vertex();
142  if (!prodVtx) return;
143  for (auto theMother: prodVtx->particles_in()) {
144  if (!theMother) continue;
145  allancestors.insert(theMother);
146  findParticleAncestors(theMother, allancestors);
147  }
148  }
149 
151 
152  template <class T> void findParticleStableDescendants(T thePart, std::set<T>& allstabledescendants) {
153  auto endVtx = thePart->end_vertex();
154  if (!endVtx) return;
155  for (auto theDaughter: endVtx->particles_out()) {
156  if (!theDaughter) continue;
157  if (isStable(theDaughter) && !HepMC::is_simulation_particle(theDaughter)) {
158  allstabledescendants.insert(theDaughter);
159  }
160  findParticleStableDescendants(theDaughter, allstabledescendants);
161  }
162  }
163 
167 
168  template <class T> bool isHardScatteringVertex(T pVert) {
169  if (pVert == nullptr) return false;
170  T pV = pVert;
171  int numOfPartIn(0);
172  int pdg(0);
173 
174  do {
175  pVert = pV;
176  auto incoming = pVert->particles_in();
177  numOfPartIn = incoming.size();
178  pdg = numOfPartIn && incoming.front() != nullptr ? incoming.front()->pdg_id() : 0;
179  pV = numOfPartIn && incoming.front() != nullptr ? incoming.front()->production_vertex() : nullptr;
180 
181  } while (numOfPartIn == 1 && (std::abs(pdg) < 81 || std::abs(pdg) > 100) && pV != nullptr);
182 
183  if (numOfPartIn == 2) {
184  auto incoming = pVert->particles_in();
185  if (incoming.at(0) && incoming.at(1) && (std::abs(incoming.at(0)->pdg_id()) < 7 || incoming.at(0)->pdg_id() == 21) && (std::abs(incoming.at(1)->pdg_id()) < 7 || incoming.at(1)->pdg_id() == 21)) return true;
186  }
187  return false;
188 }
189 
193 
194  template <class T> bool isFromHadron(T p, T hadron, bool &fromTau, bool &fromBSM) {
195  if (isHadron(p)&&!isBeam(p)) return true; // trivial case
196  auto vtx = p->production_vertex();
197  if (!vtx) return false;
198  bool fromHad = false;
199  auto incoming = vtx->particles_in();
200  for (auto parent: incoming) {
201  if (!parent) continue;
202  // should this really go into parton-level territory?
203  // probably depends where BSM particles are being decayed
204  fromBSM |= isBSM(parent);
205  if (!isPhysical(parent)) return false;
206  fromTau |= isTau(parent);
207  if (isHadron(parent)&&!isBeam(parent)) {
208  if (!hadron) hadron = parent; // assumes linear hadron parentage
209  return true;
210  }
211  fromHad |= isFromHadron(parent, hadron, fromTau, fromBSM);
212  }
213  return fromHad;
214  }
215 
218 
219  template <class T> auto findSimulatedEndVertex(T thePart) -> decltype(thePart->end_vertex()) {
220  decltype(thePart->end_vertex()) EndVert = thePart->end_vertex();
221  decltype(thePart->end_vertex()) pVert(nullptr);
222  if (EndVert != nullptr) {
223  do {
224  bool samePart = false;
225  pVert = nullptr;
226  auto outgoing = EndVert->particles_out();
227  auto incoming = EndVert->particles_in();
228  for (const auto& itrDaug: outgoing) {
229  if (!itrDaug) continue;
230  if ((( HepMC::is_same_generator_particle(itrDaug,thePart)) ||
231  // brem on generator level for tau
232  (outgoing.size() == 1 && incoming.size() == 1 &&
234  itrDaug->pdg_id() == thePart->pdg_id()) {
235  samePart = true;
236  pVert = itrDaug->end_vertex();
237  }
238  }
239  if (samePart) EndVert = pVert;
240  } while (pVert != nullptr && pVert != EndVert); // pVert!=EndVert to prevent Sherpa loop
241  }
242  return EndVert;
243  }
244 
246 
247  template <class V> auto findFinalStateParticles(V theVert) -> decltype(theVert->particles_out()) {
248  if (!theVert) return {};
249  decltype(theVert->particles_out()) finalStatePart;
250  auto outgoing = theVert->particles_out();
251  for (const auto& thePart: outgoing) {
252  if (!thePart) continue;
253  finalStatePart.push_back(thePart);
254  if (isStable(thePart)) continue;
255  V pVert = findSimulatedEndVertex(thePart);
256  if (pVert == theVert) break; // to prevent Sherpa loop
257  if (pVert != nullptr) {
258  auto vecPart = findFinalStateParticles<V>(pVert);
259  finalStatePart.insert(finalStatePart.end(),vecPart.begin(),vecPart.end());
260  }
261  }
262  return finalStatePart;
263  }
264 
265 }
266 #endif
MC::isFromHadron
bool isFromHadron(T p, T hadron, bool &fromTau, bool &fromBSM)
Function to classify the particle.
Definition: HepMCHelpers.h:194
MCTruthPartClassifier::hadron
@ hadron
Definition: TruthClassifiers.h:148
MC::findParticleAncestors
void findParticleAncestors(T thePart, std::set< T > &allancestors)
Function to find all ancestors of the particle.
Definition: HepMCHelpers.h:140
MC::isSpecialNonInteracting
bool isSpecialNonInteracting(const T &p)
Identify a special non-interacting particles.
Definition: HepMCHelpers.h:83
isBSM
bool isBSM(const T &p)
Definition: AtlasPID.h:224
MC::Pythia8::isConditionC
bool isConditionC(const T &p)
Definition: HepMCHelpers.h:27
MC
Definition: HepMCHelpers.h:19
MC::isGenStable
bool isGenStable(const T &p)
Determine if the particle is stable at the generator (not det-sim) level,.
Definition: HepMCHelpers.h:54
Pythia8
Author: James Monk (jmonk@cern.ch)
Definition: IPythia8Custom.h:13
MC::isPhysical
bool isPhysical(const T &p)
Identify if the particle is physical, i.e. is stable or decayed.
Definition: HepMCHelpers.h:51
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
HepMC::is_same_generator_particle
bool is_same_generator_particle(const T1 &p1, const T2 &p2)
Method to establish if two particles in the GenEvent actually represent the same generated particle.
Definition: MagicNumbers.h:351
MC::findFinalStateParticles
auto findFinalStateParticles(V theVert) -> decltype(theVert->particles_out())
Function to find the stable particle descendants of the given vertex..
Definition: HepMCHelpers.h:247
MC::isFinalState
bool isFinalState(const T &p)
Identify if the particle is final state particle.
Definition: HepMCHelpers.h:48
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:342
MC::findMatching
T findMatching(C TruthContainer, T p)
Function to find a particle in container.
Definition: HepMCHelpers.h:127
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
test_pyathena.parent
parent
Definition: test_pyathena.py:15
MC::findParticleStableDescendants
void findParticleStableDescendants(T thePart, std::set< T > &allstabledescendants)
Function to get the particle stable MC daughters.
Definition: HepMCHelpers.h:152
HepMC::is_simulation_vertex
bool is_simulation_vertex(const T &v)
Method to establish if the vertex was created during simulation (TODO migrate to be based on status).
Definition: MagicNumbers.h:348
MC::findMother
T findMother(T thePart)
Function to get a mother of particle. MCTruthClassifier legacy.
Definition: HepMCHelpers.h:94
MC::isSingleParticle
bool isSingleParticle(const T &p)
Identify a particlegun particle.
Definition: HepMCHelpers.h:74
isTau
bool isTau(const T &p)
Definition: AtlasPID.h:157
MC::Pythia8::isConditionA
bool isConditionA(const T &p)
To be understood.
Definition: HepMCHelpers.h:23
HepMC::SIM_STATUS_THRESHOLD
constexpr int SIM_STATUS_THRESHOLD
Constant definiting the status threshold for simulated particles, eg. can be used to separate generat...
Definition: MagicNumbers.h:45
MagicNumbers.h
MC::isChargedNonShowering
bool isChargedNonShowering(const T &p)
Identify if the particle with given PDG ID would produce ID tracks but not shower in the detector if ...
Definition: HepMCHelpers.h:36
isHadron
bool isHadron(const T &p)
Definition: AtlasPID.h:218
MC::isStableOrSimDecayed
bool isStableOrSimDecayed(const T &p)
Identify if particle is satble or decayed in simulation.
Definition: HepMCHelpers.h:65
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
MC::isStable
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
Definition: HepMCHelpers.h:45
MC::isDecayed
bool isDecayed(const T &p)
Identify if the particle decayed.
Definition: HepMCHelpers.h:42
MC::isBeam
bool isBeam(const T &p)
Identify if the particle is beam particle.
Definition: HepMCHelpers.h:39
MC::isSimInteracting
bool isSimInteracting(const T &p)
Identify if the particle could interact with the detector during the simulation, e....
Definition: HepMCHelpers.h:60
MC::Pythia8::isConditionB
bool isConditionB(const T &p)
Definition: HepMCHelpers.h:25
MC::isInteracting
bool isInteracting(const T &p)
Identify if the particle with given PDG ID would not interact with the detector, i....
Definition: HepMCHelpers.h:33
HepMC::is_sim_descendant
bool is_sim_descendant(const T1 &p1, const T2 &p2)
Method to check if the first particle is a descendant of the second in the simulation,...
Definition: MagicNumbers.h:360
MC::isSimStable
bool isSimStable(const T &p)
Identify if the particle is considered stable at the post-detector-sim stage.
Definition: HepMCHelpers.h:57
MC::isZeroEnergyPhoton
bool isZeroEnergyPhoton(const T &p)
Identify a photon with zero energy. Probably a workaround for a generator bug.
Definition: HepMCHelpers.h:71
HepMC::status
int status(const T &p)
Definition: MagicNumbers.h:125
AtlasPID.h
MC::isHardScatteringVertex
bool isHardScatteringVertex(T pVert)
Function to classify the vertex as hard scattering vertex.
Definition: HepMCHelpers.h:168
MC::findSimulatedEndVertex
auto findSimulatedEndVertex(T thePart) -> decltype(thePart->end_vertex())
Function to find the end vertex of a particle.
Definition: HepMCHelpers.h:219