@@ -133,15 +133,17 @@ void Clusterer::process(int nThreads, PixelReader& reader, CompClusCont* compClu
133133 if (stat.firstChip == chid) {
134134 thrStatIdx[ith]++;
135135 chid += stat.nChips ; // next chip to look
136- const auto clbeg = mThreads [ith]->compClusters .begin () + stat.firstClus ;
137- auto szold = compClus->size ();
138- compClus->insert (compClus->end (), clbeg, clbeg + stat.nClus );
139- if (patterns) {
140- const auto ptbeg = mThreads [ith]->patterns .begin () + stat.firstPatt ;
141- patterns->insert (patterns->end (), ptbeg, ptbeg + stat.nPatt );
142- }
143- if (labelsCl) {
144- labelsCl->mergeAtBack (mThreads [ith]->labels , stat.firstClus , stat.nClus );
136+ if (stat.nClus > 0 ) {
137+ const auto clbeg = mThreads [ith]->compClusters .begin () + stat.firstClus ;
138+ auto szold = compClus->size ();
139+ compClus->insert (compClus->end (), clbeg, clbeg + stat.nClus );
140+ if (patterns) {
141+ const auto ptbeg = mThreads [ith]->patterns .begin () + stat.firstPatt ;
142+ patterns->insert (patterns->end (), ptbeg, ptbeg + stat.nPatt );
143+ }
144+ if (labelsCl) {
145+ labelsCl->mergeAtBack (mThreads [ith]->labels , stat.firstClus , stat.nClus );
146+ }
145147 }
146148 }
147149 }
@@ -214,6 +216,14 @@ void Clusterer::ClustererThread::finishChip(ChipPixelData* curChipData, CompClus
214216 PatternCont* patternsPtr, const ConstMCTruth* labelsDigPtr, MCTruth* labelsClusPtr)
215217{
216218 const auto & pixData = curChipData->getData ();
219+
220+ // normalize all precluster indices to their roots
221+ for (int i = 0 ; i < preClusterIndices.size (); ++i) {
222+ if (preClusterIndices[i] >= 0 ) {
223+ preClusterIndices[i] = findRoot (i);
224+ }
225+ }
226+
217227 for (int i1 = 0 ; i1 < preClusterHeads.size (); ++i1) {
218228 auto ci = preClusterIndices[i1];
219229 if (ci < 0 ) {
@@ -379,11 +389,13 @@ void Clusterer::ClustererThread::updateChip(const ChipPixelData* curChipData, ui
379389 }
380390
381391 Bool_t orphan = true ;
392+ int currPreClusterIdx = -1 ;
382393
383394 if (noLeftCol) { // check only the row above
384395 if (curr[row - 1 ] >= 0 ) {
385396 expandPreCluster (ip, row, curr[row - 1 ]); // attach to the precluster of the previous row
386- return ;
397+ currPreClusterIdx = curr[row - 1 ];
398+ orphan = false ;
387399 }
388400 } else {
389401#ifdef _ALLOW_DIAGONAL_ALPIDE_CLUSTERS_
@@ -397,14 +409,20 @@ void Clusterer::ClustererThread::updateChip(const ChipPixelData* curChipData, ui
397409 }
398410 if (orphan) {
399411 expandPreCluster (ip, row, pci); // attach to the adjascent precluster
412+ currPreClusterIdx = pci;
400413 orphan = false ;
401414 continue ;
402415 }
403- // reassign precluster index to smallest one
404- if (preClusterIndices[pci] < preClusterIndices[curr[row]]) {
405- preClusterIndices[curr[row]] = preClusterIndices[pci];
406- } else {
407- preClusterIndices[pci] = preClusterIndices[curr[row]];
416+ // merge the roots of both preclusters
417+ int root1 = findRoot (pci);
418+ int root2 = findRoot (currPreClusterIdx);
419+ if (root1 != root2) {
420+ // point to higher index to lower index for consistency
421+ if (root1 < root2) {
422+ preClusterIndices[root2] = root1;
423+ } else {
424+ preClusterIndices[root1] = root2;
425+ }
408426 }
409427 }
410428 }
0 commit comments