12 #include "tbb/task_arena.h"
16 using namespace hgcal_clustering;
21 numberOfClustersPerLayer_.clear();
22 cells_.resize(2 * (maxlayer_ + 1));
23 numberOfClustersPerLayer_.resize(2 * (maxlayer_ + 1), 0);
36 for (
unsigned int i = 0;
i < hits.
size(); ++
i) {
39 unsigned int layerOnSide = (rhtools_.getLayerWithOffset(detid) - 1);
43 float sigmaNoise = 1.f;
45 int thickness_index = rhtools_.getSiThickIndex(detid);
46 if (thickness_index == -1)
47 thickness_index = maxNumberOfThickIndices_;
49 double storedThreshold = thresholds_[layerOnSide][thickness_index];
51 storedThreshold = thresholds_[layerOnSide][thickness_index + deltasi_index_regemfac_];
53 sigmaNoise = v_sigmaNoise_[layerOnSide][thickness_index];
55 if (hgrh.
energy() < storedThreshold)
59 if (!dependSensor_ && hgrh.
energy() < ecut_)
62 int offset = ((rhtools_.zside(detid) + 1) >> 1) * maxlayer_;
65 cells_[
layer].detid.emplace_back(detid);
68 if (!rhtools_.isOnlySilicon(layer)) {
69 cells_[
layer].isSi.emplace_back(rhtools_.isSilicon(detid));
74 cells_[
layer].sigmaNoise.emplace_back(sigmaNoise);
80 auto cellsSize = cells_[
l].detid.size();
81 cells_[
l].rho.resize(cellsSize, 0.
f);
82 cells_[
l].delta.resize(cellsSize, 9999999);
83 cells_[
l].nearestHigher.resize(cellsSize, -1);
84 cells_[
l].clusterIndex.resize(cellsSize, -1);
85 cells_[
l].followers.resize(cellsSize);
86 cells_[
l].isSeed.resize(cellsSize,
false);
87 if (rhtools_.isOnlySilicon(l)) {
88 cells_[
l].isSi.resize(cellsSize,
true);
89 cells_[
l].eta.resize(cellsSize, 0.
f);
90 cells_[
l].phi.resize(cellsSize, 0.
f);
101 tbb::this_task_arena::isolate([&] {
102 tbb::parallel_for(
size_t(0),
size_t(2 * maxlayer_ + 2), [&](
size_t i) {
103 prepareDataStructures(i);
106 lt.fill(cells_[i].x, cells_[i].y, cells_[i].
eta, cells_[i].phi, cells_[i].isSi);
109 if (i % maxlayer_ < lastLayerEE_)
110 delta_c = vecDeltas_[0];
111 else if (i % maxlayer_ < (firstLayerBH_ - 1))
112 delta_c = vecDeltas_[1];
114 delta_c = vecDeltas_[2];
116 LogDebug(
"HGCalCLUEAlgo") <<
"maxlayer: " << maxlayer_ <<
" lastLayerEE: " << lastLayerEE_
117 <<
" firstLayerBH: " << firstLayerBH_ <<
"\n";
119 calculateLocalDensity(lt, i, delta_c, delta_r);
120 calculateDistanceToHigher(lt, i, delta_c, delta_r);
121 numberOfClustersPerLayer_[
i] = findAndAssignClusters(i, delta_c, delta_r);
125 for (
unsigned int i = 0;
i < 2 * maxlayer_ + 2; ++
i) {
130 template <
typename T>
132 std::vector<int>
offsets(numberOfClustersPerLayer_.size(), 0);
134 int maxClustersOnLayer = numberOfClustersPerLayer_[0];
136 for (
unsigned layerId = 1; layerId <
offsets.size(); ++layerId) {
137 offsets[layerId] =
offsets[layerId - 1] + numberOfClustersPerLayer_[layerId - 1];
139 maxClustersOnLayer =
std::max(maxClustersOnLayer, numberOfClustersPerLayer_[layerId]);
142 auto totalNumberOfClusters =
offsets.back() + numberOfClustersPerLayer_.back();
143 clusters_v_.resize(totalNumberOfClusters);
144 std::vector<std::vector<int>> cellsIdInCluster;
145 cellsIdInCluster.reserve(maxClustersOnLayer);
147 for (
unsigned int layerId = 0; layerId < 2 * maxlayer_ + 2; ++layerId) {
148 cellsIdInCluster.resize(numberOfClustersPerLayer_[layerId]);
149 auto& cellsOnLayer = cells_[layerId];
150 unsigned int numberOfCells = cellsOnLayer.detid.size();
151 auto firstClusterIdx =
offsets[layerId];
153 for (
unsigned int i = 0;
i < numberOfCells; ++
i) {
154 auto clusterIndex = cellsOnLayer.clusterIndex[
i];
155 if (clusterIndex != -1)
156 cellsIdInCluster[clusterIndex].push_back(
i);
159 std::vector<std::pair<DetId, float>> thisCluster;
161 for (
auto&
cl : cellsIdInCluster) {
166 for (
auto cellIdx :
cl) {
167 energy += cellsOnLayer.weight[cellIdx];
168 thisCluster.emplace_back(cellsOnLayer.detid[cellIdx], 1.f);
169 if (cellsOnLayer.isSeed[cellIdx]) {
170 seedDetId = cellsOnLayer.detid[cellIdx];
173 auto globalClusterIndex = cellsOnLayer.clusterIndex[cl[0]] + firstClusterIdx;
175 clusters_v_[globalClusterIndex] =
177 clusters_v_[globalClusterIndex].setSeed(seedDetId);
181 cellsIdInCluster.clear();
186 template <
typename T>
188 float total_weight = 0.f;
192 unsigned int maxEnergyIndex = 0;
193 float maxEnergyValue = 0.f;
195 auto& cellsOnLayer = cells_[layerId];
200 total_weight += cellsOnLayer.weight[
i];
201 if (cellsOnLayer.weight[
i] > maxEnergyValue) {
202 maxEnergyValue = cellsOnLayer.weight[
i];
208 auto thick = rhtools_.getSiThickIndex(cellsOnLayer.detid[maxEnergyIndex]);
209 bool isSiliconCell = (thick != -1);
213 float total_weight_log = 0.f;
218 if (distance2(
i, maxEnergyIndex, layerId,
false) > positionDeltaRho2_)
220 float rhEnergy = cellsOnLayer.weight[
i];
221 float Wi =
std::max(thresholdW0_[thick] +
std::log(rhEnergy / total_weight), 0.);
222 x_log += cellsOnLayer.x[
i] * Wi;
223 y_log += cellsOnLayer.y[
i] * Wi;
224 total_weight_log += Wi;
227 total_weight = total_weight_log;
232 float rhEnergy = cellsOnLayer.weight[
i];
234 x += cellsOnLayer.x[
i] * rhEnergy;
235 y += cellsOnLayer.y[
i] * rhEnergy;
238 if (total_weight != 0.) {
239 float inv_tot_weight = 1.f / total_weight;
241 x * inv_tot_weight, y * inv_tot_weight, rhtools_.getPosition(cellsOnLayer.detid[maxEnergyIndex]).z());
246 template <
typename T>
248 auto& cellsOnLayer = cells_[layerId];
249 unsigned int numberOfCells = cellsOnLayer.detid.size();
250 bool isOnlySi(
false);
251 if (rhtools_.isOnlySilicon(layerId))
254 for (
unsigned int i = 0;
i < numberOfCells;
i++) {
255 bool isSi = isOnlySi || cellsOnLayer.isSi[
i];
257 float delta = delta_c;
258 std::array<int, 4> search_box = lt.searchBox(
259 cellsOnLayer.x[
i] - delta, cellsOnLayer.x[
i] + delta, cellsOnLayer.y[
i] - delta, cellsOnLayer.y[
i] + delta);
261 for (
int xBin = search_box[0]; xBin < search_box[1] + 1; ++xBin) {
262 for (
int yBin = search_box[2];
yBin < search_box[3] + 1; ++
yBin) {
263 int binId = lt.getGlobalBinByBin(xBin,
yBin);
264 size_t binSize = lt[binId].size();
266 for (
unsigned int j = 0;
j < binSize;
j++) {
267 unsigned int otherId = lt[binId][
j];
268 bool otherSi = isOnlySi || cellsOnLayer.isSi[otherId];
270 if (
distance(
i, otherId, layerId,
false) < delta) {
271 cellsOnLayer.rho[
i] += (
i == otherId ? 1.f : 0.5f) * cellsOnLayer.weight[otherId];
279 std::array<int, 4> search_box = lt.searchBoxEtaPhi(cellsOnLayer.eta[
i] - delta,
280 cellsOnLayer.eta[
i] + delta,
281 cellsOnLayer.phi[
i] - delta,
282 cellsOnLayer.phi[
i] + delta);
283 cellsOnLayer.rho[
i] += cellsOnLayer.weight[
i];
284 float northeast(0), northwest(0), southeast(0), southwest(0),
all(0);
287 for (
int phiBin = search_box[2]; phiBin < search_box[3] + 1; ++phiBin) {
288 int binId = lt.getGlobalBinByBinEtaPhi(
etaBin, phiBin);
289 size_t binSize = lt[binId].size();
291 for (
unsigned int j = 0;
j < binSize;
j++) {
292 unsigned int otherId = lt[binId][
j];
293 if (!cellsOnLayer.isSi[otherId]) {
294 if (
distance(
i, otherId, layerId,
true) < delta) {
299 int dIPhi = otherIPhi - iPhi;
300 dIPhi +=
abs(dIPhi) < 2 ? 0
301 : dIPhi < 0 ? scintMaxIphi_
303 int dIEta = otherIEta - iEta;
304 LogDebug(
"HGCalCLUEAlgo") <<
" Debugging calculateLocalDensity for Scintillator: \n"
305 <<
" cell: " << otherId <<
" energy: " << cellsOnLayer.weight[otherId]
306 <<
" otherIPhi: " << otherIPhi <<
" iPhi: " << iPhi
307 <<
" otherIEta: " << otherIEta <<
" iEta: " << iEta <<
"\n";
310 auto neighborCellContribution = 0.5f * cellsOnLayer.weight[otherId];
311 all += neighborCellContribution;
312 if (dIPhi >= 0 && dIEta >= 0)
313 northeast += neighborCellContribution;
314 if (dIPhi <= 0 && dIEta >= 0)
315 southeast += neighborCellContribution;
316 if (dIPhi >= 0 && dIEta <= 0)
317 northwest += neighborCellContribution;
318 if (dIPhi <= 0 && dIEta <= 0)
319 southwest += neighborCellContribution;
321 LogDebug(
"HGCalCLUEAlgo") <<
" Debugging calculateLocalDensity for Scintillator: \n"
322 <<
" northeast: " << northeast <<
" southeast: " << southeast
323 <<
" northwest: " << northwest <<
" southwest: " << southwest <<
"\n";
329 float neighborsval = (
std::max(northeast, northwest) >
std::max(southeast, southwest))
333 cellsOnLayer.rho[
i] += neighborsval;
335 cellsOnLayer.rho[
i] +=
all;
337 LogDebug(
"HGCalCLUEAlgo") <<
"Debugging calculateLocalDensity: \n"
338 <<
" cell: " <<
i <<
" isSilicon: " << cellsOnLayer.isSi[
i]
339 <<
" eta: " << cellsOnLayer.eta[
i] <<
" phi: " << cellsOnLayer.phi[
i]
340 <<
" energy: " << cellsOnLayer.weight[
i] <<
" density: " << cellsOnLayer.rho[
i] <<
"\n";
344 template <
typename T>
346 const unsigned int layerId,
349 auto& cellsOnLayer = cells_[layerId];
350 unsigned int numberOfCells = cellsOnLayer.detid.size();
351 bool isOnlySi(
false);
352 if (rhtools_.isOnlySilicon(layerId))
355 for (
unsigned int i = 0;
i < numberOfCells;
i++) {
356 bool isSi = isOnlySi || cellsOnLayer.isSi[
i];
359 float i_delta = maxDelta;
360 int i_nearestHigher = -1;
362 float delta = delta_c;
366 std::array<int, 4> search_box = lt.searchBox(
369 for (
int xBin = search_box[0]; xBin < search_box[1] + 1; ++xBin) {
370 for (
int yBin = search_box[2];
yBin < search_box[3] + 1; ++
yBin) {
372 size_t binId = lt.getGlobalBinByBin(xBin,
yBin);
374 size_t binSize = lt[binId].size();
377 for (
unsigned int j = 0;
j < binSize;
j++) {
378 unsigned int otherId = lt[binId][
j];
379 bool otherSi = isOnlySi || cellsOnLayer.isSi[otherId];
381 float dist =
distance(
i, otherId, layerId,
false);
382 bool foundHigher = (cellsOnLayer.rho[otherId] > cellsOnLayer.rho[
i]) ||
383 (cellsOnLayer.rho[otherId] == cellsOnLayer.rho[
i] &&
384 cellsOnLayer.detid[otherId] > cellsOnLayer.detid[
i]);
386 if (foundHigher && dist <= i_delta) {
390 i_nearestHigher = otherId;
397 bool foundNearestHigherInSearchBox = (i_delta != maxDelta);
398 if (foundNearestHigherInSearchBox) {
399 cellsOnLayer.delta[
i] = i_delta;
400 cellsOnLayer.nearestHigher[
i] = i_nearestHigher;
404 cellsOnLayer.delta[
i] = maxDelta;
405 cellsOnLayer.nearestHigher[
i] = -1;
411 std::array<int, 4> search_box = lt.searchBoxEtaPhi(cellsOnLayer.eta[
i] -
range,
412 cellsOnLayer.eta[
i] +
range,
413 cellsOnLayer.phi[
i] -
range,
414 cellsOnLayer.phi[
i] +
range);
416 for (
int xBin = search_box[0]; xBin < search_box[1] + 1; ++xBin) {
417 for (
int yBin = search_box[2];
yBin < search_box[3] + 1; ++
yBin) {
419 size_t binId = lt.getGlobalBinByBinEtaPhi(xBin,
yBin);
421 size_t binSize = lt[binId].size();
424 for (
unsigned int j = 0;
j < binSize;
j++) {
425 unsigned int otherId = lt[binId][
j];
426 if (!cellsOnLayer.isSi[otherId]) {
427 float dist =
distance(
i, otherId, layerId,
true);
428 bool foundHigher = (cellsOnLayer.rho[otherId] > cellsOnLayer.rho[
i]) ||
429 (cellsOnLayer.rho[otherId] == cellsOnLayer.rho[
i] &&
430 cellsOnLayer.detid[otherId] > cellsOnLayer.detid[
i]);
432 if (foundHigher && dist <= i_delta) {
436 i_nearestHigher = otherId;
443 bool foundNearestHigherInSearchBox = (i_delta != maxDelta);
444 if (foundNearestHigherInSearchBox) {
445 cellsOnLayer.delta[
i] = i_delta;
446 cellsOnLayer.nearestHigher[
i] = i_nearestHigher;
450 cellsOnLayer.delta[
i] = maxDelta;
451 cellsOnLayer.nearestHigher[
i] = -1;
454 LogDebug(
"HGCalCLUEAlgo") <<
"Debugging calculateDistanceToHigher: \n"
455 <<
" cell: " <<
i <<
" isSilicon: " << cellsOnLayer.isSi[
i]
456 <<
" eta: " << cellsOnLayer.eta[
i] <<
" phi: " << cellsOnLayer.phi[
i]
457 <<
" energy: " << cellsOnLayer.weight[
i] <<
" density: " << cellsOnLayer.rho[
i]
458 <<
" nearest higher: " << cellsOnLayer.nearestHigher[
i]
459 <<
" distance: " << cellsOnLayer.delta[
i] <<
"\n";
463 template <
typename T>
469 unsigned int nClustersOnLayer = 0;
470 auto& cellsOnLayer = cells_[layerId];
471 unsigned int numberOfCells = cellsOnLayer.detid.size();
472 std::vector<int> localStack;
474 for (
unsigned int i = 0;
i < numberOfCells;
i++) {
475 float rho_c = kappa_ * cellsOnLayer.sigmaNoise[
i];
476 bool isSi = rhtools_.isOnlySilicon(layerId) || cellsOnLayer.isSi[
i];
480 cellsOnLayer.clusterIndex[
i] = -1;
481 bool isSeed = (cellsOnLayer.delta[
i] >
delta) && (cellsOnLayer.rho[
i] >= rho_c);
482 bool isOutlier = (cellsOnLayer.delta[
i] > outlierDeltaFactor_ *
delta) && (cellsOnLayer.rho[
i] < rho_c);
484 cellsOnLayer.clusterIndex[
i] = nClustersOnLayer;
485 cellsOnLayer.isSeed[
i] =
true;
487 localStack.push_back(
i);
489 }
else if (!isOutlier) {
490 cellsOnLayer.followers[cellsOnLayer.nearestHigher[
i]].push_back(
i);
495 while (!localStack.empty()) {
496 int endStack = localStack.back();
497 auto& thisSeed = cellsOnLayer.followers[endStack];
498 localStack.pop_back();
501 for (
int j : thisSeed) {
503 cellsOnLayer.clusterIndex[
j] = cellsOnLayer.clusterIndex[endStack];
505 localStack.push_back(
j);
508 return nClustersOnLayer;
511 template <
typename T>
526 std::vector<double> dummy;
528 dummy.resize(maxNumberOfThickIndices_ + !isNose_, 0);
529 thresholds_.resize(maxlayer_, dummy);
530 v_sigmaNoise_.resize(maxlayer_, dummy);
532 for (
unsigned ilayer = 1; ilayer <= maxlayer_; ++ilayer) {
533 for (
unsigned ithick = 0; ithick < maxNumberOfThickIndices_; ++ithick) {
534 float sigmaNoise = 0.001f * fcPerEle_ * nonAgedNoises_[ithick] * dEdXweights_[ilayer] /
535 (fcPerMip_[ithick] * thicknessCorrection_[ithick]);
536 thresholds_[ilayer - 1][ithick] = sigmaNoise * ecut_;
537 v_sigmaNoise_[ilayer - 1][ithick] = sigmaNoise;
538 LogDebug(
"HGCalCLUEAlgo") <<
"ilayer: " << ilayer <<
" nonAgedNoises: " << nonAgedNoises_[ithick]
539 <<
" fcPerEle: " << fcPerEle_ <<
" fcPerMip: " << fcPerMip_[ithick]
540 <<
" noiseMip: " << fcPerEle_ * nonAgedNoises_[ithick] / fcPerMip_[ithick]
541 <<
" sigmaNoise: " << sigmaNoise <<
"\n";
545 float scintillators_sigmaNoise = 0.001f * noiseMip_ * dEdXweights_[ilayer] / sciThicknessCorrection_;
546 thresholds_[ilayer - 1][maxNumberOfThickIndices_] = ecut_ * scintillators_sigmaNoise;
547 v_sigmaNoise_[ilayer - 1][maxNumberOfThickIndices_] = scintillators_sigmaNoise;
548 LogDebug(
"HGCalCLUEAlgo") <<
"ilayer: " << ilayer <<
" noiseMip: " << noiseMip_
549 <<
" scintillators_sigmaNoise: " << scintillators_sigmaNoise <<
"\n";
554 template <
typename T>
556 auto& cellsOnLayer = cells_[layerId];
557 unsigned int numberOfCells = cellsOnLayer.detid.size();
558 for (
unsigned int i = 0;
i < numberOfCells; ++
i)
559 density_[cellsOnLayer.detid[
i]] = cellsOnLayer.rho[
i];
562 template <
typename T>
constexpr float energy() const
static std::vector< std::string > checklist log
int findAndAssignClusters(const unsigned int layerId, float delta_c, float delta_r)
Density getDensity() override
void setDensity(const unsigned int layerId)
constexpr const DetId & detid() const
void prepareDataStructures(const unsigned int layerId)
void populate(const HGCRecHitCollection &hits) override
constexpr std::array< uint8_t, layerIndexSize > layer
const uint16_t range(const Frame &aFrame)
std::map< DetId, float > Density
void calculateDistanceToHigher(const TILE <, const unsigned int layerId, float delta_c, float delta_r)
math::XYZPoint calculatePosition(const std::vector< int > &v, const unsigned int layerId) const
void calculateLocalDensity(const TILE <, const unsigned int layerId, float delta_c, float delta_r)
void getEventSetupPerAlgorithm(const edm::EventSetup &es) override
double delta_r(const Fourvec &a, const Fourvec &b)
Find the distance between two four-vectors in the two-dimensional space .
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector's numbering enum) ...
Abs< T >::type abs(const T &t)
int iphi() const
get the phi index
std::vector< reco::BasicCluster > getClusters(bool) override
XYZPointD XYZPoint
point in space with cartesian internal representation
std::pair< double, double > calculatePosition(TVirtualPad *myPad, int boundary)
< trclass="colgroup">< tdclass="colgroup"colspan=5 > DT local reconstruction</td ></tr >< tr >< td >< ahref="classDTRecHit1DPair.html"> DTRecHit1DPair</a ></td >< td >< ahref="DataFormats_DTRecHit.html"> edm::RangeMap & lt
static int position[264][3]
void makeClusters() override
int etaBin(const l1t::HGCalMulticluster *cl)
constexpr Detector det() const
get the detector field from this detid