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