ATLAS Offline Software
Loading...
Searching...
No Matches
MagicNumbers.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4/* Author: Andrii Verbytskyi andrii.verbytskyi@mpp.mpg.de */
5
6#ifndef TRUTHUTILS_MAGICNUMBERS_H
7#define TRUTHUTILS_MAGICNUMBERS_H
8
9#include <limits>
10#include <cstdint>
11#include <memory>
12#include <algorithm>
13#include <deque>
14#include <type_traits>
15#include <ranges>
16#if !defined(XAOD_STANDALONE)
17#include "AtlasHepMC/GenEvent.h"
20#endif
21namespace xAOD {
22 // Temporarily specialize for xAOD::Truth classes ahead of the barcode migration - TODO remove this
23 class TruthParticle_v1;
24 class TruthVertex_v1;
25}
26class TrackRecord;
29
30enum EBC_SUPPRESSED_TRUTH : unsigned char {
31 EBC_UNSUPPRESSED = 0, // Truth particle expected to be found in McEventCollection
32 EBC_PU_SUPPRESSED, // Link points to a suppressed pile-up truth particle do not attempt to resolve it.
34};
35
36namespace HepMC {
37
39 constexpr int SIM_BARCODE_THRESHOLD = 200000;
40
42 constexpr int SIM_REGENERATION_INCREMENT = 1000000;
43
45 constexpr int SIM_STATUS_INCREMENT = 100000;
46
48 constexpr int SIM_STATUS_THRESHOLD = 20000;
49
52
54 constexpr int SUPPRESSED_PILEUP_BARCODE(std::numeric_limits<int32_t>::max());
55
56 constexpr int INVALID_PARTICLE_BARCODE = -1;
57
58 constexpr int UNDEFINED_ID = 0;
59 constexpr int INVALID_PARTICLE_ID = -1;
60 constexpr int INVALID_VERTEX_ID = 1;
61
62 // TODO The definitions of is_smart_ptr and remove_smart_pointer
63 // below are probably too generic for this header, but putting them
64 // here initially.
65 template <typename T> struct is_smart_ptr : std::false_type {};
66 template <typename T> struct is_smart_ptr<std::shared_ptr<T>> : std::true_type {};
67 template <typename T> struct is_smart_ptr<std::unique_ptr<T>> : std::true_type {};
68 template <typename T> struct is_smart_ptr<std::weak_ptr<T>> : std::true_type {};
69 template <class T> inline constexpr bool is_smart_ptr_v = is_smart_ptr<T>::value;
70
71 template <class T> struct remove_smart_pointer { typedef T type; };
72 template <class T> struct remove_smart_pointer<std::shared_ptr<T>> { typedef T type; };
73 template <class T> struct remove_smart_pointer<std::unique_ptr<T>> { typedef T type; };
74 template <class T> struct remove_smart_pointer<std::weak_ptr<T>> { typedef T type; };
75 template <class T> using remove_smart_pointer_t = typename remove_smart_pointer<T>::type;
76
77#if defined(XAOD_STANDALONE)
78 // Needed as we can't pick up the helper functions from AtlasHepMC in this case
79 template <class T> inline int barcode(const T& p){
80 if constexpr (std::is_pointer_v<T> || is_smart_ptr_v<T>){ //T is ptr
81 return p->barcode();
82 }
83 else {
84 return p.barcode();
85 }
86 }
87 template <> inline int barcode(const int& p) { return p;}
88#endif
89 // Temporarily specialize uniqueID for xAOD::Truth classes ahead of the barcode migration - TODO remove this
90 template <typename T> inline int uniqueID(const T& p) {
91 if constexpr (std::is_integral_v<T>) {
92 return p;
93 }
94 else if constexpr (std::is_integral_v<std::remove_pointer_t<T>>) {
95 return *p;
96 }
97 else if constexpr (std::is_same_v<T, xAOD::TruthParticle_v1> || std::is_same_v<T, xAOD::TruthVertex_v1>) {
98 return p.uid();
99 }
100 else if constexpr (std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T>>>, xAOD::TruthParticle_v1> || std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T>>>, xAOD::TruthVertex_v1>) {
101 return p->uid();
102 }
103 else if constexpr (std::is_same_v<T, CaloCalibrationHit>) {
104 return p.particleUID();
105 }
106 else if constexpr (std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T>>>, CaloCalibrationHit>) {
107 return p->particleUID();
108 }
109 else if constexpr (std::is_pointer_v<T> || is_smart_ptr_v<T>){ //T is ptr
110 return p->id();
111 }
112 else {
113 return p.id();
114 }
115 }
116 template <typename T> inline int status(const T& p) {
117 if constexpr (std::is_integral_v<T>) {
118 return p;
119 }
120 else if constexpr (std::is_integral_v<std::remove_pointer_t<T>>) {
121 return *p;
122 }
123 else if constexpr (std::is_pointer_v<T> || is_smart_ptr_v<T>){ //T is ptr
124 return p->status();
125 }
126 else {
127 return p.status();
128 }
129 }
130
132 template <class T> inline void get_particle_history(const T& p, std::vector<int>& out, const int direction = 0) {
133 out.insert(out.begin(), uniqueID(p));
134 if (direction < 0) {
135 if (p->status()>SIM_STATUS_INCREMENT) {
136 auto pv = p->production_vertex();
137 if (pv) {
138 for (auto pa: pv->particles_in()) {
139 if (!pa || pa->pdg_id() != p->pdg_id()) continue;
140 get_particle_history(pa, out, direction);
141 break;
142 }
143 }
144 }
145 }
146 if (direction > 0) {
147 auto pv = p->end_vertex();
148 if (pv) {
149 for (auto pa: pv->particles_out()) {
150 if (!pa || pa->pdg_id() != p->pdg_id()) continue;
151 get_particle_history(pa, out, direction);
152 break;
153 }
154 }
155 }
156 }
157
158 template <class T> inline std::vector<int> simulation_history(const T& p, const int direction ) {
159 std::vector<int> res;
160 get_particle_history(p, res, direction);
161 return res;
162 }
163
164 namespace BarcodeBased {
166 template <class T> inline bool is_truth_suppressed_pileup(const T& p){ return (barcode(p) == SUPPRESSED_PILEUP_BARCODE);}
167
169 template <class T> inline bool no_truth_link(const T& p){ return (barcode(p) == UNDEFINED_ID);}
170
172 template <class T> inline bool ignoreTruthLink(const T& p, bool vetoPileUp){ const int b = barcode(p); return no_truth_link(b) || (vetoPileUp && is_truth_suppressed_pileup(b)); }
173
175 template <class T> inline bool is_simulation_particle(const T& p){ return (barcode(p)>SIM_BARCODE_THRESHOLD);}
176
178 template <class T> inline bool is_sim_secondary(const T& p){ return (barcode(p)%SIM_REGENERATION_INCREMENT > SIM_BARCODE_THRESHOLD); }
179
181 template <class T> inline int generations(const T& p){ return (barcode(p)/SIM_REGENERATION_INCREMENT);}
182
184 template <class T> inline bool is_simulation_vertex(const T& v){ return (barcode(v)<-SIM_BARCODE_THRESHOLD);}
185
187 template <class T1, class T2> inline bool is_same_generator_particle(const T1& p1, const T2& p2) { int b1 = barcode(p1); int b2 = barcode(p2); return b1% SIM_REGENERATION_INCREMENT == b2 % SIM_REGENERATION_INCREMENT; }
188
190 template <class T1, class T2> inline bool is_same_object(const T1& p1, const T2& p2) { int b1 = uniqueID(p1); int b2 = uniqueID(p2); return b1 == b2; } // NB Still need to adapt CaloCalibrationHits
191
193 template <class T1, class T2> inline bool is_sim_descendant(const T1& p1, const T2& p2) { int b1 = barcode(p1); int b2 = barcode(p2); return b1 % SIM_REGENERATION_INCREMENT == b2;}
194 }
195
196 namespace StatusBased {
198 template <class T> inline bool is_truth_suppressed_pileup(const T& p){
199 if constexpr (std::is_same_v<std::remove_const_t<T>, HepMcParticleLink>) {
200 return p.getTruthSuppressionType() == EBC_PU_SUPPRESSED;
201 }
202 else if constexpr (std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T>>>, HepMcParticleLink>) {
203 return p->getTruthSuppressionType() == EBC_PU_SUPPRESSED;
204 }
205 else {
206 return p == EBC_PU_SUPPRESSED;
207 }
208 }
209
211 template <class T> inline bool no_truth_link(const T& p){
212 if constexpr (std::is_same_v<std::remove_const_t<T>, HepMcParticleLink>) {
213 return p.linkIsNull();
214 }
215 else if constexpr (std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T>>>, HepMcParticleLink>) {
216 return p->linkIsNull();
217 }
218 else {
219 return (uniqueID(p) == UNDEFINED_ID);
220 }
221 }
222
224 template <class T> inline bool ignoreTruthLink(const T& p, bool vetoPileUp){
225 if constexpr (std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T>>>, HepMcParticleLink>) {
226 return no_truth_link(p) || (vetoPileUp && is_truth_suppressed_pileup(p));
227 }
228 else {
229 const int u = uniqueID(p); return no_truth_link(u) || (vetoPileUp && is_truth_suppressed_pileup(u));
230 }
231 }
232
234 template <class T> inline bool is_simulation_particle(const T& p){ return (status(p)>SIM_STATUS_THRESHOLD);}
235
237 template <class T> inline bool is_sim_secondary(const T& p){ return (status(p)%SIM_STATUS_INCREMENT > SIM_STATUS_THRESHOLD); }
238
240 template <class T> inline int generations(const T& p){ return (status(p)/SIM_STATUS_INCREMENT);}
241
243 template <class T> inline bool is_simulation_vertex(const T& v){ return (status(v)>SIM_STATUS_THRESHOLD);}
244
246 template <class T1, class T2> inline bool is_same_generator_particle(const T1& p1, const T2& p2) {
247 const int id1 = uniqueID(p1);
248 const int id2 = uniqueID(p2);
249 if (id1 == id2) { return true;} // simplest case
250 const int generations1 = generations(p1);
251 const int generations2 = generations(p2);
252 if (generations1 == generations2) { return false; } // if the id values don't match and the particles have the same generation number then they cannot be the same particle.
253 if constexpr (std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T1>>>, TrackRecord>) {
254 // No choice, but to get the history of one of the particles:
255 const int direction = (generations2 > generations1) ? -1 : 1;
256 if (!std::ranges::contains(simulation_history( p2, direction ), id1)) {
257 return false;
258 }
259 }
260 else {
261 // No choice, but to get the history of one of the particles:
262 const int direction = (generations1 > generations2) ? -1 : 1;
263 if (!std::ranges::contains(simulation_history( p1, direction ), id2)) {
264 return false;
265 }
266 }
267 return true;
268 }
269
271 template <class T1, class T2> inline bool is_same_object(const T1& p1, const T2& p2) {
272 const int id1 = uniqueID(p1);
273 const int id2 = uniqueID(p2);
274 return (id1 == id2);
275 }
276
277
279 template <class T1, class T2> inline bool is_sim_descendant(const T1& p1, const T2& p2) {
280 const int id1 = uniqueID(p1);
281 const int id2 = uniqueID(p2);
282 if (id1 == id2) { return true;} // simplest case
283 const int generations1 = generations(p1);
284 const int generations2 = generations(p2);
285 if (generations1 == generations2) { return false; } // if the id values don't match and the particles have the same generation number then they cannot be the same particle.
286 if constexpr (std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T1>>>, TrackRecord>) {
287 // No choice, but to get the descendants of p2
288 constexpr int descendents = 1;
289 if (!std::ranges::contains(simulation_history( p2, descendents ), id1)) { return false; }
290 }
291 else {
292 // No choice, but to get the history of p1
293 constexpr int ancestors = -1;
294 if (!std::ranges::contains(simulation_history( p1, ancestors ), id2)) { return false; }
295 }
296 return true;
297 }
298 }
299
301 template <class T> inline bool is_truth_suppressed_pileup(const T& p){
302 if constexpr (std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T>>>, HepMcParticleLink>) {
304 }
305 else {
307 }
308 }
309
311 template <class T> inline bool no_truth_link(const T& p){ return StatusBased::no_truth_link(p);}
312
314 template <class T> inline bool ignoreTruthLink(const T& p, bool vetoPileUp){
315 if constexpr (std::is_same_v<std::remove_const_t<remove_smart_pointer_t<std::remove_pointer_t<T>>>, HepMcParticleLink>) {
316 return StatusBased::ignoreTruthLink(p, vetoPileUp);
317 }
318 else {
319 return BarcodeBased::ignoreTruthLink(p, vetoPileUp);
320 }
321 }
322
324 template <class T> inline bool is_simulation_particle(const T& p){ return StatusBased::is_simulation_particle(p);}
325
327 template <class T> inline int generations(const T& p){ return StatusBased::generations(p);}
328
330 template <class T> inline bool is_simulation_vertex(const T& v){ return StatusBased::is_simulation_vertex(v);}
331
333 template <class T1, class T2> inline bool is_same_generator_particle(const T1& p1, const T2& p2) { return StatusBased::is_same_generator_particle(p1, p2); }
334
336 template <class T1, class T2> inline bool is_same_particle(const T1& p1, const T2& p2) { return StatusBased::is_same_object(p1, p2); }
337
339 template <class T1, class T2> inline bool is_same_vertex(const T1& p1, const T2& p2) { return StatusBased::is_same_object(p1, p2); }
340
342 template <class T1, class T2> inline bool is_sim_descendant(const T1& p1, const T2& p2) { return StatusBased::is_sim_descendant(p1, p2);}
343
345 template <class T> void old_to_new_simulation_scheme(T& evt) {
346 auto particle_status = [] (int barcode, int status) {
350 return status;
351 };
352 auto vertex_status = [] (int barcode, int status) {
354 return status;
355 };
356 for (auto p: evt->particles()) {
357 p->set_status (particle_status (HepMC::barcode(p), p->status()));
358 }
359 for (auto v: evt->vertices()) {
360 v->set_status (vertex_status (HepMC::barcode(v), v->status()));
361 }
362 }
363
365 inline int new_particle_status_from_old(const int oldStatus, const int barcode) {
366 int generations_barcode_based = (barcode/SIM_REGENERATION_INCREMENT);
367 bool is_sim_secondary_barcode_based = (barcode%SIM_REGENERATION_INCREMENT > SIM_BARCODE_THRESHOLD);
368 return oldStatus + SIM_STATUS_INCREMENT*generations_barcode_based + (is_sim_secondary_barcode_based? SIM_STATUS_THRESHOLD : 0);
369 }
370
372 inline int old_particle_status_from_new(const int newStatus) { return newStatus%SIM_STATUS_THRESHOLD; }
373
375 inline int new_vertex_status_from_old(const int oldStatus, const int barcode) {
376 bool is_simulation_vertex_barcode_based = (barcode<-SIM_BARCODE_THRESHOLD);
377 return (is_simulation_vertex_barcode_based? SIM_STATUS_THRESHOLD : 0) + oldStatus;
378 }
379
381 inline int old_vertex_status_from_new(const int newStatus) {
382 bool is_simulation_vertex_status_based = (newStatus>SIM_STATUS_THRESHOLD);
383 return ( is_simulation_vertex_status_based ? -SIM_STATUS_THRESHOLD : 0) + newStatus; }
384}
385#if !defined(XAOD_STANDALONE)
386namespace HepMC {
388inline int maxGeneratedParticleBarcode(const HepMC::GenEvent *genEvent) {
389 int maxBarcode = 0;
390 auto allbarcodes = genEvent->attribute<HepMC::GenEventBarcodes>(HepMCStr::barcodes);
391 for (const auto& bp: allbarcodes->barcode_to_particle_map()) {
392 if (!HepMC::BarcodeBased::is_simulation_particle(bp.first)) { maxBarcode=std::max(maxBarcode,bp.first); }
393 }
394 return maxBarcode;
395}
396
398inline int maxGeneratedVertexBarcode(const HepMC::GenEvent *genEvent) {
399 int maxBarcode=0;
400 auto allbarcodes = genEvent->attribute<HepMC::GenEventBarcodes>(HepMCStr::barcodes);
401 for (const auto& bp: allbarcodes->barcode_to_vertex_map()) {
402 if (!HepMC::BarcodeBased::is_simulation_vertex(bp.first)) { maxBarcode=std::min(maxBarcode,bp.first); }
403 }
404 return maxBarcode;
405}
406}
407#endif
408
409#endif
std::pair< std::vector< unsigned int >, bool > res
EBC_SUPPRESSED_TRUTH
@ EBC_NSUPP
@ EBC_PU_SUPPRESSED
@ EBC_UNSUPPRESSED
Class to store calorimeter calibration hit.
STL class.
Class describing a truth particle in the MC record.
Class describing a truth vertex in the MC record.
bool is_same_object(const T1 &p1, const T2 &p2)
Method to establish if two particles/vertices in the GenEvent actually represent the same particle.
bool is_sim_secondary(const T &p)
Method to establish if a particle (or barcode) is a new secondary created during the simulation (only...
bool is_truth_suppressed_pileup(const T &p)
Method to establish if a particle (or barcode) corresponds to truth-suppressed pile-up.
bool is_simulation_vertex(const T &v)
Method to establish if the vertex was created during simulation (only to be used in legacy TP convert...
int generations(const T &p)
Method to return how many interactions a particle has undergone during simulation (only to be used in...
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.
bool ignoreTruthLink(const T &p, bool vetoPileUp)
Helper function for SDO creation in PileUpTools.
bool no_truth_link(const T &p)
Method to establish if the object is linked to something which was never saved to the HepMC Truth - f...
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (only to be used in ...
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,...
bool is_sim_secondary(const T &p)
Method to establish if a particle is a new secondary created during the simulation based on the statu...
int generations(const T &p)
Method to return how many interactions a particle has undergone during simulation based on the status...
bool is_simulation_vertex(const T &v)
Method to establish if the vertex was created during simulation from the status.
bool is_same_object(const T1 &p1, const T2 &p2)
Method to establish if two particles/vertices in the GenEvent actually represent the same particle.
bool is_truth_suppressed_pileup(const T &p)
Method to establish if a particle corresponds to truth-suppressed pile-up.
bool is_simulation_particle(const T &p)
Method to establish if a particle was created during the simulation based on the status value.
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.
bool ignoreTruthLink(const T &p, bool vetoPileUp)
Helper function for SDO creation in PileUpTools.
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,...
bool no_truth_link(const T &p)
Method to establish if the object is linked to something which was never saved to the HepMC Truth - f...
int maxGeneratedVertexBarcode(const HepMC::GenEvent *genEvent)
Get the maximal absolute value of barcode of vertex present in the event. Returns a negative number.
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.
constexpr int INVALID_PARTICLE_ID
bool is_simulation_vertex(const T &v)
Method to establish if the vertex was created during simulation (based on the status value).
void old_to_new_simulation_scheme(T &evt)
Function that converts the old scheme of labeling the simulation particles (barcodes) into the new sc...
int barcode(const T *p)
Definition Barcode.h:15
int uniqueID(const T &p)
constexpr int UNDEFINED_ID
typename remove_smart_pointer< T >::type remove_smart_pointer_t
void get_particle_history(const T &p, std::vector< int > &out, const int direction=0)
Function to calculate all the descendants(direction=1)/ancestors(direction=-1) of the particle.
constexpr int SUPPRESSED_PILEUP_BARCODE(std::numeric_limits< int32_t >::max())
This barcode is used by objects matched to particles from pile-up interactions in standard MC Product...
constexpr int INVALID_PARTICLE_BARCODE
constexpr int SIM_STATUS_INCREMENT
Constant defining the status increment for regenerated particles, i.e. particles surviving an interac...
constexpr int SIM_STATUS_THRESHOLD
Constant defining the status threshold for simulated particles, e.g. can be used to separate generato...
int status(const T &p)
std::vector< int > simulation_history(const T &p, const int direction)
Function to calculate all the descendants(direction=1)/ancestors(direction=-1) of the particle.
bool is_same_particle(const T1 &p1, const T2 &p2)
Method to establish if two particles in the GenEvent actually represent the same particle.
int new_vertex_status_from_old(const int oldStatus, const int barcode)
Get vertex status in the new scheme from the barcode and status in the old scheme.
int old_vertex_status_from_new(const int newStatus)
Get vertex status in the old scheme from the status in the new scheme.
bool is_truth_suppressed_pileup(const T &p)
Method to establish if a particle (or barcode) corresponds to truth-suppressed pile-up (barcode based...
int maxGeneratedParticleBarcode(const HepMC::GenEvent *genEvent)
Get the maximal value of barcode of particle present in the event.
bool is_simulation_particle(const T &p)
Method to establish if a particle was created during the simulation (based on the status value).
constexpr int INVALID_VERTEX_ID
constexpr int FORWARD_TRANSPORT_MODEL_PROCESS
Special Forward transport Geant process for vertices.
int new_particle_status_from_old(const int oldStatus, const int barcode)
Get particle status in the new scheme from the barcode and status in the old scheme.
constexpr bool is_smart_ptr_v
bool no_truth_link(const T &p)
Method to establish if the object is linked to something which was never saved to the HepMC Truth - f...
int old_particle_status_from_new(const int newStatus)
Get particle status in the old scheme from the status in the new scheme.
bool is_same_vertex(const T1 &p1, const T2 &p2)
Method to establish if two vertices in the GenEvent actually represent the same vertex.
int generations(const T &p)
Method to return how many interactions a particle has undergone during simulation (based on the statu...
constexpr int SIM_BARCODE_THRESHOLD
Constant defining the barcode threshold for simulated particles, e.g. can be used to separate generat...
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,...
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39
bool ignoreTruthLink(const T &p, bool vetoPileUp)
Helper function for SDO creation in PileUpTools.
constexpr int SIM_REGENERATION_INCREMENT
Constant defining the barcode increment for regenerated particles, i.e. particles surviving an intera...
STL namespace.
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.