Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:16

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2022 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Clusterization/Clusterization.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 #include <boost/functional/hash.hpp>
0023 
0024 namespace Acts::Test {
0025 
0026 struct Cell1D {
0027   Cell1D(int colv) : col(colv) {}
0028   int col;
0029   Ccl::Label label{Ccl::NO_LABEL};
0030 };
0031 
0032 bool cellComp(const Cell1D& left, const Cell1D& right) {
0033   return left.col < right.col;
0034 }
0035 
0036 Ccl::Label& getCellLabel(Cell1D& cell) {
0037   return cell.label;
0038 }
0039 
0040 int getCellColumn(const Cell1D& cell) {
0041   return cell.col;
0042 }
0043 
0044 struct Cluster1D {
0045   std::vector<Cell1D> cells;
0046   std::size_t hash{};
0047 };
0048 
0049 void clusterAddCell(Cluster1D& cl, const Cell1D& cell) {
0050   cl.cells.push_back(cell);
0051 }
0052 
0053 bool clHashComp(const Cluster1D& left, const Cluster1D& right) {
0054   return left.hash < right.hash;
0055 }
0056 
0057 void hash(Cluster1D& cl) {
0058   std::sort(cl.cells.begin(), cl.cells.end(), cellComp);
0059   cl.hash = 0;
0060   for (const Cell1D& c : cl.cells) {
0061     boost::hash_combine(cl.hash, c.col);
0062   }
0063 }
0064 
0065 BOOST_AUTO_TEST_CASE(Grid_1D_rand) {
0066   using Cell = Cell1D;
0067   using CellC = std::vector<Cell>;
0068   using Cluster = Cluster1D;
0069   using ClusterC = std::vector<Cluster>;
0070 
0071   std::size_t minsize = 1;
0072   std::size_t maxsize = 10;
0073   std::size_t minspace = 1;
0074   std::size_t maxspace = 10;
0075   std::size_t nclusters = 100;
0076   int startSeed = 204769;
0077   std::size_t ntries = 100;
0078 
0079   std::cout << "Grid_1D_rand test with parameters:" << std::endl;
0080   std::cout << "  minsize = " << minsize << std::endl;
0081   std::cout << "  maxsize = " << maxsize << std::endl;
0082   std::cout << "  minspace = " << minspace << std::endl;
0083   std::cout << "  maxspace = " << maxspace << std::endl;
0084   std::cout << "  nclusters = " << nclusters << std::endl;
0085   std::cout << "  startSeed = " << startSeed << std::endl;
0086   std::cout << "  ntries = " << ntries << std::endl;
0087 
0088   while (ntries-- > 0) {
0089     std::mt19937_64 rnd(startSeed++);
0090     std::uniform_int_distribution<std::uint32_t> distr_size(minsize, maxsize);
0091     std::uniform_int_distribution<std::uint32_t> distr_space(minspace,
0092                                                              maxspace);
0093 
0094     int col = 0;
0095 
0096     CellC cells;
0097     ClusterC clusters;
0098     for (std::size_t i = 0; i < nclusters; i++) {
0099       Cluster cl;
0100       col += distr_space(rnd);
0101       std::uint32_t size = distr_size(rnd);
0102       for (std::uint32_t j = 0; j < size; j++) {
0103         Cell cell(col++);
0104         cells.push_back(cell);
0105         clusterAddCell(cl, cell);
0106       }
0107       clusters.push_back(std::move(cl));
0108     }
0109     for (Cluster& cl : clusters) {
0110       hash(cl);
0111     }
0112 
0113     std::shuffle(cells.begin(), cells.end(), rnd);
0114 
0115     ClusterC newCls = Ccl::createClusters<CellC, ClusterC, 1>(cells);
0116 
0117     for (Cluster& cl : newCls) {
0118       hash(cl);
0119     }
0120 
0121     std::sort(clusters.begin(), clusters.end(), clHashComp);
0122     std::sort(newCls.begin(), newCls.end(), 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 }  // namespace Acts::Test