Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooChi2Var.cxx
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*****************************************************************************
4 * Project: RooFit *
5 * Package: RooFitCore *
6 * @(#)root/roofitcore:$Id$
7 * Authors: *
8 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
9 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
10 * *
11 * Copyright (c) 2000-2005, Regents of the University of California *
12 * and Stanford University. All rights reserved. *
13 * *
14 * Redistribution and use in source and binary forms, *
15 * with or without modification, are permitted according to the terms *
16 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
17 *****************************************************************************/
18
19//////////////////////////////////////////////////////////////////////////////
20/** \class RooChi2Var
21 \ingroup Roofitcore
22 \brief Simple \f$ \chi^2 \f$ calculation from a binned dataset and a PDF.
23 *
24 * It calculates:
25 *
26 \f{align*}{
27 \chi^2 &= \sum_{\mathrm{bins}} \left( \frac{N_\mathrm{PDF,bin} - N_\mathrm{Data,bin}}{\Delta_\mathrm{bin}} \right)^2 \\
28 N_\mathrm{PDF,bin} &=
29 \begin{cases}
30 \mathrm{pdf}(\text{bin centre}) \cdot V_\mathrm{bin} \cdot N_\mathrm{Data,tot} &\text{normal PDF}\\
31 \mathrm{pdf}(\text{bin centre}) \cdot V_\mathrm{bin} \cdot N_\mathrm{Data,expected} &\text{extended PDF}
32 \end{cases} \\
33 \Delta_\mathrm{bin} &=
34 \begin{cases}
35 \sqrt{N_\mathrm{PDF,bin}} &\text{if } \mathtt{DataError == RooAbsData::Expected}\\
36 \mathtt{data{\rightarrow}weightError()} &\text{otherwise} \\
37 \end{cases}
38 \f}
39 * If the dataset doesn't have user-defined errors, errors are assumed to be \f$ \sqrt{N} \f$.
40 * In extended PDF mode, N_tot (total number of data events) is substituted with N_expected, the
41 * expected number of events that the PDF predicts.
42 *
43 * \note If the dataset has errors stored, empty bins will prevent the calculation of \f$ \chi^2 \f$, because those have
44 * zero error. This leads to messages like:
45 * ```
46 * [#0] ERROR:Eval -- RooChi2Var::RooChi2Var(chi2_GenPdf_data_hist) INFINITY ERROR: bin 2 has zero error
47 * ```
48 *
49 * \note In this case, one can use the expected errors of the PDF instead of the data errors:
50 * ```{.cpp}
51 * RooChi2Var chi2(..., ..., RooFit::DataError(RooAbsData::Expected), ...);
52 * ```
53 */
54
55#include "RooChi2Var.h"
56
57#include "FitHelpers.h"
58#include "RooDataHist.h"
59#include "RooAbsPdf.h"
60#include "RooCmdConfig.h"
61#include "RooMsgService.h"
62
63#include "Riostream.h"
64#include "TClass.h"
65
66#include "RooRealVar.h"
67#include "RooAbsDataStore.h"
68
69RooChi2Var::RooChi2Var(const char *name, const char *title, RooAbsReal &func, RooDataHist &data, bool extended,
70 RooDataHist::ErrorType etype, RooAbsTestStatistic::Configuration const &cfg)
71 : RooAbsOptTestStatistic(name, title, func, data, RooArgSet{}, cfg),
72 _etype{etype == RooAbsData::Auto ? (data.isNonPoissonWeighted() ? RooAbsData::SumW2 : RooAbsData::Expected)
73 : etype},
75{
76}
77
78
79////////////////////////////////////////////////////////////////////////////////
80/// Copy constructor
81
82RooChi2Var::RooChi2Var(const RooChi2Var& other, const char* name) :
83 RooAbsOptTestStatistic(other,name),
86{
87}
88
89
90////////////////////////////////////////////////////////////////////////////////
91/// Calculate chi^2 in partition from firstEvent to lastEvent using given stepSize
92/// Throughout the calculation, we use Kahan's algorithm for summing to
93/// prevent loss of precision - this is a factor four more expensive than
94/// straight addition, but since evaluating the PDF is usually much more
95/// expensive than that, we tolerate the additional cost...
96
97double RooChi2Var::evaluatePartition(std::size_t firstEvent, std::size_t lastEvent, std::size_t stepSize) const
98{
99 double result(0);
100 double carry(0);
101
102 // Determine normalization factor depending on type of input function
103 double normFactor(1) ;
104 switch (_funcMode) {
105 case Function: normFactor=1 ; break ;
106 case Pdf: normFactor = _dataClone->sumEntries() ; break ;
107 case ExtendedPdf: normFactor = (static_cast<RooAbsPdf*>(_funcClone))->expectedEvents(_dataClone->get()) ; break ;
108 }
109
110 // Loop over bins of dataset
111 RooDataHist* hdata = static_cast<RooDataHist*>(_dataClone) ;
112 for (auto i=firstEvent ; i<lastEvent ; i+=stepSize) {
113
114 // get the data values for this event
115 hdata->get(i);
116
117 const double nData = hdata->weight() ;
118
119 const double nPdf = _funcClone->getVal(_normSet) * normFactor * hdata->binVolume() ;
120
121 const double eExt = nPdf-nData ;
122
123
124 double eInt ;
126 double eIntLo;
127 double eIntHi;
128 hdata->weightError(eIntLo, eIntHi, _etype);
129 eInt = (eExt > 0) ? eIntHi : eIntLo;
130 } else {
131 eInt = sqrt(nPdf) ;
132 }
133
134 // Skip cases where pdf=0 and there is no data
135 if (0. == eInt * eInt && 0. == nData * nData && 0. == nPdf * nPdf) continue ;
136
137 // Return 0 if eInt=0, special handling in MINUIT will follow
138 if (0. == eInt * eInt) {
139 coutE(Eval) << "RooChi2Var::RooChi2Var(" << GetName() << ") INFINITY ERROR: bin " << i
140 << " has zero error" << std::endl;
141 return 0.;
142 }
143
144// std::cout << "Chi2Var[" << i << "] nData = " << nData << " nPdf = " << nPdf << " errorExt = " << eExt << " errorInt = " << eInt << " contrib = " << eExt*eExt/(eInt*eInt) << std::endl ;
145
146 double term = eExt*eExt/(eInt*eInt) ;
147 double y = term - carry;
148 double t = result + y;
149 carry = (t - result) - y;
150 result = t;
151 }
152
153 _evalCarry = carry;
154 return result ;
155}
156
157/// \endcond
#define coutE(a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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
Double_t(* Function)(Double_t)
Definition Functor.C:4
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
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
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Container class to hold N-dimensional binned data.
Definition RooDataHist.h:40
Double_t y[n]
Definition legend1.C:17
const UInt_t eInt[256]