Files
histogram/examples/guide_fill_histogram.cpp
Hans Dembinski f859c9fe7e Expanded docs to discuss special cases (#412)
This clarifies how to use histogram::fill efficiently with multi-dimensional histograms, and discusses a workaround for the design choice that Axis::index cannot be overloaded.

And since I haven't build the documentation locally for a long time, and a new computer where I needed to set things up again from scratch, I also added a README.md on how to do that (which is not trivial for boostbook).
2025-06-05 23:14:21 +02:00

72 lines
2.8 KiB
C++

// Copyright 2015-2018 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//[ guide_fill_histogram
#include <boost/histogram.hpp>
#include <boost/core/span.hpp>
#include <cassert>
#include <functional>
#include <numeric>
#include <utility>
#include <vector>
int main() {
using namespace boost::histogram;
auto h = make_histogram(axis::integer<>(0, 3), axis::regular<>(2, 0.0, 1.0));
// fill histogram, number of arguments must be equal to number of axes,
// types must be convertible to axis value type (here integer and double)
h(0, 0.2); // increase a cell value by one
h(2, 0.5); // increase another cell value by one
// fills from a tuple are also supported; passing a tuple of wrong size
// causes an error at compile-time or an assertion at runtime in debug mode
auto xy = std::make_tuple(1, 0.3);
h(xy);
// chunk-wise filling is also supported and more efficient, make some data...
std::vector<double> x = {0, 2, 5};
std::vector<double> y = {0.8, 0.4, 0.7};
// fill accepts an iterable over iterables, to avoid copying data we use
// std::vector<boost::span<double>> and not std::vector<std::vector<double>>
std::vector<boost::span<double>> xy2 = {boost::make_span(x), boost::make_span(y)};
// alternatively, if the number of axes is known at compile-time,
// it is better to use an array of spans, which avoids the heap allocation
// std::array<boost::span<double>, 2> xy2 = {boost::make_span(x), boost::make_span(y)};
// ... and call fill method
h.fill(xy2);
// once histogram is filled, access individual cells using operator[] or at(...)
// - operator[] can only accept a single argument in the current version of C++,
// it is convenient when you have a 1D histogram
// - at(...) can accept several values, so use this by default
// - underflow bins are at index -1, overflow bins at index `size()`
// - passing an invalid index triggers a std::out_of_range exception
assert(h.at(0, 0) == 1);
assert(h.at(0, 1) == 1);
assert(h.at(1, 0) == 1);
assert(h.at(1, 1) == 0);
assert(h.at(2, 0) == 1);
assert(h.at(2, 1) == 1);
assert(h.at(-1, -1) == 0); // underflow for axis 0 and 1
assert(h.at(-1, 0) == 0); // underflow for axis 0, normal bin for axis 1
assert(h.at(-1, 2) == 0); // underflow for axis 0, overflow for axis 1
assert(h.at(3, 1) == 1); // overflow for axis 0, normal bin for axis 1
// iteration over values works, but see next example for a better way
// - iteration using begin() and end() includes under- and overflow bins
// - iteration order is an implementation detail and should not be relied upon
const double sum = std::accumulate(h.begin(), h.end(), 0.0);
assert(sum == 6);
}
//]