1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2020 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/GenRunInfo.h"
14 #include "HepMC3/Data/GenRunInfoData.h"
15 
16 namespace HepMC3 {
17 
18 
set_weight_names(const std::vector<std::string> & names)19 void GenRunInfo::set_weight_names(const std::vector<std::string> & names) {
20     m_weight_indices.clear();
21     m_weight_names = names;
22     for ( int i = 0, N = names.size(); i < N; ++i ) {
23         std::string name = names[i];
24         if ( name.empty() ) {
25             std::ostringstream oss;
26             oss << i;
27             name = oss.str();
28             m_weight_names[i] = name;
29         }
30         if ( has_weight(name) )
31             throw std::logic_error("GenRunInfo::set_weight_names: "
32                                    "Duplicate weight name '" + name +
33                                    "' found.");
34         m_weight_indices[name] = i;
35     }
36 }
37 
attribute_as_string(const std::string & name) const38 std::string GenRunInfo::attribute_as_string(const std::string &name) const {
39     std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
40     std::map< std::string, std::shared_ptr<Attribute> >::iterator i = m_attributes.find(name);
41     if ( i == m_attributes.end() ) return std::string();
42 
43     if ( !i->second ) return std::string();
44 
45     std::string ret;
46     i->second->to_string(ret);
47 
48     return ret;
49 }
50 
write_data(GenRunInfoData & data) const51 void GenRunInfo::write_data(GenRunInfoData& data) const {
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 
attribute_names() const75 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 
read_data(const GenRunInfoData & data)83 void GenRunInfo::read_data(const GenRunInfoData& data) {
84     // Weight names
85     set_weight_names(data.weight_names);
86 
87     // Attributes
88     for (unsigned int i = 0; i < data.attribute_name.size(); ++i) {
89         add_attribute(data.attribute_name[i],
90                       std::make_shared<StringAttribute>(data.attribute_string[i]));
91     }
92 
93     // Tools
94     for (unsigned int i = 0; i < data.tool_name.size(); ++i) {
95         ToolInfo ti;
96         ti.name        = data.tool_name[i];
97         ti.version     = data.tool_version[i];
98         ti.description = data.tool_description[i];
99 
100         this->tools().push_back(ti);
101     }
102 }
103 
GenRunInfo(const GenRunInfo & r)104 GenRunInfo::GenRunInfo(const GenRunInfo& r)
105 {
106     if (this != &r)
107     {
108         std::lock(m_lock_attributes, r.m_lock_attributes);
109         std::lock_guard<std::recursive_mutex> lhs_lk(m_lock_attributes, std::adopt_lock);
110         std::lock_guard<std::recursive_mutex> rhs_lk(r.m_lock_attributes, std::adopt_lock);
111         GenRunInfoData tdata;
112         r.write_data(tdata);
113         read_data(tdata);
114     }
115 }
operator =(const GenRunInfo & r)116 GenRunInfo& GenRunInfo::operator=(const GenRunInfo& r)
117 {
118     if (this != &r)
119     {
120         std::lock(m_lock_attributes, r.m_lock_attributes);
121         std::lock_guard<std::recursive_mutex> lhs_lk(m_lock_attributes, std::adopt_lock);
122         std::lock_guard<std::recursive_mutex> rhs_lk(r.m_lock_attributes, std::adopt_lock);
123         GenRunInfoData tdata;
124         r.write_data(tdata);
125         read_data(tdata);
126     }
127     return *this;
128 }
129 
130 } // namespace HepMC3
131