ATLAS Offline Software
Loading...
Searching...
No Matches
MuonSegContainerMergerAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
8#include "TrkTrack/Track.h"
10
11namespace{
12 inline double chi2(const Trk::Segment* seg){
13 if(!seg || !seg->fitQuality()) return FLT_MAX;
14 const Trk::FitQuality* ql = seg->fitQuality();
15 return ql->chiSquared() / ql->numberDoF();
16 }
17
18}
19
22 ATH_CHECK(m_muonCandidateKeys.initialize());
23 ATH_CHECK(m_tagMaps.initialize());
24 if ( !(m_tagMaps.size() + m_muonCandidateKeys.size() ) ){
25 ATH_MSG_FATAL("No candidates were given to read the segments from");
26 return StatusCode::FAILURE;
27 }
28 ATH_MSG_INFO("Use the following Muon tags to dump the segments");
30 ATH_MSG_INFO(" *** "<<key.fullKey());
31 }
32 ATH_MSG_INFO("Use the following combined tags to dump the segments");
34 ATH_MSG_INFO(" --- "<<key.fullKey());
35 }
36 ATH_CHECK(m_segTrkContainerName.initialize());
37 ATH_CHECK(m_assocMapKey.initialize());
40 ATH_CHECK(m_ambiguityProcessor.retrieve(EnableTool{m_solveAmbi}));
41
42 return StatusCode::SUCCESS;
43}
44StatusCode MuonSegContainerMergerAlg::execute(const EventContext& ctx) const {
46 std::unique_ptr<MuonCombined::MuonTagToSegMap> persistency_link = std::make_unique<MuonCombined::MuonTagToSegMap>();
47 std::unique_ptr<Trk::SegmentCollection> out_container = std::make_unique<Trk::SegmentCollection>();
48
50 for (SG::ReadHandle<MuonCandidateCollection>& candidate_coll : m_muonCandidateKeys.makeHandles(ctx)) {
51 if (!candidate_coll.isValid()) {
52 ATH_MSG_FATAL("Failed to retrieve Muon segment candidates " << candidate_coll.fullKey());
53 return StatusCode::FAILURE;
54 } else {
55 ATH_MSG_VERBOSE("Retrieved "<<candidate_coll.fullKey()<< " with size "<<candidate_coll->size());
56 }
57 for (const MuonCombined::MuonCandidate* ms_cand : *candidate_coll) {
58 const std::vector<const Muon::MuonSegment*>& assoc_segs = ms_cand->getSegments();
59 if (assoc_segs.empty()) { ATH_MSG_WARNING("Muon candidate " << ms_cand->toString() << " does not have associated segments."); }
60 for (const Muon::MuonSegment* seg : assoc_segs) { persistency_link->persistify(seg, out_container.get()); }
61 }
62 }
64 std::vector<const MuonCombined::TagBase*> good_tags{};
65 for (SG::ReadHandle<MuonCombined::InDetCandidateToTagMap>& tag_map : m_tagMaps.makeHandles(ctx)) {
66 if (!tag_map.isValid()) {
67 ATH_MSG_FATAL("Failed to retrieve combined tag map "<<tag_map.fullKey());
68 return StatusCode::FAILURE;
69 } else {
70 ATH_MSG_VERBOSE("Retrieved "<<tag_map.fullKey()<< " with size "<<tag_map->size());
71 }
72 good_tags.reserve(tag_map->size() + good_tags.size());
73 for (const auto& tag_pair : *tag_map) {
74 good_tags.push_back(tag_pair.second.get());
75 }
76 }
77 std::stable_sort(good_tags.begin(),good_tags.end(),[](const MuonCombined::TagBase* a, const MuonCombined::TagBase* b){
79 const int auth_a = MuonCombined::authorRank(a->author());
80 const int auth_b = MuonCombined::authorRank(b->author());
81 if (auth_a != auth_b) return auth_a < auth_b;
82 const Trk::Track* prim_a = a->primaryTrack();
83 const Trk::Track* prim_b = b->primaryTrack();
85 if (prim_a && prim_b) {
86 const Trk::Perigee* per_a = prim_a->perigeeParameters();
87 const Trk::Perigee* per_b = prim_b->perigeeParameters();
88 return per_a->pT() > per_b->pT();
89 }
91 if (prim_a || prim_b) return prim_a != nullptr;
92 std::vector<const Muon::MuonSegment*> seg_a = a->associatedSegments();
93 std::vector<const Muon::MuonSegment*> seg_b = b->associatedSegments();
94 const size_t n_segs_a = seg_a.size();
95 const size_t n_segs_b = seg_b.size();
96 if (n_segs_a != n_segs_b) return n_segs_a > n_segs_b;
97 if (!n_segs_a) return false;
98 return chi2(seg_a[0]) < chi2(seg_b[0]);
99 });
100
101 for (const MuonCombined::TagBase* cmb_tag : good_tags) {
102 std::vector<const Muon::MuonSegment*> assoc_segs = cmb_tag->associatedSegments();
103 if (assoc_segs.empty() && cmb_tag->type() != xAOD::Muon::MuonType::CaloTagged) {
104 ATH_MSG_WARNING("Combined candidate " << cmb_tag->toString() << " does not have associated segments");
105 }
106 for (const Muon::MuonSegment* seg : assoc_segs) {
107 persistency_link->persistify(seg, out_container.get());
108 }
109 }
110
112 std::set<const Trk::Segment*> assoc_segs = persistency_link->getPersistifiedSegments();
114 SG::WriteHandle<Trk::SegmentCollection> usedSegWriteHandle{m_segTrkContainerName, ctx};
115 ATH_CHECK(usedSegWriteHandle.record(std::move(out_container)));
116 SG::WriteHandle<MuonCombined::MuonTagToSegMap> assocMapWriteHandle{m_assocMapKey, ctx};
117 ATH_CHECK(assocMapWriteHandle.record(std::move(persistency_link)));
119 if (!m_saveUnassocSegs) return StatusCode::SUCCESS;
120
121 std::vector<const Trk::Segment*> to_copy{};
122
124 for (SG::ReadHandle<Trk::SegmentCollection>& inputSegColl : m_inputSegContainerName.makeHandles(ctx)) {
125 if (!inputSegColl.isValid()) {
126 ATH_MSG_FATAL("Failed to retrieve segment collection " << inputSegColl.fullKey());
127 return StatusCode::FAILURE;
128 }
129 for (const Trk::Segment* seg : *inputSegColl) {
131 if (assoc_segs.count(seg)) continue;
132 to_copy.emplace_back(seg);
133 }
134 }
136 if (m_solveAmbi && to_copy.size() > 1) {
137 std::map<const Trk::Track*, const Trk::Segment*> track_seg_map{};
138 TrackCollection ambi_tracks{};
140 for (const Trk::Segment* seg : to_copy) {
141 auto tsos = std::make_unique<Trk::TrackStates>();
142 Trk::TrackInfo dummy_info{};
143 for (const Trk::MeasurementBase* meas : seg->containedMeasurements()) {
144 tsos->push_back(Muon::MuonTSOSHelper::createMeasTSOS(meas->uniqueClone(), nullptr, Trk::TrackStateOnSurface::Measurement));
145 }
146 std::unique_ptr<Trk::Track> trk =
147 std::make_unique<Trk::Track>(dummy_info, std::move(tsos), seg->fitQuality() ? seg->fitQuality()->uniqueClone() : nullptr);
148 track_seg_map[trk.get()] = seg;
149 ambi_tracks.push_back(std::move(trk));
150 }
151 std::unique_ptr<const TrackCollection> resolved_trks{m_ambiguityProcessor->process(ctx, &ambi_tracks)};
152 std::vector<const Trk::Segment*> resolved_copies{};
153 for (const Trk::Track* res : *resolved_trks) {
154 const Trk::Segment* seg = track_seg_map[res];
156 if (!seg) continue;
157 resolved_copies.emplace_back(seg);
158 }
159 ATH_MSG_DEBUG("Number of segments before the ambiguity solving " << to_copy.size() << " vs. after solving "
160 << resolved_copies.size());
161 to_copy = std::move(resolved_copies);
162 }
163
164 out_container = std::make_unique<Trk::SegmentCollection>();
165 for (const Trk::Segment* seg : to_copy) {
167 const Muon::MuonSegment* muon_seg = dynamic_cast<const Muon::MuonSegment*>(seg);
168 if (!muon_seg) continue;
169 std::unique_ptr<Muon::MuonSegment> copy = std::make_unique<Muon::MuonSegment>(*muon_seg);
170 out_container->push_back(std::move(copy));
171 }
172 SG::WriteHandle<Trk::SegmentCollection> unUsedSegWriteHandle{m_unassocTrkContainerName, ctx};
173 ATH_CHECK(unUsedSegWriteHandle.record(std::move(out_container)));
174 return StatusCode::SUCCESS;
175}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::pair< std::vector< unsigned int >, bool > res
static Double_t a
if(pathvar)
DataVector< Trk::Track > TrackCollection
This typedef represents a collection of Trk::Track objects.
value_type push_back(value_type pElem)
Add an element to the end of the collection.
base-class for combined reconstruction output Provides access to MuonType and Author
Definition TagBase.h:49
SG::ReadHandleKeyArray< Trk::SegmentCollection > m_inputSegContainerName
Optionally segments from containers that are not associated with any segment candidate are dumped int...
virtual StatusCode execute(const EventContext &ctx) const override
SG::WriteHandleKey< MuonCombined::MuonTagToSegMap > m_assocMapKey
Gaudi::Property< bool > m_saveUnassocSegs
virtual StatusCode initialize() override
SG::WriteHandleKey< Trk::SegmentCollection > m_segTrkContainerName
Name of the bulk segment output container.
ToolHandle< Trk::ITrackAmbiguityProcessorTool > m_ambiguityProcessor
SG::ReadHandleKeyArray< MuonCombined::InDetCandidateToTagMap > m_tagMaps
List of all InDetCandidate tags MuidCo, MuGirl from all muon reconstruction streams.
SG::WriteHandleKey< Trk::SegmentCollection > m_unassocTrkContainerName
Name of the unassociated segment container name.
SG::ReadHandleKeyArray< MuonCandidateCollection > m_muonCandidateKeys
Name of all MuonCandidate tags (Main / LRT / EMEO).
This is the common class for 3D segments used in the muon spectrometer.
static std::unique_ptr< Trk::TrackStateOnSurface > createMeasTSOS(std::unique_ptr< Trk::MeasurementBase > meas, std::unique_ptr< Trk::TrackParameters > pars, Trk::TrackStateOnSurface::TrackStateOnSurfaceType type)
create a TSOS with a measurement, takes ownership of the pointers
Property holding a SG store/key/clid from which a ReadHandle is made.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
Class to represent and store fit qualities from track reconstruction in terms of and number of degre...
Definition FitQuality.h:97
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
std::unique_ptr< FitQuality > uniqueClone() const
NVI uniqueClone.
This class is the pure abstract base class for all fittable tracking measurements.
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
const std::vector< const Trk::MeasurementBase * > & containedMeasurements() const
returns the vector of Trk::MeasurementBase objects
Contains information about the 'fitter' of this track.
@ Measurement
This is a measurement, and will at least contain a Trk::MeasurementBase.
double chi2(TH1 *h0, TH1 *h1)
bool copy
Definition calibdata.py:26
void stable_sort(DataModel_detail::iterator< DVL > beg, DataModel_detail::iterator< DVL > end)
Specialization of stable_sort for DataVector/List.