Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
16RooFunctorBinding makes math functions from ROOT usable in RooFit. It takes
17a ROOT::Math::IBaseFunctionMultiDim, and binds the variables of this function to
18the RooFit variables passed in the constructor.
19
20Instances of function binding
21classes are fully functional RooFit function objects with one exception:
22if the bound function is *not* a standard TMath or MathMore function the
23class cannot be persisted in a RooWorkspace without registering the function
24pointer first using RooCFunction1Binding<T1,T2>::register().
25**/
26
27/** \class RooFunctorPdfBinding
28 \ingroup Roofit
29RooFunctorPdfBinding makes math functions from ROOT usable as PDFs in RooFit. It takes
30a ROOT::Math::IBaseFunctionMultiDim, and binds the variables of this function to
31the RooFit variables passed in the constructor.
32When the PDF is evaluated, the bound function is evaluated, and also integrated numerically
33to normalise it to unity over the range of its observables.
34
35Instances of function binding
36classes are fully functional RooFit function objects with one exception:
37if the bound function is *not* a standard TMath or MathMore function the
38class cannot be persisted in a RooWorkspace without registering the function
39pointer first using RooCFunction1Binding<T1,T2>::register().
40**/
41
42#include "Riostream.h"
43#include "RooFunctorBinding.h"
44
45using 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.
56RooFunctorBinding::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////////////////////////////////////////////////////////////////////////////////
82void 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////////////////////////////////////////////////////////////////////////////////
137void 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
159namespace 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}
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
Int_t numProxies() const
Return the number of registered proxies.
RooAbsProxy * getProxy(Int_t index) const
Return the nth proxy from the proxy list.
Int_t getSize() const
RooAbsProxy is the abstact interface for proxy classes.
Definition RooAbsProxy.h:30
virtual void print(std::ostream &os, Bool_t addContents=kFALSE) const
Print proxy name.
virtual const char * name() const
Definition RooAbsProxy.h:40
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:61
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:91
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:21
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:70
RooFunctorBinding makes math functions from ROOT usable in RooFit.
const ROOT::Math::IBaseFunctionMultiDim * func
Double_t evaluate() const
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
RooFunctorPdfBinding makes math functions from ROOT usable as PDFs in RooFit.
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
Double_t evaluate() const
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
const ROOT::Math::IBaseFunctionMultiDim * func
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Reimplementation of standard RooArgList::add()
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:136
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:615
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)