yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
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-2021 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 
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  this->setParent( p.getParent() );
57  }
58 
59 
62  _x = p._x;
63  _ex = p._ex;
64  this->setParent( p.getParent() );
65  return *this;
66  }
67 
69 
70 
71  public:
72 
74  size_t dim() { return 1; }
75 
76 
79 
81  double x() const { return _x; }
82 
84  void setX(double x) { _x = x; }
85 
87 
89 
90 
93 
95  const std::pair<double,double>& xErrs( std::string source="") const {
96  if (source!="") getVariationsFromParent();
97  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
98  return _ex.at(source);
99  }
100 
102  double xErrMinus( std::string source="") const {
103  if (source!="") getVariationsFromParent();
104  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
105  return _ex.at(source).first;
106  }
107 
109  double xErrPlus( std::string source="") const {
110  if (source!="") getVariationsFromParent();
111  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
112  return _ex.at(source).second;
113  }
114 
116  double xErrAvg( std::string source="") const {
117  if (source!="") getVariationsFromParent();
118  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
119  return (_ex.at(source).first + _ex.at(source).second)/2.0;
120  }
121 
123  void setXErrMinus(double exminus, std::string source="") {
124  if (!_ex.count(source)) _ex[source] = std::make_pair(0.,0.);
125  _ex.at(source).first = exminus;
126  }
127 
129  void setXErrPlus(double explus, std::string source="") {
130  if (!_ex.count(source)) _ex[source] = std::make_pair(0.,0.);
131  _ex.at(source).second = explus;
132  }
133 
135  void setXErr(double ex, std::string source="") {
136  setXErrMinus(ex, source);
137  setXErrPlus(ex, source);
138  }
139 
141  void setXErrs(double ex, std::string source="") {
142  setXErr(ex, source);
143  }
144 
146  void setXErrs(double exminus, double explus, std::string source="") {
147  setXErrMinus(exminus, source);
148  setXErrPlus(explus, source);
149  }
150 
152  void setXErrs(const std::pair<double,double>& ex, std::string source="") {
153  _ex[source] = ex;
154  }
155 
157  double xMin(std::string source="") const {
158  if (source!="") getVariationsFromParent();
159  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
160  return _x - _ex.at(source).first;
161  }
162 
164  double xMax(std::string source="") const {
165  if (source!="") getVariationsFromParent();
166  if (!_ex.count(source)) throw RangeError("xErrs has no such key: "+source);
167  return _x + _ex.at(source).second;
168  }
169 
171 
172 
175 
177  void setX(double x, double ex, std::string source="") {
178  setX(x);
179  setXErr(ex, source);
180  }
181 
183  void setX(double x, double exminus, double explus, std::string source="") {
184  setX(x);
185  setXErrs(exminus, explus, source);
186  }
187 
189  void setX(double x, std::pair<double,double>& ex, std::string source="") {
190  setX(x);
191  setXErrs(ex, source);
192  }
193 
195 
196 
197  // @name Manipulations
199 
201  void scaleX(double scalex) {
202  setX(x()*scalex);
203  for (const auto &source : _ex){
204  setXErrs(xErrMinus()*scalex, xErrPlus()*scalex, source.first);
205  }
206  }
207 
209  void scale(size_t i, double scale) {
210  switch (i) {
211  case 1: scaleX(scale); break;
212  default: throw RangeError("Invalid axis int, must be in range 1..dim");
213  }
214  }
215 
217 
218 
221 
223  double val(size_t i) const {
224  if (i == 0 || i > 1) throw RangeError("Invalid axis int, must be in range 1..dim");
225  return x();
226  }
228  void setVal(size_t i, double val) {
229  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
230  setX(val);
231  }
232 
234  const std::map< std::string, std::pair<double,double>> & errMap() const {
236  return _ex;
237  }
238 
239  // Parse the variations from the parent AO if it exists
240  void getVariationsFromParent() const;
241 
242 
244  const std::pair<double,double>& errs(size_t i, std::string source="") const {
245  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
246  return xErrs(source);
247  }
249  double errMinus(size_t i, std::string source="") const {
250  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
251  return xErrMinus(source);
252  }
254  double errPlus(size_t i, std::string source="") const {
255  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
256  return xErrPlus(source);
257  }
259  double errAvg(size_t i, std::string source="") const {
260  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
261  return xErrAvg(source);
262  }
263 
265  void setErrMinus(size_t i, double eminus, std::string source="") {
266  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
267  setXErrMinus(eminus, source);
268  }
270  void setErrPlus(size_t i, double eplus, std::string source="") {
271  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
272  setXErrPlus(eplus, source);
273  }
274 
276  void setErr(size_t i, double e, std::string source="") {
277  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
278  setXErr(e, source);
279  }
281  void setErrs(size_t i, double eminus, double eplus, std::string source="") {
282  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
283  setXErrs(eminus, eplus, source);
284  }
286  void setErrs(size_t i, std::pair<double,double>& e, std::string source="") {
287  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
288  setXErrs(e, source);
289  }
290 
292  void set(size_t i, double val, double e, std::string source="") {
293  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
294  setX(val, e, source);
295  }
297  void set(size_t i, double val, double eminus, double eplus, std::string source="") {
298  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
299  setX(val, eminus, eplus, source);
300  }
302  void set(size_t i, double val, std::pair<double,double>& e, std::string source="") {
303  if (i != 1) throw RangeError("Invalid axis int, must be in range 1..dim");
304  setX(val, e, source);
305  }
306 
308 
309 
310  protected:
311 
314 
315  double _x;
316  // a map of the errors for each source. Nominal stored under ""
317  // to ensure backward compatibility
318  std::map< std::string, std::pair<double,double> > _ex;
319 
321 
322  };
323 
324 
325 
328 
331  inline bool operator==(const YODA::Point1D& a, const YODA::Point1D& b) {
332  if (!YODA::fuzzyEquals(a.x(), b.x()) ||
334  !YODA::fuzzyEquals(a.xErrPlus(), b.xErrPlus()) ) return false;
335  return true;
336  }
337 
339  inline bool operator != (const YODA::Point1D& a, const YODA::Point1D& b) {
340  return !(a == b);
341  }
342 
344  inline bool operator < (const YODA::Point1D& a, const YODA::Point1D& b) {
345  if (!YODA::fuzzyEquals(a.x(), b.x())) {
346  return a.x() < b.x();
347  }
348  if (!YODA::fuzzyEquals(a.xErrMinus(), b.xErrMinus())) {
349  return a.xErrMinus() < b.xErrMinus();
350  }
351  if (!YODA::fuzzyEquals(a.xErrPlus(), b.xErrPlus())) {
352  return a.xErrPlus() < b.xErrPlus();
353  }
354  return false;
355  }
356 
358  inline bool operator <= (const YODA::Point1D& a, const YODA::Point1D& b) {
359  if (a == b) return true;
360  return a < b;
361  }
362 
364  inline bool operator > (const YODA::Point1D& a, const YODA::Point1D& b) {
365  return !(a <= b);
366  }
367 
369  inline bool operator >= (const YODA::Point1D& a, const YODA::Point1D& b) {
370  return !(a < b);
371  }
372 
374 
375 
376 }
377 
378 #endif
double x() const
Get x value.
Definition: Point1D.h:81
double xErrAvg(std::string source="") const
Get average x-error value.
Definition: Point1D.h:116
Base class for all Point*Ds, providing generic access to their numerical properties.
Definition: Point.h:16
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:223
const std::pair< double, double > & xErrs(std::string source="") const
Get x-error values.
Definition: Point1D.h:95
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
void getVariationsFromParent() const
Parse the variations annotation on the parent scatter.
Definition: Point1D.cc:7
double xErrMinus(std::string source="") const
Get negative x-error value.
Definition: Point1D.h:102
double xMax(std::string source="") const
Get value plus positive x-error.
Definition: Point1D.h:164
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 setX(double x, std::pair< double, double > &ex, std::string source="")
Set x value and asymmetric error.
Definition: Point1D.h:189
void setErrs(size_t i, double eminus, double eplus, std::string source="")
Set asymmetric error for direction i.
Definition: Point1D.h:281
double errAvg(size_t i, std::string source="") const
Get average error value for direction i.
Definition: Point1D.h:259
Point1D(double x, const std::pair< double, double > &ex, std::string source="")
Constructor from values with asymmetric errors.
Definition: Point1D.h:45
void setParent(Scatter *parent)
Definition: Point.h:114
double xMin(std::string source="") const
Get value minus negative x-error.
Definition: Point1D.h:157
bool operator<(const Bin1D< DBN > &a, const Bin1D< DBN > &b)
Bin1Ds are compared for axis sorting by lower edge position.
Definition: Bin1D.h:355
void setXErrMinus(double exminus, std::string source="")
Set negative x error.
Definition: Point1D.h:123
void scale(size_t i, double scale)
Scaling along direction i.
Definition: Point1D.h:209
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:270
void setX(double x, double ex, std::string source="")
Set x value and symmetric error.
Definition: Point1D.h:177
void setXErrPlus(double explus, std::string source="")
Set positive x error.
Definition: Point1D.h:129
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:135
double errMinus(size_t i, std::string source="") const
Get negative error value for direction i.
Definition: Point1D.h:249
void setXErrs(double exminus, double explus, std::string source="")
Set asymmetric x error.
Definition: Point1D.h:146
T * getParent() const
Definition: Point.h:119
double errPlus(size_t i, std::string source="") const
Get positive error value for direction i.
Definition: Point1D.h:254
void setErrMinus(size_t i, double eminus, std::string source="")
Set negative error for direction i.
Definition: Point1D.h:265
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:201
void setVal(size_t i, double val)
Set the point value for direction i.
Definition: Point1D.h:228
Point1D & operator=(const Point1D &p)
Copy assignment.
Definition: Point1D.h:61
const std::map< std::string, std::pair< double, double > > & errMap() const
Get error map for direction i.
Definition: Point1D.h:234
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:183
void setXErrs(double ex, std::string source="")
Set symmetric x error (alias)
Definition: Point1D.h:141
double xErrPlus(std::string source="") const
Get positive x-error value.
Definition: Point1D.h:109
void setErrs(size_t i, std::pair< double, double > &e, std::string source="")
Set asymmetric error for direction i.
Definition: Point1D.h:286
const std::pair< double, double > & errs(size_t i, std::string source="") const
Get error values for direction i.
Definition: Point1D.h:244
void setXErrs(const std::pair< double, double > &ex, std::string source="")
Set asymmetric x error.
Definition: Point1D.h:152
void setX(double x)
Set x value.
Definition: Point1D.h:84
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:74
void setErr(size_t i, double e, std::string source="")
Set symmetric error for direction i.
Definition: Point1D.h:276
bool operator==(const Error< N > &a, const Error< N > &b)
Equality test.
Definition: ErrorND.h:169