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