yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
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-2017 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/ProfileBin2D.h"
11 #include "YODA/Dbn3D.h"
12 #include "YODA/Axis2D.h"
13 #include "YODA/Scatter3D.h"
14 #include "YODA/Exceptions.h"
15 
16 #include <vector>
17 #include <tuple>
18 
19 namespace YODA {
20 
21 
22  // Forward declarations
23  class Histo2D;
24  class Scatter3D;
25 
28 
29 
31  class Profile2D : public AnalysisObject {
32  public:
33 
35  typedef Profile2DAxis Axis;
36  typedef Axis::Bins Bins;
37  typedef ProfileBin2D Bin;
39 
40  typedef std::tuple<double, double, double> FillType;
41  typedef std::tuple<double, double> BinType;
42  typedef std::shared_ptr<Profile2D> Ptr;
43 
44 
46 
47 
49  Profile2D(const std::string& path="", const std::string& title="")
50  : AnalysisObject("Profile2D", path, title),
51  _axis()
52  { }
53 
54 
56  Profile2D(size_t nbinsX, double lowerX, double upperX,
57  size_t nbinsY, double lowerY, double upperY,
58  const std::string& path="", const std::string& title="")
59  : AnalysisObject("Profile2D", path, title),
60  _axis(nbinsX, std::make_pair(lowerX, upperX), nbinsY, std::make_pair(lowerY, upperY))
61  { }
62 
63 
65  Profile2D(const std::vector<double>& xedges, const std::vector<double>& yedges,
66  const std::string& path="", const std::string& title="")
67  : AnalysisObject("Profile2D", path, title),
68  _axis(xedges, yedges)
69  { }
70 
71 
73  Profile2D(const std::vector<Bin>& bins,
74  const std::string& path="", const std::string& title="")
75  : AnalysisObject("Profile2D", path, title),
76  _axis(bins)
77  { }
78 
79 
82  Profile2D(const Profile2D& p, const std::string& path="");
83 
86  Profile2D(const Scatter3D& s, const std::string& path="");
87 
90  Profile2D(const Histo2D& h, const std::string& path="");
91 
95  Profile2D(const std::vector<ProfileBin2D>& bins,
96  const Dbn3D& totalDbn,
97  const Outflows& outflows,
98  const std::string& path="", const std::string& title="")
99  : AnalysisObject("Profile2D", path, title),
100  _axis(bins, totalDbn, outflows)
101  { }
102 
103 
106  AnalysisObject::operator = (p2); //< AO treatment of paths etc.
107  _axis = p2._axis;
108  return *this;
109  }
110 
111 
113  Profile2D clone() const {
114  return Profile2D(*this);
115  }
116 
118  Profile2D* newclone() const {
119  return new Profile2D(*this);
120  }
121 
123 
124 
126  size_t dim() const { return 2; }
127 
128 
130 
131 
133  virtual void fill(double x, double y, double z, double weight=1.0, double fraction=1.0);
134  virtual void fill(const FillType & xs, double weight=1.0, double fraction=1.0) {
135  fill(std::get<0>(xs), std::get<1>(xs), std::get<2>(xs), weight, fraction);
136  }
137 
139  virtual void fillBin(size_t i, double z, double weight=1.0, double fraction=1.0);
140 
141 
145  void reset() {
146  _axis.reset();
147  }
148 
150  void scaleW(double scalefactor) {
152  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
153  _axis.scaleW(scalefactor);
154  }
155 
157  void scaleZ(double scalefactor) {
158  _axis.totalDbn().scaleZ(scalefactor);
160  // _axis.overflow().scaleZ(scalefactor);
161  // _axis.underflow().scaleZ(scalefactor);
162  for (size_t i = 0; i < bins().size(); ++i)
163  bin(i).scaleZ(scalefactor);
164  }
165 
166 
168  // /// Merge together the bin range with indices from @a from to @a to, inclusive
169  // void mergeBins(size_t from, size_t to) {
170  // _axis.mergeBins(from, to);
171  // }
172 
174  // /// Merge every group of n bins, starting from the LHS
175  // void rebin(size_t n) {
176  // throw "IMPLEMENT!";
177  // //_axis.rebin(n);
178  // }
179 
180 
181  // /// @brief Bin addition operator
182  // ///
183  // /// Add a bin to the axis, described by its x and y ranges.
184  void addBin(Axis::EdgePair1D xrange, Axis::EdgePair1D yrange) {
185  _axis.addBin(xrange, yrange);
186  }
187 
188  // /// @brief Bin addition operator
189  // ///
190  // /// Add a bin to the axis, possibly pre-populated
191  void addBin(const Bin& bin) {
192  _axis.addBin(bin);
193  }
194 
198  void addBins(const Axis::Edges& xcuts, const Axis::Edges& ycuts) {
199  _axis.addBins(xcuts, ycuts);
200  }
201 
202 
206  void addBins(const Bins& bins) {
207  _axis.addBins(bins);
208  }
209 
210 
212  // /// @brief Bin addition operator
213  // ///
214  // /// Add a set of bins delimiting coordinates of which are contained
215  // /// in binLimits vector.
216  // void addBin(const std::vector<Segment>& binLimits) {
217  // _axis.addBin(binLimits);
218  // }
219 
220  void eraseBin(size_t index) {
221  _axis.eraseBin(index);
222  }
223 
225 
226 
228 
229 
231  double xMin() const { return _axis.xMin(); }
232 
234  double xMax() const { return _axis.xMax(); }
235 
236 
238  double yMin() const { return _axis.yMin(); }
239 
241  double yMax() const { return _axis.yMax(); }
242 
243 
245  std::vector<YODA::ProfileBin2D>& bins() { return _axis.bins(); }
246 
248  const std::vector<YODA::ProfileBin2D>& bins() const { return _axis.bins(); }
249 
250 
252  ProfileBin2D& bin(size_t index) { return _axis.bins()[index]; }
253 
255  const ProfileBin2D& bin(size_t index) const { return _axis.bins()[index]; }
256 
258  int binIndexAt(double x, double y) { return _axis.binIndexAt(x, y); }
259  int binIndexAt(const BinType& t) { return _axis.binIndexAt(std::get<0>(t), std::get<1>(t)); }
260 
262  const ProfileBin2D& binAt(double x, double y) const { return _axis.binAt(x, y); }
263 
264  const ProfileBin2D& binAt(const BinType& t) const { return _axis.binAt(std::get<0>(t), std::get<1>(t)); }
265 
266 
268  size_t numBins() const { return _axis.bins().size(); }
269 
271  size_t numBinsX() const { return _axis.numBinsX(); }
272 
274  size_t numBinsY() const { return _axis.numBinsY(); }
275 
276 
278  Dbn3D& totalDbn() { return _axis.totalDbn(); }
279 
281  const Dbn3D& totalDbn() const { return _axis.totalDbn(); }
282 
284  void setTotalDbn(const Dbn3D& dbn) { _axis.setTotalDbn(dbn); }
285 
286 
287  // /// @brief Access an outflow (non-const)
288  // ///
289  // /// Two indices are used, for x and y: -1 = underflow, 0 = in-range, and +1 = overflow.
290  // /// (0,0) is not a valid overflow index pair, since it is in range for both x and y.
291  // Dbn3D& outflow(int ix, int iy) {
292  // return _axis.outflow(ix, iy);
293  // }
294 
295  // /// @brief Access an outflow (const)
296  // ///
297  // /// Two indices are used, for x and y: -1 = underflow, 0 = in-range, and +1 = overflow.
298  // /// (0,0) is not a valid overflow index pair, since it is in range for both x and y.
299  // const Dbn3D& outflow(int ix, int iy) const {
300  // return _axis.outflow(ix, iy);
301  // }
302 
304 
305 
307 
308 
310  double numEntries(bool includeoverflows=true) const;
311 
313  double effNumEntries(bool includeoverflows=true) const;
314 
316  double sumW(bool includeoverflows=true) const;
317 
319  double sumW2(bool includeoverflows=true) const;
320 
322  double xMean(bool includeoverflows=true) const;
323 
325  double yMean(bool includeoverflows=true) const;
326 
328  double xVariance(bool includeoverflows=true) const;
329 
331  double yVariance(bool includeoverflows=true) const;
332 
334  double xStdDev(bool includeoverflows=true) const {
335  return std::sqrt(xVariance(includeoverflows));
336  }
337 
339  double yStdDev(bool includeoverflows=true) const {
340  return std::sqrt(yVariance(includeoverflows));
341  }
342 
344  double xStdErr(bool includeoverflows=true) const;
345 
347  double yStdErr(bool includeoverflows=true) const;
348 
350  double xRMS(bool includeoverflows=true) const;
351 
353  double yRMS(bool includeoverflows=true) const;
354 
356 
357 
359 
360 
363  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
364  _axis += toAdd._axis;
365  return *this;
366  }
367 
369  Profile2D& operator -= (const Profile2D& toSubtract) {
370  if (hasAnnotation("ScaledBy")) rmAnnotation("ScaledBy");
371  _axis -= toSubtract._axis;
372  return *this;
373  }
374 
375  inline bool operator == (const Profile2D& other){
376  return _axis == other._axis;
377  }
378 
379  inline bool operator != (const Profile2D& other){
380  return ! operator == (other);
381  }
383 
384 
385  protected:
386 
388  ProfileBin2D& _binAt(double x, double y) { return _axis.binAt(x, y); }
389 
390 
391  private:
392 
394 
395 
398 
400  };
401 
402 
404  typedef Profile2D P2D;
405 
406 
408 
409 
411  inline Profile2D add(const Profile2D& first, const Profile2D& second) {
412  Profile2D tmp = first;
413  if (first.path() != second.path()) tmp.setPath("");
414  tmp += second;
415  return tmp;
416  }
417 
419  inline Profile2D operator + (const Profile2D& first, const Profile2D& second) {
420  return add(first,second);
421  }
422 
424  inline Profile2D subtract(const Profile2D& first, const Profile2D& second) {
425  Profile2D tmp = first;
426  if (first.path() != second.path()) tmp.setPath("");
427  tmp -= second;
428  return tmp;
429  }
430 
432  inline Profile2D operator - (const Profile2D& first, const Profile2D& second) {
433  return subtract(first,second);
434  }
435 
437  Scatter3D divide(const Profile2D& numer, const Profile2D& denom);
438 
440  inline Scatter3D operator / (const Profile2D& numer, const Profile2D& denom) {
441  return divide(numer, denom);
442  }
443 
445 
446 }
447 
448 #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:40
void rmAnnotation(const std::string &name)
Delete an annotation by name.
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:42
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 <x>
Definition: Profile2D.cc:118
const std::string path() const
Get the AO path.
Profile2D & operator=(const Profile2D &p2)
Assignment operator.
Definition: Profile2D.h:105
void setTotalDbn(const Dbn3D &dbn)
Set summary distribution, including gaps and overflows.
Definition: Profile2D.h:284
const Dbn3D & totalDbn() const
Access summary distribution, including gaps and overflows (const version)
Definition: Profile2D.h:281
Dbn3D & totalDbn()
Access summary distribution, including gaps and overflows (non-const version)
Definition: Profile2D.h:278
double numEntries(bool includeoverflows=true) const
Get the number of fills (fractional fills are possible)
Definition: Profile2D.cc:52
Profile2D(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Profile2D.h:49
double yMin() const
Low y edge of this histo&#39;s axis.
Definition: Profile2D.h:238
double yStdDev(bool includeoverflows=true) const
Get the standard deviation in y.
Definition: Profile2D.h:339
void addBin(Axis::EdgePair1D xrange, Axis::EdgePair1D yrange)
Definition: Profile2D.h:184
void setPath(const std::string &path)
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:56
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:334
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:95
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:255
Axis::Bins Bins
Definition: Profile2D.h:36
Profile2DAxis Axis
Convenience typedefs.
Definition: Profile2D.h:35
double yMax() const
High y edge of this histo&#39;s axis.
Definition: Profile2D.h:241
ProfileBin2D & bin(size_t index)
Access a bin by index (non-const)
Definition: Profile2D.h:252
Profile2D & operator+=(const Profile2D &toAdd)
Add another profile to this one.
Definition: Profile2D.h:362
Counter add(const Counter &first, const Counter &second)
Add two counters.
Definition: Counter.h:236
int binIndexAt(double x, double y)
Access a bin index by coordinate.
Definition: Profile2D.h:258
2D bin container
Definition: Axis2D.h:21
int binIndexAt(const BinType &t)
Definition: Profile2D.h:259
size_t numBinsY() const
Number of bins along the y axis.
Definition: Profile2D.h:274
Axis::Outflows Outflows
Definition: Profile2D.h:38
std::vector< YODA::ProfileBin2D > & bins()
Access the bin vector (non-const)
Definition: Profile2D.h:245
A two-dimensional profile histogram.
Definition: Profile2D.h:31
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:231
size_t numBinsX() const
Number of bins along the x axis.
Definition: Profile2D.h:271
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by a scalefactor.
Definition: Profile2D.h:150
const ProfileBin2D & binAt(const BinType &t) const
Definition: Profile2D.h:264
double yStdErr(bool includeoverflows=true) const
Get the standard error on <y>
Definition: Profile2D.cc:126
void eraseBin(size_t index)
Definition: Profile2D.h:220
Profile2D clone() const
Make a copy on the stack.
Definition: Profile2D.h:113
virtual void fill(const FillType &xs, double weight=1.0, double fraction=1.0)
Definition: Profile2D.h:134
const ProfileBin2D & binAt(double x, double y) const
Access a bin by coordinate (const)
Definition: Profile2D.h:262
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:198
Axis2D< ProfileBin2D, Dbn3D > Profile2DAxis
Convenience typedef.
Definition: Profile2D.h:24
Counter subtract(const Counter &first, const Counter &second)
Subtract two counters.
Definition: Counter.h:249
size_t numBins() const
Number of bins of this axis (not counting under/over flow)
Definition: Profile2D.h:268
ProfileBin2D Bin
Definition: Profile2D.h:37
const std::string title() const
Get the AO title.
double xMax() const
High x edge of this histo&#39;s axis.
Definition: Profile2D.h:234
Profile2D(const std::vector< Bin > &bins, const std::string &path="", const std::string &title="")
Constructor accepting an explicit collection of bins.
Definition: Profile2D.h:73
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:24
const std::vector< YODA::ProfileBin2D > & bins() const
Access the bin vector (const)
Definition: Profile2D.h:248
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:65
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:267
A 2D distribution.
Definition: Dbn3D.h:16
double sumW(bool includeoverflows=true) const
Get sum of weights in histo.
Definition: Profile2D.cc:68
A two-dimensional histogram.
Definition: Histo2D.h:31
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:145
Profile2D P2D
Convenience typedef.
Definition: Profile2D.h:404
Profile2D & operator-=(const Profile2D &toSubtract)
Subtract another profile from this one.
Definition: Profile2D.h:369
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:206
bool operator==(const Profile2D &other)
Definition: Profile2D.h:375
std::tuple< double, double > BinType
Definition: Profile2D.h:41
size_t dim() const
Fill dimension of this data object.
Definition: Profile2D.h:126
void addBin(const Bin &bin)
Definition: Profile2D.h:191
std::vector< double > Edges
Definition: Axis2D.h:34
Profile2D * newclone() const
Make a copy on the heap, via &#39;new&#39;.
Definition: Profile2D.h:118
virtual AnalysisObject & operator=(const AnalysisObject &ao)
Default copy assignment operator.
bool operator!=(const Profile2D &other)
Definition: Profile2D.h:379
void scaleZ(double scalefactor)
Rescale as if all z values had been different by factor scalefactor.
Definition: Profile2D.h:157