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