ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RooCFunction2Binding.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 ROOCFUNCTION2BINDING
14 #define ROOCFUNCTION2BINDING
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 namespace RooFit {
29 
35 
36 
42 RooAbsPdf* bindPdf(const char* name,CFUNCD2DD func,RooAbsReal& x, RooAbsReal& y) ;
43 RooAbsPdf* bindPdf(const char* name,CFUNCD2ID func,RooAbsReal& x, RooAbsReal& y) ;
44 RooAbsPdf* bindPdf(const char* name,CFUNCD2UD func,RooAbsReal& x, RooAbsReal& y) ;
45 RooAbsPdf* bindPdf(const char* name,CFUNCD2DI func,RooAbsReal& x, RooAbsReal& y) ;
46 RooAbsPdf* bindPdf(const char* name,CFUNCD2II func,RooAbsReal& x, RooAbsReal& y) ;
47 
48 }
49 
50 template<class VO, class VI1, class VI2>
52  public:
54 
55  void add(const char* name, VO (*ptr)(VI1,VI2), const char* arg1name="x", const char* arg2name="y") {
56  // Register function with given name and argument name
57  _ptrmap[name] = ptr ;
58  _namemap[ptr] = name ;
59  _argnamemap[ptr].push_back(arg1name) ;
60  _argnamemap[ptr].push_back(arg2name) ;
61  }
62 
63 
64  const char* lookupName(VO (*ptr)(VI1,VI2)) {
65  // Return name of function given by pointer
66  return _namemap[ptr].c_str() ;
67  }
68 
69  VO (*lookupPtr(const char* name))(VI1,VI2) {
70  // Return pointer of function given by name
71  return _ptrmap[name] ;
72  }
73 
74  const char* lookupArgName(VO (*ptr)(VI1,VI2), UInt_t iarg) {
75  // Return name of i-th argument of function. If function is
76  // not registered, argument names 0,1,2 are x,y,z
77  if (iarg<_argnamemap[ptr].size()) {
78  return (_argnamemap[ptr])[iarg].c_str() ;
79  }
80  switch (iarg) {
81  case 0: return "x" ;
82  case 1: return "y" ;
83  case 2: return "z" ;
84  }
85  return "w" ;
86  }
87 
88  private:
89 
90 #ifndef __CINT__
91  std::map<std::string,VO (*)(VI1,VI2)> _ptrmap ; // Pointer-to-name map
92  std::map<VO (*)(VI1,VI2),std::string> _namemap ; // Name-to-pointer map
93  std::map<VO (*)(VI1,VI2),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
94 #endif
95 } ;
96 
97 
98 
99 template<class VO, class VI1, class VI2>
100 class RooCFunction2Ref : public TObject {
101  public:
102  RooCFunction2Ref(VO (*ptr)(VI1,VI2)=0) : _ptr(ptr) {
103  // Constructor of persistable function reference
104  } ;
106 
107  VO operator()(VI1 x,VI2 y) const {
108  // Evaluate embedded function
109  return (*_ptr)(x,y) ;
110  }
111 
112  const char* name() const {
113  // Return registered name of embedded function. If function
114  // is not registered return string with hex presentation
115  // of function pointer value
116  const char* result = fmap().lookupName(_ptr) ;
117  if (result && strlen(result)) {
118  return result ;
119  }
120  // This union is to avoid a warning message:
121  union {
122  void *_ptr;
123  func_t _funcptr;
124  } temp;
125  temp._funcptr = _ptr;
126  return Form("(%p)",temp._ptr) ;
127  }
128 
129  const char* argName(Int_t iarg) {
130  // Return suggested name for i-th argument
131  return fmap().lookupArgName(_ptr,iarg) ;
132  }
133 
135  // Return reference to function pointer-to-name mapping service
136  if (!_fmap) {
138  }
139  return *_fmap ;
140  }
141 
142  private:
143 
144  static VO dummyFunction(VI1,VI2) {
145  // Dummy function used when registered function was not
146  // found in un-persisting object
147  return 0 ;
148  }
149 
150  typedef VO (*func_t)(VI1,VI2);
151  func_t _ptr; //! Pointer to embedded function
152 
153  static RooCFunction2Map<VO,VI1,VI2>* _fmap ; // Pointer to mapping service object
154 
155  ClassDef(RooCFunction2Ref,1) // Persistable reference to C function pointer
156 } ;
157 
158 
159 
160 template<class VO, class VI1, class VI2>
162 {
163  // Custom streamer for function pointer reference object. When writing,
164  // the function pointer is substituted by its registerd name. When function
165  // is unregistered name 'UNKNOWN' is written and a warning is issues. When
166  // reading back, the embedded name is converted back to a function pointer
167  // using the mapping service. When name UNKNOWN is encountered a warning is
168  // issues and a dummy null function is substituted. When the registered function
169  // name can not be mapped to a function pointer an ERROR is issued and a pointer
170  // to the dummy null function is substituted
171 
172  typedef ::RooCFunction2Ref<VO,VI1,VI2> thisClass;
173 
174  // Stream an object of class RooCFunction2Ref
175  if (R__b.IsReading()) {
176 
177  UInt_t R__s, R__c;
178  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
179 
180  // Read name from file
181  TString tmpName ;
182  tmpName.Streamer(R__b) ;
183 
184  if (tmpName=="UNKNOWN" && R__v>0) {
185 
186  coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
187  _ptr = dummyFunction ;
188 
189  } else {
190 
191  // Lookup pointer to C function wih given name
192  _ptr = fmap().lookupPtr(tmpName.Data()) ;
193 
194  if (_ptr==0) {
195  coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
196  << " but no such function is registered, object will not be functional" << std::endl ;
197  }
198  }
199 
200 
201  R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
202 
203  } else {
204 
205  UInt_t R__c;
206  R__c = R__b.WriteVersion(thisClass::IsA(), kTRUE);
207 
208  // Lookup name of reference C function
209  TString tmpName = fmap().lookupName(_ptr) ;
210  if (tmpName.Length()==0) {
211  coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("0x%lx", (ULong_t)_ptr)
212  << " written object will not be functional when read back" << std::endl ;
213  tmpName="UNKNOWN" ;
214  }
215 
216  // Persist the name
217  tmpName.Streamer(R__b) ;
218 
219  R__b.SetByteCount(R__c, kTRUE);
220 
221  }
222 }
223 
224 
225 
226 template<class VO,class VI1, class VI2>
228 public:
230  // Default constructor
231  } ;
232  RooCFunction2Binding(const char *name, const char *title, VO (*_func)(VI1,VI2), RooAbsReal& _x, RooAbsReal& _y);
233  RooCFunction2Binding(const RooCFunction2Binding& other, const char* name=0) ;
234  virtual TObject* clone(const char* newname) const { return new RooCFunction2Binding(*this,newname); }
235  inline virtual ~RooCFunction2Binding() { }
236 
237  void printArgs(std::ostream& os) const {
238  // Print object arguments and name/address of function pointer
239  os << "[ function=" << func.name() << " " ;
240  for (Int_t i=0 ; i<numProxies() ; i++) {
241  RooAbsProxy* p = getProxy(i) ;
242  if (!TString(p->name()).BeginsWith("!")) {
243  p->print(os) ;
244  os << " " ;
245  }
246  }
247  os << "]" ;
248  }
249 
250 protected:
251 
252  RooCFunction2Ref<VO,VI1,VI2> func ; // Function pointer reference
253  RooRealProxy x ; // Argument reference
254  RooRealProxy y ; // Argument reference
255 
256  Double_t evaluate() const {
257  // Return value of embedded function using value of referenced variable x
258  return func(x,y) ;
259  }
260 
261 private:
262 
263  ClassDef(RooCFunction2Binding,1) // RooAbsReal binding to external C functions
264 };
265 
266 template<class VO,class VI1, class VI2>
267 RooCFunction2Binding<VO,VI1,VI2>::RooCFunction2Binding(const char *name, const char *title, VO (*_func)(VI1,VI2),
268  RooAbsReal& _x, RooAbsReal& _y) :
269  RooAbsReal(name,title),
270  func(_func),
271  x(func.argName(0),func.argName(0),this,_x),
272  y(func.argName(1),func.argName(1),this,_y)
273 {
274  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
275  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
276  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
277  // a RooWorkspace
278 }
279 
280 
281 template<class VO,class VI1, class VI2>
283  RooAbsReal(other,name),
284  func(other.func),
285  x("x",this,other.x),
286  y("y",this,other.y)
287 {
288  // Copy constructor
289 }
290 
291 
292 
293 
294 template<class VO,class VI1, class VI2>
296 public:
298  // Default constructor
299  } ;
300  RooCFunction2PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2), RooAbsReal& _x, RooAbsReal& _y);
301  RooCFunction2PdfBinding(const RooCFunction2PdfBinding& other, const char* name=0) ;
302  virtual TObject* clone(const char* newname) const { return new RooCFunction2PdfBinding(*this,newname); }
303  inline virtual ~RooCFunction2PdfBinding() { }
304 
305  void printArgs(std::ostream& os) const {
306  // Print object arguments and name/address of function pointer
307  os << "[ function=" << func.name() << " " ;
308  for (Int_t i=0 ; i<numProxies() ; i++) {
309  RooAbsProxy* p = getProxy(i) ;
310  if (!TString(p->name()).BeginsWith("!")) {
311  p->print(os) ;
312  os << " " ;
313  }
314  }
315  os << "]" ;
316  }
317 
318 protected:
319 
320  RooCFunction2Ref<VO,VI1,VI2> func ; // Function pointer reference
321  RooRealProxy x ; // Argument reference
322  RooRealProxy y ; // Argument reference
323 
324  Double_t evaluate() const {
325  // Return value of embedded function using value of referenced variable x
326  return func(x,y) ;
327  }
328 
329 private:
330 
331  ClassDef(RooCFunction2PdfBinding,1) // RooAbsReal binding to external C functions
332 };
333 
334 template<class VO,class VI1, class VI2>
335 RooCFunction2PdfBinding<VO,VI1,VI2>::RooCFunction2PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2),
336  RooAbsReal& _x, RooAbsReal& _y) :
337  RooAbsPdf(name,title),
338  func(_func),
339  x(func.argName(0),func.argName(0),this,_x),
340  y(func.argName(1),func.argName(1),this,_y)
341 {
342  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
343  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
344  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
345  // a RooWorkspace
346 }
347 
348 
349 template<class VO,class VI1, class VI2>
351  RooAbsPdf(other,name),
352  func(other.func),
353  x("x",this,other.x),
354  y("y",this,other.y)
355 {
356  // Copy constructor
357 }
358 
359 #endif
Double_t evaluate() const
virtual void print(std::ostream &os, Bool_t addContents=kFALSE) const
Print proxy name.
Definition: RooAbsProxy.cxx:75
RooCFunction2Ref< VO, VI1, VI2 > func
Bool_t IsReading() const
Definition: TBuffer.h:83
short Version_t
Definition: RtypesCore.h:61
Ssiz_t Length() const
Definition: TString.h:390
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
RooCFunction2Map< double, int, double > * _fmap
const char * lookupArgName(VO(*ptr)(VI1, VI2), 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
RooCFunction2Ref(VO(*ptr)(VI1, VI2)=0)
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
RooCFunction2Ref< VO, VI1, VI2 > func
#define coutW(a)
Definition: RooMsgService.h:34
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
Double_t(* CFUNCD2UD)(UInt_t, Double_t)
RooAbsProxy * getProxy(Int_t index) const
Return the nth proxy from the proxy list.
Definition: RooAbsArg.cxx:1254
RooCFunction2Binding is a templated implementation of class RooAbsReal that binds generic C(++) funct...
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)
const char * Data() const
Definition: TString.h:349
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual TObject * clone(const char *newname) const
static RooCFunction2Map< VO, VI1, VI2 > * _fmap
Pointer to embedded function.
bool BeginsWith(const std::string &theString, const std::string &theSubstring)
const char * argName(Int_t iarg)
void add(const char *name, VO(*ptr)(VI1, VI2), const char *arg1name="x", const char *arg2name="y")
static RooCFunction2Map< VO, VI1, VI2 > & fmap()
Int_t numProxies() const
Return the number of registered proxies.
Definition: RooAbsArg.cxx:1267
RooAbsProxy is the abstact interface for proxy classes.
Definition: RooAbsProxy.h:32
TClass * IsA() const
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
std::map< VO(*)(VI1, VI2), std::string > _namemap
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
std::map< VO(*)(VI1, VI2), std::vector< std::string > > _argnamemap
const char * name() const
Double_t(* CFUNCD2DI)(Double_t, Int_t)
virtual const char * name() const
Definition: RooAbsProxy.h:42
VO(* func_t)(VI1, VI2)
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
unsigned long ULong_t
Definition: RtypesCore.h:51
Double_t y[n]
Definition: legend1.C:17
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Double_t(* CFUNCD2ID)(Int_t, Double_t)
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
std::map< std::string, VO(*)(VI1, VI2)> _ptrmap
Double_t(* CFUNCD2II)(Int_t, Int_t)
virtual TObject * clone(const char *newname) const
static VO dummyFunction(VI1, VI2)
RooRealProxy is the concrete proxy for RooAbsReal objects A RooRealProxy is the general mechanism to ...
Definition: RooRealProxy.h:23
Double_t(* CFUNCD2DD)(Double_t, Double_t)
VO operator()(VI1 x, VI2 y) const
double result[121]
VO(*)(VI1, VI2) lookupPtr(const char *name)
const char * lookupName(VO(*ptr)(VI1, VI2))
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0