| 4 | #include <iostream> |
| 5 | |
| 6 | int main(int, char**) { |
| 7 | namespace bh = boost::histogram; |
| 8 | using namespace bh::literals; // enables _c suffix |
| 9 | |
| 10 | /* |
| 11 | create a static 1d-histogram with an axis that has 6 equidistant |
| 12 | bins on the real line from -1.0 to 2.0, and label it as "x" |
| 13 | */ |
| 14 | auto h = bh::make_static_histogram(bh::axis::regular<>(6, -1.0, 2.0, "x")); |
| 15 | |
| 16 | // fill histogram with data, typically this happens in a loop |
| 17 | // STL algorithms are supported |
| 18 | auto data = {-0.5, 1.1, 0.3, 1.7}; |
| 19 | std::for_each(data.begin(), data.end(), h); |
| 20 | |
| 21 | /* |
| 22 | a regular axis is a sequence of semi-open bins; extra under- and |
| 23 | overflow bins extend the axis in the default configuration |
| 24 | index : -1 0 1 2 3 4 5 6 |
| 25 | bin edge: -inf -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 inf |
| 26 | */ |
| 27 | h(-1.5); // put in underflow bin -1 |
| 28 | h(-1.0); // put in bin 0, bin interval is semi-open |
| 29 | h(2.0); // put in overflow bin 6, bin interval is semi-open |
| 30 | h(20.0); // put in overflow bin 6 |
| 31 | |
| 32 | /* |
| 33 | do a weighted fill using bh::weight, a wrapper for any type, |
| 34 | which may appear at the beginning of the argument list |
| 35 | */ |
| 36 | h(bh::weight(1.0), 0.1); |
| 37 | |
| 38 | /* |
| 39 | iterate over bins with a fancy histogram iterator |
| 40 | - order in which bins are iterated over is an implementation detail |
| 41 | - iterator dereferences to histogram::element_type, which is defined by |
| 42 | its storage class; by default something with value() and |
| 43 | variance() methods; the first returns the |
| 44 | actual count, the second returns a variance estimate of the count |
| 45 | (see Rationale section for what this means) |
| 46 | - idx(N) method returns the index of the N-th axis |
| 47 | - bin(N_c) method returns current bin of N-th axis; the suffx _c turns |
| 48 | the argument into a compile-time number, which is needed to return |
| 49 | different `bin_type`s for different axes |
| 50 | - `bin_type` usually is a semi-open interval representing the bin, whose |
| 51 | edges can be accessed with methods `lower()` and `upper()`, but the |
| 52 | implementation depends on the axis, please look it up in the reference |
| 53 | */ |
| 54 | std::cout.setf(std::ios_base::fixed); |
| 55 | for (auto it = h.begin(); it != h.end(); ++it) { |
| 56 | const auto bin = it.bin(0_c); |
| 57 | std::cout << "bin " << it.idx(0) << " x in [" << std::setprecision(1) |
| 58 | << std::setw(4) << bin.lower() << ", " << std::setw(4) |
| 59 | << bin.upper() << "): " << std::setprecision(1) << it->value() |
| 60 | << " +/- " << std::setprecision(3) << std::sqrt(it->variance()) |
| 61 | << std::endl; |
| 62 | } |
| 63 | |