Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
FlexibleInterpVar.cxx
Go to the documentation of this file.
1// @(#)root/roostats:$Id: cranmer $
2// Author: Kyle Cranmer, Akira Shibata
3// Author: Giovanni Petrucciani (UCSD) (log-interpolation)
4/*************************************************************************
5 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12////////////////////////////////////////////////////////////////////////////////
13
14/** \class RooStats::HistFactory::FlexibleInterpVar
15 * \ingroup HistFactory
16 */
17
18#include <RooMsgService.h>
19#include <RooTrace.h>
20
23
24#include <Riostream.h>
25#include <TMath.h>
26
28
29using namespace RooStats;
30using namespace HistFactory;
31
32
33////////////////////////////////////////////////////////////////////////////////
34/// Default constructor
35
37{
39}
40
41
42////////////////////////////////////////////////////////////////////////////////
43
44FlexibleInterpVar::FlexibleInterpVar(const char* name, const char* title,
45 const RooArgList& paramList,
46 double argNominal, std::vector<double> const& lowVec, std::vector<double> const& highVec) :
47 FlexibleInterpVar{name, title, paramList, argNominal, lowVec, highVec, std::vector<int>(lowVec.size(), 0)}
48{
49}
50
51
52////////////////////////////////////////////////////////////////////////////////
53
54FlexibleInterpVar::FlexibleInterpVar(const char* name, const char* title,
55 const RooArgList& paramList,
56 double argNominal, std::vector<double> const& lowVec, std::vector<double> const& highVec,
57 std::vector<int> const& code) :
58 RooAbsReal(name, title),
59 _paramList("paramList","List of paramficients",this),
60 _nominal(argNominal), _low(lowVec), _high(highVec), _interpCode(code), _interpBoundary(1.)
61{
62 for (auto param : paramList) {
63 if (!dynamic_cast<RooAbsReal*>(param)) {
64 coutE(InputArguments) << "FlexibleInterpVar::ctor(" << GetName() << ") ERROR: paramficient " << param->GetName()
65 << " is not of type RooAbsReal" << std::endl ;
66 // use R__ASSERT which remains also in release mode
67 R__ASSERT(0) ;
68 }
69 _paramList.add(*param) ;
70 }
71
72 if (_low.size() != _paramList.size() || _low.size() != _high.size() || _low.size() != _interpCode.size()) {
73 coutE(InputArguments) << "FlexibleInterpVar::ctor(" << GetName() << ") invalid input std::vectors " << std::endl;
74 R__ASSERT(_low.size() == _paramList.size());
75 R__ASSERT(_low.size() == _high.size());
76 R__ASSERT(_low.size() == _interpCode.size());
77 }
78
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// Constructor of flat polynomial function
84
85FlexibleInterpVar::FlexibleInterpVar(const char* name, const char* title) :
86 RooAbsReal(name, title),
87 _paramList("paramList","List of coefficients",this)
88{
90}
91
92////////////////////////////////////////////////////////////////////////////////
93
95 RooAbsReal(other, name),
96 _paramList("paramList",this,other._paramList),
97 _nominal(other._nominal), _low(other._low), _high(other._high), _interpCode(other._interpCode), _interpBoundary(other._interpBoundary)
98
99{
101}
102
103
104////////////////////////////////////////////////////////////////////////////////
105/// Destructor
106
108{
110}
111
112
113////////////////////////////////////////////////////////////////////////////////
114
116 int index = _paramList.index(&param);
117 if(index<0){
118 coutE(InputArguments) << "FlexibleInterpVar::setInterpCode ERROR: " << param.GetName()
119 << " is not in list" << std::endl;
120 } else if(_interpCode.at(index) != code){
121 coutI(InputArguments) << "FlexibleInterpVar::setInterpCode : " << param.GetName()
122 << " is now " << code << std::endl;
123 _interpCode.at(index) = code;
124 // GHL: Adding suggestion by Swagato:
126 }
127}
128
129////////////////////////////////////////////////////////////////////////////////
130
132 for(unsigned int i=0; i<_interpCode.size(); ++i){
133 _interpCode.at(i) = code;
134 }
135 // GHL: Adding suggestion by Swagato:
137}
138
139////////////////////////////////////////////////////////////////////////////////
140
141void FlexibleInterpVar::setNominal(double newNominal){
142 coutW(InputArguments) << "FlexibleInterpVar::setNominal : nominal is now " << newNominal << std::endl ;
143 _nominal = newNominal;
144
146}
147
148////////////////////////////////////////////////////////////////////////////////
149
150void FlexibleInterpVar::setLow(RooAbsReal& param, double newLow){
151 int index = _paramList.index(&param);
152 if(index<0){
153 coutE(InputArguments) << "FlexibleInterpVar::setLow ERROR: " << param.GetName()
154 << " is not in list" << std::endl ;
155 } else {
156 coutW(InputArguments) << "FlexibleInterpVar::setLow : " << param.GetName()
157 << " is now " << newLow << std::endl ;
158 _low.at(index) = newLow;
159 }
160
162}
163
164////////////////////////////////////////////////////////////////////////////////
165
166void FlexibleInterpVar::setHigh(RooAbsReal& param, double newHigh){
167 int index = _paramList.index(&param);
168 if(index<0){
169 coutE(InputArguments) << "FlexibleInterpVar::setHigh ERROR: " << param.GetName()
170 << " is not in list" << std::endl ;
171 } else {
172 coutW(InputArguments) << "FlexibleInterpVar::setHigh : " << param.GetName()
173 << " is now " << newHigh << std::endl ;
174 _high.at(index) = newHigh;
175 }
176
178}
179
180////////////////////////////////////////////////////////////////////////////////
181
183 for(unsigned int i=0; i<_interpCode.size(); ++i){
184 coutI(InputArguments) <<"interp code for " << _paramList.at(i)->GetName() << " = " << _interpCode.at(i) << std::endl;
185 // GHL: Adding suggestion by Swagato:
186 if( _low.at(i) <= 0.001 ) coutE(InputArguments) << GetName() << ", " << _paramList.at(i)->GetName() << ": low value = " << _low.at(i) << std::endl;
187 if( _high.at(i) <= 0.001 ) coutE(InputArguments) << GetName() << ", " << _paramList.at(i)->GetName() << ": high value = " << _high.at(i) << std::endl;
188 }
189
190}
191
192
193////////////////////////////////////////////////////////////////////////////////
194/// Calculate and return value of polynomial
195
197{
198 double total(_nominal);
199 for (std::size_t i = 0; i < _paramList.size(); ++i) {
200 int code = _interpCode[i];
201 if (code < 0 || code > 4) {
202 coutE(InputArguments) << "FlexibleInterpVar::evaluate ERROR: param " << i
203 << " with unknown interpolation code" << std::endl;
204 }
205 // To get consistent codes with the PiecewiseInterpolation
206 if (code == 4) {
207 code = 5;
208 }
209 double paramVal = static_cast<const RooAbsReal *>(&_paramList[i])->getVal();
211 paramVal, total);
212 }
213
214 if (total <= 0) {
216 }
217
218 return total;
219}
220
222{
223 unsigned int n = _interpCode.size();
224
225 std::string resName = "total_" + ctx.getTmpVarName();
226 ctx.addToCodeBody(this, "double " + resName + " = " + std::to_string(_nominal) + ";\n");
227 std::string code = "";
228 for (std::size_t i = 0; i < n; ++i) {
229
230 int interpCode = _interpCode[i];
231 if (interpCode < 0 || interpCode > 4) {
232 coutE(InputArguments) << "FlexibleInterpVar::evaluate ERROR: param " << i
233 << " with unknown interpolation code" << std::endl;
234 }
235 // To get consistent codes with the PiecewiseInterpolation
236 if (interpCode == 4) {
237 interpCode = 5;
238 }
239 code += resName + " += " +
240 ctx.buildCall("RooFit::Detail::EvaluateFuncs::flexibleInterp", interpCode, _low[i], _high[i],
241 _interpBoundary, _nominal, _paramList[i], resName) +
242 ";\n";
243 }
244 code += resName + " = " + resName + " <= 0 ? TMath::Limits<double>::Min() : " + resName + ";\n";
245 ctx.addToCodeBody(this, code);
246 ctx.addResult(this, resName);
247}
248
249void FlexibleInterpVar::computeBatch(double *output, size_t /*nEvents*/, RooFit::Detail::DataMap const &dataMap) const
250{
251 double total(_nominal);
252
253 for (std::size_t i = 0; i < _paramList.size(); ++i) {
254 int code = _interpCode[i];
255 if (code < 0 || code > 4) {
256 coutE(InputArguments) << "FlexibleInterpVar::evaluate ERROR: param " << i
257 << " with unknown interpolation code" << std::endl;
258 }
259 // To get consistent codes with the PiecewiseInterpolation
260 if (code == 4) {
261 code = 5;
262 }
264 dataMap.at(&_paramList[i])[0], total);
265 }
266
267 if (total <= 0) {
269 }
270
271 output[0] = total;
272}
273
274void FlexibleInterpVar::printMultiline(std::ostream& os, Int_t contents,
275 bool verbose, TString indent) const
276{
277 RooAbsReal::printMultiline(os,contents,verbose,indent);
278 os << indent << "--- FlexibleInterpVar ---" << std::endl;
280}
281
283{
284 for (int i=0;i<(int)_low.size();i++) {
285 auto& param = static_cast<RooAbsReal&>(_paramList[i]);
286 os << std::setw(36) << param.GetName()<<": "<<std::setw(7) << _low[i]<<" "<<std::setw(7) << _high[i]
287 <<std::endl;
288 }
289}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define coutI(a)
#define coutW(a)
#define coutE(a)
#define TRACE_DESTROY
Definition RooTrace.h:24
#define TRACE_CREATE
Definition RooTrace.h:23
#define ClassImp(name)
Definition Rtypes.h:377
static void indent(ostringstream &buf, int indent_level)
#define R__ASSERT(e)
Definition TError.h:118
static unsigned int total
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
char name[80]
Definition TGX11.cxx:110
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition RooAbsArg.h:490
Int_t index(const RooAbsArg *arg) const
Returns index of given arg, or -1 if arg is not in the collection.
Storage_t::size_type size() const
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
Structure printing.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
A class to maintain the context for squashing of RooFit models into code.
std::string buildCall(std::string const &funcname, Args_t const &...args)
Build the code to call the function with name funcname, passing some arguments.
void addResult(RooAbsArg const *key, std::string const &value)
A function to save an expression that includes/depends on the result of the input node.
void addToCodeBody(RooAbsArg const *klass, std::string const &in)
Adds the input string to the squashed code body.
std::string getTmpVarName()
Get a unique variable name to be used in the generated code.
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
Definition DataMap.cxx:22
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
Interface for detailed printing of object.
void setInterpCode(RooAbsReal &param, int code)
void setLow(RooAbsReal &param, double newLow)
void setHigh(RooAbsReal &param, double newHigh)
void translate(RooFit::Detail::CodeSquashContext &ctx) const override
This function defines a translation for each RooAbsReal based object that can be used to express the ...
void computeBatch(double *output, size_t size, RooFit::Detail::DataMap const &) const override
Base function for computing multiple values of a RooAbsReal.
double evaluate() const override
Calculate and return value of polynomial.
virtual void printFlexibleInterpVars(std::ostream &os) const
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139
const Int_t n
Definition legend1.C:16
double flexibleInterp(unsigned int code, double low, double high, double boundary, double nominal, double paramVal, double res)
Namespace for the RooStats classes.
Definition Asimov.h:19
static T Min()
Returns maximum representation for type T.
Definition TMath.h:925
static void output()