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