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-2019 The HepMC collaboration (see AUTHORS for details)
5//
6/**
7 * @file GenRunInfo.cc
8 * @brief Implementation of \b class GenRunInfo
9 *
10 */
11#include "HepMC3/GenRunInfo.h"
13#include <sstream>
14
15namespace HepMC3 {
16
17
18void GenRunInfo::set_weight_names(const std::vector<std::string> & names) {
19 m_weight_indices.clear();
20 m_weight_names = names;
21 for ( int i = 0, N = names.size(); i < N; ++i ) {
22 string name = names[i];
23 if ( name.empty() ) {
24 std::ostringstream oss;
25 oss << i;
26 name = oss.str();
27 m_weight_names[i] = name;
28 }
29 if ( has_weight(name) )
30 throw std::logic_error("GenRunInfo::set_weight_names: "
31 "Duplicate weight name '" + name +
32 "' found.");
33 m_weight_indices[name] = i;
34 }
35}
36
37string GenRunInfo::attribute_as_string(const string &name) const {
38 std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
39 std::map< std::string, shared_ptr<Attribute> >::iterator i = m_attributes.find(name);
40 if( i == m_attributes.end() ) return string();
41
42 if( !i->second ) return string();
43
44 string ret;
45 i->second->to_string(ret);
46
47 return ret;
48}
49
51
52 // Weight names
53 data.weight_names = this->weight_names();
54
55 // Attributes
56 typedef std::map<std::string, shared_ptr<Attribute> >::value_type att_val_t;
57
58 for(const att_val_t& vt: m_attributes ) {
59 std::string att;
60 vt.second->to_string(att);
61
62 data.attribute_name. push_back(vt.first);
63 data.attribute_string.push_back(att);
64 }
65
66 // Tools
67 for( const ToolInfo &tool: this->tools() ) {
68 data.tool_name. push_back(tool.name);
69 data.tool_version. push_back(tool.version);
70 data.tool_description.push_back(tool.description);
71 }
72}
73
74
75std::vector<std::string> GenRunInfo::attribute_names( ) const {
76 std::vector<std::string> results;
77 for(auto vt1: m_attributes ) {
78 results.push_back( vt1.first );
79 }
80 return results;
81}
82
84
85 //this->clear();
86
87 // Weight names
89
90 // Attributes
91 for(unsigned int i=0; i<data.attribute_name.size(); ++i) {
93 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().push_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.
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.
Definition: GenRunInfo.cc:119
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
string attribute_as_string(const string &name) const
Get attribute of any type as string.
Definition: GenRunInfo.cc:37
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
HepMC3 main namespace.
Definition: ReaderGZ.h:28
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: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