Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooCFunction1Binding.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 ROOCFUNCTION1BINDING
14#define ROOCFUNCTION1BINDING
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
28
29namespace RooFit {
30
31typedef double (*CFUNCD1D)(double) ;
32typedef double (*CFUNCD1I)(Int_t) ;
33
34RooAbsReal* bindFunction(const char* name,CFUNCD1D func,RooAbsReal& x) ;
35RooAbsReal* bindFunction(const char* name,CFUNCD1I func,RooAbsReal& x) ;
36RooAbsPdf* bindPdf(const char* name,CFUNCD1D func,RooAbsReal& x) ;
37RooAbsPdf* bindPdf(const char* name,CFUNCD1I func,RooAbsReal& x) ;
38
39}
40
41
42template<class VO, class VI>
44 public:
46
47 void add(const char* name, VO (*ptr)(VI), const char* arg1name="x") {
48 // Register function with given name and argument name
49 _ptrmap[name] = ptr ;
50 _namemap[ptr] = name ;
51 _argnamemap[ptr].push_back(arg1name) ;
52 }
53
54
55 const char* lookupName(VO (*ptr)(VI)) {
56 // Return name of function given by pointer
57 return _namemap[ptr].c_str() ;
58 }
59
60 VO (*lookupPtr(const char* name))(VI) {
61 // Return pointer of function given by name
62 return _ptrmap[name] ;
63 }
64
65 const char* lookupArgName(VO (*ptr)(VI), UInt_t iarg) {
66 // Return name of i-th argument of function. If function is
67 // not registered, argument names 0,1,2 are x,y,z
68 if (iarg<_argnamemap[ptr].size()) {
69 return (_argnamemap[ptr])[iarg].c_str() ;
70 }
71 switch (iarg) {
72 case 0: return "x" ;
73 case 1: return "y" ;
74 case 2: return "z" ;
75 }
76 return "w" ;
77 }
78
79 private:
80
81#ifndef __CINT__
82 std::map<std::string,VO (*)(VI)> _ptrmap ; // Pointer-to-name map
83 std::map<VO (*)(VI),std::string> _namemap ; // Name-to-pointer map
84 std::map<VO (*)(VI),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
85#endif
86} ;
87
88
89
90template<class VO, class VI>
91class RooCFunction1Ref : public TObject {
92 public:
93 RooCFunction1Ref(VO (*ptr)(VI)=nullptr) : _ptr(ptr) {
94 // Constructor of persistable function reference
95 } ;
96
97 VO operator()(VI x) const {
98 // Evaluate embedded function
99 return (*_ptr)(x) ;
100 }
101
102 const char* name() const {
103 // Return registered name of embedded function. If function
104 // is not registered return string with hex presentation
105 // of function pointer value
106 const char* result = fmap().lookupName(_ptr) ;
107 if (result && strlen(result)) {
108 return result ;
109 }
110 // This union is to avoid a warning message:
111 union {
112 void *_ptr;
113 func_t _funcptr;
114 } temp;
115 temp._funcptr = _ptr;
116 return Form("(%p)",temp._ptr) ;
117 }
118
119 const char* argName(Int_t iarg) {
120 // Return suggested name for i-th argument
121 return fmap().lookupArgName(_ptr,iarg) ;
122 }
123
125
126 private:
127
128 static VO dummyFunction(VI) {
129 // Dummy function used when registered function was not
130 // found in un-persisting object
131 return 0 ;
132 }
133
134 typedef VO (*func_t)(VI);
135 func_t _ptr; //! Pointer to embedded function
136
137 static RooCFunction1Map<VO,VI>* _fmap ; // Pointer to mapping service object
138
139 ClassDefOverride(RooCFunction1Ref,1) // Persistable reference to C function pointer
140} ;
141
142// Define static member
143template<class VO, class VI>
145
146template<class VO, class VI>
148{
149 // Custom streamer for function pointer reference object. When writing,
150 // the function pointer is substituted by its registered name. When function
151 // is unregistered name 'UNKNOWN' is written and a warning is issues. When
152 // reading back, the embedded name is converted back to a function pointer
153 // using the mapping service. When name UNKNOWN is encountered a warning is
154 // issues and a dummy null function is substituted. When the registered function
155 // name can not be mapped to a function pointer an ERROR is issued and a pointer
156 // to the dummy null function is substituted
157
158 typedef ::RooCFunction1Ref<VO,VI> thisClass;
159
160 // Stream an object of class RooCFunction1Ref
161 if (R__b.IsReading()) {
162
163 UInt_t R__s;
164 UInt_t R__c;
165 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
166
167 // Read name from file
168 TString tmpName ;
169 tmpName.Streamer(R__b) ;
170
171 if (tmpName=="UNKNOWN" && R__v>0) {
172
173 coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
174 _ptr = dummyFunction ;
175
176 } else {
177
178 // Lookup pointer to C function with given name
179 _ptr = fmap().lookupPtr(tmpName.Data()) ;
180
181 if (_ptr==nullptr) {
182 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
183 << " but no such function is registered, object will not be functional" << std::endl ;
184 }
185 }
186
187
188 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
189
190 } else {
191
192 UInt_t R__c;
193 R__c = R__b.WriteVersion(thisClass::IsA(), true);
194
195 // Lookup name of reference C function
196 TString tmpName = fmap().lookupName(_ptr) ;
197 if (tmpName.Length()==0) {
198 // This union is to avoid a warning message:
199 union {
200 void *_ptr;
201 func_t _funcptr;
202 } temp;
203 temp._funcptr = _ptr;
204 coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("%p",temp._ptr)
205 << " written object will not be functional when read back" << std::endl ;
206 tmpName="UNKNOWN" ;
207 }
208
209 // Persist the name
210 tmpName.Streamer(R__b) ;
211
212 R__b.SetByteCount(R__c, true);
213
214 }
215}
216
217
218
219template<class VO,class VI>
221public:
223 // Default constructor
224 } ;
225 RooCFunction1Binding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x);
226 RooCFunction1Binding(const RooCFunction1Binding& other, const char* name=nullptr) ;
227 TObject* clone(const char* newname) const override { return new RooCFunction1Binding(*this,newname); }
228
229 void printArgs(std::ostream& os) const override {
230 // Print object arguments and name/address of function pointer
231 os << "[ function=" << func.name() << " " ;
232 for (Int_t i=0 ; i<numProxies() ; i++) {
233 RooAbsProxy* p = getProxy(i) ;
234 if (!TString(p->name()).BeginsWith("!")) {
235 p->print(os) ;
236 os << " " ;
237 }
238 }
239 os << "]" ;
240 }
241
242protected:
243
244 RooCFunction1Ref<VO,VI> func ; // Function pointer reference
245 RooRealProxy x ; // Argument reference
246
247 double evaluate() const override {
248 // Return value of embedded function using value of referenced variable x
249 return func(x) ;
250 }
251
252private:
253
254 ClassDefOverride(RooCFunction1Binding,1) // RooAbsReal binding to external C functions
255};
256
257
258template<class VO,class VI>
259RooCFunction1Binding<VO,VI>::RooCFunction1Binding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x) :
260 RooAbsReal(name,title),
261 func(_func),
262 x(func.argName(0),func.argName(0),this,_x)
263{
264 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
265 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
266 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
267 // a RooWorkspace
268}
269
270
271template<class VO,class VI>
273 RooAbsReal(other,name),
274 func(other.func),
275 x("x",this,other.x)
276{
277 // Copy constructor
278}
279
280
281
282template<class VO,class VI>
284public:
286 // Default constructor
287 } ;
288 RooCFunction1PdfBinding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x);
289 RooCFunction1PdfBinding(const RooCFunction1PdfBinding& other, const char* name=nullptr) ;
290 TObject* clone(const char* newname) const override { return new RooCFunction1PdfBinding(*this,newname); }
291
292 void printArgs(std::ostream& os) const override {
293 // Print object arguments and name/address of function pointer
294 os << "[ function=" << func.name() << " " ;
295 for (Int_t i=0 ; i<numProxies() ; i++) {
296 RooAbsProxy* p = getProxy(i) ;
297 if (!TString(p->name()).BeginsWith("!")) {
298 p->print(os) ;
299 os << " " ;
300 }
301 }
302 os << "]" ;
303 }
304
305protected:
306
307 RooCFunction1Ref<VO,VI> func ; // Function pointer reference
308 RooRealProxy x ; // Argument reference
309
310 double evaluate() const override {
311 // Return value of embedded function using value of referenced variable x
312 return func(x) ;
313 }
314
315private:
316
317 ClassDefOverride(RooCFunction1PdfBinding,1) // RooAbsReal binding to external C functions
318};
319
320
321template<class VO,class VI>
322RooCFunction1PdfBinding<VO,VI>::RooCFunction1PdfBinding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x) :
323 RooAbsPdf(name,title),
324 func(_func),
325 x(func.argName(0),func.argName(0),this,_x)
326{
327 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
328 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
329 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
330 // a RooWorkspace
331}
332
333
334template<class VO,class VI>
336 RooAbsPdf(other,name),
337 func(other.func),
338 x("x",this,other.x)
339{
340 // Copy constructor
341}
342
343#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
#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: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
RooCFunction1Binding is a templated implementation of class RooAbsReal that binds generic C(++) funct...
TObject * clone(const char *newname) const override
RooCFunction1Ref< VO, VI > 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.
VO(*)(VI) lookupPtr(const char *name)
const char * lookupArgName(VO(*ptr)(VI), UInt_t iarg)
void add(const char *name, VO(*ptr)(VI), const char *arg1name="x")
std::map< VO(*)(VI), std::vector< std::string > > _argnamemap
std::map< VO(*)(VI), std::string > _namemap
std::map< std::string, VO(*)(VI)> _ptrmap
const char * lookupName(VO(*ptr)(VI))
TObject * clone(const char *newname) const override
RooCFunction1Ref< VO, VI > 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.
static VO dummyFunction(VI)
void Streamer(TBuffer &) override
Stream an object of class TObject.
VO operator()(VI x) const
const char * name() const
const char * argName(Int_t iarg)
static RooCFunction1Map< VO, VI > & fmap()
static RooCFunction1Map< VO, VI > * _fmap
Pointer to embedded function.
RooCFunction1Ref(VO(*ptr)(VI)=nullptr)
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:1412
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(* CFUNCD1D)(double)
double(* CFUNCD1I)(Int_t)
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)