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