ATLAS Offline Software
MuonSegmentFinderAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "MuonSegmentFinderAlg.h"
6 
21 
22 
24  ATH_CHECK(m_idHelperSvc.retrieve());
25  ATH_CHECK(m_printer.retrieve());
27  ATH_CHECK(m_segmentSelector.retrieve(DisableTool{m_segQuality<0}));
29  ATH_CHECK(m_patternCalibration.retrieve(DisableTool{!m_runMdtSegments}));
30  ATH_CHECK(m_segmentMaker.retrieve(DisableTool{!m_runMdtSegments}));
31 
32  const bool doNSW = m_doSTgcSegments || m_doMMSegments;
33  ATH_CHECK(m_clusterCreator.retrieve(DisableTool{!doNSW}));
34  ATH_CHECK(m_clusterSegMakerNSW.retrieve(DisableTool{!doNSW}));
35  ATH_CHECK(m_csc2dSegmentFinder.retrieve(DisableTool{m_cscPrdsKey.empty()}));
36  ATH_CHECK(m_csc4dSegmentFinder.retrieve(DisableTool{m_cscPrdsKey.empty()}));
37 
38  ATH_CHECK(m_segmentCollectionKey.initialize());
40  ATH_CHECK(m_segmentNSWCollectionKey.initialize(doNSW && !m_segmentNSWCollectionKey.empty()));
41  ATH_CHECK(m_cscPrdsKey.initialize(!m_cscPrdsKey.empty())); // check for layouts without CSCs
42  ATH_CHECK(m_patternCollKey.initialize());
43 
44  return StatusCode::SUCCESS;
45 }
46 
47 StatusCode MuonSegmentFinderAlg::execute(const EventContext& ctx) const {
48 
49  NSWSegmentCache nswCache{};
50  nswCache.buildQuads = !m_segmentNSWCollectionKey.empty();
51 
52  std::unique_ptr<Trk::SegmentCollection> segmentContainer = std::make_unique<Trk::SegmentCollection>();
53  std::unique_ptr<Trk::SegmentCollection> nswSegmentContainer = !m_segmentNSWCollectionKey.empty() ? std::make_unique<Trk::SegmentCollection>()
54  : nullptr;
55 
56 
57  const MuonPatternCombinationCollection* patternColl{nullptr};
58  ATH_CHECK(SG::get(patternColl, m_patternCollKey, ctx));
59  ATH_MSG_DEBUG("Processing the pattern collections with " << patternColl->size() << " Collections ");
60 
61  for (const Muon::MuonPatternCombination* patt : *patternColl) {
62  ATH_MSG_DEBUG("Working on pattern combination " << m_printer->print(*patt));
63  // check the technology & call the corresponding segment finder
64 
65  ATH_CHECK(createSegmentsWithMDTs(ctx, patt, segmentContainer.get()));
66 
67 
68  createNSWSegments(ctx, patt, nswCache);
69 
71  segmentContainer->insert(segmentContainer->end(), std::make_move_iterator(nswCache.constructedSegs.begin()),
72  std::make_move_iterator(nswCache.constructedSegs.end()));
73 
74  nswCache.constructedSegs.clear();
75 
76  if (!nswSegmentContainer) continue;
77  nswSegmentContainer->insert(nswSegmentContainer->end(), std::make_move_iterator(nswCache.quadSegs.begin()),
78  std::make_move_iterator(nswCache.quadSegs.end()));
79  nswCache.quadSegs.clear();
80 
81  } // end loop on pattern combinations
82 
83  m_segmentOverlapRemovalTool->removeDuplicates(*segmentContainer);
84 
85  std::unique_ptr<MuonSegmentCombinationCollection> csc4dSegmentCombinations{};
86  ATH_CHECK(createCscSegments(ctx, csc4dSegmentCombinations));
87  appendSegmentsFromCombi(csc4dSegmentCombinations, segmentContainer.get());
88 
90  ATH_MSG_DEBUG("segments before overlap removal: " << segmentContainer->size());
91  m_segmentOverlapRemovalTool->removeDuplicates(*segmentContainer);
92  ATH_MSG_DEBUG(" Segments after overlap removal: " << segmentContainer->size());
93 
94 
95  if (m_printSummary){
96  ATH_MSG_INFO("Number of segments found " << segmentContainer->size());
97  for (Trk::Segment* tseg : *segmentContainer) {
98  const Muon::MuonSegment* mseg{dynamic_cast<Muon::MuonSegment*>(tseg)};
99  ATH_MSG_INFO(m_printer->print(*mseg)<<std::endl<<m_printer->print(mseg->containedMeasurements())<<std::endl);
100  }
101  }
102 
103 
105  ATH_CHECK(handle.record(std::move(segmentContainer)));
106 
107  if (!m_segmentNSWCollectionKey.empty()) {
108  m_segmentOverlapRemovalTool->removeDuplicates(*nswSegmentContainer);
110  ATH_CHECK(handle_segNSW.record(std::move(nswSegmentContainer)));
111  }
112  return StatusCode::SUCCESS;
113 } // execute
114 
116  std::unique_ptr<MuonSegmentCombinationCollection>& csc4dSegmentCombinations) const {
117 
118  const Muon::CscPrepDataContainer* cscPrds{nullptr};
119  ATH_CHECK(SG::get(cscPrds, m_cscPrdsKey, ctx));
120  if (!cscPrds) return StatusCode::SUCCESS;
121 
122  std::vector<const Muon::CscPrepDataCollection*> cscCols;
123  std::copy_if(cscPrds->begin(),cscPrds->end(), std::back_inserter(cscCols), [](const Muon::CscPrepDataCollection* coll) {return !coll->empty();});
124  ATH_MSG_DEBUG("Retrieved CscPrepDataContainer " << cscCols.size());
125  if (cscCols.empty()) return StatusCode::SUCCESS;
126  std::unique_ptr<MuonSegmentCombinationCollection> csc2dSegmentCombinations = m_csc2dSegmentFinder->find(cscCols, ctx);
127  if (!csc2dSegmentCombinations) return StatusCode::SUCCESS;
128 
129  csc4dSegmentCombinations = m_csc4dSegmentFinder->find(*csc2dSegmentCombinations, ctx);
130  return StatusCode::SUCCESS;
131 }
132 void MuonSegmentFinderAlg::appendSegmentsFromCombi(const std::unique_ptr<MuonSegmentCombinationCollection>& combi_coll,
133  Trk::SegmentCollection* segmentContainer) const {
134  if (!combi_coll) return;
136  for (const Muon::MuonSegmentCombination* combi: *combi_coll) {
137  if (!combi) {
138  ATH_MSG_WARNING(" empty MuonSegmentCombination!!! ");
139  continue;
140  }
141  const unsigned int nstations = combi->numberOfStations();
142  const bool useEta = combi->useStripsInSegment(1);
143  const bool usePhi = combi->useStripsInSegment(0);
144 
145  // loop over chambers in combi and extract segments
146  for (unsigned int i = 0; i < nstations; ++i) {
147  // loop over segments in station
148  Muon::MuonSegmentCombination::SegmentVec* segments = combi->stationSegments(i);
149  // check if not empty
150  if (!segments || segments->empty()) continue;
151  // loop over new segments, copy them into collection
152  for (std::unique_ptr<Muon::MuonSegment>& seg : *segments) {
153  if(m_segQuality >=0 && !m_segmentSelector->select(*seg, false, m_segQuality, useEta, usePhi)) continue;
154  ATH_MSG_VERBOSE("Append segment "<<std::endl<<m_printer->print(seg->containedMeasurements()));
155  segmentContainer->push_back(std::move(seg));
156  }
157 
158  }
159  }
160 }
161 void MuonSegmentFinderAlg::createNSWSegments(const EventContext& ctx,
162  const Muon::MuonPatternCombination* patt,
163  NSWSegmentCache& cache) const {
164  // turn the PRD into MuonCluster
165  if (!m_doSTgcSegments && !m_doMMSegments) return;
166  std::map<int, std::vector<std::unique_ptr<const Muon::MuonClusterOnTrack>> > clustersPerSector;
167 
168 
169  for (const Muon::MuonPatternChamberIntersect& it :patt->chamberData()) {
170  if (it.prepRawDataVec().empty()) continue;
171 
172  const Identifier id = it.prepRawDataVec().front()->identify();
173  const int sector = m_idHelperSvc->sector(id);
175  if (!m_idHelperSvc->isMM(id) && !m_idHelperSvc->issTgc(id)) continue;
176  for (const Trk::PrepRawData* pit : it.prepRawDataVec()) {
177  const Muon::MuonCluster* cl = dynamic_cast<const Muon::MuonCluster*>(pit);
178  if (!cl) continue;
179  else if (!m_doMMSegments && m_idHelperSvc->isMM(cl->identify())) continue;
180  else if (!m_doSTgcSegments && m_idHelperSvc->issTgc(cl->identify())) continue;
181  else if (m_removeUsedNswHits && cache.usedHits.count(cl->identify())) continue;
182  const Muon::MuonClusterOnTrack* newCluster = m_clusterCreator->createRIO_OnTrack(*cl, cl->globalPosition());
183  if (!newCluster) continue;
184  std::vector<std::unique_ptr<const Muon::MuonClusterOnTrack>>& clusters = clustersPerSector[sector];
185  clusters.emplace_back(newCluster);
186  }
187  }
188  for (auto&[sector, clusters] :clustersPerSector) {
189  ATH_MSG_VERBOSE("Run segment making on sector "<<sector);
190  cache.inputClust = std::move(clusters);
191  m_clusterSegMakerNSW->find(ctx, cache);
192  }
193 }
194 
196  const Muon::MuonPatternCombination* patcomb,
197  Trk::SegmentCollection* segs) const {
198 
199  if (!m_runMdtSegments) {
200  ATH_MSG_DEBUG("Do not search segments in the Mdt part of the muon spectrometer");
201  return StatusCode::SUCCESS;
202  }
203 
204  bool hasPhiMeasurements = m_patternCalibration->checkForPhiMeasurements(*patcomb);
206  ATH_CHECK(m_patternCalibration->calibrate(ctx, *patcomb, hitsPerRegion));
208  using ROTRegion = Muon::IMuonPatternCalibration::ROTRegion;
209  for (const ROTRegion& region : hitsPerRegion) {
210  for (const MdtVec& mdts : region.mdts()) {
211  if (mdts.empty()) continue;
212  ATH_MSG_VERBOSE("Calling segment finding for sector " << m_idHelperSvc->chamberNameString(mdts.front()->identify()));
213  // fit the segments
214  if (m_doFullFinder) {
215  if (msgLvl(MSG::VERBOSE)){
216  std::vector<const Trk::MeasurementBase*> meas {};
217  meas.insert(meas.end(), mdts.begin(), mdts.end());
218  meas.insert(meas.end(), region.clusters().begin(), region.clusters().end());
219  ATH_MSG_VERBOSE("Call segment maker with road pos: "<<Amg::toString(region.regionPos)
220  << " road dir: "<<Amg::toString(region.regionDir)
221  <<" nMdts: "<<mdts.size()<<" nClusters "<<region.clusters().size()
222  <<std::endl<<m_printer->print(meas));
223 
224  }
225  m_segmentMaker->find(region.regionPos, region.regionDir, mdts, region.clusters(), hasPhiMeasurements, segs,
226  region.regionDir.mag());
227  } else {
228  std::vector<const Trk::RIO_OnTrack*> rios;
229  rios.insert(rios.begin(), mdts.begin(), mdts.end());
230  m_segmentMaker->find(rios, segs);
231  }
232  } // end loop on hits per region
233  }
234  return StatusCode::SUCCESS;
235 }
Muon::MuonPatternCombination::chamberData
const std::vector< MuonPatternChamberIntersect > & chamberData() const
access to the MuonPatternChamberIntersect associated with the MuonPatternCombination
Definition: MuonPatternCombination.h:60
MuonSegmentFinderAlg::m_segQuality
Gaudi::Property< int > m_segQuality
Apply a preselection on the segments.
Definition: MuonSegmentFinderAlg.h:137
MuonCluster.h
MuonSegmentFinderAlg::appendSegmentsFromCombi
void appendSegmentsFromCombi(const std::unique_ptr< MuonSegmentCombinationCollection > &combi_coll, Trk::SegmentCollection *segments) const
Definition: MuonSegmentFinderAlg.cxx:132
Muon::IMuonPatternCalibration::MdtVec
std::vector< const MdtDriftCircleOnTrack * > MdtVec
Definition: IMuonPatternCalibration.h:27
Muon::MuonPrepDataContainer
Template for Muon PRD containers (which are basically collections of MuonPrepDataCollections).
Definition: MuonPrepDataContainer.h:42
Muon::MuonSegmentCombination
Definition: MuonSegmentCombination.h:30
MuonSegmentFinderAlg::m_doMMSegments
Gaudi::Property< bool > m_doMMSegments
Definition: MuonSegmentFinderAlg.h:132
MuonPatternChamberIntersect.h
MuonSegmentFinderAlg.h
MuonSegmentFinderAlg::initialize
virtual StatusCode initialize() override
Definition: MuonSegmentFinderAlg.cxx:23
TrackParameters.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
MuonSegmentFinderAlg::m_clusterCreator
ToolHandle< Muon::IMuonClusterOnTrackCreator > m_clusterCreator
Definition: MuonSegmentFinderAlg.h:58
MuonSegmentFinderAlg::m_csc4dSegmentFinder
ToolHandle< ICscSegmentFinder > m_csc4dSegmentFinder
Definition: MuonSegmentFinderAlg.h:73
MuonSegmentFinderAlg::m_patternCalibration
ToolHandle< Muon::IMuonPatternCalibration > m_patternCalibration
Definition: MuonSegmentFinderAlg.h:43
MuonSegmentFinderAlg::m_segmentSelector
ToolHandle< Muon::IMuonSegmentSelectionTool > m_segmentSelector
Definition: MuonSegmentFinderAlg.h:79
skel.it
it
Definition: skel.GENtoEVGEN.py:407
MuonSegmentFinderAlg::m_segmentCollectionKey
SG::WriteHandleKey< Trk::SegmentCollection > m_segmentCollectionKey
Definition: MuonSegmentFinderAlg.h:83
MuonSegmentFinderAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MuonSegmentFinderAlg.h:32
AthCommonMsg< Gaudi::Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
MuonSegmentFinderAlg::m_cscPrdsKey
SG::ReadHandleKey< Muon::CscPrepDataContainer > m_cscPrdsKey
Definition: MuonSegmentFinderAlg.h:95
MuonSegmentFinderAlg::m_doFullFinder
Gaudi::Property< bool > m_doFullFinder
Run segment finding with eta / phi determination.
Definition: MuonSegmentFinderAlg.h:127
MuonPrepDataContainer.h
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
MuonSegmentFinderAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: MuonSegmentFinderAlg.cxx:47
MuonSegmentFinderAlg::m_removeUsedNswHits
Gaudi::Property< bool > m_removeUsedNswHits
If switched to true, hits that have been already successfully combined to a segment are removed from ...
Definition: MuonSegmentFinderAlg.h:135
MuonSegmentFinderAlg::m_printer
PublicToolHandle< Muon::MuonEDMPrinterTool > m_printer
Definition: MuonSegmentFinderAlg.h:38
MuonSegmentCombination.h
Muon::IMuonPatternCalibration::ROTRegion
Definition: IMuonPatternCalibration.h:30
MuonSegmentFinderAlg::m_doSTgcSegments
Gaudi::Property< bool > m_doSTgcSegments
Run the NSW segment maker.
Definition: MuonSegmentFinderAlg.h:131
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
MuonSegmentFinderAlg::createSegmentsWithMDTs
StatusCode createSegmentsWithMDTs(const EventContext &ctx, const Muon::MuonPatternCombination *patt, Trk::SegmentCollection *segs) const
Definition: MuonSegmentFinderAlg.cxx:195
EventPrimitivesToStringConverter.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
MuonPrepDataCollection.h
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:287
Muon::MuonPatternChamberIntersect
This class holds information needed for the Moore and MoMu pattern recognition for a muon chamber.
Definition: MuonPatternChamberIntersect.h:38
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
MuonSegmentCombinationCollection.h
Trk::Segment
Definition: Tracking/TrkEvent/TrkSegment/TrkSegment/Segment.h:56
MuonPatternCombination.h
MuonPatternCombinationCollection.h
MuonSegmentFinderAlg::m_printSummary
Gaudi::Property< bool > m_printSummary
Definition: MuonSegmentFinderAlg.h:124
AnalysisUtils::copy_if
Out copy_if(In first, const In &last, Out res, const Pred &p)
Definition: IFilterUtils.h:30
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DataVector::insert
iterator insert(iterator position, value_type pElem)
Add a new element to the collection.
MuonReadoutElement.h
Muon::MuonPrepDataCollection
Template to hold collections of MuonPrepRawData objects.
Definition: MuonPrepDataCollection.h:46
DataVector< Muon::MuonPatternCombination >
Muon::IMuonNSWSegmentFinderTool::SegmentMakingCache::buildQuads
bool buildQuads
Toggle whether quad segments should be built or not.
Definition: IMuonNSWSegmentFinderTool.h:35
Muon::IMuonPatternCalibration::ROTsPerRegion
std::vector< ROTRegion > ROTsPerRegion
Definition: IMuonPatternCalibration.h:55
Trk::PrepRawData
Definition: PrepRawData.h:62
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
MuonSegmentFinderAlg::m_segmentMaker
ToolHandle< Muon::IMuonSegmentMaker > m_segmentMaker
Definition: MuonSegmentFinderAlg.h:48
Muon::IMuonNSWSegmentFinderTool::SegmentMakingCache::inputClust
std::vector< std::unique_ptr< const Muon::MuonClusterOnTrack > > inputClust
Input vector containing all muon cluster on track.
Definition: IMuonNSWSegmentFinderTool.h:39
MuonSegPatAssMap.h
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
MuonSegmentFinderAlg::createNSWSegments
void createNSWSegments(const EventContext &ctx, const Muon::MuonPatternCombination *patt, NSWSegmentCache &cache) const
Definition: MuonSegmentFinderAlg.cxx:161
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
MuonSegmentFinderAlg::m_csc2dSegmentFinder
ToolHandle< ICscSegmentFinder > m_csc2dSegmentFinder
Definition: MuonSegmentFinderAlg.h:68
MuonSegmentFinderAlg::m_segmentOverlapRemovalTool
ToolHandle< Muon::IMuonSegmentOverlapRemovalTool > m_segmentOverlapRemovalTool
Definition: MuonSegmentFinderAlg.h:53
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Muon::MuonSegmentCombination::SegmentVec
std::vector< std::unique_ptr< MuonSegment > > SegmentVec
Definition: MuonSegmentCombination.h:32
MuonSegmentFinderAlg::m_segmentNSWCollectionKey
SG::WriteHandleKey< Trk::SegmentCollection > m_segmentNSWCollectionKey
Definition: MuonSegmentFinderAlg.h:89
Muon::IMuonNSWSegmentFinderTool::SegmentMakingCache
Helper struct to parse the data around.
Definition: IMuonNSWSegmentFinderTool.h:31
RunTileMonitoring.clusters
clusters
Definition: RunTileMonitoring.py:133
MuonSegment.h
Muon::MuonCluster
Class representing clusters in the muon system.
Definition: MuonSpectrometer/MuonReconstruction/MuonRecEvent/MuonPrepRawData/MuonPrepRawData/MuonCluster.h:37
MuonClusterOnTrack.h
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:13
Muon::MuonSegment
Definition: MuonSpectrometer/MuonReconstruction/MuonRecEvent/MuonSegment/MuonSegment/MuonSegment.h:45
Muon::MuonPatternCombination
The MuonPatternCombination class provides the means to store the output of the initial global pattern...
Definition: MuonPatternCombination.h:29
Muon::IMuonNSWSegmentFinderTool::SegmentMakingCache::usedHits
std::set< Identifier > usedHits
Set of all hits used in the segment making.
Definition: IMuonNSWSegmentFinderTool.h:41
MuonSegmentFinderAlg::m_patternCollKey
SG::ReadHandleKey< MuonPatternCombinationCollection > m_patternCollKey
Definition: MuonSegmentFinderAlg.h:101
MuonSegmentFinderAlg::createCscSegments
StatusCode createCscSegments(const EventContext &ctx, std::unique_ptr< MuonSegmentCombinationCollection > &csc4dSegmentCombinations) const
Retrieve the raw outputs from the Csc segment makers for the curved combination.
Definition: MuonSegmentFinderAlg.cxx:115
dq_make_web_display.cl
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]
Definition: dq_make_web_display.py:25
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
MuonSegmentFinderAlg::m_clusterSegMakerNSW
ToolHandle< Muon::IMuonNSWSegmentFinderTool > m_clusterSegMakerNSW
Definition: MuonSegmentFinderAlg.h:63
Muon::MuonClusterOnTrack
Base class for Muon cluster RIO_OnTracks.
Definition: MuonClusterOnTrack.h:34
MuonSegmentFinderAlg::m_runMdtSegments
Gaudi::Property< bool > m_runMdtSegments
Run the Mdt segment maker (Switched of the NCB systems)
Definition: MuonSegmentFinderAlg.h:129
Identifier
Definition: IdentifierFieldParser.cxx:14