/***************************************************************************** * Project: RooFit * * Package: RooFitCore * * File: $Id: RooCFunction1Binding.h 25185 2008-08-20 14:00:42Z wouter $ * 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 #include #include namespace RooFit { typedef Double_t (*CFUNCD1D)(Double_t) ; typedef Double_t (*CFUNCD1I)(Int_t) ; RooAbsReal* bindFunction(const char* name,void* func,RooAbsReal& x) ; RooAbsPdf* bindPdf(const char* name,void* func,RooAbsReal& x) ; #ifndef __CINT__ 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) ; #endif } template 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 _ptrmap ; // Pointer-to-name map std::map _namemap ; // Name-to-pointer map std::map > _argnamemap ; // Pointer-to-argnamelist map #endif } ; template 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 ; } return Form("(0x%08x)",_ptr) ; } const char* argName(Int_t iarg) { // Return suggested name for i-th argument return fmap().lookupArgName(_ptr,iarg) ; } static RooCFunction1Map& fmap() { // Return reference to function pointer-to-name mapping service if (!_fmap) { _fmap = new RooCFunction1Map ; } return *_fmap ; } private: static VO dummyFunction(VI) { // Dummy function used when registered function was not // found in un-persisting object return 0 ; } VO (*_ptr)(VI) ; //! Pointer to embedded function static RooCFunction1Map* _fmap ; // Pointer to mapping service object ClassDef(RooCFunction1Ref,1) // Persistable reference to C function pointer } ; template void RooCFunction1Ref::Streamer(TBuffer &R__b) { // Custom streamer for function pointer reference object. When writing, // the function pointer is substituted by its registerd 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 thisClass; // Stream an object of class RooCFunction1Ref if (R__b.IsReading()) { UInt_t R__s, R__c; R__b.ReadVersion(&R__s, &R__c); // Read name from file TString tmpName ; tmpName.Streamer(R__b) ; if (tmpName=="UNKNOWN") { coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << 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" << 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) { coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("0x%08x",_ptr) << " written object will not be functional when read back" << endl ; tmpName="UNKNOWN" ; } // Persist the name tmpName.Streamer(R__b) ; R__b.SetByteCount(R__c, kTRUE); } } template 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(ostream& os) const { // Print object arguments and name/address of function pointer os << "[ function=" << func.name() << " " ; for (Int_t i=0 ; iname()).BeginsWith("!")) { p->print(os) ; os << " " ; } } os << "]" ; } protected: RooCFunction1Ref 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 RooCFunction1Binding::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 RooCFunction1Binding::RooCFunction1Binding(const RooCFunction1Binding& other, const char* name) : RooAbsReal(other,name), func(other.func), x("x",this,other.x) { // Copy constructor } template 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(ostream& os) const { // Print object arguments and name/address of function pointer os << "[ function=" << func.name() << " " ; for (Int_t i=0 ; iname()).BeginsWith("!")) { p->print(os) ; os << " " ; } } os << "]" ; } protected: RooCFunction1Ref 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 RooCFunction1PdfBinding::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 RooCFunction1PdfBinding::RooCFunction1PdfBinding(const RooCFunction1PdfBinding& other, const char* name) : RooAbsPdf(other,name), func(other.func), x("x",this,other.x) { // Copy constructor } template <> void RooCFunction1Ref::ShowMembers(TMemberInspector &R__insp, char *R__parent) ; template <> void RooCFunction1Binding::ShowMembers(TMemberInspector &R__insp, char *R__parent) ; template <> void RooCFunction1PdfBinding::ShowMembers(TMemberInspector &R__insp, char *R__parent) ; template <> void RooCFunction1Ref::ShowMembers(TMemberInspector &R__insp, char *R__parent) ; template <> void RooCFunction1Binding::ShowMembers(TMemberInspector &R__insp, char *R__parent) ; template <> void RooCFunction1PdfBinding::ShowMembers(TMemberInspector &R__insp, char *R__parent) ; #endif