HepMC3 event record library
GenVertex.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5 //
6 /**
7  * @file GenVertex.cc
8  * @brief Implementation of \b class GenVertex
9  *
10  */
11 #include "HepMC3/GenVertex.h"
12 #include "HepMC3/GenParticle.h"
13 #include "HepMC3/GenEvent.h"
14 #include "HepMC3/Setup.h"
15 #include "HepMC3/Attribute.h"
16 #include <algorithm> // std::remove
17 
18 namespace HepMC3 {
19 
20 
22  m_event(nullptr),
23  m_id(0) {
24  m_data.status = 0;
25  m_data.position = pos;
26 }
27 
29  m_event(nullptr),
30  m_id(0),
31  m_data(dat) {
32 }
33 
34 
35 void GenVertex::add_particle_in( GenParticlePtr p ) {
36  if(!p) return;
37 
38  // Avoid duplicates
39  if (std::find(particles_in().begin(),particles_in().end(),p)!=particles_in().end()) return;
40 
41  m_particles_in.push_back(p);
42 
43  if( p->end_vertex() ) p->end_vertex()->remove_particle_in(p);
44 
45  p->m_end_vertex = shared_from_this();
46 
48 }
49 
50 
51 void GenVertex::add_particle_out(GenParticlePtr p ) {
52  if(!p) return;
53 
54  // Avoid duplicates
55  if (std::find(particles_out().begin(),particles_out().end(),p)!=particles_out().end()) return;
56 
57  m_particles_out.push_back(p);
58 
59  if( p->production_vertex() ) p->production_vertex()->remove_particle_out(p);
60 
61  p->m_production_vertex = shared_from_this();
62 
64 }
65 
66 void GenVertex::remove_particle_in( GenParticlePtr p ) {
67  if(!p) return;
68  if (std::find(m_particles_in.begin(),m_particles_in.end(),p)==m_particles_in.end()) return;
69  p->m_end_vertex.reset();
70  m_particles_in.erase( std::remove( m_particles_in.begin(), m_particles_in.end(), p), m_particles_in.end());
71 }
72 
73 
74 void GenVertex::remove_particle_out( GenParticlePtr p ) {
75  if(!p) return;
76  if (std::find(m_particles_out.begin(),m_particles_out.end(),p)==m_particles_out.end()) return;
77  p->m_production_vertex.reset();
78  m_particles_out.erase( std::remove( m_particles_out.begin(), m_particles_out.end(), p), m_particles_out.end());
79 }
80 
81 void GenVertex::set_id(int id) {
82  m_id = id;
83  return;
84 }
85 
86 
87 const std::vector<ConstGenParticlePtr>& GenVertex::particles_in()const {
88  return *(reinterpret_cast<const std::vector<ConstGenParticlePtr>*>(&m_particles_in));
89 }
90 
91 const std::vector<ConstGenParticlePtr>& GenVertex::particles_out()const {
92  return *(reinterpret_cast<const std::vector<ConstGenParticlePtr>*>(&m_particles_out));
93 }
94 
96 
97  if( has_set_position() ) return m_data.position;
98 
99  // No position information - look at event and/or search ancestors
100  if( parent_event() )
101  {
102  std::shared_ptr<IntAttribute> cycles=parent_event()->attribute<IntAttribute>("cycles");
103  //This could be a recussive call. Try to prevent it.
104  if (!cycles||cycles->value()==0)
105  {
106 
107  for(ConstGenParticlePtr p: m_particles_in ) {
108  ConstGenVertexPtr v = p->production_vertex();
109  if(v) return v->position();
110  }
111  }
112  return parent_event()->event_pos();
113  }
114  return FourVector::ZERO_VECTOR();
115 }
116 
117 void GenVertex::set_position(const FourVector& new_pos) {
118  m_data.position = new_pos;
119 }
120 
121 bool GenVertex::add_attribute(const std::string& name, std::shared_ptr<Attribute> att) {
122  if ( !parent_event() ) return false;
123  parent_event()->add_attribute(name, att, id());
124  return true;
125 }
126 
127 void GenVertex::remove_attribute(const std::string& name) {
128  if ( parent_event() ) parent_event()->remove_attribute(name, id());
129 }
130 
131 std::string GenVertex::attribute_as_string(const std::string& name) const {
132  return parent_event() ? parent_event()->attribute_as_string(name, id()) : std::string();
133 }
134 
135 std::vector<std::string> GenVertex::attribute_names() const {
136  if ( parent_event() ) return parent_event()->attribute_names(id());
137 
138  return std::vector<std::string>();
139 }
140 
141 } // namespace HepMC3
bool add_attribute(const std::string &name, std::shared_ptr< Attribute > att)
Add event attribute to this vertex.
Definition: GenVertex.cc:121
Definition of class GenParticle.
void remove_attribute(const std::string &name)
Remove attribute.
Definition: GenVertex.cc:127
FourVector position
Position in time-space.
Definition: GenVertexData.h:24
static const FourVector & ZERO_VECTOR()
Static null FourVector = (0,0,0,0)
Definition: FourVector.h:292
bool has_set_position() const
Check if position of this vertex is set.
Definition: GenVertex.h:99
void set_position(const FourVector &new_pos)
Set vertex position.
Definition: GenVertex.cc:117
Definition of class GenVertex.
GenEvent * parent_event()
Get parent event.
Definition: GenVertex.h:49
void add_particle(GenParticlePtr p)
Add particle.
Definition: GenEvent.cc:49
std::vector< GenParticlePtr > m_particles_out
Outgoing particle list.
Definition: GenVertex.h:150
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
Add event attribute to event.
Definition: GenEvent.h:208
std::vector< std::string > attribute_names() const
Get list of names of attributes assigned to this particle.
Definition: GenVertex.cc:135
const FourVector & event_pos() const
Vertex representing the overall event position.
Definition: GenEvent.cc:416
const std::vector< GenParticlePtr > & particles_in()
Get list of incoming particles.
Definition: GenVertex.h:83
GenVertex(const FourVector &position=FourVector::ZERO_VECTOR())
Default constructor.
Definition: GenVertex.cc:21
void add_particle_in(GenParticlePtr p)
Add incoming particle.
Definition: GenVertex.cc:35
int m_id
Vertex id.
Definition: GenVertex.h:145
GenVertexData m_data
Vertex data.
Definition: GenVertex.h:146
std::string attribute_as_string(const std::string &name, const int &id=0) const
Get attribute of any type as string.
Definition: GenEvent.cc:798
Generic 4-vector.
Definition: FourVector.h:35
int status
Vertex status.
Definition: GenVertexData.h:23
Stores serializable vertex information.
Definition: GenVertexData.h:22
GenEvent * m_event
Parent event.
Definition: GenVertex.h:144
const std::vector< GenParticlePtr > & particles_out()
Get list of outgoing particles.
Definition: GenVertex.h:87
int id() const
Definition: GenVertex.h:60
Definition of class Setup.
void add_particle_out(GenParticlePtr p)
Add outgoing particle.
Definition: GenVertex.cc:51
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition: GenEvent.h:389
void remove_attribute(const std::string &name, const int &id=0)
Remove attribute.
Definition: GenEvent.cc:621
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition: GenVertex.cc:131
void remove_particle_in(GenParticlePtr p)
Remove incoming particle.
Definition: GenVertex.cc:66
std::vector< std::string > attribute_names(const int &id=0) const
Get list of attribute names.
Definition: GenEvent.cc:633
void remove_particle_out(GenParticlePtr p)
Remove outgoing particle.
Definition: GenVertex.cc:74
Definition of class GenEvent.
Annotation for function names.
Definition: attr.h:36
Definition of class Attribute, class IntAttribute and class StringAttribute.
std::vector< GenParticlePtr > m_particles_in
Incoming particle list.
Definition: GenVertex.h:148
void set_id(int id)
set the vertex identifier
Definition: GenVertex.cc:81
const FourVector & position() const
Get vertex position.
Definition: GenVertex.cc:95
Attribute that holds an Integer implemented as an int.
Definition: Attribute.h:158