Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-12 07:53:00

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