CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends

npstat::NUHistoAxis Class Reference

#include <NUHistoAxis.h>

List of all members.

Public Member Functions

double binCenter (const int binNum) const
Interval< double > binInterval (const int binNum) const
int binNumber (double x) const
unsigned closestValidBin (double x) const
double fltBinNumber (double x, bool mapLeftEdgeTo0=true) const
bool isClose (const NUHistoAxis &, double tol) const
double leftBinEdge (const int binNum) const
 NUHistoAxis (const std::vector< double > &binEdges, const char *label=0)
bool operator!= (const NUHistoAxis &) const
bool operator== (const NUHistoAxis &) const
double rightBinEdge (const int binNum) const
void setLabel (const char *newlabel)
double min () const
double max () const
Interval< double > interval () const
double length () const
unsigned nBins () const
double binWidth (const int binNum) const
const std::string & label () const
bool isUniform () const
gs::ClassId classId () const
bool write (std::ostream &of) const

Static Public Member Functions

static const char * classname ()
static NUHistoAxisread (const gs::ClassId &id, std::istream &in)
static unsigned version ()

Private Member Functions

 NUHistoAxis ()
 NUHistoAxis (unsigned nBins, double min, double max, const char *label=0)
unsigned overflowIndex (const double x, unsigned *binNum) const

Private Attributes

std::vector< double > binEdges_
std::string label_
double max_
double min_
unsigned nBins_
bool uniform_

Friends

class DualHistoAxis
class HistoND

Detailed Description

This class can be used to create histograms with non-uniform binning

Definition at line 27 of file NUHistoAxis.h.


Constructor & Destructor Documentation

npstat::NUHistoAxis::NUHistoAxis ( const std::vector< double > &  binEdges,
const char *  label = 0 
)

The number of bin edges provided must be at least 2. Edge coordinates will be sorted internally in the increasing order. The number of bins will be less by 1 than the number of edges.

Definition at line 15 of file NUHistoAxis.cc.

References binEdges_, label_, max_, min_, nBins_, python::multivaluedict::sort(), and AlCaHLTBitMon_QueryRunRegistry::string.

        : binEdges_(binEdges), nBins_(binEdges.size() - 1U), uniform_(false)
    {
        if (!(binEdges_.size() > 1U && binEdges_.size() < UINT_MAX/2U))
            throw npstat::NpstatInvalidArgument("In npstat::NUHistoAxis constructor: "
                                        "number of bin edges is out of range");
        std::sort(binEdges_.begin(), binEdges_.end());
        min_ = binEdges_[0];
        max_ = binEdges_[nBins_];
        if (label)
            label_ = std::string(label);
    }
npstat::NUHistoAxis::NUHistoAxis ( ) [inline, private]

Definition at line 108 of file NUHistoAxis.h.

Referenced by read().

                             : min_(0.0), max_(0.0),
                               nBins_(0), uniform_(false) {}
npstat::NUHistoAxis::NUHistoAxis ( unsigned  nBins,
double  min,
double  max,
const char *  label = 0 
) [private]

Definition at line 29 of file NUHistoAxis.cc.

References binEdges_, label_, max_, min_, nBins_, AlCaHLTBitMon_QueryRunRegistry::string, and swap().

        : min_(min), max_(max), nBins_(nBins), uniform_(true)
    {
        if (!(nBins_ && nBins_ < UINT_MAX/2U - 1U))
            throw npstat::NpstatInvalidArgument("In npstat::NUHistoAxis constructor: "
                                        "number of bins is out of range");
        if (min_ > max_)
            std::swap(min_, max_);
        binEdges_ = EquidistantInLinearSpace(min_, max_, nBins+1U);
        if (label)
            label_ = std::string(label);
    }

Member Function Documentation

double npstat::NUHistoAxis::binCenter ( const int  binNum) const [inline]

Return the coordinate of the given bin center

Definition at line 61 of file NUHistoAxis.h.

References binEdges_.

Referenced by npstat::DualHistoAxis::binCenter(), npstat::convertToGridAxis(), and fltBinNumber().

            {return 0.5*(binEdges_.at(binNum) + binEdges_.at(binNum + 1));}
Interval<double> npstat::NUHistoAxis::binInterval ( const int  binNum) const [inline]

Return the coordinate interval occupied by the given bin

Definition at line 65 of file NUHistoAxis.h.

References binEdges_.

Referenced by npstat::DualHistoAxis::binInterval().

            {return Interval<double>(binEdges_.at(binNum),
                                     binEdges_.at(binNum + 1));}
int npstat::NUHistoAxis::binNumber ( double  x) const

This method returns -1 for values below the lower limit and "nBins()" for values equal to or above the upper limit

Definition at line 73 of file NUHistoAxis.cc.

References binEdges_, delta, and x.

Referenced by npstat::DualHistoAxis::binNumber(), and overflowIndex().

    {
        const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) - 
                          binEdges_.begin();
        return delta - 1;
    }
double npstat::NUHistoAxis::binWidth ( const int  binNum) const [inline]

Definition at line 46 of file NUHistoAxis.h.

References binEdges_.

Referenced by npstat::DualHistoAxis::binWidth().

            {return binEdges_.at(binNum+1) - binEdges_.at(binNum);}
gs::ClassId npstat::NUHistoAxis::classId ( ) const [inline]

Method related to "geners" I/O

Definition at line 99 of file NUHistoAxis.h.

Referenced by npstat::DualHistoAxis::write().

{return gs::ClassId(*this);}
static const char* npstat::NUHistoAxis::classname ( ) [inline, static]

Definition at line 103 of file NUHistoAxis.h.

{return "npstat::NUHistoAxis";}
unsigned npstat::NUHistoAxis::closestValidBin ( double  x) const

This method returns the closest valid bin number (above 0 and below nBins() )

Definition at line 137 of file NUHistoAxis.cc.

References binEdges_, delta, nBins_, and x.

Referenced by npstat::DualHistoAxis::closestValidBin().

    {
        const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) - 
                          binEdges_.begin();
        int binnum = delta - 1;
        if (binnum < 0)
            binnum = 0;
        else if (static_cast<unsigned>(binnum) >= nBins_)
            binnum = nBins_ - 1U;
        return binnum;
    }
double npstat::NUHistoAxis::fltBinNumber ( double  x,
bool  mapLeftEdgeTo0 = true 
) const

Floating point bin number given the coordinate. Useful for interpolation methods and such.

Definition at line 80 of file NUHistoAxis.cc.

References binCenter(), binEdges_, delta, nBins_, and x.

Referenced by npstat::DualHistoAxis::fltBinNumber().

    {
        const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) - 
                          binEdges_.begin();
        const int binnum = delta - 1;

        if (binnum < 0)
        {
            const double left = binEdges_[0];
            const double right = binEdges_[1];
            double bval = (x - left)/(right - left);
            if (!mapLeftEdgeTo0)
                bval -= 0.5;
            if (bval < -1.0)
                bval = -1.0;
            return bval;
        }
        else if (static_cast<unsigned>(binnum) >= nBins_)
        {
            const double left = binEdges_[nBins_ - 1U];
            const double right = binEdges_[nBins_];
            double bval = nBins_ - 1U + (x - left)/(right - left);
            if (!mapLeftEdgeTo0)
                bval -= 0.5;
            if (bval > static_cast<double>(nBins_))
                bval = nBins_;
            return bval;
        }
        else
        {
            const double left = binEdges_[binnum];
            const double right = binEdges_[delta];
            if (mapLeftEdgeTo0)
                return binnum + (x - left)/(right - left);
            else
            {
                // Bin center is mapped to binnum.
                // Bin center of the next bin is mapped to binnum + 1.
                // Bin center of the previos bin is mapped to binnum - 1.
                const double binCenter = (left + right)/2.0;
                if ((binnum == 0 && x <= binCenter) ||
                    (static_cast<unsigned>(binnum) == nBins_ - 1 && x >= binCenter))
                    return binnum + (x - left)/(right - left) - 0.5;
                else if (x <= binCenter)
                {
                    const double otherBinCenter = (left + binEdges_[binnum - 1])/2.0;
                    return binnum + (x - binCenter)/(binCenter - otherBinCenter);
                }
                else
                {
                    const double otherBinCenter = (right + binEdges_[binnum + 1])/2.0;
                    return binnum + (x - binCenter)/(otherBinCenter - binCenter);
                }
            }
        }
    }
Interval<double> npstat::NUHistoAxis::interval ( ) const [inline]

Definition at line 42 of file NUHistoAxis.h.

References max_, and min_.

Referenced by npstat::DualHistoAxis::interval().

            {return Interval<double>(min_, max_);}
bool npstat::NUHistoAxis::isClose ( const NUHistoAxis r,
double  tol 
) const

Comparison of axis coordinates within given tolerance

Definition at line 44 of file NUHistoAxis.cc.

References binEdges_, npstat::closeWithinTolerance(), i, label_, max_, min_, nBins_, and uniform_.

Referenced by npstat::DualHistoAxis::isClose().

    {
        if (!(closeWithinTolerance(min_, r.min_, tol) && 
              closeWithinTolerance(max_, r.max_, tol) &&
              label_ == r.label_ &&
              nBins_ == r.nBins_ &&
              uniform_ == r.uniform_))
            return false;
        for (unsigned i=0; i<nBins_; ++i)
            if (!closeWithinTolerance(binEdges_[i], r.binEdges_[i], tol))
                return false;
        return true;
    }
bool npstat::NUHistoAxis::isUniform ( ) const [inline]

Definition at line 49 of file NUHistoAxis.h.

References uniform_.

{return uniform_;}
const std::string& npstat::NUHistoAxis::label ( ) const [inline]

Definition at line 48 of file NUHistoAxis.h.

References label_.

Referenced by npstat::convertToGridAxis(), npstat::DualHistoAxis::label(), and read().

{return label_;}
double npstat::NUHistoAxis::leftBinEdge ( const int  binNum) const [inline]

Return the coordinate of the given bin left edge

Definition at line 53 of file NUHistoAxis.h.

References binEdges_.

Referenced by npstat::DualHistoAxis::leftBinEdge().

            {return binEdges_.at(binNum);}
double npstat::NUHistoAxis::length ( ) const [inline]

Definition at line 44 of file NUHistoAxis.h.

References max_, and min_.

Referenced by npstat::DualHistoAxis::length().

{return max_ - min_;}
double npstat::NUHistoAxis::max ( ) const [inline]

Definition at line 41 of file NUHistoAxis.h.

References max_.

Referenced by npstat::DualHistoAxis::max().

{return max_;}
double npstat::NUHistoAxis::min ( ) const [inline]

Examine axis propoerties

Definition at line 40 of file NUHistoAxis.h.

References min_.

Referenced by npstat::DualHistoAxis::min().

{return min_;}
unsigned npstat::NUHistoAxis::nBins ( ) const [inline]

Definition at line 45 of file NUHistoAxis.h.

References nBins_.

Referenced by npstat::convertToGridAxis(), and npstat::DualHistoAxis::nBins().

{return nBins_;}
bool npstat::NUHistoAxis::operator!= ( const NUHistoAxis r) const

Definition at line 68 of file NUHistoAxis.cc.

References alignCSCRings::r.

    {
        return !(*this == r);
    }
bool npstat::NUHistoAxis::operator== ( const NUHistoAxis r) const

Definition at line 58 of file NUHistoAxis.cc.

References binEdges_, label_, max_, min_, nBins_, and uniform_.

    {
        return min_ == r.min_ && 
               max_ == r.max_ && 
               label_ == r.label_ &&
               nBins_ == r.nBins_ &&
               binEdges_ == r.binEdges_ &&
               uniform_ == r.uniform_;
    }
unsigned npstat::NUHistoAxis::overflowIndex ( const double  x,
unsigned *  binNum 
) const [inline, private]

Definition at line 124 of file NUHistoAxis.h.

References binNumber(), max_, and min_.

Referenced by npstat::DualHistoAxis::overflowIndex().

        {
            if (x < min_)
                return 0U;
            else if (x >= max_)
                return 2U;
            else
            {
                *binNum = binNumber(x);
                return 1U;
            }
        }
NUHistoAxis * npstat::NUHistoAxis::read ( const gs::ClassId &  id,
std::istream &  in 
) [static]

Definition at line 158 of file NUHistoAxis.cc.

References cond::rpcobimon::current, label(), NUHistoAxis(), query::result, AlCaHLTBitMon_QueryRunRegistry::string, and uniform_.

Referenced by npstat::DualHistoAxis::read().

    {
        static const gs::ClassId current(gs::ClassId::makeId<NUHistoAxis>());
        current.ensureSameId(id);

        std::vector<double> binEdges;
        std::string label;
        unsigned char unif;
        gs::read_pod_vector(in, &binEdges);
        gs::read_pod(in, &label);
        gs::read_pod(in, &unif);
        if (in.fail())
            throw gs::IOReadFailure("In npstat::UHistoAxis::read: "
                                    "input stream failure");
        NUHistoAxis* result = new NUHistoAxis(binEdges, label.c_str());
        result->uniform_ = unif;
        return result;
    }
double npstat::NUHistoAxis::rightBinEdge ( const int  binNum) const [inline]

Return the coordinate of the given bin right edge

Definition at line 57 of file NUHistoAxis.h.

References binEdges_.

Referenced by npstat::DualHistoAxis::rightBinEdge().

            {return binEdges_.at(binNum + 1);}
void npstat::NUHistoAxis::setLabel ( const char *  newlabel) [inline]

Change the axis label

Definition at line 70 of file NUHistoAxis.h.

References label_.

Referenced by npstat::DualHistoAxis::setLabel().

            {label_ = newlabel ? newlabel : "";}
static unsigned npstat::NUHistoAxis::version ( ) [inline, static]

Definition at line 104 of file NUHistoAxis.h.

{return 1;}
bool npstat::NUHistoAxis::write ( std::ostream &  of) const

Definition at line 149 of file NUHistoAxis.cc.

References binEdges_, trackerHits::c, label_, and uniform_.

Referenced by npstat::DualHistoAxis::write().

    {
        gs::write_pod_vector(of, binEdges_);
        gs::write_pod(of, label_);
        unsigned char c = uniform_;
        gs::write_pod(of, c);
        return !of.fail();
    }

Friends And Related Function Documentation

friend class DualHistoAxis [friend]

Definition at line 122 of file NUHistoAxis.h.

friend class HistoND [friend]

Definition at line 121 of file NUHistoAxis.h.


Member Data Documentation

std::vector<double> npstat::NUHistoAxis::binEdges_ [private]
std::string npstat::NUHistoAxis::label_ [private]

Definition at line 117 of file NUHistoAxis.h.

Referenced by isClose(), label(), NUHistoAxis(), operator==(), setLabel(), and write().

double npstat::NUHistoAxis::max_ [private]

Definition at line 115 of file NUHistoAxis.h.

Referenced by interval(), isClose(), length(), max(), NUHistoAxis(), operator==(), and overflowIndex().

double npstat::NUHistoAxis::min_ [private]

Definition at line 114 of file NUHistoAxis.h.

Referenced by interval(), isClose(), length(), min(), NUHistoAxis(), operator==(), and overflowIndex().

unsigned npstat::NUHistoAxis::nBins_ [private]

Definition at line 118 of file NUHistoAxis.h.

Referenced by closestValidBin(), fltBinNumber(), isClose(), nBins(), NUHistoAxis(), and operator==().

Definition at line 119 of file NUHistoAxis.h.

Referenced by isClose(), isUniform(), operator==(), read(), and write().