yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
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-2017 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 #include <memory>
14 
15 namespace YODA {
16 
17 
18  // Forward declarations
19  class Counter;
20 
21 
23  class Scatter1D : public AnalysisObject {
24  public:
25 
27  typedef Point1D Point;
28  typedef Utils::sortedvector<Point1D> Points;
29  typedef std::shared_ptr<Scatter1D> Ptr;
30 
31 
33 
34 
36  Scatter1D(const std::string& path="", const std::string& title="")
37  : AnalysisObject("Scatter1D", path, title)
38  { }
39 
40 
42  Scatter1D(const Points& points,
43  const std::string& path="", const std::string& title="")
44  : AnalysisObject("Scatter1D", path, title),
45  _points(points)
46  { }
47 
48 
50  Scatter1D(const std::vector<double>& x,
51  const std::string& path="", const std::string& title="")
52  : AnalysisObject("Scatter1D", path, title)
53  {
54  for (size_t i = 0; i < x.size(); ++i) addPoint(x[i]);
55  }
56 
57 
59  Scatter1D(const std::vector<double>& x, const std::vector<double>& ex,
60  const std::string& path="", const std::string& title="")
61  : AnalysisObject("Scatter1D", path, title)
62  {
63  if (x.size() != ex.size()) throw UserError("x and ex vectors must have same length");
64  for (size_t i = 0; i < x.size(); ++i) addPoint(x[i], ex[i]);
65  }
66 
68  Scatter1D(const std::vector<double>& x, const std::vector<std::pair<double,double> >& ex,
69  const std::string& path="", const std::string& title="")
70  : AnalysisObject("Scatter1D", path, title)
71  {
72  if (x.size() != ex.size()) throw UserError("x and ex vectors must have same length");
73  for (size_t i = 0; i < x.size(); ++i) addPoint(Point1D(x[i], ex[i]));
74  }
75 
76 
78  Scatter1D(const std::vector<double>& x,
79  const std::vector<double>& exminus,
80  const std::vector<double>& explus,
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  for ( auto &ann : annotations()){
97  setAnnotation(ann, annotation(ann));
98  }
99  }
100 
101 
104  AnalysisObject::operator = (s1); //< AO treatment of paths etc.
105  _points = s1._points;
106  return *this;
107  }
108 
110  Scatter1D clone() const {
111  return Scatter1D(*this);
112  }
113 
115  Scatter1D* newclone() const {
116  return new Scatter1D(*this);
117  }
118 
120 
121 
123  size_t dim() const { return 1; }
124 
125 
127 
128 
130  void reset() {
131  _points.clear();
132  }
133 
135  void scaleX(double scalex) {
136  for (Point1D& p : _points) p.scaleX(scalex);
137  }
138 
140 
141 
143 
145  const std::vector<std::string> variations() const ;
146 
148 
149 
151  size_t numPoints() const {
152  return _points.size();
153  }
154 
155 
157  Points& points() {
158  return _points;
159  }
160 
161 
163  const Points& points() const {
164  return _points;
165  }
166 
167 
169  Point1D& point(size_t index) {
170  if (index >= numPoints()) throw RangeError("There is no point with this index");
171  return _points.at(index);
172  }
173 
174 
176  const Point1D& point(size_t index) const {
177  if (index >= numPoints()) throw RangeError("There is no point with this index");
178  return _points.at(index);
179  }
180 
182 
183 
185 
186 
188  void addPoint(const Point1D& pt) {
189  _points.insert(pt);
190  }
191 
193  void addPoint(double x) {
194  _points.insert(Point1D(x));
195  }
196 
198  void addPoint(double x, double ex) {
199  _points.insert(Point1D(x, ex));
200  }
201 
203  void addPoint(double x, const std::pair<double,double>& ex) {
204  _points.insert(Point1D(x, ex));
205  }
206 
208  void addPoint(double x, double exminus, double explus) {
209  _points.insert(Point1D(x, exminus, explus));
210  }
211 
213  void addPoints(const Points& pts) {
214  for (const Point1D& pt : pts) addPoint(pt);
215  }
216 
218 
219 
221 
222 
224  void combineWith(const Scatter1D& other) {
225  addPoints(other.points());
226  }
227 
230  void combineWith(const std::vector<Scatter1D>& others) {
231  for (const Scatter1D& s : others) combineWith(s);
232  }
233 
235 
236 
238  bool operator == (const Scatter1D& other) {
239  return _points == other._points;
240  }
241 
243  bool operator != (const Scatter1D& other) {
244  return ! operator == (other);
245  }
246 
247 
249 
250 
251 
252  private:
253 
254  Points _points;
255 
256  };
257 
258 
260  typedef Scatter1D S1D;
261 
262 
264 
265 
266  inline Scatter1D combine(const Scatter1D& a, const Scatter1D& b) {
267  Scatter1D rtn = a;
268  rtn.combineWith(b);
269  return rtn;
270  }
271 
272  inline Scatter1D combine(const std::vector<Scatter1D>& scatters) {
273  Scatter1D rtn;
274  rtn.combineWith(scatters);
275  return rtn;
276  }
277 
279 
281 
282 
284 
285 
287  Scatter1D mkScatter(const Counter& c);
288 
291  inline Scatter1D mkScatter(const Scatter1D& s) {
292  return Scatter1D(s);
293  }
294 
296 
297 
299 
300 
302 
303 
307  template<typename FNX>
308  inline void transformX(Scatter1D& s, FNX fx) {
309  for (size_t i = 0; i < s.numPoints(); ++i) {
310  Point1D& p = s.point(i);
311  const double newx = fx(p.x());
312  const double fx_xmin = fx(p.xMin());
313  const double fx_xmax = fx(p.xMax());
314  // Deal with possible inversions of min/max ordering under the transformation
315  const double newxmin = std::min(fx_xmin, fx_xmax);
316  const double newxmax = std::max(fx_xmin, fx_xmax);
317  // Set new point x values
318  p.setX(newx);
320  p.setXErrMinus(newx - newxmin);
321  p.setXErrPlus(newxmax - newx);
322  }
323  }
324 
326 
327 
328 }
329 
330 #endif
double x() const
Get x value.
Definition: Point1D.h:78
void addPoint(double x, double exminus, double explus)
Insert a new point, defined as the x value and explicit asymmetric errors.
Definition: Scatter1D.h:208
Scatter1D combine(const Scatter1D &a, const Scatter1D &b)
Definition: Scatter1D.h:266
Scatter1D & operator=(const Scatter1D &s1)
Assignment operator.
Definition: Scatter1D.h:103
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:68
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:50
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:115
size_t numPoints() const
Number of points in the scatter.
Definition: Scatter1D.h:151
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:203
void addPoint(const Point1D &pt)
Insert a new point.
Definition: Scatter1D.h:188
bool operator!=(const Scatter1D &other)
Non-equality operator.
Definition: Scatter1D.h:243
Scatter1D(const std::string &path="", const std::string &title="")
Empty constructor.
Definition: Scatter1D.h:36
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:78
double xMax(std::string source="") const
Get value plus positive x-error.
Definition: Point1D.h:156
A weighted counter.
Definition: Counter.h:23
void addPoint(double x, double ex)
Insert a new point, defined as the x value and symmetric errors.
Definition: Scatter1D.h:198
void addPoints(const Points &pts)
Insert a collection of new points.
Definition: Scatter1D.h:213
void addPoint(double x)
Insert a new point, defined as the x value and no errors.
Definition: Scatter1D.h:193
double xMin(std::string source="") const
Get value minus negative x-error.
Definition: Point1D.h:150
void reset()
Clear all points.
Definition: Scatter1D.h:130
void setXErrMinus(double exminus, std::string source="")
Set negative x error.
Definition: Point1D.h:116
Error for problems introduced outside YODA, to put it nicely.
Definition: Exceptions.h:100
bool operator==(const Scatter1D &other)
Equality operator.
Definition: Scatter1D.h:238
void scaleX(double scalex)
Scaling of x axis.
Definition: Scatter1D.h:135
void setXErrPlus(double explus, std::string source="")
Set positive x error.
Definition: Point1D.h:122
Point1D & point(size_t index)
Get a reference to the point with index index (non-const)
Definition: Scatter1D.h:169
void combineWith(const Scatter1D &other)
Definition: Scatter1D.h:224
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:230
const std::vector< std::string > variations() const
Get the list of variations stored in the points.
Definition: Scatter1D.cc:18
Scatter1D mkScatter(const Counter &c)
Make a Scatter1D representation of a Histo1D.
Definition: Scatter1D.cc:9
Utils::sortedvector< Point1D > Points
Definition: Scatter1D.h:28
const std::string title() const
Get the AO title.
AnalysisObject is the base class for histograms and scatters.
const Points & points() const
Get the collection of points (const)
Definition: Scatter1D.h:163
size_t dim() const
Dimension of this data object.
Definition: Scatter1D.h:123
Scatter1D clone() const
Make a copy on the stack.
Definition: Scatter1D.h:110
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:59
A very generic data type which is just a collection of 1D data points with errors.
Definition: Scatter1D.h:23
Points & points()
Get the collection of points (non-const)
Definition: Scatter1D.h:157
const Point1D & point(size_t index) const
Get a reference to the point with index index (const)
Definition: Scatter1D.h:176
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
Point1D Point
Type of the native Point1D collection.
Definition: Scatter1D.h:27
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:260
std::shared_ptr< Scatter1D > Ptr
Definition: Scatter1D.h:29
void setX(double x)
Set x value.
Definition: Point1D.h:81
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:42