guide_axis_growing.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // clang-format off
  7. //[ guide_axis_growing
  8. #include <boost/histogram.hpp>
  9. #include <cassert>
  10. #include <iostream>
  11. int main() {
  12. using namespace boost::histogram;
  13. // make a growing regular axis
  14. // - it grows new bins with its constant bin width until the value is covered
  15. auto h1 = make_histogram(axis::regular<double,
  16. use_default,
  17. use_default,
  18. axis::option::growth_t>{2, 0., 1.});
  19. // nothing special happens here
  20. h1(0.1);
  21. h1(0.9);
  22. // state: [0, 0.5): 1, [0.5, 1.0): 1
  23. assert(h1.axis().size() == 2);
  24. assert(h1.axis().bin(0).lower() == 0.0);
  25. assert(h1.axis().bin(1).upper() == 1.0);
  26. // value below range: axis grows new bins until value is in range
  27. h1(-0.3);
  28. // state: [-0.5, 0.0): 1, [0, 0.5): 1, [0.5, 1.0): 1
  29. assert(h1.axis().size() == 3);
  30. assert(h1.axis().bin(0).lower() == -0.5);
  31. assert(h1.axis().bin(2).upper() == 1.0);
  32. h1(1.9);
  33. // state: [-0.5, 0.0): 1, [0, 0.5): 1, [0.5, 1.0): 1, [1.0, 1.5): 0 [1.5, 2.0): 1
  34. assert(h1.axis().size() == 5);
  35. assert(h1.axis().bin(0).lower() == -0.5);
  36. assert(h1.axis().bin(4).upper() == 2.0);
  37. // make a growing category axis (here strings)
  38. // - empty axis is allowed: very useful if categories are not known at the beginning
  39. auto h2 = make_histogram(axis::category<std::string,
  40. use_default,
  41. axis::option::growth_t>());
  42. assert(h2.size() == 0); // histogram is empty
  43. h2("foo"); // new bin foo, index 0
  44. assert(h2.size() == 1);
  45. h2("bar"); // new bin bar, index 1
  46. assert(h2.size() == 2);
  47. h2("foo");
  48. assert(h2.size() == 2);
  49. assert(h2[0] == 2);
  50. assert(h2[1] == 1);
  51. }
  52. //]