• 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_projection
8 
9 #include <boost/histogram.hpp>
10 #include <cassert>
11 #include <iostream>
12 #include <sstream>
13 
main()14 int main() {
15   using namespace boost::histogram;
16   using namespace literals; // enables _c suffix
17 
18   // make a 2d histogram
19   auto h = make_histogram(axis::regular<>(3, -1.0, 1.0), axis::integer<>(0, 2));
20 
21   h(-0.9, 0);
22   h(0.9, 1);
23   h(0.1, 0);
24 
25   auto hr0 = algorithm::project(h, 0_c); // keep only first axis
26   auto hr1 = algorithm::project(h, 1_c); // keep only second axis
27 
28   // reduce does not remove counts; returned histograms are summed over
29   // the removed axes, so h, hr0, and hr1 have same number of total counts;
30   // we compute the sum of counts with the sum algorithm
31   assert(algorithm::sum(h) == 3 && algorithm::sum(hr0) == 3 && algorithm::sum(hr1) == 3);
32 
33   std::ostringstream os1;
34   for (auto&& x : indexed(h))
35     os1 << "(" << x.index(0) << ", " << x.index(1) << "): " << *x << "\n";
36   std::cout << os1.str() << std::flush;
37   assert(os1.str() == "(0, 0): 1\n"
38                       "(1, 0): 1\n"
39                       "(2, 0): 0\n"
40                       "(0, 1): 0\n"
41                       "(1, 1): 0\n"
42                       "(2, 1): 1\n");
43 
44   std::ostringstream os2;
45   for (auto&& x : indexed(hr0)) os2 << "(" << x.index(0) << ", -): " << *x << "\n";
46   std::cout << os2.str() << std::flush;
47   assert(os2.str() == "(0, -): 1\n"
48                       "(1, -): 1\n"
49                       "(2, -): 1\n");
50 
51   std::ostringstream os3;
52   for (auto&& x : indexed(hr1)) os3 << "(- ," << x.index(0) << "): " << *x << "\n";
53   std::cout << os3.str() << std::flush;
54   assert(os3.str() == "(- ,0): 2\n"
55                       "(- ,1): 1\n");
56 }
57 
58 //]
59