Logo ROOT   6.14/05
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 "RooFit.h"
30 #include "Riostream.h"
31 
32 #include "RooErrorVar.h"
33 #include "RooErrorVar.h"
34 #include "RooAbsBinning.h"
35 #include "RooStreamParser.h"
36 #include "RooRangeBinning.h"
37 #include "RooMsgService.h"
38 
39 
40 
41 using namespace std;
42 
44 ;
45 
46 
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Construct an lvalue variable representing the error of RooRealVar input
50 
51 RooErrorVar::RooErrorVar(const char *name, const char *title, const RooRealVar& input) :
52  RooAbsRealLValue(name,title),
53  _realVar("realVar","RooRealVar with error",this,(RooAbsReal&)input)
54 {
55  _binning = new RooUniformBinning(-1,1,100) ;
56 }
57 
58 
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 
62 RooErrorVar::RooErrorVar(const RooErrorVar& other, const char* name) :
63  RooAbsRealLValue(other,name),
64  _realVar("realVar",this,other._realVar)
65 {
66  _binning = other._binning->clone() ;
67 
68  // Copy constructor
69 
70  TIterator* iter = other._altBinning.MakeIterator() ;
71  RooAbsBinning* binning ;
72  while((binning=(RooAbsBinning*)iter->Next())) {
73  _altBinning.Add(binning->clone()) ;
74  }
75  delete iter ;
76 }
77 
78 
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Destructor
82 
84 {
85  delete _binning ;
86 }
87 
88 
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Return value, i.e. error on input variable
92 
94 {
95  return evaluate();
96 }
97 
98 
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Return true if we have binning with given name
102 
104 {
105  return _altBinning.FindObject(name) ? kTRUE : kFALSE ;
106 }
107 
108 
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Return binning with given name. If no binning exists with such a name, clone the default
112 /// binning on the fly if so requested
113 
114 const RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t verbose, Bool_t createOnTheFly) const
115 {
116  return const_cast<RooErrorVar*>(this)->getBinning(name,verbose,createOnTheFly) ;
117 }
118 
119 
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Return binning with given name. If no binning exists with such a name, clone the default
123 /// binning on the fly if so requested
124 
125 RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t /*verbose*/, Bool_t createOnTheFly)
126 {
127  // Return default (normalization) binning and range if no name is specified
128  if (name==0) {
129  return *_binning ;
130  }
131 
132  // Check if binning with this name has been created already
133  RooAbsBinning* binning = (RooAbsBinning*) _altBinning.FindObject(name) ;
134  if (binning) {
135  return *binning ;
136  }
137 
138  // Return default binning if binning is not found and no creation is requested
139  if (!createOnTheFly) {
140  return *_binning ;
141  }
142 
143  // Create a new RooRangeBinning with this name with default range
144  binning = new RooRangeBinning(getMin(),getMax(),name) ;
145  coutI(Contents) << "RooErrorVar::getBinning(" << GetName() << ") new range named '"
146  << name << "' created with default bounds" << endl ;
147 
148  _altBinning.Add(binning) ;
149 
150  return *binning ;
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Get a list of all binning names. An empty name implies the default binning.
155 /// A 0 pointer should be passed to getBinning in this case.
156 
157 std::list<std::string> RooErrorVar::getBinningNames() const
158 {
159  std::list<std::string> binningNames(1, "");
160 
162  const RooAbsArg* binning = 0;
163  while((binning = iter.next())) {
164  const char* name = binning->GetName();
165  binningNames.push_back(name);
166  }
167  return binningNames;
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// Store given binning with this variable under the given name
172 
173 void RooErrorVar::setBinning(const RooAbsBinning& binning, const char* name)
174 {
175  if (!name) {
176  if (_binning) delete _binning ;
177  _binning = binning.clone() ;
178  } else {
179 
180  // Remove any old binning with this name
181  RooAbsBinning* oldBinning = (RooAbsBinning*) _altBinning.FindObject(name) ;
182  if (oldBinning) {
183  _altBinning.Remove(oldBinning) ;
184  delete oldBinning ;
185  }
186 
187  // Insert new binning in list of alternative binnings
188  RooAbsBinning* newBinning = binning.clone() ;
189  newBinning->SetName(name) ;
190  newBinning->SetTitle(name) ;
191  _altBinning.Add(newBinning) ;
192 
193  }
194 
195 
196 }
197 
198 
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// Set the lower bound of the range with the given name to the given value
202 /// If name is a null pointer, set the lower bound of the default range
203 
204 void RooErrorVar::setMin(const char* name, Double_t value)
205 {
206  // Set new minimum of fit range
207  RooAbsBinning& binning = getBinning(name) ;
208 
209  // Check if new limit is consistent
210  if (value >= getMax()) {
211  coutW(InputArguments) << "RooErrorVar::setMin(" << GetName()
212  << "): Proposed new fit min. larger than max., setting min. to max." << endl ;
213  binning.setMin(getMax()) ;
214  } else {
215  binning.setMin(value) ;
216  }
217 
218  // Clip current value in window if it fell out
219  if (!name) {
220  Double_t clipValue ;
221  if (!inRange(_value,0,&clipValue)) {
222  setVal(clipValue) ;
223  }
224  }
225 
226  setShapeDirty() ;
227 }
228 
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 /// Set the upper bound of the range with the given name to the given value
232 /// If name is a null pointer, set the upper bound of the default range
233 
234 void RooErrorVar::setMax(const char* name, Double_t value)
235 {
236  // Set new maximum of fit range
237  RooAbsBinning& binning = getBinning(name) ;
238 
239  // Check if new limit is consistent
240  if (value < getMin()) {
241  coutW(InputArguments) << "RooErrorVar::setMax(" << GetName()
242  << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
243  binning.setMax(getMin()) ;
244  } else {
245  binning.setMax(value) ;
246  }
247 
248  // Clip current value in window if it fell out
249  if (!name) {
250  Double_t clipValue ;
251  if (!inRange(_value,0,&clipValue)) {
252  setVal(clipValue) ;
253  }
254  }
255 
256  setShapeDirty() ;
257 }
258 
259 
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// Set the upper and lower lower bound of the range with the given name to the given values
263 /// If name is a null pointer, set the upper and lower bounds of the default range
264 
265 void RooErrorVar::setRange( const char* name, Double_t min, Double_t max)
266 {
267  Bool_t exists = name ? (_altBinning.FindObject(name)?kTRUE:kFALSE) : kTRUE ;
268 
269  // Set new fit range
270  RooAbsBinning& binning = getBinning(name,kFALSE) ;
271 
272  // Check if new limit is consistent
273  if (min>max) {
274  coutW(InputArguments) << "RooErrorVar::setRange(" << GetName()
275  << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
276  binning.setRange(min,min) ;
277  } else {
278  binning.setRange(min,max) ;
279  }
280 
281  if (!exists) {
282  coutI(InputArguments) << "RooErrorVar::setRange(" << GetName()
283  << ") new range named '" << name << "' created with bounds ["
284  << min << "," << max << "]" << endl ;
285  }
286 
287  setShapeDirty() ;
288 }
289 
290 
291 
292 ////////////////////////////////////////////////////////////////////////////////
293 /// Read object contents from given stream
294 
295 Bool_t RooErrorVar::readFromStream(istream& is, Bool_t /*compact*/, Bool_t verbose)
296 {
297  TString token,errorPrefix("RooErrorVar::readFromStream(") ;
298  errorPrefix.Append(GetName()) ;
299  errorPrefix.Append(")") ;
300  RooStreamParser parser(is,errorPrefix) ;
301  Double_t value(0) ;
302 
303  // Compact mode: Read single token
304  if (parser.readDouble(value,verbose)) return kTRUE ;
305  if (isValidReal(value,verbose)) {
306  setVal(value) ;
307  return kFALSE ;
308  } else {
309  return kTRUE ;
310  }
311 }
312 
313 
314 
315 ////////////////////////////////////////////////////////////////////////////////
316 /// Write value to stream
317 
318 void RooErrorVar::writeToStream(ostream& os, Bool_t /*compact*/) const
319 {
320  os << getVal() ;
321 }
322 
323 
324 ////////////////////////////////////////////////////////////////////////////////
325 /// Force the internal value cache to be up to date
326 
328 {
329  _value = evaluate() ;
330 }
331 
332 
333 
virtual Double_t getMin(const char *name=0) const
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TObject * FindObject(const char *name) const
Return pointer to obejct with given name.
virtual void setMin(Double_t xlo)
Definition: RooAbsBinning.h:51
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
virtual Double_t getMax(const char *name=0) const
virtual Double_t evaluate() const
Definition: RooErrorVar.h:41
virtual Bool_t Remove(TObject *arg)
Remove object from collection.
#define coutI(a)
Definition: RooMsgService.h:31
void setBinning(const RooAbsBinning &binning, const char *name=0)
Store given binning with this variable under the given name.
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
void syncCache(const RooArgSet *set=0)
Optional alternative ranges and binnings.
RooFIter fwdIterator() const
bool Bool_t
Definition: RtypesCore.h:59
STL namespace.
#define coutW(a)
Definition: RooMsgService.h:33
virtual RooAbsBinning * clone(const char *name=0) const =0
Iterator abstract base class.
Definition: TIterator.h:30
Bool_t hasBinning(const char *name) const
Return true if we have binning with given name.
RooAbsBinning * _binning
Definition: RooErrorVar.h:112
RooErrorVar is an auxilary class that represents the error of a RooRealVar as a seperate object...
Definition: RooErrorVar.h:28
RooRealProxy _realVar
Definition: RooErrorVar.h:111
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:62
virtual void setVal(Double_t value)
Definition: RooErrorVar.h:46
virtual ~RooErrorVar()
Destructor.
Definition: RooErrorVar.cxx:83
RooLinkedList _altBinning
Definition: RooErrorVar.h:107
void setRange(Double_t min, Double_t max)
Definition: RooErrorVar.h:69
virtual Bool_t isValidReal(Double_t value, Bool_t printError=kFALSE) const
Check if given value is valid.
Bool_t readDouble(Double_t &value, Bool_t zapOnError=kFALSE)
Read the next token and convert it to a Double_t.
void setMax(Double_t value)
Definition: RooErrorVar.h:65
RooAbsArg * next()
const Bool_t kFALSE
Definition: RtypesCore.h:88
RooAbsBinning is the abstract base class for RooRealVar binning definitions This class defines the in...
Definition: RooAbsBinning.h:26
RooUniformBinning is an implementation of RooAbsBinning that provides a uniform binning in &#39;n&#39; bins b...
#define ClassImp(name)
Definition: Rtypes.h:359
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
RooRangeBinning is binning/range definition that only defines a range but no binning.
std::list< std::string > getBinningNames() const
Get a list of all binning names.
virtual Bool_t inRange(const char *name) const
Check if current value is inside range with given name.
Double_t _value
Definition: RooAbsReal.h:389
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
void setShapeDirty() const
Definition: RooAbsArg.h:442
TIterator * MakeIterator(Bool_t dir=kTRUE) const
Return an iterator over this list.
virtual TObject * Next()=0
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write value to stream.
const Bool_t kTRUE
Definition: RtypesCore.h:87
void setMin(Double_t value)
Definition: RooErrorVar.h:61
char name[80]
Definition: TGX11.cxx:109
virtual void setMax(Double_t xhi)
Definition: RooAbsBinning.h:55
virtual Double_t getValV(const RooArgSet *set=0) const
Return value, i.e. error on input variable.
Definition: RooErrorVar.cxx:93
const RooAbsBinning & getBinning(const char *name=0, Bool_t verbose=kTRUE, Bool_t createOnTheFly=kFALSE) const
Return binning with given name.