CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
npstat::GridAxis Class Reference

#include <GridAxis.h>

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

◆ GridAxis() [1/3]

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

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().

39  : coords_(coords), npt_(coords_.size()), useLogSpace_(useLogSpace) {
40  initialize();
41  }
void initialize()
Definition: GridAxis.cc:12
unsigned npt_
Definition: GridAxis.h:122
const std::vector< double > & coords() const
Definition: GridAxis.h:43
std::vector< double > coords_
Definition: GridAxis.h:119

◆ GridAxis() [2/3]

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

Definition at line 43 of file GridAxis.cc.

References initialize().

44  : coords_(coords), label_(label ? label : ""), npt_(coords_.size()), useLogSpace_(useLogSpace) {
45  initialize();
46  }
void initialize()
Definition: GridAxis.cc:12
std::string label_
Definition: GridAxis.h:121
unsigned npt_
Definition: GridAxis.h:122
const std::string & label() const
Definition: GridAxis.h:44
const std::vector< double > & coords() const
Definition: GridAxis.h:43
std::vector< double > coords_
Definition: GridAxis.h:119

◆ GridAxis() [3/3]

npstat::GridAxis::GridAxis ( )
inlineprivate

Definition at line 125 of file GridAxis.h.

Referenced by read().

125 : npt_(0), useLogSpace_(false) {}
unsigned npt_
Definition: GridAxis.h:122

Member Function Documentation

◆ classId()

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

Method related to "geners" I/O

Definition at line 108 of file GridAxis.h.

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

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

◆ classname()

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

Definition at line 112 of file GridAxis.h.

112 { return "npstat::GridAxis"; }

◆ coordinate()

double npstat::GridAxis::coordinate ( const unsigned  i) const
inline

Definition at line 82 of file GridAxis.h.

References coords_, and mps_fire::i.

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

82 { return coords_.at(i); }
std::vector< double > coords_
Definition: GridAxis.h:119

◆ coords()

const std::vector<double>& npstat::GridAxis::coords ( ) const
inline

Basic accessor returning a parameter provided in the constructor

Definition at line 43 of file GridAxis.h.

References coords_.

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

43 { return coords_; }
std::vector< double > coords_
Definition: GridAxis.h:119

◆ getInterval()

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 48 of file GridAxis.cc.

References c, coords_, MainPageGenerator::l, dqm-mbProfile::log, logs_, pfDeepBoostedJetPreprocessParams_cfi::lower_bound, npt_, mitigatedMETSequence_cff::U, useLogSpace_, w(), and x.

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

48  {
49  if (useLogSpace_)
50  if (x <= 0.0)
51  throw npstat::NpstatDomainError("In GridAxis::getInterval: argument must be positive");
52  const double* c = &coords_[0];
53  if (x <= c[0])
54  return std::pair<unsigned, double>(0U, 1.0);
55  else if (x >= c[npt_ - 1U])
56  return std::pair<unsigned, double>(npt_ - 2U, 0.0);
57  else {
58  unsigned ibnd = lower_bound(coords_.begin(), coords_.end(), x) - coords_.begin();
59  --ibnd;
60  if (useLogSpace_) {
61  const double* l = &logs_[0];
62  const double w = 1.0 - (log(x) - l[ibnd]) / (l[ibnd + 1U] - l[ibnd]);
63  return std::pair<unsigned, double>(ibnd, w);
64  } else {
65  const double w = 1.0 - (x - c[ibnd]) / (c[ibnd + 1U] - c[ibnd]);
66  return std::pair<unsigned, double>(ibnd, w);
67  }
68  }
69  }
T w() const
unsigned npt_
Definition: GridAxis.h:122
std::vector< double > logs_
Definition: GridAxis.h:120
std::vector< double > coords_
Definition: GridAxis.h:119

◆ initialize()

void npstat::GridAxis::initialize ( void  )
private

Definition at line 12 of file GridAxis.cc.

References c, coords_, mps_fire::i, dqm-mbProfile::log, logs_, npt_, jetUpdater_cfi::sort, mitigatedMETSequence_cff::U, and useLogSpace_.

Referenced by GridAxis().

12  {
13  if (npt_ <= 1U)
15  "In npstat::GridAxis::initialize: insufficient number "
16  "of coordinates (at least 2 are required)");
17  std::sort(coords_.begin(), coords_.end());
18  const double* c = &coords_[0];
19  if (useLogSpace_) {
20  logs_.reserve(npt_);
21  for (unsigned i = 0; i < npt_; ++i) {
22  if (c[i] <= 0.0)
24  "In npstat::GridAxis::initialize: in log space"
25  "all coordinates must be positive");
26  logs_.push_back(log(c[i]));
27  }
28  }
29 
30  // Can't have duplicate coordinates
31  for (unsigned i = 1; i < npt_; ++i)
32  if (c[i] <= c[i - 1U])
34  "In npstat::GridAxis::initialize: "
35  "all coordinates must be distinct");
36  }
unsigned npt_
Definition: GridAxis.h:122
std::vector< double > logs_
Definition: GridAxis.h:120
std::vector< double > coords_
Definition: GridAxis.h:119

◆ intervalWidth()

double npstat::GridAxis::intervalWidth ( const unsigned  i = 0) const
inline

Definition at line 88 of file GridAxis.h.

References coords_, and mps_fire::i.

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

88 { return coords_.at(i + 1) - coords_.at(i); }
std::vector< double > coords_
Definition: GridAxis.h:119

◆ isClose()

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 141 of file GridAxis.cc.

References npstat::closeWithinTolerance(), coords_, mps_fire::i, label_, dqmiodumpmetadata::n, alignCSCRings::r, and useLogSpace_.

141  {
142  if (!(useLogSpace_ == r.useLogSpace_ && label_ == r.label_))
143  return false;
144  const unsigned long n = coords_.size();
145  if (n != r.coords_.size())
146  return false;
147  for (unsigned long i = 0; i < n; ++i)
148  if (!closeWithinTolerance(coords_[i], r.coords_[i], tol))
149  return false;
150  return true;
151  }
std::string label_
Definition: GridAxis.h:121
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
std::vector< double > coords_
Definition: GridAxis.h:119

◆ isUniform()

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

Definition at line 86 of file GridAxis.h.

86 { return false; }

◆ label()

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

Definition at line 44 of file GridAxis.h.

References label_.

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

44 { return label_; }
std::string label_
Definition: GridAxis.h:121

◆ length()

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

Definition at line 85 of file GridAxis.h.

References coords_.

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

85 { return coords_.back() - coords_.front(); }
std::vector< double > coords_
Definition: GridAxis.h:119

◆ linearInterval()

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 71 of file GridAxis.cc.

References c, coords_, MainPageGenerator::l, dqm-mbProfile::log, logs_, pfDeepBoostedJetPreprocessParams_cfi::lower_bound, npt_, mitigatedMETSequence_cff::U, useLogSpace_, w(), and x.

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

71  {
72  if (useLogSpace_)
73  if (x <= 0.0)
74  throw npstat::NpstatDomainError("In GridAxis::linearInterval: argument must be positive");
75  const double* c = &coords_[0];
76  if (x <= c[0]) {
77  if (useLogSpace_) {
78  const double* l = &logs_[0];
79  const double bw = l[1] - l[0];
80  return std::pair<unsigned, double>(0U, 1.0 - (log(x) - l[0]) / bw);
81  } else {
82  const double bw = c[1] - c[0];
83  return std::pair<unsigned, double>(0U, 1.0 - (x - c[0]) / bw);
84  }
85  } else if (x >= c[npt_ - 1U]) {
86  if (useLogSpace_) {
87  const double* l = &logs_[0];
88  const double bw = l[npt_ - 1U] - l[npt_ - 2U];
89  return std::pair<unsigned, double>(npt_ - 2U, (l[npt_ - 1U] - log(x)) / bw);
90  } else {
91  const double bw = c[npt_ - 1U] - c[npt_ - 2U];
92  return std::pair<unsigned, double>(npt_ - 2U, (c[npt_ - 1U] - x) / bw);
93  }
94  } else {
95  unsigned ibnd = lower_bound(coords_.begin(), coords_.end(), x) - coords_.begin();
96  --ibnd;
97  if (useLogSpace_) {
98  const double* l = &logs_[0];
99  const double w = 1.0 - (log(x) - l[ibnd]) / (l[ibnd + 1U] - l[ibnd]);
100  return std::pair<unsigned, double>(ibnd, w);
101  } else {
102  const double w = 1.0 - (x - c[ibnd]) / (c[ibnd + 1U] - c[ibnd]);
103  return std::pair<unsigned, double>(ibnd, w);
104  }
105  }
106  }
T w() const
unsigned npt_
Definition: GridAxis.h:122
std::vector< double > logs_
Definition: GridAxis.h:120
std::vector< double > coords_
Definition: GridAxis.h:119

◆ max()

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

Definition at line 84 of file GridAxis.h.

References coords_.

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

84 { return coords_.back(); }
std::vector< double > coords_
Definition: GridAxis.h:119

◆ min()

double npstat::GridAxis::min ( ) const
inline

Definition at line 83 of file GridAxis.h.

References coords_.

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

83 { return coords_.front(); }
std::vector< double > coords_
Definition: GridAxis.h:119

◆ nCoords()

unsigned npstat::GridAxis::nCoords ( ) const
inline

Convenience accessor

Definition at line 81 of file GridAxis.h.

References npt_.

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

81 { return npt_; }
unsigned npt_
Definition: GridAxis.h:122

◆ nIntervals()

unsigned npstat::GridAxis::nIntervals ( ) const
inline

Definition at line 87 of file GridAxis.h.

References coords_.

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

87 { return coords_.size() - 1; }
std::vector< double > coords_
Definition: GridAxis.h:119

◆ operator!=()

bool npstat::GridAxis::operator!= ( const GridAxis r) const
inline

Logical negation of operator==

Definition at line 95 of file GridAxis.h.

References alignCSCRings::r.

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

◆ operator==()

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

Compare two grids for equality

Definition at line 137 of file GridAxis.cc.

References coords_, label_, alignCSCRings::r, and useLogSpace_.

137  {
138  return useLogSpace_ == r.useLogSpace_ && coords_ == r.coords_ && label_ == r.label_;
139  }
std::string label_
Definition: GridAxis.h:121
std::vector< double > coords_
Definition: GridAxis.h:119

◆ read()

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

Definition at line 117 of file GridAxis.cc.

References coords(), GridAxis(), recoMuon::in, label(), and AlCaHLTBitMon_QueryRunRegistry::string.

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

117  {
118  static const gs::ClassId current(gs::ClassId::makeId<GridAxis>());
119  current.ensureSameId(id);
120 
121  std::vector<double> coords;
122  gs::read_pod_vector(in, &coords);
123 
125  gs::read_pod(in, &label);
126 
127  bool useLogSpace;
128  gs::read_pod(in, &useLogSpace);
129 
130  if (in.fail())
131  throw gs::IOReadFailure(
132  "In npstat::GridAxis::read: "
133  "input stream failure");
134  return new GridAxis(coords, label.c_str(), useLogSpace);
135  }
const std::string & label() const
Definition: GridAxis.h:44
const std::vector< double > & coords() const
Definition: GridAxis.h:43

◆ setLabel()

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

Modify the axis label

Definition at line 104 of file GridAxis.h.

References label_.

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

104 { label_ = newlabel ? newlabel : ""; }
std::string label_
Definition: GridAxis.h:121

◆ usesLogSpace()

bool npstat::GridAxis::usesLogSpace ( ) const
inline

Definition at line 45 of file GridAxis.h.

References useLogSpace_.

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

45 { return useLogSpace_; }

◆ version()

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

Definition at line 113 of file GridAxis.h.

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

113 { return 2; }

◆ write()

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

Definition at line 108 of file GridAxis.cc.

References coords_, label_, haddnano::of, and useLogSpace_.

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

108  {
109  // It is unlikely that this object will be written in isolation.
110  // So, do not bother with too many checks.
111  gs::write_pod_vector(of, coords_);
112  gs::write_pod(of, label_);
113  gs::write_pod(of, useLogSpace_);
114  return !of.fail();
115  }
std::string label_
Definition: GridAxis.h:121
std::vector< double > coords_
Definition: GridAxis.h:119

Member Data Documentation

◆ coords_

std::vector<double> npstat::GridAxis::coords_
private

◆ label_

std::string npstat::GridAxis::label_
private

◆ logs_

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

Definition at line 120 of file GridAxis.h.

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

◆ npt_

unsigned npstat::GridAxis::npt_
private

Definition at line 122 of file GridAxis.h.

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

◆ useLogSpace_

bool npstat::GridAxis::useLogSpace_
private