yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
Counter.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-2017 The YODA collaboration (see AUTHORS for details)
5 //
6 #include "YODA/Counter.h"
7 
8 #include <cmath>
9 #include <iostream>
10 using namespace std;
11 
12 namespace YODA {
13 
14 
16  Counter::Counter(const Counter& c, const std::string& path)
17  : AnalysisObject("Counter", (path.size() == 0) ? c.path() : path, c, c.title())
18  {
19  _dbn = c._dbn;
20  }
21 
22 
23  // Divide two counters
25  Scatter1D divide(const Counter& numer, const Counter& denom) {
26  Scatter1D rtn;
27  if (denom.val() != 0) {
28  const double val = numer.val() / denom.val();
29  const double err = val * add_quad(numer.relErr(), denom.relErr());
30  rtn.addPoint(val, err);
31  } else {
32  rtn.addPoint(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN());
33  }
34  return rtn;
35  }
36 
37 
38  // Calculate a histogrammed efficiency ratio of two histograms
40  Scatter1D efficiency(const Counter& accepted, const Counter& total) {
41  Scatter1D tmp = divide(accepted, total);
42  assert(tmp.numPoints() == 1);
43 
45 
46  // Check that the numerator is consistent with being a subset of the denominator (NOT effNumEntries here!)
47  if (accepted.numEntries() > total.numEntries() || accepted.sumW() > total.sumW())
48  throw UserError("Attempt to calculate an efficiency when the numerator is not a subset of the denominator");
49 
50  // If no entries on the denominator, set eff = err = 0 and move to the next bin
53  double eff = std::numeric_limits<double>::quiet_NaN();
54  double err = std::numeric_limits<double>::quiet_NaN();
55  if (total.sumW() != 0) {
56  eff = accepted.sumW() / total.sumW(); //< Actually this is already calculated by the division...
57  err = sqrt(abs( ((1-2*eff)*accepted.sumW2() + sqr(eff)*total.sumW2()) / sqr(total.sumW()) ));
58  }
59 
61 
62  tmp.point(0).setX(eff, err);
63  return tmp;
64  }
65 
66 
67 }
Scatter1D divide(const Counter &numer, const Counter &denom)
Definition: Counter.cc:25
size_t numPoints() const
Number of points in the scatter.
Definition: Scatter1D.h:151
double sumW2() const
Get the sum of squared weights.
Definition: Counter.h:132
void addPoint(const Point1D &pt)
Insert a new point.
Definition: Scatter1D.h:188
STL namespace.
A weighted counter.
Definition: Counter.h:23
Error for problems introduced outside YODA, to put it nicely.
Definition: Exceptions.h:100
NUM sqr(NUM a)
Named number-type squaring operation.
Definition: MathUtils.h:207
Point1D & point(size_t index)
Get a reference to the point with index index (non-const)
Definition: Scatter1D.h:169
Num add_quad(Num a, Num b)
Named number-type addition in quadrature operation.
Definition: MathUtils.h:213
double err() const
Definition: Counter.h:140
double sumW() const
Get the sum of weights.
Definition: Counter.h:129
double val() const
Get the value.
Definition: Counter.h:135
AnalysisObject is the base class for histograms and scatters.
double relErr() const
Definition: Counter.h:145
A very generic data type which is just a collection of 1D data points with errors.
Definition: Scatter1D.h:23
void setX(double x)
Set x value.
Definition: Point1D.h:81
Scatter1D efficiency(const Counter &accepted, const Counter &total)
Calculate an efficiency ratio of two counters.
Definition: Counter.cc:40
double numEntries() const
Get the number of fills.
Definition: Counter.h:123