YODA is hosted by Hepforge, IPPP Durham
YODA::Weights Class Reference

A named, vectorised generalisation of an event weight. More...

#include <Weights.h>

Public Member Functions

Constructors
 Weights (const Weights &other)
 
 Weights (double value)
 Convenience auto-constructor from a single double, since that's the commonest use case. More...
 
 Weights (const std::vector< std::pair< std::string, double > > &keys_values)
 Constructor from a vector of key/value pairs. More...
 
 Weights (const std::vector< std::string > &keys, const std::vector< double > &values)
 Constructor from vectors of keys and values. More...
 
 Weights (const std::vector< std::string > &keys, double value=0.0)
 Constructor from vectors of keys and a single value, defaulting to 0.0. More...
 
Arithmetic operators as members
Weightsoperator+= (const Weights &toAdd)
 Add another weights to this. More...
 
Weightsoperator-= (const Weights &toSubtract)
 Subtract another weights from this. More...
 
Weightsoperator*= (const Weights &toMultiplyBy)
 Multiply by another weights. More...
 
Weightsoperator/= (const Weights &toDivideBy)
 Divide by another weights. More...
 
Weightsoperator*= (double toMultiplyBy)
 Multiply by a double. More...
 
Weightsoperator/= (double toDivideBy)
 Divide by a double. More...
 
Weights operator- () const
 
Comparison operators
bool operator== (const Weights &other) const
 Equals. More...
 
bool operator== (const Weights &other)
 Not equals. More...
 

Accessors

Todo:
Sorted iterators of pair<std::string, double>
typedef std::map< std::string,
double >::iterator 
iterator
 
typedef std::map< std::string,
double >::const_iterator 
const_iterator
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
double & operator[] (const std::string &key)
 
const double & operator[] (const std::string &key) const
 
double & operator[] (size_t index)
 
const double & operator[] (size_t index) const
 
unsigned int size () const
 Number of weights keys. More...
 
std::vector< std::string > keys () const
 Sorted list of weight keys. More...
 
std::vector< double > values () const
 List of weight values, in the order of the sorted keys. More...
 

Detailed Description

A named, vectorised generalisation of an event weight.

Todo:

Accept general Boost.Ranges as constructor args... but start with literal arrays for convenience

Autogenerate numerical names if not given

Definition at line 22 of file Weights.h.

Member Typedef Documentation

typedef std::map<std::string, double>::const_iterator YODA::Weights::const_iterator

Definition at line 72 of file Weights.h.

typedef std::map<std::string, double>::iterator YODA::Weights::iterator

Definition at line 71 of file Weights.h.

Constructor & Destructor Documentation

YODA::Weights::Weights ( const Weights other)
inline

Definition at line 28 of file Weights.h.

29  : _values(other._values)
30  { }
YODA::Weights::Weights ( double  value)
inline

Convenience auto-constructor from a single double, since that's the commonest use case.

Definition at line 33 of file Weights.h.

33  {
34  _values["0"] = value;
35  }
YODA::Weights::Weights ( const std::vector< std::pair< std::string, double > > &  keys_values)
inline

Constructor from a vector of key/value pairs.

Definition at line 38 of file Weights.h.

38  {
39  for (std::vector<std::pair<std::string, double> >::const_iterator i = keys_values.begin(); i != keys_values.end(); ++i) {
40  _values[i->first] = i->second;
41  }
42  }
std::map< std::string, double >::const_iterator const_iterator
Definition: Weights.h:72
YODA::Weights::Weights ( const std::vector< std::string > &  keys,
const std::vector< double > &  values 
)
inline

Constructor from vectors of keys and values.

Definition at line 45 of file Weights.h.

45  {
46  if (keys.size() != values.size()) {
47  throw WeightError("Mismatch in lengths of keys and values vectors in Weights constructor");
48  }
49  for (size_t i = 0; i < keys.size(); ++i) {
50  _values[keys[i]] = values[i];
51  }
52  }
YODA::Weights::Weights ( const std::vector< std::string > &  keys,
double  value = 0.0 
)
inline

Constructor from vectors of keys and a single value, defaulting to 0.0.

Definition at line 55 of file Weights.h.

55  {
56  for (std::vector<std::string>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
57  _values[*i] = value;
58  }
59  }

Member Function Documentation

iterator YODA::Weights::begin ( )
inline

Definition at line 73 of file Weights.h.

Referenced by keys(), YODA::operator<<(), and values().

73 { return _values.begin(); }
const_iterator YODA::Weights::begin ( ) const
inline

Definition at line 74 of file Weights.h.

74 { return _values.begin(); }
iterator YODA::Weights::end ( )
inline

Definition at line 75 of file Weights.h.

Referenced by keys(), YODA::operator<<(), and values().

75 { return _values.end(); }
const_iterator YODA::Weights::end ( ) const
inline

Definition at line 76 of file Weights.h.

76 { return _values.end(); }
std::vector<std::string> YODA::Weights::keys ( ) const
inline

Sorted list of weight keys.

Definition at line 111 of file Weights.h.

References begin(), end(), and size().

Referenced by operator*=(), operator+=(), operator-=(), YODA::operator/(), operator/=(), and operator[]().

111  {
112  std::vector<std::string> rtn;
113  rtn.reserve(size());
114  for (const_iterator i = begin(); i != end(); ++i) {
115  rtn.push_back(i->first);
116  }
117  return rtn;
118  }
std::map< std::string, double >::const_iterator const_iterator
Definition: Weights.h:72
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
iterator begin()
Definition: Weights.h:73
iterator end()
Definition: Weights.h:75
Weights& YODA::Weights::operator*= ( const Weights toMultiplyBy)
inline

Multiply by another weights.

Definition at line 161 of file Weights.h.

References keys(), and size().

161  {
162  if (keys().empty()) _initToMatch(toMultiplyBy);
163  if (keys() != toMultiplyBy.keys()) {
164  throw WeightError("Mismatch in args to Weights *= operator");
165  }
166  for (size_t i = 0; i < size(); ++i) {
167  _values[keys()[i]] *= toMultiplyBy[keys()[i]];
168  }
169  return *this;
170  }
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition: Weights.h:111
Weights& YODA::Weights::operator*= ( double  toMultiplyBy)
inline

Multiply by a double.

Definition at line 185 of file Weights.h.

References keys(), and size().

185  {
186  for (size_t i = 0; i < size(); ++i) {
187  _values[keys()[i]] *= toMultiplyBy;
188  }
189  return *this;
190  }
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition: Weights.h:111
Weights& YODA::Weights::operator+= ( const Weights toAdd)
inline

Add another weights to this.

Definition at line 137 of file Weights.h.

References keys(), and size().

137  {
138  if (keys().empty()) _initToMatch(toAdd);
139  if (keys() != toAdd.keys()) {
140  throw WeightError("Mismatch in args to Weights += operator");
141  }
142  for (size_t i = 0; i < size(); ++i) {
143  _values[keys()[i]] += toAdd[keys()[i]];
144  }
145  return *this;
146  }
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition: Weights.h:111
Weights YODA::Weights::operator- ( ) const
inline

Negate

Todo:
Can/should this modify itself and return a reference?

Definition at line 202 of file Weights.h.

202  {
203  Weights rtn = *this;
204  rtn *= -1;
205  return rtn;
206  }
Weights(const Weights &other)
Definition: Weights.h:28
Weights& YODA::Weights::operator-= ( const Weights toSubtract)
inline

Subtract another weights from this.

Definition at line 149 of file Weights.h.

References keys(), and size().

149  {
150  if (keys().empty()) _initToMatch(toSubtract);
151  if (keys() != toSubtract.keys()) {
152  throw WeightError("Mismatch in args to Weights -= operator");
153  }
154  for (size_t i = 0; i < size(); ++i) {
155  _values[keys()[i]] -= toSubtract[keys()[i]];
156  }
157  return *this;
158  }
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition: Weights.h:111
Weights& YODA::Weights::operator/= ( const Weights toDivideBy)
inline

Divide by another weights.

Definition at line 173 of file Weights.h.

References keys(), and size().

173  {
174  if (keys().empty()) _initToMatch(toDivideBy);
175  if (keys() != toDivideBy.keys()) {
176  throw WeightError("Mismatch in args to Weights /= operator");
177  }
178  for (size_t i = 0; i < size(); ++i) {
179  _values[keys()[i]] /= toDivideBy[keys()[i]];
180  }
181  return *this;
182  }
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition: Weights.h:111
Weights& YODA::Weights::operator/= ( double  toDivideBy)
inline

Divide by a double.

Definition at line 193 of file Weights.h.

References keys(), and size().

193  {
194  for (size_t i = 0; i < size(); ++i) {
195  _values[keys()[i]] /= toDivideBy;
196  }
197  return *this;
198  }
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition: Weights.h:111
bool YODA::Weights::operator== ( const Weights other) const
inline

Equals.

Definition at line 215 of file Weights.h.

215  {
216  return this->_values == other._values;
217  }
bool YODA::Weights::operator== ( const Weights other)
inline

Not equals.

Definition at line 220 of file Weights.h.

220  {
221  return !(*this == other);
222  }
double& YODA::Weights::operator[] ( const std::string &  key)
inline

Definition at line 78 of file Weights.h.

78  {
79  if (_values.find(key) == _values.end()) {
80  throw WeightError("No weight found with supplied name");
81  }
82  return _values[key];
83  }
const double& YODA::Weights::operator[] ( const std::string &  key) const
inline

Definition at line 84 of file Weights.h.

84  {
85  const_iterator rtn = _values.find(key);
86  if (rtn == _values.end()) {
87  throw WeightError("No weight found with supplied name");
88  }
89  return rtn->second;
90  }
std::map< std::string, double >::const_iterator const_iterator
Definition: Weights.h:72
double& YODA::Weights::operator[] ( size_t  index)
inline

Definition at line 92 of file Weights.h.

References keys(), and size().

92  {
93  if (index >= size()) {
94  throw WeightError("Requested weight index is larger than the weights collection");
95  }
96  return _values[keys()[index]];
97  }
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition: Weights.h:111
const double& YODA::Weights::operator[] ( size_t  index) const
inline

Definition at line 98 of file Weights.h.

References keys(), and size().

98  {
99  if (index >= size()) {
100  throw WeightError("Requested weight index is larger than the weights collection");
101  }
102  return _values.find(keys()[index])->second;
103  }
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition: Weights.h:111
unsigned int YODA::Weights::size ( ) const
inline

Number of weights keys.

Definition at line 106 of file Weights.h.

Referenced by keys(), operator*=(), operator+=(), operator-=(), operator/=(), operator[](), and values().

106  {
107  return _values.size();
108  }
std::vector<double> YODA::Weights::values ( ) const
inline

List of weight values, in the order of the sorted keys.

Definition at line 121 of file Weights.h.

References begin(), end(), and size().

121  {
122  std::vector<double> rtn;
123  rtn.reserve(size());
124  for (const_iterator i = begin(); i != end(); ++i) {
125  rtn.push_back(i->second);
126  }
127  return rtn;
128  }
std::map< std::string, double >::const_iterator const_iterator
Definition: Weights.h:72
unsigned int size() const
Number of weights keys.
Definition: Weights.h:106
iterator begin()
Definition: Weights.h:73
iterator end()
Definition: Weights.h:75

The documentation for this class was generated from the following file: