]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
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_fill_profile | |
8 | ||
9 | #include <boost/format.hpp> | |
10 | #include <boost/histogram.hpp> | |
11 | #include <cassert> | |
12 | #include <iostream> | |
13 | #include <sstream> | |
14 | #include <tuple> | |
15 | ||
16 | int main() { | |
17 | using namespace boost::histogram; | |
18 | ||
19 | // make a profile, it computes the mean of the samples in each histogram cell | |
20 | auto h = make_profile(axis::integer<>(0, 3)); | |
21 | ||
22 | // mean is computed from the values marked with the sample() helper function | |
23 | h(0, sample(1)); // sample goes to cell 0 | |
24 | h(0, sample(2)); // sample goes to cell 0 | |
25 | h(1, sample(3)); // sample goes to cell 1 | |
26 | h(sample(4), 1); // sample goes to cell 1; sample can be first or last argument | |
27 | ||
28 | // fills from tuples are also supported, 5 and 6 go to cell 2 | |
29 | auto a = std::make_tuple(2, sample(5)); | |
30 | auto b = std::make_tuple(sample(6), 2); | |
31 | h(a); | |
32 | h(b); | |
33 | ||
34 | // builtin accumulators have methods to access their state | |
35 | std::ostringstream os; | |
36 | for (auto&& x : indexed(h)) { | |
37 | // use `.` to access methods of accessor, like `index()` | |
38 | // use `->` to access methods of accumulator | |
39 | const auto i = x.index(); | |
40 | const auto n = x->count(); // how many samples are in this bin | |
41 | const auto vl = x->value(); // mean value | |
42 | const auto vr = x->variance(); // estimated variance of the mean value | |
43 | os << boost::format("index %i count %i value %.1f variance %.1f\n") % i % n % vl % vr; | |
44 | } | |
45 | ||
46 | std::cout << os.str() << std::flush; | |
47 | ||
48 | assert(os.str() == "index 0 count 2 value 1.5 variance 0.5\n" | |
49 | "index 1 count 2 value 3.5 variance 0.5\n" | |
50 | "index 2 count 2 value 5.5 variance 0.5\n"); | |
51 | } | |
52 | ||
53 | //] |