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