StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WeightContainer.h
1 //--------------------------------------------------------------------------
2 #ifndef HEPMC_WEIGHT_CONTAINER_H
3 #define HEPMC_WEIGHT_CONTAINER_H
4 
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.
15 
16 #include <iostream>
17 #include <vector>
18 #include <string>
19 #include <map>
20 
21 namespace HepMC {
22 
24 
30  friend class GenEvent;
31 
32  public:
34  typedef std::size_t size_type;
36  typedef std::vector<double>::iterator iterator;
38  typedef std::vector<double>::const_iterator const_iterator;
39 
41  explicit WeightContainer( size_type n = 0, double value = 0. );
43  WeightContainer( const std::vector<double>& weights );
45  WeightContainer( const WeightContainer& in );
46  ~WeightContainer();
47 
49  void swap( WeightContainer & other);
53  WeightContainer& operator=( const std::vector<double>& in );
54 
56  void print( std::ostream& ostr = std::cout ) const;
58  void write( std::ostream& ostr = std::cout ) const;
59 
61  size_type size() const;
63  bool empty() const;
65  void push_back( const double& );
67  void pop_back();
69  void clear();
70 
72  bool has_key( const std::string& s ) const;
73 
75  double& operator[]( size_type n ); // unchecked access
77  const double& operator[]( size_type n ) const;
79  double& operator[]( const std::string& s ); // unchecked access
81  const double& operator[]( const std::string& s ) const;
82 
84  bool operator==( const WeightContainer & ) const;
86  bool operator!=( const WeightContainer & ) const;
87 
89  double& front();
91  const double& front() const;
93  double& back();
95  const double& back() const;
96 
98  iterator begin();
100  iterator end();
102  const_iterator begin() const;
104  const_iterator end() const;
105 
106  private:
107  // for internal use only
108 
111  typedef std::map<std::string,size_type>::iterator map_iterator;
114  typedef std::map<std::string,size_type>::const_iterator const_map_iterator;
117  map_iterator map_begin();
120  map_iterator map_end();
123  const_map_iterator map_begin() const;
126  const_map_iterator map_end() const;
127 
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 
138  // INLINES //
140 
142  : m_weights(in.m_weights), m_names(in.m_names)
143  {}
144 
145  inline WeightContainer::~WeightContainer() {}
146 
148  {
149  m_weights.swap( other.m_weights );
150  m_names.swap( other.m_names );
151  }
152 
153  inline WeightContainer& WeightContainer::operator=
154  ( const WeightContainer& in ) {
156  WeightContainer tmp( in );
157  swap( tmp );
158  return *this;
159  }
160 
161  inline WeightContainer& WeightContainer::operator=
162  ( const std::vector<double>& in ) {
164  WeightContainer tmp( in );
165  swap( tmp );
166  return *this;
167  }
168 
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()
174  {
175  m_weights.clear();
176  m_names.clear();
177  }
178 
180  { return m_weights[n]; }
181 
182  inline const double& WeightContainer::operator[]( size_type n ) const
183  { return m_weights[n]; }
184 
185  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 
196  { return m_weights.begin(); }
197 
199  { return m_weights.end(); }
200 
202  { return m_weights.begin(); }
203 
205  { return m_weights.end(); }
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 
219 } // HepMC
220 
221 #endif // HEPMC_WEIGHT_CONTAINER_H
222 //--------------------------------------------------------------------------
223 
224 
225 
void print(std::ostream &ostr=std::cout) const
print weights
WeightContainer & operator=(const WeightContainer &)
copy assignment
double & operator[](size_type n)
access the weight container
bool operator!=(const WeightContainer &) const
inequality
std::vector< double >::const_iterator const_iterator
const iterator for the weight container
void pop_back()
pop from weight container
The GenEvent class is the core of HepMC.
Definition: GenEvent.h:155
void push_back(const double &)
push onto weight container
iterator begin()
begining of the weight container
bool has_key(const std::string &s) const
check to see if a name exists in the map
bool empty() const
return true if weight container is empty
double & front()
returns the first element
void write(std::ostream &ostr=std::cout) const
write weights in a readable table
WeightContainer(size_type n=0, double value=0.)
default constructor
std::size_t size_type
defining the size type used by vector and map
iterator end()
end of the weight container
std::vector< double >::iterator iterator
iterator for the weight container
Container for the Weights associated with an event or vertex.
void swap(WeightContainer &other)
swap
double & back()
returns the last element
bool operator==(const WeightContainer &) const
equality
size_type size() const
size of weight container
void clear()
clear the weight container