Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:12:30

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