RooErrorVar.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooErrorVar.cxx 24269 2008-06-13 15:37:03Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 // 
00019 // BEGIN_HTML
00020 // RooErrorVar is an auxilary class that represents the error
00021 // of a RooRealVar as a seperate object. The main reason of
00022 // existence of this class is to facilitate the reuse of existing
00023 // techniques to perform calculations that involve a RooRealVars
00024 // error, such as calculating the pull value.
00025 // END_HTML
00026 //
00027 //
00028 
00029 #include "RooFit.h"
00030 #include "Riostream.h"
00031 
00032 #include "RooErrorVar.h"
00033 #include "RooErrorVar.h"
00034 #include "RooAbsBinning.h"
00035 #include "RooStreamParser.h"
00036 #include "RooRangeBinning.h"
00037 #include "RooMsgService.h"
00038 
00039 
00040 
00041 ClassImp(RooErrorVar)
00042 ;
00043 
00044 
00045 
00046 //_____________________________________________________________________________
00047 RooErrorVar::RooErrorVar(const char *name, const char *title, const RooRealVar& input) :
00048   RooAbsRealLValue(name,title),
00049   _realVar("realVar","RooRealVar with error",this,(RooAbsReal&)input)
00050 {
00051   // Construct an lvalue variable representing the error of RooRealVar input
00052 
00053   _binning = new RooUniformBinning(-1,1,100) ;
00054 }
00055 
00056 
00057 
00058 //_____________________________________________________________________________
00059 RooErrorVar::RooErrorVar(const RooErrorVar& other, const char* name) :
00060   RooAbsRealLValue(other,name),
00061   _realVar("realVar",this,other._realVar)
00062 {
00063   _binning = other._binning->clone() ;
00064 
00065   // Copy constructor
00066 
00067   TIterator* iter = other._altBinning.MakeIterator() ;
00068   RooAbsBinning* binning ;
00069   while((binning=(RooAbsBinning*)iter->Next())) {
00070     _altBinning.Add(binning->clone()) ;
00071   }
00072   delete iter ;
00073 }
00074 
00075 
00076 
00077 //_____________________________________________________________________________
00078 RooErrorVar::~RooErrorVar()
00079 {
00080   // Destructor 
00081 
00082   delete _binning ;
00083 }
00084 
00085 
00086 
00087 //_____________________________________________________________________________
00088 Double_t RooErrorVar::getVal(const RooArgSet*) const 
00089 { 
00090   // Return value, i.e. error on input variable
00091 
00092   return evaluate();
00093 }
00094 
00095 
00096 
00097 //_____________________________________________________________________________
00098 Bool_t RooErrorVar::hasBinning(const char* name) const
00099 {
00100   // Return true if we have binning with given name
00101   
00102   return _altBinning.FindObject(name) ? kTRUE : kFALSE ;
00103 }
00104 
00105 
00106 
00107 //_____________________________________________________________________________
00108 const RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t verbose, Bool_t createOnTheFly) const 
00109 {
00110   // Return binning with given name. If no binning exists with such a name, clone the default
00111   // binning on the fly if so requested
00112 
00113   return const_cast<RooErrorVar*>(this)->getBinning(name,verbose,createOnTheFly) ;
00114 }
00115 
00116 
00117 
00118 //_____________________________________________________________________________
00119 RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t /*verbose*/, Bool_t createOnTheFly) 
00120 {
00121   // Return binning with given name. If no binning exists with such a name, clone the default
00122   // binning on the fly if so requested
00123   
00124   // Return default (normalization) binning and range if no name is specified
00125   if (name==0) {
00126     return *_binning ;
00127   }
00128   
00129   // Check if binning with this name has been created already
00130   RooAbsBinning* binning = (RooAbsBinning*) _altBinning.FindObject(name) ;
00131   if (binning) {
00132     return *binning ;
00133   }
00134 
00135   // Return default binning if binning is not found and no creation is requested
00136   if (!createOnTheFly) {
00137     return *_binning ;
00138   }
00139 
00140   // Create a new RooRangeBinning with this name with default range
00141   binning = new RooRangeBinning(getMin(),getMax(),name) ;
00142   coutI(Contents) << "RooErrorVar::getBinning(" << GetName() << ") new range named '" 
00143                   << name << "' created with default bounds" << endl ;
00144 
00145   _altBinning.Add(binning) ;
00146 
00147   return *binning ;
00148 }
00149 
00150 
00151 
00152 //_____________________________________________________________________________
00153 void RooErrorVar::setBinning(const RooAbsBinning& binning, const char* name) 
00154 {
00155   // Store given binning with this variable under the given name
00156 
00157   if (!name) {
00158     if (_binning) delete _binning ;
00159     _binning = binning.clone() ;
00160   } else {
00161 
00162     // Remove any old binning with this name
00163     RooAbsBinning* oldBinning = (RooAbsBinning*) _altBinning.FindObject(name) ;
00164     if (oldBinning) {
00165       _altBinning.Remove(oldBinning) ;
00166       delete oldBinning ;
00167     }
00168 
00169     // Insert new binning in list of alternative binnings
00170     RooAbsBinning* newBinning = binning.clone() ;
00171     newBinning->SetName(name) ;
00172     newBinning->SetTitle(name) ;
00173     _altBinning.Add(newBinning) ;
00174 
00175   }
00176   
00177 
00178 }
00179 
00180 
00181 
00182 //_____________________________________________________________________________
00183 void RooErrorVar::setMin(const char* name, Double_t value) 
00184 {
00185   // Set the lower bound of the range with the given name to the given value
00186   // If name is a null pointer, set the lower bound of the default range
00187 
00188   // Set new minimum of fit range 
00189   RooAbsBinning& binning = getBinning(name) ;
00190 
00191   // Check if new limit is consistent
00192   if (value >= getMax()) {
00193     coutW(InputArguments) << "RooErrorVar::setMin(" << GetName() 
00194                           << "): Proposed new fit min. larger than max., setting min. to max." << endl ;
00195     binning.setMin(getMax()) ;
00196   } else {
00197     binning.setMin(value) ;
00198   }
00199 
00200   // Clip current value in window if it fell out
00201   if (!name) {
00202     Double_t clipValue ;
00203     if (!inRange(_value,0,&clipValue)) {
00204       setVal(clipValue) ;
00205     }
00206   }
00207     
00208   setShapeDirty() ;
00209 }
00210 
00211 
00212 //_____________________________________________________________________________
00213 void RooErrorVar::setMax(const char* name, Double_t value)
00214 {
00215   // Set the upper bound of the range with the given name to the given value
00216   // If name is a null pointer, set the upper bound of the default range
00217 
00218   // Set new maximum of fit range 
00219   RooAbsBinning& binning = getBinning(name) ;
00220 
00221   // Check if new limit is consistent
00222   if (value < getMin()) {
00223     coutW(InputArguments) << "RooErrorVar::setMax(" << GetName() 
00224                           << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
00225     binning.setMax(getMin()) ;
00226   } else {
00227     binning.setMax(value) ;
00228   }
00229 
00230   // Clip current value in window if it fell out
00231   if (!name) {
00232     Double_t clipValue ;
00233     if (!inRange(_value,0,&clipValue)) {
00234       setVal(clipValue) ;
00235     }
00236   }
00237 
00238   setShapeDirty() ;
00239 }
00240 
00241 
00242 
00243 //_____________________________________________________________________________
00244 void RooErrorVar::setRange( const char* name, Double_t min, Double_t max) 
00245 {
00246   // Set the upper and lower lower bound of the range with the given name to the given values
00247   // If name is a null pointer, set the upper and lower bounds of the default range
00248 
00249   Bool_t exists = name ? (_altBinning.FindObject(name)?kTRUE:kFALSE) : kTRUE ;
00250 
00251   // Set new fit range 
00252   RooAbsBinning& binning = getBinning(name,kFALSE) ;
00253 
00254   // Check if new limit is consistent
00255   if (min>max) {
00256     coutW(InputArguments) << "RooErrorVar::setRange(" << GetName() 
00257                           << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
00258     binning.setRange(min,min) ;
00259   } else {
00260     binning.setRange(min,max) ;
00261   }
00262 
00263   if (!exists) {
00264     coutI(InputArguments) << "RooErrorVar::setRange(" << GetName() 
00265                           << ") new range named '" << name << "' created with bounds [" 
00266                           << min << "," << max << "]" << endl ;
00267   }
00268 
00269   setShapeDirty() ;  
00270 }
00271 
00272 
00273 
00274 //_____________________________________________________________________________
00275 Bool_t RooErrorVar::readFromStream(istream& is, Bool_t /*compact*/, Bool_t verbose) 
00276 {
00277   // Read object contents from given stream
00278 
00279   TString token,errorPrefix("RooErrorVar::readFromStream(") ;
00280   errorPrefix.Append(GetName()) ;
00281   errorPrefix.Append(")") ;
00282   RooStreamParser parser(is,errorPrefix) ;
00283   Double_t value(0) ;
00284 
00285     // Compact mode: Read single token
00286   if (parser.readDouble(value,verbose)) return kTRUE ;
00287   if (isValidReal(value,verbose)) {
00288     setVal(value) ;
00289     return kFALSE ;
00290   } else {
00291     return kTRUE ;
00292   }
00293 }
00294 
00295 
00296 
00297 //_____________________________________________________________________________
00298 void RooErrorVar::writeToStream(ostream& os, Bool_t /*compact*/) const
00299 {
00300   // Write value to stream
00301 
00302   os << getVal() ;
00303 }
00304 
00305 
00306 //_____________________________________________________________________________
00307 void RooErrorVar::syncCache(const RooArgSet*) 
00308 { 
00309   // Force the internal value cache to be up to date
00310 
00311   _value = evaluate() ; 
00312 }

Generated on Tue Jul 5 15:06:29 2011 for ROOT_528-00b_version by  doxygen 1.5.1