// Numeric 1-dimensional convolution operator PDF. This class can convolve any PDF
// with any other PDF
// <p>
// This class should not be used blindly as numeric convolution is computing
// intensive and prone to stability fitting problems. If an analytic convolution
// can be calculated, you should use that or implement it if not available.
// RooNumConvolution implements reasonable defaults that should convolve most
// functions reasonably well, but results strongly depend on the shape of your
// input PDFS so always check your result.
//
// The default integration engine for the numeric convolution is the
// adaptive Gauss-Kronrod method, which empirically seems the most robust
// for this task. You can override the convolution integration settings via
// the RooNumIntConfig object reference returned by the convIntConfig() member
// function
// <p>
// By default the numeric convolution is integrated from -infinity to
// +infinity through a <pre>x -> 1/x</pre> coordinate transformation of the
// tails. For convolution with a very small bandwidth it may be
// advantageous (for both CPU consumption and stability) if the
// integration domain is limited to a finite range. The function
// setConvolutionWindow(mean,width,scale) allows to set a sliding
// window around the x value to be calculated taking a RooAbsReal
// expression for an offset and a width to be taken around the x
// value. These input expression can be RooFormulaVars or other
// function objects although the 3d 'scale' argument 'scale'
// multiplies the width RooAbsReal expression given in the 2nd
// argument, allowing for an appropriate window definition for most
// cases without need for a RooFormulaVar object: e.g. a Gaussian
// resolution PDF do setConvolutionWindow(gaussMean,gaussSigma,5)
// Note that for a 'wide' Gaussian the -inf to +inf integration
// may converge more quickly than that over a finite range!
// <p>
// The default numeric precision is 1e-7, i.e. the global default for
// numeric integration but you should experiment with this value to
// see if it is sufficient for example by studying the number of function
// calls that MINUIT needs to fit your function as function of the
// convolution precision.
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include "Riostream.h"
#include "TH2F.h"
#include "RooNumConvolution.h"
#include "RooArgList.h"
#include "RooRealVar.h"
#include "RooFormulaVar.h"
#include "RooCustomizer.h"
#include "RooConvIntegrandBinding.h"
#include "RooNumIntFactory.h"
#include "RooGenContext.h"
#include "RooConvGenContext.h"
#include "RooMsgService.h"
ClassImp(RooNumConvolution)
;
RooNumConvolution::RooNumConvolution(const char *name, const char *title, RooRealVar& convVar, RooAbsReal& inPdf, RooAbsReal& resmodel, const RooNumConvolution* proto) :
RooAbsReal(name,title),
_init(kFALSE),
_convIntConfig(RooNumIntConfig::defaultConfig()),
_integrand(0),
_integrator(0),
_origVar("origVar","Original Convolution variable",this,convVar),
_origPdf("origPdf","Original Input PDF",this,inPdf),
_origModel("origModel","Original Resolution model",this,resmodel),
_ownedClonedPdfSet("ownedClonePdfSet"),
_ownedClonedModelSet("ownedCloneModelSet"),
_cloneVar(0),
_clonePdf(0),
_cloneModel(0),
_useWindow(kFALSE),
_windowScale(1),
_windowParam("windowParam","Convolution window parameter",this,kFALSE),
_verboseThresh(2000),
_doProf(kFALSE),
_callHist(0)
{
_convIntConfig.method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ;
_convIntConfig.method1DOpen().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ;
if (proto) {
convIntConfig() = proto->convIntConfig() ;
if (proto->_useWindow) {
setConvolutionWindow((RooAbsReal&)*proto->_windowParam.at(0),(RooAbsReal&)*proto->_windowParam.at(1),proto->_windowScale) ;
}
}
}
RooNumConvolution::RooNumConvolution(const RooNumConvolution& other, const char* name) :
RooAbsReal(other,name),
_init(kFALSE),
_convIntConfig(other._convIntConfig),
_integrand(0),
_integrator(0),
_origVar("origVar",this,other._origVar),
_origPdf("origPdf",this,other._origPdf),
_origModel("origModel",this,other._origModel),
_ownedClonedPdfSet("ownedClonePdfSet"),
_ownedClonedModelSet("ownedCloneModelSet"),
_cloneVar(0),
_clonePdf(0),
_cloneModel(0),
_useWindow(other._useWindow),
_windowScale(other._windowScale),
_windowParam("windowParam",this,other._windowParam),
_verboseThresh(other._verboseThresh),
_doProf(other._doProf),
_callHist(other._callHist)
{
}
void RooNumConvolution::initialize() const
{
_ownedClonedPdfSet.removeAll() ;
_ownedClonedModelSet.removeAll() ;
if (_cloneVar) delete _cloneVar ;
_cloneVar = new RooRealVar(Form("%s_prime",_origVar.arg().GetName()),"Convolution Variable",0) ;
RooCustomizer mgr1(pdf(),"NumConv_PdfClone") ;
mgr1.setCloneBranchSet(_ownedClonedPdfSet) ;
mgr1.replaceArg(var(),*_cloneVar) ;
_clonePdf = (RooAbsReal*) mgr1.build() ;
RooCustomizer mgr2(model(),"NumConv_ModelClone") ;
mgr2.setCloneBranchSet(_ownedClonedModelSet) ;
mgr2.replaceArg(var(),*_cloneVar) ;
_cloneModel = (RooAbsReal*) mgr2.build() ;
_cloneVar->SetName(var().GetName()) ;
_integrand = new RooConvIntegrandBinding(*_clonePdf,*_cloneModel,*_cloneVar,var(),0) ;
_integrator = RooNumIntFactory::instance().createIntegrator(*_integrand,_convIntConfig,1) ;
_integrator->setUseIntegrandLimits(kFALSE) ;
_init = kTRUE ;
}
RooNumConvolution::~RooNumConvolution()
{
}
Double_t RooNumConvolution::evaluate() const
{
if (!_init) initialize() ;
Double_t x = _origVar ;
_integrand->setNormalizationSet(_origVar.nset()) ;
if (_useWindow) {
Double_t center = ((RooAbsReal*)_windowParam.at(0))->getVal() ;
Double_t width = _windowScale * ((RooAbsReal*)_windowParam.at(1))->getVal() ;
_integrator->setLimits(x-center-width,x-center+width) ;
} else {
_integrator->setLimits(-RooNumber::infinity(),RooNumber::infinity()) ;
}
if (_doProf) _integrand->resetNumCall() ;
Double_t ret = _integrator->integral(&x) ;
if (_doProf) {
_callHist->Fill(x,_integrand->numCall()) ;
if (_integrand->numCall()>_verboseThresh) {
coutW(Integration) << "RooNumConvolution::eveluate(" << GetName() << ") WARNING convolution integral at x=" << x
<< " required " << _integrand->numCall() << " function evaluations" << endl ;
}
}
return ret ;
}
Bool_t RooNumConvolution::redirectServersHook(const RooAbsCollection& , Bool_t ,
Bool_t , Bool_t )
{
_init = kFALSE ;
return kFALSE ;
}
void RooNumConvolution::clearConvolutionWindow()
{
_useWindow = kFALSE ;
_windowParam.removeAll() ;
}
void RooNumConvolution::setConvolutionWindow(RooAbsReal& centerParam, RooAbsReal& widthParam, Double_t widthScaleFactor)
{
_useWindow = kTRUE ;
_windowParam.removeAll() ;
_windowParam.add(centerParam) ;
_windowParam.add(widthParam) ;
_windowScale = widthScaleFactor ;
}
void RooNumConvolution::setCallWarning(Int_t threshold)
{
if (threshold<0) {
coutE(InputArguments) << "RooNumConvolution::setCallWarning(" << GetName() << ") ERROR: threshold must be positive, value unchanged" << endl ;
return ;
}
_verboseThresh = threshold ;
}
void RooNumConvolution::setCallProfiling(Bool_t flag, Int_t nbinX, Int_t nbinCall, Int_t nCallHigh)
{
if (flag) {
if (_doProf) {
delete _callHist ;
}
_callHist = new TH2F(Form("callHist_%s",GetName()),Form("Call Profiling of RooNumConvolution %s",GetTitle()),
nbinX,_origVar.min(),_origVar.max(),
nbinCall,0,nCallHigh) ;
_doProf=kTRUE ;
} else if (_doProf) {
delete _callHist ;
_callHist = 0 ;
_doProf = kFALSE ;
}
}
void RooNumConvolution::printCompactTreeHook(ostream& os, const char* indent)
{
os << indent << "RooNumConvolution begin cache" << endl ;
if (_init) {
_cloneVar->printCompactTree(os,Form("%s[Var]",indent)) ;
_clonePdf->printCompactTree(os,Form("%s[Pdf]",indent)) ;
_cloneModel->printCompactTree(os,Form("%s[Mod]",indent)) ;
}
os << indent << "RooNumConvolution end cache" << endl ;
}
Last change: Wed Jun 25 08:33:40 2008
Last generated: 2008-06-25 08:33
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.