Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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 /** \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 
34 ClassImp(RooFunctorBinding);
35 ClassImp(RooFunctorPdfBinding);
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 ////////////////////////////////////////////////////////////////////////////////
54 RooFunctorBinding::RooFunctorBinding(const RooFunctorBinding& other, const char* name) :
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 ////////////////////////////////////////////////////////////////////////////////
78 Double_t RooFunctorBinding::evaluate() const {
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 ////////////////////////////////////////////////////////////////////////////////
87 RooFunctorPdfBinding::RooFunctorPdfBinding(const char *name, const char *title, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& v) :
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 ////////////////////////////////////////////////////////////////////////////////
103 RooFunctorPdfBinding::RooFunctorPdfBinding(const RooFunctorPdfBinding& other, const char* name) :
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 ////////////////////////////////////////////////////////////////////////////////
127 Double_t RooFunctorPdfBinding::evaluate() const {
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 
137  RooAbsReal* bindFunction(const char* name, const ROOT::Math::IBaseFunctionMultiDim& ftor,const RooArgList& vars) {
138  return new RooFunctorBinding(name,name,ftor,vars) ;
139  }
140 
141  RooAbsPdf* bindPdf(const char* name, const ROOT::Math::IBaseFunctionMultiDim& ftor, const RooArgList& vars) {
142  return new RooFunctorPdfBinding(name,name,ftor,vars) ;
143  }
144 
145 }