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
34#include <cassert>
35
36
37
38using namespace std;
39
41;
42
43
44////////////////////////////////////////////////////////////////////////////////
45/// Construct a lightweight function binding of RooAbsReal func to
46/// variables 'vars'. Use the provided nset as normalization set to
47/// be passed to RooAbsReal::getVal() If rangeName is not null, use
48/// the range of with that name as range associated with the
49/// variables of this function binding. If clipInvalid is true,
50/// values requested to the function binding that are outside the
51/// defined range of the variables are clipped to fit in the defined
52/// range.
53
54RooRealBinding::RooRealBinding(const RooAbsReal& func, const RooArgSet &vars, const RooArgSet* nset, bool clipInvalid, const TNamed* rangeName) :
55 RooAbsFunc(vars.getSize()), _func(&func), _vars(), _nset(nset), _clipInvalid(clipInvalid), _rangeName(rangeName), _funcSave(0)
56{
57 // check that all of the arguments are real valued and store them
58 for (unsigned int index=0; index < vars.size(); ++index) {
59 RooAbsArg* var = vars[index];
60 _vars.push_back(dynamic_cast<RooAbsRealLValue*>(var));
61 if(_vars.back() == nullptr) {
62 oocoutE(nullptr,InputArguments) << "RooRealBinding: cannot bind to " << var->GetName()
63 << ". Variables need to be assignable, e.g. instances of RooRealVar." << endl ;
64 _valid= false;
65 }
66 if (!_func->dependsOn(*_vars[index])) {
67 oocoutW(nullptr, InputArguments) << "RooRealBinding: The function " << func.GetName() << " does not depend on the parameter " << _vars[index]->GetName()
68 << ". Note that passing copies of the parameters is not supported." << std::endl;
69 }
70 }
71
72 _xvecValid = true ;
73}
74
75
76////////////////////////////////////////////////////////////////////////////////
77/// Construct a lightweight function binding of RooAbsReal func to
78/// variables 'vars'. Use the provided nset as normalization set to
79/// be passed to RooAbsReal::getVal() If rangeName is not null, use
80/// the range of with that name as range associated with the
81/// variables of this function binding. If clipInvalid is true,
82/// values requested to the function binding that are outside the
83/// defined range of the variables are clipped to fit in the defined
84/// range.
85
87 RooAbsFunc(other), _func(other._func), _vars(other._vars), _nset(nset?nset:other._nset), _xvecValid(other._xvecValid),
88 _clipInvalid(other._clipInvalid), _rangeName(other._rangeName), _funcSave(other._funcSave)
89{
90
91}
92
93
95
96
97////////////////////////////////////////////////////////////////////////////////
98/// Save value of all variables
99
101{
102 if (_xsave.empty()) {
103 _xsave.resize(getDimension());
104 std::unique_ptr<RooArgSet> comps{_func->getComponents()};
105 for (auto* arg : dynamic_range_cast<RooAbsArg*>(*comps)) {
106 if (arg) {
107 _compList.push_back(static_cast<RooAbsReal*>(arg)) ;
108 _compSave.push_back(0.0) ;
109 }
110 }
111 }
113
114 // Save components
115 auto ci = _compList.begin() ;
116 auto si = _compSave.begin() ;
117 while(ci != _compList.end()) {
118 *si = (*ci)->_value ;
119 ++si;
120 ++ci;
121 }
122
123 for (UInt_t i=0 ; i<getDimension() ; i++) {
124 _xsave[i] = _vars[i]->getVal() ;
125 }
126}
127
128////////////////////////////////////////////////////////////////////////////////
129/// Restore value of all variables to previously
130/// saved values by saveXVec()
131
133{
134 if (_xsave.empty()) {
135 return ;
136 }
138
139 // Restore components
140 auto ci = _compList.begin() ;
141 auto si = _compSave.begin() ;
142 while (ci != _compList.end()) {
143 (*ci)->_value = *si ;
144 ++ci;
145 ++si;
146 }
147
148 for (UInt_t i=0 ; i<getDimension() ; i++) {
149 _vars[i]->setVal(_xsave[i]) ;
150 }
151}
152
153
154
155////////////////////////////////////////////////////////////////////////////////
156/// Load the vector of variable values into the RooRealVars associated
157/// as variables with the bound RooAbsReal function.
158/// \warning This will load as many values as the dimensionality of the function
159/// requires. The size of `xvector` is not checked.
160void RooRealBinding::loadValues(const double xvector[]) const
161{
162 _xvecValid = true ;
163 const char* range = RooNameReg::str(_rangeName) ;
164 for(UInt_t index= 0; index < _dimension; index++) {
165 if (_clipInvalid && !_vars[index]->isValidReal(xvector[index])) {
166 _xvecValid = false ;
167 } else {
168 _vars[index]->setVal(xvector[index],range);
169 }
170 }
171
172}
173
174
175////////////////////////////////////////////////////////////////////////////////
176/// Evaluate the bound RooAbsReal at the variable values provided in xvector
177
178double RooRealBinding::operator()(const double xvector[]) const
179{
180 assert(isValid());
181 _ncall++ ;
182 loadValues(xvector);
183 return _xvecValid ? _func->getVal(_nset) : 0. ;
184}
185
186
187////////////////////////////////////////////////////////////////////////////////
188/// Return lower limit on i-th variable
189
191{
192 assert(isValid());
193
194 return _vars[index]->getMin(RooNameReg::str(_rangeName));
195}
196
197
198////////////////////////////////////////////////////////////////////////////////
199/// Return upper limit on i-th variable
200
202{
203 assert(isValid());
204 return _vars[index]->getMax(RooNameReg::str(_rangeName));
205}
206
207
208////////////////////////////////////////////////////////////////////////////////
209/// Return name of function
210
211const char* RooRealBinding::getName() const
212{
213 return _func->GetName() ;
214}
215
216
217////////////////////////////////////////////////////////////////////////////////
218
219std::list<double>* RooRealBinding::plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const
220{
221 return _func->plotSamplingHint(obs,xlo,xhi) ;
222}
223
224
225////////////////////////////////////////////////////////////////////////////////
226
228{
230}
#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
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:79
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...
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
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:103
double _value
Cache for current value of object.
Definition RooAbsReal.h:543
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.
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
const RooAbsReal * _func
const TNamed * _rangeName
!
void saveXVec() const override
Save value of all variables.
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