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