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
47
48////////////////////////////////////////////////////////////////////////////////
49/// Create a RooFit function that makes `ftor` usable in RooFit.
50/// \param name Name of the object.
51/// \param title Title (e.g. for plotting)
52/// \param ftor Functor instance to be evaluated.
53/// \param v RooFit variables to be passed to the function.
54RooFunctorBinding::RooFunctorBinding(const char *name, const char *title, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& v) :
55 RooAbsReal(name,title),
56 func(&ftor),
57 vars("vars","vars",this)
58{
59 // Check that function dimension and number of variables match
60 if (ftor.NDim()!=UInt_t(v.size())) {
61 coutE(InputArguments) << "RooFunctorBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.size()
62 << ") does not match dimensionality of function (" << ftor.NDim() << ")" << std::endl ;
63 throw string("RooFunctor::ctor ERROR") ;
64 }
65 x = new double[func->NDim()] ;
66 vars.add(v) ;
67}
68
69////////////////////////////////////////////////////////////////////////////////
71 : RooAbsReal(other, name), func(other.func), vars("vars", this, other.vars), x(new double[func->NDim()])
72{
73 // Copy constructor
74}
75
76////////////////////////////////////////////////////////////////////////////////
77void RooFunctorBinding::printArgs(ostream& os) const {
78 // Print object arguments and name/address of function pointer
79 os << "[ function=" << func << " " ;
80 for (Int_t i=0 ; i<numProxies() ; i++) {
81 RooAbsProxy* p = getProxy(i) ;
82 if (!TString(p->name()).BeginsWith("!")) {
83 p->print(os) ;
84 os << " " ;
85 }
86 }
87 os << "]" ;
88}
89
90////////////////////////////////////////////////////////////////////////////////
92 // Return value of embedded function using value of referenced variable x
93 for (std::size_t i=0 ; i<vars.size() ; i++) {
94 x[i] = static_cast<RooAbsReal*>(vars.at(i))->getVal();
95 }
96 return (*func)(x) ;
97}
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Create a RooFit PDF that makes `ftor` usable as a PDF in RooFit.
102/// \param name Name of the object.
103/// \param title Title (e.g. for plotting)
104/// \param ftor Functor instance to be evaluated and normalised.
105/// \param v RooFit variables to be passed to the function.
107 RooAbsPdf(name,title),
108 func(&ftor),
109 vars("vars","vars",this)
110{
111 // Check that function dimension and number of variables match
112 if (ftor.NDim()!=UInt_t(v.size())) {
113 coutE(InputArguments) << "RooFunctorPdfBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.size()
114 << ") does not match dimensionality of function (" << ftor.NDim() << ")" << std::endl ;
115 throw string("RooFunctor::ctor ERROR") ;
116 }
117 x = new double[func->NDim()] ;
118 vars.add(v) ;
119}
120
121////////////////////////////////////////////////////////////////////////////////
123 : RooAbsPdf(other, name), func(other.func), vars("vars", this, other.vars), x(new double[func->NDim()])
124{
125 // Copy constructor
126}
127
128////////////////////////////////////////////////////////////////////////////////
129void RooFunctorPdfBinding::printArgs(ostream& os) const {
130 // Print object arguments and name/address of function pointer
131 os << "[ function=" << func << " " ;
132 for (Int_t i=0 ; i<numProxies() ; i++) {
133 RooAbsProxy* p = getProxy(i) ;
134 if (!TString(p->name()).BeginsWith("!")) {
135 p->print(os) ;
136 os << " " ;
137 }
138 }
139 os << "]" ;
140}
141
142////////////////////////////////////////////////////////////////////////////////
144 // Return value of embedded function using value of referenced variable x
145 for (std::size_t i=0 ; i<vars.size() ; i++) {
146 x[i] = static_cast<RooAbsReal*>(vars.at(i))->getVal();
147 }
148 return (*func)(x) ;
149 }
150
151namespace RooFit {
152
154 return new RooFunctorBinding(name,name,ftor,vars) ;
155 }
156
157 RooAbsPdf* bindPdf(const char* name, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& vars) {
158 return new RooFunctorPdfBinding(name,name,ftor,vars) ;
159 }
160
161}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
char name[80]
Definition TGX11.cxx:148
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.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
RooAbsPdf()
Default constructor.
friend class RooAbsReal
Definition RooAbsPdf.h:342
Abstract interface for proxy classes.
Definition RooAbsProxy.h:37
virtual void print(std::ostream &os, bool addContents=false) const
Print proxy name.
virtual const char * name() const
Definition RooAbsProxy.h:47
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
RooAbsReal()
coverity[UNINIT_CTOR] Default constructor
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
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.
Basic string class.
Definition TString.h:138
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:632
IBaseFunctionMultiDimTempl< double > IBaseFunctionMultiDim
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:72
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)