yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.0
Weights.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of YODA -- Yet more Objects for Data Analysis
4// Copyright (C) 2008-2023 The YODA collaboration (see AUTHORS for details)
5//
6#ifndef YODA_Weights_h
7#define YODA_Weights_h
8
9#include "YODA/Exceptions.h"
10#include <vector>
11#include <string>
12#include <map>
13#include <ostream>
14
15namespace YODA {
16
17
22 class Weights {
23 public:
24
27
28 Weights(const Weights& other)
29 : _values(other._values)
30 { }
31
33 Weights(double value) {
34 _values["0"] = value;
35 }
36
38 Weights(const std::vector<std::pair<std::string, double> >& keys_values) {
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 }
43
45 Weights(const std::vector<std::string>& keys, const std::vector<double>& values) {
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 }
53
55 Weights(const std::vector<std::string>& keys, double value=0.0) {
56 for (std::vector<std::string>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
57 _values[*i] = value;
58 }
59 }
60
62
63 public:
64
65
67
70
71 typedef std::map<std::string, double>::iterator iterator;
72 typedef std::map<std::string, double>::const_iterator const_iterator;
73 iterator begin() { return _values.begin(); }
74 const_iterator begin() const { return _values.begin(); }
75 iterator end() { return _values.end(); }
76 const_iterator end() const { return _values.end(); }
77
78 double& operator [] (const std::string& key) {
79 if (_values.find(key) == _values.end()) {
80 throw WeightError("No weight found with supplied name");
81 }
82 return _values[key];
83 }
84 const double& operator [] (const std::string& key) const {
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 }
91
92 double& operator [] (size_t index) {
93 if (index >= size()) {
94 throw WeightError("Requested weight index is larger than the weights collection");
95 }
96 return _values[keys()[index]];
97 }
98 const double& operator [] (size_t index) const {
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 }
104
106 unsigned int size() const {
107 return _values.size();
108 }
109
111 std::vector<std::string> keys() const {
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 }
119
121 std::vector<double> values() const {
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 }
129
131
132
135
137 Weights& operator += (const Weights& toAdd) {
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 }
147
149 Weights& operator -= (const Weights& toSubtract) {
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 }
159
161 Weights& operator *= (const Weights& toMultiplyBy) {
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 }
171
173 Weights& operator /= (const Weights& toDivideBy) {
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 }
183
185 Weights& operator *= (double toMultiplyBy) {
186 for (size_t i = 0; i < size(); ++i) {
187 _values[keys()[i]] *= toMultiplyBy;
188 }
189 return *this;
190 }
191
193 Weights& operator /= (double toDivideBy) {
194 for (size_t i = 0; i < size(); ++i) {
195 _values[keys()[i]] /= toDivideBy;
196 }
197 return *this;
198 }
199
203 Weights rtn = *this;
204 rtn *= -1;
205 return rtn;
206 }
207
209
210
213
215 bool operator == (const Weights& other) const {
216 return this->_values == other._values;
217 }
218
220 bool operator != (const Weights& other) const {
221 return !(*this == other);
222 }
223
225
226
228 // double operator (double) () {}
229
230 private:
231
233 void _initToMatch(const Weights& other) {
234 if (keys().empty()) {
235 throw LogicError("Weights::_initToMatch shouldn't ever be called if there are already defined weights keys");
236 }
237 for (size_t i = 0; i < other.size(); ++i) {
238 _values[other.keys()[i]] = 0;
239 }
240 }
241
242
243 private:
244
245 std::map<std::string, double> _values;
246
247 };
248
249
252
254 inline Weights operator + (const Weights& first, const Weights& second) {
255 Weights tmp = first;
256 tmp += second;
257 return tmp;
258 }
259
261 inline Weights operator - (const Weights& first, const Weights& second) {
262 Weights tmp = first;
263 tmp -= second;
264 return tmp;
265 }
266
268 inline Weights operator * (const Weights& first, const Weights& second) {
269 Weights tmp = first;
270 tmp *= second;
271 return tmp;
272 }
273
275 inline Weights operator / (const Weights& numer, const Weights& denom) {
276 Weights tmp = numer;
277 tmp /= denom;
278 return tmp;
279 }
280
281
283 inline Weights operator * (double a, const Weights& w) {
284 Weights tmp = w;
285 tmp *= a;
286 return tmp;
287 }
288
290 inline Weights operator * (const Weights& w, double a) {
291 return a * w;
292 }
293
295 inline Weights operator / (const Weights& w, double a) {
296 Weights tmp = w;
297 tmp /= a;
298 return tmp;
299 }
300
303 inline Weights operator / (double a, const Weights& w) {
304 Weights tmp(w.keys(), a);
305 tmp /= w;
306 return tmp;
307 }
308
310
311
313 inline std::ostream& operator<<(std::ostream& out, const Weights& w) {
314 out << "{ ";
315 for (Weights::const_iterator i = w.begin(); i != w.end(); ++i) {
316 if (i != w.begin()) out << ", ";
317 out << i->first << ": " << i->second;
318 }
319 out << "}";
320 return out;
321 }
322
323
324}
325
326#endif
Error for places where it should not have been possible to get to!
Definition Exceptions.h:55
Errors relating to event/bin weights.
Definition Exceptions.h:65
A named, vectorised generalisation of an event weight.
Definition Weights.h:22
std::vector< double > values() const
List of weight values, in the order of the sorted keys.
Definition Weights.h:121
Weights operator-() const
Definition Weights.h:202
std::map< std::string, double >::const_iterator const_iterator
Definition Weights.h:72
Weights & operator-=(const Weights &toSubtract)
Subtract another weights from this.
Definition Weights.h:149
std::vector< std::string > keys() const
Sorted list of weight keys.
Definition Weights.h:111
bool operator==(const Weights &other) const
Equals.
Definition Weights.h:215
Weights(const std::vector< std::string > &keys, double value=0.0)
Constructor from vectors of keys and a single value, defaulting to 0.0.
Definition Weights.h:55
Weights & operator*=(const Weights &toMultiplyBy)
Multiply by another weights.
Definition Weights.h:161
Weights(const Weights &other)
Definition Weights.h:28
double & operator[](const std::string &key)
Definition Weights.h:78
const_iterator end() const
Definition Weights.h:76
Weights(const std::vector< std::string > &keys, const std::vector< double > &values)
Constructor from vectors of keys and values.
Definition Weights.h:45
bool operator!=(const Weights &other) const
Not equals.
Definition Weights.h:220
Weights & operator/=(const Weights &toDivideBy)
Divide by another weights.
Definition Weights.h:173
iterator end()
Definition Weights.h:75
Weights & operator+=(const Weights &toAdd)
Add another weights to this.
Definition Weights.h:137
std::map< std::string, double >::iterator iterator
Definition Weights.h:71
iterator begin()
Definition Weights.h:73
Weights(const std::vector< std::pair< std::string, double > > &keys_values)
Constructor from a vector of key/value pairs.
Definition Weights.h:38
unsigned int size() const
Number of weights keys.
Definition Weights.h:106
Weights(double value)
Convenience auto-constructor from a single double, since that's the commonest use case.
Definition Weights.h:33
const_iterator begin() const
Definition Weights.h:74
Anonymous namespace to limit visibility.
BinnedDbn< DbnN, AxisT... > operator+(BinnedDbn< DbnN, AxisT... > first, BinnedDbn< DbnN, AxisT... > &&second)
Add two BinnedDbn objects.
Definition BinnedDbn.h:1058
BinnedEstimate< AxisT... > operator/(const BinnedDbn< DbnN, AxisT... > &numer, const BinnedDbn< DbnN, AxisT... > &denom)
Definition BinnedDbn.h:1131
std::ostream & operator<<(std::ostream &out, const Weights &w)
Standard text representaion.
Definition Weights.h:313
Weights operator*(const Weights &first, const Weights &second)
Multiply two weights.
Definition Weights.h:268
BinnedDbn< DbnN, AxisT... > operator-(BinnedDbn< DbnN, AxisT... > first, BinnedDbn< DbnN, AxisT... > &&second)
Subtract one BinnedDbn object from another.
Definition BinnedDbn.h:1074