MCPcopy Create free account
hub / github.com/boostorg/histogram / main

Function main

examples/getting_started_listing_02.cpp:12–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10namespace bh = boost::histogram;
11
12int 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//]

Callers

nothing calls this directly

Calls 6

make_dynamic_histogramFunction · 0.85
axisMethod · 0.80
atMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected