ATLAS Offline Software
Loading...
Searching...
No Matches
test-histogram.cxx File Reference
#include "HDF5Utils/histogram.h"
#include "H5Cpp.h"
Include dependency graph for test-histogram.cxx:

Go to the source code of this file.

Functions

int main (int, char *[])

Function Documentation

◆ main()

int main ( int ,
char * [] )

Definition at line 9 of file test-histogram.cxx.

9 {
10
11 namespace bh = boost::histogram;
12 namespace h5h = H5Utils::hist;
13 using def = bh::use_default;
14
15 using dax_t = bh::axis::regular<double>;
16 using daxn_t = bh::axis::regular<double, def, def, bh::axis::option::none_t>;
17 using iax_t = bh::axis::integer<int>;
18 using cax_t = bh::axis::category<short, def, bh::axis::option::overflow_t>;
19
20 H5::H5File out_file("hists.h5", H5F_ACC_TRUNC);
21
22 // build weighted hist
23 {
24 auto h3dw = bh::make_weighted_histogram(
25 daxn_t(1, -0.5, 0.5, "ax0"),
26 dax_t(1, -0.5, 0.5, "ax1"),
27 iax_t(-1, 1, "ax2"));
28 // this should put a 0.5 at [0, 1, 2] when all the overflows are
29 // accounted for
30 h3dw(0, 0, 0, bh::weight(0.5));
31 // this is to test the overflow bin
32 h3dw(0, 0, 20, bh::weight(10));
33 h5h::write_hist_to_group(out_file, h3dw, "h3dw");
34 }
35
36 // build unweighted hist
37 {
38 auto h3d = bh::make_histogram(
39 daxn_t(1, -0.5, 0.5, "ax0"),
40 dax_t(1, -0.5, 0.5, "ax1"),
41 iax_t(-1, 1, "ax2"));
42 h3d(0, 0, 0);
43 h5h::write_hist_to_group(out_file, h3d, "h3d");
44 }
45
46 // build weighted profile hist
47 {
48 auto h3dp = bh::make_weighted_profile(
49 daxn_t(1, -0.5, 0.5, "ax0"),
50 dax_t(1, -0.5, 0.5, "ax1"),
51 iax_t(-1, 1, "ax2"));
52 // we need two calls here to check the variance too
53 h3dp(0, 0, 0, bh::weight(0.5), bh::sample(1.0));
54 h3dp(0, 0, 0, bh::weight(0.5), bh::sample(1.0));
55 h5h::write_hist_to_group(out_file, h3dp, "h3dp");
56 }
57
58 // build a dyanmic hist
59 {
60 using variant = bh::axis::variant<dax_t, daxn_t, iax_t>;
61 std::vector<variant> axes {
62 daxn_t(1, -0.5, 0.5, "ax0"),
63 dax_t(1, -0.5, 0.5, "ax1"),
64 iax_t(-1, 1, "ax2")
65 };
66 auto hdyn = bh::make_weighted_histogram(axes);
67 // seems we need to do some funny organization to make weighting
68 // work with dynamic axes.
69 std::vector<std::vector<double>> vals { {0}, {0}, {0} };
70 hdyn.fill(vals, bh::weight(0.5));
71 h5h::write_hist_to_group(out_file, hdyn, "hdyn");
72 }
73
74 // integer storage
75 {
76 using int_storage = bh::dense_storage<int64_t>;
77 auto h1i = bh::make_histogram_with(
78 int_storage{},
79 dax_t(3, -1.5, 1.5, "x"));
80 h1i(-1.0); // bin 0
81 h1i(0.0); // bin 1
82 h1i(0.0); // bin 1 again
83 h5h::write_hist_to_group(out_file, h1i, "h1i");
84 }
85
86 // thread-safe (atomic) integer storage
87 {
88 using atomic_int_storage =
89 bh::dense_storage<bh::accumulators::count<int64_t, true>>;
90 auto h1a = bh::make_histogram_with(
91 atomic_int_storage{},
92 dax_t(3, -1.5, 1.5, "x"));
93 h1a(0.0); // bin 1
94 h1a(1.0); // bin 2
95 h5h::write_hist_to_group(out_file, h1a, "h1a");
96 }
97
98 // thread-safe double storage
99 {
100 using atomic_double_storage =
101 bh::dense_storage<bh::accumulators::count<double, true>>;
102 auto h1td = bh::make_histogram_with(
103 atomic_double_storage{},
104 dax_t(3, -1.5, 1.5, "x"));
105 h1td(-1.0); // bin 0
106 h1td(0.0); // bin 1
107 h1td(0.0); // bin 1 again
108 h5h::write_hist_to_group(out_file, h1td, "h1td");
109 }
110
111 // thread-safe float storage
112 {
113 using atomic_float_storage =
114 bh::dense_storage<bh::accumulators::count<float, true>>;
115 auto h1tf = bh::make_histogram_with(
116 atomic_float_storage{},
117 dax_t(3, -1.5, 1.5, "x"));
118 h1tf(0.0); // bin 1
119 h1tf(1.0); // bin 2
120 h1tf(1.0); // bin 2 again
121 h5h::write_hist_to_group(out_file, h1tf, "h1tf");
122 }
123
124 // categorical axes test
125 {
126 // flavor labels histogram
127 auto h1c = bh::make_histogram(
128 cax_t({0, 4, 5, 15}, "flavorTruthLabel") );
129 h1c(5); // add a b-hadron
130 h1c(20); // no idea what, test overflow
131 h5h::write_hist_to_group(out_file, h1c, "h1c");
132 }
133
134 // unsigned char (uint8) storage
135 {
136 using uint8_storage = bh::dense_storage<uint8_t>;
137 auto h1u8 = bh::make_histogram_with(
138 uint8_storage{}, dax_t(3, -1.5, 1.5, "x"));
139 h1u8(0.0); // bin 1, storage index 2
140 h1u8(1.0); // bin 2, storage index 3
141 h5h::write_hist_to_group(out_file, h1u8, "h1u8");
142 }
143
144 // unsigned short (uint16) storage
145 {
146 using uint16_storage = bh::dense_storage<uint16_t>;
147 auto h1u16 = bh::make_histogram_with(
148 uint16_storage{}, dax_t(3, -1.5, 1.5, "x"));
149 h1u16(0.0); // bin 1, storage index 2
150 h1u16(1.0); // bin 2, storage index 3
151 h5h::write_hist_to_group(out_file, h1u16, "h1u16");
152 }
153
154 return 0;
155}