Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooCFunction2Binding.h
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#ifndef ROOCFUNCTION2BINDING
14#define ROOCFUNCTION2BINDING
15
16#include "RooAbsReal.h"
17#include "RooAbsPdf.h"
18#include "RooRealProxy.h"
19#include "RooMsgService.h"
20
21#include "TBuffer.h"
22#include "TString.h"
23
24#include <string>
25#include <map>
26#include <vector>
27
28namespace RooFit {
29
35
36
42RooAbsPdf* bindPdf(const char* name,CFUNCD2DD func,RooAbsReal& x, RooAbsReal& y) ;
43RooAbsPdf* bindPdf(const char* name,CFUNCD2ID func,RooAbsReal& x, RooAbsReal& y) ;
44RooAbsPdf* bindPdf(const char* name,CFUNCD2UD func,RooAbsReal& x, RooAbsReal& y) ;
45RooAbsPdf* bindPdf(const char* name,CFUNCD2DI func,RooAbsReal& x, RooAbsReal& y) ;
46RooAbsPdf* bindPdf(const char* name,CFUNCD2II func,RooAbsReal& x, RooAbsReal& y) ;
47
48}
49
50template<class VO, class VI1, class VI2>
52 public:
54
55 void add(const char* name, VO (*ptr)(VI1,VI2), const char* arg1name="x", const char* arg2name="y") {
56 // Register function with given name and argument name
57 _ptrmap[name] = ptr ;
58 _namemap[ptr] = name ;
59 _argnamemap[ptr].push_back(arg1name) ;
60 _argnamemap[ptr].push_back(arg2name) ;
61 }
62
63
64 const char* lookupName(VO (*ptr)(VI1,VI2)) {
65 // Return name of function given by pointer
66 return _namemap[ptr].c_str() ;
67 }
68
69 VO (*lookupPtr(const char* name))(VI1,VI2) {
70 // Return pointer of function given by name
71 return _ptrmap[name] ;
72 }
73
74 const char* lookupArgName(VO (*ptr)(VI1,VI2), UInt_t iarg) {
75 // Return name of i-th argument of function. If function is
76 // not registered, argument names 0,1,2 are x,y,z
77 if (iarg<_argnamemap[ptr].size()) {
78 return (_argnamemap[ptr])[iarg].c_str() ;
79 }
80 switch (iarg) {
81 case 0: return "x" ;
82 case 1: return "y" ;
83 case 2: return "z" ;
84 }
85 return "w" ;
86 }
87
88 private:
89
90#ifndef __CINT__
91 std::map<std::string,VO (*)(VI1,VI2)> _ptrmap ; // Pointer-to-name map
92 std::map<VO (*)(VI1,VI2),std::string> _namemap ; // Name-to-pointer map
93 std::map<VO (*)(VI1,VI2),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
94#endif
95} ;
96
97
98
99template<class VO, class VI1, class VI2>
100class RooCFunction2Ref : public TObject {
101 public:
102 RooCFunction2Ref(VO (*ptr)(VI1,VI2)=nullptr) : _ptr(ptr) {
103 // Constructor of persistable function reference
104 } ;
105
106 VO operator()(VI1 x,VI2 y) const {
107 // Evaluate embedded function
108 return (*_ptr)(x,y) ;
109 }
110
111 const char* name() const {
112 // Return registered name of embedded function. If function
113 // is not registered return string with hex presentation
114 // of function pointer value
115 const char* result = fmap().lookupName(_ptr) ;
116 if (result && strlen(result)) {
117 return result ;
118 }
119 // This union is to avoid a warning message:
120 union {
121 void *_ptr;
122 func_t _funcptr;
123 } temp;
124 temp._funcptr = _ptr;
125 return Form("(%p)",temp._ptr) ;
126 }
127
128 const char* argName(Int_t iarg) {
129 // Return suggested name for i-th argument
130 return fmap().lookupArgName(_ptr,iarg) ;
131 }
132
134 // Return reference to function pointer-to-name mapping service
135 if (!_fmap) {
137 }
138 return *_fmap ;
139 }
140
141 private:
142
143 static VO dummyFunction(VI1,VI2) {
144 // Dummy function used when registered function was not
145 // found in un-persisting object
146 return 0 ;
147 }
148
149 typedef VO (*func_t)(VI1,VI2);
150 func_t _ptr; //! Pointer to embedded function
151
152 static RooCFunction2Map<VO,VI1,VI2>* _fmap ; // Pointer to mapping service object
153
154 ClassDefOverride(RooCFunction2Ref,1) // Persistable reference to C function pointer
155} ;
156
157// Define static member
158template<class VO, class VI1, class VI2>
160
161
162template<class VO, class VI1, class VI2>
164{
165 // Custom streamer for function pointer reference object. When writing,
166 // the function pointer is substituted by its registered name. When function
167 // is unregistered name 'UNKNOWN' is written and a warning is issues. When
168 // reading back, the embedded name is converted back to a function pointer
169 // using the mapping service. When name UNKNOWN is encountered a warning is
170 // issues and a dummy null function is substituted. When the registered function
171 // name can not be mapped to a function pointer an ERROR is issued and a pointer
172 // to the dummy null function is substituted
173
174 typedef ::RooCFunction2Ref<VO,VI1,VI2> thisClass;
175
176 // Stream an object of class RooCFunction2Ref
177 if (R__b.IsReading()) {
178
179 UInt_t R__s, R__c;
180 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
181
182 // Read name from file
183 TString tmpName ;
184 tmpName.Streamer(R__b) ;
185
186 if (tmpName=="UNKNOWN" && R__v>0) {
187
188 coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
189 _ptr = dummyFunction ;
190
191 } else {
192
193 // Lookup pointer to C function with given name
194 _ptr = fmap().lookupPtr(tmpName.Data()) ;
195
196 if (_ptr==nullptr) {
197 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
198 << " but no such function is registered, object will not be functional" << std::endl ;
199 }
200 }
201
202
203 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
204
205 } else {
206
207 UInt_t R__c;
208 R__c = R__b.WriteVersion(thisClass::IsA(), true);
209
210 // Lookup name of reference C function
211 TString tmpName = fmap().lookupName(_ptr) ;
212 if (tmpName.Length()==0) {
213 coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("0x%zx", (size_t)_ptr)
214 << " written object will not be functional when read back" << std::endl ;
215 tmpName="UNKNOWN" ;
216 }
217
218 // Persist the name
219 tmpName.Streamer(R__b) ;
220
221 R__b.SetByteCount(R__c, true);
222
223 }
224}
225
226
227
228template<class VO,class VI1, class VI2>
230public:
232 // Default constructor
233 } ;
234 RooCFunction2Binding(const char *name, const char *title, VO (*_func)(VI1,VI2), RooAbsReal& _x, RooAbsReal& _y);
235 RooCFunction2Binding(const RooCFunction2Binding& other, const char* name=nullptr) ;
236 TObject* clone(const char* newname) const override { return new RooCFunction2Binding(*this,newname); }
237
238 void printArgs(std::ostream& os) const override {
239 // Print object arguments and name/address of function pointer
240 os << "[ function=" << func.name() << " " ;
241 for (Int_t i=0 ; i<numProxies() ; i++) {
242 RooAbsProxy* p = getProxy(i) ;
243 if (!TString(p->name()).BeginsWith("!")) {
244 p->print(os) ;
245 os << " " ;
246 }
247 }
248 os << "]" ;
249 }
250
251protected:
252
253 RooCFunction2Ref<VO,VI1,VI2> func ; // Function pointer reference
254 RooRealProxy x ; // Argument reference
255 RooRealProxy y ; // Argument reference
256
257 double evaluate() const override {
258 // Return value of embedded function using value of referenced variable x
259 return func(x,y) ;
260 }
261
262private:
263
264 ClassDefOverride(RooCFunction2Binding,1) // RooAbsReal binding to external C functions
265};
266
267template<class VO,class VI1, class VI2>
268RooCFunction2Binding<VO,VI1,VI2>::RooCFunction2Binding(const char *name, const char *title, VO (*_func)(VI1,VI2),
269 RooAbsReal& _x, RooAbsReal& _y) :
270 RooAbsReal(name,title),
271 func(_func),
272 x(func.argName(0),func.argName(0),this,_x),
273 y(func.argName(1),func.argName(1),this,_y)
274{
275 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
276 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
277 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
278 // a RooWorkspace
279}
280
281
282template<class VO,class VI1, class VI2>
284 RooAbsReal(other,name),
285 func(other.func),
286 x("x",this,other.x),
287 y("y",this,other.y)
288{
289 // Copy constructor
290}
291
292
293
294
295template<class VO,class VI1, class VI2>
297public:
299 // Default constructor
300 } ;
301 RooCFunction2PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2), RooAbsReal& _x, RooAbsReal& _y);
302 RooCFunction2PdfBinding(const RooCFunction2PdfBinding& other, const char* name=nullptr) ;
303 TObject* clone(const char* newname) const override { return new RooCFunction2PdfBinding(*this,newname); }
304
305 void printArgs(std::ostream& os) const override {
306 // Print object arguments and name/address of function pointer
307 os << "[ function=" << func.name() << " " ;
308 for (Int_t i=0 ; i<numProxies() ; i++) {
309 RooAbsProxy* p = getProxy(i) ;
310 if (!TString(p->name()).BeginsWith("!")) {
311 p->print(os) ;
312 os << " " ;
313 }
314 }
315 os << "]" ;
316 }
317
318protected:
319
320 RooCFunction2Ref<VO,VI1,VI2> func ; // Function pointer reference
321 RooRealProxy x ; // Argument reference
322 RooRealProxy y ; // Argument reference
323
324 double evaluate() const override {
325 // Return value of embedded function using value of referenced variable x
326 return func(x,y) ;
327 }
328
329private:
330
331 ClassDefOverride(RooCFunction2PdfBinding,1) // RooAbsReal binding to external C functions
332};
333
334template<class VO,class VI1, class VI2>
335RooCFunction2PdfBinding<VO,VI1,VI2>::RooCFunction2PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2),
336 RooAbsReal& _x, RooAbsReal& _y) :
337 RooAbsPdf(name,title),
338 func(_func),
339 x(func.argName(0),func.argName(0),this,_x),
340 y(func.argName(1),func.argName(1),this,_y)
341{
342 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
343 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
344 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
345 // a RooWorkspace
346}
347
348
349template<class VO,class VI1, class VI2>
351 RooAbsPdf(other,name),
352 func(other.func),
353 x("x",this,other.x),
354 y("y",this,other.y)
355{
356 // Copy constructor
357}
358
359#endif
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define coutW(a)
int Int_t
Definition RtypesCore.h:45
short Version_t
Definition RtypesCore.h:65
unsigned int UInt_t
Definition RtypesCore.h:46
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
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:40
RooAbsProxy is the 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
RooCFunction2Binding is a templated implementation of class RooAbsReal that binds generic C(++) funct...
RooCFunction2Ref< VO, VI1, VI2 > func
void printArgs(std::ostream &os) const override
Print object arguments, ie its proxies.
TObject * clone(const char *newname) const override
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
std::map< VO(*)(VI1, VI2), std::vector< std::string > > _argnamemap
std::map< std::string, VO(*)(VI1, VI2)> _ptrmap
std::map< VO(*)(VI1, VI2), std::string > _namemap
void add(const char *name, VO(*ptr)(VI1, VI2), const char *arg1name="x", const char *arg2name="y")
VO(*)(VI1, VI2) lookupPtr(const char *name)
const char * lookupArgName(VO(*ptr)(VI1, VI2), UInt_t iarg)
const char * lookupName(VO(*ptr)(VI1, VI2))
TObject * clone(const char *newname) const override
void printArgs(std::ostream &os) const override
Print object arguments, ie its proxies.
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
RooCFunction2Ref< VO, VI1, VI2 > func
const char * argName(Int_t iarg)
VO operator()(VI1 x, VI2 y) const
RooCFunction2Ref(VO(*ptr)(VI1, VI2)=nullptr)
static RooCFunction2Map< VO, VI1, VI2 > & fmap()
static VO dummyFunction(VI1, VI2)
void Streamer(TBuffer &) override
Stream an object of class TObject.
const char * name() const
static RooCFunction2Map< VO, VI1, VI2 > * _fmap
Pointer to embedded function.
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
Mother of all ROOT objects.
Definition TObject.h:41
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:421
const char * Data() const
Definition TString.h:380
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:627
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1390
Double_t y[n]
Definition legend1.C:17
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
double(* CFUNCD2ID)(Int_t, double)
double(* CFUNCD2DD)(double, double)
double(* CFUNCD2DI)(double, Int_t)
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)
double(* CFUNCD2UD)(UInt_t, double)
double(* CFUNCD2II)(Int_t, Int_t)