MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
HepMC3::GenRunInfo Class Reference

Stores run-related information. More...

#include <GenRunInfo.h>

Classes

struct  ToolInfo
 Interrnal struct for keeping track of tools. More...
 

Public Member Functions

 GenRunInfo ()
 Default constructor.
 
 GenRunInfo (const GenRunInfo &r)
 Copy constructor.
 
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
 
template<class T>
std::shared_ptr< T > attribute (const std::string &name) const
 Get attribute of type T.
 
std::string attribute_as_string (const std::string &name) const
 Get attribute of any type as string.
 
std::vector< std::string > attribute_names () const
 Get list of attribute names.
 
std::map< std::string, std::shared_ptr< Attribute > > attributes () const
 Get a copy of the list of attributes.
 
bool has_weight (const std::string &name) const
 Check if a weight name is present.
 
GenRunInfooperator= (const GenRunInfo &r)
 Assignmet.
 
void remove_attribute (const std::string &name)
 Remove attribute.
 
void set_weight_names (const std::vector< std::string > &names)
 Set the names of the weights in this run.
 
std::vector< ToolInfo > & tools ()
 The vector of tools used to produce this run.
 
const std::vector< ToolInfo > & tools () const
 The vector of tools used to produce this run.
 
int weight_index (const std::string &name) const
 Return the index corresponding to a weight name.
 
std::map< std::string, int > weight_indices () const
 Returns a copy of indices map.
 
const std::vector< std::string > & weight_names () const
 Get the vector of weight names.
 
Methods to fill GenRunInfoData and to read it back
void write_data (GenRunInfoData &data) const
 Fill GenRunInfoData object.
 
void read_data (const GenRunInfoData &data)
 Fill GenRunInfo based on GenRunInfoData.
 

Detailed Description

Stores run-related information.

Manages run-related information. Contains run-wide attributes

Definition at line 33 of file GenRunInfo.h.


Class Documentation

◆ HepMC3::GenRunInfo::ToolInfo

struct HepMC3::GenRunInfo::ToolInfo

Interrnal struct for keeping track of tools.

Definition at line 38 of file GenRunInfo.h.

Class Members
string description Other information about how the tool was used in the run.
string name The name of the tool.
string version The version of the tool.

Constructor & Destructor Documentation

◆ GenRunInfo() [1/2]

HepMC3::GenRunInfo::GenRunInfo ( )
inline

Default constructor.

Definition at line 54 of file GenRunInfo.h.

54{}

Referenced by GenRunInfo(), and operator=().

◆ GenRunInfo() [2/2]

HepMC3::GenRunInfo::GenRunInfo ( const GenRunInfo & r)

Copy constructor.

Definition at line 180 of file marley_hepmc3.cc.

181{
182 if (this != &r)
183 {
184 std::lock(m_lock_attributes, r.m_lock_attributes);
185 std::lock_guard<std::recursive_mutex> lhs_lk(m_lock_attributes, std::adopt_lock);
186 std::lock_guard<std::recursive_mutex> rhs_lk(r.m_lock_attributes, std::adopt_lock);
187 GenRunInfoData tdata;
188 r.write_data(tdata);
189 read_data(tdata);
190 }
191}
void read_data(const GenRunInfoData &data)
Fill GenRunInfo based on GenRunInfoData.

References GenRunInfo(), read_data(), and write_data().

Member Function Documentation

◆ add_attribute()

void HepMC3::GenRunInfo::add_attribute ( const std::string & name,
const std::shared_ptr< Attribute > & att )
inline

add an attribute This will overwrite existing attribute if an attribute with the same name is present

Definition at line 102 of file GenRunInfo.h.

103 {
104 std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
105 if ( att ) m_attributes[name] = att;
106 }

Referenced by read_data().

◆ attribute()

template<class T>
std::shared_ptr< T > HepMC3::GenRunInfo::attribute ( const std::string & name) const

Get attribute of type T.

Definition at line 181 of file GenRunInfo.h.

181 {
182 std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
183 std::map< std::string, std::shared_ptr<Attribute> >::iterator i =
184 m_attributes.find(name);
185 if ( i == m_attributes.end() ) return std::shared_ptr<T>();
186
187 if ( !i->second->is_parsed() ) {
188
189 std::shared_ptr<T> att = std::make_shared<T>();
190 if ( att->from_string(i->second->unparsed_string()) &&
191 att->init(*this) ) {
192 // update map with new pointer
193 i->second = att;
194
195 return att;
196 }
197 else
198 return std::shared_ptr<T>();
199 }
200 else return std::dynamic_pointer_cast<T>(i->second);
201}

Referenced by marley::EventFileReader::get_flux_averaged_xsec(), and marley::OutputFile::merge_reweight_provenance_weights().

◆ attribute_as_string()

std::string HepMC3::GenRunInfo::attribute_as_string ( const std::string & name) const

Get attribute of any type as string.

Definition at line 113 of file marley_hepmc3.cc.

113 {
114 std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
115 auto i = m_attributes.find(name);
116 if ( i == m_attributes.end() ) return {};
117
118 if ( !i->second ) return {};
119
120 std::string ret;
121 i->second->to_string(ret);
122
123 return ret;
124}

Referenced by HepMC3::Print::line().

◆ attribute_names()

std::vector< std::string > HepMC3::GenRunInfo::attribute_names ( ) const

Get list of attribute names.

Definition at line 150 of file marley_hepmc3.cc.

150 {
151 std::vector<std::string> results;
152 results.reserve(m_attributes.size());
153 for (const auto& vt1: m_attributes) {
154 results.emplace_back(vt1.first);
155 }
156 return results;
157}

Referenced by HepMC3::Print::line().

◆ attributes()

std::map< std::string, std::shared_ptr< Attribute > > HepMC3::GenRunInfo::attributes ( ) const
inline

Get a copy of the list of attributes.

Note
To avoid thread issues, this is returns a copy. Better solution may be needed.

Definition at line 126 of file GenRunInfo.h.

126 {
127 return m_attributes;
128 }

Referenced by HepMC3::Print::listing().

◆ has_weight()

bool HepMC3::GenRunInfo::has_weight ( const std::string & name) const
inline

Check if a weight name is present.

Definition at line 72 of file GenRunInfo.h.

72 {
73 return m_weight_indices.find(name) != m_weight_indices.end();
74 }

Referenced by set_weight_names().

◆ operator=()

GenRunInfo & HepMC3::GenRunInfo::operator= ( const GenRunInfo & r)

Assignmet.

Definition at line 192 of file marley_hepmc3.cc.

193{
194 if (this != &r)
195 {
196 std::lock(m_lock_attributes, r.m_lock_attributes);
197 std::lock_guard<std::recursive_mutex> lhs_lk(m_lock_attributes, std::adopt_lock);
198 std::lock_guard<std::recursive_mutex> rhs_lk(r.m_lock_attributes, std::adopt_lock);
199 GenRunInfoData tdata;
200 r.write_data(tdata);
201 read_data(tdata);
202 }
203 return *this;
204}

References GenRunInfo(), read_data(), and write_data().

◆ read_data()

void HepMC3::GenRunInfo::read_data ( const GenRunInfoData & data)

Fill GenRunInfo based on GenRunInfoData.

Definition at line 159 of file marley_hepmc3.cc.

159 {
160 // Weight names
161 set_weight_names(data.weight_names);
162
163 // Attributes
164 for (unsigned int i = 0; i < data.attribute_name.size(); ++i) {
165 add_attribute(data.attribute_name[i],
166 std::make_shared<StringAttribute>(data.attribute_string[i]));
167 }
168
169 // Tools
170 for (unsigned int i = 0; i < data.tool_name.size(); ++i) {
171 ToolInfo ti;
172 ti.name = data.tool_name[i];
173 ti.version = data.tool_version[i];
174 ti.description = data.tool_description[i];
175
176 this->tools().emplace_back(ti);
177 }
178}
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
void set_weight_names(const std::vector< std::string > &names)
Set the names of the weights in this run.
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Definition GenRunInfo.h:63
std::string name
The name of the tool.
Definition GenRunInfo.h:41
Interrnal struct for keeping track of tools.
Definition GenRunInfo.h:38

References add_attribute(), HepMC3::GenRunInfoData::attribute_name, HepMC3::GenRunInfoData::attribute_string, HepMC3::GenRunInfo::ToolInfo::description, HepMC3::GenRunInfo::ToolInfo::name, set_weight_names(), HepMC3::GenRunInfoData::tool_description, HepMC3::GenRunInfoData::tool_name, HepMC3::GenRunInfoData::tool_version, tools(), HepMC3::GenRunInfo::ToolInfo::version, and HepMC3::GenRunInfoData::weight_names.

Referenced by GenRunInfo(), and operator=().

◆ remove_attribute()

void HepMC3::GenRunInfo::remove_attribute ( const std::string & name)
inline

Remove attribute.

Definition at line 109 of file GenRunInfo.h.

109 {
110 std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
111 m_attributes.erase(name);
112 }

◆ set_weight_names()

void HepMC3::GenRunInfo::set_weight_names ( const std::vector< std::string > & names)

Set the names of the weights in this run.

For consistency, the length of the vector should be the same as the number of weights in the events in the run.

Definition at line 93 of file marley_hepmc3.cc.

93 {
94 m_weight_indices.clear();
95 m_weight_names = names;
96 for ( int i = 0, N = names.size(); i < N; ++i ) {
97 std::string name = names[i];
98 if ( name.empty() ) {
99 std::ostringstream oss;
100 oss << i;
101 name = oss.str();
102 m_weight_names[i] = name;
103 }
104 if ( has_weight(name) ) {
105 throw std::logic_error("GenRunInfo::set_weight_names: "
106 "Duplicate weight name '" + name +
107 "' found.");
108 }
109 m_weight_indices[name] = i;
110 }
111}
bool has_weight(const std::string &name) const
Check if a weight name is present.
Definition GenRunInfo.h:72

References has_weight().

Referenced by read_data().

◆ tools() [1/2]

std::vector< ToolInfo > & HepMC3::GenRunInfo::tools ( )
inline

The vector of tools used to produce this run.

Definition at line 67 of file GenRunInfo.h.

67 {
68 return m_tools;
69 }

◆ tools() [2/2]

const std::vector< ToolInfo > & HepMC3::GenRunInfo::tools ( ) const
inline

The vector of tools used to produce this run.

Definition at line 63 of file GenRunInfo.h.

63 {
64 return m_tools;
65 }

Referenced by HepMC3::Print::line(), HepMC3::Print::listing(), read_data(), and write_data().

◆ weight_index()

int HepMC3::GenRunInfo::weight_index ( const std::string & name) const
inline

Return the index corresponding to a weight name.

Returns
-1 if name was not found

Definition at line 83 of file GenRunInfo.h.

83 {
84 std::map<std::string, int>::const_iterator it = m_weight_indices.find(name);
85 return it == m_weight_indices.end()? -1: it->second;
86 }

◆ weight_indices()

std::map< std::string, int > HepMC3::GenRunInfo::weight_indices ( ) const
inline

Returns a copy of indices map.

Definition at line 77 of file GenRunInfo.h.

77 {
78 return m_weight_indices;
79 }

◆ weight_names()

const std::vector< std::string > & HepMC3::GenRunInfo::weight_names ( ) const
inline

Get the vector of weight names.

Definition at line 89 of file GenRunInfo.h.

89 {
90 return m_weight_names;
91 }

Referenced by HepMC3::Print::listing(), and write_data().

◆ write_data()

void HepMC3::GenRunInfo::write_data ( GenRunInfoData & data) const

Fill GenRunInfoData object.

Definition at line 126 of file marley_hepmc3.cc.

126 {
127 // Weight names
128 data.weight_names = this->weight_names();
129
130 // Attributes
131 using att_val_t = std::map<std::string, std::shared_ptr<Attribute>>::value_type;
132
133 for (const att_val_t& vt: m_attributes) {
134 std::string att;
135 vt.second->to_string(att);
136
137 data.attribute_name. emplace_back(vt.first);
138 data.attribute_string.emplace_back(att);
139 }
140
141 // Tools
142 for ( const ToolInfo &tool: this->tools() ) {
143 data.tool_name. emplace_back(tool.name);
144 data.tool_version. emplace_back(tool.version);
145 data.tool_description.emplace_back(tool.description);
146 }
147}
const std::vector< std::string > & weight_names() const
Get the vector of weight names.
Definition GenRunInfo.h:89

References HepMC3::GenRunInfoData::attribute_name, HepMC3::GenRunInfoData::attribute_string, HepMC3::GenRunInfoData::tool_description, HepMC3::GenRunInfoData::tool_name, HepMC3::GenRunInfoData::tool_version, tools(), weight_names(), and HepMC3::GenRunInfoData::weight_names.

Referenced by GenRunInfo(), and operator=().


The documentation for this class was generated from the following files: