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#ifndef __CINT__
89 std::map<std::string,VO (*)(VI1,VI2,VI3,VI4)> _ptrmap ; // Pointer-to-name map
90 std::map<VO (*)(VI1,VI2,VI3,VI4),std::string> _namemap ; // Name-to-pointer map
91 std::map<VO (*)(VI1,VI2,VI3,VI4),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
92#endif
93} ;
94
95
96
97template<class VO, class VI1, class VI2, class VI3, class VI4>
98class RooCFunction4Ref : public TObject {
99 public:
100 RooCFunction4Ref(VO (*ptr)(VI1,VI2,VI3,VI4)=nullptr) : _ptr(ptr) {
101 // Constructor of persistable function reference
102 } ;
103
104 VO operator()(VI1 x,VI2 y,VI3 z,VI4 w) const {
105 // Evaluate embedded function
106 return (*_ptr)(x,y,z,w) ;
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 private:
139
140 static VO dummyFunction(VI1,VI2,VI3,VI4) {
141 // Dummy function used when registered function was not
142 // found in un-persisting object
143 return 0 ;
144 }
145
146
147 typedef VO (*func_t)(VI1,VI2,VI3,VI4);
148 func_t _ptr; //! Pointer to embedded function
149
150 static RooCFunction4Map<VO,VI1,VI2,VI3,VI4>* _fmap ; // Pointer to mapping service object
151
152 ClassDefOverride(RooCFunction4Ref,1) // Persistable reference to C function pointer
153} ;
154
155// Define static member
156template<class VO, class VI1, class VI2, class VI3, class VI4>
158
159template<class VO, class VI1, class VI2, class VI3, class VI4>
161{
162 // Custom streamer for function pointer reference object. When writing,
163 // the function pointer is substituted by its registered name. When function
164 // is unregistered name 'UNKNOWN' is written and a warning is issues. When
165 // reading back, the embedded name is converted back to a function pointer
166 // using the mapping service. When name UNKNOWN is encountered a warning is
167 // issues and a dummy null function is substituted. When the registered function
168 // name can not be mapped to a function pointer an ERROR is issued and a pointer
169 // to the dummy null function is substituted
170
171 typedef ::RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> thisClass;
172
173 // Stream an object of class RooCFunction4Ref
174 if (R__b.IsReading()) {
175
176 UInt_t R__s, R__c;
177 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
178
179 // Read name from file
180 TString tmpName ;
181 tmpName.Streamer(R__b) ;
182
183 if (tmpName=="UNKNOWN" && R__v>0) {
184
185 coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
186 _ptr = dummyFunction ;
187
188 } else {
189
190 // Lookup pointer to C function with given name
191 _ptr = fmap().lookupPtr(tmpName.Data()) ;
192
193 if (_ptr==nullptr) {
194 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
195 << " but no such function is registered, object will not be functional" << std::endl ;
196 }
197 }
198
199
200 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
201
202 } else {
203
204 UInt_t R__c;
205 R__c = R__b.WriteVersion(thisClass::IsA(), true);
206
207 // Lookup name of reference C function
208 TString tmpName = fmap().lookupName(_ptr) ;
209 if (tmpName.Length()==0) {
210 coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("0x%zx",(size_t)_ptr)
211 << " written object will not be functional when read back" << std::endl ;
212 tmpName="UNKNOWN" ;
213 }
214
215 // Persist the name
216 tmpName.Streamer(R__b) ;
217
218 R__b.SetByteCount(R__c, true);
219
220 }
221}
222
223
224
225template<class VO,class VI1, class VI2, class VI3, class VI4>
227public:
229 // Default constructor
230 } ;
231 RooCFunction4Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w);
232 RooCFunction4Binding(const RooCFunction4Binding& other, const char* name=nullptr) ;
233 TObject* clone(const char* newname) const override { return new RooCFunction4Binding(*this,newname); }
234
235 void printArgs(std::ostream& os) const override {
236 // Print object arguments and name/address of function pointer
237 os << "[ function=" << func.name() << " " ;
238 for (Int_t i=0 ; i<numProxies() ; i++) {
239 RooAbsProxy* p = getProxy(i) ;
240 if (!TString(p->name()).BeginsWith("!")) {
241 p->print(os) ;
242 os << " " ;
243 }
244 }
245 os << "]" ;
246 }
247
248protected:
249
250 RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> func ; // Function pointer reference
251 RooRealProxy x ; // Argument reference
252 RooRealProxy y ; // Argument reference
253 RooRealProxy z ; // Argument reference
254 RooRealProxy w ; // 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,z,w) ;
259 }
260
261private:
262
263 ClassDefOverride(RooCFunction4Binding,1) // RooAbsReal binding to external C functions
264};
265
266
267template<class VO,class VI1, class VI2, class VI3, class VI4>
268RooCFunction4Binding<VO,VI1,VI2,VI3,VI4>::RooCFunction4Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4),
269 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w) :
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 z(func.argName(2),func.argName(2),this,_z),
275 w(func.argName(3),func.argName(3),this,_w)
276{
277 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
278 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
279 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
280 // a RooWorkspace
281}
282
283
284template<class VO,class VI1, class VI2, class VI3, class VI4>
286 RooAbsReal(other,name),
287 func(other.func),
288 x("x",this,other.x),
289 y("y",this,other.y),
290 z("z",this,other.z),
291 w("w",this,other.w)
292{
293 // Copy constructor
294}
295
296
297template<class VO,class VI1, class VI2, class VI3, class VI4>
299public:
301 // Default constructor
302 } ;
303 RooCFunction4PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w);
304 RooCFunction4PdfBinding(const RooCFunction4PdfBinding& other, const char* name=nullptr) ;
305 TObject* clone(const char* newname) const override { return new RooCFunction4PdfBinding(*this,newname); }
306
307 void printArgs(std::ostream& os) const override {
308 // Print object arguments and name/address of function pointer
309 os << "[ function=" << func.name() << " " ;
310 for (Int_t i=0 ; i<numProxies() ; i++) {
311 RooAbsProxy* p = getProxy(i) ;
312 if (!TString(p->name()).BeginsWith("!")) {
313 p->print(os) ;
314 os << " " ;
315 }
316 }
317 os << "]" ;
318 }
319
320protected:
321
322 RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> func ; // Function pointer reference
323 RooRealProxy x ; // Argument reference
324 RooRealProxy y ; // Argument reference
325 RooRealProxy z ; // Argument reference
326 RooRealProxy w ; // Argument reference
327
328 double evaluate() const override {
329 // Return value of embedded function using value of referenced variable x
330 return func(x,y,z,w) ;
331 }
332
333private:
334
335 ClassDefOverride(RooCFunction4PdfBinding,1) // RooAbsReal binding to external C functions
336};
337
338
339template<class VO,class VI1, class VI2, class VI3, class VI4>
340RooCFunction4PdfBinding<VO,VI1,VI2,VI3,VI4>::RooCFunction4PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4),
341 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w) :
342 RooAbsPdf(name,title),
343 func(_func),
344 x(func.argName(0),func.argName(0),this,_x),
345 y(func.argName(1),func.argName(1),this,_y),
346 z(func.argName(2),func.argName(2),this,_z),
347 w(func.argName(3),func.argName(3),this,_w)
348{
349 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
350 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
351 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
352 // a RooWorkspace
353}
354
355
356template<class VO,class VI1, class VI2, class VI3, class VI4>
358 RooAbsPdf(other,name),
359 func(other.func),
360 x("x",this,other.x),
361 y("y",this,other.y),
362 z("z",this,other.z),
363 w("w",this,other.w)
364{
365 // Copy constructor
366}
367
368#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: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
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: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(* 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)