HepMC event record
include/HepMC/GenVertex.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2015 The HepMC collaboration (see AUTHORS for details)
5 //
6 /// @file GenVertex.h
7 /// @brief Definition of \b class GenVertex
8 //
9 #ifndef HEPMC_GENVERTEX_H
10 #define HEPMC_GENVERTEX_H
11 
12 #include "HepMC/Data/SmartPointer.h"
13 #include "HepMC/Data/GenVertexData.h"
14 #include "HepMC/FourVector.h"
15 #include "HepMC/Common.h"
16 #include "HepMC/Errors.h"
17 
18 namespace HepMC {
19 
20 
21  using namespace std;
22 
23  // /** @brief Type of iteration. Used by backward-compatibility interface */
24  // #ifndef HEPMC_NO_DEPRECATED
25  // enum IteratorRange { parents, children, family, ancestors, descendants, relatives };
26  // #endif
27 
28 
29  class GenEvent;
30  class Attribute;
31 
32 
33  /// Stores vertex-related information
34  class GenVertex {
35 
36  /// @todo Are these really needed? Friends usually indicate a problem...
37  friend class GenEvent;
38  friend class SmartPointer<GenVertex>;
39 
40 
41  public:
42 
43  /// @name Constructors
44  //@{
45 
46  /// Default constructor
47  GenVertex( const FourVector& position = FourVector::ZERO_VECTOR() );
48 
49  /// Constructor based on vertex data
50  GenVertex( const GenVertexData& data );
51 
52  //@}
53 
54  public:
55 
56  /// @name Accessors
57  //@{
58 
59  /// Get parent event
60  /// @todo Should we be returning a smart ptr?
61  GenEvent* parent_event() const { return m_event; }
62 
63  /// Check if this vertex belongs to an event
64  /// @todo Needed? Wouldn't it be good enough to just rely on user testing nullness of parent_event()?
65  bool in_event() const { return parent_event() != NULL; }
66 
67  /// Get the vertex unique identifier
68  ///
69  /// @note This is not the same as id() in HepMC v2, which is now @c status()
70  int id() const { return m_id; }
71 
72  /// Get vertex status code
73  int status() const { return m_data.status; }
74  /// Set vertex status code
75  void set_status(int stat) { m_data.status = stat; }
76 
77  /// Get vertex data
78  const GenVertexData& data() const { return m_data; }
79 
80  /// Add incoming particle
81  void add_particle_in ( GenParticlePtr p);
82  /// Add outgoing particle
83  void add_particle_out( GenParticlePtr p);
84  /// Remove incoming particle
85  void remove_particle_in ( GenParticlePtr p);
86  /// Remove outgoing particle
87  void remove_particle_out( GenParticlePtr p);
88 
89  /// Get list of associated particles
90  /// @note Note relatively inefficient return by value
91  const vector<GenParticlePtr> particles(Relationship range) const;
92  /// Get list of incoming particles
93  const vector<GenParticlePtr>& particles_in() const { return m_particles_in; }
94  /// Get list of outgoing particles
95  const vector<GenParticlePtr>& particles_out() const { return m_particles_out; }
96 
97  /// @brief Get vertex position
98  ///
99  /// Returns the position of this vertex. If a position is not set on _this_ vertex,
100  /// the production vertices of ancestors are searched to find the inherited position.
101  /// FourVector(0,0,0,0) is returned if no position information is found.
102  ///
103  /// @todo We need a way to check if there is a position on _this_ vertex, without messing up the interface. Is has_position() too intrusive?
104  const FourVector& position() const;
105  /// @brief Check if position of this vertex is set
106  bool has_set_position() const { return !(m_data.position.is_zero()); }
107 
108  /// Set vertex position
109  void set_position(const FourVector& new_pos); //!<
110 
111  /// @todo We need a way to check if there is a position on _this_ vertex, without messing up the interface. Is has_position() too intrusive?
112 
113 
114  /// @brief Add event attribute to this vertex
115  ///
116  /// This will overwrite existing attribute if an attribute with
117  /// the same name is present. The attribute will be stored in the
118  /// parent_event(). @return false if there is no parent_event();
119  bool add_attribute(string name, shared_ptr<Attribute> att);
120 
121  /// @brief Get list of names of attributes assigned to this particle
122  vector<string> attribute_names() const;
123 
124  /// @brief Remove attribute
125  void remove_attribute(string name);
126 
127  /// @brief Get attribute of type T
128  template<class T>
129  shared_ptr<T> attribute(string name) const;
130 
131  /// @brief Get attribute of any type as string
132  string attribute_as_string(string name) const;
133 
134  /// @name Deprecated functionality
135  //@{
136 
137  #ifndef HEPMC_NO_DEPRECATED
138 
139  /// Get barcode
140  ///
141  /// @note Currently barcode = id
142  // int barcode() const { return m_id; }
143 
144  /// Add incoming particle by raw pointer
145  /// @deprecated Use GenVertex::add_particle_in( const GenParticlePtr &p ) instead
146  HEPMC_DEPRECATED("Use GenParticlePtr instead of GenParticle*")
147  void add_particle_in ( GenParticle *p ) { add_particle_in( GenParticlePtr(p) ); }
148 
149  /// Add outgoing particle by raw pointer
150  /// @deprecated Use GenVertex::add_particle_out( const GenParticlePtr &p ) instead
151  HEPMC_DEPRECATED("Use GenParticlePtr instead of GenParticle*")
152  void add_particle_out( GenParticle *p ) { add_particle_out( GenParticlePtr(p) ); }
153 
154  /// Define iterator by typedef
155  typedef vector<GenParticlePtr>::const_iterator particles_in_const_iterator;
156  /// Define iterator by typedef
157  typedef vector<GenParticlePtr>::const_iterator particles_out_const_iterator;
158  /// Define iterator by typedef
159  typedef vector<GenParticlePtr>::iterator particle_iterator;
160 
161  /// @deprecated Backward compatibility iterators
162  HEPMC_DEPRECATED("Iterate over std container particles_in() instead")
163  particles_in_const_iterator particles_in_const_begin() const { return m_particles_in.begin(); } //!< @deprecated Backward compatibility iterators
164 
165  /// @deprecated Backward compatibility iterators
166  HEPMC_DEPRECATED("Iterate over std container particles_in() instead")
167  particles_in_const_iterator particles_in_const_end() const { return m_particles_in.end(); } //!< @deprecated Backward compatibility iterators
168 
169  /// @deprecated Backward compatibility iterators
170  HEPMC_DEPRECATED("Iterate over std container particles_out() instead")
171  particles_out_const_iterator particles_out_const_begin() const { return m_particles_out.begin(); } //!< @deprecated Backward compatibility iterators
172 
173  /// @deprecated Backward compatibility iterators
174  HEPMC_DEPRECATED("Iterate over std container particles_out() instead")
175  particles_out_const_iterator particles_out_const_end() const { return m_particles_out.end(); }
176 
177  /// @deprecated Backward compatibility iterators
178  HEPMC_DEPRECATED("Use particles_in/out() functions instead")
179  particle_iterator particles_begin(IteratorRange range) {
180  if (range == parents) return m_particles_in.begin();
181  if (range == children) return m_particles_out.begin();
182  throw Exception("GenVertex::particles_begin: Only 'parents' and 'children' ranges allowed.");
183  }
184 
185  /// @deprecated Backward compatibility iterators
186  HEPMC_DEPRECATED("Use particles_in/out() functions instead")
187  particle_iterator particles_end(IteratorRange range) {
188  if (range == parents) return m_particles_in.end();
189  if (range == children) return m_particles_out.end();
190  throw Exception("GenVertex::particles_end: Only 'parents' and 'children' ranges allowed.");
191  }
192 
193  /// @deprecated Backward compatibility
194  HEPMC_DEPRECATED("Use particles_in().size() instead")
195  int particles_in_size() const { return m_particles_in.size(); }
196 
197  /// @deprecated Backward compatibility
198  HEPMC_DEPRECATED("Use particles_out().size() instead")
199  int particles_out_size() const { return m_particles_out.size(); }
200 
201  #endif
202 
203  //@}
204 
205 
206  private:
207 
208  /// @name Fields
209  //@{
210  GenEvent *m_event; //!< Parent event
211  int m_id; //!< Vertex id
212  GenVertexData m_data; //!< Vertex data
213 
214  vector<GenParticlePtr> m_particles_in; //!< Incoming particle list
215  vector<GenParticlePtr> m_particles_out; //!< Outgoing particle list
216  weak_ptr<GenVertex> m_this; //!< Pointer to shared pointer managing this vertex
217  //@}
218 
219  };
220 
221 
222 } // namespace HepMC
223 
224 #include "HepMC/GenEvent.h"
225 
226 /// @brief Get attribute of type T
227 template<class T>
228 HepMC::shared_ptr<T> HepMC::GenVertex::attribute(string name) const {
229  return parent_event()?
230  parent_event()->attribute<T>(name, id()): HepMC::shared_ptr<T>();
231 }
232 
233 #endif
const vector< GenParticlePtr > & particles_out() const
Get list of outgoing particles.
HEPMC_DEPRECATED("Use particles_out().size() instead") int particles_out_size() const
vector< GenParticlePtr >::const_iterator particles_out_const_iterator
Define iterator by typedef.
void set_status(int stat)
Set vertex status code.
SmartPointer< class GenParticle > GenParticlePtr
Smart pointer to GenParticle.
STL namespace.
static const FourVector & ZERO_VECTOR()
Static null FourVector = (0,0,0,0)
bool has_set_position() const
Check if position of this vertex is set.
HEPMC_DEPRECATED("Use particles_in().size() instead") int particles_in_size() const
Stores event-related information.
GenEvent * parent_event() const
Stores serializable vertex information.
vector< GenParticlePtr >::iterator particle_iterator
Define iterator by typedef.
int status() const
Get vertex status code.
void add_particle_out(GenParticle *p)
const vector< GenParticlePtr > & particles_in() const
Get list of incoming particles.
shared_ptr< T > attribute(string name) const
Get attribute of type T.
vector< GenParticlePtr >::const_iterator particles_in_const_iterator
Define iterator by typedef.
void add_particle_in(GenParticle *p)
Definition of template class SmartPointer.
const GenVertexData & data() const
Get vertex data.
Stores particle-related information.
Relationship
List of methods of searching starting from a particle or vertex.