ROOT  6.06/09
Reference Guide
RooNumConvPdf.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 //////////////////////////////////////////////////////////////////////////////
18 //
19 // BEGIN_HTML
20 // Numeric 1-dimensional convolution operator PDF. This class can convolve any PDF
21 // with any other PDF using a straightforward numeric calculation of the
22 // convolution integral
23 // <p>
24 // This class should be used as last resort as numeric convolution calculated
25 // this way is computationally intensive and prone to stability fitting problems.
26 // <b>The preferred way to compute numeric convolutions is RooFFTConvPdf</b>,
27 // which calculates convolutions using Fourier Transforms (requires external free
28 // FFTW3 package)
29 // <p>
30 // RooNumConvPdf implements reasonable defaults that should convolve most
31 // functions reasonably well, but results strongly depend on the shape of your
32 // input PDFS so always check your result.
33 // <p>
34 // The default integration engine for the numeric convolution is the
35 // adaptive Gauss-Kronrod method, which empirically seems the most robust
36 // for this task. You can override the convolution integration settings via
37 // the RooNumIntConfig object reference returned by the convIntConfig() member
38 // function
39 // <p>
40 // By default the numeric convolution is integrated from -infinity to
41 // +infinity through a <pre>x -> 1/x</pre> coordinate transformation of the
42 // tails. For convolution with a very small bandwidth it may be
43 // advantageous (for both CPU consumption and stability) if the
44 // integration domain is limited to a finite range. The function
45 // setConvolutionWindow(mean,width,scale) allows to set a sliding
46 // window around the x value to be calculated taking a RooAbsReal
47 // expression for an offset and a width to be taken around the x
48 // value. These input expression can be RooFormulaVars or other
49 // function objects although the 3d 'scale' argument 'scale'
50 // multiplies the width RooAbsReal expression given in the 2nd
51 // argument, allowing for an appropriate window definition for most
52 // cases without need for a RooFormulaVar object: e.g. a Gaussian
53 // resolution PDF do setConvolutionWindow(gaussMean,gaussSigma,5)
54 // Note that for a 'wide' Gaussian the -inf to +inf integration
55 // may converge more quickly than that over a finite range!
56 // <p>
57 // The default numeric precision is 1e-7, i.e. the global default for
58 // numeric integration but you should experiment with this value to
59 // see if it is sufficient for example by studying the number of function
60 // calls that MINUIT needs to fit your function as function of the
61 // convolution precision.
62 // END_HTML
63 //
64 
65 #include "RooFit.h"
66 
67 #include "Riostream.h"
68 #include "Riostream.h"
69 #include "TH2F.h"
70 #include "RooNumConvPdf.h"
71 #include "RooArgList.h"
72 #include "RooRealVar.h"
73 #include "RooFormulaVar.h"
74 #include "RooCustomizer.h"
76 #include "RooNumIntFactory.h"
77 #include "RooGenContext.h"
78 #include "RooConvGenContext.h"
79 
80 
81 
82 using namespace std;
83 
85 ;
86 
87 
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 
92  _init(kFALSE),
93  _conv(0)
94 {
95 }
96 
97 
98 
99 
100 //_____________________________________________________________________________R
101 RooNumConvPdf::RooNumConvPdf(const char *name, const char *title, RooRealVar& convVar, RooAbsPdf& inPdf, RooAbsPdf& resmodel) :
102  RooAbsPdf(name,title),
103  _init(kFALSE),
104  _conv(0),
105  _origVar("!origVar","Original Convolution variable",this,convVar),
106  _origPdf("!origPdf","Original Input PDF",this,inPdf),
107  _origModel("!origModel","Original Resolution model",this,resmodel)
108 {
109  // Constructor of convolution operator PDF
110  //
111  // convVar : convolution variable (on which both pdf and resmodel should depend)
112  // pdf : input 'physics' pdf
113  // resmodel : input 'resultion' pdf
114  //
115  // output is pdf(x) (X) resmodel(x) = Int [ pdf(x') resmodel (x-x') ] dx'
116  //
117 }
118 
119 
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Copy constructor
123 
124 RooNumConvPdf::RooNumConvPdf(const RooNumConvPdf& other, const char* name) :
125  RooAbsPdf(other,name),
126  _init(kFALSE),
127  _origVar("!origVar",this,other._origVar),
128  _origPdf("!origPdf",this,other._origPdf),
129  _origModel("!origModel",this,other._origModel)
130 {
131  // Make temporary clone of original convolution to preserve configuration information
132  // This information will be propagated to a newly create convolution in a subsequent
133  // call to initialize()
134  if (other._conv) {
135  _conv = new RooNumConvolution(*other._conv,Form("%s_CONV",name?name:GetName())) ;
136  } else {
137  _conv = 0 ;
138  }
139 }
140 
141 
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Destructor
145 
147 {
148  if (_init) {
149  delete _conv ;
150  }
151 }
152 
153 
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Calculate and return value of p.d.f
157 
159 {
160  if (!_init) initialize() ;
161 
162  return _conv->evaluate() ;
163 }
164 
165 
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// One-time initialization of object
169 
171 {
172  // Save pointer to any prototype convolution object (only present if this object is made through
173  // a copy constructor)
174  RooNumConvolution* protoConv = _conv ;
175 
176  // Optionally pass along configuration data from prototype object
177  _conv = new RooNumConvolution(Form("%s_CONV",GetName()),GetTitle(),var(),pdf(),model(),protoConv) ;
178 
179  // Delete prototype object now
180  if (protoConv) {
181  delete protoConv ;
182  }
183 
184  _init = kTRUE ;
185 }
186 
187 
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Return appropriate generator context for this convolved p.d.f. If both pdf and resolution
191 /// model support internal generation return and optimization convolution generation context
192 /// that uses a smearing algorithm. Otherwise return a standard accept/reject sampling
193 /// context on the convoluted shape.
194 
196  const RooArgSet* auxProto, Bool_t verbose) const
197 {
198  if (!_init) initialize() ;
199 
200  // Check if physics PDF and resolution model can both directly generate the convolution variable
201  RooArgSet* modelDep = _conv->model().getObservables(&vars) ;
202  modelDep->remove(_conv->var(),kTRUE,kTRUE) ;
203  Int_t numAddDep = modelDep->getSize() ;
204  delete modelDep ;
205 
206  RooArgSet dummy ;
207  Bool_t pdfCanDir = (((RooAbsPdf&)_conv->pdf()).getGenerator(_conv->var(),dummy) != 0 && \
209  Bool_t resCanDir = (((RooAbsPdf&)_conv->model()).getGenerator(_conv->var(),dummy) !=0 &&
211 
212  if (numAddDep>0 || !pdfCanDir || !resCanDir) {
213  // Any resolution model with more dependents than the convolution variable
214  // or pdf or resmodel do not support direct generation
215  return new RooGenContext(*this,vars,prototype,auxProto,verbose) ;
216  }
217 
218  // Any other resolution model: use specialized generator context
219  return new RooConvGenContext(*this,vars,prototype,auxProto,verbose) ;
220 }
221 
222 
223 
224 ////////////////////////////////////////////////////////////////////////////////
225 /// Customized printing of arguments of a RooNumConvPdf to more intuitively reflect the contents of the
226 /// product operator construction
227 
228 void RooNumConvPdf::printMetaArgs(ostream& os) const
229 {
230  os << _origPdf.arg().GetName() << "(" << _origVar.arg().GetName() << ") (*) " << _origModel.arg().GetName() << "(" << _origVar.arg().GetName() << ") " ;
231 }
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
void initialize() const
do not persist
Double_t evaluate() const
Calculate convolution integral.
virtual ~RooNumConvPdf()
Destructor.
RooAbsReal & model() const
Definition: RooNumConvPdf.h:54
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Definition: RooAbsArg.h:194
RooRealVar & var() const
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
RooAbsReal & model() const
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
const RooAbsReal & arg() const
Definition: RooRealProxy.h:43
friend class RooConvGenContext
Definition: RooNumConvPdf.h:76
virtual Double_t evaluate() const
Calculate and return value of p.d.f.
virtual Bool_t isDirectGenSafe(const RooAbsArg &arg) const
Check if given observable can be safely generated using the pdfs internal generator mechanism (if tha...
Definition: RooAbsPdf.cxx:2136
RooAbsReal & pdf() const
virtual RooAbsGenContext * genContext(const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t verbose=kFALSE) const
Return appropriate generator context for this convolved p.d.f.
void printMetaArgs(std::ostream &os) const
Customized printing of arguments of a RooNumConvPdf to more intuitively reflect the contents of the p...
bool verbose
char * Form(const char *fmt,...)
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
RooRealVar & var() const
Definition: RooNumConvPdf.h:52
double Double_t
Definition: RtypesCore.h:55
static RooMathCoreReg dummy
RooRealProxy _origPdf
Definition: RooNumConvPdf.h:70
#define name(a, b)
Definition: linkTestLib0.cpp:5
RooRealProxy _origVar
Actual convolution calculation.
Definition: RooNumConvPdf.h:69
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
RooRealProxy _origModel
Definition: RooNumConvPdf.h:71
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
ClassImp(RooNumConvPdf)
RooAbsReal & pdf() const
Definition: RooNumConvPdf.h:53
Int_t getSize() const
const Bool_t kTRUE
Definition: Rtypes.h:91
RooNumConvolution * _conv
Definition: RooNumConvPdf.h:67
virtual Int_t getGenerator(const RooArgSet &directVars, RooArgSet &generateVars, Bool_t staticInitOK=kTRUE) const
Load generatedVars with the subset of directVars that we can generate events for, and return a code t...
Definition: RooAbsPdf.cxx:2101