272 {
273 std::vector<int> yHits;
274 std::vector<int> stereoHits;
275 for (
int i = 0; i < static_cast<int>(hits.size()); ++
i) {
276 const auto&
w = *hits[
i].wire;
277 if (isUpstream(w) != upstream) {
278 continue;
279 }
280 (isStereo(w) ? stereoHits : yHits).push_back(i);
281 }
282 const Layers yLayers = groupLayers(hits, yHits);
283 const Layers stereoLayers = groupLayers(hits, stereoHits);
284 const int nY = static_cast<int>(yLayers.size());
285 const int nStereo = static_cast<int>(stereoLayers.size());
286 if (nY <
m_cfg.minYHits || nStereo <
m_cfg.minStereoHits) {
287 return {};
288 }
289
290
291
292
293
294 std::vector<LineSegment> yLines;
295 for (const auto& [la, lb] : seedLayerPairs(yLayers, hits)) {
296 {
297 for (const int a : yLayers[la]) {
298 for (const int b : yLayers[lb]) {
299 const auto& wa = *hits[a].wire;
300 const auto& wb = *hits[b].wire;
301 for (const int sa : {-1, 1}) {
302 for (const int sb : {-1, 1}) {
303 LineSegment line;
304 line.z0 = wa.z;
305 line.y0 = wa.y + sa * hits[a].drift;
306 line.ty = (wb.y + sb * hits[b].drift - line.y0) / (wb.z - wa.z);
307 if (std::abs(line.ty) >
m_cfg.maxSlope) {
308 continue;
309 }
310 line.hits = {a, b};
311 for (int l = 0; l < nY; ++l) {
312 if (l == la || l == lb) {
313 continue;
314 }
316 closestHit(line, yLayers[l], hits,
m_cfg.tolerance);
317 if (h >= 0) {
318 line.hits.push_back(h);
319 }
320 }
321 if (
static_cast<int>(line.hits.size()) <
m_cfg.minYHits) {
322 continue;
323 }
324 if (fitLine(line, hits,
false,
m_cfg.tolerance) &&
325 static_cast<int>(line.hits.size()) >=
m_cfg.minYHits) {
326 yLines.push_back(std::move(line));
327 }
328 }
329 }
330 }
331 }
332 }
333 }
334
335
336
337
338
339
340
341 struct XPoint {
342 int hit;
345 };
346 std::vector<LineSegment> segments;
347 for (const auto& yLine : yLines) {
348 std::vector<std::vector<XPoint>> points(nStereo);
349 for (int l = 0; l < nStereo; ++l) {
350 for (const int h : stereoLayers[l]) {
351 const auto&
w = *hits[
h].wire;
352 const Eigen::Vector3d
m = signDirection(yLine, w);
353 if (std::abs(
m.x()) < 1e-3) {
354 continue;
355 }
356 for (const int s : {-1, 1}) {
358 w.x + (
s * hits[
h].drift -
m.y() * (yLine.y(
w.z) -
w.y)) /
m.x();
359
360 const double along = (
x -
w.x) *
w.dx + (yLine.y(
w.z) -
w.y) *
w.dy;
361 if (std::abs(along) <=
w.halfLength) {
362 points[l].push_back({
h,
w.z,
x});
363 }
364 }
365 }
366 }
367
368 std::vector<LineSegment> xLines;
369 for (const auto& [la, lb] : seedLayerPairs(stereoLayers, hits)) {
370 {
371 for (const auto& pa : points[la]) {
372 for (const auto& pb : points[lb]) {
373 LineSegment line = yLine;
374 line.tx = (pb.x - pa.x) / (pb.z - pa.z);
375 if (std::abs(line.tx) >
m_cfg.maxSlope) {
376 continue;
377 }
378 line.x0 = pa.x + line.tx * (line.z0 - pa.z);
379 line.signs.clear();
380 line.hits.push_back(pa.hit);
381 line.hits.push_back(pb.hit);
382 for (int l = 0; l < nStereo; ++l) {
383 if (l == la || l == lb) {
384 continue;
385 }
387 closestHit(line, stereoLayers[l], hits,
m_cfg.tolerance);
388 if (h >= 0) {
389 line.hits.push_back(h);
390 }
391 }
392 if (countHits(line, hits, isStereo) <
m_cfg.minStereoHits) {
393 continue;
394 }
395 if (fitLine(line, hits,
true,
m_cfg.tolerance) &&
396 countHits(line, hits, isY) >=
m_cfg.minYHits &&
397 countHits(line, hits, isStereo) >=
m_cfg.minStereoHits &&
398 countHits(line, hits, isU) >=
m_cfg.minHitsPerStereoView &&
399 countHits(line, hits, isV) >=
m_cfg.minHitsPerStereoView) {
400 xLines.push_back(std::move(line));
401 }
402 }
403 }
404 }
405 }
406 xLines = removeDuplicates(
407 std::move(xLines), std::numeric_limits<int>::max(), kSameLineFraction);
408 if (static_cast<int>(xLines.size()) > kXLinesPerYLine) {
409 xLines.resize(kXLinesPerYLine);
410 }
411 for (auto& line : xLines) {
412 segments.push_back(std::move(line));
413 }
414 }
415
416
417
418 segments = removeDuplicates(std::move(segments),
m_cfg.maxSharedHits);
419 if (maxSegments > 0 && static_cast<int>(segments.size()) > maxSegments) {
420 segments.resize(maxSegments);
421 }
422 return segments;
423}