Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "RooFitResult.h"
33#include "RooChi2MCSModule.h"
34#include "RooMsgService.h"
35#include "RooDataHist.h"
36#include "TMath.h"
37#include "RooGlobalFunc.h"
38
40
41////////////////////////////////////////////////////////////////////////////////
42
43RooChi2MCSModule::RooChi2MCSModule() : RooAbsMCStudyModule("RooChi2MCSModule", "RooChi2Module") {}
44
45////////////////////////////////////////////////////////////////////////////////
46/// Copy constructor
47
49
50////////////////////////////////////////////////////////////////////////////////
51/// Destructor
52
54{
55 if (_chi2) {
56 delete _chi2 ;
57 }
58 if (_ndof) {
59 delete _ndof ;
60 }
61 if (_chi2red) {
62 delete _chi2red ;
63 }
64 if (_prob) {
65 delete _prob ;
66 }
67 if (_data) {
68 delete _data ;
69 }
70}
71
72////////////////////////////////////////////////////////////////////////////////
73/// Initialize module after attachment to RooMCStudy object
74
76{
77 // Construct variable that holds -log(L) fit with null hypothesis for given parameter
78 _chi2 = new RooRealVar("chi2","chi^2",0) ;
79 _ndof = new RooRealVar("ndof","number of degrees of freedom",0) ;
80 _chi2red = new RooRealVar("chi2red","reduced chi^2",0) ;
81 _prob = new RooRealVar("prob","prob(chi2,ndof)",0) ;
82
83 // Create new dataset to be merged with RooMCStudy::fitParDataSet
84 _data = new RooDataSet("Chi2Data","Additional data for Chi2 study",RooArgSet(*_chi2,*_ndof,*_chi2red,*_prob)) ;
85
86 return true ;
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// Initialize module at beginning of RooCMStudy run
91
93{
94 _data->reset() ;
95 return true ;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Return auxiliary dataset with results of chi2 analysis
100/// calculations of this module so that it is merged with
101/// RooMCStudy::fitParDataSet() by RooMCStudy
102
104{
105 return _data ;
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// Bin dataset and calculate chi2 of p.d.f w.r.t binned dataset
110
112{
114 std::unique_ptr<RooDataHist> binnedDataOwned;
115 RooDataHist* binnedData = dynamic_cast<RooDataHist*>(data) ;
116 if (!binnedData) {
117 binnedDataOwned = std::unique_ptr<RooDataHist>{static_cast<RooDataSet*>(data)->binnedClone()};
118 binnedData = binnedDataOwned.get();
119 }
120
121 std::unique_ptr<RooAbsReal> chi2Var{fitModel()->createChi2(*binnedData,RooFit::Extended(extendedGen()),RooFit::DataError(RooAbsData::SumW2))};
122
123 std::unique_ptr<RooArgSet> floatPars{static_cast<RooArgSet*>(fitParams()->selectByAttrib("Constant",false))};
124
125 _chi2->setVal(chi2Var->getVal()) ;
126 _ndof->setVal(binnedData->numEntries()-floatPars->size()-1) ;
128 _prob->setVal(TMath::Prob(_chi2->getVal(),static_cast<int>(_ndof->getVal()))) ;
129
131
132 return true ;
133}
#define ClassImp(name)
Definition Rtypes.h:377
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...
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
virtual void reset()
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Base class for add-on modules to RooMCStudy that can perform additional calculations on each generate...
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.
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
virtual RooFit::OwningPtr< RooAbsReal > createChi2(RooDataHist &data, const RooLinkedList &cmdList)
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
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.
bool initializeRun(Int_t) override
Initialize module at beginning of RooCMStudy run.
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...
Container class to hold N-dimensional binned data.
Definition RooDataHist.h:39
const RooArgSet * get() const override
Get bin centre of current bin.
Definition RooDataHist.h:76
Container class to hold unbinned data.
Definition RooDataSet.h:57
void add(const RooArgSet &row, double weight, double weightError)
Add one ore more rows of data.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void setVal(double value) override
Set value of variable to 'value'.
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:637