mirror of
https://github.com/boostorg/histogram.git
synced 2025-05-09 23:04:07 +00:00
80 lines
2.8 KiB
C++
80 lines
2.8 KiB
C++
//[ getting_started_listing_01
|
|
|
|
#include <boost/histogram.hpp>
|
|
#include <algorithm>
|
|
#include <functional>
|
|
#include <sstream>
|
|
#include <cassert>
|
|
|
|
int main() {
|
|
namespace bh = boost::histogram;
|
|
using namespace bh::literals; // enables _c suffix
|
|
|
|
/*
|
|
create a static 1d-histogram with an axis that has 6 equidistant
|
|
bins on the real line from -1.0 to 2.0, and label it as "x"
|
|
*/
|
|
auto h = bh::make_histogram(bh::axis::regular<>(6, -1.0, 2.0, "x"));
|
|
|
|
// Fill histogram with data, typically this happens in a loop.
|
|
// STL algorithms are supported, but make sure to use std::ref in
|
|
// the call to std::for_each to avoid copying the argument.
|
|
auto data = {-0.5, 1.1, 0.3, 1.7};
|
|
std::for_each(data.begin(), data.end(), std::ref(h));
|
|
|
|
/*
|
|
a regular axis is a sequence of semi-open bins; extra under- and
|
|
overflow bins extend the axis in the default configuration
|
|
index : -1 0 1 2 3 4 5 6
|
|
bin edge: -inf -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 inf
|
|
*/
|
|
h(-1.5); // put in underflow bin -1
|
|
h(-1.0); // put in bin 0, bin interval is semi-open
|
|
h(2.0); // put in overflow bin 6, bin interval is semi-open
|
|
h(20.0); // put in overflow bin 6
|
|
|
|
/*
|
|
do a weighted fill using bh::weight, a wrapper for any type,
|
|
which may appear at the beginning of the argument list
|
|
*/
|
|
h(bh::weight(1.0), 0.1);
|
|
|
|
/*
|
|
iterate over bins with a fancy histogram iterator
|
|
- order in which bins are iterated over is an implementation detail
|
|
- iterator dereferences to histogram::const_reference, which is defined by
|
|
its storage class; for the default storage it is actually a plain double
|
|
- idx(N) method returns the index of the N-th axis
|
|
- bin(N_c) method returns current bin of N-th axis; the suffx _c turns
|
|
the argument into a compile-time number, which is needed to return
|
|
a different `bin_type`s for each axis
|
|
- `bin_type` usually is a semi-open interval representing the bin, whose
|
|
edges can be accessed with methods `lower()` and `upper()`, but the
|
|
implementation depends on the axis, please look it up in the reference
|
|
*/
|
|
std::ostringstream os;
|
|
os.setf(std::ios_base::fixed);
|
|
for (auto it = h.begin(); it != h.end(); ++it) {
|
|
const auto bin = it.bin(0_c);
|
|
os << "bin " << std::setw(2) << it.idx(0) << " [" << std::setprecision(1)
|
|
<< std::setw(4) << bin.lower() << ", " << std::setw(4)
|
|
<< bin.upper() << "): " << *it << "\n";
|
|
}
|
|
|
|
std::cout << os.str() << std::endl;
|
|
|
|
assert(os.str() ==
|
|
"bin 0 [-1.0, -0.5): 1.0\n"
|
|
"bin 1 [-0.5, -0.0): 1.0\n"
|
|
"bin 2 [-0.0, 0.5): 2.0\n"
|
|
"bin 3 [ 0.5, 1.0): 0.0\n"
|
|
"bin 4 [ 1.0, 1.5): 1.0\n"
|
|
"bin 5 [ 1.5, 2.0): 1.0\n"
|
|
"bin 6 [ 2.0, inf): 2.0\n"
|
|
"bin -1 [-inf, -1.0): 1.0\n"
|
|
);
|
|
// note how under- and overflow bins appear at the end
|
|
}
|
|
|
|
//]
|