yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
ErrorND.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_ERRORND_H
7 #define YODA_ERRORND_H
8 
9 #include "YODA/Exceptions.h"
10 #include "YODA/Utils/MathUtils.h"
12 #include "YODA/Utils/ndarray.h"
13 #include <utility>
14 #include <algorithm>
15 
16 namespace YODA {
17 
18 
20  template<int N>
21  class Error {
22  public:
23 
24  typedef Utils::ndarray<double, N> NdVal;
25  typedef Utils::ndarray<std::pair<double,double>, N> NdValPair;
26 
27 
29 
30 
31  // Default constructor
32  Error(const std::string& name="")
33  : _name(name)
34  {
35  clear();
36  }
37 
38 
40  Error(const NdValPair& err, const std::string& name="")
41  : _name(name), _val(err)
42  { }
43 
44 
46  Error(const NdVal& errsymm, const std::string& name="")
47  : _name(name)
48  {
49  for (size_t i = 0; i < N; ++i) {
50  _val[i] = std::make_pair(errsymm[i], errsymm[i]);
51  }
52  }
53 
54 
56  Error(const NdVal& errminus, const NdVal& errplus, const std::string& name="")
57  : _name(name)
58  {
59  for (size_t i = 0; i < N; ++i) {
60  _val[i] = std::make_pair(errminus[i], errplus[i]);
61  }
62  }
63 
65 
66 
68 
69 
71  const std::string& name() const {
72  return _name;
73  }
74 
76  void setName(const std::string& name) {
77  _name = name;
78  }
79 
81  void clear() {
82  _val.clear();
83  }
84 
85  // /// Get the error pair array
86  // NdValPair& errs() {
87  // return _val;
88  // }
89 
90  // /// Get the error pair array (const version)
91  // const NdValPair& errs() const {
92  // return _val;
93  // }
94 
96  std::pair<double,double>& err(size_t dim) {
97  return _val[dim];
98  }
99 
101  const std::pair<double,double>& err(size_t dim) const {
102  return _val[dim];
103  }
104 
106  std::pair<double,double>& operator[](size_t dim) {
107  return _val[dim];
108  }
109 
111  const std::pair<double,double>& operator[](size_t dim) const {
112  return _val[dim];
113  }
114 
116  double errMinus(size_t dim) const {
117  return _val[dim].first;
118  }
119 
121  double errPlus(size_t dim) const {
122  return _val[dim].second;
123  }
124 
126  double errAvg(size_t dim) const {
127  return (_val[dim].first + _val[dim].second)/2.0;
128  }
129 
131 
132 
133 
135 
136 
138  void scale(const NdVal& scales) {
139  for (size_t i = 0; i < N; ++i) {
140  _val[i].first *= scales[i];
141  _val[i].second *= scales[i];
142  }
143  }
144 
146 
148 
149 
150  protected:
151 
153 
154 
155  std::string _name;
156  NdValPair _val;
157 
159 
160  };
161 
162 
163 
165 
166 
168  template <int N>
169  inline bool operator==(const Error<N>& a, const Error<N>& b) {
170  if (a.name() != b.name()) return false;
171  for (size_t i = 0; i < N; ++i) {
172  if (!fuzzyEquals(a.errMinus(i), b.errMinus(i))) return false;
173  if (!fuzzyEquals(a.errPlus(i), b.errPlus(i))) return false;
174  }
175  return true;
176  }
177 
179  template <int N>
180  inline bool operator!=(const Error<N>& a, const Error<N>& b) {
181  return !(a == b);
182  }
183 
184 
186  template <int N>
187  inline bool operator<(const Error<N>& a, const Error<N>& b) {
188  #define LT_IF_NOT_EQ(a,b) { if (!fuzzyEquals(a, b)) return a < b; }
189  for (size_t i = 0; i < N; ++i) {
190  LT_IF_NOT_EQ(a.err(i).first, b.err(i).first);
191  LT_IF_NOT_EQ(a.err(i).second, b.err(i).second);
192  }
193  #undef LT_IF_NOT_EQ
194  return a.name() < b.name();
195  }
196 
198  template <int N>
199  inline bool operator<=(const Error<N>& a, const Error<N>& b) {
200  if (a == b) return true;
201  return a < b;
202  }
203 
205  template <int N>
206  inline bool operator>(const Error<N>& a, const Error<N>& b) {
207  return !(a <= b);
208  }
209 
211  template <int N>
212  inline bool operator>=(const Error<N>& a, const Error<N>& b) {
213  return !(a < b);
214  }
215 
217 
218 
219 }
220 
221 #endif
double errPlus(size_t dim) const
Get the plus error in dimension dim.
Definition: ErrorND.h:121
std::pair< double, double > & operator[](size_t dim)
Access the error pair in dimension dim.
Definition: ErrorND.h:106
Error(const NdVal &errminus, const NdVal &errplus, const std::string &name="")
Constructor of an asymmetric error from two ND arrays of values.
Definition: ErrorND.h:56
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
Error(const NdValPair &err, const std::string &name="")
Constructor from ND array of error pairs.
Definition: ErrorND.h:40
Utils::ndarray< double, N > NdVal
Definition: ErrorND.h:24
double errMinus(size_t dim) const
Get the minus error in dimension dim.
Definition: ErrorND.h:116
const std::pair< double, double > & operator[](size_t dim) const
Get the error pair in dimension dim (const version)
Definition: ErrorND.h:111
void setName(const std::string &name)
Clear the point values and errors.
Definition: ErrorND.h:76
bool operator>(const Error< N > &a, const Error< N > &b)
Greater-than operator used to sort errors.
Definition: ErrorND.h:206
void clear()
Clear the point values and errors.
Definition: ErrorND.h:81
double errAvg(size_t dim) const
Get the mean error in dimension dim.
Definition: ErrorND.h:126
Error(const std::string &name="")
Definition: ErrorND.h:32
#define LT_IF_NOT_EQ(a, b)
bool operator!=(const Error< N > &a, const Error< N > &b)
Inequality test.
Definition: ErrorND.h:180
Utils::ndarray< std::pair< double, double >, N > NdValPair
Definition: ErrorND.h:25
An N-dimensional error to be contained in a Point<N>
Definition: ErrorND.h:21
bool operator>=(const Error< N > &a, const Error< N > &b)
Greater-than-or-equals operator used to sort errors.
Definition: ErrorND.h:212
const std::pair< double, double > & err(size_t dim) const
Get the error pair in dimension dim (const version)
Definition: ErrorND.h:101
Error(const NdVal &errsymm, const std::string &name="")
Constructor of a symmetric error from one ND array of values.
Definition: ErrorND.h:46
void scale(const NdVal &scales)
Uniform scaling.
Definition: ErrorND.h:138
const std::string & name() const
Clear the point values and errors.
Definition: ErrorND.h:71
std::pair< double, double > & err(size_t dim)
Access the error pair in dimension dim.
Definition: ErrorND.h:96
bool operator==(const Error< N > &a, const Error< N > &b)
Equality test.
Definition: ErrorND.h:169