Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ClusterizationTests1D.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ClusterizationTests1D.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
11 
13 
14 #include <algorithm>
15 #include <cstdlib>
16 #include <iostream>
17 #include <memory>
18 #include <random>
19 #include <utility>
20 #include <vector>
21 
22 #include <boost/functional/hash.hpp>
23 
24 namespace Acts {
25 namespace Test {
26 
27 struct Cell1D {
28  Cell1D(int colv) : col(colv) {}
29  int col;
31 };
32 
33 bool cellComp(const Cell1D& left, const Cell1D& right) {
34  return left.col < right.col;
35 }
36 
38  return cell.label;
39 }
40 
41 int getCellColumn(const Cell1D& cell) {
42  return cell.col;
43 }
44 
45 struct Cluster1D {
46  std::vector<Cell1D> cells;
47  size_t hash{};
48 };
49 
50 void clusterAddCell(Cluster1D& cl, const Cell1D& cell) {
51  cl.cells.push_back(cell);
52 }
53 
54 bool clHashComp(const Cluster1D& left, const Cluster1D& right) {
55  return left.hash < right.hash;
56 }
57 
58 void hash(Cluster1D& cl) {
59  std::sort(cl.cells.begin(), cl.cells.end(), cellComp);
60  cl.hash = 0;
61  for (const Cell1D& c : cl.cells) {
62  boost::hash_combine(cl.hash, c.col);
63  }
64 }
65 
66 BOOST_AUTO_TEST_CASE(Grid_1D_rand) {
67  using Cell = Cell1D;
68  using CellC = std::vector<Cell>;
69  using Cluster = Cluster1D;
70  using ClusterC = std::vector<Cluster>;
71 
72  size_t minsize = 1;
73  size_t maxsize = 10;
74  size_t minspace = 1;
75  size_t maxspace = 10;
76  size_t nclusters = 100;
77  int startSeed = 204769;
78  size_t ntries = 100;
79 
80  std::cout << "Grid_1D_rand test with parameters:" << std::endl;
81  std::cout << " minsize = " << minsize << std::endl;
82  std::cout << " maxsize = " << maxsize << std::endl;
83  std::cout << " minspace = " << minspace << std::endl;
84  std::cout << " maxspace = " << maxspace << std::endl;
85  std::cout << " nclusters = " << nclusters << std::endl;
86  std::cout << " startSeed = " << startSeed << std::endl;
87  std::cout << " ntries = " << ntries << std::endl;
88 
89  while (ntries-- > 0) {
90  std::mt19937_64 rnd(startSeed++);
91  std::uniform_int_distribution<size_t> distr_size(minsize, maxsize);
92  std::uniform_int_distribution<size_t> distr_space(minspace, maxspace);
93 
94  int col = 0;
95 
96  CellC cells;
97  ClusterC clusters;
98  for (size_t i = 0; i < nclusters; i++) {
99  Cluster cl;
100  col += distr_space(rnd);
101  size_t size = distr_size(rnd);
102  for (size_t j = 0; j < size; j++) {
103  Cell cell(col++);
104  cells.push_back(cell);
105  clusterAddCell(cl, cell);
106  }
107  clusters.push_back(std::move(cl));
108  }
109  for (Cluster& cl : clusters) {
110  hash(cl);
111  }
112 
113  std::shuffle(cells.begin(), cells.end(), rnd);
114 
115  ClusterC newCls = Ccl::createClusters<CellC, ClusterC, 1>(cells);
116 
117  for (Cluster& cl : newCls) {
118  hash(cl);
119  }
120 
121  std::sort(clusters.begin(), clusters.end(), clHashComp);
122  std::sort(newCls.begin(), newCls.end(), clHashComp);
123 
124  BOOST_CHECK_EQUAL(clusters.size(), newCls.size());
125  for (size_t i = 0; i < clusters.size(); i++) {
126  BOOST_CHECK_EQUAL(clusters.at(i).hash, newCls.at(i).hash);
127  }
128  }
129 }
130 
131 } // namespace Test
132 } // namespace Acts