Line data Source code
1 : //--------------------------------------------------------------------------
2 : #ifndef HEPMC_WEIGHT_CONTAINER_H
3 : #define HEPMC_WEIGHT_CONTAINER_H
4 :
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).
9 : //
10 : // Container for the Weights associated with an event or vertex.
11 : //
12 : // This implementation adds a map-like interface in addition to the
13 : // vector-like interface.
14 : //////////////////////////////////////////////////////////////////////////
15 :
16 : #include <iostream>
17 : #include <vector>
18 : #include <string>
19 : #include <map>
20 :
21 : namespace HepMC {
22 :
23 : //! Container for the Weights associated with an event or vertex.
24 :
25 : ///
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;
31 :
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 :
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 );
44 : /// copy
45 : WeightContainer( const WeightContainer& in );
46 : ~WeightContainer();
47 :
48 : /// swap
49 : void swap( WeightContainer & other);
50 : /// copy assignment
51 : WeightContainer& operator=( const WeightContainer& );
52 : /// alternate assignment using a vector of doubles
53 : WeightContainer& operator=( const std::vector<double>& in );
54 :
55 : /// print weights
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;
59 :
60 : /// size of weight container
61 : size_type size() const;
62 : /// return true if weight container is empty
63 : bool empty() const;
64 : /// push onto weight container
65 : void push_back( const double& );
66 : /// pop from weight container
67 : void pop_back();
68 : /// clear the weight container
69 : void clear();
70 :
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 :
88 : /// returns the first element
89 : double& front();
90 : /// returns the first element
91 : const double& front() const;
92 : /// returns the last element
93 : double& back();
94 : /// returns the last element
95 : const double& back() const;
96 :
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;
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;
127 :
128 : /// used by the constructors to set initial names
129 : /// for internal use only
130 : void set_default_names( size_type n );
131 :
132 : private:
133 : std::vector<double> m_weights;
134 : std::map<std::string,size_type> m_names;
135 : };
136 :
137 : ///////////////////////////
138 : // INLINES //
139 : ///////////////////////////
140 :
141 : inline WeightContainer::WeightContainer( const WeightContainer& in )
142 0 : : m_weights(in.m_weights), m_names(in.m_names)
143 0 : {}
144 :
145 0 : inline WeightContainer::~WeightContainer() {}
146 :
147 : inline void WeightContainer::swap( WeightContainer & other)
148 : {
149 0 : m_weights.swap( other.m_weights );
150 0 : m_names.swap( other.m_names );
151 0 : }
152 :
153 : inline WeightContainer& WeightContainer::operator=
154 : ( const WeightContainer& in ) {
155 : /// best practices implementation
156 0 : WeightContainer tmp( in );
157 0 : swap( tmp );
158 : return *this;
159 0 : }
160 :
161 : inline WeightContainer& WeightContainer::operator=
162 : ( const std::vector<double>& in ) {
163 : /// best practices implementation
164 0 : WeightContainer tmp( in );
165 0 : swap( tmp );
166 : return *this;
167 0 : }
168 :
169 0 : inline WeightContainer::size_type WeightContainer::size() const { return m_weights.size(); }
170 :
171 0 : inline bool WeightContainer::empty() const { return m_weights.empty(); }
172 :
173 : inline void WeightContainer::clear()
174 : {
175 : m_weights.clear();
176 : m_names.clear();
177 : }
178 :
179 : inline double& WeightContainer::operator[]( size_type n )
180 0 : { return m_weights[n]; }
181 :
182 : inline const double& WeightContainer::operator[]( size_type n ) const
183 : { return m_weights[n]; }
184 :
185 0 : inline double& WeightContainer::front() { return m_weights.front(); }
186 :
187 : inline const double& WeightContainer::front() const
188 : { return m_weights.front(); }
189 :
190 : inline double& WeightContainer::back() { return m_weights.back(); }
191 :
192 : inline const double& WeightContainer::back() const
193 : { return m_weights.back(); }
194 :
195 : inline WeightContainer::iterator WeightContainer::begin()
196 : { return m_weights.begin(); }
197 :
198 : inline WeightContainer::iterator WeightContainer::end()
199 : { return m_weights.end(); }
200 :
201 : inline WeightContainer::const_iterator WeightContainer::begin() const
202 0 : { return m_weights.begin(); }
203 :
204 : inline WeightContainer::const_iterator WeightContainer::end() const
205 0 : { return m_weights.end(); }
206 :
207 : inline WeightContainer::map_iterator WeightContainer::map_begin()
208 0 : { return m_names.begin(); }
209 :
210 : inline WeightContainer::map_iterator WeightContainer::map_end()
211 0 : { return m_names.end(); }
212 :
213 : inline WeightContainer::const_map_iterator WeightContainer::map_begin() const
214 0 : { return m_names.begin(); }
215 :
216 : inline WeightContainer::const_map_iterator WeightContainer::map_end() const
217 0 : { return m_names.end(); }
218 :
219 : } // HepMC
220 :
221 : #endif // HEPMC_WEIGHT_CONTAINER_H
222 : //--------------------------------------------------------------------------
223 :
224 :
225 :
|