HepMC event record
build/outputs/include/HepMC/GenRunInfo.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014 The HepMC collaboration (see AUTHORS for details)
5 //
6 ///
7 /// @file GenRunInfo.h
8 /// @brief Definition of \b class GenRunInfo
9 ///
10 #ifndef HEPMC_GENRUNINFO_H
11 #define HEPMC_GENRUNINFO_H
12 
13 #if !defined(__CINT__)
14 #include "HepMC/Common.h"
15 #include "HepMC/Data/SmartPointer.h"
16 #include "HepMC/Units.h"
17 #include "HepMC/Attribute.h"
18 #endif // __CINT__
19 
20 #ifdef HEPMC_ROOTIO
21 class TBuffer;
22 #endif
23 
24 namespace HepMC {
25 
26 
27 struct GenRunInfoData;
28 
29 /// @brief Stores run-related information
30 ///
31 /// Manages run-related information.
32 /// Contains run-wide attributes
33 class GenRunInfo {
34 
35 public:
36 
37  /// @brief Interrnal struct for keeping track of tools.
38  struct ToolInfo {
39 
40  /// @brief The name of the tool.
41  string name;
42 
43  /// @brief The version of the tool.
44  string version;
45 
46  /// @brief Other information about how the tool was used in
47  /// the run.
48  string description;
49  };
50 
51 public:
52 
53  /// @brief Default constructor
55 
56  #if !defined(__CINT__)
57 
58  /// @brief The vector of tools used to produce this run.
59  const std::vector<ToolInfo> & tools() const {
60  return m_tools;
61  }
62  /// @brief The vector of tools used to produce this run.
63  std::vector<ToolInfo> & tools() {
64  return m_tools;
65  }
66 
67  /// @brief Check if a weight name is present.
68  bool has_weight(string name) const {
69  return m_weight_indices.find(name) != m_weight_indices.end();
70  }
71 
72  /// @brief Return the index corresponding to a weight name.
73  /// @return -1 if name was not found
74  /// @todo Throw exception instead? Or return ssize_t for better signed/unsigned safety?
75  int weight_index(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  if ( att ) m_attributes[name] = att;
97  }
98 
99  /// @brief Remove attribute
100  void remove_attribute(const string &name) {
101  m_attributes.erase(name);
102  }
103 
104  /// @brief Get attribute of type T
105  template<class T>
106  shared_ptr<T> attribute(const string &name) const;
107 
108  /// @brief Get attribute of any type as string
109  string attribute_as_string(const string &name) const;
110 
111  /// @brief Get list of attributes
112  const std::map< std::string, shared_ptr<Attribute> > & attributes() const {
113  return m_attributes;
114  }
115 
116  #endif // __CINT__
117 
118  /// @name Methods to fill GenRunInfoData and to read it back
119  //@{
120 
121  /// @brief Fill GenRunInfoData object
122  void write_data(GenRunInfoData &data) const;
123 
124  /// @brief Fill GenRunInfo based on GenRunInfoData
125  void read_data(const GenRunInfoData &data);
126 
127  #ifdef HEPMC_ROOTIO
128  /// @brief ROOT I/O streamer
129  void Streamer(TBuffer &b);
130  //@}
131  #endif
132 
133 private:
134 
135  /// @name Fields
136  //@{
137 
138  #if !defined(__CINT__)
139 
140  /// @brief The vector of tools used to produce this run.
141  std::vector<ToolInfo> m_tools;
142 
143  /// @brief A map of weight names mapping to indices.
144  std::map<std::string, int> m_weight_indices;
145 
146  /// @brief A vector of weight names.
147  std::vector<std::string> m_weight_names;
148 
149  /// @brief Map of attributes
150  mutable std::map< std::string, shared_ptr<Attribute> > m_attributes;
151  //@}
152 
153  #endif // __CINT__
154 };
155 
156 #if !defined(__CINT__)
157 
158 //
159 // Template methods
160 //
161 
162 template<class T>
163 shared_ptr<T> GenRunInfo::attribute(const string &name) const {
164 
165  std::map< std::string, shared_ptr<Attribute> >::iterator i =
166  m_attributes.find(name);
167  if( i == m_attributes.end() ) return shared_ptr<T>();
168 
169  if( !i->second->is_parsed() ) {
170 
171  shared_ptr<T> att = make_shared<T>();
172  if ( att->from_string(i->second->unparsed_string()) &&
173  att->init(*this) ) {
174  // update map with new pointer
175  i->second = att;
176 
177  return att;
178  }
179  else
180  return shared_ptr<T>();
181  }
182  else return dynamic_pointer_cast<T>(i->second);
183 }
184 
185 #endif // __CINT__
186 
187 } // namespace HepMC
188 
189 #endif
string description
Other information about how the tool was used in the run.
shared_ptr< T > attribute(const string &name) const
Get attribute of type T.
int weight_index(string name) const
Return the index corresponding to a weight name.
std::vector< ToolInfo > m_tools
The vector of tools used to produce this run.
void remove_attribute(const string &name)
Remove attribute.
void read_data(const GenRunInfoData &data)
Fill GenRunInfo based on GenRunInfoData.
Definition: GenRunInfo.cc:74
std::vector< ToolInfo > & tools()
The vector of tools used to produce this run.
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Stores serializable run information.
Stores run-related information.
const std::map< std::string, shared_ptr< Attribute > > & attributes() const
Get list of attributes.
std::map< std::string, shared_ptr< Attribute > > m_attributes
Map of attributes.
std::vector< std::string > m_weight_names
A vector of weight names.
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...
const std::vector< std::string > & weight_names() const
Get the vector of weight names.
bool has_weight(string name) const
Check if a weight name is present.
void write_data(GenRunInfoData &data) const
Fill GenRunInfoData object.
Definition: GenRunInfo.cc:50
std::map< std::string, int > m_weight_indices
A map of weight names mapping to indices.
Definition of template class SmartPointer.
string attribute_as_string(const string &name) const
Get attribute of any type as string.
Definition: GenRunInfo.cc:37
Interrnal struct for keeping track of tools.
void set_weight_names(const std::vector< std::string > &names)
Set the names of the weights in this run.
Definition: GenRunInfo.cc:18