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;
184 UInt_t R__c;
185 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
186
187 // Read name from file
188 TString tmpName ;
189 tmpName.Streamer(R__b) ;
190
191 if (tmpName=="UNKNOWN" && R__v>0) {
192
193 coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
194 _ptr = dummyFunction ;
195
196 } else {
197
198 // Lookup pointer to C function with given name
199 _ptr = fmap().lookupPtr(tmpName.Data()) ;
200
201 if (_ptr==nullptr) {
202 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
203 << " but no such function is registered, object will not be functional" << std::endl ;
204 }
205 }
206
207
208 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
209
210 } else {
211
212 UInt_t R__c;
213 R__c = R__b.WriteVersion(thisClass::IsA(), true);
214
215 // Lookup name of reference C function
216 TString tmpName = fmap().lookupName(_ptr) ;
217 if (tmpName.Length()==0) {
218 // This union is to avoid a warning message:
219 union {
220 void *_ptr;
221 func_t _funcptr;
222 } temp;
223 temp._funcptr = _ptr;
224 coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("%p",temp._ptr)
225 << " written object will not be functional when read back" << std::endl ;
226 tmpName="UNKNOWN" ;
227 }
228
229 // Persist the name
230 tmpName.Streamer(R__b) ;
231
232 R__b.SetByteCount(R__c, true);
233
234 }
235}
236
237
238
239template<class VO,class VI1, class VI2, class VI3>
241public:
243 // Default constructor
244 } ;
245 RooCFunction3Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z);
246 RooCFunction3Binding(const RooCFunction3Binding& other, const char* name=nullptr) ;
247 TObject* clone(const char* newname) const override { return new RooCFunction3Binding(*this,newname); }
248
249 void printArgs(std::ostream& os) const override {
250 // Print object arguments and name/address of function pointer
251 os << "[ function=" << func.name() << " " ;
252 for (Int_t i=0 ; i<numProxies() ; i++) {
253 RooAbsProxy* p = getProxy(i) ;
254 if (!TString(p->name()).BeginsWith("!")) {
255 p->print(os) ;
256 os << " " ;
257 }
258 }
259 os << "]" ;
260 }
261
262protected:
263
264 RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
265 RooRealProxy x ; // Argument reference
266 RooRealProxy y ; // Argument reference
267 RooRealProxy z ; // Argument reference
268
269 double evaluate() const override {
270 // Return value of embedded function using value of referenced variable x
271 return func(x,y,z) ;
272 }
273
274private:
275
276 ClassDefOverride(RooCFunction3Binding,1) // RooAbsReal binding to external C functions
277};
278
279
280
281template<class VO,class VI1, class VI2, class VI3>
282RooCFunction3Binding<VO,VI1,VI2,VI3>::RooCFunction3Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3),
283 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
284 RooAbsReal(name,title),
285 func(_func),
286 x(func.argName(0),func.argName(0),this,_x),
287 y(func.argName(1),func.argName(1),this,_y),
288 z(func.argName(2),func.argName(2),this,_z)
289{
290 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
291 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
292 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
293 // a RooWorkspace
294}
295
296
297template<class VO,class VI1, class VI2, class VI3>
299 RooAbsReal(other,name),
300 func(other.func),
301 x("x",this,other.x),
302 y("y",this,other.y),
303 z("z",this,other.z)
304{
305 // Copy constructor
306}
307
308
309template<class VO,class VI1, class VI2, class VI3>
311public:
313 // Default constructor
314 } ;
315 RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z);
316 RooCFunction3PdfBinding(const RooCFunction3PdfBinding& other, const char* name=nullptr) ;
317 TObject* clone(const char* newname) const override { return new RooCFunction3PdfBinding(*this,newname); }
318
319 void printArgs(std::ostream& os) const override {
320 // Print object arguments and name/address of function pointer
321 os << "[ function=" << func.name() << " " ;
322 for (Int_t i=0 ; i<numProxies() ; i++) {
323 RooAbsProxy* p = getProxy(i) ;
324 if (!TString(p->name()).BeginsWith("!")) {
325 p->print(os) ;
326 os << " " ;
327 }
328 }
329 os << "]" ;
330 }
331
332protected:
333
334 RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
335 RooRealProxy x ; // Argument reference
336 RooRealProxy y ; // Argument reference
337 RooRealProxy z ; // Argument reference
338
339 double evaluate() const override {
340 // Return value of embedded function using value of referenced variable x
341 return func(x,y,z) ;
342 }
343
344private:
345
346 ClassDefOverride(RooCFunction3PdfBinding,1) // RooAbsReal binding to external C functions
347};
348
349
350
351template<class VO,class VI1, class VI2, class VI3>
352RooCFunction3PdfBinding<VO,VI1,VI2,VI3>::RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3),
353 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
354 RooAbsPdf(name,title),
355 func(_func),
356 x(func.argName(0),func.argName(0),this,_x),
357 y(func.argName(1),func.argName(1),this,_y),
358 z(func.argName(2),func.argName(2),this,_z)
359{
360 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
361 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
362 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
363 // a RooWorkspace
364}
365
366
367template<class VO,class VI1, class VI2, class VI3>
369 RooAbsPdf(other,name),
370 func(other.func),
371 x("x",this,other.x),
372 y("y",this,other.y),
373 z("z",this,other.z)
374{
375 // Copy constructor
376}
377
378#endif
RooAbsReal * _func
Pointer to original input function.
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
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:419
const char * Data() const
Definition TString.h:378
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:625
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)