HepMC3 event record library
GenEvent.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 GenEvent.h
8 /// @brief Definition of \b class GenEvent
9 ///
10 #ifndef HEPMC3_GENEVENT_H
11 #define HEPMC3_GENEVENT_H
12 
13 #include "HepMC3/Units.h"
14 #include "HepMC3/GenParticle_fwd.h"
15 #include "HepMC3/GenVertex_fwd.h"
16 #include "HepMC3/GenPdfInfo_fwd.h"
17 #include "HepMC3/GenHeavyIon_fwd.h"
18 #include "HepMC3/GenCrossSection_fwd.h"
19 
20 #if !defined(__CINT__)
21 #include "HepMC3/GenHeavyIon.h"
22 #include "HepMC3/GenPdfInfo.h"
23 #include "HepMC3/GenCrossSection.h"
24 #include "HepMC3/GenRunInfo.h"
25 #include <mutex>
26 #endif // __CINT__
27 
28 #ifdef HEPMC3_ROOTIO
29 class TBuffer;
30 #endif
31 
32 
33 namespace HepMC3 {
34 
35 struct GenEventData;
36 
37 /// @brief Stores event-related information
38 ///
39 /// Manages event-related information.
40 /// Contains lists of GenParticle and GenVertex objects
41 class GenEvent {
42 
43 public:
44 
45  /// @brief Event constructor without a run
47  Units::LengthUnit length_unit = Units::MM);
48 
49 #if !defined(__CINT__)
50 
51  /// @brief Constructor with associated run
52  GenEvent(shared_ptr<GenRunInfo> run,
54  Units::LengthUnit length_unit = Units::MM);
55 
56  /// @brief Copy constructor
57  GenEvent(const GenEvent&);
58 
59  /// @brief Destructor
60  ~GenEvent();
61 
62  /// @brief Assignment operator
63  GenEvent& operator=(const GenEvent&);
64 
65  /// @name Particle and vertex access
66  //@{
67 
68  /// @brief Get list of particles (const)
69  const std::vector<ConstGenParticlePtr>& particles() const;
70  /// @brief Get list of vertices (const)
71  const std::vector<ConstGenVertexPtr>& vertices() const;
72 
73 
74  /// @brief Get/set list of particles (non-const)
75  const std::vector<GenParticlePtr>& particles() { return m_particles; }
76  /// @brief Get/set list of vertices (non-const)
77  const std::vector<GenVertexPtr>& vertices() { return m_vertices; }
78 
79  //@}
80 
81 
82  /// @name Event weights
83  //@{
84 
85  /// Get event weight values as a vector
86  const std::vector<double>& weights() const { return m_weights; }
87  /// Get event weights as a vector (non-const)
88  std::vector<double>& weights() { return m_weights; }
89  /// Get event weight accessed by index (or the canonical/first one if there is no argument)
90  /// @note It's the user's responsibility to ensure that the given index exists!
91  double weight(const unsigned long& index=0) const { return weights().at(index); }
92  /// Get event weight accessed by weight name
93  /// @note Requires there to be an attached GenRunInfo, otherwise will throw an exception
94  /// @note It's the user's responsibility to ensure that the given name exists!
95  double weight(const std::string& name) const {
96  if (!run_info()) throw std::runtime_error("GenEvent::weight(str): named access to event weights requires the event to have a GenRunInfo");
97  return weight(run_info()->weight_index(name));
98  }
99  /// Get event weight accessed by weight name
100  /// @note Requires there to be an attached GenRunInfo, otherwise will throw an exception
101  /// @note It's the user's responsibility to ensure that the given name exists!
102  double& weight(const std::string& name) {
103  if (!run_info()) throw std::runtime_error("GenEvent::weight(str): named access to event weights requires the event to have a GenRunInfo");
104  int pos=run_info()->weight_index(name);
105  if (pos<0) throw std::runtime_error("GenEvent::weight(str): no weight with given name in this run");
106  return m_weights[pos];
107  }
108  /// Get event weight names, if there are some
109  /// @note Requires there to be an attached GenRunInfo with registered weight names, otherwise will throw an exception
110  const std::vector<std::string>& weight_names() const {
111  if (!run_info()) throw std::runtime_error("GenEvent::weight_names(): access to event weight names requires the event to have a GenRunInfo");
112  const std::vector<std::string>& weightnames = run_info()->weight_names();
113  if (weightnames.empty()) throw std::runtime_error("GenEvent::weight_names(): no event weight names are registered for this run");
114  return weightnames;
115  }
116 
117  //@}
118 
119 
120  /// @name Auxiliary info and event metadata
121  //@{
122 
123  /// @brief Get a pointer to the the GenRunInfo object.
124  shared_ptr<GenRunInfo> run_info() const {
125  return m_run_info;
126  }
127  /// @brief Set the GenRunInfo object by smart pointer.
128  void set_run_info(shared_ptr<GenRunInfo> run) {
129  m_run_info = run;
130  if ( run && !run->weight_names().empty() )
131  m_weights.resize(run->weight_names().size(), 1.0);
132  }
133 
134  /// @brief Get event number
135  int event_number() const { return m_event_number; }
136  /// @brief Set event number
137  void set_event_number(const int& num) { m_event_number = num; }
138 
139  /// @brief Get momentum unit
141  /// @brief Get length unit
142  const Units::LengthUnit& length_unit() const { return m_length_unit; }
143  /// @brief Change event units
144  /// Converts event from current units to new ones
145  void set_units( Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit);
146 
147  /// @brief Get heavy ion generator additional information
148  GenHeavyIonPtr heavy_ion() { return attribute<GenHeavyIon>("GenHeavyIon"); }
149  /// @brief Get heavy ion generator additional information (const version)
150  ConstGenHeavyIonPtr heavy_ion() const { return attribute<GenHeavyIon>("GenHeavyIon"); }
151  /// @brief Set heavy ion generator additional information
152  void set_heavy_ion(GenHeavyIonPtr hi) { add_attribute("GenHeavyIon",hi); }
153 
154  /// @brief Get PDF information
155  GenPdfInfoPtr pdf_info() { return attribute<GenPdfInfo>("GenPdfInfo"); }
156  /// @brief Get PDF information (const version)
157  ConstGenPdfInfoPtr pdf_info() const { return attribute<GenPdfInfo>("GenPdfInfo"); }
158  /// @brief Set PDF information
159  void set_pdf_info(GenPdfInfoPtr pi) { add_attribute("GenPdfInfo",pi); }
160 
161  /// @brief Get cross-section information
162  GenCrossSectionPtr cross_section() { return attribute<GenCrossSection>("GenCrossSection"); }
163  /// @brief Get cross-section information (const version)
164  ConstGenCrossSectionPtr cross_section() const { return attribute<GenCrossSection>("GenCrossSection"); }
165  /// @brief Set cross-section information
166  void set_cross_section(GenCrossSectionPtr cs) { add_attribute("GenCrossSection",cs); }
167 
168  //@}
169 
170 
171  /// @name Event position
172  //@{
173 
174  /// Vertex representing the overall event position
175  const FourVector& event_pos() const;
176 
177  /// @brief Vector of beam particles
178  std::vector<ConstGenParticlePtr> beams() const;
179 
180  /// @brief Vector of beam particles
181  const std::vector<GenParticlePtr> & beams();
182 
183  /// @brief Shift position of all vertices in the event by @a delta
184  void shift_position_by( const FourVector & delta );
185 
186  /// @brief Shift position of all vertices in the event to @a op
187  void shift_position_to( const FourVector & newpos ) {
188  const FourVector delta = newpos - event_pos();
189  shift_position_by(delta);
190  }
191 
192  /// @brief Boost event using x,y,z components of @a v as velocities
193  bool boost( const FourVector& v );
194  /// @brief Rotate event using x,y,z components of @a v as rotation angles
195  bool rotate( const FourVector& v );
196  /// @brief Change sign of @a axis
197  bool reflect(const int axis);
198 
199  //@}
200 
201 
202  /// @name Additional attributes
203  //@{
204  /// @brief Add event attribute to event
205  ///
206  /// This will overwrite existing attribute if an attribute
207  /// with the same name is present
208  void add_attribute(const string &name, const shared_ptr<Attribute> &att, const int& id = 0) {
209  std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
210  if ( att ) {
211  m_attributes[name][id] = att;
212  att->m_event = this;
213  if ( id > 0 && id <= int(particles().size()) )
214  att->m_particle = particles()[id - 1];
215  if ( id < 0 && -id <= int(vertices().size()) )
216  att->m_vertex = vertices()[-id - 1];
217  }
218  }
219 
220  /// @brief Remove attribute
221  void remove_attribute(const string &name, const int& id = 0);
222 
223  /// @brief Get attribute of type T
224  template<class T>
225  shared_ptr<T> attribute(const string &name, const int& id = 0) const;
226 
227  /// @brief Get attribute of any type as string
228  string attribute_as_string(const string &name, const int& id = 0) const;
229 
230  /// @brief Get list of attribute names
231  std::vector<string> attribute_names( const int& id = 0) const;
232 
233  /// @brief Get a copy of the list of attributes
234  /// @note To avoid thread issues, this is returns a copy. Better solution may be needed.
235  std::map< string, std::map<int, shared_ptr<Attribute> > > attributes() const {
236  std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
237  return m_attributes;
238  }
239 
240  //@}
241 
242 
243  /// @name Particle and vertex modification
244  //@{
245 
246  /// @brief Add particle
247  void add_particle( GenParticlePtr p );
248 
249  /// @brief Add vertex
250  void add_vertex( GenVertexPtr v );
251 
252  /// @brief Remove particle from the event
253  ///
254  /// This function will remove whole sub-tree starting from this particle
255  /// if it is the only incoming particle of this vertex.
256  /// It will also production vertex of this particle if this vertex
257  /// has no more outgoing particles
258  void remove_particle( GenParticlePtr v );
259 
260  /// @brief Remove a set of particles
261  ///
262  /// This function follows rules of GenEvent::remove_particle to remove
263  /// a list of particles from the event.
264  void remove_particles( std::vector<GenParticlePtr> v );
265 
266  /// @brief Remove vertex from the event
267  ///
268  /// This will remove all sub-trees of all outgoing particles of this vertex
269  void remove_vertex( GenVertexPtr v );
270 
271  /// @brief Add whole tree in topological order
272  ///
273  /// This function will find the beam particles (particles
274  /// that have no production vertices or their production vertices
275  /// have no particles) and will add the whole decay tree starting from
276  /// these particles.
277  ///
278  /// @note Any particles on this list that do not belong to the tree
279  /// will be ignored.
280  void add_tree( const std::vector<GenParticlePtr> &particles );
281 
282  /// @brief Reserve memory for particles and vertices
283  ///
284  /// Helps optimize event creation when size of the event is known beforehand
285  void reserve(const size_t& particles, const size_t& vertices = 0);
286 
287  /// @brief Remove contents of this event
288  void clear();
289 
290  //@}
291 
292  /// @name Deprecated functionality
293  //@{
294 
295  /// @brief Add particle by raw pointer
296  /// @deprecated Use GenEvent::add_particle( const GenParticlePtr& ) instead
297  void add_particle( GenParticle *p );
298 
299  /// @brief Add vertex by raw pointer
300  /// @deprecated Use GenEvent::add_vertex( const GenVertexPtr& ) instead
301  void add_vertex ( GenVertex *v );
302 
303 
304  /// @brief Set incoming beam particles
305  /// @deprecated Backward compatibility
306  void set_beam_particles(GenParticlePtr p1, GenParticlePtr p2);
307 
308 
309  /// @brief Add particle to root vertex
310 
311  void add_beam_particle(GenParticlePtr p1);
312 
313 
314  //@}
315 
316 #endif // __CINT__
317 
318 
319  /// @name Methods to fill GenEventData and to read it back
320  //@{
321 
322  /// @brief Fill GenEventData object
323  void write_data(GenEventData &data) const;
324 
325  /// @brief Fill GenEvent based on GenEventData
326  void read_data(const GenEventData &data);
327 
328 #ifdef HEPMC3_ROOTIO
329  /// @brief ROOT I/O streamer
330  void Streamer(TBuffer &b);
331  //@}
332 #endif
333 
334 private:
335 
336  /// @name Fields
337  //@{
338 
339 #if !defined(__CINT__)
340 
341  /// List of particles
342  std::vector<GenParticlePtr> m_particles;
343  /// List of vertices
344  std::vector<GenVertexPtr> m_vertices;
345 
346  /// Event number
348 
349  /// Event weights
350  std::vector<double> m_weights;
351 
352  /// Momentum unit
354  /// Length unit
356 
357  /// The root vertex is stored outside the normal vertices list to block user access to it
358  GenVertexPtr m_rootvertex;
359 
360  /// Global run information.
361  shared_ptr<GenRunInfo> m_run_info;
362 
363  /// @brief Map of event, particle and vertex attributes
364  ///
365  /// Keys are name and ID (0 = event, <0 = vertex, >0 = particle)
366  mutable std::map< string, std::map<int, shared_ptr<Attribute> > > m_attributes;
367 
368  /// @brief Attribute map key type
369  typedef std::map< string, std::map<int, shared_ptr<Attribute> > >::value_type att_key_t;
370 
371  /// @brief Attribute map value type
372  typedef std::map<int, shared_ptr<Attribute> >::value_type att_val_t;
373 
374  /// @brief Mutex lock for the m_attibutes map.
375  mutable std::recursive_mutex m_lock_attributes;
376 #endif // __CINT__
377 
378  //@}
379 
380 };
381 
382 #if !defined(__CINT__)
383 //
384 // Template methods
385 //
386 template<class T>
387 shared_ptr<T> GenEvent::attribute(const std::string &name, const int& id) const {
388  std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
389  std::map< string, std::map<int, shared_ptr<Attribute> > >::iterator i1 =
390  m_attributes.find(name);
391  if( i1 == m_attributes.end() ) {
392  if ( id == 0 && run_info() ) {
393  return run_info()->attribute<T>(name);
394  }
395  return shared_ptr<T>();
396  }
397 
398  std::map<int, shared_ptr<Attribute> >::iterator i2 = i1->second.find(id);
399  if (i2 == i1->second.end() ) return shared_ptr<T>();
400 
401  if (!i2->second->is_parsed() ) {
402 
403  shared_ptr<T> att = make_shared<T>();
404  att->m_event = this;
405 
406  if ( id > 0 && id <= int(particles().size()) )
407  att->m_particle = m_particles[id - 1];
408  if ( id < 0 && -id <= int(vertices().size()) )
409  att->m_vertex = m_vertices[-id - 1];
410  if ( att->from_string(i2->second->unparsed_string()) &&
411  att->init() ) {
412  // update map with new pointer
413  i2->second = att;
414  return att;
415  } else {
416  return shared_ptr<T>();
417  }
418  }
419  else return dynamic_pointer_cast<T>(i2->second);
420 }
421 #endif // __CINT__
422 
423 } // namespace HepMC3
424 #endif
Units::MomentumUnit m_momentum_unit
Momentum unit.
Definition: GenEvent.h:353
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:128
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
Definition: GenEvent.cc:44
Definition of class GenRunInfo.
void remove_particle(GenParticlePtr v)
Remove particle from the event.
Definition: GenEvent.cc:118
GenHeavyIonPtr heavy_ion()
Get heavy ion generator additional information.
Definition: GenEvent.h:148
void set_cross_section(GenCrossSectionPtr cs)
Set cross-section information.
Definition: GenEvent.h:166
void add_beam_particle(GenParticlePtr p1)
Add particle to root vertex.
Definition: GenEvent.cc:780
void remove_particles(std::vector< GenParticlePtr > v)
Remove a set of particles.
Definition: GenEvent.cc:186
void add_vertex(GenVertexPtr v)
Add vertex.
Definition: GenEvent.cc:98
bool boost(const FourVector &v)
Boost event using x,y,z components of v as velocities.
Definition: GenEvent.cc:559
void remove_vertex(GenVertexPtr v)
Remove vertex from the event.
Definition: GenEvent.cc:195
void shift_position_by(const FourVector &delta)
Shift position of all vertices in the event by delta.
Definition: GenEvent.cc:429
const std::vector< std::string > & weight_names() const
Definition: GenEvent.h:110
GenEvent(Units::MomentumUnit momentum_unit=Units::GEV, Units::LengthUnit length_unit=Units::MM)
Event constructor without a run.
Definition: GenEvent.cc:22
Stores vertex-related information.
Definition: GenVertex.h:26
GenPdfInfoPtr pdf_info()
Get PDF information.
Definition: GenEvent.h:155
std::vector< GenVertexPtr > m_vertices
List of vertices.
Definition: GenEvent.h:344
ConstGenHeavyIonPtr heavy_ion() const
Get heavy ion generator additional information (const version)
Definition: GenEvent.h:150
Definition of attribute class GenHeavyIon.
GenCrossSectionPtr cross_section()
Get cross-section information.
Definition: GenEvent.h:162
Stores particle-related information.
Definition: GenParticle.h:31
const Units::LengthUnit & length_unit() const
Get length unit.
Definition: GenEvent.h:142
ConstGenPdfInfoPtr pdf_info() const
Get PDF information (const version)
Definition: GenEvent.h:157
bool reflect(const int axis)
Change sign of axis.
Definition: GenEvent.cc:527
shared_ptr< T > attribute(const string &name, const int &id=0) const
Get attribute of type T.
Definition: GenEvent.h:387
void add_particle(GenParticlePtr p)
Add particle.
Definition: GenEvent.cc:50
int event_number() const
Get event number.
Definition: GenEvent.h:135
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition: GenEvent.h:124
const FourVector & event_pos() const
Vertex representing the overall event position.
Definition: GenEvent.cc:417
const std::vector< GenParticlePtr > & particles()
Get/set list of particles (non-const)
Definition: GenEvent.h:75
LengthUnit
Position units.
Definition: Units.h:32
MomentumUnit
Momentum units.
Definition: Units.h:29
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Definition: GenEvent.h:140
Stores event-related information.
Definition: GenEvent.h:41
Stores serializable event information.
Definition: GenEventData.h:26
Generic 4-vector.
Definition: FourVector.h:35
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition: GenEvent.cc:646
void set_pdf_info(GenPdfInfoPtr pi)
Set PDF information.
Definition: GenEvent.h:159
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
Definition: GenEvent.h:375
void set_beam_particles(GenParticlePtr p1, GenParticlePtr p2)
Set incoming beam particles.
Definition: GenEvent.cc:775
std::vector< double > & weights()
Get event weights as a vector (non-const)
Definition: GenEvent.h:88
shared_ptr< GenRunInfo > m_run_info
Global run information.
Definition: GenEvent.h:361
void remove_attribute(const string &name, const int &id=0)
Remove attribute.
Definition: GenEvent.cc:622
bool rotate(const FourVector &v)
Rotate event using x,y,z components of v as rotation angles.
Definition: GenEvent.cc:439
ConstGenCrossSectionPtr cross_section() const
Get cross-section information (const version)
Definition: GenEvent.h:164
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition: GenEvent.cc:704
std::map< int, shared_ptr< Attribute > >::value_type att_val_t
Attribute map value type.
Definition: GenEvent.h:372
Definition of class Units.
double & weight(const std::string &name)
Definition: GenEvent.h:102
std::vector< ConstGenParticlePtr > beams() const
Vector of beam particles.
Definition: GenEvent.cc:421
string attribute_as_string(const string &name, const int &id=0) const
Get attribute of any type as string.
Definition: GenEvent.cc:799
void add_tree(const std::vector< GenParticlePtr > &particles)
Add whole tree in topological order.
Definition: GenEvent.cc:268
Units::LengthUnit m_length_unit
Length unit.
Definition: GenEvent.h:355
std::map< string, std::map< int, shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
Definition: GenEvent.h:235
std::vector< GenParticlePtr > m_particles
List of particles.
Definition: GenEvent.h:342
const std::vector< GenVertexPtr > & vertices()
Get/set list of vertices (non-const)
Definition: GenEvent.h:77
void reserve(const size_t &particles, const size_t &vertices=0)
Reserve memory for particles and vertices.
Definition: GenEvent.cc:390
std::map< string, std::map< int, shared_ptr< Attribute > > > m_attributes
Map of event, particle and vertex attributes.
Definition: GenEvent.h:366
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:396
void add_attribute(const string &name, const shared_ptr< Attribute > &att, const int &id=0)
Add event attribute to event.
Definition: GenEvent.h:208
int m_event_number
Event number.
Definition: GenEvent.h:347
Definition of event attribute class GenPdfInfo.
double weight(const std::string &name) const
Definition: GenEvent.h:95
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:86
GenVertexPtr m_rootvertex
The root vertex is stored outside the normal vertices list to block user access to it...
Definition: GenEvent.h:358
Definition of attribute class GenCrossSection.
std::vector< string > attribute_names(const int &id=0) const
Get list of attribute names.
Definition: GenEvent.cc:634
GenEvent & operator=(const GenEvent &)
Assignment operator.
Definition: GenEvent.cc:84
std::map< string, std::map< int, shared_ptr< Attribute > > >::value_type att_key_t
Attribute map key type.
Definition: GenEvent.h:369
~GenEvent()
Destructor.
Definition: GenEvent.cc:76
double weight(const unsigned long &index=0) const
Definition: GenEvent.h:91
void clear()
Remove contents of this event.
Definition: GenEvent.cc:609
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
Definition: GenEvent.cc:40
std::vector< double > m_weights
Event weights.
Definition: GenEvent.h:350
void set_heavy_ion(GenHeavyIonPtr hi)
Set heavy ion generator additional information.
Definition: GenEvent.h:152
void set_event_number(const int &num)
Set event number.
Definition: GenEvent.h:137
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.
Definition: GenEvent.h:187