RooFunctorBinding.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  *    File: $Id: RooFunctorBinding.cxx 31838 2009-12-11 09:28:00Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, NIKHEF, verkerke@nikhef.nl                         *
00007  *                                                                           *
00008  * Copyright (c) 2000-2008, NIKHEF, Regents of the University of California  *
00009  *                          and Stanford University. All rights reserved.    *
00010  *                                                                           *
00011  *****************************************************************************/
00012 
00013 //////////////////////////////////////////////////////////////////////////////
00014 // 
00015 // BEGIN_HTML
00016 // RooCFunction1Binding is a templated implementation of class RooAbsReal that binds 
00017 // generic C(++) functions to a RooAbsReal argument thus allowing generic C++
00018 // functions to be used as RooFit functions. Instances of function binding
00019 // classes are fully functional RooFit function objects with one exception:
00020 // if the bound function is _not_ a standard TMath or MathMore function the
00021 // class cannot be persisted in a RooWorkspace without registering the function
00022 // pointer first using RooCFunction1Binding<T1,T2>::register().
00023 // END_HTML
00024 //
00025 
00026 #include "Riostream.h" 
00027 #include "RooFunctorBinding.h" 
00028 
00029 using namespace std ;
00030 
00031 ClassImp(RooFunctorBinding) 
00032 ClassImp(RooFunctorPdfBinding) 
00033   ;
00034 
00035 
00036 RooFunctorBinding::RooFunctorBinding(const char *name, const char *title, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& v) :
00037   RooAbsReal(name,title), 
00038   func(&ftor),
00039   vars("vars","vars",this) 
00040 { 
00041   // Check that function dimension and number of variables match 
00042   if (ftor.NDim()!=UInt_t(v.getSize())) {
00043     coutE(InputArguments) << "RooFunctorBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.getSize() 
00044                           << ") does not match dimensionality of function (" << ftor.NDim() << ")" << endl ;
00045     throw string("RooFunctor::ctor ERROR") ;
00046   }
00047   x = new Double_t[func->NDim()] ;
00048   vars.add(v) ;
00049 } 
00050 
00051 
00052 RooFunctorBinding::RooFunctorBinding(const RooFunctorBinding& other, const char* name) :  
00053   RooAbsReal(other,name), 
00054   func(other.func),
00055   vars("vars",this,other.vars)
00056 { 
00057   // Copy constructor
00058   x = new Double_t[func->NDim()] ;
00059 } 
00060 
00061 
00062 
00063 void RooFunctorBinding::printArgs(ostream& os) const {
00064   // Print object arguments and name/address of function pointer
00065   os << "[ function=" << func << " " ;    
00066   for (Int_t i=0 ; i<numProxies() ; i++) {
00067     RooAbsProxy* p = getProxy(i) ;
00068     if (!TString(p->name()).BeginsWith("!")) {
00069       p->print(os) ;
00070       os << " " ;
00071     }
00072   }    
00073   os << "]" ;  
00074 }
00075 
00076 Double_t RooFunctorBinding::evaluate() const {
00077     // Return value of embedded function using value of referenced variable x
00078     for (int i=0 ; i<vars.getSize() ; i++) {
00079       x[i] = ((RooAbsReal*)vars.at(i))->getVal() ;
00080     }
00081     return (*func)(x) ;
00082   }
00083 
00084 
00085 
00086 RooFunctorPdfBinding::RooFunctorPdfBinding(const char *name, const char *title, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& v) :
00087   RooAbsPdf(name,title), 
00088   func(&ftor),
00089   vars("vars","vars",this) 
00090 { 
00091   // Check that function dimension and number of variables match 
00092   if (ftor.NDim()!=UInt_t(v.getSize())) {
00093     coutE(InputArguments) << "RooFunctorPdfBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.getSize() 
00094                           << ") does not match dimensionality of function (" << ftor.NDim() << ")" << endl ;
00095     throw string("RooFunctor::ctor ERROR") ;
00096   }
00097   x = new Double_t[func->NDim()] ;
00098   vars.add(v) ;
00099 } 
00100 
00101 
00102 RooFunctorPdfBinding::RooFunctorPdfBinding(const RooFunctorPdfBinding& other, const char* name) :  
00103   RooAbsPdf(other,name), 
00104   func(other.func),
00105   vars("vars",this,other.vars)
00106 { 
00107   // Copy constructor
00108   x = new Double_t[func->NDim()] ;
00109 } 
00110 
00111 
00112 
00113 void RooFunctorPdfBinding::printArgs(ostream& os) const {
00114   // Print object arguments and name/address of function pointer
00115   os << "[ function=" << func << " " ;    
00116   for (Int_t i=0 ; i<numProxies() ; i++) {
00117     RooAbsProxy* p = getProxy(i) ;
00118     if (!TString(p->name()).BeginsWith("!")) {
00119       p->print(os) ;
00120       os << " " ;
00121     }
00122   }    
00123   os << "]" ;  
00124 }
00125 
00126 Double_t RooFunctorPdfBinding::evaluate() const {
00127     // Return value of embedded function using value of referenced variable x
00128     for (int i=0 ; i<vars.getSize() ; i++) {
00129       x[i] = ((RooAbsReal*)vars.at(i))->getVal() ;
00130     }
00131     return (*func)(x) ;
00132   }
00133 
00134 
00135 
00136 
00137 namespace RooFit {
00138 
00139   RooAbsReal* bindFunction(const char* name, const ROOT::Math::IBaseFunctionMultiDim& ftor,const RooArgList& vars) {
00140     return new RooFunctorBinding(name,name,ftor,vars) ;
00141   }
00142 
00143   RooAbsPdf*  bindPdf(const char* name, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& vars) {
00144     return new RooFunctorPdfBinding(name,name,ftor,vars) ;
00145   }
00146 
00147 }
00148 
00149 
00150 

Generated on Tue Jul 5 14:55:12 2011 for ROOT_528-00b_version by  doxygen 1.5.1