| 10 | namespace bh = boost::histogram; |
| 11 | |
| 12 | int main() { |
| 13 | /* |
| 14 | create a dynamic histogram with the factory `make_dynamic_histogram` |
| 15 | - axis can be passed directly just like for `make_static_histogram` |
| 16 | - in addition, the factory also accepts iterators over a sequence of |
| 17 | axis::any, the polymorphic type that can hold concrete axis types |
| 18 | */ |
| 19 | std::vector<bh::axis::any_std> axes; |
| 20 | axes.emplace_back(bh::axis::category<std::string>({"red", "blue"})); |
| 21 | axes.emplace_back(bh::axis::regular<>(5, -5, 5, "x")); |
| 22 | axes.emplace_back(bh::axis::regular<>(5, -5, 5, "y")); |
| 23 | auto h = bh::make_dynamic_histogram(axes.begin(), axes.end()); |
| 24 | |
| 25 | // fill histogram with random numbers |
| 26 | br::mt19937 gen; |
| 27 | br::normal_distribution<> norm; |
| 28 | for (int i = 0; i < 1000; ++i) |
| 29 | h(i % 2 ? "red" : "blue", norm(gen), norm(gen)); |
| 30 | |
| 31 | /* |
| 32 | print dynamic histogram by iterating over bins |
| 33 | - for most axis types, the for loop looks just like for a static |
| 34 | histogram, except that we can pass runtime numbers, too |
| 35 | - if the [bin type] of the axis is not convertible to a |
| 36 | double interval, one needs to cast axis::any before looping; |
| 37 | this is here the case for the category axis |
| 38 | */ |
| 39 | using cas = bh::axis::category<std::string>; |
| 40 | for (auto cbin : static_cast<const cas&>(h.axis(0))) { |
| 41 | std::printf("%s\n", cbin.value().c_str()); |
| 42 | for (auto ybin : h.axis(2)) { // rows |
| 43 | for (auto xbin : h.axis(1)) { // columns |
| 44 | std::printf("%3.0f ", h.at(cbin, xbin, ybin).value()); |
| 45 | } |
| 46 | std::printf("\n"); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | //] |