ROOT  6.06/09
Reference Guide
RooCFunction3Binding.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * File: $Id$
5  * Authors: * WV, Wouter Verkerke, NIKHEF, verkerke@nikhef.nl *
6  * *
7  * Copyright (c) 2000-2008, NIKHEF, Regents of the University of California *
8  * and Stanford University. All rights reserved. *
9  * *
10  *****************************************************************************/
11 
12 #ifndef ROOCFUNCTION3BINDING
13 #define ROOCFUNCTION3BINDING
14 
15 #include "TString.h"
16 #include "RooAbsReal.h"
17 #include "RooRealProxy.h"
18 #include "RooMsgService.h"
19 #include "RooAbsPdf.h"
20 #include <string>
21 #include <map>
22 #include <vector>
23 
24 
25 namespace RooFit {
26 
33 
46 
47 }
48 
49 template<class VO, class VI1, class VI2, class VI3>
51  public:
53 
54  void add(const char* name, VO (*ptr)(VI1,VI2,VI3), const char* arg1name="x", const char* arg2name="y", const char* arg3name="z") {
55  // Register function with given name and argument name
56  _ptrmap[name] = ptr ;
57  _namemap[ptr] = name ;
58  _argnamemap[ptr].push_back(arg1name) ;
59  _argnamemap[ptr].push_back(arg2name) ;
60  _argnamemap[ptr].push_back(arg3name) ;
61  }
62 
63 
64  const char* lookupName(VO (*ptr)(VI1,VI2,VI3)) {
65  // Return name of function given by pointer
66  return _namemap[ptr].c_str() ;
67  }
68 
69  VO (*lookupPtr(const char* name))(VI1,VI2,VI3) {
70  // Return pointer of function given by name
71  return _ptrmap[name] ;
72  }
73 
74  const char* lookupArgName(VO (*ptr)(VI1,VI2,VI3), 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,VI3)> _ptrmap ; // Pointer-to-name map
92  std::map<VO (*)(VI1,VI2,VI3),std::string> _namemap ; // Name-to-pointer map
93  std::map<VO (*)(VI1,VI2,VI3),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
94 #endif
95 } ;
96 
97 
98 
99 template<class VO, class VI1, class VI2, class VI3>
100 class RooCFunction3Ref : public TObject {
101  public:
102  RooCFunction3Ref(VO (*ptr)(VI1,VI2,VI3)=0) : _ptr(ptr) {
103  // Constructor of persistable function reference
104  } ;
106 
107  VO operator()(VI1 x,VI2 y, VI3 z) const {
108  // Evaluate embedded function
109  return (*_ptr)(x,y,z) ;
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,VI3) {
145  // Dummy function used when registered function was not
146  // found in un-persisting object
147  return 0 ;
148  }
149 
150 
151  typedef VO (*func_t)(VI1,VI2,VI3) ; //! Pointer to embedded function
152  func_t _ptr; //! Pointer to embedded function
153 
154  static RooCFunction3Map<VO,VI1,VI2,VI3>* _fmap ; // Pointer to mapping service object
155 
156  ClassDef(RooCFunction3Ref,1) // Persistable reference to C function pointer
157 } ;
158 
159 
160 
161 template<class VO, class VI1, class VI2, class VI3>
163 {
164  // Custom streamer for function pointer reference object. When writing,
165  // the function pointer is substituted by its registerd name. When function
166  // is unregistered name 'UNKNOWN' is written and a warning is issues. When
167  // reading back, the embedded name is converted back to a function pointer
168  // using the mapping service. When name UNKNOWN is encountered a warning is
169  // issues and a dummy null function is substituted. When the registered function
170  // name can not be mapped to a function pointer an ERROR is issued and a pointer
171  // to the dummy null function is substituted
172 
173  typedef ::RooCFunction3Ref<VO,VI1,VI2,VI3> thisClass;
174 
175  // Stream an object of class RooCFunction3Ref
176  if (R__b.IsReading()) {
177 
178  UInt_t R__s, R__c;
179  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
180 
181  // Read name from file
182  TString tmpName ;
183  tmpName.Streamer(R__b) ;
184 
185  if (tmpName=="UNKNOWN" && R__v>0) {
186 
187  coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
188  _ptr = dummyFunction ;
189 
190  } else {
191 
192  // Lookup pointer to C function wih given name
193  _ptr = fmap().lookupPtr(tmpName.Data()) ;
194 
195  if (_ptr==0) {
196  coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
197  << " but no such function is registered, object will not be functional" << std::endl ;
198  }
199  }
200 
201 
202  R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
203 
204  } else {
205 
206  UInt_t R__c;
207  R__c = R__b.WriteVersion(thisClass::IsA(), kTRUE);
208 
209  // Lookup name of reference C function
210  TString tmpName = fmap().lookupName(_ptr) ;
211  if (tmpName.Length()==0) {
212  // This union is to avoid a warning message:
213  union {
214  void *_ptr;
215  func_t _funcptr;
216  } temp;
217  temp._funcptr = _ptr;
218  coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("%p",temp._ptr)
219  << " written object will not be functional when read back" << std::endl ;
220  tmpName="UNKNOWN" ;
221  }
222 
223  // Persist the name
224  tmpName.Streamer(R__b) ;
225 
226  R__b.SetByteCount(R__c, kTRUE);
227 
228  }
229 }
230 
231 
232 
233 template<class VO,class VI1, class VI2, class VI3>
235 public:
237  // Default constructor
238  } ;
239  RooCFunction3Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z);
240  RooCFunction3Binding(const RooCFunction3Binding& other, const char* name=0) ;
241  virtual TObject* clone(const char* newname) const { return new RooCFunction3Binding(*this,newname); }
242  inline virtual ~RooCFunction3Binding() { }
243 
244  void printArgs(std::ostream& os) const {
245  // Print object arguments and name/address of function pointer
246  os << "[ function=" << func.name() << " " ;
247  for (Int_t i=0 ; i<numProxies() ; i++) {
248  RooAbsProxy* p = getProxy(i) ;
249  if (!TString(p->name()).BeginsWith("!")) {
250  p->print(os) ;
251  os << " " ;
252  }
253  }
254  os << "]" ;
255  }
256 
257 protected:
258 
259  RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
260  RooRealProxy x ; // Argument reference
261  RooRealProxy y ; // Argument reference
262  RooRealProxy z ; // Argument reference
263 
264  Double_t evaluate() const {
265  // Return value of embedded function using value of referenced variable x
266  return func(x,y,z) ;
267  }
268 
269 private:
270 
271  ClassDef(RooCFunction3Binding,1) // RooAbsReal binding to external C functions
272 };
273 
274 
275 
276 template<class VO,class VI1, class VI2, class VI3>
277 RooCFunction3Binding<VO,VI1,VI2,VI3>::RooCFunction3Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3),
278  RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
279  RooAbsReal(name,title),
280  func(_func),
281  x(func.argName(0),func.argName(0),this,_x),
282  y(func.argName(1),func.argName(1),this,_y),
283  z(func.argName(2),func.argName(2),this,_z)
284 {
285  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
286  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
287  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
288  // a RooWorkspace
289 }
290 
291 
292 template<class VO,class VI1, class VI2, class VI3>
294  RooAbsReal(other,name),
295  func(other.func),
296  x("x",this,other.x),
297  y("y",this,other.y),
298  z("z",this,other.z)
299 {
300  // Copy constructor
301 }
302 
303 
304 template<class VO,class VI1, class VI2, class VI3>
306 public:
308  // Default constructor
309  } ;
310  RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z);
311  RooCFunction3PdfBinding(const RooCFunction3PdfBinding& other, const char* name=0) ;
312  virtual TObject* clone(const char* newname) const { return new RooCFunction3PdfBinding(*this,newname); }
313  inline virtual ~RooCFunction3PdfBinding() { }
314 
315  void printArgs(std::ostream& os) const {
316  // Print object arguments and name/address of function pointer
317  os << "[ function=" << func.name() << " " ;
318  for (Int_t i=0 ; i<numProxies() ; i++) {
319  RooAbsProxy* p = getProxy(i) ;
320  if (!TString(p->name()).BeginsWith("!")) {
321  p->print(os) ;
322  os << " " ;
323  }
324  }
325  os << "]" ;
326  }
327 
328 protected:
329 
330  RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
331  RooRealProxy x ; // Argument reference
332  RooRealProxy y ; // Argument reference
333  RooRealProxy z ; // Argument reference
334 
335  Double_t evaluate() const {
336  // Return value of embedded function using value of referenced variable x
337  return func(x,y,z) ;
338  }
339 
340 private:
341 
342  ClassDef(RooCFunction3PdfBinding,1) // RooAbsReal binding to external C functions
343 };
344 
345 
346 
347 template<class VO,class VI1, class VI2, class VI3>
348 RooCFunction3PdfBinding<VO,VI1,VI2,VI3>::RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3),
349  RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
350  RooAbsPdf(name,title),
351  func(_func),
352  x(func.argName(0),func.argName(0),this,_x),
353  y(func.argName(1),func.argName(1),this,_y),
354  z(func.argName(2),func.argName(2),this,_z)
355 {
356  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
357  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
358  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
359  // a RooWorkspace
360 }
361 
362 
363 template<class VO,class VI1, class VI2, class VI3>
365  RooAbsPdf(other,name),
366  func(other.func),
367  x("x",this,other.x),
368  y("y",this,other.y),
369  z("z",this,other.z)
370 {
371  // Copy constructor
372 }
373 
374 #endif
VO(*)(VI1, VI2, VI3) lookupPtr(const char *name)
virtual void print(std::ostream &os, Bool_t addContents=kFALSE) const
Print proxy name.
Definition: RooAbsProxy.cxx:74
const char * name() const
static VO dummyFunction(VI1, VI2, VI3)
RooCFunction3Ref(VO(*ptr)(VI1, VI2, VI3)=0)
const char * argName(Int_t iarg)
Double_t(* CFUNCD3UDU)(UInt_t, Double_t, UInt_t)
Bool_t IsReading() const
Definition: TBuffer.h:81
short Version_t
Definition: RtypesCore.h:61
Ssiz_t Length() const
Definition: TString.h:390
RooCFunction3Ref< VO, VI1, VI2, VI3 > func
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
std::map< VO(*)(VI1, VI2, VI3), std::string > _namemap
Double_t(* CFUNCD3DII)(Double_t, Int_t, Int_t)
virtual TObject * clone(const char *newname) const
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
RooCFunction3Ref< VO, VI1, VI2, VI3 > func
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 * lookupName(VO(*ptr)(VI1, VI2, VI3))
#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
Double_t evaluate() const
std::map< std::string, VO(*)(VI1, VI2, VI3)> _ptrmap
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)
VO(* func_t)(VI1, VI2, VI3)
const char * Data() const
Definition: TString.h:349
const char * lookupArgName(VO(*ptr)(VI1, VI2, VI3), UInt_t iarg)
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)
std::map< VO(*)(VI1, VI2, VI3), std::vector< std::string > > _argnamemap
virtual TObject * clone(const char *newname) const
func_t _ptr
Pointer to embedded function.
Double_t(* CFUNCD3UDD)(UInt_t, Double_t, Double_t)
Int_t numProxies() const
Return the number of registered proxies.
Definition: RooAbsArg.cxx:1266
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
TClass * IsA() const
static RooCFunction3Map< VO, VI1, VI2, VI3 > * _fmap
Pointer to embedded function.
void add(const char *name, VO(*ptr)(VI1, VI2, VI3), const char *arg1name="x", const char *arg2name="y", const char *arg3name="z")
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
virtual const char * name() const
Definition: RooAbsProxy.h:42
static RooCFunction3Map< VO, VI1, VI2, VI3 > & fmap()
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_t y[n]
Definition: legend1.C:17
double func(double *x, double *p)
Definition: stressTF1.cxx:213
#define name(a, b)
Definition: linkTestLib0.cpp:5
RooCFunction3Map< double, double, double, bool > * _fmap
Mother of all ROOT objects.
Definition: TObject.h:58
Double_t(* CFUNCD3UUD)(UInt_t, UInt_t, Double_t)
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
Double_t(* CFUNCD3DDD)(Double_t, Double_t, Double_t)
double result[121]
VO operator()(VI1 x, VI2 y, VI3 z) const
const Bool_t kTRUE
Definition: Rtypes.h:91
Double_t(* CFUNCD3DDB)(Double_t, Double_t, Bool_t)
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0