24 : m_event_number(0), m_weights(std::vector<double>(1, 1.0)),
25 m_momentum_unit(mu), m_length_unit(lu),
32 : m_event_number(0), m_weights(std::vector<double>(1, 1.0)),
33 m_momentum_unit(mu), m_length_unit(lu),
36 if ( run && !run->weight_names().empty() )
37 m_weights = std::vector<double>(run->weight_names().size(), 1.0);
41 return *(
reinterpret_cast<const std::vector<ConstGenParticlePtr>*
>(&
m_particles));
45 return *(
reinterpret_cast<const std::vector<ConstGenVertexPtr>*
>(&
m_vertices));
51 if( p->in_event() )
return;
59 if( !p->production_vertex() )
69 std::lock_guard<std::recursive_mutex> rhs_lk(e.
m_lock_attributes, std::adopt_lock);
77 for ( std::map<
string, std::map<
int, shared_ptr<Attribute> > >::iterator attm=
m_attributes.begin();attm!=
m_attributes.end();++attm)
78 for ( std::map<
int, shared_ptr<Attribute> >::iterator att=attm->second.begin();att!=attm->second.end();++att) att->second->m_event =
nullptr;
80 for ( std::vector<GenVertexPtr>::iterator v=
m_vertices.begin();v!=
m_vertices.end();++v )
if ((*v)->m_event==
this) (*v)->m_event=
nullptr;
81 for ( std::vector<GenParticlePtr>::iterator p=
m_particles.begin();p!=
m_particles.end();++p )
if ((*p)->m_event==
this) (*p)->m_event=
nullptr;
89 std::lock_guard<std::recursive_mutex> rhs_lk(e.
m_lock_attributes, std::adopt_lock);
100 if( v->in_event() )
return;
107 for(
auto p: v->particles_in() ) {
109 p->m_end_vertex = v->shared_from_this();
112 for(
auto p: v->particles_out() ) {
114 p->m_production_vertex = v;
120 if( !p || p->parent_event() != this )
return;
122 DEBUG( 30,
"GenEvent::remove_particle - called with particle: "<<p->id() );
123 GenVertexPtr end_vtx = p->end_vertex();
125 end_vtx->remove_particle_in(p);
128 if( end_vtx->particles_in().size() == 0 )
remove_vertex(end_vtx);
131 GenVertexPtr prod_vtx = p->production_vertex();
133 prod_vtx->remove_particle_out(p);
136 if( prod_vtx->particles_out().size() == 0 )
remove_vertex(prod_vtx);
139 DEBUG( 30,
"GenEvent::remove_particle - erasing particle: " << p->id() )
146 vector<string> atts = p->attribute_names();
147 for(
const string &s: atts) {
148 p->remove_attribute(s);
154 vector< pair< int, shared_ptr<Attribute> > > changed_attributes;
157 changed_attributes.clear();
159 for ( std::map<
int, shared_ptr<Attribute> >::iterator vt2=vt1.second.begin();vt2!=vt1.second.end();++vt2){
160 if( (*vt2).first > p->id() ) {
161 changed_attributes.push_back(*vt2);
165 for(
att_val_t val: changed_attributes ) {
166 vt1.second.erase(val.first);
167 vt1.second[val.first-1] = val.second;
176 p->m_event =
nullptr;
181 inline bool operator()(
const GenParticlePtr& p1,
const GenParticlePtr& p2) {
182 return (p1->id() > p2->id());
190 for (std::vector<GenParticlePtr>::iterator p=v.begin();p!=v.end();++p) {
196 if( !v || v->parent_event() != this )
return;
198 DEBUG( 30,
"GenEvent::remove_vertex - called with vertex: "<<v->id() );
199 shared_ptr<GenVertex> null_vtx;
201 for(
auto p: v->particles_in() ) {
202 p->m_end_vertex = std::weak_ptr<GenVertex>();
205 for(
auto p: v->particles_out() ) {
206 p->m_production_vertex = std::weak_ptr<GenVertex>();
213 DEBUG( 30,
"GenEvent::remove_vertex - erasing vertex: " << v->id() )
219 vector<string> atts = v->attribute_names();
220 for(
string s: atts) {
221 v->remove_attribute(s);
228 vector< pair< int, shared_ptr<Attribute> > > changed_attributes;
231 changed_attributes.clear();
233 for ( std::map<
int, shared_ptr<Attribute> >::iterator vt2=vt1.second.begin();vt2!=vt1.second.end();++vt2){
234 if( (*vt2).first < v->id() ) {
235 changed_attributes.push_back(*vt2);
239 for(
att_val_t val: changed_attributes ) {
240 vt1.second.erase(val.first);
241 vt1.second[val.first+1] = val.second;
251 v->m_event =
nullptr;
256 static bool visit_children(std::map<ConstGenVertexPtr,int> &a, ConstGenVertexPtr v)
258 for ( ConstGenParticlePtr p: v->particles_out())
261 if (a[p->end_vertex()]!=0)
return true;
262 else a[p->end_vertex()]++;
270 shared_ptr<IntAttribute> existing_hc=attribute<IntAttribute>(
"cycles");
271 bool has_cycles=
false;
272 std::map<GenVertexPtr,int> sortingv;
273 std::vector<GenVertexPtr> noinv;
274 if (existing_hc)
if (existing_hc->value()!=0) has_cycles=
true;
277 for(GenParticlePtr p: parts ) {
278 GenVertexPtr v = p->production_vertex();
280 if( !v || v->particles_in().size()==0 ) {
281 GenVertexPtr v2 = p->end_vertex();
282 if(v2) {noinv.push_back(v2); sortingv[v2]=0;}
285 for (GenVertexPtr v: noinv){
286 std::map<ConstGenVertexPtr,int> sorting_temp(sortingv.begin(), sortingv.end());
292 for( std::map<GenVertexPtr,int>::iterator vi=sortingv.begin();vi!=sortingv.end();++vi)
if( !vi->first->in_event() )
add_vertex(vi->first);
296 deque<GenVertexPtr> sorting;
299 for(
auto p: parts ) {
300 const GenVertexPtr &v = p->production_vertex();
301 if( !v || v->particles_in().size()==0 ) {
302 const GenVertexPtr &v2 = p->end_vertex();
303 if(v2) sorting.push_back(v2);
308 unsigned int sorting_loop_count = 0;
309 unsigned int max_deque_size = 0;
313 while( !sorting.empty() ) {
315 if( sorting.size() > max_deque_size ) max_deque_size = sorting.size();
316 ++sorting_loop_count;
319 GenVertexPtr &v = sorting.front();
324 for(
auto p: v->particles_in() ) {
325 GenVertexPtr v2 = p->production_vertex();
326 if( v2 && !v2->in_event() ) {
327 sorting.push_front(v2);
334 if( added )
continue;
337 if( !v->in_event() ) {
342 for(
auto p: v->particles_out() ) {
343 GenVertexPtr v2 = p->end_vertex();
344 if( v2 && !v2->in_event() ) {
345 sorting.push_back(v2);
354 DEBUG( 6,
"GenEvent - particles sorted: "
355 <<this->
particles().size()<<
", max deque size: "
356 <<max_deque_size<<
", iterations: "<<sorting_loop_count )
405 if ( v->has_set_position() )
406 v->set_position( v->position() + delta );
426 map< string, map<int, shared_ptr<Attribute> > >::iterator i1 =
430 map<int, shared_ptr<Attribute> >::iterator i2 = i1->second.find(
id);
431 if( i2 == i1->second.end() )
return;
433 i1->second.erase(i2);
437 vector<string> results;
440 for(
const att_val_t& vt2: vt1.second ) {
441 if( vt2.first ==
id ) results.push_back( vt1.first );
467 for( ConstGenParticlePtr p: this->
particles() ) {
471 for(ConstGenVertexPtr v: this->
vertices() ) {
472 data.
vertices.push_back( v->data() );
475 for(ConstGenParticlePtr p: v->particles_in() ) {
476 data.
links1.push_back( p->id() );
477 data.
links2.push_back( v_id );
480 for(ConstGenParticlePtr p: v->particles_out() ) {
481 data.
links1.push_back( v_id );
482 data.
links2.push_back( p->id() );
487 for(
const att_val_t& vt2: vt1.second ) {
491 bool status = vt2.second->to_string(st);
494 WARNING(
"GenEvent::write_data: problem serializing attribute: "<<vt1.first )
517 GenParticlePtr p = make_shared<GenParticle>(pd);
527 GenVertexPtr v = make_shared<GenVertex>(vd);
536 for(
unsigned int i=0; i<data.
links1.size(); ++i) {
545 for(
unsigned int i=0; i<data.
attribute_id.size(); ++i) {
580 std::map< string, std::map<int, shared_ptr<Attribute> > >::iterator i1 =
584 return run_info()->attribute_as_string(name);
589 std::map<int, shared_ptr<Attribute> >::iterator i2 = i1->second.find(
id);
590 if (i2 == i1->second.end() )
return string();
592 if( !i2->second )
return string();
595 i2->second->to_string(ret);
Units::MomentumUnit m_momentum_unit
Momentum unit.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
void remove_particle(GenParticlePtr v)
Remove particle from the event.
void add_beam_particle(GenParticlePtr p1)
Add particle to root vertex.
void remove_particles(std::vector< GenParticlePtr > v)
Remove a set of particles.
void add_vertex(GenVertexPtr v)
Add vertex.
std::vector< int > attribute_id
Attribute owner id.
Definition of class GenParticle.
void remove_vertex(GenVertexPtr v)
Remove vertex from the event.
void shift_position_by(const FourVector &delta)
Shift position of all vertices in the event by delta.
GenEvent(Units::MomentumUnit momentum_unit=Units::GEV, Units::LengthUnit length_unit=Units::MM)
Event constructor without a run.
Stores vertex-related information.
std::vector< int > links2
Second id of the vertex links.
std::vector< GenVertexPtr > m_vertices
List of vertices.
Definition of class GenVertex.
bool is_zero() const
Check if the length of this vertex is zero.
Stores particle-related information.
const Units::LengthUnit & length_unit() const
Get length unit.
int event_number
Event number.
void add_particle(GenParticlePtr p)
Add particle.
static void convert(FourVector &m, MomentumUnit from, MomentumUnit to)
Convert FourVector to different momentum unit.
Definition of struct GenEventData.
int event_number() const
Get event number.
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Units::MomentumUnit momentum_unit
Momentum unit.
const FourVector & event_pos() const
Vertex representing the overall event position.
std::vector< int > links1
First id of the vertex links.
#define DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
FourVector event_pos
Event position.
std::vector< std::string > attribute_string
Attribute serialized as string.
LengthUnit
Position units.
#define DEBUG_CODE_BLOCK(x)
Macro for storing code useful for debugging.
MomentumUnit
Momentum units.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Stores event-related information.
Stores serializable event information.
void write_data(GenEventData &data) const
Fill GenEventData object.
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
std::vector< std::string > attribute_name
Attribute name.
Stores serializable vertex information.
void set_beam_particles(GenParticlePtr p1, GenParticlePtr p2)
Set incoming beam particles.
void remove_attribute(const string &name, const int &id=0)
Remove attribute.
Stores serializable particle information.
static bool visit_children(std::map< ConstGenVertexPtr, int > &a, ConstGenVertexPtr v)
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
std::map< int, shared_ptr< Attribute > >::value_type att_val_t
Attribute map value type.
std::vector< GenParticleData > particles
Particles.
std::vector< ConstGenParticlePtr > beams() const
Vector of beam particles.
string attribute_as_string(const string &name, const int &id=0) const
Get attribute of any type as string.
void add_tree(const std::vector< GenParticlePtr > &particles)
Add whole tree in topological order.
#define WARNING(MESSAGE)
Macro for printing warning messages.
Units::LengthUnit m_length_unit
Length unit.
std::map< string, std::map< int, shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
std::vector< GenParticlePtr > m_particles
List of particles.
void reserve(const size_t &particles, const size_t &vertices=0)
Reserve memory for particles and vertices.
std::map< string, std::map< int, shared_ptr< Attribute > > > m_attributes
Map of event, particle and vertex attributes.
void set_units(Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit)
Change event units Converts event from current units to new ones.
void add_attribute(const string &name, const shared_ptr< Attribute > &att, const int &id=0)
Add event attribute to event.
int m_event_number
Event number.
std::vector< double > weights
Weights.
std::vector< GenVertexData > vertices
Vertices.
const std::vector< double > & weights() const
Get event weight values as a vector.
GenVertexPtr m_rootvertex
The root vertex is stored outside the normal vertices list to block user access to it...
Definition of class GenEvent.
std::vector< string > attribute_names(const int &id=0) const
Get list of attribute names.
GenEvent & operator=(const GenEvent &)
Assignment operator.
std::map< string, std::map< int, shared_ptr< Attribute > > >::value_type att_key_t
Attribute map key type.
void clear()
Remove contents of this event.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
std::vector< double > m_weights
Event weights.
Units::LengthUnit length_unit
Length unit.
void set_event_number(const int &num)
Set event number.
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.