]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/histogram/examples/guide_custom_accumulators_3.cpp
1 // Copyright 2019 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_3
9 #include <boost/format.hpp>
10 #include <boost/histogram.hpp>
15 using namespace boost::histogram
;
17 // Accumulator accepts two samples and an optional weight and computes the mean of each.
19 accumulators::mean
<> mx
, my
;
21 // called when no weight is passed
22 void operator()(double x
, double y
) {
27 // called when a weight is passed
28 void operator()(weight_type
<double> w
, double x
, double y
) {
33 // Note: The implementation can be made more efficient by sharing the sum of weights.
35 // Create a 1D histogram that uses the custom accumulator.
36 auto h
= make_histogram_with(dense_storage
<multi_mean
>(), axis::integer
<>(0, 2));
37 h(0, sample(1, 2)); // samples go to first cell
38 h(0, sample(3, 4)); // samples go to first cell
39 h(1, sample(5, 6), weight(2)); // samples go to second cell
40 h(1, sample(7, 8), weight(3)); // samples go to second cell
42 std::ostringstream os
;
43 for (auto&& bin
: indexed(h
)) {
44 os
<< boost::format("index %i mean-x %.1f mean-y %.1f\n") % bin
.index() %
45 bin
->mx
.value() % bin
->my
.value();
47 std::cout
<< os
.str() << std::flush
;
48 assert(os
.str() == "index 0 mean-x 2.0 mean-y 3.0\n"
49 "index 1 mean-x 6.2 mean-y 7.2\n");