RooChi2Var.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooChi2Var.cxx 29139 2009-06-22 14:31:47Z brun $
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 // Class RooChi2Var implements a simple chi^2 calculation from a binned dataset
00020 // and a PDF. The chi^2 is calculated as 
00021 //
00022 //             / (f_PDF * N_tot/ V_bin) - N_bin \+2
00023 //  Sum[bins] |  ------------------------------ |
00024 //             \         err_bin                /
00025 //
00026 // If no user-defined errors are defined for the dataset, poisson errors
00027 // are used. In extended PDF mode, N_tot is substituted with N_expected.
00028 //
00029 
00030 #include "RooFit.h"
00031 
00032 #include "RooChi2Var.h"
00033 #include "RooChi2Var.h"
00034 #include "RooDataHist.h"
00035 #include "RooAbsPdf.h"
00036 #include "RooCmdConfig.h"
00037 #include "RooMsgService.h"
00038 
00039 #include "Riostream.h"
00040 
00041 #include "RooRealVar.h"
00042 
00043 
00044 ClassImp(RooChi2Var)
00045 ;
00046 
00047 RooArgSet RooChi2Var::_emptySet ;
00048 
00049 
00050 //_____________________________________________________________________________
00051 RooChi2Var::RooChi2Var(const char *name, const char* title, RooAbsReal& func, RooDataHist& hdata,
00052                        const RooCmdArg& arg1,const RooCmdArg& arg2,const RooCmdArg& arg3,
00053                        const RooCmdArg& arg4,const RooCmdArg& arg5,const RooCmdArg& arg6,
00054                        const RooCmdArg& arg7,const RooCmdArg& arg8,const RooCmdArg& arg9) :
00055   RooAbsOptTestStatistic(name,title,func,hdata,_emptySet,
00056                          RooCmdConfig::decodeStringOnTheFly("RooChi2Var::RooChi2Var","RangeWithName",0,"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00057                          0,
00058                          RooCmdConfig::decodeIntOnTheFly("RooChi2Var::RooChi2Var","NumCPU",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00059                          RooCmdConfig::decodeIntOnTheFly("RooChi2Var::RooChi2Var","Verbose",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00060                          0)
00061   //  RooChi2Var constructor. Optional arguments taken
00062   //
00063   //  DataError()  -- Choose between Poisson errors and Sum-of-weights errors
00064   //  NumCPU()     -- Activate parallel processing feature
00065   //  Range()      -- Fit only selected region
00066   //  Verbose()    -- Verbose output of GOF framework
00067 {
00068   RooCmdConfig pc("RooChi2Var::RooChi2Var") ;
00069   pc.defineInt("etype","DataError",0,(Int_t)RooDataHist::SumW2) ;  
00070   pc.allowUndefined() ;
00071 
00072   pc.process(arg1) ;  pc.process(arg2) ;  pc.process(arg3) ;
00073   pc.process(arg4) ;  pc.process(arg5) ;  pc.process(arg6) ;
00074   pc.process(arg7) ;  pc.process(arg8) ;  pc.process(arg9) ;
00075 
00076   _funcMode = Function ;
00077   _etype = (RooDataHist::ErrorType) pc.getInt("etype") ;
00078 }
00079 
00080 
00081 
00082 //_____________________________________________________________________________
00083 RooChi2Var::RooChi2Var(const char *name, const char* title, RooAbsPdf& pdf, RooDataHist& hdata,
00084                        const RooCmdArg& arg1,const RooCmdArg& arg2,const RooCmdArg& arg3,
00085                        const RooCmdArg& arg4,const RooCmdArg& arg5,const RooCmdArg& arg6,
00086                        const RooCmdArg& arg7,const RooCmdArg& arg8,const RooCmdArg& arg9) :
00087   RooAbsOptTestStatistic(name,title,pdf,hdata,
00088                          *(const RooArgSet*)RooCmdConfig::decodeObjOnTheFly("RooChi2Var::RooChi2Var","ProjectedObservables",0,&_emptySet
00089                                                                             ,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00090                          RooCmdConfig::decodeStringOnTheFly("RooChi2Var::RooChi2Var","RangeWithName",0,"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00091                          RooCmdConfig::decodeStringOnTheFly("RooChi2Var::RooChi2Var","AddCoefRange",0,"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00092                          RooCmdConfig::decodeIntOnTheFly("RooChi2Var::RooChi2Var","NumCPU",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00093                          RooCmdConfig::decodeIntOnTheFly("RooChi2Var::RooChi2Var","Verbose",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00094                          RooCmdConfig::decodeIntOnTheFly("RooChi2Var::RooChi2Var","SplitRange",0,0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9))             
00095   //  RooChi2Var constructor. Optional arguments taken
00096   //
00097   //  Extended()   -- Include extended term in calculation
00098   //  DataError()  -- Choose between Poisson errors and Sum-of-weights errors
00099   //  NumCPU()     -- Activate parallel processing feature
00100   //  Range()      -- Fit only selected region
00101   //  SumCoefRange() -- Set the range in which to interpret the coefficients of RooAddPdf components 
00102   //  SplitRange() -- Fit range is split by index catory of simultaneous PDF
00103   //  ConditionalObservables() -- Define projected observables 
00104   //  Verbose()    -- Verbose output of GOF framework
00105 {
00106   RooCmdConfig pc("RooChi2Var::RooChi2Var") ;
00107   pc.defineInt("extended","Extended",0,kFALSE) ;
00108   pc.defineInt("etype","DataError",0,(Int_t)RooDataHist::SumW2) ;  
00109   pc.allowUndefined() ;
00110 
00111   pc.process(arg1) ;  pc.process(arg2) ;  pc.process(arg3) ;
00112   pc.process(arg4) ;  pc.process(arg5) ;  pc.process(arg6) ;
00113   pc.process(arg7) ;  pc.process(arg8) ;  pc.process(arg9) ;
00114 
00115   _funcMode = pc.getInt("extended") ? ExtendedPdf : Pdf ;
00116   _etype = (RooDataHist::ErrorType) pc.getInt("etype") ;
00117 }
00118 
00119 
00120 
00121 //_____________________________________________________________________________
00122 RooChi2Var::RooChi2Var(const char *name, const char *title, RooAbsPdf& pdf, RooDataHist& hdata,
00123                        Bool_t extended, const char* cutRange, const char* addCoefRange,
00124                        Int_t nCPU, Bool_t interleave, Bool_t verbose, Bool_t splitCutRange, RooDataHist::ErrorType etype) : 
00125   RooAbsOptTestStatistic(name,title,pdf,hdata,RooArgSet(),cutRange,addCoefRange,nCPU,interleave,verbose,splitCutRange),
00126    _etype(etype), _funcMode(extended?ExtendedPdf:Pdf)
00127 {
00128   // Constructor of a chi2 for given p.d.f. with respect given binned
00129   // dataset. If cutRange is specified the calculation of the chi2 is
00130   // restricted to that named range. If addCoefRange is specified, the
00131   // interpretation of fractions for all component RooAddPdfs that do
00132   // not have a frozen range interpretation is set to chosen range
00133   // name. If nCPU is greater than one the chi^2 calculation is
00134   // paralellized over the specified number of processors. If
00135   // interleave is true the partitioning of event over processors
00136   // follows a (i % n == i_set) strategy rather than a bulk
00137   // partitioning strategy which may result in unequal load balancing
00138   // in binned datasets with many (adjacent) zero bins. If
00139   // splitCutRange is true the cutRange is used to construct an
00140   // individual cutRange for each RooSimultaneous index category state
00141   // name cutRange_{indexStateName}.
00142 }
00143 
00144 
00145 
00146 //_____________________________________________________________________________
00147 RooChi2Var::RooChi2Var(const char *name, const char *title, RooAbsReal& func, RooDataHist& hdata,
00148                        const RooArgSet& projDeps, RooChi2Var::FuncMode fmode, const char* cutRange, const char* addCoefRange, 
00149                        Int_t nCPU, Bool_t interleave, Bool_t verbose, Bool_t splitCutRange, RooDataHist::ErrorType etype) : 
00150   RooAbsOptTestStatistic(name,title,func,hdata,projDeps,cutRange,addCoefRange,nCPU,interleave,verbose,splitCutRange),
00151   _etype(etype), _funcMode(fmode)
00152 {
00153   // Constructor of a chi2 for given p.d.f. with respect given binned
00154   // dataset taking the observables specified in projDeps as projected
00155   // observables. If cutRange is specified the calculation of the chi2
00156   // is restricted to that named range. If addCoefRange is specified,
00157   // the interpretation of fractions for all component RooAddPdfs that
00158   // do not have a frozen range interpretation is set to chosen range
00159   // name. If nCPU is greater than one the chi^2 calculation is
00160   // paralellized over the specified number of processors. If
00161   // interleave is true the partitioning of event over processors
00162   // follows a (i % n == i_set) strategy rather than a bulk
00163   // partitioning strategy which may result in unequal load balancing
00164   // in binned datasets with many (adjacent) zero bins. If
00165   // splitCutRange is true the cutRange is used to construct an
00166   // individual cutRange for each RooSimultaneous index category state
00167   // name cutRange_{indexStateName}.
00168 }
00169 
00170 
00171 
00172 //_____________________________________________________________________________
00173 RooChi2Var::RooChi2Var(const RooChi2Var& other, const char* name) : 
00174   RooAbsOptTestStatistic(other,name),
00175   _etype(other._etype),
00176   _funcMode(other._funcMode)
00177 {
00178   // Copy constructor
00179 }
00180 
00181 
00182 
00183 //_____________________________________________________________________________
00184 RooChi2Var::~RooChi2Var()
00185 {
00186   // Destructor
00187 }
00188 
00189 
00190 
00191 //_____________________________________________________________________________
00192 Double_t RooChi2Var::evaluatePartition(Int_t firstEvent, Int_t lastEvent, Int_t stepSize) const 
00193 {
00194   // Calculate chi^2 in partition from firstEvent to lastEvent using given stepSize
00195 
00196   Int_t i ;
00197   Double_t result(0) ;
00198 
00199 
00200   // Determine normalization factor depending on type of input function
00201   Double_t normFactor(Function) ;
00202   switch (_funcMode) {
00203   case Function: normFactor=1 ; break ;
00204   case Pdf: normFactor = _dataClone->sumEntries() ; break ;
00205   case ExtendedPdf: normFactor = ((RooAbsPdf*)_funcClone)->expectedEvents(_dataClone->get()) ; break ;
00206   }
00207 
00208   // Loop over bins of dataset
00209   RooDataHist* hdata = (RooDataHist*) _dataClone ;
00210     for (i=firstEvent ; i<lastEvent ; i+=stepSize) {
00211     
00212     // get the data values for this event
00213     hdata->get(i);
00214 
00215     if (!hdata->valid()) {
00216       continue ;
00217     }
00218 
00219     Double_t nData = hdata->weight() ;
00220 
00221     Double_t nPdf = _funcClone->getVal(_normSet) * normFactor * hdata->binVolume() ;
00222 
00223     Double_t eExt = nPdf-nData ;
00224 
00225     Double_t eIntLo,eIntHi ;
00226     hdata->weightError(eIntLo,eIntHi,_etype) ;
00227     Double_t eInt = (eExt>0) ? eIntHi : eIntLo ;
00228 
00229     
00230     // Skip cases where pdf=0 and there is no data
00231     if (eInt==0. && nData==0. && nPdf==0) continue ;
00232 
00233     // Return 0 if eInt=0, special handling in MINUIT will follow
00234     if (eInt==0.) {
00235       coutE(Eval) << "RooChi2Var::RooChi2Var(" << GetName() << ") INFINITY ERROR: bin " << i 
00236                   << " has zero error, but function is not zero (" << nPdf << ")" << endl ;
00237       return 0 ;
00238     }
00239 
00240     //cout << "Chi2Var[" << i << "] nData = " << nData << " nPdf = " << nPdf << " errorExt = " << eExt << " errorInt = " << eInt << " contrib = " << eExt*eExt/(eInt*eInt) << endl ;
00241 
00242     result += eExt*eExt/(eInt*eInt) ;
00243   }
00244   
00245 
00246   return result ;
00247 }
00248 
00249 
00250 

Generated on Tue Jul 5 15:06:20 2011 for ROOT_528-00b_version by  doxygen 1.5.1