• 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 // clang-format off
8 
9 //[ getting_started_listing_02
10 
11 #include <boost/format.hpp>
12 #include <boost/histogram.hpp>
13 #include <cassert>
14 #include <iostream>
15 #include <sstream>
16 #include <string>
17 
main()18 int main() {
19   using namespace boost::histogram;
20 
21   /*
22     Create a histogram which can be configured dynamically at run-time. The axis
23     configuration is first collected in a vector of axis::variant type, which
24     can hold different axis types (those in its template argument list). Here,
25     we use a variant that can store a regular and a category axis.
26   */
27   using reg = axis::regular<>;
28   using cat = axis::category<std::string>;
29   using variant = axis::variant<axis::regular<>, axis::category<std::string>>;
30   std::vector<variant> axes;
31   axes.emplace_back(cat({"red", "blue"}));
32   axes.emplace_back(reg(3, 0.0, 1.0, "x"));
33   axes.emplace_back(reg(3, 0.0, 1.0, "y"));
34   // passing an iterator range also works here
35   auto h = make_histogram(std::move(axes));
36 
37   // fill histogram with data, usually this happens in a loop
38   h("red", 0.1, 0.2);
39   h("blue", 0.7, 0.3);
40   h("red", 0.3, 0.7);
41   h("red", 0.7, 0.7);
42 
43   /*
44     Print histogram by iterating over bins.
45     Since the [bin type] of the category axis cannot be converted into a double,
46     it cannot be handled by the polymorphic interface of axis::variant. We use
47     axis::get to "cast" the variant type to the actual category type.
48   */
49 
50   // get reference to category axis, performs a run-time checked static cast
51   const auto& cat_axis = axis::get<cat>(h.axis(0));
52   std::ostringstream os;
53   for (auto&& x : indexed(h)) {
54     os << boost::format("(%i, %i, %i) %4s [%3.1f, %3.1f) [%3.1f, %3.1f) %3.0f\n")
55           % x.index(0) % x.index(1) % x.index(2)
56           % cat_axis.bin(x.index(0))
57           % x.bin(1).lower() % x.bin(1).upper()
58           % x.bin(2).lower() % x.bin(2).upper()
59           % *x;
60   }
61 
62   std::cout << os.str() << std::flush;
63   assert(os.str() == "(0, 0, 0)  red [0.0, 0.3) [0.0, 0.3)   1\n"
64                      "(1, 0, 0) blue [0.0, 0.3) [0.0, 0.3)   0\n"
65                      "(0, 1, 0)  red [0.3, 0.7) [0.0, 0.3)   0\n"
66                      "(1, 1, 0) blue [0.3, 0.7) [0.0, 0.3)   0\n"
67                      "(0, 2, 0)  red [0.7, 1.0) [0.0, 0.3)   0\n"
68                      "(1, 2, 0) blue [0.7, 1.0) [0.0, 0.3)   1\n"
69                      "(0, 0, 1)  red [0.0, 0.3) [0.3, 0.7)   0\n"
70                      "(1, 0, 1) blue [0.0, 0.3) [0.3, 0.7)   0\n"
71                      "(0, 1, 1)  red [0.3, 0.7) [0.3, 0.7)   0\n"
72                      "(1, 1, 1) blue [0.3, 0.7) [0.3, 0.7)   0\n"
73                      "(0, 2, 1)  red [0.7, 1.0) [0.3, 0.7)   0\n"
74                      "(1, 2, 1) blue [0.7, 1.0) [0.3, 0.7)   0\n"
75                      "(0, 0, 2)  red [0.0, 0.3) [0.7, 1.0)   1\n"
76                      "(1, 0, 2) blue [0.0, 0.3) [0.7, 1.0)   0\n"
77                      "(0, 1, 2)  red [0.3, 0.7) [0.7, 1.0)   0\n"
78                      "(1, 1, 2) blue [0.3, 0.7) [0.7, 1.0)   0\n"
79                      "(0, 2, 2)  red [0.7, 1.0) [0.7, 1.0)   1\n"
80                      "(1, 2, 2) blue [0.7, 1.0) [0.7, 1.0)   0\n");
81 }
82 
83 //]
84