Logo ROOT  
Reference Guide
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 
22 RooErrorVar is an auxilary class that represents the error
23 of a RooRealVar as a seperate object. The main reason of
24 existence of this class is to facilitate the reuse of existing
25 techniques to perform calculations that involve a RooRealVars
26 error, 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 
36 using namespace std;
37 
39 ;
40 
41 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Construct an lvalue variable representing the error of RooRealVar input
45 
46 RooErrorVar::RooErrorVar(const char *name, const char *title, const RooRealVar& input) :
47  RooAbsRealLValue(name,title),
48  _realVar("realVar","RooRealVar with error",this,(RooAbsReal&)input)
49 {
50  _binning = new RooUniformBinning(-1,1,100) ;
51 }
52 
53 
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 RooErrorVar::RooErrorVar(const RooErrorVar& other, const char* name) :
58  RooAbsRealLValue(other,name),
59  _realVar("realVar",this,other._realVar)
60 {
61  _binning = other._binning->clone() ;
62 
63  // Copy constructor
64 
65  TIterator* iter = other._altBinning.MakeIterator() ;
66  RooAbsBinning* binning ;
67  while((binning=(RooAbsBinning*)iter->Next())) {
68  _altBinning.Add(binning->clone()) ;
69  }
70  delete iter ;
71 }
72 
73 
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Destructor
77 
79 {
80  delete _binning ;
81 }
82 
83 
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Return value, i.e. error on input variable
87 
89 {
90  return evaluate();
91 }
92 
93 
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Return true if we have binning with given name
97 
99 {
100  return _altBinning.FindObject(name) ? kTRUE : kFALSE ;
101 }
102 
103 
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Return binning with given name. If no binning exists with such a name, clone the default
107 /// binning on the fly if so requested
108 
109 const RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t verbose, Bool_t createOnTheFly) const
110 {
111  return const_cast<RooErrorVar*>(this)->getBinning(name,verbose,createOnTheFly) ;
112 }
113 
114 
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// Return binning with given name. If no binning exists with such a name, clone the default
118 /// binning on the fly if so requested
119 
120 RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t /*verbose*/, Bool_t createOnTheFly)
121 {
122  // Return default (normalization) binning and range if no name is specified
123  if (name==0) {
124  return *_binning ;
125  }
126 
127  // Check if binning with this name has been created already
129  if (binning) {
130  return *binning ;
131  }
132 
133  // Return default binning if binning is not found and no creation is requested
134  if (!createOnTheFly) {
135  return *_binning ;
136  }
137 
138  // Create a new RooRangeBinning with this name with default range
139  binning = new RooRangeBinning(getMin(),getMax(),name) ;
140  coutI(Contents) << "RooErrorVar::getBinning(" << GetName() << ") new range named '"
141  << name << "' created with default bounds" << endl ;
142 
143  _altBinning.Add(binning) ;
144 
145  return *binning ;
146 }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Get a list of all binning names. An empty name implies the default binning.
150 /// A 0 pointer should be passed to getBinning in this case.
151 
152 std::list<std::string> RooErrorVar::getBinningNames() const
153 {
154  std::list<std::string> binningNames(1, "");
155 
157  const RooAbsArg* binning = 0;
158  while((binning = iter.next())) {
159  const char* name = binning->GetName();
160  binningNames.push_back(name);
161  }
162  return binningNames;
163 }
164 
165 
166 /// Remove lower bound from named binning, or default binning if name is null
167 void RooErrorVar::removeMin(const char* name) {
169 }
170 
171 
172 /// Remove upper bound from named binning, or default binning if name is null
173 void RooErrorVar::removeMax(const char* name) {
175 }
176 
177 
178 /// Remove both upper and lower bounds from named binning, or
179 /// default binning if name is null
180 void RooErrorVar::removeRange(const char* name) {
182 }
183 
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 /// Store given binning with this variable under the given name
187 
188 void RooErrorVar::setBinning(const RooAbsBinning& binning, const char* name)
189 {
190  if (!name) {
191  if (_binning) delete _binning ;
192  _binning = binning.clone() ;
193  } else {
194 
195  // Remove any old binning with this name
197  if (oldBinning) {
198  _altBinning.Remove(oldBinning) ;
199  delete oldBinning ;
200  }
201 
202  // Insert new binning in list of alternative binnings
203  RooAbsBinning* newBinning = binning.clone() ;
204  newBinning->SetName(name) ;
205  newBinning->SetTitle(name) ;
206  _altBinning.Add(newBinning) ;
207 
208  }
209 
210 
211 }
212 
213 
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// Set the lower bound of the range with the given name to the given value
217 /// If name is a null pointer, set the lower bound of the default range
218 
219 void RooErrorVar::setMin(const char* name, Double_t value)
220 {
221  // Set new minimum of fit range
222  RooAbsBinning& binning = getBinning(name) ;
223 
224  // Check if new limit is consistent
225  if (value >= getMax()) {
226  coutW(InputArguments) << "RooErrorVar::setMin(" << GetName()
227  << "): Proposed new fit min. larger than max., setting min. to max." << endl ;
228  binning.setMin(getMax()) ;
229  } else {
230  binning.setMin(value) ;
231  }
232 
233  // Clip current value in window if it fell out
234  if (!name) {
235  Double_t clipValue ;
236  if (!inRange(_value,0,&clipValue)) {
237  setVal(clipValue) ;
238  }
239  }
240 
241  setShapeDirty() ;
242 }
243 
244 
245 ////////////////////////////////////////////////////////////////////////////////
246 /// Set the upper bound of the range with the given name to the given value
247 /// If name is a null pointer, set the upper bound of the default range
248 
249 void RooErrorVar::setMax(const char* name, Double_t value)
250 {
251  // Set new maximum of fit range
252  RooAbsBinning& binning = getBinning(name) ;
253 
254  // Check if new limit is consistent
255  if (value < getMin()) {
256  coutW(InputArguments) << "RooErrorVar::setMax(" << GetName()
257  << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
258  binning.setMax(getMin()) ;
259  } else {
260  binning.setMax(value) ;
261  }
262 
263  // Clip current value in window if it fell out
264  if (!name) {
265  Double_t clipValue ;
266  if (!inRange(_value,0,&clipValue)) {
267  setVal(clipValue) ;
268  }
269  }
270 
271  setShapeDirty() ;
272 }
273 
274 /// Set default binning to nBins uniform bins
277 }
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 /// Set the upper and lower lower bound of the range with the given name to the given values
281 /// If name is a null pointer, set the upper and lower bounds of the default range
282 
283 void RooErrorVar::setRange( const char* name, Double_t min, Double_t max)
284 {
286 
287  // Set new fit range
288  RooAbsBinning& binning = getBinning(name,kFALSE) ;
289 
290  // Check if new limit is consistent
291  if (min>max) {
292  coutW(InputArguments) << "RooErrorVar::setRange(" << GetName()
293  << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
294  binning.setRange(min,min) ;
295  } else {
296  binning.setRange(min,max) ;
297  }
298 
299  if (!exists) {
300  coutI(InputArguments) << "RooErrorVar::setRange(" << GetName()
301  << ") new range named '" << name << "' created with bounds ["
302  << min << "," << max << "]" << endl ;
303  }
304 
305  setShapeDirty() ;
306 }
307 
308 
309 
310 ////////////////////////////////////////////////////////////////////////////////
311 /// Read object contents from given stream
312 
314 {
315  TString token,errorPrefix("RooErrorVar::readFromStream(") ;
316  errorPrefix.Append(GetName()) ;
317  errorPrefix.Append(")") ;
318  RooStreamParser parser(is,errorPrefix) ;
319  Double_t value(0) ;
320 
321  // Compact mode: Read single token
322  if (parser.readDouble(value,verbose)) return kTRUE ;
323  if (isValidReal(value,verbose)) {
324  setVal(value) ;
325  return kFALSE ;
326  } else {
327  return kTRUE ;
328  }
329 }
330 
331 
332 
333 ////////////////////////////////////////////////////////////////////////////////
334 /// Write value to stream
335 
336 void RooErrorVar::writeToStream(ostream& os, Bool_t /*compact*/) const
337 {
338  os << getVal() ;
339 }
340 
341 
342 ////////////////////////////////////////////////////////////////////////////////
343 /// Force the internal value cache to be up to date
344 
346 {
347  _value = evaluate() ;
348 }
349 
350 
351 
RooStreamParser.h
RooLinkedList::MakeIterator
TIterator * MakeIterator(Bool_t forward=kTRUE) const
Create a TIterator for this list.
Definition: RooLinkedList.cxx:747
RooAbsRealLValue::inRange
virtual Bool_t inRange(const char *name) const
Check if current value is inside range with given name.
Definition: RooAbsRealLValue.cxx:504
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooMsgService.h
RooUniformBinning.h
TNamed::SetName
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
RooErrorVar::setMin
void setMin(Double_t value)
Definition: RooErrorVar.h:61
RooAbsRealLValue::getMax
virtual Double_t getMax(const char *name=0) const
Get maximum of currently defined range.
Definition: RooAbsRealLValue.h:83
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
RooErrorVar::getValV
virtual Double_t getValV(const RooArgSet *set=0) const
Return value, i.e. error on input variable.
Definition: RooErrorVar.cxx:88
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooErrorVar::hasBinning
Bool_t hasBinning(const char *name) const
Return true if we have binning with given name.
Definition: RooErrorVar.cxx:98
coutW
#define coutW(a)
Definition: RooMsgService.h:32
RooErrorVar::setBins
void setBins(Int_t nBins)
Set default binning to nBins uniform bins.
Definition: RooErrorVar.cxx:275
RooErrorVar::removeRange
void removeRange(const char *name=0)
Remove both upper and lower bounds from named binning, or default binning if name is null.
Definition: RooErrorVar.cxx:180
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooRangeBinning.h
RooErrorVar::~RooErrorVar
virtual ~RooErrorVar()
Destructor.
Definition: RooErrorVar.cxx:78
RooErrorVar::removeMin
void removeMin(const char *name=0)
Remove lower bound from named binning, or default binning if name is null.
Definition: RooErrorVar.cxx:167
coutI
#define coutI(a)
Definition: RooMsgService.h:30
RooAbsBinning::setMax
virtual void setMax(Double_t xhi)
Definition: RooAbsBinning.h:55
RooAbsReal
Definition: RooAbsReal.h:61
RooErrorVar
Definition: RooErrorVar.h:28
RooLinkedList::fwdIterator
RooFIter fwdIterator() const
Create a one-time-use forward iterator for this list.
Definition: RooLinkedList.cxx:766
TString
Definition: TString.h:136
RooLinkedList::FindObject
TObject * FindObject(const char *name) const
Return pointer to obejct with given name.
Definition: RooLinkedList.cxx:539
RooErrorVar::evaluate
virtual Double_t evaluate() const
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
Definition: RooErrorVar.h:41
RooErrorVar::setVal
virtual void setVal(Double_t value)
Definition: RooErrorVar.h:46
bool
RooErrorVar::_altBinning
RooLinkedList _altBinning
Definition: RooErrorVar.h:94
TIterator
Definition: TIterator.h:30
RooAbsRealLValue::getMin
virtual Double_t getMin(const char *name=0) const
Get miniminum of currently defined range.
Definition: RooAbsRealLValue.h:80
RooErrorVar::setBinning
void setBinning(const RooAbsBinning &binning, const char *name=0)
Store given binning with this variable under the given name.
Definition: RooErrorVar.cxx:188
RooStreamParser
Definition: RooStreamParser.h:21
RooErrorVar::_binning
RooAbsBinning * _binning
Definition: RooErrorVar.h:99
RooAbsBinning.h
RooErrorVar::setMax
void setMax(Double_t value)
Definition: RooErrorVar.h:65
RooFIter
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
Definition: RooLinkedListIter.h:39
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
RooStreamParser::readDouble
Bool_t readDouble(Double_t &value, Bool_t zapOnError=kFALSE)
Read the next token and convert it to a Double_t.
Definition: RooStreamParser.cxx:416
RooAbsBinning
Definition: RooAbsBinning.h:26
RooUniformBinning
Definition: RooUniformBinning.h:23
RooFIter::next
RooAbsArg * next()
Return next element or nullptr if at end.
Definition: RooLinkedListIter.h:49
RooErrorVar::removeMax
void removeMax(const char *name=0)
Remove upper bound from named binning, or default binning if name is null.
Definition: RooErrorVar.cxx:173
RooLinkedList::Add
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:62
RooAbsReal::_value
Double_t _value
Definition: RooAbsReal.h:450
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
TIterator::Next
virtual TObject * Next()=0
RooErrorVar::writeToStream
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write value to stream.
Definition: RooErrorVar.cxx:336
RooNumber::infinity
static Double_t infinity()
Return internal infinity representation.
Definition: RooNumber.cxx:49
Double_t
double Double_t
Definition: RtypesCore.h:59
RooAbsArg::setShapeDirty
void setShapeDirty()
Notify that a shape-like property (e.g. binning) has changed.
Definition: RooAbsArg.h:510
RooErrorVar::getBinning
const RooAbsBinning & getBinning(const char *name=0, Bool_t verbose=kTRUE, Bool_t createOnTheFly=kFALSE) const
Return binning with given name.
Definition: RooErrorVar.cxx:109
RooAbsBinning::setMin
virtual void setMin(Double_t xlo)
Definition: RooAbsBinning.h:51
RooErrorVar::RooErrorVar
RooErrorVar()
Definition: RooErrorVar.h:31
RooAbsBinning::clone
virtual RooAbsBinning * clone(const char *name=0) const =0
RooErrorVar::setRange
void setRange(Double_t min, Double_t max)
Definition: RooErrorVar.h:69
RooRangeBinning
Definition: RooRangeBinning.h:21
name
char name[80]
Definition: TGX11.cxx:110
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooAbsBinning::setRange
virtual void setRange(Double_t xlo, Double_t xhi)=0
RooAbsArg
Definition: RooAbsArg.h:73
RooErrorVar.h
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
RooAbsRealLValue::isValidReal
virtual Bool_t isValidReal(Double_t value, Bool_t printError=kFALSE) const
Check if given value is valid.
Definition: RooAbsRealLValue.cxx:124
RooRealVar
Definition: RooRealVar.h:35
RooErrorVar::readFromStream
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
Definition: RooErrorVar.cxx:313
RooErrorVar::getBinningNames
std::list< std::string > getBinningNames() const
Get a list of all binning names.
Definition: RooErrorVar.cxx:152
RooFit::Contents
@ Contents
Definition: RooGlobalFunc.h:69
RooAbsRealLValue
Definition: RooAbsRealLValue.h:31
RooArgSet
Definition: RooArgSet.h:28
RooLinkedList::Remove
virtual Bool_t Remove(TObject *arg)
Remove object from collection.
Definition: RooLinkedList.cxx:455
int
RooErrorVar::syncCache
void syncCache(const RooArgSet *set=0)
Optional alternative ranges and binnings.
Definition: RooErrorVar.cxx:345