yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
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-2021 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/Scatter.h"
11 #include "YODA/Point1D.h"
13 #include <utility>
14 #include <memory>
15 
16 namespace YODA {
17 
18 
19  // Forward declarations
20  class Counter;
21 
22 
26  class Scatter1D : public AnalysisObject, public Scatter {
27  public:
28 
30  typedef Point1D Point;
31  typedef Utils::sortedvector<Point1D> Points;
32  typedef std::shared_ptr<Scatter1D> Ptr;
33 
34 
37 
39  Scatter1D(const std::string& path="", const std::string& title="")
40  : AnalysisObject("Scatter1D", path, title)
41  { }
42 
43 
45  Scatter1D(const Points& points,
46  const std::string& path="", const std::string& title="")
47  : AnalysisObject("Scatter1D", path, title),
48  _points(points)
49  { }
50 
51 
53  Scatter1D(const std::vector<double>& x,
54  const std::string& path="", const std::string& title="")
55  : AnalysisObject("Scatter1D", path, title)
56  {
57  for (size_t i = 0; i < x.size(); ++i) addPoint(x[i]);
58  }
59 
60 
62  Scatter1D(const std::vector<double>& x, const std::vector<double>& ex,
63  const std::string& path="", const std::string& title="")
64  : AnalysisObject("Scatter1D", path, title)
65  {
66  if (x.size() != ex.size()) throw UserError("x and ex vectors must have same length");
67  for (size_t i = 0; i < x.size(); ++i) addPoint(x[i], ex[i]);
68  }
69 
71  Scatter1D(const std::vector<double>& x, const std::vector<std::pair<double,double> >& ex,
72  const std::string& path="", const std::string& title="")
73  : AnalysisObject("Scatter1D", path, title)
74  {
75  if (x.size() != ex.size()) throw UserError("x and ex vectors must have same length");
76  for (size_t i = 0; i < x.size(); ++i) addPoint(Point1D(x[i], ex[i]));
77  }
78 
79 
81  Scatter1D(const std::vector<double>& x,
82  const std::vector<double>& exminus,
83  const std::vector<double>& explus,
84  const std::string& path="", const std::string& title="")
85  : AnalysisObject("Scatter1D", path, title)
86  {
87  if (x.size() != exminus.size()) throw UserError("x and ex vectors must have same length");
88  if (exminus.size() != explus.size()) throw UserError("ex plus and minus vectors must have same length");
89  for (size_t i = 0; i < x.size(); ++i) addPoint(Point1D(x[i], exminus[i], explus[i]));
90  }
91 
92 
95  Scatter1D(const Scatter1D& s1, const std::string& path="")
96  : AnalysisObject("Scatter1D", (path.size() == 0) ? s1.path() : path, s1, s1.title()),
97  _points(s1._points)
98  {
99  for ( auto &ann : annotations()){
100  setAnnotation(ann, annotation(ann));
101  }
102  }
103 
104 
107  AnalysisObject::operator = (s1); //< AO treatment of paths etc.
108  _points = s1._points;
109  return *this;
110  }
111 
113  Scatter1D clone() const {
114  return Scatter1D(*this);
115  }
116 
118  Scatter1D* newclone() const {
119  return new Scatter1D(*this);
120  }
121 
123 
124 
126  size_t dim() const { return 1; }
127 
128 
131 
133  void reset() {
134  _points.clear();
135  }
136 
138  void scaleX(double scalex) {
139  for (Point1D& p : _points) p.scaleX(scalex);
140  }
141 
143  void scale(size_t i, double scale) {
144  switch (i) {
145  case 1: scaleX(scale); break;
146  default: throw RangeError("Invalid axis int, must be in range 1..dim");
147  }
148  }
149 
151 
152 
154 
155  void parseVariations();
156 
158  std::vector<std::string> variations() const;
159 
162 
164  size_t numPoints() const {
165  return _points.size();
166  }
167 
168 
170  Points& points() {
171  return _points;
172  }
173 
174 
176  const Points& points() const {
177  return _points;
178  }
179 
180 
182  Point1D& point(size_t index) {
183  if (index >= numPoints()) throw RangeError("There is no point with this index");
184  return _points.at(index);
185  }
186 
187 
189  const Point1D& point(size_t index) const {
190  if (index >= numPoints()) throw RangeError("There is no point with this index");
191  return _points.at(index);
192  }
193 
195 
196 
199 
201  void addPoint(const Point1D& pt) {
202  _points.insert(pt);
203  }
204 
206  void addPoint(double x) {
207  Point1D thisPoint = Point1D(x);
208  thisPoint.setParent(this);
209  _points.insert(thisPoint);
210  }
211 
213  void addPoint(double x, double ex) {
214  Point1D thisPoint = Point1D(x, ex);
215  thisPoint.setParent(this);
216  _points.insert(thisPoint);
217  }
218 
220  void addPoint(double x, const std::pair<double,double>& ex) {
221  Point1D thisPoint = Point1D(x, ex);
222  thisPoint.setParent(this);
223  _points.insert(thisPoint);
224  }
225 
227  void addPoint(double x, double exminus, double explus) {
228  Point1D thisPoint = Point1D(x, exminus, explus);
229  thisPoint.setParent(this);
230  _points.insert(thisPoint);
231  }
232 
234  void addPoints(const Points& pts) {
235  for (const Point1D& pt : pts) addPoint(pt);
236  }
237 
239 
240 
243 
245  void rmPoint(size_t index) {
246  _points.erase(_points.begin()+index);
247  }
248 
249  // /// Remove the points with indices @a indices
250  // void rmPoints(std::vector<size_t> indices) {
251  // // reverse-sort so the erasure-loop doesn't invalidate the indices
252  // std::sort(indices.begin(), indices.end(), std::greater<size_t>());
253  // for (size_t i : indices) rmPoint(i);
254  // }
255 
257 
258 
261 
263  void combineWith(const Scatter1D& other) {
264  addPoints(other.points());
265  }
266 
269  void combineWith(const std::vector<Scatter1D>& others) {
270  for (const Scatter1D& s : others) combineWith(s);
271  }
272 
274 
275 
277  bool operator == (const Scatter1D& other) {
278  return _points == other._points;
279  }
280 
282  bool operator != (const Scatter1D& other) {
283  return ! operator == (other);
284  }
285 
286 
288 
289 
290 
291  private:
292 
293  Points _points;
294 
295  bool _variationsParsed =false ;
296 
297  };
298 
299 
301  typedef Scatter1D S1D;
302 
303 
306 
307  inline Scatter1D combine(const Scatter1D& a, const Scatter1D& b) {
308  Scatter1D rtn = a;
309  rtn.combineWith(b);
310  return rtn;
311  }
312 
313  inline Scatter1D combine(const std::vector<Scatter1D>& scatters) {
314  Scatter1D rtn;
315  rtn.combineWith(scatters);
316  return rtn;
317  }
318 
320 
322 
323 
326 
328  Scatter1D mkScatter(const Counter& c);
329 
332  inline Scatter1D mkScatter(const Scatter1D& s) {
333  return Scatter1D(s);
334  }
335 
337 
338 
340 
341 
344 
348  template<typename FNX>
349  inline void transformX(Scatter1D& s, FNX fx) {
350  for (size_t i = 0; i < s.numPoints(); ++i) {
351  Point1D& p = s.point(i);
352  const double newx = fx(p.x());
353  const double fx_xmin = fx(p.xMin());
354  const double fx_xmax = fx(p.xMax());
355  // Deal with possible inversions of min/max ordering under the transformation
356  const double newxmin = std::min(fx_xmin, fx_xmax);
357  const double newxmax = std::max(fx_xmin, fx_xmax);
358  // Set new point x values
359  p.setX(newx);
361  p.setXErrMinus(newx - newxmin);
362  p.setXErrPlus(newxmax - newx);
363  }
364  }
365 
367 
368 
369 }
370 
371 #endif
double x() const
Get x value.
Definition: Point1D.h:81
void addPoint(double x, double exminus, double explus)
Insert a new point, defined as the x value and explicit asymmetric errors.
Definition: Scatter1D.h:227
Scatter1D combine(const Scatter1D &a, const Scatter1D &b)
Definition: Scatter1D.h:307
Scatter1D & operator=(const Scatter1D &s1)
Assignment operator.
Definition: Scatter1D.h:106
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:71
Scatter1D(const Scatter1D &s1, const std::string &path="")
Definition: Scatter1D.h:95
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:53
const std::string path() const
Get the AO path.
Scatter1D * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Scatter1D.h:118
size_t numPoints() const
Number of points in the scatter.
Definition: Scatter1D.h:164
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:220
void scale(size_t i, double scale)
Scaling along direction i.
Definition: Scatter1D.h:143
void addPoint(const Point1D &pt)
Insert a new point.
Definition: Scatter1D.h:201
bool operator!=(const Scatter1D &other)
Non-equality operator.
Definition: Scatter1D.h:282
Scatter1D(const std::string &path="", const std::string &title="")
Empty constructor.
Definition: Scatter1D.h:39
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
Scatter1D(const std::vector< double > &x, const std::vector< double > &exminus, const std::vector< double > &explus, const std::string &path="", const std::string &title="")
Constructor from values with completely explicit asymmetric errors.
Definition: Scatter1D.h:81
double xMax(std::string source="") const
Get value plus positive x-error.
Definition: Point1D.h:164
A weighted counter.
Definition: Counter.h:24
void addPoint(double x, double ex)
Insert a new point, defined as the x value and symmetric errors.
Definition: Scatter1D.h:213
void addPoints(const Points &pts)
Insert a collection of new points.
Definition: Scatter1D.h:234
void addPoint(double x)
Insert a new point, defined as the x value and no errors.
Definition: Scatter1D.h:206
void setParent(Scatter *parent)
Definition: Point.h:114
double xMin(std::string source="") const
Get value minus negative x-error.
Definition: Point1D.h:157
void reset()
Clear all points.
Definition: Scatter1D.h:133
void setXErrMinus(double exminus, std::string source="")
Set negative x error.
Definition: Point1D.h:123
Error for problems introduced outside YODA, to put it nicely.
Definition: Exceptions.h:100
bool operator==(const Scatter1D &other)
Equality operator.
Definition: Scatter1D.h:277
void parseVariations()
Definition: Scatter1D.cc:25
void scaleX(double scalex)
Scaling of x axis.
Definition: Scatter1D.h:138
void setXErrPlus(double explus, std::string source="")
Set positive x error.
Definition: Point1D.h:129
Point1D & point(size_t index)
Get a reference to the point with index index (non-const)
Definition: Scatter1D.h:182
void rmPoint(size_t index)
Remove the point with index index.
Definition: Scatter1D.h:245
void combineWith(const Scatter1D &other)
Definition: Scatter1D.h:263
A 1D data point to be contained in a Scatter1D.
Definition: Point1D.h:18
const std::string & annotation(const std::string &name) const
Get an annotation by name (as a string)
void combineWith(const std::vector< Scatter1D > &others)
Definition: Scatter1D.h:269
std::vector< std::string > variations() const
Get the list of variations stored in the points.
Definition: Scatter1D.cc:48
Scatter1D mkScatter(const Counter &c)
Make a Scatter1D representation of a Histo1D.
Definition: Scatter1D.cc:13
Utils::sortedvector< Point1D > Points
Definition: Scatter1D.h:31
const std::string title() const
Get the AO title.
AnalysisObject is the base class for histograms and scatters.
A base class for common operations on scatter types (Scatter1D, etc.)
Definition: Scatter.h:15
const Points & points() const
Get the collection of points (const)
Definition: Scatter1D.h:176
size_t dim() const
Dimension of this data object.
Definition: Scatter1D.h:126
Scatter1D clone() const
Make a copy on the stack.
Definition: Scatter1D.h:113
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:62
Points & points()
Get the collection of points (non-const)
Definition: Scatter1D.h:170
const Point1D & point(size_t index) const
Get a reference to the point with index index (const)
Definition: Scatter1D.h:189
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
Point1D Point
Type of the native Point1D collection.
Definition: Scatter1D.h:30
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.
Scatter1D S1D
Convenience typedef.
Definition: Scatter1D.h:301
std::shared_ptr< Scatter1D > Ptr
Definition: Scatter1D.h:32
void setX(double x)
Set x value.
Definition: Point1D.h:84
std::vector< std::string > annotations() const
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:45