Binning.h
Go to the documentation of this file.
1 #ifndef WIRECELLUTIL_BINNING_H
2 #define WIRECELLUTIL_BINNING_H
3 
4 #include <map> // for std::pair
5 #include <cmath>
6 #include <iostream> // for ostream
7 
8 namespace WireCell {
9 
10  /** A binning is a uniform discretization of a linear space.
11 
12  This class largely provides methods that give semantic labels
13  to various calculations related to a binned region.
14  */
15  class Binning
16  {
17  int m_nbins;
19 
20  public:
21  /** Create a binning
22  \param nbins gives the number of uniform, discrete separation between bounds.
23  \param minval gives the lower bound of the linear space (low edge of bin 0)
24  \param maxval gives the upper bound of the linear space (high edge of bin nbins-1)
25  */
26  Binning(int nbins, double minval, double maxval)
27  : m_nbins(0), m_minval(0), m_maxval(0), m_binsize(0)
28  { set(nbins, minval, maxval); }
30  : m_nbins(0), m_minval(0), m_maxval(0), m_binsize(0)
31  { }
32 
33  // Post constructor setting
34  void set(int nbins, double minval, double maxval) {
35  m_nbins = nbins;
36  m_minval = minval;
37  m_maxval = maxval;
38  m_binsize = ((maxval-minval)/nbins);
39  }
40 
41  // Access given number of bins.
42  int nbins() const {
43  return m_nbins;
44  }
45 
46  // Access given minimum range of binning.
47  double min() const {
48  return m_minval;
49  }
50 
51  // Access given maximum range of binning.
52  double max() const {
53  return m_maxval;
54  }
55 
56  /// Return the max-min
57  double span() const {
58  return m_maxval - m_minval;
59  }
60 
61  // Binning as a range.
62  std::pair<double, double> range() const {
63  return std::make_pair(m_minval, m_maxval);
64  }
65 
66  // Return half open range of bin indices or alternatively
67  // fully closed range of edge indices.
68  std::pair<int, int> irange() const {
69  return std::make_pair(0, m_nbins);
70  }
71 
72  // Access resulting bin size..
73  double binsize() const {
74  return m_binsize;
75  }
76 
77  /// Return the bin containing value. If val is in range,
78  /// return value is [0,nbins-1] but no range checking is
79  /// performed.
80  int bin(double val) const {
81  return int((val-m_minval)/m_binsize);
82  }
83 
84  /// Return the center value of given bin. Range checking is
85  /// not done.
86  double center(int ind) const {
87  return m_minval + (ind+0.5)*m_binsize;
88  }
89 
90  /// Return the edge, nominally in [0,nbins] closest to the
91  /// given value. Range checking is not done so returned edge
92  /// may be outside of range.
93  int edge_index(double val) const {
94  return int(round((val-m_minval)/m_binsize));
95  }
96 
97  /// Return the position of the given bin edge. Range checking
98  /// is not done.
99  double edge(int ind) const {
100  return m_minval + ind*m_binsize;
101  }
102 
103  /// Return true value is in range. Range is considered half
104  /// open. Ig, edge(nbins) is not inside range.
105  bool inside(double val) const {
106  return m_minval <= val && val < m_maxval;
107  }
108 
109  /// Return true if bin is in bounds.
110  bool inbounds(int bin) const {
111  return 0 <= bin && bin < m_nbins;
112  }
113 
114  /// Return half-open bin range which covers the range of
115  /// values. Bounds are forced to return values in [0,nbins].
116  std::pair<int,int> sample_bin_range(double minval, double maxval) const {
117  return std::make_pair(std::max(bin(minval), 0),
118  std::min(bin(maxval)+1, m_nbins));
119  }
120 
121  };
122 
123  inline
124  std::ostream& operator<<(std::ostream& os, const WireCell::Binning& bins)
125  {
126  os << bins.nbins() << "@[" << bins.min() << "," << bins.max() << "]";
127  return os;
128  }
129 
130 } // WireCell
131 
132 
133 #endif /* WIRECELLUTIL_BINNING_H */
int edge_index(double val) const
Definition: Binning.h:93
double m_maxval
Definition: Binning.h:18
double center(int ind) const
Definition: Binning.h:86
Binning(int nbins, double minval, double maxval)
Definition: Binning.h:26
double span() const
Return the max-min.
Definition: Binning.h:57
double m_minval
Definition: Binning.h:18
double binsize() const
Definition: Binning.h:73
double max() const
Definition: Binning.h:52
std::pair< int, int > irange() const
Definition: Binning.h:68
int bin(double val) const
Definition: Binning.h:80
double min() const
Definition: Binning.h:47
static int max(int a, int b)
Definition: Main.h:22
bool inside(double val) const
Definition: Binning.h:105
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
std::ostream & operator<<(std::ostream &os, const WireCell::WirePlaneId &wpid)
Definition: WirePlaneId.cxx:83
bool inbounds(int bin) const
Return true if bin is in bounds.
Definition: Binning.h:110
double edge(int ind) const
Definition: Binning.h:99
std::pair< int, int > sample_bin_range(double minval, double maxval) const
Definition: Binning.h:116
int nbins() const
Definition: Binning.h:42
std::pair< double, double > range() const
Definition: Binning.h:62
double m_binsize
Definition: Binning.h:18