HepMC3 event record library: GenParticle.h Source File

HepMC3 event record library

GenParticle.h
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#ifndef HEPMC3_GENPARTICLE_H
7#define HEPMC3_GENPARTICLE_H
8/**
9 * @file GenParticle.h
10 * @brief Definition of \b class GenParticle
11 *
12 * @class HepMC3::GenParticle
13 * @brief Stores particle-related information
14 *
15 */
16#include <string>
17#include "HepMC3/Data/GenParticleData.h"
18#include "HepMC3/FourVector.h"
19
20#include "HepMC3/GenParticle_fwd.h"
21#include "HepMC3/GenVertex_fwd.h"
22
23#ifdef HEPMC3_PROTOBUFIO
24namespace HepMC3_pb {
25class GenEventData_GenParticleData;
26}
27#endif
28
29namespace HepMC3 {
30
31class GenEvent;
32class Attribute;
33
34 class GenParticle : public std::enable_shared_from_this<GenParticle> {
35
36 friend class GenVertex;
37 friend class GenEvent;
38
39//
40// Constructors
41//
42public:
43
44 /// @brief Default constructor
45 GenParticle( const FourVector &momentum = FourVector::ZERO_VECTOR(), int pid = 0, int status = 0 );
46
47 /// @brief Constructor based on particle data
48 GenParticle( const GenParticleData &data );
49
50#ifdef HEPMC3_PROTOBUFIO
51 /// @brief Constructor based on protobuf messages
52 GenParticle( HepMC3_pb::GenEventData_GenParticleData const &data );
53#endif
54
55//
56// Functions
57//
58public:
59 /// @brief Check if this particle belongs to an event
60 bool in_event() const { return static_cast<bool>((m_event)); }
61
62 /// @brief Get the parent event
63 GenEvent* parent_event() { return m_event; }
64 /// @brief Get the parent event (const)
65 const GenEvent* parent_event() const { return m_event; }
66
67 /// @brief Get the particle ID number (*not* PDG ID)
68 int id() const { return m_id; }
69
70 const GenParticleData& data() const { return m_data; } //!< Get particle data
71
72 ConstGenVertexPtr production_vertex() const; //!< Get production vertex (const version)
73 ConstGenVertexPtr end_vertex() const; //!< Get end vertex (const version)
74
75 GenVertexPtr production_vertex(); //!< Get production vertex
76 GenVertexPtr end_vertex(); //!< Get end vertex
77
78 /// @brief Convenience access to immediate incoming particles via production vertex
79 /// @note Less efficient than via the vertex since return must be by value (in case there is no vertex)
80 std::vector<GenParticlePtr> parents();
81
82 /// @brief Convenience access to immediate incoming particles via production vertex (const version)
83 /// @note Less efficient than via the vertex since return must be by value (in case there is no vertex)
84 std::vector<ConstGenParticlePtr> parents() const;
85
86 /// @brief Convenience access to immediate outgoing particles via end vertex
87 /// @note Less efficient than via the vertex since return must be by value (in case there is no vertex)
88 std::vector<GenParticlePtr> children();
89
90 /// @brief Convenience access to immediate outgoing particles via end vertex
91 /// @note Less efficient than via the vertex since return must be by value (in case there is no vertex)
92 std::vector<ConstGenParticlePtr> children() const;
93
94 int pid() const { return m_data.pid; } //!< Get PDG ID
95 int abs_pid() const { return std::abs(pid()); } //!< Get absolute value of PDG ID
96 int status() const { return m_data.status; } //!< Get status code
97 const FourVector& momentum() const { return m_data.momentum; } //!< Get momentum
98 bool is_generated_mass_set() const { return m_data.is_mass_set; } //!< Check if generated mass is set
99
100 /// @brief Get generated mass
101 ///
102 /// This function will return mass as set by a generator/tool.
103 /// If not set, it will return momentum().m()
104 double generated_mass() const;
105
106
107 void set_pid(int pid); //!< Set PDG ID
108 void set_status(int status); //!< Set status code
109 void set_momentum(const FourVector& momentum); //!< Set momentum
110 void set_generated_mass(double m); //!< Set generated mass
111 void unset_generated_mass(); //!< Declare that generated mass is not set
112
113 /// @brief Add an attribute to this particle
114 ///
115 /// This will overwrite existing attribute if an attribute with
116 /// the same name is present. The attribute will be stored in the
117 /// parent_event(). @return false if there is no parent_event();
118 bool add_attribute(const std::string& name, std::shared_ptr<Attribute> att);
119
120 /// @brief Get list of names of attributes assigned to this particle
121 std::vector<std::string> attribute_names() const;
122
123 /// @brief Remove attribute
124 void remove_attribute(const std::string& name);
125
126 /// @brief Get attribute of type T
127 template<class T>
128 std::shared_ptr<T> attribute(const std::string& name) const;
129
130 /// @brief Get attribute of any type as string
131 std::string attribute_as_string(const std::string& name) const;
132
133
134 /// @name Deprecated functionality
135 /// @{
136
137 /// @brief Get PDG ID
138 /// @deprecated Use pid() instead
139 int pdg_id() const { return pid(); }
140
141 /// @brief Set PDG ID
142 /// @deprecated Use set_pid() instead
143 void set_pdg_id(const int& pidin) { set_pid(pidin); }
144
145 /// @}
146//
147// Fields
148//
149private:
150 GenEvent *m_event; //!< Parent event
151 int m_id; //!< Index
152 GenParticleData m_data; //!< Particle data
153
154 std::weak_ptr<GenVertex> m_production_vertex; //!< Production vertex
155 std::weak_ptr<GenVertex> m_end_vertex; //!< End vertex
156};
157
158} // namespace HepMC3
159
160
161
162#include "HepMC3/GenEvent.h"
163namespace HepMC3 {
164/// @brief Get attribute of type T
165 template<class T> std::shared_ptr<T> GenParticle::attribute(const std::string& name) const {
166 return parent_event()?
167 parent_event()->attribute<T>(name, id()): std::shared_ptr<T>();
168}
169}
170#endif
FourVector.h
Definition of class FourVector.
GenEvent.h
Definition of class GenEvent.
GenParticleData.h
Definition of class GenParticleData.
HepMC3::Attribute
Base attribute class.
Definition Attribute.h:44
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
Stores event-related information.
Definition GenEvent.h:47
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::GenParticle::set_pid
void set_pid(int pid)
Set PDG ID.
Definition GenParticle.cc:40
HepMC3::GenParticle::end_vertex
ConstGenVertexPtr end_vertex() const
Get end vertex (const version).
Definition GenParticle.cc:74
HepMC3::GenParticle::unset_generated_mass
void unset_generated_mass()
Declare that generated mass is not set.
Definition GenParticle.cc:57
HepMC3::GenParticle::attribute
std::shared_ptr< T > attribute(const std::string &name) const
Get attribute of type T.
Definition GenParticle.h:165
HepMC3::GenParticle::GenParticle
GenParticle(const FourVector &momentum=FourVector::ZERO_VECTOR(), int pid=0, int status=0)
Default constructor.
Definition GenParticle.cc:20
HepMC3::GenParticle::m_event
GenEvent * m_event
Parent event.
Definition GenParticle.h:150
HepMC3::GenParticle::remove_attribute
void remove_attribute(const std::string &name)
Remove attribute.
Definition GenParticle.cc:105
HepMC3::GenParticle::m_production_vertex
std::weak_ptr< GenVertex > m_production_vertex
Production vertex.
Definition GenParticle.h:154
HepMC3::GenParticle::children
std::vector< GenParticlePtr > children()
Convenience access to immediate outgoing particles via end vertex.
Definition GenParticle.cc:86
HepMC3::GenParticle::add_attribute
bool add_attribute(const std::string &name, std::shared_ptr< Attribute > att)
Add an attribute to this particle.
Definition GenParticle.cc:94
HepMC3::GenParticle::id
int id() const
Get the particle ID number (not PDG ID).
Definition GenParticle.h:68
HepMC3::GenParticle::set_pdg_id
void set_pdg_id(const int &pidin)
Set PDG ID.
Definition GenParticle.h:143
HepMC3::GenParticle::attribute_names
std::vector< std::string > attribute_names() const
Get list of names of attributes assigned to this particle.
Definition GenParticle.cc:100
HepMC3::GenParticle::pid
int pid() const
Get PDG ID.
Definition GenParticle.h:94
HepMC3::GenParticle::m_end_vertex
std::weak_ptr< GenVertex > m_end_vertex
End vertex.
Definition GenParticle.h:155
HepMC3::GenParticle::data
const GenParticleData & data() const
Get particle data.
Definition GenParticle.h:70
HepMC3::GenParticle::parents
std::vector< GenParticlePtr > parents()
Convenience access to immediate incoming particles via production vertex.
Definition GenParticle.cc:78
HepMC3::GenParticle::production_vertex
ConstGenVertexPtr production_vertex() const
Get production vertex (const version).
Definition GenParticle.cc:66
HepMC3::GenParticle::status
int status() const
Get status code.
Definition GenParticle.h:96
HepMC3::GenParticle::set_momentum
void set_momentum(const FourVector &momentum)
Set momentum.
Definition GenParticle.cc:48
HepMC3::GenParticle::set_status
void set_status(int status)
Set status code.
Definition GenParticle.cc:44
HepMC3::GenParticle::parent_event
GenEvent * parent_event()
Get the parent event.
Definition GenParticle.h:63
HepMC3::GenParticle::set_generated_mass
void set_generated_mass(double m)
Set generated mass.
Definition GenParticle.cc:52
HepMC3::GenParticle::generated_mass
double generated_mass() const
Get generated mass.
Definition GenParticle.cc:36
HepMC3::GenParticle::m_id
int m_id
Index.
Definition GenParticle.h:151
HepMC3::GenParticle::in_event
bool in_event() const
Check if this particle belongs to an event.
Definition GenParticle.h:60
HepMC3::GenParticle::pdg_id
int pdg_id() const
Get PDG ID.
Definition GenParticle.h:139
HepMC3::GenParticle::is_generated_mass_set
bool is_generated_mass_set() const
Check if generated mass is set.
Definition GenParticle.h:98
HepMC3::GenParticle::abs_pid
int abs_pid() const
Get absolute value of PDG ID.
Definition GenParticle.h:95
HepMC3::GenParticle::parent_event
const GenEvent * parent_event() const
Get the parent event (const).
Definition GenParticle.h:65
HepMC3::GenParticle::m_data
GenParticleData m_data
Particle data.
Definition GenParticle.h:152
HepMC3::GenParticle::attribute_as_string
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition GenParticle.cc:109
HepMC3::GenParticle::momentum
const FourVector & momentum() const
Get momentum.
Definition GenParticle.h:97
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::GenParticleData
Stores serializable particle information.
Definition GenParticleData.h:24