yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
Point2D.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_POINT2D_H
7 #define YODA_POINT2D_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 Point2D : public Point {
19  public:
20 
22 
23 
24  // Default constructor
25  Point2D() { }
26 
27 
29  Point2D(double x, double y, double ex=0.0, double ey=0.0, std::string source="")
30  : _x(x), _y(y)
31  {
32  _ex = std::make_pair(ex, ex);
33  _ey[source] = std::make_pair(ey, ey);
34  }
35 
36 
38  Point2D(double x, double y,
39  double exminus,
40  double explus,
41  double eyminus,
42  double eyplus, std::string source="")
43  : _x(x), _y(y)
44  {
45  _ex = std::make_pair(exminus, explus);
46  _ey[source] = std::make_pair(eyminus, eyplus);
47  }
48 
49 
50  // /// Constructor from values with symmetric errors on x and asymmetric errors on y
51  // Point2D(double x, double y, double ex, const std::pair<double,double>& ey)
52  // : _x(x), _y(y), _ey(ey)
53  // {
54  // _ex = std::make_pair(ex, ex);
55  // }
56 
57 
58  // /// Constructor from values with asymmetric errors on x and symmetric errors on y
59  // Point2D(double x, double y, const std::pair<double,double>& ex, double ey)
60  // : _x(x), _y(y), _ex(ex)
61  // {
62  // _ey = std::make_pair(ey, ey);
63  // }
64 
65 
67  Point2D(double x, double y, const std::pair<double,double>& ex, const std::pair<double,double>& ey, std::string source="")
68  : _x(x), _y(y)
69  {
70  _ex = ex;
71  _ey[source] = ey;
72  }
73 
74 
76  Point2D(const Point2D& p)
77  : _x(p._x), _y(p._y)
78  {
79  _ex = p._ex;
80  _ey = p._ey;
81  }
82 
83 
86  _x = p._x;
87  _y = p._y;
88  _ex = p._ex;
89  _ey = p._ey;
90  return *this;
91  }
92 
94 
95 
96  public:
97 
99  size_t dim() { return 2; }
100 
101 
103 
104 
106  double x() const { return _x; }
107 
109  void setX(double x) { _x = x; }
110 
112  double y() const { return _y; }
113 
115  void setY(double y) { _y = y; }
116 
118 
120  std::pair<double,double> xy() const { return std::make_pair(_x, _y); }
121 
123  void setXY(double x, double y) { setX(x); setY(y); }
124 
126  void setXY(const std::pair<double,double>& xy) { setX(xy.first); setY(xy.second); }
127 
129 
130 
132 
133 
134 
136  const std::pair<double,double>& xErrs() const {
137  return _ex;
138  }
139 
141  double xErrMinus() const {
142  return _ex.first;
143  }
144 
146  double xErrPlus() const {
147  return _ex.second;
148 
149  }
150 
152  double xErrAvg() const {
153  return (_ex.first + _ex.second)/2.0;
154  }
155 
157  void setXErrMinus(double exminus) {
158  _ex.first = exminus;
159  }
160 
162  void setXErrPlus(double explus) {
163  _ex.second = explus;
164  }
165 
167  void setXErr(double ex) {
168  setXErrMinus(ex);
169  setXErrPlus(ex);
170  }
171 
173  void setXErrs(double ex) {
174  setXErr(ex);
175  }
176 
178  void setXErrs(double exminus, double explus) {
179  setXErrMinus(exminus);
180  setXErrPlus(explus);
181  }
182 
184  void setXErrs(const std::pair<double,double>& ex) {
185  _ex = ex;
186  }
187 
192  double xMin() const {
193  return _x - _ex.first;
194  }
195 
200  double xMax() const {
201  return _x + _ex.second;
202  }
203 
205 
206 
208 
209 
211  const std::pair<double,double>& yErrs(std::string source="") const {
212  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
213  return _ey.at(source);
214  }
215 
217  double yErrMinus(std::string source="") const {
218  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
219  return _ey.at(source).first;
220  }
221 
223  double yErrPlus(std::string source="") const {
224  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
225  return _ey.at(source).second;
226  }
227 
229  double yErrAvg(std::string source="") const {
230  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
231  return (_ey.at(source).first + _ey.at(source).second)/2.0;
232  }
233 
235  void setYErrMinus(double eyminus, std::string source="") {
236  if (!_ey.count(source)) _ey[source] = std::make_pair(0.,0.);
237  _ey.at(source).first = eyminus;
238  }
239 
241  void setYErrPlus(double eyplus, std::string source="") {
242  if (!_ey.count(source)) _ey[source] = std::make_pair(0.,0.);
243  _ey.at(source).second = eyplus;
244  }
245 
247  void setYErr(double ey, std::string source="") {
248  setYErrMinus(ey, source );
249  setYErrPlus(ey, source );
250  }
251 
253  void setYErrs(double ey, std::string source="") {
254  setYErr(ey, source);
255  }
256 
258  void setYErrs(double eyminus, double eyplus, std::string source="") {
259  setYErrMinus(eyminus, source);
260  setYErrPlus(eyplus, source );
261  }
262 
264  void setYErrs(const std::pair<double,double>& ey, std::string source="") {
265  _ey[source] = ey;
266  }
267 
269  double yMin(std::string source="") const {
270  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
271  return _y - _ey.at(source).first;
272  }
273 
275  double yMax(std::string source="") const {
276  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
277  return _y + _ey.at(source).second;
278  }
279 
281 
282 
283 
284 
286 
287 
289  void setX(double x, double ex) {
290  setX(x);
291  setXErrs(ex);
292  }
293 
295  void setX(double x, double exminus, double explus) {
296  setX(x);
297  setXErrs(exminus, explus);
298  }
299 
301  void setX(double x, std::pair<double,double>& ex) {
302  setX(x);
303  setXErrs(ex);
304  }
305 
306 
308  void setY(double y, double ey, std::string source="") {
309  setY(y);
310  setYErrs(ey, source);
311  }
312 
314  void setY(double y, double eyminus, double eyplus, std::string source="") {
315  setY(y);
316  setYErrs(eyminus, eyplus, source);
317  }
318 
320  void setY(double y, std::pair<double,double>& ey, std::string source="") {
321  setY(y);
322  setYErrs(ey, source);
323  }
324 
326 
327 
328  // @name Manipulations
330 
332  void scaleX(double scalex) {
333  setX(x()*scalex);
334  setXErrs(xErrMinus()*scalex, xErrPlus()*scalex);
335  }
336 
338  void scaleY(double scaley) {
339  setY(y()*scaley);
340  for (const auto &source : _ey){
341  setYErrs(yErrMinus()*scaley, yErrPlus()*scaley, source.first);
342  }
343  }
344 
346  void scaleXY(double scalex, double scaley) {
347  scaleX(scalex);
348  scaleY(scaley);
349  }
350 
353  void scale(double scalex, double scaley) {
354  scaleXY(scalex, scaley);
355  }
356 
358 
359 
361 
362 
364  double val(size_t i) const {
365  switch (i) {
366  case 1: return x();
367  case 2: return y();
368  default: throw RangeError("Invalid axis int, must be in range 1..dim");
369  }
370  }
372  void setVal(size_t i, double val) {
373  switch (i) {
374  case 1: setX(val); break;
375  case 2: setY(val); break;
376  default: throw RangeError("Invalid axis int, must be in range 1..dim");
377  }
378  }
379 
381  const std::map< std::string, std::pair<double,double>> & errMap() const {
382  return _ey;
383  }
384 
386  const std::pair<double,double>& errs(size_t i, std::string source="") const {
387  switch (i) {
388  case 1: return xErrs();
389  case 2: return yErrs(source);
390  default: throw RangeError("Invalid axis int, must be in range 1..dim");
391  }
392  }
394  double errMinus(size_t i, std::string source="") const {
395  switch (i) {
396  case 1: return xErrMinus();
397  case 2: return yErrMinus(source);
398  default: throw RangeError("Invalid axis int, must be in range 1..dim");
399  }
400  }
402  double errPlus(size_t i, std::string source="") const {
403  switch (i) {
404  case 1: return xErrPlus();
405  case 2: return yErrPlus(source);
406  default: throw RangeError("Invalid axis int, must be in range 1..dim");
407  }
408  }
410  double errAvg(size_t i, std::string source="") const {
411  switch (i) {
412  case 1: return xErrAvg();
413  case 2: return yErrAvg(source);
414  default: throw RangeError("Invalid axis int, must be in range 1..dim");
415  }
416  }
417 
419  void setErrMinus(size_t i, double eminus, std::string source="") {
420  switch (i) {
421  case 1: setXErrMinus(eminus); break;
422  case 2: setYErrMinus(eminus, source); break;
423  default: throw RangeError("Invalid axis int, must be in range 1..dim");
424  }
425  }
427  void setErrPlus(size_t i, double eplus, std::string source="") {
428  switch (i) {
429  case 1: setXErrPlus(eplus); break;
430  case 2: setYErrPlus(eplus, source); break;
431  default: throw RangeError("Invalid axis int, must be in range 1..dim");
432  }
433  }
434 
436  void setErr(size_t i, double e, std::string source="") {
437  switch (i) {
438  case 1: setXErrs(e); break;
439  case 2: setYErrs(e, source); break;
440  default: throw RangeError("Invalid axis int, must be in range 1..dim");
441  }
442  }
444  void setErrs(size_t i, double eminus, double eplus, std::string source="") {
445  switch (i) {
446  case 1: setXErrs(eminus, eplus); break;
447  case 2: setYErrs(eminus, eplus, source); break;
448  default: throw RangeError("Invalid axis int, must be in range 1..dim");
449  }
450  }
452  void setErrs(size_t i, std::pair<double,double>& e, std::string source="") {
453  switch (i) {
454  case 1: setXErrs(e); break;
455  case 2: setYErrs(e, source); break;
456  default: throw RangeError("Invalid axis int, must be in range 1..dim");
457  }
458  }
459 
461  void set(size_t i, double val, double e, std::string source="") {
462  switch (i) {
463  case 1: setX(val, e); break;
464  case 2: setY(val, e, source); break;
465  default: throw RangeError("Invalid axis int, must be in range 1..dim");
466  }
467  }
469  void set(size_t i, double val, double eminus, double eplus, std::string source="") {
470  switch (i) {
471  case 1: setX(val, eminus, eplus); break;
472  case 2: setY(val, eminus, eplus, source); break;
473  default: throw RangeError("Invalid axis int, must be in range 1..dim");
474  }
475  }
477  void set(size_t i, double val, std::pair<double,double>& e, std::string source="") {
478  switch (i) {
479  case 1: setX(val, e); break;
480  case 2: setY(val, e, source); break;
481  default: throw RangeError("Invalid axis int, must be in range 1..dim");
482  }
483  }
484 
486 
487 
488  protected:
489 
491 
492 
493  double _x;
494  double _y;
495  std::pair<double,double> _ex;
496  // a map of the errors for each source. Nominal stored under ""
497  // to ensure backward compatibility
498  std::map< std::string, std::pair<double,double> > _ey;
499 
501 
502  };
503 
504 
505 
507 
508 
511  inline bool operator==(const YODA::Point2D& a, const YODA::Point2D& b) {
512  const bool same_val = YODA::fuzzyEquals(a.x(), b.x());
513  const bool same_eminus = YODA::fuzzyEquals(a.xErrMinus(), b.xErrMinus());
514  const bool same_eplus = YODA::fuzzyEquals(a.xErrPlus(), b.xErrPlus());
515  return same_val && same_eminus && same_eplus;
516  }
517 
519  inline bool operator != (const YODA::Point2D& a, const YODA::Point2D& b) {
520  return !(a == b);
521  }
522 
524  inline bool operator < (const YODA::Point2D& a, const YODA::Point2D& b) {
525  if (!YODA::fuzzyEquals(a.x(), b.x())) {
526  return a.x() < b.x();
527  }
528  if (!YODA::fuzzyEquals(a.xErrMinus(), b.xErrMinus())) {
529  return a.xErrMinus() < b.xErrMinus();
530  }
531  if (!YODA::fuzzyEquals(a.xErrPlus(), b.xErrPlus())) {
532  return a.xErrPlus() < b.xErrPlus();
533  }
534  return false;
535  }
536 
538  inline bool operator <= (const YODA::Point2D& a, const YODA::Point2D& b) {
539  if (a == b) return true;
540  return a < b;
541  }
542 
544  inline bool operator > (const YODA::Point2D& a, const YODA::Point2D& b) {
545  return !(a <= b);
546  }
547 
549  inline bool operator >= (const YODA::Point2D& a, const YODA::Point2D& b) {
550  return !(a < b);
551  }
552 
554 
555 
556 }
557 
558 #endif
void setY(double y)
Set y value.
Definition: Point2D.h:115
void setYErrs(const std::pair< double, double > &ey, std::string source="")
Set asymmetric y error.
Definition: Point2D.h:264
Point2D(const Point2D &p)
Copy constructor.
Definition: Point2D.h:76
void setX(double x, double ex)
Set x value and symmetric error.
Definition: Point2D.h:289
void setYErrs(double ey, std::string source="")
Set symmetric y error (alias)
Definition: Point2D.h:253
double x() const
Get x value.
Definition: Point2D.h:106
void scaleX(double scalex)
Scaling of x axis.
Definition: Point2D.h:332
Base class for all Point*Ds, providing generic access to their numerical properties.
Definition: Point.h:15
void setYErr(double ey, std::string source="")
Set symmetric y error.
Definition: Point2D.h:247
std::pair< double, double > xy() const
Get x,y value pair.
Definition: Point2D.h:120
void setX(double x, double exminus, double explus)
Set x value and asymmetric error.
Definition: Point2D.h:295
void setYErrPlus(double eyplus, std::string source="")
Set positive y error.
Definition: Point2D.h:241
void setX(double x, std::pair< double, double > &ex)
Set x value and asymmetric error.
Definition: Point2D.h:301
double y() const
Get y value.
Definition: Point2D.h:112
void setVal(size_t i, double val)
Set the point value for direction i.
Definition: Point2D.h:372
void setXErr(double ex)
Set symmetric x error.
Definition: Point2D.h:167
void setXErrs(double exminus, double explus)
Set asymmetric x error.
Definition: Point2D.h:178
const std::pair< double, double > & xErrs() const
Get x-error values.
Definition: Point2D.h:136
double xMax() const
Definition: Point2D.h:200
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
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 setYErrs(double eyminus, double eyplus, std::string source="")
Set asymmetric y error.
Definition: Point2D.h:258
Point2D(double x, double y, double exminus, double explus, double eyminus, double eyplus, std::string source="")
Constructor from values with explicit asymmetric errors.
Definition: Point2D.h:38
double yErrPlus(std::string source="") const
Get positive y-error value.
Definition: Point2D.h:223
void setXErrPlus(double explus)
Set positive x error.
Definition: Point2D.h:162
bool operator<(const Bin1D< DBN > &a, const Bin1D< DBN > &b)
Bin1Ds are compared for axis sorting by lower edge position.
Definition: Bin1D.h:347
double xErrAvg() const
Get average x-error value.
Definition: Point2D.h:152
void setXErrs(double ex)
Set symmetric x error (alias)
Definition: Point2D.h:173
double yErrMinus(std::string source="") const
Get negative y-error value.
Definition: Point2D.h:217
const std::pair< double, double > & errs(size_t i, std::string source="") const
Get error values for direction i.
Definition: Point2D.h:386
void setXY(const std::pair< double, double > &xy)
Set x and y values.
Definition: Point2D.h:126
size_t dim()
Space dimension of the point.
Definition: Point2D.h:99
bool operator>(const Error< N > &a, const Error< N > &b)
Greater-than operator used to sort errors.
Definition: ErrorND.h:206
Point2D(double x, double y, const std::pair< double, double > &ex, const std::pair< double, double > &ey, std::string source="")
Constructor from values with asymmetric errors on both x and y.
Definition: Point2D.h:67
double yMax(std::string source="") const
Get value plus positive y-error.
Definition: Point2D.h:275
double yErrAvg(std::string source="") const
Get average y-error value.
Definition: Point2D.h:229
double val(size_t i) const
Get the point value for direction i.
Definition: Point2D.h:364
double xMin() const
Definition: Point2D.h:192
void setYErrMinus(double eyminus, std::string source="")
Set negative y error.
Definition: Point2D.h:235
void setXY(double x, double y)
Set x and y values.
Definition: Point2D.h:123
void setXErrs(const std::pair< double, double > &ex)
Set asymmetric x error.
Definition: Point2D.h:184
void scaleY(double scaley)
Scaling of y axis.
Definition: Point2D.h:338
Point2D(double x, double y, double ex=0.0, double ey=0.0, std::string source="")
Constructor from values with optional symmetric errors.
Definition: Point2D.h:29
double errAvg(size_t i, std::string source="") const
Get average error value for direction i.
Definition: Point2D.h:410
bool operator!=(const Error< N > &a, const Error< N > &b)
Inequality test.
Definition: ErrorND.h:180
void setErrMinus(size_t i, double eminus, std::string source="")
Set negative error for direction i.
Definition: Point2D.h:419
void setErr(size_t i, double e, std::string source="")
Set symmetric error for direction i.
Definition: Point2D.h:436
void setY(double y, double eyminus, double eyplus, std::string source="")
Set y value and asymmetric error.
Definition: Point2D.h:314
void setX(double x)
Set x value.
Definition: Point2D.h:109
void setErrs(size_t i, std::pair< double, double > &e, std::string source="")
Set asymmetric error for direction i.
Definition: Point2D.h:452
bool operator>=(const Error< N > &a, const Error< N > &b)
Greater-than-or-equals operator used to sort errors.
Definition: ErrorND.h:212
Point2D & operator=(const Point2D &p)
Copy assignment.
Definition: Point2D.h:85
void scale(double scalex, double scaley)
Definition: Point2D.h:353
void setY(double y, double ey, std::string source="")
Set y value and symmetric error.
Definition: Point2D.h:308
const std::pair< double, double > & yErrs(std::string source="") const
Get y-error values.
Definition: Point2D.h:211
double errMinus(size_t i, std::string source="") const
Get negative error value for direction i.
Definition: Point2D.h:394
void scaleXY(double scalex, double scaley)
Scaling of both axes.
Definition: Point2D.h:346
double yMin(std::string source="") const
Get value minus negative y-error.
Definition: Point2D.h:269
const std::map< std::string, std::pair< double, double > > & errMap() const
Get error map for direction i.
Definition: Point2D.h:381
void setY(double y, std::pair< double, double > &ey, std::string source="")
Set y value and asymmetric error.
Definition: Point2D.h:320
double xErrMinus() const
Get negative x-error value.
Definition: Point2D.h:141
double xErrPlus() const
Get positive x-error value.
Definition: Point2D.h:146
void setErrPlus(size_t i, double eplus, std::string source="")
Set positive error for direction i.
Definition: Point2D.h:427
bool operator<=(const Error< N > &a, const Error< N > &b)
Less-than-or-equals operator used to sort errors.
Definition: ErrorND.h:199
void setXErrMinus(double exminus)
Set negative x error.
Definition: Point2D.h:157
void setErrs(size_t i, double eminus, double eplus, std::string source="")
Set asymmetric error for direction i.
Definition: Point2D.h:444
bool operator==(const Error< N > &a, const Error< N > &b)
Equality test.
Definition: ErrorND.h:169
double errPlus(size_t i, std::string source="") const
Get positive error value for direction i.
Definition: Point2D.h:402
A 2D data point to be contained in a Scatter2D.
Definition: Point2D.h:18