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>
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
34class 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
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
165template<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
Definition of class FourVector.
Definition of class GenEvent.
Definition of class GenParticleData.
Base attribute class.
Definition Attribute.h:44
Generic 4-vector.
Definition FourVector.h:36
static const FourVector & ZERO_VECTOR()
Static null FourVector = (0,0,0,0)
Definition FourVector.h:306
Stores event-related information.
Definition GenEvent.h:47
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition GenEvent.h:420
void set_pid(int pid)
Set PDG ID.
ConstGenVertexPtr end_vertex() const
Get end vertex (const version)
void unset_generated_mass()
Declare that generated mass is not set.
std::shared_ptr< T > attribute(const std::string &name) const
Get attribute of type T.
GenParticle(const FourVector &momentum=FourVector::ZERO_VECTOR(), int pid=0, int status=0)
Default constructor.
GenEvent * m_event
Parent event.
void remove_attribute(const std::string &name)
Remove attribute.
std::weak_ptr< GenVertex > m_production_vertex
Production vertex.
std::vector< GenParticlePtr > children()
Convenience access to immediate outgoing particles via end vertex.
bool add_attribute(const std::string &name, std::shared_ptr< Attribute > att)
Add an attribute to this particle.
int id() const
Get the particle ID number (not PDG ID)
Definition GenParticle.h:68
void set_pdg_id(const int &pidin)
Set PDG ID.
std::vector< std::string > attribute_names() const
Get list of names of attributes assigned to this particle.
int pid() const
Get PDG ID.
Definition GenParticle.h:94
std::weak_ptr< GenVertex > m_end_vertex
End vertex.
const GenParticleData & data() const
Get particle data.
Definition GenParticle.h:70
std::vector< GenParticlePtr > parents()
Convenience access to immediate incoming particles via production vertex.
ConstGenVertexPtr production_vertex() const
Get production vertex (const version)
int status() const
Get status code.
Definition GenParticle.h:96
void set_momentum(const FourVector &momentum)
Set momentum.
void set_status(int status)
Set status code.
GenEvent * parent_event()
Get the parent event.
Definition GenParticle.h:63
void set_generated_mass(double m)
Set generated mass.
double generated_mass() const
Get generated mass.
bool in_event() const
Check if this particle belongs to an event.
Definition GenParticle.h:60
int pdg_id() const
Get PDG ID.
bool is_generated_mass_set() const
Check if generated mass is set.
Definition GenParticle.h:98
int abs_pid() const
Get absolute value of PDG ID.
Definition GenParticle.h:95
const GenEvent * parent_event() const
Get the parent event (const)
Definition GenParticle.h:65
GenParticleData m_data
Particle data.
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
const FourVector & momentum() const
Get momentum.
Definition GenParticle.h:97
HepMC3 main namespace.
Stores serializable particle information.