• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 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 // clang-format off
8 
9 //[ getting_started_listing_03
10 
11 #include <boost/format.hpp>
12 #include <boost/histogram.hpp>
13 #include <cassert>
14 #include <iostream>
15 #include <sstream>
16 
main()17 int main() {
18   using namespace boost::histogram;
19 
20   /*
21     Create a profile. Profiles does not only count entries in each cell, but
22     also compute the mean of a sample value in each cell.
23   */
24   auto p = make_profile(axis::regular<>(5, 0.0, 1.0));
25 
26   /*
27     Fill profile with data, usually this happens in a loop. You pass the sample
28     with the `sample` helper function. The sample can be the first or last
29     argument.
30   */
31   p(0.1, sample(1));
32   p(0.15, sample(3));
33   p(0.2, sample(4));
34   p(0.9, sample(5));
35 
36   /*
37     Iterate over bins and print profile.
38   */
39   std::ostringstream os;
40   for (auto&& x : indexed(p)) {
41     os << boost::format("bin %i [%3.1f, %3.1f) count %i mean %g\n")
42           % x.index() % x.bin().lower() % x.bin().upper()
43           % x->count() % x->value();
44   }
45 
46   std::cout << os.str() << std::flush;
47   assert(os.str() == "bin 0 [0.0, 0.2) count 2 mean 2\n"
48                      "bin 1 [0.2, 0.4) count 1 mean 4\n"
49                      "bin 2 [0.4, 0.6) count 0 mean 0\n"
50                      "bin 3 [0.6, 0.8) count 0 mean 0\n"
51                      "bin 4 [0.8, 1.0) count 1 mean 5\n");
52 }
53 
54 //]
55