yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.0
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.
 
 Weights (const std::vector< std::pair< std::string, double > > &keys_values)
 Constructor from a vector of key/value pairs.
 
 Weights (const std::vector< std::string > &keys, const std::vector< double > &values)
 Constructor from vectors of keys and values.
 
 Weights (const std::vector< std::string > &keys, double value=0.0)
 Constructor from vectors of keys and a single value, defaulting to 0.0.
 
Arithmetic operators as members
Weightsoperator+= (const Weights &toAdd)
 Add another weights to this.
 
Weightsoperator-= (const Weights &toSubtract)
 Subtract another weights from this.
 
Weightsoperator*= (const Weights &toMultiplyBy)
 Multiply by another weights.
 
Weightsoperator/= (const Weights &toDivideBy)
 Divide by another weights.
 
Weightsoperator*= (double toMultiplyBy)
 Multiply by a double.
 
Weightsoperator/= (double toDivideBy)
 Divide by a double.
 
Weights operator- () const
 
Comparison operators
bool operator== (const Weights &other) const
 Equals.
 
bool operator!= (const Weights &other) const
 Not equals.
 

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.
 
std::vector< std::string > keys () const
 Sorted list of weight keys.
 
std::vector< double > values () const
 List of weight values, in the order of the sorted keys.
 

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

◆ const_iterator

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

Definition at line 72 of file Weights.h.

◆ iterator

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

Definition at line 71 of file Weights.h.

Constructor & Destructor Documentation

◆ Weights() [1/5]

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

Definition at line 28 of file Weights.h.

29 : _values(other._values)
30 { }

◆ Weights() [2/5]

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 }

◆ Weights() [3/5]

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

◆ Weights() [4/5]

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 }
std::vector< double > values() const
List of weight values, in the order of the sorted keys.
Definition Weights.h:121
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition Weights.h:111

References keys(), and values().

◆ Weights() [5/5]

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 }

References keys().

Member Function Documentation

◆ begin() [1/2]

iterator YODA::Weights::begin ( )
inline

Definition at line 73 of file Weights.h.

73{ return _values.begin(); }

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

◆ begin() [2/2]

const_iterator YODA::Weights::begin ( ) const
inline

Definition at line 74 of file Weights.h.

74{ return _values.begin(); }

◆ end() [1/2]

iterator YODA::Weights::end ( )
inline

Definition at line 75 of file Weights.h.

75{ return _values.end(); }

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

◆ end() [2/2]

const_iterator YODA::Weights::end ( ) const
inline

Definition at line 76 of file Weights.h.

76{ return _values.end(); }

◆ keys()

std::vector< std::string > YODA::Weights::keys ( ) const
inline

Sorted list of weight keys.

Definition at line 111 of file Weights.h.

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 }
iterator end()
Definition Weights.h:75
iterator begin()
Definition Weights.h:73
unsigned int size() const
Number of weights keys.
Definition Weights.h:106

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

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

◆ operator!=()

bool YODA::Weights::operator!= ( const Weights other) const
inline

Not equals.

Definition at line 220 of file Weights.h.

220 {
221 return !(*this == other);
222 }

◆ operator*=() [1/2]

Weights & YODA::Weights::operator*= ( const Weights toMultiplyBy)
inline

Multiply by another weights.

Definition at line 161 of file Weights.h.

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 }

References keys(), and size().

◆ operator*=() [2/2]

Weights & YODA::Weights::operator*= ( double  toMultiplyBy)
inline

Multiply by a double.

Definition at line 185 of file Weights.h.

185 {
186 for (size_t i = 0; i < size(); ++i) {
187 _values[keys()[i]] *= toMultiplyBy;
188 }
189 return *this;
190 }

References keys(), and size().

◆ operator+=()

Weights & YODA::Weights::operator+= ( const Weights toAdd)
inline

Add another weights to this.

Definition at line 137 of file Weights.h.

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 }

References keys(), and size().

◆ operator-()

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

◆ operator-=()

Weights & YODA::Weights::operator-= ( const Weights toSubtract)
inline

Subtract another weights from this.

Definition at line 149 of file Weights.h.

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 }

References keys(), and size().

◆ operator/=() [1/2]

Weights & YODA::Weights::operator/= ( const Weights toDivideBy)
inline

Divide by another weights.

Definition at line 173 of file Weights.h.

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 }

References keys(), and size().

◆ operator/=() [2/2]

Weights & YODA::Weights::operator/= ( double  toDivideBy)
inline

Divide by a double.

Definition at line 193 of file Weights.h.

193 {
194 for (size_t i = 0; i < size(); ++i) {
195 _values[keys()[i]] /= toDivideBy;
196 }
197 return *this;
198 }

References keys(), and size().

◆ operator==()

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 }

◆ operator[]() [1/4]

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 }

◆ operator[]() [2/4]

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 }

◆ operator[]() [3/4]

double & YODA::Weights::operator[] ( size_t  index)
inline

Definition at line 92 of file Weights.h.

92 {
93 if (index >= size()) {
94 throw WeightError("Requested weight index is larger than the weights collection");
95 }
96 return _values[keys()[index]];
97 }

References keys(), and size().

◆ operator[]() [4/4]

const double & YODA::Weights::operator[] ( size_t  index) const
inline

Definition at line 98 of file Weights.h.

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 }

References keys(), and size().

◆ size()

unsigned int YODA::Weights::size ( ) const
inline

Number of weights keys.

Definition at line 106 of file Weights.h.

106 {
107 return _values.size();
108 }

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

◆ values()

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.

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 }

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

Referenced by Weights().


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