-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
MTDThresholdClusterizer.cc
342 lines (304 loc) · 15 KB
/
MTDThresholdClusterizer.cc
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
// Our own includes
#include "RecoLocalFastTime/FTLClusterizer/interface/MTDArrayBuffer.h"
#include "RecoLocalFastTime/FTLClusterizer/interface/MTDThresholdClusterizer.h"
#include "RecoLocalFastTime/FTLClusterizer/interface/BTLRecHitsErrorEstimatorIM.h"
#include "DataFormats/ForwardDetId/interface/BTLDetId.h"
#include "DataFormats/ForwardDetId/interface/ETLDetId.h"
#include "Geometry/MTDGeometryBuilder/interface/ProxyMTDTopology.h"
#include "Geometry/MTDGeometryBuilder/interface/RectangularMTDTopology.h"
#include "Geometry/MTDCommonData/interface/MTDTopologyMode.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
// STL
#include <stack>
#include <vector>
#include <iostream>
#include <atomic>
#include <cmath>
using namespace std;
//----------------------------------------------------------------------------
//! Constructor:
//----------------------------------------------------------------------------
MTDThresholdClusterizer::MTDThresholdClusterizer(edm::ParameterSet const& conf)
: // Get energy thresholds
theHitThreshold(conf.getParameter<double>("HitThreshold")),
theSeedThreshold(conf.getParameter<double>("SeedThreshold")),
theClusterThreshold(conf.getParameter<double>("ClusterThreshold")),
theTimeThreshold(conf.getParameter<double>("TimeThreshold")),
thePositionThreshold(conf.getParameter<double>("PositionThreshold")),
theNumOfRows(0),
theNumOfCols(0),
theCurrentId(0),
theBuffer(theNumOfRows, theNumOfCols),
bufferAlreadySet(false) {}
/////////////////////////////////////////////////////////////////////////////
MTDThresholdClusterizer::~MTDThresholdClusterizer() {}
// Configuration descriptions
void MTDThresholdClusterizer::fillPSetDescription(edm::ParameterSetDescription& desc) {
desc.add<double>("HitThreshold", 0.);
desc.add<double>("SeedThreshold", 0.);
desc.add<double>("ClusterThreshold", 0.);
desc.add<double>("TimeThreshold", 10.);
desc.add<double>("PositionThreshold", -1.0);
}
//----------------------------------------------------------------------------
//! Prepare the Clusterizer to work on a particular DetUnit. Re-init the
//! size of the panel/plaquette (so update nrows and ncols),
//----------------------------------------------------------------------------
bool MTDThresholdClusterizer::setup(const MTDGeometry* geom, const MTDTopology* topo, const DetId& id) {
theCurrentId = id;
//using geopraphicalId here
const auto& thedet = geom->idToDet(id);
if (thedet == nullptr) {
throw cms::Exception("MTDThresholdClusterizer")
<< "GeographicalID: " << std::hex << id.rawId() << " is invalid!" << std::dec << std::endl;
return false;
}
const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(thedet->topology());
const RectangularMTDTopology& topol = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());
// Get the new sizes.
unsigned int nrows = topol.nrows(); // rows in x
unsigned int ncols = topol.ncolumns(); // cols in y
theNumOfRows = nrows; // Set new sizes
theNumOfCols = ncols;
LogDebug("MTDThresholdClusterizer") << "Buffer size [" << theNumOfRows + 1 << "," << theNumOfCols + 1 << "]";
if (nrows > theBuffer.rows() || ncols > theBuffer.columns()) { // change only when a larger is needed
// Resize the buffer
theBuffer.setSize(nrows, ncols);
bufferAlreadySet = true;
}
return true;
}
//----------------------------------------------------------------------------
//! \brief Cluster hits.
//! This method operates on a matrix of hits
//! and finds the largest contiguous cluster around
//! each seed hit.
//! Input and output data stored in DetSet
//----------------------------------------------------------------------------
void MTDThresholdClusterizer::clusterize(const FTLRecHitCollection& input,
const MTDGeometry* geom,
const MTDTopology* topo,
FTLClusterCollection& output) {
FTLRecHitCollection::const_iterator begin = input.begin();
FTLRecHitCollection::const_iterator end = input.end();
// Do not bother for empty detectors
if (begin == end) {
edm::LogInfo("MTDThresholdClusterizer") << "No hits to clusterize";
return;
}
LogDebug("MTDThresholdClusterizer") << "Input collection " << input.size();
assert(output.empty());
std::set<unsigned> geoIds;
std::multimap<unsigned, unsigned> geoIdToIdx;
unsigned index = 0;
for (const auto& hit : input) {
MTDDetId mtdId = MTDDetId(hit.detid());
if (mtdId.subDetector() != MTDDetId::FastTime) {
throw cms::Exception("MTDThresholdClusterizer")
<< "MTDDetId: " << std::hex << mtdId.rawId() << " is invalid!" << std::dec << std::endl;
}
if (mtdId.mtdSubDetector() == MTDDetId::BTL) {
BTLDetId hitId(hit.detid());
//for BTL topology gives different layout id
DetId geoId = hitId.geographicalId(MTDTopologyMode::crysLayoutFromTopoMode(topo->getMTDTopologyMode()));
geoIdToIdx.emplace(geoId, index);
geoIds.emplace(geoId);
++index;
} else if (mtdId.mtdSubDetector() == MTDDetId::ETL) {
ETLDetId hitId(hit.detid());
DetId geoId = hitId.geographicalId();
geoIdToIdx.emplace(geoId, index);
geoIds.emplace(geoId);
++index;
} else {
throw cms::Exception("MTDThresholdClusterizer")
<< "MTDDetId: " << std::hex << mtdId.rawId() << " is invalid!" << std::dec << std::endl;
}
}
//cluster hits within geoIds (modules)
for (unsigned id : geoIds) {
// Set up the clusterization on this DetId.
if (!setup(geom, topo, DetId(id)))
return;
auto range = geoIdToIdx.equal_range(id);
LogDebug("MTDThresholdClusterizer") << "Matching Ids for " << std::hex << id << std::dec << " ["
<< range.first->second << "," << range.second->second << "]";
// Copy MTDRecHits to the buffer array; select the seed hits
// on the way, and store them in theSeeds.
for (auto itr = range.first; itr != range.second; ++itr) {
const unsigned hitidx = itr->second;
copy_to_buffer(begin + hitidx, geom, topo);
}
FTLClusterCollection::FastFiller clustersOnDet(output, id);
for (unsigned int i = 0; i < theSeeds.size(); i++) {
if (theBuffer.energy(theSeeds[i]) > theSeedThreshold) { // Is this seed still valid?
// Make a cluster around this seed
const FTLCluster& cluster = make_cluster(theSeeds[i]);
// Check if the cluster is above threshold
if (cluster.energy() > theClusterThreshold) {
LogDebug("MTDThresholdClusterizer")
<< "putting in this cluster " << i << " #hits:" << cluster.size() << " E:" << cluster.energy()
<< " T +/- DT:" << cluster.time() << " +/- " << cluster.timeError() << " X:" << cluster.x()
<< " Y:" << cluster.y() << " xpos +/- err " << cluster.getClusterPosX() << " +/- "
<< cluster.getClusterErrorX();
clustersOnDet.push_back(cluster);
}
}
}
// Erase the seeds.
theSeeds.clear();
// Need to clean unused hits from the buffer array.
for (auto itr = range.first; itr != range.second; ++itr) {
const unsigned hitidx = itr->second;
clear_buffer(begin + hitidx);
}
}
}
//----------------------------------------------------------------------------
//! \brief Clear the internal buffer array.
//!
//! MTDs which are not part of recognized clusters are NOT ERASED
//! during the cluster finding. Erase them now.
//!
//----------------------------------------------------------------------------
void MTDThresholdClusterizer::clear_buffer(RecHitIterator itr) { theBuffer.clear(itr->row(), itr->column()); }
//----------------------------------------------------------------------------
//! \brief Copy FTLRecHit into the buffer, identify seeds.
//----------------------------------------------------------------------------
void MTDThresholdClusterizer::copy_to_buffer(RecHitIterator itr, const MTDGeometry* geom, const MTDTopology* topo) {
MTDDetId mtdId = MTDDetId(itr->detid());
int row = itr->row();
int col = itr->column();
GeomDetEnumerators::Location subDet = GeomDetEnumerators::invalidLoc;
float energy = itr->energy();
float time = itr->time();
float timeError = itr->timeError();
float position = itr->position();
float xpos = 0.f;
// position is the longitudinal offset that should be added into local x for bars in phi geometry
LocalPoint local_point(0, 0, 0);
LocalError local_error(0, 0, 0);
if (mtdId.mtdSubDetector() == MTDDetId::BTL) {
subDet = GeomDetEnumerators::barrel;
BTLDetId id = itr->id();
DetId geoId = id.geographicalId(MTDTopologyMode::crysLayoutFromTopoMode(topo->getMTDTopologyMode()));
const auto& det = geom->idToDet(geoId);
const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(det->topology());
const RectangularMTDTopology& topol = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());
LocalPoint lp_pixel(position, 0, 0);
local_point = topol.pixelToModuleLocalPoint(lp_pixel, row, col);
BTLRecHitsErrorEstimatorIM btlError(det, local_point);
local_error = btlError.localError();
xpos = local_point.x();
} else if (mtdId.mtdSubDetector() == MTDDetId::ETL) {
subDet = GeomDetEnumerators::endcap;
ETLDetId id = itr->id();
DetId geoId = id.geographicalId();
const auto& det = geom->idToDet(geoId);
const ProxyMTDTopology& topoproxy = static_cast<const ProxyMTDTopology&>(det->topology());
const RectangularMTDTopology& topol = static_cast<const RectangularMTDTopology&>(topoproxy.specificTopology());
LocalPoint lp_pixel(0, 0, 0);
local_point = topol.pixelToModuleLocalPoint(lp_pixel, row, col);
}
LogDebug("MTDThresholdClusterizer") << "DetId " << mtdId.rawId() << " subd " << mtdId.mtdSubDetector() << " row/col "
<< row << " / " << col << " energy " << energy << " time " << time
<< " time error " << timeError << " local_point " << local_point.x() << " "
<< local_point.y() << " " << local_point.z() << " local error "
<< std::sqrt(local_error.xx()) << " " << std::sqrt(local_error.yy()) << " xpos "
<< xpos;
if (energy > theHitThreshold) {
theBuffer.set(row, col, subDet, energy, time, timeError, local_error, local_point, xpos);
if (energy > theSeedThreshold)
theSeeds.push_back(FTLCluster::FTLHitPos(row, col));
//sort seeds?
}
}
//----------------------------------------------------------------------------
//! \brief The actual clustering algorithm: group the neighboring hits around the seed.
//----------------------------------------------------------------------------
FTLCluster MTDThresholdClusterizer::make_cluster(const FTLCluster::FTLHitPos& hit) {
//First we acquire the seeds for the clusters
GeomDetEnumerators::Location seed_subdet = theBuffer.subDet(hit.row(), hit.col());
float seed_energy = theBuffer.energy(hit.row(), hit.col());
float seed_time = theBuffer.time(hit.row(), hit.col());
float seed_time_error = theBuffer.time_error(hit.row(), hit.col());
auto const seedPoint = theBuffer.local_point(hit.row(), hit.col());
double seed_error_xx = theBuffer.local_error(hit.row(), hit.col()).xx();
double seed_error_yy = theBuffer.local_error(hit.row(), hit.col()).yy();
double seed_xpos = theBuffer.xpos(hit.row(), hit.col());
theBuffer.clear(hit);
AccretionCluster acluster;
acluster.add(hit, seed_energy, seed_time, seed_time_error);
// for BTL position along crystals add auxiliary vectors
std::array<float, AccretionCluster::MAXSIZE> pixel_x{{-99999.}};
std::array<float, AccretionCluster::MAXSIZE> pixel_errx2{{-99999.}};
if (seed_subdet == GeomDetEnumerators::barrel) {
pixel_x[acluster.top()] = seed_xpos;
pixel_errx2[acluster.top()] = seed_error_xx;
}
bool stopClus = false;
//Here we search all hits adjacent to all hits in the cluster.
while (!acluster.empty() && !stopClus) {
//This is the standard algorithm to find and add a hit
auto curInd = acluster.top();
acluster.pop();
for (auto c = std::max(0, int(acluster.y[curInd] - 1));
c < std::min(int(acluster.y[curInd] + 2), int(theBuffer.columns())) && !stopClus;
++c) {
for (auto r = std::max(0, int(acluster.x[curInd] - 1));
r < std::min(int(acluster.x[curInd] + 2), int(theBuffer.rows())) && !stopClus;
++r) {
LogDebug("MTDThresholdClusterizer")
<< "Clustering subdet " << seed_subdet << " around " << curInd << " X,Y = " << acluster.x[curInd] << ","
<< acluster.y[curInd] << " r,c = " << r << "," << c << " energy,time = " << theBuffer.energy(r, c) << " "
<< theBuffer.time(r, c);
if (theBuffer.energy(r, c) > theHitThreshold) {
if (std::abs(theBuffer.time(r, c) - seed_time) >
theTimeThreshold *
sqrt(theBuffer.time_error(r, c) * theBuffer.time_error(r, c) + seed_time_error * seed_time_error))
continue;
if ((seed_subdet == GeomDetEnumerators::barrel) && (theBuffer.subDet(r, c) == GeomDetEnumerators::barrel) &&
(thePositionThreshold > 0)) {
double hit_error_xx = theBuffer.local_error(r, c).xx();
double hit_error_yy = theBuffer.local_error(r, c).yy();
if (((theBuffer.local_point(r, c) - seedPoint).mag2()) >
thePositionThreshold * thePositionThreshold *
(hit_error_xx + seed_error_xx + hit_error_yy + seed_error_yy))
continue;
}
FTLCluster::FTLHitPos newhit(r, c);
if (!acluster.add(newhit, theBuffer.energy(r, c), theBuffer.time(r, c), theBuffer.time_error(r, c))) {
stopClus = true;
break;
}
if (theBuffer.subDet(r, c) == GeomDetEnumerators::barrel) {
pixel_x[acluster.top()] = theBuffer.xpos(r, c);
pixel_errx2[acluster.top()] = theBuffer.local_error(r, c).xx();
}
theBuffer.clear(newhit);
}
}
}
} // while accretion
FTLCluster cluster(theCurrentId,
acluster.isize,
acluster.energy.data(),
acluster.time.data(),
acluster.timeError.data(),
acluster.x.data(),
acluster.y.data(),
acluster.xmin,
acluster.ymin);
// For BTL compute the optimal position along crystal and uncertainty on it in absolute length units
if (seed_subdet == GeomDetEnumerators::barrel) {
float sumW(0.f), sumXW(0.f), sumXW2(0.f);
for (unsigned int index = 0; index < acluster.top(); index++) {
sumW += acluster.energy[index];
sumXW += acluster.energy[index] * pixel_x[index];
sumXW2 += acluster.energy[index] * acluster.energy[index] * pixel_errx2[index];
}
cluster.setClusterPosX(sumXW / sumW);
cluster.setClusterErrorX(std::sqrt(sumXW2) / sumW);
}
return cluster;
}