ATLAS Offline Software
Loading...
Searching...
No Matches
TrackSegmentAssociationTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3*/
4
6
12#include "TrkTrack/Track.h"
13
14namespace {
15
16 struct MatchResult {
17 const Muon::MuonSegment* link{nullptr};
18 Muon::MuonSegmentKey key;
19 unsigned int numberOfMatchedMeasurements;
20 bool isSelected{true};
21 MatchResult(const Muon::MuonSegment* link_, const Muon::MuonSegmentKey& key_, unsigned int numberOfMatchedMeasurements_) :
22 link(link_), key(key_), numberOfMatchedMeasurements(numberOfMatchedMeasurements_) {}
23 };
24} // namespace
25
26namespace MuonCombined {
27
28 TrackSegmentAssociationTool::TrackSegmentAssociationTool(const std::string& t, const std::string& n, const IInterface* p) :
29 AthAlgTool(t, n, p) {
30 declareInterface<IMuonTrackToSegmentAssociationTool>(this);
31 }
32
34 ATH_CHECK(m_edmHelperSvc.retrieve());
35 ATH_CHECK(m_printer.retrieve());
36 ATH_CHECK(m_idHelperSvc.retrieve());
37 return StatusCode::SUCCESS;
38 }
39
41 std::vector<const Muon::MuonSegment*>& assoc_segments) const {
42 if (!segments) {
43 ATH_MSG_DEBUG("no segment container passed, returning");
44 return false;
45 }
46 if (segments->empty()) {
47 ATH_MSG_DEBUG("no segments in container, returning");
48 return false;
49 }
50 std::vector<MatchResult> matched_segs;
51 ATH_MSG_DEBUG("track " << m_printer->print(track) << std::endl << m_printer->printStations(track));
52
53 Muon::MuonSegmentKey trackKeys(track.measurementsOnTrack()->stdcont());
54
55 Muon::CompareMuonSegmentKeys compareKeys{};
56
57 for (const Trk::Segment* trk_seg : *segments) {
58 const Muon::MuonSegment* muonSegment = dynamic_cast<const Muon::MuonSegment*>(trk_seg);
59 if (!muonSegment) continue;
60 Muon::MuonSegmentKey segmentKeys(*muonSegment);
61 Muon::CompareMuonSegmentKeys::OverlapResult overlapResult = compareKeys(trackKeys, segmentKeys, true);
62 if (overlapResult == Muon::CompareMuonSegmentKeys::NoOverlap) continue;
63 MatchResult matchResult(muonSegment, segmentKeys, compareKeys.intersectionSize);
64 matched_segs.push_back(matchResult);
65 ATH_MSG_DEBUG("numberOfMatchedMeasurements = " << matchResult.numberOfMatchedMeasurements);
66 }
67 ATH_MSG_DEBUG("Number of matched space-time points = " << matched_segs.size());
68
69 // refined selection
70 for (unsigned int i = 0; i < matched_segs.size(); i++) {
71 MatchResult& result_i = matched_segs[i];
72 if (!result_i.isSelected) continue;
73
74 const Muon::MuonSegment* seg_i = result_i.link;
75 int nMatched_i = result_i.numberOfMatchedMeasurements;
76 float chi2PerDof_i = seg_i->fitQuality()->chiSquared() / seg_i->fitQuality()->numberDoF();
77
78 for (unsigned int j = i + 1; j < matched_segs.size(); j++) {
79 MatchResult& result_j = matched_segs[j];
80 if (!result_j.isSelected) continue;
81
82 const Muon::MuonSegment* seg_j = result_j.link;
83 int nMatched_j = result_j.numberOfMatchedMeasurements;
84 float chi2PerDof_j = seg_j->fitQuality()->chiSquared() / seg_j->fitQuality()->numberDoF();
85
86 // In case the two segments have common hits:
87 // 1) choose the one with higher number of matched hits to the track.
88 // 2) if the numbers of matched hits are the same, choose the one with smaller chi2/DoF.
89 Muon::CompareMuonSegmentKeys::OverlapResult overlapResult = compareKeys(result_i.key, result_j.key, true);
90 if (overlapResult == Muon::CompareMuonSegmentKeys::NoOverlap) continue;
91 if (nMatched_j > nMatched_i) {
92 result_i.isSelected = false;
93 } else if (nMatched_j < nMatched_i) {
94 result_j.isSelected = false;
95 } else {
96 if (chi2PerDof_j < chi2PerDof_i)
97 result_i.isSelected = false;
98 else
99 result_j.isSelected = false;
100 }
101
102 ATH_MSG_VERBOSE("Segments " << i << " and " << j << " have " << compareKeys.intersectionSize << "hit(s) in common.");
103 if (result_i.isSelected)
104 ATH_MSG_VERBOSE("Keeping segment " << i);
105 else
106 ATH_MSG_VERBOSE("Keeping segment " << j);
107 }
108 }
109
110 for (MatchResult& matches : matched_segs) {
111 if (matches.isSelected) { assoc_segments.push_back(matches.link); }
112 }
113 std::sort(assoc_segments.begin(), assoc_segments.end(), [this](const Muon::MuonSegment* a, const Muon::MuonSegment* b) -> bool {
114 using chamIdx = Muon::MuonStationIndex::ChIndex;
115 chamIdx ch_a = m_idHelperSvc->chamberIndex(m_edmHelperSvc->chamberId(*a));
116 chamIdx ch_b = m_idHelperSvc->chamberIndex(m_edmHelperSvc->chamberId(*b));
117 Muon::MuonStationIndex::StIndex st_a = Muon::MuonStationIndex::toStationIndex(ch_a);
118 Muon::MuonStationIndex::StIndex st_b = Muon::MuonStationIndex::toStationIndex(ch_b);
119 if (st_a != st_b) return st_a < st_b;
121 if (ch_a == chamIdx::CSL || ch_a == chamIdx::CSS || ch_b == chamIdx::CSS || ch_b == chamIdx::CSL)
122 return (ch_a == chamIdx::CSL) + 2 * (ch_a == chamIdx::CSS) > (ch_b == chamIdx::CSL) + 2 * (ch_b == chamIdx::CSS);
123 return ch_a < ch_b;
124 });
125 if (msgLevel(MSG::DEBUG)) {
126 std::stringstream sstr;
127 for (const Muon::MuonSegment*& seg : assoc_segments) {
128 sstr << Muon::MuonStationIndex::chName(m_idHelperSvc->chamberIndex(m_edmHelperSvc->chamberId(*seg))) << " ("
129 << Muon::MuonStationIndex::technologyName(m_idHelperSvc->technologyIndex(m_edmHelperSvc->chamberId(*seg))) << "), ";
130 }
131 ATH_MSG_DEBUG("Selected segments " << assoc_segments.size() << " " << sstr.str());
132 }
133 return true;
134 }
135
136} // namespace MuonCombined
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
static Double_t a
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
bool empty() const noexcept
Returns true if the collection is empty.
PublicToolHandle< Muon::MuonEDMPrinterTool > m_printer
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
TrackSegmentAssociationTool(const std::string &, const std::string &, const IInterface *)
ServiceHandle< Muon::IMuonEDMHelperSvc > m_edmHelperSvc
bool associatedSegments(const Trk::Track &muon, const Trk::SegmentCollection *segments, std::vector< const Muon::MuonSegment * > &assocSegmentVec) const override
IMuonSegmentTagTool interface: build muons from ID and MuonSegments.
Class to cache the identifiers on a segment in sets that can later be used to perform an overlap remo...
This is the common class for 3D segments used in the muon spectrometer.
int numberDoF() const
returns the number of degrees of freedom of the overall track or vertex fit as integer
Definition FitQuality.h:60
double chiSquared() const
returns the of the overall track fit
Definition FitQuality.h:56
Base class for all TrackSegment implementations, extends the common MeasurementBase.
const FitQuality * fitQuality() const
return the FitQuality object, returns NULL if no FitQuality is defined
The MuonTagToSegMap is an auxillary construct that links the MuonSegments associated with a combined ...
const std::string & technologyName(TechnologyIndex index)
convert LayerIndex into a string
const std::string & chName(ChIndex index)
convert ChIndex into a string
std::pair< DCOnTrackVec, DCVec > MatchResult
counts the number of hits shared by the two segments
DataVector< Trk::Segment > SegmentCollection
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
OverlapResult
enum for the overlap result