// RooErrorVar is an auxilary class that represents the error
// of a RooRealVar as a seperate object. The main reason of
// existence of this class is to facilitate the reuse of existing
// techniques to perform calculations that involve a RooRealVars
// error, such as calculating the pull value.
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include "RooErrorVar.h"
#include "RooErrorVar.h"
#include "RooAbsBinning.h"
#include "RooStreamParser.h"
#include "RooRangeBinning.h"
#include "RooMsgService.h"
using namespace std;
ClassImp(RooErrorVar)
;
RooErrorVar::RooErrorVar(const char *name, const char *title, const RooRealVar& input) :
RooAbsRealLValue(name,title),
_realVar("realVar","RooRealVar with error",this,(RooAbsReal&)input)
{
_binning = new RooUniformBinning(-1,1,100) ;
}
RooErrorVar::RooErrorVar(const RooErrorVar& other, const char* name) :
RooAbsRealLValue(other,name),
_realVar("realVar",this,other._realVar)
{
_binning = other._binning->clone() ;
TIterator* iter = other._altBinning.MakeIterator() ;
RooAbsBinning* binning ;
while((binning=(RooAbsBinning*)iter->Next())) {
_altBinning.Add(binning->clone()) ;
}
delete iter ;
}
RooErrorVar::~RooErrorVar()
{
delete _binning ;
}
Double_t RooErrorVar::getValV(const RooArgSet*) const
{
return evaluate();
}
Bool_t RooErrorVar::hasBinning(const char* name) const
{
return _altBinning.FindObject(name) ? kTRUE : kFALSE ;
}
const RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t verbose, Bool_t createOnTheFly) const
{
return const_cast<RooErrorVar*>(this)->getBinning(name,verbose,createOnTheFly) ;
}
RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t , Bool_t createOnTheFly)
{
if (name==0) {
return *_binning ;
}
RooAbsBinning* binning = (RooAbsBinning*) _altBinning.FindObject(name) ;
if (binning) {
return *binning ;
}
if (!createOnTheFly) {
return *_binning ;
}
binning = new RooRangeBinning(getMin(),getMax(),name) ;
coutI(Contents) << "RooErrorVar::getBinning(" << GetName() << ") new range named '"
<< name << "' created with default bounds" << endl ;
_altBinning.Add(binning) ;
return *binning ;
}
std::list<std::string> RooErrorVar::getBinningNames() const
{
std::list<std::string> binningNames(1, "");
RooFIter iter = _altBinning.fwdIterator();
const RooAbsArg* binning = 0;
while((binning = iter.next())) {
const char* name = binning->GetName();
binningNames.push_back(name);
}
return binningNames;
}
void RooErrorVar::setBinning(const RooAbsBinning& binning, const char* name)
{
if (!name) {
if (_binning) delete _binning ;
_binning = binning.clone() ;
} else {
RooAbsBinning* oldBinning = (RooAbsBinning*) _altBinning.FindObject(name) ;
if (oldBinning) {
_altBinning.Remove(oldBinning) ;
delete oldBinning ;
}
RooAbsBinning* newBinning = binning.clone() ;
newBinning->SetName(name) ;
newBinning->SetTitle(name) ;
_altBinning.Add(newBinning) ;
}
}
void RooErrorVar::setMin(const char* name, Double_t value)
{
RooAbsBinning& binning = getBinning(name) ;
if (value >= getMax()) {
coutW(InputArguments) << "RooErrorVar::setMin(" << GetName()
<< "): Proposed new fit min. larger than max., setting min. to max." << endl ;
binning.setMin(getMax()) ;
} else {
binning.setMin(value) ;
}
if (!name) {
Double_t clipValue ;
if (!inRange(_value,0,&clipValue)) {
setVal(clipValue) ;
}
}
setShapeDirty() ;
}
void RooErrorVar::setMax(const char* name, Double_t value)
{
RooAbsBinning& binning = getBinning(name) ;
if (value < getMin()) {
coutW(InputArguments) << "RooErrorVar::setMax(" << GetName()
<< "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
binning.setMax(getMin()) ;
} else {
binning.setMax(value) ;
}
if (!name) {
Double_t clipValue ;
if (!inRange(_value,0,&clipValue)) {
setVal(clipValue) ;
}
}
setShapeDirty() ;
}
void RooErrorVar::setRange( const char* name, Double_t min, Double_t max)
{
Bool_t exists = name ? (_altBinning.FindObject(name)?kTRUE:kFALSE) : kTRUE ;
RooAbsBinning& binning = getBinning(name,kFALSE) ;
if (min>max) {
coutW(InputArguments) << "RooErrorVar::setRange(" << GetName()
<< "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
binning.setRange(min,min) ;
} else {
binning.setRange(min,max) ;
}
if (!exists) {
coutI(InputArguments) << "RooErrorVar::setRange(" << GetName()
<< ") new range named '" << name << "' created with bounds ["
<< min << "," << max << "]" << endl ;
}
setShapeDirty() ;
}
Bool_t RooErrorVar::readFromStream(istream& is, Bool_t , Bool_t verbose)
{
TString token,errorPrefix("RooErrorVar::readFromStream(") ;
errorPrefix.Append(GetName()) ;
errorPrefix.Append(")") ;
RooStreamParser parser(is,errorPrefix) ;
Double_t value(0) ;
if (parser.readDouble(value,verbose)) return kTRUE ;
if (isValidReal(value,verbose)) {
setVal(value) ;
return kFALSE ;
} else {
return kTRUE ;
}
}
void RooErrorVar::writeToStream(ostream& os, Bool_t ) const
{
os << getVal() ;
}
void RooErrorVar::syncCache(const RooArgSet*)
{
_value = evaluate() ;
}