1 //--------------------------------------------------------------------------
2 #ifndef HEPMC_WEIGHT_CONTAINER_H
3 #define HEPMC_WEIGHT_CONTAINER_H
5 //////////////////////////////////////////////////////////////////////////
6 // Matt.Dobbs@Cern.CH, November 2000, refer to:
7 // M. Dobbs and J.B. Hansen, "The HepMC C++ Monte Carlo Event Record for
8 // High Energy Physics", Computer Physics Communications (to be published).
10 // Container for the Weights associated with an event or vertex.
12 // This implementation adds a map-like interface in addition to the
13 // vector-like interface.
14 //////////////////////////////////////////////////////////////////////////
23 //! Container for the Weights associated with an event or vertex.
26 /// \class WeightContainer
27 /// This class has both map-like and vector-like functionality.
28 /// Named weights are now supported.
29 class WeightContainer {
30 friend class GenEvent;
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;
40 /// default constructor
41 explicit WeightContainer( size_type n = 0, double value = 0. );
42 /// construct from a vector of weights
43 WeightContainer( const std::vector<double>& weights );
45 WeightContainer( const WeightContainer& in );
49 void swap( WeightContainer & other);
51 WeightContainer& operator=( const WeightContainer& );
52 /// alternate assignment using a vector of doubles
53 WeightContainer& operator=( const std::vector<double>& in );
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;
60 /// size of weight container
61 size_type size() const;
62 /// return true if weight container is empty
64 /// push onto weight container
65 void push_back( const double& );
66 /// pop from weight container
68 /// clear the weight container
71 /// check to see if a name exists in the map
72 bool has_key( const std::string& s ) const;
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;
84 bool operator==( const WeightContainer & ) const;
86 bool operator!=( const WeightContainer & ) const;
88 /// returns the first element
90 /// returns the first element
91 const double& front() const;
92 /// returns the last element
94 /// returns the last element
95 const double& back() const;
97 /// begining of the weight container
99 /// end of the weight container
101 /// begining of the weight container
102 const_iterator begin() const;
103 /// end of the weight container
104 const_iterator end() const;
107 // for internal use only
109 /// maplike iterator for the weight container
110 /// for internal use only
111 typedef std::map<std::string,size_type>::iterator map_iterator;
112 /// const iterator for the weight container
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;
128 /// used by the constructors to set initial names
129 /// for internal use only
130 void set_default_names( size_type n );
133 std::vector<double> m_weights;
134 std::map<std::string,size_type> m_names;
137 ///////////////////////////
139 ///////////////////////////
141 inline WeightContainer::WeightContainer( const WeightContainer& in )
142 : m_weights(in.m_weights), m_names(in.m_names)
145 inline WeightContainer::~WeightContainer() {}
147 inline void WeightContainer::swap( WeightContainer & other)
149 m_weights.swap( other.m_weights );
150 m_names.swap( other.m_names );
153 inline WeightContainer& WeightContainer::operator=
154 ( const WeightContainer& in ) {
155 /// best practices implementation
156 WeightContainer tmp( in );
161 inline WeightContainer& WeightContainer::operator=
162 ( const std::vector<double>& in ) {
163 /// best practices implementation
164 WeightContainer tmp( in );
169 inline WeightContainer::size_type WeightContainer::size() const { return m_weights.size(); }
171 inline bool WeightContainer::empty() const { return m_weights.empty(); }
173 inline void WeightContainer::clear()
179 inline double& WeightContainer::operator[]( size_type n )
180 { return m_weights[n]; }
182 inline const double& WeightContainer::operator[]( size_type n ) const
183 { return m_weights[n]; }
185 inline double& WeightContainer::front() { return m_weights.front(); }
187 inline const double& WeightContainer::front() const
188 { return m_weights.front(); }
190 inline double& WeightContainer::back() { return m_weights.back(); }
192 inline const double& WeightContainer::back() const
193 { return m_weights.back(); }
195 inline WeightContainer::iterator WeightContainer::begin()
196 { return m_weights.begin(); }
198 inline WeightContainer::iterator WeightContainer::end()
199 { return m_weights.end(); }
201 inline WeightContainer::const_iterator WeightContainer::begin() const
202 { return m_weights.begin(); }
204 inline WeightContainer::const_iterator WeightContainer::end() const
205 { return m_weights.end(); }
207 inline WeightContainer::map_iterator WeightContainer::map_begin()
208 { return m_names.begin(); }
210 inline WeightContainer::map_iterator WeightContainer::map_end()
211 { return m_names.end(); }
213 inline WeightContainer::const_map_iterator WeightContainer::map_begin() const
214 { return m_names.begin(); }
216 inline WeightContainer::const_map_iterator WeightContainer::map_end() const
217 { return m_names.end(); }
221 #endif // HEPMC_WEIGHT_CONTAINER_H
222 //--------------------------------------------------------------------------