guide_histogram_streaming.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_histogram_streaming
  7. #include <boost/histogram.hpp>
  8. #include <boost/histogram/ostream.hpp>
  9. #include <cassert>
  10. #include <iostream>
  11. #include <sstream>
  12. #include <string>
  13. int main() {
  14. using namespace boost::histogram;
  15. std::ostringstream os;
  16. auto h1 = make_histogram(axis::regular<>(5, -1.0, 1.0, "axis 1"));
  17. h1.at(0) = 2;
  18. h1.at(1) = 4;
  19. h1.at(2) = 3;
  20. h1.at(4) = 1;
  21. // 1D histograms are rendered as an ASCII drawing
  22. os << h1;
  23. auto h2 = make_histogram(axis::regular<>(2, -1.0, 1.0, "axis 1"),
  24. axis::category<std::string>({"red", "blue"}, "axis 2"));
  25. // higher dimensional histograms just have their cell counts listed
  26. os << h2;
  27. std::cout << os.str() << std::endl;
  28. assert(
  29. os.str() ==
  30. "histogram(regular(5, -1, 1, metadata=\"axis 1\", options=underflow | overflow))\n"
  31. " +-------------------------------------------------------------+\n"
  32. "[-inf, -1) 0 | |\n"
  33. "[ -1, -0.6) 2 |============================== |\n"
  34. "[-0.6, -0.2) 4 |============================================================ |\n"
  35. "[-0.2, 0.2) 3 |============================================= |\n"
  36. "[ 0.2, 0.6) 0 | |\n"
  37. "[ 0.6, 1) 1 |=============== |\n"
  38. "[ 1, inf) 0 | |\n"
  39. " +-------------------------------------------------------------+\n"
  40. "histogram(\n"
  41. " regular(2, -1, 1, metadata=\"axis 1\", options=underflow | overflow)\n"
  42. " category(\"red\", \"blue\", metadata=\"axis 2\", options=overflow)\n"
  43. " (-1 0): 0 ( 0 0): 0 ( 1 0): 0 ( 2 0): 0 (-1 1): 0 ( 0 1): 0\n"
  44. " ( 1 1): 0 ( 2 1): 0 (-1 2): 0 ( 0 2): 0 ( 1 2): 0 ( 2 2): 0\n"
  45. ")");
  46. }
  47. //]