#include "RooFit.h"
#include "Riostream.h"
#include "TIterator.h"
#include "TList.h"
#include "RooRealSumPdf.h"
#include "RooRealProxy.h"
#include "RooPlot.h"
#include "RooRealVar.h"
#include "RooAddGenContext.h"
#include "RooRealConstant.h"
#include "RooRealIntegral.h"
#include "RooMsgService.h"
ClassImp(RooRealSumPdf)
;
RooRealSumPdf::RooRealSumPdf()
{
_funcIter = _funcList.createIterator() ;
_coefIter = _coefList.createIterator() ;
_extended = kFALSE ;
}
RooRealSumPdf::RooRealSumPdf(const char *name, const char *title) :
RooAbsPdf(name,title),
_normIntMgr(this,10),
_haveLastCoef(kFALSE),
_funcList("!funcList","List of functions",this),
_coefList("!coefList","List of coefficients",this),
_extended(kFALSE)
{
_funcIter = _funcList.createIterator() ;
_coefIter = _coefList.createIterator() ;
}
RooRealSumPdf::RooRealSumPdf(const char *name, const char *title,
RooAbsReal& func1, RooAbsReal& func2, RooAbsReal& coef1) :
RooAbsPdf(name,title),
_normIntMgr(this,10),
_haveLastCoef(kFALSE),
_funcList("!funcList","List of functions",this),
_coefList("!coefList","List of coefficients",this),
_extended(kFALSE)
{
_funcIter = _funcList.createIterator() ;
_coefIter = _coefList.createIterator() ;
_funcList.add(func1) ;
_funcList.add(func2) ;
_coefList.add(coef1) ;
}
RooRealSumPdf::RooRealSumPdf(const char *name, const char *title, const RooArgList& inFuncList, const RooArgList& inCoefList, Bool_t extended) :
RooAbsPdf(name,title),
_normIntMgr(this,10),
_haveLastCoef(kFALSE),
_funcList("!funcList","List of functions",this),
_coefList("!coefList","List of coefficients",this),
_extended(extended)
{
if (!(inFuncList.getSize()==inCoefList.getSize()+1 || inFuncList.getSize()==inCoefList.getSize())) {
coutE(InputArguments) << "RooRealSumPdf::RooRealSumPdf(" << GetName()
<< ") number of pdfs and coefficients inconsistent, must have Nfunc=Ncoef or Nfunc=Ncoef+1" << endl ;
assert(0) ;
}
_funcIter = _funcList.createIterator() ;
_coefIter = _coefList.createIterator() ;
TIterator* funcIter = inFuncList.createIterator() ;
TIterator* coefIter = inCoefList.createIterator() ;
RooAbsArg* func ;
RooAbsArg* coef ;
while((coef = (RooAbsArg*)coefIter->Next())) {
func = (RooAbsArg*) funcIter->Next() ;
if (!dynamic_cast<RooAbsReal*>(coef)) {
coutW(InputArguments) << "RooRealSumPdf::RooRealSumPdf(" << GetName() << ") coefficient " << coef->GetName() << " is not of type RooAbsReal, ignored" << endl ;
continue ;
}
if (!dynamic_cast<RooAbsReal*>(func)) {
coutW(InputArguments) << "RooRealSumPdf::RooRealSumPdf(" << GetName() << ") func " << func->GetName() << " is not of type RooAbsReal, ignored" << endl ;
continue ;
}
_funcList.add(*func) ;
_coefList.add(*coef) ;
}
func = (RooAbsReal*) funcIter->Next() ;
if (func) {
if (!dynamic_cast<RooAbsReal*>(func)) {
coutE(InputArguments) << "RooRealSumPdf::RooRealSumPdf(" << GetName() << ") last func " << coef->GetName() << " is not of type RooAbsReal, fatal error" << endl ;
assert(0) ;
}
_funcList.add(*func) ;
} else {
_haveLastCoef = kTRUE ;
}
delete funcIter ;
delete coefIter ;
}
RooRealSumPdf::RooRealSumPdf(const RooRealSumPdf& other, const char* name) :
RooAbsPdf(other,name),
_normIntMgr(other._normIntMgr,this),
_haveLastCoef(other._haveLastCoef),
_funcList("!funcList",this,other._funcList),
_coefList("!coefList",this,other._coefList),
_extended(other._extended)
{
_funcIter = _funcList.createIterator() ;
_coefIter = _coefList.createIterator() ;
}
RooRealSumPdf::~RooRealSumPdf()
{
delete _funcIter ;
delete _coefIter ;
}
RooAbsPdf::ExtendMode RooRealSumPdf::extendMode() const
{
return (_extended && (_funcList.getSize()==_coefList.getSize())) ? CanBeExtended : CanNotBeExtended ;
}
Double_t RooRealSumPdf::evaluate() const
{
Double_t value(0) ;
_funcIter->Reset() ;
_coefIter->Reset() ;
RooAbsReal* coef ;
RooAbsReal* func ;
Double_t lastCoef(1) ;
while((coef=(RooAbsReal*)_coefIter->Next())) {
func = (RooAbsReal*)_funcIter->Next() ;
Double_t coefVal = coef->getVal() ;
if (coefVal) {
cxcoutD(Eval) << "RooRealSumPdf::eval(" << GetName() << ") coefVal = " << coefVal << " funcVal = " << func->getVal() << endl ;
if (func->isSelectedComp()) {
value += func->getVal()*coefVal ;
}
lastCoef -= coef->getVal() ;
}
}
if (!_haveLastCoef) {
func = (RooAbsReal*) _funcIter->Next() ;
if (func->isSelectedComp()) {
value += func->getVal()*lastCoef ;
}
cxcoutD(Eval) << "RooRealSumPdf::eval(" << GetName() << ") lastCoef = " << lastCoef << " funcVal = " << func->getVal() << endl ;
if (lastCoef<0 || lastCoef>1) {
coutW(Eval) << "RooRealSumPdf::evaluate(" << GetName()
<< " WARNING: sum of FUNC coefficients not in range [0-1], value="
<< 1-lastCoef << endl ;
}
}
return value ;
}
Bool_t RooRealSumPdf::checkObservables(const RooArgSet* nset) const
{
Bool_t ret(kFALSE) ;
_funcIter->Reset() ;
_coefIter->Reset() ;
RooAbsReal* coef ;
RooAbsReal* func ;
while((coef=(RooAbsReal*)_coefIter->Next())) {
func = (RooAbsReal*)_funcIter->Next() ;
if (func->observableOverlaps(nset,*coef)) {
coutE(InputArguments) << "RooRealSumPdf::checkObservables(" << GetName() << "): ERROR: coefficient " << coef->GetName()
<< " and FUNC " << func->GetName() << " have one or more observables in common" << endl ;
ret = kTRUE ;
}
if (coef->dependsOn(*nset)) {
coutE(InputArguments) << "RooRealPdf::checkObservables(" << GetName() << "): ERROR coefficient " << coef->GetName()
<< " depends on one or more of the following observables" ; nset->Print("1") ;
ret = kTRUE ;
}
}
return ret ;
}
Int_t RooRealSumPdf::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars,
const RooArgSet* normSet2, const char* ) const
{
if (allVars.getSize()==0) return 0 ;
if (_forceNumInt) return 0 ;
analVars.add(allVars) ;
RooArgSet* normSet = normSet2 ? getObservables(normSet2) : 0 ;
Int_t sterileIdx(-1) ;
CacheElem* cache = (CacheElem*) _normIntMgr.getObj(normSet,&analVars,&sterileIdx,0) ;
if (cache) {
return _normIntMgr.lastIndex()+1 ;
}
cache = new CacheElem ;
_funcIter->Reset() ;
RooAbsReal *func ;
while((func=(RooAbsReal*)_funcIter->Next())) {
RooAbsReal* funcInt = func->createIntegral(analVars) ;
cache->_funcIntList.addOwned(*funcInt) ;
if (normSet && normSet->getSize()>0) {
RooAbsReal* funcNorm = func->createIntegral(*normSet) ;
cache->_funcNormList.addOwned(*funcNorm) ;
}
}
Int_t code = _normIntMgr.setObj(normSet,&analVars,(RooAbsCacheElement*)cache,0) ;
if (normSet) {
delete normSet ;
}
return code+1 ;
}
Double_t RooRealSumPdf::analyticalIntegralWN(Int_t code, const RooArgSet* normSet2, const char* ) const
{
if (code==0) return getVal(normSet2) ;
CacheElem* cache = (CacheElem*) _normIntMgr.getObjByIndex(code-1) ;
TIterator* funcIntIter = cache->_funcIntList.createIterator() ;
_coefIter->Reset() ;
_funcIter->Reset() ;
RooAbsReal *coef(0), *funcInt(0), *func(0) ;
Double_t value(0) ;
Double_t lastCoef(1) ;
while((coef=(RooAbsReal*)_coefIter->Next())) {
funcInt = (RooAbsReal*)funcIntIter->Next() ;
func = (RooAbsReal*)_funcIter->Next() ;
Double_t coefVal = coef->getVal(normSet2) ;
if (coefVal) {
if (func->isSelectedComp()) {
value += funcInt->getVal()*coefVal ;
}
lastCoef -= coef->getVal(normSet2) ;
}
}
if (!_haveLastCoef) {
funcInt = (RooAbsReal*) funcIntIter->Next() ;
if (func->isSelectedComp()) {
value += funcInt->getVal()*lastCoef ;
}
if (lastCoef<0 || lastCoef>1) {
coutW(Eval) << "RooRealSumPdf::evaluate(" << GetName()
<< " WARNING: sum of FUNC coefficients not in range [0-1], value="
<< 1-lastCoef << endl ;
}
}
delete funcIntIter ;
Double_t normVal(1) ;
if (normSet2) {
normVal = 0 ;
RooAbsReal* funcNorm ;
TIterator* funcNormIter = cache->_funcNormList.createIterator() ;
_coefIter->Reset() ;
while((coef=(RooAbsReal*)_coefIter->Next())) {
funcNorm = (RooAbsReal*)funcNormIter->Next() ;
Double_t coefVal = coef->getVal(normSet2) ;
if (coefVal) {
normVal += funcNorm->getVal()*coefVal ;
}
}
if (!_haveLastCoef) {
funcNorm = (RooAbsReal*) funcNormIter->Next() ;
normVal += funcNorm->getVal()*lastCoef ;
}
delete funcNormIter ;
}
return value / normVal;
}
Double_t RooRealSumPdf::expectedEvents(const RooArgSet* nset) const
{
_coefIter->Reset() ;
Double_t coefSum(0) ;
RooAbsReal* coef ;
while((coef=(RooAbsReal*)_coefIter->Next())) {
coefSum += coef->getVal(nset) ;
}
return coefSum ;
}
void RooRealSumPdf::printMetaArgs(ostream& os) const
{
_funcIter->Reset() ;
_coefIter->Reset() ;
Bool_t first(kTRUE) ;
RooAbsArg* coef, *func ;
if (_coefList.getSize()!=0) {
while((coef=(RooAbsArg*)_coefIter->Next())) {
if (!first) {
os << " + " ;
} else {
first = kFALSE ;
}
func=(RooAbsArg*)_funcIter->Next() ;
os << coef->GetName() << " * " << func->GetName() ;
}
func = (RooAbsArg*) _funcIter->Next() ;
if (func) {
os << " + [%] * " << func->GetName() ;
}
} else {
while((func=(RooAbsArg*)_funcIter->Next())) {
if (!first) {
os << " + " ;
} else {
first = kFALSE ;
}
os << func->GetName() ;
}
}
os << " " ;
}