guide_histogram_reduction.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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_reduction
  7. #include <boost/histogram.hpp>
  8. #include <cassert>
  9. int main() {
  10. using namespace boost::histogram;
  11. // make a 2d histogram
  12. auto h = make_histogram(axis::regular<>(4, 0.0, 4.0), axis::regular<>(4, -1.0, 1.0));
  13. h(0, -0.9);
  14. h(1, 0.9);
  15. h(2, 0.1);
  16. h(3, 0.1);
  17. // shrink the first axis to remove the highest bin
  18. // rebin the second axis by merging pairs of adjacent bins
  19. auto h2 = algorithm::reduce(h, algorithm::shrink(0, 0.0, 3.0), algorithm::rebin(1, 2));
  20. assert(h2.axis(0).size() == 3);
  21. assert(h2.axis(0).bin(0).lower() == 0.0);
  22. assert(h2.axis(0).bin(2).upper() == 3.0);
  23. assert(h2.axis(1).size() == 2);
  24. assert(h2.axis(1).bin(0).lower() == -1.0);
  25. assert(h2.axis(1).bin(1).upper() == 1.0);
  26. // reduce does not change the total count if the histogram has underflow/overflow bins;
  27. // counts in removed bins are added to the corresponding under- and overflow bins
  28. assert(algorithm::sum(h) == 4 && algorithm::sum(h2) == 4);
  29. }
  30. //]