Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-19 07:48:53

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
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   // Verify count in bin containing 5.0
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   // Create histogram with variable binning
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   // Fill value that falls in bin [1, 3)
0046   hist.fill({2.0});
0047 
0048   // Access boost histogram
0049   const auto& bh = hist.histogram();
0050   // Check number of regular bins (not including underflow/overflow)
0051   BOOST_CHECK_EQUAL(bh.axis(0).size(), 4);
0052 
0053   // Verify the value is in the correct bin
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   // Verify other bins are empty
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   // Create 2D histogram with variable binning on both axes
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   // Fill multiple entries
0094   hist.fill({2.0, 0.5});
0095   hist.fill({2.0, 0.5});
0096   hist.fill({0.5, -1.5});
0097 
0098   // Access boost histogram
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   // Verify first filled bin (2.0, 0.5) - filled twice
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   // Verify second filled bin (0.5, -1.5) - filled once
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   // Create histogram to test underflow/overflow handling
0118   auto axis = AxisVariant(BoostRegularAxis(10, 0.0, 10.0, "x"));
0119   Histogram1 hist("test_flow", "Test Flow", {axis});
0120 
0121   // Fill values in range, underflow, and overflow
0122   hist.fill({5.0});   // in range
0123   hist.fill({-1.0});  // underflow
0124   hist.fill({15.0});  // overflow
0125 
0126   const auto& bh = hist.histogram();
0127 
0128   // boost::histogram has underflow/overflow bins by default
0129   // Regular bins: 0..9, underflow: -1, overflow: 10
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   // Note: accessing underflow/overflow requires special handling
0135   // which is implementation detail - converters will handle this
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 // Projection tests removed - projections are not yet implemented for
0150 // multi-dimensional Histogram class
0151 
0152 BOOST_AUTO_TEST_SUITE_END()