HepMC3 event record library: GenRunInfo.cc Source File

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
13#include "HepMC3/Data/GenRunInfoData.h"
14#include "HepMC3/GenRunInfo.h"
15
16
17namespace HepMC3 {
18
19
20 void 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
53 void GenRunInfo::write_data(GenRunInfoData& data) const {
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
86 void GenRunInfo::read_data(const GenRunInfoData& data) {
87 // Weight names
88 set_weight_names(data.weight_names);
89
90 // Attributes
91 for (unsigned int i = 0; i < data.attribute_name.size(); ++i) {
92 add_attribute(data.attribute_name[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
107 GenRunInfo::GenRunInfo(const GenRunInfo& r)
108{
109 if (this != &r)
110 {
111 std::lock(m_lock_attributes, r.m_lock_attributes);
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}
119 GenRunInfo& GenRunInfo::operator=(const GenRunInfo& r)
120{
121 if (this != &r)
122 {
123 std::lock(m_lock_attributes, r.m_lock_attributes);
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
GenRunInfoData.h
Definition of struct GenRunInfoData.
GenRunInfo.h
Definition of class GenRunInfo.
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::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_attributes
std::map< std::string, std::shared_ptr< Attribute > > m_attributes
Map of attributes.
Definition GenRunInfo.h:165
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::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::GenRunInfoData::tool_name
std::vector< std::string > tool_name
Tool names.
Definition GenRunInfoData.h:26
HepMC3::GenRunInfoData::tool_version
std::vector< std::string > tool_version
Tool versions.
Definition GenRunInfoData.h:27
HepMC3::GenRunInfoData::attribute_string
std::vector< std::string > attribute_string
Attribute serialized as string.
Definition GenRunInfoData.h:31
HepMC3::GenRunInfoData::attribute_name
std::vector< std::string > attribute_name
Attribute name.
Definition GenRunInfoData.h:30
HepMC3::GenRunInfoData::tool_description
std::vector< std::string > tool_description
Tool descriptions.
Definition GenRunInfoData.h:28
HepMC3::GenRunInfoData::weight_names
std::vector< std::string > weight_names
Weight names.
Definition GenRunInfoData.h:24
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