34 if ( run && !run->weight_names().empty() ) {
35 m_weights = std::vector<double>(run->weight_names().size(), 1.0);
40 return *(
reinterpret_cast<const std::vector<ConstGenParticlePtr>*
>(&
m_particles));
44 return *(
reinterpret_cast<const std::vector<ConstGenVertexPtr>*
>(&
m_vertices));
49 if ( !p || p->in_event() )
return;
57 if ( !p->production_vertex() ) {
68 std::lock_guard<std::recursive_mutex> rhs_lk(e.
m_lock_attributes, std::adopt_lock);
77 for (
auto att = attm->second.begin(); att != attm->second.end(); ++att) {
if (att->second) att->second->m_event =
nullptr;}
79 for (
auto v =
m_vertices.begin(); v !=
m_vertices.end(); ++v )
if (*v)
if ((*v)->m_event ==
this) (*v)->m_event =
nullptr;
80 for (
auto p =
m_particles.begin(); p !=
m_particles.end(); ++p )
if (*p)
if ((*p)->m_event ==
this) (*p)->m_event =
nullptr;
88 std::lock_guard<std::recursive_mutex> rhs_lk(e.
m_lock_attributes, std::adopt_lock);
98 if ( !v|| v->in_event() )
return;
105 for (
const auto& p: v->particles_in()) {
107 p->m_end_vertex = v->shared_from_this();
110 for (
const auto& p: v->particles_out()) {
112 p->m_production_vertex = v;
118 if ( !p || p->parent_event() !=
this )
return;
120 HEPMC3_DEBUG(30,
"GenEvent::remove_particle - called with particle: " << p->id());
121 GenVertexPtr end_vtx = p->end_vertex();
123 end_vtx->remove_particle_in(p);
126 if ( end_vtx->particles_in().empty() )
remove_vertex(end_vtx);
129 GenVertexPtr prod_vtx = p->production_vertex();
131 prod_vtx->remove_particle_out(p);
134 if ( prod_vtx->particles_out().empty() )
remove_vertex(prod_vtx);
137 HEPMC3_DEBUG(30,
"GenEvent::remove_particle - erasing particle: " << p->id())
145 auto vt2 = vt1.second.find(idx);
146 if (vt2 == vt1.second.end())
continue;
147 vt1.second.erase(vt2);
153 std::vector< std::pair< int, std::shared_ptr<Attribute> > > changed_attributes;
156 changed_attributes.clear();
158 for (
auto vt2 = vt1.second.begin(); vt2 != vt1.second.end(); ++vt2) {
159 if ( (*vt2).first > p->id() ) {
160 changed_attributes.emplace_back(*vt2);
164 for (
const auto& val: changed_attributes ) {
165 vt1.second.erase(val.first);
166 vt1.second[val.first-1] = val.second;
175 p->m_event =
nullptr;
180 std::sort(v.begin(), v.end(), [](
const GenParticlePtr& p1,
const GenParticlePtr& p2) { return p1->id() > p2->id();});
182 for (
auto p = v.begin(); p != v.end(); ++p) {
188 if ( !v || v->parent_event() !=
this )
return;
190 HEPMC3_DEBUG(30,
"GenEvent::remove_vertex - called with vertex: " << v->id());
191 std::shared_ptr<GenVertex> null_vtx;
193 for (
const auto& p: v->particles_in()) {
194 p->m_end_vertex = std::weak_ptr<GenVertex>();
197 for (
const auto& p: v->particles_out()) {
198 p->m_production_vertex = std::weak_ptr<GenVertex>();
205 HEPMC3_DEBUG(30,
"GenEvent::remove_vertex - erasing vertex: " << v->id())
212 auto vt2 = vt1.second.find(-idx);
213 if (vt2 == vt1.second.end())
continue;
214 vt1.second.erase(vt2);
221 std::vector< std::pair< int, std::shared_ptr<Attribute> > > changed_attributes;
224 changed_attributes.clear();
226 for (
auto vt2 = vt1.second.begin(); vt2 != vt1.second.end(); ++vt2) {
227 if ( (*vt2).first < v->id() ) {
228 changed_attributes.emplace_back(*vt2);
232 for (
const auto& val: changed_attributes ) {
233 vt1.second.erase(val.first);
234 vt1.second[val.first+1] = val.second;
244 v->m_event =
nullptr;
249static bool visit_children(std::map<ConstGenVertexPtr, int> &a,
const ConstGenVertexPtr& v)
251 for (
const ConstGenParticlePtr& p: v->particles_out()) {
254 if (a[p->end_vertex()] != 0) {
return true; }
255 a[p->end_vertex()]++;
266 bool has_cycles =
false;
267 std::map<GenVertexPtr, int> sortingv;
268 std::vector<GenVertexPtr> noinv;
269 if (existing_hc)
if (existing_hc->value() != 0) has_cycles =
true;
272 for (
const GenParticlePtr& p: parts) {
273 GenVertexPtr v = p->production_vertex();
274 if (v) sortingv[v]=0;
275 if ( !v || v->particles_in().empty()) {
276 GenVertexPtr v2 = p->end_vertex();
277 if (v2) {noinv.emplace_back(v2); sortingv[v2] = 0;}
280 for (
const GenVertexPtr& v: noinv) {
281 std::map<ConstGenVertexPtr, int> sorting_temp(sortingv.begin(), sortingv.end());
293 std::deque<GenVertexPtr> sorting;
296 for (
const auto& p: parts) {
297 const GenVertexPtr &v = p->production_vertex();
298 if ( !v || v->particles_in().empty() ) {
299 const GenVertexPtr &v2 = p->end_vertex();
300 if (v2) sorting.emplace_back(v2);
305 unsigned int sorting_loop_count = 0;
306 unsigned int max_deque_size = 0;
310 while ( !sorting.empty() ) {
312 if ( sorting.size() > max_deque_size ) max_deque_size = sorting.size();
313 ++sorting_loop_count;
316 GenVertexPtr &v = sorting.front();
321 for (
const auto& p: v->particles_in() ) {
322 GenVertexPtr v2 = p->production_vertex();
323 if ( v2 && !v2->in_event() && find(sorting.begin(), sorting.end(), v2) == sorting.end() ) {
324 sorting.push_front(v2);
331 if ( added )
continue;
334 if ( !v->in_event() ) {
338 for (
const auto& p: v->particles_out()) {
339 GenVertexPtr v2 = p->end_vertex();
340 if ( v2 && !v2->in_event()&& find(sorting.begin(), sorting.end(), v2) == sorting.end() ) {
341 sorting.emplace_back(v2);
357 std::vector< std::pair< int, std::shared_ptr<Attribute> > > changed_attributes;
358 for (
const auto& vt2 : vt1.second ) {
359 if ( vt2.first <= rootid ) {
360 changed_attributes.emplace_back(vt2);
363 for (
const auto& val : changed_attributes ) {
364 vt1.second.erase(val.first);
365 vt1.second[val.first == rootid? 0: val.first + 1] = val.second;
373 HEPMC3_WARNING(
"ReaderAsciiHepMC2: Suspicious looking rootvertex found. Will try to cope.")
379 << this->
particles().size() <<
", max deque size: "
380 << max_deque_size <<
", iterations: " << sorting_loop_count)
417 if (!status)
return std::const_pointer_cast<const GenVertex>(
m_rootvertex)->particles_out();
418 std::vector<ConstGenParticlePtr> ret;
419 for (
auto p:
m_rootvertex->particles_out())
if (p->status() == status) ret.emplace_back(p);
424 return std::const_pointer_cast<const GenVertex>(
m_rootvertex)->particles_out();
437 if ( v->has_set_position() ) {
438 v->set_position(v->position() + delta);
448 long double tempX = mom.
x();
449 long double tempY = mom.
y();
450 long double tempZ = mom.
z();
452 long double cosa = std::cos(delta.
x());
453 long double sina = std::sin(delta.
x());
455 long double tempY_ = cosa*tempY+sina*tempZ;
456 long double tempZ_ = -sina*tempY+cosa*tempZ;
461 long double cosb = std::cos(delta.
y());
462 long double sinb = std::sin(delta.
y());
464 long double tempX_ = cosb*tempX-sinb*tempZ;
465 tempZ_ = sinb*tempX+cosb*tempZ;
469 long double cosg = std::cos(delta.
z());
470 long double sing = std::sin(delta.
z());
472 tempX_ = cosg*tempX+sing*tempY;
473 tempY_ = -sing*tempX+cosg*tempY;
478 p->set_momentum(temp);
483 long double tempX = pos.
x();
484 long double tempY = pos.
y();
485 long double tempZ = pos.
z();
487 long double cosa = std::cos(delta.
x());
488 long double sina = std::sin(delta.
x());
490 long double tempY_ = cosa*tempY+sina*tempZ;
491 long double tempZ_ = -sina*tempY+cosa*tempZ;
496 long double cosb = std::cos(delta.
y());
497 long double sinb = std::sin(delta.
y());
499 long double tempX_ = cosb*tempX-sinb*tempZ;
500 tempZ_ = sinb*tempX+cosb*tempZ;
504 long double cosg = std::cos(delta.
z());
505 long double sing = std::sin(delta.
z());
507 tempX_ = cosg*tempX+sing*tempY;
508 tempY_ = -sing*tempX+cosg*tempY;
513 v->set_position(temp);
522 if ( axis > 3 || axis < 0 )
531 for (
auto& v:
m_vertices) {
FourVector temp = v->position(); temp.
setX(-v->position().x()); v->set_position(temp);}
535 for (
auto& v:
m_vertices) {
FourVector temp = v->position(); temp.
setY(-v->position().y()); v->set_position(temp);}
539 for (
auto& v:
m_vertices) {
FourVector temp = v->position(); temp.
setZ(-v->position().z()); v->set_position(temp);}
543 for (
auto& v:
m_vertices) {
FourVector temp = v->position(); temp.
setT(-v->position().t()); v->set_position(temp);}
554 double deltalength2d = delta.
length2();
555 if (deltalength2d > 1.0)
557 HEPMC3_WARNING(
"GenEvent::boost: wrong large boost vector. Will leave event as is.")
560 if (std::abs(deltalength2d-1.0) < std::numeric_limits<double>::epsilon())
562 HEPMC3_WARNING(
"GenEvent::boost: too large gamma. Will leave event as is.")
565 if (std::abs(deltalength2d) < std::numeric_limits<double>::epsilon())
567 HEPMC3_WARNING(
"GenEvent::boost: wrong small boost vector. Will leave event as is.")
570 long double deltaX = delta.
x();
571 long double deltaY = delta.
y();
572 long double deltaZ = delta.
z();
573 long double deltalength2 = deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ;
574 long double deltalength = std::sqrt(deltalength2);
575 long double gamma = 1.0/std::sqrt(1.0-deltalength2);
581 long double tempX = mom.
x();
582 long double tempY = mom.
y();
583 long double tempZ = mom.
z();
584 long double tempE = mom.
e();
585 long double nr = (deltaX*tempX+deltaY*tempY+deltaZ*tempZ)/deltalength;
587 tempX+=(deltaX*((gamma-1)*nr/deltalength)-deltaX*(tempE*gamma));
588 tempY+=(deltaY*((gamma-1)*nr/deltalength)-deltaY*(tempE*gamma));
589 tempZ+=(deltaZ*((gamma-1)*nr/deltalength)-deltaZ*(tempE*gamma));
590 tempE = gamma*(tempE-deltalength*nr);
592 p->set_momentum(temp);
613 auto i2 = i1->second.find(
id);
614 if ( i2 == i1->second.end() )
return;
616 i1->second.erase(i2);
620 std::vector<std::string> results;
623 if ( vt1.second.count(
id) == 1 ) {
624 results.emplace_back(vt1.first);
650 for (
const ConstGenParticlePtr& p: this->
particles()) {
654 for (
const ConstGenVertexPtr& v: this->
vertices()) {
655 data.
vertices.emplace_back(v->data());
658 for (
const ConstGenParticlePtr& p: v->particles_in()) {
659 data.
links1.emplace_back(p->id());
660 data.
links2.emplace_back(v_id);
663 for (
const ConstGenParticlePtr& p: v->particles_out()) {
664 data.
links1.emplace_back(v_id);
665 data.
links2.emplace_back(p->id());
673 bool status = vt2.second->to_string(st);
676 HEPMC3_WARNING(
"GenEvent::write_data: problem serializing attribute: " << vt1.first)
703 m_particles.emplace_back(std::make_shared<GenParticle>(pd));
710 m_vertices.emplace_back(std::make_shared<GenVertex>(vd));
716 for (
unsigned int i = 0; i < data.
links1.size(); ++i) {
717 const int id1 = data.
links1[i];
718 const int id2 = data.
links2[i];
724 if ((id1 < 0 && id2 <0) || (id1 > 0 && id2 > 0)) {
HEPMC3_WARNING(
"GenEvent::read_data: wrong link: " << id1 <<
" " << id2);
continue;}
733 for (
unsigned int i = 0; i < data.
attribute_id.size(); ++i) {
736 if (name.length() == 0)
continue;
744 if (
id < 0 && -
id <=
int(
m_vertices.size()) ) {
774 HEPMC3_WARNING(
"Attempting to add an empty particle as beam particle. Ignored.")
777 if (p1->in_event() && p1->parent_event() !=
this)
779 HEPMC3_WARNING(
"Attempting to add particle from another event. Ignored.")
782 if (p1->production_vertex()) p1->production_vertex()->remove_particle_out(p1);
794 return run_info()->attribute_as_string(name);
799 auto i2 = i1->second.find(
id);
800 if (i2 == i1->second.end() )
return {};
802 if ( !i2->second )
return {};
805 i2->second->to_string(ret);
812 if (name.length() == 0)
return;
818 if (
id > 0 &&
id <=
int(
particles().size()) ) {
821 if (
id < 0 && -
id <=
int(
vertices().size()) ) {
822 att->m_vertex =
vertices()[-
id - 1];
827void GenEvent::add_attributes(
const std::vector<std::string> &names,
const std::vector<std::shared_ptr<Attribute> > &atts,
const std::vector<int>& ids) {
828 size_t N = names.size();
829 if ( N == 0 )
return;
830 if (N != atts.size())
return;
831 if (N != ids.size())
return;
833 std::vector<std::string> unames = names;
834 vector<std::string>::iterator ip;
835 ip = std::unique(unames.begin(), unames.end());
836 unames.resize(std::distance(unames.begin(), ip));
838 for (
const auto& name: unames) {
841 const int particles_size = int(
m_particles.size());
843 for (
size_t i = 0; i < N; i++) {
845 if (names.at(i).length() == 0)
continue;
846 if (!atts[i])
continue;
848 atts[i]->m_event =
this;
849 if ( ids.at(i) > 0 && ids.at(i) <= particles_size )
850 { atts[i]->m_particle =
m_particles[ids.at(i) - 1]; }
853 atts[i]->m_vertex =
m_vertices[-ids.at(i) - 1];
859void GenEvent::add_attributes(
const std::string& name,
const std::vector<std::shared_ptr<Attribute> > &atts,
const std::vector<int>& ids) {
860 if (name.length() == 0)
return;
861 size_t N = ids.size();
863 if ( N != atts.size())
return;
868 const int particles_size = int(
m_particles.size());
870 for (
size_t i = 0; i < N; i++) {
872 if (!atts[i])
continue;
873 tmap[ids.at(i)] = atts[i];
874 atts[i]->m_event =
this;
875 if ( ids.at(i) > 0 && ids.at(i) <= particles_size )
876 { atts[i]->m_particle =
m_particles[ids.at(i) - 1]; }
879 atts[i]->m_vertex =
m_vertices[-ids.at(i) - 1];
885 if (name.length() == 0)
return;
886 if (atts.empty())
return;
890 const int particles_size = int(
m_particles.size());
892 for (
const auto& att: atts) {
894 if (!att.second)
continue;
896 att.second->m_event =
this;
897 if ( att.first > 0 && att.first <= particles_size )
898 { att.second->m_particle =
m_particles[att.first - 1]; }
901 att.second->m_vertex =
m_vertices[-att.first - 1];
#define HEPMC3_DEBUG_CODE_BLOCK(x)
Macro for storing code useful for debugging.
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
#define HEPMC3_DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
Definition of struct GenEventData.
Definition of class GenEvent.
Definition of class GenParticle.
Definition of class GenVertex.
double e() const
Energy component of momentum.
double t() const
Time component of position/displacement.
bool is_zero() const
Check if the length of this vertex is zero.
double x() const
x-component of position/displacement
double length2() const
Squared magnitude of (x, y, z) 3-vector.
double y() const
y-component of position/displacement
double z() const
z-component of position/displacement
GenEvent(Units::MomentumUnit mu=Units::GEV, Units::LengthUnit lu=Units::MM)
Event constructor without a run.
bool rotate(const FourVector &delta)
Rotate event using x,y,z components of delta as rotation angles.
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
void add_vertex(GenVertexPtr v)
Add vertex.
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.
int vertices_size() const
Vertices size, HepMC2 compatibility.
int event_number() const
Get event number.
std::vector< std::string > attribute_names(const int &id=0) const
Get list of attribute names.
void write_data(GenEventData &data) const
Fill GenEventData object.
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
void remove_particles(std::vector< GenParticlePtr > v)
Remove a set of particles.
void add_particle(GenParticlePtr p)
Add particle.
void set_units(Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit)
Change event units Converts event from current units to new ones.
std::vector< double > m_weights
Event weights.
std::map< int, std::shared_ptr< Attribute > >::value_type att_val_t
Attribute map value type.
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > >::value_type att_key_t
Attribute map key type.
void shift_position_by(const FourVector &delta)
Shift position of all vertices in the event by delta.
void remove_vertex(GenVertexPtr v)
Remove vertex from the event.
void reserve(const size_t &parts, const size_t &verts=0)
Reserve memory for particles and vertices.
void set_event_number(const int &num)
Set event number.
bool boost(const FourVector &delta)
Boost event using x,y,z components of delta as velocities.
std::vector< ConstGenParticlePtr > beams() const
Vector of beam particles.
int m_event_number
Event number.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
const Units::LengthUnit & length_unit() const
Get length unit.
bool reflect(const int axis)
Change sign of axis.
const FourVector & event_pos() const
Vertex representing the overall event position.
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
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.
Units::MomentumUnit m_momentum_unit
Momentum unit.
void remove_attribute(const std::string &name, const int &id=0)
Remove attribute.
void remove_particle(GenParticlePtr p)
Remove particle from the event.
std::vector< GenVertexPtr > m_vertices
List of vertices.
GenVertexPtr m_rootvertex
The root vertex is stored outside the normal vertices list to block user access to it.
std::string attribute_as_string(const std::string &name, const int &id=0) const
Get attribute of any type as string.
const std::vector< double > & weights() const
Get event weight values as a vector.
void clear()
Remove contents of this event.
Units::LengthUnit m_length_unit
Length unit.
std::shared_ptr< GenRunInfo > m_run_info
Global run information.
std::vector< GenParticlePtr > m_particles
List of particles.
void add_beam_particle(GenParticlePtr p1)
Add particle to root vertex.
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.
void set_beam_particles(GenParticlePtr p1, GenParticlePtr p2)
Set incoming beam particles.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
void add_tree(const std::vector< GenParticlePtr > &parts)
Add whole tree in topological order.
GenEvent & operator=(const GenEvent &)
Assignment operator.
Stores particle-related information.
Stores vertex-related information.
static void convert(T &m, MomentumUnit from, MomentumUnit to)
Convert FourVector to different momentum unit.
LengthUnit
Position units.
MomentumUnit
Momentum units.
static bool visit_children(std::map< ConstGenVertexPtr, int > &a, const ConstGenVertexPtr &v)
Stores serializable event information.
std::vector< GenVertexData > vertices
Vertices.
std::vector< int > links2
Second id of the vertex links.
int event_number
Event number.
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< GenParticleData > particles
Particles.
std::vector< int > links1
First id of the vertex links.
std::vector< std::string > attribute_name
Attribute name.
Units::LengthUnit length_unit
Length unit.
std::vector< int > attribute_id
Attribute owner id.
FourVector event_pos
Event position.
std::vector< double > weights
Weights.
Units::MomentumUnit momentum_unit
Momentum unit.
Stores serializable particle information.
Stores serializable vertex information.