CMS 3D CMS Logo

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

npstat::GridAxis Class Reference

#include <GridAxis.h>

List of all members.

Public Member Functions

std::pair< unsigned, double > getInterval (double coordinate) const
bool isClose (const GridAxis &r, double tol) const
std::pair< unsigned, double > linearInterval (double coordinate) const
bool operator!= (const GridAxis &r) const
bool operator== (const GridAxis &r) const
void setLabel (const char *newlabel)
 GridAxis (const std::vector< double > &coords, bool useLogSpace=false)
 GridAxis (const std::vector< double > &coords, const char *label, bool useLogSpace=false)
const std::vector< double > & coords () const
const std::string & label () const
bool usesLogSpace () const
unsigned nCoords () const
double coordinate (const unsigned i) const
double min () const
double max () const
double length () const
bool isUniform () const
unsigned nIntervals () const
double intervalWidth (const unsigned i=0) const
gs::ClassId classId () const
bool write (std::ostream &of) const

Static Public Member Functions

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

Private Member Functions

 GridAxis ()
void initialize ()

Private Attributes

std::vector< double > coords_
std::string label_
std::vector< double > logs_
unsigned npt_
bool useLogSpace_

Detailed Description

Information needed to define an axis of a rectangular grid. The distance between grid points can change from point to point.

The UniformAxis class will be more efficient in representing equidistant grids.

Definition at line 30 of file GridAxis.h.


Constructor & Destructor Documentation

npstat::GridAxis::GridAxis ( const std::vector< double > &  coords,
bool  useLogSpace = false 
)

The number of grid coordinates provided must be at least 2. Coordinates will be sorted internally in the increasing order.

Definition at line 38 of file GridAxis.cc.

References initialize().

        : coords_(coords),
          npt_(coords_.size()),
          useLogSpace_(useLogSpace)
    {
        initialize();
    }
npstat::GridAxis::GridAxis ( const std::vector< double > &  coords,
const char *  label,
bool  useLogSpace = false 
)

Definition at line 47 of file GridAxis.cc.

References initialize().

        : coords_(coords),
          label_(label ? label : ""),
          npt_(coords_.size()),
          useLogSpace_(useLogSpace)
    {
        initialize();
    }
npstat::GridAxis::GridAxis ( ) [inline, private]

Definition at line 123 of file GridAxis.h.

Referenced by read().

: npt_(0), useLogSpace_(false) {}

Member Function Documentation

gs::ClassId npstat::GridAxis::classId ( ) const [inline]

Method related to "geners" I/O

Definition at line 114 of file GridAxis.h.

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

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

Definition at line 118 of file GridAxis.h.

{return "npstat::GridAxis";}
double npstat::GridAxis::coordinate ( const unsigned  i) const [inline]

Definition at line 84 of file GridAxis.h.

References coords_.

Referenced by npstat::convertToHistoAxis(), and npstat::DualAxis::coordinate().

            {return coords_.at(i);}
const std::vector<double>& npstat::GridAxis::coords ( ) const [inline]

Basic accessor returning a parameter provided in the constructor

Definition at line 45 of file GridAxis.h.

References coords_.

Referenced by npstat::DualAxis::coords(), and read().

{return coords_;}
std::pair< unsigned, double > npstat::GridAxis::getInterval ( double  coordinate) const

This method returns the grid interval number and the weight of the point at the left side of the interval. The weight will be set to 1 if the given coordinate coincides with the grid point and will decay to 0 linearly as the coordinate moves towards the next point on the right.

The coordinates below the leftmost grid point are mapped into the 0th interval with weight 1. The coordinates above the rightmost grid point are mapped into the last interval with weight 0 for the left point (it is expected that weight 1 will then be assigned to the right point).

Definition at line 58 of file GridAxis.cc.

References trackerHits::c, coords_, prof2calltree::l, create_public_lumi_plots::log, logs_, npt_, useLogSpace_, w(), and x.

Referenced by npstat::DualAxis::getInterval().

    {
        if (useLogSpace_)
            if (x <= 0.0) throw npstat::NpstatDomainError(
                "In GridAxis::getInterval: argument must be positive");
        const double* c = &coords_[0];
        if (x <= c[0])
            return std::pair<unsigned,double>(0U, 1.0);
        else if (x >= c[npt_ - 1U])
            return std::pair<unsigned,double>(npt_ - 2U, 0.0);
        else
        {
            unsigned ibnd = lower_bound
                (coords_.begin(), coords_.end(), x) - coords_.begin();
            --ibnd;
            if (useLogSpace_)
            {
                const double* l = &logs_[0];
                const double w = 1.0 - (log(x) - l[ibnd])/
                    (l[ibnd + 1U] - l[ibnd]);
                return std::pair<unsigned,double>(ibnd, w);
            }
            else
            {
                const double w = 1.0 - (x - c[ibnd])/(c[ibnd + 1U] - c[ibnd]);
                return std::pair<unsigned,double>(ibnd, w);
            }
        }
    }
void npstat::GridAxis::initialize ( ) [private]

Definition at line 12 of file GridAxis.cc.

References trackerHits::c, coords_, i, create_public_lumi_plots::log, logs_, npt_, python::multivaluedict::sort(), and useLogSpace_.

Referenced by GridAxis().

    {
        if (npt_ <= 1U) throw npstat::NpstatInvalidArgument(
            "In npstat::GridAxis::initialize: insufficient number "
            "of coordinates (at least 2 are required)");
        std::sort(coords_.begin(), coords_.end());
        const double* c = &coords_[0];
        if (useLogSpace_)
        {
            logs_.reserve(npt_);
            for (unsigned i=0; i<npt_; ++i)
            {
                if (c[i] <= 0.0) throw npstat::NpstatInvalidArgument(
                    "In npstat::GridAxis::initialize: in log space"
                    "all coordinates must be positive");
                logs_.push_back(log(c[i]));
            }
        }

        // Can't have duplicate coordinates
        for (unsigned i=1; i<npt_; ++i)
            if (c[i] <= c[i - 1U]) throw npstat::NpstatInvalidArgument(
                "In npstat::GridAxis::initialize: "
                "all coordinates must be distinct");
    }
double npstat::GridAxis::intervalWidth ( const unsigned  i = 0) const [inline]

Definition at line 91 of file GridAxis.h.

References coords_, and i.

Referenced by npstat::DualAxis::intervalWidth().

            {return coords_.at(i+1) - coords_.at(i);}
bool npstat::GridAxis::isClose ( const GridAxis r,
double  tol 
) const

Check for closeness of coordinates with another axis within the given relative tolerance

Definition at line 181 of file GridAxis.cc.

References npstat::closeWithinTolerance(), coords_, i, label_, n, and useLogSpace_.

    {
        if (!(useLogSpace_ == r.useLogSpace_ &&
              label_ == r.label_))
            return false;
        const unsigned long n = coords_.size();
        if (n != r.coords_.size())
            return false;
        for (unsigned long i=0; i<n; ++i)
            if (!closeWithinTolerance(coords_[i], r.coords_[i], tol))
                return false;
        return true;
    }
bool npstat::GridAxis::isUniform ( ) const [inline]

Definition at line 89 of file GridAxis.h.

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

Definition at line 46 of file GridAxis.h.

References label_.

Referenced by npstat::convertToHistoAxis(), npstat::DualAxis::label(), and read().

{return label_;}
double npstat::GridAxis::length ( ) const [inline]

Definition at line 88 of file GridAxis.h.

References coords_.

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

{return coords_.back() - coords_.front();}
std::pair< unsigned, double > npstat::GridAxis::linearInterval ( double  coordinate) const

This method returns the grid interval number and the weight of the point at the left side of the interval. The weight will be set to 1 if the given coordinate coincides with the grid point and will decay to 0 linearly as the coordinate moves towards the next point on the right. The weight for the point on the right should be set to one minus the weight on the left.

The coordinates outside of grid boundaries will result in weights which are less than zero or more than one. They will be calculated by linear extrapolation from the closest interval in the grid (i.e., leftmost or rightmost).

Definition at line 88 of file GridAxis.cc.

References trackerHits::c, coords_, prof2calltree::l, create_public_lumi_plots::log, logs_, npt_, useLogSpace_, w(), and x.

Referenced by npstat::DualAxis::linearInterval().

    {
        if (useLogSpace_)
            if (x <= 0.0) throw npstat::NpstatDomainError(
                "In GridAxis::linearInterval: argument must be positive");
        const double* c = &coords_[0];
        if (x <= c[0])
        {
            if (useLogSpace_)   
            {
                const double* l = &logs_[0];
                const double bw = l[1] - l[0];
                return std::pair<unsigned,double>(0U, 1.0 - (log(x) - l[0])/bw);
            }
            else
            {
                const double bw = c[1] - c[0];
                return std::pair<unsigned,double>(0U, 1.0 - (x - c[0])/bw);
            }
        }
        else if (x >= c[npt_ - 1U])
        {
            if (useLogSpace_)
            {
                const double* l = &logs_[0];
                const double bw = l[npt_ - 1U] - l[npt_ - 2U];
                return std::pair<unsigned,double>(
                    npt_ - 2U, (l[npt_ - 1U] - log(x))/bw);
            }
            else
            {
                const double bw = c[npt_ - 1U] - c[npt_ - 2U];
                return std::pair<unsigned,double>(
                    npt_ - 2U, (c[npt_ - 1U] - x)/bw);
            }
        }
        else
        {
            unsigned ibnd = lower_bound
                (coords_.begin(), coords_.end(), x) - coords_.begin();
            --ibnd;
            if (useLogSpace_)
            {
                const double* l = &logs_[0];
                const double w = 1.0 - (log(x) - l[ibnd])/
                    (l[ibnd + 1U] - l[ibnd]);
                return std::pair<unsigned,double>(ibnd, w);
            }
            else
            {
                const double w = 1.0 - (x - c[ibnd])/(c[ibnd + 1U] - c[ibnd]);
                return std::pair<unsigned,double>(ibnd, w);
            }
        }
    }
double npstat::GridAxis::max ( ) const [inline]

Definition at line 87 of file GridAxis.h.

References coords_.

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

{return coords_.back();}
double npstat::GridAxis::min ( ) const [inline]

Definition at line 86 of file GridAxis.h.

References coords_.

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

{return coords_.front();}
unsigned npstat::GridAxis::nCoords ( ) const [inline]

Convenience accessor

Definition at line 83 of file GridAxis.h.

References npt_.

Referenced by npstat::convertToHistoAxis(), and npstat::DualAxis::nCoords().

{return npt_;}
unsigned npstat::GridAxis::nIntervals ( ) const [inline]

Definition at line 90 of file GridAxis.h.

References coords_.

Referenced by npstat::DualAxis::nIntervals().

{return coords_.size() - 1;}
bool npstat::GridAxis::operator!= ( const GridAxis r) const [inline]

Logical negation of operator==

Definition at line 99 of file GridAxis.h.

References alignCSCRings::r.

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

Compare two grids for equality

Definition at line 174 of file GridAxis.cc.

References coords_, label_, and useLogSpace_.

    {
        return useLogSpace_ == r.useLogSpace_ &&
               coords_ == r.coords_ &&
               label_ == r.label_;
    }
GridAxis * npstat::GridAxis::read ( const gs::ClassId &  id,
std::istream &  in 
) [static]

Definition at line 154 of file GridAxis.cc.

References coords(), cond::rpcobimon::current, GridAxis(), label(), and AlCaHLTBitMon_QueryRunRegistry::string.

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

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

        std::vector<double> coords;
        gs::read_pod_vector(in, &coords);

        std::string label;
        gs::read_pod(in, &label);

        bool useLogSpace;
        gs::read_pod(in, &useLogSpace);

        if (in.fail())
            throw gs::IOReadFailure("In npstat::GridAxis::read: "
                                    "input stream failure");
        return new GridAxis(coords, label.c_str(), useLogSpace);
    }
void npstat::GridAxis::setLabel ( const char *  newlabel) [inline]

Modify the axis label

Definition at line 109 of file GridAxis.h.

References label_.

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

            {label_ = newlabel ? newlabel : "";}
bool npstat::GridAxis::usesLogSpace ( ) const [inline]

Definition at line 47 of file GridAxis.h.

References useLogSpace_.

Referenced by npstat::DualAxis::usesLogSpace().

{return useLogSpace_;}
static unsigned npstat::GridAxis::version ( ) [inline, static]

Definition at line 119 of file GridAxis.h.

{return 2;}
bool npstat::GridAxis::write ( std::ostream &  of) const

Definition at line 144 of file GridAxis.cc.

References coords_, label_, and useLogSpace_.

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

    {
        // It is unlikely that this object will be written in isolation.
        // So, do not bother with too many checks.
        gs::write_pod_vector(of, coords_);
        gs::write_pod(of, label_);
        gs::write_pod(of, useLogSpace_);
        return !of.fail();        
    }

Member Data Documentation

std::vector<double> npstat::GridAxis::coords_ [private]
std::string npstat::GridAxis::label_ [private]

Definition at line 129 of file GridAxis.h.

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

std::vector<double> npstat::GridAxis::logs_ [private]

Definition at line 128 of file GridAxis.h.

Referenced by getInterval(), initialize(), and linearInterval().

unsigned npstat::GridAxis::npt_ [private]

Definition at line 130 of file GridAxis.h.

Referenced by getInterval(), initialize(), linearInterval(), and nCoords().