Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-17 07:36:12

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/Clusterization.hpp"
0012 #include "Acts/Utilities/HashCombine.hpp"
0013 
0014 #include <algorithm>
0015 #include <cstdlib>
0016 #include <iostream>
0017 #include <memory>
0018 #include <random>
0019 #include <utility>
0020 #include <vector>
0021 
0022 using namespace Acts;
0023 
0024 struct Cell1D {
0025   explicit Cell1D(int colv) : col(colv) {}
0026   int col;
0027   Ccl::Label label{Ccl::NO_LABEL};
0028 };
0029 
0030 bool cellComp(const Cell1D& left, const Cell1D& right) {
0031   return left.col < right.col;
0032 }
0033 
0034 int getCellColumn(const Cell1D& cell) {
0035   return cell.col;
0036 }
0037 
0038 struct Cluster1D {
0039   std::vector<Cell1D> cells;
0040   std::size_t hash{};
0041 };
0042 
0043 void clusterAddCell(Cluster1D& cl, const Cell1D& cell) {
0044   cl.cells.push_back(cell);
0045 }
0046 
0047 bool clHashComp(const Cluster1D& left, const Cluster1D& right) {
0048   return left.hash < right.hash;
0049 }
0050 
0051 void hash(Cluster1D& cl) {
0052   std::ranges::sort(cl.cells, cellComp);
0053   cl.hash = 0;
0054   for (const Cell1D& c : cl.cells) {
0055     cl.hash = Acts::hashMixAndCombine(cl.hash, c.col);
0056   }
0057 }
0058 
0059 namespace ActsTests {
0060 
0061 BOOST_AUTO_TEST_SUITE(ClusterizationSuite)
0062 
0063 BOOST_AUTO_TEST_CASE(Grid_1D_rand) {
0064   using Cell = Cell1D;
0065   using CellC = std::vector<Cell>;
0066   using Cluster = Cluster1D;
0067   using ClusterC = std::vector<Cluster>;
0068 
0069   std::size_t minsize = 1;
0070   std::size_t maxsize = 10;
0071   std::size_t minspace = 1;
0072   std::size_t maxspace = 10;
0073   std::size_t nclusters = 100;
0074   int startSeed = 204769;
0075   std::size_t ntries = 100;
0076 
0077   std::cout << "Grid_1D_rand test with parameters:" << std::endl;
0078   std::cout << "  minsize = " << minsize << std::endl;
0079   std::cout << "  maxsize = " << maxsize << std::endl;
0080   std::cout << "  minspace = " << minspace << std::endl;
0081   std::cout << "  maxspace = " << maxspace << std::endl;
0082   std::cout << "  nclusters = " << nclusters << std::endl;
0083   std::cout << "  startSeed = " << startSeed << std::endl;
0084   std::cout << "  ntries = " << ntries << std::endl;
0085 
0086   while (ntries-- > 0) {
0087     std::mt19937_64 rnd(startSeed++);
0088     std::uniform_int_distribution<std::uint32_t> distr_size(minsize, maxsize);
0089     std::uniform_int_distribution<std::uint32_t> distr_space(minspace,
0090                                                              maxspace);
0091 
0092     int col = 0;
0093 
0094     CellC cells;
0095     ClusterC clusters;
0096     for (std::size_t i = 0; i < nclusters; i++) {
0097       Cluster cl;
0098       col += distr_space(rnd);
0099       std::uint32_t size = distr_size(rnd);
0100       for (std::uint32_t j = 0; j < size; j++) {
0101         Cell cell(col++);
0102         cells.push_back(cell);
0103         clusterAddCell(cl, cell);
0104       }
0105       clusters.push_back(std::move(cl));
0106     }
0107     for (Cluster& cl : clusters) {
0108       hash(cl);
0109     }
0110 
0111     std::shuffle(cells.begin(), cells.end(), rnd);
0112 
0113     Ccl::ClusteringData data;
0114     ClusterC newCls;
0115     Ccl::createClusters<CellC, ClusterC, 1>(data, cells, newCls);
0116 
0117     for (Cluster& cl : newCls) {
0118       hash(cl);
0119     }
0120 
0121     std::ranges::sort(clusters, clHashComp);
0122     std::ranges::sort(newCls, clHashComp);
0123 
0124     BOOST_CHECK_EQUAL(clusters.size(), newCls.size());
0125     for (std::size_t i = 0; i < clusters.size(); i++) {
0126       BOOST_CHECK_EQUAL(clusters.at(i).hash, newCls.at(i).hash);
0127     }
0128   }
0129 }
0130 
0131 BOOST_AUTO_TEST_CASE(Grid_1D_duplicate_cells) {
0132   using Cell = Cell1D;
0133   using CellC = std::vector<Cell>;
0134   using Cluster = Cluster1D;
0135   using ClusterC = std::vector<Cluster>;
0136 
0137   CellC cells = {Cell(42), Cell(42)};
0138   ClusterC clusters;
0139 
0140   Ccl::ClusteringData data;
0141 
0142   BOOST_CHECK_THROW(
0143       (Ccl::createClusters<CellC, ClusterC, 1>(data, cells, clusters)),
0144       std::invalid_argument);
0145 }
0146 
0147 BOOST_AUTO_TEST_SUITE_END()
0148 
0149 }  // namespace ActsTests