ATLAS Offline Software
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 <vector>
10 #include <memory>
11 namespace MC {
12 template <class Evt, class Prt, class Vtx>
13 class Loops {
14 public:
15  Loops() {}
16  Loops(const Evt* evt) {
17  findLoops(evt,true);
18  }
19  bool isLoop(const Prt& p) const {
20  return std::find(m_loop_particles.begin(), m_loop_particles.end(), p) != m_loop_particles.end();
21  }
22  bool isLoop(const Vtx& v) const {
23  return std::find(m_loop_vertices.begin(), m_loop_vertices.end(), v) != m_loop_vertices.end();
24  }
25  const std::vector<Prt>& loop_particles() const {
26  return m_loop_particles;
27  }
28  const std::vector<Vtx>& loop_vertices() const {
29  return m_loop_vertices;
30  }
31  int findLoops(const Evt* evt, bool force) {
32  if (!evt) return -1;
33  if (evt == m_evt && !force) return 0;
34  m_evt = evt;
35  m_loop_particles.clear();
36  m_loop_vertices.clear();
37  std::map<Prt, int> incycle;
38  for (const auto & p: *m_evt) if (!p->end_vertex()||!p->production_vertex()) incycle[p] = -1;
39  else incycle[p] = 0;
40  size_t minincycle = m_evt->particles_size();
41  for (;;) {
42  size_t unknown = 0;
43  for (const auto & p: *m_evt) {
44  if (incycle[p] != 0) continue;
45  unknown++;
46  auto ev = p->end_vertex();
47  if (ev) {
48  bool goodo = true;
49  for (auto& po: *ev) goodo = goodo && (incycle[po] == -1);
50  if (goodo) incycle[p] = -1;
51  }
52  auto pv = p->production_vertex();
53  if (pv) {
54  bool goodi = true;
55 #ifdef HEPMC3
56  for (auto& pi: ev->particles_in()) goodi = goodi && (incycle[pi] == -1);
57 #else
58  for (auto ip = ev->particles_in_const_begin();
59  ip != ev->particles_in_const_end();
60  ++ip)
61  {
62  goodi = goodi && (incycle[*ip] == -1);
63  }
64 #endif
65  if (goodi) incycle[p] = -1;
66  }
67  }
68  if (minincycle == unknown) break;
69  minincycle = std::min(minincycle, unknown);
70  }
71  for (const auto & p: *m_evt) if (incycle[p] == 0) incycle[p] = 1;
72 
73  for (const auto & p: *m_evt) if (incycle[p] == 1) m_loop_particles.push_back(p);
74 #ifdef HEPMC3
75  for (auto & v: m_evt->vertices()) {
76 #else
77  for (auto iv = m_evt->vertices_begin(); iv != m_evt->vertices_end(); ++iv) {
78  auto v = *iv;
79 #endif
80  bool push = false;
81 #ifdef HEPMC3
82  for ( auto& pin: v->particles_in()) if (incycle[pin] == 1) {
83 #else
84  for ( auto ipin = v->particles_in_const_begin();
85  ipin != v->particles_in_const_end();
86  ++ipin)
87  if (incycle[*ipin] == 1) {
88 #endif
89  push = true;
90  break;
91  }
92  if(!push) {
93  for ( const auto& pou: *v) {
94  if (incycle[pou] == 1) {
95  push = true;
96  break;
97  }
98  }
99  }
100  if (push) m_loop_vertices.push_back(v);
101  }
102  return 0;
103  }
104 
105 private:
106  const Evt* m_evt = nullptr;
107  std::vector<Prt> m_loop_particles;
108  std::vector<Vtx> m_loop_vertices;
109 };
110 }
111 #endif
112 #endif
MC::Loops::isLoop
bool isLoop(const Vtx &v) const
Definition: Loops.h:22
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
Vtx
calibdata.force
bool force
Definition: calibdata.py:19
MC::Loops::m_loop_vertices
std::vector< Vtx > m_loop_vertices
Definition: Loops.h:108
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
MC
Definition: HepMCHelpers.h:19
pi
#define pi
Definition: TileMuonFitter.cxx:65
MC::Loops::Loops
Loops()
Definition: Loops.h:15
MC::Loops::m_loop_particles
std::vector< Prt > m_loop_particles
Definition: Loops.h:107
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ev
int ev
Definition: globals.cxx:25
find_tgc_unfilled_channelids.ip
ip
Definition: find_tgc_unfilled_channelids.py:3
MC::Loops::m_evt
const Evt * m_evt
Definition: Loops.h:106
Muon::nsw::unknown
@ unknown
Definition: NSWTriggerElink.h:36
MC::Loops::loop_particles
const std::vector< Prt > & loop_particles() const
Definition: Loops.h:25
MC::Loops::isLoop
bool isLoop(const Prt &p) const
Definition: Loops.h:19
python.PyAthena.v
v
Definition: PyAthena.py:154
MC::Loops::loop_vertices
const std::vector< Vtx > & loop_vertices() const
Definition: Loops.h:28
MC::Loops
Definition: Loops.h:13
python.changerun.pv
pv
Definition: changerun.py:81
MC::Loops::findLoops
int findLoops(const Evt *evt, bool force)
Definition: Loops.h:31
MC::Loops::Loops
Loops(const Evt *evt)
Definition: Loops.h:16