1 // Copyright 2019 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_custom_accumulators_ouroboros
8
9 #include <boost/histogram.hpp>
10 #include <cmath>
11 #include <iostream>
12 #include <sstream>
13 #include <string>
14
main()15 int main() {
16 using namespace boost::histogram;
17
18 // First we define the nested histogram type.
19 using axis_t = axis::category<int, axis::null_type, axis::option::growth_t>;
20 using base_t = histogram<std::tuple<axis_t>>;
21
22 // Now we make an accumulator out of it by using inheritance.
23 // We only need to implement operator(). A matching version of operator() is actually
24 // present in base_t, but it is templated and this is not allowed by the accumulator
25 // concept. Initialization could also happen here. We don't need to initialize anything
26 // here, because the default constructor of base_t is called automatically and
27 // sufficient for this example.
28 struct hist_t : base_t {
29 void operator()(const double x) { base_t::operator()(x); }
30 };
31
32 auto h = make_histogram_with(dense_storage<hist_t>(), axis::integer<>(1, 4));
33
34 auto x = {1, 1, 2, 2};
35 auto s = {1, 2, 3, 3}; // samples are filled into the nested histograms
36 h.fill(x, sample(s));
37
38 std::ostringstream os;
39 for (auto&& x : indexed(h)) {
40 os << x.bin() << " ";
41 for (auto&& y : indexed(*x)) { os << "(" << y.bin() << ": " << *y << ") "; }
42 os << "\n";
43 }
44
45 std::cout << os.str() << std::flush;
46 assert(os.str() == "1 (1: 1) (2: 1) \n"
47 "2 (3: 2) \n"
48 "3 \n");
49 }
50
51 //]
52