Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-01 07:54:22

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/Plugins/ExaTrkX/TruthGraphMetricsHook.hpp"
0012 
0013 #include <cassert>
0014 
0015 void testTruthTestGraph(std::vector<std::int64_t> &truthGraph,
0016                         std::vector<std::int64_t> &testGraph,
0017                         const std::string &resStr) {
0018   std::stringstream ss;
0019   auto logger = Acts::getDefaultLogger("Test", Acts::Logging::INFO, &ss);
0020 
0021   Acts::TruthGraphMetricsHook hook(truthGraph, std::move(logger));
0022 
0023   auto numTestEdges = testGraph.size() / 2;
0024   auto edgeIndexTensor = Acts::Tensor<std::int64_t>::Create(
0025       {2, numTestEdges}, {Acts::Device::Cpu(), {}});
0026 
0027   // Transpose the input vector into the tensor
0028   for (auto i = 0ul; i < numTestEdges; ++i) {
0029     *(edgeIndexTensor.data() + i) = testGraph.at(2 * i);
0030     *(edgeIndexTensor.data() + numTestEdges + i) = testGraph.at(2 * i + 1);
0031   }
0032 
0033   Acts::PipelineTensors tensors{
0034       Acts::Tensor<float>::Create({1, 1}, {Acts::Device::Cpu(), {}}),
0035       std::move(edgeIndexTensor),
0036       {},
0037       {}};
0038 
0039   hook(tensors, {Acts::Device::Cpu(), {}});
0040 
0041   const auto str = ss.str();
0042 
0043   auto begin = str.begin() + str.find("Efficiency");
0044   BOOST_CHECK_EQUAL(std::string(begin, str.end() - 1), resStr);
0045 }
0046 
0047 BOOST_AUTO_TEST_CASE(same_graph) {
0048   // clang-format off
0049   std::vector<std::int64_t> truthGraph = {
0050     1,2,
0051     2,3,
0052     3,4,
0053     4,5,
0054   };
0055   // clang-format on
0056 
0057   // clang-format off
0058   std::vector<std::int64_t> testGraph = {
0059     3,4,
0060     4,5,
0061     1,2,
0062     2,3,
0063   };
0064   // clang-format on
0065 
0066   testTruthTestGraph(truthGraph, testGraph, "Efficiency=1, purity=1");
0067 }
0068 
0069 // Test large numbers because overflows are a danger for cantor pairing
0070 BOOST_AUTO_TEST_CASE(same_graph_large_numbers) {
0071   // clang-format off
0072   std::int64_t k = 100'000;
0073 
0074   std::vector<std::int64_t> truthGraph = {
0075     1,2,
0076     2,3,
0077     3,4,
0078     4,5,
0079   };
0080   // clang-format on
0081   std::transform(truthGraph.begin(), truthGraph.end(), truthGraph.begin(),
0082                  [&](auto i) { return k + i; });
0083 
0084   // clang-format off
0085   std::vector<std::int64_t> testGraph = {
0086     3,4,
0087     4,5,
0088     1,2,
0089     2,3,
0090   };
0091   // clang-format on
0092   std::transform(testGraph.begin(), testGraph.end(), testGraph.begin(),
0093                  [&](auto i) { return k + i; });
0094 
0095   testTruthTestGraph(truthGraph, testGraph, "Efficiency=1, purity=1");
0096 }
0097 
0098 BOOST_AUTO_TEST_CASE(fifty_fifty) {
0099   // clang-format off
0100   std::vector<std::int64_t> truthGraph = {
0101     1,2,
0102     2,3,
0103     3,4,
0104     4,5,
0105   };
0106   // clang-format on
0107 
0108   // clang-format off
0109   std::vector<std::int64_t> testGraph = {
0110     3,4,
0111     4,5,
0112     6,9,
0113     5,1,
0114   };
0115   // clang-format on
0116 
0117   testTruthTestGraph(truthGraph, testGraph, "Efficiency=0.5, purity=0.5");
0118 }