Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooChi2Var.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17//////////////////////////////////////////////////////////////////////////////
18/** \class RooChi2Var
19 \ingroup Roofitcore
20 \brief RooChi2Var implements a simple \f$ \chi^2 \f$ calculation from a binned dataset and a PDF.
21 *
22 * It calculates:
23 *
24 \f{align*}{
25 \chi^2 &= \sum_{\mathrm{bins}} \left( \frac{N_\mathrm{PDF,bin} - N_\mathrm{Data,bin}}{\Delta_\mathrm{bin}} \right)^2 \\
26 N_\mathrm{PDF,bin} &=
27 \begin{cases}
28 \mathrm{pdf}(\text{bin centre}) \cdot V_\mathrm{bin} \cdot N_\mathrm{Data,tot} &\text{normal PDF}\\
29 \mathrm{pdf}(\text{bin centre}) \cdot V_\mathrm{bin} \cdot N_\mathrm{Data,expected} &\text{extended PDF}
30 \end{cases} \\
31 \Delta_\mathrm{bin} &=
32 \begin{cases}
33 \sqrt{N_\mathrm{PDF,bin}} &\text{if } \mathtt{DataError == RooAbsData::Expected}\\
34 \mathtt{data{\rightarrow}weightError()} &\text{otherwise} \\
35 \end{cases}
36 \f}
37 * If the dataset doesn't have user-defined errors, errors are assumed to be \f$ \sqrt{N} \f$.
38 * In extended PDF mode, N_tot (total number of data events) is substituted with N_expected, the
39 * expected number of events that the PDF predicts.
40 *
41 * \note If the dataset has errors stored, empty bins will prevent the calculation of \f$ \chi^2 \f$, because those have
42 * zero error. This leads to messages like:
43 * ```
44 * [#0] ERROR:Eval -- RooChi2Var::RooChi2Var(chi2_GenPdf_data_hist) INFINITY ERROR: bin 2 has zero error
45 * ```
46 *
47 * \note In this case, one can use the expected errors of the PDF instead of the data errors:
48 * ```{.cpp}
49 * RooChi2Var chi2(..., ..., RooFit::DataError(RooAbsData::Expected), ...);
50 * ```
51 */
52
53#include "RooChi2Var.h"
54
55#include "FitHelpers.h"
56#include "RooDataHist.h"
57#include "RooAbsPdf.h"
58#include "RooCmdConfig.h"
59#include "RooMsgService.h"
60
61#include "Riostream.h"
62#include "TClass.h"
63
64#include "RooRealVar.h"
65#include "RooAbsDataStore.h"
66
67
68using namespace std;
69
70namespace {
71 template<class ...Args>
72 RooAbsTestStatistic::Configuration makeRooAbsTestStatisticCfg(Args const& ... args) {
74 cfg.rangeName = RooCmdConfig::decodeStringOnTheFly("RooChi2Var::RooChi2Var","RangeWithName",0,"",args...);
75 cfg.nCPU = RooCmdConfig::decodeIntOnTheFly("RooChi2Var::RooChi2Var","NumCPU",0,1,args...);
77 cfg.verbose = static_cast<bool>(RooCmdConfig::decodeIntOnTheFly("RooChi2Var::RooChi2Var","Verbose",0,1,args...));
78 cfg.cloneInputData = false;
79 cfg.integrateOverBinsPrecision = RooCmdConfig::decodeDoubleOnTheFly("RooChi2Var::RooChi2Var", "IntegrateBins", 0, -1., {args...});
80 cfg.addCoefRangeName = RooCmdConfig::decodeStringOnTheFly("RooChi2Var::RooChi2Var","AddCoefRange",0,"",args...);
81 cfg.splitCutRange = static_cast<bool>(RooCmdConfig::decodeIntOnTheFly("RooChi2Var::RooChi2Var","SplitRange",0,0,args...));
82 return cfg;
83 }
84}
85
87
89
90
91////////////////////////////////////////////////////////////////////////////////
92/// RooChi2Var constructor. Optional arguments are:
93/// \param[in] name Name of the PDF
94/// \param[in] title Title for plotting etc.
95/// \param[in] func Function
96/// \param[in] hdata Data histogram
97/// \param[in] arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9 Optional arguments according to table below.
98/// <table>
99/// <tr><th> Type of CmdArg <th> Effect on \f$ \chi^2 \f$
100/// <tr><td>
101/// <tr><td> `DataError()` <td> Choose between:
102/// - RooAbsData::Expected: Expected Poisson error (\f$ \sqrt{n_\text{expected}} \f$ from the PDF).
103/// - RooAbsData::SumW2: The observed error from the square root of the sum of weights,
104/// i.e., symmetric errors calculated with the standard deviation of a Poisson distribution.
105/// - RooAbsData::Poisson: Asymmetric errors from the central 68 % interval around a Poisson distribution with mean \f$ n_\text{observed} \f$.
106/// If for a given bin \f$ n_\text{expected} \f$ is lower than the \f$ n_\text{observed} \f$, the lower uncertainty is taken
107/// (e.g., the difference between the mean and the 16 % quantile).
108/// If \f$ n_\text{expected} \f$ is higher than \f$ n_\text{observed} \f$, the higher uncertainty is taken
109/// (e.g., the difference between the 84 % quantile and the mean).
110/// - RooAbsData::Auto (default): RooAbsData::Expected for unweighted data, RooAbsData::SumW2 for weighted data.
111/// <tr><td>
112/// `Extended()` <td> Use expected number of events of an extended p.d.f as normalization
113/// <tr><td>
114/// NumCPU() <td> Activate parallel processing feature
115/// <tr><td>
116/// Range() <td> Calculate \f$ \chi^2 \f$ only in selected region
117/// <tr><td>
118/// Verbose() <td> Verbose output of GOF framework
119/// <tr><td>
120/// IntegrateBins() <td> Integrate PDF within each bin. This sets the desired precision. Only useful for binned fits.
121/// <tr><td> `SumCoefRange()` <td> Set the range in which to interpret the coefficients of RooAddPdf components
122/// <tr><td> `SplitRange()` <td> Fit ranges used in different categories get named after the category.
123/// Using `Range("range"), SplitRange()` as switches, different ranges could be set like this:
124/// ```
125/// myVariable.setRange("range_pi0", 135, 210);
126/// myVariable.setRange("range_gamma", 50, 210);
127/// ```
128/// <tr><td> `ConditionalObservables(Args_t &&... argsOrArgSet)` <td> Define projected observables.
129/// Arguments can either be multiple RooRealVar or a single RooArgSet containing them.
130///
131/// </table>
132
133RooChi2Var::RooChi2Var(const char *name, const char* title, RooAbsReal& func, RooDataHist& hdata,
134 const RooCmdArg& arg1,const RooCmdArg& arg2,const RooCmdArg& arg3,
135 const RooCmdArg& arg4,const RooCmdArg& arg5,const RooCmdArg& arg6,
136 const RooCmdArg& arg7,const RooCmdArg& arg8,const RooCmdArg& arg9) :
137 RooAbsOptTestStatistic(name,title,func,hdata,_emptySet,
138 makeRooAbsTestStatisticCfg(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9))
139{
140 RooCmdConfig pc("RooChi2Var::RooChi2Var") ;
141 pc.defineInt("etype","DataError",0,(Int_t)RooDataHist::Auto) ;
142 pc.defineInt("extended","Extended",0,RooFit::FitHelpers::extendedFitDefault);
143 pc.allowUndefined() ;
144
145 pc.process(arg1) ; pc.process(arg2) ; pc.process(arg3) ;
146 pc.process(arg4) ; pc.process(arg5) ; pc.process(arg6) ;
147 pc.process(arg7) ; pc.process(arg8) ; pc.process(arg9) ;
148
149 if (auto pdf = dynamic_cast<RooAbsPdf*>(&func)) {
150 _funcMode = pdf->interpretExtendedCmdArg(pc.getInt("extended")) ? ExtendedPdf : Pdf ;
151 } else {
153 }
154 _etype = (RooDataHist::ErrorType) pc.getInt("etype") ;
155
158 }
159
160}
161
162
163////////////////////////////////////////////////////////////////////////////////
164/// Copy constructor
165
166RooChi2Var::RooChi2Var(const RooChi2Var& other, const char* name) :
168 _etype(other._etype),
169 _funcMode(other._funcMode)
170{
171}
172
173
174////////////////////////////////////////////////////////////////////////////////
175/// Calculate chi^2 in partition from firstEvent to lastEvent using given stepSize
176/// Throughout the calculation, we use Kahan's algorithm for summing to
177/// prevent loss of precision - this is a factor four more expensive than
178/// straight addition, but since evaluating the PDF is usually much more
179/// expensive than that, we tolerate the additional cost...
180
181double RooChi2Var::evaluatePartition(std::size_t firstEvent, std::size_t lastEvent, std::size_t stepSize) const
182{
183 double result(0), carry(0);
184
185 // Determine normalization factor depending on type of input function
186 double normFactor(1) ;
187 switch (_funcMode) {
188 case Function: normFactor=1 ; break ;
189 case Pdf: normFactor = _dataClone->sumEntries() ; break ;
190 case ExtendedPdf: normFactor = ((RooAbsPdf*)_funcClone)->expectedEvents(_dataClone->get()) ; break ;
191 }
192
193 // Loop over bins of dataset
195 for (auto i=firstEvent ; i<lastEvent ; i+=stepSize) {
196
197 // get the data values for this event
198 hdata->get(i);
199
200 const double nData = hdata->weight() ;
201
202 const double nPdf = _funcClone->getVal(_normSet) * normFactor * hdata->binVolume() ;
203
204 const double eExt = nPdf-nData ;
205
206
207 double eInt ;
209 double eIntLo,eIntHi ;
210 hdata->weightError(eIntLo,eIntHi,_etype) ;
211 eInt = (eExt>0) ? eIntHi : eIntLo ;
212 } else {
213 eInt = sqrt(nPdf) ;
214 }
215
216 // Skip cases where pdf=0 and there is no data
217 if (0. == eInt * eInt && 0. == nData * nData && 0. == nPdf * nPdf) continue ;
218
219 // Return 0 if eInt=0, special handling in MINUIT will follow
220 if (0. == eInt * eInt) {
221 coutE(Eval) << "RooChi2Var::RooChi2Var(" << GetName() << ") INFINITY ERROR: bin " << i
222 << " has zero error" << endl ;
223 return 0.;
224 }
225
226// cout << "Chi2Var[" << i << "] nData = " << nData << " nPdf = " << nPdf << " errorExt = " << eExt << " errorInt = " << eInt << " contrib = " << eExt*eExt/(eInt*eInt) << endl ;
227
228 double term = eExt*eExt/(eInt*eInt) ;
229 double y = term - carry;
230 double t = result + y;
231 carry = (t - result) - y;
232 result = t;
233 }
234
235 _evalCarry = carry;
236 return result ;
237}
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char name[80]
Definition TGX11.cxx:110
virtual double sumEntries() const =0
Return effective number of entries in dataset, i.e., sum all weights.
virtual const RooArgSet * get() const
Definition RooAbsData.h:101
RooAbsOptTestStatistic is the abstract base class for test statistics objects that evaluate a functio...
RooAbsReal * _funcClone
Pointer to internal clone of input function.
RooArgSet * _normSet
Pointer to set with observables used for normalization.
RooAbsData * _dataClone
Pointer to internal clone if input data.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
double _evalCarry
! carry of Kahan sum in evaluatePartition
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooChi2Var implements a simple calculation from a binned dataset and a PDF.
Definition RooChi2Var.h:25
double evaluatePartition(std::size_t firstEvent, std::size_t lastEvent, std::size_t stepSize) const override
Calculate chi^2 in partition from firstEvent to lastEvent using given stepSize Throughout the calcula...
RooDataHist::ErrorType _etype
Error type store in associated RooDataHist.
Definition RooChi2Var.h:64
RooChi2Var(const char *name, const char *title, RooAbsReal &func, RooDataHist &data, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={})
RooChi2Var constructor.
FuncMode _funcMode
Function, P.d.f. or extended p.d.f?
Definition RooChi2Var.h:65
static RooArgSet _emptySet
Supports named argument constructor.
Definition RooChi2Var.h:62
Named container for two doubles, two integers two object points and three string pointers that can be...
Definition RooCmdArg.h:26
Class RooCmdConfig is a configurable parser for RooCmdArg named arguments.
bool process(const RooCmdArg &arg)
Process given RooCmdArg.
static std::string decodeStringOnTheFly(const char *callerID, const char *cmdArgName, int intIdx, const char *defVal, Args_t &&...args)
Static decoder function allows to retrieve string property from set of RooCmdArgs For use in base mem...
static double decodeDoubleOnTheFly(const char *callerID, const char *cmdArgName, int idx, double defVal, std::initializer_list< std::reference_wrapper< const RooCmdArg > > args)
Find a given double in a list of RooCmdArg.
bool defineInt(const char *name, const char *argName, int intNum, int defValue=0)
Define integer property name 'name' mapped to integer in slot 'intNum' in RooCmdArg with name argName...
void allowUndefined(bool flag=true)
If flag is true the processing of unrecognized RooCmdArgs is not considered an error.
int getInt(const char *name, int defaultValue=0) const
Return integer property registered with name 'name'.
static int decodeIntOnTheFly(const char *callerID, const char *cmdArgName, int intIdx, int defVal, Args_t &&...args)
Static decoder function allows to retrieve integer property from set of RooCmdArgs For use in base me...
The RooDataHist is a container class to hold N-dimensional binned data.
Definition RooDataHist.h:39
double weight(std::size_t i) const
Return weight of i-th bin.
void weightError(double &lo, double &hi, ErrorType etype=Poisson) const override
Return the asymmetric errors on the current weight.
bool isNonPoissonWeighted() const override
Returns true if dataset contains entries with a non-integer weight.
double binVolume(std::size_t i) const
Return bin volume of i-th bin.
const RooArgSet * get() const override
Get bin centre of current bin.
Definition RooDataHist.h:76
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Double_t y[n]
Definition legend1.C:17
constexpr int extendedFitDefault
Definition FitHelpers.h:37
std::string rangeName
Stores the configuration parameters for RooAbsTestStatistic.