ROOT  6.06/09
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 //
19 // BEGIN_HTML
20 // Lightweight interface adaptor that binds a RooAbsReal object to a subset
21 // of its servers and present it as a simple array oriented interface.
22 // END_HTML
23 //
24 
25 
26 #include "RooFit.h"
27 #include "Riostream.h"
28 
29 #include "RooRealBinding.h"
30 #include "RooAbsReal.h"
31 #include "RooArgSet.h"
32 #include "RooAbsRealLValue.h"
33 #include "RooNameReg.h"
34 #include "RooMsgService.h"
35 
36 #include <assert.h>
37 
38 
39 
40 using namespace std;
41 
43 ;
44 
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Construct a lightweight function binding of RooAbsReal func to
48 /// variables 'vars'. Use the provided nset as normalization set to
49 /// be passed to RooAbsReal::getVal() If rangeName is not null, use
50 /// the range of with that name as range associated with the
51 /// variables of this function binding. If clipInvalid is true,
52 /// values requested to the function binding that are outside the
53 /// defined range of the variables are clipped to fit in the defined
54 /// range.
55 
56 RooRealBinding::RooRealBinding(const RooAbsReal& func, const RooArgSet &vars, const RooArgSet* nset, Bool_t clipInvalid, const TNamed* rangeName) :
57  RooAbsFunc(vars.getSize()), _func(&func), _vars(0), _nset(nset), _clipInvalid(clipInvalid), _xsave(0), _rangeName(rangeName), _funcSave(0)
58 {
59  // allocate memory
61  if(0 == _vars) {
62  _valid= kFALSE;
63  return;
64  }
65  // check that all of the arguments are real valued and store them
66  RooAbsArg *var = 0;
67  TIterator* iter = vars.createIterator() ;
68  Int_t index(0) ;
69  while((var=(RooAbsArg*)iter->Next())) {
70  _vars[index]= dynamic_cast<RooAbsRealLValue*>(var);
71  if(0 == _vars[index]) {
72  oocoutE((TObject*)0,InputArguments) << "RooRealBinding: cannot bind to " << var->GetName() << endl ;
73  _valid= kFALSE;
74  }
75  index++ ;
76  }
77  delete iter ;
78  _xvecValid = kTRUE ;
79 }
80 
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 /// Construct a lightweight function binding of RooAbsReal func to
84 /// variables 'vars'. Use the provided nset as normalization set to
85 /// be passed to RooAbsReal::getVal() If rangeName is not null, use
86 /// the range of with that name as range associated with the
87 /// variables of this function binding. If clipInvalid is true,
88 /// values requested to the function binding that are outside the
89 /// defined range of the variables are clipped to fit in the defined
90 /// range.
91 
93  RooAbsFunc(other), _func(other._func), _nset(nset?nset:other._nset), _xvecValid(other._xvecValid),
94  _clipInvalid(other._clipInvalid), _xsave(0), _rangeName(other._rangeName), _funcSave(other._funcSave)
95 {
96  // allocate memory
98 
99  for(unsigned int index=0 ; index<getDimension() ; index++) {
100  _vars[index]= other._vars[index] ;
101  }
102 }
103 
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Destructor
107 
109 {
110  if(0 != _vars) delete[] _vars;
111  if (_xsave) delete[] _xsave ;
112 }
113 
114 
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// Save value of all variables
118 
120 {
121  if (!_xsave) {
122  _xsave = new Double_t[getDimension()] ;
123  RooArgSet* comps = _func->getComponents() ;
124  RooFIter iter = comps->fwdIterator() ;
125  RooAbsArg* arg ;
126  while ((arg=iter.next())) {
127  if (dynamic_cast<RooAbsReal*>(arg)) {
128  _compList.push_back((RooAbsReal*)(arg)) ;
129  _compSave.push_back(0) ;
130  }
131  }
132  delete comps ;
133  }
134  _funcSave = _func->_value ;
135 
136  // Save components
137  list<RooAbsReal*>::iterator ci = _compList.begin() ;
138  list<Double_t>::iterator si = _compSave.begin() ;
139  while(ci!=_compList.end()) {
140  *si = (*ci)->_value ;
141  si++ ; ci++ ;
142  }
143 
144  for (UInt_t i=0 ; i<getDimension() ; i++) {
145  _xsave[i] = _vars[i]->getVal() ;
146  }
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// Restore value of all variables to previously
151 /// saved values by saveXVec()
152 
154 {
155  if (!_xsave) {
156  return ;
157  }
158  _func->_value = _funcSave ;
159 
160  // Restore components
161  list<RooAbsReal*>::iterator ci = _compList.begin() ;
162  list<Double_t>::iterator si = _compSave.begin() ;
163  while (ci!=_compList.end()) {
164  (*ci)->_value = *si ;
165  ci++ ; si++ ;
166  }
167 
168  for (UInt_t i=0 ; i<getDimension() ; i++) {
169  _vars[i]->setVal(_xsave[i]) ;
170  }
171 }
172 
173 
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 /// Load the vector of variable values into the RooRealVars associated
177 /// as variables with the bound RooAbsReal function
178 
179 void RooRealBinding::loadValues(const Double_t xvector[]) const
180 {
181  _xvecValid = kTRUE ;
182  const char* range = RooNameReg::instance().constStr(_rangeName) ;
183  for(UInt_t index= 0; index < _dimension; index++) {
184  if (_clipInvalid && !_vars[index]->isValidReal(xvector[index])) {
185  _xvecValid = kFALSE ;
186  } else {
187  _vars[index]->setVal(xvector[index],range);
188  }
189  }
190 
191 }
192 
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Evaluate the bound RooAbsReal at the variable values provided in xvector
196 
198 {
199  assert(isValid());
200  _ncall++ ;
201  loadValues(xvector);
202  //cout << getName() << "(x=" << xvector[0] << ")=" << _func->getVal(_nset) << " (nset = " << (_nset? *_nset:RooArgSet()) << ")" << endl ;
203  return _xvecValid ? _func->getVal(_nset) : 0. ;
204 }
205 
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Return lower limit on i-th variable
209 
211 {
212  assert(isValid());
213 
214  return _vars[index]->getMin(RooNameReg::str(_rangeName));
215 }
216 
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// Return upper limit on i-th variable
220 
222 {
223  assert(isValid());
224  return _vars[index]->getMax(RooNameReg::str(_rangeName));
225 }
226 
227 
228 ////////////////////////////////////////////////////////////////////////////////
229 /// Return name of function
230 
231 const char* RooRealBinding::getName() const
232 {
233  return _func->GetName() ;
234 }
235 
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 
239 std::list<Double_t>* RooRealBinding::plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
240 {
241  return _func->plotSamplingHint(obs,xlo,xhi) ;
242 }
243 
244 
245 ////////////////////////////////////////////////////////////////////////////////
246 
247 std::list<Double_t>* RooRealBinding::binBoundaries(Int_t index) const
248 {
249  return _func->binBoundaries(*_vars[index],getMinLimit(index),getMaxLimit(index));
250 }
virtual std::list< Double_t > * binBoundaries(RooAbsRealLValue &, Double_t, Double_t) const
Definition: RooAbsReal.h:278
RooFIter fwdIterator() const
virtual void restoreXVec() const
Restore value of all variables to previously saved values by saveXVec()
#define assert(cond)
Definition: unittest.h:542
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:134
virtual Double_t operator()(const Double_t xvector[]) const
Evaluate the bound RooAbsReal at the variable values provided in xvector.
virtual Double_t getMin(const char *name=0) const
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
UInt_t getDimension() const
Definition: RooAbsFunc.h:29
Iterator abstract base class.
Definition: TIterator.h:32
Double_t _funcSave
const RooAbsReal * _func
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
#define oocoutE(o, a)
Definition: RooMsgService.h:48
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
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 'vars'.
TIterator * createIterator(Bool_t dir=kIterForward) const
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
const RooArgSet * _nset
void loadValues(const Double_t xvector[]) const
Load the vector of variable values into the RooRealVars associated as variables with the bound RooAbs...
static RooNameReg & instance()
Return reference to singleton instance.
Definition: RooNameReg.cxx:63
Int_t _ncall
Definition: RooAbsFunc.h:73
RooAbsRealLValue ** _vars
return
Definition: TBase64.cxx:62
virtual const char * getName() const
Return name of function.
const TNamed * _rangeName
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual std::list< Double_t > * binBoundaries(Int_t) const
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
Bool_t isValid() const
Definition: RooAbsFunc.h:33
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &, Double_t, Double_t) const
Definition: RooAbsReal.h:279
virtual Double_t getMaxLimit(UInt_t dimension) const
Return upper limit on i-th variable.
virtual Double_t getMinLimit(UInt_t dimension) const
Return lower limit on i-th variable.
virtual void setVal(Double_t value)=0
ClassImp(RooRealBinding)
RooAbsArg * next()
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.
double func(double *x, double *p)
Definition: stressTF1.cxx:213
RooArgSet * getComponents() const
Definition: RooAbsArg.cxx:687
Double_t _value
Definition: RooAbsReal.h:389
Mother of all ROOT objects.
Definition: TObject.h:58
virtual Double_t getMax(const char *name=0) const
virtual TObject * Next()=0
std::list< RooAbsReal * > _compList
Double_t * _xsave
Bool_t _valid
Definition: RooAbsFunc.h:75
ClassImp(TSlaveInfo) Int_t TSlaveInfo const TSlaveInfo * si
Used to sort slaveinfos by ordinal.
Definition: TProof.cxx:167
std::list< Double_t > _compSave
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
virtual void saveXVec() const
Save value of all variables.
const Bool_t kTRUE
Definition: Rtypes.h:91
UInt_t _dimension
Definition: RooAbsFunc.h:74
const char * constStr(const TNamed *namePtr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:114
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &, Double_t, Double_t) const