HepMC3 event record library: GenVertex.cc Source File

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
22GenVertex::GenVertex(const FourVector& pos):
23 m_event(nullptr),
24 m_id(0) {
25 m_data.status = 0;
26 m_data.position = pos;
27}
28
29 GenVertex::GenVertex(const GenVertexData &dat):
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.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
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.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
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 const std::vector<ConstGenParticlePtr>& GenVertex::particles_in()const {
83 return *(reinterpret_cast<const std::vector<ConstGenParticlePtr>*>(&m_particles_in));
84}
85
86 const std::vector<ConstGenParticlePtr>& GenVertex::particles_out()const {
87 return *(reinterpret_cast<const std::vector<ConstGenParticlePtr>*>(&m_particles_out));
88}
89
90 const FourVector& GenVertex::position() const {
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 }
107 return FourVector::ZERO_VECTOR();
108}
109
110 void GenVertex::set_position(const FourVector& new_pos) {
111 m_data.position = new_pos;
112}
113
114 bool 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
120 void 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
Attribute.h
Definition of class Attribute, class IntAttribute and class StringAttribute.
GenEvent.h
Definition of class GenEvent.
GenParticle.h
Definition of class GenParticle.
GenVertex.h
Definition of class GenVertex.
Setup.h
Definition of class Setup.
HepMC3::FourVector
Generic 4-vector.
Definition FourVector.h:36
HepMC3::FourVector::ZERO_VECTOR
static const FourVector & ZERO_VECTOR()
Static null FourVector = (0,0,0,0).
Definition FourVector.h:306
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:420
HepMC3::GenEvent::attribute_names
std::vector< std::string > attribute_names(const int &id=0) const
Get list of attribute names.
Definition GenEvent.cc:613
HepMC3::GenEvent::event_pos
const FourVector & event_pos() const
Vertex representing the overall event position.
Definition GenEvent.cc:418
HepMC3::GenEvent::add_attribute
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
Definition GenEvent.cc:797
HepMC3::GenEvent::remove_attribute
void remove_attribute(const std::string &name, const int &id=0)
Remove attribute.
Definition GenEvent.cc:602
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:776
HepMC3::GenVertex::GenVertex
GenVertex(const FourVector &position=FourVector::ZERO_VECTOR())
Default constructor.
Definition GenVertex.cc:22
HepMC3::GenVertex::remove_particle_out
void remove_particle_out(GenParticlePtr p)
Remove outgoing particle.
Definition GenVertex.cc:75
HepMC3::GenVertex::m_data
GenVertexData m_data
Vertex data.
Definition GenVertex.h:141
HepMC3::GenVertex::particles_out
const std::vector< GenParticlePtr > & particles_out()
Get list of outgoing particles.
Definition GenVertex.h:97
HepMC3::GenVertex::remove_particle_in
void remove_particle_in(GenParticlePtr p)
Remove incoming particle.
Definition GenVertex.cc:67
HepMC3::GenVertex::m_particles_in
std::vector< GenParticlePtr > m_particles_in
Incoming particle list.
Definition GenVertex.h:143
HepMC3::GenVertex::m_event
GenEvent * m_event
Parent event.
Definition GenVertex.h:139
HepMC3::GenVertex::add_particle_in
void add_particle_in(GenParticlePtr p)
Add incoming particle.
Definition GenVertex.cc:36
HepMC3::GenVertex::remove_attribute
void remove_attribute(const std::string &name)
Remove attribute.
Definition GenVertex.cc:120
HepMC3::GenVertex::set_position
void set_position(const FourVector &new_pos)
Set vertex position.
Definition GenVertex.cc:110
HepMC3::GenVertex::m_particles_out
std::vector< GenParticlePtr > m_particles_out
Outgoing particle list.
Definition GenVertex.h:145
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:114
HepMC3::GenVertex::attribute_names
std::vector< std::string > attribute_names() const
Get list of names of attributes assigned to this particle.
Definition GenVertex.cc:128
HepMC3::GenVertex::position
const FourVector & position() const
Get vertex position.
Definition GenVertex.cc:90
HepMC3::GenVertex::parent_event
GenEvent * parent_event()
Get parent event.
Definition GenVertex.h:56
HepMC3::GenVertex::m_id
int m_id
Vertex id.
Definition GenVertex.h:140
HepMC3::GenVertex::has_set_position
bool has_set_position() const
Check if position of this vertex is set.
Definition GenVertex.h:109
HepMC3::GenVertex::add_particle_out
void add_particle_out(GenParticlePtr p)
Add outgoing particle.
Definition GenVertex.cc:52
HepMC3::GenVertex::particles_in
const std::vector< GenParticlePtr > & particles_in()
Get list of incoming particles.
Definition GenVertex.h:93
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:124
HepMC3::IntAttribute
Attribute that holds an Integer implemented as an int.
Definition Attribute.h:157
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::GenVertexData
Stores serializable vertex information.
Definition GenVertexData.h:22