HepMC3 event record library: GenRunInfo.h Source File

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-2023 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
33 class GenRunInfo {
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
54 GenRunInfo() {}
55 /// @brief Copy constructor
56 GenRunInfo(const GenRunInfo& r);
57 /// @brief Assignmet
58 GenRunInfo& operator=(const GenRunInfo& r);
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
Attribute.h
Definition of class Attribute, class IntAttribute and class StringAttribute.
Units.h
Definition of class Units.
HepMC3::GenRunInfo
Stores run-related information.
Definition GenRunInfo.h:33
HepMC3::GenRunInfo::m_lock_attributes
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
Definition GenRunInfo.h:168
HepMC3::GenRunInfo::operator=
GenRunInfo & operator=(const GenRunInfo &r)
Assignmet.
Definition GenRunInfo.cc:119
HepMC3::GenRunInfo::attribute
std::shared_ptr< T > attribute(const std::string &name) const
Get attribute of type T.
Definition GenRunInfo.h:181
HepMC3::GenRunInfo::m_weight_indices
std::map< std::string, int > m_weight_indices
A map of weight names mapping to indices.
Definition GenRunInfo.h:159
HepMC3::GenRunInfo::m_tools
std::vector< ToolInfo > m_tools
The vector of tools used to produce this run.
Definition GenRunInfo.h:156
HepMC3::GenRunInfo::weight_indices
std::map< std::string, int > weight_indices() const
Returns a copy of indices map.
Definition GenRunInfo.h:77
HepMC3::GenRunInfo::attributes
std::map< std::string, std::shared_ptr< Attribute > > attributes() const
Get a copy of the list of attributes.
Definition GenRunInfo.h:126
HepMC3::GenRunInfo::remove_attribute
void remove_attribute(const std::string &name)
Remove attribute.
Definition GenRunInfo.h:109
HepMC3::GenRunInfo::m_attributes
std::map< std::string, std::shared_ptr< Attribute > > m_attributes
Map of attributes.
Definition GenRunInfo.h:165
HepMC3::GenRunInfo::tools
std::vector< ToolInfo > & tools()
The vector of tools used to produce this run.
Definition GenRunInfo.h:67
HepMC3::GenRunInfo::attribute_names
std::vector< std::string > attribute_names() const
Get list of attribute names.
Definition GenRunInfo.cc:77
HepMC3::GenRunInfo::add_attribute
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
HepMC3::GenRunInfo::has_weight
bool has_weight(const std::string &name) const
Check if a weight name is present.
Definition GenRunInfo.h:72
HepMC3::GenRunInfo::m_weight_names
std::vector< std::string > m_weight_names
A vector of weight names.
Definition GenRunInfo.h:162
HepMC3::GenRunInfo::read_data
void read_data(const GenRunInfoData &data)
Fill GenRunInfo based on GenRunInfoData.
Definition GenRunInfo.cc:86
HepMC3::GenRunInfo::GenRunInfo
GenRunInfo()
Default constructor.
Definition GenRunInfo.h:54
HepMC3::GenRunInfo::weight_names
const std::vector< std::string > & weight_names() const
Get the vector of weight names.
Definition GenRunInfo.h:89
HepMC3::GenRunInfo::write_data
void write_data(GenRunInfoData &data) const
Fill GenRunInfoData object.
Definition GenRunInfo.cc:53
HepMC3::GenRunInfo::weight_index
int weight_index(const std::string &name) const
Return the index corresponding to a weight name.
Definition GenRunInfo.h:83
HepMC3::GenRunInfo::tools
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Definition GenRunInfo.h:63
HepMC3::GenRunInfo::set_weight_names
void set_weight_names(const std::vector< std::string > &names)
Set the names of the weights in this run.
Definition GenRunInfo.cc:20
HepMC3::GenRunInfo::attribute_as_string
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition GenRunInfo.cc:40
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::GenRunInfoData
Stores serializable run information.
Definition GenRunInfoData.h:23
HepMC3::GenRunInfo::ToolInfo
Interrnal struct for keeping track of tools.
Definition GenRunInfo.h:38
HepMC3::GenRunInfo::ToolInfo::description
std::string description
Other information about how the tool was used in the run.
Definition GenRunInfo.h:48
HepMC3::GenRunInfo::ToolInfo::version
std::string version
The version of the tool.
Definition GenRunInfo.h:44
HepMC3::GenRunInfo::ToolInfo::name
std::string name
The name of the tool.
Definition GenRunInfo.h:41