HepMC event record
build/outputs/include/HepMC/GenEvent.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 ///
7 /// @file GenEvent.h
8 /// @brief Definition of \b class GenEvent
9 ///
10 #ifndef HEPMC_GENEVENT_H
11 #define HEPMC_GENEVENT_H
12 
13 #include "HepMC/Units.h"
14 
15 #if !defined(__CINT__)
16 #include "HepMC/Data/SmartPointer.h"
17 #include "HepMC/Errors.h"
18 #include "HepMC/GenHeavyIon.h"
19 #include "HepMC/GenPdfInfo.h"
20 #include "HepMC/GenCrossSection.h"
21 #include "HepMC/GenRunInfo.h"
22 #endif // __CINT__
23 
24 #ifdef HEPMC_ROOTIO
25 class TBuffer;
26 #endif
27 
28 
29 namespace HepMC {
30 
31 struct GenEventData;
32 
33 /// @brief Stores event-related information
34 ///
35 /// Manages event-related information.
36 /// Contains lists of GenParticle and GenVertex objects
37 class GenEvent {
38 
39 public:
40 
41  /// @brief Event constructor without a run
43  Units::LengthUnit length_unit = Units::MM);
44 
45  #if !defined(__CINT__)
46 
47  /// @brief Constructor with associated run
48  GenEvent(shared_ptr<GenRunInfo> run,
50  Units::LengthUnit length_unit = Units::MM);
51 
52 
53  /// @name Particle and vertex access
54  //@{
55 
56  /// @brief Get list of particles (const)
57  const std::vector<GenParticlePtr>& particles() const { return m_particles; }
58  /// @brief Get list of vertices (const)
59  const std::vector<GenVertexPtr>& vertices() const { return m_vertices; }
60 
61 
62  /// @brief Get/set list of particles (non-const)
63  std::vector<GenParticlePtr>& particles() { return m_particles; }
64  /// @brief Get/set list of vertices (non-const)
65  std::vector<GenVertexPtr>& vertices() { return m_vertices; }
66 
67  //@}
68 
69 
70  /// @name Event weights
71  //@{
72 
73  /// Get event weight values as a vector
74  const std::vector<double>& weights() const { return m_weights; }
75  /// Get event weights as a vector (non-const)
76  std::vector<double>& weights() { return m_weights; }
77  /// Get event weight accessed by index (or the canonical/first one if there is no argument)
78  /// @note It's the user's responsibility to ensure that the given index exists!
79  double weight(size_t index=0) const { return weights().at(index); }
80  /// Get event weight accessed by weight name
81  /// @note Requires there to be an attached GenRunInfo, otherwise will throw an exception
82  /// @note It's the user's responsibility to ensure that the given name exists!
83  double weight(const std::string& name) const {
84  if (!run_info()) throw WeightError("GenEvent::weight(str): named access to event weights requires the event to have a GenRunInfo");
85  return weight(run_info()->weight_index(name));
86  }
87  /// Get event weight names, if there are some
88  /// @note Requires there to be an attached GenRunInfo with registered weight names, otherwise will throw an exception
89  const std::vector<std::string>& weight_names(const std::string& /*name*/) const {
90  if (!run_info()) throw WeightError("GenEvent::weight_names(): access to event weight names requires the event to have a GenRunInfo");
91  const std::vector<std::string>& weightnames = run_info()->weight_names();
92  if (weightnames.empty()) throw WeightError("GenEvent::weight_names(): no event weight names are registered for this run");
93  return weightnames;
94  }
95 
96  //@}
97 
98 
99  /// @name Auxiliary info and event metadata
100  //@{
101 
102  /// @brief Get a pointer to the the GenRunInfo object.
103  shared_ptr<GenRunInfo> run_info() const {
104  return m_run_info;
105  }
106  /// @brief Set the GenRunInfo object by smart pointer.
107  void set_run_info(shared_ptr<GenRunInfo> run) {
108  m_run_info = run;
109  }
110 
111  /// @brief Get event number
112  int event_number() const { return m_event_number; }
113  /// @brief Set event number
114  void set_event_number(int num) { m_event_number = num; }
115 
116  /// @brief Get momentum unit
118  /// @brief Get length unit
119  const Units::LengthUnit& length_unit() const { return m_length_unit; }
120  /// @brief Change event units
121  /// Converts event from current units to new ones
122  void set_units( Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit);
123 
124  #ifndef HEPMC_NO_DEPRECATED
125  /// Converts event from current units to new ones (compatibility name)
126  void use_units( Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit) {
127  set_units(new_momentum_unit, new_length_unit);
128  }
129  #endif
130 
131  /// @brief Get heavy ion generator additional information
132  const GenHeavyIonPtr heavy_ion() const { return attribute<GenHeavyIon>("GenHeavyIon"); }
133  /// @brief Set heavy ion generator additional information
134  void set_heavy_ion(const GenHeavyIonPtr &hi) { add_attribute("GenHeavyIon",hi); }
135 
136  /// @brief Get PDF information
137  const GenPdfInfoPtr pdf_info() const { return attribute<GenPdfInfo>("GenPdfInfo"); }
138  /// @brief Set PDF information
139  void set_pdf_info(const GenPdfInfoPtr &pi) { add_attribute("GenPdfInfo",pi); }
140 
141  /// @brief Get cross-section information
142  const GenCrossSectionPtr cross_section() const { return attribute<GenCrossSection>("GenCrossSection"); }
143  /// @brief Set cross-section information
144  void set_cross_section(const GenCrossSectionPtr &cs) { add_attribute("GenCrossSection",cs); }
145 
146  //@}
147 
148 
149  /// @name Event position
150  //@{
151 
152  /// Vertex representing the overall event position
153  const FourVector& event_pos() const;
154 
155  /// @brief Vector of beam particles
156  const std::vector<GenParticlePtr>& beams() const;
157 
158  /// @brief Shift position of all vertices in the event by @a delta
159  void shift_position_by( const FourVector & delta );
160 
161  /// @brief Shift position of all vertices in the event to @a op
162  void shift_position_to( const FourVector & newpos ) {
163  const FourVector delta = newpos - event_pos();
164  shift_position_by(delta);
165  }
166 
167  //@}
168 
169 
170  /// @name Additional attributes
171  //@{
172  /// @brief Add event attribute to event
173  ///
174  /// This will overwrite existing attribute if an attribute
175  /// with the same name is present
176  void add_attribute(const string &name, const shared_ptr<Attribute> &att, int id = 0) {
177  if ( att ) m_attributes[name][id] = att;
178  }
179 
180  /// @brief Remove attribute
181  void remove_attribute(const string &name, int id = 0);
182 
183  /// @brief Get attribute of type T
184  template<class T>
185  shared_ptr<T> attribute(const string &name, int id = 0) const;
186 
187  /// @brief Get attribute of any type as string
188  string attribute_as_string(const string &name, int id = 0) const;
189 
190  /// @brief Get list of attribute names
191  std::vector<string> attribute_names(int id = 0) const;
192 
193  /// @brief Get list of attributes
194  const std::map< string, std::map<int, shared_ptr<Attribute> > >& attributes() const { return m_attributes; }
195 
196  //@}
197 
198 
199  /// @name Particle and vertex modification
200  //@{
201 
202  /// @brief Add particle
203  void add_particle( GenParticlePtr p );
204 
205  /// @brief Add vertex
206  void add_vertex( GenVertexPtr v );
207 
208  /// @brief Remove particle from the event
209  ///
210  /// This function will remove whole sub-tree starting from this particle
211  /// if it is the only incoming particle of this vertex.
212  /// It will also production vertex of this particle if this vertex
213  /// has no more outgoing particles
215 
216  /// @brief Remove a set of particles
217  ///
218  /// This function follows rules of GenEvent::remove_particle to remove
219  /// a list of particles from the event.
220  void remove_particles( std::vector<GenParticlePtr> v );
221 
222  /// @brief Remove vertex from the event
223  ///
224  /// This will remove all sub-trees of all outgoing particles of this vertex
225  /// @todo Optimize. Currently each particle/vertex is erased separately
226  void remove_vertex( GenVertexPtr v );
227 
228  /// @brief Add whole tree in topological order
229  ///
230  /// This function will find the beam particles (particles
231  /// that have no production vertices or their production vertices
232  /// have no particles) and will add the whole decay tree starting from
233  /// these particles.
234  ///
235  /// @note Any particles on this list that do not belong to the tree
236  /// will be ignored.
237  void add_tree( const std::vector<GenParticlePtr> &particles );
238 
239  /// @brief Reserve memory for particles and vertices
240  ///
241  /// Helps optimize event creation when size of the event is known beforehand
242  void reserve(unsigned int particles, unsigned int vertices = 0);
243 
244  /// @brief Remove contents of this event
245  void clear();
246 
247  //@}
248 
249  /// @name Deprecated functionality
250  //@{
251 
252  #ifndef HEPMC_NO_DEPRECATED
253 
254  /// @brief Add particle by raw pointer
255  /// @deprecated Use GenEvent::add_particle( const GenParticlePtr& ) instead
256  HEPMC_DEPRECATED("Use GenParticlePtr instead of GenParticle*")
257  void add_particle( GenParticle *p );
258 
259  /// @brief Add vertex by raw pointer
260  /// @deprecated Use GenEvent::add_vertex( const GenVertexPtr& ) instead
261  HEPMC_DEPRECATED("Use GenVertexPtr instead of GenVertex*")
262  void add_vertex ( GenVertex *v );
263 
264  /// @brief Set heavy ion generator additional information by raw pointer
265  /// @deprecated Use GenEvent::set_heavy_ion( GenHeavyIonPtr hi) instead
266  HEPMC_DEPRECATED("Use GenHeavyIonPtr instead of GenHeavyIon*")
267  void set_heavy_ion(GenHeavyIon *hi);
268 
269  /// @brief Set PDF information by raw pointer
270  /// @deprecated Use GenEvent::set_pdf_info( GenPdfInfoPtr pi) instead
271  HEPMC_DEPRECATED("Use GenPdfInfoPtr instead of GenPdfInfo*")
272  void set_pdf_info(GenPdfInfo *pi);
273 
274  /// @brief Set cross-section information by raw pointer
275  /// @deprecated Use GenEvent::set_cross_section( GenCrossSectionPtr cs) instead
278 
279  /// @deprecated Backward compatibility typedefs
280  typedef std::vector<double> GenWeights;
281  /// @deprecated Backward compatibility typedefs
282  typedef std::vector<double> WeightContainer;
283 
284  /// @deprecated Backward compatibility iterators
285  typedef std::vector<GenParticlePtr>::iterator particle_iterator;
286  /// @deprecated Backward compatibility iterators
287  typedef std::vector<GenParticlePtr>::const_iterator particle_const_iterator;
288 
289  /// @deprecated Backward compatibility iterators
290  typedef std::vector<GenVertexPtr>::iterator vertex_iterator;
291  /// @deprecated Backward compatibility iterators
292  typedef std::vector<GenVertexPtr>::const_iterator vertex_const_iterator;
293 
294  /// @deprecated Backward compatibility iterators
295  HEPMC_DEPRECATED("Iterate over std container particles() instead")
297 
298  /// @deprecated Backward compatibility iterators
299  HEPMC_DEPRECATED("Iterate over std container particles() instead")
301 
302  /// @deprecated Backward compatibility iterators
303  HEPMC_DEPRECATED("Iterate over std container particles() instead")
305 
306  /// @deprecated Backward compatibility iterators
307  HEPMC_DEPRECATED("Iterate over std container particles() instead")
309 
310  /// @deprecated Backward compatibility iterators
311  HEPMC_DEPRECATED("Iterate over std container vertices() instead")
313 
314  /// @deprecated Backward compatibility iterators
315  HEPMC_DEPRECATED("Iterate over std container vertices() instead")
317 
318  /// @deprecated Backward compatibility iterators
319  HEPMC_DEPRECATED("Iterate over std container vertices() instead")
320  vertex_const_iterator vertices_begin() const { return m_vertices.begin(); }
321 
322  /// @deprecated Backward compatibility iterators
323  HEPMC_DEPRECATED("Iterate over std container vertices() instead")
325 
326  /// @deprecated Backward compatibility
327  HEPMC_DEPRECATED("Use particles().size() instead")
328  int particles_size() const { return m_particles.size(); }
329 
330  /// @deprecated Backward compatibility
331  HEPMC_DEPRECATED("Use particles().empty() instead")
332  bool particles_empty() const { return m_particles.empty(); }
333 
334  /// @deprecated Backward compatibility
335  HEPMC_DEPRECATED("Use vertices().size() instead")
336  int vertices_size() const { return m_vertices.size(); }
337 
338  /// @deprecated Backward compatibility
339  HEPMC_DEPRECATED("Use vertices().empty() instead")
340  bool vertices_empty() const { return m_vertices.empty(); }
341 
342  /// @brief Test to see if we have exactly two particles in event_pos() vertex
343  /// @deprecated Backward compatibility
344  HEPMC_DEPRECATED("Use beams() to access beam particles")
345  bool valid_beam_particles() const;
346 
347  /// @brief Get first two particles of the event_pos() vertex
348  /// @deprecated Backward compatibility
349  HEPMC_DEPRECATED("Use beams() to access beam particles")
351 
352  /// @brief Set incoming beam particles
353  /// @deprecated Backward compatibility
354  /// @todo Set/require status = 4 at the same time?
355  HEPMC_DEPRECATED("instead add particle without production vertex to the event")
356  void set_beam_particles(const GenParticlePtr& p1, const GenParticlePtr& p2);
357 
358  /// @brief Set incoming beam particles
359  /// @deprecated Backward compatibility
360  /// @todo Set/require status = 4 at the same time?
361  HEPMC_DEPRECATED("instead add particle without production vertex to the event")
362  void set_beam_particles(const std::pair<GenParticlePtr,GenParticlePtr>& p);
363 
364  #endif
365 
366  //@}
367 
368  #endif // __CINT__
369 
370 
371  /// @name Methods to fill GenEventData and to read it back
372  //@{
373 
374  /// @brief Fill GenEventData object
375  void write_data(GenEventData &data) const;
376 
377  /// @brief Fill GenEvent based on GenEventData
378  void read_data(const GenEventData &data);
379 
380  #ifdef HEPMC_ROOTIO
381  /// @brief ROOT I/O streamer
382  void Streamer(TBuffer &b);
383  //@}
384  #endif
385 
386 private:
387 
388  /// @name Fields
389  //@{
390 
391  #if !defined(__CINT__)
392 
393  /// List of particles
394  std::vector<GenParticlePtr> m_particles;
395  /// List of vertices
396  std::vector<GenVertexPtr> m_vertices;
397 
398  /// Event number
399  /// @todo Move to attributes?
401 
402  /// Event weights
403  std::vector<double> m_weights;
404 
405  /// Momentum unit
407  /// Length unit
409 
410  /// The root vertex is stored outside the normal vertices list to block user access to it
412 
413  /// Global run information.
414  shared_ptr<GenRunInfo> m_run_info;
415 
416  /// @brief Map of event, particle and vertex attributes
417  ///
418  /// Keys are name and ID (0 = event, <0 = vertex, >0 = particle)
419  mutable std::map< string, std::map<int, shared_ptr<Attribute> > > m_attributes;
420 
421  /// @brief Attribute map key type
422  typedef std::map< string, std::map<int, shared_ptr<Attribute> > >::value_type att_key_t;
423 
424  /// @brief Attribute map value type
425  typedef std::map<int, shared_ptr<Attribute> >::value_type att_val_t;
426  #endif // __CINT__
427 
428  //@}
429 
430 };
431 
432 
433 
434 #if !defined(__CINT__)
435 
436 //
437 // Template methods
438 //
439 
440 template<class T>
441 shared_ptr<T> GenEvent::attribute(const std::string &name, int id) const {
442 
443  std::map< string, std::map<int, shared_ptr<Attribute> > >::iterator i1 = m_attributes.find(name);
444  if( i1 == m_attributes.end() ) {
445  if ( id == 0 && run_info() ) {
446  return run_info()->attribute<T>(name);
447  }
448  return shared_ptr<T>();
449  }
450 
451  std::map<int, shared_ptr<Attribute> >::iterator i2 = i1->second.find(id);
452  if (i2 == i1->second.end() ) return shared_ptr<T>();
453 
454  if (!i2->second->is_parsed() ) {
455 
456  shared_ptr<T> att = make_shared<T>();
457  if ( att->from_string(i2->second->unparsed_string()) && att->init(*this) ) {
458  // update map with new pointer
459  i2->second = att;
460  return att;
461  } else {
462  return shared_ptr<T>();
463  }
464  }
465  else return dynamic_pointer_cast<T>(i2->second);
466 }
467 
468 #endif // __CINT__
469 
470 
471 #ifndef HEPMC_NO_DEPRECATED
472 
473 /// Deprecated backward compatibility typedef
474 ///
475 /// There is no distinct weight container type in HepMC3 -- the information
476 /// comes from several sources spread between GenEvent and GenRunInfo.
477 typedef std::vector<double> WeightContainer;
478 
479 #endif
480 
481 
482 } // namespace HepMC
483 
484 #endif
const GenCrossSectionPtr cross_section() const
Get cross-section information.
const std::vector< GenParticlePtr > & particles() const
Get list of particles (const)
const GenPdfInfoPtr pdf_info() const
Get PDF information.
Stores additional information about Heavy Ion generator.
Exception related to weight lookups, setting, and index consistency.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
string attribute_as_string(const string &name, int id=0) const
Get attribute of any type as string.
Definition: GenEvent.cc:555
void remove_particles(std::vector< GenParticlePtr > v)
Remove a set of particles.
Definition: GenEvent.cc:163
void add_vertex(GenVertexPtr v)
Add vertex.
Definition: GenEvent.cc:57
Stores additional information about PDFs.
void set_pdf_info(const GenPdfInfoPtr &pi)
Set PDF information.
std::map< string, std::map< int, shared_ptr< Attribute > > > m_attributes
Map of event, particle and vertex attributes.
std::pair< GenParticlePtr, GenParticlePtr > beam_particles() const
Get first two particles of the event_pos() vertex.
Definition: GenEvent.cc:532
Stores additional information about cross-section.
double weight(size_t index=0) const
bool valid_beam_particles() const
Test to see if we have exactly two particles in event_pos() vertex.
Definition: GenEvent.cc:527
shared_ptr< T > attribute(const string &name, int id=0) const
Get attribute of type T.
void set_event_number(int num)
Set event number.
int event_number() const
Get event number.
void remove_attribute(const string &name, int id=0)
Remove attribute.
Definition: GenEvent.cc:398
STL namespace.
const std::map< string, std::map< int, shared_ptr< Attribute > > > & attributes() const
Get list of attributes.
shared_ptr< class GenPdfInfo > GenPdfInfoPtr
Shared pointer to GenPdfInfo.
void set_units(Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit)
Change event units Converts event from current units to new ones.
Definition: GenEvent.cc:335
HEPMC_DEPRECATED("Use vertices().size() instead") int vertices_size() const
void add_tree(const std::vector< GenParticlePtr > &particles)
Add whole tree in topological order.
Definition: GenEvent.cc:262
void remove_particle(GenParticlePtr v)
Remove particle from the event.
Definition: GenEvent.cc:78
std::vector< double > & weights()
Get event weights as a vector (non-const)
void remove_vertex(GenVertexPtr v)
Remove vertex from the event.
Definition: GenEvent.cc:174
shared_ptr< class GenCrossSection > GenCrossSectionPtr
Shared pointer to GenCrossSection.
void shift_position_by(const FourVector &delta)
Shift position of all vertices in the event by delta.
Definition: GenEvent.cc:363
Stores vertex-related information.
void clear()
Remove contents of this event.
Definition: GenEvent.cc:374
void use_units(Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit)
Converts event from current units to new ones (compatibility name)
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Stores event-related information.
HEPMC_DEPRECATED("Use particles().size() instead") int particles_size() const
std::map< int, shared_ptr< Attribute > >::value_type att_val_t
Attribute map value type.
std::vector< GenVertexPtr > & vertices()
Get/set list of vertices (non-const)
HEPMC_DEPRECATED("Use vertices().empty() instead") bool vertices_empty() const
std::vector< GenParticlePtr >::iterator particle_iterator
void set_heavy_ion(const GenHeavyIonPtr &hi)
Set heavy ion generator additional information.
std::vector< GenParticlePtr > m_particles
List of particles.
Units::LengthUnit m_length_unit
Length unit.
std::vector< double > m_weights
Event weights.
const std::vector< GenParticlePtr > & beams() const
Vector of beam particles.
Definition: GenEvent.cc:359
GenVertexPtr m_rootvertex
The root vertex is stored outside the normal vertices list to block user access to it...
std::vector< string > attribute_names(int id=0) const
Get list of attribute names.
Definition: GenEvent.cc:408
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition: GenEvent.cc:478
void add_attribute(const string &name, const shared_ptr< Attribute > &att, int id=0)
Add event attribute to event.
std::vector< double > WeightContainer
void add_particle(GenParticlePtr p)
Add particle.
Definition: GenEvent.cc:42
std::vector< GenVertexPtr > m_vertices
List of vertices.
std::vector< GenVertexPtr >::iterator vertex_iterator
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.
std::map< string, std::map< int, shared_ptr< Attribute > > >::value_type att_key_t
Attribute map key type.
GenEvent(Units::MomentumUnit momentum_unit=Units::GEV, Units::LengthUnit length_unit=Units::MM)
Event constructor without a run.
Definition: GenEvent.cc:25
shared_ptr< GenRunInfo > m_run_info
Global run information.
const std::vector< GenVertexPtr > & vertices() const
Get list of vertices (const)
double weight(const std::string &name) const
shared_ptr< class GenHeavyIon > GenHeavyIonPtr
Shared pointer to GenHeavyIon.
void reserve(unsigned int particles, unsigned int vertices=0)
Reserve memory for particles and vertices.
Definition: GenEvent.cc:329
std::vector< GenParticlePtr > & particles()
Get/set list of particles (non-const)
void set_beam_particles(const GenParticlePtr &p1, const GenParticlePtr &p2)
Set incoming beam particles.
Definition: GenEvent.cc:541
std::vector< GenParticlePtr >::const_iterator particle_const_iterator
const FourVector & event_pos() const
Vertex representing the overall event position.
Definition: GenEvent.cc:355
HEPMC_DEPRECATED("Use particles().empty() instead") bool particles_empty() const
Definition of template class SmartPointer.
void set_cross_section(const GenCrossSectionPtr &cs)
Set cross-section information.
std::vector< GenVertexPtr >::const_iterator vertex_const_iterator
Units::MomentumUnit m_momentum_unit
Momentum unit.
const Units::LengthUnit & length_unit() const
Get length unit.
Stores serializable event information.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
const std::vector< double > & weights() const
Get event weight values as a vector.
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition: GenEvent.cc:420
const GenHeavyIonPtr heavy_ion() const
Get heavy ion generator additional information.
Stores particle-related information.
const std::vector< std::string > & weight_names(const std::string &) const