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