Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 07:53:06

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/Clusterization/TimedClusterization.hpp"
0012 
0013 #include <algorithm>
0014 
0015 namespace Acts::Test {
0016 
0017 // Define objects
0018 using Identifier = std::size_t;
0019 struct Cell {
0020   Cell(Identifier identifier, int c, int r, double t)
0021       : id(identifier), column(c), row(r), time(t) {}
0022 
0023   Identifier id{};
0024   int column{0};
0025   int row{0};
0026   int label{-1};
0027   double time{0.};
0028 };
0029 
0030 struct Cluster {
0031   std::vector<Identifier> ids{};
0032 };
0033 
0034 using CellCollection = std::vector<Acts::Test::Cell>;
0035 using ClusterCollection = std::vector<Acts::Test::Cluster>;
0036 
0037 // Define functions
0038 static inline int getCellRow(const Cell& cell) {
0039   return cell.row;
0040 }
0041 
0042 static inline int getCellColumn(const Cell& cell) {
0043   return cell.column;
0044 }
0045 
0046 static inline int& getCellLabel(Cell& cell) {
0047   return cell.label;
0048 }
0049 
0050 static inline double getCellTime(const Cell& cell) {
0051   return cell.time;
0052 }
0053 
0054 static void clusterAddCell(Cluster& cl, const Cell& cell) {
0055   cl.ids.push_back(cell.id);
0056 }
0057 
0058 BOOST_AUTO_TEST_CASE(TimedGrid_1D_withtime) {
0059   // 1x10 matrix
0060   /*
0061     X X X Y O X Y Y X X
0062   */
0063   // 6 + 3 cells -> 3 + 2 clusters in total
0064 
0065   std::vector<Cell> cells;
0066   // X
0067   cells.emplace_back(0ul, 0, -1, 0);
0068   cells.emplace_back(1ul, 1, -1, 0);
0069   cells.emplace_back(2ul, 2, -1, 0);
0070   cells.emplace_back(3ul, 5, -1, 0);
0071   cells.emplace_back(4ul, 8, -1, 0);
0072   cells.emplace_back(5ul, 9, -1, 0);
0073   // Y
0074   cells.emplace_back(6ul, 3, 0, 1);
0075   cells.emplace_back(7ul, 6, 1, 1);
0076   cells.emplace_back(8ul, 7, 1, 1);
0077 
0078   std::vector<std::vector<Identifier>> expectedResults;
0079   expectedResults.push_back({0ul, 1ul, 2ul});
0080   expectedResults.push_back({6ul});
0081   expectedResults.push_back({3ul});
0082   expectedResults.push_back({7ul, 8ul});
0083   expectedResults.push_back({4ul, 5ul});
0084 
0085   ClusterCollection clusters =
0086       Acts::Ccl::createClusters<CellCollection, ClusterCollection, 1>(
0087           cells, Acts::Ccl::TimedConnect<Cell, 1>(0.5));
0088 
0089   BOOST_CHECK_EQUAL(5ul, clusters.size());
0090 
0091   for (std::size_t i(0); i < clusters.size(); ++i) {
0092     std::vector<Identifier>& timedIds = clusters[i].ids;
0093     const std::vector<Identifier>& expected = expectedResults[i];
0094     std::ranges::sort(timedIds);
0095     BOOST_CHECK_EQUAL(timedIds.size(), expected.size());
0096 
0097     for (std::size_t j(0); j < timedIds.size(); ++j) {
0098       BOOST_CHECK_EQUAL(timedIds[j], expected[j]);
0099     }
0100   }
0101 }
0102 
0103 BOOST_AUTO_TEST_CASE(TimedGrid_2D_notime) {
0104   // 4x4 matrix
0105   /*
0106     X O O X
0107     O O O X
0108     X X O O
0109     X O O X
0110   */
0111   // 7 cells -> 4 clusters in total
0112 
0113   std::vector<Cell> cells;
0114   cells.emplace_back(0ul, 0, 0, 0);
0115   cells.emplace_back(1ul, 3, 0, 0);
0116   cells.emplace_back(2ul, 3, 1, 0);
0117   cells.emplace_back(3ul, 0, 2, 0);
0118   cells.emplace_back(4ul, 1, 2, 0);
0119   cells.emplace_back(5ul, 0, 3, 0);
0120   cells.emplace_back(6ul, 3, 3, 0);
0121 
0122   std::vector<std::vector<Identifier>> expectedResults;
0123   expectedResults.push_back({0ul});
0124   expectedResults.push_back({3ul, 4ul, 5ul});
0125   expectedResults.push_back({1ul, 2ul});
0126   expectedResults.push_back({6ul});
0127 
0128   ClusterCollection clusters =
0129       Acts::Ccl::createClusters<CellCollection, ClusterCollection, 2>(
0130           cells,
0131           Acts::Ccl::TimedConnect<Cell, 2>(std::numeric_limits<double>::max()));
0132 
0133   BOOST_CHECK_EQUAL(4ul, clusters.size());
0134 
0135   // Compare against default connect (only space)
0136   ClusterCollection defaultClusters =
0137       Acts::Ccl::createClusters<CellCollection, ClusterCollection, 2>(
0138           cells, Acts::Ccl::DefaultConnect<Cell, 2>());
0139 
0140   BOOST_CHECK_EQUAL(4ul, defaultClusters.size());
0141   BOOST_CHECK_EQUAL(defaultClusters.size(), expectedResults.size());
0142 
0143   std::vector<std::size_t> sizes{1, 3, 2, 1};
0144   for (std::size_t i(0); i < clusters.size(); ++i) {
0145     std::vector<Identifier>& timedIds = clusters[i].ids;
0146     std::vector<Identifier>& defaultIds = defaultClusters[i].ids;
0147     const std::vector<Identifier>& expected = expectedResults[i];
0148     BOOST_CHECK_EQUAL(timedIds.size(), defaultIds.size());
0149     BOOST_CHECK_EQUAL(timedIds.size(), sizes[i]);
0150     BOOST_CHECK_EQUAL(timedIds.size(), expected.size());
0151 
0152     std::ranges::sort(timedIds);
0153     std::ranges::sort(defaultIds);
0154     for (std::size_t j(0); j < timedIds.size(); ++j) {
0155       BOOST_CHECK_EQUAL(timedIds[j], defaultIds[j]);
0156       BOOST_CHECK_EQUAL(timedIds[j], expected[j]);
0157     }
0158   }
0159 }
0160 
0161 BOOST_AUTO_TEST_CASE(TimedGrid_2D_withtime) {
0162   // 4x4 matrix
0163   /*
0164     X Y O X
0165     O Y Y X
0166     X X Z Z
0167     X O O X
0168   */
0169   // 7 + 3 + 2 cells -> 4 + 1 + 1 clusters in total
0170 
0171   std::vector<Cell> cells;
0172   // X
0173   cells.emplace_back(0ul, 0, 0, 0);
0174   cells.emplace_back(1ul, 3, 0, 0);
0175   cells.emplace_back(2ul, 3, 1, 0);
0176   cells.emplace_back(3ul, 0, 2, 0);
0177   cells.emplace_back(4ul, 1, 2, 0);
0178   cells.emplace_back(5ul, 0, 3, 0);
0179   cells.emplace_back(6ul, 3, 3, 0);
0180   // Y
0181   cells.emplace_back(7ul, 1, 0, 1);
0182   cells.emplace_back(8ul, 1, 1, 1);
0183   cells.emplace_back(9ul, 2, 1, 1);
0184   // Z
0185   cells.emplace_back(10ul, 2, 2, 2);
0186   cells.emplace_back(11ul, 3, 2, 2);
0187 
0188   std::vector<std::vector<Identifier>> expectedResults;
0189   expectedResults.push_back({0ul});
0190   expectedResults.push_back({3ul, 4ul, 5ul});
0191   expectedResults.push_back({7ul, 8ul, 9ul});
0192   expectedResults.push_back({10ul, 11ul});
0193   expectedResults.push_back({1ul, 2ul});
0194   expectedResults.push_back({6ul});
0195 
0196   ClusterCollection clusters =
0197       Acts::Ccl::createClusters<CellCollection, ClusterCollection, 2>(
0198           cells, Acts::Ccl::TimedConnect<Cell, 2>(0.5));
0199 
0200   BOOST_CHECK_EQUAL(6ul, clusters.size());
0201 
0202   std::vector<std::size_t> sizes{1, 3, 3, 2, 2, 1};
0203   for (std::size_t i(0); i < clusters.size(); ++i) {
0204     std::vector<Identifier>& timedIds = clusters[i].ids;
0205     BOOST_CHECK_EQUAL(timedIds.size(), sizes[i]);
0206     std::ranges::sort(timedIds);
0207 
0208     const std::vector<Identifier>& expected = expectedResults[i];
0209     BOOST_CHECK_EQUAL(timedIds.size(), expected.size());
0210 
0211     for (std::size_t j(0); j < timedIds.size(); ++j) {
0212       BOOST_CHECK_EQUAL(timedIds[j], expected[j]);
0213     }
0214   }
0215 }
0216 
0217 BOOST_AUTO_TEST_CASE(TimedGrid_2D_noTollerance) {
0218   // 4x4 matrix
0219   /*
0220     X O O X
0221     O O O X
0222     X X O O
0223     X O O X
0224    */
0225   // 7 cells -> 7 clusters in total
0226   // since time requirement will never be satisfied
0227 
0228   std::vector<Cell> cells;
0229   cells.emplace_back(0ul, 0, 0, 0);
0230   cells.emplace_back(1ul, 3, 0, 0);
0231   cells.emplace_back(2ul, 3, 1, 0);
0232   cells.emplace_back(3ul, 0, 2, 0);
0233   cells.emplace_back(4ul, 1, 2, 0);
0234   cells.emplace_back(5ul, 0, 3, 0);
0235   cells.emplace_back(6ul, 3, 3, 0);
0236 
0237   std::vector<std::vector<Identifier>> expectedResults;
0238   expectedResults.push_back({0ul});
0239   expectedResults.push_back({3ul});
0240   expectedResults.push_back({5ul});
0241   expectedResults.push_back({4ul});
0242   expectedResults.push_back({1ul});
0243   expectedResults.push_back({2ul});
0244   expectedResults.push_back({6ul});
0245 
0246   ClusterCollection clusters =
0247       Acts::Ccl::createClusters<CellCollection, ClusterCollection, 2>(
0248           cells, Acts::Ccl::TimedConnect<Cell, 2>(0.));
0249 
0250   BOOST_CHECK_EQUAL(7ul, clusters.size());
0251 
0252   for (std::size_t i(0); i < clusters.size(); ++i) {
0253     std::vector<Identifier>& timedIds = clusters[i].ids;
0254     const std::vector<Identifier>& expected = expectedResults[i];
0255 
0256     BOOST_CHECK_EQUAL(timedIds.size(), 1);
0257     BOOST_CHECK_EQUAL(timedIds.size(), expected.size());
0258     BOOST_CHECK_EQUAL(timedIds[0], expected[0]);
0259   }
0260 }
0261 
0262 }  // namespace Acts::Test