ATLAS Offline Software
Loading...
Searching...
No Matches
TauAODMuonRemovalTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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 // Read in muon container
22 if (bool fail_muon = !muon_input_handle.isValid(); fail_muon) {
23 ATH_MSG_ERROR( "Could not retrieve Muon container with key " + muon_input_handle.key() );
24 return StatusCode::FAILURE;
25 }
26 auto muon_container = muon_input_handle.cptr();
27 //Add the Aux element as empty vector
28 const SG::Accessor<std::vector<ElementLink<xAOD::MuonContainer>>> acc_removed_muons("removedMuons");
29 acc_removed_muons(tau).clear();
30 //get the muon tracks and clusters
31 auto muon_and_tracks = decltype((getMuonAndTrk)(tau, *muon_container))();
32 auto muon_and_clusters = decltype((getMuonAndCls)(tau, *muon_container))();
33 if(m_doMuonTrkRm) muon_and_tracks = getMuonAndTrk(tau, *muon_container);
34 if(m_doMuonClsRm) muon_and_clusters = getMuonAndCls(tau, *muon_container);
35 // if nothing found just give up here
36 if(muon_and_tracks.empty() && muon_and_clusters.empty()) return StatusCode::SUCCESS;
37 // remove the links from the tau
38 auto tau_track_links = tau.allTauTrackLinksNonConst();
39 auto tau_cluster_links = tau.clusterLinks();
40 auto trk_removed_muons = removeTrks(tau_track_links, muon_and_tracks);
41 auto cls_removed_muons = removeClss(tau_cluster_links, muon_and_clusters);
44 tau.setClusterLinks(tau_cluster_links);
45 tau.setAllTauTrackLinks(tau_track_links);
46 //Merge the resulting vector and add them to sets
47 auto removed_muons = std::move(trk_removed_muons);
48 removed_muons.insert(removed_muons.end(), cls_removed_muons.begin(), cls_removed_muons.end());
49 auto removed_muons_set = std::set(removed_muons.begin(), removed_muons.end());
50 //set link to the removed lepton
51 for (auto muon : removed_muons_set ){
53 link.toContainedElement(*muon_container, muon);
54 acc_removed_muons(tau).push_back(link);
55 }
56 //notify the runner alg that the tau was modified
57 if (!acc_removed_muons(tau).empty())
58 {
59 const SG::Accessor<char> acc_modified("ModifiedInAOD");
60 acc_modified(tau) = static_cast<char>(true);
61 }
62 return StatusCode::SUCCESS;
63}
64
65//helpers
66std::vector<const xAOD::CaloCluster*> TauAODMuonRemovalTool::getOriginalTopoClusters(const xAOD::CaloCluster *cluster) const {
67 static const SG::Accessor<std::vector<ElementLink<xAOD::CaloClusterContainer>>> acc_origClusterLinks("constituentClusterLinks");
68 std::vector< const xAOD::CaloCluster* > orig_cls;
69 if(acc_origClusterLinks.isAvailable(*cluster)) {
70 auto links = acc_origClusterLinks(*cluster);
71 for (const auto &link : links) {
72 if (link.dataID() != "CaloCalTopoClusters")
73 ATH_MSG_WARNING("the clusters in the lepton cannot be converted to CaloCalTopoClusters, the ID is " << link.dataID());
74 if (link.isValid())
75 orig_cls.push_back(*link);
76 }
77 }
78 return orig_cls;
79}
80
81std::vector<std::pair<const xAOD::TrackParticle*, const xAOD::Muon*>> TauAODMuonRemovalTool::getMuonAndTrk(const xAOD::TauJet& tau, const xAOD::MuonContainer& muon_container) const {
82 std::vector<std::pair<const xAOD::TrackParticle*, const xAOD::Muon*>> ret;
83 std::for_each(muon_container.cbegin(), muon_container.cend(),
84 [&](auto muon) -> void {
85 if(tau.p4().DeltaR(muon->p4()) < m_lepRemovalConeSize && muon->quality() <= m_muonWpUi) {
86 if(const auto & muon_ID_tracks_link = muon->inDetTrackParticleLink(); muon_ID_tracks_link.isValid())
87 ret.push_back(std::make_pair(std::move(*muon_ID_tracks_link), muon));
88 }
89 }
90 );
91 return ret;
92}
93
94std::vector<std::pair<const xAOD::CaloCluster*, const xAOD::Muon*>> TauAODMuonRemovalTool::getMuonAndCls(const xAOD::TauJet& tau, const xAOD::MuonContainer& muon_container) const {
95 std::vector<std::pair<const xAOD::CaloCluster*, const xAOD::Muon*>> ret;
96 std::for_each(muon_container.cbegin(), muon_container.cend(),
97 [&](auto muon) -> void {
98 if(tau.p4().DeltaR(muon->p4()) < m_lepRemovalConeSize && muon->quality() <= m_muonWpUi) {
99 if(const auto & muon_cluster_link = muon->clusterLink(); muon_cluster_link.isValid()) {
100 auto muon_cluster = std::move(*muon_cluster_link);
101 auto muon_e = muon->e();
102 auto loss_e = muon->floatParameter(xAOD::Muon::ParamEnergyLoss);
103 auto cls_e = muon_cluster->e();
104 auto loss_diff = ((cls_e - loss_e) / (cls_e + loss_e));
105 if (muon_e > cls_e && loss_diff < 0.1 && loss_diff > -0.3) {
106 auto orig_muon_clusters = getOriginalTopoClusters(muon_cluster);
107 for (auto cluster : orig_muon_clusters)
108 ret.push_back(std::make_pair(cluster, muon));
109 }
110 }
111 }
112 }
113 );
114 return ret;
115}
116
117template<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 {
118 std::vector<Tlep> ret;
119 tau_trk_links.erase(
120 std::remove_if(tau_trk_links.begin(), tau_trk_links.end(),
121 [&](auto tau_trk_link) -> bool {
122 bool match = false;
123 if(tau_trk_link.isValid()) {
124 auto tau_trk = (*tau_trk_link)->track();
125 auto where = std::find_if(tracks_and_leps.cbegin(), tracks_and_leps.cend(),
126 [&](auto track_and_lep){ return tau_trk == track_and_lep.first; });
127 if(where != tracks_and_leps.cend()) {
128 ATH_MSG_DEBUG("track with pt " << tau_trk->pt()/GeV << " GeV removed");
129 ret.push_back(where->second);
130 match = true;
131 }
132 }
133 return match;
134 }
135 ),
136 tau_trk_links.end()
137 );
138 return ret;
139}
140
141template<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 {
142 std::vector<Tlep> ret;
143 tau_cls_links.erase(
144 std::remove_if(tau_cls_links.begin(), tau_cls_links.end(),
145 [&](auto tau_cls_link) -> bool {
146 bool match = false;
147 if(tau_cls_link.isValid()) {
148 auto tau_cls = static_cast<const xAOD::CaloCluster*>(*tau_cls_link);
149 auto where = std::find_if(clusters_and_leps.cbegin(), clusters_and_leps.cend(),
150 [&](auto cluster_and_lep){ return tau_cls == cluster_and_lep.first; });
151 if(where != clusters_and_leps.cend()) {
152 ATH_MSG_DEBUG("cluster with pt " << tau_cls->pt()/GeV << " GeV removed");
153 ret.push_back(where->second);
154 match = true;
155 }
156 }
157 return match;
158 }
159 ),
160 tau_cls_links.end()
161 );
162 return ret;
163}
#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
std::vector< const xAOD::CaloCluster * > getOriginalTopoClusters(const xAOD::CaloCluster *cluster) const
virtual StatusCode execute(xAOD::TauJet &) const override
Execute - called for each tau candidate.
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)
const IParticleLinks_t & clusterLinks() const
void clearTauTrackLinks()
Remove all tracks from the tau.
TauTrackLinks_t & allTauTrackLinksNonConst()
In order to sort track links.
void setAllTauTrackLinks(const TauTrackLinks_t &tauTracks)
void clearClusterLinks()
Remove all clusters from the tau.
void setClusterLinks(const IParticleLinks_t &clusters)
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:357
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".
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".