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-2021 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"
15 
16 namespace HepMC3 {
17 
18 
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 
38 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 
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 
84  // Weight names
86 
87  // Attributes
88  for (unsigned int i = 0; i < data.attribute_name.size(); ++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 
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 }
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
Definition of struct GenRunInfoData.
Definition of class GenRunInfo.
Stores run-related information.
Definition: GenRunInfo.h:33
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
Definition: GenRunInfo.h:168
GenRunInfo & operator=(const GenRunInfo &r)
Assignmet.
Definition: GenRunInfo.cc:116
std::map< std::string, int > m_weight_indices
A map of weight names mapping to indices.
Definition: GenRunInfo.h:159
const std::vector< std::string > & weight_names() const
Get the vector of weight names.
Definition: GenRunInfo.h:89
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:75
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:83
GenRunInfo()
Default constructor.
Definition: GenRunInfo.h:54
void write_data(GenRunInfoData &data) const
Fill GenRunInfoData object.
Definition: GenRunInfo.cc:51
void set_weight_names(const std::vector< std::string > &names)
Set the names of the weights in this run.
Definition: GenRunInfo.cc:19
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition: GenRunInfo.cc:38
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Definition: GenRunInfo.h:63
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