ATLAS Offline Software
Loading...
Searching...
No Matches
TauAODMuonRemovalTool.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
7#define GeV 1000
8
10 TauRecToolBase(name) {
11}
12
14 ATH_CHECK(m_muonInputContainer.initialize());
16 return StatusCode::SUCCESS;
17}
18
20 const EventContext& ctx) const {
21
22 // Read in muon container
24 if (bool fail_muon = !muon_input_handle.isValid(); fail_muon) {
25 ATH_MSG_ERROR( "Could not retrieve Muon container with key " + muon_input_handle.key() );
26 return StatusCode::FAILURE;
27 }
28 auto muon_container = muon_input_handle.cptr();
29 //Add the Aux element as empty vector
30 const SG::Accessor<std::vector<ElementLink<xAOD::MuonContainer>>> acc_removed_muons("removedMuons");
31 acc_removed_muons(tau).clear();
32 //get the muon tracks and clusters
33 auto muon_and_tracks = decltype((getMuonAndTrk)(tau, *muon_container))();
34 auto muon_and_clusters = decltype((getMuonAndCls)(tau, *muon_container))();
35 if(m_doMuonTrkRm) muon_and_tracks = getMuonAndTrk(tau, *muon_container);
36 if(m_doMuonClsRm) muon_and_clusters = getMuonAndCls(tau, *muon_container);
37 // if nothing found just give up here
38 if(muon_and_tracks.empty() && muon_and_clusters.empty()) return StatusCode::SUCCESS;
39 // remove the links from the tau
40 auto tau_track_links = tau.allTauTrackLinksNonConst();
41 auto tau_cluster_links = tau.clusterLinks();
42 auto trk_removed_muons = removeTrks(tau_track_links, muon_and_tracks);
43 auto cls_removed_muons = removeClss(tau_cluster_links, muon_and_clusters);
44 tau.clearTauTrackLinks();
45 tau.clearClusterLinks();
46 tau.setClusterLinks(tau_cluster_links);
47 tau.setAllTauTrackLinks(tau_track_links);
48 //Merge the resulting vector and add them to sets
49 auto removed_muons = std::move(trk_removed_muons);
50 removed_muons.insert(removed_muons.end(), cls_removed_muons.begin(), cls_removed_muons.end());
51 auto removed_muons_set = std::set(removed_muons.begin(), removed_muons.end());
52 //set link to the removed lepton
53 for (auto muon : removed_muons_set ){
55 link.toContainedElement(*muon_container, muon);
56 acc_removed_muons(tau).push_back(link);
57 }
58 //notify the runner alg that the tau was modified
59 if (!acc_removed_muons(tau).empty())
60 {
61 const SG::Accessor<char> acc_modified("ModifiedInAOD");
62 acc_modified(tau) = static_cast<char>(true);
63 }
64 return StatusCode::SUCCESS;
65}
66
67//helpers
68std::vector<const xAOD::CaloCluster*> TauAODMuonRemovalTool::getOriginalTopoClusters(const xAOD::CaloCluster *cluster) const {
69 static const SG::Accessor<std::vector<ElementLink<xAOD::CaloClusterContainer>>> acc_origClusterLinks("constituentClusterLinks");
70 std::vector< const xAOD::CaloCluster* > orig_cls;
71 if(acc_origClusterLinks.isAvailable(*cluster)) {
72 auto links = acc_origClusterLinks(*cluster);
73 for (const auto &link : links) {
74 if (link.dataID() != "CaloCalTopoClusters")
75 ATH_MSG_WARNING("the clusters in the lepton cannot be converted to CaloCalTopoClusters, the ID is " << link.dataID());
76 if (link.isValid())
77 orig_cls.push_back(*link);
78 }
79 }
80 return orig_cls;
81}
82
83std::vector<std::pair<const xAOD::TrackParticle*, const xAOD::Muon*>> TauAODMuonRemovalTool::getMuonAndTrk(const xAOD::TauJet& tau, const xAOD::MuonContainer& muon_container) const {
84 std::vector<std::pair<const xAOD::TrackParticle*, const xAOD::Muon*>> ret;
85 std::for_each(muon_container.cbegin(), muon_container.cend(),
86 [&](auto muon) -> void {
87 if(tau.p4().DeltaR(muon->p4()) < m_lepRemovalConeSize && static_cast<unsigned>(muon->quality()) <= m_muonWpUi) {
88 if(const xAOD::TrackParticle* idTrk = muon->trackParticle(xAOD::Muon::TrackParticleType::InnerDetectorTrackParticle); idTrk != nullptr)
89 ret.push_back(std::make_pair(idTrk, muon));
90 }
91 }
92 );
93 return ret;
94}
95
96std::vector<std::pair<const xAOD::CaloCluster*, const xAOD::Muon*>> TauAODMuonRemovalTool::getMuonAndCls(const xAOD::TauJet& tau, const xAOD::MuonContainer& muon_container) const {
97 std::vector<std::pair<const xAOD::CaloCluster*, const xAOD::Muon*>> ret;
98 std::for_each(muon_container.cbegin(), muon_container.cend(),
99 [&](const xAOD::Muon* muon) {
100 if(tau.p4().DeltaR(muon->p4()) < m_lepRemovalConeSize &&
101 static_cast<unsigned>(muon->quality()) <= m_muonWpUi) {
102 if( const xAOD::CaloCluster* muon_cluster = muon->cluster(); muon_cluster != nullptr) {
103 auto muon_e = muon->e();
104 auto loss_e = muon->floatParameter(xAOD::Muon::ParamDef::ParamEnergyLoss);
105 auto cls_e = muon_cluster->e();
106 auto loss_diff = ((cls_e - loss_e) / (cls_e + loss_e));
107 if (muon_e > cls_e && loss_diff < 0.1 && loss_diff > -0.3) {
108 auto orig_muon_clusters = getOriginalTopoClusters(muon_cluster);
109 for (auto cluster : orig_muon_clusters)
110 ret.push_back(std::make_pair(cluster, muon));
111 }
112 }
113 }
114 }
115 );
116 return ret;
117}
118
119template<typename Tlep, typename Tlinks> std::vector<Tlep> TauAODMuonRemovalTool::removeTrks(Tlinks& tau_trk_links, std::vector<std::pair<const xAOD::TrackParticle*, Tlep>>& tracks_and_leps) const {
120 std::vector<Tlep> ret;
121 tau_trk_links.erase(
122 std::remove_if(tau_trk_links.begin(), tau_trk_links.end(),
123 [&](auto tau_trk_link) -> bool {
124 bool match = false;
125 if(tau_trk_link.isValid()) {
126 auto tau_trk = (*tau_trk_link)->track();
127 auto where = std::find_if(tracks_and_leps.cbegin(), tracks_and_leps.cend(),
128 [&](auto track_and_lep){ return tau_trk == track_and_lep.first; });
129 if(where != tracks_and_leps.cend()) {
130 ATH_MSG_DEBUG("track with pt " << tau_trk->pt()/GeV << " GeV removed");
131 ret.push_back(where->second);
132 match = true;
133 }
134 }
135 return match;
136 }
137 ),
138 tau_trk_links.end()
139 );
140 return ret;
141}
142
143template<typename Tlep, typename Tlinks> std::vector<Tlep> TauAODMuonRemovalTool::removeClss(Tlinks& tau_cls_links, std::vector<std::pair<const xAOD::CaloCluster*, Tlep>>& clusters_and_leps) const {
144 std::vector<Tlep> ret;
145 tau_cls_links.erase(
146 std::remove_if(tau_cls_links.begin(), tau_cls_links.end(),
147 [&](auto tau_cls_link) -> bool {
148 bool match = false;
149 if(tau_cls_link.isValid()) {
150 auto tau_cls = static_cast<const xAOD::CaloCluster*>(*tau_cls_link);
151 auto where = std::find_if(clusters_and_leps.cbegin(), clusters_and_leps.cend(),
152 [&](auto cluster_and_lep){ return tau_cls == cluster_and_lep.first; });
153 if(where != clusters_and_leps.cend()) {
154 ATH_MSG_DEBUG("cluster with pt " << tau_cls->pt()/GeV << " GeV removed");
155 ret.push_back(where->second);
156 match = true;
157 }
158 }
159 return match;
160 }
161 ),
162 tau_cls_links.end()
163 );
164 return ret;
165}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
static const Attributes_t empty
const_iterator cbegin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
const_iterator cend() const noexcept
Return a const_iterator pointing past the end of the collection.
Helper class to provide type-safe access to aux data.
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
Gaudi::Property< std::string > m_strMinMuonIdWp
virtual StatusCode executeTool(xAOD::TauJet &tau, const EventContext &ctx) const override
Execute - called for each tau candidate.
std::vector< const xAOD::CaloCluster * > getOriginalTopoClusters(const xAOD::CaloCluster *cluster) const
SG::ReadHandleKey< xAOD::MuonContainer > m_muonInputContainer
std::vector< std::pair< const xAOD::TrackParticle *, const xAOD::Muon * > > getMuonAndTrk(const xAOD::TauJet &tau, const xAOD::MuonContainer &muon_cont) const
std::vector< Tlep > removeClss(Tlinks &tau_cls_links, std::vector< std::pair< const xAOD::CaloCluster *, Tlep > > &clusters_and_leps) const
TauAODMuonRemovalTool(const std::string &type)
const std::map< std::string, uint > m_mapMuonIdWp
std::vector< Tlep > removeTrks(Tlinks &tau_trk_links, std::vector< std::pair< const xAOD::TrackParticle *, Tlep > > &removings) const
Gaudi::Property< bool > m_doMuonClsRm
virtual StatusCode initialize() override
Tool initializer.
std::vector< std::pair< const xAOD::CaloCluster *, const xAOD::Muon * > > getMuonAndCls(const xAOD::TauJet &tau, const xAOD::MuonContainer &muon_cont) const
Gaudi::Property< bool > m_doMuonTrkRm
TauRecToolBase(const std::string &name)
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:359
DataModel_detail::iterator< DVL > remove_if(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, Predicate pred)
Specialization of remove_if for DataVector/List.
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
TauJet_v3 TauJet
Definition of the current "tau version".
Definition TauJet.h:17
Muon_v1 Muon
Reference the current persistent version:
Definition Muon.h:13
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".