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
48 if (m_event) m_event->add_particle(p);
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
64 if (m_event) m_event->add_particle(p);
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
82const std::vector<ConstGenParticlePtr>& GenVertex::particles_in()const {
83 return *(reinterpret_cast<const std::vector<ConstGenParticlePtr>*>(&m_particles_in));
84}
85
86const std::vector<ConstGenParticlePtr>& GenVertex::particles_out()const {
87 return *(reinterpret_cast<const std::vector<ConstGenParticlePtr>*>(&m_particles_out));
88}
89
91 if ( has_set_position() ) return m_data.position;
92
93 // No position information - look at event and/or search ancestors
94 if ( parent_event() )
95 {
96 std::shared_ptr<IntAttribute> cycles = parent_event()->attribute<IntAttribute>("cycles");
97 //This could be a recussive call. Try to prevent it.
98 if (!cycles || cycles->value() == 0)
99 {
100 for (const auto& p: m_particles_in) {
101 ConstGenVertexPtr v = p->production_vertex();
102 if (v) return v->position();
103 }
104 }
105 return parent_event()->event_pos();
106 }
108}
109
111 m_data.position = new_pos;
112}
113
114bool GenVertex::add_attribute(const std::string& name, std::shared_ptr<Attribute> att) {
115 if ( !parent_event() ) return false;
116 parent_event()->add_attribute(name, att, id());
117 return true;
118}
119
120void GenVertex::remove_attribute(const std::string& name) {
121 if ( parent_event() ) parent_event()->remove_attribute(name, id());
122}
123
124std::string GenVertex::attribute_as_string(const std::string& name) const {
125 return parent_event() ? parent_event()->attribute_as_string(name, id()) : std::string();
126}
127
128std::vector<std::string> GenVertex::attribute_names() const {
129 if ( parent_event() ) return parent_event()->attribute_names(id());
130
131 return {};
132}
133
134} // 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:306
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition GenEvent.h:420
std::vector< std::string > attribute_names(const int &id=0) const
Get list of attribute names.
Definition GenEvent.cc:613
const FourVector & event_pos() const
Vertex representing the overall event position.
Definition GenEvent.cc:418
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
Definition GenEvent.cc:797
void remove_attribute(const std::string &name, const int &id=0)
Remove attribute.
Definition GenEvent.cc:602
std::string attribute_as_string(const std::string &name, const int &id=0) const
Get attribute of any type as string.
Definition GenEvent.cc:776
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:141
const std::vector< GenParticlePtr > & particles_out()
Get list of outgoing particles.
Definition GenVertex.h:97
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:143
GenEvent * m_event
Parent event.
Definition GenVertex.h:139
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:120
void set_position(const FourVector &new_pos)
Set vertex position.
Definition GenVertex.cc:110
std::vector< GenParticlePtr > m_particles_out
Outgoing particle list.
Definition GenVertex.h:145
bool add_attribute(const std::string &name, std::shared_ptr< Attribute > att)
Add event attribute to this vertex.
Definition GenVertex.cc:114
std::vector< std::string > attribute_names() const
Get list of names of attributes assigned to this particle.
Definition GenVertex.cc:128
const FourVector & position() const
Get vertex position.
Definition GenVertex.cc:90
GenEvent * parent_event()
Get parent event.
Definition GenVertex.h:56
int m_id
Vertex id.
Definition GenVertex.h:140
bool has_set_position() const
Check if position of this vertex is set.
Definition GenVertex.h:109
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:93
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition GenVertex.cc:124
Attribute that holds an Integer implemented as an int.
Definition Attribute.h:157
HepMC3 main namespace.
Stores serializable vertex information.