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