| 7 | namespace bh = boost::histogram; |
| 8 | |
| 9 | int main() { |
| 10 | // make histogram with 2 x 2 = 4 bins (not counting under-/overflow bins) |
| 11 | auto h = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1), |
| 12 | bh::axis::regular<>(2, 2, 4)); |
| 13 | |
| 14 | h(bh::weight(1), -0.5, 2.5); // bin index 0, 0 |
| 15 | h(bh::weight(2), -0.5, 3.5); // bin index 0, 1 |
| 16 | h(bh::weight(3), 0.5, 2.5); // bin index 1, 0 |
| 17 | h(bh::weight(4), 0.5, 3.5); // bin index 1, 1 |
| 18 | |
| 19 | // access count value, number of indices must match number of axes |
| 20 | std::cout << h.at(0, 0).value() << " " << h.at(0, 1).value() << " " |
| 21 | << h.at(1, 0).value() << " " << h.at(1, 1).value() << std::endl; |
| 22 | |
| 23 | // prints: 1 2 3 4 |
| 24 | |
| 25 | // access count variance, number of indices must match number of axes |
| 26 | std::cout << h.at(0, 0).variance() << " " << h.at(0, 1).variance() << " " |
| 27 | << h.at(1, 0).variance() << " " << h.at(1, 1).variance() |
| 28 | << std::endl; |
| 29 | // prints: 1 4 9 16 |
| 30 | |
| 31 | // this is more efficient when you want to query value and variance |
| 32 | auto c11 = h.at(1, 1); |
| 33 | std::cout << c11.value() << " " << c11.variance() << std::endl; |
| 34 | // prints: 4 16 |
| 35 | |
| 36 | // histogram also supports access via container; using a container of |
| 37 | // wrong size trips an assertion in debug mode |
| 38 | auto idx = {0, 1}; |
| 39 | std::cout << h.at(idx).value() << std::endl; |
| 40 | // prints: 2 |
| 41 | |
| 42 | // histogram also provides bin iterators |
| 43 | auto sum = std::accumulate(h.begin(), h.end(), |
| 44 | typename decltype(h)::element_type(0)); |
| 45 | std::cout << sum.value() << " " << sum.variance() << std::endl; |
| 46 | // prints: 10 30 |
| 47 | |
| 48 | // bin iterators are fancy iterators with extra methods |
| 49 | for (auto it = h.begin(), end = h.end(); it != end; ++it) { |
| 50 | const auto x = *it; |
| 51 | std::cout << it.idx(0) << " " << it.idx(1) << ": " |
| 52 | << x.value() << " " << x.variance() << std::endl; |
| 53 | } |
| 54 | // prints: (iteration order is an implementation detail, don't rely on it) |
| 55 | // 0 0: 1 1 |
| 56 | // 1 0: 3 9 |
| 57 | // ... |
| 58 | // 2 -1: 0 0 |
| 59 | // -1 -1: 0 0 |
| 60 | } |
| 61 | |
| 62 | //] |