Logo ROOT  
Reference Guide
RooFunctorBinding.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * File: $Id$
5  * Authors: *
6  * WV, Wouter Verkerke, NIKHEF, verkerke@nikhef.nl *
7  * *
8  * Copyright (c) 2000-2008, NIKHEF, Regents of the University of California *
9  * and Stanford University. All rights reserved. *
10  * *
11  *****************************************************************************/
12 
13 /** \class RooFunctorBinding
14  \ingroup Roofit
15 
16 RooFunctorBinding makes math functions from ROOT usable in RooFit. It takes
17 a ROOT::Math::IBaseFunctionMultiDim, and binds the variables of this function to
18 the RooFit variables passed in the constructor.
19 
20 Instances of function binding
21 classes are fully functional RooFit function objects with one exception:
22 if the bound function is *not* a standard TMath or MathMore function the
23 class cannot be persisted in a RooWorkspace without registering the function
24 pointer first using RooCFunction1Binding<T1,T2>::register().
25 **/
26 
27 /** \class RooFunctorPdfBinding
28  \ingroup Roofit
29 RooFunctorPdfBinding makes math functions from ROOT usable as PDFs in RooFit. It takes
30 a ROOT::Math::IBaseFunctionMultiDim, and binds the variables of this function to
31 the RooFit variables passed in the constructor.
32 When the PDF is evaluated, the bound function is evaluated, and also integrated numerically
33 to normalise it to unity over the range of its observables.
34 
35 Instances of function binding
36 classes are fully functional RooFit function objects with one exception:
37 if the bound function is *not* a standard TMath or MathMore function the
38 class cannot be persisted in a RooWorkspace without registering the function
39 pointer first using RooCFunction1Binding<T1,T2>::register().
40 **/
41 
42 #include "Riostream.h"
43 #include "RooFunctorBinding.h"
44 
45 using namespace std ;
46 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Create a RooFit function that makes `ftor` usable in RooFit.
52 /// \param name Name of the object.
53 /// \param title Title (e.g. for plotting)
54 /// \param ftor Functor instance to be evaluated.
55 /// \param v RooFit variables to be passed to the function.
56 RooFunctorBinding::RooFunctorBinding(const char *name, const char *title, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& v) :
57  RooAbsReal(name,title),
58  func(&ftor),
59  vars("vars","vars",this)
60 {
61  // Check that function dimension and number of variables match
62  if (ftor.NDim()!=UInt_t(v.getSize())) {
63  coutE(InputArguments) << "RooFunctorBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.getSize()
64  << ") does not match dimensionality of function (" << ftor.NDim() << ")" << endl ;
65  throw string("RooFunctor::ctor ERROR") ;
66  }
67  x = new Double_t[func->NDim()] ;
68  vars.add(v) ;
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
73  RooAbsReal(other,name),
74  func(other.func),
75  vars("vars",this,other.vars)
76 {
77  // Copy constructor
78  x = new Double_t[func->NDim()] ;
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 void RooFunctorBinding::printArgs(ostream& os) const {
83  // Print object arguments and name/address of function pointer
84  os << "[ function=" << func << " " ;
85  for (Int_t i=0 ; i<numProxies() ; i++) {
86  RooAbsProxy* p = getProxy(i) ;
87  if (!TString(p->name()).BeginsWith("!")) {
88  p->print(os) ;
89  os << " " ;
90  }
91  }
92  os << "]" ;
93 }
94 
95 ////////////////////////////////////////////////////////////////////////////////
97  // Return value of embedded function using value of referenced variable x
98  for (int i=0 ; i<vars.getSize() ; i++) {
99  x[i] = ((RooAbsReal*)vars.at(i))->getVal() ;
100  }
101  return (*func)(x) ;
102 }
103 
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Create a RooFit PDF that makes `ftor` usable as a PDF in RooFit.
107 /// \param name Name of the object.
108 /// \param title Title (e.g. for plotting)
109 /// \param ftor Functor instance to be evaluated and normalised.
110 /// \param v RooFit variables to be passed to the function.
112  RooAbsPdf(name,title),
113  func(&ftor),
114  vars("vars","vars",this)
115 {
116  // Check that function dimension and number of variables match
117  if (ftor.NDim()!=UInt_t(v.getSize())) {
118  coutE(InputArguments) << "RooFunctorPdfBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.getSize()
119  << ") does not match dimensionality of function (" << ftor.NDim() << ")" << endl ;
120  throw string("RooFunctor::ctor ERROR") ;
121  }
122  x = new Double_t[func->NDim()] ;
123  vars.add(v) ;
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
128  RooAbsPdf(other,name),
129  func(other.func),
130  vars("vars",this,other.vars)
131 {
132  // Copy constructor
133  x = new Double_t[func->NDim()] ;
134 }
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 void RooFunctorPdfBinding::printArgs(ostream& os) const {
138  // Print object arguments and name/address of function pointer
139  os << "[ function=" << func << " " ;
140  for (Int_t i=0 ; i<numProxies() ; i++) {
141  RooAbsProxy* p = getProxy(i) ;
142  if (!TString(p->name()).BeginsWith("!")) {
143  p->print(os) ;
144  os << " " ;
145  }
146  }
147  os << "]" ;
148 }
149 
150 ////////////////////////////////////////////////////////////////////////////////
152  // Return value of embedded function using value of referenced variable x
153  for (int i=0 ; i<vars.getSize() ; i++) {
154  x[i] = ((RooAbsReal*)vars.at(i))->getVal() ;
155  }
156  return (*func)(x) ;
157  }
158 
159 namespace RooFit {
160 
162  return new RooFunctorBinding(name,name,ftor,vars) ;
163  }
164 
165  RooAbsPdf* bindPdf(const char* name, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& vars) {
166  return new RooFunctorPdfBinding(name,name,ftor,vars) ;
167  }
168 
169 }
RooAbsProxy::print
virtual void print(std::ostream &os, Bool_t addContents=kFALSE) const
Print proxy name.
Definition: RooAbsProxy.cxx:74
RooAbsArg::numProxies
Int_t numProxies() const
Return the number of registered proxies.
Definition: RooAbsArg.cxx:1275
RooFunctorBinding::printArgs
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
Definition: RooFunctorBinding.cxx:82
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
coutE
#define coutE(a)
Definition: RooMsgService.h:33
RooArgList
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooAbsProxy::name
virtual const char * name() const
Definition: RooAbsProxy.h:40
ROOT::Math::IBaseFunctionMultiDimTempl::NDim
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
RooAbsReal
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:61
RooAbsArg::getProxy
RooAbsProxy * getProxy(Int_t index) const
Return the nth proxy from the proxy list.
Definition: RooAbsArg.cxx:1262
RooArgList::at
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition: RooArgList.h:70
RooFunctorBinding::x
Double_t * x
Definition: RooFunctorBinding.h:48
RooFunctorBinding::func
const ROOT::Math::IBaseFunctionMultiDim * func
Definition: RooFunctorBinding.h:46
RooFunctorPdfBinding::x
Double_t * x
Definition: RooFunctorBinding.h:75
RooFunctorBinding.h
TString
Basic string class.
Definition: TString.h:136
v
@ v
Definition: rootcling_impl.cxx:3635
RooFunctorBinding::evaluate
Double_t evaluate() const
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
Definition: RooFunctorBinding.cxx:96
RooFit::bindFunction
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)
Definition: RooCFunction1Binding.cxx:59
RooFunctorPdfBinding::printArgs
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
Definition: RooFunctorBinding.cxx:137
RooFit
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: RooCFunction1Binding.h:29
RooFunctorBinding::vars
RooListProxy vars
Definition: RooFunctorBinding.h:47
RooFunctorPdfBinding::RooFunctorPdfBinding
RooFunctorPdfBinding()
Definition: RooFunctorBinding.h:60
TString::BeginsWith
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:615
RooFunctorPdfBinding::func
const ROOT::Math::IBaseFunctionMultiDim * func
Definition: RooFunctorBinding.h:73
RooFunctorBinding
RooFunctorBinding makes math functions from ROOT usable in RooFit.
Definition: RooFunctorBinding.h:31
unsigned int
RooFunctorBinding::RooFunctorBinding
RooFunctorBinding()
Definition: RooFunctorBinding.h:33
RooFunctorPdfBinding
RooFunctorPdfBinding makes math functions from ROOT usable as PDFs in RooFit.
Definition: RooFunctorBinding.h:58
Double_t
double Double_t
Definition: RtypesCore.h:59
RooFunctorPdfBinding::vars
RooListProxy vars
Definition: RooFunctorBinding.h:74
name
char name[80]
Definition: TGX11.cxx:110
RooFunctorPdfBinding::evaluate
Double_t evaluate() const
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
Definition: RooFunctorBinding.cxx:151
RooListProxy::add
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Reimplementation of standard RooArgList::add()
Definition: RooListProxy.cxx:104
RooAbsProxy
RooAbsProxy is the abstact interface for proxy classes.
Definition: RooAbsProxy.h:30
RooAbsPdf
Definition: RooAbsPdf.h:43
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
ROOT::Math::IBaseFunctionMultiDimTempl< double >
RooFit::bindPdf
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
Definition: RooCFunction1Binding.cxx:67
Riostream.h
RooAbsCollection::getSize
Int_t getSize() const
Definition: RooAbsCollection.h:172
int