00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include "RooFit.h"
00062
00063 #include "Riostream.h"
00064 #include "Riostream.h"
00065 #include "TH2F.h"
00066 #include "RooNumConvolution.h"
00067 #include "RooArgList.h"
00068 #include "RooRealVar.h"
00069 #include "RooFormulaVar.h"
00070 #include "RooCustomizer.h"
00071 #include "RooConvIntegrandBinding.h"
00072 #include "RooNumIntFactory.h"
00073 #include "RooGenContext.h"
00074 #include "RooConvGenContext.h"
00075 #include "RooMsgService.h"
00076
00077
00078 ClassImp(RooNumConvolution)
00079 ;
00080
00081
00082
00083 RooNumConvolution::RooNumConvolution() :
00084 _init(kFALSE),
00085 _integrand(0),
00086 _integrator(0),
00087 _callHist(0)
00088 {
00089 }
00090
00091
00092
00093
00094 RooNumConvolution::RooNumConvolution(const char *name, const char *title, RooRealVar& convVar, RooAbsReal& inPdf, RooAbsReal& resmodel, const RooNumConvolution* proto) :
00095 RooAbsReal(name,title),
00096 _init(kFALSE),
00097 _convIntConfig(RooNumIntConfig::defaultConfig()),
00098 _integrand(0),
00099 _integrator(0),
00100 _origVar("origVar","Original Convolution variable",this,convVar),
00101 _origPdf("origPdf","Original Input PDF",this,inPdf),
00102 _origModel("origModel","Original Resolution model",this,resmodel),
00103 _ownedClonedPdfSet("ownedClonePdfSet"),
00104 _ownedClonedModelSet("ownedCloneModelSet"),
00105 _cloneVar(0),
00106 _clonePdf(0),
00107 _cloneModel(0),
00108 _useWindow(kFALSE),
00109 _windowScale(1),
00110 _windowParam("windowParam","Convolution window parameter",this,kFALSE),
00111 _verboseThresh(2000),
00112 _doProf(kFALSE),
00113 _callHist(0)
00114 {
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 _convIntConfig.method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ;
00126 _convIntConfig.method1DOpen().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ;
00127
00128 if (proto) {
00129 convIntConfig() = proto->convIntConfig() ;
00130 if (proto->_useWindow) {
00131 setConvolutionWindow((RooAbsReal&)*proto->_windowParam.at(0),(RooAbsReal&)*proto->_windowParam.at(1),proto->_windowScale) ;
00132 }
00133 }
00134 }
00135
00136
00137
00138
00139 RooNumConvolution::RooNumConvolution(const RooNumConvolution& other, const char* name) :
00140 RooAbsReal(other,name),
00141 _init(kFALSE),
00142 _convIntConfig(other._convIntConfig),
00143 _integrand(0),
00144 _integrator(0),
00145 _origVar("origVar",this,other._origVar),
00146 _origPdf("origPdf",this,other._origPdf),
00147 _origModel("origModel",this,other._origModel),
00148 _ownedClonedPdfSet("ownedClonePdfSet"),
00149 _ownedClonedModelSet("ownedCloneModelSet"),
00150 _cloneVar(0),
00151 _clonePdf(0),
00152 _cloneModel(0),
00153 _useWindow(other._useWindow),
00154 _windowScale(other._windowScale),
00155 _windowParam("windowParam",this,other._windowParam),
00156 _verboseThresh(other._verboseThresh),
00157 _doProf(other._doProf),
00158 _callHist(other._callHist)
00159 {
00160
00161 }
00162
00163
00164
00165
00166 void RooNumConvolution::initialize() const
00167 {
00168
00169
00170
00171
00172
00173
00174 _ownedClonedPdfSet.removeAll() ;
00175 _ownedClonedModelSet.removeAll() ;
00176
00177 if (_cloneVar) delete _cloneVar ;
00178
00179
00180
00181 _cloneVar = new RooRealVar(Form("%s_prime",_origVar.arg().GetName()),"Convolution Variable",0) ;
00182
00183 RooCustomizer mgr1(pdf(),"NumConv_PdfClone") ;
00184 mgr1.setCloneBranchSet(_ownedClonedPdfSet) ;
00185 mgr1.replaceArg(var(),*_cloneVar) ;
00186 _clonePdf = (RooAbsReal*) mgr1.build() ;
00187
00188 RooCustomizer mgr2(model(),"NumConv_ModelClone") ;
00189 mgr2.setCloneBranchSet(_ownedClonedModelSet) ;
00190 mgr2.replaceArg(var(),*_cloneVar) ;
00191 _cloneModel = (RooAbsReal*) mgr2.build() ;
00192
00193
00194 _cloneVar->SetName(var().GetName()) ;
00195
00196
00197 _integrand = new RooConvIntegrandBinding(*_clonePdf,*_cloneModel,*_cloneVar,var(),0) ;
00198
00199
00200 _integrator = RooNumIntFactory::instance().createIntegrator(*_integrand,_convIntConfig,1) ;
00201 _integrator->setUseIntegrandLimits(kFALSE) ;
00202
00203 _init = kTRUE ;
00204 }
00205
00206
00207
00208
00209
00210 RooNumConvolution::~RooNumConvolution()
00211 {
00212
00213 }
00214
00215
00216
00217
00218 Double_t RooNumConvolution::evaluate() const
00219 {
00220
00221
00222
00223 if (!_init) initialize() ;
00224
00225
00226 Double_t x = _origVar ;
00227
00228
00229 _integrand->setNormalizationSet(_origVar.nset()) ;
00230
00231
00232 if (_useWindow) {
00233 Double_t center = ((RooAbsReal*)_windowParam.at(0))->getVal() ;
00234 Double_t width = _windowScale * ((RooAbsReal*)_windowParam.at(1))->getVal() ;
00235 _integrator->setLimits(x-center-width,x-center+width) ;
00236 } else {
00237 _integrator->setLimits(-RooNumber::infinity(),RooNumber::infinity()) ;
00238 }
00239
00240
00241 if (_doProf) _integrand->resetNumCall() ;
00242 Double_t ret = _integrator->integral(&x) ;
00243 if (_doProf) {
00244 _callHist->Fill(x,_integrand->numCall()) ;
00245 if (_integrand->numCall()>_verboseThresh) {
00246 coutW(Integration) << "RooNumConvolution::eveluate(" << GetName() << ") WARNING convolution integral at x=" << x
00247 << " required " << _integrand->numCall() << " function evaluations" << endl ;
00248 }
00249 }
00250
00251 return ret ;
00252 }
00253
00254
00255
00256
00257 Bool_t RooNumConvolution::redirectServersHook(const RooAbsCollection& , Bool_t ,
00258 Bool_t , Bool_t )
00259 {
00260
00261
00262 _init = kFALSE ;
00263 return kFALSE ;
00264 }
00265
00266
00267
00268
00269 void RooNumConvolution::clearConvolutionWindow()
00270 {
00271
00272
00273 _useWindow = kFALSE ;
00274 _windowParam.removeAll() ;
00275 }
00276
00277
00278
00279
00280 void RooNumConvolution::setConvolutionWindow(RooAbsReal& centerParam, RooAbsReal& widthParam, Double_t widthScaleFactor)
00281 {
00282
00283
00284
00285
00286 _useWindow = kTRUE ;
00287 _windowParam.removeAll() ;
00288 _windowParam.add(centerParam) ;
00289 _windowParam.add(widthParam) ;
00290 _windowScale = widthScaleFactor ;
00291 }
00292
00293
00294
00295
00296 void RooNumConvolution::setCallWarning(Int_t threshold)
00297 {
00298
00299
00300
00301 if (threshold<0) {
00302 coutE(InputArguments) << "RooNumConvolution::setCallWarning(" << GetName() << ") ERROR: threshold must be positive, value unchanged" << endl ;
00303 return ;
00304 }
00305 _verboseThresh = threshold ;
00306 }
00307
00308
00309
00310 void RooNumConvolution::setCallProfiling(Bool_t flag, Int_t nbinX, Int_t nbinCall, Int_t nCallHigh)
00311 {
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 if (flag) {
00324 if (_doProf) {
00325 delete _callHist ;
00326 }
00327 _callHist = new TH2F(Form("callHist_%s",GetName()),Form("Call Profiling of RooNumConvolution %s",GetTitle()),
00328 nbinX,_origVar.min(),_origVar.max(),
00329 nbinCall,0,nCallHigh) ;
00330 _doProf=kTRUE ;
00331
00332 } else if (_doProf) {
00333
00334 delete _callHist ;
00335 _callHist = 0 ;
00336 _doProf = kFALSE ;
00337 }
00338
00339 }
00340
00341
00342
00343
00344 void RooNumConvolution::printCompactTreeHook(ostream& os, const char* indent)
00345 {
00346
00347
00348
00349 os << indent << "RooNumConvolution begin cache" << endl ;
00350
00351 if (_init) {
00352 _cloneVar->printCompactTree(os,Form("%s[Var]",indent)) ;
00353 _clonePdf->printCompactTree(os,Form("%s[Pdf]",indent)) ;
00354 _cloneModel->printCompactTree(os,Form("%s[Mod]",indent)) ;
00355 }
00356
00357 os << indent << "RooNumConvolution end cache" << endl ;
00358 }
00359
00360