CMS 3D CMS Logo

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=nullptr)
 
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=nullptr)
 
 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 28 of file NUHistoAxis.h.

Constructor & Destructor Documentation

◆ NUHistoAxis() [1/3]

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

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(), label_, max_, min_, nBins_, jetUpdater_cfi::sort, AlCaHLTBitMon_QueryRunRegistry::string, and mitigatedMETSequence_cff::U.

16  : binEdges_(binEdges), nBins_(binEdges.size() - 1U), uniform_(false) {
17  if (!(binEdges_.size() > 1U && binEdges_.size() < UINT_MAX / 2U))
19  "In npstat::NUHistoAxis constructor: "
20  "number of bin edges is out of range");
21  std::sort(binEdges_.begin(), binEdges_.end());
22  min_ = binEdges_[0];
24  if (label)
26  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108
std::string label_
Definition: NUHistoAxis.h:109
const std::string & label() const
Definition: NUHistoAxis.h:45

◆ NUHistoAxis() [2/3]

npstat::NUHistoAxis::NUHistoAxis ( unsigned  nBins,
double  min,
double  max,
const char *  label = nullptr 
)
private

Definition at line 28 of file NUHistoAxis.cc.

References binEdges_, label(), label_, max_, min_, nBins(), nBins_, AlCaHLTBitMon_QueryRunRegistry::string, edm::swap(), and mitigatedMETSequence_cff::U.

29  : min_(min), max_(max), nBins_(nBins), uniform_(true) {
30  if (!(nBins_ && nBins_ < UINT_MAX / 2U - 1U))
32  "In npstat::NUHistoAxis constructor: "
33  "number of bins is out of range");
34  if (min_ > max_)
36  binEdges_ = EquidistantInLinearSpace(min_, max_, nBins + 1U);
37  if (label)
39  }
double max() const
Definition: NUHistoAxis.h:40
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108
unsigned nBins() const
Definition: NUHistoAxis.h:43
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
std::string label_
Definition: NUHistoAxis.h:109
const std::string & label() const
Definition: NUHistoAxis.h:45
double min() const
Definition: NUHistoAxis.h:39

◆ NUHistoAxis() [3/3]

npstat::NUHistoAxis::NUHistoAxis ( )
inlineprivate

Definition at line 128 of file NUHistoAxis.h.

Referenced by read(), and rebin().

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

Member Function Documentation

◆ binCenter()

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

Return the coordinate of the given bin center

Definition at line 56 of file NUHistoAxis.h.

References binEdges_.

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

56 { return 0.5 * (binEdges_.at(binNum) + binEdges_.at(binNum + 1)); }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108

◆ binInterval()

Interval<double> npstat::NUHistoAxis::binInterval ( const int  binNum) const
inline

Return the coordinate interval occupied by the given bin

Definition at line 59 of file NUHistoAxis.h.

References binEdges_.

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

59  {
60  return Interval<double>(binEdges_.at(binNum), binEdges_.at(binNum + 1));
61  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108

◆ binNumber()

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 62 of file NUHistoAxis.cc.

References binEdges_, dumpMFGeometry_cfg::delta, pfDeepBoostedJetPreprocessParams_cfi::upper_bound, and x.

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

62  {
63  const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) - binEdges_.begin();
64  return delta - 1;
65  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108
float x

◆ binWidth()

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

Definition at line 44 of file NUHistoAxis.h.

References binEdges_.

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

44 { return binEdges_.at(binNum + 1) - binEdges_.at(binNum); }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108

◆ classId()

gs::ClassId npstat::NUHistoAxis::classId ( ) const
inline

Method related to "geners" I/O

Definition at line 95 of file NUHistoAxis.h.

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

95 { return gs::ClassId(*this); }

◆ classname()

static const char* npstat::NUHistoAxis::classname ( )
inlinestatic

Definition at line 99 of file NUHistoAxis.h.

99 { return "npstat::NUHistoAxis"; }

◆ closestValidBin()

unsigned npstat::NUHistoAxis::closestValidBin ( double  x) const

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

Definition at line 124 of file NUHistoAxis.cc.

References binEdges_, dumpMFGeometry_cfg::delta, nBins_, mitigatedMETSequence_cff::U, pfDeepBoostedJetPreprocessParams_cfi::upper_bound, and x.

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

124  {
125  const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) - binEdges_.begin();
126  int binnum = delta - 1;
127  if (binnum < 0)
128  binnum = 0;
129  else if (static_cast<unsigned>(binnum) >= nBins_)
130  binnum = nBins_ - 1U;
131  return binnum;
132  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108
float x

◆ fltBinNumber()

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 67 of file NUHistoAxis.cc.

References binEdges_, dumpMFGeometry_cfg::delta, nBins_, mitigatedMETSequence_cff::U, pfDeepBoostedJetPreprocessParams_cfi::upper_bound, and x.

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

67  {
68  const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) - binEdges_.begin();
69  const int binnum = delta - 1;
70 
71  if (binnum < 0) {
72  const double left = binEdges_[0];
73  const double right = binEdges_[1];
74  double bval = (x - left) / (right - left);
75  if (!mapLeftEdgeTo0)
76  bval -= 0.5;
77  if (bval < -1.0)
78  bval = -1.0;
79  return bval;
80  } else if (static_cast<unsigned>(binnum) >= nBins_) {
81  const double left = binEdges_[nBins_ - 1U];
82  const double right = binEdges_[nBins_];
83  double bval = nBins_ - 1U + (x - left) / (right - left);
84  if (!mapLeftEdgeTo0)
85  bval -= 0.5;
86  if (bval > static_cast<double>(nBins_))
87  bval = nBins_;
88  return bval;
89  } else {
90  const double left = binEdges_[binnum];
91  const double right = binEdges_[delta];
92  if (mapLeftEdgeTo0)
93  return binnum + (x - left) / (right - left);
94  else {
95  // It is not obvious what is best to do here.
96  // The following works to preserve interpolation
97  // of 0th order. The commented out snippet below
98  // would instead connect bin centers by straight
99  // lines during interpolation.
100  return binnum + (x - left) / (right - left) - 0.5;
101 
102  // Bin center is mapped to binnum.
103  // Bin center of the next bin is mapped to binnum + 1.
104  // Bin center of the previous bin is mapped to binnum - 1.
105  //
106  // const double binCenter = (left + right)/2.0;
107  // if ((binnum == 0 && x <= binCenter) ||
108  // (static_cast<unsigned>(binnum) == nBins_ - 1 && x >= binCenter))
109  // return binnum + (x - left)/(right - left) - 0.5;
110  // else if (x <= binCenter)
111  // {
112  // const double otherBinCenter = (left + binEdges_[binnum - 1])/2.0;
113  // return binnum + (x - binCenter)/(binCenter - otherBinCenter);
114  // }
115  // else
116  // {
117  // const double otherBinCenter = (right + binEdges_[binnum + 2])/2.0;
118  // return binnum + (x - binCenter)/(otherBinCenter - binCenter);
119  // }
120  }
121  }
122  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108
float x

◆ interval()

Interval<double> npstat::NUHistoAxis::interval ( ) const
inline

Definition at line 41 of file NUHistoAxis.h.

References max_, and min_.

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

41 { return Interval<double>(min_, max_); }

◆ isClose()

bool npstat::NUHistoAxis::isClose ( const NUHistoAxis r,
double  tol 
) const

Comparison of axis coordinates within given tolerance

Definition at line 45 of file NUHistoAxis.cc.

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

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

45  {
46  if (!(closeWithinTolerance(min_, r.min_, tol) && closeWithinTolerance(max_, r.max_, tol) && label_ == r.label_ &&
47  nBins_ == r.nBins_ && uniform_ == r.uniform_))
48  return false;
49  for (unsigned i = 0; i < nBins_; ++i)
50  if (!closeWithinTolerance(binEdges_[i], r.binEdges_[i], tol))
51  return false;
52  return true;
53  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
std::string label_
Definition: NUHistoAxis.h:109

◆ isUniform()

bool npstat::NUHistoAxis::isUniform ( ) const
inline

Definition at line 46 of file NUHistoAxis.h.

References uniform_.

46 { return uniform_; }

◆ label()

const std::string& npstat::NUHistoAxis::label ( ) const
inline

Definition at line 45 of file NUHistoAxis.h.

References label_.

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

45 { return label_; }
std::string label_
Definition: NUHistoAxis.h:109

◆ leftBinEdge()

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

Return the coordinate of the given bin left edge

Definition at line 50 of file NUHistoAxis.h.

References binEdges_.

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

50 { return binEdges_.at(binNum); }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108

◆ length()

double npstat::NUHistoAxis::length ( ) const
inline

Definition at line 42 of file NUHistoAxis.h.

References max_, and min_.

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

42 { return max_ - min_; }

◆ max()

double npstat::NUHistoAxis::max ( ) const
inline

Definition at line 40 of file NUHistoAxis.h.

References max_.

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

40 { return max_; }

◆ min()

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_; }

◆ nBins()

unsigned npstat::NUHistoAxis::nBins ( ) const
inline

Definition at line 43 of file NUHistoAxis.h.

References nBins_.

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

43 { return nBins_; }

◆ operator!=()

bool npstat::NUHistoAxis::operator!= ( const NUHistoAxis r) const

Definition at line 60 of file NUHistoAxis.cc.

60 { return !(*this == r); }

◆ operator==()

bool npstat::NUHistoAxis::operator== ( const NUHistoAxis r) const

Definition at line 55 of file NUHistoAxis.cc.

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

55  {
56  return min_ == r.min_ && max_ == r.max_ && label_ == r.label_ && nBins_ == r.nBins_ && binEdges_ == r.binEdges_ &&
57  uniform_ == r.uniform_;
58  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108
std::string label_
Definition: NUHistoAxis.h:109

◆ overflowIndex()

unsigned npstat::NUHistoAxis::overflowIndex ( const double  x,
unsigned *  binNum 
) const
inlineprivate

Definition at line 117 of file NUHistoAxis.h.

References binNumber(), max_, min_, mitigatedMETSequence_cff::U, and x.

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

117  {
118  if (x < min_)
119  return 0U;
120  else if (x >= max_)
121  return 2U;
122  else {
123  *binNum = binNumber(x);
124  return 1U;
125  }
126  }
int binNumber(double x) const
Definition: NUHistoAxis.cc:62

◆ read()

NUHistoAxis * npstat::NUHistoAxis::read ( const gs::ClassId &  id,
std::istream &  in 
)
static

Definition at line 142 of file NUHistoAxis.cc.

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

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

142  {
143  static const gs::ClassId current(gs::ClassId::makeId<NUHistoAxis>());
144  current.ensureSameId(id);
145 
146  std::vector<double> binEdges;
148  unsigned char unif;
149  gs::read_pod_vector(in, &binEdges);
150  gs::read_pod(in, &label);
151  gs::read_pod(in, &unif);
152  if (in.fail())
153  throw gs::IOReadFailure(
154  "In npstat::UHistoAxis::read: "
155  "input stream failure");
156  NUHistoAxis* result = new NUHistoAxis(binEdges, label.c_str());
157  result->uniform_ = unif;
158  return result;
159  }
const std::string & label() const
Definition: NUHistoAxis.h:45

◆ rebin()

NUHistoAxis npstat::NUHistoAxis::rebin ( unsigned  newBins) const

Return uniformly rebinned axis

Definition at line 41 of file NUHistoAxis.cc.

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

41  {
42  return NUHistoAxis(newBins, min_, max_, label_.c_str());
43  }
std::string label_
Definition: NUHistoAxis.h:109

◆ rightBinEdge()

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

Return the coordinate of the given bin right edge

Definition at line 53 of file NUHistoAxis.h.

References binEdges_.

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

53 { return binEdges_.at(binNum + 1); }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108

◆ setLabel()

void npstat::NUHistoAxis::setLabel ( const char *  newlabel)
inline

Change the axis label

Definition at line 64 of file NUHistoAxis.h.

References label_.

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

64 { label_ = newlabel ? newlabel : ""; }
std::string label_
Definition: NUHistoAxis.h:109

◆ version()

static unsigned npstat::NUHistoAxis::version ( )
inlinestatic

Definition at line 100 of file NUHistoAxis.h.

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

100 { return 1; }

◆ write()

bool npstat::NUHistoAxis::write ( std::ostream &  of) const

Definition at line 134 of file NUHistoAxis.cc.

References binEdges_, DummyCfis::c, label_, haddnano::of, and uniform_.

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

134  {
135  gs::write_pod_vector(of, binEdges_);
136  gs::write_pod(of, label_);
137  unsigned char c = uniform_;
138  gs::write_pod(of, c);
139  return !of.fail();
140  }
std::vector< double > binEdges_
Definition: NUHistoAxis.h:108
std::string label_
Definition: NUHistoAxis.h:109

Friends And Related Function Documentation

◆ DualHistoAxis

friend class DualHistoAxis
friend

Definition at line 115 of file NUHistoAxis.h.

◆ HistoND

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

Definition at line 114 of file NUHistoAxis.h.

Member Data Documentation

◆ binEdges_

std::vector<double> npstat::NUHistoAxis::binEdges_
private

◆ label_

std::string npstat::NUHistoAxis::label_
private

◆ max_

double npstat::NUHistoAxis::max_
private

Definition at line 107 of file NUHistoAxis.h.

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

◆ min_

double npstat::NUHistoAxis::min_
private

Definition at line 106 of file NUHistoAxis.h.

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

◆ nBins_

unsigned npstat::NUHistoAxis::nBins_
private

Definition at line 110 of file NUHistoAxis.h.

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

◆ uniform_

bool npstat::NUHistoAxis::uniform_
private

Definition at line 111 of file NUHistoAxis.h.

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