ATLAS Offline Software
Loading...
Searching...
No Matches
Loops.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef TRUTHUTILS_LOOPS_H
6#define TRUTHUTILS_LOOPS_H
8
9namespace MC {
10
17
21template <class V, class T> bool TruthLoopDetectionMethod3(const V& childOrigVtx, const T& parent)
22{
23 const auto parentOrigVtx = parent->production_vertex();
24 return parentOrigVtx && HepMC::is_same_vertex(parentOrigVtx, childOrigVtx);
25}
26
32template <class V, class T> bool TruthLoopDetectionMethod1(const V& childOrigVtx, const T& parent)
33{
34 const int parentPDG = parent->pdg_id();
35 for (const auto& aChild: childOrigVtx->particles_out()) {
36 if (!aChild) continue;
37 if (parentPDG == aChild->pdg_id() && HepMC::is_same_generator_particle(aChild, parent)) return true;
38 }
39 return TruthLoopDetectionMethod3(childOrigVtx, parent);
40}
41
47template <class T1, class T2> bool TruthLoopDetectionMethod2(const T1& child, const T2& parent)
48{
49 decltype(child->production_vertex()) child_prdVtx{};
50 decltype(child->end_vertex()) child_endVtx{};
51 if (child) {
52 child_prdVtx = child->production_vertex();
53 child_endVtx = child->end_vertex();
54 }
55 decltype(parent->production_vertex()) parent_prdVtx{};
56 decltype(parent->end_vertex()) parent_endVtx{};
57 if (parent) {
58 parent_prdVtx = parent->production_vertex();
59 parent_endVtx = parent->end_vertex();
60 }
61 return (child_endVtx == parent_prdVtx && child_prdVtx == parent_endVtx);
62}
63}
64
65#ifndef XAOD_ANALYSIS
66#include <algorithm>
67#include <map>
68#include <vector>
69#include <memory>
70
71namespace MC {
72template <class Evt, class Prt, class Vtx>
73class Loops {
74public:
75
77 Loops() {}
78
80 Loops(const Evt* evt) {
81 findLoops(evt,true);
82 }
83
85 bool isLoop(const Prt& p) const {
86 return std::find(m_loop_particles.begin(), m_loop_particles.end(), p) != m_loop_particles.end();
87 }
88
90 bool isLoop(const Vtx& v) const {
91 return std::find(m_loop_vertices.begin(), m_loop_vertices.end(), v) != m_loop_vertices.end();
92 }
93
95 const std::vector<Prt>& loop_particles() const {
96 return m_loop_particles;
97 }
98
100 const std::vector<Vtx>& loop_vertices() const {
101 return m_loop_vertices;
102 }
103
106 int findLoops(const Evt* evt, bool force) {
107 // If no event is passed, return -1 (an error code, basically)
108 if (!evt) return -1;
109 // If we have already indexed the event and are not forced to do it again, return
110 if (evt == m_evt && !force) return 0;
111
112 // Update the local event pointer. This is now the most recent event that has been indexed.
113 m_evt = evt;
114 // Clear the lists of particles and vertices. These will now refer to this event.
115 m_loop_particles.clear();
116 m_loop_vertices.clear();
117
118 // Create a map, keeping track of whether particles are in loops.
119 // 0 for "unknown", 1 for "in a loop", -1 for "not in a loop"
120 std::map<Prt, int> incycle;
121
122 // Easiest case to deal with: if a particle has no production or end vertex, it is not in a loop
123 for (const auto & p: *m_evt){
124 if (!p->end_vertex()||!p->production_vertex()){
125 incycle[p] = -1;
126 } else {
127 incycle[p] = 0;
128 }
129 }
130
131 // Now begin iterating through particles to try to find loops
132 size_t minincycle = m_evt->particles_size();
133 for (;;) {
134 // Number of particles still unknown in the current iteration
135 size_t unknown = 0;
136 for (const auto & p: *m_evt) {
137
138 // If the particle was already identified as in/not in a loop, skip it
139 if (incycle[p] != 0) continue;
140 // Otherwise it is unknown on this iteration
141 unknown++;
142
143 // Start from the end vertex of the particle
144 auto ev = p->end_vertex();
145 if (ev) {
146 // If the end vertex of a particle exclusively has particles not in loops
147 // then this particle cannot be in a loop either
148 bool goodo = true;
149 for (auto& po: *ev) goodo = goodo && (incycle[po] == -1);
150 if (goodo) incycle[p] = -1;
151 }
152
153 // Now start from the production vertex of the particle
154 auto pv = p->production_vertex();
155 if (pv) {
156 // If the production vertex of a particle exclusively has particles not
157 // in loops, then this particle cannot be in a loop either
158 bool goodi = true;
159 for (auto& pi: pv->particles_in()) goodi = goodi && (incycle[pi] == -1);
160 if (goodi) incycle[p] = -1;
161 }
162 }
163 // If the number of unknown particles has not changed, we are done iterating
164 if (minincycle == unknown) break;
165 // If the number of unknown particles has changed, update and iterate again
166 minincycle = std::min(minincycle, unknown);
167 } // Outer loop - iterate until all particles have been dealt with
168
169 // Any remaining particles that have not been identified are a part of a loop
170 // Add them to our list of looping particles
171 for (const auto & p: *m_evt){
172 if (incycle[p] == 0) incycle[p] = 1;
173 if (incycle[p] == 1) m_loop_particles.push_back(p);
174 }
175
176 // Now loop over all vertices.
177 for (auto & v: m_evt->vertices()) {
178 bool push = false;
179 // First check incoming particles.
180 // If any incoming are in loops, consider this vertex to be in a loop.
181 for ( auto& pin: v->particles_in()){
182 if (incycle[pin] == 1) {
183 push = true;
184 break;
185 }
186 } // End of loop over incoming particles
187 // In case we have not yet identified the vertex as being in a loop
188 // then check the outgoing particles from the vertex.
189 // If any outgoing are in loops, consider this vertex to be in a loop.
190 if(!push) {
191 for ( const auto& pou: *v) {
192 if (incycle[pou] == 1) {
193 push = true;
194 break;
195 }
196 }
197 }
198 // Update the records if this vertex is in a loop.
199 if (push) m_loop_vertices.push_back(v);
200 } // End of loop over vertices
201
202 // All finished. Return success.
203 return 0;
204 } // End of the findLoops function
205
206private:
207
209 const Evt* m_evt = nullptr;
210
212 std::vector<Prt> m_loop_particles;
213
215 std::vector<Vtx> m_loop_vertices;
216};
217}
218#endif
219#endif
#define pi
bool isLoop(const Prt &p) const
Is this particle in a loop?
Definition Loops.h:85
const Evt * m_evt
Local pointer to the event passed in the constructor or most recent call to findLoops.
Definition Loops.h:209
Loops(const Evt *evt)
Constructor passing the event; immediately index the event that is passed.
Definition Loops.h:80
Loops()
Default constructor.
Definition Loops.h:77
bool isLoop(const Vtx &v) const
Is this vertex in a loop?
Definition Loops.h:90
const std::vector< Vtx > & loop_vertices() const
Accessor: return the full list of vertices in loops.
Definition Loops.h:100
std::vector< Vtx > m_loop_vertices
List of all vertices in m_evt that are in loops.
Definition Loops.h:215
int findLoops(const Evt *evt, bool force)
Function that does the work to identify loops Pass the event of interest.
Definition Loops.h:106
const std::vector< Prt > & loop_particles() const
Accessor: return the full list of particles in loops.
Definition Loops.h:95
std::vector< Prt > m_loop_particles
List of all particles in m_evt that are in loops.
Definition Loops.h:212
int ev
Definition globals.cxx:25
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 is_same_vertex(const T1 &p1, const T2 &p2)
Method to establish if two vertices in the GenEvent actually represent the same vertex.
bool TruthLoopDetectionMethod1(const V &childOrigVtx, const T &parent)
Method 1: true if the parent is among the children produced at the child's production vertex,...
Definition Loops.h:32
bool TruthLoopDetectionMethod2(const T1 &child, const T2 &parent)
Method 2: true if the parent's production vertex is the child's decay vertex and the child's producti...
Definition Loops.h:47
bool TruthLoopDetectionMethod3(const V &childOrigVtx, const T &parent)
Protection against loops in the truth record (MCTruthClassifier legacy).
Definition Loops.h:21
A namespace for all vertexing packages and related stuff.