YODA is hosted by Hepforge, IPPP Durham
 YODA - Yet more Objects for Data Analysis  1.3.1
Profile2D.cc
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-2015 The YODA collaboration (see AUTHORS for details)
5 //
6 #include "YODA/Profile2D.h"
7 #include "YODA/Scatter3D.h"
8 #include "YODA/Histo2D.h"
9
10 using namespace std;
11
12 namespace YODA {
13
14
15  void Profile2D::fill(double x, double y, double z, double weight) {
16  if ( std::isnan(x) ) throw RangeError("X is NaN");
17  if ( std::isnan(y) ) throw RangeError("Y is NaN");
18  if ( std::isnan(z) ) throw RangeError("Z is NaN");
19
20  // Fill the overall distribution
21  _axis.totalDbn().fill(x, y, z, weight);
22
23  // Fill the bins and overflows
25  if (inRange(x, _axis.xMin(), _axis.xMax()) && inRange(y, _axis.yMin(), _axis.yMax())) {
26  try {
28  binAt(x, y).fill(x, y, z, weight);
29  } catch (const RangeError& re) { }
30  }
32  // else {
33  // size_t ix(0), iy(0);
34  // if (x < _axis.xMin()) ix = -1; else if (x >= _axis.xMax()) ix = 1;
35  // if (y < _axis.yMin()) iy = -1; else if (y >= _axis.yMax()) iy = 1;
36  // _axis.outflow(ix, iy).fill(x, y, z, weight);
37  // }
38
39  // Lock the axis now that a fill has happened
40  _axis._setLock(true);
41  }
42
43
44  void Profile2D::fillBin(size_t i, double z, double weight) {
45  pair<double, double> mid = bin(i).xyMid();
46  fill(mid.first, mid.second, z, weight);
47  }
48
49
50  double Profile2D::sumW(bool includeoverflows) const {
51  if (includeoverflows) return _axis.totalDbn().sumW2();
52  double sumw = 0;
53  BOOST_FOREACH (const ProfileBin2D& b, bins()) sumw += b.sumW();
54  return sumw;
55  }
56
57
58  double Profile2D::sumW2(bool includeoverflows) const {
59  if (includeoverflows) return _axis.totalDbn().sumW2();
60  double sumw = 0;
61  BOOST_FOREACH (const ProfileBin2D& b, bins()) sumw += b.sumW();
62  return sumw;
63  }
64
65
66  double Profile2D::xMean(bool includeoverflows) const {
67  if (includeoverflows) return _axis.totalDbn().xMean();
68  double sumwx = 0;
69  BOOST_FOREACH (const ProfileBin2D& b, bins()) sumwx += b.sumWX();
70  return sumwx/sumW();
71  }
72
73
74  double Profile2D::yMean(bool includeoverflows) const {
75  if (includeoverflows) return _axis.totalDbn().yMean();
76  double sumwy = 0;
77  BOOST_FOREACH (const ProfileBin2D& b, bins()) sumwy += b.sumWY();
78  return sumwy/sumW();
79  }
80
81
82  double Profile2D::xVariance(bool includeoverflows) const {
83  if (includeoverflows) return _axis.totalDbn().xVariance();
85  double sigma2 = 0;
86  const double xMean = this->xMean();
87  for (size_t i = 0; i < bins().size(); ++i) {
88  const double diff = bin(i).xFocus() - xMean;
89  sigma2 += diff * diff * bin(i).sumW();
90  }
91  return sigma2/sumW();
92  }
93
94
95  double Profile2D::yVariance(bool includeoverflows) const {
96  if (includeoverflows) return _axis.totalDbn().yVariance();
98  double sigma2 = 0;
99  const double yMean = this->yMean();
100  for (size_t i = 0; i < bins().size(); ++i) {
101  const double diff = bin(i).yFocus() - yMean;
102  sigma2 += diff * diff * bin(i).sumW();
103  }
104  return sigma2/sumW();
105  }
106
107
108  double Profile2D::xStdErr(bool includeoverflows) const {
109  if (includeoverflows) return _axis.totalDbn().xStdErr();
110  const double effNumEntries = sumW(false)*sumW(false)/sumW2(false);
111  return std::sqrt(xVariance(false) / effNumEntries);
112  }
113
114
115  double Profile2D::yStdErr(bool includeoverflows) const {
116  if (includeoverflows) return _axis.totalDbn().yStdErr();
117  const double effNumEntries = sumW(false)*sumW(false)/sumW2(false);
118  return std::sqrt(yVariance(false) / effNumEntries);
119  }
120
121
122  // double Profile2D::xRMS(bool includeoverflows) const {
123  // if (includeoverflows) return _axis.totalDbn().xRMS();
124  // /// @todo Finish
125  // }
126
127
128  // double Profile2D::yRMS(bool includeoverflows) const {
129  // if (includeoverflows) return _axis.totalDbn().yRMS();
130  // /// @todo Finish
131  // }
132
133
134
136
137
139  Profile2D::Profile2D(const Profile2D& p, const std::string& path)
140  : AnalysisObject("Profile2D",
141  (path.size() == 0) ? p.path() : path,
142  p, p.title()),
143  _axis(p._axis)
144  { }
145
146
148  Profile2D::Profile2D(const Scatter3D& s, const std::string& path)
149  : AnalysisObject("Profile2D",
150  (path.size() == 0) ? s.path() : path,
151  s, s.title())
152  {
153  Bins bins;
154  BOOST_FOREACH (const Scatter3D::Point& p, s.points()) {
155  bins.push_back(ProfileBin2D(p.xMin(), p.yMin(), p.xMax(), p.yMax()));
156  }
157  _axis = Profile2DAxis(bins);
158  }
159
160
162  Profile2D::Profile2D(const Histo2D& h, const std::string& path)
163  : AnalysisObject("Profile2D", (path.size() == 0) ? h.path() : path, h, h.title())
164  {
165  Bins bins;
166  BOOST_FOREACH (const HistoBin2D& b, h.bins()) {
167  bins.push_back(ProfileBin2D(b.xMin(), b.yMin(), b.xMax(), b.yMax()));
168  }
169  _axis = Profile2DAxis(bins);
170  }
171
172
174  Scatter3D divide(const Profile2D& numer, const Profile2D& denom) {
175  return divide(mkScatter(numer), mkScatter(denom));
176  }
177
178
180
181
182 }
bool inRange(NUM value, NUM low, NUM high, RangeBoundary lowbound=CLOSED, RangeBoundary highbound=OPEN)
Determine if value is in the range low to high, for floating point numbers.
Definition: MathUtils.h:151
A Bin1D specialised for handling profile-type information.
Definition: ProfileBin2D.h:23
std::vector< YODA::HistoBin2D > & bins()
Access the bin vector (non-const version)
Definition: Histo2D.h:241
Scatter1D divide(const Counter &numer, const Counter &denom)
Definition: Counter.cc:24
Profile2D(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Profile2D.h:44
double xMax() const
Upper x limit of the bin (exclusive).
Definition: Bin2D.h:141
double sumW() const
The sum of weights.
Definition: Bin2D.h:296
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.h:34
double xMin() const
Lower x limit of the bin (inclusive).
Definition: Bin2D.h:136
Axis::Bins Bins
Definition: Profile2D.h:35
std::vector< YODA::ProfileBin2D > & bins()
Access the bin vector (non-const)
Definition: Profile2D.h:236
A two-dimensional profile histogram.
Definition: Profile2D.h:30
A 3D data point to be contained in a Scatter3D.
Definition: Point3D.h:17
double yMin() const
Lower y limit of the bin (inclusive).
Definition: Bin2D.h:152
Axis2D< ProfileBin2D, Dbn3D > Profile2DAxis
Convenience typedef.
Definition: Profile2D.h:23
Scatter1D mkScatter(const Counter &c)
Make a Scatter1D representation of a Histo1D.
Definition: Scatter1D.cc:8
double yMax() const
Get value plus positive y-error.
Definition: Point3D.h:243
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:23
A Bin2D specialised for handling histogram-type information.
Definition: HistoBin2D.h:21
AnalysisObject is the base class for histograms and scatters.
double sumWY() const
The sum of y*weight.
Definition: Bin2D.h:311
Points & points()
Get the collection of points (non-const)
Definition: Scatter3D.h:187
double yMin() const
Get value minus negative y-error.
Definition: Point3D.h:238
A two-dimensional histogram.
Definition: Histo2D.h:30
double yMax() const
Upper y limit of the bin (exclusive).
Definition: Bin2D.h:157
double xMin() const
Get value minus negative x-error.
Definition: Point3D.h:175
double sumWX() const
The sum of x*weight.
Definition: Bin2D.h:306
double xMax() const
Get value plus positive x-error.
Definition: Point3D.h:180