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 ~RooCFunction4Ref() override {} ;
104
105 VO operator()(VI1 x,VI2 y,VI3 z,VI4 w) const {
106 // Evaluate embedded function
107 return (*_ptr)(x,y,z,w) ;
108 }
109
110 const char* name() const {
111 // Return registered name of embedded function. If function
112 // is not registered return string with hex presentation
113 // of function pointer value
114 const char* result = fmap().lookupName(_ptr) ;
115 if (result && strlen(result)) {
116 return result ;
117 }
118 // This union is to avoid a warning message:
119 union {
120 void *_ptr;
121 func_t _funcptr;
122 } temp;
123 temp._funcptr = _ptr;
124 return Form("(%p)",temp._ptr) ;
125 }
126
127 const char* argName(Int_t iarg) {
128 // Return suggested name for i-th argument
129 return fmap().lookupArgName(_ptr,iarg) ;
130 }
131
133 // Return reference to function pointer-to-name mapping service
134 if (!_fmap) {
136 }
137 return *_fmap ;
138 }
139 private:
140
141 static VO dummyFunction(VI1,VI2,VI3,VI4) {
142 // Dummy function used when registered function was not
143 // found in un-persisting object
144 return 0 ;
145 }
146
147
148 typedef VO (*func_t)(VI1,VI2,VI3,VI4);
149 func_t _ptr; //! Pointer to embedded function
150
151 static RooCFunction4Map<VO,VI1,VI2,VI3,VI4>* _fmap ; // Pointer to mapping service object
152
153 ClassDefOverride(RooCFunction4Ref,1) // Persistable reference to C function pointer
154} ;
155
156// Define static member
157template<class VO, class VI1, class VI2, class VI3, class VI4>
159
160template<class VO, class VI1, class VI2, class VI3, class VI4>
162{
163 // Custom streamer for function pointer reference object. When writing,
164 // the function pointer is substituted by its registerd 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 ::RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> thisClass;
173
174 // Stream an object of class RooCFunction4Ref
175 if (R__b.IsReading()) {
176
177 UInt_t R__s, R__c;
178 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
179
180 // Read name from file
181 TString tmpName ;
182 tmpName.Streamer(R__b) ;
183
184 if (tmpName=="UNKNOWN" && R__v>0) {
185
186 coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
187 _ptr = dummyFunction ;
188
189 } else {
190
191 // Lookup pointer to C function wih given name
192 _ptr = fmap().lookupPtr(tmpName.Data()) ;
193
194 if (_ptr==nullptr) {
195 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
196 << " but no such function is registered, object will not be functional" << std::endl ;
197 }
198 }
199
200
201 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
202
203 } else {
204
205 UInt_t R__c;
206 R__c = R__b.WriteVersion(thisClass::IsA(), true);
207
208 // Lookup name of reference C function
209 TString tmpName = fmap().lookupName(_ptr) ;
210 if (tmpName.Length()==0) {
211 coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("0x%zx",(size_t)_ptr)
212 << " written object will not be functional when read back" << std::endl ;
213 tmpName="UNKNOWN" ;
214 }
215
216 // Persist the name
217 tmpName.Streamer(R__b) ;
218
219 R__b.SetByteCount(R__c, true);
220
221 }
222}
223
224
225
226template<class VO,class VI1, class VI2, class VI3, class VI4>
228public:
230 // Default constructor
231 } ;
232 RooCFunction4Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w);
233 RooCFunction4Binding(const RooCFunction4Binding& other, const char* name=nullptr) ;
234 TObject* clone(const char* newname) const override { return new RooCFunction4Binding(*this,newname); }
235 inline ~RooCFunction4Binding() override { }
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 RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> func ; // Function pointer reference
253 RooRealProxy x ; // Argument reference
254 RooRealProxy y ; // Argument reference
255 RooRealProxy z ; // Argument reference
256 RooRealProxy w ; // Argument reference
257
258 double evaluate() const override {
259 // Return value of embedded function using value of referenced variable x
260 return func(x,y,z,w) ;
261 }
262
263private:
264
265 ClassDefOverride(RooCFunction4Binding,1) // RooAbsReal binding to external C functions
266};
267
268
269template<class VO,class VI1, class VI2, class VI3, class VI4>
270RooCFunction4Binding<VO,VI1,VI2,VI3,VI4>::RooCFunction4Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4),
271 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w) :
272 RooAbsReal(name,title),
273 func(_func),
274 x(func.argName(0),func.argName(0),this,_x),
275 y(func.argName(1),func.argName(1),this,_y),
276 z(func.argName(2),func.argName(2),this,_z),
277 w(func.argName(3),func.argName(3),this,_w)
278{
279 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
280 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
281 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
282 // a RooWorkspace
283}
284
285
286template<class VO,class VI1, class VI2, class VI3, class VI4>
288 RooAbsReal(other,name),
289 func(other.func),
290 x("x",this,other.x),
291 y("y",this,other.y),
292 z("z",this,other.z),
293 w("w",this,other.w)
294{
295 // Copy constructor
296}
297
298
299template<class VO,class VI1, class VI2, class VI3, class VI4>
301public:
303 // Default constructor
304 } ;
305 RooCFunction4PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w);
306 RooCFunction4PdfBinding(const RooCFunction4PdfBinding& other, const char* name=nullptr) ;
307 TObject* clone(const char* newname) const override { return new RooCFunction4PdfBinding(*this,newname); }
308 inline ~RooCFunction4PdfBinding() override { }
309
310 void printArgs(std::ostream& os) const override {
311 // Print object arguments and name/address of function pointer
312 os << "[ function=" << func.name() << " " ;
313 for (Int_t i=0 ; i<numProxies() ; i++) {
314 RooAbsProxy* p = getProxy(i) ;
315 if (!TString(p->name()).BeginsWith("!")) {
316 p->print(os) ;
317 os << " " ;
318 }
319 }
320 os << "]" ;
321 }
322
323protected:
324
325 RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> func ; // Function pointer reference
326 RooRealProxy x ; // Argument reference
327 RooRealProxy y ; // Argument reference
328 RooRealProxy z ; // Argument reference
329 RooRealProxy w ; // Argument reference
330
331 double evaluate() const override {
332 // Return value of embedded function using value of referenced variable x
333 return func(x,y,z,w) ;
334 }
335
336private:
337
338 ClassDefOverride(RooCFunction4PdfBinding,1) // RooAbsReal binding to external C functions
339};
340
341
342template<class VO,class VI1, class VI2, class VI3, class VI4>
343RooCFunction4PdfBinding<VO,VI1,VI2,VI3,VI4>::RooCFunction4PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4),
344 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w) :
345 RooAbsPdf(name,title),
346 func(_func),
347 x(func.argName(0),func.argName(0),this,_x),
348 y(func.argName(1),func.argName(1),this,_y),
349 z(func.argName(2),func.argName(2),this,_z),
350 w(func.argName(3),func.argName(3),this,_w)
351{
352 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
353 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
354 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
355 // a RooWorkspace
356}
357
358
359template<class VO,class VI1, class VI2, class VI3, class VI4>
361 RooAbsPdf(other,name),
362 func(other.func),
363 x("x",this,other.x),
364 y("y",this,other.y),
365 z("z",this,other.z),
366 w("w",this,other.w)
367{
368 // Copy constructor
369}
370
371#endif
RooAbsReal * _func
Definition RooMinuit.h:90
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.
RooAbsProxy is the abstact interface for proxy classes.
Definition RooAbsProxy.h:37
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
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 Common.h:18
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)