Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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), _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), _rangeName(other._rangeName), _funcSave(other._funcSave)
90{
91
92}
93
94
96
97
98////////////////////////////////////////////////////////////////////////////////
99/// Save value of all variables
100
102{
103 if (_xsave.empty()) {
104 _xsave.resize(getDimension());
105 std::unique_ptr<RooArgSet> comps{_func->getComponents()};
106 for (auto* arg : dynamic_range_cast<RooAbsArg*>(*comps)) {
107 if (arg) {
108 _compList.push_back(static_cast<RooAbsReal*>(arg)) ;
109 _compSave.push_back(0.0) ;
110 }
111 }
112 }
114
115 // Save components
116 auto ci = _compList.begin() ;
117 auto si = _compSave.begin() ;
118 while(ci != _compList.end()) {
119 *si = (*ci)->_value ;
120 ++si;
121 ++ci;
122 }
123
124 for (UInt_t i=0 ; i<getDimension() ; i++) {
125 _xsave[i] = _vars[i]->getVal() ;
126 }
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Restore value of all variables to previously
131/// saved values by saveXVec()
132
134{
135 if (_xsave.empty()) {
136 return ;
137 }
139
140 // Restore components
141 auto ci = _compList.begin() ;
142 auto si = _compSave.begin() ;
143 while (ci != _compList.end()) {
144 (*ci)->_value = *si ;
145 ++ci;
146 ++si;
147 }
148
149 for (UInt_t i=0 ; i<getDimension() ; i++) {
150 _vars[i]->setVal(_xsave[i]) ;
151 }
152}
153
154
155
156////////////////////////////////////////////////////////////////////////////////
157/// Load the vector of variable values into the RooRealVars associated
158/// as variables with the bound RooAbsReal function.
159/// \warning This will load as many values as the dimensionality of the function
160/// requires. The size of `xvector` is not checked.
161void RooRealBinding::loadValues(const double xvector[]) const
162{
163 _xvecValid = true ;
164 const char* range = RooNameReg::str(_rangeName) ;
165 for(UInt_t index= 0; index < _dimension; index++) {
166 if (_clipInvalid && !_vars[index]->isValidReal(xvector[index])) {
167 _xvecValid = false ;
168 } else {
169 _vars[index]->setVal(xvector[index],range);
170 }
171 }
172
173}
174
175
176////////////////////////////////////////////////////////////////////////////////
177/// Evaluate the bound RooAbsReal at the variable values provided in xvector
178
179double RooRealBinding::operator()(const double xvector[]) const
180{
181 assert(isValid());
182 _ncall++ ;
183 loadValues(xvector);
184 return _xvecValid ? _func->getVal(_nset) : 0. ;
185}
186
187
188////////////////////////////////////////////////////////////////////////////////
189/// Evaluate the bound object at all locations indicated by the data in `coordinates`.
190/// If `_clipInvalid` is set, the function is set to zero at all points in the arguments
191/// that are not within the range of the observables.
192/// \param coordinates Vector of spans that contain the points where the function should be evaluated.
193/// The ordinal position in the vector corresponds to the ordinal position in the set of
194/// {observables, parameters} that were passed to the constructor.
195/// The spans can either have a size of `n`, in which case a batch of `n` results is returned, or they can have
196/// a size of 1. In the latter case, the value in the span is broadcast to all `n` events.
197/// \return Batch of function values for each coordinate given in the input spans. If a parameter is invalid, i.e.,
198/// out of its range, an empty span is returned. If an observable is invalid, the function value is 0.
200 assert(isValid());
201 _ncall += coordinates.front().size();
202
203 bool parametersValid = true;
204
205 // Use _evalData to hold on to memory between integration calls
206 if (!_evalData) {
207 _evalData = std::make_unique<RooBatchCompute::RunContext>();
208 } else {
209 _evalData->clear();
210 }
212
213 for (unsigned int dim=0; dim < coordinates.size(); ++dim) {
214 const RooSpan<const double>& values = coordinates[dim];
215 RooAbsRealLValue& var = *_vars[dim];
216 _evalData->spans[&var] = values;
217 if (_clipInvalid && values.size() == 1) {
218 // The argument is a parameter of the function. Check it
219 // here, so we can do early stopping if it's invalid.
220 parametersValid &= var.isValidReal(values[0]);
221 assert(values.size() == 1);
222 }
223 }
224
225 if (!parametersValid)
226 return {};
227
228 auto results = getValuesOfBoundFunction(*_evalData);
229
230 if (_clipInvalid) {
231 RooSpan<double> resultsWritable(_evalData->getWritableBatch(_func));
232 assert(results.data() == resultsWritable.data());
233 assert(results.size() == resultsWritable.size());
234
235 // Run through all events, and check if the given coordinates are valid:
236 for (std::size_t coord=0; coord < coordinates.size(); ++coord) {
237 if (coordinates[coord].size() == 1)
238 continue; // We checked all parameters above
239
240 for (std::size_t evt=0; evt < coordinates[coord].size(); ++evt) {
241 if (!_vars[coord]->isValidReal(coordinates[coord][evt]))
242 resultsWritable[evt] = 0.;
243 }
244 }
245 }
246
247 return results;
248}
249
250
251////////////////////////////////////////////////////////////////////////////////
252/// Evaluate the bound object at all locations indicated by the data in `evalData`.
253/// \see RooAbsReal::getValues().
254/// \param[in,out] evalData Struct with spans pointing to the data to be used for evaluation.
255/// The spans can either have a size of `n`, in which case a batch of `n` results is returned, or they can have
256/// a size of 1. In the latter case, the value in the span is broadcast to all `n` events.
257/// \return Batch of function values for each coordinate given in the input spans.
259 return _func->getValues(evalData, _nset);
260}
261
262
263////////////////////////////////////////////////////////////////////////////////
264/// Return lower limit on i-th variable
265
267{
268 assert(isValid());
269
270 return _vars[index]->getMin(RooNameReg::str(_rangeName));
271}
272
273
274////////////////////////////////////////////////////////////////////////////////
275/// Return upper limit on i-th variable
276
278{
279 assert(isValid());
280 return _vars[index]->getMax(RooNameReg::str(_rangeName));
281}
282
283
284////////////////////////////////////////////////////////////////////////////////
285/// Return name of function
286
287const char* RooRealBinding::getName() const
288{
289 return _func->GetName() ;
290}
291
292
293////////////////////////////////////////////////////////////////////////////////
294
295std::list<double>* RooRealBinding::plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const
296{
297 return _func->plotSamplingHint(obs,xlo,xhi) ;
298}
299
300
301////////////////////////////////////////////////////////////////////////////////
302
304{
306}
RooAbsReal * _func
Definition RooMinuit.h:90
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define oocoutW(o, a)
#define oocoutE(o, a)
#define ClassImp(name)
Definition Rtypes.h:377
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:74
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.
RooFit::OwningPtr< RooArgSet > getComponents() const
Create a RooArgSet with all components (branch nodes) of the expression tree headed by this object.
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
double _value
Cache for current value of object.
Definition RooAbsReal.h:509
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:55
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
std::vector< double > _xsave
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::size_t size() const noexcept
Definition RooSpan.h:119
constexpr std::span< T >::pointer data() const
Definition RooSpan.h:102
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
This struct enables passing computation data around between elements of a computation graph.
Definition RunContext.h:32