FairShip
Loading...
Searching...
No Matches
SHiP::StrawPatRec Class Reference

#include <StrawPatRec.hpp>

Collaboration diagram for SHiP::StrawPatRec:
[legend]

Classes

struct  Config

Public Member Functions

 StrawPatRec (const Config &config, double bendingZ, double fieldIntegral)
std::vector< TrackCandidate > findTracks (const std::vector< PatRecHit > &hits) const
 Find tracks among all hits of the event.
std::optional< TrackCandidate > fitCandidate (const std::vector< PatRecHit > &hits) const

Private Member Functions

std::vector< LineSegment > findSegments (const std::vector< PatRecHit > &hits, bool upstream, int maxSegments) const
std::vector< TrackCandidate > matchSegments (const std::vector< LineSegment > &upstream, const std::vector< LineSegment > &downstream) const
std::optional< TrackCandidate > match (const LineSegment &upstream, const LineSegment &downstream) const

Private Attributes

Config m_cfg
double m_bendingZ
double m_fieldIntegral

Detailed Description

Straw tracker pattern recognition from drift circles.

The spectrometer has two stations before and two after the magnet, whose field (mostly Bx) bends tracks in y. Within each pair of stations tracks are straight lines, which are found from the drift circles of the hits: first in the y views, then, for each y line, in the stereo views. The segments before and after the magnet are then matched, which also gives the momentum and charge.

Definition at line 58 of file StrawPatRec.hpp.

Constructor & Destructor Documentation

◆ StrawPatRec()

SHiP::StrawPatRec::StrawPatRec ( const Config & config,
double bendingZ,
double fieldIntegral )

bendingZ is the centre of the field integral along z (cm) and fieldIntegral the integral of Bx along z through the magnet (T m).

Definition at line 267 of file StrawPatRec.cpp.

269 : m_cfg(config), m_bendingZ(bendingZ), m_fieldIntegral(fieldIntegral) {}

Member Function Documentation

◆ findSegments()

std::vector< LineSegment > SHiP::StrawPatRec::findSegments ( const std::vector< PatRecHit > & hits,
bool upstream,
int maxSegments ) const
private

Definition at line 271 of file StrawPatRec.cpp.

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 // Seeds pair a hit in one of the first two layers with one in the last two,
290 // of the pair of stations and of each station
291
292 // y lines from pairs of drift circles: the two candidate y positions of
293 // each hit give four lines per pair
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 }
315 const int h =
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 // The y lines are not deduplicated: tracks that overlap in y have similar
335 // y lines, and are told apart only in the stereo views, and the same hits
336 // can fit with the sides of some wires mirrored, which projects the stereo
337 // hits differently. Duplicate segments are removed after the stereo views.
338
339 // For each y line, find the x line in the stereo views. Each stereo hit
340 // gives two x positions, one for each side of the wire.
341 struct XPoint {
342 int hit;
343 double z;
344 double x;
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}) {
357 const double x =
358 w.x + (s * hits[h].drift - m.y() * (yLine.y(w.z) - w.y)) / m.x();
359 // Only where the wire actually is
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 // Several x lines per y line (see kXLinesPerYLine)
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 }
386 const int h =
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 // Only near-duplicates are removed here. Neighbouring tracks share
416 // straws, and the choice between conflicting candidates is left to the
417 // fit of the whole track.
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}
constexpr Double_t m
int i
Definition ShipAna.py:108

◆ findTracks()

std::vector< TrackCandidate > SHiP::StrawPatRec::findTracks ( const std::vector< PatRecHit > & hits) const

Find tracks among all hits of the event.

Definition at line 486 of file StrawPatRec.cpp.

487 {
488 return matchSegments(findSegments(hits, true, 0),
489 findSegments(hits, false, 0));
490}
std::vector< LineSegment > findSegments(const std::vector< PatRecHit > &hits, bool upstream, int maxSegments) const
std::vector< TrackCandidate > matchSegments(const std::vector< LineSegment > &upstream, const std::vector< LineSegment > &downstream) const

◆ fitCandidate()

std::optional< TrackCandidate > SHiP::StrawPatRec::fitCandidate ( const std::vector< PatRecHit > & hits) const

Fit the hits of one given candidate, resolving the left/right ambiguity. Returns nothing if the hits do not form segments on both sides.

Definition at line 492 of file StrawPatRec.cpp.

493 {
494 const auto upstream = findSegments(hits, true, 1);
495 const auto downstream = findSegments(hits, false, 1);
496 if (upstream.empty() || downstream.empty()) {
497 return std::nullopt;
498 }
499 return match(upstream.front(), downstream.front());
500}
std::optional< TrackCandidate > match(const LineSegment &upstream, const LineSegment &downstream) const

◆ match()

std::optional< TrackCandidate > SHiP::StrawPatRec::match ( const LineSegment & upstream,
const LineSegment & downstream ) const
private

Definition at line 425 of file StrawPatRec.cpp.

426 {
427 TrackCandidate candidate{upstream, downstream};
428 // The field kicks the track in y by q * kKick * integral(Bx dz) / pz
429 const double kink = downstream.ty - upstream.ty;
430 const double kick = kKick * m_fieldIntegral;
431 if (kink == 0. || kick == 0.) {
432 return std::nullopt;
433 }
434 const double pz = std::min(std::abs(kick / kink), kMaxMomentum);
435 candidate.momentum = pz * std::sqrt(1. + upstream.tx * upstream.tx +
436 upstream.ty * upstream.ty);
437 candidate.charge = kink / kick > 0. ? 1 : -1;
438
439 const double z = m_bendingZ;
440 const double p = candidate.momentum;
441 const auto sigma = [p](double a, double b) { return std::hypot(a, b / p); };
442 const double dx = (upstream.x(z) - downstream.x(z)) /
443 sigma(m_cfg.matchSigmaX, m_cfg.matchSigmaXPerP);
444 const double dtx = (upstream.tx - downstream.tx) /
445 sigma(m_cfg.matchSigmaTx, m_cfg.matchSigmaTxPerP);
446 const double dy = (upstream.y(z) - downstream.y(z)) /
447 sigma(m_cfg.matchSigmaY, m_cfg.matchSigmaYPerP);
448 candidate.matchChi2 = dx * dx + dtx * dtx + dy * dy;
449 return candidate;
450}

◆ matchSegments()

std::vector< TrackCandidate > SHiP::StrawPatRec::matchSegments ( const std::vector< LineSegment > & upstream,
const std::vector< LineSegment > & downstream ) const
private

Definition at line 452 of file StrawPatRec.cpp.

454 {
455 std::vector<TrackCandidate> pairs;
456 for (const auto& u : upstream) {
457 for (const auto& d : downstream) {
458 auto candidate = match(u, d);
459 if (candidate &&
460 (candidate->matchChi2 < m_cfg.maxMatchChi2 ||
461 (candidate->momentum < m_cfg.lowMomentum &&
462 candidate->matchChi2 < m_cfg.maxLowMomentumMatchChi2))) {
463 pairs.push_back(std::move(*candidate));
464 }
465 }
466 }
467 // Greedy one-to-one assignment, best match first
468 std::sort(pairs.begin(), pairs.end(), [](const auto& a, const auto& b) {
469 return a.matchChi2 < b.matchChi2;
470 });
471 std::vector<TrackCandidate> tracks;
472 std::set<std::vector<int>> usedUp;
473 std::set<std::vector<int>> usedDown;
474 for (auto& pair : pairs) {
475 if (usedUp.count(pair.upstream.hits) ||
476 usedDown.count(pair.downstream.hits)) {
477 continue;
478 }
479 usedUp.insert(pair.upstream.hits);
480 usedDown.insert(pair.downstream.hits);
481 tracks.push_back(std::move(pair));
482 }
483 return tracks;
484}

Member Data Documentation

◆ m_bendingZ

double SHiP::StrawPatRec::m_bendingZ
private

Definition at line 118 of file StrawPatRec.hpp.

◆ m_cfg

Config SHiP::StrawPatRec::m_cfg
private

Definition at line 117 of file StrawPatRec.hpp.

◆ m_fieldIntegral

double SHiP::StrawPatRec::m_fieldIntegral
private

Definition at line 119 of file StrawPatRec.hpp.


The documentation for this class was generated from the following files: