JayBeams  0.1
Another project to have fun coding.
explicit_cuts_binning.hpp
Go to the documentation of this file.
1 #ifndef jb_explicit_cuts_binning_hpp
2 #define jb_explicit_cuts_binning_hpp
3 
5 
6 #include <algorithm>
7 #include <limits>
8 #include <stdexcept>
9 #include <vector>
10 
11 namespace jb {
12 
13 /**
14  * A histogram binning_strategy for integer numbers with user defined cuts.
15  *
16  * This class defines histogram bins at cutting points explicity
17  * defined by the user. For example, cutting at [0, 1, 10, 100] would
18  * create 4 buckets, from 0 to 1, from 1 to 10, from 10 to 100.
19  * Samples below the minimum or maximum bucket are recorded as
20  * underflows or overflows, respectively.
21  *
22  * Users can then create all kinds of interesting binning just by
23  * providing the right initial values, for example:
24  * [0, 1, 2, ..., 9, 10, 20, ..., 90, 100, ... , 900, 1000]
25  * offers a good tradeoff between accuracy and memory usage for tail
26  * heavy distributions.
27  *
28  * Insertion takes O(log(n)) on the number of cuts.
29  *
30  * @tparam sample_type_t the type of samples, should be an integer type.
31  */
32 template <typename sample_type_t>
34 public:
35  /// type traits as required by @ref binning_strategy_concept
36  typedef sample_type_t sample_type;
37 
38  /**
39  * Constructor based on an initializer list.
40  */
41  explicit explicit_cuts_binning(std::initializer_list<sample_type> const& il)
42  : explicit_cuts_binning(il.begin(), il.end()) {
43  }
44 
45  /**
46  * Constructor based on an iterator range.
47  */
48  template <typename iterator_t>
49  explicit_cuts_binning(iterator_t begin, iterator_t end)
50  : cuts_(begin, end) {
51  if (cuts_.size() < 2) {
52  throw std::invalid_argument(
53  "explicit_cuts_binning requires at least 2 cuts");
54  }
55  if (not std::is_sorted(cuts_.begin(), cuts_.end())) {
56  throw std::invalid_argument(
57  "explicit_cuts_binning requires a sorted set of cuts");
58  }
59  if (cuts_.end() != std::adjacent_find(cuts_.begin(), cuts_.end())) {
60  throw std::invalid_argument(
61  "explicit_cuts_binning requires unique elements");
62  }
63  }
64 
65  //@{
66  /**
67  * @name Implement binning_strategy_concept interface.
68  *
69  * Please see @ref binning_strategy_concept for detailed
70  * documentation of each member function.
71  */
72  sample_type histogram_min() const {
73  return cuts_.front();
74  }
75  sample_type histogram_max() const {
76  return cuts_.back();
77  }
78  sample_type theoretical_min() const {
79  return std::numeric_limits<sample_type>::min();
80  }
81  sample_type theoretical_max() const {
82  return std::numeric_limits<sample_type>::max();
83  }
84  std::size_t sample2bin(sample_type t) const {
85  auto it = std::upper_bound(cuts_.begin(), cuts_.end(), t);
86  return std::distance(cuts_.begin(), it) - 1;
87  }
88  sample_type bin2sample(std::size_t i) const {
89  return cuts_[i];
90  }
91  sample_type interpolate(
92  sample_type x_a, sample_type x_b, double y_a, double s, double q) const {
93  return histogram_binning_linear_interpolation(x_a, x_b, y_a, s, q);
94  }
95  //@}
96 
97 private:
98  std::vector<sample_type> cuts_;
99 };
100 
101 } // namespace jb
102 
103 #endif // jb_explicit_cuts_binning_hpp
explicit_cuts_binning(iterator_t begin, iterator_t end)
Constructor based on an iterator range.
sample_type theoretical_min() const
sample_type interpolate(sample_type x_a, sample_type x_b, double y_a, double s, double q) const
sample_type histogram_binning_linear_interpolation(sample_type x_a, sample_type x_b, double y_a, double s, double q)
Convenience function to implement the binning_strategy_concept.
sample_type bin2sample(std::size_t i) const
std::size_t sample2bin(sample_type t) const
sample_type_t sample_type
type traits as required by binning_strategy_concept
sample_type theoretical_max() const
std::vector< sample_type > cuts_
A histogram binning_strategy for integer numbers with user defined cuts.
explicit_cuts_binning(std::initializer_list< sample_type > const &il)
Constructor based on an initializer list.
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7