Logo ROOT   6.14/05
Reference Guide
RooFunctorBinding.cxx
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 /** \class RooFunctorBinding
14  \ingroup Roofit
15 
16 RooCFunction1Binding is a templated implementation of class RooAbsReal that binds
17 generic C(++) functions to a RooAbsReal argument thus allowing generic C++
18 functions to be used as RooFit functions. Instances of function binding
19 classes are fully functional RooFit function objects with one exception:
20 if the bound function is _not_ a standard TMath or MathMore function the
21 class cannot be persisted in a RooWorkspace without registering the function
22 pointer first using RooCFunction1Binding<T1,T2>::register().
23 **/
24 
25 /** \class RooFunctorPdfBinding
26  \ingroup Roofit
27 **/
28 
29 #include "Riostream.h"
30 #include "RooFunctorBinding.h"
31 
32 using namespace std ;
33 
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 RooFunctorBinding::RooFunctorBinding(const char *name, const char *title, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& v) :
39  RooAbsReal(name,title),
40  func(&ftor),
41  vars("vars","vars",this)
42 {
43  // Check that function dimension and number of variables match
44  if (ftor.NDim()!=UInt_t(v.getSize())) {
45  coutE(InputArguments) << "RooFunctorBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.getSize()
46  << ") does not match dimensionality of function (" << ftor.NDim() << ")" << endl ;
47  throw string("RooFunctor::ctor ERROR") ;
48  }
49  x = new Double_t[func->NDim()] ;
50  vars.add(v) ;
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
55  RooAbsReal(other,name),
56  func(other.func),
57  vars("vars",this,other.vars)
58 {
59  // Copy constructor
60  x = new Double_t[func->NDim()] ;
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 void RooFunctorBinding::printArgs(ostream& os) const {
65  // Print object arguments and name/address of function pointer
66  os << "[ function=" << func << " " ;
67  for (Int_t i=0 ; i<numProxies() ; i++) {
68  RooAbsProxy* p = getProxy(i) ;
69  if (!TString(p->name()).BeginsWith("!")) {
70  p->print(os) ;
71  os << " " ;
72  }
73  }
74  os << "]" ;
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
79  // Return value of embedded function using value of referenced variable x
80  for (int i=0 ; i<vars.getSize() ; i++) {
81  x[i] = ((RooAbsReal*)vars.at(i))->getVal() ;
82  }
83  return (*func)(x) ;
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
88  RooAbsPdf(name,title),
89  func(&ftor),
90  vars("vars","vars",this)
91 {
92  // Check that function dimension and number of variables match
93  if (ftor.NDim()!=UInt_t(v.getSize())) {
94  coutE(InputArguments) << "RooFunctorPdfBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.getSize()
95  << ") does not match dimensionality of function (" << ftor.NDim() << ")" << endl ;
96  throw string("RooFunctor::ctor ERROR") ;
97  }
98  x = new Double_t[func->NDim()] ;
99  vars.add(v) ;
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
104  RooAbsPdf(other,name),
105  func(other.func),
106  vars("vars",this,other.vars)
107 {
108  // Copy constructor
109  x = new Double_t[func->NDim()] ;
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 void RooFunctorPdfBinding::printArgs(ostream& os) const {
114  // Print object arguments and name/address of function pointer
115  os << "[ function=" << func << " " ;
116  for (Int_t i=0 ; i<numProxies() ; i++) {
117  RooAbsProxy* p = getProxy(i) ;
118  if (!TString(p->name()).BeginsWith("!")) {
119  p->print(os) ;
120  os << " " ;
121  }
122  }
123  os << "]" ;
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
128  // Return value of embedded function using value of referenced variable x
129  for (int i=0 ; i<vars.getSize() ; i++) {
130  x[i] = ((RooAbsReal*)vars.at(i))->getVal() ;
131  }
132  return (*func)(x) ;
133  }
134 
135 namespace RooFit {
136 
138  return new RooFunctorBinding(name,name,ftor,vars) ;
139  }
140 
142  return new RooFunctorPdfBinding(name,name,ftor,vars) ;
143  }
144 
145 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
#define coutE(a)
Definition: RooMsgService.h:34
virtual const char * name() const
Definition: RooAbsProxy.h:41
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
Double_t evaluate() const
int Int_t
Definition: RtypesCore.h:41
STL namespace.
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)
RooCFunction1Binding is a templated implementation of class RooAbsReal that binds generic C(++) funct...
Double_t evaluate() const
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
const ROOT::Math::IBaseFunctionMultiDim * func
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
Int_t getSize() const
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
SVector< double, 2 > v
Definition: Dict.h:5
RooAbsArg * at(Int_t idx) const
Definition: RooArgList.h:84
const ROOT::Math::IBaseFunctionMultiDim * func
RooAbsProxy is the abstact interface for proxy classes.
Definition: RooAbsProxy.h:31
unsigned int UInt_t
Definition: RtypesCore.h:42
#define ClassImp(name)
Definition: Rtypes.h:359
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:53
RooAbsProxy * getProxy(Int_t index) const
Return the nth proxy from the proxy list.
Definition: RooAbsArg.cxx:1245
Int_t numProxies() const
Return the number of registered proxies.
Definition: RooAbsArg.cxx:1258
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
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 unsigned int NDim() const =0
Retrieve the dimension of the function.