• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 //[ guide_histogram_reduction
8 
9 #include <boost/histogram.hpp>
10 #include <cassert>
11 
main()12 int main() {
13   using namespace boost::histogram;
14   // import reduce commands into local namespace to save typing
15   using algorithm::rebin;
16   using algorithm::shrink;
17   using algorithm::slice;
18 
19   // make a 2d histogram
20   auto h = make_histogram(axis::regular<>(4, 0.0, 4.0), axis::regular<>(4, -2.0, 2.0));
21 
22   h(0, -0.9);
23   h(1, 0.9);
24   h(2, 0.1);
25   h(3, 0.1);
26 
27   // reduce takes positional commands which are applied to the axes in order
28   // - shrink is applied to the first axis; the new axis range is 0.0 to 3.0
29   // - rebin is applied to the second axis; pairs of adjacent bins are merged
30   auto h2 = algorithm::reduce(h, shrink(0.0, 3.0), rebin(2));
31 
32   assert(h2.axis(0) == axis::regular<>(3, 0.0, 3.0));
33   assert(h2.axis(1) == axis::regular<>(2, -2.0, 2.0));
34 
35   // reduce does not change the total count if the histogram has underflow/overflow bins
36   assert(algorithm::sum(h) == 4 && algorithm::sum(h2) == 4);
37 
38   // One can also explicitly specify the index of the axis in the histogram on which the
39   // command should act, by using this index as the the first parameter. The position of
40   // the command in the argument list of reduce is then ignored. We use this to slice only
41   // the second axis (axis has index 1 in the histogram) from bin index 2 to 4.
42   auto h3 = algorithm::reduce(h, slice(1, 2, 4));
43 
44   assert(h3.axis(0) == h.axis(0)); // unchanged
45   assert(h3.axis(1) == axis::regular<>(2, 0.0, 2.0));
46 }
47 
48 //]
49