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