HepMC3 event record library
Attribute.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_ATTRIBUTE_H
7 #define HEPMC3_ATTRIBUTE_H
8 /**
9  * @file Attribute.h
10  * @brief Definition of \b class Attribute, \b class IntAttribute and \b class StringAttribute
11  *
12  * @class HepMC3::Attribute
13  * @brief Base class for all attributes
14  *
15  * Contains virtual functions to_string and from_string that
16  * each attribute must implement, as well as init function that
17  * attributes should overload to initialize parsed attribute
18  *
19  * @ingroup attributes
20  *
21  */
22 #include <cstdio> // sprintf
23 #include <string>
24 #include <limits>
25 #include <sstream>
26 #include <iomanip>
27 #include <map>
28 
29 #include "HepMC3/GenParticle_fwd.h"
30 #include "HepMC3/GenVertex_fwd.h"
31 
32 using std::string;
33 
34 namespace HepMC3 {
35 
36 /** @brief Forward declaration of GenEvent. */
37 class GenEvent;
38 
39 /** @brief Forward declaration of GenRunInfo. */
40 class GenRunInfo;
41 
42 /** @brief Forward declaration of GenParticle. */
43 // class GenParticle;
44 
45 class Attribute {
46 //
47 // Constructors
48 //
49 public:
50  /** @brief Default constructor */
51  //Note: m_event should be set to nullptr in case event is deleted!
52  Attribute():m_is_parsed(true) { m_event=nullptr; }
53 
54  /** @brief Virtual destructor */
55  virtual ~Attribute() {}
56 
57 protected:
58  /** @brief Protected constructor that allows to set string
59  *
60  * Used when parsing attributes from file. An StringAttribute class
61  * object is made, which uses this constructor to signify that
62  * it just holds string without parsing it.
63  *
64  * @note There should be no need for user class to ever use this constructor
65  */
66  //Note: m_event should be set to nullptr n case event is deleted!
67  explicit Attribute(const string &st):m_is_parsed(false),m_string(st) { m_event=nullptr; }
68 
69  /** @brief GenEvent is a friend */
70  friend class GenEvent;
71 
72 //
73 // Virtual Functions
74 //
75 public:
76  /** @brief Fill class content from string.
77  */
78  virtual bool from_string(const string & att) = 0;
79 
80  /** @brief Optionally initialize the attribute after from_string.
81  */
82  virtual bool init() {
83  return true;
84  }
85 
86  /** @brief Optionally initialize the attribute after from_string
87  *
88  * Is passed a reference to the GenRunInfo object to which the
89  * Attribute belongs.
90  */
91  virtual bool init(const GenRunInfo & ) {
92  return true;
93  }
94 
95  /** @brief Fill string from class content */
96  virtual bool to_string(string &att) const = 0;
97 
98 //
99 // Accessors
100 //
101 public:
102  /** @brief Check if this attribute is parsed */
103  bool is_parsed() const { return m_is_parsed; }
104 
105  /** @brief Get unparsed string */
106  const string& unparsed_string() const { return m_string; }
107 
108  /** return the GenEvent to which this Attribute belongs, if at all. */
109  const GenEvent * event() const {
110  return m_event;
111  }
112 
113  /** return the GenParticle to which this Attribute belongs, if at all. */
114  GenParticlePtr particle() {
115  return m_particle;
116  }
117 
118  /** return the GenParticle to which this Attribute belongs, if at all. */
119  ConstGenParticlePtr particle() const {
120  return std::const_pointer_cast<GenParticle>(m_particle);
121  }
122 
123  /** return the GenVertex to which this Attribute belongs, if at all. */
124  GenVertexPtr vertex() {
125  return m_vertex;
126  }
127 
128  /** return the GenVertex to which this Attribute belongs, if at all. */
129  ConstGenVertexPtr vertex() const {
130  return std::const_pointer_cast<GenVertex>(m_vertex);
131  }
132 
133 protected:
134  /** @brief Set is_parsed flag */
135  void set_is_parsed(bool flag) { m_is_parsed = flag; }
136 
137  /** @brief Set unparsed string */
138  void set_unparsed_string(const string &st) { m_string = st; }
139 
140 //
141 // Fields
142 //
143 private:
144  bool m_is_parsed; //!< Is this attribute parsed?
145  string m_string; //!< Raw (unparsed) string
146  const GenEvent * m_event; //!< Possibility to be aware of the
147  //! controlling GenEvent object.
148  GenParticlePtr m_particle; //!< Particle to which assigned.
149  GenVertexPtr m_vertex; //!< Vertex to which assigned.
150 };
151 
152 /**
153  * @class HepMC3::IntAttribute
154  * @brief Attribute that holds an Integer implemented as an int
155  *
156  * @ingroup attributes
157  */
158 class IntAttribute : public Attribute {
159 public:
160 
161  /** @brief Default constructor */
163 
164  /** @brief Constructor initializing attribute value */
165  IntAttribute(int val):Attribute(),m_val(val) {}
166 
167  /** @brief Implementation of Attribute::from_string */
168  bool from_string(const string &att) {
169  m_val = atoi( att.c_str() );
170  return true;
171  }
172 
173  /** @brief Implementation of Attribute::to_string */
174  bool to_string(string &att) const {
175  att = std::to_string(m_val);
176  return true;
177  }
178 
179  /** @brief get the value associated to this Attribute. */
180  int value() const {
181  return m_val;
182  }
183 
184  /** @brief set the value associated to this Attribute. */
185  void set_value(const int& i) {
186  m_val = i;
187  }
188 
189 private:
190  int m_val; ///< Attribute value
191 };
192 
193 /**
194  * @class HepMC3::LongAttribute
195  * @brief Attribute that holds an Integer implemented as an int
196  *
197  * @ingroup attributes
198  */
199 class LongAttribute : public Attribute {
200 public:
201 
202  /** @brief Default constructor */
204 
205  /** @brief Constructor initializing attribute value */
206  LongAttribute(long val): Attribute(), m_val(val) {}
207 
208  /** @brief Implementation of Attribute::from_string */
209  bool from_string(const string &att) {
210  m_val = atoi( att.c_str() );
211  return true;
212  }
213 
214  /** @brief Implementation of Attribute::to_string */
215  bool to_string(string &att) const {
216  att = std::to_string(m_val);
217  return true;
218  }
219 
220  /** @brief get the value associated to this Attribute. */
221  long value() const {
222  return m_val;
223  }
224 
225  /** @brief set the value associated to this Attribute. */
226  void set_value(const long& l) {
227  m_val = l;
228  }
229 
230 private:
231 
232  long m_val; ///< Attribute value
233 
234 };
235 
236 /**
237  * @class HepMC3::DoubleAttribute
238  * @brief Attribute that holds a real number as a double.
239  *
240  * @ingroup attributes
241  */
242 class DoubleAttribute : public Attribute {
243 public:
244 
245  /** @brief Default constructor */
247 
248  /** @brief Constructor initializing attribute value */
249  DoubleAttribute(double val): Attribute(), m_val(val) {}
250 
251  /** @brief Implementation of Attribute::from_string */
252  bool from_string(const string &att) {
253  m_val = atof( att.c_str() );
254  return true;
255  }
256 
257  /** @brief Implementation of Attribute::to_string */
258  bool to_string(string &att) const {
259  std::ostringstream oss;
260  oss << std::setprecision(std::numeric_limits<double>::digits10)
261  << m_val;
262  att = oss.str();
263  return true;
264  }
265 
266  /** @brief get the value associated to this Attribute. */
267  double value() const {
268  return m_val;
269  }
270 
271  /** @brief set the value associated to this Attribute. */
272  void set_value(const double& d) {
273  m_val = d;
274  }
275 
276 private:
277 
278  double m_val; ///< Attribute value
279 };
280 
281 /**
282  * @class HepMC3::FloatAttribute
283  * @brief Attribute that holds a real number as a float.
284  *
285  * @ingroup attributes
286  */
287 class FloatAttribute : public Attribute {
288 public:
289 
290  /** @brief Default constructor */
292 
293  /** @brief Constructor initializing attribute value */
294  FloatAttribute(float val): Attribute(), m_val(val) {}
295 
296  /** @brief Implementation of Attribute::from_string */
297  bool from_string(const string &att) {
298  m_val = float(atof( att.c_str() ));
299  return true;
300  }
301 
302  /** @brief Implementation of Attribute::to_string */
303  bool to_string(string &att) const {
304  std::ostringstream oss;
305  oss << std::setprecision(std::numeric_limits<float>::digits10)
306  << m_val;
307  att = oss.str();
308  return true;
309  }
310 
311  /** @brief get the value associated to this Attribute. */
312  float value() const {
313  return m_val;
314  }
315 
316  /** @brief set the value associated to this Attribute. */
317  void set_value(const float& f) {
318  m_val = f;
319  }
320 
321 private:
322 
323  float m_val; ///< Attribute value
324 };
325 
326 /**
327  * @class HepMC3::StringAttribute
328  * @brief Attribute that holds a string
329  *
330  * Default attribute constructed when reading input files.
331  * It can be then parsed by other attributes or left as a string.
332  *
333  * @ingroup attributes
334  *
335  */
336 class StringAttribute : public Attribute {
337 public:
338 
339  /** @brief Default constructor - empty string */
341 
342  /** @brief String-based constructor
343  *
344  * The Attribute constructor used here marks that this is an unparsed
345  * string that can be (but does not have to be) parsed
346  *
347  */
348  StringAttribute(const string &st):Attribute(st) {}
349 
350  /** @brief Implementation of Attribute::from_string */
351  bool from_string(const string &att) {
352  set_unparsed_string(att);
353  return true;
354  }
355 
356  /** @brief Implementation of Attribute::to_string */
357  bool to_string(string &att) const {
358  att = unparsed_string();
359  return true;
360  }
361 
362  /** @brief get the value associated to this Attribute. */
363  string value() const {
364  return unparsed_string();
365  }
366 
367  /** @brief set the value associated to this Attribute. */
368  void set_value(const string& s) {
370  }
371 
372 };
373 
374 } // namespace HepMC3
375 
376 #endif
LongAttribute(long val)
Constructor initializing attribute value.
Definition: Attribute.h:206
const string & unparsed_string() const
Get unparsed string.
Definition: Attribute.h:106
Forward declaration of GenParticle.
Definition: Attribute.h:45
void set_value(const double &d)
set the value associated to this Attribute.
Definition: Attribute.h:272
float value() const
get the value associated to this Attribute.
Definition: Attribute.h:312
string m_string
Raw (unparsed) string.
Definition: Attribute.h:145
virtual bool to_string(string &att) const =0
Fill string from class content.
bool to_string(string &att) const
Implementation of Attribute::to_string.
Definition: Attribute.h:215
LongAttribute()
Default constructor.
Definition: Attribute.h:203
void set_value(const string &s)
set the value associated to this Attribute.
Definition: Attribute.h:368
FloatAttribute(float val)
Constructor initializing attribute value.
Definition: Attribute.h:294
bool is_parsed() const
Check if this attribute is parsed.
Definition: Attribute.h:103
GenParticlePtr particle()
Definition: Attribute.h:114
virtual bool from_string(const string &att)=0
Fill class content from string.
Attribute that holds a real number as a float.
Definition: Attribute.h:287
Stores vertex-related information.
Definition: GenVertex.h:27
bool to_string(string &att) const
Implementation of Attribute::to_string.
Definition: Attribute.h:357
GenVertexPtr vertex()
Definition: Attribute.h:124
long m_val
Attribute value.
Definition: Attribute.h:232
ConstGenParticlePtr particle() const
Definition: Attribute.h:119
string value() const
get the value associated to this Attribute.
Definition: Attribute.h:363
Stores run-related information.
Definition: GenRunInfo.h:32
virtual bool init()
Optionally initialize the attribute after from_string.
Definition: Attribute.h:82
Stores particle-related information.
Definition: GenParticle.h:30
void set_value(const int &i)
set the value associated to this Attribute.
Definition: Attribute.h:185
long value() const
get the value associated to this Attribute.
Definition: Attribute.h:221
Attribute that holds a string.
Definition: Attribute.h:336
double value() const
get the value associated to this Attribute.
Definition: Attribute.h:267
void set_unparsed_string(const string &st)
Set unparsed string.
Definition: Attribute.h:138
GenParticlePtr m_particle
Particle to which assigned.
Definition: Attribute.h:148
int m_val
Attribute value.
Definition: Attribute.h:190
float m_val
Attribute value.
Definition: Attribute.h:323
bool from_string(const string &att)
Implementation of Attribute::from_string.
Definition: Attribute.h:168
const GenEvent * event() const
Definition: Attribute.h:109
double m_val
Attribute value.
Definition: Attribute.h:278
Stores event-related information.
Definition: GenEvent.h:42
bool from_string(const string &att)
Implementation of Attribute::from_string.
Definition: Attribute.h:209
DoubleAttribute()
Default constructor.
Definition: Attribute.h:246
Attribute that holds a real number as a double.
Definition: Attribute.h:242
bool to_string(string &att) const
Implementation of Attribute::to_string.
Definition: Attribute.h:303
ConstGenVertexPtr vertex() const
Definition: Attribute.h:129
const GenEvent * m_event
Definition: Attribute.h:146
Minimal forward declarations for GenParticle.
Minimal forward declarations for GenVertex.
StringAttribute(const string &st)
String-based constructor.
Definition: Attribute.h:348
void set_value(const float &f)
set the value associated to this Attribute.
Definition: Attribute.h:317
bool to_string(string &att) const
Implementation of Attribute::to_string.
Definition: Attribute.h:174
virtual bool init(const GenRunInfo &)
Optionally initialize the attribute after from_string.
Definition: Attribute.h:91
void set_value(const long &l)
set the value associated to this Attribute.
Definition: Attribute.h:226
bool to_string(string &att) const
Implementation of Attribute::to_string.
Definition: Attribute.h:258
IntAttribute(int val)
Constructor initializing attribute value.
Definition: Attribute.h:165
GenVertexPtr m_vertex
Vertex to which assigned.
Definition: Attribute.h:149
void set_is_parsed(bool flag)
Set is_parsed flag.
Definition: Attribute.h:135
IntAttribute()
Default constructor.
Definition: Attribute.h:162
virtual ~Attribute()
Virtual destructor.
Definition: Attribute.h:55
FloatAttribute()
Default constructor.
Definition: Attribute.h:291
Attribute that holds an Integer implemented as an int.
Definition: Attribute.h:199
bool from_string(const string &att)
Implementation of Attribute::from_string.
Definition: Attribute.h:297
DoubleAttribute(double val)
Constructor initializing attribute value.
Definition: Attribute.h:249
bool from_string(const string &att)
Implementation of Attribute::from_string.
Definition: Attribute.h:351
int value() const
get the value associated to this Attribute.
Definition: Attribute.h:180
bool m_is_parsed
Is this attribute parsed?
Definition: Attribute.h:144
Attribute(const string &st)
Protected constructor that allows to set string.
Definition: Attribute.h:67
bool from_string(const string &att)
Implementation of Attribute::from_string.
Definition: Attribute.h:252
Attribute that holds an Integer implemented as an int.
Definition: Attribute.h:158
StringAttribute()
Default constructor - empty string.
Definition: Attribute.h:340
Attribute()
Default constructor.
Definition: Attribute.h:52