HepMC3 event record library
Relatives.h
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.h
8 /// @brief Defines helper classes to extract relatives of an input GenParticle or GenVertex
9 ///
10 #ifndef HEPMC3_RELATIVES_H
11 #define HEPMC3_RELATIVES_H
12 
13 #include "HepMC3/GenParticle.h"
14 #include "HepMC3/GenVertex.h"
15 
16 
17 namespace HepMC3 {
18 
19 /// forward declare the Relatives interface in which _parents and _children are wrapped
20 template<typename T>
22 /// forward declare the recursion wrapper
23 template<typename T>
24 class Recursive;
25 
26 // forward declare _parents class
27 class _parents;
28 // forward declare _children class
29 class _children;
30 
31 /// alias of _parents wrapped in the Relatives interface
33 /// alias of _children wrapped in the Relatives interface
35 /// Ancestors is an alias to Recursion applied to the _parents and wrapped in the Relatives interface
37 /// Descendants is an alias to Recursion applied to the _children and wrapped in the Relatives interface
39 
40 /** @brief Define a common interface that all Relatives objects will satisfy
41  * Relatives provides an operator to get the relatives of a range of different
42  * GenObject types. The following are examples
43  *
44  * Relatives::ANCESTORS(GenParticlePtr);// returns ancestors of the particle
45  * Descendants descendants;
46  * descendants(GenVertexPtr);// descendants of the vertex
47  * vector<Relatives*> relations = {&Relatives::CHILDREN, &Relatives::DESCENDANTS, &Relatives::PARENTS, new Ancestors()}; // make a vector of Relatives
48  *
49  * You can also define your own relation and wrap it in the Relatives interface using
50  * Relatives * relo = new RelativesInterface<MyRelationClass>();
51  */
52 class Relatives {
53 
54 public:
55 
56  virtual std::vector<GenParticlePtr> operator()(GenParticlePtr input) const = 0;
57  virtual std::vector<ConstGenParticlePtr> operator()(ConstGenParticlePtr input) const = 0;
58  virtual std::vector<GenParticlePtr> operator()(GenVertexPtr input) const = 0;
59  virtual std::vector<ConstGenParticlePtr> operator()(ConstGenVertexPtr input) const = 0;
60 
61  static const Parents PARENTS;
62  static const Children CHILDREN;
63  static thread_local const Ancestors ANCESTORS;
64  static thread_local const Descendants DESCENDANTS;
65 };
66 
67 /** @brief wrap a templated class that implements Relatives
68  * Since we need to template the functionality on the input
69  * type (GenParticlePtr, ConstGenVertexPtr etc.) we must wrap a
70  * class that has a templated operator in this that provides the
71  * Relatives interface and calls through to the underlying template
72  * method.
73  */
74 template<typename Relative_type>
75 class RelativesInterface : public Relatives {
76 
77 public:
78 
79  //RelativesInterface(Relative_type relatives): _internal(relatives){}
80 
81  constexpr RelativesInterface() {}
82 
83  GenParticles_type<GenParticlePtr> operator()(GenParticlePtr input) const override {return _internal(input);}
84  GenParticles_type<ConstGenParticlePtr> operator()(ConstGenParticlePtr input) const override {return _internal(input);}
85  GenParticles_type<GenVertexPtr> operator()(GenVertexPtr input) const override {return _internal(input);}
86  GenParticles_type<ConstGenVertexPtr> operator()(ConstGenVertexPtr input) const override {return _internal(input);}
87 
88 private:
89 
90  Relative_type _internal;
91 
92 };
93 /** @brief Recursive */
94 template<typename Relation_type>
95 class Recursive {
96 
97 public:
98 
99  template<typename GenObject_type>
100  GenParticles_type<GenObject_type> operator()(GenObject_type input) const {
101  for(auto obj: m_checkedObjects) {
102  delete obj;
103  }
104  m_checkedObjects.clear();
105  return _recursive(input);
106  }
107 
108 private:
109 
110  template<typename GenObject_type, typename dummy>
111  GenParticles_type<GenObject_type> _recursive(GenObject_type input) const ;
112 
113  GenParticles_type<GenVertexPtr> _recursive(GenVertexPtr input) const {
114 
115  GenParticles_type <GenVertexPtr> results;
116  if ( !input ) return results;
117  for(auto v: m_checkedObjects) {
118  if(v->id() == input->id()) return results;
119  }
120 
121  m_checkedObjects.emplace_back(new idInterface<GenVertexPtr>(input));
122 
123  for(auto p: m_applyRelation(input)) {
124  results.emplace_back(p);
125  GenParticles_type <GenVertexPtr> tmp = _recursive(p);
126  results.insert(results.end(),
127  std::make_move_iterator(tmp.begin()),
128  std::make_move_iterator(tmp.end()));
129  }
130 
131  return results;
132  }
133 
134  GenParticles_type<ConstGenVertexPtr> _recursive(ConstGenVertexPtr input) const {
135 
136  GenParticles_type <ConstGenVertexPtr> results;
137  if ( !input ) return results;
138  for(auto v: m_checkedObjects) {
139  if(v->id() == input->id()) return results;
140  }
141 
142  m_checkedObjects.emplace_back(new idInterface<ConstGenVertexPtr>(input));
143 
144  for(auto p: m_applyRelation(input)) {
145  results.emplace_back(p);
146  GenParticles_type <ConstGenVertexPtr> tmp = _recursive(p);
147  results.insert(results.end(),
148  std::make_move_iterator(tmp.begin()),
149  std::make_move_iterator(tmp.end()));
150  }
151 
152  return results;
153  }
154 
155  GenParticles_type<GenParticlePtr> _recursive(GenParticlePtr input) const {
156  return _recursive(m_applyRelation.vertex(input));
157  }
158  GenParticles_type<ConstGenParticlePtr> _recursive(ConstGenParticlePtr input) const {
159  return _recursive(m_applyRelation.vertex(input));
160  }
161 
162 
163  /** @brief hasID */
164  class hasId {
165 
166  public:
167  virtual ~hasId() {}
168  virtual int id() const = 0;
169  };
170  /** @brief iDinterface */
171  template<typename ID_type>
172  class idInterface : public hasId {
173 
174  public:
175  constexpr idInterface(ID_type genObject): m_object(genObject) {}
176  int id() const {return m_object->id();}
177 
178  private:
179 
180  ID_type m_object;
181 
182  };
183 
184  Relation_type m_applyRelation;
185  mutable std::vector<hasId*> m_checkedObjects;
186 
187 };
188 
189 /** @brief Provides operator to find the parent particles of a Vertex or Particle
190  *
191  * Note you would usually not instantiate this directly, but wrap it in a RelativesInterface
192  */
193 class _parents {
194 
195 public:
196 
197  template<typename GenObject_type, typename dummy>
198  GenParticles_type<GenObject_type> operator()(GenObject_type input) const;
199 
200  template<typename GenObject_type, typename std::enable_if<std::is_same<GenVertex, typename std::remove_const<typename GenObject_type::element_type>::type>::value, int*>::type = nullptr>
201  GenParticles_type<GenObject_type> operator()(GenObject_type input) const {return input->particles_in();}
202 
203  template<typename GenObject_type, typename std::enable_if<std::is_same<GenParticle, typename std::remove_const<typename GenObject_type::element_type>::type>::value, int*>::type = nullptr>
204  GenParticles_type<GenObject_type> operator()(GenObject_type input) const {return (*this)(vertex(input));}
205 
206  template<typename GenObject_type>
207  GenVertex_type<GenObject_type> vertex(GenObject_type input) const {return input->production_vertex();}
208 
209 };
210 
211 /** @brief Provides operator to find the child particles of a Vertex or Particle
212  *
213  * Note you would usually not instantiate this directly, but wrap it in a RelativesInterface
214  */
215 class _children {
216 
217 public:
218 
219  template<typename GenObject_type, typename dummy>
220  GenParticles_type<GenObject_type> operator()(GenObject_type input) const;
221 
222  template<typename GenObject_type, typename std::enable_if<std::is_same<GenVertex, typename std::remove_const<typename GenObject_type::element_type>::type>::value, int*>::type = nullptr>
223  GenParticles_type<GenObject_type> operator()(GenObject_type input) const {return input->particles_out();}
224 
225  template<typename GenObject_type, typename std::enable_if<std::is_same<GenParticle, typename std::remove_const<typename GenObject_type::element_type>::type>::value, int*>::type = nullptr>
226  GenParticles_type<GenObject_type> operator()(GenObject_type input) const {return (*this)(vertex(input));}
227 
228  template<typename GenObject_type>
229  GenVertex_type<GenObject_type> vertex(GenObject_type input) const {return input->end_vertex();}
230 
231 };
232 
233 }
234 
235 
236 namespace HepMC3 {
237 
238 std::vector<HepMC3::GenParticlePtr> children_particles(HepMC3::GenVertexPtr O); ///< Return children particles
239 std::vector<HepMC3::ConstGenParticlePtr> children_particles(HepMC3::ConstGenVertexPtr O); ///< Return children particles
240 std::vector<HepMC3::GenVertexPtr> children_vertices(HepMC3::GenParticlePtr O); ///< Return children vertices
241 std::vector<HepMC3::ConstGenVertexPtr> children_vertices(HepMC3::ConstGenParticlePtr O); ///< Return children vertices
242 std::vector<HepMC3::GenParticlePtr> grandchildren_particles(HepMC3::GenParticlePtr O); ///< Return grandchildren particles
243 std::vector<HepMC3::ConstGenParticlePtr> grandchildren_particles(HepMC3::ConstGenParticlePtr O); ///< Return grandchildren particles
244 std::vector<HepMC3::GenVertexPtr> grandchildren_vertices(HepMC3::GenVertexPtr O); ///< Return grandchildren vertices
245 std::vector<HepMC3::ConstGenVertexPtr> grandchildren_vertices(HepMC3::ConstGenVertexPtr O); ///< Return grandchildren vertices
246 std::vector<HepMC3::GenParticlePtr> parent_particles(HepMC3::GenVertexPtr O); ///< Return parent particles
247 std::vector<HepMC3::ConstGenParticlePtr> parent_particles(HepMC3::ConstGenVertexPtr O); ///< Return parent particles
248 std::vector<HepMC3::GenVertexPtr> parent_vertices(HepMC3::GenParticlePtr O); ///< Return parent vertices
249 std::vector<HepMC3::ConstGenVertexPtr> parent_vertices(HepMC3::ConstGenParticlePtr O); ///< Return parent vertices
250 std::vector<HepMC3::GenParticlePtr> grandparent_particles(HepMC3::GenParticlePtr O); ///< Return grandparent particles
251 std::vector<HepMC3::ConstGenParticlePtr> grandparent_particles(HepMC3::ConstGenParticlePtr O); ///< Return grandparent particles
252 std::vector<HepMC3::GenVertexPtr> grandparent_vertices(HepMC3::GenVertexPtr O); ///< Return grandparent vertices
253 std::vector<HepMC3::ConstGenVertexPtr> grandparent_vertices(HepMC3::ConstGenVertexPtr O); ///< Return grandparent vertices
254 std::vector<HepMC3::ConstGenParticlePtr> descendant_particles(HepMC3::ConstGenVertexPtr obj); ///< Return descendant particles
255 std::vector<HepMC3::GenParticlePtr> descendant_particles(HepMC3::GenVertexPtr obj); ///< Return descendant particles
256 std::vector<HepMC3::ConstGenParticlePtr> descendant_particles(HepMC3::ConstGenParticlePtr obj); ///< Return descendant particles
257 std::vector<HepMC3::GenParticlePtr> descendant_particles(HepMC3::GenParticlePtr obj); ///< Return descendant particles
258 std::vector<HepMC3::ConstGenVertexPtr> descendant_vertices(HepMC3::ConstGenParticlePtr obj); ///< Return descendant vertices
259 std::vector<HepMC3::GenVertexPtr> descendant_vertices(HepMC3::GenParticlePtr obj); ///< Return descendant vertices
260 std::vector<HepMC3::ConstGenVertexPtr> descendant_vertices(HepMC3::ConstGenVertexPtr obj); ///< Return descendant vertices
261 std::vector<HepMC3::GenVertexPtr> descendant_vertices(HepMC3::GenVertexPtr obj); ///< Return descendant vertices
262 std::vector<HepMC3::ConstGenParticlePtr> ancestor_particles(HepMC3::ConstGenVertexPtr obj); ///< Return ancestor particles
263 std::vector<HepMC3::GenParticlePtr> ancestor_particles(HepMC3::GenVertexPtr obj); ///< Return ancestor particles
264 std::vector<HepMC3::ConstGenParticlePtr> ancestor_particles(HepMC3::ConstGenParticlePtr obj); ///< Return ancestor particles
265 std::vector<HepMC3::GenParticlePtr> ancestor_particles(HepMC3::GenParticlePtr obj); ///< Return ancestor particles
266 std::vector<HepMC3::ConstGenVertexPtr> ancestor_vertices(HepMC3::ConstGenParticlePtr obj); ///< Return ancestor vertices
267 std::vector<HepMC3::GenVertexPtr> ancestor_vertices(HepMC3::GenParticlePtr obj); ///< Return ancestor vertices
268 std::vector<HepMC3::ConstGenVertexPtr> ancestor_vertices(HepMC3::ConstGenVertexPtr obj); ///< Return ancestor vertices
269 std::vector<HepMC3::GenVertexPtr> ancestor_vertices(HepMC3::GenVertexPtr obj); ///< Return ancestor vertices
270 }
271 
272 #endif
273 
std::vector< HepMC3::GenVertexPtr > grandchildren_vertices(HepMC3::GenVertexPtr O)
Return grandchildren vertices.
Definition: Relatives.cc:208
Provides operator to find the child particles of a Vertex or Particle.
Definition: Relatives.h:215
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
Definition of class GenParticle.
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
Provides operator to find the parent particles of a Vertex or Particle.
Definition: Relatives.h:193
forward declare the Relatives interface in which _parents and _children are wrapped ...
Definition: Relatives.h:21
std::vector< HepMC3::GenVertexPtr > children_vertices(HepMC3::GenParticlePtr O)
Return children vertices.
Definition: Relatives.cc:204
Definition of class GenVertex.
Define a common interface that all Relatives objects will satisfy Relatives provides an operator to g...
Definition: Relatives.h:52
std::vector< HepMC3::GenVertexPtr > parent_vertices(HepMC3::GenParticlePtr O)
Return parent vertices.
Definition: Relatives.cc:212
forward declare the recursion wrapper
Definition: Relatives.h:24
std::vector< HepMC3::GenParticlePtr > parent_particles(HepMC3::GenVertexPtr O)
Return parent particles.
Definition: Relatives.cc:210
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
std::vector< HepMC3::ConstGenParticlePtr > ancestor_particles(HepMC3::ConstGenVertexPtr obj)
Return ancestor particles.
Definition: Relatives.cc:179
Definition: pytypes.h:904