ATLAS Offline Software
Loading...
Searching...
No Matches
AnalysisMuonThinningAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
7namespace {
10 using TrkThinOptional = std::optional<TrkThinning>;
12 using KeepMap = std::map<const xAOD::TrackParticleContainer*, std::vector<bool>>;
13 using KeepPair = std::pair<const xAOD::TrackParticleContainer*, std::vector<bool>>;
14
17
18 constexpr double MeVtoGeV = 1./ Gaudi::Units::GeV;
19} // namespace
20namespace DerivationFramework {
21
22 AnalysisMuonThinningAlg::AnalysisMuonThinningAlg(const std::string& n, ISvcLocator* p):
24
26 const std::string& stream = m_streamName.value();
27 if (stream.empty()) {
28 ATH_MSG_FATAL("Please give a valid stream for thinning");
29 return StatusCode::FAILURE;
30 }
31 ATH_CHECK(m_muonSelTool.retrieve());
32 ATH_CHECK(m_muonKey.initialize(stream));
33 ATH_CHECK(m_IdTrkKey.initialize(stream, !m_IdTrkKey.empty()));
34 ATH_CHECK(m_FwdIdTrkKey.initialize(stream, !m_FwdIdTrkKey.empty()));
35 ATH_CHECK(m_MSTrkKey.initialize(stream, !m_MSTrkKey.empty()));
36 ATH_CHECK(m_METrkKey.initialize(stream, !m_METrkKey.empty()));
37 ATH_CHECK(m_MSOETrkKey.initialize(stream, !m_MSOETrkKey.empty()));
38 ATH_CHECK(m_CmbTrkKey.initialize(stream, !m_CmbTrkKey.empty()));
39 ATH_CHECK(m_SegmentKey.initialize(stream, !m_SegmentKey.empty()));
40
41 ATH_CHECK(m_MuonPassKeys.initialize());
42 ATH_CHECK(m_TrkPassKeys.initialize());
43 if (!m_MuonPassKeys.empty()) {
44 std::stringstream sstr{};
45 for (const auto& key : m_MuonPassKeys) {
46 sstr<<" *** "<<key.fullKey()<<std::endl;
47 }
48 ATH_MSG_INFO("Accept muons with upstream flag "<<std::endl<<sstr.str());
49 }
50 if (!m_TrkPassKeys.empty()) {
51 std::stringstream sstr{};
52 for (const auto& key : m_TrkPassKeys) {
53 sstr<<" *** "<<key.fullKey()<<std::endl;
54 }
55 ATH_MSG_INFO("Accept tracks with upstream flag "<<std::endl<<sstr.str());
56 }
57
58 return StatusCode::SUCCESS;
59 }
60
61 StatusCode AnalysisMuonThinningAlg::execute(const EventContext& ctx) const {
64 if (!MuonContainer.isValid()) {
65 ATH_MSG_FATAL("Failed to establish thinning handle for " << m_muonKey.fullKey());
66 return StatusCode::FAILURE;
67 }
68 std::vector<bool> keep_muo(MuonContainer->size(), false);
69
71 std::vector<MuonPassDecor> mu_passFlags;
72 for (const SG::ReadDecorHandleKey<xAOD::MuonContainer>& pass_key : m_MuonPassKeys) { mu_passFlags.emplace_back(pass_key, ctx); }
73
75 KeepMap thin_decisions;
76 std::vector<std::unique_ptr<TrkThinning>> TrkContainers;
77
79 auto make_handle = [&, this](const TrkThinKey& key) {
80 if (key.empty()) {
81 ATH_MSG_DEBUG("No key was given for thinning");
82 return StatusCode::SUCCESS;
83 }
84 ATH_MSG_DEBUG("Create new thinning handle " << key.fullKey());
85 TrkContainers.emplace_back(std::make_unique<TrkThinning>(key, ctx));
86
87 std::unique_ptr<TrkThinning>& last = TrkContainers.back();
88 if (!last->isValid()) {
89 ATH_MSG_FATAL("Failed to create a track thinning handle " << key.fullKey());
90 return StatusCode::FAILURE;
91 }
92 const xAOD::TrackParticleContainer* cont = (*last).cptr();
93 thin_decisions[cont].resize(cont->size(), false);
94 return StatusCode::SUCCESS;
95 };
96
97 ATH_CHECK(make_handle(m_IdTrkKey));
98 ATH_CHECK(make_handle(m_FwdIdTrkKey));
99 ATH_CHECK(make_handle(m_MSTrkKey));
100 ATH_CHECK(make_handle(m_METrkKey));
101 ATH_CHECK(make_handle(m_MSOETrkKey));
102 ATH_CHECK(make_handle(m_CmbTrkKey));
103
105 std::unique_ptr<SegmentThinning> segmentContainer =
106 !m_SegmentKey.empty() ? std::make_unique<SegmentThinning>(m_SegmentKey, ctx) : nullptr;
107 std::vector<bool> keep_seg(segmentContainer ? segmentContainer->cptr()->size() : 0, false);
108
110 for (const xAOD::Muon* muon : *MuonContainer) {
113 ATH_MSG_DEBUG("Check muon with pt " << muon->pt() * MeVtoGeV << " [GeV], eta: " << muon->eta() << ", phi: " << muon->phi() << " q: " << muon->charge()
114 << " quality: " << muon->quality() << ", author: " << muon->author());
115
116 if (!m_muonSelTool->accept(*muon) &&
117 std::find_if(mu_passFlags.begin(), mu_passFlags.end(),
118 [this, muon](const MuonPassDecor& decor) ->bool {
119 ATH_MSG_VERBOSE("Check decorator "<<decor.decorKey()<<" "
120 <<SG::AuxTypeRegistry::instance().getName(decor.auxid())
121 <<" "<< decor(*muon));
122 return decor(*muon);
123 }) == mu_passFlags.end())
124 continue;
125 keep_muo[muon->index()] = true;
126 ATH_MSG_DEBUG("Muon will be dumped");
127
129 for (auto tp :
130 {xAOD::Muon::InnerDetectorTrackParticle, xAOD::Muon::MuonSpectrometerTrackParticle, xAOD::Muon::CombinedTrackParticle,
131 xAOD::Muon::ExtrapolatedMuonSpectrometerTrackParticle, xAOD::Muon::MSOnlyExtrapolatedMuonSpectrometerTrackParticle}) {
132 const xAOD::TrackParticle* track = muon->trackParticle(tp);
133 if (!track) {
134 ATH_MSG_DEBUG("No track particle given");
135 continue;
136 }
137 KeepMap::iterator itr = std::find_if(thin_decisions.begin(), thin_decisions.end(),
138 [track](const KeepPair& pair) { return pair.first == track->container(); });
139 if (itr == thin_decisions.end()) {
140 ATH_MSG_WARNING("Could not find for track pT " << track->pt() << " eta: " << track->eta() << ", phi: " << track->phi()
141 << " a valid associated container");
142 continue;
143 }
144 itr->second[track->index()] = true;
145 }
147 if (segmentContainer) {
148 for (size_t s = 0; s < muon->nMuonSegments(); ++s) {
149 const xAOD::MuonSegment* seg = muon->muonSegment(s);
150 if (seg) keep_seg[seg->index()] = true;
151 }
152 }
153 }
157 TrackPassDecor handle{key, ctx};
158 const xAOD::TrackParticleContainer* trks = handle.cptr();
159
160 std::vector<bool>& trk_thin = thin_decisions[trks];
163 if (!trks->empty() && trk_thin.empty()) {
164 ATH_MSG_WARNING("The container " << key.fullKey() << " is not part of the current thinning scheme. Please check");
165 continue;
166 }
168 for (const xAOD::TrackParticle* trk : *trks) {
169 if (handle(*trk)) trk_thin[trk->index()] = true;
170 }
171 }
172
174 MuonContainer.keep(keep_muo);
175 for (std::unique_ptr<TrkThinning>& thin : TrkContainers) {
176 std::vector<bool>& dec = thin_decisions[thin->cptr()];
177 thin->keep(dec);
178 }
179 if (segmentContainer) { segmentContainer->keep(keep_seg); }
180
181 return StatusCode::SUCCESS;
182 }
183
184} // namespace DerivationFramework
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle for requesting thinning for a data object.
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
An algorithm that can be simultaneously executed in multiple threads.
size_type size() const noexcept
Returns the number of elements in the collection.
bool empty() const noexcept
Returns true if the collection is empty.
SG::ThinningHandleKey< xAOD::MuonContainer > m_muonKey
virtual StatusCode execute(const EventContext &ctx) const override
ToolHandle< CP::IMuonSelectionTool > m_muonSelTool
AnalysisMuonThinningAlg(const std::string &n, ISvcLocator *p)
SG::ThinningHandleKey< xAOD::MuonSegmentContainer > m_SegmentKey
SG::ReadDecorHandleKeyArray< xAOD::MuonContainer > m_MuonPassKeys
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_FwdIdTrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_MSTrkKey
Gaudi::Property< std::string > m_streamName
Thinning of unneeded muon tracks.
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_METrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_MSOETrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_CmbTrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_IdTrkKey
SG::ReadDecorHandleKeyArray< xAOD::TrackParticleContainer > m_TrkPassKeys
size_t index() const
Return the index of this element within its container.
Property holding a SG store/key/clid/attr name from which a ReadDecorHandle is made.
Handle class for reading a decoration on an object.
HandleKey object for adding thinning to an object.
Handle for requesting thinning for a data object.
STL class.
constexpr float MeVtoGeV
THE reconstruction tool.
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
Muon_v1 Muon
Reference the current persistent version:
MuonSegment_v1 MuonSegment
Reference the current persistent version: