guide_custom_minimal_axis.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2015-2018 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //[ guide_custom_minimal_axis
  7. #include <boost/histogram.hpp>
  8. #include <cassert>
  9. int main() {
  10. using namespace boost::histogram;
  11. // stateless axis which returns 1 if the input is even and 0 otherwise
  12. struct even_odd_axis {
  13. axis::index_type index(int x) const { return x % 2; }
  14. axis::index_type size() const { return 2; }
  15. };
  16. // threshold axis which returns 1 if the input is above threshold
  17. struct threshold_axis {
  18. threshold_axis(double x) : thr(x) {}
  19. axis::index_type index(double x) const { return x >= thr; }
  20. axis::index_type size() const { return 2; }
  21. double thr;
  22. };
  23. auto h = make_histogram(even_odd_axis(), threshold_axis(3.0));
  24. h(0, 2.0);
  25. h(1, 4.0);
  26. h(2, 4.0);
  27. assert(h.at(0, 0) == 1); // even, below threshold
  28. assert(h.at(0, 1) == 1); // even, above threshold
  29. assert(h.at(1, 0) == 0); // odd, below threshold
  30. assert(h.at(1, 1) == 1); // odd, above threshold
  31. }
  32. //]