YODA is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.3.1
Scatter1D.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-2015 The YODA collaboration (see AUTHORS for details)
5 //
6 #ifndef YODA_SCATTER1D_H
7 #define YODA_SCATTER1D_H
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/Point1D.h"
12 #include <utility>
13 
14 namespace YODA {
15 
16 
17  // Forward declarations
18  class Counter;
19 
20 
22  class Scatter1D : public AnalysisObject {
23  public:
24 
26  typedef Point1D Point;
27  typedef Utils::sortedvector<Point1D> Points;
28 
29 
31 
32 
34  Scatter1D(const std::string& path="", const std::string& title="")
35  : AnalysisObject("Scatter1D", path, title)
36  { }
37 
38 
41  const std::string& path="", const std::string& title="")
42  : AnalysisObject("Scatter1D", path, title),
43  _points(points)
44  { }
45 
46 
48  Scatter1D(const std::vector<double>& x,
49  const std::string& path="", const std::string& title="")
50  : AnalysisObject("Scatter1D", path, title)
51  {
52  for (size_t i = 0; i < x.size(); ++i) addPoint(x[i]);
53  }
54 
55 
57  Scatter1D(const std::vector<double>& x, const std::vector<double>& ex,
58  const std::string& path="", const std::string& title="")
59  : AnalysisObject("Scatter1D", path, title)
60  {
61  if (x.size() != ex.size()) throw UserError("x and ex vectors must have same length");
62  for (size_t i = 0; i < x.size(); ++i) addPoint(x[i], ex[i]);
63  }
64 
66  Scatter1D(const std::vector<double>& x, const std::vector<std::pair<double,double> >& ex,
67  const std::string& path="", const std::string& title="")
68  : AnalysisObject("Scatter1D", path, title)
69  {
70  if (x.size() != ex.size()) throw UserError("x and ex vectors must have same length");
71  for (size_t i = 0; i < x.size(); ++i) addPoint(Point1D(x[i], ex[i]));
72  }
73 
74 
76  Scatter1D(const std::vector<double>& x, const std::vector<double>& y,
77  const std::vector<double>& exminus,
78  const std::vector<double>& explus,
79  const std::vector<double>& eyminus,
80  const std::vector<double>& eyplus,
81  const std::string& path="", const std::string& title="")
82  : AnalysisObject("Scatter1D", path, title)
83  {
84  if (x.size() != exminus.size()) throw UserError("x and ex vectors must have same length");
85  if (exminus.size() != explus.size()) throw UserError("ex plus and minus vectors must have same length");
86  for (size_t i = 0; i < x.size(); ++i) addPoint(Point1D(x[i], exminus[i], explus[i]));
87  }
88 
89 
92  Scatter1D(const Scatter1D& s1, const std::string& path="")
93  : AnalysisObject("Scatter1D", (path.size() == 0) ? s1.path() : path, s1, s1.title()),
94  _points(s1._points)
95  { }
96 
97 
100  AnalysisObject::operator = (s1); //< AO treatment of paths etc.
101  _points = s1._points;
102  return *this;
103  }
104 
106  Scatter1D clone() const {
107  return Scatter1D(*this);
108  }
109 
111  Scatter1D* newclone() const {
112  return new Scatter1D(*this);
113  }
114 
116 
117 
119 
120 
122  void reset() {
123  _points.clear();
124  }
125 
127  void scaleX(double scalex) {
128  BOOST_FOREACH (Point1D& p, _points) p.scaleX(scalex);
129  }
130 
132 
133 
135 
136 
138 
139 
141  size_t numPoints() const {
142  return _points.size();
143  }
144 
145 
148  return _points;
149  }
150 
151 
153  const Points& points() const {
154  return _points;
155  }
156 
157 
159  Point1D& point(size_t index) {
160  if (index >= numPoints()) throw RangeError("There is no point with this index");
161  return _points.at(index);
162  }
163 
164 
166  const Point1D& point(size_t index) const {
167  if (index >= numPoints()) throw RangeError("There is no point with this index");
168  return _points.at(index);
169  }
170 
172 
173 
175 
176 
178  void addPoint(const Point1D& pt) {
179  _points.insert(pt);
180  }
181 
183  void addPoint(double x) {
184  _points.insert(Point1D(x));
185  }
186 
188  void addPoint(double x, double ex) {
189  _points.insert(Point1D(x, ex));
190  }
191 
193  void addPoint(double x, const std::pair<double,double>& ex) {
194  _points.insert(Point1D(x, ex));
195  }
196 
198  void addPoint(double x, double exminus, double explus) {
199  _points.insert(Point1D(x, exminus, explus));
200  }
201 
203  void addPoints(const Points& pts) {
204  BOOST_FOREACH (const Point1D& pt, pts) addPoint(pt);
205  }
206 
208 
209 
211 
212 
214  void combineWith(const Scatter1D& other) {
215  addPoints(other.points());
216  }
217 
220  void combineWith(const std::vector<Scatter1D>& others) {
221  BOOST_FOREACH (const Scatter1D& s, others) combineWith(s);
222  }
223 
225 
226 
228  bool operator == (const Scatter1D& other) {
229  return _points == other._points;
230  }
231 
233  bool operator != (const Scatter1D& other) {
234  return ! operator == (other);
235  }
236 
237 
238  private:
239 
240  Points _points;
241 
242  };
243 
244 
246 
247 
248  inline Scatter1D combine(const Scatter1D& a, const Scatter1D& b) {
249  Scatter1D rtn = a;
250  rtn.combineWith(b);
251  return rtn;
252  }
253 
254  inline Scatter1D combine(const std::vector<Scatter1D>& scatters) {
255  Scatter1D rtn;
256  rtn.combineWith(scatters);
257  return rtn;
258  }
259 
261 
262 
264 
265 
267 
268 
270  Scatter1D mkScatter(const Counter& c);
271 
274  inline Scatter1D mkScatter(const Scatter1D& s) {
275  return Scatter1D(s);
276  }
277 
279 
280 
282 
283 
284  // /// @name Combining scatters: global operators, assuming aligned points
285  // /// @todo This "1D histo-like behaviour" breaks the x/y symmetry... is there another way?
286  // //@{
287 
288  // /// Add two scatters
289  // Scatter1D add(const Scatter1D& first, const Scatter1D& second);
290 
291 
292  // /// Add two scatters
293  // inline Scatter1D operator + (const Scatter1D& first, const Scatter1D& second) {
294  // return add(first, second);
295  // }
296 
297 
298  // /// Subtract two scatters
299  // Scatter1D subtract(const Scatter1D& first, const Scatter1D& second);
300 
301 
302  // /// Subtract two scatters
303  // inline Scatter1D operator - (const Scatter1D& first, const Scatter1D& second) {
304  // return subtract(first, second);
305  // }
306 
307 
308  // /// Divide two scatters
309  // Scatter1D divide(const Scatter1D& numer, const Scatter1D& denom);
310 
311 
312  // /// Divide two scatters
313  // inline Scatter1D operator / (const Scatter1D& numer, const Scatter1D& denom) {
314  // return divide(numer, denom);
315  // }
316 
317  // //@}
318 
319 
321 
322 
326  template<typename FNX>
327  inline void transformX(Scatter1D& s, FNX fx) {
328  for (size_t i = 0; i < s.numPoints(); ++i) {
329  Point1D& p = s.point(i);
330  const double newx = fx(p.x());
331  const double fx_xmin = fx(p.xMin());
332  const double fx_xmax = fx(p.xMax());
333  // Deal with possible inversions of min/max ordering under the transformation
334  const double newxmin = std::min(fx_xmin, fx_xmax);
335  const double newxmax = std::max(fx_xmin, fx_xmax);
336  // Set new point x values
337  p.setX(newx);
339  p.setXErrMinus(newx - newxmin);
340  p.setXErrPlus(newxmax - newx);
341  }
342  }
343 
345 
346 
347 }
348 
349 #endif
Scatter1D * newclone() const
Make a copy on the heap, via 'new'.
Definition: Scatter1D.h:111
void addPoint(double x, double exminus, double explus)
Insert a new point, defined as the x value and explicit asymmetric errors.
Definition: Scatter1D.h:198
Scatter1D combine(const Scatter1D &a, const Scatter1D &b)
Definition: Scatter1D.h:248
Scatter1D & operator=(const Scatter1D &s1)
Assignment operator.
Definition: Scatter1D.h:99
Scatter1D(const std::vector< double > &x, const std::vector< std::pair< double, double > > &ex, const std::string &path="", const std::string &title="")
Constructor from x values with asymmetric errors.
Definition: Scatter1D.h:66
void setXErrPlus(double explus)
Set positive x error.
Definition: Point1D.h:113
Scatter1D(const Scatter1D &s1, const std::string &path="")
Definition: Scatter1D.h:92
Scatter1D(const std::vector< double > &x, const std::string &path="", const std::string &title="")
Constructor from a vector of x values with no errors.
Definition: Scatter1D.h:48
const Points & points() const
Get the collection of points (const)
Definition: Scatter1D.h:153
double xMin() const
Definition: Point1D.h:136
void addPoint(double x, const std::pair< double, double > &ex)
Insert a new point, defined as the x value and an asymmetric error pair.
Definition: Scatter1D.h:193
size_t numPoints() const
Number of points in the scatter.
Definition: Scatter1D.h:141
void addPoint(const Point1D &pt)
Insert a new point.
Definition: Scatter1D.h:178
bool operator!=(const Scatter1D &other)
Non-equality operator.
Definition: Scatter1D.h:233
Scatter1D(const std::string &path="", const std::string &title="")
Empty constructor.
Definition: Scatter1D.h:34
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
void addPoint(double x, double ex)
Insert a new point, defined as the x value and symmetric errors.
Definition: Scatter1D.h:188
void addPoints(const Points &pts)
Insert a collection of new points.
Definition: Scatter1D.h:203
void addPoint(double x)
Insert a new point, defined as the x value and no errors.
Definition: Scatter1D.h:183
void reset()
Clear all points.
Definition: Scatter1D.h:122
Error for problems introduced outside YODA, to put it nicely.
Definition: Exceptions.h:93
bool operator==(const Scatter1D &other)
Equality operator.
Definition: Scatter1D.h:228
Scatter1D(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &exminus, const std::vector< double > &explus, const std::vector< double > &eyminus, const std::vector< double > &eyplus, const std::string &path="", const std::string &title="")
Constructor from values with completely explicit asymmetric errors.
Definition: Scatter1D.h:76
Scatter1D clone() const
Make a copy on the stack.
Definition: Scatter1D.h:106
void scaleX(double scalex)
Scaling of x axis.
Definition: Scatter1D.h:127
void setXErrMinus(double exminus)
Set negative x error.
Definition: Point1D.h:108
double xMax() const
Definition: Point1D.h:142
Point1D & point(size_t index)
Get a reference to the point with index index (non-const)
Definition: Scatter1D.h:159
void combineWith(const Scatter1D &other)
Definition: Scatter1D.h:214
A 1D data point to be contained in a Scatter1D.
Definition: Point1D.h:17
void combineWith(const std::vector< Scatter1D > &others)
Definition: Scatter1D.h:220
Scatter1D mkScatter(const Counter &c)
Make a Scatter1D representation of a Histo1D.
Definition: Scatter1D.cc:8
Utils::sortedvector< Point1D > Points
Definition: Scatter1D.h:27
void scaleX(double scalex)
Scaling of x axis.
Definition: Point1D.h:194
AnalysisObject is the base class for histograms and scatters.
const Point1D & point(size_t index) const
Get a reference to the point with index index (const)
Definition: Scatter1D.h:166
Scatter1D(const std::vector< double > &x, const std::vector< double > &ex, const std::string &path="", const std::string &title="")
Constructor from vectors of x values with symmetric errors.
Definition: Scatter1D.h:57
A very generic data type which is just a collection of 1D data points with errors.
Definition: Scatter1D.h:22
const std::string path() const
Points & points()
Get the collection of points (non-const)
Definition: Scatter1D.h:147
const std::string title() const
void transformX(Scatter1D &s, FNX fx)
Apply transformation fx(x) to all values and error positions (operates in-place on s) ...
Definition: Scatter1D.h:327
Point1D Point
Type of the native Point1D collection.
Definition: Scatter1D.h:26
double x() const
Get x value.
Definition: Point1D.h:74
void setX(double x)
Set x value.
Definition: Point1D.h:77
virtual AnalysisObject & operator=(const AnalysisObject &ao)
Default copy assignment operator.
Scatter1D(const Points &points, const std::string &path="", const std::string &title="")
Constructor from a set of points.
Definition: Scatter1D.h:40