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 ~RooCFunction3Ref() override {} ;
108
109 VO operator()(VI1 x,VI2 y, VI3 z) const {
110 // Evaluate embedded function
111 return (*_ptr)(x,y,z) ;
112 }
113
114 const char* name() const {
115 // Return registered name of embedded function. If function
116 // is not registered return string with hex presentation
117 // of function pointer value
118 const char* result = fmap().lookupName(_ptr) ;
119 if (result && strlen(result)) {
120 return result ;
121 }
122 // This union is to avoid a warning message:
123 union {
124 void *_ptr;
125 func_t _funcptr;
126 } temp;
127 temp._funcptr = _ptr;
128 return Form("(%p)",temp._ptr) ;
129 }
130
131 const char* argName(Int_t iarg) {
132 // Return suggested name for i-th argument
133 return fmap().lookupArgName(_ptr,iarg) ;
134 }
135
137 // Return reference to function pointer-to-name mapping service
138 if (!_fmap) {
140 }
141 return *_fmap ;
142 }
143
144 private:
145
146 static VO dummyFunction(VI1,VI2,VI3) {
147 // Dummy function used when registered function was not
148 // found in un-persisting object
149 return 0 ;
150 }
151
152
153 typedef VO (*func_t)(VI1,VI2,VI3) ; //! Pointer to embedded function
154 func_t _ptr; //! Pointer to embedded function
155
156 static RooCFunction3Map<VO,VI1,VI2,VI3>* _fmap ; // Pointer to mapping service object
157
158 ClassDefOverride(RooCFunction3Ref,1) // Persistable reference to C function pointer
159} ;
160
161// Define static member
162template<class VO, class VI1, class VI2, class VI3>
164
165
166
167template<class VO, class VI1, class VI2, class VI3>
169{
170 // Custom streamer for function pointer reference object. When writing,
171 // the function pointer is substituted by its registerd name. When function
172 // is unregistered name 'UNKNOWN' is written and a warning is issues. When
173 // reading back, the embedded name is converted back to a function pointer
174 // using the mapping service. When name UNKNOWN is encountered a warning is
175 // issues and a dummy null function is substituted. When the registered function
176 // name can not be mapped to a function pointer an ERROR is issued and a pointer
177 // to the dummy null function is substituted
178
179 typedef ::RooCFunction3Ref<VO,VI1,VI2,VI3> thisClass;
180
181 // Stream an object of class RooCFunction3Ref
182 if (R__b.IsReading()) {
183
184 UInt_t R__s, 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 wih 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 inline ~RooCFunction3Binding() override { }
249
250 void printArgs(std::ostream& os) const override {
251 // Print object arguments and name/address of function pointer
252 os << "[ function=" << func.name() << " " ;
253 for (Int_t i=0 ; i<numProxies() ; i++) {
254 RooAbsProxy* p = getProxy(i) ;
255 if (!TString(p->name()).BeginsWith("!")) {
256 p->print(os) ;
257 os << " " ;
258 }
259 }
260 os << "]" ;
261 }
262
263protected:
264
265 RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
266 RooRealProxy x ; // Argument reference
267 RooRealProxy y ; // Argument reference
268 RooRealProxy z ; // Argument reference
269
270 double evaluate() const override {
271 // Return value of embedded function using value of referenced variable x
272 return func(x,y,z) ;
273 }
274
275private:
276
277 ClassDefOverride(RooCFunction3Binding,1) // RooAbsReal binding to external C functions
278};
279
280
281
282template<class VO,class VI1, class VI2, class VI3>
283RooCFunction3Binding<VO,VI1,VI2,VI3>::RooCFunction3Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3),
284 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
285 RooAbsReal(name,title),
286 func(_func),
287 x(func.argName(0),func.argName(0),this,_x),
288 y(func.argName(1),func.argName(1),this,_y),
289 z(func.argName(2),func.argName(2),this,_z)
290{
291 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
292 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
293 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
294 // a RooWorkspace
295}
296
297
298template<class VO,class VI1, class VI2, class VI3>
300 RooAbsReal(other,name),
301 func(other.func),
302 x("x",this,other.x),
303 y("y",this,other.y),
304 z("z",this,other.z)
305{
306 // Copy constructor
307}
308
309
310template<class VO,class VI1, class VI2, class VI3>
312public:
314 // Default constructor
315 } ;
316 RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z);
317 RooCFunction3PdfBinding(const RooCFunction3PdfBinding& other, const char* name=nullptr) ;
318 TObject* clone(const char* newname) const override { return new RooCFunction3PdfBinding(*this,newname); }
319 inline ~RooCFunction3PdfBinding() override { }
320
321 void printArgs(std::ostream& os) const override {
322 // Print object arguments and name/address of function pointer
323 os << "[ function=" << func.name() << " " ;
324 for (Int_t i=0 ; i<numProxies() ; i++) {
325 RooAbsProxy* p = getProxy(i) ;
326 if (!TString(p->name()).BeginsWith("!")) {
327 p->print(os) ;
328 os << " " ;
329 }
330 }
331 os << "]" ;
332 }
333
334protected:
335
336 RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
337 RooRealProxy x ; // Argument reference
338 RooRealProxy y ; // Argument reference
339 RooRealProxy z ; // Argument reference
340
341 double evaluate() const override {
342 // Return value of embedded function using value of referenced variable x
343 return func(x,y,z) ;
344 }
345
346private:
347
348 ClassDefOverride(RooCFunction3PdfBinding,1) // RooAbsReal binding to external C functions
349};
350
351
352
353template<class VO,class VI1, class VI2, class VI3>
354RooCFunction3PdfBinding<VO,VI1,VI2,VI3>::RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3),
355 RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
356 RooAbsPdf(name,title),
357 func(_func),
358 x(func.argName(0),func.argName(0),this,_x),
359 y(func.argName(1),func.argName(1),this,_y),
360 z(func.argName(2),func.argName(2),this,_z)
361{
362 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
363 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
364 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
365 // a RooWorkspace
366}
367
368
369template<class VO,class VI1, class VI2, class VI3>
371 RooAbsPdf(other,name),
372 func(other.func),
373 x("x",this,other.x),
374 y("y",this,other.y),
375 z("z",this,other.z)
376{
377 // Copy constructor
378}
379
380#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
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.
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
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 Common.h:18
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)