yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
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-2021 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/Scatter.h"
11 #include "YODA/Point3D.h"
13 #include <utility>
14 #include <memory>
15 
16 namespace YODA {
17 
18 
19  // Forward declarations
20  class Histo2D;
21  class Profile2D;
22 
23 
25  class Scatter3D : public AnalysisObject, public Scatter {
26  public:
27 
29  typedef Point3D Point;
30  typedef Utils::sortedvector<Point3D> Points;
31  typedef std::shared_ptr<Scatter3D> Ptr;
32 
33 
36 
38  Scatter3D(const std::string& path="", const std::string& title="")
39  : AnalysisObject("Scatter3D", path, title)
40  { }
41 
42 
44  Scatter3D(const Points& points,
45  const std::string& path="", const std::string& title="")
46  : AnalysisObject("Scatter3D", path, title),
47  _points(points)
48  {
49  std::sort(_points.begin(), _points.end());
50  }
51 
52 
54  Scatter3D(const std::vector<double>& x,
55  const std::vector<double>& y,
56  const std::vector<double>& z,
57  const std::string& path="",
58  const std::string& title="")
59  : AnalysisObject("Scatter3D", path, title)
60  {
61  if (x.size() != y.size() || y.size() != z.size()) {
62  throw RangeError("There are different numbers of x, y, and z values in the provided vectors.");
63  }
64  const std::pair<double,double> nullerr = std::make_pair(0.0, 0.0);
65  for (size_t i = 0; i < x.size(); ++i) {
66  addPoint(Point3D(x[i], y[i], z[i], nullerr, nullerr, nullerr));
67  }
68  std::sort(_points.begin(), _points.end());
69  }
70 
71 
73  Scatter3D(const std::vector<double>& x, const std::vector<double>& y, const std::vector<double>& z,
74  const std::vector<std::pair<double,double> >& ex, const std::vector<std::pair<double,double> >& ey, const std::vector<std::pair<double,double> >& ez,
75  const std::string& path="", const std::string& title="")
76  : AnalysisObject("Scatter3D", path, title)
77  {
78  if (x.size() != y.size() || y.size() != z.size()) {
79  throw RangeError("There are different numbers of x, y, and z values in the provided vectors.");
80  }
81  if (x.size() != ex.size() || y.size() != ey.size() || z.size() != ez.size()) {
82  throw RangeError("The sizes of the provided error vectors don't match the corresponding x, y, or z value vectors.");
83  }
84  for (size_t i = 0; i < x.size(); ++i) {
85  addPoint(Point3D(x[i], y[i], z[i], ex[i], ey[i], ez[i]));
86  }
87  std::sort(_points.begin(), _points.end());
88  }
89 
90 
92  Scatter3D(const std::vector<double>& x, const std::vector<double>& y, const std::vector<double> z,
93  const std::vector<double>& exminus,
94  const std::vector<double>& explus,
95  const std::vector<double>& eyminus,
96  const std::vector<double>& eyplus,
97  const std::vector<double>& ezminus,
98  const std::vector<double>& ezplus,
99  const std::string& path="", const std::string& title="")
100  : AnalysisObject("Scatter3D", path, title)
101  {
102  if(x.size() != y.size() || y.size() != z.size() ||
103  x.size() != exminus.size() || x.size() != explus.size() ||
104  y.size() != eyminus.size() || y.size() != eyplus.size() ||
105  z.size() != ezminus.size() || z.size() != ezplus.size())
106  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!");
107 
108  for (size_t i = 0; i < x.size(); ++i) {
109  addPoint(Point3D(x[i], y[i], z[i], exminus[i], explus[i], eyminus[i], eyplus[i], ezminus[i], ezplus[i]));
110  }
111 
112  std::sort(_points.begin(), _points.end());
113  }
114 
115 
118  Scatter3D(const Scatter3D& s3, const std::string& path="")
119  : AnalysisObject("Scatter3D", (path.size() == 0) ? s3.path() : path, s3, s3.title()),
120  _points(s3._points)
121  {
122  for ( auto &ann : annotations()){
123  setAnnotation(ann, annotation(ann));
124  }
125  }
126 
129  AnalysisObject::operator = (s3); //< AO treatment of paths etc.
130  _points = s3._points;
131  return *this;
132  }
133 
135  Scatter3D clone() const {
136  return Scatter3D(*this);
137  }
138 
140  Scatter3D* newclone() const {
141  return new Scatter3D(*this);
142  }
143 
145 
146 
148  size_t dim() const { return 3; }
149 
150 
153 
155  void reset() {
156  _points.clear();
157  }
158 
160  void scaleX(double scalex) {
161  for (Point3D& p : _points) p.scaleX(scalex);
162  }
163 
165  void scaleY(double scaley) {
166  for (Point3D& p : _points) p.scaleY(scaley);
167  }
168 
170  void scaleZ(double scalez) {
171  for (Point3D& p : _points) p.scaleZ(scalez);
172  }
173 
175  void scaleXYZ(double scalex, double scaley, double scalez) {
176  for (Point3D& p : _points) p.scaleXYZ(scalex, scaley, scalez);
177  }
178 
180  void scale(size_t i, double scale) {
181  switch (i) {
182  case 1: scaleX(scale); break;
183  case 2: scaleY(scale); break;
184  case 3: scaleZ(scale); break;
185  default: throw RangeError("Invalid axis int, must be in range 1..dim");
186  }
187  }
188 
190 
192 
193  void parseVariations();
194 
196  std::vector<std::string> variations() const;
197 
198 
201 
203  size_t numPoints() const {
204  return _points.size();
205  }
206 
207 
209  Points& points() {
210  return _points;
211  }
212 
213 
215  const Points& points() const {
216  return _points;
217  }
218 
219 
221  Point3D& point(size_t index) {
222  if (index >= numPoints()) throw RangeError("There is no point with this index");
223  return _points.at(index);
224  }
225 
226 
228  const Point3D& point(size_t index) const {
229  if (index >= numPoints()) throw RangeError("There is no point with such index!");
230  return _points.at(index);
231  }
232 
234 
235 
238 
240  void addPoint(const Point3D& pt) {
241  _points.insert(pt);
242  }
243 
245  void addPoint(double x, double y, double z) {
246  Point3D thisPoint = Point3D(x, y, z);
247  thisPoint.setParent(this);
248  _points.insert(thisPoint);
249  }
250 
252  void addPoint(double x, double y, double z,
253  double ex, double ey, double ez) {
254  Point3D thisPoint = Point3D(x, y, z, ex, ey, ez);
255  thisPoint.setParent(this);
256  _points.insert(thisPoint);
257  }
258 
260  void addPoint(double x, double y, double z,
261  const std::pair<double,double>& ex, const std::pair<double,double>& ey, const std::pair<double,double>& ez) {
262  Point3D thisPoint = Point3D(x, y, z, ex, ey, ez);
263  thisPoint.setParent(this);
264  _points.insert(thisPoint);
265  }
266 
268  void addPoint(double x, double y, double z,
269  double exminus, double explus,
270  double eyminus, double eyplus,
271  double ezminus, double ezplus) {
272  Point3D thisPoint = Point3D(x, y, z, exminus, explus, eyminus, eyplus, ezminus, ezplus);
273  thisPoint.setParent(this);
274  _points.insert(thisPoint);
275  }
276 
278  void addPoints(const Points& pts) {
279  for (const Point3D& pt : pts) addPoint(pt);
280  }
281 
283 
284 
287 
289  void rmPoint(size_t index) {
290  _points.erase(_points.begin()+index);
291  }
292 
293  // /// Remove the points with indices @a indices
294  // void rmPoints(std::vector<size_t> indices) {
295  // // reverse-sort so the erasure-loop doesn't invalidate the indices
296  // std::sort(indices.begin(), indices.end(), std::greater<size_t>());
297  // for (size_t i : indices) rmPoint(i);
298  // }
299 
301 
302 
304  void combineWith(const Scatter3D& other) {
305  addPoints(other.points());
306  //return *this;
307  }
308 
309 
312  void combineWith(const std::vector<Scatter3D>& others) {
313  for (const Scatter3D& s : others) combineWith(s);
314  }
315 
316 
318  bool operator == (const Scatter3D& other) {
319  return _points == other._points;
320  }
321 
323  bool operator != (const Scatter3D& other) {
324  return ! operator == (other);
325  }
326 
327 
329 
330 
331 
332  private:
333 
334  Points _points;
335 
336  bool _variationsParsed =false ;
337 
338  };
339 
340 
342  typedef Scatter3D S3D;
343 
344 
347 
348  inline Scatter3D combine(const Scatter3D& a, const Scatter3D& b) {
349  Scatter3D rtn = a;
350  rtn.combineWith(b);
351  return rtn;
352  }
353 
354  inline Scatter3D combine(const std::vector<Scatter3D>& scatters) {
355  Scatter3D rtn;
356  rtn.combineWith(scatters);
357  return rtn;
358  }
359 
361 
362 
364 
365 
368 
373  Scatter3D mkScatter(const Histo2D& h, bool usefocus=false, bool binareadiv=true);
374 
381  Scatter3D mkScatter(const Profile2D& p, bool usefocus=false, bool usestddev=false);
382 
385  inline Scatter3D mkScatter(const Scatter3D& s) { return Scatter3D(s); }
386  // /// @note The usefocus arg is just for consistency and has no effect for Scatter -> Scatter
387  //inline Scatter3D mkScatter(const Scatter3D& s, bool) { return mkScatter(s); }
388 
390 
391 
393 
394 
397 
401  template<typename FNX>
402  inline void transformX(Scatter3D& s, FNX fx) {
403  for (size_t i = 0; i < s.numPoints(); ++i) {
404  Point3D& p = s.point(i);
405  const double newx = fx(p.x());
406  const double fx_xmin = fx(p.xMin());
407  const double fx_xmax = fx(p.xMax());
408  // Deal with possible inversions of min/max ordering under the transformation
409  const double newxmin = std::min(fx_xmin, fx_xmax);
410  const double newxmax = std::max(fx_xmin, fx_xmax);
411  // Set new point x values
412  p.setX(newx);
414  p.setXErrMinus(newx - newxmin);
415  p.setXErrPlus(newxmax - newx);
416  }
417  }
418 
419 
423  template<typename FNY>
424  inline void transformY(Scatter3D& s, FNY fy) {
425  for (size_t i = 0; i < s.numPoints(); ++i) {
426  Point3D& p = s.point(i);
427  const double newy = fy(p.y());
428  const double fy_ymin = fy(p.yMin());
429  const double fy_ymax = fy(p.yMax());
430  // Deal with possible inversions of min/max ordering under the transformation
431  const double newymin = std::min(fy_ymin, fy_ymax);
432  const double newymax = std::max(fy_ymin, fy_ymax);
433  // Set new point y values
434  p.setY(newy);
436  p.setYErrMinus(newy - newymin);
437  p.setYErrPlus(newymax - newy);
438  }
439  }
440 
441 
445  template<typename FNZ>
446  inline void transformZ(Scatter3D& s, FNZ fz) {
447  for (size_t i = 0; i < s.numPoints(); ++i) {
448  Point3D& p = s.point(i);
449  const double newz = fz(p.z());
450  const double fz_zmin = fz(p.zMin());
451  const double fz_zmax = fz(p.zMax());
452  // Deal with possible inversions of min/max ordering under the transformation
453  const double newzmin = std::min(fz_zmin, fz_zmax);
454  const double newzmax = std::max(fz_zmin, fz_zmax);
455  // Set new point z values
456  p.setZ(newz);
458  p.setZErrMinus(newz - newzmin);
459  p.setZErrPlus(newzmax - newz);
460  }
461  }
462 
463 
465 
467 
468 
469 
470 }
471 
472 #endif
bool operator==(const Scatter3D &other)
Equality operator.
Definition: Scatter3D.h:318
Utils::sortedvector< Point3D > Points
Definition: Scatter3D.h:30
Scatter3D & operator=(const Scatter3D &s3)
Assignment operator.
Definition: Scatter3D.h:128
Scatter1D combine(const Scatter1D &a, const Scatter1D &b)
Definition: Scatter1D.h:307
size_t dim() const
Dimension of this data object.
Definition: Scatter3D.h:148
double z() const
Get z value.
Definition: Point3D.h:112
void setYErrMinus(double eyminus)
Set negative y error.
Definition: Point3D.h:223
void reset()
Clear all points.
Definition: Scatter3D.h:155
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:228
void combineWith(const std::vector< Scatter3D > &others)
Definition: Scatter3D.h:312
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:140
Scatter3D S3D
Convenience typedef.
Definition: Scatter3D.h:342
double yMax() const
Get value plus positive y-error.
Definition: Point3D.h:260
size_t numPoints() const
Number of points in the scatter.
Definition: Scatter3D.h:203
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:73
double y() const
Get y value.
Definition: Point3D.h:106
void setParent(Scatter *parent)
Definition: Point.h:114
void transformZ(Scatter3D &s, FNZ fz)
Apply transformation fz(z) to all values and error positions (operates in-place on s) ...
Definition: Scatter3D.h:446
void setZErrMinus(double ezminus, std::string source="")
Set negative z error.
Definition: Point3D.h:300
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:260
void parseVariations()
Definition: Scatter3D.cc:123
std::vector< std::string > variations() const
Get the list of variations stored in the points.
Definition: Scatter3D.cc:145
Point3D & point(size_t index)
Get a reference to the point with index index.
Definition: Scatter3D.h:221
A two-dimensional profile histogram.
Definition: Profile2D.h:32
A 3D data point to be contained in a Scatter3D.
Definition: Point3D.h:18
void rmPoint(size_t index)
Remove the point with index index.
Definition: Scatter3D.h:289
void scaleY(double scaley)
Scaling of y axis.
Definition: Scatter3D.h:165
void setXErrPlus(double explus)
Set positive x error.
Definition: Point3D.h:160
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:54
void addPoints(const Points &pts)
Insert a collection of new points.
Definition: Scatter3D.h:278
void combineWith(const Scatter3D &other)
Definition: Scatter3D.h:304
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:44
Scatter3D(const std::string &path="", const std::string &title="")
Empty constructor.
Definition: Scatter3D.h:38
double xMax() const
Get value plus positive x-error.
Definition: Point3D.h:192
Scatter3D(const Scatter3D &s3, const std::string &path="")
Definition: Scatter3D.h:118
double zMax(std::string source="") const
Get value plus positive z-error.
Definition: Point3D.h:341
Scatter1D mkScatter(const Counter &c)
Make a Scatter1D representation of a Histo1D.
Definition: Scatter1D.cc:13
void scaleX(double scalex)
Scaling of x axis.
Definition: Scatter3D.h:160
const std::string title() const
Get the AO title.
void setYErrPlus(double eyplus)
Set positive y error.
Definition: Point3D.h:228
void scaleXYZ(double scalex, double scaley, double scalez)
Scaling of all three axes.
Definition: Scatter3D.h:175
std::shared_ptr< Scatter3D > Ptr
Definition: Scatter3D.h:31
double yMin() const
Get value minus negative y-error.
Definition: Point3D.h:255
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:25
void setZErrPlus(double ezplus, std::string source="")
Set positive z error.
Definition: Point3D.h:306
AnalysisObject is the base class for histograms and scatters.
A base class for common operations on scatter types (Scatter1D, etc.)
Definition: Scatter.h:15
void setX(double x)
Set x value.
Definition: Point3D.h:103
Points & points()
Get the collection of points (non-const)
Definition: Scatter3D.h:209
Point3D Point
Types of the native Point3D collection.
Definition: Scatter3D.h:29
A two-dimensional histogram.
Definition: Histo2D.h:32
void scale(size_t i, double scale)
Scaling along direction i.
Definition: Scatter3D.h:180
void transformX(Scatter1D &s, FNX fx)
Apply transformation fx(x) to all values and error positions (operates in-place on s) ...
Definition: Scatter1D.h:349
void setY(double y)
Set y value.
Definition: Point3D.h:109
bool operator!=(const Scatter3D &other)
Non-equality operator.
Definition: Scatter3D.h:323
void setXErrMinus(double exminus)
Set negative x error.
Definition: Point3D.h:155
double zMin(std::string source="") const
Get value minus negative z-error.
Definition: Point3D.h:334
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:115
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:92
double xMin() const
Get value minus negative x-error.
Definition: Point3D.h:187
void addPoint(const Point3D &pt)
Insert a new point.
Definition: Scatter3D.h:240
const Points & points() const
Get the collection of points (const)
Definition: Scatter3D.h:215
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:268
void scaleZ(double scalez)
Scaling of z axis.
Definition: Scatter3D.h:170
double x() const
Get x value.
Definition: Point3D.h:100
std::vector< std::string > annotations() const
Scatter3D clone() const
Make a copy on the stack.
Definition: Scatter3D.h:135
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:252
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:416
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:245