guide_axis_basic_demo.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2019 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_axis_basic_demo
  7. #include <boost/histogram/axis.hpp>
  8. #include <limits>
  9. int main() {
  10. using namespace boost::histogram;
  11. // make a regular axis with 10 bins over interval from 1.5 to 2.5
  12. auto r = axis::regular<>{10, 1.5, 2.5};
  13. // `<>` is needed in C++14 because the axis is templated,
  14. // in C++17 you can do: auto r = axis::regular{10, 1.5, 2.5};
  15. assert(r.size() == 10);
  16. // alternatively, you can define the step size with the `step` marker
  17. auto r_step = axis::regular<>{axis::step(0.1), 1.5, 2.5};
  18. assert(r_step == r);
  19. // histogram uses the `index` method to convert values to indices
  20. // note: intervals of builtin axis types are always semi-open [a, b)
  21. assert(r.index(1.5) == 0);
  22. assert(r.index(1.6) == 1);
  23. assert(r.index(2.4) == 9);
  24. // index for a value below the start of the axis is always -1
  25. assert(r.index(1.0) == -1);
  26. assert(r.index(-std::numeric_limits<double>::infinity()) == -1);
  27. // index for a value below the above the end of the axis is always `size()`
  28. assert(r.index(3.0) == 10);
  29. assert(r.index(std::numeric_limits<double>::infinity()) == 10);
  30. // index for not-a-number is also `size()` by convention
  31. assert(r.index(std::numeric_limits<double>::quiet_NaN()) == 10);
  32. // make a variable axis with 3 bins [-1.5, 0.1), [0.1, 0.3), [0.3, 10)
  33. auto v = axis::variable<>{-1.5, 0.1, 0.3, 10.};
  34. assert(v.index(-2.0) == -1);
  35. assert(v.index(-1.5) == 0);
  36. assert(v.index(0.1) == 1);
  37. assert(v.index(0.3) == 2);
  38. assert(v.index(10) == 3);
  39. assert(v.index(20) == 3);
  40. // make an integer axis with 3 bins at -1, 0, 1
  41. auto i = axis::integer<>{-1, 2};
  42. assert(i.index(-2) == -1);
  43. assert(i.index(-1) == 0);
  44. assert(i.index(0) == 1);
  45. assert(i.index(1) == 2);
  46. assert(i.index(2) == 3);
  47. // make an integer axis called "foo"
  48. auto i_with_label = axis::integer<>{-1, 2, "foo"};
  49. // all builtin axis types allow you to pass some optional metadata as the last
  50. // argument in the constructor; a string by default, but can be any copyable type
  51. // two axis do not compare equal if they differ in their metadata
  52. assert(i != i_with_label);
  53. // integer axis also work well with unscoped enums
  54. enum { red, blue };
  55. auto i_for_enum = axis::integer<>{red, blue + 1};
  56. assert(i_for_enum.index(red) == 0);
  57. assert(i_for_enum.index(blue) == 1);
  58. // make a category axis from a scoped enum and/or if the identifiers are not consecutive
  59. enum class Bar { red = 12, blue = 6 };
  60. auto c = axis::category<Bar>{Bar::red, Bar::blue};
  61. assert(c.index(Bar::red) == 0);
  62. assert(c.index(Bar::blue) == 1);
  63. // c.index(12) is a compile-time error, since the argument must be of type `Bar`
  64. // category axis can be created for any copyable and equal-comparable type
  65. auto c_str = axis::category<std::string>{"red", "blue"};
  66. assert(c_str.index("red") == 0);
  67. assert(c_str.index("blue") == 1);
  68. }
  69. //]