getting_started_listing_01.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_01
  8. #include <algorithm> // std::for_each
  9. #include <boost/format.hpp> // only needed for printing
  10. #include <boost/histogram.hpp> // make_histogram, regular, weight, indexed
  11. #include <cassert> // assert (used to test this example for correctness)
  12. #include <functional> // std::ref
  13. #include <iostream> // std::cout, std::flush
  14. #include <sstream> // std::ostringstream
  15. int main() {
  16. using namespace boost::histogram; // strip the boost::histogram prefix
  17. /*
  18. Create a 1d-histogram with a regular axis that has 6 equidistant bins on
  19. the real line from -1.0 to 2.0, and label it as "x". A family of overloaded
  20. factory functions called `make_histogram` makes creating histograms easy.
  21. A regular axis is a sequence of semi-open bins. Extra under- and overflow
  22. bins extend the axis by default (this can be turned off).
  23. index : -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6
  24. bin edges: -inf -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 inf
  25. */
  26. auto h = make_histogram(axis::regular<>(6, -1.0, 2.0, "x"));
  27. /*
  28. Let's fill a histogram with data, typically this happens in a loop.
  29. STL algorithms are supported. std::for_each is very convenient to fill a
  30. histogram from an iterator range. Use std::ref in the call, if you don't
  31. want std::for_each to make a copy of your histogram.
  32. */
  33. auto data = {-0.5, 1.1, 0.3, 1.7};
  34. std::for_each(data.begin(), data.end(), std::ref(h));
  35. // let's fill some more values manually
  36. h(-1.5); // is placed in underflow bin -1
  37. h(-1.0); // is placed in bin 0, bin interval is semi-open
  38. h(2.0); // is placed in overflow bin 6, bin interval is semi-open
  39. h(20.0); // is placed in overflow bin 6
  40. /*
  41. This does a weighted fill using the `weight` function as an additional
  42. argument. It may appear at the beginning or end of the argument list. C++
  43. doesn't have keyword arguments like Python, this is the next-best thing.
  44. */
  45. h(0.1, weight(1.0));
  46. /*
  47. Iterate over bins with the `indexed` range generator, which provides a
  48. special accessor object, that can be used to obtain the current bin index,
  49. and the current bin value by dereferncing (it acts like a pointer to the
  50. value). Using `indexed` is convenient and gives you better performance than
  51. looping over the histogram cells with hand-written for loops. By default,
  52. under- and overflow bins are skipped. Passing `coverage::all` as the
  53. optional second argument iterates over all bins.
  54. - Access the value with the dereference operator.
  55. - Access the current index with `index(d)` method of the accessor.
  56. - Access the corresponding bin interval view with `bin(d)`.
  57. The return type of `bin(d)` depends on the axis type (see the axis reference
  58. for details). It usually is a class that represents a semi-open interval.
  59. Edges can be accessed with methods `lower()` and `upper()`.
  60. */
  61. std::ostringstream os;
  62. for (auto&& x : indexed(h, coverage::all)) {
  63. os << boost::format("bin %2i [%4.1f, %4.1f): %i\n")
  64. % x.index() % x.bin().lower() % x.bin().upper() % *x;
  65. }
  66. std::cout << os.str() << std::flush;
  67. assert(os.str() == "bin -1 [-inf, -1.0): 1\n"
  68. "bin 0 [-1.0, -0.5): 1\n"
  69. "bin 1 [-0.5, -0.0): 1\n"
  70. "bin 2 [-0.0, 0.5): 2\n"
  71. "bin 3 [ 0.5, 1.0): 0\n"
  72. "bin 4 [ 1.0, 1.5): 1\n"
  73. "bin 5 [ 1.5, 2.0): 1\n"
  74. "bin 6 [ 2.0, inf): 2\n");
  75. }
  76. //]