guide_custom_2d_axis.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 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_2d_axis
  7. #include <boost/histogram.hpp>
  8. #include <cassert>
  9. int main() {
  10. using namespace boost::histogram;
  11. // axis which returns 1 if the input falls inside the unit circle and zero otherwise
  12. struct circle_axis {
  13. // accepts a 2D point in form of a std::tuple
  14. axis::index_type index(const std::tuple<double, double>& point) const {
  15. const auto x = std::get<0>(point);
  16. const auto y = std::get<1>(point);
  17. return x * x + y * y <= 1.0;
  18. }
  19. axis::index_type size() const { return 2; }
  20. };
  21. auto h1 = make_histogram(circle_axis());
  22. // fill looks normal for a histogram which has only one Nd-axis
  23. h1(0, 0); // in
  24. h1(0, -1); // in
  25. h1(0, 1); // in
  26. h1(-1, 0); // in
  27. h1(1, 0); // in
  28. h1(1, 1); // out
  29. h1(-1, -1); // out
  30. // 2D histogram, but only 1D index
  31. assert(h1.at(0) == 2); // out
  32. assert(h1.at(1) == 5); // in
  33. // other axes can be combined with a Nd-axis
  34. auto h2 = make_histogram(circle_axis(), axis::category<std::string>({"red", "blue"}));
  35. // now we need to pass arguments for Nd-axis explicitly as std::tuple
  36. h2(std::make_tuple(0, 0), "red");
  37. h2(std::make_tuple(1, 1), "blue");
  38. // 3D histogram, but only 2D index
  39. assert(h2.at(0, 0) == 0); // out, red
  40. assert(h2.at(0, 1) == 1); // out, blue
  41. assert(h2.at(1, 0) == 1); // in, red
  42. assert(h2.at(1, 1) == 0); // in, blue
  43. }
  44. //]