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 
13 namespace Belle2 {
19  namespace PXD {
20 
21  ClusterCache::ClusterCache(unsigned int maxU): m_maxU(maxU + 2)
22  {
23  m_clsTop = new ClusterCandidate*[m_maxU];
24  m_clsCur = new ClusterCandidate*[m_maxU];
25  clear();
26  }
27 
29  {
30  delete[] m_clsTop;
31  delete[] m_clsCur;
32  }
33 
35  void ClusterCache::clear()
36  {
37  memset(m_clsTop, 0, m_maxU * sizeof(ClusterCandidate*));
38  memset(m_clsCur, 0, m_maxU * sizeof(ClusterCandidate*));
39  m_curV = 0;
40  m_currCluster = m_clusters.begin();
41  }
42 
44  ClusterCandidate& ClusterCache::findCluster(unsigned int u, unsigned int v)
45  {
46  if (u >= m_maxU - 2) {
47  throw std::out_of_range("u cell id is outside of valid range");
48  }
49  switchRow(v);
50  const unsigned int u1 = u + 1;
51  //Look for cluster top of current pixel (0,0 at top left corner, rows going
52  //down, columns going left) The ClusterCache is two columns wider than the
53  //actual pixel sensor to get rid of edge effects. Column 0 is at index 1 so
54  //the three neighbors of column u have the indices (u,u+1,u+2)
55 
56  //So the left neighbor has index u in the current row
57  ClusterCandidate* cls = m_clsCur[u];
58  //And the topleft, top and topright clusters have u+i, i in 0..2 But if we
59  //already have a left neighbor we do not need to check topleft and top as
60  //those are guaranteed to be already merged with the left one
61  if (!cls) {
62  cls = mergeCluster(cls, m_clsTop[u]);
63  cls = mergeCluster(cls, m_clsTop[u1]);
64  }
65  cls = mergeCluster(cls, m_clsTop[u + 2]);
66 
67  //If no cluster was found create a new one
68  if (!cls) {
69  if (m_currCluster == m_clusters.end()) {
70  //We already use all ClusterCandidates, create a new one
71  m_clusters.emplace_front();
72  cls = &m_clusters.front();
73  } else {
74  //There are some Candidates left, use them
75  cls = &(*m_currCluster++);
76  cls->clear();
77  }
78  }
79  //Save the cluster and the current position in the cache
80  m_curV = v;
81  m_clsCur[u1] = cls;
82  //Return the cluster
83  return *cls;
84  }
85 
87  ClusterCandidate* ClusterCache::mergeCluster(ClusterCandidate* cls1, ClusterCandidate* cls2)
88  {
89  if (cls2) {
90  if (!cls1 || cls1 == cls2) return cls2;
91  return cls1->merge(*cls2);
92  }
93  return cls1;
94  }
95 
97  void ClusterCache::switchRow(unsigned int v)
98  {
99  if (v == m_curV) return;
100  //Clear top row
101  std::memset(m_clsTop, 0, m_maxU * sizeof(ClusterCandidate*));
102  //We skipped a row, forget current row, no need to switch rows, both got emptied
103  if (v > m_curV + 1) {
104  std::memset(m_clsCur, 0, m_maxU * sizeof(ClusterCandidate*));
105  } else {
106  //Switch rows, Current row will be top and we reuse memory of last top
107  //row as new current row
108  std::swap(m_clsTop, m_clsCur);
109  }
110  //save current row coordinate
111  m_curV = v;
112  }
113  }
115 } //Belle2 namespace
Belle2::PXD::ClusterCache::clear
void clear()
Clear the cache structure.
Definition: ClusterCache.cc:43
Belle2::PXD::ClusterCandidate::clear
void clear()
Clear the Cluster information (to reuse the same cluster instance)
Definition: ClusterCandidate.h:57
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::ClusterCache::switchRow
void switchRow(unsigned int v)
Switch the internal rows.
Definition: ClusterCache.cc:105
Belle2::PXD::ClusterCache::m_currCluster
std::deque< ClusterCandidate >::iterator m_currCluster
iterator to the next free cluster to be used if a new cluster is needed.
Definition: ClusterCache.h:168
Belle2::PXD::ClusterCache::ClusterCache
ClusterCache(unsigned int maxU=c_defaultNumberColumns)
Create a new cache.
Definition: ClusterCache.cc:29
Belle2::PXD::ClusterCache::m_curV
unsigned int m_curV
current v coordinate, needed to switch top row
Definition: ClusterCache.h:154
Belle2::PXD::ClusterCache::m_clusters
std::deque< ClusterCandidate > m_clusters
list of all the clusters created so far
Definition: ClusterCache.h:160
Belle2::PXD::ClusterCache::~ClusterCache
~ClusterCache()
Delete the cache and free the memory.
Definition: ClusterCache.cc:36
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
prepareAsicCrosstalkSimDB.u1
def u1
auxiliary variable, split ADC range in two < 1024 and above (u1: below)
Definition: prepareAsicCrosstalkSimDB.py:42
Belle2::PXD::ClusterCache::mergeCluster
ClusterCandidate * mergeCluster(ClusterCandidate *cls1, ClusterCandidate *cls2)
Merge two cluster and update the list of cached clusters.
Definition: ClusterCache.cc:95
Belle2::PXD::ClusterCache::m_clsCur
ClusterCandidate ** m_clsCur
cache of the current row
Definition: ClusterCache.h:158
Belle2::PXD::ClusterCache::m_maxU
const unsigned int m_maxU
number of columns of the cache.
Definition: ClusterCache.h:152
Belle2::PXD::ClusterCache::m_clsTop
ClusterCandidate ** m_clsTop
cache of the top row
Definition: ClusterCache.h:156
Belle2::PXD::ClusterCache::findCluster
ClusterCandidate & findCluster(unsigned int u, unsigned int v)
Find a cluster adjacent to the given coordinates.
Definition: ClusterCache.cc:52