histogram/examples/guide_access_bin_counts.cpp
Hans Dembinski 06596efe4e doc fixes
2018-08-24 11:22:31 +02:00

63 lines
2.1 KiB
C++

//[ guide_access_bin_counts
#include <boost/histogram.hpp>
#include <iostream>
#include <numeric>
namespace bh = boost::histogram;
int main() {
// make histogram with 2 x 2 = 4 bins (not counting under-/overflow bins)
auto h = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1),
bh::axis::regular<>(2, 2, 4));
h(bh::weight(1), -0.5, 2.5); // bin index 0, 0
h(bh::weight(2), -0.5, 3.5); // bin index 0, 1
h(bh::weight(3), 0.5, 2.5); // bin index 1, 0
h(bh::weight(4), 0.5, 3.5); // bin index 1, 1
// access count value, number of indices must match number of axes
std::cout << h.at(0, 0).value() << " " << h.at(0, 1).value() << " "
<< h.at(1, 0).value() << " " << h.at(1, 1).value() << std::endl;
// prints: 1 2 3 4
// access count variance, number of indices must match number of axes
std::cout << h.at(0, 0).variance() << " " << h.at(0, 1).variance() << " "
<< h.at(1, 0).variance() << " " << h.at(1, 1).variance()
<< std::endl;
// prints: 1 4 9 16
// this is more efficient when you want to query value and variance
auto c11 = h.at(1, 1);
std::cout << c11.value() << " " << c11.variance() << std::endl;
// prints: 4 16
// histogram also supports access via container; using a container of
// wrong size trips an assertion in debug mode
auto idx = {0, 1};
std::cout << h.at(idx).value() << std::endl;
// prints: 2
// histogram also provides bin iterators
auto sum = std::accumulate(h.begin(), h.end(),
typename decltype(h)::element_type(0));
std::cout << sum.value() << " " << sum.variance() << std::endl;
// prints: 10 30
// bin iterators are fancy iterators with extra methods
for (auto it = h.begin(), end = h.end(); it != end; ++it) {
const auto x = *it;
std::cout << it.idx(0) << " " << it.idx(1) << ": "
<< x.value() << " " << x.variance() << std::endl;
}
// prints: (iteration order is an implementation detail, don't rely on it)
// 0 0: 1 1
// 1 0: 3 9
// ...
// 2 -1: 0 0
// -1 -1: 0 0
}
//]