yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
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-2021 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 
15 namespace YODA {
16 
17 
19  class Point2D : public Point {
20  public:
21 
24 
25  // Default constructor
26  Point2D() { }
27 
28 
30  Point2D(double x, double y, double ex=0.0, double ey=0.0, std::string source="")
31  : _x(x), _y(y)
32  {
33  _ex = std::make_pair(ex, ex);
34  _ey[source] = std::make_pair(ey, ey);
35  }
36 
37 
39  Point2D(double x, double y,
40  double exminus,
41  double explus,
42  double eyminus,
43  double eyplus, std::string source="")
44  : _x(x), _y(y)
45  {
46  _ex = std::make_pair(exminus, explus);
47  _ey[source] = std::make_pair(eyminus, eyplus);
48  }
49 
50 
51  // /// Constructor from values with symmetric errors on x and asymmetric errors on y
52  // Point2D(double x, double y, double ex, const std::pair<double,double>& ey)
53  // : _x(x), _y(y), _ey(ey)
54  // {
55  // _ex = std::make_pair(ex, ex);
56  // }
57 
58 
59  // /// Constructor from values with asymmetric errors on x and symmetric errors on y
60  // Point2D(double x, double y, const std::pair<double,double>& ex, double ey)
61  // : _x(x), _y(y), _ex(ex)
62  // {
63  // _ey = std::make_pair(ey, ey);
64  // }
65 
66 
68  Point2D(double x, double y, const std::pair<double,double>& ex, const std::pair<double,double>& ey, std::string source="")
69  : _x(x), _y(y)
70  {
71  _ex = ex;
72  _ey[source] = ey;
73  }
74 
75 
77  Point2D(const Point2D& p)
78  : _x(p._x), _y(p._y)
79  {
80  _ex = p._ex;
81  _ey = p._ey;
82  this->setParent( p.getParent() );
83  }
84 
85 
88  _x = p._x;
89  _y = p._y;
90  _ex = p._ex;
91  _ey = p._ey;
92  this->setParent( p.getParent() );
93  return *this;
94  }
95 
97 
98 
99  public:
100 
102  size_t dim() { return 2; }
103 
104 
107 
109  double x() const { return _x; }
110 
112  void setX(double x) { _x = x; }
113 
115  double y() const { return _y; }
116 
118  void setY(double y) { _y = y; }
119 
121 
123  std::pair<double,double> xy() const { return std::make_pair(_x, _y); }
124 
126  void setXY(double x, double y) { setX(x); setY(y); }
127 
129  void setXY(const std::pair<double,double>& xy) { setX(xy.first); setY(xy.second); }
130 
132 
133 
136 
137 
139  const std::pair<double,double>& xErrs() const {
140  return _ex;
141  }
142 
144  double xErrMinus() const {
145  return _ex.first;
146  }
147 
149  double xErrPlus() const {
150  return _ex.second;
151 
152  }
153 
155  double xErrAvg() const {
156  return (_ex.first + _ex.second)/2.0;
157  }
158 
160  void setXErrMinus(double exminus) {
161  _ex.first = exminus;
162  }
163 
165  void setXErrPlus(double explus) {
166  _ex.second = explus;
167  }
168 
170  void setXErr(double ex) {
171  setXErrMinus(ex);
172  setXErrPlus(ex);
173  }
174 
176  void setXErrs(double ex) {
177  setXErr(ex);
178  }
179 
181  void setXErrs(double exminus, double explus) {
182  setXErrMinus(exminus);
183  setXErrPlus(explus);
184  }
185 
187  void setXErrs(const std::pair<double,double>& ex) {
188  _ex = ex;
189  }
190 
195  double xMin() const {
196  return _x - _ex.first;
197  }
198 
203  double xMax() const {
204  return _x + _ex.second;
205  }
206 
208 
209 
212 
214  const std::pair<double,double>& yErrs(std::string source="") const {
215  if (source!="") getVariationsFromParent();
216  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
217  return _ey.at(source);
218  }
219 
221  double yErrMinus(std::string source="") const {
222  if (source!="") getVariationsFromParent();
223  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
224  return _ey.at(source).first;
225  }
226 
228  double yErrPlus(std::string source="") const {
229  if (source!="") getVariationsFromParent();
230  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
231  return _ey.at(source).second;
232  }
233 
235  double yErrAvg(std::string source="") const {
236  if (source!="") getVariationsFromParent();
237  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
238  double res=(fabs(_ey.at(source).first) + fabs(_ey.at(source).second))/2.;
239  return res;
240  }
241 
243  void setYErrMinus(double eyminus, std::string source="") {
244  if (!_ey.count(source)) {
245  _ey[source] = std::make_pair(0.,0.);
246  }
247  _ey.at(source).first = eyminus;
248  }
249 
251  void setYErrPlus(double eyplus, std::string source="") {
252  if (!_ey.count(source)) _ey[source] = std::make_pair(0.,0.);
253  _ey.at(source).second = eyplus;
254  }
255 
257  void setYErr(double ey, std::string source="") {
258  setYErrMinus(ey, source );
259  setYErrPlus(ey, source );
260  }
261 
263  void setYErrs(double ey, std::string source="") {
264  setYErr(ey, source);
265  }
266 
268  void setYErrs(double eyminus, double eyplus, std::string source="") {
269  setYErrMinus(eyminus, source);
270  setYErrPlus(eyplus, source );
271  }
272 
274  void setYErrs(const std::pair<double,double>& ey, std::string source="") {
275  _ey[source] = ey;
276  }
277 
279  double yMin(std::string source="") const {
280  if (source!="") getVariationsFromParent();
281  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
282  return _y - _ey.at(source).first;
283  }
284 
286  double yMax(std::string source="") const {
287  if (source!="") getVariationsFromParent();
288  if (!_ey.count(source)) throw RangeError("yErrs has no such key: "+source);
289  return _y + _ey.at(source).second;
290  }
291 
293 
294 
295 
296 
299 
301  void setX(double x, double ex) {
302  setX(x);
303  setXErrs(ex);
304  }
305 
307  void setX(double x, double exminus, double explus) {
308  setX(x);
309  setXErrs(exminus, explus);
310  }
311 
313  void setX(double x, std::pair<double,double>& ex) {
314  setX(x);
315  setXErrs(ex);
316  }
317 
318 
320  void setY(double y, double ey, std::string source="") {
321  setY(y);
322  setYErrs(ey, source);
323  }
324 
326  void setY(double y, double eyminus, double eyplus, std::string source="") {
327  setY(y);
328  setYErrs(eyminus, eyplus, source);
329  }
330 
332  void setY(double y, std::pair<double,double>& ey, std::string source="") {
333  setY(y);
334  setYErrs(ey, source);
335  }
336 
338 
339 
340  // @name Manipulations
342 
344  void scaleX(double scalex) {
345  setX(x()*scalex);
346  setXErrs(xErrMinus()*scalex, xErrPlus()*scalex);
347  }
348 
350  void scaleY(double scaley) {
351  setY(y()*scaley);
352  for (const auto &source : _ey){
353  setYErrs(yErrMinus()*scaley, yErrPlus()*scaley, source.first);
354  }
355  }
356 
358  void scaleXY(double scalex, double scaley) {
359  scaleX(scalex);
360  scaleY(scaley);
361  }
362 
364  void scale(size_t i, double scale) {
365  switch (i) {
366  case 1: scaleX(scale); break;
367  case 2: scaleY(scale); break;
368  default: throw RangeError("Invalid axis int, must be in range 1..dim");
369  }
370  }
371 
373 
374 
377 
379  double val(size_t i) const {
380  switch (i) {
381  case 1: return x();
382  case 2: return y();
383  default: throw RangeError("Invalid axis int, must be in range 1..dim");
384  }
385  }
387  void setVal(size_t i, double val) {
388  switch (i) {
389  case 1: setX(val); break;
390  case 2: setY(val); break;
391  default: throw RangeError("Invalid axis int, must be in range 1..dim");
392  }
393  }
394 
396  const std::map< std::string, std::pair<double,double>> & errMap() const;
397 
398  // Parse the variations from the parent AO if it exists
399  void getVariationsFromParent() const;
400 
402  const std::pair<double,double>& errs(size_t i, std::string source="") const {
403  switch (i) {
404  case 1: return xErrs();
405  case 2: return yErrs(source);
406  default: throw RangeError("Invalid axis int, must be in range 1..dim");
407  }
408  }
410  double errMinus(size_t i, std::string source="") const {
411  switch (i) {
412  case 1: return xErrMinus();
413  case 2: return yErrMinus(source);
414  default: throw RangeError("Invalid axis int, must be in range 1..dim");
415  }
416  }
418  double errPlus(size_t i, std::string source="") const {
419  switch (i) {
420  case 1: return xErrPlus();
421  case 2: return yErrPlus(source);
422  default: throw RangeError("Invalid axis int, must be in range 1..dim");
423  }
424  }
426  double errAvg(size_t i, std::string source="") const {
427  switch (i) {
428  case 1: return xErrAvg();
429  case 2: return yErrAvg(source);
430  default: throw RangeError("Invalid axis int, must be in range 1..dim");
431  }
432  }
433 
435  void setErrMinus(size_t i, double eminus, std::string source="") {
436  switch (i) {
437  case 1: setXErrMinus(eminus); break;
438  case 2: setYErrMinus(eminus, source); break;
439  default: throw RangeError("Invalid axis int, must be in range 1..dim");
440  }
441  }
443  void setErrPlus(size_t i, double eplus, std::string source="") {
444  switch (i) {
445  case 1: setXErrPlus(eplus); break;
446  case 2: setYErrPlus(eplus, source); break;
447  default: throw RangeError("Invalid axis int, must be in range 1..dim");
448  }
449  }
450 
452  void setErr(size_t i, 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  }
460  void setErrs(size_t i, double eminus, double eplus, std::string source="") {
461  switch (i) {
462  case 1: setXErrs(eminus, eplus); break;
463  case 2: setYErrs(eminus, eplus, source); break;
464  default: throw RangeError("Invalid axis int, must be in range 1..dim");
465  }
466  }
468  void setErrs(size_t i, std::pair<double,double>& e, std::string source="") {
469  switch (i) {
470  case 1: setXErrs(e); break;
471  case 2: setYErrs(e, source); break;
472  default: throw RangeError("Invalid axis int, must be in range 1..dim");
473  }
474  }
475 
477  void set(size_t i, double val, 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  }
485  void set(size_t i, double val, double eminus, double eplus, std::string source="") {
486  switch (i) {
487  case 1: setX(val, eminus, eplus); break;
488  case 2: setY(val, eminus, eplus, source); break;
489  default: throw RangeError("Invalid axis int, must be in range 1..dim");
490  }
491  }
493  void set(size_t i, double val, std::pair<double,double>& e, std::string source="") {
494  switch (i) {
495  case 1: setX(val, e); break;
496  case 2: setY(val, e, source); break;
497  default: throw RangeError("Invalid axis int, must be in range 1..dim");
498  }
499  }
500 
501 
503 
504 
505  protected:
506 
509 
510 
511  double _x;
512  double _y;
513  std::pair<double,double> _ex;
514  // a map of the errors for each source. Nominal stored under ""
515  // to ensure backward compatibility
516  std::map< std::string, std::pair<double,double> > _ey;
517 
519 
520  };
521 
522 
523 
526 
529  inline bool operator==(const YODA::Point2D& a, const YODA::Point2D& b) {
530  if (!YODA::fuzzyEquals(a.x(), b.x()) ||
532  !YODA::fuzzyEquals(a.xErrPlus(), b.xErrPlus()) ) return false;
533  if (!YODA::fuzzyEquals(a.y(), b.y()) ||
535  !YODA::fuzzyEquals(a.yErrPlus(), b.yErrPlus()) ) return false;
536  return true;
537  }
538 
540  inline bool operator != (const YODA::Point2D& a, const YODA::Point2D& b) {
541  return !(a == b);
542  }
543 
545  inline bool operator < (const YODA::Point2D& a, const YODA::Point2D& b) {
546  if (!YODA::fuzzyEquals(a.x(), b.x())) {
547  return a.x() < b.x();
548  }
549  if (!YODA::fuzzyEquals(a.xErrMinus(), b.xErrMinus())) {
550  return a.xErrMinus() < b.xErrMinus();
551  }
552  if (!YODA::fuzzyEquals(a.xErrPlus(), b.xErrPlus())) {
553  return a.xErrPlus() < b.xErrPlus();
554  }
555  return false;
556  }
557 
559  inline bool operator <= (const YODA::Point2D& a, const YODA::Point2D& b) {
560  if (a == b) return true;
561  return a < b;
562  }
563 
565  inline bool operator > (const YODA::Point2D& a, const YODA::Point2D& b) {
566  return !(a <= b);
567  }
568 
570  inline bool operator >= (const YODA::Point2D& a, const YODA::Point2D& b) {
571  return !(a < b);
572  }
573 
575 
576 
577 }
578 
579 #endif
void setY(double y)
Set y value.
Definition: Point2D.h:118
void setYErrs(const std::pair< double, double > &ey, std::string source="")
Set asymmetric y error.
Definition: Point2D.h:274
Point2D(const Point2D &p)
Copy constructor.
Definition: Point2D.h:77
void setX(double x, double ex)
Set x value and symmetric error.
Definition: Point2D.h:301
void setYErrs(double ey, std::string source="")
Set symmetric y error (alias)
Definition: Point2D.h:263
double x() const
Get x value.
Definition: Point2D.h:109
void scaleX(double scalex)
Scaling of x axis.
Definition: Point2D.h:344
Base class for all Point*Ds, providing generic access to their numerical properties.
Definition: Point.h:16
void setYErr(double ey, std::string source="")
Set symmetric y error.
Definition: Point2D.h:257
std::pair< double, double > xy() const
Get x,y value pair.
Definition: Point2D.h:123
void setX(double x, double exminus, double explus)
Set x value and asymmetric error.
Definition: Point2D.h:307
void setYErrPlus(double eyplus, std::string source="")
Set positive y error.
Definition: Point2D.h:251
void setX(double x, std::pair< double, double > &ex)
Set x value and asymmetric error.
Definition: Point2D.h:313
double y() const
Get y value.
Definition: Point2D.h:115
void setVal(size_t i, double val)
Set the point value for direction i.
Definition: Point2D.h:387
void setXErr(double ex)
Set symmetric x error.
Definition: Point2D.h:170
void setXErrs(double exminus, double explus)
Set asymmetric x error.
Definition: Point2D.h:181
const std::pair< double, double > & xErrs() const
Get x-error values.
Definition: Point2D.h:139
double xMax() const
Definition: Point2D.h:203
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:73
void setYErrs(double eyminus, double eyplus, std::string source="")
Set asymmetric y error.
Definition: Point2D.h:268
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:39
double yErrPlus(std::string source="") const
Get positive y-error value.
Definition: Point2D.h:228
void setXErrPlus(double explus)
Set positive x error.
Definition: Point2D.h:165
void setParent(Scatter *parent)
Definition: Point.h:114
bool operator<(const Bin1D< DBN > &a, const Bin1D< DBN > &b)
Bin1Ds are compared for axis sorting by lower edge position.
Definition: Bin1D.h:355
double xErrAvg() const
Get average x-error value.
Definition: Point2D.h:155
void setXErrs(double ex)
Set symmetric x error (alias)
Definition: Point2D.h:176
double yErrMinus(std::string source="") const
Get negative y-error value.
Definition: Point2D.h:221
const std::pair< double, double > & errs(size_t i, std::string source="") const
Get error values for direction i.
Definition: Point2D.h:402
const std::map< std::string, std::pair< double, double > > & errMap() const
Get error map for direction i.
Definition: Point2D.cc:8
void setXY(const std::pair< double, double > &xy)
Set x and y values.
Definition: Point2D.h:129
size_t dim()
Space dimension of the point.
Definition: Point2D.h:102
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:68
double yMax(std::string source="") const
Get value plus positive y-error.
Definition: Point2D.h:286
double yErrAvg(std::string source="") const
Get average y-error value.
Definition: Point2D.h:235
double val(size_t i) const
Get the point value for direction i.
Definition: Point2D.h:379
double xMin() const
Definition: Point2D.h:195
void setYErrMinus(double eyminus, std::string source="")
Set negative y error.
Definition: Point2D.h:243
void setXY(double x, double y)
Set x and y values.
Definition: Point2D.h:126
void setXErrs(const std::pair< double, double > &ex)
Set asymmetric x error.
Definition: Point2D.h:187
T * getParent() const
Definition: Point.h:119
void scaleY(double scaley)
Scaling of y axis.
Definition: Point2D.h:350
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:30
double errAvg(size_t i, std::string source="") const
Get average error value for direction i.
Definition: Point2D.h:426
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:435
void setErr(size_t i, double e, std::string source="")
Set symmetric error for direction i.
Definition: Point2D.h:452
void setY(double y, double eyminus, double eyplus, std::string source="")
Set y value and asymmetric error.
Definition: Point2D.h:326
void setX(double x)
Set x value.
Definition: Point2D.h:112
void setErrs(size_t i, std::pair< double, double > &e, std::string source="")
Set asymmetric error for direction i.
Definition: Point2D.h:468
void getVariationsFromParent() const
Parse the variations annotation on the parent scatter.
Definition: Point2D.cc:14
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:87
void setY(double y, double ey, std::string source="")
Set y value and symmetric error.
Definition: Point2D.h:320
const std::pair< double, double > & yErrs(std::string source="") const
Get y-error values.
Definition: Point2D.h:214
double errMinus(size_t i, std::string source="") const
Get negative error value for direction i.
Definition: Point2D.h:410
void scaleXY(double scalex, double scaley)
Scaling of both axes.
Definition: Point2D.h:358
double yMin(std::string source="") const
Get value minus negative y-error.
Definition: Point2D.h:279
void scale(size_t i, double scale)
Scaling along direction i.
Definition: Point2D.h:364
void setY(double y, std::pair< double, double > &ey, std::string source="")
Set y value and asymmetric error.
Definition: Point2D.h:332
double xErrMinus() const
Get negative x-error value.
Definition: Point2D.h:144
double xErrPlus() const
Get positive x-error value.
Definition: Point2D.h:149
void setErrPlus(size_t i, double eplus, std::string source="")
Set positive error for direction i.
Definition: Point2D.h:443
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:160
void setErrs(size_t i, double eminus, double eplus, std::string source="")
Set asymmetric error for direction i.
Definition: Point2D.h:460
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:418
A 2D data point to be contained in a Scatter2D.
Definition: Point2D.h:19