Logo ROOT   6.10/09
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 
20 RooChi2MCSModule is an add-on modules to RooMCStudy that
21 calculates the chi-squared of fitted p.d.f with respect to a binned
22 version of the data. For each fit the chi-squared, the reduced chi-squared
23 the number of degrees of freedom and the probability of the chi-squared
24 is store in the summary dataset
25 **/
26 
27 #include "Riostream.h"
28 
29 #include "RooDataSet.h"
30 #include "RooRealVar.h"
31 #include "TString.h"
32 #include "RooFit.h"
33 #include "RooFitResult.h"
34 #include "RooChi2MCSModule.h"
35 #include "RooMsgService.h"
36 #include "RooChi2Var.h"
37 #include "RooDataHist.h"
38 #include "TMath.h"
39 #include "RooGlobalFunc.h"
40 
41 using namespace std;
42 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 
48  RooAbsMCStudyModule("RooChi2MCSModule","RooChi2Module"),
49  _data(0), _chi2(0), _ndof(0), _chi2red(0), _prob(0)
50 
51 {
52  // Constructor of module
53 }
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Copy constructor
57 
59  RooAbsMCStudyModule(other),
60  _data(0), _chi2(0), _ndof(0), _chi2red(0), _prob(0)
61 {
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Destructor
66 
68 {
69  if (_chi2) {
70  delete _chi2 ;
71  }
72  if (_ndof) {
73  delete _ndof ;
74  }
75  if (_chi2red) {
76  delete _chi2red ;
77  }
78  if (_prob) {
79  delete _prob ;
80  }
81  if (_data) {
82  delete _data ;
83  }
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Initialize module after attachment to RooMCStudy object
88 
90 {
91  // Construct variable that holds -log(L) fit with null hypothesis for given parameter
92  _chi2 = new RooRealVar("chi2","chi^2",0) ;
93  _ndof = new RooRealVar("ndof","number of degrees of freedom",0) ;
94  _chi2red = new RooRealVar("chi2red","reduced chi^2",0) ;
95  _prob = new RooRealVar("prob","prob(chi2,ndof)",0) ;
96 
97  // Create new dataset to be merged with RooMCStudy::fitParDataSet
98  _data = new RooDataSet("Chi2Data","Additional data for Chi2 study",RooArgSet(*_chi2,*_ndof,*_chi2red,*_prob)) ;
99 
100  return kTRUE ;
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Initialize module at beginning of RooCMStudy run
105 
107 {
108  _data->reset() ;
109  return kTRUE ;
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Return auxiliary dataset with results of chi2 analysis
114 /// calculations of this module so that it is merged with
115 /// RooMCStudy::fitParDataSet() by RooMCStudy
116 
118 {
119  return _data ;
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Bin dataset and calculate chi2 of p.d.f w.r.t binned dataset
124 
126 {
127  RooAbsData* data = genSample() ;
128  RooDataHist* binnedData = dynamic_cast<RooDataHist*>(data) ;
129  Bool_t deleteData(kFALSE) ;
130  if (!binnedData) {
131  deleteData = kTRUE ;
132  binnedData = ((RooDataSet*)data)->binnedClone() ;
133  }
134 
135  RooChi2Var chi2Var("chi2Var","chi2Var",*fitModel(),*binnedData,RooFit::Extended(extendedGen()),RooFit::DataError(RooAbsData::SumW2)) ;
136 
137  RooArgSet* floatPars = (RooArgSet*) fitParams()->selectByAttrib("Constant",kFALSE) ;
138 
139  _chi2->setVal(chi2Var.getVal()) ;
140  _ndof->setVal(binnedData->numEntries()-floatPars->getSize()-1) ;
142  _prob->setVal(TMath::Prob(_chi2->getVal(),static_cast<int>(_ndof->getVal()))) ;
143 
145 
146  if (deleteData) {
147  delete binnedData ;
148  }
149  delete floatPars ;
150 
151  return kTRUE ;
152 }
Bool_t initializeInstance()
Initialize module after attachment to RooMCStudy object.
RooAbsData * genSample()
RooAbsMCStudyModule is a base class for add-on modules to RooMCStudy that can perform additional calc...
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
Bool_t initializeRun(Int_t)
Initialize module at beginning of RooCMStudy run.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
STL namespace.
RooCmdArg Extended(Bool_t flag=kTRUE)
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:624
RooRealVar * _prob
RooRealVar * _chi2red
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
virtual void reset()
Definition: RooAbsData.cxx:276
RooCmdArg DataError(Int_t)
RooRealVar * _chi2
RooAbsCollection * selectByAttrib(const char *name, Bool_t value) const
Create a subset of the current collection, consisting only of those elements with the specified attri...
Bool_t processAfterFit(Int_t)
Bin dataset and calculate chi2 of p.d.f w.r.t binned dataset.
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooChi2MCSModule is an add-on modules to RooMCStudy that calculates the chi-squared of fitted p...
virtual void setVal(Double_t value)
Set value of variable to &#39;value&#39;.
Definition: RooRealVar.cxx:205
Int_t getSize() const
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
virtual void add(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0)
Add a data point, with its coordinates specified in the &#39;data&#39; argset, to the data set...
const Bool_t kFALSE
Definition: RtypesCore.h:92
#define ClassImp(name)
Definition: Rtypes.h:336
virtual Int_t numEntries() const
Return the number of bins.
RooDataSet * _data
RooDataSet * finalizeRun()
Return auxiliary dataset with results of chi2 analysis calculations of this module so that it is merg...
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual ~RooChi2MCSModule()
Destructor.
RooRealVar * _ndof