Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:12

0001 // -*- C++ -*-
0002 //
0003 // This file is part of HepMC
0004 // Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
0005 //
0006 ///
0007 /// @file GenRunInfo.h
0008 /// @brief Definition of \b class GenRunInfo
0009 ///
0010 #ifndef HEPMC3_GENRUNINFO_H
0011 #define HEPMC3_GENRUNINFO_H
0012 
0013 #if !defined(__CINT__)
0014 #include "HepMC3/Units.h"
0015 #include "HepMC3/Attribute.h"
0016 #include <mutex>
0017 #endif // __CINT__
0018 
0019 #ifdef HEPMC3_ROOTIO
0020 class TBuffer;
0021 #endif
0022 
0023 namespace HepMC3 {
0024 /** Deprecated */
0025 using namespace std;
0026 
0027 struct GenRunInfoData;
0028 
0029 /// @brief Stores run-related information
0030 ///
0031 /// Manages run-related information.
0032 /// Contains run-wide attributes
0033 class GenRunInfo {
0034 
0035 public:
0036 
0037     /// @brief Interrnal struct for keeping track of tools.
0038     struct ToolInfo {
0039 
0040         /// @brief The name of the tool.
0041         std::string name;
0042 
0043         /// @brief The version of the tool.
0044         std::string version;
0045 
0046         /// @brief Other information about how the tool was used in
0047         /// the run.
0048         std::string description;
0049     };
0050 
0051 public:
0052 
0053     /// @brief Default constructor
0054     GenRunInfo() {}
0055     /// @brief Copy constructor
0056     GenRunInfo(const GenRunInfo& r);
0057     /// @brief Assignmet
0058     GenRunInfo& operator=(const GenRunInfo& r);
0059 
0060 #if !defined(__CINT__)
0061 
0062     /// @brief The vector of tools used to produce this run.
0063     const std::vector<ToolInfo> & tools() const {
0064         return m_tools;
0065     }
0066     /// @brief The vector of tools used to produce this run.
0067     std::vector<ToolInfo> & tools() {
0068         return m_tools;
0069     }
0070 
0071     /// @brief Check if a weight name is present.
0072     bool has_weight(const std::string& name) const {
0073         return m_weight_indices.find(name) !=  m_weight_indices.end();
0074     }
0075 
0076     /// @brief Returns a copy of indices map.
0077     std::map<std::string, int> weight_indices() const {
0078         return m_weight_indices;
0079     }
0080 
0081     /// @brief Return the index corresponding to a weight name.
0082     /// @return -1 if name was not found
0083     int weight_index(const std::string& name) const {
0084         std::map<std::string, int>::const_iterator it = m_weight_indices.find(name);
0085         return it == m_weight_indices.end()? -1: it->second;
0086     }
0087 
0088     /// @brief Get the vector of weight names.
0089     const std::vector<std::string> & weight_names() const {
0090         return m_weight_names;
0091     }
0092 
0093     /// @brief Set the names of the weights in this run.
0094     ///
0095     /// For consistency, the length of the vector should be the same as
0096     /// the number of weights in the events in the run.
0097     void set_weight_names(const std::vector<std::string> & names);
0098 
0099     /// @brief add an attribute
0100     /// This will overwrite existing attribute if an attribute
0101     /// with the same name is present
0102     void add_attribute(const std::string &name,
0103                        const std::shared_ptr<Attribute> &att) {
0104         std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
0105         if ( att ) m_attributes[name] = att;
0106     }
0107 
0108     /// @brief Remove attribute
0109     void remove_attribute(const std::string &name) {
0110         std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
0111         m_attributes.erase(name);
0112     }
0113 
0114     /// @brief Get attribute of type T
0115     template<class T>
0116     std::shared_ptr<T> attribute(const std::string &name) const;
0117 
0118     /// @brief Get attribute of any type as string
0119     std::string attribute_as_string(const std::string &name) const;
0120 
0121     /// @brief Get list of attribute names
0122     std::vector<std::string> attribute_names() const;
0123 
0124     /// @brief Get a copy of the list of attributes
0125     /// @note To avoid thread issues, this is returns a copy. Better solution may be needed.
0126     std::map< std::string, std::shared_ptr<Attribute> > attributes() const {
0127         return m_attributes;
0128     }
0129 
0130 
0131 #endif // __CINT__
0132 
0133     /// @name Methods to fill GenRunInfoData and to read it back
0134     /// @{
0135 
0136     /// @brief Fill GenRunInfoData object
0137     void write_data(GenRunInfoData &data) const;
0138 
0139     /// @brief Fill GenRunInfo based on GenRunInfoData
0140     void read_data(const GenRunInfoData &data);
0141 
0142 #ifdef HEPMC3_ROOTIO
0143     /// @brief ROOT I/O streamer
0144     void Streamer(TBuffer &b);
0145     /// @}
0146 #endif
0147 
0148 private:
0149 
0150     /// @name Fields
0151     /// @{
0152 
0153 #if !defined(__CINT__)
0154 
0155     /// @brief The vector of tools used to produce this run.
0156     std::vector<ToolInfo> m_tools;
0157 
0158     /// @brief A map of weight names mapping to indices.
0159     std::map<std::string, int> m_weight_indices;
0160 
0161     /// @brief A vector of weight names.
0162     std::vector<std::string> m_weight_names;
0163 
0164     /// @brief Map of attributes
0165     mutable std::map< std::string, std::shared_ptr<Attribute> > m_attributes;
0166 
0167     /// @brief Mutex lock for the m_attibutes map.
0168     mutable std::recursive_mutex m_lock_attributes;
0169     /// @}
0170 
0171 #endif // __CINT__
0172 };
0173 
0174 #if !defined(__CINT__)
0175 
0176 //
0177 // Template methods
0178 //
0179 
0180 template<class T>
0181 std::shared_ptr<T> GenRunInfo::attribute(const std::string &name) const {
0182     std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
0183     std::map< std::string, std::shared_ptr<Attribute> >::iterator i =
0184         m_attributes.find(name);
0185     if ( i == m_attributes.end() ) return std::shared_ptr<T>();
0186 
0187     if ( !i->second->is_parsed() ) {
0188 
0189         std::shared_ptr<T> att = std::make_shared<T>();
0190         if ( att->from_string(i->second->unparsed_string()) &&
0191                 att->init(*this) ) {
0192             // update map with new pointer
0193             i->second = att;
0194 
0195             return att;
0196         }
0197         else
0198             return std::shared_ptr<T>();
0199     }
0200     else return std::dynamic_pointer_cast<T>(i->second);
0201 }
0202 
0203 /*
0204 /// @brief Returns a copy of indices map.
0205 template<typename T> std::map<std::string, T> weight_indices() const {
0206         std::map<std::string, T> ret;
0207         ret.insert(m_weight_indices.begin(),m_weight_indices.end());
0208         return ret;
0209 }
0210 */
0211 
0212 #endif // __CINT__
0213 
0214 } // namespace HepMC3
0215 
0216 #endif