// Class RooNLLVar implements a a -log(likelihood) calculation from a dataset
// and a PDF. The NLL is calculated as
// <pre>
// Sum[data] -log( pdf(x_data) )
// </pre>
// In extended mode, a (Nexpect - Nobserved*log(NExpected) term is added
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include "RooNLLVar.h"
#include "RooAbsData.h"
#include "RooAbsPdf.h"
#include "RooCmdConfig.h"
#include "RooMsgService.h"
#include "RooAbsDataStore.h"
#include "RooRealVar.h"
ClassImp(RooNLLVar)
;
RooArgSet RooNLLVar::_emptySet ;
RooNLLVar::RooNLLVar(const char *name, const char* title, RooAbsPdf& pdf, RooAbsData& indata,
const RooCmdArg& arg1, const RooCmdArg& arg2,const RooCmdArg& arg3,
const RooCmdArg& arg4, const RooCmdArg& arg5,const RooCmdArg& arg6,
const RooCmdArg& arg7, const RooCmdArg& arg8,const RooCmdArg& arg9) :
RooAbsOptTestStatistic(name,title,pdf,indata,
*(const RooArgSet*)RooCmdConfig::decodeObjOnTheFly("RooNLLVar::RooNLLVar","ProjectedObservables",0,&_emptySet
,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
RooCmdConfig::decodeStringOnTheFly("RooNLLVar::RooNLLVar","RangeWithName",0,"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
RooCmdConfig::decodeStringOnTheFly("RooNLLVar::RooNLLVar","AddCoefRange",0,"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","NumCPU",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
kFALSE,
RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","Verbose",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","SplitRange",0,0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","CloneData",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9))
{
RooCmdConfig pc("RooNLLVar::RooNLLVar") ;
pc.allowUndefined() ;
pc.defineInt("extended","Extended",0,kFALSE) ;
pc.process(arg1) ; pc.process(arg2) ; pc.process(arg3) ;
pc.process(arg4) ; pc.process(arg5) ; pc.process(arg6) ;
pc.process(arg7) ; pc.process(arg8) ; pc.process(arg9) ;
_extended = pc.getInt("extended") ;
_weightSq = kFALSE ;
_first = kTRUE ;
}
RooNLLVar::RooNLLVar(const char *name, const char *title, RooAbsPdf& pdf, RooAbsData& indata,
Bool_t extended, const char* rangeName, const char* addCoefRangeName,
Int_t nCPU, Bool_t interleave, Bool_t verbose, Bool_t splitRange, Bool_t cloneData) :
RooAbsOptTestStatistic(name,title,pdf,indata,RooArgSet(),rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData),
_extended(extended),
_weightSq(kFALSE),
_first(kTRUE)
{
}
RooNLLVar::RooNLLVar(const char *name, const char *title, RooAbsPdf& pdf, RooAbsData& indata,
const RooArgSet& projDeps, Bool_t extended, const char* rangeName,const char* addCoefRangeName,
Int_t nCPU,Bool_t interleave,Bool_t verbose, Bool_t splitRange, Bool_t cloneData) :
RooAbsOptTestStatistic(name,title,pdf,indata,projDeps,rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData),
_extended(extended),
_weightSq(kFALSE),
_first(kTRUE)
{
}
RooNLLVar::RooNLLVar(const RooNLLVar& other, const char* name) :
RooAbsOptTestStatistic(other,name),
_extended(other._extended),
_weightSq(other._weightSq),
_first(kTRUE)
{
}
RooNLLVar::~RooNLLVar()
{
}
Double_t RooNLLVar::evaluatePartition(Int_t firstEvent, Int_t lastEvent, Int_t stepSize) const
{
Int_t i ;
Double_t result(0) ;
RooAbsPdf* pdfClone = (RooAbsPdf*) _funcClone ;
_dataClone->store()->recalculateCache() ;
Double_t sumWeight(0) ;
for (i=firstEvent ; i<lastEvent ; i+=stepSize) {
_dataClone->get(i) ;
if (!_dataClone->valid()) {
continue ;
}
if (_dataClone->weight()==0) continue ;
Double_t eventWeight = _dataClone->weight() ;
if (_weightSq) eventWeight *= eventWeight ;
Double_t term = eventWeight * pdfClone->getLogVal(_normSet);
sumWeight += eventWeight ;
result-= term;
}
if(_extended && firstEvent==0) {
if (_weightSq) {
Double_t sumW2(0) ;
for (i=0 ; i<_dataClone->numEntries() ; i++) {
_dataClone->get(i) ;
Double_t eventWeight = _dataClone->weight() ;
sumW2 += eventWeight * eventWeight ;
}
result+= pdfClone->extendedTerm(sumW2 , _dataClone->get());
} else {
result+= pdfClone->extendedTerm(_dataClone->sumEntries(),_dataClone->get());
}
}
if (_simCount>1) {
result += sumWeight*log(1.0*_simCount) ;
}
if (_first) {
_first = kFALSE ;
_funcClone->wireAllCaches() ;
}
return result ;
}