getting_started_listing_02.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // clang-format off
  7. //[ getting_started_listing_02
  8. #include <boost/format.hpp>
  9. #include <boost/histogram.hpp>
  10. #include <cassert>
  11. #include <iostream>
  12. #include <sstream>
  13. #include <string>
  14. int main() {
  15. using namespace boost::histogram;
  16. /*
  17. Create a histogram which can be configured dynamically at run-time. The axis
  18. configuration is first collected in a vector of axis::variant type, which
  19. can hold different axis types (those in its template argument list). Here,
  20. we use a variant that can store a regular and a category axis.
  21. */
  22. using reg = axis::regular<>;
  23. using cat = axis::category<std::string>;
  24. using variant = axis::variant<axis::regular<>, axis::category<std::string>>;
  25. std::vector<variant> axes;
  26. axes.emplace_back(cat({"red", "blue"}));
  27. axes.emplace_back(reg(3, 0.0, 1.0, "x"));
  28. axes.emplace_back(reg(3, 0.0, 1.0, "y"));
  29. // passing an iterator range also works here
  30. auto h = make_histogram(std::move(axes));
  31. // fill histogram with data, usually this happens in a loop
  32. h("red", 0.1, 0.2);
  33. h("blue", 0.7, 0.3);
  34. h("red", 0.3, 0.7);
  35. h("red", 0.7, 0.7);
  36. /*
  37. Print histogram by iterating over bins.
  38. Since the [bin type] of the category axis cannot be converted into a double,
  39. it cannot be handled by the polymorphic interface of axis::variant. We use
  40. axis::get to "cast" the variant type to the actual category type.
  41. */
  42. // get reference to category axis, performs a run-time checked static cast
  43. const auto& cat_axis = axis::get<cat>(h.axis(0));
  44. std::ostringstream os;
  45. for (auto&& x : indexed(h)) {
  46. os << boost::format("(%i, %i, %i) %4s [%3.1f, %3.1f) [%3.1f, %3.1f) %3.0f\n")
  47. % x.index(0) % x.index(1) % x.index(2)
  48. % cat_axis.bin(x.index(0))
  49. % x.bin(1).lower() % x.bin(1).upper()
  50. % x.bin(2).lower() % x.bin(2).upper()
  51. % *x;
  52. }
  53. std::cout << os.str() << std::flush;
  54. assert(os.str() == "(0, 0, 0) red [0.0, 0.3) [0.0, 0.3) 1\n"
  55. "(1, 0, 0) blue [0.0, 0.3) [0.0, 0.3) 0\n"
  56. "(0, 1, 0) red [0.3, 0.7) [0.0, 0.3) 0\n"
  57. "(1, 1, 0) blue [0.3, 0.7) [0.0, 0.3) 0\n"
  58. "(0, 2, 0) red [0.7, 1.0) [0.0, 0.3) 0\n"
  59. "(1, 2, 0) blue [0.7, 1.0) [0.0, 0.3) 1\n"
  60. "(0, 0, 1) red [0.0, 0.3) [0.3, 0.7) 0\n"
  61. "(1, 0, 1) blue [0.0, 0.3) [0.3, 0.7) 0\n"
  62. "(0, 1, 1) red [0.3, 0.7) [0.3, 0.7) 0\n"
  63. "(1, 1, 1) blue [0.3, 0.7) [0.3, 0.7) 0\n"
  64. "(0, 2, 1) red [0.7, 1.0) [0.3, 0.7) 0\n"
  65. "(1, 2, 1) blue [0.7, 1.0) [0.3, 0.7) 0\n"
  66. "(0, 0, 2) red [0.0, 0.3) [0.7, 1.0) 1\n"
  67. "(1, 0, 2) blue [0.0, 0.3) [0.7, 1.0) 0\n"
  68. "(0, 1, 2) red [0.3, 0.7) [0.7, 1.0) 0\n"
  69. "(1, 1, 2) blue [0.3, 0.7) [0.7, 1.0) 0\n"
  70. "(0, 2, 2) red [0.7, 1.0) [0.7, 1.0) 1\n"
  71. "(1, 2, 2) blue [0.7, 1.0) [0.7, 1.0) 0\n");
  72. }
  73. //]