yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
Scatter2D.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_SCATTER2D_H
7 #define YODA_SCATTER2D_H
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/Point2D.h"
12 #include <utility>
13 #include <memory>
14 
15 namespace YODA {
16 
17 
18  // Forward declarations
19  class Histo1D;
20  class Profile1D;
21 
22 
24  class Scatter2D : public AnalysisObject {
25  public:
26 
28  typedef Point2D Point;
29  typedef Utils::sortedvector<Point2D> Points;
30  typedef std::shared_ptr<Scatter2D> Ptr;
31 
32 
34 
35 
37  Scatter2D(const std::string& path="", const std::string& title="")
38  : AnalysisObject("Scatter2D", path, title)
39  { }
40 
41 
43  Scatter2D(const Points& points,
44  const std::string& path="", const std::string& title="")
45  : AnalysisObject("Scatter2D", path, title),
46  _points(points)
47  { }
48 
49 
51  Scatter2D(const std::vector<double>& x, const std::vector<double>& y,
52  const std::string& path="", const std::string& title="")
53  : AnalysisObject("Scatter2D", path, title)
54  {
55  if (x.size() != y.size()) throw UserError("x and y vectors must have same length");
56  for (size_t i = 0; i < x.size(); ++i) addPoint(x[i], y[i]);
57  }
58 
59 
61  Scatter2D(const std::vector<double>& x, const std::vector<double>& y,
62  const std::vector<double>& ex, const std::vector<double>& ey,
63  const std::string& path="", const std::string& title="")
64  : AnalysisObject("Scatter2D", path, title)
65  {
66  if (x.size() != y.size()) throw UserError("x and y vectors must have same length");
67  if (x.size() != ex.size()) throw UserError("x and ex vectors must have same length");
68  if (y.size() != ey.size()) throw UserError("y and ey vectors must have same length");
69  for (size_t i = 0; i < x.size(); ++i) addPoint(x[i], y[i], ex[i], ey[i]);
70  }
71 
72 
74  Scatter2D(const std::vector<double>& x, const std::vector<double>& y,
75  const std::vector<std::pair<double,double> >& ex, const std::vector<std::pair<double,double> >& ey,
76  const std::string& path="", const std::string& title="")
77  : AnalysisObject("Scatter2D", path, title)
78  {
79  if (x.size() != y.size()) throw UserError("x and y vectors must have same length");
80  if (x.size() != ex.size()) throw UserError("x and ex vectors must have same length");
81  if (y.size() != ey.size()) throw UserError("y and ey vectors must have same length");
82  for (size_t i = 0; i < x.size(); ++i) addPoint(Point2D(x[i], y[i], ex[i], ey[i]));
83  }
84 
85 
87  Scatter2D(const std::vector<double>& x, const std::vector<double>& y,
88  const std::vector<double>& exminus, const std::vector<double>& explus,
89  const std::vector<double>& eyminus, const std::vector<double>& eyplus,
90  const std::string& path="", const std::string& title="")
91  : AnalysisObject("Scatter2D", path, title)
92  {
93  if (x.size() != y.size()) throw UserError("x and y vectors must have same length");
94  if (x.size() != exminus.size()) throw UserError("x and ex vectors must have same length");
95  if (y.size() != eyminus.size()) throw UserError("y and ey vectors must have same length");
96  if (exminus.size() != explus.size()) throw UserError("ex plus and minus vectors must have same length");
97  if (eyminus.size() != eyplus.size()) throw UserError("ey plus and minus vectors must have same length");
98  for (size_t i = 0; i < x.size(); ++i) addPoint(Point2D(x[i], y[i], exminus[i], explus[i], eyminus[i], eyplus[i]));
99  }
100 
101 
104  Scatter2D(const Scatter2D& s2, const std::string& path="")
105  : AnalysisObject("Scatter2D", (path.size() == 0) ? s2.path() : path, s2, s2.title()),
106  _points(s2._points)
107  {
108  for ( auto &ann : annotations()){
109  setAnnotation(ann, annotation(ann));
110  }
111  }
112 
113 
116  AnalysisObject::operator = (s2); //< AO treatment of paths etc.
117  _points = s2._points;
118  return *this;
119  }
120 
122  Scatter2D clone() const {
123  return Scatter2D(*this);
124  }
125 
127  Scatter2D* newclone() const {
128  return new Scatter2D(*this);
129  }
130 
132 
133 
135  size_t dim() const { return 2; }
136 
137 
139 
140 
142  void reset() {
143  _points.clear();
144  }
145 
147  void scaleX(double scalex) {
148  for (Point2D& p : _points) p.scaleX(scalex);
149  }
150 
152  void scaleY(double scaley) {
153  for (Point2D& p : _points) p.scaleY(scaley);
154  }
155 
157  void scaleXY(double scalex, double scaley) {
158  for (Point2D& p : _points) p.scaleXY(scalex, scaley);
159  }
160 
163  void scale(double scalex, double scaley) {
164  scaleXY(scalex, scaley);
165  }
166 
168 
169 
171 
173  const std::vector<std::string> variations() const;
174 
176 
177 
179  size_t numPoints() const {
180  return _points.size();
181  }
182 
183 
185  Points& points() {
186  return _points;
187  }
188 
189 
191  const Points& points() const {
192  return _points;
193  }
194 
195 
197  Point2D& point(size_t index) {
198  if (index >= numPoints()) throw RangeError("There is no point with this index");
199  return _points.at(index);
200  }
201 
202 
204  const Point2D& point(size_t index) const {
205  if (index >= numPoints()) throw RangeError("There is no point with this index");
206  return _points.at(index);
207  }
208 
210 
211 
213 
214 
216  void addPoint(const Point2D& pt) {
217  _points.insert(pt);
218  }
219 
221  void addPoint(double x, double y) {
222  _points.insert(Point2D(x, y));
223  }
224 
226  void addPoint(double x, double y,
227  double ex, double ey) {
228  _points.insert(Point2D(x, y, ex, ey));
229  }
230 
232  void addPoint(double x, double y,
233  const std::pair<double,double>& ex, const std::pair<double,double>& ey) {
234  _points.insert(Point2D(x, y, ex, ey));
235  }
236 
238  void addPoint(double x, double y,
239  double exminus, double explus,
240  double eyminus, double eyplus) {
241  _points.insert(Point2D(x, y, exminus, explus, eyminus, eyplus));
242  }
243 
245  void addPoints(const Points& pts) {
246  for (const Point2D& pt : pts) addPoint(pt);
247  }
248 
250 
251 
253 
254 
256  void combineWith(const Scatter2D& other) {
257  addPoints(other.points());
258  //return *this;
259  }
260 
263  void combineWith(const std::vector<Scatter2D>& others) {
264  for (const Scatter2D& s : others) combineWith(s);
265  //return *this;
266  }
267 
269 
270 
272  bool operator == (const Scatter2D& other) {
273  return _points == other._points;
274  }
275 
277  bool operator != (const Scatter2D& other) {
278  return ! operator == (other);
279  }
280 
281 
283 
284 
285  private:
286 
287  Points _points;
288 
289  };
290 
291 
293  typedef Scatter2D S2D;
294 
295 
297 
298 
299  inline Scatter2D combine(const Scatter2D& a, const Scatter2D& b) {
300  Scatter2D rtn = a;
301  rtn.combineWith(b);
302  return rtn;
303  }
304 
305  inline Scatter2D combine(const std::vector<Scatter2D>& scatters) {
306  Scatter2D rtn;
307  rtn.combineWith(scatters);
308  return rtn;
309  }
310 
312 
313 
315 
316 
318 
319 
324  Scatter2D mkScatter(const Histo1D& h, bool usefocus=false);
325 
332  Scatter2D mkScatter(const Profile1D& p, bool usefocus=false, bool usestddev=false);
333 
336  inline Scatter2D mkScatter(const Scatter2D& s) { return Scatter2D(s); }
337  // /// @note The usefocus arg is just for consistency and has no effect for Scatter -> Scatter
338  // inline Scatter2D mkScatter(const Scatter2D& s, bool) { return mkScatter(s); }
339 
341 
342 
344 
345 
347 
348 
352  template<typename FNX>
353  inline void transformX(Scatter2D& s, FNX fx) {
354  for (size_t i = 0; i < s.numPoints(); ++i) {
355  Point2D& p = s.point(i);
356  const double newx = fx(p.x());
357  const double fx_xmin = fx(p.xMin());
358  const double fx_xmax = fx(p.xMax());
359  // Deal with possible inversions of min/max ordering under the transformation
360  const double newxmin = std::min(fx_xmin, fx_xmax);
361  const double newxmax = std::max(fx_xmin, fx_xmax);
362  // Set new point x values
363  p.setX(newx);
365  p.setXErrMinus(newx - newxmin);
366  p.setXErrPlus(newxmax - newx);
367  }
368  }
369 
370 
374  template<typename FNY>
375  inline void transformY(Scatter2D& s, FNY fy) {
376  for (size_t i = 0; i < s.numPoints(); ++i) {
377  Point2D& p = s.point(i);
378  const double newy = fy(p.y());
379  const double fy_ymin = fy(p.yMin());
380  const double fy_ymax = fy(p.yMax());
381  // Deal with possible inversions of min/max ordering under the transformation
382  const double newymin = std::min(fy_ymin, fy_ymax);
383  const double newymax = std::max(fy_ymin, fy_ymax);
384  // Set new point y values
385  p.setY(newy);
387  p.setYErrMinus(newy - newymin);
388  p.setYErrPlus(newymax - newy);
389  }
390  }
391 
392 
394 
395 
397  inline void flip(Scatter2D& s) {
398  for (size_t i = 0; i < s.numPoints(); ++i) {
399  Point2D& p = s.point(i);
400  const double newx = p.y();
401  const double newy = p.x();
402  const double newxmin = p.yMin();
403  const double newxmax = p.yMax();
404  const double newymin = p.xMin();
405  const double newymax = p.xMax();
406  p.setX(newx);
407  p.setY(newy);
409  p.setXErrMinus(newx - newxmin);
410  p.setXErrPlus(newxmax - newx);
411  p.setYErrMinus(newy - newymin);
412  p.setYErrPlus(newymax - newy);
413  }
414  }
415 
417 
418 
419 }
420 
421 #endif
void scale(double scalex, double scaley)
Definition: Scatter2D.h:163
void setY(double y)
Set y value.
Definition: Point2D.h:115
Scatter2D(const std::vector< double > &x, const std::vector< double > &y, const std::vector< std::pair< double, double > > &ex, const std::vector< std::pair< double, double > > &ey, const std::string &path="", const std::string &title="")
Constructor from values with asymmetric errors on both x and y.
Definition: Scatter2D.h:74
Scatter1D combine(const Scatter1D &a, const Scatter1D &b)
Definition: Scatter1D.h:266
double x() const
Get x value.
Definition: Point2D.h:106
void addPoint(double x, double y)
Insert a new point, defined as the x/y value pair and no errors.
Definition: Scatter2D.h:221
Scatter2D * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Scatter2D.h:127
const std::string path() const
Get the AO path.
void addPoint(const Point2D &pt)
Insert a new point.
Definition: Scatter2D.h:216
A very generic data type which is just a collection of 2D data points with errors.
Definition: Scatter2D.h:24
void setYErrPlus(double eyplus, std::string source="")
Set positive y error.
Definition: Point2D.h:241
double y() const
Get y value.
Definition: Point2D.h:112
void combineWith(const Scatter2D &other)
Definition: Scatter2D.h:256
Scatter2D(const Scatter2D &s2, const std::string &path="")
Definition: Scatter2D.h:104
double xMax() const
Definition: Point2D.h:200
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
Scatter2D(const Points &points, const std::string &path="", const std::string &title="")
Constructor from a set of points.
Definition: Scatter2D.h:43
A one-dimensional histogram.
Definition: Histo1D.h:26
void scaleX(double scalex)
Scaling of x axis.
Definition: Scatter2D.h:147
void setXErrPlus(double explus)
Set positive x error.
Definition: Point2D.h:162
void scaleY(double scaley)
Scaling of y axis.
Definition: Scatter2D.h:152
Scatter2D & operator=(const Scatter2D &s2)
Assignment operator.
Definition: Scatter2D.h:115
bool operator==(const Scatter2D &other)
Equality operator.
Definition: Scatter2D.h:272
Error for problems introduced outside YODA, to put it nicely.
Definition: Exceptions.h:100
Scatter2D(const std::vector< double > &x, const std::vector< double > &y, const std::string &path="", const std::string &title="")
Constructor from a vector of values with no errors.
Definition: Scatter2D.h:51
Scatter2D clone() const
Make a copy on the stack.
Definition: Scatter2D.h:122
Point2D Point
Type of the native Point2D collection.
Definition: Scatter2D.h:28
Point2D & point(size_t index)
Get a reference to the point with index index (non-const)
Definition: Scatter2D.h:197
size_t numPoints() const
Number of points in the scatter.
Definition: Scatter2D.h:179
double yMax(std::string source="") const
Get value plus positive y-error.
Definition: Point2D.h:275
size_t dim() const
Dimension of this data object.
Definition: Scatter2D.h:135
double xMin() const
Definition: Point2D.h:192
const std::vector< std::string > variations() const
Get the list of variations stored in the points.
Definition: Scatter2D.cc:72
void setYErrMinus(double eyminus, std::string source="")
Set negative y error.
Definition: Point2D.h:235
bool operator!=(const Scatter2D &other)
Non-equality operator.
Definition: Scatter2D.h:277
Scatter2D(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: Scatter2D.h:87
const std::string & annotation(const std::string &name) const
Get an annotation by name (as a string)
Scatter2D(const std::string &path="", const std::string &title="")
Empty constructor.
Definition: Scatter2D.h:37
void addPoint(double x, double y, double ex, double ey)
Insert a new point, defined as the x/y value pair and symmetric errors.
Definition: Scatter2D.h:226
Scatter1D mkScatter(const Counter &c)
Make a Scatter1D representation of a Histo1D.
Definition: Scatter1D.cc:9
const std::string title() const
Get the AO title.
Scatter2D S2D
Convenience typedef.
Definition: Scatter2D.h:293
void setX(double x)
Set x value.
Definition: Point2D.h:109
const Point2D & point(size_t index) const
Get a reference to the point with index index (const)
Definition: Scatter2D.h:204
void addPoint(double x, double y, const std::pair< double, double > &ex, const std::pair< double, double > &ey)
Insert a new point, defined as the x/y value pair and asymmetric error pairs.
Definition: Scatter2D.h:232
AnalysisObject is the base class for histograms and scatters.
const Points & points() const
Get the collection of points (const)
Definition: Scatter2D.h:191
void flip(Scatter2D &s)
Exchange the x and y axes (operates in-place on s)
Definition: Scatter2D.h:397
Utils::sortedvector< Point2D > Points
Definition: Scatter2D.h:29
void reset()
Clear all points.
Definition: Scatter2D.h:142
Scatter2D(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &ex, const std::vector< double > &ey, const std::string &path="", const std::string &title="")
Constructor from vectors of values with symmetric errors on x and y.
Definition: Scatter2D.h:61
void addPoint(double x, double y, double exminus, double explus, double eyminus, double eyplus)
Insert a new point, defined as the x/y value pair and asymmetric errors.
Definition: Scatter2D.h:238
void transformX(Scatter1D &s, FNX fx)
Apply transformation fx(x) to all values and error positions (operates in-place on s) ...
Definition: Scatter1D.h:308
A one-dimensional profile histogram.
Definition: Profile1D.h:33
double yMin(std::string source="") const
Get value minus negative y-error.
Definition: Point2D.h:269
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.
void addPoints(const Points &pts)
Insert a collection of new points.
Definition: Scatter2D.h:245
void scaleXY(double scalex, double scaley)
Scaling of both axes.
Definition: Scatter2D.h:157
std::vector< std::string > annotations() const
void setXErrMinus(double exminus)
Set negative x error.
Definition: Point2D.h:157
virtual AnalysisObject & operator=(const AnalysisObject &ao)
Default copy assignment operator.
void transformY(Scatter2D &s, FNY fy)
Apply transformation fy(y) to all values and error positions (operates in-place on s) ...
Definition: Scatter2D.h:375
void combineWith(const std::vector< Scatter2D > &others)
Definition: Scatter2D.h:263
Points & points()
Get the collection of points (non-const)
Definition: Scatter2D.h:185
std::shared_ptr< Scatter2D > Ptr
Definition: Scatter2D.h:30
A 2D data point to be contained in a Scatter2D.
Definition: Point2D.h:18