Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/RooCFunction3Binding.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  * Package: RooFitCore                                                       *
0004  *    File: $Id$
0005  * Authors:                                                                   *   WV, Wouter Verkerke, NIKHEF, verkerke@nikhef.nl                         *
0006  *                                                                           *
0007  * Copyright (c) 2000-2008, NIKHEF, Regents of the University of California  *
0008  *                          and Stanford University. All rights reserved.    *
0009  *                                                                           *
0010  *****************************************************************************/
0011 
0012 #ifndef ROOCFUNCTION3BINDING
0013 #define ROOCFUNCTION3BINDING
0014 
0015 #include "RooAbsReal.h"
0016 #include "RooRealProxy.h"
0017 #include "RooMsgService.h"
0018 #include "RooAbsPdf.h"
0019 
0020 #include "TBuffer.h"
0021 #include "TString.h"
0022 
0023 #include <string>
0024 #include <map>
0025 #include <vector>
0026 
0027 
0028 namespace RooFit {
0029 
0030 typedef double (*CFUNCD3DDD)(double,double,double) ;
0031 typedef double (*CFUNCD3DDB)(double,double,bool) ;
0032 typedef double (*CFUNCD3DII)(double,Int_t,Int_t) ;
0033 typedef double (*CFUNCD3UDU)(UInt_t,double,UInt_t) ;
0034 typedef double (*CFUNCD3UDD)(UInt_t,double,double) ;
0035 typedef double (*CFUNCD3UUD)(UInt_t,UInt_t,double) ;
0036 
0037 RooAbsReal* bindFunction(const char* name,CFUNCD3DDD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0038 RooAbsReal* bindFunction(const char* name,CFUNCD3DDB func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0039 RooAbsReal* bindFunction(const char* name,CFUNCD3DII func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0040 RooAbsReal* bindFunction(const char* name,CFUNCD3UDU func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0041 RooAbsReal* bindFunction(const char* name,CFUNCD3UDD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0042 RooAbsReal* bindFunction(const char* name,CFUNCD3UUD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0043 RooAbsPdf* bindPdf(const char* name,CFUNCD3DDD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0044 RooAbsPdf* bindPdf(const char* name,CFUNCD3DDB func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0045 RooAbsPdf* bindPdf(const char* name,CFUNCD3DII func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0046 RooAbsPdf* bindPdf(const char* name,CFUNCD3UDU func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0047 RooAbsPdf* bindPdf(const char* name,CFUNCD3UDD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0048 RooAbsPdf* bindPdf(const char* name,CFUNCD3UUD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
0049 
0050 }
0051 
0052 template<class VO, class VI1, class VI2, class VI3>
0053 class RooCFunction3Map {
0054  public:
0055   RooCFunction3Map() {} ;
0056 
0057   void add(const char* name, VO (*ptr)(VI1,VI2,VI3), const char* arg1name="x", const char* arg2name="y", const char* arg3name="z") {
0058     // Register function with given name and argument name
0059     _ptrmap[name] = ptr ;
0060     _namemap[ptr] = name ;
0061     _argnamemap[ptr].push_back(arg1name) ;
0062     _argnamemap[ptr].push_back(arg2name) ;
0063     _argnamemap[ptr].push_back(arg3name) ;
0064   }
0065 
0066 
0067   const char* lookupName(VO (*ptr)(VI1,VI2,VI3)) {
0068     // Return name of function given by pointer
0069     return _namemap[ptr].c_str() ;
0070   }
0071 
0072   VO (*lookupPtr(const char* name))(VI1,VI2,VI3) {
0073     // Return pointer of function given by name
0074     return _ptrmap[name] ;
0075   }
0076 
0077   const char* lookupArgName(VO (*ptr)(VI1,VI2,VI3), UInt_t iarg) {
0078     // Return name of i-th argument of function. If function is
0079     // not registered, argument names 0,1,2 are x,y,z
0080     if (iarg<_argnamemap[ptr].size()) {
0081       return (_argnamemap[ptr])[iarg].c_str() ;
0082     }
0083     switch (iarg) {
0084     case 0: return "x" ;
0085     case 1: return "y" ;
0086     case 2: return "z" ;
0087     }
0088     return "w" ;
0089   }
0090 
0091  private:
0092 
0093   std::map<std::string,VO (*)(VI1,VI2,VI3)> _ptrmap ; // Pointer-to-name map
0094   std::map<VO (*)(VI1,VI2,VI3),std::string> _namemap ; // Name-to-pointer map
0095   std::map<VO (*)(VI1,VI2,VI3),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
0096 } ;
0097 
0098 
0099 template<class VO, class VI1, class VI2, class VI3>
0100 class RooCFunction3Ref : public TObject {
0101  public:
0102   RooCFunction3Ref(VO (*ptr)(VI1,VI2,VI3)=nullptr) : _ptr(ptr) {
0103     // Constructor of persistable function reference
0104   } ;
0105 
0106   VO operator()(VI1 x,VI2 y, VI3 z) const {
0107     // Evaluate embedded function
0108     return (*_ptr)(x,y,z) ;
0109   }
0110 
0111   const char* name() const {
0112     // Return registered name of embedded function. If function
0113     // is not registered return string with hex presentation
0114     // of function pointer value
0115     const char* result = fmap().lookupName(_ptr) ;
0116     if (result && strlen(result)) {
0117       return result ;
0118     }
0119     // This union is to avoid a warning message:
0120     union {
0121        void *_ptr;
0122        func_t _funcptr;
0123     } temp;
0124     temp._funcptr = _ptr;
0125     return Form("(%p)",temp._ptr) ;
0126   }
0127 
0128   const char* argName(Int_t iarg) {
0129     // Return suggested name for i-th argument
0130     return fmap().lookupArgName(_ptr,iarg) ;
0131   }
0132 
0133   static RooCFunction3Map<VO,VI1,VI2,VI3>& fmap() {
0134     // Return reference to function pointer-to-name mapping service
0135     if (!_fmap) {
0136       _fmap = new RooCFunction3Map<VO,VI1,VI2,VI3> ;
0137     }
0138     return *_fmap ;
0139   }
0140 
0141  private:
0142 
0143   static VO dummyFunction(VI1,VI2,VI3) {
0144     // Dummy function used when registered function was not
0145     // found in un-persisting object
0146     return 0 ;
0147   }
0148 
0149 
0150   typedef VO (*func_t)(VI1,VI2,VI3) ; //! Pointer to embedded function
0151   func_t _ptr; //! Pointer to embedded function
0152 
0153   static RooCFunction3Map<VO,VI1,VI2,VI3>* _fmap ; // Pointer to mapping service object
0154 
0155   ClassDefOverride(RooCFunction3Ref,1) // Persistable reference to C function pointer
0156 } ;
0157 
0158 // Define static member
0159 template<class VO, class VI1, class VI2, class VI3>
0160 RooCFunction3Map<VO,VI1,VI2,VI3>* RooCFunction3Ref<VO,VI1,VI2,VI3>::_fmap = nullptr;
0161 
0162 
0163 
0164 template<class VO, class VI1, class VI2, class VI3>
0165 void RooCFunction3Ref<VO,VI1,VI2,VI3>::Streamer(TBuffer &R__b)
0166 {
0167   // Custom streamer for function pointer reference object. When writing,
0168   // the function pointer is substituted by its registered name. When function
0169   // is unregistered name 'UNKNOWN' is written and a warning is issues. When
0170   // reading back, the embedded name is converted back to a function pointer
0171   // using the mapping service. When name UNKNOWN is encountered a warning is
0172   // issues and a dummy null function is substituted. When the registered function
0173   // name can not be mapped to a function pointer an ERROR is issued and a pointer
0174   // to the dummy null function is substituted
0175 
0176   typedef ::RooCFunction3Ref<VO,VI1,VI2,VI3> thisClass;
0177 
0178    // Stream an object of class RooCFunction3Ref
0179    if (R__b.IsReading()) {
0180 
0181      UInt_t R__s;
0182      UInt_t R__c;
0183      Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
0184 
0185      // Read name from file
0186      TString tmpName ;
0187      tmpName.Streamer(R__b) ;
0188 
0189      if (tmpName=="UNKNOWN" && R__v>0) {
0190 
0191        coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
0192        _ptr = dummyFunction ;
0193 
0194      } else {
0195 
0196        // Lookup pointer to C function with given name
0197        _ptr = fmap().lookupPtr(tmpName.Data()) ;
0198 
0199        if (_ptr==nullptr) {
0200     coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName
0201                 << " but no such function is registered, object will not be functional" << std::endl ;
0202        }
0203      }
0204 
0205 
0206      R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
0207 
0208    } else {
0209 
0210      UInt_t R__c;
0211      R__c = R__b.WriteVersion(thisClass::IsA(), true);
0212 
0213      // Lookup name of reference C function
0214      TString tmpName = fmap().lookupName(_ptr) ;
0215      if (tmpName.Length()==0) {
0216        // This union is to avoid a warning message:
0217        union {
0218           void *_ptr;
0219           func_t _funcptr;
0220        } temp;
0221        temp._funcptr = _ptr;
0222        coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("%p",temp._ptr)
0223               << " written object will not be functional when read back" <<  std::endl ;
0224        tmpName="UNKNOWN" ;
0225      }
0226 
0227      // Persist the name
0228      tmpName.Streamer(R__b) ;
0229 
0230      R__b.SetByteCount(R__c, true);
0231 
0232    }
0233 }
0234 
0235 
0236 
0237 template<class VO,class VI1, class VI2, class VI3>
0238 class RooCFunction3Binding : public RooAbsReal {
0239 public:
0240   RooCFunction3Binding() {
0241     // Default constructor
0242   } ;
0243   RooCFunction3Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z);
0244   RooCFunction3Binding(const RooCFunction3Binding& other, const char* name=nullptr) ;
0245   TObject* clone(const char* newname) const override { return new RooCFunction3Binding(*this,newname); }
0246 
0247   void printArgs(std::ostream& os) const override {
0248     // Print object arguments and name/address of function pointer
0249     os << "[ function=" << func.name() << " " ;
0250     for (Int_t i=0 ; i<numProxies() ; i++) {
0251       RooAbsProxy* p = getProxy(i) ;
0252       if (!TString(p->name()).BeginsWith("!")) {
0253    p->print(os) ;
0254    os << " " ;
0255       }
0256     }
0257     os << "]" ;
0258   }
0259 
0260 protected:
0261 
0262   RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
0263   RooRealProxy x ;              // Argument reference
0264   RooRealProxy y ;              // Argument reference
0265   RooRealProxy z ;              // Argument reference
0266 
0267   double evaluate() const override {
0268     // Return value of embedded function using value of referenced variable x
0269     return func(x,y,z) ;
0270   }
0271 
0272 private:
0273 
0274   ClassDefOverride(RooCFunction3Binding,1) // RooAbsReal binding to external C functions
0275 };
0276 
0277 
0278 
0279 template<class VO,class VI1, class VI2, class VI3>
0280 RooCFunction3Binding<VO,VI1,VI2,VI3>::RooCFunction3Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3),
0281                          RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
0282   RooAbsReal(name,title),
0283   func(_func),
0284   x(func.argName(0),func.argName(0),this,_x),
0285   y(func.argName(1),func.argName(1),this,_y),
0286   z(func.argName(2),func.argName(2),this,_z)
0287 {
0288   // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
0289   // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
0290   // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
0291   // a RooWorkspace
0292 }
0293 
0294 
0295 template<class VO,class VI1, class VI2, class VI3>
0296 RooCFunction3Binding<VO,VI1,VI2,VI3>::RooCFunction3Binding(const RooCFunction3Binding& other, const char* name) :
0297   RooAbsReal(other,name),
0298   func(other.func),
0299   x("x",this,other.x),
0300   y("y",this,other.y),
0301   z("z",this,other.z)
0302 {
0303   // Copy constructor
0304 }
0305 
0306 
0307 template<class VO,class VI1, class VI2, class VI3>
0308 class RooCFunction3PdfBinding : public RooAbsPdf {
0309 public:
0310   RooCFunction3PdfBinding() {
0311     // Default constructor
0312   } ;
0313   RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z);
0314   RooCFunction3PdfBinding(const RooCFunction3PdfBinding& other, const char* name=nullptr) ;
0315   TObject* clone(const char* newname) const override { return new RooCFunction3PdfBinding(*this,newname); }
0316 
0317   void printArgs(std::ostream& os) const override {
0318     // Print object arguments and name/address of function pointer
0319     os << "[ function=" << func.name() << " " ;
0320     for (Int_t i=0 ; i<numProxies() ; i++) {
0321       RooAbsProxy* p = getProxy(i) ;
0322       if (!TString(p->name()).BeginsWith("!")) {
0323    p->print(os) ;
0324    os << " " ;
0325       }
0326     }
0327     os << "]" ;
0328   }
0329 
0330 protected:
0331 
0332   RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
0333   RooRealProxy x ;              // Argument reference
0334   RooRealProxy y ;              // Argument reference
0335   RooRealProxy z ;              // Argument reference
0336 
0337   double evaluate() const override {
0338     // Return value of embedded function using value of referenced variable x
0339     return func(x,y,z) ;
0340   }
0341 
0342 private:
0343 
0344   ClassDefOverride(RooCFunction3PdfBinding,1) // RooAbsReal binding to external C functions
0345 };
0346 
0347 
0348 
0349 template<class VO,class VI1, class VI2, class VI3>
0350 RooCFunction3PdfBinding<VO,VI1,VI2,VI3>::RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3),
0351                          RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
0352   RooAbsPdf(name,title),
0353   func(_func),
0354   x(func.argName(0),func.argName(0),this,_x),
0355   y(func.argName(1),func.argName(1),this,_y),
0356   z(func.argName(2),func.argName(2),this,_z)
0357 {
0358   // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
0359   // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
0360   // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
0361   // a RooWorkspace
0362 }
0363 
0364 
0365 template<class VO,class VI1, class VI2, class VI3>
0366 RooCFunction3PdfBinding<VO,VI1,VI2,VI3>::RooCFunction3PdfBinding(const RooCFunction3PdfBinding& other, const char* name) :
0367   RooAbsPdf(other,name),
0368   func(other.func),
0369   x("x",this,other.x),
0370   y("y",this,other.y),
0371   z("z",this,other.z)
0372 {
0373   // Copy constructor
0374 }
0375 
0376 #endif