24 : m_event_number(0), m_weights(std::vector<double>()),
25 m_momentum_unit(mu), m_length_unit(lu),
32 : m_event_number(0), m_weights(std::vector<double>()),
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);
99 if( v->in_event() )
return;
106 for(
auto p: v->particles_in() ) {
108 p->m_end_vertex = v->shared_from_this();
111 for(
auto p: v->particles_out() ) {
113 p->m_production_vertex = v;
119 if( !p || p->parent_event() != this )
return;
121 DEBUG( 30,
"GenEvent::remove_particle - called with particle: "<<p->id() );
122 GenVertexPtr end_vtx = p->end_vertex();
124 end_vtx->remove_particle_in(p);
127 if( end_vtx->particles_in().size() == 0 )
remove_vertex(end_vtx);
130 GenVertexPtr prod_vtx = p->production_vertex();
132 prod_vtx->remove_particle_out(p);
135 if( prod_vtx->particles_out().size() == 0 )
remove_vertex(prod_vtx);
138 DEBUG( 30,
"GenEvent::remove_particle - erasing particle: " << p->id() )
145 vector<string> atts = p->attribute_names();
146 for(
const string &s: atts) {
147 p->remove_attribute(s);
153 vector< pair< int, shared_ptr<Attribute> > > changed_attributes;
156 changed_attributes.clear();
158 for ( std::map<
int, shared_ptr<Attribute> >::iterator vt2=vt1.second.begin();vt2!=vt1.second.end();++vt2){
159 if( (*vt2).first > p->id() ) {
160 changed_attributes.push_back(*vt2);
164 for(
att_val_t val: changed_attributes ) {
165 vt1.second.erase(val.first);
166 vt1.second[val.first-1] = val.second;
175 p->m_event =
nullptr;
180 inline bool operator()(
const GenParticlePtr& p1,
const GenParticlePtr& p2) {
181 return (p1->id() > p2->id());
189 for (std::vector<GenParticlePtr>::iterator p=v.begin();p!=v.end();++p) {
195 if( !v || v->parent_event() != this )
return;
197 DEBUG( 30,
"GenEvent::remove_vertex - called with vertex: "<<v->id() );
198 shared_ptr<GenVertex> null_vtx;
200 for(
auto p: v->particles_in() ) {
201 p->m_end_vertex = std::weak_ptr<GenVertex>();
204 for(
auto p: v->particles_out() ) {
205 p->m_production_vertex = std::weak_ptr<GenVertex>();
212 DEBUG( 30,
"GenEvent::remove_vertex - erasing vertex: " << v->id() )
218 vector<string> atts = v->attribute_names();
219 for(
string s: atts) {
220 v->remove_attribute(s);
227 vector< pair< int, shared_ptr<Attribute> > > changed_attributes;
230 changed_attributes.clear();
232 for ( std::map<
int, shared_ptr<Attribute> >::iterator vt2=vt1.second.begin();vt2!=vt1.second.end();++vt2){
233 if( (*vt2).first < v->id() ) {
234 changed_attributes.push_back(*vt2);
238 for(
att_val_t val: changed_attributes ) {
239 vt1.second.erase(val.first);
240 vt1.second[val.first+1] = val.second;
250 v->m_event =
nullptr;
255 static bool visit_children(std::map<ConstGenVertexPtr,int> &a, ConstGenVertexPtr v)
257 for ( ConstGenParticlePtr p: v->particles_out())
260 if (a[p->end_vertex()]!=0)
return true;
261 else a[p->end_vertex()]++;
269 shared_ptr<IntAttribute> existing_hc=attribute<IntAttribute>(
"cycles");
270 bool has_cycles=
false;
271 std::map<GenVertexPtr,int> sortingv;
272 std::vector<GenVertexPtr> noinv;
273 if (existing_hc)
if (existing_hc->value()!=0) has_cycles=
true;
276 for(GenParticlePtr p: parts ) {
277 GenVertexPtr v = p->production_vertex();
279 if( !v || v->particles_in().size()==0 ) {
280 GenVertexPtr v2 = p->end_vertex();
281 if(v2) {noinv.push_back(v2); sortingv[v2]=0;}
284 for (GenVertexPtr v: noinv){
285 std::map<ConstGenVertexPtr,int> sorting_temp(sortingv.begin(), sortingv.end());
297 deque<GenVertexPtr> sorting;
300 for(
auto p: parts ) {
301 const GenVertexPtr &v = p->production_vertex();
302 if( !v || v->particles_in().size()==0 ) {
303 const GenVertexPtr &v2 = p->end_vertex();
304 if(v2) sorting.push_back(v2);
309 unsigned int sorting_loop_count = 0;
310 unsigned int max_deque_size = 0;
314 while( !sorting.empty() ) {
316 if( sorting.size() > max_deque_size ) max_deque_size = sorting.size();
317 ++sorting_loop_count;
320 GenVertexPtr &v = sorting.front();
325 for(
auto p: v->particles_in() ) {
326 GenVertexPtr v2 = p->production_vertex();
327 if( v2 && !v2->in_event() && find(sorting.begin(),sorting.end(),v2)==sorting.end()){
328 sorting.push_front(v2);
335 if( added )
continue;
338 if( !v->in_event() ) {
343 for(
auto p: v->particles_out() ) {
344 GenVertexPtr v2 = p->end_vertex();
345 if( v2 && !v2->in_event()&& find(sorting.begin(),sorting.end(),v2)==sorting.end() ) {
346 sorting.push_back(v2);
362 vector< pair< int, shared_ptr<Attribute> > > changed_attributes;
363 for (
auto vt2 : vt1.second )
364 if( vt2.first <= rootid )
365 changed_attributes.push_back(vt2);
366 for(
auto val : changed_attributes ) {
367 vt1.second.erase(val.first);
368 vt1.second[val.first == rootid? 0: val.first + 1] = val.second;
376 WARNING(
"ReaderAsciiHepMC2: Suspicious looking rootvertex found. Will try to cope." )
381 DEBUG( 6,
"GenEvent - particles sorted: " 382 <<this->
particles().size()<<
", max deque size: " 383 <<max_deque_size<<
", iterations: "<<sorting_loop_count )
420 return std::const_pointer_cast<const GenVertex>(
m_rootvertex)->particles_out();
432 if ( v->has_set_position() )
433 v->set_position( v->position() + delta );
453 map< string, map<int, shared_ptr<Attribute> > >::iterator i1 =
457 map<int, shared_ptr<Attribute> >::iterator i2 = i1->second.find(
id);
458 if( i2 == i1->second.end() )
return;
460 i1->second.erase(i2);
464 vector<string> results;
467 for(
const att_val_t& vt2: vt1.second ) {
468 if( vt2.first ==
id ) results.push_back( vt1.first );
494 for( ConstGenParticlePtr p: this->
particles() ) {
498 for(ConstGenVertexPtr v: this->
vertices() ) {
499 data.
vertices.push_back( v->data() );
502 for(ConstGenParticlePtr p: v->particles_in() ) {
503 data.
links1.push_back( p->id() );
504 data.
links2.push_back( v_id );
507 for(ConstGenParticlePtr p: v->particles_out() ) {
508 data.
links1.push_back( v_id );
509 data.
links2.push_back( p->id() );
514 for(
const att_val_t& vt2: vt1.second ) {
518 bool status = vt2.second->to_string(st);
521 WARNING(
"GenEvent::write_data: problem serializing attribute: "<<vt1.first )
546 GenParticlePtr p = make_shared<GenParticle>(pd);
556 GenVertexPtr v = make_shared<GenVertex>(vd);
565 for(
unsigned int i=0; i<data.
links1.size(); ++i) {
573 if ( (id1<0&&id2<0)|| (id1>0&&id2>0) ) {
WARNING(
"GenEvent::read_data: wrong link: "<<id1<<
" "<<id2 );
continue;}
581 for(
unsigned int i=0; i<data.
attribute_id.size(); ++i) {
610 if( p1->in_event())
if (p1->parent_event()!=
this)
612 WARNING(
"Attempting to add particle from anothe event. Ignored.")
615 if (p1->production_vertex()) p1->production_vertex()->remove_particle_out(p1);
625 std::map< string, std::map<int, shared_ptr<Attribute> > >::iterator i1 =
629 return run_info()->attribute_as_string(name);
634 std::map<int, shared_ptr<Attribute> >::iterator i2 = i1->second.find(
id);
635 if (i2 == i1->second.end() )
return string();
637 if( !i2->second )
return string();
640 i2->second->to_string(ret);
Units::MomentumUnit m_momentum_unit
Momentum unit.
int event_number() const
Get event number.
void remove_particle(GenParticlePtr v)
Remove particle from the event.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
void add_beam_particle(GenParticlePtr p1)
Add particle to root vertex.
const Units::LengthUnit & length_unit() const
Get length unit.
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.
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition of class GenParticle.
void remove_vertex(GenVertexPtr v)
Remove vertex from the event.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
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.
std::vector< string > attribute_names(const int &id=0) const
Get list of attribute names.
Definition of class GenVertex.
Stores particle-related information.
int event_number
Event number.
void add_particle(GenParticlePtr p)
Add particle.
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
static void convert(FourVector &m, MomentumUnit from, MomentumUnit to)
Convert FourVector to different momentum unit.
Definition of struct GenEventData.
Units::MomentumUnit momentum_unit
Momentum unit.
bool is_zero() const
Check if the length of this vertex is zero.
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.
const std::vector< double > & weights() const
Get event weight values as a vector.
const FourVector & event_pos() const
Vertex representing the overall event position.
LengthUnit
Position units.
#define DEBUG_CODE_BLOCK(x)
Macro for storing code useful for debugging.
MomentumUnit
Momentum units.
Stores event-related information.
std::map< string, std::map< int, shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
Stores serializable event information.
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.
string attribute_as_string(const string &name, const int &id=0) const
Get attribute of any type as string.
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.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
std::vector< GenParticleData > particles
Particles.
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::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.
GenVertexPtr m_rootvertex
The root vertex is stored outside the normal vertices list to block user access to it.
std::vector< ConstGenParticlePtr > beams() const
Vector of beam particles.
Definition of class GenEvent.
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.
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.