Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooCFunction4Binding.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 ROOCFUNCTION4BINDING
14#define ROOCFUNCTION4BINDING
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
33
40
41}
42
43
44template<class VO, class VI1, class VI2, class VI3, class VI4>
46 public:
48
49 void add(const char* name, VO (*ptr)(VI1,VI2,VI3,VI4), const char* arg1name="x", const char* arg2name="y",
50 const char* arg3name="z", const char* arg4name="w") {
51 // Register function with given name and argument name
52 _ptrmap[name] = ptr ;
53 _namemap[ptr] = name ;
54 _argnamemap[ptr].push_back(arg1name) ;
55 _argnamemap[ptr].push_back(arg2name) ;
56 _argnamemap[ptr].push_back(arg3name) ;
57 _argnamemap[ptr].push_back(arg4name) ;
58 }
59
60
61 const char* lookupName(VO (*ptr)(VI1,VI2,VI3,VI4)) {
62 // Return name of function given by pointer
63 return _namemap[ptr].c_str() ;
64 }
65
66 VO (*lookupPtr(const char* name))(VI1,VI2,VI3,VI4) {
67 // Return pointer of function given by name
68 return _ptrmap[name] ;
69 }
70
71 const char* lookupArgName(VO (*ptr)(VI1,VI2,VI3,VI4), UInt_t iarg) {
72 // Return name of i-th argument of function. If function is
73 // not registered, argument names 0,1,2 are x,y,z
74 if (iarg<_argnamemap[ptr].size()) {
75 return (_argnamemap[ptr])[iarg].c_str() ;
76 }
77 switch (iarg) {
78 case 0: return "x" ;
79 case 1: return "y" ;
80 case 2: return "z" ;
81 case 3: return "w" ;
82 }
83 return "v" ;
84 }
85
86 private:
87
88 std::map<std::string,VO (*)(VI1,VI2,VI3,VI4)> _ptrmap ; // Pointer-to-name map
89 std::map<VO (*)(VI1,VI2,VI3,VI4),std::string> _namemap ; // Name-to-pointer map
90 std::map<VO (*)(VI1,VI2,VI3,VI4),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
91} ;
92
93
94
95template<class VO, class VI1, class VI2, class VI3, class VI4>
96class RooCFunction4Ref : public TObject {
97 public:
98 RooCFunction4Ref(VO (*ptr)(VI1,VI2,VI3,VI4)=nullptr) : _ptr(ptr) {
99 // Constructor of persistable function reference
100 } ;
101
102 VO operator()(VI1 x,VI2 y,VI3 z,VI4 w) const {
103 // Evaluate embedded function
104 return (*_ptr)(x,y,z,w) ;
105 }
106
107 const char* name() const {
108 // Return registered name of embedded function. If function
109 // is not registered return string with hex presentation
110 // of function pointer value
111 const char* result = fmap().lookupName(_ptr) ;
112 if (result && strlen(result)) {
113 return result ;
114 }
115 // This union is to avoid a warning message:
116 union {
117 void *_ptr;
118 func_t _funcptr;
119 } temp;
120 temp._funcptr = _ptr;
121 return Form("(%p)",temp._ptr) ;
122 }
123
124 const char* argName(Int_t iarg) {
125 // Return suggested name for i-th argument
126 return fmap().lookupArgName(_ptr,iarg) ;
127 }
128
130 // Return reference to function pointer-to-name mapping service
131 if (!_fmap) {
133 }
134 return *_fmap ;
135 }
136 private:
137
138 static VO dummyFunction(VI1,VI2,VI3,VI4) {
139 // Dummy function used when registered function was not
140 // found in un-persisting object
141 return 0 ;
142 }
143
144
145 typedef VO (*func_t)(VI1,VI2,VI3,VI4);
146 func_t _ptr; //! Pointer to embedded function
147
148 static RooCFunction4Map<VO,VI1,VI2,VI3,VI4>* _fmap ; // Pointer to mapping service object
149
150 ClassDefOverride(RooCFunction4Ref,1) // Persistable reference to C function pointer
151} ;
152
153// Define static member
154template<class VO, class VI1, class VI2, class VI3, class VI4>
156
157template<class VO, class VI1, class VI2, class VI3, class VI4>
159{
160 // Custom streamer for function pointer reference object. When writing,
161 // the function pointer is substituted by its registered name. When function
162 // is unregistered name 'UNKNOWN' is written and a warning is issues. When
163 // reading back, the embedded name is converted back to a function pointer
164 // using the mapping service. When name UNKNOWN is encountered a warning is
165 // issues and a dummy null function is substituted. When the registered function
166 // name can not be mapped to a function pointer an ERROR is issued and a pointer
167 // to the dummy null function is substituted
168
169 typedef ::RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> thisClass;
170
171 // Stream an object of class RooCFunction4Ref
172 if (R__b.IsReading()) {
173
174 UInt_t R__s;
175 UInt_t R__c;
176 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
177
178 // Read name from file
179 TString tmpName ;
180 tmpName.Streamer(R__b) ;
181
182 if (tmpName=="UNKNOWN" && R__v>0) {
183
184 coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
185 _ptr = dummyFunction ;
186
187 } else {
188
189 // Lookup pointer to C function with given name
190 _ptr = fmap().lookupPtr(tmpName.Data()) ;
191
192 if (_ptr==nullptr) {
193 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
194 << " but no such function is registered, object will not be functional" << std::endl ;
195 }
196 }
197
198
199 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
200
201 } else {
202
203 UInt_t R__c;
204 R__c = R__b.WriteVersion(thisClass::IsA(), true);
205
206 // Lookup name of reference C function
207 TString tmpName = fmap().lookupName(_ptr) ;
208 if (tmpName.Length()==0) {
209 coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("0x%zx",(size_t)_ptr)
210 << " written object will not be functional when read back" << std::endl ;
211 tmpName="UNKNOWN" ;
212 }
213
214 // Persist the name
215 tmpName.Streamer(R__b) ;
216
217 R__b.SetByteCount(R__c, true);
218
219 }
220}
221
222
223
224template<class VO,class VI1, class VI2, class VI3, class VI4>
226public:
228 // Default constructor
229 } ;
230 RooCFunction4Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w);
231 RooCFunction4Binding(const RooCFunction4Binding& other, const char* name=nullptr) ;
232 TObject* clone(const char* newname) const override { return new RooCFunction4Binding(*this,newname); }
233
234 void printArgs(std::ostream& os) const override {
235 // Print object arguments and name/address of function pointer
236 os << "[ function=" << func.name() << " " ;
237 for (Int_t i=0 ; i<numProxies() ; i++) {
238 RooAbsProxy* p = getProxy(i) ;
239 if (!TString(p->name()).BeginsWith("!")) {
240 p->print(os) ;
241 os << " " ;
242 }
243 }
244 os << "]" ;
245 }
246
247protected:
248
249 RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> func ; // Function pointer reference
250 RooRealProxy x ; // Argument reference
251 RooRealProxy y ; // Argument reference
252 RooRealProxy z ; // Argument reference
253 RooRealProxy w ; // Argument reference
254
255 double evaluate() const override {
256 // Return value of embedded function using value of referenced variable x
257 return func(x,y,z,w) ;
258 }
259
260private:
261
262 ClassDefOverride(RooCFunction4Binding,1) // RooAbsReal binding to external C functions
263};
264
265
266template<class VO,class VI1, class VI2, class VI3, class VI4>
267RooCFunction4Binding<VO,VI1,VI2,VI3,VI4>::RooCFunction4Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4),
268 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w) :
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 z(func.argName(2),func.argName(2),this,_z),
274 w(func.argName(3),func.argName(3),this,_w)
275{
276 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
277 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
278 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
279 // a RooWorkspace
280}
281
282
283template<class VO,class VI1, class VI2, class VI3, class VI4>
285 RooAbsReal(other,name),
286 func(other.func),
287 x("x",this,other.x),
288 y("y",this,other.y),
289 z("z",this,other.z),
290 w("w",this,other.w)
291{
292 // Copy constructor
293}
294
295
296template<class VO,class VI1, class VI2, class VI3, class VI4>
298public:
300 // Default constructor
301 } ;
302 RooCFunction4PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w);
303 RooCFunction4PdfBinding(const RooCFunction4PdfBinding& other, const char* name=nullptr) ;
304 TObject* clone(const char* newname) const override { return new RooCFunction4PdfBinding(*this,newname); }
305
306 void printArgs(std::ostream& os) const override {
307 // Print object arguments and name/address of function pointer
308 os << "[ function=" << func.name() << " " ;
309 for (Int_t i=0 ; i<numProxies() ; i++) {
310 RooAbsProxy* p = getProxy(i) ;
311 if (!TString(p->name()).BeginsWith("!")) {
312 p->print(os) ;
313 os << " " ;
314 }
315 }
316 os << "]" ;
317 }
318
319protected:
320
321 RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> func ; // Function pointer reference
322 RooRealProxy x ; // Argument reference
323 RooRealProxy y ; // Argument reference
324 RooRealProxy z ; // Argument reference
325 RooRealProxy w ; // Argument reference
326
327 double evaluate() const override {
328 // Return value of embedded function using value of referenced variable x
329 return func(x,y,z,w) ;
330 }
331
332private:
333
334 ClassDefOverride(RooCFunction4PdfBinding,1) // RooAbsReal binding to external C functions
335};
336
337
338template<class VO,class VI1, class VI2, class VI3, class VI4>
339RooCFunction4PdfBinding<VO,VI1,VI2,VI3,VI4>::RooCFunction4PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4),
340 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w) :
341 RooAbsPdf(name,title),
342 func(_func),
343 x(func.argName(0),func.argName(0),this,_x),
344 y(func.argName(1),func.argName(1),this,_y),
345 z(func.argName(2),func.argName(2),this,_z),
346 w(func.argName(3),func.argName(3),this,_w)
347{
348 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
349 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
350 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
351 // a RooWorkspace
352}
353
354
355template<class VO,class VI1, class VI2, class VI3, class VI4>
357 RooAbsPdf(other,name),
358 func(other.func),
359 x("x",this,other.x),
360 y("y",this,other.y),
361 z("z",this,other.z),
362 w("w",this,other.w)
363{
364 // Copy constructor
365}
366
367#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
#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
RooCFunction4Binding is a templated implementation of class RooAbsReal that binds generic C(++) funct...
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.
RooCFunction4Ref< VO, VI1, VI2, VI3, VI4 > func
TObject * clone(const char *newname) const override
VO(*)(VI1, VI2, VI3, VI4) lookupPtr(const char *name)
std::map< std::string, VO(*)(VI1, VI2, VI3, VI4)> _ptrmap
const char * lookupName(VO(*ptr)(VI1, VI2, VI3, VI4))
void add(const char *name, VO(*ptr)(VI1, VI2, VI3, VI4), const char *arg1name="x", const char *arg2name="y", const char *arg3name="z", const char *arg4name="w")
const char * lookupArgName(VO(*ptr)(VI1, VI2, VI3, VI4), UInt_t iarg)
std::map< VO(*)(VI1, VI2, VI3, VI4), std::vector< std::string > > _argnamemap
std::map< VO(*)(VI1, VI2, VI3, VI4), std::string > _namemap
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.
TObject * clone(const char *newname) const override
RooCFunction4Ref< VO, VI1, VI2, VI3, VI4 > func
static RooCFunction4Map< VO, VI1, VI2, VI3, VI4 > * _fmap
Pointer to embedded function.
RooCFunction4Ref(VO(*ptr)(VI1, VI2, VI3, VI4)=nullptr)
void Streamer(TBuffer &) override
Stream an object of class TObject.
const char * argName(Int_t iarg)
const char * name() const
VO(* func_t)(VI1, VI2, VI3, VI4)
VO operator()(VI1 x, VI2 y, VI3 z, VI4 w) const
static RooCFunction4Map< VO, VI1, VI2, VI3, VI4 > & fmap()
static VO dummyFunction(VI1, VI2, VI3, VI4)
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 JSONIO.h:26
double(* CFUNCD4DDDB)(double, double, double, bool)
double(* CFUNCD4DDDD)(double, double, double, double)
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
double(* CFUNCD4DDDI)(double, double, double, Int_t)
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)