ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 /**
14 \file RooFunctorBinding.cxx
15 \class RooFunctorBinding
16 \ingroup Roofit
17 
18 RooCFunction1Binding is a templated implementation of class RooAbsReal that binds
19 generic C(++) functions to a RooAbsReal argument thus allowing generic C++
20 functions to be used as RooFit functions. Instances of function binding
21 classes are fully functional RooFit function objects with one exception:
22 if the bound function is _not_ a standard TMath or MathMore function the
23 class cannot be persisted in a RooWorkspace without registering the function
24 pointer first using RooCFunction1Binding<T1,T2>::register().
25 **/
26 
27 #include "Riostream.h"
28 #include "RooFunctorBinding.h"
29 
30 using namespace std ;
31 
34  ;
35 
36 
37 RooFunctorBinding::RooFunctorBinding(const char *name, const char *title, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& v) :
38  RooAbsReal(name,title),
39  func(&ftor),
40  vars("vars","vars",this)
41 {
42  // Check that function dimension and number of variables match
43  if (ftor.NDim()!=UInt_t(v.getSize())) {
44  coutE(InputArguments) << "RooFunctorBinding::ctor(" << GetName() << ") ERROR number of provided variables (" << v.getSize()
45  << ") does not match dimensionality of function (" << ftor.NDim() << ")" << endl ;
46  throw string("RooFunctor::ctor ERROR") ;
47  }
48  x = new Double_t[func->NDim()] ;
49  vars.add(v) ;
50 }
51 
52 
54  RooAbsReal(other,name),
55  func(other.func),
56  vars("vars",this,other.vars)
57 {
58  // Copy constructor
59  x = new Double_t[func->NDim()] ;
60 }
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 
78  // Return value of embedded function using value of referenced variable x
79  for (int i=0 ; i<vars.getSize() ; i++) {
80  x[i] = ((RooAbsReal*)vars.at(i))->getVal() ;
81  }
82  return (*func)(x) ;
83  }
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 
114 void RooFunctorPdfBinding::printArgs(ostream& os) const {
115  // Print object arguments and name/address of function pointer
116  os << "[ function=" << func << " " ;
117  for (Int_t i=0 ; i<numProxies() ; i++) {
118  RooAbsProxy* p = getProxy(i) ;
119  if (!TString(p->name()).BeginsWith("!")) {
120  p->print(os) ;
121  os << " " ;
122  }
123  }
124  os << "]" ;
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 
136 
137 
138 namespace RooFit {
139 
141  return new RooFunctorBinding(name,name,ftor,vars) ;
142  }
143 
144  RooAbsPdf* bindPdf(const char* name, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& vars) {
145  return new RooFunctorPdfBinding(name,name,ftor,vars) ;
146  }
147 
148 }
149 
150 
151 
virtual void print(std::ostream &os, Bool_t addContents=kFALSE) const
Print proxy name.
Definition: RooAbsProxy.cxx:75
#define coutE(a)
Definition: RooMsgService.h:35
RooAbsPdf * bindPdf(const char *name, CFUNCD1D func, RooAbsReal &x)
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
ClassImp(RooFunctorBinding) ClassImp(RooFunctorPdfBinding)
RooAbsProxy * getProxy(Int_t index) const
Return the nth proxy from the proxy list.
Definition: RooAbsArg.cxx:1254
RooAbsReal * bindFunction(const char *name, CFUNCD1D func, RooAbsReal &x)
Double_t x[n]
Definition: legend1.C:17
RooCFunction1Binding is a templated implementation of class RooAbsReal that binds generic C(++) funct...
bool BeginsWith(const std::string &theString, const std::string &theSubstring)
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
const ROOT::Math::IBaseFunctionMultiDim * func
Double_t evaluate() const
Int_t numProxies() const
Return the number of registered proxies.
Definition: RooAbsArg.cxx:1267
SVector< double, 2 > v
Definition: Dict.h:5
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")
const ROOT::Math::IBaseFunctionMultiDim * func
RooAbsProxy is the abstact interface for proxy classes.
Definition: RooAbsProxy.h:32
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
unsigned int UInt_t
Definition: RtypesCore.h:42
void printArgs(std::ostream &os) const
Print object arguments, ie its proxies.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual const char * name() const
Definition: RooAbsProxy.h:42
Double_t evaluate() const
RooAbsArg * at(Int_t idx) const
Definition: RooArgList.h:84
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
double func(double *x, double *p)
Definition: stressTF1.cxx:213
#define name(a, b)
Definition: linkTestLib0.cpp:5
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
Int_t getSize() const
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63