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