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
22Lightweight interface adaptor that binds a RooAbsReal object to a subset
23of 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
39using 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
55RooRealBinding::RooRealBinding(const RooAbsReal& func, const RooArgSet &vars, const RooArgSet* nset, bool 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(nullptr,InputArguments) << "RooRealBinding: cannot bind to " << var->GetName()
64 << ". Variables need to be assignable, e.g. instances of RooRealVar." << endl ;
65 _valid= false;
66 }
67 if (!_func->dependsOn(*_vars[index])) {
68 oocoutW(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 = true ;
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[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 }
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 }
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.
171void RooRealBinding::loadValues(const double xvector[]) const
172{
173 _xvecValid = true ;
174 const char* range = RooNameReg::str(_rangeName) ;
175 for(UInt_t index= 0; index < _dimension; index++) {
176 if (_clipInvalid && !_vars[index]->isValidReal(xvector[index])) {
177 _xvecValid = false ;
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
189double RooRealBinding::operator()(const double xvector[]) const
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
297const char* RooRealBinding::getName() const
298{
299 return _func->GetName() ;
300}
301
302
303////////////////////////////////////////////////////////////////////////////////
304
305std::list<double>* RooRealBinding::plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const
306{
307 return _func->plotSamplingHint(obs,xlo,xhi) ;
308}
309
310
311////////////////////////////////////////////////////////////////////////////////
312
314{
316}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define oocoutW(o, a)
Definition: RooMsgService.h:51
#define oocoutE(o, a)
Definition: RooMsgService.h:52
#define ClassImp(name)
Definition: Rtypes.h:375
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
bool dependsOn(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=0, bool valueOnly=false) const
Test whether we depend on (ie, are served by) any object in the specified collection.
Definition: RooAbsArg.cxx:829
RooArgSet * getComponents() const
Create a RooArgSet with all components (branch nodes) of the expression tree headed by this object.
Definition: RooAbsArg.cxx:778
RooFIter fwdIterator() const
One-time forward iterator.
Storage_t::size_type size() const
Abstract interface for evaluating a real-valued function of one real variable and performing numerica...
Definition: RooAbsFunc.h:27
UInt_t _dimension
Number of observables.
Definition: RooAbsFunc.h:79
bool isValid() const
Definition: RooAbsFunc.h:37
Int_t _ncall
Function call counter.
Definition: RooAbsFunc.h:78
UInt_t getDimension() const
Definition: RooAbsFunc.h:33
bool _valid
Is binding in valid state?
Definition: RooAbsFunc.h:80
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
bool isValidReal(double value, bool printError=false) const override
Check if given value is valid.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:64
virtual std::list< double > * binBoundaries(RooAbsRealLValue &obs, double xlo, double xhi) const
Retrieve bin boundaries if this distribution is binned in obs.
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:94
virtual RooSpan< const double > getValues(RooBatchCompute::RunContext &evalData, const RooArgSet *normSet=nullptr) const
Definition: RooAbsReal.cxx:311
double _value
Cache for current value of object.
Definition: RooAbsReal.h:484
virtual std::list< double > * plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:57
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
RooAbsArg * next()
Return next element or nullptr if at end.
static const char * str(const TNamed *ptr)
Return C++ string corresponding to given TNamed pointer.
Definition: RooNameReg.h:37
Lightweight interface adaptor that binds a RooAbsReal object to a subset of its servers and present i...
std::list< double > * plotSamplingHint(RooAbsRealLValue &, double, double) const override
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
double getMinLimit(UInt_t dimension) const override
Return lower limit on i-th variable.
~RooRealBinding() override
Destructor.
double operator()(const double xvector[]) const override
Evaluate the bound RooAbsReal at the variable values provided in xvector.
double getMaxLimit(UInt_t dimension) const override
Return upper limit on i-th variable.
void restoreXVec() const override
Restore value of all variables to previously saved values by saveXVec()
std::list< double > * binBoundaries(Int_t) const override
std::vector< RooAbsRealLValue * > _vars
Non-owned pointers to variables.
std::vector< RooAbsReal * > _compList
!
std::vector< double > _compSave
!
const char * getName() const override
Return name of function.
RooSpan< const double > getValuesOfBoundFunction(RooBatchCompute::RunContext &evalData) const
Evaluate the bound object at all locations indicated by the data in evalData.
void loadValues(const double xvector[]) const
Load the vector of variable values into the RooRealVars associated as variables with the bound RooAbs...
const RooArgSet * _nset
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.
const RooAbsReal * _func
std::unique_ptr< RooBatchCompute::RunContext > _evalData
Memory for batch evaluations.
const TNamed * _rangeName
!
RooRealBinding(const RooAbsReal &func, const RooArgSet &vars, const RooArgSet *nset=0, bool clipInvalid=false, const TNamed *rangeName=0)
Construct a lightweight function binding of RooAbsReal func to variables 'vars'.
void saveXVec() const override
Save value of all variables.
A simple container to hold a batch of data values.
Definition: RooSpan.h:34
constexpr std::span< T >::pointer data() const
Definition: RooSpan.h:106
constexpr std::span< T >::index_type size() const noexcept
Definition: RooSpan.h:121
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
@ InputArguments
Definition: RooGlobalFunc.h:64
This struct enables passing computation data around between elements of a computation graph.
Definition: RunContext.h:32