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