// Class RooBinning is an implements RooAbsBinning in terms
// of an array of boundary values, posing no constraints on the choice
// of binning, thus allowing variable bin sizes. Various methods allow
// the user to add single bin boundaries, mirrored pairs, or sets of
// uniformly spaced boundaries.
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include "Riostream.h"
#include "RooBinning.h"
#include "RooDouble.h"
#include "RooAbsPdf.h"
#include "RooRealVar.h"
#include "RooNumber.h"
#include "RooMsgService.h"
#include "TList.h"
ClassImp(RooBinning)
;
RooBinning::RooBinning(Double_t xlo, Double_t xhi, const char* name) :
RooAbsBinning(name),
_xlo(0),
_xhi(0),
_ownBoundLo(kTRUE),
_ownBoundHi(kTRUE),
_array(0)
{
setRange(xlo,xhi) ;
}
RooBinning::RooBinning(Int_t nbins, Double_t xlo, Double_t xhi, const char* name) :
RooAbsBinning(name),
_xlo(0),
_xhi(0),
_ownBoundLo(kTRUE),
_ownBoundHi(kTRUE),
_array(0)
{
setRange(xlo,xhi) ;
addUniform(nbins,xlo,xhi) ;
}
RooBinning::RooBinning(Int_t nbins, const Double_t* boundaries, const char* name) :
RooAbsBinning(name),
_xlo(0),
_xhi(0),
_ownBoundLo(kTRUE),
_ownBoundHi(kTRUE),
_array(0)
{
setRange(boundaries[0],boundaries[nbins]) ;
while(nbins--) addBoundary(boundaries[nbins]) ;
}
RooBinning::RooBinning(const RooBinning& other, const char* name) :
RooAbsBinning(name),
_boundaries(other._boundaries),
_array(0)
{
_xlo = other._xlo ;
_xhi = other._xhi ;
_ownBoundLo = other._ownBoundLo ;
_ownBoundHi = other._ownBoundHi ;
_nbins = other._nbins ;
}
RooBinning::~RooBinning()
{
if (_array) delete[] _array ;
}
Bool_t RooBinning::addBoundary(Double_t boundary)
{
if (_boundaries.find(boundary)!=_boundaries.end()) {
if (boundary==_xlo) _ownBoundLo = kFALSE ;
if (boundary==_xhi) _ownBoundHi = kFALSE ;
return kFALSE ;
}
_boundaries.insert(boundary) ;
updateBinCount() ;
return kTRUE ;
}
void RooBinning::addBoundaryPair(Double_t boundary, Double_t mirrorPoint)
{
addBoundary(boundary) ;
addBoundary(2*mirrorPoint-boundary) ;
}
Bool_t RooBinning::removeBoundary(Double_t boundary)
{
if (_boundaries.find(boundary)!=_boundaries.end()) {
_boundaries.erase(boundary) ;
return kFALSE ;
}
return kTRUE ;
}
Bool_t RooBinning::hasBoundary(Double_t boundary)
{
return (_boundaries.find(boundary)!=_boundaries.end()) ;
}
void RooBinning::addUniform(Int_t nbins, Double_t xlo, Double_t xhi)
{
Int_t i ;
Double_t binw = (xhi-xlo)/nbins ;
for (i=0 ; i<=nbins ; i++)
addBoundary(xlo+i*binw) ;
}
Int_t RooBinning::binNumber(Double_t x) const
{
Int_t n(0) ;
for (set<Double_t>::const_iterator iter = _boundaries.begin() ; iter!=_boundaries.end() ; ++iter) {
if (x<*iter) {
return n ;
}
if (*iter> _xlo && n<_nbins-1) n++ ;
}
return n;
}
Int_t RooBinning::rawBinNumber(Double_t x) const
{
Int_t n(0) ;
for (set<Double_t>::const_iterator iter = _boundaries.begin() ; iter!=_boundaries.end() ; ++iter) {
if (x<*iter) return n>0?n-1:0 ;
n++ ;
}
return n-1;
}
Double_t RooBinning::nearestBoundary(Double_t x) const
{
Int_t bn = binNumber(x) ;
if (fabs(binLow(bn)-x)<fabs(binHigh(bn)-x)) {
return binLow(bn) ;
} else {
return binHigh(bn) ;
}
}
Double_t* RooBinning::array() const
{
if (_array) delete[] _array ;
_array = new Double_t[numBoundaries()] ;
Int_t i(0) ;
for (set<Double_t>::const_iterator iter = _boundaries.begin() ; iter!=_boundaries.end() ; ++iter) {
if (*iter>=_xlo && *iter <=_xhi) {
_array[i++] = *iter ;
}
}
return _array ;
}
void RooBinning::setRange(Double_t xlo, Double_t xhi)
{
if (xlo>xhi) {
coutE(InputArguments) << "RooUniformBinning::setRange: ERROR low bound > high bound" << endl ;
return ;
}
for (set<Double_t>::iterator iter = _boundaries.begin() ; iter!=_boundaries.end() ; ++iter) {
if ((*iter == _xlo && _ownBoundLo) || (*iter == _xhi && _ownBoundHi)) {
_boundaries.erase(iter) ;
}
}
_ownBoundLo = kFALSE ;
_ownBoundHi = kFALSE ;
if (!hasBoundary(xlo)) {
addBoundary(xlo) ;
_ownBoundLo = kTRUE ;
}
if (!hasBoundary(xhi)) {
addBoundary(xhi) ;
_ownBoundHi = kTRUE ;
}
_xlo = xlo ;
_xhi = xhi ;
updateBinCount() ;
}
void RooBinning::updateBinCount()
{
Int_t i(-1) ;
for (set<Double_t>::const_iterator iter = _boundaries.begin() ; iter!=_boundaries.end() ; ++iter) {
if (*iter>=_xlo && *iter <=_xhi) {
i++ ;
}
}
_nbins = i ;
}
Bool_t RooBinning::binEdges(Int_t bin, Double_t& xlo, Double_t& xhi) const
{
if (bin<0 || bin>= _nbins) {
coutE(InputArguments) << "RooBinning::binEdges ERROR: bin number must be in range (0," << _nbins << ")" << endl ;
return kTRUE ;
}
Int_t n(0) ;
for (set<Double_t>::const_iterator iter = _boundaries.begin() ; iter!=_boundaries.end() ; ++iter) {
if (n==bin && *iter>=_xlo) {
xlo = *iter ;
iter++ ;
xhi = *iter ;
return kFALSE ;
}
if (*iter>= _xlo && n<_nbins-1) n++ ;
}
return kTRUE ;
}
Double_t RooBinning::binCenter(Int_t bin) const
{
Double_t xlo,xhi ;
if (binEdges(bin,xlo,xhi)) return 0 ;
return (xlo+xhi)/2 ;
}
Double_t RooBinning::binWidth(Int_t bin) const
{
Double_t xlo,xhi ;
if (binEdges(bin,xlo,xhi)) return 0 ;
return (xhi-xlo);
}
Double_t RooBinning::binLow(Int_t bin) const
{
Double_t xlo,xhi ;
if (binEdges(bin,xlo,xhi)) return 0 ;
return xlo ;
}
Double_t RooBinning::binHigh(Int_t bin) const
{
Double_t xlo,xhi ;
if (binEdges(bin,xlo,xhi)) return 0 ;
return xhi ;
}
void RooBinning::Streamer(TBuffer &R__b)
{
if (R__b.IsReading()) {
UInt_t R__s, R__c;
Version_t R__v = R__b.ReadVersion(&R__s, &R__c); if (R__v) { }
if (R__v>1) {
R__b.ReadClassBuffer(RooBinning::Class(),this,R__v,R__s,R__c);
} else {
RooAbsBinning::Streamer(R__b);
R__b >> _xlo;
R__b >> _xhi;
R__b >> _ownBoundLo;
R__b >> _ownBoundHi;
R__b >> _nbins;
TList boundaries ;
boundaries.Streamer(R__b);
TIterator* iter = boundaries.MakeIterator() ;
RooDouble* elem ;
while((elem=(RooDouble*)iter->Next())) {
_boundaries.insert(*elem) ;
}
delete iter ;
R__b.CheckByteCount(R__s, R__c, RooBinning::IsA());
}
} else {
R__b.WriteClassBuffer(RooBinning::Class(),this);
}
}