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