RooNLLVar.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooNLLVar.cxx 37128 2010-11-30 22:24:50Z 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 // Class RooNLLVar implements a a -log(likelihood) calculation from a dataset
00021 // and a PDF. The NLL is calculated as 
00022 // <pre>
00023 //  Sum[data] -log( pdf(x_data) )
00024 // </pre>
00025 // In extended mode, a (Nexpect - Nobserved*log(NExpected) term is added
00026 // END_HTML
00027 //
00028 
00029 #include "RooFit.h"
00030 #include "Riostream.h"
00031 
00032 #include "RooNLLVar.h"
00033 #include "RooAbsData.h"
00034 #include "RooAbsPdf.h"
00035 #include "RooCmdConfig.h"
00036 #include "RooMsgService.h"
00037 
00038 #include "RooRealVar.h"
00039 
00040 
00041 ClassImp(RooNLLVar)
00042 ;
00043 
00044 RooArgSet RooNLLVar::_emptySet ;
00045 
00046 
00047 //_____________________________________________________________________________
00048 RooNLLVar::RooNLLVar(const char *name, const char* title, RooAbsPdf& pdf, RooAbsData& indata,
00049                      const RooCmdArg& arg1, const RooCmdArg& arg2,const RooCmdArg& arg3,
00050                      const RooCmdArg& arg4, const RooCmdArg& arg5,const RooCmdArg& arg6,
00051                      const RooCmdArg& arg7, const RooCmdArg& arg8,const RooCmdArg& arg9) :
00052   RooAbsOptTestStatistic(name,title,pdf,indata,
00053                          *(const RooArgSet*)RooCmdConfig::decodeObjOnTheFly("RooNLLVar::RooNLLVar","ProjectedObservables",0,&_emptySet
00054                                                                             ,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00055                          RooCmdConfig::decodeStringOnTheFly("RooNLLVar::RooNLLVar","RangeWithName",0,"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00056                          RooCmdConfig::decodeStringOnTheFly("RooNLLVar::RooNLLVar","AddCoefRange",0,"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00057                          RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","NumCPU",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00058                          kFALSE,
00059                          RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","Verbose",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00060                          RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","SplitRange",0,0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
00061                          RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","CloneData",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9))             
00062 {
00063   // Construct likelihood from given p.d.f and (binned or unbinned dataset)
00064   //
00065   //  Extended()     -- Include extended term in calculation
00066   //  NumCPU()       -- Activate parallel processing feature
00067   //  Range()        -- Fit only selected region
00068   //  SumCoefRange() -- Set the range in which to interpret the coefficients of RooAddPdf components 
00069   //  SplitRange()   -- Fit range is split by index catory of simultaneous PDF
00070   //  ConditionalObservables() -- Define conditional observables 
00071   //  Verbose()      -- Verbose output of GOF framework classes
00072   //  CloneData()    -- Clone input dataset for internal use (default is kTRUE)
00073 
00074   RooCmdConfig pc("RooNLLVar::RooNLLVar") ;
00075   pc.allowUndefined() ;
00076   pc.defineInt("extended","Extended",0,kFALSE) ;
00077 
00078   pc.process(arg1) ;  pc.process(arg2) ;  pc.process(arg3) ;
00079   pc.process(arg4) ;  pc.process(arg5) ;  pc.process(arg6) ;
00080   pc.process(arg7) ;  pc.process(arg8) ;  pc.process(arg9) ;
00081 
00082   _extended = pc.getInt("extended") ;
00083   _weightSq = kFALSE ;
00084 
00085 }
00086 
00087 
00088 
00089 //_____________________________________________________________________________
00090 RooNLLVar::RooNLLVar(const char *name, const char *title, RooAbsPdf& pdf, RooAbsData& indata,
00091                      Bool_t extended, const char* rangeName, const char* addCoefRangeName,
00092                      Int_t nCPU, Bool_t interleave, Bool_t verbose, Bool_t splitRange, Bool_t cloneData) : 
00093   RooAbsOptTestStatistic(name,title,pdf,indata,RooArgSet(),rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData),
00094   _extended(extended),
00095   _weightSq(kFALSE)
00096 {
00097   // Construct likelihood from given p.d.f and (binned or unbinned dataset)
00098   // For internal use.
00099 
00100 }
00101 
00102 
00103 
00104 //_____________________________________________________________________________
00105 RooNLLVar::RooNLLVar(const char *name, const char *title, RooAbsPdf& pdf, RooAbsData& indata,
00106                      const RooArgSet& projDeps, Bool_t extended, const char* rangeName,const char* addCoefRangeName, 
00107                      Int_t nCPU,Bool_t interleave,Bool_t verbose, Bool_t splitRange, Bool_t cloneData) : 
00108   RooAbsOptTestStatistic(name,title,pdf,indata,projDeps,rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData),
00109   _extended(extended),
00110   _weightSq(kFALSE)
00111 {
00112   // Construct likelihood from given p.d.f and (binned or unbinned dataset)
00113   // For internal use.  
00114 
00115 
00116 }
00117 
00118 
00119 
00120 //_____________________________________________________________________________
00121 RooNLLVar::RooNLLVar(const RooNLLVar& other, const char* name) : 
00122   RooAbsOptTestStatistic(other,name),
00123   _extended(other._extended),
00124   _weightSq(other._weightSq)
00125 {
00126   // Copy constructor
00127 }
00128 
00129 
00130 
00131 
00132 //_____________________________________________________________________________
00133 RooNLLVar::~RooNLLVar()
00134 {
00135   // Destructor
00136 }
00137 
00138 
00139 
00140 //_____________________________________________________________________________
00141 Double_t RooNLLVar::evaluatePartition(Int_t firstEvent, Int_t lastEvent, Int_t stepSize) const 
00142 {
00143   // Calculate and return likelihood on subset of data from firstEvent to lastEvent
00144   // processed with a step size of 'stepSize'. If this an extended likelihood and
00145   // and the zero event is processed the extended term is added to the return
00146   // likelihood.
00147 
00148   Int_t i ;
00149   Double_t result(0) ;
00150   
00151   RooAbsPdf* pdfClone = (RooAbsPdf*) _funcClone ;
00152 
00153   Double_t sumWeight(0) ;
00154   for (i=firstEvent ; i<lastEvent ; i+=stepSize) {
00155     
00156     // get the data values for this event
00157     //Double_t wgt = _dataClone->weight(i) ;
00158     //if (wgt==0) continue ;
00159 
00160     _dataClone->get(i);
00161 
00162     if (!_dataClone->valid()) {
00163       continue ;
00164     }
00165 
00166     if (_dataClone->weight()==0) continue ;
00167 
00168 
00169     Double_t eventWeight = _dataClone->weight() ;
00170     if (_weightSq) eventWeight *= eventWeight ;
00171 
00172     Double_t term = eventWeight * pdfClone->getLogVal(_normSet);
00173     sumWeight += eventWeight ;
00174 
00175     result-= term;
00176   }
00177   
00178   // include the extended maximum likelihood term, if requested
00179   if(_extended && firstEvent==0) {
00180     if (_weightSq) {
00181       // Calculate sum of weights-squared here for extended term
00182 
00183       Double_t sumW2(0) ;
00184       for (i=0 ; i<_dataClone->numEntries() ; i++) {
00185         _dataClone->get(i) ;
00186         Double_t eventWeight = _dataClone->weight() ;
00187         //cout << "sumW2 += " << eventWeight << " ^2" << endl ; 
00188         sumW2 += eventWeight * eventWeight ;    
00189       }
00190       //cout << "weight squared extended mode: sumW2 = " << sumW2 << " sumentries = " << _dataClone->sumEntries() << endl ;
00191 
00192       result+= pdfClone->extendedTerm((Int_t)sumW2,_dataClone->get());
00193     } else {
00194       result+= pdfClone->extendedTerm((Int_t)_dataClone->sumEntries(),_dataClone->get());
00195     }
00196   }    
00197 
00198   // If part of simultaneous PDF normalize probability over 
00199   // number of simultaneous PDFs: -sum(log(p/n)) = -sum(log(p)) + N*log(n) 
00200   if (_simCount>1) {
00201     result += sumWeight*log(1.0*_simCount) ;
00202   }
00203 
00204   //cout << "RooNLLVar(first=" << firstEvent << ", last=" << lastEvent << ", step=" << stepSize << ") result = " << result << endl ;
00205 
00206   return result ;
00207 }
00208 
00209 
00210 

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