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 XAOD_ANALYSIS
6#ifndef TRUTHUTILS_LOOPS_H
7#define TRUTHUTILS_LOOPS_H
8#include <algorithm>
9#include <map>
10#include <vector>
11#include <memory>
12
13namespace MC {
14template <class Evt, class Prt, class Vtx>
15class Loops {
16public:
17
19 Loops() {}
20
22 Loops(const Evt* evt) {
23 findLoops(evt,true);
24 }
25
27 bool isLoop(const Prt& p) const {
28 return std::find(m_loop_particles.begin(), m_loop_particles.end(), p) != m_loop_particles.end();
29 }
30
32 bool isLoop(const Vtx& v) const {
33 return std::find(m_loop_vertices.begin(), m_loop_vertices.end(), v) != m_loop_vertices.end();
34 }
35
37 const std::vector<Prt>& loop_particles() const {
38 return m_loop_particles;
39 }
40
42 const std::vector<Vtx>& loop_vertices() const {
43 return m_loop_vertices;
44 }
45
48 int findLoops(const Evt* evt, bool force) {
49 // If the event is empty, return -1 (an error code, basically)
50 if (!evt) return -1;
51 // If we have already indexed the event and are not forced to do it again, return
52 if (evt == m_evt && !force) return 0;
53
54 // Update the local event pointer. This is now the most recent event that has been indexed.
55 m_evt = evt;
56 // Clear the lists of particles and vertices. These will now refer to this event.
57 m_loop_particles.clear();
58 m_loop_vertices.clear();
59
60 // Create a map, keeping track of whether particles are in loops.
61 // 0 for "unknown", 1 for "in a loop", -1 for "not in a loop"
62 std::map<Prt, int> incycle;
63
64 // Easiest case to deal with: if a particle has no production or end vertex, it is not in a loop
65 for (const auto & p: *m_evt){
66 if (!p->end_vertex()||!p->production_vertex()){
67 incycle[p] = -1;
68 } else {
69 incycle[p] = 0;
70 }
71 }
72
73 // Now begin interating through particles to try to find loops
74 size_t minincycle = m_evt->particles_size();
75 for (;;) {
76 // Number of particles still unknown in the current iteration
77 size_t unknown = 0;
78 for (const auto & p: *m_evt) {
79
80 // If the particle was already identified as in/not in a loop, skip it
81 if (incycle[p] != 0) continue;
82 // Otherwise it is unknown on this iteration
83 unknown++;
84
85 // Start from the end vertex of the particle
86 auto ev = p->end_vertex();
87 if (ev) {
88 // If the end vertex of a particle exclusively has particles not in loops
89 // then this particle cannot be in a loop either
90 bool goodo = true;
91 for (auto& po: *ev) goodo = goodo && (incycle[po] == -1);
92 if (goodo) incycle[p] = -1;
93 }
94
95 // Now start from the production vertex of the particle
96 auto pv = p->production_vertex();
97 if (pv) {
98 // If the production vertex of a particle exclusively has particles not
99 // in loops, then this particle cannot be in a loop either
100 bool goodi = true;
101 for (auto& pi: ev->particles_in()) goodi = goodi && (incycle[pi] == -1);
102 if (goodi) incycle[p] = -1;
103 }
104 }
105 // If the number of unknown particles has not changed, we are done iterating
106 if (minincycle == unknown) break;
107 // If the number of unknown particles has changed, update at iterate again
108 minincycle = std::min(minincycle, unknown);
109 } // Outer loop - iterate until all particles have been dealt with
110
111 // Any remaining particles that have not been identified are a part of a loop
112 // Add them to our list of looping particles
113 for (const auto & p: *m_evt){
114 if (incycle[p] == 0) incycle[p] = 1;
115 if (incycle[p] == 1) m_loop_particles.push_back(p);
116 }
117
118 // Now loop over all vertices.
119 for (auto & v: m_evt->vertices()) {
120 bool push = false;
121 // First check incoming particles.
122 // If any incoming are in loops, consider this vertex to be in a loop.
123 for ( auto& pin: v->particles_in()){
124 if (incycle[pin] == 1) {
125 push = true;
126 break;
127 }
128 } // End of loop over incoming particles
129 // In case we have not yet identified the vertex as being in a loop
130 // then check the outgoing particles from the vertex.
131 // If any outgoing are in loops, consider this vertex to be in a loop.
132 if(!push) {
133 for ( const auto& pou: *v) {
134 if (incycle[pou] == 1) {
135 push = true;
136 break;
137 }
138 }
139 }
140 // Update the records if this vertex is in a loop.
141 if (push) m_loop_vertices.push_back(v);
142 } // End of loop over vertices
143
144 // All finished. Return success.
145 return 0;
146 } // End of the findLoops function
147
148private:
149
151 const Evt* m_evt = nullptr;
152
154 std::vector<Prt> m_loop_particles;
155
157 std::vector<Vtx> m_loop_vertices;
158};
159}
160#endif
161#endif
#define pi
bool isLoop(const Prt &p) const
Is this particle in a loop?
Definition Loops.h:27
const Evt * m_evt
Local pointer to the event passed in the constructor or most recent call to findLoops.
Definition Loops.h:151
Loops(const Evt *evt)
Constructor passing the event; immediately index the event that is passed.
Definition Loops.h:22
Loops()
Default constructor.
Definition Loops.h:19
bool isLoop(const Vtx &v) const
Is this vertex in a loop?
Definition Loops.h:32
const std::vector< Vtx > & loop_vertices() const
Accessor: return the full list of vertices in loops.
Definition Loops.h:42
std::vector< Vtx > m_loop_vertices
List of all vertices in m_evt that are in loops.
Definition Loops.h:157
int findLoops(const Evt *evt, bool force)
Function that does the work to identify loops Pass the event of interest.
Definition Loops.h:48
const std::vector< Prt > & loop_particles() const
Accessor: return the full list of particles in loops.
Definition Loops.h:37
std::vector< Prt > m_loop_particles
List of all particles in m_evt that are in loops.
Definition Loops.h:154
int ev
Definition globals.cxx:25
A namespace for all vertexing packages and related stuff.