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, R__c;
164 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
165
166 // Read name from file
167 TString tmpName ;
168 tmpName.Streamer(R__b) ;
169
170 if (tmpName=="UNKNOWN" && R__v>0) {
171
172 coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
173 _ptr = dummyFunction ;
174
175 } else {
176
177 // Lookup pointer to C function with given name
178 _ptr = fmap().lookupPtr(tmpName.Data()) ;
179
180 if (_ptr==nullptr) {
181 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
182 << " but no such function is registered, object will not be functional" << std::endl ;
183 }
184 }
185
186
187 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
188
189 } else {
190
191 UInt_t R__c;
192 R__c = R__b.WriteVersion(thisClass::IsA(), true);
193
194 // Lookup name of reference C function
195 TString tmpName = fmap().lookupName(_ptr) ;
196 if (tmpName.Length()==0) {
197 // This union is to avoid a warning message:
198 union {
199 void *_ptr;
200 func_t _funcptr;
201 } temp;
202 temp._funcptr = _ptr;
203 coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("%p",temp._ptr)
204 << " written object will not be functional when read back" << std::endl ;
205 tmpName="UNKNOWN" ;
206 }
207
208 // Persist the name
209 tmpName.Streamer(R__b) ;
210
211 R__b.SetByteCount(R__c, true);
212
213 }
214}
215
216
217
218template<class VO,class VI>
220public:
222 // Default constructor
223 } ;
224 RooCFunction1Binding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x);
225 RooCFunction1Binding(const RooCFunction1Binding& other, const char* name=nullptr) ;
226 TObject* clone(const char* newname) const override { return new RooCFunction1Binding(*this,newname); }
227
228 void printArgs(std::ostream& os) const override {
229 // Print object arguments and name/address of function pointer
230 os << "[ function=" << func.name() << " " ;
231 for (Int_t i=0 ; i<numProxies() ; i++) {
232 RooAbsProxy* p = getProxy(i) ;
233 if (!TString(p->name()).BeginsWith("!")) {
234 p->print(os) ;
235 os << " " ;
236 }
237 }
238 os << "]" ;
239 }
240
241protected:
242
243 RooCFunction1Ref<VO,VI> func ; // Function pointer reference
244 RooRealProxy x ; // Argument reference
245
246 double evaluate() const override {
247 // Return value of embedded function using value of referenced variable x
248 return func(x) ;
249 }
250
251private:
252
253 ClassDefOverride(RooCFunction1Binding,1) // RooAbsReal binding to external C functions
254};
255
256
257template<class VO,class VI>
258RooCFunction1Binding<VO,VI>::RooCFunction1Binding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x) :
259 RooAbsReal(name,title),
260 func(_func),
261 x(func.argName(0),func.argName(0),this,_x)
262{
263 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
264 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
265 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
266 // a RooWorkspace
267}
268
269
270template<class VO,class VI>
272 RooAbsReal(other,name),
273 func(other.func),
274 x("x",this,other.x)
275{
276 // Copy constructor
277}
278
279
280
281template<class VO,class VI>
283public:
285 // Default constructor
286 } ;
287 RooCFunction1PdfBinding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x);
288 RooCFunction1PdfBinding(const RooCFunction1PdfBinding& other, const char* name=nullptr) ;
289 TObject* clone(const char* newname) const override { return new RooCFunction1PdfBinding(*this,newname); }
290
291 void printArgs(std::ostream& os) const override {
292 // Print object arguments and name/address of function pointer
293 os << "[ function=" << func.name() << " " ;
294 for (Int_t i=0 ; i<numProxies() ; i++) {
295 RooAbsProxy* p = getProxy(i) ;
296 if (!TString(p->name()).BeginsWith("!")) {
297 p->print(os) ;
298 os << " " ;
299 }
300 }
301 os << "]" ;
302 }
303
304protected:
305
306 RooCFunction1Ref<VO,VI> func ; // Function pointer reference
307 RooRealProxy x ; // Argument reference
308
309 double evaluate() const override {
310 // Return value of embedded function using value of referenced variable x
311 return func(x) ;
312 }
313
314private:
315
316 ClassDefOverride(RooCFunction1PdfBinding,1) // RooAbsReal binding to external C functions
317};
318
319
320template<class VO,class VI>
321RooCFunction1PdfBinding<VO,VI>::RooCFunction1PdfBinding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x) :
322 RooAbsPdf(name,title),
323 func(_func),
324 x(func.argName(0),func.argName(0),this,_x)
325{
326 // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
327 // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
328 // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
329 // a RooWorkspace
330}
331
332
333template<class VO,class VI>
335 RooAbsPdf(other,name),
336 func(other.func),
337 x("x",this,other.x)
338{
339 // Copy constructor
340}
341
342#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: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
RooAbsProxy is the 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: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 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)