Changeset 572 in svn for trunk/Utilities/HepMC/interface/WeightContainer.h
- Timestamp:
- Nov 2, 2011, 5:39:26 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Utilities/HepMC/interface/WeightContainer.h
r349 r572 9 9 // 10 10 // Container for the Weights associated with an event or vertex. 11 // Basically just an interface to STL vector. 11 // 12 // This implementation adds a map-like interface in addition to the 13 // vector-like interface. 12 14 ////////////////////////////////////////////////////////////////////////// 13 15 14 16 #include <iostream> 15 17 #include <vector> 18 #include <string> 19 #include <map> 16 20 17 21 namespace HepMC { … … 21 25 /// 22 26 /// \class WeightContainer 23 /// Basically just an interface to STL vector. 27 /// This class has both map-like and vector-like functionality. 28 /// Named weights are now supported. 24 29 class WeightContainer { 30 friend class GenEvent; 25 31 26 32 public: 33 /// defining the size type used by vector and map 34 typedef std::size_t size_type; 35 /// iterator for the weight container 36 typedef std::vector<double>::iterator iterator; 37 /// const iterator for the weight container 38 typedef std::vector<double>::const_iterator const_iterator; 39 27 40 /// default constructor 28 WeightContainer( unsigned int n = 0, const double&value = 0. );41 explicit WeightContainer( size_type n = 0, double value = 0. ); 29 42 /// construct from a vector of weights 30 43 WeightContainer( const std::vector<double>& weights ); 31 44 /// copy 32 45 WeightContainer( const WeightContainer& in ); 33 virtual~WeightContainer();46 ~WeightContainer(); 34 47 35 48 /// swap 36 49 void swap( WeightContainer & other); 37 /// copy 50 /// copy assignment 38 51 WeightContainer& operator=( const WeightContainer& ); 39 /// copy52 /// alternate assignment using a vector of doubles 40 53 WeightContainer& operator=( const std::vector<double>& in ); 41 54 42 55 /// print weights 43 56 void print( std::ostream& ostr = std::cout ) const; 57 /// write weights in a readable table 58 void write( std::ostream& ostr = std::cout ) const; 44 59 45 60 /// size of weight container 46 intsize() const;61 size_type size() const; 47 62 /// return true if weight container is empty 48 63 bool empty() const; … … 54 69 void clear(); 55 70 56 /// access the weight container 57 double& operator[]( unsigned int n ); // unchecked access 58 /// access the weight container 59 const double& operator[]( unsigned int n ) const; 60 71 /// check to see if a name exists in the map 72 bool has_key( const std::string& s ) const; 73 74 /// access the weight container 75 double& operator[]( size_type n ); // unchecked access 76 /// access the weight container 77 const double& operator[]( size_type n ) const; 78 /// access the weight container 79 double& operator[]( const std::string& s ); // unchecked access 80 /// access the weight container 81 const double& operator[]( const std::string& s ) const; 82 83 /// equality 84 bool operator==( const WeightContainer & ) const; 85 /// inequality 86 bool operator!=( const WeightContainer & ) const; 87 61 88 /// returns the first element 62 89 double& front(); … … 68 95 const double& back() const; 69 96 70 /// iterator for the weight container 71 typedef std::vector<double>::iterator iterator; 97 /// begining of the weight container 98 iterator begin(); 99 /// end of the weight container 100 iterator end(); 101 /// begining of the weight container 102 const_iterator begin() const; 103 /// end of the weight container 104 const_iterator end() const; 105 106 private: 107 // for internal use only 108 109 /// maplike iterator for the weight container 110 /// for internal use only 111 typedef std::map<std::string,size_type>::iterator map_iterator; 72 112 /// const iterator for the weight container 73 typedef std::vector<double>::const_iterator const_iterator; 74 /// begining of the weight container 75 iterator begin(); 76 /// end of the weight container 77 iterator end(); 78 /// begining of the weight container 79 const_iterator begin() const; 80 /// end of the weight container 81 const_iterator end() const; 113 /// for internal use only 114 typedef std::map<std::string,size_type>::const_iterator const_map_iterator; 115 /// begining of the weight container 116 /// for internal use only 117 map_iterator map_begin(); 118 /// end of the weight container 119 /// for internal use only 120 map_iterator map_end(); 121 /// begining of the weight container 122 /// for internal use only 123 const_map_iterator map_begin() const; 124 /// end of the weight container 125 /// for internal use only 126 const_map_iterator map_end() const; 127 128 /// used by the constructors to set initial names 129 /// for internal use only 130 void set_default_names( size_type n ); 82 131 83 132 private: 84 std::vector<double> m_weights; 133 std::vector<double> m_weights; 134 std::map<std::string,size_type> m_names; 85 135 }; 86 136 … … 89 139 /////////////////////////// 90 140 91 inline WeightContainer::WeightContainer( unsigned int n, 92 const double& value ) 93 : m_weights(n,value) 141 inline WeightContainer::WeightContainer( const WeightContainer& in ) 142 : m_weights(in.m_weights), m_names(in.m_names) 94 143 {} 95 144 96 inline WeightContainer::WeightContainer( const std::vector<double>& wgts )97 : m_weights(wgts)98 {}99 100 inline WeightContainer::WeightContainer( const WeightContainer& in )101 : m_weights(in.m_weights)102 {}103 104 145 inline WeightContainer::~WeightContainer() {} 105 146 106 147 inline void WeightContainer::swap( WeightContainer & other) 107 { m_weights.swap( other.m_weights ); } 148 { 149 m_weights.swap( other.m_weights ); 150 m_names.swap( other.m_names ); 151 } 108 152 109 153 inline WeightContainer& WeightContainer::operator= … … 123 167 } 124 168 125 inline void WeightContainer::print( std::ostream& ostr ) const 169 inline WeightContainer::size_type WeightContainer::size() const { return m_weights.size(); } 170 171 inline bool WeightContainer::empty() const { return m_weights.empty(); } 172 173 inline void WeightContainer::clear() 126 174 { 127 for ( const_iterator w = begin(); w != end(); ++w ) 128 { 129 ostr << *w << " "; 130 } 131 ostr << std::endl; 132 } 133 134 inline int WeightContainer::size() const { return m_weights.size(); } 135 136 inline bool WeightContainer::empty() const { return m_weights.empty(); } 137 138 inline void WeightContainer::push_back( const double& value) 139 { m_weights.push_back(value); } 140 141 inline void WeightContainer::pop_back() { m_weights.pop_back(); } 142 143 inline void WeightContainer::clear() { m_weights.clear(); } 144 145 inline double& WeightContainer::operator[]( unsigned int n ) 146 { return m_weights[(int)n]; } 147 148 inline const double& WeightContainer::operator[]( unsigned int n ) const 149 { return m_weights[(int)n]; } 175 m_weights.clear(); 176 m_names.clear(); 177 } 178 179 inline double& WeightContainer::operator[]( size_type n ) 180 { return m_weights[n]; } 181 182 inline const double& WeightContainer::operator[]( size_type n ) const 183 { return m_weights[n]; } 150 184 151 185 inline double& WeightContainer::front() { return m_weights.front(); } … … 171 205 { return m_weights.end(); } 172 206 207 inline WeightContainer::map_iterator WeightContainer::map_begin() 208 { return m_names.begin(); } 209 210 inline WeightContainer::map_iterator WeightContainer::map_end() 211 { return m_names.end(); } 212 213 inline WeightContainer::const_map_iterator WeightContainer::map_begin() const 214 { return m_names.begin(); } 215 216 inline WeightContainer::const_map_iterator WeightContainer::map_end() const 217 { return m_names.end(); } 218 173 219 } // HepMC 174 220
Note:
See TracChangeset
for help on using the changeset viewer.