ATLAS Offline Software
TauAODLeptonRemovalTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
9 }
10 
13  ATH_CHECK(m_muonInputContainer.initialize());
16  return StatusCode::SUCCESS;
17 }
18 
20  // Read in elec and muon container
23  if (bool fail_elec = !elec_input_handle.isValid(), fail_muon = !muon_input_handle.isValid(); fail_elec || fail_muon) {
24  ATH_MSG_ERROR( (fail_elec ? "Could not retrieve Electron container with key " + elec_input_handle.key() : "") +
25  (fail_muon ? "\tCould not retrieve Muon container with key " + muon_input_handle.key() : "")
26  );
27  return StatusCode::FAILURE;
28  }
29  auto elec_container = elec_input_handle.cptr();
30  auto muon_container = muon_input_handle.cptr();
31  //Add the Aux element as empty vector
32  const SG::AuxElement::Accessor<std::vector<ElementLink<xAOD::MuonContainer>>> acc_removed_muons("removedMuons");
33  const SG::AuxElement::Accessor<std::vector<ElementLink<xAOD::ElectronContainer>>> acc_removed_elecs("removedElecs");
34  acc_removed_muons(tau).clear();
35  acc_removed_elecs(tau).clear();
36  //get the muon and electron tracks and clusters
37  auto elec_and_tracks = decltype((getElecAndTrk)(tau, *elec_container))();
38  auto elec_and_clusters = decltype((getElecAndCls)(tau, *elec_container))();
39  auto muon_and_tracks = decltype((getMuonAndTrk)(tau, *muon_container))();
40  auto muon_and_clusters = decltype((getMuonAndCls)(tau, *muon_container))();
41  if(m_doElecTrkRm) elec_and_tracks = getElecAndTrk(tau, *elec_container);
42  if(m_doElecClsRm) elec_and_clusters = getElecAndCls(tau, *elec_container);
43  if(m_doMuonTrkRm) muon_and_tracks = getMuonAndTrk(tau, *muon_container);
44  if(m_doMuonClsRm) muon_and_clusters = getMuonAndCls(tau, *muon_container);
45  // if nothing found just give up here
46  if(elec_and_tracks.empty() && elec_and_clusters.empty() && muon_and_tracks.empty() && muon_and_clusters.empty()) return StatusCode::SUCCESS;
47  // remove the links from the tau
48  auto tau_track_links = tau.allTauTrackLinksNonConst();
49  auto tau_cluster_links = tau.clusterLinks();
50  auto trk_removed_muons = removeTrks(tau_track_links, muon_and_tracks);
51  auto trk_removed_elecs = removeTrks(tau_track_links, elec_and_tracks);
52  auto cls_removed_muons = removeClss(tau_cluster_links, muon_and_clusters);
53  auto cls_removed_elecs = removeClss(tau_cluster_links, elec_and_clusters);
54  tau.clearTauTrackLinks();
55  tau.clearClusterLinks();
56  tau.setClusterLinks(tau_cluster_links);
57  tau.setAllTauTrackLinks(tau_track_links);
58  //Merge the resulting vector and add them to sets
59  auto removed_muons = std::move(trk_removed_muons);
60  auto removed_elecs = std::move(trk_removed_elecs);
61  removed_muons.insert(removed_muons.end(), cls_removed_muons.begin(), cls_removed_muons.end());
62  removed_elecs.insert(removed_elecs.end(), cls_removed_elecs.begin(), cls_removed_elecs.end());
63  auto removed_muons_set = std::set(removed_muons.begin(), removed_muons.end());
64  auto removed_elecs_set = std::set(removed_elecs.begin(), removed_elecs.end());
65  //set link to the removed lepton
66  for (auto muon : removed_muons_set ){
68  link.toContainedElement(*muon_container, muon);
69  acc_removed_muons(tau).push_back(link);
70  }
71  for (auto elec : removed_elecs_set){
73  link.toContainedElement(*elec_container, elec);
74  acc_removed_elecs(tau).push_back(link);
75  }
76  //notify the runner alg that the tau was modified
77  if (!acc_removed_elecs(tau).empty() || !acc_removed_muons(tau).empty())
78  {
79  const SG::AuxElement::Accessor<char> acc_modified("ModifiedInAOD");
80  acc_modified(tau) = static_cast<char>(true);
81  }
82  return StatusCode::SUCCESS;
83 }
84 
85 //helpers
86 std::vector<const xAOD::CaloCluster*> TauAODLeptonRemovalTool::getOrignalTopoClusters(const xAOD::CaloCluster *cluster) const {
87  static const SG::AuxElement::Accessor<std::vector<ElementLink<xAOD::CaloClusterContainer>>> acc_origClusterLinks("constituentClusterLinks");
88  std::vector< const xAOD::CaloCluster* > orig_cls;
89  if(acc_origClusterLinks.isAvailable(*cluster)) {
90  auto links = acc_origClusterLinks(*cluster);
91  for (const auto &link : links) {
92  if (link.dataID() != "CaloCalTopoClusters")
93  ATH_MSG_WARNING("the clusters in the lepton cannot be converted to CaloCalTopoClusters, the ID is " << link.dataID());
94  if (link.isValid())
95  orig_cls.push_back(*link);
96  }
97  }
98  return orig_cls;
99 }
100 
102  static const SG::AuxElement::Accessor<ElementLink<xAOD::TrackParticleContainer>> acc_origTracks ("originalTrackParticle");
103  const xAOD::TrackParticle* orig_trk = nullptr;
104  if(acc_origTracks.isAvailable(*trk)) {
105  if (const auto & orig_link = acc_origTracks(*trk); orig_link.isValid()) {
106  if (orig_link.dataID() != "InDetTrackParticles")
107  ATH_MSG_WARNING("the tracks in the lepton cannot be converted to InDetTrackParticles, the ID is " << orig_link.dataID());
108  orig_trk = *orig_link;
109  }
110  }
111  return orig_trk;
112 }
113 
114 std::vector<std::pair<const xAOD::TrackParticle*, const xAOD::Electron*>> TauAODLeptonRemovalTool::getElecAndTrk(const xAOD::TauJet& tau, const xAOD::ElectronContainer& elec_container) const {
115  std::vector<std::pair<const xAOD::TrackParticle*, const xAOD::Electron*>> ret;
116  std::for_each(elec_container.cbegin(), elec_container.cend(),
117  [&](auto elec) -> void {
118  if(tau.p4().DeltaR(elec->p4()) < m_lepRemovalConeSize && elec->passSelection(m_elecWpStr)) {
119  auto elec_ID_tracks_links = elec->trackParticleLinks();
120  for (const auto &elec_ID_tracks_link : elec_ID_tracks_links) {
121  if (elec_ID_tracks_link.isValid()) {
122  if(auto orig_ele_trk = getOrignalTrackParticle(*elec_ID_tracks_link); orig_ele_trk)
123  ret.push_back(std::make_pair(orig_ele_trk, elec));
124  }
125  }
126  }
127  }
128  );
129  return ret;
130 }
131 
132 std::vector<std::pair<const xAOD::CaloCluster*, const xAOD::Electron*>> TauAODLeptonRemovalTool::getElecAndCls(const xAOD::TauJet& tau, const xAOD::ElectronContainer& elec_container) const {
133  std::vector<std::pair<const xAOD::CaloCluster*, const xAOD::Electron*>> ret;
134  std::for_each(elec_container.cbegin(), elec_container.cend(),
135  [&](auto elec) -> void {
136  if(tau.p4().DeltaR(elec->p4()) < m_lepRemovalConeSize && elec->passSelection(m_elecWpStr)) {
137  auto elec_cluster_links = elec->caloClusterLinks();
138  for (const auto & elec_cluster_link : elec_cluster_links) {
139  if (elec_cluster_link.isValid()) {
140  auto orig_elec_clusters = getOrignalTopoClusters(*elec_cluster_link);
141  for (auto cluster : orig_elec_clusters){
142  ret.push_back(std::make_pair(cluster, elec));
143  }
144  }
145  }
146  }
147  }
148  );
149  return ret;
150 }
151 
152 std::vector<std::pair<const xAOD::TrackParticle*, const xAOD::Muon*>> TauAODLeptonRemovalTool::getMuonAndTrk(const xAOD::TauJet& tau, const xAOD::MuonContainer& muon_container) const {
153  std::vector<std::pair<const xAOD::TrackParticle*, const xAOD::Muon*>> ret;
154  std::for_each(muon_container.cbegin(), muon_container.cend(),
155  [&](auto muon) -> void {
156  if(tau.p4().DeltaR(muon->p4()) < m_lepRemovalConeSize && muon->quality() <= m_muonWpUi) {
157  if(const auto & muon_ID_tracks_link = muon->inDetTrackParticleLink(); muon_ID_tracks_link.isValid())
158  ret.push_back(std::make_pair(std::move(*muon_ID_tracks_link), muon));
159  }
160  }
161  );
162  return ret;
163 }
164 
165 std::vector<std::pair<const xAOD::CaloCluster*, const xAOD::Muon*>> TauAODLeptonRemovalTool::getMuonAndCls(const xAOD::TauJet& tau, const xAOD::MuonContainer& muon_container) const {
166  std::vector<std::pair<const xAOD::CaloCluster*, const xAOD::Muon*>> ret;
167  std::for_each(muon_container.cbegin(), muon_container.cend(),
168  [&](auto muon) -> void {
169  if(tau.p4().DeltaR(muon->p4()) < m_lepRemovalConeSize && muon->quality() <= m_muonWpUi) {
170  if(const auto & muon_cluster_link = muon->clusterLink(); muon_cluster_link.isValid()) {
171  auto muon_cluster = std::move(*muon_cluster_link);
172  auto muon_e = muon->e();
173  auto loss_e = muon->floatParameter(xAOD::Muon::ParamEnergyLoss);
174  auto cls_e = muon_cluster->e();
175  auto loss_diff = ((cls_e - loss_e) / (cls_e + loss_e));
176  if (muon_e > cls_e && loss_diff < 0.1 && loss_diff > -0.3) {
177  auto orig_muon_clusters = getOrignalTopoClusters(muon_cluster);
178  for (auto cluster : orig_muon_clusters)
179  ret.push_back(std::make_pair(cluster, muon));
180  }
181  }
182  }
183  }
184  );
185  return ret;
186 }
187 
188 template<typename Tlep, typename Tlinks> std::vector<Tlep> TauAODLeptonRemovalTool::removeTrks(Tlinks& tau_trk_links, std::vector<std::pair<const xAOD::TrackParticle*, Tlep>>& tracks_and_leps) const {
189  std::vector<Tlep> ret;
190  tau_trk_links.erase(
191  std::remove_if(tau_trk_links.begin(), tau_trk_links.end(),
192  [&](auto tau_trk_link) -> bool {
193  bool match = false;
194  if(tau_trk_link.isValid()) {
195  auto tau_trk = (*tau_trk_link)->track();
196  auto where = std::find_if(tracks_and_leps.cbegin(), tracks_and_leps.cend(),
197  [&](auto track_and_lep){ return tau_trk == track_and_lep.first; });
198  if(where != tracks_and_leps.cend()) {
199  ATH_MSG_DEBUG("track with pt " << tau_trk->pt()/1000 << " GeV removed");
200  ret.push_back(where->second);
201  match = true;
202  }
203  }
204  return match;
205  }
206  ),
207  tau_trk_links.end()
208  );
209  return ret;
210 }
211 
212 template<typename Tlep, typename Tlinks> std::vector<Tlep> TauAODLeptonRemovalTool::removeClss(Tlinks& tau_cls_links, std::vector<std::pair<const xAOD::CaloCluster*, Tlep>>& clusters_and_leps) const {
213  std::vector<Tlep> ret;
214  tau_cls_links.erase(
215  std::remove_if(tau_cls_links.begin(), tau_cls_links.end(),
216  [&](auto tau_cls_link) -> bool {
217  bool match = false;
218  if(tau_cls_link.isValid()) {
219  auto tau_cls = static_cast<const xAOD::CaloCluster*>(*tau_cls_link);
220  auto where = std::find_if(clusters_and_leps.cbegin(), clusters_and_leps.cend(),
221  [&](auto cluster_and_lep){ return tau_cls == cluster_and_lep.first; });
222  if(where != clusters_and_leps.cend()) {
223  ATH_MSG_DEBUG("cluster with pt " << tau_cls->pt()/1000 << " GeV removed");
224  ret.push_back(where->second);
225  match = true;
226  }
227  }
228  return match;
229  }
230  ),
231  tau_cls_links.end()
232  );
233  return ret;
234 }
TauAODLeptonRemovalTool::getOrignalTopoClusters
std::vector< const xAOD::CaloCluster * > getOrignalTopoClusters(const xAOD::CaloCluster *cluster) const
Definition: TauAODLeptonRemovalTool.cxx:86
TauAODLeptonRemovalTool::m_muonWpUi
uint m_muonWpUi
Definition: TauAODLeptonRemovalTool.h:30
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
TauAODLeptonRemovalTool::execute
virtual StatusCode execute(xAOD::TauJet &) const override
Execute - called for each tau candidate.
Definition: TauAODLeptonRemovalTool.cxx:19
xAOD::TauJet_v3::setAllTauTrackLinks
void setAllTauTrackLinks(const TauTrackLinks_t &tauTracks)
Definition: TauJet_v3.cxx:424
xAOD::TauJet_v3::clusterLinks
const IParticleLinks_t & clusterLinks() const
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
xAOD::TauJet_v3::clearTauTrackLinks
void clearTauTrackLinks()
Remove all tracks from the tau.
Definition: TauJet_v3.cxx:567
TauRecToolBase
The base class for all tau tools.
Definition: TauRecToolBase.h:21
xAOD::TauJet_v3::clearClusterLinks
void clearClusterLinks()
Remove all clusters from the tau.
Definition: TauJet_v3.cxx:620
TauAODLeptonRemovalTool::m_muonInputContainer
SG::ReadHandleKey< xAOD::MuonContainer > m_muonInputContainer
Definition: TauAODLeptonRemovalTool.h:31
DataVector::cend
const_iterator cend() const noexcept
Return a const_iterator pointing past the end of the collection.
TauAODLeptonRemovalTool::removeTrks
std::vector< Tlep > removeTrks(Tlinks &tau_trk_links, std::vector< std::pair< const xAOD::TrackParticle *, Tlep >> &removings) const
Definition: TauAODLeptonRemovalTool.cxx:188
TauAODLeptonRemovalTool::initialize
virtual StatusCode initialize() override
Tool initializer.
Definition: TauAODLeptonRemovalTool.cxx:11
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
TauAODLeptonRemovalTool.h
TauAODLeptonRemovalTool::m_elecWpStr
std::string m_elecWpStr
Definition: TauAODLeptonRemovalTool.h:29
TauAODLeptonRemovalTool::m_elecInputContainer
SG::ReadHandleKey< xAOD::ElectronContainer > m_elecInputContainer
Definition: TauAODLeptonRemovalTool.h:32
xAOD::TauJet_v3::setClusterLinks
void setClusterLinks(const IParticleLinks_t &clusters)
TauAODLeptonRemovalTool::getOrignalTrackParticle
const xAOD::TrackParticle * getOrignalTrackParticle(const xAOD::TrackParticle *trk) const
Definition: TauAODLeptonRemovalTool.cxx:101
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:59
TauAODLeptonRemovalTool::m_doMuonClsRm
Gaudi::Property< bool > m_doMuonClsRm
Definition: TauAODLeptonRemovalTool.h:36
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TauAODLeptonRemovalTool::getMuonAndTrk
std::vector< std::pair< const xAOD::TrackParticle *, const xAOD::Muon * > > getMuonAndTrk(const xAOD::TauJet &tau, const xAOD::MuonContainer &muon_cont) const
Definition: TauAODLeptonRemovalTool.cxx:152
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
DMTest::links
links
Definition: CLinks_v1.cxx:22
xAOD::TauJet_v3
Class describing a tau jet.
Definition: TauJet_v3.h:41
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAOD::TauJet_v3::allTauTrackLinksNonConst
TauTrackLinks_t & allTauTrackLinksNonConst()
In order to sort track links.
Definition: TauJet_v3.cxx:446
TauAODLeptonRemovalTool::getMuonAndCls
std::vector< std::pair< const xAOD::CaloCluster *, const xAOD::Muon * > > getMuonAndCls(const xAOD::TauJet &tau, const xAOD::MuonContainer &muon_cont) const
Definition: TauAODLeptonRemovalTool.cxx:165
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
TauAODLeptonRemovalTool::m_strElecIdWpPrefix
Gaudi::Property< std::string > m_strElecIdWpPrefix
Definition: TauAODLeptonRemovalTool.h:40
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
SG::VarHandleBase::key
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:64
TauAODLeptonRemovalTool::m_doElecTrkRm
Gaudi::Property< bool > m_doElecTrkRm
Definition: TauAODLeptonRemovalTool.h:35
TauAODLeptonRemovalTool::getElecAndTrk
std::vector< std::pair< const xAOD::TrackParticle *, const xAOD::Electron * > > getElecAndTrk(const xAOD::TauJet &tau, const xAOD::ElectronContainer &elec_cont) const
Definition: TauAODLeptonRemovalTool.cxx:114
TauAODLeptonRemovalTool::removeClss
std::vector< Tlep > removeClss(Tlinks &tau_cls_links, std::vector< std::pair< const xAOD::CaloCluster *, Tlep >> &clusters_and_leps) const
Definition: TauAODLeptonRemovalTool.cxx:212
TauAODLeptonRemovalTool::m_mapMuonIdWp
const std::map< std::string, uint > m_mapMuonIdWp
Definition: TauAODLeptonRemovalTool.h:28
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TauAODLeptonRemovalTool::m_doElecClsRm
Gaudi::Property< bool > m_doElecClsRm
Definition: TauAODLeptonRemovalTool.h:37
SG::ConstAccessor< T, AuxAllocator_t< T > >::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
DataVector::cbegin
const_iterator cbegin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
TauAODLeptonRemovalTool::m_strMinElecIdWp
Gaudi::Property< std::string > m_strMinElecIdWp
Definition: TauAODLeptonRemovalTool.h:38
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
TauAODLeptonRemovalTool::getElecAndCls
std::vector< std::pair< const xAOD::CaloCluster *, const xAOD::Electron * > > getElecAndCls(const xAOD::TauJet &tau, const xAOD::ElectronContainer &elec_cont) const
Definition: TauAODLeptonRemovalTool.cxx:132
TauAODLeptonRemovalTool::m_strMinMuonIdWp
Gaudi::Property< std::string > m_strMinMuonIdWp
Definition: TauAODLeptonRemovalTool.h:39
TauAODLeptonRemovalTool::TauAODLeptonRemovalTool
TauAODLeptonRemovalTool(const std::string &type)
Definition: TauAODLeptonRemovalTool.cxx:7
TauAODLeptonRemovalTool::m_doMuonTrkRm
Gaudi::Property< bool > m_doMuonTrkRm
Definition: TauAODLeptonRemovalTool.h:34
match
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition: hcg.cxx:356