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 std::map<std::string,VO (*)(VI1,VI2)> _ptrmap ; // Pointer-to-name map
91 std::map<VO (*)(VI1,VI2),std::string> _namemap ; // Name-to-pointer map
92 std::map<VO (*)(VI1,VI2),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
93} ;
94
95
96
97template<class VO, class VI1, class VI2>
98class RooCFunction2Ref : public TObject {
99 public:
100 RooCFunction2Ref(VO (*ptr)(VI1,VI2)=nullptr) : _ptr(ptr) {
101 // Constructor of persistable function reference
102 } ;
103
104 VO operator()(VI1 x,VI2 y) const {
105 // Evaluate embedded function
106 return (*_ptr)(x,y) ;
107 }
108
109 const char* name() const {
110 // Return registered name of embedded function. If function
111 // is not registered return string with hex presentation
112 // of function pointer value
113 const char* result = fmap().lookupName(_ptr) ;
114 if (result && strlen(result)) {
115 return result ;
116 }
117 // This union is to avoid a warning message:
118 union {
119 void *_ptr;
120 func_t _funcptr;
121 } temp;
122 temp._funcptr = _ptr;
123 return Form("(%p)",temp._ptr) ;
124 }
125
126 const char* argName(Int_t iarg) {
127 // Return suggested name for i-th argument
128 return fmap().lookupArgName(_ptr,iarg) ;
129 }
130
132 // Return reference to function pointer-to-name mapping service
133 if (!_fmap) {
135 }
136 return *_fmap ;
137 }
138
139 private:
140
141 static VO dummyFunction(VI1,VI2) {
142 // Dummy function used when registered function was not
143 // found in un-persisting object
144 return 0 ;
145 }
146
147 typedef VO (*func_t)(VI1,VI2);
148 func_t _ptr; //! Pointer to embedded function
149
150 static RooCFunction2Map<VO,VI1,VI2>* _fmap ; // Pointer to mapping service object
151
152 ClassDefOverride(RooCFunction2Ref,1) // Persistable reference to C function pointer
153} ;
154
155// Define static member
156template<class VO, class VI1, class VI2>
158
159
160template<class VO, class VI1, class VI2>
162{
163 // Custom streamer for function pointer reference object. When writing,
164 // the function pointer is substituted by its registered name. When function
165 // is unregistered name 'UNKNOWN' is written and a warning is issues. When
166 // reading back, the embedded name is converted back to a function pointer
167 // using the mapping service. When name UNKNOWN is encountered a warning is
168 // issues and a dummy null function is substituted. When the registered function
169 // name can not be mapped to a function pointer an ERROR is issued and a pointer
170 // to the dummy null function is substituted
171
172 typedef ::RooCFunction2Ref<VO,VI1,VI2> thisClass;
173
174 // Stream an object of class RooCFunction2Ref
175 if (R__b.IsReading()) {
176
177 UInt_t R__s;
178 UInt_t R__c;
179 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
180
181 // Read name from file
182 TString tmpName ;
183 tmpName.Streamer(R__b) ;
184
185 if (tmpName=="UNKNOWN" && R__v>0) {
186
187 coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
188 _ptr = dummyFunction ;
189
190 } else {
191
192 // Lookup pointer to C function with given name
193 _ptr = fmap().lookupPtr(tmpName.Data()) ;
194
195 if (_ptr==nullptr) {
196 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
197 << " but no such function is registered, object will not be functional" << std::endl ;
198 }
199 }
200
201
202 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
203
204 } else {
205
206 UInt_t R__c;
207 R__c = R__b.WriteVersion(thisClass::IsA(), true);
208
209 // Lookup name of reference C function
210 TString tmpName = fmap().lookupName(_ptr) ;
211 if (tmpName.Length()==0) {
212 coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("0x%zx", (size_t)_ptr)
213 << " written object will not be functional when read back" << std::endl ;
214 tmpName="UNKNOWN" ;
215 }
216
217 // Persist the name
218 tmpName.Streamer(R__b) ;
219
220 R__b.SetByteCount(R__c, true);
221
222 }
223}
224
225
226
227template<class VO,class VI1, class VI2>
229public:
231 // Default constructor
232 } ;
233 RooCFunction2Binding(const char *name, const char *title, VO (*_func)(VI1,VI2), RooAbsReal& _x, RooAbsReal& _y);
234 RooCFunction2Binding(const RooCFunction2Binding& other, const char* name=nullptr) ;
235 TObject* clone(const char* newname) const override { return new RooCFunction2Binding(*this,newname); }
236
237 void printArgs(std::ostream& os) const override {
238 // Print object arguments and name/address of function pointer
239 os << "[ function=" << func.name() << " " ;
240 for (Int_t i=0 ; i<numProxies() ; i++) {
241 RooAbsProxy* p = getProxy(i) ;
242 if (!TString(p->name()).BeginsWith("!")) {
243 p->print(os) ;
244 os << " " ;
245 }
246 }
247 os << "]" ;
248 }
249
250protected:
251
252 RooCFunction2Ref<VO,VI1,VI2> func ; // Function pointer reference
253 RooRealProxy x ; // Argument reference
254 RooRealProxy y ; // Argument reference
255
256 double evaluate() const override {
257 // Return value of embedded function using value of referenced variable x
258 return func(x,y) ;
259 }
260
261private:
262
263 ClassDefOverride(RooCFunction2Binding,1) // RooAbsReal binding to external C functions
264};
265
266template<class VO,class VI1, class VI2>
267RooCFunction2Binding<VO,VI1,VI2>::RooCFunction2Binding(const char *name, const char *title, VO (*_func)(VI1,VI2),
268 RooAbsReal& _x, RooAbsReal& _y) :
269 RooAbsReal(name,title),
270 func(_func),
271 x(func.argName(0),func.argName(0),this,_x),
272 y(func.argName(1),func.argName(1),this,_y)
273{
274 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
275 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
276 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
277 // a RooWorkspace
278}
279
280
281template<class VO,class VI1, class VI2>
283 RooAbsReal(other,name),
284 func(other.func),
285 x("x",this,other.x),
286 y("y",this,other.y)
287{
288 // Copy constructor
289}
290
291
292
293
294template<class VO,class VI1, class VI2>
296public:
298 // Default constructor
299 } ;
300 RooCFunction2PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2), RooAbsReal& _x, RooAbsReal& _y);
301 RooCFunction2PdfBinding(const RooCFunction2PdfBinding& other, const char* name=nullptr) ;
302 TObject* clone(const char* newname) const override { return new RooCFunction2PdfBinding(*this,newname); }
303
304 void printArgs(std::ostream& os) const override {
305 // Print object arguments and name/address of function pointer
306 os << "[ function=" << func.name() << " " ;
307 for (Int_t i=0 ; i<numProxies() ; i++) {
308 RooAbsProxy* p = getProxy(i) ;
309 if (!TString(p->name()).BeginsWith("!")) {
310 p->print(os) ;
311 os << " " ;
312 }
313 }
314 os << "]" ;
315 }
316
317protected:
318
319 RooCFunction2Ref<VO,VI1,VI2> func ; // Function pointer reference
320 RooRealProxy x ; // Argument reference
321 RooRealProxy y ; // Argument reference
322
323 double evaluate() const override {
324 // Return value of embedded function using value of referenced variable x
325 return func(x,y) ;
326 }
327
328private:
329
330 ClassDefOverride(RooCFunction2PdfBinding,1) // RooAbsReal binding to external C functions
331};
332
333template<class VO,class VI1, class VI2>
334RooCFunction2PdfBinding<VO,VI1,VI2>::RooCFunction2PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2),
335 RooAbsReal& _x, RooAbsReal& _y) :
336 RooAbsPdf(name,title),
337 func(_func),
338 x(func.argName(0),func.argName(0),this,_x),
339 y(func.argName(1),func.argName(1),this,_y)
340{
341 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
342 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
343 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
344 // a RooWorkspace
345}
346
347
348template<class VO,class VI1, class VI2>
350 RooAbsPdf(other,name),
351 func(other.func),
352 x("x",this,other.x),
353 y("y",this,other.y)
354{
355 // Copy constructor
356}
357
358#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:346
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:2489
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
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:417
const char * Data() const
Definition TString.h:376
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:623
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1412
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 CodegenImpl.h:64
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)