ROOT  6.06/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 //////////////////////////////////////////////////////////////////////////////
18 //
19 // BEGIN_HTML
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 // END_HTML
26 //
27 //
28 
29 #include "Riostream.h"
30 
31 #include "RooDataSet.h"
32 #include "RooRealVar.h"
33 #include "TString.h"
34 #include "RooFit.h"
35 #include "RooFitResult.h"
36 #include "RooChi2MCSModule.h"
37 #include "RooMsgService.h"
38 #include "RooChi2Var.h"
39 #include "RooDataHist.h"
40 #include "TMath.h"
41 #include "RooGlobalFunc.h"
42 
43 
44 
45 using namespace std;
46 
48  ;
49 
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 
55  RooAbsMCStudyModule("RooChi2MCSModule","RooChi2Module"),
56  _data(0), _chi2(0), _ndof(0), _chi2red(0), _prob(0)
57 
58 {
59  // Constructor of module
60 }
61 
62 
63 
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Copy constructor
67 
69  RooAbsMCStudyModule(other),
70  _data(0), _chi2(0), _ndof(0), _chi2red(0), _prob(0)
71 {
72 }
73 
74 
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Destructor
78 
80 {
81  if (_chi2) {
82  delete _chi2 ;
83  }
84  if (_ndof) {
85  delete _ndof ;
86  }
87  if (_chi2red) {
88  delete _chi2red ;
89  }
90  if (_prob) {
91  delete _prob ;
92  }
93  if (_data) {
94  delete _data ;
95  }
96 }
97 
98 
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Initialize module after attachment to RooMCStudy object
102 
104 {
105  // Construct variable that holds -log(L) fit with null hypothesis for given parameter
106  _chi2 = new RooRealVar("chi2","chi^2",0) ;
107  _ndof = new RooRealVar("ndof","number of degrees of freedom",0) ;
108  _chi2red = new RooRealVar("chi2red","reduced chi^2",0) ;
109  _prob = new RooRealVar("prob","prob(chi2,ndof)",0) ;
110 
111  // Create new dataset to be merged with RooMCStudy::fitParDataSet
112  _data = new RooDataSet("Chi2Data","Additional data for Chi2 study",RooArgSet(*_chi2,*_ndof,*_chi2red,*_prob)) ;
113 
114  return kTRUE ;
115 }
116 
117 
118 
119 ////////////////////////////////////////////////////////////////////////////////
120 /// Initialize module at beginning of RooCMStudy run
121 
123 {
124  _data->reset() ;
125  return kTRUE ;
126 }
127 
128 
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Return auxiliary dataset with results of chi2 analysis
132 /// calculations of this module so that it is merged with
133 /// RooMCStudy::fitParDataSet() by RooMCStudy
134 
136 {
137  return _data ;
138 }
139 
140 
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// Bin dataset and calculate chi2 of p.d.f w.r.t binned dataset
144 
146 {
147  RooAbsData* data = genSample() ;
148  RooDataHist* binnedData = dynamic_cast<RooDataHist*>(data) ;
149  Bool_t deleteData(kFALSE) ;
150  if (!binnedData) {
151  deleteData = kTRUE ;
152  binnedData = ((RooDataSet*)data)->binnedClone() ;
153  }
154 
155  RooChi2Var chi2Var("chi2Var","chi2Var",*fitModel(),*binnedData,RooFit::Extended(extendedGen()),RooFit::DataError(RooAbsData::SumW2)) ;
156 
157  RooArgSet* floatPars = (RooArgSet*) fitParams()->selectByAttrib("Constant",kFALSE) ;
158 
159  _chi2->setVal(chi2Var.getVal()) ;
160  _ndof->setVal(binnedData->numEntries()-floatPars->getSize()-1) ;
162  _prob->setVal(TMath::Prob(_chi2->getVal(),static_cast<int>(_ndof->getVal()))) ;
163 
165 
166  if (deleteData) {
167  delete binnedData ;
168  }
169  delete floatPars ;
170 
171  return kTRUE ;
172 }
Bool_t initializeInstance()
Initialize module after attachment to RooMCStudy object.
RooAbsData * genSample()
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
const Bool_t kFALSE
Definition: Rtypes.h:92
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:619
RooRealVar * _prob
RooRealVar * _chi2red
virtual void reset()
Definition: RooAbsData.cxx:302
RooCmdArg DataError(Int_t)
RooRealVar * _chi2
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
Bool_t processAfterFit(Int_t)
Bin dataset and calculate chi2 of p.d.f w.r.t binned dataset.
ClassImp(RooChi2MCSModule)
virtual void setVal(Double_t value)
Set value of variable to 'value'.
Definition: RooRealVar.cxx:202
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 'data' argset, to the data set...
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...
Int_t getSize() const
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual ~RooChi2MCSModule()
Destructor.
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...
RooRealVar * _ndof