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 #include "Acts/Utilities/ProtoAxis.hpp"
0013 
0014 using namespace Acts;
0015 using namespace Acts::Experimental;
0016 
0017 BOOST_AUTO_TEST_SUITE(ProfileEfficiencySuite)
0018 
0019 BOOST_AUTO_TEST_CASE(ProfileHistogram_BasicFill) {
0020   ProtoAxis protoAxis(AxisBoundaryType::Bound, 0.0, 10.0, 10);
0021   auto xAxis = BoostVariableAxis(protoAxis.getAxis().getBinEdges(), "x");
0022   ProfileHistogram1 profile("test_prof", "Test Profile", {xAxis}, "y value");
0023 
0024   BOOST_CHECK_EQUAL(profile.name(), "test_prof");
0025   BOOST_CHECK_EQUAL(profile.title(), "Test Profile");
0026   BOOST_CHECK_EQUAL(profile.sampleAxisTitle(), "y value");
0027 
0028   // Fill x=2.5 with y=10.0, y=20.0, y=30.0
0029   profile.fill({2.5}, 10.0);
0030   profile.fill({2.5}, 20.0);
0031   profile.fill({2.5}, 30.0);
0032 
0033   const auto& bh = profile.histogram();
0034   auto xIdx = bh.axis(0).index(2.5);
0035 
0036   // Mean should be 20.0
0037   double mean = bh.at(xIdx).value();
0038   BOOST_CHECK_CLOSE(mean, 20.0, 1e-6);
0039 
0040   // Count should be 3
0041   double count = bh.at(xIdx).count();
0042   BOOST_CHECK_CLOSE(count, 3.0, 1e-10);
0043 }
0044 
0045 BOOST_AUTO_TEST_CASE(ProfileHistogram_MultipleBins) {
0046   ProtoAxis protoAxis(AxisBoundaryType::Bound, -2.5, 2.5, 5);
0047   auto xAxis = BoostVariableAxis(protoAxis.getAxis().getBinEdges(), "eta");
0048   ProfileHistogram1 profile("res_vs_eta", "Residual vs Eta", {xAxis},
0049                             "residual");
0050 
0051   // Fill different eta bins with different mean values
0052   profile.fill({-2.0}, 1.0);  // bin 0
0053   profile.fill({-2.0}, 3.0);
0054   profile.fill({0.0}, 5.0);  // bin 2 (middle)
0055   profile.fill({0.0}, 7.0);
0056   profile.fill({2.0}, 9.0);  // bin 4
0057   profile.fill({2.0}, 11.0);
0058 
0059   const auto& bh = profile.histogram();
0060 
0061   // Check mean in first bin
0062   auto idx0 = bh.axis(0).index(-2.0);
0063   BOOST_CHECK_CLOSE(bh.at(idx0).value(), 2.0, 1e-6);
0064 
0065   // Check mean in middle bin
0066   auto idx2 = bh.axis(0).index(0.0);
0067   BOOST_CHECK_CLOSE(bh.at(idx2).value(), 6.0, 1e-6);
0068 
0069   // Check mean in last bin
0070   auto idx4 = bh.axis(0).index(2.0);
0071   BOOST_CHECK_CLOSE(bh.at(idx4).value(), 10.0, 1e-6);
0072 }
0073 
0074 BOOST_AUTO_TEST_CASE(Efficiency1D_BasicFill) {
0075   ProtoAxis protoAxis(AxisBoundaryType::Bound, -3.0, 3.0, 10);
0076   auto axis =
0077       AxisVariant(BoostVariableAxis(protoAxis.getAxis().getBinEdges(), "eta"));
0078   Efficiency1 eff("eff_vs_eta", "Efficiency vs Eta", {axis});
0079 
0080   BOOST_CHECK_EQUAL(eff.name(), "eff_vs_eta");
0081   BOOST_CHECK_EQUAL(eff.title(), "Efficiency vs Eta");
0082 
0083   // Fill eta=0.5: 7 accepted, 3 failed
0084   for (int i = 0; i < 7; ++i) {
0085     eff.fill({0.5}, true);
0086   }
0087   for (int i = 0; i < 3; ++i) {
0088     eff.fill({0.5}, false);
0089   }
0090 
0091   // Get efficiency for bin containing 0.5
0092   auto binIdx = eff.acceptedHistogram().axis(0).index(0.5);
0093   double accepted = static_cast<double>(eff.acceptedHistogram().at(binIdx));
0094   double total = static_cast<double>(eff.totalHistogram().at(binIdx));
0095 
0096   BOOST_CHECK_CLOSE(accepted, 7.0, 1e-10);
0097   BOOST_CHECK_CLOSE(total, 10.0, 1e-10);
0098   BOOST_CHECK_CLOSE(accepted / total, 0.7, 1e-6);
0099 }
0100 
0101 BOOST_AUTO_TEST_CASE(Efficiency1D_MultipleBins) {
0102   ProtoAxis protoAxis(AxisBoundaryType::Bound, 0.0, 5.0, 5);
0103   auto axis =
0104       AxisVariant(BoostVariableAxis(protoAxis.getAxis().getBinEdges(), "pt"));
0105   Efficiency1 eff("eff_vs_pt", "Efficiency vs pT", {axis});
0106 
0107   // Bin 0: 50% efficiency
0108   eff.fill({0.5}, true);
0109   eff.fill({0.5}, false);
0110 
0111   // Bin 2: 80% efficiency
0112   for (int i = 0; i < 8; ++i) {
0113     eff.fill({2.5}, true);
0114   }
0115   for (int i = 0; i < 2; ++i) {
0116     eff.fill({2.5}, false);
0117   }
0118 
0119   // Bin 4: 100% efficiency
0120   for (int i = 0; i < 5; ++i) {
0121     eff.fill({4.5}, true);
0122   }
0123 
0124   const auto& accepted = eff.acceptedHistogram();
0125   const auto& total = eff.totalHistogram();
0126 
0127   auto idx0 = accepted.axis(0).index(0.5);
0128   BOOST_CHECK_CLOSE(static_cast<double>(accepted.at(idx0)) /
0129                         static_cast<double>(total.at(idx0)),
0130                     0.5, 1e-6);
0131 
0132   auto idx2 = accepted.axis(0).index(2.5);
0133   BOOST_CHECK_CLOSE(static_cast<double>(accepted.at(idx2)) /
0134                         static_cast<double>(total.at(idx2)),
0135                     0.8, 1e-6);
0136 
0137   auto idx4 = accepted.axis(0).index(4.5);
0138   BOOST_CHECK_CLOSE(static_cast<double>(accepted.at(idx4)) /
0139                         static_cast<double>(total.at(idx4)),
0140                     1.0, 1e-6);
0141 }
0142 
0143 BOOST_AUTO_TEST_CASE(Efficiency2D_BasicFill) {
0144   ProtoAxis protoX(AxisBoundaryType::Bound, -2.5, 2.5, 5);
0145   ProtoAxis protoY(AxisBoundaryType::Bound, 0.0, 5.0, 5);
0146   auto xAxis =
0147       AxisVariant(BoostVariableAxis(protoX.getAxis().getBinEdges(), "eta"));
0148   auto yAxis =
0149       AxisVariant(BoostVariableAxis(protoY.getAxis().getBinEdges(), "pt"));
0150   Efficiency2 eff("eff_vs_eta_pt", "Efficiency vs Eta and pT", {xAxis, yAxis});
0151 
0152   BOOST_CHECK_EQUAL(eff.name(), "eff_vs_eta_pt");
0153   BOOST_CHECK_EQUAL(eff.title(), "Efficiency vs Eta and pT");
0154 
0155   // Fill (0.0, 2.5): 3 accepted, 1 failed
0156   eff.fill({0.0, 2.5}, true);
0157   eff.fill({0.0, 2.5}, true);
0158   eff.fill({0.0, 2.5}, true);
0159   eff.fill({0.0, 2.5}, false);
0160 
0161   const auto& accepted = eff.acceptedHistogram();
0162   const auto& total = eff.totalHistogram();
0163 
0164   auto xIdx = accepted.axis(0).index(0.0);
0165   auto yIdx = accepted.axis(1).index(2.5);
0166 
0167   double acceptedCount = static_cast<double>(accepted.at(xIdx, yIdx));
0168   double totalCount = static_cast<double>(total.at(xIdx, yIdx));
0169 
0170   BOOST_CHECK_CLOSE(acceptedCount, 3.0, 1e-10);
0171   BOOST_CHECK_CLOSE(totalCount, 4.0, 1e-10);
0172   BOOST_CHECK_CLOSE(acceptedCount / totalCount, 0.75, 1e-6);
0173 }
0174 
0175 BOOST_AUTO_TEST_SUITE_END()