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

#include <ActsStrawTracker.h>

Classes

struct  Impl

Public Member Functions

 ActsStrawTracker (const TGeoManager *geometry, TVirtualMagField *field)
 ~ActsStrawTracker ()
 ActsStrawTracker (const ActsStrawTracker &)=delete
ActsStrawTracker & operator= (const ActsStrawTracker &)=delete
void ClearEvent ()
 Forget the measurements and tracks of the previous event.
Int_t AddMeasurement (Int_t detectorId, Double_t driftRadius, Double_t resolution)
Int_t FindTracks ()
 Find and fit all tracks among the measurements; returns their number.
Int_t FitCandidate (const std::vector< Int_t > &measurements)
std::vector< Int_t > GetTrackMeasurements (Int_t track) const
 Measurements used in the fit of a track.
Int_t GetNTracks () const
RecoTrack GetTrack (Int_t track) const
Bool_t FitVertex (const std::vector< Int_t > &tracks, RecoVertex &vertex) const
Double_t DistanceToHit (Int_t track, const TVector3 &hit) const
Int_t GetNStraws () const
 Number of straws found in the geometry.

Private Attributes

std::unique_ptr< Impl > fImpl

Detailed Description

Straw tracker track and vertex fitting with ACTS.

Finds tracks with its own pattern recognition (StrawPatRec), or fits track candidates from another pattern recognition (or MC truth), with the ACTS Kalman filter; the left/right ambiguity of each straw hit is resolved in the fit. Also fits vertices with the Billoir vertex fitter and extrapolates fitted tracks to arbitrary planes. All inputs and outputs are in the SHiP frame (cm, GeV); ACTS is an implementation detail.

Usage per event: ClearEvent(), AddMeasurement() for each straw hit, then FindTracks() or FitCandidate() for each candidate, then GetTrack()/FitVertex()/DistanceToHit().

Definition at line 33 of file ActsStrawTracker.h.

Constructor & Destructor Documentation

◆ ActsStrawTracker() [1/2]

SHiP::ActsStrawTracker::ActsStrawTracker ( const TGeoManager * geometry,
TVirtualMagField * field )

Build the straw surfaces from geometry and use field for the fit.

Neither is owned; both must outlive the tracker.

Definition at line 384 of file ActsStrawTracker.cxx.

385 {
386 if (geometry == nullptr) {
387 throw std::invalid_argument("ActsStrawTracker: geometry is null");
388 }
389 fImpl = std::make_unique<Impl>(*geometry, field);
390}
std::unique_ptr< Impl > fImpl

◆ ~ActsStrawTracker()

SHiP::ActsStrawTracker::~ActsStrawTracker ( )
default

◆ ActsStrawTracker() [2/2]

SHiP::ActsStrawTracker::ActsStrawTracker ( const ActsStrawTracker & )
delete

Member Function Documentation

◆ AddMeasurement()

Int_t SHiP::ActsStrawTracker::AddMeasurement ( Int_t detectorId,
Double_t driftRadius,
Double_t resolution )

Add a straw measurement and return its index.

driftRadius is the (unsigned) drift distance in cm and resolution its uncertainty. Returns -1 if the detector ID does not belong to a straw.

Definition at line 400 of file ActsStrawTracker.cxx.

401 {
402 const Acts::Surface* surface = fImpl->straws.surface(detectorId);
403 const StrawWire* wire = fImpl->straws.wire(detectorId);
404 if (surface == nullptr || wire == nullptr) {
405 return -1;
406 }
407 const double sigma = resolution * ActsFrame::kShipLength;
408 fImpl->measurements.push_back({surface, wire,
409 std::abs(driftRadius) * ActsFrame::kShipLength,
410 sigma * sigma});
411 return static_cast<Int_t>(fImpl->measurements.size() - 1);
412}
constexpr double kShipLength
Length unit of the SHiP frame, in ACTS units.
Definition ActsFrame.hpp:36

◆ ClearEvent()

void SHiP::ActsStrawTracker::ClearEvent ( )

Forget the measurements and tracks of the previous event.

Definition at line 394 of file ActsStrawTracker.cxx.

394 {
395 fImpl->measurements.clear();
396 fImpl->tracks.clear();
397 fImpl->trackMeasurements.clear();
398}

◆ DistanceToHit()

Double_t SHiP::ActsStrawTracker::DistanceToHit ( Int_t track,
const TVector3 & hit ) const

Distance in cm between a hit and a fitted track at the plane of constant z through the hit, or -1 if the extrapolation fails

Definition at line 810 of file ActsStrawTracker.cxx.

811 {
812 const auto& impl = *fImpl;
813 // Plane of constant z centred on the hit
814 const auto plane =
815 constantZPlane(ActsFrame::positionToActs(hit.X(), hit.Y(), hit.Z()));
816
817 const auto result =
818 impl.extrapolate(impl.extrapolator, impl.trackParameters(track), *plane);
819 if (!result) {
820 return -1.;
821 }
822 const auto& local = result->parameters();
823 return std::hypot(local[Acts::eBoundLoc0], local[Acts::eBoundLoc1]) /
825}
Acts::Vector3 positionToActs(double x, double y, double z)
Definition ActsFrame.hpp:38

◆ FindTracks()

Int_t SHiP::ActsStrawTracker::FindTracks ( )

Find and fit all tracks among the measurements; returns their number.

Definition at line 584 of file ActsStrawTracker.cxx.

584 {
585 auto& impl = *fImpl;
586 std::vector<Int_t> all(impl.measurements.size());
587 std::iota(all.begin(), all.end(), 0);
588 const auto hits = impl.patRecHits(all);
589 const auto candidates = impl.patRec->findTracks(hits);
590
591 // Fit every candidate, then resolve the ambiguities between the fitted
592 // tracks: best first, rejecting tracks that share too many hits with an
593 // accepted one or fit badly
594 struct Fitted {
595 std::size_t candidate;
596 double chi2PerNdf;
597 std::vector<Int_t> measurements;
598 };
599 std::vector<Fitted> fitted;
600 for (std::size_t i = 0; i < candidates.size(); ++i) {
601 const Int_t track = impl.fit(candidates[i], hits);
602 if (track < 0) {
603 continue;
604 }
605 const auto proxy = impl.tracks.getTrack(track);
606 if (proxy.nDoF() > 0) {
607 fitted.push_back(
608 {i, proxy.chi2() / proxy.nDoF(), impl.trackMeasurements[track]});
609 }
610 }
611 const auto rank = [](const Fitted& f) {
612 return f.chi2PerNdf -
613 kTrackHitWeight * static_cast<double>(f.measurements.size());
614 };
615 std::sort(fitted.begin(), fitted.end(),
616 [&](const auto& a, const auto& b) { return rank(a) < rank(b); });
617 std::vector<const Fitted*> accepted;
618 for (const auto& f : fitted) {
619 if (f.chi2PerNdf > kMaxTrackChi2PerNdf ||
620 f.measurements.size() < kMinTrackHits) {
621 continue;
622 }
623 const bool conflict =
624 std::any_of(accepted.begin(), accepted.end(), [&](const Fitted* a) {
625 int shared = 0;
626 for (const Int_t m : f.measurements) {
627 shared +=
628 std::count(a->measurements.begin(), a->measurements.end(), m);
629 }
630 const auto shorter =
631 std::min(f.measurements.size(), a->measurements.size());
632 return shared > kMaxSharedFraction * static_cast<double>(shorter);
633 });
634 if (!conflict) {
635 accepted.push_back(&f);
636 }
637 }
638 // Refit the accepted tracks into an empty container, in candidate order,
639 // so the track indices are contiguous
640 std::sort(accepted.begin(), accepted.end(),
641 [](const Fitted* a, const Fitted* b) {
642 return a->candidate < b->candidate;
643 });
644 impl.tracks.clear();
645 impl.trackMeasurements.clear();
646 for (const Fitted* f : accepted) {
647 impl.fit(candidates[f->candidate], hits);
648 }
649 return GetNTracks();
650}
int i
Definition ShipAna.py:108

◆ FitCandidate()

Int_t SHiP::ActsStrawTracker::FitCandidate ( const std::vector< Int_t > & measurements)

Fit the measurements of one track candidate (indices returned by AddMeasurement), and return the index of the fitted track, or -1 if the candidate cannot be fitted. Measurements that do not fit the track are dropped.

Definition at line 652 of file ActsStrawTracker.cxx.

652 {
653 auto& impl = *fImpl;
654 const auto hits = impl.patRecHits(measurements);
655 const auto candidate = impl.patRec->fitCandidate(hits);
656 return candidate ? impl.fit(*candidate, hits) : -1;
657}

◆ FitVertex()

Bool_t SHiP::ActsStrawTracker::FitVertex ( const std::vector< Int_t > & tracks,
RecoVertex & vertex ) const

Fit a vertex to the given tracks (track indices).

Track IDs in vertex are the indices passed in. Returns false if the fit fails.

Definition at line 708 of file ActsStrawTracker.cxx.

709 {
710 const auto& impl = *fImpl;
711 if (tracks.size() < 2) {
712 return false;
713 }
714 std::vector<Acts::BoundTrackParameters> fitted;
715 for (const Int_t track : tracks) {
716 fitted.push_back(impl.trackParameters(track));
717 }
718
719 // As in the GenFit vertexing: start from the closest approach of the
720 // straight tracks, then extrapolate them through the field to the plane of
721 // constant z through the current estimate, and take the closest approach
722 // of their tangents there, until it no longer moves
723 Acts::Vector3 point = vertexSeed(fitted, impl.gctx);
724 std::vector<Acts::BoundTrackParameters> atPlane;
725 for (int iteration = 0; iteration < kVertexIterations; ++iteration) {
726 const auto plane = constantZPlane(point);
727 atPlane.clear();
728 for (const auto& params : fitted) {
729 auto extrapolated = impl.extrapolate(impl.extrapolator, params, *plane);
730 if (!extrapolated) {
731 return false;
732 }
733 atPlane.push_back(std::move(*extrapolated));
734 }
735 const Acts::Vector3 next = closestPoint(tangents(atPlane, impl.gctx));
736 if (!next.allFinite()) {
737 return false;
738 }
739 const double step = (next - point).norm();
740 point = next;
741 if (step < kVertexTolerance) {
742 break;
743 }
744 }
745
746 // Covariance of the vertex (and of the DOCA) from the covariances of the
747 // tangents' positions and directions on the plane, by linear propagation
748 // with numerical derivatives
749 const auto base = tangents(atPlane, impl.gctx);
750 const int nPar = 4; // loc0, loc1, phi, theta
751 const int n = static_cast<int>(atPlane.size());
752 Eigen::MatrixXd jacobian = Eigen::MatrixXd::Zero(4, nPar * n);
753 Eigen::MatrixXd covariance = Eigen::MatrixXd::Zero(nPar * n, nPar * n);
754 const bool twoTracks = n == 2;
755 const double baseDoca = twoTracks
756 ? lineDistance(base[0].first, base[0].second,
757 base[1].first, base[1].second)
758 : 0.;
759 for (int i = 0; i < n; ++i) {
760 const auto& cov = atPlane[i].covariance().value();
761 covariance.block(nPar * i, nPar * i, nPar, nPar) =
762 cov.topLeftCorner(nPar, nPar);
763 for (int k = 0; k < nPar; ++k) {
764 const double epsilon = std::max(1e-3 * std::sqrt(cov(k, k)), 1e-12);
765 Acts::BoundVector shifted = atPlane[i].parameters();
766 shifted[k] += epsilon;
767 auto lines = base;
768 lines[i] = tangent(atPlane[i].referenceSurface(), shifted, impl.gctx);
769 jacobian.block(0, nPar * i + k, 3, 1) =
770 (closestPoint(lines) - point) / epsilon;
771 if (twoTracks) {
772 jacobian(3, nPar * i + k) =
773 (lineDistance(lines[0].first, lines[0].second, lines[1].first,
774 lines[1].second) -
775 baseDoca) /
776 epsilon;
777 }
778 }
779 }
780 const Eigen::MatrixXd propagated =
781 jacobian * covariance * jacobian.transpose();
782 const Acts::SquareMatrix3 actsCovariance = propagated.topLeftCorner(3, 3);
783
784 const Acts::RotationMatrix3 rotation = ActsFrame::toShipRotation();
785 const Acts::Vector3 position = ActsFrame::positionToShip(point);
786 const Acts::SquareMatrix3 positionCovariance =
787 rotation * actsCovariance * rotation.transpose() /
789 // Fit quality: the DOCA against its uncertainty for two tracks
790 const double docaVariance = propagated(3, 3);
791 const double chi2 =
792 twoTracks && docaVariance > 0. ? baseDoca * baseDoca / docaVariance : 0.;
793 vertex = RecoVertex(TVector3(position.x(), position.y(), position.z()),
794 upperTriangle<3>(positionCovariance), chi2,
795 twoTracks ? 1. : 2. * n - 3.);
796
797 for (int i = 0; i < n; ++i) {
798 const Acts::Vector3 momentum =
800 vertex.AddTrack(tracks[i],
801 TVector3(momentum.x(), momentum.y(), momentum.z()),
802 upperTriangle<3>(shipMomentumCovariance(atPlane[i])));
803 }
804 if (twoTracks) {
805 vertex.SetDoca(baseDoca / ActsFrame::kShipLength);
806 }
807 return true;
808}
Acts::Vector3 momentumToShip(const Acts::Vector3 &momentum)
Definition ActsFrame.hpp:51
Acts::RotationMatrix3 toShipRotation()
Rotation taking ACTS-frame vectors to the SHiP frame.
Definition ActsFrame.hpp:22
Acts::Vector3 positionToShip(const Acts::Vector3 &position)
Definition ActsFrame.hpp:42

◆ GetNStraws()

Int_t SHiP::ActsStrawTracker::GetNStraws ( ) const

Number of straws found in the geometry.

Definition at line 827 of file ActsStrawTracker.cxx.

827 {
828 return static_cast<Int_t>(fImpl->straws.size());
829}

◆ GetNTracks()

Int_t SHiP::ActsStrawTracker::GetNTracks ( ) const

Definition at line 667 of file ActsStrawTracker.cxx.

667 {
668 return static_cast<Int_t>(fImpl->tracks.size());
669}

◆ GetTrack()

RecoTrack SHiP::ActsStrawTracker::GetTrack ( Int_t track) const

Definition at line 671 of file ActsStrawTracker.cxx.

671 {
672 const auto& impl = *fImpl;
673 const auto params = impl.trackParameters(track);
674 const auto proxy = impl.tracks.getTrack(track);
675
676 std::vector<Double_t> residuals;
677 std::vector<Double_t> pulls;
678 for (const auto state : proxy.trackStatesReversed()) {
679 if (!state.hasCalibrated() || !state.hasSmoothed()) {
680 continue;
681 }
682 const double measured = state.template calibrated<1>()(0);
683 const double variance = state.template calibratedCovariance<1>()(0, 0);
684 const double smoothed = state.smoothed()[Acts::eBoundLoc0];
685 const double smoothedVariance =
686 state.smoothedCovariance()(Acts::eBoundLoc0, Acts::eBoundLoc0);
687 const double residual = measured - smoothed;
688 // Residuals of smoothed states have variance V - HCH^T
689 const double residualVariance = variance - smoothedVariance;
690 residuals.push_back(residual / ActsFrame::kShipLength);
691 pulls.push_back(
692 residualVariance > 0. ? residual / std::sqrt(residualVariance) : 0.);
693 }
694 std::reverse(residuals.begin(), residuals.end());
695 std::reverse(pulls.begin(), pulls.end());
696
697 const Acts::Vector3 position =
698 ActsFrame::positionToShip(params.position(impl.gctx));
699 const Acts::Vector3 momentum = ActsFrame::momentumToShip(params.momentum());
700 return RecoTrack(TVector3(position.x(), position.y(), position.z()),
701 TVector3(momentum.x(), momentum.y(), momentum.z()),
702 static_cast<Int_t>(std::lround(params.charge())),
703 upperTriangle<6>(shipCovariance(params, impl.gctx)),
704 proxy.chi2(), proxy.nDoF(), proxy.nMeasurements(),
705 std::move(residuals), std::move(pulls));
706}

◆ GetTrackMeasurements()

std::vector< Int_t > SHiP::ActsStrawTracker::GetTrackMeasurements ( Int_t track) const

Measurements used in the fit of a track.

Definition at line 659 of file ActsStrawTracker.cxx.

659 {
660 if (track < 0 ||
661 static_cast<std::size_t>(track) >= fImpl->trackMeasurements.size()) {
662 throw std::out_of_range("ActsStrawTracker: track index out of range");
663 }
664 return fImpl->trackMeasurements[track];
665}

◆ operator=()

ActsStrawTracker & SHiP::ActsStrawTracker::operator= ( const ActsStrawTracker & )
delete

Member Data Documentation

◆ fImpl

std::unique_ptr<Impl> SHiP::ActsStrawTracker::fImpl
private

Definition at line 84 of file ActsStrawTracker.h.


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