yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
Point1D.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-2017 The YODA collaboration (see AUTHORS for details)
5 //
6 #ifndef YODA_POINT1D_H
7 #define YODA_POINT1D_H
8 
9 #include "YODA/Point.h"
10 #include "YODA/Exceptions.h"
11 #include "YODA/Utils/MathUtils.h"
12 #include <utility>
13 
14 namespace YODA {
15 
16 
18  class Point1D : public Point {
19  public:
20 
22 
23 
24  // Default constructor
25  Point1D() { }
26 
27 
29  Point1D(double x, double ex=0.0, std::string source="")
30  : _x(x)
31  {
32  _ex[source] = std::make_pair(ex, ex);
33  }
34 
35 
37  Point1D(double x, double exminus, double explus, std::string source="")
38  : _x(x)
39  {
40  _ex[source] = std::make_pair(exminus, explus);
41  }
42 
43 
45  Point1D(double x, const std::pair<double,double>& ex, std::string source="")
46  : _x(x)
47  {
48  _ex[source] = ex;
49  }
50 
51 
53  Point1D(const Point1D& p)
54  : _x(p._x), _ex(p._ex)
55  { }
56 
57 
60  _x = p._x;
61  _ex = p._ex;
62  return *this;
63  }
64 
66 
67 
68  public:
69 
71  size_t dim() { return 1; }
72 
73 
75 
76 
78  double x() const { return _x; }
79 
81  void setX(double x) { _x = x; }
82 
84 
86 
87 
89 
90 
92  const std::pair<double,double>& xErrs( std::string source="") const {
93  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
94  return _ex.at(source);
95  }
96 
98  double xErrMinus( std::string source="") const {
99  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
100  return _ex.at(source).first;
101  }
102 
104  double xErrPlus( std::string source="") const {
105  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
106  return _ex.at(source).second;
107  }
108 
110  double xErrAvg( std::string source="") const {
111  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
112  return (_ex.at(source).first + _ex.at(source).second)/2.0;
113  }
114 
116  void setXErrMinus(double exminus, std::string source="") {
117  if (!_ex.count(source)) _ex[source] = std::make_pair(0.,0.);
118  _ex.at(source).first = exminus;
119  }
120 
122  void setXErrPlus(double explus, std::string source="") {
123  if (!_ex.count(source)) _ex[source] = std::make_pair(0.,0.);
124  _ex.at(source).second = explus;
125  }
126 
128  void setXErr(double ex, std::string source="") {
129  setXErrMinus(ex, source);
130  setXErrPlus(ex, source);
131  }
132 
134  void setXErrs(double ex, std::string source="") {
135  setXErr(ex, source);
136  }
137 
139  void setXErrs(double exminus, double explus, std::string source="") {
140  setXErrMinus(exminus, source);
141  setXErrPlus(explus, source);
142  }
143 
145  void setXErrs(const std::pair<double,double>& ex, std::string source="") {
146  _ex[source] = ex;
147  }
148 
150  double xMin(std::string source="") const {
151  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
152  return _x - _ex.at(source).first;
153  }
154 
156  double xMax(std::string source="") const {
157  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
158  return _x + _ex.at(source).second;
159  }
160 
162 
163 
165 
166 
168  void setX(double x, double ex, std::string source="") {
169  setX(x);
170  setXErr(ex, source);
171  }
172 
174  void setX(double x, double exminus, double explus, std::string source="") {
175  setX(x);
176  setXErrs(exminus, explus, source);
177  }
178 
180  void setX(double x, std::pair<double,double>& ex, std::string source="") {
181  setX(x);
182  setXErrs(ex, source);
183  }
184 
186 
187 
188  // @name Manipulations
190 
192  void scaleX(double scalex) {
193  setX(x()*scalex);
194  for (const auto &source : _ex){
195  setXErrs(xErrMinus()*scalex, xErrPlus()*scalex, source.first);
196  }
197  }
198 
200 
201 
203 
204 
206  double val(size_t i) const {
207  if (i == 0 || i > 1) throw RangeError("Invalid axis int, must be in range 1..dim");
208  return x();
209  }
211  void setVal(size_t i, double val) {
212  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
213  setX(val);
214  }
215 
217  const std::map< std::string, std::pair<double,double>> & errMap() const {
218  return _ex;
219  }
220 
222  const std::pair<double,double>& errs(size_t i, std::string source="") const {
223  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
224  return xErrs(source);
225  }
227  double errMinus(size_t i, std::string source="") const {
228  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
229  return xErrMinus(source);
230  }
232  double errPlus(size_t i, std::string source="") const {
233  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
234  return xErrPlus(source);
235  }
237  double errAvg(size_t i, std::string source="") const {
238  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
239  return xErrAvg(source);
240  }
241 
243  void setErrMinus(size_t i, double eminus, std::string source="") {
244  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
245  setXErrMinus(eminus, source);
246  }
248  void setErrPlus(size_t i, double eplus, std::string source="") {
249  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
250  setXErrPlus(eplus, source);
251  }
252 
254  void setErr(size_t i, double e, std::string source="") {
255  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
256  setXErr(e, source);
257  }
259  void setErrs(size_t i, double eminus, double eplus, std::string source="") {
260  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
261  setXErrs(eminus, eplus, source);
262  }
264  void setErrs(size_t i, std::pair<double,double>& e, std::string source="") {
265  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
266  setXErrs(e, source);
267  }
268 
270  void set(size_t i, double val, double e, std::string source="") {
271  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
272  setX(val, e, source);
273  }
275  void set(size_t i, double val, double eminus, double eplus, std::string source="") {
276  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
277  setX(val, eminus, eplus, source);
278  }
280  void set(size_t i, double val, std::pair<double,double>& e, std::string source="") {
281  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
282  setX(val, e, source);
283  }
284 
286 
287 
288  protected:
289 
291 
292 
293  double _x;
294  // a map of the errors for each source. Nominal stored under ""
295  // to ensure backward compatibility
296  std::map< std::string, std::pair<double,double> > _ex;
297 
299 
300  };
301 
302 
303 
305 
306 
308  inline bool operator==(const YODA::Point1D& a, const YODA::Point1D& b) {
309  const bool same_val = YODA::fuzzyEquals(a.x(), b.x());
310  const bool same_eminus = YODA::fuzzyEquals(a.xErrMinus(), b.xErrMinus());
311  const bool same_eplus = YODA::fuzzyEquals(a.xErrPlus(), b.xErrPlus());
312  return same_val && same_eminus && same_eplus;
313  }
314 
316  inline bool operator != (const YODA::Point1D& a, const YODA::Point1D& b) {
317  return !(a == b);
318  }
319 
321  inline bool operator < (const YODA::Point1D& a, const YODA::Point1D& b) {
322  if (!YODA::fuzzyEquals(a.x(), b.x())) {
323  return a.x() < b.x();
324  }
325  if (!YODA::fuzzyEquals(a.xErrMinus(), b.xErrMinus())) {
326  return a.xErrMinus() < b.xErrMinus();
327  }
328  if (!YODA::fuzzyEquals(a.xErrPlus(), b.xErrPlus())) {
329  return a.xErrPlus() < b.xErrPlus();
330  }
331  return false;
332  }
333 
335  inline bool operator <= (const YODA::Point1D& a, const YODA::Point1D& b) {
336  if (a == b) return true;
337  return a < b;
338  }
339 
341  inline bool operator > (const YODA::Point1D& a, const YODA::Point1D& b) {
342  return !(a <= b);
343  }
344 
346  inline bool operator >= (const YODA::Point1D& a, const YODA::Point1D& b) {
347  return !(a < b);
348  }
349 
351 
352 
353 }
354 
355 #endif
double x() const
Get x value.
Definition: Point1D.h:78
double xErrAvg(std::string source="") const
Get average x-error value.
Definition: Point1D.h:110
Base class for all Point*Ds, providing generic access to their numerical properties.
Definition: Point.h:15
Point1D(const Point1D &p)
Copy constructor.
Definition: Point1D.h:53
double val(size_t i) const
Get the point value for direction i.
Definition: Point1D.h:206
const std::pair< double, double > & xErrs(std::string source="") const
Get x-error values.
Definition: Point1D.h:92
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
double xErrMinus(std::string source="") const
Get negative x-error value.
Definition: Point1D.h:98
double xMax(std::string source="") const
Get value plus positive x-error.
Definition: Point1D.h:156
bool fuzzyEquals(double a, double b, double tolerance=1E-5)
Compare two floating point numbers for equality with a degree of fuzziness.
Definition: MathUtils.h:72
void setX(double x, std::pair< double, double > &ex, std::string source="")
Set x value and asymmetric error.
Definition: Point1D.h:180
void setErrs(size_t i, double eminus, double eplus, std::string source="")
Set asymmetric error for direction i.
Definition: Point1D.h:259
double errAvg(size_t i, std::string source="") const
Get average error value for direction i.
Definition: Point1D.h:237
Point1D(double x, const std::pair< double, double > &ex, std::string source="")
Constructor from values with asymmetric errors.
Definition: Point1D.h:45
double xMin(std::string source="") const
Get value minus negative x-error.
Definition: Point1D.h:150
bool operator<(const Bin1D< DBN > &a, const Bin1D< DBN > &b)
Bin1Ds are compared for axis sorting by lower edge position.
Definition: Bin1D.h:347
void setXErrMinus(double exminus, std::string source="")
Set negative x error.
Definition: Point1D.h:116
bool operator>(const Error< N > &a, const Error< N > &b)
Greater-than operator used to sort errors.
Definition: ErrorND.h:206
void setErrPlus(size_t i, double eplus, std::string source="")
Set positive error for direction i.
Definition: Point1D.h:248
void setX(double x, double ex, std::string source="")
Set x value and symmetric error.
Definition: Point1D.h:168
void setXErrPlus(double explus, std::string source="")
Set positive x error.
Definition: Point1D.h:122
A 1D data point to be contained in a Scatter1D.
Definition: Point1D.h:18
void setXErr(double ex, std::string source="")
Set symmetric x error.
Definition: Point1D.h:128
double errMinus(size_t i, std::string source="") const
Get negative error value for direction i.
Definition: Point1D.h:227
void setXErrs(double exminus, double explus, std::string source="")
Set asymmetric x error.
Definition: Point1D.h:139
double errPlus(size_t i, std::string source="") const
Get positive error value for direction i.
Definition: Point1D.h:232
void setErrMinus(size_t i, double eminus, std::string source="")
Set negative error for direction i.
Definition: Point1D.h:243
bool operator!=(const Error< N > &a, const Error< N > &b)
Inequality test.
Definition: ErrorND.h:180
void scaleX(double scalex)
Scaling of x axis.
Definition: Point1D.h:192
void setVal(size_t i, double val)
Set the point value for direction i.
Definition: Point1D.h:211
Point1D & operator=(const Point1D &p)
Copy assignment.
Definition: Point1D.h:59
const std::map< std::string, std::pair< double, double > > & errMap() const
Get error map for direction i.
Definition: Point1D.h:217
Point1D(double x, double ex=0.0, std::string source="")
Constructor from values with optional symmetric errors.
Definition: Point1D.h:29
bool operator>=(const Error< N > &a, const Error< N > &b)
Greater-than-or-equals operator used to sort errors.
Definition: ErrorND.h:212
Point1D(double x, double exminus, double explus, std::string source="")
Constructor from values with explicit asymmetric errors.
Definition: Point1D.h:37
void setX(double x, double exminus, double explus, std::string source="")
Set x value and asymmetric error.
Definition: Point1D.h:174
void setXErrs(double ex, std::string source="")
Set symmetric x error (alias)
Definition: Point1D.h:134
double xErrPlus(std::string source="") const
Get positive x-error value.
Definition: Point1D.h:104
void setErrs(size_t i, std::pair< double, double > &e, std::string source="")
Set asymmetric error for direction i.
Definition: Point1D.h:264
const std::pair< double, double > & errs(size_t i, std::string source="") const
Get error values for direction i.
Definition: Point1D.h:222
void setXErrs(const std::pair< double, double > &ex, std::string source="")
Set asymmetric x error.
Definition: Point1D.h:145
void setX(double x)
Set x value.
Definition: Point1D.h:81
bool operator<=(const Error< N > &a, const Error< N > &b)
Less-than-or-equals operator used to sort errors.
Definition: ErrorND.h:199
size_t dim()
Space dimension of the point.
Definition: Point1D.h:71
void setErr(size_t i, double e, std::string source="")
Set symmetric error for direction i.
Definition: Point1D.h:254
bool operator==(const Error< N > &a, const Error< N > &b)
Equality test.
Definition: ErrorND.h:169