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