Logo ROOT   6.10/09
Reference Guide
RooResolutionModel.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 // RooResolutionModel is the base class of for PDFs that represent a
20 // resolution model that can be convoluted with physics a physics model of the form
21 //
22 // Phys(x,a,b) = Sum_k coef_k(a) * basis_k(x,b)
23 //
24 // where basis_k are a limited number of functions in terms of the variable
25 // to be convoluted and coef_k are coefficients independent of the convolution
26 // variable.
27 //
28 // Classes derived from RooResolutionModel implement
29 // _ _ _ _
30 // R_k(x,b,c) = Int(dx') basis_k(x',b) * resModel(x-x',c)
31 //
32 // which RooAbsAnaConvPdf uses to construct the pdf for [ Phys (x) R ] :
33 // _ _ _ _ _ _
34 // PDF(x,a,b,c) = Sum_k coef_k(a) * R_k(x,b,c)
35 //
36 // A minimal implementation of a RooResolutionModel consists of a
37 //
38 // Int_t basisCode(const char* name)
39 //
40 // function indication which basis functions this resolution model supports, and
41 //
42 // Double_t evaluate()
43 //
44 // Implementing the resolution model, optionally convoluted with one of the
45 // supported basis functions. RooResolutionModel objects can be used as regular
46 // PDFs (They inherit from RooAbsPdf), or as resolution model convoluted with
47 // a basis function. The implementation of evaluate() can identify the requested
48 // from of use from the basisCode() function. If zero, the regular PDF value
49 // should be calculated. If non-zero, the models value convoluted with the
50 // basis function identified by the code should be calculated.
51 //
52 // Optionally, analytical integrals can be advertised and implemented, in the
53 // same way as done for regular PDFS (see RooAbsPdf for further details).
54 // Also in getAnalyticalIntegral()/analyticalIntegral() the implementation
55 // should use basisCode() to determine for which scenario the integral is
56 // requested.
57 //
58 // The choice of basis returned by basisCode() is guaranteed not to change
59 // of the lifetime of a RooResolutionModel object.
60 //
61 
62 #include "RooFit.h"
63 
64 #include "TClass.h"
65 #include "TMath.h"
66 #include "Riostream.h"
67 #include "RooResolutionModel.h"
68 #include "RooMsgService.h"
69 #include "RooSentinel.h"
70 
71 using namespace std;
72 
74 ;
75 
77 
78 
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Cleanup hook for RooSentinel atexit handler
82 
84 {
85  delete _identity ;
86  _identity = 0 ;
87 }
88 
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Constructor with convolution variable 'x'
92 
93 RooResolutionModel::RooResolutionModel(const char *name, const char *title, RooRealVar& _x) :
94  RooAbsPdf(name,title),
95  x("x","Dependent or convolution variable",this,_x),
96  _basisCode(0), _basis(0),
97  _ownBasis(kFALSE)
98 {
99  if (!_identity) {
100  _identity = identity() ;
101  }
102 }
103 
104 
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Copy constructor
108 
110  RooAbsPdf(other,name),
111  x("x",this,other.x),
112  _basisCode(other._basisCode), _basis(0),
114 {
115  if (other._basis) {
116  _basis = (RooFormulaVar*) other._basis->Clone() ;
117  _ownBasis = kTRUE ;
118  //_basis = other._basis ;
119  }
120 
121  if (_basis) {
122  TIterator* bsIter = _basis->serverIterator() ;
123  RooAbsArg* basisServer ;
124  while((basisServer = (RooAbsArg*)bsIter->Next())) {
125  addServer(*basisServer,kTRUE,kFALSE) ;
126  }
127  delete bsIter ;
128  }
129 }
130 
131 
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Destructor
135 
137 {
138  if (_ownBasis && _basis) {
139  delete _basis ;
140  }
141 }
142 
143 
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// Return identity formula pointer
147 
149 {
150  if (!_identity) {
151  _identity = new RooFormulaVar("identity","1",RooArgSet("")) ;
153  }
154 
155  return _identity ;
156 }
157 
158 
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// Instantiate a clone of this resolution model representing a convolution with given
162 /// basis function. The owners object name is incorporated in the clones name
163 /// to avoid multiple convolution objects with the same name in complex PDF structures.
164 ///
165 /// Note: The 'inBasis' formula expression must be a RooFormulaVar that encodes the formula
166 /// in the title of the object and this expression must be an exact match against the
167 /// implemented basis function strings (see derived class implementation of method basisCode()
168 /// for those strings
169 
171 {
172  // Check that primary variable of basis functions is our convolution variable
173  if (inBasis->getParameter(0) != x.absArg()) {
174  coutE(InputArguments) << "RooResolutionModel::convolution(" << GetName() << "," << this
175  << ") convolution parameter of basis function and PDF don't match" << endl
176  << "basis->findServer(0) = " << inBasis->findServer(0) << endl
177  << "x.absArg() = " << x.absArg() << endl ;
178  return 0 ;
179  }
180 
181  if (basisCode(inBasis->GetTitle())==0) {
182  coutE(InputArguments) << "RooResolutionModel::convolution(" << GetName() << "," << this
183  << ") basis function '" << inBasis->GetTitle() << "' is not supported." << endl ;
184  return 0 ;
185  }
186 
187  TString newName(GetName()) ;
188  newName.Append("_conv_") ;
189  newName.Append(inBasis->GetName()) ;
190  newName.Append("_[") ;
191  newName.Append(owner->GetName()) ;
192  newName.Append("]") ;
193 
194  RooResolutionModel* conv = (RooResolutionModel*) clone(newName) ;
195 
196  TString newTitle(conv->GetTitle()) ;
197  newTitle.Append(" convoluted with basis function ") ;
198  newTitle.Append(inBasis->GetName()) ;
199  conv->SetTitle(newTitle.Data()) ;
200 
201  conv->changeBasis(inBasis) ;
202 
203  return conv ;
204 }
205 
206 
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Change the basis function we convolute with.
210 /// For one-time use by convolution() only.
211 
213 {
214  // Remove client-server link to old basis
215  if (_basis) {
216  TIterator* bsIter = _basis->serverIterator() ;
217  RooAbsArg* basisServer ;
218  while((basisServer = (RooAbsArg*)bsIter->Next())) {
219  removeServer(*basisServer) ;
220  }
221  delete bsIter ;
222 
223  if (_ownBasis) {
224  delete _basis ;
225  }
226  }
227  _ownBasis = kFALSE ;
228 
229  // Change basis pointer and update client-server link
230  _basis = inBasis ;
231  if (_basis) {
232  TIterator* bsIter = _basis->serverIterator() ;
233  RooAbsArg* basisServer ;
234  while((basisServer = (RooAbsArg*)bsIter->Next())) {
235  addServer(*basisServer,kTRUE,kFALSE) ;
236  }
237  delete bsIter ;
238  }
239 
240  _basisCode = inBasis?basisCode(inBasis->GetTitle()):0 ;
241 }
242 
243 
244 
245 ////////////////////////////////////////////////////////////////////////////////
246 /// Return the convolution variable of the selection basis function.
247 /// This is, by definition, the first parameter of the basis function
248 
250 {
251  // Convolution variable is by definition first server of basis function
252  TIterator* sIter = basis().serverIterator() ;
253  RooRealVar* var = (RooRealVar*) sIter->Next() ;
254  delete sIter ;
255 
256  return *var ;
257 }
258 
259 
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// Return the convolution variable of the resolution model
263 
265 {
266  return (RooRealVar&) x.arg() ;
267 }
268 
269 
270 
271 ////////////////////////////////////////////////////////////////////////////////
272 /// Modified version of RooAbsPdf::getValF(). If used as regular PDF,
273 /// call RooAbsPdf::getValF(), otherwise return unnormalized value
274 /// regardless of specified normalization set
275 
277 {
278  if (!_basis) return RooAbsPdf::getValV(nset) ;
279 
280  // Return value of object. Calculated if dirty, otherwise cached value is returned.
281  if (isValueDirty()) {
282  _value = evaluate() ;
283 
284  // WVE insert traceEval traceEval
285  if (_verboseDirty) cxcoutD(Tracing) << "RooResolutionModel(" << GetName() << ") value = " << _value << endl ;
286 
287  clearValueDirty() ;
288  clearShapeDirty() ;
289  }
290 
291  return _value ;
292 }
293 
294 
295 
296 ////////////////////////////////////////////////////////////////////////////////
297 /// Forward redirectServers call to our basis function, which is not connected to either resolution
298 /// model or the physics model.
299 
300 Bool_t RooResolutionModel::redirectServersHook(const RooAbsCollection& newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t /*isRecursive*/)
301 {
302  if (!_basis) {
303  _norm = 0 ;
304  return kFALSE ;
305  }
306 
307  RooFormulaVar* newBasis = (RooFormulaVar*) newServerList.find(_basis->GetName()) ;
308  if (newBasis) {
309 
310  if (_ownBasis) {
311  delete _basis ;
312  }
313 
314  _basis = newBasis ;
315  _ownBasis = kFALSE ;
316  }
317 
318  _basis->redirectServers(newServerList,mustReplaceAll,nameChange) ;
319 
320  return (mustReplaceAll && !newBasis) ;
321 }
322 
323 
324 
325 ////////////////////////////////////////////////////////////////////////////////
326 /// Floating point error checking and tracing for given float value
327 
329 {
330  // check for a math error or negative value
331  return TMath::IsNaN(value) ;
332 }
333 
334 
335 
336 ////////////////////////////////////////////////////////////////////////////////
337 /// Return the list of servers used by our normalization integral
338 
340 {
341  _norm->leafNodeServerList(&list) ;
342 }
343 
344 
345 
346 ////////////////////////////////////////////////////////////////////////////////
347 /// Return the integral of this PDF over all elements of 'nset'.
348 
350 {
351  if (!nset) {
352  return getVal() ;
353  }
354 
355  syncNormalization(nset,kFALSE) ;
356  if (_verboseEval>1) cxcoutD(Tracing) << IsA()->GetName() << "::getNorm(" << GetName()
357  << "): norm(" << _norm << ") = " << _norm->getVal() << endl ;
358 
359  Double_t ret = _norm->getVal() ;
360  return ret ;
361 }
362 
363 
364 
365 ////////////////////////////////////////////////////////////////////////////////
366 /// Print info about this object to the specified stream. In addition to the info
367 /// from RooAbsArg::printStream() we add:
368 ///
369 /// Shape : value, units, plot range
370 /// Verbose : default binning and print label
371 
372 void RooResolutionModel::printMultiline(ostream& os, Int_t content, Bool_t verbose, TString indent) const
373 {
374  RooAbsPdf::printMultiline(os,content,verbose,indent) ;
375 
376  if(verbose) {
377  os << indent << "--- RooResolutionModel ---" << endl;
378  os << indent << "basis function = " ;
379  if (_basis) {
381  } else {
382  os << "<none>" << endl ;
383  }
384  }
385 }
386 
void clearValueDirty() const
Definition: RooAbsArg.h:447
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
#define coutE(a)
Definition: RooMsgService.h:34
virtual void printStream(std::ostream &os, Int_t contents, StyleOption style, TString indent="") const
Print description of object on ostream, printing contents set by contents integer, which is interpreted as an OR of &#39;enum ContentsOptions&#39; values and in the style given by &#39;enum StyleOption&#39;.
virtual ~RooResolutionModel()
Destructor.
void leafNodeServerList(RooAbsCollection *list, const RooAbsArg *arg=0, Bool_t recurseNonDerived=kFALSE) const
Fill supplied list with all leaf nodes of the arg tree, starting with ourself as top node...
Definition: RooAbsArg.cxx:493
virtual Double_t evaluate() const =0
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Print multi line detailed information of this RooAbsPdf.
Definition: RooAbsPdf.cxx:1611
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
#define cxcoutD(a)
Definition: RooMsgService.h:79
const RooRealVar & basisConvVar() const
Return the convolution variable of the selection basis function.
RooRealVar & convVar() const
Return the convolution variable of the resolution model.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual TObject * Clone(const char *newname=0) const
Make a clone of an object using the Streamer facility.
Definition: RooAbsArg.h:75
virtual RooResolutionModel * convolution(RooFormulaVar *basis, RooAbsArg *owner) const
Instantiate a clone of this resolution model representing a convolution with given basis function...
STL namespace.
Double_t getValV(const RooArgSet *nset=0) const
Modified version of RooAbsPdf::getValF().
Iterator abstract base class.
Definition: TIterator.h:30
const RooFormulaVar & basis() const
RooAbsArg * findServer(const char *name) const
Definition: RooAbsArg.h:122
Double_t x[n]
Definition: legend1.C:17
static Bool_t _verboseDirty
Definition: RooAbsArg.h:552
friend class RooArgSet
Definition: RooAbsArg.h:469
static void cleanup()
Cleanup hook for RooSentinel atexit handler.
virtual Bool_t syncNormalization(const RooArgSet *dset, Bool_t adjustProxies=kTRUE) const
Verify that the normalization integral cached with this PDF is valid for given set of normalization o...
Definition: RooAbsPdf.cxx:435
RooFormulaVar * _basis
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print info about this object to the specified stream.
virtual void changeBasis(RooFormulaVar *basis)
Change the basis function we convolute with.
virtual Double_t getValV(const RooArgSet *set=0) const
Return current value, normalizated by integrating over the observables in &#39;nset&#39;. ...
Definition: RooAbsPdf.cxx:252
RooAbsReal * _norm
Definition: RooAbsPdf.h:301
static Int_t _verboseEval
Definition: RooAbsPdf.h:295
bool verbose
RooAbsArg * absArg() const
Definition: RooArgProxy.h:37
static RooFormulaVar * identity()
Return identity formula pointer.
Double_t getNorm(const RooArgSet *nset=0) const
Return the integral of this PDF over all elements of &#39;nset&#39;.
TIterator * serverIterator() const
Definition: RooAbsArg.h:112
const Bool_t kFALSE
Definition: RtypesCore.h:92
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:71
#define ClassImp(name)
Definition: Rtypes.h:336
RooAbsArg * find(const char *name) const
Find object with given name in list.
double Double_t
Definition: RtypesCore.h:55
Bool_t isValueDirty() const
Definition: RooAbsArg.h:336
Double_t _value
Definition: RooAbsReal.h:389
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects...
static RooFormulaVar * _identity
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
virtual TObject * Next()=0
Int_t IsNaN(Double_t x)
Definition: TMath.h:778
virtual TObject * clone(const char *newname) const =0
virtual void normLeafServerList(RooArgSet &list) const
Return the list of servers used by our normalization integral.
virtual Int_t basisCode(const char *name) const =0
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const RooAbsReal & arg() const
Definition: RooRealProxy.h:43
void addServer(RooAbsArg &server, Bool_t valueProp=kTRUE, Bool_t shapeProp=kFALSE)
Register another RooAbsArg as a server to us, ie, declare that we depend on it.
Definition: RooAbsArg.cxx:362
virtual Bool_t redirectServersHook(const RooAbsCollection &newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t isRecursive)
Forward redirectServers call to our basis function, which is not connected to either resolution model...
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
Bool_t traceEvalHook(Double_t value) const
Floating point error checking and tracing for given float value.
Bool_t redirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t isRecursionStep=kFALSE)
Iterator over _clientListValue.
Definition: RooAbsArg.cxx:929
void removeServer(RooAbsArg &server, Bool_t force=kFALSE)
Unregister another RooAbsArg as a server to us, ie, declare that we no longer depend on its value and...
Definition: RooAbsArg.cxx:413
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooAbsArg * getParameter(const char *name) const
Definition: RooFormulaVar.h:39
void clearShapeDirty() const
Definition: RooAbsArg.h:450
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48