ATLAS Offline Software
TauIDDecoratorWrapper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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  AthAlgTool(t,n,p)
14  {
15  declareInterface<DerivationFramework::IAugmentationTool>(this);
16  }
17 
19  {
20  // initialize tauRecTools tools
21  ATH_CHECK( m_tauIDTools.retrieve() );
22 
23  // parse the properties of TauWPDecorator tools
24  for (const auto& tool : m_tauIDTools) {
25  if (tool->type() != "TauWPDecorator") continue;
26 
27  // check whether we must compute eVeto WPs, as this requires the recalculation of a variable
28  BooleanProperty useAbsEta("UseAbsEta", false);
29  ATH_CHECK( tool->getProperty(&useAbsEta) );
30  if (useAbsEta.value()) {
31  m_doEvetoWP = true;
32  }
33 
34  // retrieve the names of ID scores and WPs
35  StringProperty scoreName("ScoreName", "");
36  ATH_CHECK( tool->getProperty(&scoreName) );
37  // the original RNNEleScore should not be overriden
38  if (scoreName.value() != "RNNEleScore") {
39  m_scores.push_back(scoreName.value());
40  }
41 
42  StringProperty newScoreName("NewScoreName", "");
43  ATH_CHECK( tool->getProperty(&newScoreName) );
44  m_scores.push_back(newScoreName.value());
45 
46  StringArrayProperty decorWPNames("DecorWPNames", {});
47  ATH_CHECK( tool->getProperty(&decorWPNames) );
48  for (const auto& WP : decorWPNames.value()) m_WPs.push_back(WP);
49  }
50 
51  // declare decorations to the scheduler
52  for (const std::string& score : m_scores) {
53  m_scoreDecorKeys.emplace_back(m_tauContainerKey.key() + "." + score);
54  }
55  for (const std::string& WP : m_WPs) {
56  m_WPDecorKeys.emplace_back(m_tauContainerKey.key() + "." + WP);
57  }
58 
59  // initialize read/write handle keys
60  ATH_CHECK( m_tauContainerKey.initialize() );
61  ATH_CHECK( m_muonContainerKey.initialize() );
62  ATH_CHECK( m_vtxContainerKey.initialize() );
63  ATH_CHECK( m_scoreDecorKeys.initialize() );
64  ATH_CHECK( m_WPDecorKeys.initialize() );
65  ATH_CHECK( m_trackWidthKey.initialize() );
66  ATH_CHECK( m_passTATTauMuonOLRKey.initialize() );
67 
68  return StatusCode::SUCCESS;
69  }
70 
72  {
73  return StatusCode::SUCCESS;
74  }
75 
77  {
78  const EventContext& ctx = Gaudi::Hive::currentContext();
79 
80  // retrieve tau container
82  if (!tauJetsReadHandle.isValid()) {
83  ATH_MSG_ERROR ("Could not retrieve TauJetContainer with key " << tauJetsReadHandle.key());
84  return StatusCode::FAILURE;
85  }
86  const xAOD::TauJetContainer* tauContainer = tauJetsReadHandle.cptr();
87 
88  // retrieve PrimaryVertices container
90  if (!vtxReadHandle.isValid()) {
91  ATH_MSG_ERROR ("Could not retrieve VertexContainer with key " << vtxReadHandle.key());
92  return StatusCode::FAILURE;
93  }
94  const xAOD::VertexContainer* vtxContainer = vtxReadHandle.cptr();
95  const xAOD::Vertex* pVtx = nullptr;
96  float sumpt_PV0 = 0., sumpt2_PV0 = 0.;
97 
98  // Check that PV container exists and is non-empty, find the PV if possible
99  if (vtxContainer != nullptr && !vtxContainer->empty()) {
100  auto itrVtx = std::find_if(vtxContainer->begin(), vtxContainer->end(),
101  [](const xAOD::Vertex* vtx) {
102  return vtx->vertexType() == xAOD::VxType::PriVtx;
103  });
104  pVtx = (itrVtx == vtxContainer->end() ? nullptr : *itrVtx);
105  if (pVtx == nullptr){
106  ATH_MSG_DEBUG("No PV found, using the first element instead!");
107  pVtx = vtxContainer->at(0);
108  }
109 
111  sumpt_PV0 += (*trk)->pt();
112  sumpt2_PV0 += std::pow((*trk)->pt(), 2.);
113  }
114  }
115 
116  //Create accessors
117  static const SG::AuxElement::Accessor<float> acc_absEtaLead("ABS_ETA_LEAD_TRACK");
118  static const SG::AuxElement::Accessor<float> acc_dz0_TV_PV0("dz0_TV_PV0");
119  static const SG::AuxElement::Accessor<float> acc_log_sumpt_TV("log_sumpt_TV");
120  static const SG::AuxElement::Accessor<float> acc_log_sumpt2_TV("log_sumpt2_TV");
121  static const SG::AuxElement::Accessor<float> acc_log_sumpt_PV0("log_sumpt_PV0");
122  static const SG::AuxElement::Accessor<float> acc_log_sumpt2_PV0("log_sumpt2_PV0");
123 
124  std::vector<SG::WriteDecorHandle<xAOD::TauJetContainer, float> > scoreDecors;
125  scoreDecors.reserve (m_scores.size());
127  scoreDecors.emplace_back (k, ctx);
128  }
129  std::vector<SG::WriteDecorHandle<xAOD::TauJetContainer, char> > WPDecors;
130  WPDecors.reserve (m_WPs.size());
132  WPDecors.emplace_back (k, ctx);
133  }
134 
137  for (const auto tau : *tauContainer) {
138  float tauTrackBasedWidth = 0.;
139  // equivalent to tracks(xAOD::TauJetParameters::TauTrackFlag::classifiedCharged)
140  std::vector<const xAOD::TauTrack *> tauTracks = tau->tracks();
142  tauTracks.push_back(trk);
143  }
144  double sumWeightedDR = 0.;
145  double ptSum = 0.;
146  for (const xAOD::TauTrack *track : tauTracks) {
147  double deltaR = tau->p4().DeltaR(track->p4());
148  sumWeightedDR += deltaR * track->pt();
149  ptSum += track->pt();
150  }
151  if (ptSum > 0.) {
152  tauTrackBasedWidth = sumWeightedDR / ptSum;
153  }
154 
155  dec_trackWidth(*tau) = tauTrackBasedWidth;
156  }
157 
158  // create shallow copy
159  auto shallowCopy = xAOD::shallowCopyContainer (*tauContainer);
160 
161  for (auto tau : *shallowCopy.first) {
162 
163  //Add in the TV/PV0 vertex variables needed for some calculators in TauGNNUtils.cxx (for GNTau)
164  float dz0_TV_PV0 = -999., sumpt_TV = 0., sumpt2_TV = 0.;
165  if (pVtx!=nullptr) {
166  dz0_TV_PV0 = tau->vertex()->z() - pVtx->z();
167  for (const ElementLink<xAOD::TrackParticleContainer>& trk : tau->vertex()->trackParticleLinks()) {
168  sumpt_TV += (*trk)->pt();
169  sumpt2_TV += std::pow((*trk)->pt(), 2.);
170  }
171  }
172  acc_dz0_TV_PV0(*tau) = dz0_TV_PV0;
173  acc_log_sumpt_TV(*tau) = (sumpt_TV>0.) ? std::log(sumpt_TV) : 0.;
174  acc_log_sumpt2_TV(*tau) = (sumpt2_TV>0.) ? std::log(sumpt2_TV) : 0.;
175  acc_log_sumpt_PV0(*tau) = (sumpt_PV0>0.) ? std::log(sumpt_PV0) : 0.;
176  acc_log_sumpt2_PV0(*tau) = (sumpt2_PV0>0.) ? std::log(sumpt2_PV0) : 0.;
177 
178  // ABS_ETA_LEAD_TRACK is removed from the AOD content and must be redecorated when computing eVeto WPs
179  // note: this redecoration is not robust against charged track thinning, but charged tracks should never be thinned
180  if (m_doEvetoWP) {
181  float absEtaLead = -1111.;
182  if(tau->nTracks() > 0) {
183  const xAOD::TrackParticle* track = tau->track(0)->track();
184  absEtaLead = std::abs( track->eta() );
185  }
186  acc_absEtaLead(*tau) = absEtaLead;
187  }
188 
189  // pass the shallow copy to the tools
190  for (const auto& tool : m_tauIDTools) {
191  ATH_CHECK( tool->execute(*tau) );
192  }
193 
194  // copy over the relevant decorations (scores and working points)
195  const xAOD::TauJet* xTau = tauContainer->at(tau->index());
196  for (SG::WriteDecorHandle<xAOD::TauJetContainer, float>& dec : scoreDecors) {
197  SG::ConstAccessor<float> scoreAcc (dec.auxid());
198  dec(*xTau) = scoreAcc(*tau);
199  }
201  SG::ConstAccessor<char> WPAcc (dec.auxid());
202  dec(*xTau) = WPAcc(*tau);
203  }
204  }
205 
206  delete shallowCopy.first;
207  delete shallowCopy.second;
208 
209  // add TauAnalysisTool MuonOLR
211  if (!muonReadHandle.isValid()) {
212  ATH_MSG_DEBUG ("Could not retrieve MuonContainer with key " << muonReadHandle.key() << " so won't add TAT MuonOLR flag");
213  return StatusCode::SUCCESS;
214  }
215  const xAOD::MuonContainer* muonContainer = muonReadHandle.cptr();
216 
217  for (const auto tau : *tauContainer) {
218  bool bTauMuonOLR = true;
219  for (auto muon : *muonContainer){
220  if(muon->pt() < 2000.) continue; // pt > 2 GeV
221  if(muon->muonType() == xAOD::Muon::CaloTagged) continue; // not calo-tagged
222  if(muon->p4().DeltaR( tau->p4() ) > 0.2 ) continue; // delta R < 0.2
223  bTauMuonOLR = false; // muon-tau overlapped
224  break;
225  }
226  dec_passTATTauMuonOLR(*tau) = bTauMuonOLR;
227  }
228 
229  return StatusCode::SUCCESS;
230  }
231 }
muonContainer
xAOD::MuonContainer * muonContainer
Definition: TrigGlobEffCorrValidation.cxx:188
ShallowCopy.h
DerivationFramework::TauIDDecoratorWrapper::m_WPDecorKeys
SG::WriteDecorHandleKeyArray< xAOD::TauJetContainer > m_WPDecorKeys
Definition: TauIDDecoratorWrapper.h:40
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:195
DerivationFramework::TauIDDecoratorWrapper::TauIDDecoratorWrapper
TauIDDecoratorWrapper(const std::string &t, const std::string &n, const IInterface *p)
Definition: TauIDDecoratorWrapper.cxx:12
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
DerivationFramework::TauIDDecoratorWrapper::m_tauIDTools
ToolHandleArray< TauRecToolBase > m_tauIDTools
Definition: TauIDDecoratorWrapper.h:44
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
DerivationFramework::TauIDDecoratorWrapper::m_WPs
std::vector< std::string > m_WPs
Definition: TauIDDecoratorWrapper.h:47
IsoCloseByCorrectionTest.WP
WP
Definition: IsoCloseByCorrectionTest.py:56
xAOD::Vertex_v1::trackParticleLinks
const TrackParticleLinks_t & trackParticleLinks() const
Get all the particles associated with the vertex.
TauGNNUtils::Variables::Track::dz0_TV_PV0
bool dz0_TV_PV0(const xAOD::TauJet &tau, const xAOD::TauTrack &, double &out)
Definition: TauGNNUtils.cxx:769
SG::ConstAccessor< float >
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
DerivationFramework::TauIDDecoratorWrapper::m_doEvetoWP
bool m_doEvetoWP
Definition: TauIDDecoratorWrapper.h:45
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
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:42
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
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_vtxContainerKey
SG::ReadHandleKey< xAOD::VertexContainer > m_vtxContainerKey
Definition: TauIDDecoratorWrapper.h:38
DerivationFramework::TauIDDecoratorWrapper::m_scores
std::vector< std::string > m_scores
Definition: TauIDDecoratorWrapper.h:46
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAOD::Vertex_v1::z
float z() const
Returns the z position.
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
DerivationFramework::TauIDDecoratorWrapper::initialize
virtual StatusCode initialize() override
Definition: TauIDDecoratorWrapper.cxx:18
DerivationFramework::TauIDDecoratorWrapper::m_tauContainerKey
SG::ReadHandleKey< xAOD::TauJetContainer > m_tauContainerKey
Definition: TauIDDecoratorWrapper.h:36
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:453
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:513
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
DerivationFramework::TauIDDecoratorWrapper::m_scoreDecorKeys
SG::WriteDecorHandleKeyArray< xAOD::TauJetContainer > m_scoreDecorKeys
Definition: TauIDDecoratorWrapper.h:39
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
xAOD::TauTrack_v1
Definition: TauTrack_v1.h:27
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
ReadHandle.h
Handle class for reading from StoreGate.
AthAlgTool
Definition: AthAlgTool.h:26
DerivationFramework::TauIDDecoratorWrapper::addBranches
virtual StatusCode addBranches() const override
Pass the thinning service
Definition: TauIDDecoratorWrapper.cxx:76
pow
constexpr int pow(int base, int exp) noexcept
Definition: ap_fixedTest.cxx:15
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:71
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
DerivationFramework::TauIDDecoratorWrapper::m_muonContainerKey
SG::ReadHandleKey< xAOD::MuonContainer > m_muonContainerKey
Definition: TauIDDecoratorWrapper.h:37
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
fitman.k
k
Definition: fitman.py:528
DerivationFramework::TauIDDecoratorWrapper::m_trackWidthKey
SG::WriteDecorHandleKey< xAOD::TauJetContainer > m_trackWidthKey
Definition: TauIDDecoratorWrapper.h:41