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