ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
29 namespace RooFit {
30 
31 typedef Double_t (*CFUNCD1D)(Double_t) ;
32 typedef Double_t (*CFUNCD1I)(Int_t) ;
33 
34 RooAbsReal* bindFunction(const char* name,CFUNCD1D func,RooAbsReal& x) ;
35 RooAbsReal* bindFunction(const char* name,CFUNCD1I func,RooAbsReal& x) ;
36 RooAbsPdf* bindPdf(const char* name,CFUNCD1D func,RooAbsReal& x) ;
37 RooAbsPdf* bindPdf(const char* name,CFUNCD1I func,RooAbsReal& x) ;
38 
39 }
40 
41 
42 template<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 
90 template<class VO, class VI>
91 class RooCFunction1Ref : public TObject {
92  public:
93  RooCFunction1Ref(VO (*ptr)(VI)=0) : _ptr(ptr) {
94  // Constructor of persistable function reference
95  } ;
97 
98  VO operator()(VI x) const {
99  // Evaluate embedded function
100  return (*_ptr)(x) ;
101  }
102 
103  const char* name() const {
104  // Return registered name of embedded function. If function
105  // is not registered return string with hex presentation
106  // of function pointer value
107  const char* result = fmap().lookupName(_ptr) ;
108  if (result && strlen(result)) {
109  return result ;
110  }
111  // This union is to avoid a warning message:
112  union {
113  void *_ptr;
114  func_t _funcptr;
115  } temp;
116  temp._funcptr = _ptr;
117  return Form("(%p)",temp._ptr) ;
118  }
119 
120  const char* argName(Int_t iarg) {
121  // Return suggested name for i-th argument
122  return fmap().lookupArgName(_ptr,iarg) ;
123  }
124 
125  static RooCFunction1Map<VO,VI>& fmap();
126 
127  private:
128 
129  static VO dummyFunction(VI) {
130  // Dummy function used when registered function was not
131  // found in un-persisting object
132  return 0 ;
133  }
134 
135  typedef VO (*func_t)(VI);
136  func_t _ptr; //! Pointer to embedded function
137 
138  static RooCFunction1Map<VO,VI>* _fmap ; // Pointer to mapping service object
139 
140  ClassDef(RooCFunction1Ref,1) // Persistable reference to C function pointer
141 } ;
142 
143 
144 
145 template<class VO, class VI>
147 {
148  // Custom streamer for function pointer reference object. When writing,
149  // the function pointer is substituted by its registered name. When function
150  // is unregistered name 'UNKNOWN' is written and a warning is issues. When
151  // reading back, the embedded name is converted back to a function pointer
152  // using the mapping service. When name UNKNOWN is encountered a warning is
153  // issues and a dummy null function is substituted. When the registered function
154  // name can not be mapped to a function pointer an ERROR is issued and a pointer
155  // to the dummy null function is substituted
156 
157  typedef ::RooCFunction1Ref<VO,VI> thisClass;
158 
159  // Stream an object of class RooCFunction1Ref
160  if (R__b.IsReading()) {
161 
162  UInt_t R__s, 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 wih given name
177  _ptr = fmap().lookupPtr(tmpName.Data()) ;
178 
179  if (_ptr==0) {
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(), kTRUE);
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, kTRUE);
211 
212  }
213 }
214 
215 
216 
217 template<class VO,class VI>
219 public:
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=0) ;
225  virtual TObject* clone(const char* newname) const { return new RooCFunction1Binding(*this,newname); }
226  inline virtual ~RooCFunction1Binding() { }
227 
228  void printArgs(std::ostream& os) const {
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 
241 protected:
242 
243  RooCFunction1Ref<VO,VI> func ; // Function pointer reference
244  RooRealProxy x ; // Argument reference
245 
246  Double_t evaluate() const {
247  // Return value of embedded function using value of referenced variable x
248  return func(x) ;
249  }
250 
251 private:
252 
253  ClassDef(RooCFunction1Binding,1) // RooAbsReal binding to external C functions
254 };
255 
256 
257 template<class VO,class VI>
258 RooCFunction1Binding<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 
270 template<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 
281 template<class VO,class VI>
283 public:
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=0) ;
289  virtual TObject* clone(const char* newname) const { return new RooCFunction1PdfBinding(*this,newname); }
290  inline virtual ~RooCFunction1PdfBinding() { }
291 
292  void printArgs(std::ostream& os) const {
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 
305 protected:
306 
307  RooCFunction1Ref<VO,VI> func ; // Function pointer reference
308  RooRealProxy x ; // Argument reference
309 
310  Double_t evaluate() const {
311  // Return value of embedded function using value of referenced variable x
312  return func(x) ;
313  }
314 
315 private:
316 
317  ClassDef(RooCFunction1PdfBinding,1) // RooAbsReal binding to external C functions
318 };
319 
320 
321 template<class VO,class VI>
322 RooCFunction1PdfBinding<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 
334 template<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
virtual TObject * clone(const char *newname) const
virtual void print(std::ostream &os, Bool_t addContents=kFALSE) const
Print proxy name.
Definition: RooAbsProxy.cxx:75
RooCFunction1Ref< VO, VI > func
Bool_t IsReading() const
Definition: TBuffer.h:83
const char * argName(Int_t iarg)
short Version_t
Definition: RtypesCore.h:61
Ssiz_t Length() const
Definition: TString.h:390
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
const char * lookupArgName(VO(*ptr)(VI), UInt_t iarg)
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
void add(const char *name, VO(*ptr)(VI), const char *arg1name="x")
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
#define coutW(a)
Definition: RooMsgService.h:34
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
RooAbsProxy * getProxy(Int_t index) const
Return the nth proxy from the proxy list.
Definition: RooAbsArg.cxx:1254
const char * name() const
virtual TObject * clone(const char *newname) const
static RooCFunction1Map< VO, VI > & fmap()
Double_t(* CFUNCD1D)(Double_t)
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)
std::map< VO(*)(VI), std::string > _namemap
const char * Data() const
Definition: TString.h:349
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
bool BeginsWith(const std::string &theString, const std::string &theSubstring)
VO operator()(VI x) const
Int_t numProxies() const
Return the number of registered proxies.
Definition: RooAbsArg.cxx:1267
RooCFunction1Ref< VO, VI > func
RooAbsProxy is the abstact interface for proxy classes.
Definition: RooAbsProxy.h:32
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
TClass * IsA() const
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
std::map< VO(*)(VI), std::vector< std::string > > _argnamemap
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
virtual const char * name() const
Definition: RooAbsProxy.h:42
std::map< std::string, VO(*)(VI)> _ptrmap
Double_t evaluate() const
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
RooCFunction1Binding is a templated implementation of class RooAbsReal that binds generic C(++) funct...
double func(double *x, double *p)
Definition: stressTF1.cxx:213
static RooCFunction1Map< VO, VI > * _fmap
Pointer to embedded function.
VO(*)(VI) lookupPtr(const char *name)
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
RooRealProxy is the concrete proxy for RooAbsReal objects A RooRealProxy is the general mechanism to ...
Definition: RooRealProxy.h:23
static VO dummyFunction(VI)
double result[121]
Double_t(* CFUNCD1I)(Int_t)
RooCFunction1Ref(VO(*ptr)(VI)=0)
const Bool_t kTRUE
Definition: Rtypes.h:91
const char * lookupName(VO(*ptr)(VI))
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0