ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Clusterization.ipp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Clusterization.ipp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018 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 <algorithm>
10 #include <unordered_map>
11 #include <utility>
12 #include <vector>
13 
14 template <typename cell_t>
15 std::vector<std::vector<cell_t>> Acts::createClusters(
16  std::unordered_map<size_t, std::pair<cell_t, bool>>& cellMap, size_t nBins0,
17  bool commonCorner, double energyCut) {
18  // the output
19  std::vector<std::vector<cell_t>> mergedCells;
20  // now go through cells and label
21  for (auto& cell : cellMap) {
22  // check if the cell was already used
23  if (!(cell.second.second) &&
24  (cell.second.first.depositedEnergy() >= energyCut)) {
25  // create new cluster
26  mergedCells.push_back(std::vector<cell_t>());
27  // add current cell to current cluster
28  mergedCells.back().push_back(cell.second.first);
29  // set cell to be used already
30  cell.second.second = true;
31  // fill all cells belonging to that cluster
32  fillCluster(mergedCells, cellMap, cell.first, nBins0, commonCorner,
33  energyCut);
34  }
35  }
36  // return the grouped together cells
37  return mergedCells;
38 }
39 
40 template <typename cell_t>
42  std::vector<std::vector<cell_t>>& mergedCells,
43  std::unordered_map<size_t, std::pair<cell_t, bool>>& cellMap, size_t index,
44  size_t nBins0, bool commonCorner, double energyCut) {
45  // go recursively through all neighbours of this cell, if present
46  // calculate neighbour indices first
47  constexpr int iMin = -1;
48  int jMin = -nBins0;
49  constexpr int iMax = 1;
50  int jMax = nBins0;
51  std::vector<int> neighbourIndices;
52  // the neighbour indices - filled depending on merging case
53  if ((index % nBins0) == 0) {
54  // left edge case
55  if (commonCorner) {
56  neighbourIndices = {jMin, jMin + iMax, iMax, jMax, jMax + iMax};
57  } else {
58  neighbourIndices = {jMin, iMax, jMax};
59  }
60  } else if (((index + 1) % nBins0) == 0) {
61  // right edge case
62  if (commonCorner) {
63  neighbourIndices = {jMin + iMin, jMin, iMin, jMax + iMin, jMax};
64  } else {
65  neighbourIndices = {jMin, iMin, jMax};
66  }
67  } else {
68  if (commonCorner) {
69  neighbourIndices = {jMin + iMin, jMin, jMin + iMax, iMin,
70  iMax, jMax + iMin, jMax, jMax + iMax};
71  } else {
72  neighbourIndices = {jMin, iMin, iMax, jMax};
73  }
74  }
75  // go through neighbours and recursively call connected component algorithm
76  for (auto& i : neighbourIndices) {
77  // calculate neighbour index of current cell
78  int neighbourIndex = int(index) + i;
79  // check if neighbour is there
81  auto search = cellMap.find(neighbourIndex);
82  if ((search != cellMap.end())) {
83  // get the corresponding index and call function again
84  auto newIndex = search->first;
85  auto currentCell = search->second.first;
86  // if cell was not already added to cluster & deposited energy is higher
87  // than the energy threshold, add it to the cluster
88  if (!search->second.second &&
89  currentCell.depositedEnergy() >= energyCut) {
90  // add current cell to current cluster
91  mergedCells.back().push_back(currentCell);
92  // set cell to be used already
93  search->second.second = true;
94  // add all neighbours to cluster
95  fillCluster(mergedCells, cellMap, newIndex, nBins0, commonCorner,
96  energyCut);
97  } // check if was used already
98  } // check if neighbour is there
99  } // go through neighbour indics
100 }