mirror of
https://github.com/boostorg/histogram.git
synced 2025-05-11 13:14:06 +00:00
56 lines
1.7 KiB
C++
56 lines
1.7 KiB
C++
//[ getting_started_listing_02
|
|
|
|
#include <boost/histogram.hpp>
|
|
#include <cstdlib>
|
|
#include <string>
|
|
|
|
namespace bh = boost::histogram;
|
|
|
|
int main() {
|
|
/*
|
|
Create a dynamic histogram with the factory `make_dynamic_histogram`.
|
|
- axis can be passed directly just like for `make_static_histogram`
|
|
- in addition, the factory also accepts iterators over a sequence of
|
|
axis::variant, the polymorphic type that can hold concrete axis types
|
|
*/
|
|
std::vector<
|
|
bh::axis::variant<
|
|
bh::axis::regular<>,
|
|
bh::axis::category<std::string>
|
|
>
|
|
> axes;
|
|
axes.emplace_back(bh::axis::category<std::string>({"red", "blue"}));
|
|
axes.emplace_back(bh::axis::regular<>(5, 0, 1, "x"));
|
|
axes.emplace_back(bh::axis::regular<>(5, 0, 1, "y"));
|
|
auto h = bh::make_histogram(axes.begin(), axes.end());
|
|
|
|
// fill histogram with data, usually this happens in a loop
|
|
h("red", 0.1, 0.2);
|
|
h("blue", 0.3, 0.4);
|
|
h("red", 0.5, 0.6);
|
|
h("red", 0.7, 0.8);
|
|
|
|
/*
|
|
Print dynamic histogram by iterating over bins.
|
|
If the [bin type] of the axis is not convertible to a
|
|
double interval, you need to cast axis::variant before looping;
|
|
this is here the case for the category axis.
|
|
*/
|
|
using cas = bh::axis::category<std::string>;
|
|
for (auto cbin : bh::axis::get<cas>(h.axis(0))) {
|
|
for (auto ybin : h.axis(2)) { // rows
|
|
for (auto xbin : h.axis(1)) { // columns
|
|
const auto v = h.at(cbin, xbin, ybin);
|
|
if (v)
|
|
std::printf("%4s [%3.1f, %3.1f) [%3.1f, %3.1f) %3.0f\n",
|
|
cbin.value().c_str(),
|
|
xbin.lower(), xbin.upper(),
|
|
ybin.lower(), ybin.upper(),
|
|
v);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//]
|