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