yoda is hosted by Hepforge, IPPP Durham
 YODA - Yet more Objects for Data Analysis 2.0.1
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-2024 The YODA collaboration (see AUTHORS for details)
5//
6#include "YODA/Counter.h"
7
8#include <cmath>
9#include <iostream>
10#include <iomanip>
11
12using namespace std;
13
14namespace YODA {
15
16
17 // Divide two counters
19 Estimate0D divide(const Counter& numer, const Counter& denom) {
20 Estimate0D rtn;
21 if (numer.path() == denom.path()) rtn.setPath(numer.path());
22 if (rtn.hasAnnotation("ScaledBy")) rtn.rmAnnotation("ScaledBy");
23 if (denom.val() != 0) {
24 const double val = numer.val() / denom.val();
26 rtn.set(val, {-err, err});
27 }
28 return rtn;
29 }
30
31
32 // Calculate a histogrammed efficiency ratio of two histograms
34 Estimate0D efficiency(const Counter& accepted, const Counter& total) {
35 Estimate0D tmp = divide(accepted, total);
36
37 // Check that the numerator is consistent with being a subset of the denominator (NOT effNumEntries here!)
38 if (accepted.numEntries() > total.numEntries() || accepted.sumW() > total.sumW())
39 throw UserError("Attempt to calculate an efficiency when the numerator is not a subset of the denominator");
40
41 // If no entries on the denominator, set eff = err = 0 and move to the next bin
44 double eff = std::numeric_limits<double>::quiet_NaN();
45 double err = std::numeric_limits<double>::quiet_NaN();
46 if (total.sumW() != 0) {
47 eff = accepted.sumW() / total.sumW(); //< Actually this is already calculated by the division...
48 err = sqrt(abs( ((1-2*eff)*accepted.sumW2() + sqr(eff)*total.sumW2()) / sqr(total.sumW()) ));
49 }
50
51 tmp.set(eff, {-err, err});
52 return tmp;
53 }
54
55
56}
57
void setPath(const std::string &path)
const std::string path() const
Get the AO path.
void rmAnnotation(const std::string &name)
Delete an annotation by name.
bool hasAnnotation(const std::string &name) const
Check if an annotation is defined.
A weighted counter.
Definition Counter.h:26
double sumW2(bool=false) const
Get the sum of squared weights.
Definition Counter.h:186
double numEntries(bool=false) const
Get the number of fills.
Definition Counter.h:177
double relErr() const
Definition Counter.h:198
double val(bool=false) const
Get the value.
Definition Counter.h:189
double sumW(bool=false) const
Get the sum of weights.
Definition Counter.h:183
An estimate in 0D.
Definition Estimate0D.h:24
void set(const double val, const std::pair< double, double > &err, const std::string source="")
Set both central value and uncertainty component.
Definition Estimate.h:181
Error for problems introduced outside YODA, to put it nicely.
Definition Exceptions.h:100
Anonymous namespace to limit visibility.
NUM sqr(NUM a)
Named number-type squaring operation.
Definition MathUtils.h:216
BinnedEstimate< AxisT... > efficiency(const BinnedDbn< DbnN, AxisT... > &accepted, const BinnedDbn< DbnN, AxisT... > &total)
Calculate a binned efficiency ratio of two BinnedDbn objects.
Definition BinnedDbn.h:1162