11 #include "HepMC/GenEvent.h" 12 #include "HepMC/GenParticle.h" 13 #include "HepMC/GenVertex.h" 15 #include "HepMC/Data/GenEventData.h" 16 #include "HepMC/Search/FindParticles.h" 27 : m_event_number(0), m_momentum_unit(mu),
35 : m_event_number(0), m_momentum_unit(mu),
43 if( p->in_event() )
return;
51 if( !p->production_vertex() )
58 if( v->in_event() )
return;
68 p->m_end_vertex = v->m_this.lock();
73 p->m_production_vertex = v->m_this.lock();
79 if( !p || p->parent_event() != this )
return;
81 DEBUG( 30,
"GenEvent::remove_particle - called with particle: "<<p->id() );
84 end_vtx->remove_particle_in(p);
87 if( end_vtx->particles_in().size() == 0 )
remove_vertex(end_vtx);
92 prod_vtx->remove_particle_out(p);
95 if( prod_vtx->particles_out().size() == 0 )
remove_vertex(prod_vtx);
98 DEBUG( 30,
"GenEvent::remove_particle - erasing particle: " << p->id() )
104 vector<string> atts = p->attribute_names();
105 FOREACH(
string s, atts) {
106 p->remove_attribute(s);
112 #ifndef HEPMC_HAS_CXX0X_GCC_ONLY 113 vector<att_val_t> changed_attributes;
116 changed_attributes.clear();
118 FOREACH(
const att_val_t& vt2, vt1.second ) {
119 if( vt2.first > p->id() ) {
120 changed_attributes.push_back(vt2);
124 FOREACH(
att_val_t val, changed_attributes ) {
125 vt1.second.erase(val.first);
126 vt1.second[val.first-1] = val.second;
130 vector< pair< int, shared_ptr<Attribute> > > changed_attributes;
133 changed_attributes.clear();
135 for ( std::map<
int, shared_ptr<Attribute> >::iterator vt2=vt1.second.begin();vt2!=vt1.second.end();vt2++){
136 if( (*vt2).first > p->id() ) {
137 changed_attributes.push_back(*vt2);
141 FOREACH(
att_val_t val, changed_attributes ) {
142 vt1.second.erase(val.first);
143 vt1.second[val.first-1] = val.second;
159 return (p1->id() > p2->id());
175 if( !v || v->parent_event() != this )
return;
177 DEBUG( 30,
"GenEvent::remove_vertex - called with vertex: "<<v->id() );
178 shared_ptr<GenVertex> null_vtx;
181 p->m_end_vertex.reset();
185 p->m_production_vertex.reset();
192 DEBUG( 30,
"GenEvent::remove_vertex - erasing vertex: " << v->id() )
198 vector<string> atts = v->attribute_names();
199 FOREACH(
string s, atts) {
200 v->remove_attribute(s);
206 #ifndef HEPMC_HAS_CXX0X_GCC_ONLY 207 vector<att_val_t> changed_attributes;
210 changed_attributes.clear();
212 FOREACH(
const att_val_t& vt2, vt1.second ) {
213 if( vt2.first < v->id() ) {
214 changed_attributes.push_back(vt2);
218 FOREACH(
att_val_t val, changed_attributes ) {
219 vt1.second.erase(val.first);
220 vt1.second[val.first+1] = val.second;
234 vector< pair< int, shared_ptr<Attribute> > > changed_attributes;
236 FOREACH( att_key_t& vt1, m_attributes ) {
237 changed_attributes.clear();
239 for ( std::map<
int, shared_ptr<Attribute> >::iterator vt2=vt1.second.begin();vt2!=vt1.second.end();vt2++){
240 if( (*vt2).first < v->id() ) {
241 changed_attributes.push_back(*vt2);
245 FOREACH( att_val_t val, changed_attributes ) {
246 vt1.second.erase(val.first);
247 vt1.second[val.first+1] = val.second;
252 for(;it != m_vertices.end(); ++it) {
262 void GenEvent::add_tree(
const vector<GenParticlePtr> &parts ) {
264 deque<GenVertexPtr> sorting;
269 if( !v || v->particles_in().size()==0 ) {
271 if(v2) sorting.push_back(v2);
276 unsigned int sorting_loop_count = 0;
277 unsigned int max_deque_size = 0;
281 while( !sorting.empty() ) {
283 if( sorting.size() > max_deque_size ) max_deque_size = sorting.size();
284 ++sorting_loop_count;
294 if( v2 && !v2->in_event() ) {
295 sorting.push_front(v2);
302 if( added )
continue;
305 if( !v->in_event() ) {
312 if( v2 && !v2->in_event() ) {
313 sorting.push_back(v2);
322 DEBUG( 6,
"GenEvent - particles sorted: " 323 <<this->particles().size()<<
", max deque size: " 324 <<max_deque_size<<
", iterations: "<<sorting_loop_count )
329 void GenEvent::reserve(
unsigned int parts,
unsigned int verts) {
330 m_particles.reserve(parts);
331 m_vertices.reserve(verts);
336 if( new_momentum_unit != m_momentum_unit ) {
338 Units::convert( p->
m_data.momentum, m_momentum_unit, new_momentum_unit );
341 m_momentum_unit = new_momentum_unit;
344 if( new_length_unit != m_length_unit ) {
347 if( !fv.
is_zero() ) Units::convert( fv, m_length_unit, new_length_unit );
350 m_length_unit = new_length_unit;
356 return m_rootvertex->data().position;
359 const vector<GenParticlePtr>& GenEvent::beams()
const {
360 return m_rootvertex->particles_out();
363 void GenEvent::shift_position_by(
const FourVector & delta ) {
364 m_rootvertex->set_position( event_pos() + delta );
368 if ( v->has_set_position() )
369 v->set_position( v->position() + delta );
374 void GenEvent::clear() {
376 m_rootvertex = make_shared<GenVertex>();
378 m_attributes.clear();
398 void GenEvent::remove_attribute(
const string &name,
int id) {
399 map< string, map<int, shared_ptr<Attribute> > >::iterator i1 = m_attributes.find(name);
400 if( i1 == m_attributes.end() )
return;
402 map<int, shared_ptr<Attribute> >::iterator i2 = i1->second.find(
id);
403 if( i2 == i1->second.end() )
return;
405 i1->second.erase(i2);
408 vector<string> GenEvent::attribute_names(
int id)
const {
409 vector<string> results;
411 FOREACH(
const att_key_t& vt1, this->attributes() ) {
412 FOREACH(
const att_val_t& vt2, vt1.second ) {
413 if( vt2.first ==
id ) results.push_back( vt1.first );
422 data.
particles.reserve( this->particles().size() );
423 data.
vertices.reserve( this->vertices().size() );
424 data.
links1.reserve( this->particles().size()*2 );
425 data.
links2.reserve( this->particles().size()*2 );
437 data.
weights = this->weights();
444 data.
vertices.push_back( v->data() );
448 data.
links1.push_back( p->id() );
449 data.
links2.push_back( v_id );
453 data.
links1.push_back( v_id );
454 data.
links2.push_back( p->id() );
458 FOREACH(
const att_key_t& vt1, this->attributes() ) {
459 FOREACH(
const att_val_t& vt2, vt1.second ) {
463 bool status = vt2.second->to_string(st);
466 WARNING(
"GenEvent::write_data: problem serializing attribute: "<<vt1.first )
482 this->shift_position_to( data.
event_pos );
485 this->weights() = data.
weights;
491 m_particles.push_back(p);
494 p->m_id = particles().size();
501 m_vertices.push_back(v);
504 v->m_id = -(int)vertices().size();
508 for(
unsigned int i=0; i<data.
links1.size(); ++i) {
512 if( id1 > 0 ) m_vertices[ (-id2)-1 ]->add_particle_in ( m_particles[ id1-1 ] );
513 else m_vertices[ (-id1)-1 ]->add_particle_out( m_particles[ id2-1 ] );
517 for(
unsigned int i=0; i<data.
attribute_id.size(); ++i) {
525 #ifndef HEPMC_NO_DEPRECATED 527 bool GenEvent::valid_beam_particles()
const {
529 return (m_rootvertex->particles_out().size()==2);
532 pair<GenParticlePtr,GenParticlePtr> GenEvent::beam_particles()
const {
534 switch( m_rootvertex->particles_out().size() ) {
536 case 1:
return make_pair(m_rootvertex->particles_out()[0],
GenParticlePtr());
537 default:
return make_pair(m_rootvertex->particles_out()[0], m_rootvertex->particles_out()[1]);
543 m_rootvertex->add_particle_out(p1);
544 m_rootvertex->add_particle_out(p2);
547 void GenEvent::set_beam_particles(
const pair<GenParticlePtr,GenParticlePtr>& p) {
549 m_rootvertex->add_particle_out(p.first);
550 m_rootvertex->add_particle_out(p.second);
555 string GenEvent::attribute_as_string(
const string &name,
int id)
const {
557 std::map< string, std::map<int, shared_ptr<Attribute> > >::iterator i1 = m_attributes.find(name);
558 if( i1 == m_attributes.end() ) {
559 if (
id == 0 && run_info() ) {
560 return run_info()->attribute_as_string(name);
565 std::map<int, shared_ptr<Attribute> >::iterator i2 = i1->second.find(
id);
566 if (i2 == i1->second.end() )
return string();
568 if( !i2->second )
return string();
571 i2->second->to_string(ret);
const std::vector< GenParticlePtr > & particles() const
Get list of particles (const)
Stores serializable particle information.
std::vector< std::string > attribute_string
Attribute serialized as string.
void remove_particles(std::vector< GenParticlePtr > v)
Remove a set of particles.
void add_vertex(GenVertexPtr v)
Add vertex.
std::vector< int > links1
First id of the vertex links.
std::map< string, std::map< int, shared_ptr< Attribute > > > m_attributes
Map of event, particle and vertex attributes.
shared_ptr< T > m_data
Shared pointer.
void remove_particle(GenParticlePtr v)
Remove particle from the event.
std::vector< GenVertexData > vertices
Vertices.
Smart pointer for HepMC objects.
void remove_vertex(GenVertexPtr v)
Remove vertex from the event.
bool is_zero() const
Check if the length of this vertex is zero.
Stores vertex-related information.
std::map< int, shared_ptr< Attribute > >::value_type att_val_t
Attribute map value type.
std::vector< GenParticlePtr > m_particles
List of particles.
Stores serializable vertex information.
GenVertexPtr m_rootvertex
The root vertex is stored outside the normal vertices list to block user access to it...
std::vector< int > attribute_id
Attribute owner id.
std::vector< int > links2
Second id of the vertex links.
void add_particle(GenParticlePtr p)
Add particle.
std::vector< GenVertexPtr > m_vertices
List of vertices.
MomentumUnit
Momentum units.
std::map< string, std::map< int, shared_ptr< Attribute > > >::value_type att_key_t
Attribute map key type.
GenEvent(Units::MomentumUnit momentum_unit=Units::GEV, Units::LengthUnit length_unit=Units::MM)
Event constructor without a run.
const std::vector< GenVertexPtr > & vertices() const
Get list of vertices (const)
int event_number
Event number.
std::vector< std::string > attribute_name
Attribute name.
Units::LengthUnit length_unit
Length unit.
Definition of template class SmartPointer.
std::vector< GenParticleData > particles
Particles.
std::vector< double > weights
Weights.
Units::MomentumUnit momentum_unit
Momentum unit.
Stores serializable event information.
Stores particle-related information.
LengthUnit
Position units.
FourVector event_pos
Event position.