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