MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
GenEvent.h
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
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
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
47class GenEvent {
48
49public:
50
52 GenEvent(Units::MomentumUnit mu = Units::GEV,
53 Units::LengthUnit lu = Units::MM);
54
55#if !defined(__CINT__)
56
58 GenEvent(std::shared_ptr<GenRunInfo> run,
59 Units::MomentumUnit mu = Units::GEV,
60 Units::LengthUnit lu = Units::MM);
61
63 GenEvent(const GenEvent&);
64
66 ~GenEvent();
67
70
73
75 const std::vector<ConstGenParticlePtr>& particles() const;
77 const std::vector<ConstGenVertexPtr>& vertices() const;
78
80 const std::vector<GenParticlePtr>& particles() { return m_particles; }
82 const std::vector<GenVertexPtr>& vertices() { return m_vertices; }
83
85
86
89
91 inline int particles_size() const { return m_particles.size(); }
93 inline bool particles_empty() const { return m_particles.empty(); }
95 inline int vertices_size() const { return m_vertices.size(); }
97 inline bool vertices_empty() const { return m_vertices.empty(); }
99
100
103
105 const std::vector<double>& weights() const { return m_weights; }
107 std::vector<double>& weights() { return m_weights; }
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; }
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
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
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
138
139
142
144 std::shared_ptr<GenRunInfo> run_info() const {
145 return m_run_info;
146 }
147
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
155 int event_number() const { return m_event_number; }
157 void set_event_number(const int& num) { m_event_number = num; }
158
160 const Units::MomentumUnit& momentum_unit() const { return m_momentum_unit; }
162 const Units::LengthUnit& length_unit() const { return m_length_unit; }
165 void set_units( Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit);
166
168 GenHeavyIonPtr heavy_ion() { return attribute<GenHeavyIon>("GenHeavyIon"); }
170 ConstGenHeavyIonPtr heavy_ion() const { return attribute<GenHeavyIon>("GenHeavyIon"); }
172 void set_heavy_ion(GenHeavyIonPtr hi) { add_attribute("GenHeavyIon",hi); }
173
175 GenPdfInfoPtr pdf_info() { return attribute<GenPdfInfo>("GenPdfInfo"); }
177 ConstGenPdfInfoPtr pdf_info() const { return attribute<GenPdfInfo>("GenPdfInfo"); }
179 void set_pdf_info(GenPdfInfoPtr pi) { add_attribute("GenPdfInfo",pi); }
180
182 GenCrossSectionPtr cross_section() { return attribute<GenCrossSection>("GenCrossSection"); }
184 ConstGenCrossSectionPtr cross_section() const { return attribute<GenCrossSection>("GenCrossSection"); }
186 void set_cross_section(GenCrossSectionPtr cs) { add_attribute("GenCrossSection",cs); }
187
189
190
193
195 const FourVector& event_pos() const;
196
198 std::vector<ConstGenParticlePtr> beams() const;
199
201 std::vector<ConstGenParticlePtr> beams(const int status) const;
202
203
205 const std::vector<GenParticlePtr> & beams();
206
208 void shift_position_by( const FourVector & delta );
209
211 void shift_position_to( const FourVector & newpos ) {
212 const FourVector delta = newpos - event_pos();
213 shift_position_by(delta);
214 }
215
217 bool boost( const FourVector& delta );
219 bool rotate( const FourVector& delta );
221 bool reflect(const int axis);
222
224
225
232 void add_attribute(const std::string &name, const std::shared_ptr<Attribute> &att, const int& id = 0);
233
238 void add_attributes(const std::vector<std::string> &names, const std::vector<std::shared_ptr<Attribute> > &atts, const std::vector<int>& ids);
239
244 void add_attributes(const std::string& name, const std::vector<std::shared_ptr<Attribute> > &atts, const std::vector<int>& ids);
245
250 void add_attributes(const std::string& name, const std::vector<std::pair<int, std::shared_ptr<Attribute> > > &atts);
251
252
254 void remove_attribute(const std::string &name, const int& id = 0);
255
257 template<class T>
258 std::shared_ptr<T> attribute(const std::string &name, const int& id = 0) const;
259
261 std::string attribute_as_string(const std::string &name, const int& id = 0) const;
262
264 std::vector<std::string> attribute_names( const int& id = 0) const;
265
268 std::map< std::string, std::map<int, std::shared_ptr<Attribute> > > attributes() const {
269 std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
270 return m_attributes;
271 }
272
274
275
278
280 void add_particle( GenParticlePtr p );
281
283 void add_vertex( GenVertexPtr v );
284
291 void remove_particle( GenParticlePtr p );
292
297 void remove_particles( std::vector<GenParticlePtr> v );
298
302 void remove_vertex( GenVertexPtr v );
303
313 void add_tree( const std::vector<GenParticlePtr> &parts );
314
318 void reserve(const size_t& parts, const size_t& verts = 0);
319
321 void clear();
322
324
327
330 void set_beam_particles(GenParticlePtr p1, GenParticlePtr p2);
331
332
334
335 void add_beam_particle(GenParticlePtr p1);
336
337
339
340#endif // __CINT__
341
342
345
347 void write_data(GenEventData &data) const;
348
350 void read_data(const GenEventData &data);
351
352#ifdef HEPMC3_ROOTIO
354 void Streamer(TBuffer &b);
355#endif
356
357#ifdef HEPMC3_PROTOBUFIO
359 void read_data(HepMC3_pb::GenEventData const &data);
360#endif
362
363private:
364
367
368#if !defined(__CINT__)
369
371 std::vector<GenParticlePtr> m_particles;
373 std::vector<GenVertexPtr> m_vertices;
374
376 int m_event_number = 0;
377
379 std::vector<double> m_weights;
380
382 Units::MomentumUnit m_momentum_unit = Units::GEV;
384 Units::LengthUnit m_length_unit = Units::MM;
385
387 GenVertexPtr m_rootvertex;
388
390 std::shared_ptr<GenRunInfo> m_run_info;
391
395 mutable std::map< std::string, std::map<int, std::shared_ptr<Attribute> > > m_attributes;
396
398 typedef std::map< std::string, std::map<int, std::shared_ptr<Attribute> > >::value_type att_key_t;
399
401 typedef std::map<int, std::shared_ptr<Attribute> >::value_type att_val_t;
402
404 mutable std::recursive_mutex m_lock_attributes;
405#endif // __CINT__
406
408
409};
410
411
412
413#if !defined(__CINT__)
414//
415// Template methods
416//
417template<class T>
418std::shared_ptr<T> GenEvent::attribute(const std::string &name, const int& id) const {
419 std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
420 std::map< std::string, std::map<int, std::shared_ptr<Attribute> > >::iterator i1 = m_attributes.find(name);
421 if ( i1 == m_attributes.end() ) {
422 if ( id == 0 && run_info() ) {
423 return run_info()->attribute<T>(name);
424 }
425 return std::shared_ptr<T>();
426 }
427
428 std::map<int, std::shared_ptr<Attribute> >::iterator i2 = i1->second.find(id);
429 if (i2 == i1->second.end() ) return std::shared_ptr<T>();
430
431 if (!i2->second->is_parsed() ) {
432
433 std::shared_ptr<T> att = std::make_shared<T>();
434 att->m_event = this;
435
436 if ( id > 0 && id <= int(particles().size()) ) {
437 att->m_particle = m_particles[id - 1];
438 }
439 if ( id < 0 && -id <= int(vertices().size()) ) {
440 att->m_vertex = m_vertices[-id - 1];
441 }
442 if ( att->from_string(i2->second->unparsed_string()) &&
443 att->init() ) {
444 // update map with new pointer
445 i2->second = att;
446 return att;
447 } else {
448 return std::shared_ptr<T>();
449 }
450 }
451 else return std::dynamic_pointer_cast<T>(i2->second);
452}
453#endif // __CINT__
454
455
456} // namespace HepMC3
457#endif
Generic 4-vector.
Definition FourVector.h:36
std::vector< std::string > attribute_names(const int &id=0) const
Get list of attribute names.
double weight(const unsigned long &index=0) const
Definition GenEvent.h:110
ConstGenCrossSectionPtr cross_section() const
Get cross-section information (const version)
Definition GenEvent.h:184
int vertices_size() const
Vertices size, HepMC2 compatibility.
Definition GenEvent.h:95
std::vector< double > & weights()
Get event weights as a vector (non-const)
Definition GenEvent.h:107
void set_beam_particles(GenParticlePtr p1, GenParticlePtr p2)
Set incoming beam particles.
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.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
Definition GenEvent.h:268
bool vertices_empty() const
Vertices empty, HepMC2 compatibility.
Definition GenEvent.h:97
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition GenEvent.h:105
GenHeavyIonPtr heavy_ion()
Get heavy ion generator additional information.
Definition GenEvent.h:168
GenCrossSectionPtr cross_section()
Get cross-section information.
Definition GenEvent.h:182
void remove_particles(std::vector< GenParticlePtr > v)
Remove a set of particles.
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.
Definition GenEvent.h:211
void add_particle(GenParticlePtr p)
Add particle.
const std::vector< std::string > & weight_names() const
Definition GenEvent.h:130
void set_event_number(const int &num)
Set event number.
Definition GenEvent.h:157
GenPdfInfoPtr pdf_info()
Get PDF information.
Definition GenEvent.h:175
bool boost(const FourVector &delta)
Boost event using x,y,z components of delta as velocities.
int event_number() const
Get event number.
Definition GenEvent.h:155
void add_tree(const std::vector< GenParticlePtr > &parts)
Add whole tree in topological order.
void set_units(Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit)
Change event units Converts event from current units to new ones.
GenEvent & operator=(const GenEvent &)
Copy Assignment operator.
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition GenEvent.h:418
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Definition GenEvent.h:160
void set_cross_section(GenCrossSectionPtr cs)
Set cross-section information.
Definition GenEvent.h:186
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition GenEvent.h:148
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
void clear()
Remove contents of this event.
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
void set_pdf_info(GenPdfInfoPtr pi)
Set PDF information.
Definition GenEvent.h:179
void remove_vertex(GenVertexPtr v)
Remove vertex from the event.
GenEvent(Units::MomentumUnit mu=Units::GEV, Units::LengthUnit lu=Units::MM)
Event constructor without a run.
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition GenEvent.h:144
bool rotate(const FourVector &delta)
Rotate event using x,y,z components of delta as rotation angles.
double weight(const std::string &name) const
Definition GenEvent.h:114
bool particles_empty() const
Particles empty, HepMC2 compatibility.
Definition GenEvent.h:93
void remove_particle(GenParticlePtr p)
Remove particle from the event.
void set_heavy_ion(GenHeavyIonPtr hi)
Set heavy ion generator additional information.
Definition GenEvent.h:172
~GenEvent()
Destructor.
std::vector< ConstGenParticlePtr > beams() const
Vector of beam particles.
double & weight(const std::string &name)
Definition GenEvent.h:121
void write_data(GenEventData &data) const
Fill GenEventData object.
const std::vector< GenParticlePtr > & particles()
Get/set list of particles (non-const)
Definition GenEvent.h:80
void reserve(const size_t &parts, const size_t &verts=0)
Reserve memory for particles and vertices.
void add_vertex(GenVertexPtr v)
Add vertex.
void add_beam_particle(GenParticlePtr p1)
Add particle to root vertex.
void remove_attribute(const std::string &name, const int &id=0)
Remove attribute.
ConstGenHeavyIonPtr heavy_ion() const
Get heavy ion generator additional information (const version)
Definition GenEvent.h:170
const FourVector & event_pos() const
Vertex representing the overall event position.
std::string attribute_as_string(const std::string &name, const int &id=0) const
Get attribute of any type as string.
void shift_position_by(const FourVector &delta)
Shift position of all vertices in the event by delta.
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
const Units::LengthUnit & length_unit() const
Get length unit.
Definition GenEvent.h:162
const std::vector< GenVertexPtr > & vertices()
Get/set list of vertices (non-const)
Definition GenEvent.h:82
int particles_size() const
Particles size, HepMC2 compatibility.
Definition GenEvent.h:91
bool reflect(const int axis)
Change sign of axis.
ConstGenPdfInfoPtr pdf_info() const
Get PDF information (const version)
Definition GenEvent.h:177
LengthUnit
Position units.
Definition Units.h:32
MomentumUnit
Momentum units.
Definition Units.h:29
Stores serializable event information.