RooChi2MCSModule.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooChi2MCSModule.cxx 28963 2009-06-12 15:47:45Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 // 
00019 // BEGIN_HTML
00020 // RooChi2MCSModule is an add-on modules to RooMCStudy that
00021 // calculates the chi-squared of fitted p.d.f with respect to a binned
00022 // version of the data. For each fit the chi-squared, the reduced chi-squared
00023 // the number of degrees of freedom and the probability of the chi-squared
00024 // is store in the summary dataset
00025 // END_HTML
00026 //
00027 //
00028 
00029 #include "Riostream.h"
00030 
00031 #include "RooDataSet.h"
00032 #include "RooRealVar.h"
00033 #include "TString.h"
00034 #include "RooFit.h"
00035 #include "RooFitResult.h"
00036 #include "RooChi2MCSModule.h"
00037 #include "RooMsgService.h"
00038 #include "RooChi2Var.h"
00039 #include "RooDataHist.h"
00040 #include "TMath.h"
00041 #include "RooGlobalFunc.h"
00042 
00043 
00044 
00045 ClassImp(RooChi2MCSModule)
00046   ;
00047 
00048 
00049 
00050 //_____________________________________________________________________________
00051 RooChi2MCSModule::RooChi2MCSModule() : 
00052   RooAbsMCStudyModule("RooChi2MCSModule","RooChi2Module"),
00053   _data(0), _chi2(0), _ndof(0), _chi2red(0), _prob(0)
00054 
00055 {
00056   // Constructor of module 
00057 }
00058 
00059 
00060 
00061 
00062 //_____________________________________________________________________________
00063 RooChi2MCSModule::RooChi2MCSModule(const RooChi2MCSModule& other) : 
00064   RooAbsMCStudyModule(other), 
00065   _data(0), _chi2(0), _ndof(0), _chi2red(0), _prob(0)
00066 {
00067   // Copy constructor
00068 }
00069 
00070 
00071 
00072 //_____________________________________________________________________________
00073 RooChi2MCSModule:: ~RooChi2MCSModule() 
00074 {
00075   // Destructor
00076 
00077   if (_chi2) {
00078     delete _chi2 ;
00079   }
00080   if (_ndof) {
00081     delete _ndof ;
00082   }
00083   if (_chi2red) {
00084     delete _chi2red ;
00085   }
00086   if (_prob) {
00087     delete _prob ;
00088   }
00089   if (_data) {
00090     delete _data ;
00091   }
00092 }
00093 
00094 
00095 
00096 //_____________________________________________________________________________
00097 Bool_t RooChi2MCSModule::initializeInstance()
00098 {
00099   // Initialize module after attachment to RooMCStudy object
00100 
00101   // Construct variable that holds -log(L) fit with null hypothesis for given parameter
00102   _chi2     = new RooRealVar("chi2","chi^2",0) ;
00103   _ndof     = new RooRealVar("ndof","number of degrees of freedom",0) ;   
00104   _chi2red  = new RooRealVar("chi2red","reduced chi^2",0) ; 
00105   _prob     = new RooRealVar("prob","prob(chi2,ndof)",0) ;
00106 
00107   // Create new dataset to be merged with RooMCStudy::fitParDataSet
00108   _data = new RooDataSet("Chi2Data","Additional data for Chi2 study",RooArgSet(*_chi2,*_ndof,*_chi2red,*_prob)) ;
00109 
00110   return kTRUE ;
00111 }
00112 
00113 
00114 
00115 //_____________________________________________________________________________
00116 Bool_t RooChi2MCSModule::initializeRun(Int_t /*numSamples*/) 
00117 {
00118   // Initialize module at beginning of RooCMStudy run
00119 
00120   _data->reset() ;
00121   return kTRUE ;
00122 }
00123 
00124 
00125 
00126 //_____________________________________________________________________________
00127 RooDataSet* RooChi2MCSModule::finalizeRun() 
00128 {
00129   // Return auxiliary dataset with results of chi2 analysis
00130   // calculations of this module so that it is merged with
00131   // RooMCStudy::fitParDataSet() by RooMCStudy
00132 
00133   return _data ;
00134 }
00135 
00136 
00137 
00138 //_____________________________________________________________________________
00139 Bool_t RooChi2MCSModule::processAfterFit(Int_t /*sampleNum*/)  
00140 {
00141   // Bin dataset and calculate chi2 of p.d.f w.r.t binned dataset
00142 
00143   RooAbsData* data = genSample() ;
00144   RooDataHist* binnedData = dynamic_cast<RooDataHist*>(data) ;
00145   Bool_t deleteData(kFALSE) ;
00146   if (!binnedData) {
00147     deleteData = kTRUE ;
00148     binnedData = ((RooDataSet*)data)->binnedClone() ;
00149   }
00150 
00151   RooChi2Var chi2Var("chi2Var","chi2Var",*fitModel(),*binnedData,RooFit::Extended(extendedGen()),RooFit::DataError(RooAbsData::SumW2)) ;
00152 
00153   RooArgSet* floatPars = (RooArgSet*) fitParams()->selectByAttrib("Constant",kFALSE) ;  
00154 
00155   _chi2->setVal(chi2Var.getVal()) ;
00156   _ndof->setVal(binnedData->numEntries()-floatPars->getSize()-1) ; 
00157   _chi2red->setVal(_chi2->getVal()/_ndof->getVal()) ;
00158   _prob->setVal(TMath::Prob(_chi2->getVal(),static_cast<int>(_ndof->getVal()))) ;
00159 
00160   _data->add(RooArgSet(*_chi2,*_ndof,*_chi2red,*_prob)) ;
00161 
00162   if (deleteData) {
00163     delete binnedData ;
00164   }
00165   delete floatPars ;
00166 
00167   return kTRUE ;
00168 }

Generated on Tue Jul 5 14:55:10 2011 for ROOT_528-00b_version by  doxygen 1.5.1