Logo ROOT  
Reference Guide
RooChi2MCSModule.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/** \class RooChi2MCSModule
18 \ingroup Roofit
19
20RooChi2MCSModule is an add-on module to RooMCStudy that
21calculates the chi-squared of fitted p.d.f with respect to a binned
22version of the data. For each fit the chi-squared, the reduced chi-squared
23the number of degrees of freedom and the probability of the chi-squared
24is store in the summary dataset.
25**/
26
27#include "Riostream.h"
28
29#include "RooAbsPdf.h"
30#include "RooDataSet.h"
31#include "RooRealVar.h"
32#include "TString.h"
33#include "RooFitResult.h"
34#include "RooChi2MCSModule.h"
35#include "RooMsgService.h"
36#include "RooDataHist.h"
37#include "TMath.h"
38#include "RooGlobalFunc.h"
39
40using namespace std;
41
43
44////////////////////////////////////////////////////////////////////////////////
45
47 RooAbsMCStudyModule("RooChi2MCSModule","RooChi2Module"),
48 _data(0), _chi2(0), _ndof(0), _chi2red(0), _prob(0)
49
50{
51 // Constructor of module
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Copy constructor
56
59 _data(0), _chi2(0), _ndof(0), _chi2red(0), _prob(0)
60{
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Destructor
65
67{
68 if (_chi2) {
69 delete _chi2 ;
70 }
71 if (_ndof) {
72 delete _ndof ;
73 }
74 if (_chi2red) {
75 delete _chi2red ;
76 }
77 if (_prob) {
78 delete _prob ;
79 }
80 if (_data) {
81 delete _data ;
82 }
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// Initialize module after attachment to RooMCStudy object
87
89{
90 // Construct variable that holds -log(L) fit with null hypothesis for given parameter
91 _chi2 = new RooRealVar("chi2","chi^2",0) ;
92 _ndof = new RooRealVar("ndof","number of degrees of freedom",0) ;
93 _chi2red = new RooRealVar("chi2red","reduced chi^2",0) ;
94 _prob = new RooRealVar("prob","prob(chi2,ndof)",0) ;
95
96 // Create new dataset to be merged with RooMCStudy::fitParDataSet
97 _data = new RooDataSet("Chi2Data","Additional data for Chi2 study",RooArgSet(*_chi2,*_ndof,*_chi2red,*_prob)) ;
98
99 return true ;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// Initialize module at beginning of RooCMStudy run
104
106{
107 _data->reset() ;
108 return true ;
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Return auxiliary dataset with results of chi2 analysis
113/// calculations of this module so that it is merged with
114/// RooMCStudy::fitParDataSet() by RooMCStudy
115
117{
118 return _data ;
119}
120
121////////////////////////////////////////////////////////////////////////////////
122/// Bin dataset and calculate chi2 of p.d.f w.r.t binned dataset
123
125{
127 RooDataHist* binnedData = dynamic_cast<RooDataHist*>(data) ;
128 bool deleteData(false) ;
129 if (!binnedData) {
130 deleteData = true ;
131 binnedData = ((RooDataSet*)data)->binnedClone() ;
132 }
133
134 std::unique_ptr<RooAbsReal> chi2Var{fitModel()->createChi2(*binnedData,RooFit::Extended(extendedGen()),RooFit::DataError(RooAbsData::SumW2))};
135
136 RooArgSet* floatPars = (RooArgSet*) fitParams()->selectByAttrib("Constant",false) ;
137
138 _chi2->setVal(chi2Var->getVal()) ;
139 _ndof->setVal(binnedData->numEntries()-floatPars->getSize()-1) ;
141 _prob->setVal(TMath::Prob(_chi2->getVal(),static_cast<int>(_ndof->getVal()))) ;
142
144
145 if (deleteData) {
146 delete binnedData ;
147 }
148 delete floatPars ;
149
150 return true ;
151}
#define ClassImp(name)
Definition: Rtypes.h:375
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
RooAbsCollection * selectByAttrib(const char *name, bool value) const
Create a subset of the current collection, consisting only of those elements with the specified attri...
Int_t getSize() const
Return the number of elements in the collection.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:62
virtual void reset()
Definition: RooAbsData.cxx:386
RooAbsMCStudyModule is a base class for add-on modules to RooMCStudy that can perform additional calc...
bool extendedGen()
If true extended mode generation is requested.
RooAbsData * genSample()
Return generate sample.
RooAbsPdf * fitModel()
Return fit model.
RooArgSet * fitParams()
Return current value of parameters of fit model.
RooAbsReal * createChi2(RooDataHist &data, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) override
Create a from a histogram and this function.
Definition: RooAbsPdf.cxx:1736
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:94
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:57
RooChi2MCSModule is an add-on module to RooMCStudy that calculates the chi-squared of fitted p....
~RooChi2MCSModule() override
Destructor.
RooRealVar * _chi2red
bool processAfterFit(Int_t) override
Bin dataset and calculate chi2 of p.d.f w.r.t binned dataset.
RooRealVar * _ndof
bool initializeRun(Int_t) override
Initialize module at beginning of RooCMStudy run.
RooRealVar * _chi2
RooRealVar * _prob
bool initializeInstance() override
Initialize module after attachment to RooMCStudy object.
RooDataSet * finalizeRun() override
Return auxiliary dataset with results of chi2 analysis calculations of this module so that it is merg...
RooDataSet * _data
The RooDataHist is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:45
Int_t numEntries() const override
Return the number of bins.
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
void add(const RooArgSet &row, double weight=1.0, double weightError=0) override
Add one ore more rows of data.
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
void setVal(double value) override
Set value of variable to 'value'.
Definition: RooRealVar.cxx:281
RooCmdArg DataError(Int_t)
RooCmdArg Extended(bool flag=true)
Double_t Prob(Double_t chi2, Int_t ndf)
Computation of the probability for a certain Chi-squared (chi2) and number of degrees of freedom (ndf...
Definition: TMath.cxx:619