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-2021 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
19namespace 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
36void 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
49}
50
51
52void 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
65}
66
67void 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
75void 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
82void GenVertex::set_id(int id) {
83 m_id = id;
84 return;
85}
86
87
88const std::vector<ConstGenParticlePtr>& GenVertex::particles_in()const {
89 return *(reinterpret_cast<const std::vector<ConstGenParticlePtr>*>(&m_particles_in));
90}
91
92const 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 }
114}
115
117 m_data.position = new_pos;
118}
119
120bool 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
126void GenVertex::remove_attribute(const std::string& name) {
127 if ( parent_event() ) parent_event()->remove_attribute(name, id());
128}
129
130std::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
134std::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
Definition of class Attribute, class IntAttribute and class StringAttribute.
Definition of class GenEvent.
Definition of class GenParticle.
Definition of class GenVertex.
Definition of class Setup.
Generic 4-vector.
Definition: FourVector.h:36
static const FourVector & ZERO_VECTOR()
Static null FourVector = (0,0,0,0)
Definition: FourVector.h:293
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition: GenEvent.h:409
std::vector< std::string > attribute_names(const int &id=0) const
Get list of attribute names.
Definition: GenEvent.cc:621
void add_particle(GenParticlePtr p)
Add particle.
Definition: GenEvent.cc:48
const FourVector & event_pos() const
Vertex representing the overall event position.
Definition: GenEvent.cc:412
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
Definition: GenEvent.cc:806
void remove_attribute(const std::string &name, const int &id=0)
Remove attribute.
Definition: GenEvent.cc:609
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
GenVertex(const FourVector &position=FourVector::ZERO_VECTOR())
Default constructor.
Definition: GenVertex.cc:22
void remove_particle_out(GenParticlePtr p)
Remove outgoing particle.
Definition: GenVertex.cc:75
GenVertexData m_data
Vertex data.
Definition: GenVertex.h:152
const std::vector< GenParticlePtr > & particles_out()
Get list of outgoing particles.
Definition: GenVertex.h:93
void remove_particle_in(GenParticlePtr p)
Remove incoming particle.
Definition: GenVertex.cc:67
std::vector< GenParticlePtr > m_particles_in
Incoming particle list.
Definition: GenVertex.h:154
GenEvent * m_event
Parent event.
Definition: GenVertex.h:150
void add_particle_in(GenParticlePtr p)
Add incoming particle.
Definition: GenVertex.cc:36
void remove_attribute(const std::string &name)
Remove attribute.
Definition: GenVertex.cc:126
void set_position(const FourVector &new_pos)
Set vertex position.
Definition: GenVertex.cc:116
std::vector< GenParticlePtr > m_particles_out
Outgoing particle list.
Definition: GenVertex.h:156
bool add_attribute(const std::string &name, std::shared_ptr< Attribute > att)
Add event attribute to this vertex.
Definition: GenVertex.cc:120
int id() const
Definition: GenVertex.h:60
std::vector< std::string > attribute_names() const
Get list of names of attributes assigned to this particle.
Definition: GenVertex.cc:134
void set_id(int id)
set the vertex identifier
Definition: GenVertex.cc:82
const FourVector & position() const
Get vertex position.
Definition: GenVertex.cc:96
GenEvent * parent_event()
Get parent event.
Definition: GenVertex.h:49
int m_id
Vertex id.
Definition: GenVertex.h:151
bool has_set_position() const
Check if position of this vertex is set.
Definition: GenVertex.h:105
void add_particle_out(GenParticlePtr p)
Add outgoing particle.
Definition: GenVertex.cc:52
const std::vector< GenParticlePtr > & particles_in()
Get list of incoming particles.
Definition: GenVertex.h:89
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition: GenVertex.cc:130
Attribute that holds an Integer implemented as an int.
Definition: Attribute.h:157
HepMC3 main namespace.
Stores serializable vertex information.
Definition: GenVertexData.h:22
int status
Vertex status.
Definition: GenVertexData.h:23
FourVector position
Position in time-space.
Definition: GenVertexData.h:24