HepMC3 event record library
Relatives.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2021 The HepMC collaboration (see AUTHORS for details)
5 //
6 ///
7 /// @file Relatives.cc
8 /// @brief Implementation of \b Relatives class
9 ///
10 #include "HepMC3/Relatives.h"
11 
12 namespace HepMC3 {
15 #ifdef _MSC_VER
18 #else
19 thread_local const Ancestors Relatives::ANCESTORS;
20 thread_local const Descendants Relatives::DESCENDANTS;
21 #endif
22 }
23 
24 namespace HepMC3 {
25 /// @brief Returns children of vertex, i.e. outgoing particles.
26 std::vector<HepMC3::GenParticlePtr> children(HepMC3::GenVertexPtr O) {
27  if (O) return O->particles_out();
28  return std::vector<HepMC3::GenParticlePtr>();
29 }
30 /// @brief Returns children of const vertex, i.e. outgoing particles.
31 std::vector<HepMC3::ConstGenParticlePtr> children(HepMC3::ConstGenVertexPtr O) {
32  if (O) return O->particles_out();
33  return std::vector<HepMC3::ConstGenParticlePtr>();
34 }
35 /// @brief Returns children of particle, i.e. the end vertex.
36 std::vector<HepMC3::GenVertexPtr> children(HepMC3::GenParticlePtr O) {
37  std::vector<HepMC3::GenVertexPtr> result;
38  if (O->end_vertex()) result.push_back(O->end_vertex());
39  return result;
40 }
41 /// @brief Returns children of const particle, i.e. the end vertex.
42 std::vector<HepMC3::ConstGenVertexPtr> children(HepMC3::ConstGenParticlePtr O) {
43  std::vector<HepMC3::ConstGenVertexPtr> result;
44  if (O->end_vertex()) result.push_back(O->end_vertex());
45  return result;
46 }
47 /// @brief Returns grandchildren of particle, i.e. the outgoing particles of the end vertex.
48 std::vector<HepMC3::GenParticlePtr> grandchildren(HepMC3::GenParticlePtr O) {
49  if (O) if (O->end_vertex()) return O->end_vertex()->particles_out();
50  return std::vector<HepMC3::GenParticlePtr> ();
51 }
52 /// @brief Returns grandchildren of const particle, i.e. the outgoing particles of the end vertex.
53 std::vector<HepMC3::ConstGenParticlePtr> grandchildren(HepMC3::ConstGenParticlePtr O) {
54  if (O) if (O->end_vertex()) return O->end_vertex()->particles_out();
55  return std::vector<HepMC3::ConstGenParticlePtr> ();
56 }
57 /// @brief Returns grandchildren of vertex, i.e. the end vertices of the outgoing particles.
58 std::vector<HepMC3::GenVertexPtr> grandchildren(HepMC3::GenVertexPtr O) {
59  std::vector<HepMC3::GenVertexPtr> result;
60  if (O) for (auto o: O->particles_out()) if (o->end_vertex()) result.push_back(o->end_vertex());
61  return result;
62 }
63 /// @brief Returns grandchildren of const vertex, i.e. the end vertices of the outgoing particles.
64 std::vector<HepMC3::ConstGenVertexPtr> grandchildren(HepMC3::ConstGenVertexPtr O) {
65  std::vector<HepMC3::ConstGenVertexPtr> result;
66  if (O) for (auto o:O->particles_out()) if (o->end_vertex()) result.push_back(o->end_vertex());
67  return result;
68 }
69 /// @brief Returns parents of vertex, i.e. incoming particles.
70 std::vector<HepMC3::GenParticlePtr> parents(HepMC3::GenVertexPtr O) {
71  if (O) return O->particles_in();
72  return std::vector<GenParticlePtr>();
73 }
74 /// @brief Returns parents of const vertex, i.e. incoming particles.
75 std::vector<HepMC3::ConstGenParticlePtr> parents(HepMC3::ConstGenVertexPtr O) {
76  if (O) return O->particles_in();
77  return std::vector<HepMC3::ConstGenParticlePtr>();
78 }
79 /// @brief Returns parents of particle, i.e. production vertex.
80 std::vector<HepMC3::GenVertexPtr> parents(HepMC3::GenParticlePtr O) {
81  std::vector<HepMC3::GenVertexPtr> result;
82  if (O->production_vertex()) result.push_back(O->production_vertex());
83  return result;
84 }
85 /// @brief Returns parents of const particle, i.e. production vertex.
86 std::vector<HepMC3::ConstGenVertexPtr> parents(HepMC3::ConstGenParticlePtr O) {
87  std::vector<HepMC3::ConstGenVertexPtr> result;
88  if (O->production_vertex()) result.push_back(O->production_vertex());
89  return result;
90 }
91 /// @brief Returns grandparents of particle, i.e. incoming particles of production vertex.
92 std::vector<HepMC3::GenParticlePtr> grandparents(HepMC3::GenParticlePtr O) {
93  if (O) if (O->production_vertex()) return O->production_vertex()->particles_in();
94  return std::vector<HepMC3::GenParticlePtr> ();
95 }
96 /// @brief Returns grandparents of const particle, i.e. incoming particles of production vertex.
97 std::vector<HepMC3::ConstGenParticlePtr> grandparents(HepMC3::ConstGenParticlePtr O) {
98  if (O) if (O->production_vertex()) return O->production_vertex()->particles_in();
99  return std::vector<HepMC3::ConstGenParticlePtr> ();
100 }
101 /// @brief Returns grandparents of vertex, i.e. production vertices of incoming particles.
102 std::vector<HepMC3::GenVertexPtr> grandparents(HepMC3::GenVertexPtr O) {
103  std::vector<HepMC3::GenVertexPtr> result;
104  if (O) for (auto o: O->particles_in()) if (o->production_vertex()) result.push_back(o->production_vertex());
105  return result;
106 }
107 /// @brief Returns grandparents of const vertex, i.e. production vertices of incoming particles.
108 std::vector<HepMC3::ConstGenVertexPtr> grandparents(HepMC3::ConstGenVertexPtr O) {
109  std::vector<HepMC3::ConstGenVertexPtr> result;
110  if (O) for (auto o:O->particles_in()) if (o->end_vertex()) result.push_back(o->production_vertex());
111  return result;
112 }
113 /// @brief Returns descendands of the same type, i.e. vertices for vertex and particles for particle
114 template <class O> std::vector<O> descendants_of_same_type(O obj)
115 {
116  std::vector<O> result = grandchildren(obj);
117  size_t gc = 0;
118  for (;;)
119  {
120  std::vector<O> temp;
121  for (; gc < result.size(); gc++)
122  {
123  auto temp0 = grandchildren(result[gc]);
124  temp.insert(temp.end(), temp0.begin(), temp0.end());
125  }
126  for (auto p2: temp) if (std::find(result.begin(), result.end(), p2) == result.end()) result.push_back(p2);
127  if (gc >= result.size()) break;
128  }
129  return result;
130 }
131 /// @brief Returns descendands of the other type, i.e. vertices for particle and particles for vertex
132 template <class O, class R> std::vector<R> descendants_of_other_type(O obj)
133 {
134  std::vector<R> localchildren = children(obj);
135  std::vector<R> result = localchildren;
136  for (auto c: localchildren)
137  {
138  std::vector<R> desc = descendants_of_same_type(c);
139  for (auto d: desc) if (std::find(result.begin(), result.end(), d) == result.end()) result.push_back(d);
140  }
141  return result;
142 }
143 /// @brief Returns ancestors of the same type, i.e. vertices for vertex and particles for particle
144 template <class O> std::vector<O> ancestors_of_same_type(O obj)
145 {
146  std::vector<O> result = grandparents(obj);
147  size_t gc = 0;
148  for (;;)
149  {
150  std::vector<O> temp;
151  for (; gc < result.size(); gc++)
152  {
153  auto temp0 = grandparents(result[gc]);
154  temp.insert(temp.end(), temp0.begin(), temp0.end());
155  }
156  for (auto p2: temp) if (std::find(result.begin(), result.end(), p2) == result.end()) result.push_back(p2);
157  if (gc >= result.size()) break;
158  }
159  return result;
160 }
161 /// @brief Returns ancestors of the other type, i.e. vertices for particle and particles for vertex
162 template <class O, class R> std::vector<R> ancestors_of_other_type(O obj)
163 {
164  std::vector<R> localparents = parents(obj);
165  std::vector<R> result = localparents;
166  for (auto c: localparents)
167  {
168  std::vector<R> desc = ancestors_of_same_type(c);
169  for (auto d: desc) if (std::find(result.begin(), result.end(), d) == result.end()) result.push_back(d);
170  }
171  return result;
172 }
173 
174 std::vector<HepMC3::ConstGenParticlePtr> descendant_particles(HepMC3::ConstGenVertexPtr obj) {
175  return descendants_of_other_type<HepMC3::ConstGenVertexPtr, HepMC3::ConstGenParticlePtr>(obj);
176 }
177 std::vector<HepMC3::GenParticlePtr> descendant_particles(HepMC3::GenVertexPtr obj) {
178  return descendants_of_other_type<HepMC3::GenVertexPtr, HepMC3::GenParticlePtr>(obj);
179 }
180 
181 std::vector<ConstGenVertexPtr> descendant_vertices(HepMC3::ConstGenParticlePtr obj) {
182  return descendants_of_other_type<HepMC3::ConstGenParticlePtr, HepMC3::ConstGenVertexPtr>(obj);
183 }
184 std::vector<HepMC3::GenVertexPtr> descendant_vertices(HepMC3::GenParticlePtr obj) {
185  return descendants_of_other_type<HepMC3::GenParticlePtr, HepMC3::GenVertexPtr>(obj);
186 }
187 
188 std::vector<HepMC3::ConstGenParticlePtr> ancestor_particles(HepMC3::ConstGenVertexPtr obj) {
189  return ancestors_of_other_type<HepMC3::ConstGenVertexPtr, HepMC3::ConstGenParticlePtr>(obj);
190 }
191 std::vector<HepMC3::GenParticlePtr> ancestor_particles(HepMC3::GenVertexPtr obj) {
192  return ancestors_of_other_type<HepMC3::GenVertexPtr, HepMC3::GenParticlePtr>(obj);
193 }
194 
195 std::vector<HepMC3::ConstGenVertexPtr> ancestor_vertices(HepMC3::ConstGenParticlePtr obj) {
196  return ancestors_of_other_type<HepMC3::ConstGenParticlePtr, HepMC3::ConstGenVertexPtr>(obj);
197 }
198 std::vector<HepMC3::GenVertexPtr> ancestor_vertices(HepMC3::GenParticlePtr obj) {
199  return ancestors_of_other_type<HepMC3::GenParticlePtr, HepMC3::GenVertexPtr>(obj);
200 }
201 
202 
203 std::vector<HepMC3::ConstGenParticlePtr> descendant_particles(HepMC3::ConstGenParticlePtr obj) { return descendants_of_same_type(obj); }
204 std::vector<HepMC3::GenParticlePtr> descendant_particles(HepMC3::GenParticlePtr obj) { return descendants_of_same_type(obj); }
205 std::vector<HepMC3::ConstGenVertexPtr> descendant_vertices(HepMC3::ConstGenVertexPtr obj) { return descendants_of_same_type(obj); }
206 std::vector<HepMC3::GenVertexPtr> descendant_vertices(HepMC3::GenVertexPtr obj) { return descendants_of_same_type(obj); }
207 std::vector<HepMC3::ConstGenParticlePtr> ancestor_particles(HepMC3::ConstGenParticlePtr obj) { return ancestors_of_same_type(obj); }
208 std::vector<HepMC3::GenParticlePtr> ancestor_particles(HepMC3::GenParticlePtr obj) { return ancestors_of_same_type(obj); }
209 std::vector<HepMC3::ConstGenVertexPtr> ancestor_vertices(HepMC3::ConstGenVertexPtr obj) { return ancestors_of_same_type(obj); }
210 std::vector<HepMC3::GenVertexPtr> ancestor_vertices(HepMC3::GenVertexPtr obj) { return ancestors_of_same_type(obj); }
211 std::vector<HepMC3::GenParticlePtr> children_particles(HepMC3::GenVertexPtr O) { return children(O); }
212 std::vector<HepMC3::ConstGenParticlePtr> children_particles(HepMC3::ConstGenVertexPtr O) { return children(O); }
213 std::vector<HepMC3::GenVertexPtr> children_vertices(HepMC3::GenParticlePtr O) { return children(O); }
214 std::vector<HepMC3::ConstGenVertexPtr> children_vertices(HepMC3::ConstGenParticlePtr O) { return children(O); }
215 std::vector<HepMC3::GenParticlePtr> grandchildren_particles(HepMC3::GenParticlePtr O) { return grandchildren(O); }
216 std::vector<HepMC3::ConstGenParticlePtr> grandchildren_particles(HepMC3::ConstGenParticlePtr O) { return grandchildren(O); }
217 std::vector<HepMC3::GenVertexPtr> grandchildren_vertices(HepMC3::GenVertexPtr O) { return grandchildren(O); }
218 std::vector<HepMC3::ConstGenVertexPtr> grandchildren_vertices(HepMC3::ConstGenVertexPtr O) { return grandchildren(O); }
219 std::vector<HepMC3::GenParticlePtr> parent_particles(HepMC3::GenVertexPtr O) { return parents(O); }
220 std::vector<HepMC3::ConstGenParticlePtr> parent_particles(HepMC3::ConstGenVertexPtr O) { return parents(O); }
221 std::vector<HepMC3::GenVertexPtr> parent_vertices(HepMC3::GenParticlePtr O) { return parents(O); }
222 std::vector<HepMC3::ConstGenVertexPtr> parent_vertices(HepMC3::ConstGenParticlePtr O) { return parents(O); }
223 std::vector<HepMC3::GenParticlePtr> grandparent_particles(HepMC3::GenParticlePtr O) { return grandparents(O); }
224 std::vector<HepMC3::ConstGenParticlePtr> grandparent_particles(HepMC3::ConstGenParticlePtr O) { return grandparents(O); }
225 std::vector<HepMC3::GenVertexPtr> grandparent_vertices(HepMC3::GenVertexPtr O) { return grandparents(O); }
226 std::vector<HepMC3::ConstGenVertexPtr> grandparent_vertices(HepMC3::ConstGenVertexPtr O) { return grandparents(O); }
227 
228 
229 } // namespace HepMC3
Defines helper classes to extract relatives of an input GenParticle or GenVertex.
static HEPMC3search_Relatives_EXPORT_API thread_local const Ancestors ANCESTORS
Ancestors.
Definition: Relatives.h:203
static HEPMC3search_Relatives_EXPORT_API const Parents PARENTS
Parents.
Definition: Relatives.h:201
static HEPMC3search_Relatives_EXPORT_API const Children CHILDREN
Children.
Definition: Relatives.h:202
static HEPMC3search_Relatives_EXPORT_API thread_local const Descendants DESCENDANTS
Descendants.
Definition: Relatives.h:204
HepMC3 main namespace.
std::vector< HepMC3::GenParticlePtr > grandparents(HepMC3::GenParticlePtr O)
Returns grandparents of particle, i.e. incoming particles of production vertex.
Definition: Relatives.cc:92
std::vector< HepMC3::GenParticlePtr > parent_particles(HepMC3::GenVertexPtr O)
Return parent particles.
Definition: Relatives.cc:219
std::vector< HepMC3::ConstGenParticlePtr > ancestor_particles(HepMC3::ConstGenVertexPtr obj)
Return ancestor particles.
Definition: Relatives.cc:188
std::vector< HepMC3::GenParticlePtr > grandparent_particles(HepMC3::GenParticlePtr O)
Return grandparent particles.
Definition: Relatives.cc:223
std::vector< HepMC3::ConstGenVertexPtr > descendant_vertices(HepMC3::ConstGenParticlePtr obj)
Return descendant vertices.
Definition: Relatives.cc:181
std::vector< HepMC3::ConstGenVertexPtr > ancestor_vertices(HepMC3::ConstGenParticlePtr obj)
Return ancestor vertices.
Definition: Relatives.cc:195
RelativesInterface< Recursive< _children > > Descendants
Descendants is an alias to Recursion applied to the _children and wrapped in the Relatives interface.
Definition: Relatives.h:168
std::vector< O > descendants_of_same_type(O obj)
Returns descendands of the same type, i.e. vertices for vertex and particles for particle.
Definition: Relatives.cc:114
RelativesInterface< Recursive< _parents > > Ancestors
Ancestors is an alias to Recursion applied to the _parents and wrapped in the Relatives interface.
Definition: Relatives.h:166
std::vector< HepMC3::GenParticlePtr > parents(HepMC3::GenVertexPtr O)
Returns parents of vertex, i.e. incoming particles.
Definition: Relatives.cc:70
std::vector< HepMC3::ConstGenParticlePtr > descendant_particles(HepMC3::ConstGenVertexPtr obj)
Return descendant particles.
Definition: Relatives.cc:174
std::vector< HepMC3::GenParticlePtr > children_particles(HepMC3::GenVertexPtr O)
Return children particles.
Definition: Relatives.cc:211
std::vector< HepMC3::GenParticlePtr > grandchildren(HepMC3::GenParticlePtr O)
Returns grandchildren of particle, i.e. the outgoing particles of the end vertex.
Definition: Relatives.cc:48
std::vector< HepMC3::GenParticlePtr > children(HepMC3::GenVertexPtr O)
Returns children of vertex, i.e. outgoing particles.
Definition: Relatives.cc:26
std::vector< R > ancestors_of_other_type(O obj)
Returns ancestors of the other type, i.e. vertices for particle and particles for vertex.
Definition: Relatives.cc:162
std::vector< HepMC3::GenVertexPtr > grandchildren_vertices(HepMC3::GenVertexPtr O)
Return grandchildren vertices.
Definition: Relatives.cc:217
std::vector< HepMC3::GenParticlePtr > grandchildren_particles(HepMC3::GenParticlePtr O)
Return grandchildren particles.
Definition: Relatives.cc:215
std::vector< HepMC3::GenVertexPtr > parent_vertices(HepMC3::GenParticlePtr O)
Return parent vertices.
Definition: Relatives.cc:221
std::vector< R > descendants_of_other_type(O obj)
Returns descendands of the other type, i.e. vertices for particle and particles for vertex.
Definition: Relatives.cc:132
std::vector< HepMC3::GenVertexPtr > children_vertices(HepMC3::GenParticlePtr O)
Return children vertices.
Definition: Relatives.cc:213
RelativesInterface< _parents > Parents
alias of _parents wrapped in the Relatives interface
Definition: Relatives.h:162
RelativesInterface< _children > Children
alias of _children wrapped in the Relatives interface
Definition: Relatives.h:164
std::vector< HepMC3::GenVertexPtr > grandparent_vertices(HepMC3::GenVertexPtr O)
Return grandparent vertices.
Definition: Relatives.cc:225
std::vector< O > ancestors_of_same_type(O obj)
Returns ancestors of the same type, i.e. vertices for vertex and particles for particle.
Definition: Relatives.cc:144