Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //--------------------------------------------------------------------------
0002 #ifndef HEPMC_WEIGHT_CONTAINER_H
0003 #define HEPMC_WEIGHT_CONTAINER_H
0004 
0005 //////////////////////////////////////////////////////////////////////////
0006 // Matt.Dobbs@Cern.CH, November 2000, refer to:
0007 // M. Dobbs and J.B. Hansen, "The HepMC C++ Monte Carlo Event Record for
0008 // High Energy Physics", Computer Physics Communications (to be published).
0009 //
0010 // Container for the Weights associated with an event or vertex.
0011 //
0012 // This implementation adds a map-like interface in addition to the
0013 // vector-like interface.
0014 //////////////////////////////////////////////////////////////////////////
0015 
0016 #include <iostream>
0017 #include <vector>
0018 #include <string>
0019 #include <map>
0020 
0021 namespace HepMC {
0022 
0023 
0024     //! Container for the Weights associated with an event or vertex.
0025 
0026     ///
0027     /// \class  WeightContainer
0028     /// This class has both map-like and vector-like functionality.
0029     /// Named weights are now supported.
0030     class WeightContainer {
0031     friend class GenEvent;
0032 
0033     public:
0034         /// defining the size type used by vector and map
0035     typedef std::size_t size_type;
0036         /// iterator for the weight container
0037     typedef std::vector<double>::iterator iterator;
0038         /// const iterator for the weight container
0039     typedef std::vector<double>::const_iterator const_iterator;
0040 
0041         /// default constructor
0042     explicit WeightContainer( size_type n = 0, double value = 0. );
0043         /// construct from a vector of weights
0044     WeightContainer( const std::vector<double>& weights );
0045         /// copy
0046     WeightContainer( const WeightContainer& in );
0047     ~WeightContainer();
0048 
0049     /// swap
0050     void swap( WeightContainer & other);
0051     /// copy assignment
0052     WeightContainer& operator=( const WeightContainer& );
0053     /// alternate assignment using a vector of doubles
0054     WeightContainer& operator=( const std::vector<double>& in );
0055 
0056     /// print weights
0057     void          print( std::ostream& ostr = std::cout ) const;
0058     /// write weights in a readable table
0059     void          write( std::ostream& ostr = std::cout ) const;
0060 
0061     /// size of weight container
0062     size_type     size() const;
0063     /// return true if weight container is empty
0064     bool          empty() const;
0065     /// push onto weight container
0066     void          push_back( const double& );
0067     /// pop from weight container
0068     void          pop_back();
0069     /// clear the weight container
0070     void          clear();
0071 
0072     /// the list of weight values
0073     std::vector<double>& weights() { return m_weights; }
0074     /// the list of weight values (const)
0075     const std::vector<double>& weights() const { return m_weights; }
0076 
0077     /// the weight names, ordered to match the values
0078     /// @warning This computation is not efficient, and should be avoided in tight loops
0079     std::vector<std::string> weight_names() const;
0080     /// check to see if a name exists in the map
0081     bool          has_key( const std::string& s ) const;
0082 
0083     /// access the weight container
0084     double&       operator[]( size_type n );  // unchecked access
0085     /// access the weight container
0086     const double& operator[]( size_type n ) const;
0087     /// access the weight container
0088     double&       operator[]( const std::string& s );  // unchecked access
0089     /// access the weight container
0090     const double& operator[]( const std::string& s ) const;
0091 
0092     /// equality
0093     bool operator==( const WeightContainer & ) const;
0094     /// inequality
0095     bool operator!=( const WeightContainer & ) const;
0096 
0097     /// returns the first element
0098     double&       front();
0099     /// returns the first element
0100     const double& front() const;
0101     /// returns the last element
0102     double&       back();
0103     /// returns the last element
0104     const double& back() const;
0105 
0106     /// beginning of the weight container
0107     iterator            begin();
0108     /// end of the weight container
0109     iterator            end();
0110     /// begining of the weight container
0111     const_iterator      begin() const;
0112     /// end of the weight container
0113     const_iterator      end() const;
0114 
0115     private:
0116         // for internal use only
0117 
0118         /// maplike iterator for the weight container
0119     /// for internal use only
0120     typedef std::map<std::string,size_type>::iterator       map_iterator;
0121         /// const iterator for the weight container
0122     /// for internal use only
0123     typedef std::map<std::string,size_type>::const_iterator const_map_iterator;
0124     /// begining of the weight container
0125     /// for internal use only
0126     map_iterator            map_begin();
0127     /// end of the weight container
0128     /// for internal use only
0129     map_iterator            map_end();
0130     /// begining of the weight container
0131     /// for internal use only
0132     const_map_iterator      map_begin() const;
0133     /// end of the weight container
0134     /// for internal use only
0135     const_map_iterator      map_end() const;
0136 
0137     /// used by the constructors to set initial names
0138     /// for internal use only
0139     void set_default_names( size_type n );
0140 
0141     private:
0142     std::vector<double>          m_weights;
0143     std::map<std::string,size_type> m_names;
0144     };
0145 
0146     ///////////////////////////
0147     // INLINES               //
0148     ///////////////////////////
0149 
0150     inline WeightContainer::WeightContainer( const WeightContainer& in )
0151     : m_weights(in.m_weights), m_names(in.m_names)
0152     {}
0153 
0154     inline WeightContainer::~WeightContainer() {}
0155 
0156     inline void WeightContainer::swap( WeightContainer & other)
0157     {
0158         m_weights.swap( other.m_weights );
0159         m_names.swap( other.m_names );
0160     }
0161 
0162     inline WeightContainer& WeightContainer::operator=
0163     ( const WeightContainer& in ) {
0164         /// best practices implementation
0165     WeightContainer tmp( in );
0166     swap( tmp );
0167     return *this;
0168     }
0169 
0170     inline WeightContainer& WeightContainer::operator=
0171     ( const std::vector<double>& in ) {
0172         /// best practices implementation
0173     WeightContainer tmp( in );
0174     swap( tmp );
0175     return *this;
0176     }
0177 
0178     inline WeightContainer::size_type WeightContainer::size() const { return m_weights.size(); }
0179 
0180     inline bool WeightContainer::empty() const { return m_weights.empty(); }
0181 
0182     inline void WeightContainer::clear()
0183     {
0184     m_weights.clear();
0185     m_names.clear();
0186     }
0187 
0188     inline double& WeightContainer::operator[]( size_type n )
0189     { return m_weights[n]; }
0190 
0191     inline const double& WeightContainer::operator[]( size_type n ) const
0192     { return m_weights[n]; }
0193 
0194     inline double& WeightContainer::front() { return m_weights.front(); }
0195 
0196     inline const double& WeightContainer::front() const
0197     { return m_weights.front(); }
0198 
0199     inline double& WeightContainer::back() { return m_weights.back(); }
0200 
0201     inline const double& WeightContainer::back() const
0202     { return m_weights.back(); }
0203 
0204     inline WeightContainer::iterator WeightContainer::begin()
0205     { return m_weights.begin(); }
0206 
0207     inline WeightContainer::iterator WeightContainer::end()
0208     { return m_weights.end(); }
0209 
0210     inline WeightContainer::const_iterator WeightContainer::begin() const
0211     { return m_weights.begin(); }
0212 
0213     inline WeightContainer::const_iterator WeightContainer::end() const
0214     { return m_weights.end(); }
0215 
0216     inline WeightContainer::map_iterator WeightContainer::map_begin()
0217     { return m_names.begin(); }
0218 
0219     inline WeightContainer::map_iterator WeightContainer::map_end()
0220     { return m_names.end(); }
0221 
0222     inline WeightContainer::const_map_iterator WeightContainer::map_begin() const
0223     { return m_names.begin(); }
0224 
0225     inline WeightContainer::const_map_iterator WeightContainer::map_end() const
0226     { return m_names.end(); }
0227 
0228 } // HepMC
0229 
0230 #endif  // HEPMC_WEIGHT_CONTAINER_H
0231 //--------------------------------------------------------------------------