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 for (auto* arg : dynamic_range_cast<RooAbsArg*>(*comps)) {
114 if (arg) {
115 _compList.push_back(static_cast<RooAbsReal*>(arg)) ;
116 _compSave.push_back(0) ;
117 }
118 }
119 delete comps ;
120 }
122
123 // Save components
124 auto ci = _compList.begin() ;
125 auto si = _compSave.begin() ;
126 while(ci != _compList.end()) {
127 *si = (*ci)->_value ;
128 ++si;
129 ++ci;
130 }
131
132 for (UInt_t i=0 ; i<getDimension() ; i++) {
133 _xsave[i] = _vars[i]->getVal() ;
134 }
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// Restore value of all variables to previously
139/// saved values by saveXVec()
140
142{
143 if (!_xsave) {
144 return ;
145 }
147
148 // Restore components
149 auto ci = _compList.begin() ;
150 auto si = _compSave.begin() ;
151 while (ci != _compList.end()) {
152 (*ci)->_value = *si ;
153 ++ci;
154 ++si;
155 }
156
157 for (UInt_t i=0 ; i<getDimension() ; i++) {
158 _vars[i]->setVal(_xsave[i]) ;
159 }
160}
161
162
163
164////////////////////////////////////////////////////////////////////////////////
165/// Load the vector of variable values into the RooRealVars associated
166/// as variables with the bound RooAbsReal function.
167/// \warning This will load as many values as the dimensionality of the function
168/// requires. The size of `xvector` is not checked.
169void RooRealBinding::loadValues(const double xvector[]) const
170{
171 _xvecValid = true ;
172 const char* range = RooNameReg::str(_rangeName) ;
173 for(UInt_t index= 0; index < _dimension; index++) {
174 if (_clipInvalid && !_vars[index]->isValidReal(xvector[index])) {
175 _xvecValid = false ;
176 } else {
177 _vars[index]->setVal(xvector[index],range);
178 }
179 }
180
181}
182
183
184////////////////////////////////////////////////////////////////////////////////
185/// Evaluate the bound RooAbsReal at the variable values provided in xvector
186
187double RooRealBinding::operator()(const double xvector[]) const
188{
189 assert(isValid());
190 _ncall++ ;
191 loadValues(xvector);
192 return _xvecValid ? _func->getVal(_nset) : 0. ;
193}
194
195
196////////////////////////////////////////////////////////////////////////////////
197/// Evaluate the bound object at all locations indicated by the data in `coordinates`.
198/// If `_clipInvalid` is set, the function is set to zero at all points in the arguments
199/// that are not within the range of the observables.
200/// \param coordinates Vector of spans that contain the points where the function should be evaluated.
201/// The ordinal position in the vector corresponds to the ordinal position in the set of
202/// {observables, parameters} that were passed to the constructor.
203/// The spans can either have a size of `n`, in which case a batch of `n` results is returned, or they can have
204/// a size of 1. In the latter case, the value in the span is broadcast to all `n` events.
205/// \return Batch of function values for each coordinate given in the input spans. If a parameter is invalid, i.e.,
206/// out of its range, an empty span is returned. If an observable is invalid, the function value is 0.
208 assert(isValid());
209 _ncall += coordinates.front().size();
210
211 bool parametersValid = true;
212
213 // Use _evalData to hold on to memory between integration calls
214 if (!_evalData) {
216 } else {
217 _evalData->clear();
218 }
220
221 for (unsigned int dim=0; dim < coordinates.size(); ++dim) {
222 const RooSpan<const double>& values = coordinates[dim];
223 RooAbsRealLValue& var = *_vars[dim];
224 _evalData->spans[&var] = values;
225 if (_clipInvalid && values.size() == 1) {
226 // The argument is a parameter of the function. Check it
227 // here, so we can do early stopping if it's invalid.
228 parametersValid &= var.isValidReal(values[0]);
229 assert(values.size() == 1);
230 }
231 }
232
233 if (!parametersValid)
234 return {};
235
236 auto results = getValuesOfBoundFunction(*_evalData);
237
238 if (_clipInvalid) {
239 RooSpan<double> resultsWritable(_evalData->getWritableBatch(_func));
240 assert(results.data() == resultsWritable.data());
241 assert(results.size() == resultsWritable.size());
242
243 // Run through all events, and check if the given coordinates are valid:
244 for (std::size_t coord=0; coord < coordinates.size(); ++coord) {
245 if (coordinates[coord].size() == 1)
246 continue; // We checked all parameters above
247
248 for (std::size_t evt=0; evt < coordinates[coord].size(); ++evt) {
249 if (!_vars[coord]->isValidReal(coordinates[coord][evt]))
250 resultsWritable[evt] = 0.;
251 }
252 }
253 }
254
255 return results;
256}
257
258
259////////////////////////////////////////////////////////////////////////////////
260/// Evaluate the bound object at all locations indicated by the data in `evalData`.
261/// \see RooAbsReal::getValues().
262/// \param[in,out] evalData Struct with spans pointing to the data to be used for evaluation.
263/// The spans can either have a size of `n`, in which case a batch of `n` results is returned, or they can have
264/// a size of 1. In the latter case, the value in the span is broadcast to all `n` events.
265/// \return Batch of function values for each coordinate given in the input spans.
267 return _func->getValues(evalData, _nset);
268}
269
270
271////////////////////////////////////////////////////////////////////////////////
272/// Return lower limit on i-th variable
273
275{
276 assert(isValid());
277
278 return _vars[index]->getMin(RooNameReg::str(_rangeName));
279}
280
281
282////////////////////////////////////////////////////////////////////////////////
283/// Return upper limit on i-th variable
284
286{
287 assert(isValid());
288 return _vars[index]->getMax(RooNameReg::str(_rangeName));
289}
290
291
292////////////////////////////////////////////////////////////////////////////////
293/// Return name of function
294
295const char* RooRealBinding::getName() const
296{
297 return _func->GetName() ;
298}
299
300
301////////////////////////////////////////////////////////////////////////////////
302
303std::list<double>* RooRealBinding::plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const
304{
305 return _func->plotSamplingHint(obs,xlo,xhi) ;
306}
307
308
309////////////////////////////////////////////////////////////////////////////////
310
312{
314}
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:71
bool dependsOn(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=nullptr, bool valueOnly=false) const
Test whether we depend on (ie, are served by) any object in the specified collection.
Definition: RooAbsArg.cxx:805
RooArgSet * getComponents() const
Create a RooArgSet with all components (branch nodes) of the expression tree headed by this object.
Definition: RooAbsArg.cxx:754
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:62
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:91
virtual RooSpan< const double > getValues(RooBatchCompute::RunContext &evalData, const RooArgSet *normSet=nullptr) const
Definition: RooAbsReal.cxx:280
double _value
Cache for current value of object.
Definition: RooAbsReal.h:480
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:56
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.
RooRealBinding(const RooAbsReal &func, const RooArgSet &vars, const RooArgSet *nset=nullptr, bool clipInvalid=false, const TNamed *rangeName=nullptr)
Construct a lightweight function binding of RooAbsReal func to variables 'vars'.
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
!
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:62
This struct enables passing computation data around between elements of a computation graph.
Definition: RunContext.h:32