yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
PointND.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_POINTND_H
7 #define YODA_POINTND_H
8 
9 #include "YODA/Exceptions.h"
10 #include "YODA/ErrorND.h"
11 #include "YODA/Utils/MathUtils.h"
13 #include "YODA/Utils/ndarray.h"
14 #include <utility>
15 #include <algorithm>
16 
17 namespace YODA {
18 
19 
21  template<int N>
22  class Point {
23  public:
24 
25  // Typedefs
26  typedef Utils::ndarray<double, N> NdVal;
27  typedef Utils::ndarray<std::pair<double,double>, N> NdValPair;
28  typedef Utils::sortedvector< Error<N> > Errors;
29 
30 
33 
34  // Default constructor
35  Point() {
36  clear();
37  }
38 
39 
41  Point(const NdVal& pos)
42  : _pos(pos)
43  { }
44 
45 
46  // /// Constructor from values with a single set of symmetric errors
47  // /// @todo Unnecessary since Error can be implicitly constructed this way
48  // Point(const NdVal& pos, const NdVal& errs)
49  // : _pos(pos)
50  // {
51  // _errs.insert(Error<N>(errs));
52  // }
53 
54 
56  Point(const NdVal& pos, const NdValPair& errs)
57  : _pos(pos)
58  {
59  _errs.insert(Error<N>(errs));
60  }
61 
62 
64  Point(const NdVal& pos, const Error<N>& err)
65  : _pos(pos)
66  {
67  _errs.insert(err);
68  }
69 
70 
72  Point(const std::vector<double>& pos, const std::vector< Error<N> >& errs)
73  : _pos(pos), _errs(errs)
74  { }
75 
77 
78 
81 
83  void clear() {
84  for (size_t i = 0; i < N; ++i) _pos[i] = 0;
85  _errs.clear();
86  }
87 
89 
91 
92 
93  public:
94 
97 
99  NdVal& pos() { return _pos; }
100 
102  const NdVal& pos() const { return _pos; }
103 
105  void setPos(const NdVal& pos) {
106  _pos = pos;
107  }
108 
110 
111 
114 
116  Errors& errs() {
117  return _errs;
118  }
119 
121  const Errors& errs() const {
122  return _errs;
123  }
124 
126  void setErrs(const Errors& errs) {
127  _errs = errs;
128  }
129 
131 
132 
135 
137  void scale(const NdVal& scales) {
138  for (size_t i = 0; i < N; ++i) _pos[i] *= scales[i];
139  for (Error<N>& e : errs()) e.scale(scales);
140  }
141 
142 
143  // /// Generalised transformations with functors
144  // void scale(const Trf<N>& trf) {
145  // for (size_t i = 0; i < N; ++i)
146  // _pos = trf.transform(_pos);
147  // for (Error e : errs())
148  // rf.transformErrs(_pos, e);
149  // }
150 
152 
153 
154  protected:
155 
158 
159  NdVal _pos;
160  Errors _errs;
161 
163 
164  };
165 
166 
167 
170 
172  template <int N>
173  inline bool operator==(const Point<N>& a, const Point<N>& b) {
174  // Compare positions
175  for (size_t i = 0; i < N; ++i) {
176  if ( !fuzzyEquals(a.pos()[i], b.pos()[i]) ) return false;
177  }
178  // Compare number of errors and then (sorted) error equality
179  if (a.errs().size() != b.errs().size()) return false;
180  for (size_t i = 0; i < a.errs().size(); ++i) {
181  if (a.errs()[i] != b.errs()[i]) return false;
182  }
183  return true;
184  }
185 
187  template <int N>
188  inline bool operator!=(const Point<N>& a, const Point<N>& b) {
189  return !(a == b);
190  }
191 
192 
194  template <int N>
195  inline bool operator<(const Point<N>& a, const Point<N>& b) {
196  #define LT_IF_NOT_EQ(a,b) { if (!fuzzyEquals(a, b)) return a < b; }
197  for (size_t i = 0; i < N; ++i) LT_IF_NOT_EQ(a.pos()[i], b.pos()[i]);
198  if (a.errs().size() != b.errs().size()) return a.errs().size() < b.errs().size();
199  for (size_t i = 0; i < a.errs().size(); ++i) {
200  if (a.errs()[i] != b.errs()[i]) return a.errs()[i] < b.errs()[i];
201  }
202  #undef LT_IF_NOT_EQ
203  return false;
204  }
205 
207  template <int N>
208  inline bool operator<=(const Point<N>& a, const Point<N>& b) {
209  if (a == b) return true;
210  return a < b;
211  }
212 
214  template <int N>
215  inline bool operator>(const Point<N>& a, const Point<N>& b) {
216  return !(a <= b);
217  }
218 
220  template <int N>
221  inline bool operator>=(const Point<N>& a, const Point<N>& b) {
222  return !(a < b);
223  }
224 
226 
227 
228 }
229 
230 #endif
const NdVal & pos() const
Get the coordinate vector (const version)
Definition: PointND.h:102
Utils::sortedvector< Error< N > > Errors
Definition: PointND.h:28
Point(const NdVal &pos, const Error< N > &err)
Constructor from values and a single Error object.
Definition: PointND.h:64
Base class for all Point*Ds, providing generic access to their numerical properties.
Definition: Point.h:16
Point(const std::vector< double > &pos, const std::vector< Error< N > > &errs)
Constructor from values and a collection of Error objects.
Definition: PointND.h:72
Errors & errs()
Get error values.
Definition: PointND.h:116
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
NdVal & pos()
Get the coordinate vector.
Definition: PointND.h:99
void setPos(const NdVal &pos)
Set the coordinate vector.
Definition: PointND.h:105
const Errors & errs() const
Get error values (const version)
Definition: PointND.h:121
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: PointND.h:83
void setErrs(const Errors &errs)
Set the error values.
Definition: PointND.h:126
Utils::ndarray< double, N > NdVal
Definition: PointND.h:26
Point(const NdVal &pos, const NdValPair &errs)
Constructor from values with a single set of asymmetric errors.
Definition: PointND.h:56
bool operator!=(const Error< N > &a, const Error< N > &b)
Inequality test.
Definition: ErrorND.h:180
void scale(const NdVal &scales)
Uniform scaling.
Definition: PointND.h:137
An N-dimensional error to be contained in a Point<N>
Definition: ErrorND.h:21
#define LT_IF_NOT_EQ(a, b)
bool operator>=(const Error< N > &a, const Error< N > &b)
Greater-than-or-equals operator used to sort errors.
Definition: ErrorND.h:212
Utils::ndarray< std::pair< double, double >, N > NdValPair
Definition: PointND.h:27
Point(const NdVal &pos)
Constructor from position values without errors.
Definition: PointND.h:41
bool operator==(const Error< N > &a, const Error< N > &b)
Equality test.
Definition: ErrorND.h:169