#include "RooFit.h"
#include "TClass.h"
#include "Riostream.h"
#include "TMath.h"
#include "TObjString.h"
#include "TPaveText.h"
#include "TList.h"
#include "TH1.h"
#include "TH2.h"
#include "RooAbsPdf.h"
#include "RooDataSet.h"
#include "RooArgSet.h"
#include "RooArgProxy.h"
#include "RooRealProxy.h"
#include "RooRealVar.h"
#include "RooGenContext.h"
#include "RooPlot.h"
#include "RooCurve.h"
#include "RooNLLVar.h"
#include "RooMinuit.h"
#include "RooCategory.h"
#include "RooNameReg.h"
#include "RooCmdConfig.h"
#include "RooGlobalFunc.h"
#include "RooAddition.h"
#include "RooRandom.h"
#include "RooInt.h"
ClassImp(RooAbsPdf)
;
Int_t RooAbsPdf::_verboseEval = 0;
Bool_t RooAbsPdf::_globalSelectComp = kFALSE ;
Bool_t RooAbsPdf::_evalError = kFALSE ;
RooAbsPdf::RooAbsPdf(const char *name, const char *title) :
RooAbsReal(name,title), _norm(0), _normSet(0), _normMgr(10), _selectComp(kTRUE)
{
resetErrorCounters() ;
setTraceCounter(0) ;
}
RooAbsPdf::RooAbsPdf(const char *name, const char *title,
Double_t plotMin, Double_t plotMax) :
RooAbsReal(name,title,plotMin,plotMax), _norm(0), _normSet(0), _normMgr(10), _selectComp(kTRUE)
{
resetErrorCounters() ;
setTraceCounter(0) ;
}
RooAbsPdf::RooAbsPdf(const RooAbsPdf& other, const char* name) :
RooAbsReal(other,name), _norm(0), _normSet(0), _normMgr(10), _selectComp(other._selectComp)
{
resetErrorCounters() ;
setTraceCounter(other._traceCount) ;
}
RooAbsPdf::~RooAbsPdf()
{
}
Double_t RooAbsPdf::getVal(const RooArgSet* nset) const
{
if (!nset) {
Double_t val = evaluate() ;
Bool_t error = traceEvalPdf(val) ;
if (_verboseEval>1) cout << IsA()->GetName() << "::getVal(" << GetName()
<< "): value = " << val << " (unnormalized)" << endl ;
if (error) {
raiseEvalError() ;
return 0 ;
}
return val ;
}
Bool_t nsetChanged(kFALSE) ;
if (nset!=_normSet) {
nsetChanged = syncNormalization(nset) ;
}
if ((isValueDirty() || nsetChanged || _norm->isValueDirty()) && operMode()!=AClean) {
Double_t rawVal = evaluate() ;
Bool_t error = traceEvalPdf(rawVal) ;
Double_t normVal(_norm->getVal()) ;
Double_t normError(kFALSE) ;
if (normVal==0.) normError=kTRUE ;
if (normError||error) raiseEvalError() ;
_value = normError ? 0 : (rawVal / normVal) ;
if (_verboseEval>1) cout << IsA()->GetName() << "::getVal(" << GetName() << "): value = "
<< rawVal << " / " << _norm->getVal() << " = " << _value << endl ;
clearValueDirty() ;
clearShapeDirty() ;
}
if (_traceCount>0) {
cout << "[" << _traceCount << "] " ;
Int_t tmp = _traceCount ;
_traceCount = 0 ;
Print() ;
_traceCount = tmp-1 ;
}
return _value ;
}
Double_t RooAbsPdf::analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName) const
{
if (_verboseEval>1) {
cout << "RooAbsPdf::analyticalIntegralWN(" << GetName() << ") code = " << code << " normset = " ;
if (normSet) normSet->Print("1") ; else cout << "<none>" << endl ;
}
if (code==0) return getVal(normSet) ;
if (normSet) {
return analyticalIntegral(code,rangeName) / getNorm(normSet) ;
} else {
return analyticalIntegral(code,rangeName) ;
}
}
Bool_t RooAbsPdf::traceEvalPdf(Double_t value) const
{
Bool_t error= isnan(value) || (value < 0);
if(!error) return error ;
if(++_errorCount <= 10) {
cout << "*** Evaluation Error " << _errorCount << " ";
if(_errorCount == 10) cout << "(no more will be printed) ";
}
else {
return error ;
}
Print() ;
return error ;
}
Double_t RooAbsPdf::getNorm(const RooArgSet* nset) const
{
if (!nset) return 1 ;
syncNormalization(nset,kTRUE) ;
if (_verboseEval>1) cout << IsA()->GetName() << "::getNorm(" << GetName() << "): norm(" << _norm << ") = " << _norm->getVal() << endl ;
Double_t ret = _norm->getVal() ;
if (ret==0.) {
if(++_errorCount <= 10) {
cout << "RooAbsPdf::getNorm(" << GetName() << ":: WARNING normalization is zero, nset = " ; nset->Print("1") ;
if(_errorCount == 10) cout << "RooAbsPdf::getNorm(" << GetName() << ") INFO: no more messages will be printed " << endl ;
}
}
return ret ;
}
const RooAbsReal* RooAbsPdf::getNormObj(const RooArgSet* nset, const RooArgSet* iset, const TNamed* rangeName) const
{
RooAbsReal* norm = _normMgr.getNormalization(this,nset,iset,rangeName) ;
if (norm) {
return norm ;
}
RooArgSet* depList = getObservables(iset) ;
norm = createIntegral(*depList,*nset, *getIntegratorConfig(), RooNameReg::str(rangeName)) ;
delete depList ;
_normMgr.setNormalization(this,nset,iset,rangeName,norm) ;
return norm ;
}
Bool_t RooAbsPdf::syncNormalizationPreHook(RooAbsReal*,const RooArgSet*) const
{
return kFALSE ;
}
void RooAbsPdf::syncNormalizationPostHook(RooAbsReal*,const RooArgSet*) const
{
}
Bool_t RooAbsPdf::syncNormalization(const RooArgSet* nset, Bool_t adjustProxies) const
{
_normSet = (RooArgSet*) nset ;
RooAbsReal* norm = _normMgr.getNormalization(this,nset,0) ;
if (norm) {
Bool_t nsetChanged = (_norm!=norm) ;
_norm = norm ;
if (nsetChanged && adjustProxies) {
((RooAbsPdf*) this)->setProxyNormSet(nset) ;
}
return nsetChanged ;
}
if (adjustProxies) {
((RooAbsPdf*) this)->setProxyNormSet(nset) ;
}
Bool_t fullNorm = syncNormalizationPreHook(_norm,nset) ;
RooArgSet* depList ;
if (fullNorm) {
depList = ((RooArgSet*)nset) ;
} else {
depList = getObservables(nset) ;
}
if (_verboseEval>0) {
if (!selfNormalized()) {
cout << IsA()->GetName() << "::syncNormalization(" << GetName()
<< ") recreating normalization integral " << endl ;
if (depList) depList->printToStream(cout,OneLine) ; else cout << "<none>" << endl ;
} else {
cout << IsA()->GetName() << "::syncNormalization(" << GetName() << ") selfNormalized, creating unit norm" << endl;
}
}
if (selfNormalized() || !dependsOn(*depList)) {
TString ntitle(GetTitle()) ; ntitle.Append(" Unit Normalization") ;
TString nname(GetName()) ; nname.Append("_UnitNorm") ;
_norm = new RooRealVar(nname.Data(),ntitle.Data(),1) ;
} else {
_norm = createIntegral(*depList,*getIntegratorConfig()) ;
}
_normMgr.setNormalization(this,nset,0,0,_norm) ;
syncNormalizationPostHook(_norm,nset) ;
if (!fullNorm) delete depList ;
return kTRUE ;
}
Bool_t RooAbsPdf::traceEvalHook(Double_t value) const
{
Bool_t error= isnan(value) || (value < 0);
if(!error && _traceCount <= 0) return error ;
if(error && ++_errorCount <= 10) {
cout << "*** Evaluation Error " << _errorCount << " ";
if(_errorCount == 10) cout << "(no more will be printed) ";
}
else if(_traceCount > 0) {
cout << '[' << _traceCount-- << "] ";
}
else {
return error ;
}
Print() ;
return error ;
}
void RooAbsPdf::resetErrorCounters(Int_t resetValue)
{
_errorCount = resetValue ;
_negCount = resetValue ;
}
void RooAbsPdf::setTraceCounter(Int_t value, Bool_t allNodes)
{
if (!allNodes) {
_traceCount = value ;
return ;
} else {
RooArgList branchList ;
branchNodeServerList(&branchList) ;
TIterator* iter = branchList.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
RooAbsPdf* pdf = dynamic_cast<RooAbsPdf*>(arg) ;
if (pdf) pdf->setTraceCounter(value,kFALSE) ;
}
delete iter ;
}
}
void RooAbsPdf::operModeHook()
{
}
Double_t RooAbsPdf::getLogVal(const RooArgSet* nset) const
{
Double_t prob = getVal(nset) ;
if(prob <= 0) {
if (_negCount-- > 0) {
cout << endl
<< "RooAbsPdf::getLogVal(" << GetName() << ") WARNING: PDF evaluates to zero or negative value (" << prob << ")" << endl;
RooArgSet* params = getParameters(nset) ;
RooArgSet* depends = getObservables(nset) ;
cout << " Current values of PDF dependents:" ;
depends->Print("v") ;
cout << " Current values of PDF parameters:" ;
params->Print("v") ;
delete params ;
delete depends ;
if(_negCount == 0) cout << "(no more such warnings will be printed) "<<endl;
}
return 0;
}
return log(prob);
}
Double_t RooAbsPdf::extendedTerm(UInt_t observed, const RooArgSet* nset) const
{
if(!canBeExtended()) {
cout << fName << ": this PDF does not support extended maximum likelihood"
<< endl;
return 0;
}
Double_t expected= expectedEvents(nset);
if(expected < 0) {
cout << fName << ": calculated negative expected events: " << expected
<< endl;
return 0;
}
Double_t extra= expected - observed*log(expected);
Bool_t trace(kFALSE) ;
if(trace) {
cout << fName << "::extendedTerm: expected " << expected << " events, got "
<< observed << " events. extendedTerm = " << extra << endl;
}
return extra;
}
RooFitResult* RooAbsPdf::fitTo(RooAbsData& data, RooCmdArg arg1, RooCmdArg arg2, RooCmdArg arg3, RooCmdArg arg4,
RooCmdArg arg5, RooCmdArg arg6, RooCmdArg arg7, RooCmdArg arg8)
{
RooLinkedList l ;
l.Add((TObject*)&arg1) ; l.Add((TObject*)&arg2) ;
l.Add((TObject*)&arg3) ; l.Add((TObject*)&arg4) ;
l.Add((TObject*)&arg5) ; l.Add((TObject*)&arg6) ;
l.Add((TObject*)&arg7) ; l.Add((TObject*)&arg8) ;
return fitTo(data,l) ;
}
RooFitResult* RooAbsPdf::fitTo(RooAbsData& data, const RooLinkedList& cmdList)
{
RooCmdConfig pc(Form("RooAbsPdf::fitTo(%s)",GetName())) ;
pc.defineString("fitOpt","FitOptions",0,"") ;
pc.defineString("rangeName","RangeWithName",0,"",kTRUE) ;
pc.defineDouble("rangeLo","Range",0,-999.) ;
pc.defineDouble("rangeHi","Range",1,-999.) ;
pc.defineInt("splitRange","SplitRange",0,0) ;
pc.defineInt("optConst","Optimize",0,1) ;
pc.defineInt("verbose","Verbose",0,0) ;
pc.defineInt("doSave","Save",0,0) ;
pc.defineInt("doTimer","Timer",0,0) ;
pc.defineInt("plevel","PrintLevel",0,1) ;
pc.defineInt("strat","Strategy",0,1) ;
pc.defineInt("initHesse","InitialHesse",0,0) ;
pc.defineInt("hesse","Hesse",0,1) ;
pc.defineInt("minos","Minos",0,1) ;
pc.defineInt("ext","Extended",0,0) ;
pc.defineInt("numcpu","NumCPU",0,1) ;
pc.defineObject("projDepSet","ProjectedObservables",0,0) ;
pc.defineObject("minosSet","Minos",0,0) ;
pc.defineMutex("FitOptions","Verbose") ;
pc.defineMutex("FitOptions","Save") ;
pc.defineMutex("FitOptions","Timer") ;
pc.defineMutex("FitOptions","Strategy") ;
pc.defineMutex("FitOptions","InitialHesse") ;
pc.defineMutex("FitOptions","Hesse") ;
pc.defineMutex("FitOptions","Minos") ;
pc.defineMutex("Range","RangeWithName") ;
pc.process(cmdList) ;
if (!pc.ok(kTRUE)) {
return 0 ;
}
const char* fitOpt = pc.getString("fitOpt",0,kTRUE) ;
const char* rangeName = pc.getString("rangeName",0,kTRUE) ;
Int_t optConst = pc.getInt("optConst") ;
Int_t verbose = pc.getInt("verbose") ;
Int_t doSave = pc.getInt("doSave") ;
Int_t doTimer = pc.getInt("doTimer") ;
Int_t plevel = pc.getInt("plevel") ;
Int_t strat = pc.getInt("strat") ;
Int_t initHesse= pc.getInt("initHesse") ;
Int_t hesse = pc.getInt("hesse") ;
Int_t minos = pc.getInt("minos") ;
Int_t ext = pc.getInt("ext") ;
Int_t numcpu = pc.getInt("numcpu") ;
Int_t splitr = pc.getInt("splitRange") ;
const RooArgSet* minosSet = static_cast<RooArgSet*>(pc.getObject("minosSet")) ;
if (pc.hasProcessed("Range")) {
Double_t rangeLo = pc.getDouble("rangeLo") ;
Double_t rangeHi = pc.getDouble("rangeHi") ;
RooArgSet* obs = getObservables(&data) ;
TIterator* iter = obs->createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
RooRealVar* rrv = dynamic_cast<RooRealVar*>(arg) ;
if (rrv) rrv->setRange("fit",rangeLo,rangeHi) ;
}
rangeName = "fit" ;
}
RooArgSet projDeps ;
RooArgSet* tmp = (RooArgSet*) pc.getObject("projDepSet") ;
if (tmp) projDeps.add(*tmp) ;
RooAbsReal* nll ;
if (!rangeName || strchr(rangeName,',')==0) {
nll = new RooNLLVar("nll","-log(likelihood)",*this,data,projDeps,ext,rangeName,numcpu,plevel!=-1,splitr) ;
} else {
RooArgList nllList ;
char* buf = new char[strlen(rangeName)+1] ;
strcpy(buf,rangeName) ;
char* token = strtok(buf,",") ;
while(token) {
RooAbsReal* nllComp = new RooNLLVar(Form("nll_%s",token),"-log(likelihood)",*this,data,projDeps,ext,token,numcpu,plevel!=-1,splitr) ;
nllList.add(*nllComp) ;
token = strtok(0,",") ;
}
delete[] buf ;
nll = new RooAddition("nll","-log(likelihood)",nllList,kTRUE) ;
}
RooMinuit m(*nll) ;
if (plevel!=1) {
m.setPrintLevel(plevel) ;
}
if (optConst) {
m.optimizeConst(1) ;
}
RooFitResult *ret = 0 ;
if (fitOpt) {
ret = m.fit(fitOpt) ;
} else {
if (verbose) {
m.setVerbose(1) ;
}
if (doTimer) {
m.setProfile(1) ;
}
if (strat!=1) {
m.setStrategy(strat) ;
}
if (initHesse) {
m.hesse() ;
}
m.migrad() ;
if (hesse) {
m.hesse() ;
}
if (minos) {
if (minosSet) {
m.minos(*minosSet) ;
} else {
m.minos() ;
}
}
if (doSave) {
ret = m.save() ;
}
}
delete nll ;
return ret ;
}
RooFitResult* RooAbsPdf::fitTo(RooAbsData& data, Option_t *fitOpt, Option_t *optOpt, const char* fitRange)
{
return fitTo(data,RooArgSet(),fitOpt,optOpt,fitRange) ;
}
RooFitResult* RooAbsPdf::fitTo(RooAbsData& data, const RooArgSet& projDeps, Option_t *fitOpt, Option_t *optOpt, const char* fitRange)
{
TString fopt(fitOpt) ;
TString oopt(optOpt) ;
fopt.ToLower() ;
oopt.ToLower() ;
Bool_t extended = fopt.Contains("e") ;
Bool_t saveRes = fopt.Contains("r") ;
Bool_t cOpt = oopt.Contains("p") ||
oopt.Contains("c") ;
Bool_t blindfit = fopt.Contains("b") ;
Int_t ncpu = 1 ;
if (oopt.Contains("2")) ncpu=2 ;
if (oopt.Contains("3")) ncpu=3 ;
if (oopt.Contains("4")) ncpu=4 ;
if (oopt.Contains("5")) ncpu=5 ;
if (oopt.Contains("6")) ncpu=6 ;
if (oopt.Contains("7")) ncpu=7 ;
if (oopt.Contains("8")) ncpu=8 ;
if (oopt.Contains("9")) ncpu=9 ;
RooNLLVar nll("nll","-log(likelihood)",*this,data,projDeps,extended,fitRange,ncpu) ;
RooMinuit m(nll) ;
if(blindfit)
m.setPrintLevel(-1);
if (cOpt) m.optimizeConst(1) ;
m.fit(fopt) ;
if (saveRes) {
return m.save() ;
} else {
return 0 ;
}
}
void RooAbsPdf::printToStream(ostream& os, PrintOption opt, TString indent) const
{
if (opt == OneLine) {
RooAbsArg::printToStream(os,opt,indent);
}
if (opt == Standard) {
os << ClassName() << "::" << GetName() << "(" ;
RooArgSet* paramList = getParameters((RooArgSet*)0) ;
TIterator* pIter = paramList->createIterator() ;
Bool_t first=kTRUE ;
RooAbsArg* var ;
while((var=(RooAbsArg*)pIter->Next())) {
if (!first) {
os << "," ;
} else {
first=kFALSE ;
}
os << var->GetName() ;
}
os << ") = " << getVal(0) << endl ;
delete pIter ;
delete paramList ;
}
if(opt >= Verbose) {
RooAbsArg::printToStream(os,opt,indent);
os << indent << "--- RooAbsPdf ---" << endl;
os << indent << "Cached value = " << _value << endl ;
if (_norm) {
os << " Normalization integral: " << endl ;
TString moreIndent(indent) ; moreIndent.Append(" ") ;
_norm->printToStream(os,Verbose,moreIndent.Data()) ;
_norm->printToStream(os,Standard,moreIndent.Data()) ;
}
}
}
RooAbsGenContext* RooAbsPdf::genContext(const RooArgSet &vars, const RooDataSet *prototype,
const RooArgSet* auxProto, Bool_t verbose) const
{
return new RooGenContext(*this,vars,prototype,auxProto,verbose) ;
}
RooDataSet *RooAbsPdf::generate(const RooArgSet& whatVars, Int_t nEvents, const RooCmdArg& arg1,
const RooCmdArg& arg2, const RooCmdArg& arg3,const RooCmdArg& arg4, const RooCmdArg& arg5)
{
return generate(whatVars,RooFit::NumEvents(nEvents),arg1,arg2,arg3,arg4,arg5) ;
}
RooDataSet *RooAbsPdf::generate(const RooArgSet& whatVars, const RooCmdArg& arg1,const RooCmdArg& arg2,
const RooCmdArg& arg3,const RooCmdArg& arg4, const RooCmdArg& arg5,const RooCmdArg& arg6)
{
RooCmdConfig pc(Form("RooAbsPdf::generate(%s)",GetName())) ;
pc.defineObject("proto","PrototypeData",0,0) ;
pc.defineInt("randProto","PrototypeData",0,0) ;
pc.defineInt("resampleProto","PrototypeData",1,0) ;
pc.defineInt("verbose","Verbose",0,0) ;
pc.defineInt("extended","Extended",0,0) ;
pc.defineInt("nEvents","NumEvents",0,0) ;
pc.process(arg1,arg2,arg3,arg4,arg5,arg6) ;
if (!pc.ok(kTRUE)) {
return 0 ;
}
RooDataSet* protoData = static_cast<RooDataSet*>(pc.getObject("proto",0)) ;
Int_t nEvents = pc.getInt("nEvents") ;
Bool_t verbose = pc.getInt("verbose") ;
Bool_t randProto = pc.getInt("randProto") ;
Bool_t resampleProto = pc.getInt("resampleProto") ;
Bool_t extended = pc.getInt("extended") ;
if (extended) {
nEvents = RooRandom::randomGenerator()->Poisson(nEvents==0?expectedEvents(&whatVars):nEvents) ;
}
if (extended && protoData && !randProto) {
cout << "RooAbsPdf::generate: WARNING Using generator option Extended() (Poisson distribution of #events) together " << endl
<< " with a prototype dataset implies incomplete sampling or oversampling of proto data." << endl
<< " Set randomize flag in ProtoData() option to randomize prototype dataset order and thus" << endl
<< " to randomize the set of over/undersampled prototype events for each generation cycle." << endl ;
}
if (protoData) {
return generate(whatVars,*protoData,nEvents,verbose,randProto,resampleProto) ;
} else {
return generate(whatVars,nEvents,verbose) ;
}
}
RooDataSet *RooAbsPdf::generate(const RooArgSet &whatVars, Int_t nEvents, Bool_t verbose) const {
RooDataSet *generated = 0;
RooAbsGenContext *context= genContext(whatVars,0,0,verbose);
if(0 != context && context->isValid()) {
generated= context->generate(nEvents);
}
else {
cout << ClassName() << "::" << GetName() << ":generate: cannot create a valid context" << endl;
}
if(0 != context) delete context;
return generated;
}
RooDataSet *RooAbsPdf::generate(const RooArgSet &whatVars, const RooDataSet &prototype,
Int_t nEvents, Bool_t verbose, Bool_t randProtoOrder, Bool_t resampleProto) const {
RooDataSet *generated = 0;
RooAbsGenContext *context= genContext(whatVars,&prototype,0,verbose);
if (resampleProto) {
randProtoOrder=kTRUE ;
}
if (randProtoOrder && prototype.numEntries()!=nEvents) {
cout << "RooAbsPdf::generate (Re)randomizing event order in prototype dataset (Nevt=" << nEvents << ")" << endl ;
Int_t* newOrder = randomizeProtoOrder(prototype.numEntries(),nEvents,resampleProto) ;
context->setProtoDataOrder(newOrder) ;
delete[] newOrder ;
}
if(0 != context && context->isValid()) {
generated= context->generate(nEvents);
}
else {
cout << ClassName() << "::" << GetName() << ":generate: cannot create a valid context" << endl;
}
if(0 != context) delete context;
return generated;
}
Int_t* RooAbsPdf::randomizeProtoOrder(Int_t nProto, Int_t, Bool_t resampleProto) const
{
RooLinkedList l ;
Int_t i ;
for (i=0 ; i<nProto ; i++) {
l.Add(new RooInt(i)) ;
}
Int_t* lut = new Int_t[nProto] ;
if (!resampleProto) {
for (i=0 ; i<nProto ; i++) {
Int_t iran = RooRandom::integer(nProto-i) ;
RooInt* sample = (RooInt*) l.At(iran) ;
lut[i] = *sample ;
l.Remove(sample) ;
delete sample ;
}
} else {
for (i=0 ; i<nProto ; i++) {
lut[i] = RooRandom::integer(nProto);
}
}
return lut ;
}
Int_t RooAbsPdf::getGenerator(const RooArgSet &, RooArgSet &, Bool_t ) const {
return 0 ;
}
void RooAbsPdf::initGenerator(Int_t )
{
}
void RooAbsPdf::generateEvent(Int_t ) {
}
Bool_t RooAbsPdf::isDirectGenSafe(const RooAbsArg& arg) const
{
if (!findServer(arg.GetName())) return kFALSE ;
TIterator* sIter = serverIterator() ;
const RooAbsArg *server = 0;
while((server=(const RooAbsArg*)sIter->Next())) {
if(server == &arg) continue;
if(server->dependsOn(arg)) {
delete sIter ;
return kFALSE ;
}
}
delete sIter ;
return kTRUE ;
}
RooPlot* RooAbsPdf::plotOn(RooPlot* frame, RooLinkedList& cmdList) const
{
if (plotSanityChecks(frame)) return frame ;
RooCmdConfig pc(Form("RooAbsPdf::plotOn(%s)",GetName())) ;
pc.defineDouble("scaleFactor","Normalization",0,1.0) ;
pc.defineInt("scaleType","Normalization",0,RooAbsPdf::Relative) ;
pc.defineObject("compSet","SelectCompSet",0) ;
pc.defineString("compSpec","SelectCompSpec",0) ;
pc.defineObject("asymCat","Asymmetry",0) ;
pc.defineDouble("rangeLo","Range",0,-999.) ;
pc.defineDouble("rangeHi","Range",1,-999.) ;
pc.defineString("rangeName","RangeWithName",0,"") ;
pc.defineInt("rangeAdjustNorm","Range",0,0) ;
pc.defineInt("rangeWNAdjustNorm","RangeWithName",0,0) ;
pc.defineMutex("SelectCompSet","SelectCompSpec") ;
pc.defineMutex("Range","RangeWithName") ;
pc.allowUndefined() ;
pc.process(cmdList) ;
if (!pc.ok(kTRUE)) {
return frame ;
}
ScaleType stype = (ScaleType) pc.getInt("scaleType") ;
Double_t scaleFactor = pc.getDouble("scaleFactor") ;
const RooAbsCategoryLValue* asymCat = (const RooAbsCategoryLValue*) pc.getObject("asymCat") ;
const char* compSpec = pc.getString("compSpec") ;
const RooArgSet* compSet = (const RooArgSet*) pc.getObject("compSet") ;
Bool_t haveCompSel = (strlen(compSpec)>0 || compSet) ;
pc.stripCmdList(cmdList,"SelectCompSet,SelectCompSpec") ;
if (asymCat) {
return RooAbsReal::plotOn(frame,cmdList) ;
}
Double_t nExpected(1) ;
if (stype==RelativeExpected) {
if (!canBeExtended()) {
cout << "RooAbsPdf::plotOn(" << GetName()
<< "): ERROR the 'Expected' scale option can only be used on extendable PDFs" << endl ;
return frame ;
}
nExpected = expectedEvents(frame->getNormVars()) ;
}
if (stype != Raw) {
if (frame->getFitRangeNEvt() && stype==Relative) {
Bool_t hasCustomRange(kFALSE), adjustNorm(kFALSE) ;
Double_t rangeLo(0), rangeHi(0) ;
if (pc.hasProcessed("Range")) {
rangeLo = pc.getDouble("rangeLo") ;
rangeHi = pc.getDouble("rangeHi") ;
adjustNorm = pc.getInt("rangeAdjustNorm") ;
hasCustomRange = kTRUE ;
} else if (pc.hasProcessed("RangeWithName")) {
rangeLo = frame->getPlotVar()->getMin(pc.getString("rangeName",0,kTRUE)) ;
rangeHi = frame->getPlotVar()->getMax(pc.getString("rangeName",0,kTRUE)) ;
adjustNorm = pc.getInt("rangeWNAdjustNorm") ;
hasCustomRange = kTRUE ;
} else {
RooArgSet* plotDep = getObservables(*frame->getPlotVar()) ;
RooRealVar* plotDepVar = (RooRealVar*) plotDep->find(frame->getPlotVar()->GetName()) ;
if (plotDepVar->hasBinning("fit")) {
rangeLo = plotDepVar->getMin("fit") ;
rangeHi = plotDepVar->getMax("fit") ;
adjustNorm = kTRUE ;
hasCustomRange = kTRUE ;
cout << "RooAbsPdf::plotOn(" << GetName() << ") INFO: pdf has been fit over restricted range, plotting only fitted "
<< "part of PDF normalized data in restricted range" << endl ;
}
delete plotDep ;
}
if (hasCustomRange && adjustNorm) {
scaleFactor *= frame->getFitRangeNEvt(rangeLo,rangeHi)/nExpected ;
} else {
scaleFactor *= frame->getFitRangeNEvt()/nExpected ;
}
} else if (stype==RelativeExpected) {
scaleFactor *= nExpected ;
} else if (stype==NumEvent) {
scaleFactor /= nExpected ;
}
scaleFactor *= frame->getFitRangeBinW() ;
}
frame->updateNormVars(*frame->getPlotVar()) ;
RooCmdArg tmp = RooFit::Normalization(scaleFactor,Raw) ;
cmdList.Add(&tmp) ;
if (haveCompSel) {
RooArgSet branchNodeSet ;
branchNodeServerList(&branchNodeSet) ;
TIterator* iter = branchNodeSet.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
if (!dynamic_cast<RooAbsPdf*>(arg)) {
branchNodeSet.remove(*arg) ;
}
}
delete iter ;
RooArgSet* dirSelNodes ;
if (compSet) {
dirSelNodes = (RooArgSet*) branchNodeSet.selectCommon(*compSet) ;
} else {
dirSelNodes = (RooArgSet*) branchNodeSet.selectByName(compSpec) ;
}
cout << "RooAbsPdf::plotOn(" << GetName() << ") directly selected PDF components: " ;
dirSelNodes->Print("1") ;
plotOnCompSelect(dirSelNodes) ;
}
RooPlot* ret = RooAbsReal::plotOn(frame,cmdList) ;
if (haveCompSel) plotOnCompSelect(0) ;
return ret ;
}
void RooAbsPdf::plotOnCompSelect(RooArgSet* selNodes) const
{
RooArgSet branchNodeSet ;
branchNodeServerList(&branchNodeSet) ;
TIterator* iter = branchNodeSet.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
if (!dynamic_cast<RooAbsPdf*>(arg)) {
branchNodeSet.remove(*arg) ;
}
}
if (!selNodes) {
iter->Reset() ;
while((arg=(RooAbsArg*)iter->Next())) {
((RooAbsPdf*)arg)->selectComp(kTRUE) ;
}
delete iter ;
return ;
}
iter->Reset() ;
TIterator* sIter = selNodes->createIterator() ;
RooArgSet tmp ;
while((arg=(RooAbsArg*)iter->Next())) {
sIter->Reset() ;
RooAbsArg* selNode ;
while((selNode=(RooAbsArg*)sIter->Next())) {
if (selNode->dependsOn(*arg)) {
tmp.add(*arg,kTRUE) ;
}
}
}
delete sIter ;
iter->Reset() ;
while((arg=(RooAbsArg*)iter->Next())) {
if (arg->dependsOn(*selNodes)) {
tmp.add(*arg,kTRUE) ;
}
}
tmp.remove(*selNodes,kTRUE) ;
tmp.remove(*this) ;
selNodes->add(tmp) ;
cout << "RooAbsPdf::plotOn(" << GetName() << ") indirectly selected PDF components: " ;
tmp.Print("1") ;
iter->Reset() ;
while((arg=(RooAbsArg*)iter->Next())) {
Bool_t select = selNodes->find(arg->GetName()) ? kTRUE : kFALSE ;
((RooAbsPdf*)arg)->selectComp(select) ;
}
delete iter ;
}
RooPlot* RooAbsPdf::plotOn(RooPlot *frame, PlotOpt o) const
{
if (plotSanityChecks(frame)) return frame ;
Double_t nExpected(1) ;
if (o.stype==RelativeExpected) {
if (!canBeExtended()) {
cout << "RooAbsPdf::plotOn(" << GetName()
<< "): ERROR the 'Expected' scale option can only be used on extendable PDFs" << endl ;
return frame ;
}
nExpected = expectedEvents(frame->getNormVars()) ;
}
if (o.stype != Raw) {
if (frame->getFitRangeNEvt() && o.stype==Relative) {
o.scaleFactor *= frame->getFitRangeNEvt()/nExpected ;
} else if (o.stype==RelativeExpected) {
o.scaleFactor *= nExpected ;
} else if (o.stype==NumEvent) {
o.scaleFactor /= nExpected ;
}
o.scaleFactor *= frame->getFitRangeBinW() ;
}
frame->updateNormVars(*frame->getPlotVar()) ;
return RooAbsReal::plotOn(frame,o) ;
}
RooPlot* RooAbsPdf::plotCompOn(RooPlot *frame, const RooArgSet& compSet, Option_t* drawOptions,
Double_t scaleFactor, ScaleType stype, const RooAbsData* projData,
const RooArgSet* projSet) const
{
if (plotSanityChecks(frame)) return frame ;
RooArgSet branchNodeSet ;
branchNodeServerList(&branchNodeSet) ;
TIterator* iter = branchNodeSet.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
if (!dynamic_cast<RooAbsPdf*>(arg)) {
branchNodeSet.remove(*arg) ;
}
}
delete iter ;
RooArgSet* selNodes = (RooArgSet*) branchNodeSet.selectCommon(compSet) ;
cout << "RooAbsPdf::plotCompOn(" << GetName() << ") directly selected PDF components: " ;
selNodes->Print("1") ;
return plotCompOnEngine(frame,selNodes,drawOptions,scaleFactor,stype,projData,projSet) ;
}
RooPlot* RooAbsPdf::plotCompOn(RooPlot *frame, const char* compNameList, Option_t* drawOptions,
Double_t scaleFactor, ScaleType stype, const RooAbsData* projData,
const RooArgSet* projSet) const
{
if (plotSanityChecks(frame)) return frame ;
RooArgSet branchNodeSet ;
branchNodeServerList(&branchNodeSet) ;
TIterator* iter = branchNodeSet.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
if (!dynamic_cast<RooAbsPdf*>(arg)) {
branchNodeSet.remove(*arg) ;
}
}
delete iter ;
RooArgSet* selNodes = (RooArgSet*) branchNodeSet.selectByName(compNameList) ;
cout << "RooAbsPdf::plotCompOn(" << GetName() << ") directly selected PDF components: " ;
selNodes->Print("1") ;
return plotCompOnEngine(frame,selNodes,drawOptions,scaleFactor,stype,projData,projSet) ;
}
RooPlot* RooAbsPdf::plotCompOnEngine(RooPlot *frame, RooArgSet* selNodes, Option_t* drawOptions,
Double_t scaleFactor, ScaleType stype, const RooAbsData* projData,
const RooArgSet* projSet) const
{
RooArgSet branchNodeSet ;
branchNodeServerList(&branchNodeSet) ;
TIterator* iter = branchNodeSet.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
if (!dynamic_cast<RooAbsPdf*>(arg)) {
branchNodeSet.remove(*arg) ;
}
}
iter->Reset() ;
TIterator* sIter = selNodes->createIterator() ;
RooArgSet tmp ;
while((arg=(RooAbsArg*)iter->Next())) {
sIter->Reset() ;
RooAbsArg* selNode ;
while((selNode=(RooAbsArg*)sIter->Next())) {
if (selNode->dependsOn(*arg)) {
tmp.add(*arg,kTRUE) ;
}
}
}
delete sIter ;
iter->Reset() ;
while((arg=(RooAbsArg*)iter->Next())) {
if (arg->dependsOn(*selNodes)) {
tmp.add(*arg,kTRUE) ;
}
}
tmp.remove(*selNodes,kTRUE) ;
tmp.remove(*this) ;
selNodes->add(tmp) ;
iter->Reset() ;
while((arg=(RooAbsArg*)iter->Next())) {
Bool_t select = selNodes->find(arg->GetName()) ? kTRUE : kFALSE ;
((RooAbsPdf*)arg)->selectComp(select) ;
}
PlotOpt o ;
o.drawOptions = drawOptions ;
o.scaleFactor = scaleFactor ;
o.stype = stype ;
o.projData = projData ;
o.projSet = projSet ;
frame = plotOn(frame,0) ;
iter->Reset() ;
while((arg=(RooAbsArg*)iter->Next())) {
((RooAbsPdf*)arg)->selectComp(kTRUE) ;
}
delete selNodes ;
delete iter ;
return frame ;
}
RooPlot* RooAbsPdf::plotCompSliceOn(RooPlot *frame, const char* compNameList, const RooArgSet& sliceSet,
Option_t* drawOptions, Double_t scaleFactor, ScaleType stype,
const RooAbsData* projData) const
{
RooArgSet projectedVars ;
makeProjectionSet(frame->getPlotVar(),frame->getNormVars(),projectedVars,kTRUE) ;
TIterator* iter = sliceSet.createIterator() ;
RooAbsArg* sliceArg ;
while((sliceArg=(RooAbsArg*)iter->Next())) {
RooAbsArg* arg = projectedVars.find(sliceArg->GetName()) ;
if (arg) {
projectedVars.remove(*arg) ;
} else {
cout << "RooAddPdf::plotCompSliceOn(" << GetName() << ") slice variable "
<< sliceArg->GetName() << " was not projected anyway" << endl ;
}
}
delete iter ;
return plotCompOn(frame,compNameList,drawOptions,scaleFactor,stype,projData,&projectedVars) ;
}
RooPlot* RooAbsPdf::plotCompSliceOn(RooPlot *frame, const RooArgSet& compSet, const RooArgSet& sliceSet,
Option_t* drawOptions, Double_t scaleFactor, ScaleType stype,
const RooAbsData* projData) const
{
RooArgSet projectedVars ;
makeProjectionSet(frame->getPlotVar(),frame->getNormVars(),projectedVars,kTRUE) ;
TIterator* iter = sliceSet.createIterator() ;
RooAbsArg* sliceArg ;
while((sliceArg=(RooAbsArg*)iter->Next())) {
RooAbsArg* arg = projectedVars.find(sliceArg->GetName()) ;
if (arg) {
projectedVars.remove(*arg) ;
} else {
cout << "RooAddPdf::plotCompSliceOn(" << GetName() << ") slice variable "
<< sliceArg->GetName() << " was not projected anyway" << endl ;
}
}
delete iter ;
return plotCompOn(frame,compSet,drawOptions,scaleFactor,stype,projData,&projectedVars) ;
}
RooPlot* RooAbsPdf::paramOn(RooPlot* frame, const RooCmdArg& arg1, const RooCmdArg& arg2,
const RooCmdArg& arg3, const RooCmdArg& arg4, const RooCmdArg& arg5,
const RooCmdArg& arg6, const RooCmdArg& arg7, const RooCmdArg& arg8)
{
RooLinkedList cmdList;
cmdList.Add(const_cast<RooCmdArg*>(&arg1)) ; cmdList.Add(const_cast<RooCmdArg*>(&arg2)) ;
cmdList.Add(const_cast<RooCmdArg*>(&arg3)) ; cmdList.Add(const_cast<RooCmdArg*>(&arg4)) ;
cmdList.Add(const_cast<RooCmdArg*>(&arg5)) ; cmdList.Add(const_cast<RooCmdArg*>(&arg6)) ;
cmdList.Add(const_cast<RooCmdArg*>(&arg7)) ; cmdList.Add(const_cast<RooCmdArg*>(&arg8)) ;
RooCmdConfig pc(Form("RooAbsPdf::paramOn(%s)",GetName())) ;
pc.defineString("label","Label",0,"") ;
pc.defineDouble("xmin","Layout",0,0.65) ;
pc.defineDouble("xmax","Layout",1,0.99) ;
pc.defineInt("ymaxi","Layout",0,Int_t(0.95*10000)) ;
pc.defineInt("showc","ShowConstants",0,0) ;
pc.defineObject("params","Parameters",0,0) ;
pc.defineString("formatStr","Format",0,"NELU") ;
pc.defineInt("sigDigit","Format",0,2) ;
pc.defineInt("dummy","FormatArgs",0,0) ;
pc.defineMutex("Format","FormatArgs") ;
pc.process(cmdList) ;
if (!pc.ok(kTRUE)) {
return frame ;
}
const char* label = pc.getString("label") ;
Double_t xmin = pc.getDouble("xmin") ;
Double_t xmax = pc.getDouble("xmax") ;
Double_t ymax = pc.getInt("ymaxi") / 10000. ;
Int_t showc = pc.getInt("showc") ;
const char* formatStr = pc.getString("formatStr") ;
Int_t sigDigit = pc.getInt("sigDigit") ;
RooArgSet* params = static_cast<RooArgSet*>(pc.getObject("params")) ;
if (!params) {
params = getParameters(frame->getNormVars()) ;
if (pc.hasProcessed("FormatArgs")) {
const RooCmdArg* formatCmd = static_cast<RooCmdArg*>(cmdList.FindObject("FormatArgs")) ;
paramOn(frame,*params,showc,label,0,0,xmin,xmax,ymax,formatCmd) ;
} else {
paramOn(frame,*params,showc,label,sigDigit,formatStr,xmin,xmax,ymax) ;
}
delete params ;
} else {
RooArgSet* pdfParams = getParameters(frame->getNormVars()) ;
RooArgSet* selParams = static_cast<RooArgSet*>(pdfParams->selectCommon(*params)) ;
if (pc.hasProcessed("FormatArgs")) {
const RooCmdArg* formatCmd = static_cast<RooCmdArg*>(cmdList.FindObject("FormatArgs")) ;
paramOn(frame,*selParams,showc,label,0,0,xmin,xmax,ymax,formatCmd) ;
} else {
paramOn(frame,*selParams,showc,label,sigDigit,formatStr,xmin,xmax,ymax) ;
}
delete selParams ;
delete pdfParams ;
}
return frame ;
}
RooPlot* RooAbsPdf::paramOn(RooPlot* frame, const RooAbsData* data, const char *label,
Int_t sigDigits, Option_t *options, Double_t xmin,
Double_t xmax ,Double_t ymax)
{
RooArgSet* params = getParameters(data) ;
TString opts(options) ;
paramOn(frame,*params,opts.Contains("c"),label,sigDigits,options,xmin,xmax,ymax) ;
delete params ;
return frame ;
}
RooPlot* RooAbsPdf::paramOn(RooPlot* frame, const RooArgSet& params, Bool_t showConstants, const char *label,
Int_t sigDigits, Option_t *options, Double_t xmin,
Double_t xmax ,Double_t ymax, const RooCmdArg* formatCmd)
{
TString opts = options;
opts.ToLower();
Bool_t showLabel= (label != 0 && strlen(label) > 0);
TIterator* pIter = params.createIterator() ;
Int_t nPar= params.getSize();
Double_t ymin(ymax), dy(0.06);
Int_t index(nPar);
RooRealVar *var = 0;
while((var=(RooRealVar*)pIter->Next())) {
if(showConstants || !var->isConstant()) ymin-= dy;
}
if(showLabel) ymin-= dy;
TPaveText *box= new TPaveText(xmin,ymax,xmax,ymin,"BRNDC");
if(!box) return 0;
box->SetFillColor(0);
box->SetBorderSize(1);
box->SetTextAlign(12);
box->SetTextSize(0.04F);
box->SetFillStyle(1001);
box->SetFillColor(0);
TText *text = 0;
index= nPar;
pIter->Reset() ;
while((var=(RooRealVar*)pIter->Next())) {
if(var->isConstant() && !showConstants) continue;
TString *formatted= options ? var->format(sigDigits, options) : var->format(*formatCmd) ;
text= box->AddText(formatted->Data());
delete formatted;
}
if(showLabel) text= box->AddText(label);
frame->addObject(box) ;
delete pIter ;
return frame ;
}
void RooAbsPdf::fixAddCoefNormalization(const RooArgSet& addNormSet)
{
RooArgSet* compSet = getComponents() ;
TIterator* iter = compSet->createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
RooAbsPdf* pdf = dynamic_cast<RooAbsPdf*>(arg) ;
if (pdf) {
if (addNormSet.getSize()>0) {
pdf->selectNormalization(&addNormSet,kTRUE) ;
} else {
pdf->selectNormalization(0,kTRUE) ;
}
}
}
delete iter ;
delete compSet ;
}
void RooAbsPdf::fixAddCoefRange(const char* rangeName)
{
RooArgSet* compSet = getComponents() ;
TIterator* iter = compSet->createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
RooAbsPdf* pdf = dynamic_cast<RooAbsPdf*>(arg) ;
if (pdf) {
pdf->selectNormalizationRange(rangeName,kTRUE) ;
}
}
delete iter ;
delete compSet ;
}
RooPlot* RooAbsPdf::plotNLLOn(RooPlot* frame, RooDataSet* data, Bool_t extended, const RooArgSet& ,
Option_t* , Double_t prec, Bool_t fixMinToZero) {
RooNLLVar nll("nll","-log(L)",*this,*data,extended) ;
if (fixMinToZero) {
nll.plotOn(frame,RooFit::DrawOption("L"),RooFit::Precision(prec),RooFit::ShiftToZero()) ;
} else {
nll.plotOn(frame,RooFit::DrawOption("L"),RooFit::Precision(prec)) ;
}
return frame ;
}
Bool_t RooAbsPdf::redirectServersHook(const RooAbsCollection& newServerList,
Bool_t mustReplaceAll, Bool_t nameChange, Bool_t )
{
Bool_t ret(kFALSE) ;
Int_t i ;
for (i=0 ; i<_normMgr.cacheSize() ; i++) {
RooAbsArg* norm = _normMgr.getNormByIndex(i) ;
ret |= norm->recursiveRedirectServers(newServerList,mustReplaceAll,nameChange) ;
}
return ret ;
}
Double_t RooAbsPdf::expectedEvents(const RooArgSet*) const
{
return 0 ;
}
void RooAbsPdf::verboseEval(Int_t stat)
{
_verboseEval = stat ;
}
Int_t RooAbsPdf::verboseEval()
{
return _verboseEval ;
}
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.