Logo ROOT   6.12/07
Reference Guide
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 // Define static member
157 template<class VO, class VI1, class VI2, class VI3, class VI4>
159 
160 template<class VO, class VI1, class VI2, class VI3, class VI4>
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 ::RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> thisClass;
173 
174  // Stream an object of class RooCFunction4Ref
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, class VI3, class VI4>
228 public:
230  // Default constructor
231  } ;
232  RooCFunction4Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w);
233  RooCFunction4Binding(const RooCFunction4Binding& other, const char* name=0) ;
234  virtual TObject* clone(const char* newname) const { return new RooCFunction4Binding(*this,newname); }
235  inline virtual ~RooCFunction4Binding() { }
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  RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> func ; // Function pointer reference
253  RooRealProxy x ; // Argument reference
254  RooRealProxy y ; // Argument reference
255  RooRealProxy z ; // Argument reference
256  RooRealProxy w ; // Argument reference
257 
258  Double_t evaluate() const {
259  // Return value of embedded function using value of referenced variable x
260  return func(x,y,z,w) ;
261  }
262 
263 private:
264 
265  ClassDef(RooCFunction4Binding,1) // RooAbsReal binding to external C functions
266 };
267 
268 
269 template<class VO,class VI1, class VI2, class VI3, class VI4>
270 RooCFunction4Binding<VO,VI1,VI2,VI3,VI4>::RooCFunction4Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4),
271  RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w) :
272  RooAbsReal(name,title),
273  func(_func),
274  x(func.argName(0),func.argName(0),this,_x),
275  y(func.argName(1),func.argName(1),this,_y),
276  z(func.argName(2),func.argName(2),this,_z),
277  w(func.argName(3),func.argName(3),this,_w)
278 {
279  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
280  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
281  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
282  // a RooWorkspace
283 }
284 
285 
286 template<class VO,class VI1, class VI2, class VI3, class VI4>
288  RooAbsReal(other,name),
289  func(other.func),
290  x("x",this,other.x),
291  y("y",this,other.y),
292  z("z",this,other.z),
293  w("w",this,other.w)
294 {
295  // Copy constructor
296 }
297 
298 
299 template<class VO,class VI1, class VI2, class VI3, class VI4>
301 public:
303  // Default constructor
304  } ;
305  RooCFunction4PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w);
306  RooCFunction4PdfBinding(const RooCFunction4PdfBinding& other, const char* name=0) ;
307  virtual TObject* clone(const char* newname) const { return new RooCFunction4PdfBinding(*this,newname); }
308  inline virtual ~RooCFunction4PdfBinding() { }
309 
310  void printArgs(std::ostream& os) const {
311  // Print object arguments and name/address of function pointer
312  os << "[ function=" << func.name() << " " ;
313  for (Int_t i=0 ; i<numProxies() ; i++) {
314  RooAbsProxy* p = getProxy(i) ;
315  if (!TString(p->name()).BeginsWith("!")) {
316  p->print(os) ;
317  os << " " ;
318  }
319  }
320  os << "]" ;
321  }
322 
323 protected:
324 
325  RooCFunction4Ref<VO,VI1,VI2,VI3,VI4> func ; // Function pointer reference
326  RooRealProxy x ; // Argument reference
327  RooRealProxy y ; // Argument reference
328  RooRealProxy z ; // Argument reference
329  RooRealProxy w ; // Argument reference
330 
331  Double_t evaluate() const {
332  // Return value of embedded function using value of referenced variable x
333  return func(x,y,z,w) ;
334  }
335 
336 private:
337 
338  ClassDef(RooCFunction4PdfBinding,1) // RooAbsReal binding to external C functions
339 };
340 
341 
342 template<class VO,class VI1, class VI2, class VI3, class VI4>
343 RooCFunction4PdfBinding<VO,VI1,VI2,VI3,VI4>::RooCFunction4PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3,VI4),
344  RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z, RooAbsReal& _w) :
345  RooAbsPdf(name,title),
346  func(_func),
347  x(func.argName(0),func.argName(0),this,_x),
348  y(func.argName(1),func.argName(1),this,_y),
349  z(func.argName(2),func.argName(2),this,_z),
350  w(func.argName(3),func.argName(3),this,_w)
351 {
352  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
353  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
354  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
355  // a RooWorkspace
356 }
357 
358 
359 template<class VO,class VI1, class VI2, class VI3, class VI4>
361  RooAbsPdf(other,name),
362  func(other.func),
363  x("x",this,other.x),
364  y("y",this,other.y),
365  z("z",this,other.z),
366  w("w",this,other.w)
367 {
368  // Copy constructor
369 }
370 
371 #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")
Bool_t IsReading() const
Definition: TBuffer.h:83
VO operator()(VI1 x, VI2 y, VI3 z, VI4 w) const
static VO dummyFunction(VI1, VI2, VI3, VI4)
virtual const char * name() const
Definition: RooAbsProxy.h:41
short Version_t
Definition: RtypesCore.h:61
const char * lookupName(VO(*ptr)(VI1, VI2, VI3, VI4))
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
Double_t(* CFUNCD4DDDB)(Double_t, Double_t, Double_t, Bool_t)
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
Double_t(* CFUNCD4DDDI)(Double_t, Double_t, Double_t, Int_t)
Basic string class.
Definition: TString.h:125
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:33
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
std::map< VO(*)(VI1, VI2, VI3, VI4), std::string > _namemap
std::map< std::string, VO(*)(VI1, VI2, VI3, VI4)> _ptrmap
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)
const char * name() const
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:320
RooCFunction4Binding is a templated implementation of class RooAbsReal that binds generic C(++) funct...
RooCFunction4Ref< VO, VI1, VI2, VI3, VI4 > func
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
RooCFunction4Ref(VO(*ptr)(VI1, VI2, VI3, VI4)=0)
std::map< VO(*)(VI1, VI2, VI3, VI4), std::vector< std::string > > _argnamemap
RooAbsProxy is the abstact interface for proxy classes.
Definition: RooAbsProxy.h:31
static RooCFunction4Map< VO, VI1, VI2, VI3, VI4 > * _fmap
Pointer to embedded function.
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:386
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
Double_t(* CFUNCD4DDDD)(Double_t, Double_t, Double_t, Double_t)
static RooCFunction4Map< VO, VI1, VI2, VI3, VI4 > & fmap()
virtual TObject * clone(const char *newname) 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
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
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
const char * lookupArgName(VO(*ptr)(VI1, VI2, VI3, VI4), UInt_t iarg)
RooAbsProxy * getProxy(Int_t index) const
Return the nth proxy from the proxy list.
Definition: RooAbsArg.cxx:1250
Mother of all ROOT objects.
Definition: TObject.h:37
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
Int_t numProxies() const
Return the number of registered proxies.
Definition: RooAbsArg.cxx:1263
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
virtual TObject * clone(const char *newname) const
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual void print(std::ostream &os, Bool_t addContents=kFALSE) const
Print proxy name.
Definition: RooAbsProxy.cxx:75
char name[80]
Definition: TGX11.cxx:109
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
const char * Data() const
Definition: TString.h:345