ATLAS Offline Software
TauIDDecoratorWrapper.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 #include "StoreGate/ReadHandle.h"
7 #include "xAODCore/ShallowCopy.h"
9 
10 namespace DerivationFramework {
11 
12  TauIDDecoratorWrapper::TauIDDecoratorWrapper(const std::string& t, const std::string& n, const IInterface* p) :
13  base_class(t,n,p)
14  {
15  }
16 
18  {
19  // initialize tauRecTools tools
20  ATH_CHECK( m_tauIDTools.retrieve() );
21 
22  // parse the properties of TauWPDecorator tools
23  for (const auto& tool : m_tauIDTools) {
24  if (tool->type() != "TauWPDecorator") continue;
25 
26  // check whether we must compute eVeto WPs, as this requires the recalculation of a variable
27  BooleanProperty useAbsEta("UseAbsEta", false);
28  ATH_CHECK( tool->getProperty(&useAbsEta) );
29  if (useAbsEta.value()) {
30  m_doEvetoWP = true;
31  }
32 
33  // retrieve the names of ID scores and WPs
34  StringProperty scoreName("ScoreName", "");
35  ATH_CHECK( tool->getProperty(&scoreName) );
36  // the original RNNEleScore should not be overriden
37  if (scoreName.value() != "RNNEleScore") {
38  m_scores.push_back(scoreName.value());
39  }
40 
41  StringProperty newScoreName("NewScoreName", "");
42  ATH_CHECK( tool->getProperty(&newScoreName) );
43  m_scores.push_back(newScoreName.value());
44 
45  StringArrayProperty decorWPNames("DecorWPNames", {});
46  ATH_CHECK( tool->getProperty(&decorWPNames) );
47  for (const auto& WP : decorWPNames.value()) m_WPs.push_back(WP);
48  }
49 
50  // declare decorations to the scheduler
51  for (const std::string& score : m_scores) {
52  m_scoreDecorKeys.emplace_back(m_tauContainerKey.key() + "." + score);
53  }
54  for (const std::string& WP : m_WPs) {
55  m_WPDecorKeys.emplace_back(m_tauContainerKey.key() + "." + WP);
56  }
57 
58  // initialize read/write handle keys
59  ATH_CHECK( m_tauContainerKey.initialize() );
61  ATH_CHECK( m_scoreDecorKeys.initialize() );
62  ATH_CHECK( m_WPDecorKeys.initialize() );
63  ATH_CHECK( m_trackWidthKey.initialize() );
64  ATH_CHECK( m_passTATTauMuonOLRKey.initialize() );
65 
66  return StatusCode::SUCCESS;
67  }
68 
70  {
71  return StatusCode::SUCCESS;
72  }
73 
74  StatusCode TauIDDecoratorWrapper::addBranches(const EventContext& ctx) const
75  {
76 
77  // retrieve tau container
79  if (!tauJetsReadHandle.isValid()) {
80  ATH_MSG_ERROR ("Could not retrieve TauJetContainer with key " << tauJetsReadHandle.key());
81  return StatusCode::FAILURE;
82  }
83  const xAOD::TauJetContainer* tauContainer = tauJetsReadHandle.cptr();
84 
85  //Create accessors
86  static const SG::Accessor<float> acc_absEtaLead("ABS_ETA_LEAD_TRACK");
87 
88  std::vector<SG::WriteDecorHandle<xAOD::TauJetContainer, float> > scoreDecors;
89  scoreDecors.reserve (m_scores.size());
91  scoreDecors.emplace_back (k, ctx);
92  }
93  std::vector<SG::WriteDecorHandle<xAOD::TauJetContainer, char> > WPDecors;
94  WPDecors.reserve (m_WPs.size());
96  WPDecors.emplace_back (k, ctx);
97  }
98 
101  for (const auto tau : *tauContainer) {
102  float tauTrackBasedWidth = 0.;
103  // equivalent to tracks(xAOD::TauJetParameters::TauTrackFlag::classifiedCharged)
104  std::vector<const xAOD::TauTrack *> tauTracks = tau->tracks();
106  tauTracks.push_back(trk);
107  }
108  double sumWeightedDR = 0.;
109  double ptSum = 0.;
110  for (const xAOD::TauTrack *track : tauTracks) {
111  double deltaR = tau->p4().DeltaR(track->p4());
112  sumWeightedDR += deltaR * track->pt();
113  ptSum += track->pt();
114  }
115  if (ptSum > 0.) {
116  tauTrackBasedWidth = sumWeightedDR / ptSum;
117  }
118 
119  dec_trackWidth(*tau) = tauTrackBasedWidth;
120  }
121 
122  // create shallow copy
123  auto shallowCopy = xAOD::shallowCopyContainer (*tauContainer);
124 
125  for (auto tau : *shallowCopy.first) {
126 
127  // ABS_ETA_LEAD_TRACK is removed from the AOD content and must be redecorated when computing eVeto WPs
128  // note: this redecoration is not robust against charged track thinning, but charged tracks should never be thinned
129  if (m_doEvetoWP) {
130  float absEtaLead = -1111.;
131  if(tau->nTracks() > 0) {
132  const xAOD::TrackParticle* track = tau->track(0)->track();
133  absEtaLead = std::abs( track->eta() );
134  }
135  acc_absEtaLead(*tau) = absEtaLead;
136  }
137 
138  // pass the shallow copy to the tools
139  for (const auto& tool : m_tauIDTools) {
140  ATH_CHECK( tool->execute(*tau) );
141  }
142 
143  // copy over the relevant decorations (scores and working points)
144  const xAOD::TauJet* xTau = tauContainer->at(tau->index());
145  for (SG::WriteDecorHandle<xAOD::TauJetContainer, float>& dec : scoreDecors) {
146  SG::ConstAccessor<float> scoreAcc (dec.auxid());
147  dec(*xTau) = scoreAcc(*tau);
148  }
150  SG::ConstAccessor<char> WPAcc (dec.auxid());
151  dec(*xTau) = WPAcc(*tau);
152  }
153  }
154 
155  delete shallowCopy.first;
156  delete shallowCopy.second;
157 
158  // add TauAnalysisTool MuonOLR
160  if (!muonReadHandle.isValid()) {
161  ATH_MSG_DEBUG ("Could not retrieve MuonContainer with key " << muonReadHandle.key() << " so won't add TAT MuonOLR flag");
162  return StatusCode::SUCCESS;
163  }
164  const xAOD::MuonContainer* muonContainer = muonReadHandle.cptr();
165 
166  for (const auto tau : *tauContainer) {
167  bool bTauMuonOLR = true;
168  for (auto muon : *muonContainer){
169  if(muon->pt() < 2000.) continue; // pt > 2 GeV
170  if(muon->muonType() == xAOD::Muon::CaloTagged) continue; // not calo-tagged
171  if(muon->p4().DeltaR( tau->p4() ) > 0.2 ) continue; // delta R < 0.2
172  bTauMuonOLR = false; // muon-tau overlapped
173  break;
174  }
175  dec_passTATTauMuonOLR(*tau) = bTauMuonOLR;
176  }
177 
178  return StatusCode::SUCCESS;
179  }
180 }
muonContainer
xAOD::MuonContainer * muonContainer
Definition: TrigGlobEffCorrValidation.cxx:188
ShallowCopy.h
DerivationFramework::TauIDDecoratorWrapper::m_WPDecorKeys
SG::WriteDecorHandleKeyArray< xAOD::TauJetContainer > m_WPDecorKeys
Definition: TauIDDecoratorWrapper.h:38
TauIDDecoratorWrapper.h
SG::WriteDecorHandleKey
Property holding a SG store/key/clid/attr name from which a WriteDecorHandle is made.
Definition: StoreGate/StoreGate/WriteDecorHandleKey.h:89
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:196
DerivationFramework::TauIDDecoratorWrapper::TauIDDecoratorWrapper
TauIDDecoratorWrapper(const std::string &t, const std::string &n, const IInterface *p)
Definition: TauIDDecoratorWrapper.cxx:12
DerivationFramework::TauIDDecoratorWrapper::addBranches
virtual StatusCode addBranches(const EventContext &ctx) const override
Definition: TauIDDecoratorWrapper.cxx:74
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
SG::Accessor< float >
DerivationFramework::TauIDDecoratorWrapper::m_tauIDTools
ToolHandleArray< TauRecToolBase > m_tauIDTools
Definition: TauIDDecoratorWrapper.h:42
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
DerivationFramework::TauIDDecoratorWrapper::m_WPs
std::vector< std::string > m_WPs
Definition: TauIDDecoratorWrapper.h:45
IsoCloseByCorrectionTest.WP
WP
Definition: IsoCloseByCorrectionTest.py:56
SG::ConstAccessor< float >
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
DerivationFramework::TauIDDecoratorWrapper::m_doEvetoWP
bool m_doEvetoWP
Definition: TauIDDecoratorWrapper.h:43
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
DerivationFramework::TauIDDecoratorWrapper::m_passTATTauMuonOLRKey
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_passTATTauMuonOLRKey
Definition: TauIDDecoratorWrapper.h:40
beamspotman.n
n
Definition: beamspotman.py:727
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
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
xAOD::TauJet_v3
Class describing a tau jet.
Definition: TauJet_v3.h:41
DerivationFramework::TauIDDecoratorWrapper::m_scores
std::vector< std::string > m_scores
Definition: TauIDDecoratorWrapper.h:44
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
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:795
DerivationFramework::TauIDDecoratorWrapper::initialize
virtual StatusCode initialize() override
Definition: TauIDDecoratorWrapper.cxx:17
DerivationFramework::TauIDDecoratorWrapper::m_tauContainerKey
SG::ReadHandleKey< xAOD::TauJetContainer > m_tauContainerKey
Definition: TauIDDecoratorWrapper.h:35
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
xAOD::TauJetParameters::classifiedIsolation
@ classifiedIsolation
Definition: TauDefs.h:407
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:452
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
xAOD::shallowCopyContainer
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, [[maybe_unused]] const EventContext &ctx)
Function making a shallow copy of a constant container.
Definition: ShallowCopy.h:110
xAOD::score
@ score
Definition: TrackingPrimitives.h:514
DerivationFramework::TauIDDecoratorWrapper::m_scoreDecorKeys
SG::WriteDecorHandleKeyArray< xAOD::TauJetContainer > m_scoreDecorKeys
Definition: TauIDDecoratorWrapper.h:37
xAOD::TauTrack_v1
Definition: TauTrack_v1.h:27
xAOD::track
@ track
Definition: TrackingPrimitives.h:513
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:44
ReadHandle.h
Handle class for reading from StoreGate.
makeComparison.deltaR
float deltaR
Definition: makeComparison.py:36
Decorator.h
Helper class to provide type-safe access to aux data.
DerivationFramework::TauIDDecoratorWrapper::finalize
virtual StatusCode finalize() override
Definition: TauIDDecoratorWrapper.cxx:69
DerivationFramework::TauIDDecoratorWrapper::m_muonContainerKey
SG::ReadHandleKey< xAOD::MuonContainer > m_muonContainerKey
Definition: TauIDDecoratorWrapper.h:36
fitman.k
k
Definition: fitman.py:528
DerivationFramework::TauIDDecoratorWrapper::m_trackWidthKey
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_trackWidthKey
Definition: TauIDDecoratorWrapper.h:39