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