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