Belle II Software  release-08-01-10
ClusterCache.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 <pxd/reconstruction/ClusterCache.h>
10 #include <gtest/gtest.h>
11 #include <bitset>
12 
13 using namespace std;
14 
15 namespace Belle2 {
20  namespace PXD {
45  TEST(ClusterCache, FindNeighbours)
46  {
47  ClusterCache cache;
48  for (int v = 0; v < 4; ++v) {
49  for (int u = 0; u < 5; ++u) {
50  if (v == 0 && u <= 2) continue;
51  cache.clear();
52  ClusterCandidate& cls1 = cache.findCluster(2, 0);
53  if ((v == 0 && u == 3) || (v == 1 && u >= 1 && u <= 3)) {
54  //Check that neighboring pixels return the same cluster
55  EXPECT_EQ(&cls1, &cache.findCluster(u, v)) << "u: " << u << " v: " << v;
56  } else {
57  //And all other pixels return another cluster
58  EXPECT_NE(&cls1, &cache.findCluster(u, v)) << "u: " << u << " v: " << v;
59  }
60  }
61  }
62  }
63 
90  TEST(ClusterCache, Merging)
91  {
92  //Create clusters 1, 2, 3 and 4
93  ClusterCache cache;
94  ClusterCandidate& cls1 = cache.findCluster(2, 0);
95  ClusterCandidate& cls2 = cache.findCluster(4, 0);
96  cls1.add(Pixel(1));
97  cls2.add(Pixel(2));
98  cache.findCluster(6, 0).add(Pixel(3));
99  cache.findCluster(0, 1).add(Pixel(4));
100 
101  //Add Pixel X
102  ClusterCandidate& foundX = cache.findCluster(3, 1);
103  //and the clusters 1 and 2 should have been merged
104  ASSERT_EQ(2u, foundX.size());
105  //The pointer should be one of the two
106  EXPECT_TRUE(&foundX == &cls1 || &foundX == &cls2);
107  //The sum of indices should be ok
108  EXPECT_EQ(3u, foundX.pixels()[0].getIndex() + foundX.pixels()[1].getIndex());
109  //the other cluster should be empty now
110  EXPECT_TRUE((cls1.size() == 0 && cls2.size() == 2) || (cls1.size() == 2 && cls2.size() == 0));
111 
112  //And add pixel 5, 6 and Y
113  cache.findCluster(6, 1).add(Pixel(5));
114  cache.findCluster(1, 2).add(Pixel(6));
115  ClusterCandidate& foundY = cache.findCluster(2, 2);
116  EXPECT_EQ(4u, foundY.size());
117  {
118  //Check for equality by merging empty clusters. merge returns a pointer
119  //to the topmost cluster in a group of merged clusters
120  ClusterCandidate t1, t2;
121  EXPECT_EQ(foundX.merge(t1), foundY.merge(t2));
122  }
123 
124  //And finally pixel 7 and Z
125  cache.findCluster(4, 2).add(Pixel(7));
126  ClusterCandidate& foundZ = cache.findCluster(5, 2);
127  ASSERT_EQ(7u, foundY.size());
128  {
129  //Check for equality by merging empty clusters. merge returns a pointer
130  //to the topmost cluster in a group of merged clusters
131  ClusterCandidate t1, t2;
132  EXPECT_EQ(foundX.merge(t1), foundZ.merge(t2));
133  }
134 
135  //Check if all 7 pixels are present using a bitmask to see if we get every index from 1 to 7 once
136  std::bitset<7> check_index(-1);
137  for (const Pixel& px : foundZ.pixels()) {
138  ASSERT_LT(0u, px.getIndex());
139  ASSERT_GE(7u, px.getIndex());
140  EXPECT_TRUE(check_index[px.getIndex() - 1]) << "index: " << px.getIndex();
141  check_index[px.getIndex() - 1] = false;
142  }
143  EXPECT_TRUE(check_index.none());
144  }
145 
150  {
151  ClusterCache cache;
152  ASSERT_TRUE(cache.empty());
153  cache.findCluster(0, 0);
154  ASSERT_FALSE(cache.empty());
155  }
156 
158  TEST(ClusterCache, OutOfRange)
159  {
160  ClusterCache cache(250);
161  for (int i = -10; i < 260; ++i) {
162  if (i >= 0 && i < 250) {
163  ASSERT_NO_THROW(cache.findCluster(i, 0));
164  } else {
165  ASSERT_THROW(cache.findCluster(i, 0), std::out_of_range);
166  }
167  }
168  }
169  } //PXD namespace
171 } //Belle namespace
Class to remember recently assigned clusters This class will remember the current and the last pixel ...
Definition: ClusterCache.h:77
ClusterCandidate & findCluster(unsigned int u, unsigned int v)
Find a cluster adjacent to the given coordinates.
Definition: ClusterCache.cc:42
bool empty() const
Check if there are any clusters.
Definition: ClusterCache.h:126
void clear()
Clear the cache structure.
Definition: ClusterCache.cc:33
Class representing a possible cluster during clustering of the PXD It supports merging of different c...
size_t size() const
get the cluster size
const std::vector< Pixel > & pixels() const
get a reference to all pixels in the cluster
ClusterCandidate * merge(ClusterCandidate &cls)
Merge the given cluster with this one.
void add(const Pixel &pixel)
Add a Pixel to the current cluster.
Class to represent one pixel, used in clustering for fast access.
Definition: Pixel.h:36
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Abstract base class for different kinds of events.