CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends
npstat::NUHistoAxis Class Reference

#include <NUHistoAxis.h>

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
 
NUHistoAxis rebin (unsigned newBins) 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 (unsigned nBins, double min, double max, const char *label=0)
 
 NUHistoAxis ()
 
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
 
template<typename Numeric , class Axis >
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_, and AlCaHLTBitMon_QueryRunRegistry::string.

17  : binEdges_(binEdges), nBins_(binEdges.size() - 1U), uniform_(false)
18  {
19  if (!(binEdges_.size() > 1U && binEdges_.size() < UINT_MAX/2U))
20  throw npstat::NpstatInvalidArgument("In npstat::NUHistoAxis constructor: "
21  "number of bin edges is out of range");
22  std::sort(binEdges_.begin(), binEdges_.end());
23  min_ = binEdges_[0];
25  if (label)
27  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
std::string label_
Definition: NUHistoAxis.h:116
const std::string & label() const
Definition: NUHistoAxis.h:47
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 std::swap().

32  : min_(min), max_(max), nBins_(nBins), uniform_(true)
33  {
34  if (!(nBins_ && nBins_ < UINT_MAX/2U - 1U))
35  throw npstat::NpstatInvalidArgument("In npstat::NUHistoAxis constructor: "
36  "number of bins is out of range");
37  if (min_ > max_)
39  binEdges_ = EquidistantInLinearSpace(min_, max_, nBins+1U);
40  if (label)
42  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
double min() const
Definition: NUHistoAxis.h:39
unsigned nBins() const
Definition: NUHistoAxis.h:44
std::string label_
Definition: NUHistoAxis.h:116
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
double max() const
Definition: NUHistoAxis.h:40
const std::string & label() const
Definition: NUHistoAxis.h:47
npstat::NUHistoAxis::NUHistoAxis ( )
inlineprivate

Definition at line 137 of file NUHistoAxis.h.

Referenced by read(), and rebin().

137  : min_(0.0), max_(0.0),
138  nBins_(0), uniform_(false) {}

Member Function Documentation

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

Return the coordinate of the given bin center

Definition at line 60 of file NUHistoAxis.h.

References binEdges_.

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

61  {return 0.5*(binEdges_.at(binNum) + binEdges_.at(binNum + 1));}
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
Interval<double> npstat::NUHistoAxis::binInterval ( const int  binNum) const
inline

Return the coordinate interval occupied by the given bin

Definition at line 64 of file NUHistoAxis.h.

References binEdges_.

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

65  {return Interval<double>(binEdges_.at(binNum),
66  binEdges_.at(binNum + 1));}
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
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 78 of file NUHistoAxis.cc.

References binEdges_, and delta.

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

79  {
80  const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) -
81  binEdges_.begin();
82  return delta - 1;
83  }
dbl * delta
Definition: mlp_gen.cc:36
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
double npstat::NUHistoAxis::binWidth ( const int  binNum) const
inline

Definition at line 45 of file NUHistoAxis.h.

References binEdges_.

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

46  {return binEdges_.at(binNum+1) - binEdges_.at(binNum);}
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
gs::ClassId npstat::NUHistoAxis::classId ( ) const
inline

Method related to "geners" I/O

Definition at line 101 of file NUHistoAxis.h.

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

101 {return gs::ClassId(*this);}
static const char* npstat::NUHistoAxis::classname ( )
inlinestatic

Definition at line 105 of file NUHistoAxis.h.

105 {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 150 of file NUHistoAxis.cc.

References binEdges_, delta, and nBins_.

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

151  {
152  const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) -
153  binEdges_.begin();
154  int binnum = delta - 1;
155  if (binnum < 0)
156  binnum = 0;
157  else if (static_cast<unsigned>(binnum) >= nBins_)
158  binnum = nBins_ - 1U;
159  return binnum;
160  }
dbl * delta
Definition: mlp_gen.cc:36
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
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 85 of file NUHistoAxis.cc.

References binEdges_, delta, and nBins_.

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

86  {
87  const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) -
88  binEdges_.begin();
89  const int binnum = delta - 1;
90 
91  if (binnum < 0)
92  {
93  const double left = binEdges_[0];
94  const double right = binEdges_[1];
95  double bval = (x - left)/(right - left);
96  if (!mapLeftEdgeTo0)
97  bval -= 0.5;
98  if (bval < -1.0)
99  bval = -1.0;
100  return bval;
101  }
102  else if (static_cast<unsigned>(binnum) >= nBins_)
103  {
104  const double left = binEdges_[nBins_ - 1U];
105  const double right = binEdges_[nBins_];
106  double bval = nBins_ - 1U + (x - left)/(right - left);
107  if (!mapLeftEdgeTo0)
108  bval -= 0.5;
109  if (bval > static_cast<double>(nBins_))
110  bval = nBins_;
111  return bval;
112  }
113  else
114  {
115  const double left = binEdges_[binnum];
116  const double right = binEdges_[delta];
117  if (mapLeftEdgeTo0)
118  return binnum + (x - left)/(right - left);
119  else
120  {
121  // It is not obvious what is best to do here.
122  // The following works to preserve interpolation
123  // of 0th order. The commented out snippet below
124  // would instead connect bin centers by straight
125  // lines during interpolation.
126  return binnum + (x - left)/(right - left) - 0.5;
127 
128  // Bin center is mapped to binnum.
129  // Bin center of the next bin is mapped to binnum + 1.
130  // Bin center of the previous bin is mapped to binnum - 1.
131  //
132  // const double binCenter = (left + right)/2.0;
133  // if ((binnum == 0 && x <= binCenter) ||
134  // (static_cast<unsigned>(binnum) == nBins_ - 1 && x >= binCenter))
135  // return binnum + (x - left)/(right - left) - 0.5;
136  // else if (x <= binCenter)
137  // {
138  // const double otherBinCenter = (left + binEdges_[binnum - 1])/2.0;
139  // return binnum + (x - binCenter)/(binCenter - otherBinCenter);
140  // }
141  // else
142  // {
143  // const double otherBinCenter = (right + binEdges_[binnum + 2])/2.0;
144  // return binnum + (x - binCenter)/(otherBinCenter - binCenter);
145  // }
146  }
147  }
148  }
dbl * delta
Definition: mlp_gen.cc:36
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
Interval<double> npstat::NUHistoAxis::interval ( ) const
inline

Definition at line 41 of file NUHistoAxis.h.

References max_, and min_.

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

42  {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 49 of file NUHistoAxis.cc.

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

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

50  {
51  if (!(closeWithinTolerance(min_, r.min_, tol) &&
52  closeWithinTolerance(max_, r.max_, tol) &&
53  label_ == r.label_ &&
54  nBins_ == r.nBins_ &&
55  uniform_ == r.uniform_))
56  return false;
57  for (unsigned i=0; i<nBins_; ++i)
58  if (!closeWithinTolerance(binEdges_[i], r.binEdges_[i], tol))
59  return false;
60  return true;
61  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
int i
Definition: DBlmapReader.cc:9
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
std::string label_
Definition: NUHistoAxis.h:116
bool npstat::NUHistoAxis::isUniform ( ) const
inline

Definition at line 48 of file NUHistoAxis.h.

References uniform_.

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

Definition at line 47 of file NUHistoAxis.h.

References label_.

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

47 {return label_;}
std::string label_
Definition: NUHistoAxis.h:116
double npstat::NUHistoAxis::leftBinEdge ( const int  binNum) const
inline

Return the coordinate of the given bin left edge

Definition at line 52 of file NUHistoAxis.h.

References binEdges_.

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

53  {return binEdges_.at(binNum);}
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
double npstat::NUHistoAxis::length ( ) const
inline

Definition at line 43 of file NUHistoAxis.h.

References max_, and min_.

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

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

Definition at line 40 of file NUHistoAxis.h.

References max_.

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

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

Examine axis properties

Definition at line 39 of file NUHistoAxis.h.

References min_.

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

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

Definition at line 44 of file NUHistoAxis.h.

References nBins_.

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

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

Definition at line 73 of file NUHistoAxis.cc.

References alignCSCRings::r.

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

Definition at line 63 of file NUHistoAxis.cc.

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

64  {
65  return min_ == r.min_ &&
66  max_ == r.max_ &&
67  label_ == r.label_ &&
68  nBins_ == r.nBins_ &&
69  binEdges_ == r.binEdges_ &&
70  uniform_ == r.uniform_;
71  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
std::string label_
Definition: NUHistoAxis.h:116
unsigned npstat::NUHistoAxis::overflowIndex ( const double  x,
unsigned *  binNum 
) const
inlineprivate

Definition at line 123 of file NUHistoAxis.h.

References binNumber(), max_, and min_.

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

125  {
126  if (x < min_)
127  return 0U;
128  else if (x >= max_)
129  return 2U;
130  else
131  {
132  *binNum = binNumber(x);
133  return 1U;
134  }
135  }
int binNumber(double x) const
Definition: NUHistoAxis.cc:78
NUHistoAxis * npstat::NUHistoAxis::read ( const gs::ClassId &  id,
std::istream &  in 
)
static

Definition at line 171 of file NUHistoAxis.cc.

References label(), NUHistoAxis(), mps_fire::result, AlCaHLTBitMon_QueryRunRegistry::string, and uniform_.

Referenced by edmIntegrityCheck.PublishToFileSystem::get(), and npstat::DualHistoAxis::read().

172  {
173  static const gs::ClassId current(gs::ClassId::makeId<NUHistoAxis>());
174  current.ensureSameId(id);
175 
176  std::vector<double> binEdges;
178  unsigned char unif;
179  gs::read_pod_vector(in, &binEdges);
180  gs::read_pod(in, &label);
181  gs::read_pod(in, &unif);
182  if (in.fail())
183  throw gs::IOReadFailure("In npstat::UHistoAxis::read: "
184  "input stream failure");
185  NUHistoAxis* result = new NUHistoAxis(binEdges, label.c_str());
186  result->uniform_ = unif;
187  return result;
188  }
tuple result
Definition: mps_fire.py:84
const std::string & label() const
Definition: NUHistoAxis.h:47
NUHistoAxis npstat::NUHistoAxis::rebin ( unsigned  newBins) const

Return uniformly rebinned axis

Definition at line 44 of file NUHistoAxis.cc.

References label_, max_, min_, and NUHistoAxis().

45  {
46  return NUHistoAxis(newBins, min_, max_, label_.c_str());
47  }
std::string label_
Definition: NUHistoAxis.h:116
double npstat::NUHistoAxis::rightBinEdge ( const int  binNum) const
inline

Return the coordinate of the given bin right edge

Definition at line 56 of file NUHistoAxis.h.

References binEdges_.

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

57  {return binEdges_.at(binNum + 1);}
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
void npstat::NUHistoAxis::setLabel ( const char *  newlabel)
inline

Change the axis label

Definition at line 69 of file NUHistoAxis.h.

References label_.

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

70  {label_ = newlabel ? newlabel : "";}
std::string label_
Definition: NUHistoAxis.h:116
static unsigned npstat::NUHistoAxis::version ( )
inlinestatic

Definition at line 106 of file NUHistoAxis.h.

Referenced by validation.Sample::datasetpattern(), and validation.Sample::filename().

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

Definition at line 162 of file NUHistoAxis.cc.

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

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

163  {
164  gs::write_pod_vector(of, binEdges_);
165  gs::write_pod(of, label_);
166  unsigned char c = uniform_;
167  gs::write_pod(of, c);
168  return !of.fail();
169  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:115
std::string label_
Definition: NUHistoAxis.h:116

Friends And Related Function Documentation

friend class DualHistoAxis
friend

Definition at line 121 of file NUHistoAxis.h.

template<typename Numeric , class Axis >
friend class HistoND
friend

Definition at line 120 of file NUHistoAxis.h.

Member Data Documentation

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

Definition at line 114 of file NUHistoAxis.h.

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

double npstat::NUHistoAxis::min_
private

Definition at line 113 of file NUHistoAxis.h.

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

unsigned npstat::NUHistoAxis::nBins_
private

Definition at line 117 of file NUHistoAxis.h.

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

bool npstat::NUHistoAxis::uniform_
private

Definition at line 118 of file NUHistoAxis.h.

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