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