]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/histogram/examples/guide_custom_accumulators_1.cpp
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_1
9 #include <boost/format.hpp>
10 #include <boost/histogram.hpp>
16 using namespace boost::histogram
;
17 using mean
= accumulators::mean
<>;
19 // Create a 1D-profile, which computes the mean of samples in each bin.
20 auto h
= make_histogram_with(dense_storage
<mean
>(), axis::integer
<>(0, 2));
21 // The factory function `make_profile` is provided as a shorthand for this, so this is
22 // equivalent to the previous line: auto h = make_profile(axis::integer<>(0, 2));
24 // An argument marked as `sample` is passed to the accumulator.
25 h(0, sample(1)); // sample goes to first cell
26 h(0, sample(2)); // sample goes to first cell
27 h(1, sample(3)); // sample goes to second cell
28 h(1, sample(4)); // sample goes to second cell
30 std::ostringstream os
;
31 for (auto&& x
: indexed(h
)) {
32 // Accumulators usually have methods to access their state. Use the arrow
33 // operator to access them. Here, `count()` gives the number of samples,
34 // `value()` the mean, and `variance()` the variance estimate of the mean.
35 os
<< boost::format("index %i count %i mean %.1f variance %.1f\n") % x
.index() %
36 x
->count() % x
->value() % x
->variance();
38 std::cout
<< os
.str() << std::flush
;
39 assert(os
.str() == "index 0 count 2 mean 1.5 variance 0.5\n"
40 "index 1 count 2 mean 3.5 variance 0.5\n");