Belle II Software  release-08-01-10
Clusterizend.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include "framework/logging/Logger.h"
10 #include "trg/cdc/NDFinderDefs.h"
11 #include "trg/cdc/Clusterizend.h"
12 
13 using namespace Belle2;
14 using namespace std;
15 
16 
17 bool Clusterizend::has_before(cell_index entry, ushort dim)
18 {
19  if (entry[dim] > 0 || m_params.var_cyclic[dim]) {
20  return true;
21  }
22  return false;
23 }
24 cell_index Clusterizend::before(cell_index entry, ushort dim)
25 {
26  if (entry[dim] > 0) {
27  entry[dim] -= 1;
28  return entry;
29  } else if (m_params.var_cyclic[dim]) {
30  entry[dim] = m_valmax[dim];
31  return entry;
32  } else {
33  B2ERROR("no before(), check with has_before");
34  return entry;
35  }
36 }
37 
38 bool Clusterizend::has_after(cell_index entry, ushort dim)
39 {
40  if (entry[dim] < m_valmax[dim] || m_params.var_cyclic[dim]) {
41  return true;
42  }
43  return false;
44 }
45 cell_index Clusterizend::after(cell_index entry, ushort dim)
46 {
47  if (entry[dim] < m_valmax[dim]) {
48  entry[dim] += 1;
49  return entry;
50  } else if (m_params.var_cyclic[dim]) {
51  entry[dim] = 0;
52  return entry;
53  } else {
54  B2ERROR("no after(), check with has_before()");
55  return entry;
56  }
57 }
58 
59 
60 void Clusterizend::blockcheck(vector<cell_index>* neighbors, cell_index elem, ushort dim)
61 {
62  if (dim > 10) {
63  B2ERROR("dim too large, dim=" << dim);
64  }
65  if (has_before(elem, dim)) {
66  cell_index ind = before(elem, dim);
67  ushort leftwe = (*m_houghVals)[ind[0]][ind[1]][ind[2]];
68  ushort leftvi = m_houghVisit[ind[0]][ind[1]][ind[2]];
69  if (leftwe > m_params.minweight && leftvi == 0) {
70  neighbors->push_back(ind);
71  }
72  if (m_params.diagonal && dim > 0) {
73  blockcheck(neighbors, ind, dim - 1);
74  }
75  }
76  if (has_after(elem, dim)) {
77  cell_index ind = after(elem, dim);
78  ushort rightwe = (*m_houghVals)[ind[0]][ind[1]][ind[2]];
79  ushort rightvi = m_houghVisit[ind[0]][ind[1]][ind[2]];
80  if (rightwe > m_params.minweight && rightvi == 0) {
81  neighbors->push_back(ind);
82  }
83  if (m_params.diagonal && dim > 0) {
84  blockcheck(neighbors, ind, dim - 1);
85  }
86  }
87 
88  if (dim > 0) {
89  blockcheck(neighbors, elem, dim - 1);
90  }
91 }
92 
93 vector<cell_index>
94 Clusterizend::regionQuery(cell_index entry)
95 {
96  vector<cell_index> neighbours;
97  blockcheck(&neighbours, entry, m_dimsize - 1);
98  return neighbours;
99 }
100 
101 vector<SimpleCluster>
102 Clusterizend::dbscan()
103 {
104  vector<SimpleCluster> C;
105  vector<cell_index> candidates = getCandidates();
106  for (unsigned long icand = 0; icand < candidates.size(); icand++) {
107  cell_index entry = candidates[icand];
108  c3index iom = entry[0];
109  c3index iph = entry[1];
110  c3index ith = entry[2];
111 
112  if (m_houghVisit[iom][iph][ith] == 0) {
113  //B2DEBUG(19, "dbscan: unvisited cell");
114  m_houghVisit[iom][iph][ith] = 1;
115  vector<cell_index> N = regionQuery(entry);
116  if (N.size() >= m_params.minpts) {
117  //B2DEBUG(19, "dbscan: starting cluster, neightbors = " << N.size());
118  SimpleCluster newcluster(entry);
119  expandCluster(N, newcluster);
120  C.push_back(newcluster);
121  }
122  }
123  }
124  return C;
125 }
126 
127 void
128 Clusterizend::expandCluster(vector<cell_index>& N, SimpleCluster& C)
129 {
130  while (N.size() > 0) {
131  cell_index nextP = N.back();
132  N.pop_back();
133  ushort iom = nextP[0];
134  ushort iph = nextP[1];
135  ushort ith = nextP[2];
136  if (m_houghVisit[iom][iph][ith] == 0) {
137  m_houghVisit[iom][iph][ith] = 1;
138  if ((*m_houghVals)[iom][iph][ith] < m_params.minweight) {
139  continue;
140  }
141  vector<cell_index> nextN = regionQuery(nextP);
142  if (nextN.size() >= m_params.minpts) {
143  N.insert(N.end(), nextN.begin(), nextN.end());
144  }
145  C.append(nextP);
146  }
147  }
148 }
149 
150 
151 vector<cell_index>
153 {
154  vector<cell_index> candidates;
156  for (c3index iom = 0; iom < 40; iom++) {
157  for (c3index iph = 0; iph < 384; iph++) {
158  for (c3index ith = 0; ith < 9; ith++) {
159  if ((*m_houghVals)[iom][iph][ith] > m_params.minweight) {
160  cell_index elem = {iom, iph, ith};
161  candidates.push_back(elem);
162  }
163  }
164  }
165  }
166  return candidates;
167 }
bool has_before(cell_index entry, ushort dim)
Clustering logic.
Definition: Clusterizend.cc:17
std::vector< cell_index > getCandidates()
Type for found clusters.
Definition: Clusterizend.h:34
void append(cell_index next_entry)
Add a track-space cell to the cluster.
Definition: Clusterizend.h:67
Abstract base class for different kinds of events.