File indexing completed on 2026-04-19 07:48:53
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/unit_test.hpp>
0010
0011 #include "Acts/Utilities/Histogram.hpp"
0012
0013 #include <vector>
0014
0015 using namespace Acts;
0016 using namespace Acts::Experimental;
0017
0018 BOOST_AUTO_TEST_SUITE(HistogramSuite)
0019
0020 BOOST_AUTO_TEST_CASE(Histogram1D_UniformBinning) {
0021 auto axis = AxisVariant(BoostRegularAxis(10, 0.0, 10.0, "x"));
0022 Histogram1 hist("test", "Test Histogram", {axis});
0023
0024 BOOST_CHECK_EQUAL(hist.name(), "test");
0025 BOOST_CHECK_EQUAL(hist.title(), "Test Histogram");
0026
0027 hist.fill({5.0});
0028 hist.fill({5.0});
0029
0030 const auto& bh = hist.histogram();
0031 BOOST_CHECK_EQUAL(bh.axis(0).size(), 10);
0032
0033
0034 auto binIndex = bh.axis(0).index(5.0);
0035 double binContent = bh.at(binIndex);
0036 BOOST_CHECK_CLOSE(binContent, 2.0, 1e-10);
0037 }
0038
0039 BOOST_AUTO_TEST_CASE(Histogram1D_VariableBinning) {
0040
0041 std::vector<double> edges = {0.0, 1.0, 3.0, 7.0, 10.0};
0042 auto axis = AxisVariant(BoostVariableAxis(edges, "x"));
0043 Histogram1 hist("test_var", "Test Variable Binning", {axis});
0044
0045
0046 hist.fill({2.0});
0047
0048
0049 const auto& bh = hist.histogram();
0050
0051 BOOST_CHECK_EQUAL(bh.axis(0).size(), 4);
0052
0053
0054 auto binIndex = bh.axis(0).index(2.0);
0055 BOOST_CHECK_EQUAL(binIndex, 1);
0056 double binContent = bh.at(binIndex);
0057 BOOST_CHECK_CLOSE(binContent, 1.0, 1e-10);
0058
0059
0060 for (int i = 0; i < bh.axis(0).size(); ++i) {
0061 if (i != binIndex) {
0062 double content = bh.at(i);
0063 BOOST_CHECK_EQUAL(content, 0.0);
0064 }
0065 }
0066 }
0067
0068 BOOST_AUTO_TEST_CASE(Histogram2D_FillAndAccess) {
0069 auto xAxis = AxisVariant(BoostRegularAxis(10, 0.0, 10.0, "x"));
0070 auto yAxis = AxisVariant(BoostRegularAxis(10, -5.0, 5.0, "y"));
0071 Histogram2 hist("test_2d", "Test 2D Histogram", {xAxis, yAxis});
0072
0073 BOOST_CHECK_EQUAL(hist.name(), "test_2d");
0074 BOOST_CHECK_EQUAL(hist.title(), "Test 2D Histogram");
0075
0076 hist.fill({5.0, 2.0});
0077
0078 const auto& bh = hist.histogram();
0079 auto xIdx = bh.axis(0).index(5.0);
0080 auto yIdx = bh.axis(1).index(2.0);
0081 double binContent = bh.at(xIdx, yIdx);
0082 BOOST_CHECK_CLOSE(binContent, 1.0, 1e-10);
0083 }
0084
0085 BOOST_AUTO_TEST_CASE(Histogram2D_VariableBinning) {
0086
0087 std::vector<double> xEdges = {0.0, 1.0, 3.0, 5.0};
0088 std::vector<double> yEdges = {-2.0, -1.0, 0.0, 1.0, 2.0};
0089 auto xAxis = AxisVariant(BoostVariableAxis(xEdges, "eta"));
0090 auto yAxis = AxisVariant(BoostVariableAxis(yEdges, "res"));
0091 Histogram2 hist("res_vs_eta", "Residual vs Eta", {xAxis, yAxis});
0092
0093
0094 hist.fill({2.0, 0.5});
0095 hist.fill({2.0, 0.5});
0096 hist.fill({0.5, -1.5});
0097
0098
0099 const auto& bh = hist.histogram();
0100 BOOST_CHECK_EQUAL(bh.axis(0).size(), 3);
0101 BOOST_CHECK_EQUAL(bh.axis(1).size(), 4);
0102
0103
0104 auto xIdx1 = bh.axis(0).index(2.0);
0105 auto yIdx1 = bh.axis(1).index(0.5);
0106 double binContent1 = bh.at(xIdx1, yIdx1);
0107 BOOST_CHECK_CLOSE(binContent1, 2.0, 1e-10);
0108
0109
0110 auto xIdx2 = bh.axis(0).index(0.5);
0111 auto yIdx2 = bh.axis(1).index(-1.5);
0112 double binContent2 = bh.at(xIdx2, yIdx2);
0113 BOOST_CHECK_CLOSE(binContent2, 1.0, 1e-10);
0114 }
0115
0116 BOOST_AUTO_TEST_CASE(Histogram1D_UnderflowOverflow) {
0117
0118 auto axis = AxisVariant(BoostRegularAxis(10, 0.0, 10.0, "x"));
0119 Histogram1 hist("test_flow", "Test Flow", {axis});
0120
0121
0122 hist.fill({5.0});
0123 hist.fill({-1.0});
0124 hist.fill({15.0});
0125
0126 const auto& bh = hist.histogram();
0127
0128
0129
0130 auto inRangeIdx = bh.axis(0).index(5.0);
0131 double binContent = bh.at(inRangeIdx);
0132 BOOST_CHECK_CLOSE(binContent, 1.0, 1e-10);
0133
0134
0135
0136 }
0137
0138 BOOST_AUTO_TEST_CASE(Histogram1D_EmptyHistogram) {
0139 auto axis = AxisVariant(BoostRegularAxis(10, -5.0, 5.0, "x"));
0140 Histogram1 hist("empty", "Empty Histogram", {axis});
0141
0142 const auto& bh = hist.histogram();
0143 for (int i = 0; i < bh.axis(0).size(); ++i) {
0144 double content = bh.at(i);
0145 BOOST_CHECK_EQUAL(content, 0.0);
0146 }
0147 }
0148
0149
0150
0151
0152 BOOST_AUTO_TEST_SUITE_END()