ATLAS Offline Software
AnalysisMuonThinningAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 namespace {
10  using TrkThinOptional = std::optional<TrkThinning>;
11  using SegmentThinning = SG::ThinningHandle<xAOD::MuonSegmentContainer>;
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
20 namespace 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
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
DerivationFramework::AnalysisMuonThinningAlg::m_MSOETrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_MSOETrkKey
Definition: AnalysisMuonThinningAlg.h:50
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
GeV
#define GeV
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:17
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
DerivationFramework::AnalysisMuonThinningAlg::m_streamName
Gaudi::Property< std::string > m_streamName
Thinning of unneeded muon tracks.
Definition: AnalysisMuonThinningAlg.h:38
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
AnalysisMuonThinningAlg.h
DerivationFramework::AnalysisMuonThinningAlg::initialize
virtual StatusCode initialize() override
Definition: AnalysisMuonThinningAlg.cxx:25
DerivationFramework::AnalysisMuonThinningAlg::m_muonKey
SG::ThinningHandleKey< xAOD::MuonContainer > m_muonKey
Definition: AnalysisMuonThinningAlg.h:40
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
DerivationFramework::AnalysisMuonThinningAlg::m_CmbTrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_CmbTrkKey
Definition: AnalysisMuonThinningAlg.h:52
xAOD::MuonContainer
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
Definition: Event/xAOD/xAODMuon/xAODMuon/MuonContainer.h:14
SG::ThinningHandleKey
HandleKey object for adding thinning to an object.
Definition: ThinningHandleKey.h:38
ThinningHandle.h
Handle for requesting thinning for a data object.
CP::MeVtoGeV
constexpr float MeVtoGeV
Definition: IsolationCloseByCorrectionTool.cxx:33
xAOD::MuonSegment_v1
Class describing a MuonSegment.
Definition: MuonSegment_v1.h:33
ParticleTest.tp
tp
Definition: ParticleTest.py:25
DerivationFramework::AnalysisMuonThinningAlg::m_IdTrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_IdTrkKey
Definition: AnalysisMuonThinningAlg.h:41
DerivationFramework::AnalysisMuonThinningAlg::m_TrkPassKeys
SG::ReadDecorHandleKeyArray< xAOD::TrackParticleContainer > m_TrkPassKeys
Definition: AnalysisMuonThinningAlg.h:34
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
DerivationFramework::AnalysisMuonThinningAlg::m_SegmentKey
SG::ThinningHandleKey< xAOD::MuonSegmentContainer > m_SegmentKey
Definition: AnalysisMuonThinningAlg.h:54
DerivationFramework::AnalysisMuonThinningAlg::m_MuonPassKeys
SG::ReadDecorHandleKeyArray< xAOD::MuonContainer > m_MuonPassKeys
Definition: AnalysisMuonThinningAlg.h:31
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
DerivationFramework::AnalysisMuonThinningAlg::m_muonSelTool
ToolHandle< CP::IMuonSelectionTool > m_muonSelTool
Definition: AnalysisMuonThinningAlg.h:27
beamspotman.n
n
Definition: beamspotman.py:731
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
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
DerivationFramework::AnalysisMuonThinningAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: AnalysisMuonThinningAlg.cxx:61
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
DataVector::resize
void resize(size_type sz)
Resizes the collection to the specified number of elements.
DerivationFramework::AnalysisMuonThinningAlg::AnalysisMuonThinningAlg
AnalysisMuonThinningAlg(const std::string &n, ISvcLocator *p)
Definition: AnalysisMuonThinningAlg.cxx:22
DerivationFramework::AnalysisMuonThinningAlg::m_METrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_METrkKey
Definition: AnalysisMuonThinningAlg.h:48
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
DerivationFramework::AnalysisMuonThinningAlg::m_MSTrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_MSTrkKey
Definition: AnalysisMuonThinningAlg.h:46
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
SG::ReadDecorHandleKey
Property holding a SG store/key/clid/attr name from which a ReadDecorHandle is made.
Definition: StoreGate/StoreGate/ReadDecorHandleKey.h:85
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DerivationFramework::AnalysisMuonThinningAlg::m_FwdIdTrkKey
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_FwdIdTrkKey
Definition: AnalysisMuonThinningAlg.h:43
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37