HepMC3 event record library
GenRunInfo.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 ///
7 /// @file GenRunInfo.h
8 /// @brief Definition of \b class GenRunInfo
9 ///
10 #ifndef HEPMC3_GENRUNINFO_H
11 #define HEPMC3_GENRUNINFO_H
12 
13 #if !defined(__CINT__)
14 #include "HepMC3/Units.h"
15 #include "HepMC3/Attribute.h"
16 #include <mutex>
17 #endif // __CINT__
18 
19 #ifdef HEPMC3_ROOTIO
20 class TBuffer;
21 #endif
22 
23 namespace HepMC3 {
24 using namespace std;
25 
26 struct GenRunInfoData;
27 
28 /// @brief Stores run-related information
29 ///
30 /// Manages run-related information.
31 /// Contains run-wide attributes
32 class GenRunInfo {
33 
34 public:
35 
36  /// @brief Interrnal struct for keeping track of tools.
37  struct ToolInfo {
38 
39  /// @brief The name of the tool.
40  string name;
41 
42  /// @brief The version of the tool.
43  string version;
44 
45  /// @brief Other information about how the tool was used in
46  /// the run.
47  string description;
48  };
49 
50 public:
51 
52  /// @brief Default constructor
54  /// @brief Copy constructor
55  GenRunInfo(const GenRunInfo& r);
56  /// @brief Assignmet
57  GenRunInfo& operator=(const GenRunInfo& r);
58 
59 #if !defined(__CINT__)
60 
61  /// @brief The vector of tools used to produce this run.
62  const std::vector<ToolInfo> & tools() const {
63  return m_tools;
64  }
65  /// @brief The vector of tools used to produce this run.
66  std::vector<ToolInfo> & tools() {
67  return m_tools;
68  }
69 
70  /// @brief Check if a weight name is present.
71  bool has_weight(const string& name) const {
72  return m_weight_indices.find(name) != m_weight_indices.end();
73  }
74 
75  /// @brief Return the index corresponding to a weight name.
76  /// @return -1 if name was not found
77  int weight_index(const string& name) const {
78  std::map<std::string, int>::const_iterator it = m_weight_indices.find(name);
79  return it == m_weight_indices.end()? -1: it->second;
80  }
81 
82  /// @brief Get the vector of weight names.
83  const std::vector<std::string> & weight_names() const {
84  return m_weight_names;
85  }
86 
87  /// @brief Set the names of the weights in this run.
88  ///
89  /// For consistency, the length of the vector should be the same as
90  /// the number of weights in the events in the run.
91  void set_weight_names(const std::vector<std::string> & names);
92 
93  /// @brief add an attribute
94  /// This will overwrite existing attribute if an attribute
95  /// with the same name is present
96  void add_attribute(const string &name,
97  const shared_ptr<Attribute> &att) {
98  std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
99  if ( att ) m_attributes[name] = att;
100  }
101 
102  /// @brief Remove attribute
103  void remove_attribute(const string &name) {
104  std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
105  m_attributes.erase(name);
106  }
107 
108  /// @brief Get attribute of type T
109  template<class T>
110  shared_ptr<T> attribute(const string &name) const;
111 
112  /// @brief Get attribute of any type as string
113  string attribute_as_string(const string &name) const;
114 
115  /// @brief Get list of attribute names
116  std::vector<string> attribute_names() const;
117 
118  /// @brief Get a copy of the list of attributes
119  /// @note To avoid thread issues, this is returns a copy. Better solution may be needed.
120  std::map< std::string, shared_ptr<Attribute> > attributes() const {
121  return m_attributes;
122  }
123 
124 
125 #endif // __CINT__
126 
127  /// @name Methods to fill GenRunInfoData and to read it back
128  //@{
129 
130  /// @brief Fill GenRunInfoData object
131  void write_data(GenRunInfoData &data) const;
132 
133  /// @brief Fill GenRunInfo based on GenRunInfoData
134  void read_data(const GenRunInfoData &data);
135 
136 #ifdef HEPMC3_ROOTIO
137  /// @brief ROOT I/O streamer
138  void Streamer(TBuffer &b);
139  //@}
140 #endif
141 
142 private:
143 
144  /// @name Fields
145  //@{
146 
147 #if !defined(__CINT__)
148 
149  /// @brief The vector of tools used to produce this run.
150  std::vector<ToolInfo> m_tools;
151 
152  /// @brief A map of weight names mapping to indices.
153  std::map<std::string, int> m_weight_indices;
154 
155  /// @brief A vector of weight names.
156  std::vector<std::string> m_weight_names;
157 
158  /// @brief Map of attributes
159  mutable std::map< std::string, shared_ptr<Attribute> > m_attributes;
160 
161  /// @brief Mutex lock for the m_attibutes map.
162  mutable std::recursive_mutex m_lock_attributes;
163  //@}
164 
165 #endif // __CINT__
166 };
167 
168 #if !defined(__CINT__)
169 
170 //
171 // Template methods
172 //
173 
174 template<class T>
175 shared_ptr<T> GenRunInfo::attribute(const string &name) const {
176  std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
177  std::map< std::string, shared_ptr<Attribute> >::iterator i =
178  m_attributes.find(name);
179  if( i == m_attributes.end() ) return shared_ptr<T>();
180 
181  if( !i->second->is_parsed() ) {
182 
183  shared_ptr<T> att = make_shared<T>();
184  if ( att->from_string(i->second->unparsed_string()) &&
185  att->init(*this) ) {
186  // update map with new pointer
187  i->second = att;
188 
189  return att;
190  }
191  else
192  return shared_ptr<T>();
193  }
194  else return dynamic_pointer_cast<T>(i->second);
195 }
196 
197 #endif // __CINT__
198 
199 } // namespace HepMC3
200 
201 #endif
HepMC3::GenRunInfo::tools
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Definition: GenRunInfo.h:62
HepMC3::GenRunInfo::add_attribute
void add_attribute(const string &name, const shared_ptr< Attribute > &att)
add an attribute This will overwrite existing attribute if an attribute with the same name is present
Definition: GenRunInfo.h:96
HepMC3::GenRunInfo::ToolInfo::version
string version
The version of the tool.
Definition: GenRunInfo.h:43
HepMC3::GenRunInfo::attribute
shared_ptr< T > attribute(const string &name) const
Get attribute of type T.
Definition: GenRunInfo.h:175
HepMC3::GenRunInfo::ToolInfo::name
string name
The name of the tool.
Definition: GenRunInfo.h:40
HepMC3
HepMC3 main namespace.
Definition: ReaderGZ.h:28
HepMC3::GenRunInfo::ToolInfo::description
string description
Other information about how the tool was used in the run.
Definition: GenRunInfo.h:47
HepMC3::GenRunInfo::remove_attribute
void remove_attribute(const string &name)
Remove attribute.
Definition: GenRunInfo.h:103
HepMC3::GenRunInfo::tools
std::vector< ToolInfo > & tools()
The vector of tools used to produce this run.
Definition: GenRunInfo.h:66
HepMC3::GenRunInfo::m_tools
std::vector< ToolInfo > m_tools
The vector of tools used to produce this run.
Definition: GenRunInfo.h:150
HepMC3::GenRunInfoData
Stores serializable run information.
Definition: GenRunInfoData.h:23
HepMC3::GenRunInfo::m_weight_indices
std::map< std::string, int > m_weight_indices
A map of weight names mapping to indices.
Definition: GenRunInfo.h:153
HepMC3::GenRunInfo::weight_names
const std::vector< std::string > & weight_names() const
Get the vector of weight names.
Definition: GenRunInfo.h:83
HepMC3::GenRunInfo::attributes
std::map< std::string, shared_ptr< Attribute > > attributes() const
Get a copy of the list of attributes.
Definition: GenRunInfo.h:120
Units.h
Definition of class Units.
HepMC3::GenRunInfo::has_weight
bool has_weight(const string &name) const
Check if a weight name is present.
Definition: GenRunInfo.h:71
HepMC3::GenRunInfo::ToolInfo
Interrnal struct for keeping track of tools.
Definition: GenRunInfo.h:37
HepMC3::GenRunInfo::m_lock_attributes
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
Definition: GenRunInfo.h:162
HepMC3::GenRunInfo::weight_index
int weight_index(const string &name) const
Return the index corresponding to a weight name.
Definition: GenRunInfo.h:77
HepMC3::GenRunInfo::m_weight_names
std::vector< std::string > m_weight_names
A vector of weight names.
Definition: GenRunInfo.h:156
HepMC3::GenRunInfo::GenRunInfo
GenRunInfo()
Default constructor.
Definition: GenRunInfo.h:53
Attribute.h
Definition of class Attribute, class IntAttribute and class StringAttribute.
HepMC3::GenRunInfo::m_attributes
std::map< std::string, shared_ptr< Attribute > > m_attributes
Map of attributes.
Definition: GenRunInfo.h:159
HepMC3::GenRunInfo
Stores run-related information.
Definition: GenRunInfo.h:32