ATLAS Offline Software
MuonSegContainerMergerAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
8 #include "TrkTrack/Track.h"
10 
11 namespace{
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 
20 MuonSegContainerMergerAlg::MuonSegContainerMergerAlg(const std::string& name, ISvcLocator* pSvcLocator) :
21  AthReentrantAlgorithm(name, pSvcLocator) {}
22 
25  ATH_CHECK(m_muonCandidateKeys.initialize());
26  ATH_CHECK(m_tagMaps.initialize());
27  if ( !(m_tagMaps.size() + m_muonCandidateKeys.size() ) ){
28  ATH_MSG_FATAL("No candidates were given to read the segments from");
29  return StatusCode::FAILURE;
30  }
31  ATH_MSG_INFO("Use the following Muon tags to dump the segments");
33  ATH_MSG_INFO(" *** "<<key.fullKey());
34  }
35  ATH_MSG_INFO("Use the following combined tags to dump the segments");
37  ATH_MSG_INFO(" --- "<<key.fullKey());
38  }
39  ATH_CHECK(m_segTrkContainerName.initialize());
44 
45  return StatusCode::SUCCESS;
46 }
47 StatusCode MuonSegContainerMergerAlg::execute(const EventContext& ctx) const {
49  std::unique_ptr<MuonCombined::MuonTagToSegMap> persistency_link = std::make_unique<MuonCombined::MuonTagToSegMap>();
50  std::unique_ptr<Trk::SegmentCollection> out_container = std::make_unique<Trk::SegmentCollection>();
51 
53  for (SG::ReadHandle<MuonCandidateCollection>& candidate_coll : m_muonCandidateKeys.makeHandles(ctx)) {
54  if (!candidate_coll.isValid()) {
55  ATH_MSG_FATAL("Failed to retrieve Muon segment candidates " << candidate_coll.fullKey());
56  return StatusCode::FAILURE;
57  } else {
58  ATH_MSG_VERBOSE("Retrieved "<<candidate_coll.fullKey()<< " with size "<<candidate_coll->size());
59  }
60  for (const MuonCombined::MuonCandidate* ms_cand : *candidate_coll) {
61  const std::vector<const Muon::MuonSegment*>& assoc_segs = ms_cand->getSegments();
62  if (assoc_segs.empty()) { ATH_MSG_WARNING("Muon candidate " << ms_cand->toString() << " does not have associated segments."); }
63  for (const Muon::MuonSegment* seg : assoc_segs) { persistency_link->persistify(seg, out_container.get()); }
64  }
65  }
67  std::vector<const MuonCombined::TagBase*> good_tags{};
68  for (SG::ReadHandle<MuonCombined::InDetCandidateToTagMap>& tag_map : m_tagMaps.makeHandles(ctx)) {
69  if (!tag_map.isValid()) {
70  ATH_MSG_FATAL("Failed to retrieve combined tag map "<<tag_map.fullKey());
71  return StatusCode::FAILURE;
72  } else {
73  ATH_MSG_VERBOSE("Retrieved "<<tag_map.fullKey()<< " with size "<<tag_map->size());
74  }
75  good_tags.reserve(tag_map->size() + good_tags.size());
76  for (const auto& tag_pair : *tag_map) {
77  good_tags.push_back(tag_pair.second.get());
78  }
79  }
80  std::stable_sort(good_tags.begin(),good_tags.end(),[](const MuonCombined::TagBase* a, const MuonCombined::TagBase* b){
81  if (a->isCommissioning() != b->isCommissioning()) return b->isCommissioning();
83  const int auth_a = MuonCombined::authorRank(a->author());
84  const int auth_b = MuonCombined::authorRank(b->author());
85  if (auth_a != auth_b) return auth_a < auth_b;
86  const Trk::Track* prim_a = a->primaryTrack();
87  const Trk::Track* prim_b = b->primaryTrack();
89  if (prim_a && prim_b) {
90  const Trk::Perigee* per_a = prim_a->perigeeParameters();
91  const Trk::Perigee* per_b = prim_b->perigeeParameters();
92  return per_a->pT() > per_b->pT();
93  }
95  if (prim_a || prim_b) return prim_a != nullptr;
96  std::vector<const Muon::MuonSegment*> seg_a = a->associatedSegments();
97  std::vector<const Muon::MuonSegment*> seg_b = b->associatedSegments();
98  const size_t n_segs_a = seg_a.size();
99  const size_t n_segs_b = seg_b.size();
100  if (n_segs_a != n_segs_b) return n_segs_a > n_segs_b;
101  if (!n_segs_a) return false;
102  return chi2(seg_a[0]) < chi2(seg_b[0]);
103  });
104 
105  for (const MuonCombined::TagBase* cmb_tag : good_tags) {
106  std::vector<const Muon::MuonSegment*> assoc_segs = cmb_tag->associatedSegments();
107  if (assoc_segs.empty() && cmb_tag->type() != xAOD::Muon::CaloTagged) {
108  ATH_MSG_WARNING("Combined candidate " << cmb_tag->toString() << " does not have associated segments");
109  }
110  for (const Muon::MuonSegment* seg : assoc_segs) {
111  persistency_link->persistify(seg, out_container.get());
112  }
113  }
114 
116  std::set<const Trk::Segment*> assoc_segs = persistency_link->getPersistifiedSegments();
118  SG::WriteHandle<Trk::SegmentCollection> usedSegWriteHandle{m_segTrkContainerName, ctx};
119  ATH_CHECK(usedSegWriteHandle.record(std::move(out_container)));
120  SG::WriteHandle<MuonCombined::MuonTagToSegMap> assocMapWriteHandle{m_assocMapKey, ctx};
121  ATH_CHECK(assocMapWriteHandle.record(std::move(persistency_link)));
123  if (!m_saveUnassocSegs) return StatusCode::SUCCESS;
124 
125  std::vector<const Trk::Segment*> to_copy{};
126 
128  for (SG::ReadHandle<Trk::SegmentCollection>& inputSegColl : m_inputSegContainerName.makeHandles(ctx)) {
129  if (!inputSegColl.isValid()) {
130  ATH_MSG_FATAL("Failed to retrieve segment collection " << inputSegColl.fullKey());
131  return StatusCode::FAILURE;
132  }
133  for (const Trk::Segment* seg : *inputSegColl) {
135  if (assoc_segs.count(seg)) continue;
136  to_copy.emplace_back(seg);
137  }
138  }
140  if (m_solveAmbi && to_copy.size() > 1) {
141  std::map<const Trk::Track*, const Trk::Segment*> track_seg_map{};
142  TrackCollection ambi_tracks{};
144  for (const Trk::Segment* seg : to_copy) {
145  auto tsos = std::make_unique<Trk::TrackStates>();
146  Trk::TrackInfo dummy_info{};
147  for (const Trk::MeasurementBase* meas : seg->containedMeasurements()) {
148  tsos->push_back(Muon::MuonTSOSHelper::createMeasTSOS(meas->uniqueClone(), nullptr, Trk::TrackStateOnSurface::Measurement));
149  }
150  std::unique_ptr<Trk::Track> trk =
151  std::make_unique<Trk::Track>(dummy_info, std::move(tsos), seg->fitQuality() ? seg->fitQuality()->uniqueClone() : nullptr);
152  track_seg_map[trk.get()] = seg;
153  ambi_tracks.push_back(std::move(trk));
154  }
155  std::unique_ptr<const TrackCollection> resolved_trks{m_ambiguityProcessor->process(&ambi_tracks)};
156  std::vector<const Trk::Segment*> resolved_copies{};
157  for (const Trk::Track* res : *resolved_trks) {
158  const Trk::Segment* seg = track_seg_map[res];
160  if (!seg) continue;
161  resolved_copies.emplace_back(seg);
162  }
163  ATH_MSG_DEBUG("Number of segments before the ambiguity solving " << to_copy.size() << " vs. after solving "
164  << resolved_copies.size());
165  to_copy = std::move(resolved_copies);
166  }
167 
168  out_container = std::make_unique<Trk::SegmentCollection>();
169  for (const Trk::Segment* seg : to_copy) {
171  const Muon::MuonSegment* muon_seg = dynamic_cast<const Muon::MuonSegment*>(seg);
172  if (!muon_seg) continue;
173  std::unique_ptr<Muon::MuonSegment> copy = std::make_unique<Muon::MuonSegment>(*muon_seg);
174  out_container->push_back(std::move(copy));
175  }
176  SG::WriteHandle<Trk::SegmentCollection> unUsedSegWriteHandle{m_unassocTrkContainerName, ctx};
177  ATH_CHECK(unUsedSegWriteHandle.record(std::move(out_container)));
178  return StatusCode::SUCCESS;
179 }
Trk::TrackInfo
Contains information about the 'fitter' of this track.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:32
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trk::Track
The ATLAS Track class.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/Track.h:73
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
MuonSegContainerMergerAlg::MuonSegContainerMergerAlg
MuonSegContainerMergerAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MuonSegContainerMergerAlg.cxx:20
MuonSegContainerMergerAlg::m_segTrkContainerName
SG::WriteHandleKey< Trk::SegmentCollection > m_segTrkContainerName
Name of the bulk segment output container.
Definition: MuonSegContainerMergerAlg.h:45
MuonSegContainerMergerAlg::m_saveUnassocSegs
Gaudi::Property< bool > m_saveUnassocSegs
Definition: MuonSegContainerMergerAlg.h:54
MuonSegContainerMergerAlg::m_solveAmbi
Gaudi::Property< bool > m_solveAmbi
Definition: MuonSegContainerMergerAlg.h:56
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
Track.h
MuonTSOSHelper.h
MuonCombined::MuonCandidate
Definition: Reconstruction/MuonIdentification/MuonCombinedEvent/MuonCombinedEvent/MuonCandidate.h:25
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
Muon::MuonTSOSHelper::createMeasTSOS
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
Definition: MuonTSOSHelper.h:62
Trk::Segment::containedMeasurements
const std::vector< const Trk::MeasurementBase * > & containedMeasurements() const
returns the vector of Trk::MeasurementBase objects
Definition: TrkEvent/TrkSegment/TrkSegment/Segment.h:166
Trk::Segment
Definition: TrkEvent/TrkSegment/TrkSegment/Segment.h:56
TrackCollection.h
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
chi2
double chi2(TH1 *h0, TH1 *h1)
Definition: comparitor.cxx:522
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonSegContainerMergerAlg::m_muonCandidateKeys
SG::ReadHandleKeyArray< MuonCandidateCollection > m_muonCandidateKeys
Name of all MuonCandidate tags (Main / LRT / EMEO)
Definition: MuonSegContainerMergerAlg.h:34
Trk::FitQuality
Class to represent and store fit qualities from track reconstruction in terms of and number of degre...
Definition: FitQuality.h:97
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DataVector< Trk::Track >
MuonSegContainerMergerAlg::m_ambiguityProcessor
ToolHandle< Trk::ITrackAmbiguityProcessorTool > m_ambiguityProcessor
Definition: MuonSegContainerMergerAlg.h:52
Trk::MeasurementBase
Definition: MeasurementBase.h:58
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
Trk::FitQuality::uniqueClone
std::unique_ptr< FitQuality > uniqueClone() const
NVI uniqueClone.
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
MuonSegContainerMergerAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: MuonSegContainerMergerAlg.cxx:47
MuonCombined::MuonTagToSegMap::persistify
unsigned int persistify(const Muon::MuonSegment *assoc_seg, Trk::SegmentCollection *container)
Pushes back the segment cached in the store gate back to the new segment container meant for write-ou...
Definition: MuonTagToSegMap.cxx:9
MuonSegContainerMergerAlg::m_assocMapKey
SG::WriteHandleKey< MuonCombined::MuonTagToSegMap > m_assocMapKey
Definition: MuonSegContainerMergerAlg.h:47
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
a
TList * a
Definition: liststreamerinfos.cxx:10
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MuonSegContainerMergerAlg::initialize
virtual StatusCode initialize() override
Definition: MuonSegContainerMergerAlg.cxx:23
MuonSegContainerMergerAlg.h
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:569
MuonSegContainerMergerAlg::m_tagMaps
SG::ReadHandleKeyArray< MuonCombined::InDetCandidateToTagMap > m_tagMaps
List of all InDetCandidate tags MuidCo, MuGirl from all muon reconstruction streams.
Definition: MuonSegContainerMergerAlg.h:36
Trk::FitQuality::chiSquared
double chiSquared() const
returns the of the overall track fit
Definition: FitQuality.h:56
MuonSegContainerMergerAlg::m_unassocTrkContainerName
SG::WriteHandleKey< Trk::SegmentCollection > m_unassocTrkContainerName
Name of the unassociated segment container name.
Definition: MuonSegContainerMergerAlg.h:50
Trk::FitQuality::numberDoF
int numberDoF() const
returns the number of degrees of freedom of the overall track or vertex fit as integer
Definition: FitQuality.h:60
calibdata.copy
bool copy
Definition: calibdata.py:27
Muon::MuonSegment
Definition: MuonSpectrometer/MuonReconstruction/MuonRecEvent/MuonSegment/MuonSegment/MuonSegment.h:45
Trk::Segment::fitQuality
const FitQuality * fitQuality() const
return the FitQuality object, returns NULL if no FitQuality is defined
Definition: TrkEvent/TrkSegment/TrkSegment/Segment.h:160
Trk::TrackStateOnSurface::Measurement
@ Measurement
This is a measurement, and will at least contain a Trk::MeasurementBase.
Definition: TrackStateOnSurface.h:101
MuonCombined::TagBase
base-class for combined reconstruction output Provides access to MuonType and Author
Definition: TagBase.h:48
MuonSegContainerMergerAlg::m_inputSegContainerName
SG::ReadHandleKeyArray< Trk::SegmentCollection > m_inputSegContainerName
Optionally segments from containers that are not associated with any segment candidate are dumped int...
Definition: MuonSegContainerMergerAlg.h:41
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37