Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooErrorVar.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 RooErrorVar.cxx
19\class RooErrorVar
20\ingroup Roofitcore
21
22Auxiliary class that represents the error
23of a RooRealVar as a separate object. The main reason of
24existence of this class is to facilitate the reuse of existing
25techniques to perform calculations that involve a RooRealVars
26error, such as calculating the pull value.
27**/
28
29#include "RooErrorVar.h"
30#include "RooAbsBinning.h"
31#include "RooStreamParser.h"
32#include "RooRangeBinning.h"
33#include "RooMsgService.h"
34#include "RooUniformBinning.h"
35
36using std::endl, std::istream, std::ostream;
37
39
40
41////////////////////////////////////////////////////////////////////////////////
42/// Construct an lvalue variable representing the error of RooRealVar input
43
44RooErrorVar::RooErrorVar(const char *name, const char *title, const RooRealVar& input) :
46 _realVar("realVar","RooRealVar with error",this,(RooAbsReal&)input)
47{
48 _binning = std::make_unique<RooUniformBinning>(-1,1,100) ;
49}
50
51
52
53////////////////////////////////////////////////////////////////////////////////
54
55RooErrorVar::RooErrorVar(const RooErrorVar& other, const char* name) :
57 _realVar("realVar",this,other._realVar)
58{
59 _binning = std::unique_ptr<RooAbsBinning>{other._binning->clone()};
60
61 // Copy constructor
62 for(auto * binning : static_range_cast<RooAbsBinning*>(other._altBinning)) _altBinning.Add(binning->clone());
63}
64
66
67////////////////////////////////////////////////////////////////////////////////
68/// Return value, i.e. error on input variable
69
70double RooErrorVar::getValV(const RooArgSet*) const
71{
72 return evaluate();
73}
74
75
76
77////////////////////////////////////////////////////////////////////////////////
78/// Return true if we have binning with given name
79
80bool RooErrorVar::hasBinning(const char* name) const
81{
82 return _altBinning.FindObject(name) ? true : false ;
83}
84
85
86////////////////////////////////////////////////////////////////////////////////
87/// Return binning with given name. If no binning exists with such a name, clone the default
88/// binning on the fly if so requested
89
90const RooAbsBinning& RooErrorVar::getBinning(const char* name, bool verbose, bool createOnTheFly) const
91{
92 return const_cast<RooErrorVar*>(this)->getBinning(name,verbose,createOnTheFly) ;
93}
94
95
96
97////////////////////////////////////////////////////////////////////////////////
98/// Return binning with given name. If no binning exists with such a name, clone the default
99/// binning on the fly if so requested
100
101RooAbsBinning& RooErrorVar::getBinning(const char* name, bool /*verbose*/, bool createOnTheFly)
102{
103 // Return default (normalization) binning and range if no name is specified
104 if (name==nullptr) {
105 return *_binning ;
106 }
107
108 // Check if binning with this name has been created already
109 RooAbsBinning* binning = static_cast<RooAbsBinning*>(_altBinning.FindObject(name)) ;
110 if (binning) {
111 return *binning ;
112 }
113
114 // Return default binning if binning is not found and no creation is requested
115 if (!createOnTheFly) {
116 return *_binning ;
117 }
118
119 // Create a new RooRangeBinning with this name with default range
120 binning = new RooRangeBinning(getMin(),getMax(),name) ;
121 coutI(Contents) << "RooErrorVar::getBinning(" << GetName() << ") new range named '"
122 << name << "' created with default bounds" << endl ;
123
124 _altBinning.Add(binning) ;
125
126 return *binning ;
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Get a list of all binning names. An empty name implies the default binning.
131/// A 0 pointer should be passed to getBinning in this case.
132
133std::list<std::string> RooErrorVar::getBinningNames() const
134{
135 std::list<std::string> binningNames(1, "");
136
137 for(auto * binning : _altBinning) {
138 const char* name = binning->GetName();
139 binningNames.push_back(name);
140 }
141 return binningNames;
142}
143
144
145/// Remove lower bound from named binning, or default binning if name is null
146void RooErrorVar::removeMin(const char* name) {
148}
149
150
151/// Remove upper bound from named binning, or default binning if name is null
152void RooErrorVar::removeMax(const char* name) {
154}
155
156
157/// Remove both upper and lower bounds from named binning, or
158/// default binning if name is null
161}
162
163
164////////////////////////////////////////////////////////////////////////////////
165/// Store given binning with this variable under the given name
166
167void RooErrorVar::setBinning(const RooAbsBinning& binning, const char* name)
168{
169 if (!name) {
170 _binning = std::unique_ptr<RooAbsBinning>{binning.clone()};
171 } else {
172
173 // Remove any old binning with this name
174 if (std::unique_ptr<RooAbsBinning> oldBinning{static_cast<RooAbsBinning*>(_altBinning.FindObject(name))}) {
175 _altBinning.Remove(oldBinning.get()) ;
176 }
177
178 // Insert new binning in list of alternative binnings
179 RooAbsBinning* newBinning = binning.clone() ;
180 newBinning->SetName(name) ;
181 newBinning->SetTitle(name) ;
182 _altBinning.Add(newBinning) ;
183
184 }
185
186
187}
188
189
190
191////////////////////////////////////////////////////////////////////////////////
192/// Set the lower bound of the range with the given name to the given value
193/// If name is a null pointer, set the lower bound of the default range
194
195void RooErrorVar::setMin(const char* name, double value)
196{
197 // Set new minimum of fit range
198 RooAbsBinning& binning = getBinning(name) ;
199
200 // Check if new limit is consistent
201 if (value >= getMax()) {
202 coutW(InputArguments) << "RooErrorVar::setMin(" << GetName()
203 << "): Proposed new fit min. larger than max., setting min. to max." << endl ;
204 binning.setMin(getMax()) ;
205 } else {
206 binning.setMin(value) ;
207 }
208
209 // Clip current value in window if it fell out
210 if (!name) {
211 double clipValue ;
212 if (!inRange(_value,nullptr,&clipValue)) {
213 setVal(clipValue) ;
214 }
215 }
216
217 setShapeDirty() ;
218}
219
220
221////////////////////////////////////////////////////////////////////////////////
222/// Set the upper bound of the range with the given name to the given value
223/// If name is a null pointer, set the upper bound of the default range
224
225void RooErrorVar::setMax(const char* name, double value)
226{
227 // Set new maximum of fit range
228 RooAbsBinning& binning = getBinning(name) ;
229
230 // Check if new limit is consistent
231 if (value < getMin()) {
232 coutW(InputArguments) << "RooErrorVar::setMax(" << GetName()
233 << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
234 binning.setMax(getMin()) ;
235 } else {
236 binning.setMax(value) ;
237 }
238
239 // Clip current value in window if it fell out
240 if (!name) {
241 double clipValue ;
242 if (!inRange(_value,nullptr,&clipValue)) {
243 setVal(clipValue) ;
244 }
245 }
246
247 setShapeDirty() ;
248}
249
250/// Set default binning to nBins uniform bins
253}
254
255////////////////////////////////////////////////////////////////////////////////
256/// Set the upper and lower lower bound of the range with the given name to the given values
257/// If name is a null pointer, set the upper and lower bounds of the default range
258
259void RooErrorVar::setRange( const char* name, double min, double max)
260{
261 bool exists = name ? (_altBinning.FindObject(name)?true:false) : true ;
262
263 // Set new fit range
264 RooAbsBinning& binning = getBinning(name,false) ;
265
266 // Check if new limit is consistent
267 if (min>max) {
268 coutW(InputArguments) << "RooErrorVar::setRange(" << GetName()
269 << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
270 binning.setRange(min,min) ;
271 } else {
272 binning.setRange(min,max) ;
273 }
274
275 if (!exists) {
276 coutI(InputArguments) << "RooErrorVar::setRange(" << GetName()
277 << ") new range named '" << name << "' created with bounds ["
278 << min << "," << max << "]" << endl ;
279 }
280
281 setShapeDirty() ;
282}
283
284
285
286////////////////////////////////////////////////////////////////////////////////
287/// Read object contents from given stream
288
289bool RooErrorVar::readFromStream(istream& is, bool /*compact*/, bool verbose)
290{
291 TString token;
292 TString errorPrefix("RooErrorVar::readFromStream(");
293 errorPrefix.Append(GetName()) ;
294 errorPrefix.Append(")") ;
295 RooStreamParser parser(is,errorPrefix) ;
296 double value(0) ;
297
298 // Compact mode: Read single token
299 if (parser.readDouble(value,verbose)) return true ;
300 if (isValidReal(value,verbose)) {
301 setVal(value) ;
302 return false ;
303 } else {
304 return true ;
305 }
306}
307
308
309
310////////////////////////////////////////////////////////////////////////////////
311/// Write value to stream
312
313void RooErrorVar::writeToStream(ostream& os, bool /*compact*/) const
314{
315 os << getVal() ;
316}
317
318
319////////////////////////////////////////////////////////////////////////////////
320/// Force the internal value cache to be up to date
321
323{
324 _value = evaluate() ;
325}
326
327
328
#define coutI(a)
#define coutW(a)
#define ClassImp(name)
Definition Rtypes.h:382
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:110
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition RooAbsArg.h:467
Abstract base class for RooRealVar binning definitions.
virtual void setRange(double xlo, double xhi)=0
virtual void setMin(double xlo)
Change lower bound to xlo.
virtual void setMax(double xhi)
Change upper bound to xhi.
virtual RooAbsBinning * clone(const char *name=nullptr) const =0
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
bool isValidReal(double value, bool printError=false) const override
Check if given value is valid.
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
bool inRange(const char *name) const override
Check if current value is inside range with given name.
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
double _value
Cache for current value of object.
Definition RooAbsReal.h:536
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Auxiliary class that represents the error of a RooRealVar as a separate object.
Definition RooErrorVar.h:28
bool hasBinning(const char *name) const override
Return true if we have binning with given name.
void removeMin(const char *name=nullptr)
Remove lower bound from named binning, or default binning if name is null.
void setBinning(const RooAbsBinning &binning, const char *name=nullptr)
Store given binning with this variable under the given name.
~RooErrorVar() override
void removeRange(const char *name=nullptr)
Remove both upper and lower bounds from named binning, or default binning if name is null.
void setMax(double value)
Set upper bound of default range to value.
Definition RooErrorVar.h:66
std::list< std::string > getBinningNames() const override
Get a list of all binning names.
RooLinkedList _altBinning
! Optional alternative ranges and binnings
Definition RooErrorVar.h:94
void setRange(double min, double max)
Set default ranges to [min,max].
Definition RooErrorVar.h:70
void setVal(double value) override
Set the current value of the object. Needs to be overridden by implementations.
Definition RooErrorVar.h:46
double getValV(const RooArgSet *set=nullptr) const override
Return value, i.e. error on input variable.
std::unique_ptr< RooAbsBinning > _binning
! Pointer to default binning definition
Definition RooErrorVar.h:99
RooErrorVar()
Default constructor.
Definition RooErrorVar.h:32
void setMin(double value)
Set lower bound of default range to value.
Definition RooErrorVar.h:62
void syncCache(const RooArgSet *set=nullptr) override
Force the internal value cache to be up to date.
void writeToStream(std::ostream &os, bool compact) const override
Write value to stream.
void removeMax(const char *name=nullptr)
Remove upper bound from named binning, or default binning if name is null.
const RooAbsBinning & getBinning(const char *name=nullptr, bool verbose=true, bool createOnTheFly=false) const override
Return binning with given name.
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
Definition RooErrorVar.h:41
bool readFromStream(std::istream &is, bool compact, bool verbose=false) override
Read object contents from given stream.
void setBins(Int_t nBins)
Set default binning to nBins uniform bins.
virtual void Add(TObject *arg)
TObject * FindObject(const char *name) const override
Return pointer to object with given name.
virtual bool Remove(TObject *arg)
Remove object from collection.
static constexpr double infinity()
Return internal infinity representation.
Definition RooNumber.h:25
Binning/range definition that only defines a range but no binning.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
bool readDouble(double &value, bool zapOnError=false)
Read the next token and convert it to a double.
Implementation of RooAbsBinning that provides a uniform binning in 'n' bins between the range end poi...
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
Basic string class.
Definition TString.h:139
TString & Append(const char *cs)
Definition TString.h:572