yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
Scatter3D.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_SCATTER3D_H
7 #define YODA_SCATTER3D_H
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/Point3D.h"
12 #include <utility>
13 #include <memory>
14 
15 namespace YODA {
16 
17 
18  // Forward declarations
19  class Histo2D;
20  class Profile2D;
21 
22 
24  class Scatter3D : public AnalysisObject {
25  public:
26 
28  typedef Point3D Point;
29  typedef Utils::sortedvector<Point3D> Points;
30  typedef std::shared_ptr<Scatter3D> Ptr;
31 
32 
34 
35 
37  Scatter3D(const std::string& path="", const std::string& title="")
38  : AnalysisObject("Scatter3D", path, title)
39  { }
40 
41 
43  Scatter3D(const Points& points,
44  const std::string& path="", const std::string& title="")
45  : AnalysisObject("Scatter3D", path, title),
46  _points(points)
47  {
48  std::sort(_points.begin(), _points.end());
49  }
50 
51 
53  Scatter3D(const std::vector<double>& x,
54  const std::vector<double>& y,
55  const std::vector<double>& z,
56  const std::string& path="",
57  const std::string& title="")
58  : AnalysisObject("Scatter3D", path, title)
59  {
60  if (x.size() != y.size() || y.size() != z.size()) {
61  throw RangeError("There are different numbers of x, y, and z values in the provided vectors.");
62  }
63  const std::pair<double,double> nullerr = std::make_pair(0.0, 0.0);
64  for (size_t i = 0; i < x.size(); ++i) {
65  addPoint(Point3D(x[i], y[i], z[i], nullerr, nullerr, nullerr));
66  }
67  std::sort(_points.begin(), _points.end());
68  }
69 
70 
72  Scatter3D(const std::vector<double>& x, const std::vector<double>& y, const std::vector<double>& z,
73  const std::vector<std::pair<double,double> >& ex, const std::vector<std::pair<double,double> >& ey, const std::vector<std::pair<double,double> >& ez,
74  const std::string& path="", const std::string& title="")
75  : AnalysisObject("Scatter3D", path, title)
76  {
77  if (x.size() != y.size() || y.size() != z.size()) {
78  throw RangeError("There are different numbers of x, y, and z values in the provided vectors.");
79  }
80  if (x.size() != ex.size() || y.size() != ey.size() || z.size() != ez.size()) {
81  throw RangeError("The sizes of the provided error vectors don't match the corresponding x, y, or z value vectors.");
82  }
83  for (size_t i = 0; i < x.size(); ++i) {
84  addPoint(Point3D(x[i], y[i], z[i], ex[i], ey[i], ez[i]));
85  }
86  std::sort(_points.begin(), _points.end());
87  }
88 
89 
91  Scatter3D(const std::vector<double>& x, const std::vector<double>& y, const std::vector<double> z,
92  const std::vector<double>& exminus,
93  const std::vector<double>& explus,
94  const std::vector<double>& eyminus,
95  const std::vector<double>& eyplus,
96  const std::vector<double>& ezminus,
97  const std::vector<double>& ezplus,
98  const std::string& path="", const std::string& title="")
99  : AnalysisObject("Scatter3D", path, title)
100  {
101  if(x.size() != y.size() || y.size() != z.size() ||
102  x.size() != exminus.size() || x.size() != explus.size() ||
103  y.size() != eyminus.size() || y.size() != eyplus.size() ||
104  z.size() != ezminus.size() || z.size() != ezplus.size())
105  throw RangeError("There are either different amounts of points on x/y/z vectors or not every of these vectors has properly defined error vectors!");
106 
107  for (size_t i = 0; i < x.size(); ++i) {
108  addPoint(Point3D(x[i], y[i], z[i], exminus[i], explus[i], eyminus[i], eyplus[i], ezminus[i], ezplus[i]));
109  }
110 
111  std::sort(_points.begin(), _points.end());
112  }
113 
114 
117  Scatter3D(const Scatter3D& s3, const std::string& path="")
118  : AnalysisObject("Scatter3D", (path.size() == 0) ? s3.path() : path, s3, s3.title()),
119  _points(s3._points)
120  {
121  for ( auto &ann : annotations()){
122  setAnnotation(ann, annotation(ann));
123  }
124  }
125 
128  AnalysisObject::operator = (s3); //< AO treatment of paths etc.
129  _points = s3._points;
130  return *this;
131  }
132 
134  Scatter3D clone() const {
135  return Scatter3D(*this);
136  }
137 
139  Scatter3D* newclone() const {
140  return new Scatter3D(*this);
141  }
142 
144 
145 
147  size_t dim() const { return 3; }
148 
149 
151 
152 
154  void reset() {
155  _points.clear();
156  }
157 
159  void scaleX(double scalex) {
160  for (Point3D& p : _points) p.scaleX(scalex);
161  }
162 
164  void scaleY(double scaley) {
165  for (Point3D& p : _points) p.scaleY(scaley);
166  }
167 
169  void scaleZ(double scalez) {
170  for (Point3D& p : _points) p.scaleZ(scalez);
171  }
172 
174  void scaleXYZ(double scalex, double scaley, double scalez) {
175  for (Point3D& p : _points) p.scaleXYZ(scalex, scaley, scalez);
176  }
177 
180  void scale(double scalex, double scaley, double scalez) {
181  scaleXYZ(scalex, scaley, scalez);
182  }
183 
185 
187 
189  const std::vector<std::string> variations() const;
190 
191 
193 
194 
196  size_t numPoints() const {
197  return _points.size();
198  }
199 
200 
202  Points& points() {
203  return _points;
204  }
205 
206 
208  const Points& points() const {
209  return _points;
210  }
211 
212 
214  Point3D& point(size_t index) {
215  if (index >= numPoints()) throw RangeError("There is no point with this index");
216  return _points.at(index);
217  }
218 
219 
221  const Point3D& point(size_t index) const {
222  if (index >= numPoints()) throw RangeError("There is no point with such index!");
223  return _points.at(index);
224  }
225 
227 
228 
230 
231 
233  void addPoint(const Point3D& pt) {
234  _points.insert(pt);
235  }
236 
238  void addPoint(double x, double y, double z) {
239  _points.insert(Point3D(x, y, z));
240  }
241 
243  void addPoint(double x, double y, double z,
244  double ex, double ey, double ez) {
245  _points.insert(Point3D(x, y, z, ex, ey, ez));
246  }
247 
249  void addPoint(double x, double y, double z,
250  const std::pair<double,double>& ex, const std::pair<double,double>& ey, const std::pair<double,double>& ez) {
251  _points.insert(Point3D(x, y, z, ex, ey, ez));
252  }
253 
255  void addPoint(double x, double y, double z,
256  double exminus, double explus,
257  double eyminus, double eyplus,
258  double ezminus, double ezplus) {
259  _points.insert(Point3D(x, y, z, exminus, explus, eyminus, eyplus, ezminus, ezplus));
260  }
261 
263  void addPoints(const Points& pts) {
264  for (const Point3D& pt : pts) addPoint(pt);
265  }
266 
268 
269 
271  void combineWith(const Scatter3D& other) {
272  addPoints(other.points());
273  //return *this;
274  }
275 
276 
279  void combineWith(const std::vector<Scatter3D>& others) {
280  for (const Scatter3D& s : others) combineWith(s);
281  }
282 
283 
285  bool operator == (const Scatter3D& other) {
286  return _points == other._points;
287  }
288 
290  bool operator != (const Scatter3D& other) {
291  return ! operator == (other);
292  }
293 
294 
296 
297 
298 
299  private:
300 
301  Points _points;
302 
303  };
304 
305 
307  typedef Scatter3D S3D;
308 
309 
311 
312 
313  inline Scatter3D combine(const Scatter3D& a, const Scatter3D& b) {
314  Scatter3D rtn = a;
315  rtn.combineWith(b);
316  return rtn;
317  }
318 
319  inline Scatter3D combine(const std::vector<Scatter3D>& scatters) {
320  Scatter3D rtn;
321  rtn.combineWith(scatters);
322  return rtn;
323  }
324 
326 
327 
329 
330 
332 
333 
338  Scatter3D mkScatter(const Histo2D& h, bool usefocus=false);
339 
346  Scatter3D mkScatter(const Profile2D& p, bool usefocus=false, bool usestddev=false);
347 
350  inline Scatter3D mkScatter(const Scatter3D& s) { return Scatter3D(s); }
351  // /// @note The usefocus arg is just for consistency and has no effect for Scatter -> Scatter
352  //inline Scatter3D mkScatter(const Scatter3D& s, bool) { return mkScatter(s); }
353 
355 
356 
358 
359 
361 
362 
366  template<typename FNX>
367  inline void transformX(Scatter3D& s, FNX fx) {
368  for (size_t i = 0; i < s.numPoints(); ++i) {
369  Point3D& p = s.point(i);
370  const double newx = fx(p.x());
371  const double fx_xmin = fx(p.xMin());
372  const double fx_xmax = fx(p.xMax());
373  // Deal with possible inversions of min/max ordering under the transformation
374  const double newxmin = std::min(fx_xmin, fx_xmax);
375  const double newxmax = std::max(fx_xmin, fx_xmax);
376  // Set new point x values
377  p.setX(newx);
379  p.setXErrMinus(newx - newxmin);
380  p.setXErrPlus(newxmax - newx);
381  }
382  }
383 
384 
388  template<typename FNY>
389  inline void transformY(Scatter3D& s, FNY fy) {
390  for (size_t i = 0; i < s.numPoints(); ++i) {
391  Point3D& p = s.point(i);
392  const double newy = fy(p.y());
393  const double fy_ymin = fy(p.yMin());
394  const double fy_ymax = fy(p.yMax());
395  // Deal with possible inversions of min/max ordering under the transformation
396  const double newymin = std::min(fy_ymin, fy_ymax);
397  const double newymax = std::max(fy_ymin, fy_ymax);
398  // Set new point y values
399  p.setY(newy);
401  p.setYErrMinus(newy - newymin);
402  p.setYErrPlus(newymax - newy);
403  }
404  }
405 
406 
410  template<typename FNZ>
411  inline void transformZ(Scatter3D& s, FNZ fz) {
412  for (size_t i = 0; i < s.numPoints(); ++i) {
413  Point3D& p = s.point(i);
414  const double newz = fz(p.z());
415  const double fz_zmin = fz(p.zMin());
416  const double fz_zmax = fz(p.zMax());
417  // Deal with possible inversions of min/max ordering under the transformation
418  const double newzmin = std::min(fz_zmin, fz_zmax);
419  const double newzmax = std::max(fz_zmin, fz_zmax);
420  // Set new point z values
421  p.setZ(newz);
423  p.setZErrMinus(newz - newzmin);
424  p.setZErrPlus(newzmax - newz);
425  }
426  }
427 
428 
430 
432 
433 
434 }
435 
436 #endif
bool operator==(const Scatter3D &other)
Equality operator.
Definition: Scatter3D.h:285
Utils::sortedvector< Point3D > Points
Definition: Scatter3D.h:29
Scatter3D & operator=(const Scatter3D &s3)
Assignment operator.
Definition: Scatter3D.h:127
Scatter1D combine(const Scatter1D &a, const Scatter1D &b)
Definition: Scatter1D.h:266
size_t dim() const
Dimension of this data object.
Definition: Scatter3D.h:147
double z() const
Get z value.
Definition: Point3D.h:109
void setYErrMinus(double eyminus)
Set negative y error.
Definition: Point3D.h:220
const std::vector< std::string > variations() const
Get the list of variations stored in the points.
Definition: Scatter3D.cc:109
void reset()
Clear all points.
Definition: Scatter3D.h:154
const std::string path() const
Get the AO path.
const Point3D & point(size_t index) const
Get the point with index index (const version)
Definition: Scatter3D.h:221
void combineWith(const std::vector< Scatter3D > &others)
Definition: Scatter3D.h:279
void scale(double scalex, double scaley, double scalez)
Definition: Scatter3D.h:180
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
Scatter3D * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Scatter3D.h:139
Scatter3D S3D
Convenience typedef.
Definition: Scatter3D.h:307
double yMax() const
Get value plus positive y-error.
Definition: Point3D.h:257
size_t numPoints() const
Number of points in the scatter.
Definition: Scatter3D.h:196
Scatter3D(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z, const std::vector< std::pair< double, double > > &ex, const std::vector< std::pair< double, double > > &ey, const std::vector< std::pair< double, double > > &ez, const std::string &path="", const std::string &title="")
Constructor from vectors of values with asymmetric errors on both x and y.
Definition: Scatter3D.h:72
double y() const
Get y value.
Definition: Point3D.h:103
void transformZ(Scatter3D &s, FNZ fz)
Apply transformation fz(z) to all values and error positions (operates in-place on s) ...
Definition: Scatter3D.h:411
void setZErrMinus(double ezminus, std::string source="")
Set negative z error.
Definition: Point3D.h:293
void addPoint(double x, double y, double z, const std::pair< double, double > &ex, const std::pair< double, double > &ey, const std::pair< double, double > &ez)
Insert a new point, defined as the x/y/z value triplet and asymmetric error pairs.
Definition: Scatter3D.h:249
Point3D & point(size_t index)
Get a reference to the point with index index.
Definition: Scatter3D.h:214
A two-dimensional profile histogram.
Definition: Profile2D.h:31
A 3D data point to be contained in a Scatter3D.
Definition: Point3D.h:18
void scaleY(double scaley)
Scaling of y axis.
Definition: Scatter3D.h:164
void setXErrPlus(double explus)
Set positive x error.
Definition: Point3D.h:157
Scatter3D(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z, const std::string &path="", const std::string &title="")
Constructor from vectors of values with no errors.
Definition: Scatter3D.h:53
void addPoints(const Points &pts)
Insert a collection of new points.
Definition: Scatter3D.h:263
void combineWith(const Scatter3D &other)
Definition: Scatter3D.h:271
const std::string & annotation(const std::string &name) const
Get an annotation by name (as a string)
Scatter3D(const Points &points, const std::string &path="", const std::string &title="")
Constructor from a set of points.
Definition: Scatter3D.h:43
Scatter3D(const std::string &path="", const std::string &title="")
Empty constructor.
Definition: Scatter3D.h:37
double xMax() const
Get value plus positive x-error.
Definition: Point3D.h:189
Scatter3D(const Scatter3D &s3, const std::string &path="")
Definition: Scatter3D.h:117
double zMax(std::string source="") const
Get value plus positive z-error.
Definition: Point3D.h:333
Scatter1D mkScatter(const Counter &c)
Make a Scatter1D representation of a Histo1D.
Definition: Scatter1D.cc:9
void scaleX(double scalex)
Scaling of x axis.
Definition: Scatter3D.h:159
const std::string title() const
Get the AO title.
void setYErrPlus(double eyplus)
Set positive y error.
Definition: Point3D.h:225
void scaleXYZ(double scalex, double scaley, double scalez)
Scaling of all three axes.
Definition: Scatter3D.h:174
std::shared_ptr< Scatter3D > Ptr
Definition: Scatter3D.h:30
double yMin() const
Get value minus negative y-error.
Definition: Point3D.h:252
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:24
void setZErrPlus(double ezplus, std::string source="")
Set positive z error.
Definition: Point3D.h:299
AnalysisObject is the base class for histograms and scatters.
void setX(double x)
Set x value.
Definition: Point3D.h:100
Points & points()
Get the collection of points (non-const)
Definition: Scatter3D.h:202
Point3D Point
Types of the native Point3D collection.
Definition: Scatter3D.h:28
A two-dimensional histogram.
Definition: Histo2D.h:31
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
void setY(double y)
Set y value.
Definition: Point3D.h:106
bool operator!=(const Scatter3D &other)
Non-equality operator.
Definition: Scatter3D.h:290
void setXErrMinus(double exminus)
Set negative x error.
Definition: Point3D.h:152
double zMin(std::string source="") const
Get value minus negative z-error.
Definition: Point3D.h:327
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.
void setZ(double z)
Set z value.
Definition: Point3D.h:112
Scatter3D(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > z, const std::vector< double > &exminus, const std::vector< double > &explus, const std::vector< double > &eyminus, const std::vector< double > &eyplus, const std::vector< double > &ezminus, const std::vector< double > &ezplus, const std::string &path="", const std::string &title="")
Constructor from vectors of values with completely explicit asymmetric errors.
Definition: Scatter3D.h:91
double xMin() const
Get value minus negative x-error.
Definition: Point3D.h:184
void addPoint(const Point3D &pt)
Insert a new point.
Definition: Scatter3D.h:233
const Points & points() const
Get the collection of points (const)
Definition: Scatter3D.h:208
void addPoint(double x, double y, double z, double exminus, double explus, double eyminus, double eyplus, double ezminus, double ezplus)
Insert a new point, defined as the x/y/z value triplet and asymmetric errors.
Definition: Scatter3D.h:255
void scaleZ(double scalez)
Scaling of z axis.
Definition: Scatter3D.h:169
double x() const
Get x value.
Definition: Point3D.h:97
std::vector< std::string > annotations() const
Scatter3D clone() const
Make a copy on the stack.
Definition: Scatter3D.h:134
void addPoint(double x, double y, double z, double ex, double ey, double ez)
Insert a new point, defined as the x/y/z value triplet and symmetric errors.
Definition: Scatter3D.h:243
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 addPoint(double x, double y, double z)
Insert a new point, defined as the x/y/z value triplet and no errors.
Definition: Scatter3D.h:238