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-2023 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/Attribute.h"
14#include "HepMC3/GenEvent.h"
15#include "HepMC3/GenParticle.h"
16#include "HepMC3/GenVertex.h"
17#include "HepMC3/Setup.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.emplace_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.emplace_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}
85
86
87const std::vector<ConstGenParticlePtr>& GenVertex::particles_in()const {
88 return *(reinterpret_cast<const std::vector<ConstGenParticlePtr>*>(&m_particles_in));
89}
90
91const std::vector<ConstGenParticlePtr>& GenVertex::particles_out()const {
92 return *(reinterpret_cast<const std::vector<ConstGenParticlePtr>*>(&m_particles_out));
93}
94
96 if ( has_set_position() ) return m_data.position;
97
98 // No position information - look at event and/or search ancestors
99 if ( parent_event() )
100 {
101 std::shared_ptr<IntAttribute> cycles = parent_event()->attribute<IntAttribute>("cycles");
102 //This could be a recussive call. Try to prevent it.
103 if (!cycles || cycles->value() == 0)
104 {
105 for (const auto& p: m_particles_in) {
106 ConstGenVertexPtr v = p->production_vertex();
107 if (v) return v->position();
108 }
109 }
110 return parent_event()->event_pos();
111 }
113}
114
116 m_data.position = new_pos;
117}
118
119bool GenVertex::add_attribute(const std::string& name, std::shared_ptr<Attribute> att) {
120 if ( !parent_event() ) return false;
121 parent_event()->add_attribute(name, att, id());
122 return true;
123}
124
125void GenVertex::remove_attribute(const std::string& name) {
126 if ( parent_event() ) parent_event()->remove_attribute(name, id());
127}
128
129std::string GenVertex::attribute_as_string(const std::string& name) const {
130 return parent_event() ? parent_event()->attribute_as_string(name, id()) : std::string();
131}
132
133std::vector<std::string> GenVertex::attribute_names() const {
134 if ( parent_event() ) return parent_event()->attribute_names(id());
135
136 return {};
137}
138
139} // 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:297
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition GenEvent.h:417
std::vector< std::string > attribute_names(const int &id=0) const
Get list of attribute names.
Definition GenEvent.cc:619
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:810
void remove_attribute(const std::string &name, const int &id=0)
Remove attribute.
Definition GenEvent.cc:608
std::string attribute_as_string(const std::string &name, const int &id=0) const
Get attribute of any type as string.
Definition GenEvent.cc:789
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:150
const std::vector< GenParticlePtr > & particles_out()
Get list of outgoing particles.
Definition GenVertex.h:91
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:152
GenEvent * m_event
Parent event.
Definition GenVertex.h:148
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:125
void set_position(const FourVector &new_pos)
Set vertex position.
Definition GenVertex.cc:115
std::vector< GenParticlePtr > m_particles_out
Outgoing particle list.
Definition GenVertex.h:154
bool add_attribute(const std::string &name, std::shared_ptr< Attribute > att)
Add event attribute to this vertex.
Definition GenVertex.cc:119
int id() const
Definition GenVertex.h:58
std::vector< std::string > attribute_names() const
Get list of names of attributes assigned to this particle.
Definition GenVertex.cc:133
void set_id(int id)
set the vertex identifier
Definition GenVertex.cc:82
const FourVector & position() const
Get vertex position.
Definition GenVertex.cc:95
GenEvent * parent_event()
Get parent event.
Definition GenVertex.h:47
int m_id
Vertex id.
Definition GenVertex.h:149
bool has_set_position() const
Check if position of this vertex is set.
Definition GenVertex.h:103
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:87
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition GenVertex.cc:129
Attribute that holds an Integer implemented as an int.
Definition Attribute.h:157
HepMC3 main namespace.
Stores serializable vertex information.
int status
Vertex status.
FourVector position
Position in time-space.