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_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 ;
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
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 }
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_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])) {
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/// \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 = _func->getValues(*_evalData, _nset);
237 assert(coordinates.front().size() == results.size());
238
239 if (_clipInvalid) {
240 RooSpan<double> resultsWritable(_evalData->getWritableBatch(_func));
241 assert(results.data() == resultsWritable.data());
242 assert(results.size() == resultsWritable.size());
243
244 // Run through all events, and check if the given coordinates are valid:
245 for (std::size_t coord=0; coord < coordinates.size(); ++coord) {
246 if (coordinates[coord].size() == 1)
247 continue; // We checked all parameters above
248
249 for (std::size_t evt=0; evt < coordinates[coord].size(); ++evt) {
250 if (!_vars[coord]->isValidReal(coordinates[coord][evt]))
251 resultsWritable[evt] = 0.;
252 }
253 }
254 }
255
256 return results;
257}
258
259
260////////////////////////////////////////////////////////////////////////////////
261/// Return lower limit on i-th variable
262
264{
265 assert(isValid());
266
267 return _vars[index]->getMin(RooNameReg::str(_rangeName));
268}
269
270
271////////////////////////////////////////////////////////////////////////////////
272/// Return upper limit on i-th variable
273
275{
276 assert(isValid());
277 return _vars[index]->getMax(RooNameReg::str(_rangeName));
278}
279
280
281////////////////////////////////////////////////////////////////////////////////
282/// Return name of function
283
284const char* RooRealBinding::getName() const
285{
286 return _func->GetName() ;
287}
288
289
290////////////////////////////////////////////////////////////////////////////////
291
292std::list<Double_t>* RooRealBinding::plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
293{
294 return _func->plotSamplingHint(obs,xlo,xhi) ;
295}
296
297
298////////////////////////////////////////////////////////////////////////////////
299
300std::list<Double_t>* RooRealBinding::binBoundaries(Int_t index) const
301{
302 return _func->binBoundaries(*_vars[index],getMinLimit(index),getMaxLimit(index));
303}
#define oocoutW(o, a)
#define oocoutE(o, a)
const Bool_t kFALSE
Definition RtypesCore.h:92
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassImp(name)
Definition Rtypes.h:364
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:72
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.
RooArgSet * getComponents() const
Create a RooArgSet with all components (branch nodes) of the expression tree headed by this object.
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
Bool_t isValid() const
Definition RooAbsFunc.h:37
UInt_t _dimension
Definition RooAbsFunc.h:81
Int_t _ncall
Definition RooAbsFunc.h:80
UInt_t getDimension() const
Definition RooAbsFunc.h:33
Bool_t _valid
Definition RooAbsFunc.h:82
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
virtual Bool_t isValidReal(Double_t value, Bool_t printError=kFALSE) const
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:61
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.
Double_t _value
Definition RooAbsReal.h:475
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...
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:91
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...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:29
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.
static RooNameReg & instance()
Return reference to singleton instance.
const char * constStr(const TNamed *namePtr)
Return C++ string corresponding to given TNamed pointer.
Lightweight interface adaptor that binds a RooAbsReal object to a subset of its servers and present i...
virtual ~RooRealBinding()
Destructor.
virtual void restoreXVec() const
Restore value of all variables to previously saved values by saveXVec()
void loadValues(const Double_t xvector[]) const
Load the vector of variable values into the RooRealVars associated as variables with the bound RooAbs...
virtual std::list< Double_t > * binBoundaries(Int_t) const
Double_t * _xsave
virtual void saveXVec() const
Save value of all variables.
virtual const char * getName() const
Return name of function.
std::vector< RooAbsRealLValue * > _vars
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'.
std::vector< RooAbsReal * > _compList
virtual Double_t getMinLimit(UInt_t dimension) const
Return lower limit on i-th variable.
virtual Double_t operator()(const Double_t xvector[]) const
Evaluate the bound RooAbsReal at the variable values provided in xvector.
std::vector< Double_t > _compSave
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
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &, Double_t, Double_t) const
const TNamed * _rangeName
virtual Double_t getMaxLimit(UInt_t dimension) const
Return upper limit on i-th variable.
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
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:37
This struct enables passing computation data around between elements of a computation graph.
Definition RunContext.h:31