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