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
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

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

40  : coords_(coords),
41  npt_(coords_.size()),
42  useLogSpace_(useLogSpace)
43  {
44  initialize();
45  }
void initialize()
Definition: GridAxis.cc:12
unsigned npt_
Definition: GridAxis.h:129
const std::vector< double > & coords() const
Definition: GridAxis.h:46
std::vector< double > coords_
Definition: GridAxis.h:126
npstat::GridAxis::GridAxis ( const std::vector< double > &  coords,
const char *  label,
bool  useLogSpace = false 
)

Definition at line 47 of file GridAxis.cc.

References initialize().

50  : coords_(coords),
51  label_(label ? label : ""),
52  npt_(coords_.size()),
53  useLogSpace_(useLogSpace)
54  {
55  initialize();
56  }
void initialize()
Definition: GridAxis.cc:12
std::string label_
Definition: GridAxis.h:128
const std::string & label() const
Definition: GridAxis.h:47
unsigned npt_
Definition: GridAxis.h:129
const std::vector< double > & coords() const
Definition: GridAxis.h:46
std::vector< double > coords_
Definition: GridAxis.h:126
npstat::GridAxis::GridAxis ( )
inlineprivate

Definition at line 132 of file GridAxis.h.

Referenced by read().

132 : npt_(0), useLogSpace_(false) {}
unsigned npt_
Definition: GridAxis.h:129

Member Function Documentation

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

Method related to "geners" I/O

Definition at line 115 of file GridAxis.h.

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

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

Definition at line 119 of file GridAxis.h.

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

Definition at line 85 of file GridAxis.h.

References coords_.

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

86  {return coords_.at(i);}
int i
Definition: DBlmapReader.cc:9
std::vector< double > coords_
Definition: GridAxis.h:126
const std::vector<double>& npstat::GridAxis::coords ( ) const
inline

Basic accessor returning a parameter provided in the constructor

Definition at line 46 of file GridAxis.h.

References coords_.

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

46 {return coords_;}
std::vector< double > coords_
Definition: GridAxis.h:126
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 EnergyCorrector::c, coords_, prof2calltree::l, create_public_lumi_plots::log, logs_, npt_, useLogSpace_, w, and x.

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

59  {
60  if (useLogSpace_)
61  if (x <= 0.0) throw npstat::NpstatDomainError(
62  "In GridAxis::getInterval: argument must be positive");
63  const double* c = &coords_[0];
64  if (x <= c[0])
65  return std::pair<unsigned,double>(0U, 1.0);
66  else if (x >= c[npt_ - 1U])
67  return std::pair<unsigned,double>(npt_ - 2U, 0.0);
68  else
69  {
70  unsigned ibnd = lower_bound
71  (coords_.begin(), coords_.end(), x) - coords_.begin();
72  --ibnd;
73  if (useLogSpace_)
74  {
75  const double* l = &logs_[0];
76  const double w = 1.0 - (log(x) - l[ibnd])/
77  (l[ibnd + 1U] - l[ibnd]);
78  return std::pair<unsigned,double>(ibnd, w);
79  }
80  else
81  {
82  const double w = 1.0 - (x - c[ibnd])/(c[ibnd + 1U] - c[ibnd]);
83  return std::pair<unsigned,double>(ibnd, w);
84  }
85  }
86  }
const double w
Definition: UKUtility.cc:23
unsigned npt_
Definition: GridAxis.h:129
std::vector< double > logs_
Definition: GridAxis.h:127
Definition: DDAxes.h:10
std::vector< double > coords_
Definition: GridAxis.h:126
void npstat::GridAxis::initialize ( )
private

Definition at line 12 of file GridAxis.cc.

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

Referenced by GridAxis().

13  {
14  if (npt_ <= 1U) throw npstat::NpstatInvalidArgument(
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  {
21  logs_.reserve(npt_);
22  for (unsigned i=0; i<npt_; ++i)
23  {
24  if (c[i] <= 0.0) throw npstat::NpstatInvalidArgument(
25  "In npstat::GridAxis::initialize: in log space"
26  "all coordinates must be positive");
27  logs_.push_back(log(c[i]));
28  }
29  }
30 
31  // Can't have duplicate coordinates
32  for (unsigned i=1; i<npt_; ++i)
33  if (c[i] <= c[i - 1U]) throw npstat::NpstatInvalidArgument(
34  "In npstat::GridAxis::initialize: "
35  "all coordinates must be distinct");
36  }
int i
Definition: DBlmapReader.cc:9
unsigned npt_
Definition: GridAxis.h:129
std::vector< double > logs_
Definition: GridAxis.h:127
std::vector< double > coords_
Definition: GridAxis.h:126
double npstat::GridAxis::intervalWidth ( const unsigned  i = 0) const
inline

Definition at line 92 of file GridAxis.h.

References coords_, and i.

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

93  {return coords_.at(i+1) - coords_.at(i);}
int i
Definition: DBlmapReader.cc:9
std::vector< double > coords_
Definition: GridAxis.h:126
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_.

182  {
183  if (!(useLogSpace_ == r.useLogSpace_ &&
184  label_ == r.label_))
185  return false;
186  const unsigned long n = coords_.size();
187  if (n != r.coords_.size())
188  return false;
189  for (unsigned long i=0; i<n; ++i)
190  if (!closeWithinTolerance(coords_[i], r.coords_[i], tol))
191  return false;
192  return true;
193  }
std::string label_
Definition: GridAxis.h:128
int i
Definition: DBlmapReader.cc:9
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
std::vector< double > coords_
Definition: GridAxis.h:126
bool npstat::GridAxis::isUniform ( ) const
inline

Definition at line 90 of file GridAxis.h.

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

Definition at line 47 of file GridAxis.h.

References label_.

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

47 {return label_;}
std::string label_
Definition: GridAxis.h:128
double npstat::GridAxis::length ( ) const
inline

Definition at line 89 of file GridAxis.h.

References coords_.

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

89 {return coords_.back() - coords_.front();}
std::vector< double > coords_
Definition: GridAxis.h:126
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 EnergyCorrector::c, coords_, prof2calltree::l, create_public_lumi_plots::log, logs_, npt_, useLogSpace_, w, and x.

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

89  {
90  if (useLogSpace_)
91  if (x <= 0.0) throw npstat::NpstatDomainError(
92  "In GridAxis::linearInterval: argument must be positive");
93  const double* c = &coords_[0];
94  if (x <= c[0])
95  {
96  if (useLogSpace_)
97  {
98  const double* l = &logs_[0];
99  const double bw = l[1] - l[0];
100  return std::pair<unsigned,double>(0U, 1.0 - (log(x) - l[0])/bw);
101  }
102  else
103  {
104  const double bw = c[1] - c[0];
105  return std::pair<unsigned,double>(0U, 1.0 - (x - c[0])/bw);
106  }
107  }
108  else if (x >= c[npt_ - 1U])
109  {
110  if (useLogSpace_)
111  {
112  const double* l = &logs_[0];
113  const double bw = l[npt_ - 1U] - l[npt_ - 2U];
114  return std::pair<unsigned,double>(
115  npt_ - 2U, (l[npt_ - 1U] - log(x))/bw);
116  }
117  else
118  {
119  const double bw = c[npt_ - 1U] - c[npt_ - 2U];
120  return std::pair<unsigned,double>(
121  npt_ - 2U, (c[npt_ - 1U] - x)/bw);
122  }
123  }
124  else
125  {
126  unsigned ibnd = lower_bound
127  (coords_.begin(), coords_.end(), x) - coords_.begin();
128  --ibnd;
129  if (useLogSpace_)
130  {
131  const double* l = &logs_[0];
132  const double w = 1.0 - (log(x) - l[ibnd])/
133  (l[ibnd + 1U] - l[ibnd]);
134  return std::pair<unsigned,double>(ibnd, w);
135  }
136  else
137  {
138  const double w = 1.0 - (x - c[ibnd])/(c[ibnd + 1U] - c[ibnd]);
139  return std::pair<unsigned,double>(ibnd, w);
140  }
141  }
142  }
const double w
Definition: UKUtility.cc:23
unsigned npt_
Definition: GridAxis.h:129
std::vector< double > logs_
Definition: GridAxis.h:127
Definition: DDAxes.h:10
std::vector< double > coords_
Definition: GridAxis.h:126
double npstat::GridAxis::max ( ) const
inline

Definition at line 88 of file GridAxis.h.

References coords_.

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

88 {return coords_.back();}
std::vector< double > coords_
Definition: GridAxis.h:126
double npstat::GridAxis::min ( ) const
inline

Definition at line 87 of file GridAxis.h.

References coords_.

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

87 {return coords_.front();}
std::vector< double > coords_
Definition: GridAxis.h:126
unsigned npstat::GridAxis::nCoords ( ) const
inline

Convenience accessor

Definition at line 84 of file GridAxis.h.

References npt_.

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

84 {return npt_;}
unsigned npt_
Definition: GridAxis.h:129
unsigned npstat::GridAxis::nIntervals ( ) const
inline

Definition at line 91 of file GridAxis.h.

References coords_.

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

91 {return coords_.size() - 1;}
std::vector< double > coords_
Definition: GridAxis.h:126
bool npstat::GridAxis::operator!= ( const GridAxis r) const
inline

Logical negation of operator==

Definition at line 100 of file GridAxis.h.

References alignCSCRings::r.

101  {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_.

175  {
176  return useLogSpace_ == r.useLogSpace_ &&
177  coords_ == r.coords_ &&
178  label_ == r.label_;
179  }
std::string label_
Definition: GridAxis.h:128
std::vector< double > coords_
Definition: GridAxis.h:126
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().

155  {
156  static const gs::ClassId current(gs::ClassId::makeId<GridAxis>());
157  current.ensureSameId(id);
158 
159  std::vector<double> coords;
160  gs::read_pod_vector(in, &coords);
161 
163  gs::read_pod(in, &label);
164 
165  bool useLogSpace;
166  gs::read_pod(in, &useLogSpace);
167 
168  if (in.fail())
169  throw gs::IOReadFailure("In npstat::GridAxis::read: "
170  "input stream failure");
171  return new GridAxis(coords, label.c_str(), useLogSpace);
172  }
const std::string & label() const
Definition: GridAxis.h:47
const std::vector< double > & coords() const
Definition: GridAxis.h:46
void npstat::GridAxis::setLabel ( const char *  newlabel)
inline

Modify the axis label

Definition at line 110 of file GridAxis.h.

References label_.

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

111  {label_ = newlabel ? newlabel : "";}
std::string label_
Definition: GridAxis.h:128
bool npstat::GridAxis::usesLogSpace ( ) const
inline

Definition at line 48 of file GridAxis.h.

References useLogSpace_.

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

48 {return useLogSpace_;}
static unsigned npstat::GridAxis::version ( )
inlinestatic

Definition at line 120 of file GridAxis.h.

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

145  {
146  // It is unlikely that this object will be written in isolation.
147  // So, do not bother with too many checks.
148  gs::write_pod_vector(of, coords_);
149  gs::write_pod(of, label_);
150  gs::write_pod(of, useLogSpace_);
151  return !of.fail();
152  }
std::string label_
Definition: GridAxis.h:128
std::vector< double > coords_
Definition: GridAxis.h:126

Member Data Documentation

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

Definition at line 127 of file GridAxis.h.

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

unsigned npstat::GridAxis::npt_
private

Definition at line 129 of file GridAxis.h.

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

bool npstat::GridAxis::useLogSpace_
private