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 std::endl, std::ostream, std::string;
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.size())) {
63 coutE(InputArguments) << "RooFunctorBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.size()
64 << ") does not match dimensionality of function (" << ftor.NDim() << ")" << endl ;
65 throw string("RooFunctor::ctor ERROR") ;
66 }
67 x = new double[func->NDim()] ;
68 vars.add(v) ;
69}
70
71////////////////////////////////////////////////////////////////////////////////
73 : RooAbsReal(other, name), func(other.func), vars("vars", this, other.vars), x(new double[func->NDim()])
74{
75 // Copy constructor
76}
77
78////////////////////////////////////////////////////////////////////////////////
79void RooFunctorBinding::printArgs(ostream& os) const {
80 // Print object arguments and name/address of function pointer
81 os << "[ function=" << func << " " ;
82 for (Int_t i=0 ; i<numProxies() ; i++) {
83 RooAbsProxy* p = getProxy(i) ;
84 if (!TString(p->name()).BeginsWith("!")) {
85 p->print(os) ;
86 os << " " ;
87 }
88 }
89 os << "]" ;
90}
91
92////////////////////////////////////////////////////////////////////////////////
94 // Return value of embedded function using value of referenced variable x
95 for (std::size_t i=0 ; i<vars.size() ; i++) {
96 x[i] = static_cast<RooAbsReal*>(vars.at(i))->getVal();
97 }
98 return (*func)(x) ;
99}
100
101
102////////////////////////////////////////////////////////////////////////////////
103/// Create a RooFit PDF that makes `ftor` usable as a PDF in RooFit.
104/// \param name Name of the object.
105/// \param title Title (e.g. for plotting)
106/// \param ftor Functor instance to be evaluated and normalised.
107/// \param v RooFit variables to be passed to the function.
109 RooAbsPdf(name,title),
110 func(&ftor),
111 vars("vars","vars",this)
112{
113 // Check that function dimension and number of variables match
114 if (ftor.NDim()!=UInt_t(v.size())) {
115 coutE(InputArguments) << "RooFunctorPdfBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.size()
116 << ") does not match dimensionality of function (" << ftor.NDim() << ")" << endl ;
117 throw string("RooFunctor::ctor ERROR") ;
118 }
119 x = new double[func->NDim()] ;
120 vars.add(v) ;
121}
122
123////////////////////////////////////////////////////////////////////////////////
125 : RooAbsPdf(other, name), func(other.func), vars("vars", this, other.vars), x(new double[func->NDim()])
126{
127 // Copy constructor
128}
129
130////////////////////////////////////////////////////////////////////////////////
131void RooFunctorPdfBinding::printArgs(ostream& os) const {
132 // Print object arguments and name/address of function pointer
133 os << "[ function=" << func << " " ;
134 for (Int_t i=0 ; i<numProxies() ; i++) {
135 RooAbsProxy* p = getProxy(i) ;
136 if (!TString(p->name()).BeginsWith("!")) {
137 p->print(os) ;
138 os << " " ;
139 }
140 }
141 os << "]" ;
142}
143
144////////////////////////////////////////////////////////////////////////////////
146 // Return value of embedded function using value of referenced variable x
147 for (std::size_t i=0 ; i<vars.size() ; i++) {
148 x[i] = static_cast<RooAbsReal*>(vars.at(i))->getVal();
149 }
150 return (*func)(x) ;
151 }
152
153namespace RooFit {
154
156 return new RooFunctorBinding(name,name,ftor,vars) ;
157 }
158
159 RooAbsPdf* bindPdf(const char* name, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& vars) {
160 return new RooFunctorPdfBinding(name,name,ftor,vars) ;
161 }
162
163}
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:382
winID h TVirtualViewer3D TVirtualGLPainter p
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.
Storage_t::size_type size() const
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
Abstract interface for proxy classes.
Definition RooAbsProxy.h:37
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
RooFunctorBinding makes math functions from ROOT usable in RooFit.
const ROOT::Math::IBaseFunctionMultiDim * func
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
void printArgs(std::ostream &os) const override
Print object arguments, ie its proxies.
RooFunctorBinding()=default
RooFunctorPdfBinding makes math functions from ROOT usable as PDFs in RooFit.
void printArgs(std::ostream &os) const override
Print object arguments, ie its proxies.
RooFunctorPdfBinding()=default
const ROOT::Math::IBaseFunctionMultiDim * func
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:623
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)