1 // Copyright 2018 Hans Dembinski
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)
7 //[ guide_custom_accumulators_2
9 #include <boost/format.hpp>
10 #include <boost/histogram.hpp>
16 using namespace boost::histogram;
18 // Make a custom accumulator, which tracks the maximum of the samples.
19 // It must have a call operator that accepts the argument of the `sample` function.
21 // return value is ignored, so we use void
22 void operator()(double x) {
23 if (x > value) value = x;
25 double value = 0; // value is initialized to zero
28 // Create 1D histogram that uses the custom accumulator.
29 auto h = make_histogram_with(dense_storage<maximum>(), axis::integer<>(0, 2));
30 h(0, sample(1.0)); // sample goes to first cell
31 h(0, sample(2.0)); // sample goes to first cell
32 h(1, sample(3.0)); // sample goes to second cell
33 h(1, sample(4.0)); // sample goes to second cell
35 std::ostringstream os;
36 for (auto&& x : indexed(h)) {
37 os << boost::format("index %i maximum %.1f\n") % x.index() % x->value;
39 std::cout << os.str() << std::flush;
40 assert(os.str() == "index 0 maximum 2.0\n"
41 "index 1 maximum 4.0\n");