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
20class TBuffer;
21#endif
22
23namespace HepMC3 {
24using namespace std;
25
26struct GenRunInfoData;
27
28/// @brief Stores run-related information
29///
30/// Manages run-related information.
31/// Contains run-wide attributes
33
34public:
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.
48 };
49
50public:
51
52 /// @brief Default constructor
54 /// @brief Copy constructor
55 GenRunInfo(const GenRunInfo& r);
56 /// @brief Assignmet
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
142private:
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
174template<class T>
175shared_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
Definition of class Attribute, class IntAttribute and class StringAttribute.
Definition of class Units.
Stores run-related information.
Definition GenRunInfo.h:32
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
Definition GenRunInfo.h:162
GenRunInfo & operator=(const GenRunInfo &r)
Assignmet.
std::map< std::string, shared_ptr< Attribute > > m_attributes
Map of attributes.
Definition GenRunInfo.h:159
std::map< std::string, int > m_weight_indices
A map of weight names mapping to indices.
Definition GenRunInfo.h:153
std::vector< ToolInfo > m_tools
The vector of tools used to produce this run.
Definition GenRunInfo.h:150
shared_ptr< T > attribute(const string &name) const
Get attribute of type T.
Definition GenRunInfo.h:175
void remove_attribute(const string &name)
Remove attribute.
Definition GenRunInfo.h:103
string attribute_as_string(const string &name) const
Get attribute of any type as string.
Definition GenRunInfo.cc:37
std::vector< ToolInfo > & tools()
The vector of tools used to produce this run.
Definition GenRunInfo.h:66
std::vector< string > attribute_names() const
Get list of attribute names.
Definition GenRunInfo.cc:75
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
std::vector< std::string > m_weight_names
A vector of weight names.
Definition GenRunInfo.h:156
void read_data(const GenRunInfoData &data)
Fill GenRunInfo based on GenRunInfoData.
Definition GenRunInfo.cc:83
GenRunInfo()
Default constructor.
Definition GenRunInfo.h:53
const std::vector< std::string > & weight_names() const
Get the vector of weight names.
Definition GenRunInfo.h:83
void write_data(GenRunInfoData &data) const
Fill GenRunInfoData object.
Definition GenRunInfo.cc:50
bool has_weight(const string &name) const
Check if a weight name is present.
Definition GenRunInfo.h:71
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Definition GenRunInfo.h:62
void set_weight_names(const std::vector< std::string > &names)
Set the names of the weights in this run.
Definition GenRunInfo.cc:18
std::map< std::string, shared_ptr< Attribute > > attributes() const
Get a copy of the list of attributes.
Definition GenRunInfo.h:120
int weight_index(const string &name) const
Return the index corresponding to a weight name.
Definition GenRunInfo.h:77
HepMC3 main namespace.
Definition ReaderGZ.h:28
Stores serializable run information.
Interrnal struct for keeping track of tools.
Definition GenRunInfo.h:37
string description
Other information about how the tool was used in the run.
Definition GenRunInfo.h:47
string name
The name of the tool.
Definition GenRunInfo.h:40
string version
The version of the tool.
Definition GenRunInfo.h:43