Logo ROOT   6.08/07
Reference Guide
RooRealBinding.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 /**
18 \file RooRealBinding.cxx
19 \class RooRealBinding
20 \ingroup Roofitcore
21 
22 Lightweight interface adaptor that binds a RooAbsReal object to a subset
23 of its servers and present it as a simple array oriented interface.
24 **/
25 
26 
27 #include "RooFit.h"
28 #include "Riostream.h"
29 
30 #include "RooRealBinding.h"
31 #include "RooAbsReal.h"
32 #include "RooArgSet.h"
33 #include "RooAbsRealLValue.h"
34 #include "RooNameReg.h"
35 #include "RooMsgService.h"
36 
37 #include <assert.h>
38 
39 
40 
41 using namespace std;
42 
44 ;
45 
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// Construct a lightweight function binding of RooAbsReal func to
49 /// variables 'vars'. Use the provided nset as normalization set to
50 /// be passed to RooAbsReal::getVal() If rangeName is not null, use
51 /// the range of with that name as range associated with the
52 /// variables of this function binding. If clipInvalid is true,
53 /// values requested to the function binding that are outside the
54 /// defined range of the variables are clipped to fit in the defined
55 /// range.
56 
57 RooRealBinding::RooRealBinding(const RooAbsReal& func, const RooArgSet &vars, const RooArgSet* nset, Bool_t clipInvalid, const TNamed* rangeName) :
58  RooAbsFunc(vars.getSize()), _func(&func), _vars(0), _nset(nset), _clipInvalid(clipInvalid), _xsave(0), _rangeName(rangeName), _funcSave(0)
59 {
60  // allocate memory
62  if(0 == _vars) {
63  _valid= kFALSE;
64  return;
65  }
66  // check that all of the arguments are real valued and store them
67  RooAbsArg *var = 0;
68  TIterator* iter = vars.createIterator() ;
69  Int_t index(0) ;
70  while((var=(RooAbsArg*)iter->Next())) {
71  _vars[index]= dynamic_cast<RooAbsRealLValue*>(var);
72  if(0 == _vars[index]) {
73  oocoutE((TObject*)0,InputArguments) << "RooRealBinding: cannot bind to " << var->GetName() << endl ;
74  _valid= kFALSE;
75  }
76  index++ ;
77  }
78  delete iter ;
79  _xvecValid = kTRUE ;
80 }
81 
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// Construct a lightweight function binding of RooAbsReal func to
85 /// variables 'vars'. Use the provided nset as normalization set to
86 /// be passed to RooAbsReal::getVal() If rangeName is not null, use
87 /// the range of with that name as range associated with the
88 /// variables of this function binding. If clipInvalid is true,
89 /// values requested to the function binding that are outside the
90 /// defined range of the variables are clipped to fit in the defined
91 /// range.
92 
94  RooAbsFunc(other), _func(other._func), _nset(nset?nset:other._nset), _xvecValid(other._xvecValid),
96 {
97  // allocate memory
99 
100  for(unsigned int index=0 ; index<getDimension() ; index++) {
101  _vars[index]= other._vars[index] ;
102  }
103 }
104 
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Destructor
108 
110 {
111  if(0 != _vars) delete[] _vars;
112  if (_xsave) delete[] _xsave ;
113 }
114 
115 
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// Save value of all variables
119 
121 {
122  if (!_xsave) {
123  _xsave = new Double_t[getDimension()] ;
124  RooArgSet* comps = _func->getComponents() ;
125  RooFIter iter = comps->fwdIterator() ;
126  RooAbsArg* arg ;
127  while ((arg=iter.next())) {
128  if (dynamic_cast<RooAbsReal*>(arg)) {
129  _compList.push_back((RooAbsReal*)(arg)) ;
130  _compSave.push_back(0) ;
131  }
132  }
133  delete comps ;
134  }
135  _funcSave = _func->_value ;
136 
137  // Save components
138  list<RooAbsReal*>::iterator ci = _compList.begin() ;
139  list<Double_t>::iterator si = _compSave.begin() ;
140  while(ci!=_compList.end()) {
141  *si = (*ci)->_value ;
142  si++ ; ci++ ;
143  }
144 
145  for (UInt_t i=0 ; i<getDimension() ; i++) {
146  _xsave[i] = _vars[i]->getVal() ;
147  }
148 }
149 
150 ////////////////////////////////////////////////////////////////////////////////
151 /// Restore value of all variables to previously
152 /// saved values by saveXVec()
153 
155 {
156  if (!_xsave) {
157  return ;
158  }
159  _func->_value = _funcSave ;
160 
161  // Restore components
162  list<RooAbsReal*>::iterator ci = _compList.begin() ;
163  list<Double_t>::iterator si = _compSave.begin() ;
164  while (ci!=_compList.end()) {
165  (*ci)->_value = *si ;
166  ci++ ; si++ ;
167  }
168 
169  for (UInt_t i=0 ; i<getDimension() ; i++) {
170  _vars[i]->setVal(_xsave[i]) ;
171  }
172 }
173 
174 
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// Load the vector of variable values into the RooRealVars associated
178 /// as variables with the bound RooAbsReal function
179 
180 void RooRealBinding::loadValues(const Double_t xvector[]) const
181 {
182  _xvecValid = kTRUE ;
183  const char* range = RooNameReg::instance().constStr(_rangeName) ;
184  for(UInt_t index= 0; index < _dimension; index++) {
185  if (_clipInvalid && !_vars[index]->isValidReal(xvector[index])) {
186  _xvecValid = kFALSE ;
187  } else {
188  _vars[index]->setVal(xvector[index],range);
189  }
190  }
191 
192 }
193 
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// Evaluate the bound RooAbsReal at the variable values provided in xvector
197 
199 {
200  assert(isValid());
201  _ncall++ ;
202  loadValues(xvector);
203  //cout << getName() << "(x=" << xvector[0] << ")=" << _func->getVal(_nset) << " (nset = " << (_nset? *_nset:RooArgSet()) << ")" << endl ;
204  return _xvecValid ? _func->getVal(_nset) : 0. ;
205 }
206 
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Return lower limit on i-th variable
210 
212 {
213  assert(isValid());
214 
215  return _vars[index]->getMin(RooNameReg::str(_rangeName));
216 }
217 
218 
219 ////////////////////////////////////////////////////////////////////////////////
220 /// Return upper limit on i-th variable
221 
223 {
224  assert(isValid());
225  return _vars[index]->getMax(RooNameReg::str(_rangeName));
226 }
227 
228 
229 ////////////////////////////////////////////////////////////////////////////////
230 /// Return name of function
231 
232 const char* RooRealBinding::getName() const
233 {
234  return _func->GetName() ;
235 }
236 
237 
238 ////////////////////////////////////////////////////////////////////////////////
239 
240 std::list<Double_t>* RooRealBinding::plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
241 {
242  return _func->plotSamplingHint(obs,xlo,xhi) ;
243 }
244 
245 
246 ////////////////////////////////////////////////////////////////////////////////
247 
248 std::list<Double_t>* RooRealBinding::binBoundaries(Int_t index) const
249 {
250  return _func->binBoundaries(*_vars[index],getMinLimit(index),getMaxLimit(index));
251 }
virtual Double_t getMin(const char *name=0) const
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
TIterator * createIterator(Bool_t dir=kIterForward) const
virtual Double_t getMax(const char *name=0) const
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:135
virtual std::list< Double_t > * binBoundaries(RooAbsRealLValue &, Double_t, Double_t) const
Definition: RooAbsReal.h:278
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
virtual Double_t getMinLimit(UInt_t dimension) const
Return lower limit on i-th variable.
Bool_t isValid() const
Definition: RooAbsFunc.h:33
Iterator abstract base class.
Definition: TIterator.h:32
Double_t _funcSave
const RooAbsReal * _func
virtual Double_t getMaxLimit(UInt_t dimension) const
Return upper limit on i-th variable.
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
#define oocoutE(o, a)
Definition: RooMsgService.h:48
RooRealBinding(const RooAbsReal &func, const RooArgSet &vars, const RooArgSet *nset=0, Bool_t clipInvalid=kFALSE, const TNamed *rangeName=0)
Construct a lightweight function binding of RooAbsReal func to variables &#39;vars&#39;.
const RooArgSet * _nset
virtual Double_t operator()(const Double_t xvector[]) const
Evaluate the bound RooAbsReal at the variable values provided in xvector.
static RooNameReg & instance()
Return reference to singleton instance.
Definition: RooNameReg.cxx:64
Int_t _ncall
Definition: RooAbsFunc.h:73
virtual void saveXVec() const
Save value of all variables.
RooAbsRealLValue ** _vars
RooArgSet * getComponents() const
Definition: RooAbsArg.cxx:688
UInt_t getDimension() const
Definition: RooAbsFunc.h:29
const TNamed * _rangeName
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void setVal(Double_t value)=0
RooAbsArg * next()
virtual const char * getName() const
Return name of function.
#define ClassImp(name)
Definition: Rtypes.h:279
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
virtual ~RooRealBinding()
Destructor.
RooFIter fwdIterator() const
virtual std::list< Double_t > * binBoundaries(Int_t) const
double func(double *x, double *p)
Definition: stressTF1.cxx:213
void loadValues(const Double_t xvector[]) const
Load the vector of variable values into the RooRealVars associated as variables with the bound RooAbs...
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &, Double_t, Double_t) const
Definition: RooAbsReal.h:279
Double_t _value
Definition: RooAbsReal.h:389
Mother of all ROOT objects.
Definition: TObject.h:37
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &, Double_t, Double_t) const
virtual TObject * Next()=0
std::list< RooAbsReal * > _compList
Double_t * _xsave
Bool_t _valid
Definition: RooAbsFunc.h:75
std::list< Double_t > _compSave
Lightweight interface adaptor that binds a RooAbsReal object to a subset of its servers and present i...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
virtual void restoreXVec() const
Restore value of all variables to previously saved values by saveXVec()
const Bool_t kTRUE
Definition: Rtypes.h:91
return
Definition: HLFactory.cxx:514
Abstract interface for evaluating a real-valued function of one real variable and performing numerica...
Definition: RooAbsFunc.h:23
UInt_t _dimension
Definition: RooAbsFunc.h:74
const char * constStr(const TNamed *namePtr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:115