ROOT logo
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 *    File: $Id$
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, NIKHEF, verkerke@nikhef.nl                         *
 *                                                                           *
 * Copyright (c) 2000-2008, NIKHEF, Regents of the University of California  *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 *****************************************************************************/

#ifndef ROOCFUNCTION1BINDING
#define ROOCFUNCTION1BINDING

#include "TString.h"
#include "RooAbsReal.h"
#include "RooAbsPdf.h"
#include "RooRealProxy.h"
#include "RooMsgService.h"
#include <string>
#include <map>
#include <vector>


namespace RooFit {

typedef Double_t (*CFUNCD1D)(Double_t) ;
typedef Double_t (*CFUNCD1I)(Int_t) ;

RooAbsReal* bindFunction(const char* name,CFUNCD1D func,RooAbsReal& x) ;
RooAbsReal* bindFunction(const char* name,CFUNCD1I func,RooAbsReal& x) ;
RooAbsPdf*  bindPdf(const char* name,CFUNCD1D func,RooAbsReal& x) ;
RooAbsPdf*  bindPdf(const char* name,CFUNCD1I func,RooAbsReal& x) ;

}


template<class VO, class VI>
class RooCFunction1Map {
 public:
  RooCFunction1Map() {} ;

  void add(const char* name, VO (*ptr)(VI), const char* arg1name="x") {
    // Register function with given name and argument name
    _ptrmap[name] = ptr ;
    _namemap[ptr] = name ;
    _argnamemap[ptr].push_back(arg1name) ;
  }
  

  const char* lookupName(VO (*ptr)(VI)) {
    // Return name of function given by pointer
    return _namemap[ptr].c_str() ;
  }
    
  VO (*lookupPtr(const char* name))(VI) {
    // Return pointer of function given by name
    return _ptrmap[name] ;
  }

  const char* lookupArgName(VO (*ptr)(VI), UInt_t iarg) {
    // Return name of i-th argument of function. If function is
    // not registered, argument names 0,1,2 are x,y,z
    if (iarg<_argnamemap[ptr].size()) {
      return (_argnamemap[ptr])[iarg].c_str() ;
    }
    switch (iarg) {
    case 0: return "x" ;
    case 1: return "y" ;
    case 2: return "z" ;
    }
    return "w" ;
  }
  
 private:

#ifndef __CINT__
  std::map<std::string,VO (*)(VI)> _ptrmap ; // Pointer-to-name map
  std::map<VO (*)(VI),std::string> _namemap ; // Name-to-pointer map
  std::map<VO (*)(VI),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
#endif
} ;



template<class VO, class VI>
class RooCFunction1Ref : public TObject {
 public:
  RooCFunction1Ref(VO (*ptr)(VI)=0) : _ptr(ptr) {
    // Constructor of persistable function reference
  } ;
  ~RooCFunction1Ref() {} ;

  VO operator()(VI x) const {
    // Evaluate embedded function
    return (*_ptr)(x) ;
  }

  const char* name() const {
    // Return registered name of embedded function. If function
    // is not registered return string with hex presentation
    // of function pointer value
    const char* result = fmap().lookupName(_ptr) ;
    if (result && strlen(result)) {
      return result ;
    } 
    // This union is to avoid a warning message:
    union { 
       void *_ptr;
       func_t _funcptr;
    } temp;
    temp._funcptr = _ptr;
    return Form("(%p)",temp._ptr) ;
  }

  const char* argName(Int_t iarg) {
    // Return suggested name for i-th argument
    return fmap().lookupArgName(_ptr,iarg) ;    
  }

 static RooCFunction1Map<VO,VI>& fmap();

 private:

  static VO dummyFunction(VI) {
    // Dummy function used when registered function was not
    // found in un-persisting object
    return 0 ;
  }

  typedef VO (*func_t)(VI); 
  func_t _ptr; //! Pointer to embedded function

  static RooCFunction1Map<VO,VI>* _fmap ; // Pointer to mapping service object

  ClassDef(RooCFunction1Ref,1) // Persistable reference to C function pointer
} ;



template<class VO, class VI>
void RooCFunction1Ref<VO,VI>::Streamer(TBuffer &R__b)
{
  // Custom streamer for function pointer reference object. When writing,
  // the function pointer is substituted by its registered name. When function
  // is unregistered name 'UNKNOWN' is written and a warning is issues. When
  // reading back, the embedded name is converted back to a function pointer
  // using the mapping service. When name UNKNOWN is encountered a warning is
  // issues and a dummy null function is substituted. When the registered function
  // name can not be mapped to a function pointer an ERROR is issued and a pointer
  // to the dummy null function is substituted

  typedef ::RooCFunction1Ref<VO,VI> thisClass;

   // Stream an object of class RooCFunction1Ref
   if (R__b.IsReading()) {

     UInt_t R__s, R__c;
     Version_t R__v = R__b.ReadVersion(&R__s, &R__c);      

     // Read name from file
     TString tmpName ;
     tmpName.Streamer(R__b) ;       

     if (tmpName=="UNKNOWN" && R__v>0) {

       coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
       _ptr = dummyFunction ;

     } else {
     
       // Lookup pointer to C function wih given name
       _ptr = fmap().lookupPtr(tmpName.Data()) ;

       if (_ptr==0) {
	 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName 
			       << " but no such function is registered, object will not be functional" << std::endl ;
       }
     }


     R__b.CheckByteCount(R__s, R__c, thisClass::IsA());

   } else {
     
     UInt_t R__c;
     R__c = R__b.WriteVersion(thisClass::IsA(), kTRUE);    

     // Lookup name of reference C function
     TString tmpName = fmap().lookupName(_ptr) ;
     if (tmpName.Length()==0) {
        // This union is to avoid a warning message:
        union { 
           void *_ptr;
           func_t _funcptr;
        } temp;
        temp._funcptr = _ptr;
        coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("%p",temp._ptr) 
                              << " written object will not be functional when read back" <<  std::endl ;
       tmpName="UNKNOWN" ;
     } 
     
     // Persist the name
     tmpName.Streamer(R__b) ;            

     R__b.SetByteCount(R__c, kTRUE);
     
   }
}



template<class VO,class VI>
class RooCFunction1Binding : public RooAbsReal {
public:
  RooCFunction1Binding() {
    // Default constructor
  } ; 
  RooCFunction1Binding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x);
  RooCFunction1Binding(const RooCFunction1Binding& other, const char* name=0) ;
  virtual TObject* clone(const char* newname) const { return new RooCFunction1Binding(*this,newname); }
  inline virtual ~RooCFunction1Binding() { }

  void printArgs(std::ostream& os) const {
    // Print object arguments and name/address of function pointer
    os << "[ function=" << func.name() << " " ;    
    for (Int_t i=0 ; i<numProxies() ; i++) {
      RooAbsProxy* p = getProxy(i) ;
      if (!TString(p->name()).BeginsWith("!")) {
	p->print(os) ;
	os << " " ;
      }
    }    
    os << "]" ;  
  }

protected:

  RooCFunction1Ref<VO,VI> func ; // Function pointer reference
  RooRealProxy x ;              // Argument reference
  
  Double_t evaluate() const {
    // Return value of embedded function using value of referenced variable x
    return func(x) ;
  }

private:

  ClassDef(RooCFunction1Binding,1) // RooAbsReal binding to external C functions
};


template<class VO,class VI>
RooCFunction1Binding<VO,VI>::RooCFunction1Binding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x) :
  RooAbsReal(name,title), 
  func(_func),
  x(func.argName(0),func.argName(0),this,_x)
{ 
  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
  // a RooWorkspace
} 


template<class VO,class VI>
RooCFunction1Binding<VO,VI>::RooCFunction1Binding(const RooCFunction1Binding& other, const char* name) :  
  RooAbsReal(other,name), 
  func(other.func),
  x("x",this,other.x)
{ 
  // Copy constructor
} 



template<class VO,class VI>
class RooCFunction1PdfBinding : public RooAbsPdf {
public:
  RooCFunction1PdfBinding() {
    // Default constructor
  } ; 
  RooCFunction1PdfBinding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x);
  RooCFunction1PdfBinding(const RooCFunction1PdfBinding& other, const char* name=0) ;
  virtual TObject* clone(const char* newname) const { return new RooCFunction1PdfBinding(*this,newname); }
  inline virtual ~RooCFunction1PdfBinding() { }

  void printArgs(std::ostream& os) const {
    // Print object arguments and name/address of function pointer
    os << "[ function=" << func.name() << " " ;    
    for (Int_t i=0 ; i<numProxies() ; i++) {
      RooAbsProxy* p = getProxy(i) ;
      if (!TString(p->name()).BeginsWith("!")) {
	p->print(os) ;
	os << " " ;
      }
    }    
    os << "]" ;  
  }

protected:

  RooCFunction1Ref<VO,VI> func ; // Function pointer reference
  RooRealProxy x ;              // Argument reference
  
  Double_t evaluate() const {
    // Return value of embedded function using value of referenced variable x
    return func(x) ;
  }

private:

  ClassDef(RooCFunction1PdfBinding,1) // RooAbsReal binding to external C functions
};


template<class VO,class VI>
RooCFunction1PdfBinding<VO,VI>::RooCFunction1PdfBinding(const char *name, const char *title, VO (*_func)(VI), RooAbsReal& _x) :
  RooAbsPdf(name,title), 
  func(_func),
  x(func.argName(0),func.argName(0),this,_x)
{ 
  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
  // a RooWorkspace
} 


template<class VO,class VI>
RooCFunction1PdfBinding<VO,VI>::RooCFunction1PdfBinding(const RooCFunction1PdfBinding& other, const char* name) :  
  RooAbsPdf(other,name), 
  func(other.func),
  x("x",this,other.x)
{ 
  // Copy constructor
} 

#endif
 RooCFunction1Binding.h:1
 RooCFunction1Binding.h:2
 RooCFunction1Binding.h:3
 RooCFunction1Binding.h:4
 RooCFunction1Binding.h:5
 RooCFunction1Binding.h:6
 RooCFunction1Binding.h:7
 RooCFunction1Binding.h:8
 RooCFunction1Binding.h:9
 RooCFunction1Binding.h:10
 RooCFunction1Binding.h:11
 RooCFunction1Binding.h:12
 RooCFunction1Binding.h:13
 RooCFunction1Binding.h:14
 RooCFunction1Binding.h:15
 RooCFunction1Binding.h:16
 RooCFunction1Binding.h:17
 RooCFunction1Binding.h:18
 RooCFunction1Binding.h:19
 RooCFunction1Binding.h:20
 RooCFunction1Binding.h:21
 RooCFunction1Binding.h:22
 RooCFunction1Binding.h:23
 RooCFunction1Binding.h:24
 RooCFunction1Binding.h:25
 RooCFunction1Binding.h:26
 RooCFunction1Binding.h:27
 RooCFunction1Binding.h:28
 RooCFunction1Binding.h:29
 RooCFunction1Binding.h:30
 RooCFunction1Binding.h:31
 RooCFunction1Binding.h:32
 RooCFunction1Binding.h:33
 RooCFunction1Binding.h:34
 RooCFunction1Binding.h:35
 RooCFunction1Binding.h:36
 RooCFunction1Binding.h:37
 RooCFunction1Binding.h:38
 RooCFunction1Binding.h:39
 RooCFunction1Binding.h:40
 RooCFunction1Binding.h:41
 RooCFunction1Binding.h:42
 RooCFunction1Binding.h:43
 RooCFunction1Binding.h:44
 RooCFunction1Binding.h:45
 RooCFunction1Binding.h:46
 RooCFunction1Binding.h:47
 RooCFunction1Binding.h:48
 RooCFunction1Binding.h:49
 RooCFunction1Binding.h:50
 RooCFunction1Binding.h:51
 RooCFunction1Binding.h:52
 RooCFunction1Binding.h:53
 RooCFunction1Binding.h:54
 RooCFunction1Binding.h:55
 RooCFunction1Binding.h:56
 RooCFunction1Binding.h:57
 RooCFunction1Binding.h:58
 RooCFunction1Binding.h:59
 RooCFunction1Binding.h:60
 RooCFunction1Binding.h:61
 RooCFunction1Binding.h:62
 RooCFunction1Binding.h:63
 RooCFunction1Binding.h:64
 RooCFunction1Binding.h:65
 RooCFunction1Binding.h:66
 RooCFunction1Binding.h:67
 RooCFunction1Binding.h:68
 RooCFunction1Binding.h:69
 RooCFunction1Binding.h:70
 RooCFunction1Binding.h:71
 RooCFunction1Binding.h:72
 RooCFunction1Binding.h:73
 RooCFunction1Binding.h:74
 RooCFunction1Binding.h:75
 RooCFunction1Binding.h:76
 RooCFunction1Binding.h:77
 RooCFunction1Binding.h:78
 RooCFunction1Binding.h:79
 RooCFunction1Binding.h:80
 RooCFunction1Binding.h:81
 RooCFunction1Binding.h:82
 RooCFunction1Binding.h:83
 RooCFunction1Binding.h:84
 RooCFunction1Binding.h:85
 RooCFunction1Binding.h:86
 RooCFunction1Binding.h:87
 RooCFunction1Binding.h:88
 RooCFunction1Binding.h:89
 RooCFunction1Binding.h:90
 RooCFunction1Binding.h:91
 RooCFunction1Binding.h:92
 RooCFunction1Binding.h:93
 RooCFunction1Binding.h:94
 RooCFunction1Binding.h:95
 RooCFunction1Binding.h:96
 RooCFunction1Binding.h:97
 RooCFunction1Binding.h:98
 RooCFunction1Binding.h:99
 RooCFunction1Binding.h:100
 RooCFunction1Binding.h:101
 RooCFunction1Binding.h:102
 RooCFunction1Binding.h:103
 RooCFunction1Binding.h:104
 RooCFunction1Binding.h:105
 RooCFunction1Binding.h:106
 RooCFunction1Binding.h:107
 RooCFunction1Binding.h:108
 RooCFunction1Binding.h:109
 RooCFunction1Binding.h:110
 RooCFunction1Binding.h:111
 RooCFunction1Binding.h:112
 RooCFunction1Binding.h:113
 RooCFunction1Binding.h:114
 RooCFunction1Binding.h:115
 RooCFunction1Binding.h:116
 RooCFunction1Binding.h:117
 RooCFunction1Binding.h:118
 RooCFunction1Binding.h:119
 RooCFunction1Binding.h:120
 RooCFunction1Binding.h:121
 RooCFunction1Binding.h:122
 RooCFunction1Binding.h:123
 RooCFunction1Binding.h:124
 RooCFunction1Binding.h:125
 RooCFunction1Binding.h:126
 RooCFunction1Binding.h:127
 RooCFunction1Binding.h:128
 RooCFunction1Binding.h:129
 RooCFunction1Binding.h:130
 RooCFunction1Binding.h:131
 RooCFunction1Binding.h:132
 RooCFunction1Binding.h:133
 RooCFunction1Binding.h:134
 RooCFunction1Binding.h:135
 RooCFunction1Binding.h:136
 RooCFunction1Binding.h:137
 RooCFunction1Binding.h:138
 RooCFunction1Binding.h:139
 RooCFunction1Binding.h:140
 RooCFunction1Binding.h:141
 RooCFunction1Binding.h:142
 RooCFunction1Binding.h:143
 RooCFunction1Binding.h:144
 RooCFunction1Binding.h:145
 RooCFunction1Binding.h:146
 RooCFunction1Binding.h:147
 RooCFunction1Binding.h:148
 RooCFunction1Binding.h:149
 RooCFunction1Binding.h:150
 RooCFunction1Binding.h:151
 RooCFunction1Binding.h:152
 RooCFunction1Binding.h:153
 RooCFunction1Binding.h:154
 RooCFunction1Binding.h:155
 RooCFunction1Binding.h:156
 RooCFunction1Binding.h:157
 RooCFunction1Binding.h:158
 RooCFunction1Binding.h:159
 RooCFunction1Binding.h:160
 RooCFunction1Binding.h:161
 RooCFunction1Binding.h:162
 RooCFunction1Binding.h:163
 RooCFunction1Binding.h:164
 RooCFunction1Binding.h:165
 RooCFunction1Binding.h:166
 RooCFunction1Binding.h:167
 RooCFunction1Binding.h:168
 RooCFunction1Binding.h:169
 RooCFunction1Binding.h:170
 RooCFunction1Binding.h:171
 RooCFunction1Binding.h:172
 RooCFunction1Binding.h:173
 RooCFunction1Binding.h:174
 RooCFunction1Binding.h:175
 RooCFunction1Binding.h:176
 RooCFunction1Binding.h:177
 RooCFunction1Binding.h:178
 RooCFunction1Binding.h:179
 RooCFunction1Binding.h:180
 RooCFunction1Binding.h:181
 RooCFunction1Binding.h:182
 RooCFunction1Binding.h:183
 RooCFunction1Binding.h:184
 RooCFunction1Binding.h:185
 RooCFunction1Binding.h:186
 RooCFunction1Binding.h:187
 RooCFunction1Binding.h:188
 RooCFunction1Binding.h:189
 RooCFunction1Binding.h:190
 RooCFunction1Binding.h:191
 RooCFunction1Binding.h:192
 RooCFunction1Binding.h:193
 RooCFunction1Binding.h:194
 RooCFunction1Binding.h:195
 RooCFunction1Binding.h:196
 RooCFunction1Binding.h:197
 RooCFunction1Binding.h:198
 RooCFunction1Binding.h:199
 RooCFunction1Binding.h:200
 RooCFunction1Binding.h:201
 RooCFunction1Binding.h:202
 RooCFunction1Binding.h:203
 RooCFunction1Binding.h:204
 RooCFunction1Binding.h:205
 RooCFunction1Binding.h:206
 RooCFunction1Binding.h:207
 RooCFunction1Binding.h:208
 RooCFunction1Binding.h:209
 RooCFunction1Binding.h:210
 RooCFunction1Binding.h:211
 RooCFunction1Binding.h:212
 RooCFunction1Binding.h:213
 RooCFunction1Binding.h:214
 RooCFunction1Binding.h:215
 RooCFunction1Binding.h:216
 RooCFunction1Binding.h:217
 RooCFunction1Binding.h:218
 RooCFunction1Binding.h:219
 RooCFunction1Binding.h:220
 RooCFunction1Binding.h:221
 RooCFunction1Binding.h:222
 RooCFunction1Binding.h:223
 RooCFunction1Binding.h:224
 RooCFunction1Binding.h:225
 RooCFunction1Binding.h:226
 RooCFunction1Binding.h:227
 RooCFunction1Binding.h:228
 RooCFunction1Binding.h:229
 RooCFunction1Binding.h:230
 RooCFunction1Binding.h:231
 RooCFunction1Binding.h:232
 RooCFunction1Binding.h:233
 RooCFunction1Binding.h:234
 RooCFunction1Binding.h:235
 RooCFunction1Binding.h:236
 RooCFunction1Binding.h:237
 RooCFunction1Binding.h:238
 RooCFunction1Binding.h:239
 RooCFunction1Binding.h:240
 RooCFunction1Binding.h:241
 RooCFunction1Binding.h:242
 RooCFunction1Binding.h:243
 RooCFunction1Binding.h:244
 RooCFunction1Binding.h:245
 RooCFunction1Binding.h:246
 RooCFunction1Binding.h:247
 RooCFunction1Binding.h:248
 RooCFunction1Binding.h:249
 RooCFunction1Binding.h:250
 RooCFunction1Binding.h:251
 RooCFunction1Binding.h:252
 RooCFunction1Binding.h:253
 RooCFunction1Binding.h:254
 RooCFunction1Binding.h:255
 RooCFunction1Binding.h:256
 RooCFunction1Binding.h:257
 RooCFunction1Binding.h:258
 RooCFunction1Binding.h:259
 RooCFunction1Binding.h:260
 RooCFunction1Binding.h:261
 RooCFunction1Binding.h:262
 RooCFunction1Binding.h:263
 RooCFunction1Binding.h:264
 RooCFunction1Binding.h:265
 RooCFunction1Binding.h:266
 RooCFunction1Binding.h:267
 RooCFunction1Binding.h:268
 RooCFunction1Binding.h:269
 RooCFunction1Binding.h:270
 RooCFunction1Binding.h:271
 RooCFunction1Binding.h:272
 RooCFunction1Binding.h:273
 RooCFunction1Binding.h:274
 RooCFunction1Binding.h:275
 RooCFunction1Binding.h:276
 RooCFunction1Binding.h:277
 RooCFunction1Binding.h:278
 RooCFunction1Binding.h:279
 RooCFunction1Binding.h:280
 RooCFunction1Binding.h:281
 RooCFunction1Binding.h:282
 RooCFunction1Binding.h:283
 RooCFunction1Binding.h:284
 RooCFunction1Binding.h:285
 RooCFunction1Binding.h:286
 RooCFunction1Binding.h:287
 RooCFunction1Binding.h:288
 RooCFunction1Binding.h:289
 RooCFunction1Binding.h:290
 RooCFunction1Binding.h:291
 RooCFunction1Binding.h:292
 RooCFunction1Binding.h:293
 RooCFunction1Binding.h:294
 RooCFunction1Binding.h:295
 RooCFunction1Binding.h:296
 RooCFunction1Binding.h:297
 RooCFunction1Binding.h:298
 RooCFunction1Binding.h:299
 RooCFunction1Binding.h:300
 RooCFunction1Binding.h:301
 RooCFunction1Binding.h:302
 RooCFunction1Binding.h:303
 RooCFunction1Binding.h:304
 RooCFunction1Binding.h:305
 RooCFunction1Binding.h:306
 RooCFunction1Binding.h:307
 RooCFunction1Binding.h:308
 RooCFunction1Binding.h:309
 RooCFunction1Binding.h:310
 RooCFunction1Binding.h:311
 RooCFunction1Binding.h:312
 RooCFunction1Binding.h:313
 RooCFunction1Binding.h:314
 RooCFunction1Binding.h:315
 RooCFunction1Binding.h:316
 RooCFunction1Binding.h:317
 RooCFunction1Binding.h:318
 RooCFunction1Binding.h:319
 RooCFunction1Binding.h:320
 RooCFunction1Binding.h:321
 RooCFunction1Binding.h:322
 RooCFunction1Binding.h:323
 RooCFunction1Binding.h:324
 RooCFunction1Binding.h:325
 RooCFunction1Binding.h:326
 RooCFunction1Binding.h:327
 RooCFunction1Binding.h:328
 RooCFunction1Binding.h:329
 RooCFunction1Binding.h:330
 RooCFunction1Binding.h:331
 RooCFunction1Binding.h:332
 RooCFunction1Binding.h:333
 RooCFunction1Binding.h:334
 RooCFunction1Binding.h:335
 RooCFunction1Binding.h:336
 RooCFunction1Binding.h:337
 RooCFunction1Binding.h:338
 RooCFunction1Binding.h:339
 RooCFunction1Binding.h:340