yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
Profile2D.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_Profile2D_h
7 #define YODA_Profile2D_h
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/Fillable.h"
11 #include "YODA/Binned.h"
12 #include "YODA/ProfileBin2D.h"
13 #include "YODA/Dbn3D.h"
14 #include "YODA/Axis2D.h"
15 #include "YODA/Scatter3D.h"
16 #include "YODA/Exceptions.h"
17 #include <vector>
18 #include <tuple>
19 
20 namespace YODA {
21 
22 
23  // Forward declarations
24  class Histo2D;
25  class Scatter3D;
26 
29 
30 
32  class Profile2D : public AnalysisObject, public Fillable, public Binned {
33  public:
34 
36  typedef Profile2DAxis Axis;
37  typedef Axis::Bins Bins;
38  typedef ProfileBin2D Bin;
40 
41  typedef std::tuple<double, double, double> FillType;
42  typedef std::tuple<double, double> BinType;
43  typedef std::shared_ptr<Profile2D> Ptr;
44 
45 
48 
50  Profile2D(const std::string& path="", const std::string& title="")
51  : AnalysisObject("Profile2D", path, title),
52  _axis()
53  { }
54 
55 
57  Profile2D(size_t nbinsX, double lowerX, double upperX,
58  size_t nbinsY, double lowerY, double upperY,
59  const std::string& path="", const std::string& title="")
60  : AnalysisObject("Profile2D", path, title),
61  _axis(nbinsX, std::make_pair(lowerX, upperX), nbinsY, std::make_pair(lowerY, upperY))
62  { }
63 
64 
66  Profile2D(const std::vector<double>& xedges, const std::vector<double>& yedges,
67  const std::string& path="", const std::string& title="")
68  : AnalysisObject("Profile2D", path, title),
69  _axis(xedges, yedges)
70  { }
71 
72 
74  Profile2D(const std::vector<Bin>& bins,
75  const std::string& path="", const std::string& title="")
76  : AnalysisObject("Profile2D", path, title),
77  _axis(bins)
78  { }
79 
80 
83  Profile2D(const Profile2D& p, const std::string& path="");
84 
87  Profile2D(const Scatter3D& s, const std::string& path="");
88 
91  Profile2D(const Histo2D& h, const std::string& path="");
92 
96  Profile2D(const std::vector<ProfileBin2D>& bins,
97  const Dbn3D& totalDbn,
98  const Outflows& outflows,
99  const std::string& path="", const std::string& title="")
100  : AnalysisObject("Profile2D", path, title),
101  _axis(bins, totalDbn, outflows)
102  { }
103 
104 
107  AnalysisObject::operator = (p2); //< AO treatment of paths etc.
108  _axis = p2._axis;
109  return *this;
110  }
111 
112 
114  Profile2D clone() const {
115  return Profile2D(*this);
116  }
117 
119  Profile2D* newclone() const {
120  return new Profile2D(*this);
121  }
122 
124 
125 
129  size_t dim() const { return 2; }
130 
132  size_t fillDim() const { return 2; }
133 
134 
137 
139  virtual void fill(double x, double y, double z, double weight=1.0, double fraction=1.0);
140  virtual void fill(const FillType & xs, double weight=1.0, double fraction=1.0) {
141  fill(std::get<0>(xs), std::get<1>(xs), std::get<2>(xs), weight, fraction);
142  }
143 
145  virtual void fillBin(size_t i, double z, double weight=1.0, double fraction=1.0);
146 
147 
151  void reset() {
152  _axis.reset();
153  }
154 
156  void scaleW(double scalefactor) {
158  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
159  _axis.scaleW(scalefactor);
160  }
161 
163  void scaleZ(double scalefactor) {
164  _axis.totalDbn().scaleZ(scalefactor);
166  // _axis.overflow().scaleZ(scalefactor);
167  // _axis.underflow().scaleZ(scalefactor);
168  for (size_t i = 0; i < bins().size(); ++i)
169  bin(i).scaleZ(scalefactor);
170  }
171 
172 
174  // /// Merge together the bin range with indices from @a from to @a to, inclusive
175  // void mergeBins(size_t from, size_t to) {
176  // _axis.mergeBins(from, to);
177  // }
178 
180  // /// Merge every group of n bins, starting from the LHS
181  // void rebin(size_t n) {
182  // throw "IMPLEMENT!";
183  // //_axis.rebin(n);
184  // }
185 
187 
188 
191 
192  // /// @brief Bin addition operator
193  // ///
194  // /// Add a bin to the axis, described by its x and y ranges.
195  void addBin(Axis::EdgePair1D xrange, Axis::EdgePair1D yrange) {
196  _axis.addBin(xrange, yrange);
197  }
198 
199  // /// @brief Bin addition operator
200  // ///
201  // /// Add a bin to the axis, possibly pre-populated
202  void addBin(const Bin& bin) {
203  _axis.addBin(bin);
204  }
205 
209  void addBins(const Axis::Edges& xcuts, const Axis::Edges& ycuts) {
210  _axis.addBins(xcuts, ycuts);
211  }
212 
213 
217  void addBins(const Bins& bins) {
218  _axis.addBins(bins);
219  }
220 
222  bool sameBinning(const Profile2D& p2) {
223  return _axis == p2._axis;
224  }
225 
226 
228  // /// @brief Bin addition operator
229  // ///
230  // /// Add a set of bins delimiting coordinates of which are contained
231  // /// in binLimits vector.
232  // void addBin(const std::vector<Segment>& binLimits) {
233  // _axis.addBin(binLimits);
234  // }
235 
236  void rmBin(size_t index) {
237  _axis.eraseBin(index);
238  }
239 
241 
242 
245 
250  const std::vector<double> xEdges() const { return _axis.xEdges(); }
251 
256  const std::vector<double> yEdges() const { return _axis.yEdges(); }
257 
259 
261  double xMin() const { return _axis.xMin(); }
262 
264  double xMax() const { return _axis.xMax(); }
265 
266 
268  double yMin() const { return _axis.yMin(); }
269 
271  double yMax() const { return _axis.yMax(); }
272 
273 
275  std::vector<YODA::ProfileBin2D>& bins() { return _axis.bins(); }
276 
278  const std::vector<YODA::ProfileBin2D>& bins() const { return _axis.bins(); }
279 
280 
282  ProfileBin2D& bin(size_t index) { return _axis.bins()[index]; }
283 
285  const ProfileBin2D& bin(size_t index) const { return _axis.bins()[index]; }
286 
288  int binIndexAt(double x, double y) { return _axis.binIndexAt(x, y); }
289  int binIndexAt(const BinType& t) { return _axis.binIndexAt(std::get<0>(t), std::get<1>(t)); }
290 
292  const ProfileBin2D& binAt(double x, double y) const { return _axis.binAt(x, y); }
293 
294  const ProfileBin2D& binAt(const BinType& t) const { return _axis.binAt(std::get<0>(t), std::get<1>(t)); }
295 
296 
298  size_t numBins() const { return _axis.bins().size(); }
299 
301  size_t numBinsX() const { return _axis.numBinsX(); }
302 
304  size_t numBinsY() const { return _axis.numBinsY(); }
305 
306 
308  Dbn3D& totalDbn() { return _axis.totalDbn(); }
309 
311  const Dbn3D& totalDbn() const { return _axis.totalDbn(); }
312 
314  void setTotalDbn(const Dbn3D& dbn) { _axis.setTotalDbn(dbn); }
315 
316 
317  // /// @brief Access an outflow (non-const)
318  // ///
319  // /// Two indices are used, for x and y: -1 = underflow, 0 = in-range, and +1 = overflow.
320  // /// (0,0) is not a valid overflow index pair, since it is in range for both x and y.
321  // Dbn3D& outflow(int ix, int iy) {
322  // return _axis.outflow(ix, iy);
323  // }
324 
325  // /// @brief Access an outflow (const)
326  // ///
327  // /// Two indices are used, for x and y: -1 = underflow, 0 = in-range, and +1 = overflow.
328  // /// (0,0) is not a valid overflow index pair, since it is in range for both x and y.
329  // const Dbn3D& outflow(int ix, int iy) const {
330  // return _axis.outflow(ix, iy);
331  // }
332 
334 
335 
338 
340  double numEntries(bool includeoverflows=true) const;
341 
343  double effNumEntries(bool includeoverflows=true) const;
344 
346  double sumW(bool includeoverflows=true) const;
347 
349  double sumW2(bool includeoverflows=true) const;
350 
352  double xMean(bool includeoverflows=true) const;
353 
355  double yMean(bool includeoverflows=true) const;
356 
358  double xVariance(bool includeoverflows=true) const;
359 
361  double yVariance(bool includeoverflows=true) const;
362 
364  double xStdDev(bool includeoverflows=true) const {
365  return std::sqrt(xVariance(includeoverflows));
366  }
367 
369  double yStdDev(bool includeoverflows=true) const {
370  return std::sqrt(yVariance(includeoverflows));
371  }
372 
374  double xStdErr(bool includeoverflows=true) const;
375 
377  double yStdErr(bool includeoverflows=true) const;
378 
380  double xRMS(bool includeoverflows=true) const;
381 
383  double yRMS(bool includeoverflows=true) const;
384 
386 
387 
390 
393  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
394  _axis += toAdd._axis;
395  return *this;
396  }
397 
399  Profile2D& operator -= (const Profile2D& toSubtract) {
400  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
401  _axis -= toSubtract._axis;
402  return *this;
403  }
404 
405  inline bool operator == (const Profile2D& other){
406  return _axis == other._axis;
407  }
408 
409  inline bool operator != (const Profile2D& other){
410  return ! operator == (other);
411  }
413 
414 
415  protected:
416 
418  ProfileBin2D& _binAt(double x, double y) { return _axis.binAt(x, y); }
419 
420 
421  private:
422 
425 
428 
430  };
431 
432 
434  typedef Profile2D P2D;
435 
436 
439 
441  inline Profile2D add(const Profile2D& first, const Profile2D& second) {
442  Profile2D tmp = first;
443  if (first.path() != second.path()) tmp.setPath("");
444  tmp += second;
445  return tmp;
446  }
447 
449  inline Profile2D operator + (const Profile2D& first, const Profile2D& second) {
450  return add(first,second);
451  }
452 
454  inline Profile2D subtract(const Profile2D& first, const Profile2D& second) {
455  Profile2D tmp = first;
456  if (first.path() != second.path()) tmp.setPath("");
457  tmp -= second;
458  return tmp;
459  }
460 
462  inline Profile2D operator - (const Profile2D& first, const Profile2D& second) {
463  return subtract(first,second);
464  }
465 
467  Scatter3D divide(const Profile2D& numer, const Profile2D& denom);
468 
470  inline Scatter3D operator / (const Profile2D& numer, const Profile2D& denom) {
471  return divide(numer, denom);
472  }
473 
475 
476 }
477 
478 #endif
std::pair< double, double > EdgePair1D
Definition: Axis2D.h:35
virtual void fill(double x, double y, double z, double weight=1.0, double fraction=1.0)
Fill histo by value and weight.
Definition: Profile2D.cc:15
std::tuple< double, double, double > FillType
Definition: Profile2D.h:41
void rmAnnotation(const std::string &name)
Delete an annotation by name.
size_t fillDim() const
Fill dimension of this data object.
Definition: Profile2D.h:132
A Bin1D specialised for handling profile-type information.
Definition: ProfileBin2D.h:23
double yVariance(bool includeoverflows=true) const
Get the variance in y.
Definition: Profile2D.cc:110
double yRMS(bool includeoverflows=true) const
Get the RMS in y.
Definition: Profile2D.cc:142
std::shared_ptr< Profile2D > Ptr
Definition: Profile2D.h:43
Scatter1D divide(const Counter &numer, const Counter &denom)
Definition: Counter.cc:25
double xVariance(bool includeoverflows=true) const
Get the variance in x.
Definition: Profile2D.cc:102
double effNumEntries(bool includeoverflows=true) const
Get the effective number of fills.
Definition: Profile2D.cc:60
double xStdErr(bool includeoverflows=true) const
Get the standard error on the mean x.
Definition: Profile2D.cc:118
const std::string path() const
Get the AO path.
Profile2D & operator=(const Profile2D &p2)
Assignment operator.
Definition: Profile2D.h:106
void setTotalDbn(const Dbn3D &dbn)
Set summary distribution, including gaps and overflows.
Definition: Profile2D.h:314
const Dbn3D & totalDbn() const
Access summary distribution, including gaps and overflows (const version)
Definition: Profile2D.h:311
Dbn3D & totalDbn()
Access summary distribution, including gaps and overflows (non-const version)
Definition: Profile2D.h:308
double numEntries(bool includeoverflows=true) const
Get the number of fills (fractional fills are possible)
Definition: Profile2D.cc:52
bool sameBinning(const Profile2D &p2)
Check if binning is the same as different Profile2D.
Definition: Profile2D.h:222
Profile2D(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Profile2D.h:50
double yMin() const
Low y edge of this histo&#39;s axis.
Definition: Profile2D.h:268
double yStdDev(bool includeoverflows=true) const
Get the standard deviation in y.
Definition: Profile2D.h:369
void addBin(Axis::EdgePair1D xrange, Axis::EdgePair1D yrange)
Definition: Profile2D.h:195
void setPath(const std::string &path)
A base class for all fillable objects.
Definition: Fillable.h:16
double xMean(bool includeoverflows=true) const
Get the mean x.
Definition: Profile2D.cc:86
Profile2D(size_t nbinsX, double lowerX, double upperX, size_t nbinsY, double lowerY, double upperY, const std::string &path="", const std::string &title="")
Constructor giving range and number of bins.
Definition: Profile2D.h:57
Axis1D< BIN1D, DBN > operator-(const Axis1D< BIN1D, DBN > &first, const Axis1D< BIN1D, DBN > &second)
Subtract the statistics on two axis.
Definition: Axis1D.h:597
double xStdDev(bool includeoverflows=true) const
Get the standard deviation in x.
Definition: Profile2D.h:364
A base class for all binned objects.
Definition: Binned.h:16
Profile2D(const std::vector< ProfileBin2D > &bins, const Dbn3D &totalDbn, const Outflows &outflows, const std::string &path="", const std::string &title="")
State-setting constructor.
Definition: Profile2D.h:96
STL namespace.
std::vector< Bin > Bins
A vector containing 2D bins. Not used for searching.
Definition: Axis2D.h:31
const ProfileBin2D & bin(size_t index) const
Access a bin by index (const)
Definition: Profile2D.h:285
Axis::Bins Bins
Definition: Profile2D.h:37
Profile2DAxis Axis
Convenience typedefs.
Definition: Profile2D.h:36
double yMax() const
High y edge of this histo&#39;s axis.
Definition: Profile2D.h:271
ProfileBin2D & bin(size_t index)
Access a bin by index (non-const)
Definition: Profile2D.h:282
Profile2D & operator+=(const Profile2D &toAdd)
Add another profile to this one.
Definition: Profile2D.h:392
Counter add(const Counter &first, const Counter &second)
Add two counters.
Definition: Counter.h:247
int binIndexAt(double x, double y)
Access a bin index by coordinate.
Definition: Profile2D.h:288
2D bin container
Definition: Axis2D.h:21
int binIndexAt(const BinType &t)
Definition: Profile2D.h:289
size_t numBinsY() const
Number of bins along the y axis.
Definition: Profile2D.h:304
Axis::Outflows Outflows
Definition: Profile2D.h:39
std::vector< YODA::ProfileBin2D > & bins()
Access the bin vector (non-const)
Definition: Profile2D.h:275
A two-dimensional profile histogram.
Definition: Profile2D.h:32
double yMean(bool includeoverflows=true) const
Get the mean y.
Definition: Profile2D.cc:94
double xMin() const
Low x edge of this histo&#39;s axis.
Definition: Profile2D.h:261
size_t numBinsX() const
Number of bins along the x axis.
Definition: Profile2D.h:301
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by a scalefactor.
Definition: Profile2D.h:156
const ProfileBin2D & binAt(const BinType &t) const
Definition: Profile2D.h:294
double yStdErr(bool includeoverflows=true) const
Get the standard error on the mean y.
Definition: Profile2D.cc:126
const std::vector< double > yEdges() const
Definition: Profile2D.h:256
Profile2D clone() const
Make a copy on the stack.
Definition: Profile2D.h:114
virtual void fill(const FillType &xs, double weight=1.0, double fraction=1.0)
Definition: Profile2D.h:140
const ProfileBin2D & binAt(double x, double y) const
Access a bin by coordinate (const)
Definition: Profile2D.h:292
Axis1D< BIN1D, DBN > operator+(const Axis1D< BIN1D, DBN > &first, const Axis1D< BIN1D, DBN > &second)
Add the statistics on two axes.
Definition: Axis1D.h:589
virtual void fillBin(size_t i, double z, double weight=1.0, double fraction=1.0)
Fill histo x-y bin i with the given z value and weight.
Definition: Profile2D.cc:44
void addBins(const Axis::Edges &xcuts, const Axis::Edges &ycuts)
Bins addition operator.
Definition: Profile2D.h:209
Axis2D< ProfileBin2D, Dbn3D > Profile2DAxis
Convenience typedef.
Definition: Profile2D.h:25
Counter subtract(const Counter &first, const Counter &second)
Subtract two counters.
Definition: Counter.h:260
size_t numBins() const
Number of bins of this axis (not counting under/over flow)
Definition: Profile2D.h:298
ProfileBin2D Bin
Definition: Profile2D.h:38
const std::string title() const
Get the AO title.
double xMax() const
High x edge of this histo&#39;s axis.
Definition: Profile2D.h:264
const std::vector< double > xEdges() const
Definition: Profile2D.h:250
Profile2D(const std::vector< Bin > &bins, const std::string &path="", const std::string &title="")
Constructor accepting an explicit collection of bins.
Definition: Profile2D.h:74
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:25
const std::vector< YODA::ProfileBin2D > & bins() const
Access the bin vector (const)
Definition: Profile2D.h:278
Profile2D(const std::vector< double > &xedges, const std::vector< double > &yedges, const std::string &path="", const std::string &title="")
Constructor giving explicit bin edges in the direction of X and Y.
Definition: Profile2D.h:66
std::vector< Outflow > Outflows
Definition: Axis2D.h:41
AnalysisObject is the base class for histograms and scatters.
bool hasAnnotation(const std::string &name) const
Check if an annotation is defined.
double xRMS(bool includeoverflows=true) const
Get the RMS in x.
Definition: Profile2D.cc:134
Scatter1D operator/(const Counter &numer, const Counter &denom)
Definition: Counter.h:278
A 2D distribution.
Definition: Dbn3D.h:16
void rmBin(size_t index)
Definition: Profile2D.h:236
double sumW(bool includeoverflows=true) const
Get sum of weights in histo.
Definition: Profile2D.cc:68
A two-dimensional histogram.
Definition: Histo2D.h:32
double sumW2(bool includeoverflows=true) const
Get the sum of squared weights in histo.
Definition: Profile2D.cc:76
void reset()
Reset the histogram.
Definition: Profile2D.h:151
Profile2D P2D
Convenience typedef.
Definition: Profile2D.h:434
Profile2D & operator-=(const Profile2D &toSubtract)
Subtract another profile from this one.
Definition: Profile2D.h:399
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.
void scaleZ(double az)
Definition: ProfileBin2D.h:100
void addBins(const Bins &bins)
Bins addition operator.
Definition: Profile2D.h:217
bool operator==(const Profile2D &other)
Definition: Profile2D.h:405
std::tuple< double, double > BinType
Definition: Profile2D.h:42
size_t dim() const
Fill dimension of this data object.
Definition: Profile2D.h:129
void addBin(const Bin &bin)
Definition: Profile2D.h:202
std::vector< double > Edges
Definition: Axis2D.h:34
Profile2D * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Profile2D.h:119
virtual AnalysisObject & operator=(const AnalysisObject &ao)
Default copy assignment operator.
bool operator!=(const Profile2D &other)
Definition: Profile2D.h:409
void scaleZ(double scalefactor)
Rescale as if all z values had been different by factor scalefactor.
Definition: Profile2D.h:163