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