HepMC3 event record library
GenRunInfo.cc
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.cc
8 * @brief Implementation of \b class GenRunInfo
9 *
10 */
11#include <sstream>
12
14#include "HepMC3/GenRunInfo.h"
15
16
17namespace HepMC3 {
18
19
20void GenRunInfo::set_weight_names(const std::vector<std::string> & names) {
21 m_weight_indices.clear();
22 m_weight_names = names;
23 for ( int i = 0, N = names.size(); i < N; ++i ) {
24 std::string name = names[i];
25 if ( name.empty() ) {
26 std::ostringstream oss;
27 oss << i;
28 name = oss.str();
29 m_weight_names[i] = name;
30 }
31 if ( has_weight(name) ) {
32 throw std::logic_error("GenRunInfo::set_weight_names: "
33 "Duplicate weight name '" + name +
34 "' found.");
35 }
36 m_weight_indices[name] = i;
37 }
38}
39
40std::string GenRunInfo::attribute_as_string(const std::string &name) const {
41 std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
42 auto i = m_attributes.find(name);
43 if ( i == m_attributes.end() ) return {};
44
45 if ( !i->second ) return {};
46
47 std::string ret;
48 i->second->to_string(ret);
49
50 return ret;
51}
52
54 // Weight names
55 data.weight_names = this->weight_names();
56
57 // Attributes
58 using att_val_t = std::map<std::string, std::shared_ptr<Attribute>>::value_type;
59
60 for (const att_val_t& vt: m_attributes) {
61 std::string att;
62 vt.second->to_string(att);
63
64 data.attribute_name. emplace_back(vt.first);
65 data.attribute_string.emplace_back(att);
66 }
67
68 // Tools
69 for ( const ToolInfo &tool: this->tools() ) {
70 data.tool_name. emplace_back(tool.name);
71 data.tool_version. emplace_back(tool.version);
72 data.tool_description.emplace_back(tool.description);
73 }
74}
75
76
77std::vector<std::string> GenRunInfo::attribute_names() const {
78 std::vector<std::string> results;
79 results.reserve(m_attributes.size());
80 for (const auto& vt1: m_attributes) {
81 results.emplace_back(vt1.first);
82 }
83 return results;
84}
85
87 // Weight names
89
90 // Attributes
91 for (unsigned int i = 0; i < data.attribute_name.size(); ++i) {
93 std::make_shared<StringAttribute>(data.attribute_string[i]));
94 }
95
96 // Tools
97 for (unsigned int i = 0; i < data.tool_name.size(); ++i) {
98 ToolInfo ti;
99 ti.name = data.tool_name[i];
100 ti.version = data.tool_version[i];
101 ti.description = data.tool_description[i];
102
103 this->tools().emplace_back(ti);
104 }
105}
106
108{
109 if (this != &r)
110 {
112 std::lock_guard<std::recursive_mutex> lhs_lk(m_lock_attributes, std::adopt_lock);
113 std::lock_guard<std::recursive_mutex> rhs_lk(r.m_lock_attributes, std::adopt_lock);
114 GenRunInfoData tdata;
115 r.write_data(tdata);
116 read_data(tdata);
117 }
118}
120{
121 if (this != &r)
122 {
124 std::lock_guard<std::recursive_mutex> lhs_lk(m_lock_attributes, std::adopt_lock);
125 std::lock_guard<std::recursive_mutex> rhs_lk(r.m_lock_attributes, std::adopt_lock);
126 GenRunInfoData tdata;
127 r.write_data(tdata);
128 read_data(tdata);
129 }
130 return *this;
131}
132
133} // namespace HepMC3
Definition of struct GenRunInfoData.
Definition of class GenRunInfo.
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
Definition GenRunInfo.h:168
GenRunInfo & operator=(const GenRunInfo &r)
Assignmet.
std::map< std::string, int > m_weight_indices
A map of weight names mapping to indices.
Definition GenRunInfo.h:159
std::map< std::string, std::shared_ptr< Attribute > > m_attributes
Map of attributes.
Definition GenRunInfo.h:165
std::vector< std::string > attribute_names() const
Get list of attribute names.
Definition GenRunInfo.cc:77
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:86
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:53
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:20
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition GenRunInfo.cc:40
HepMC3 main namespace.
Stores serializable run information.
std::vector< std::string > tool_name
Tool names.
std::vector< std::string > tool_version
Tool versions.
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< std::string > attribute_name
Attribute name.
std::vector< std::string > tool_description
Tool descriptions.
std::vector< std::string > weight_names
Weight names.
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