forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusterer.cxx
More file actions
494 lines (470 loc) · 18.9 KB
/
Clusterer.cxx
File metadata and controls
494 lines (470 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file Clusterer.cxx
/// \brief Implementation of the ITS cluster finder
#include <algorithm>
#include <TTree.h>
#include "Framework/Logger.h"
#include "ITSMFTReconstruction/Clusterer.h"
#include "SimulationDataFormat/MCTruthContainer.h"
#include "CommonDataFormat/InteractionRecord.h"
#ifdef WITH_OPENMP
#include <omp.h>
#endif
using namespace o2::itsmft;
//__________________________________________________
void Clusterer::process(int nThreads, PixelReader& reader, CompClusCont* compClus,
PatternCont* patterns, ROFRecCont* vecROFRec, MCTruth* labelsCl)
{
#ifdef _PERFORM_TIMING_
mTimer.Start(kFALSE);
#endif
if (nThreads < 1) {
nThreads = 1;
}
auto autoDecode = reader.getDecodeNextAuto();
int rofcount{0};
o2::InteractionRecord lastIR{};
do {
if (autoDecode) {
reader.setDecodeNextAuto(false); // internally do not autodecode
if (!reader.decodeNextTrigger()) {
break; // on the fly decoding was requested, but there were no data left
}
}
if (reader.getInteractionRecord().isDummy()) {
continue; // No IR info was found
}
if (!lastIR.isDummy() && lastIR >= reader.getInteractionRecord()) {
const int MaxErrLog = 2;
static int errLocCount = 0;
if (errLocCount++ < MaxErrLog) {
LOGP(warn, "Impossible ROF IR {}, does not exceed previous {}, discarding in clusterization", reader.getInteractionRecord().asString(), lastIR.asString());
}
continue;
}
lastIR = reader.getInteractionRecord();
// pre-fetch all non-empty chips of current ROF
ChipPixelData* curChipData = nullptr;
mFiredChipsPtr.clear();
size_t nPix = 0;
while ((curChipData = reader.getNextChipData(mChips))) {
mFiredChipsPtr.push_back(curChipData);
nPix += curChipData->getData().size();
}
auto& rof = vecROFRec->emplace_back(reader.getInteractionRecord(), vecROFRec->size(), compClus->size(), 0); // create new ROF
uint16_t nFired = mFiredChipsPtr.size();
if (!nFired) {
if (autoDecode) {
continue;
}
break; // just 1 ROF was asked to be processed
}
if (nFired < nThreads) {
nThreads = nFired;
}
#ifndef WITH_OPENMP
nThreads = 1;
#endif
uint16_t chipStep = nThreads > 1 ? (nThreads == 2 ? 20 : 10) : nFired;
int dynGrp = std::min(4, std::max(1, nThreads / 2));
if (nThreads > mThreads.size()) {
int oldSz = mThreads.size();
mThreads.resize(nThreads);
for (int i = oldSz; i < nThreads; i++) {
mThreads[i] = std::make_unique<ClustererThread>(this, i);
}
}
#ifdef WITH_OPENMP
#pragma omp parallel for schedule(dynamic, dynGrp) num_threads(nThreads)
//>> start of MT region
for (uint16_t ic = 0; ic < nFired; ic += chipStep) {
auto ith = omp_get_thread_num();
if (nThreads > 1) {
mThreads[ith]->process(ic, std::min(chipStep, uint16_t(nFired - ic)),
&mThreads[ith]->compClusters,
patterns ? &mThreads[ith]->patterns : nullptr,
labelsCl ? reader.getDigitsMCTruth() : nullptr,
labelsCl ? &mThreads[ith]->labels : nullptr, rof);
} else { // put directly to the destination
mThreads[0]->process(0, nFired, compClus, patterns, labelsCl ? reader.getDigitsMCTruth() : nullptr, labelsCl, rof);
}
}
//<< end of MT region
#else
mThreads[0]->process(0, nFired, compClus, patterns, labelsCl ? reader.getDigitsMCTruth() : nullptr, labelsCl, rof);
#endif
// copy data of all threads but the 1st one to final destination
if (nThreads > 1) {
#ifdef _PERFORM_TIMING_
mTimerMerge.Start(false);
#endif
size_t nClTot = 0, nPattTot = 0;
int chid = 0, thrStatIdx[nThreads];
for (int ith = 0; ith < nThreads; ith++) {
std::sort(mThreads[ith]->stats.begin(), mThreads[ith]->stats.end(), [](const ThreadStat& a, const ThreadStat& b) { return a.firstChip < b.firstChip; });
thrStatIdx[ith] = 0;
nClTot += mThreads[ith]->compClusters.size();
nPattTot += mThreads[ith]->patterns.size();
}
compClus->reserve(nClTot);
if (patterns) {
patterns->reserve(nPattTot);
}
while (chid < nFired) {
for (int ith = 0; ith < nThreads; ith++) {
if (thrStatIdx[ith] >= mThreads[ith]->stats.size()) {
continue;
}
const auto& stat = mThreads[ith]->stats[thrStatIdx[ith]];
if (stat.firstChip == chid) {
thrStatIdx[ith]++;
chid += stat.nChips; // next chip to look
if (stat.nClus > 0) {
const auto clbeg = mThreads[ith]->compClusters.begin() + stat.firstClus;
auto szold = compClus->size();
compClus->insert(compClus->end(), clbeg, clbeg + stat.nClus);
if (patterns) {
const auto ptbeg = mThreads[ith]->patterns.begin() + stat.firstPatt;
patterns->insert(patterns->end(), ptbeg, ptbeg + stat.nPatt);
}
if (labelsCl) {
labelsCl->mergeAtBack(mThreads[ith]->labels, stat.firstClus, stat.nClus);
}
}
}
}
}
for (int ith = 0; ith < nThreads; ith++) {
mThreads[ith]->patterns.clear();
mThreads[ith]->compClusters.clear();
mThreads[ith]->labels.clear();
mThreads[ith]->stats.clear();
}
#ifdef _PERFORM_TIMING_
mTimerMerge.Stop();
#endif
} else {
mThreads[0]->stats.clear();
}
rof.setNEntries(compClus->size() - rof.getFirstEntry()); // update
} while (autoDecode);
reader.setDecodeNextAuto(autoDecode); // restore setting
#ifdef _PERFORM_TIMING_
mTimer.Stop();
#endif
}
//__________________________________________________
void Clusterer::ClustererThread::process(uint16_t chip, uint16_t nChips, CompClusCont* compClusPtr, PatternCont* patternsPtr,
const ConstMCTruth* labelsDigPtr, MCTruth* labelsClPtr, const ROFRecord& rofPtr)
{
if (stats.empty() || stats.back().firstChip + stats.back().nChips != chip) { // there is a jump, register new block
stats.emplace_back(ThreadStat{chip, 0, uint32_t(compClusPtr->size()), patternsPtr ? uint32_t(patternsPtr->size()) : 0, 0, 0});
}
for (int ic = 0; ic < nChips; ic++) {
auto* curChipData = parent->mFiredChipsPtr[chip + ic];
auto chipID = curChipData->getChipID();
if (parent->mMaxBCSeparationToMask > 0) { // mask pixels fired from the previous ROF
const auto& chipInPrevROF = parent->mChipsOld[chipID];
if (std::abs(rofPtr.getBCData().differenceInBC(chipInPrevROF.getInteractionRecord())) < parent->mMaxBCSeparationToMask) {
parent->mMaxRowColDiffToMask ? curChipData->maskFiredInSample(parent->mChipsOld[chipID], parent->mMaxRowColDiffToMask) : curChipData->maskFiredInSample(parent->mChipsOld[chipID]);
}
}
auto nclus0 = compClusPtr->size();
auto validPixID = curChipData->getFirstUnmasked();
auto npix = curChipData->getData().size();
if (validPixID < npix) { // chip data may have all of its pixels masked!
auto valp = validPixID++;
if (validPixID == npix) { // special case of a single pixel fired on the chip
finishChipSingleHitFast(valp, curChipData, compClusPtr, patternsPtr, labelsDigPtr, labelsClPtr);
} else {
initChip(curChipData, valp);
for (; validPixID < npix; validPixID++) {
if (!curChipData->getData()[validPixID].isMasked()) {
updateChip(curChipData, validPixID);
}
}
finishChip(curChipData, compClusPtr, patternsPtr, labelsDigPtr, labelsClPtr);
}
}
if (parent->mMaxBCSeparationToMask > 0) { // current chip data will be used in the next ROF to mask overflow pixels
parent->mChipsOld[chipID].swap(*curChipData);
}
}
auto& currStat = stats.back();
currStat.nChips += nChips;
currStat.nClus = compClusPtr->size() - currStat.firstClus;
currStat.nPatt = patternsPtr ? (patternsPtr->size() - currStat.firstPatt) : 0;
}
//__________________________________________________
void Clusterer::ClustererThread::finishChip(ChipPixelData* curChipData, CompClusCont* compClusPtr,
PatternCont* patternsPtr, const ConstMCTruth* labelsDigPtr, MCTruth* labelsClusPtr)
{
const auto& pixData = curChipData->getData();
// normalize all precluster indices to their roots
for (int i = 0; i < preClusterIndices.size(); ++i) {
if (preClusterIndices[i] >= 0) {
preClusterIndices[i] = findRoot(i);
}
}
for (int i1 = 0; i1 < preClusterHeads.size(); ++i1) {
auto ci = preClusterIndices[i1];
if (ci < 0) {
continue;
}
BBox bbox(curChipData->getChipID());
int nlab = 0;
int next = preClusterHeads[i1];
pixArrBuff.clear();
while (next >= 0) {
const auto& pixEntry = pixels[next];
const auto pix = pixData[pixEntry.second];
pixArrBuff.push_back(pix); // needed for cluster topology
bbox.adjust(pix.getRowDirect(), pix.getCol());
if (labelsClusPtr) {
if (parent->mSquashingDepth) { // the MCtruth for this pixel is stored in chip data: due to squashing we lose contiguity
fetchMCLabels(curChipData->getOrderedPixId(pixEntry.second), labelsDigPtr, nlab);
} else { // the MCtruth for this pixel is at curChipData->startID+pixEntry.second
fetchMCLabels(pixEntry.second + curChipData->getStartID(), labelsDigPtr, nlab);
}
}
next = pixEntry.first;
}
preClusterIndices[i1] = -1;
for (int i2 = i1 + 1; i2 < preClusterHeads.size(); ++i2) {
if (preClusterIndices[i2] != ci) {
continue;
}
next = preClusterHeads[i2];
while (next >= 0) {
const auto& pixEntry = pixels[next];
const auto pix = pixData[pixEntry.second]; // PixelData
pixArrBuff.push_back(pix); // needed for cluster topology
bbox.adjust(pix.getRowDirect(), pix.getCol());
if (labelsClusPtr) {
if (parent->mSquashingDepth) { // the MCtruth for this pixel is stored in chip data: due to squashing we lose contiguity
fetchMCLabels(curChipData->getOrderedPixId(pixEntry.second), labelsDigPtr, nlab);
} else { // the MCtruth for this pixel is at curChipData->startID+pixEntry.second
fetchMCLabels(pixEntry.second + curChipData->getStartID(), labelsDigPtr, nlab);
}
}
next = pixEntry.first;
}
preClusterIndices[i2] = -1;
}
if (bbox.isAcceptableSize()) {
parent->streamCluster(pixArrBuff, &labelsBuff, bbox, parent->mPattIdConverter, compClusPtr, patternsPtr, labelsClusPtr, nlab);
} else {
auto warnLeft = MaxHugeClusWarn - parent->mNHugeClus;
if (!parent->mDropHugeClusters) {
if (warnLeft > 0) {
LOGP(warn, "Splitting a huge cluster: chipID {}, rows {}:{} cols {}:{}{}", bbox.chipID, bbox.rowMin, bbox.rowMax, bbox.colMin, bbox.colMax,
warnLeft == 1 ? " (Further warnings will be muted)" : "");
#ifdef WITH_OPENMP
#pragma omp critical
#endif
{
parent->mNHugeClus++;
}
}
BBox bboxT(bbox); // truncated box
std::vector<PixelData> pixbuf;
do {
bboxT.rowMin = bbox.rowMin;
bboxT.colMax = std::min(bbox.colMax, uint16_t(bboxT.colMin + o2::itsmft::ClusterPattern::MaxColSpan - 1));
do { // Select a subset of pixels fitting the reduced bounding box
bboxT.rowMax = std::min(bbox.rowMax, uint16_t(bboxT.rowMin + o2::itsmft::ClusterPattern::MaxRowSpan - 1));
for (const auto& pix : pixArrBuff) {
if (bboxT.isInside(pix.getRowDirect(), pix.getCol())) {
pixbuf.push_back(pix);
}
}
if (!pixbuf.empty()) { // Stream a piece of cluster only if the reduced bounding box is not empty
parent->streamCluster(pixbuf, &labelsBuff, bboxT, parent->mPattIdConverter, compClusPtr, patternsPtr, labelsClusPtr, nlab, true);
pixbuf.clear();
}
bboxT.rowMin = bboxT.rowMax + 1;
} while (bboxT.rowMin < bbox.rowMax);
bboxT.colMin = bboxT.colMax + 1;
} while (bboxT.colMin < bbox.colMax);
}
}
}
}
//__________________________________________________
void Clusterer::ClustererThread::finishChipSingleHitFast(uint32_t hit, ChipPixelData* curChipData, CompClusCont* compClusPtr,
PatternCont* patternsPtr, const ConstMCTruth* labelsDigPtr, MCTruth* labelsClusPtr)
{
auto pix = curChipData->getData()[hit];
uint16_t row = pix.getRowDirect(), col = pix.getCol();
if (labelsClusPtr) { // MC labels were requested
int nlab = 0;
fetchMCLabels(curChipData->getStartID() + hit, labelsDigPtr, nlab);
auto cnt = compClusPtr->size();
for (int i = nlab; i--;) {
labelsClusPtr->addElement(cnt, labelsBuff[i]);
}
}
// add to compact clusters, which must be always filled
unsigned char patt[ClusterPattern::MaxPatternBytes]{0x1 << (7 - (0 % 8))}; // unrolled 1 hit version of full loop in finishChip
uint16_t pattID = (parent->mPattIdConverter.size() == 0) ? CompCluster::InvalidPatternID : parent->mPattIdConverter.findGroupID(1, 1, patt);
if ((pattID == CompCluster::InvalidPatternID || parent->mPattIdConverter.isGroup(pattID)) && patternsPtr) {
patternsPtr->emplace_back(1); // rowspan
patternsPtr->emplace_back(1); // colspan
patternsPtr->insert(patternsPtr->end(), std::begin(patt), std::begin(patt) + 1);
}
compClusPtr->emplace_back(row, col, pattID, curChipData->getChipID());
}
//__________________________________________________
Clusterer::Clusterer() : mPattIdConverter()
{
#ifdef _PERFORM_TIMING_
mTimer.Stop();
mTimer.Reset();
mTimerMerge.Stop();
mTimerMerge.Reset();
#endif
}
//__________________________________________________
void Clusterer::ClustererThread::initChip(const ChipPixelData* curChipData, uint32_t first)
{
// init chip with the 1st unmasked pixel (entry "from" in the mChipData)
prev = column1 + 1;
curr = column2 + 1;
resetColumn(curr);
pixels.clear();
preClusterHeads.clear();
preClusterIndices.clear();
auto pix = curChipData->getData()[first];
currCol = pix.getCol();
curr[pix.getRowDirect()] = 0; // can use getRowDirect since the pixel is not masked
// start the first pre-cluster
preClusterHeads.push_back(0);
preClusterIndices.push_back(0);
pixels.emplace_back(-1, first); // id of current pixel
noLeftCol = true; // flag that there is no column on the left to check yet
}
//__________________________________________________
void Clusterer::ClustererThread::updateChip(const ChipPixelData* curChipData, uint32_t ip)
{
const auto pix = curChipData->getData()[ip];
uint16_t row = pix.getRowDirect(); // can use getRowDirect since the pixel is not masked
if (currCol != pix.getCol()) { // switch the buffers
swapColumnBuffers();
resetColumn(curr);
noLeftCol = false;
if (pix.getCol() > currCol + 1) {
// no connection with previous column, this pixel cannot belong to any of the
// existing preclusters, create a new precluster and flag to check only the row above for next pixels of this column
currCol = pix.getCol();
addNewPrecluster(ip, row);
noLeftCol = true;
return;
}
currCol = pix.getCol();
}
Bool_t orphan = true;
int currPreClusterIdx = -1;
if (noLeftCol) { // check only the row above
if (curr[row - 1] >= 0) {
expandPreCluster(ip, row, curr[row - 1]); // attach to the precluster of the previous row
currPreClusterIdx = curr[row - 1];
orphan = false;
}
} else {
#ifdef _ALLOW_DIAGONAL_ALPIDE_CLUSTERS_
int neighbours[]{curr[row - 1], prev[row], prev[row + 1], prev[row - 1]};
#else
int neighbours[]{curr[row - 1], prev[row]};
#endif
for (auto pci : neighbours) {
if (pci < 0) {
continue;
}
if (orphan) {
expandPreCluster(ip, row, pci); // attach to the adjascent precluster
currPreClusterIdx = pci;
orphan = false;
continue;
}
// merge the roots of both preclusters
int root1 = findRoot(pci);
int root2 = findRoot(currPreClusterIdx);
if (root1 != root2) {
// point to higher index to lower index for consistency
if (root1 < root2) {
preClusterIndices[root2] = root1;
} else {
preClusterIndices[root1] = root2;
}
}
}
}
if (orphan) {
addNewPrecluster(ip, row); // start new precluster
}
}
//__________________________________________________
void Clusterer::ClustererThread::fetchMCLabels(int digID, const ConstMCTruth* labelsDig, int& nfilled)
{
// transfer MC labels to cluster
if (nfilled >= MaxLabels) {
return;
}
const auto& lbls = labelsDig->getLabels(digID);
for (int i = lbls.size(); i--;) {
int ic = nfilled;
for (; ic--;) { // check if the label is already present
if (labelsBuff[ic] == lbls[i]) {
return; // label is found, do nothing
}
}
labelsBuff[nfilled++] = lbls[i];
if (nfilled >= MaxLabels) {
break;
}
}
//
}
//__________________________________________________
void Clusterer::clear()
{
// reset
#ifdef _PERFORM_TIMING_
mTimer.Stop();
mTimer.Reset();
mTimerMerge.Stop();
mTimerMerge.Reset();
#endif
}
//__________________________________________________
void Clusterer::print() const
{
// print settings
LOGP(info, "Clusterizer squashes overflow pixels separated by {} BC and <= {} in row/col seeking down to {} neighbour ROFs", mMaxBCSeparationToSquash, mMaxRowColDiffToMask, mSquashingDepth);
LOGP(info, "Clusterizer masks overflow pixels separated by < {} BC and <= {} in row/col", mMaxBCSeparationToMask, mMaxRowColDiffToMask);
LOGP(info, "Clusterizer does {} drop huge clusters", mDropHugeClusters ? "" : "not");
#ifdef _PERFORM_TIMING_
auto& tmr = const_cast<TStopwatch&>(mTimer); // ugly but this is what root does internally
auto& tmrm = const_cast<TStopwatch&>(mTimerMerge);
LOG(info) << "Inclusive clusterization timing (w/o disk IO): Cpu: " << tmr.CpuTime()
<< " Real: " << tmr.RealTime() << " s in " << tmr.Counter() << " slots";
LOG(info) << "Threads output merging timing : Cpu: " << tmrm.CpuTime()
<< " Real: " << tmrm.RealTime() << " s in " << tmrm.Counter() << " slots";
#endif
}
//__________________________________________________
void Clusterer::reset()
{
// reset for new run
clear();
mNHugeClus = 0;
}