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 
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 
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 
84 
85  //this->clear();
86 
87  // Weight names
89 
90  // Attributes
91  for(unsigned int i=0; i<data.attribute_name.size(); ++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 
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 }
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
std::string version
The version of the tool.
Definition: GenRunInfo.h:44
Definition of class GenRunInfo.
GenRunInfo()
Default constructor.
Definition: GenRunInfo.h:54
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
Definition: GenRunInfo.h:163
std::vector< std::string > m_weight_names
A vector of weight names.
Definition: GenRunInfo.h:157
void set_weight_names(const std::vector< std::string > &names)
Set the names of the weights in this run.
Definition: GenRunInfo.cc:18
std::vector< std::string > attribute_name
Attribute name.
void write_data(GenRunInfoData &data) const
Fill GenRunInfoData object.
Definition: GenRunInfo.cc:50
std::vector< std::string > attribute_names() const
Get list of attribute names.
Definition: GenRunInfo.cc:75
Stores run-related information.
Definition: GenRunInfo.h:33
std::string description
Other information about how the tool was used in the run.
Definition: GenRunInfo.h:48
GenRunInfo & operator=(const GenRunInfo &r)
Assignmet.
Definition: GenRunInfo.cc:119
const std::vector< std::string > & weight_names() const
Get the vector of weight names.
Definition: GenRunInfo.h:84
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Definition: GenRunInfo.h:63
std::map< std::string, int > m_weight_indices
A map of weight names mapping to indices.
Definition: GenRunInfo.h:154
bool has_weight(const std::string &name) const
Check if a weight name is present.
Definition: GenRunInfo.h:72
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:97
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition: GenRunInfo.cc:37
std::map< std::string, std::shared_ptr< Attribute > > m_attributes
Map of attributes.
Definition: GenRunInfo.h:160
void read_data(const GenRunInfoData &data)
Fill GenRunInfo based on GenRunInfoData.
Definition: GenRunInfo.cc:83
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< std::string > tool_name
Tool names.
Stores serializable run information.
Interrnal struct for keeping track of tools.
Definition: GenRunInfo.h:38
std::vector< std::string > tool_version
Tool versions.
std::string name
The name of the tool.
Definition: GenRunInfo.h:41
Annotation for function names.
Definition: attr.h:36
std::vector< std::string > weight_names
Weight names.
std::vector< std::string > tool_description
Tool descriptions.
Definition of struct GenRunInfoData.