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