Logo ROOT  
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 #include "RooRealBinding.h"
27 
28 #include "RooAbsReal.h"
29 #include "RooArgSet.h"
30 #include "RooAbsRealLValue.h"
31 #include "RooNameReg.h"
32 #include "RooMsgService.h"
33 #include "RunContext.h"
34 
35 #include <cassert>
36 
37 
38 
39 using namespace std;
40 
42 ;
43 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// Construct a lightweight function binding of RooAbsReal func to
47 /// variables 'vars'. Use the provided nset as normalization set to
48 /// be passed to RooAbsReal::getVal() If rangeName is not null, use
49 /// the range of with that name as range associated with the
50 /// variables of this function binding. If clipInvalid is true,
51 /// values requested to the function binding that are outside the
52 /// defined range of the variables are clipped to fit in the defined
53 /// range.
54 
55 RooRealBinding::RooRealBinding(const RooAbsReal& func, const RooArgSet &vars, const RooArgSet* nset, Bool_t clipInvalid, const TNamed* rangeName) :
56  RooAbsFunc(vars.getSize()), _func(&func), _vars(), _nset(nset), _clipInvalid(clipInvalid), _xsave(0), _rangeName(rangeName), _funcSave(0)
57 {
58  // check that all of the arguments are real valued and store them
59  for (unsigned int index=0; index < vars.size(); ++index) {
60  RooAbsArg* var = vars[index];
61  _vars.push_back(dynamic_cast<RooAbsRealLValue*>(var));
62  if(_vars.back() == nullptr) {
63  oocoutE((TObject*)0,InputArguments) << "RooRealBinding: cannot bind to " << var->GetName()
64  << ". Variables need to be assignable, e.g. instances of RooRealVar." << endl ;
65  _valid= kFALSE;
66  }
67  if (!_func->dependsOn(*_vars[index])) {
68  oocoutW((TObject*)nullptr, InputArguments) << "RooRealBinding: The function " << func.GetName() << " does not depend on the parameter " << _vars[index]->GetName()
69  << ". Note that passing copies of the parameters is not supported." << std::endl;
70  }
71  }
72 
73  _xvecValid = kTRUE ;
74 }
75 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Construct a lightweight function binding of RooAbsReal func to
79 /// variables 'vars'. Use the provided nset as normalization set to
80 /// be passed to RooAbsReal::getVal() If rangeName is not null, use
81 /// the range of with that name as range associated with the
82 /// variables of this function binding. If clipInvalid is true,
83 /// values requested to the function binding that are outside the
84 /// defined range of the variables are clipped to fit in the defined
85 /// range.
86 
88  RooAbsFunc(other), _func(other._func), _vars(other._vars), _nset(nset?nset:other._nset), _xvecValid(other._xvecValid),
89  _clipInvalid(other._clipInvalid), _xsave(0), _rangeName(other._rangeName), _funcSave(other._funcSave)
90 {
91 
92 }
93 
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Destructor
97 
99 {
100  if (_xsave) delete[] _xsave ;
101 }
102 
103 
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Save value of all variables
107 
109 {
110  if (!_xsave) {
111  _xsave = new Double_t[getDimension()] ;
112  RooArgSet* comps = _func->getComponents() ;
113  RooFIter iter = comps->fwdIterator() ;
114  RooAbsArg* arg ;
115  while ((arg=iter.next())) {
116  if (dynamic_cast<RooAbsReal*>(arg)) {
117  _compList.push_back((RooAbsReal*)(arg)) ;
118  _compSave.push_back(0) ;
119  }
120  }
121  delete comps ;
122  }
123  _funcSave = _func->_value ;
124 
125  // Save components
126  auto ci = _compList.begin() ;
127  auto si = _compSave.begin() ;
128  while(ci != _compList.end()) {
129  *si = (*ci)->_value ;
130  ++si;
131  ++ci;
132  }
133 
134  for (UInt_t i=0 ; i<getDimension() ; i++) {
135  _xsave[i] = _vars[i]->getVal() ;
136  }
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Restore value of all variables to previously
141 /// saved values by saveXVec()
142 
144 {
145  if (!_xsave) {
146  return ;
147  }
148  _func->_value = _funcSave ;
149 
150  // Restore components
151  auto ci = _compList.begin() ;
152  auto si = _compSave.begin() ;
153  while (ci != _compList.end()) {
154  (*ci)->_value = *si ;
155  ++ci;
156  ++si;
157  }
158 
159  for (UInt_t i=0 ; i<getDimension() ; i++) {
160  _vars[i]->setVal(_xsave[i]) ;
161  }
162 }
163 
164 
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Load the vector of variable values into the RooRealVars associated
168 /// as variables with the bound RooAbsReal function.
169 /// \warning This will load as many values as the dimensionality of the function
170 /// requires. The size of `xvector` is not checked.
171 void RooRealBinding::loadValues(const Double_t xvector[]) const
172 {
173  _xvecValid = kTRUE ;
174  const char* range = RooNameReg::instance().constStr(_rangeName) ;
175  for(UInt_t index= 0; index < _dimension; index++) {
176  if (_clipInvalid && !_vars[index]->isValidReal(xvector[index])) {
177  _xvecValid = kFALSE ;
178  } else {
179  _vars[index]->setVal(xvector[index],range);
180  }
181  }
182 
183 }
184 
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Evaluate the bound RooAbsReal at the variable values provided in xvector
188 
190 {
191  assert(isValid());
192  _ncall++ ;
193  loadValues(xvector);
194  return _xvecValid ? _func->getVal(_nset) : 0. ;
195 }
196 
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Evaluate the bound object at all locations indicated by the data in `coordinates`.
200 /// If `_clipInvalid` is set, the function is set to zero at all points in the arguments
201 /// that are not within the range of the observables.
202 /// \param coordinates Vector of spans that contain the points where the function should be evaluated.
203 /// The ordinal position in the vector corresponds to the ordinal position in the set of
204 /// {observables, parameters} that were passed to the constructor.
205 /// The spans can either have a size of `n`, in which case a batch of `n` results is returned, or they can have
206 /// a size of 1. In the latter case, the value in the span is broadcast to all `n` events.
207 /// \return Batch of function values for each coordinate given in the input spans. If a parameter is invalid, i.e.,
208 /// out of its range, an empty span is returned. If an observable is invalid, the function value is 0.
210  assert(isValid());
211  _ncall += coordinates.front().size();
212 
213  bool parametersValid = true;
214 
215  // Use _evalData to hold on to memory between integration calls
216  if (!_evalData) {
218  } else {
219  _evalData->clear();
220  }
222 
223  for (unsigned int dim=0; dim < coordinates.size(); ++dim) {
224  const RooSpan<const double>& values = coordinates[dim];
225  RooAbsRealLValue& var = *_vars[dim];
226  _evalData->spans[&var] = values;
227  if (_clipInvalid && values.size() == 1) {
228  // The argument is a parameter of the function. Check it
229  // here, so we can do early stopping if it's invalid.
230  parametersValid &= var.isValidReal(values[0]);
231  assert(values.size() == 1);
232  }
233  }
234 
235  if (!parametersValid)
236  return {};
237 
238  auto results = getValuesOfBoundFunction(*_evalData);
239 
240  if (_clipInvalid) {
241  RooSpan<double> resultsWritable(_evalData->getWritableBatch(_func));
242  assert(results.data() == resultsWritable.data());
243  assert(results.size() == resultsWritable.size());
244 
245  // Run through all events, and check if the given coordinates are valid:
246  for (std::size_t coord=0; coord < coordinates.size(); ++coord) {
247  if (coordinates[coord].size() == 1)
248  continue; // We checked all parameters above
249 
250  for (std::size_t evt=0; evt < coordinates[coord].size(); ++evt) {
251  if (!_vars[coord]->isValidReal(coordinates[coord][evt]))
252  resultsWritable[evt] = 0.;
253  }
254  }
255  }
256 
257  return results;
258 }
259 
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// Evaluate the bound object at all locations indicated by the data in `evalData`.
263 /// \see RooAbsReal::getValues().
264 /// \param[in/out] evalData Struct with spans pointing to the data to be used for evaluation.
265 /// The spans can either have a size of `n`, in which case a batch of `n` results is returned, or they can have
266 /// a size of 1. In the latter case, the value in the span is broadcast to all `n` events.
267 /// \return Batch of function values for each coordinate given in the input spans.
269  return _func->getValues(evalData, _nset);
270 }
271 
272 
273 ////////////////////////////////////////////////////////////////////////////////
274 /// Return lower limit on i-th variable
275 
277 {
278  assert(isValid());
279 
280  return _vars[index]->getMin(RooNameReg::str(_rangeName));
281 }
282 
283 
284 ////////////////////////////////////////////////////////////////////////////////
285 /// Return upper limit on i-th variable
286 
288 {
289  assert(isValid());
290  return _vars[index]->getMax(RooNameReg::str(_rangeName));
291 }
292 
293 
294 ////////////////////////////////////////////////////////////////////////////////
295 /// Return name of function
296 
297 const char* RooRealBinding::getName() const
298 {
299  return _func->GetName() ;
300 }
301 
302 
303 ////////////////////////////////////////////////////////////////////////////////
304 
305 std::list<Double_t>* RooRealBinding::plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
306 {
307  return _func->plotSamplingHint(obs,xlo,xhi) ;
308 }
309 
310 
311 ////////////////////////////////////////////////////////////////////////////////
312 
313 std::list<Double_t>* RooRealBinding::binBoundaries(Int_t index) const
314 {
315  return _func->binBoundaries(*_vars[index],getMinLimit(index),getMaxLimit(index));
316 }
RooRealBinding::restoreXVec
virtual void restoreXVec() const
Restore value of all variables to previously saved values by saveXVec()
Definition: RooRealBinding.cxx:143
RooAbsFunc::_ncall
Int_t _ncall
Definition: RooAbsFunc.h:77
RooNameReg::instance
static RooNameReg & instance()
Return reference to singleton instance.
Definition: RooNameReg.cxx:51
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:100
RooAbsReal.h
RooMsgService.h
RooAbsReal::plotSamplingHint
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &obs, Double_t xlo, Double_t xhi) const
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
Definition: RooAbsReal.cxx:3816
RooRealBinding::_vars
std::vector< RooAbsRealLValue * > _vars
Definition: RooRealBinding.h:55
RooAbsFunc::getDimension
UInt_t getDimension() const
Definition: RooAbsFunc.h:33
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:61
RooAbsArg::getComponents
RooArgSet * getComponents() const
Create a RooArgSet with all components (branch nodes) of the expression tree headed by this object.
Definition: RooAbsArg.cxx:746
RooRealBinding::_evalData
std::unique_ptr< RooBatchCompute::RunContext > _evalData
Definition: RooRealBinding.h:65
RooArgSet.h
RooRealBinding::RooRealBinding
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'.
Definition: RooRealBinding.cxx:55
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooRealBinding::getValues
virtual RooSpan< const double > getValues(std::vector< RooSpan< const double >> coordinates) const
Evaluate the bound object at all locations indicated by the data in coordinates.
Definition: RooRealBinding.cxx:209
RooAbsCollection::fwdIterator
RooFIter fwdIterator() const
One-time forward iterator.
Definition: RooAbsCollection.h:193
RooAbsReal::binBoundaries
virtual std::list< Double_t > * binBoundaries(RooAbsRealLValue &obs, Double_t xlo, Double_t xhi) const
Retrieve bin boundaries if this distribution is binned in obs.
Definition: RooAbsReal.cxx:3805
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooRealBinding::saveXVec
virtual void saveXVec() const
Save value of all variables.
Definition: RooRealBinding.cxx:108
RooRealBinding::_xvecValid
Bool_t _xvecValid
Definition: RooRealBinding.h:57
RooRealBinding::_func
const RooAbsReal * _func
Definition: RooRealBinding.h:54
RooAbsReal
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:61
RooRealBinding::getMinLimit
virtual Double_t getMinLimit(UInt_t dimension) const
Return lower limit on i-th variable.
Definition: RooRealBinding.cxx:276
RooRealBinding::getMaxLimit
virtual Double_t getMaxLimit(UInt_t dimension) const
Return upper limit on i-th variable.
Definition: RooRealBinding.cxx:287
oocoutE
#define oocoutE(o, a)
Definition: RooMsgService.h:48
RooAbsFunc::_valid
Bool_t _valid
Definition: RooAbsFunc.h:79
bool
RooAbsFunc
Abstract interface for evaluating a real-valued function of one real variable and performing numerica...
Definition: RooAbsFunc.h:27
RooRealBinding::_compList
std::vector< RooAbsReal * > _compList
Definition: RooRealBinding.h:62
RooSpan::size
constexpr std::span< T >::index_type size() const noexcept
Definition: RooSpan.h:121
RooRealBinding::operator()
virtual Double_t operator()(const Double_t xvector[]) const
Evaluate the bound RooAbsReal at the variable values provided in xvector.
Definition: RooRealBinding.cxx:189
RooRealBinding
Lightweight interface adaptor that binds a RooAbsReal object to a subset of its servers and present i...
Definition: RooRealBinding.h:29
RooAbsFunc::_dimension
UInt_t _dimension
Definition: RooAbsFunc.h:78
size
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
RooFIter
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
Definition: RooLinkedListIter.h:40
RooRealBinding::_funcSave
Double_t _funcSave
Definition: RooRealBinding.h:64
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:101
RooRealBinding::getName
virtual const char * getName() const
Return name of function.
Definition: RooRealBinding.cxx:297
RooSpan::data
constexpr std::span< T >::pointer data() const
Definition: RooSpan.h:106
RooFIter::next
RooAbsArg * next()
Return next element or nullptr if at end.
Definition: RooLinkedListIter.h:49
RooRealBinding::plotSamplingHint
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &, Double_t, Double_t) const
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
Definition: RooRealBinding.cxx:305
RooAbsCollection::size
Storage_t::size_type size() const
Definition: RooAbsCollection.h:214
RooAbsReal::_value
Double_t _value
Definition: RooAbsReal.h:477
oocoutW
#define oocoutW(o, a)
Definition: RooMsgService.h:47
RooNameReg::constStr
const char * constStr(const TNamed *namePtr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:83
RooRealBinding::binBoundaries
virtual std::list< Double_t > * binBoundaries(Int_t) const
Definition: RooRealBinding.cxx:313
RooRealBinding::_nset
const RooArgSet * _nset
Definition: RooRealBinding.h:56
unsigned int
RooRealBinding::loadValues
void loadValues(const Double_t xvector[]) const
Load the vector of variable values into the RooRealVars associated as variables with the bound RooAbs...
Definition: RooRealBinding.cxx:171
RooAbsFunc::isValid
Bool_t isValid() const
Definition: RooAbsFunc.h:37
Double_t
double Double_t
Definition: RtypesCore.h:59
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
RooRealBinding::~RooRealBinding
virtual ~RooRealBinding()
Destructor.
Definition: RooRealBinding.cxx:98
RooNameReg::str
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.cxx:103
RooRealBinding::_rangeName
const TNamed * _rangeName
Definition: RooRealBinding.h:60
RooRealBinding::_compSave
std::vector< Double_t > _compSave
Definition: RooRealBinding.h:63
RooRealBinding::_xsave
Double_t * _xsave
Definition: RooRealBinding.h:59
RooAbsArg::dependsOn
Bool_t dependsOn(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=0, Bool_t valueOnly=kFALSE) const
Test whether we depend on (ie, are served by) any object in the specified collection.
Definition: RooAbsArg.cxx:799
RunContext.h
RooRealBinding.h
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
RooAbsReal::getValues
virtual RooSpan< const double > getValues(RooBatchCompute::RunContext &evalData, const RooArgSet *normSet=nullptr) const
by this change, please consult the release notes for ROOT 6.24 for guidance on how to make this trans...
Definition: RooAbsReal.cxx:312
RooRealBinding::getValuesOfBoundFunction
RooSpan< const double > getValuesOfBoundFunction(RooBatchCompute::RunContext &evalData) const
Evaluate the bound object at all locations indicated by the data in evalData.
Definition: RooRealBinding.cxx:268
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
RooAbsRealLValue::isValidReal
virtual Bool_t isValidReal(Double_t value, Bool_t printError=kFALSE) const
Check if given value is valid.
Definition: RooAbsRealLValue.cxx:124
RooAbsRealLValue
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
Definition: RooAbsRealLValue.h:31
RooAbsRealLValue.h
RooNameReg.h
RooBatchCompute::RunContext
This struct enables passing computation data around between elements of a computation graph.
Definition: RunContext.h:31
RooRealBinding::_clipInvalid
Bool_t _clipInvalid
Definition: RooRealBinding.h:58
RooSpan
A simple container to hold a batch of data values.
Definition: RooSpan.h:34
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:33
int