// 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 <algorithm>
#include "RooFit.h"
#include "Riostream.h"
#include "TMath.h"
#include "RooNLLVar.h"
#include "RooAbsData.h"
#include "RooAbsPdf.h"
#include "RooCmdConfig.h"
#include "RooMsgService.h"
#include "RooAbsDataStore.h"
#include "RooRealMPFE.h"
#include "RooRealSumPdf.h"
#include "RooRealVar.h"
#include "RooProdPdf.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),
RooFit::BulkPartition,
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 ;
_offset = 0.;
_offsetCarry = 0.;
_offsetSaveW2 = 0.;
_offsetCarrySaveW2 = 0.;
_binnedPdf = 0 ;
}
RooNLLVar::RooNLLVar(const char *name, const char *title, RooAbsPdf& pdf, RooAbsData& indata,
Bool_t extended, const char* rangeName, const char* addCoefRangeName,
Int_t nCPU, RooFit::MPSplit interleave, Bool_t verbose, Bool_t splitRange, Bool_t cloneData, Bool_t binnedL) :
RooAbsOptTestStatistic(name,title,pdf,indata,RooArgSet(),rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData),
_extended(extended),
_weightSq(kFALSE),
_first(kTRUE), _offsetSaveW2(0.), _offsetCarrySaveW2(0.)
{
_binnedPdf = binnedL ? (RooRealSumPdf*)_funcClone : 0 ;
if (_binnedPdf) {
RooArgSet* obs = _funcClone->getObservables(_dataClone) ;
if (obs->getSize()!=1) {
_binnedPdf = 0 ;
} else {
RooRealVar* var = (RooRealVar*) obs->first() ;
std::list<Double_t>* boundaries = _binnedPdf->binBoundaries(*var,var->getMin(),var->getMax()) ;
std::list<Double_t>::iterator biter = boundaries->begin() ;
_binw.resize(boundaries->size()-1) ;
Double_t lastBound = (*biter) ;
biter++ ;
int ibin=0 ;
while (biter!=boundaries->end()) {
_binw[ibin] = (*biter) - lastBound ;
lastBound = (*biter) ;
ibin++ ;
biter++ ;
}
}
}
}
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,RooFit::MPSplit interleave,Bool_t verbose, Bool_t splitRange, Bool_t cloneData, Bool_t binnedL) :
RooAbsOptTestStatistic(name,title,pdf,indata,projDeps,rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData),
_extended(extended),
_weightSq(kFALSE),
_first(kTRUE), _offsetSaveW2(0.), _offsetCarrySaveW2(0.)
{
_binnedPdf = binnedL ? (RooRealSumPdf*)_funcClone : 0 ;
if (_binnedPdf) {
RooArgSet* obs = _funcClone->getObservables(_dataClone) ;
if (obs->getSize()!=1) {
_binnedPdf = 0 ;
} else {
RooRealVar* var = (RooRealVar*) obs->first() ;
std::list<Double_t>* boundaries = _binnedPdf->binBoundaries(*var,var->getMin(),var->getMax()) ;
std::list<Double_t>::iterator biter = boundaries->begin() ;
_binw.resize(boundaries->size()-1) ;
Double_t lastBound = (*biter) ;
biter++ ;
int ibin=0 ;
while (biter!=boundaries->end()) {
_binw[ibin] = (*biter) - lastBound ;
lastBound = (*biter) ;
ibin++ ;
biter++ ;
}
}
}
}
RooNLLVar::RooNLLVar(const RooNLLVar& other, const char* name) :
RooAbsOptTestStatistic(other,name),
_extended(other._extended),
_weightSq(other._weightSq),
_first(kTRUE), _offsetSaveW2(other._offsetSaveW2),
_offsetCarrySaveW2(other._offsetCarrySaveW2),
_binw(other._binw) {
_binnedPdf = other._binnedPdf ? (RooRealSumPdf*)_funcClone : 0 ;
}
RooNLLVar::~RooNLLVar()
{
}
void RooNLLVar::applyWeightSquared(Bool_t flag)
{
if (_gofOpMode==Slave) {
if (flag != _weightSq) {
_weightSq = flag;
std::swap(_offset, _offsetSaveW2);
std::swap(_offsetCarry, _offsetCarrySaveW2);
}
setValueDirty();
} else if ( _gofOpMode==MPMaster) {
for (Int_t i=0 ; i<_nCPU ; i++)
_mpfeArray[i]->applyNLLWeightSquared(flag);
} else if ( _gofOpMode==SimMaster) {
for (Int_t i=0 ; i<_nGof ; i++)
((RooNLLVar*)_gofArray[i])->applyWeightSquared(flag);
}
}
Double_t RooNLLVar::evaluatePartition(Int_t firstEvent, Int_t lastEvent, Int_t stepSize) const
{
Int_t i ;
Double_t result(0), carry(0);
RooAbsPdf* pdfClone = (RooAbsPdf*) _funcClone ;
_dataClone->store()->recalculateCache( _projDeps, firstEvent, lastEvent, stepSize ) ;
Double_t sumWeight(0), sumWeightCarry(0);
if (_binnedPdf) {
for (i=firstEvent ; i<lastEvent ; i+=stepSize) {
_dataClone->get(i) ;
if (!_dataClone->valid()) continue;
Double_t eventWeight = _dataClone->weight();
Double_t N = eventWeight ;
Double_t mu = _binnedPdf->getVal()*_binw[i] ;
Double_t term = -1*(-mu + N*log(mu) - TMath::LnGamma(N+1)) ;
Double_t y = eventWeight - sumWeightCarry;
Double_t t = sumWeight + y;
sumWeightCarry = (t - sumWeight) - y;
sumWeight = t;
y = term - carry;
t = result + y;
carry = (t - result) - y;
result = t;
}
} else {
for (i=firstEvent ; i<lastEvent ; i+=stepSize) {
_dataClone->get(i) ;
if (!_dataClone->valid()) continue;
Double_t eventWeight = _dataClone->weight();
if (0. == eventWeight * eventWeight) continue ;
if (_weightSq) eventWeight = _dataClone->weightSquared() ;
Double_t term = -eventWeight * pdfClone->getLogVal(_normSet);
Double_t y = eventWeight - sumWeightCarry;
Double_t t = sumWeight + y;
sumWeightCarry = (t - sumWeight) - y;
sumWeight = t;
y = term - carry;
t = result + y;
carry = (t - result) - y;
result = t;
}
if(_extended && _setNum==_extSet) {
if (_weightSq) {
Double_t sumW2(0), sumW2carry(0);
for (i=0 ; i<_dataClone->numEntries() ; i++) {
_dataClone->get(i);
Double_t y = _dataClone->weightSquared() - sumW2carry;
Double_t t = sumW2 + y;
sumW2carry = (t - sumW2) - y;
sumW2 = t;
}
Double_t expected= pdfClone->expectedEvents(_dataClone->get());
Double_t expectedW2 = expected * sumW2 / _dataClone->sumEntries() ;
Double_t extra= expectedW2 - sumW2*log(expected );
Double_t y = extra - carry ;
Double_t t = result + y;
carry = (t - result) - y;
result = t;
} else {
Double_t y = pdfClone->extendedTerm(_dataClone->sumEntries(), _dataClone->get()) - carry;
Double_t t = result + y;
carry = (t - result) - y;
result = t;
}
}
}
if (_simCount>1) {
Double_t y = sumWeight*log(1.0*_simCount) - carry;
Double_t t = result + y;
carry = (t - result) - y;
result = t;
}
if (_first) {
_first = kFALSE ;
_funcClone->wireAllCaches() ;
}
if (_doOffset) {
if (_offset==0 && result !=0 ) {
coutI(Minimization) << "RooNLLVar::evaluatePartition(" << GetName() << ") first = "<< firstEvent << " last = " << lastEvent << " Likelihood offset now set to " << result << std::endl ;
_offset = result ;
_offsetCarry = carry;
}
Double_t y = -_offset - (carry + _offsetCarry);
Double_t t = result + y;
carry = (t - result) - y;
result = t;
}
_evalCarry = carry;
return result ;
}