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"
|
---|
12 | #include "HepMC3/Data/GenRunInfoData.h"
|
---|
13 | #include <sstream>
|
---|
14 |
|
---|
15 | namespace HepMC3 {
|
---|
16 |
|
---|
17 |
|
---|
18 | void 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 | std::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 |
|
---|
37 | std::string GenRunInfo::attribute_as_string(const std::string &name) const {
|
---|
38 | std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
|
---|
39 | std::map< std::string, std::shared_ptr<Attribute> >::iterator i = m_attributes.find(name);
|
---|
40 | if( i == m_attributes.end() ) return std::string();
|
---|
41 |
|
---|
42 | if( !i->second ) return std::string();
|
---|
43 |
|
---|
44 | std::string ret;
|
---|
45 | i->second->to_string(ret);
|
---|
46 |
|
---|
47 | return ret;
|
---|
48 | }
|
---|
49 |
|
---|
50 | void GenRunInfo::write_data(GenRunInfoData& data) const {
|
---|
51 |
|
---|
52 | // Weight names
|
---|
53 | data.weight_names = this->weight_names();
|
---|
54 |
|
---|
55 | // Attributes
|
---|
56 | typedef std::map<std::string, std::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 |
|
---|
75 | std::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 |
|
---|
83 | void GenRunInfo::read_data(const GenRunInfoData& data) {
|
---|
84 |
|
---|
85 | //this->clear();
|
---|
86 |
|
---|
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().push_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
|
---|