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  // declare decorations to the scheduler
51  for (const std::string& score : m_scores) {
52  m_decorKeys.emplace_back(m_tauContainerKey.key() + "." + score);
53  }
54  for (const std::string& WP : m_WPs) {
55  m_decorKeys.emplace_back(m_tauContainerKey.key() + "." + WP);
56  }
57  }
58 
59  // initialize read/write handle keys
60  ATH_CHECK( m_tauContainerKey.initialize() );
61  ATH_CHECK( m_vtxContainerKey.initialize() );
62  ATH_CHECK( m_decorKeys.initialize() );
63 
64  return StatusCode::SUCCESS;
65  }
66 
68  {
69  return StatusCode::SUCCESS;
70  }
71 
73  {
74  // retrieve tau container
76  if (!tauJetsReadHandle.isValid()) {
77  ATH_MSG_ERROR ("Could not retrieve TauJetContainer with key " << tauJetsReadHandle.key());
78  return StatusCode::FAILURE;
79  }
80  const xAOD::TauJetContainer* tauContainer = tauJetsReadHandle.cptr();
81 
82  // retrieve PrimaryVertices container
84  if (!vtxReadHandle.isValid()) {
85  ATH_MSG_ERROR ("Could not retrieve VertexContainer with key " << vtxReadHandle.key());
86  return StatusCode::FAILURE;
87  }
88  const xAOD::VertexContainer* vtxContainer = vtxReadHandle.cptr();
89  const xAOD::Vertex* pVtx = nullptr;
90  float sumpt_PV0 = 0., sumpt2_PV0 = 0.;
91 
92  // Check that PV container exists and is non-empty, find the PV if possible
93  if (vtxContainer != nullptr && !vtxContainer->empty()) {
94  auto itrVtx = std::find_if(vtxContainer->begin(), vtxContainer->end(),
95  [](const xAOD::Vertex* vtx) {
96  return vtx->vertexType() == xAOD::VxType::PriVtx;
97  });
98  pVtx = (itrVtx == vtxContainer->end() ? nullptr : *itrVtx);
99  if (pVtx == nullptr){
100  ATH_MSG_DEBUG("No PV found, using the first element instead!");
101  pVtx = vtxContainer->at(0);
102  }
103 
105  sumpt_PV0 += (*trk)->pt();
106  sumpt2_PV0 += std::pow((*trk)->pt(), 2.);
107  }
108  }
109 
110  //Create accessors
111  static const SG::AuxElement::Decorator<float> acc_trackWidth("trackWidth");
112  static const SG::AuxElement::Accessor<float> acc_absEtaLead("ABS_ETA_LEAD_TRACK");
113  static const SG::AuxElement::Accessor<float> acc_dz0_TV_PV0("dz0_TV_PV0");
114  static const SG::AuxElement::Accessor<float> acc_log_sumpt_TV("log_sumpt_TV");
115  static const SG::AuxElement::Accessor<float> acc_log_sumpt2_TV("log_sumpt2_TV");
116  static const SG::AuxElement::Accessor<float> acc_log_sumpt_PV0("log_sumpt_PV0");
117  static const SG::AuxElement::Accessor<float> acc_log_sumpt2_PV0("log_sumpt2_PV0");
118 
119  for (const auto tau : *tauContainer) {
120  float tauTrackBasedWidth = 0.;
121  // equivalent to tracks(xAOD::TauJetParameters::TauTrackFlag::classifiedCharged)
122  std::vector<const xAOD::TauTrack *> tauTracks = tau->tracks();
124  tauTracks.push_back(trk);
125  }
126  double sumWeightedDR = 0.;
127  double ptSum = 0.;
128  for (const xAOD::TauTrack *track : tauTracks) {
129  double deltaR = tau->p4().DeltaR(track->p4());
130  sumWeightedDR += deltaR * track->pt();
131  ptSum += track->pt();
132  }
133  if (ptSum > 0.) {
134  tauTrackBasedWidth = sumWeightedDR / ptSum;
135  }
136 
137  acc_trackWidth(*tau) = tauTrackBasedWidth;
138  }
139 
140  // create shallow copy
141  auto shallowCopy = xAOD::shallowCopyContainer (*tauContainer);
142 
143  for (auto tau : *shallowCopy.first) {
144 
145  //Add in the TV/PV0 vertex variables needed for some calculators in TauGNNUtils.cxx (for GNTau)
146  float dz0_TV_PV0 = -999., sumpt_TV = 0., sumpt2_TV = 0.;
147  if (pVtx!=nullptr) {
148  dz0_TV_PV0 = tau->vertex()->z() - pVtx->z();
149  for (const ElementLink<xAOD::TrackParticleContainer>& trk : tau->vertex()->trackParticleLinks()) {
150  sumpt_TV += (*trk)->pt();
151  sumpt2_TV += std::pow((*trk)->pt(), 2.);
152  }
153  }
154  acc_dz0_TV_PV0(*tau) = dz0_TV_PV0;
155  acc_log_sumpt_TV(*tau) = (sumpt_TV>0.) ? std::log(sumpt_TV) : 0.;
156  acc_log_sumpt2_TV(*tau) = (sumpt2_TV>0.) ? std::log(sumpt2_TV) : 0.;
157  acc_log_sumpt_PV0(*tau) = (sumpt_PV0>0.) ? std::log(sumpt_PV0) : 0.;
158  acc_log_sumpt2_PV0(*tau) = (sumpt2_PV0>0.) ? std::log(sumpt2_PV0) : 0.;
159 
160  // ABS_ETA_LEAD_TRACK is removed from the AOD content and must be redecorated when computing eVeto WPs
161  // note: this redecoration is not robust against charged track thinning, but charged tracks should never be thinned
162  if (m_doEvetoWP) {
163  float absEtaLead = -1111.;
164  if(tau->nTracks() > 0) {
165  const xAOD::TrackParticle* track = tau->track(0)->track();
166  absEtaLead = std::abs( track->eta() );
167  }
168  acc_absEtaLead(*tau) = absEtaLead;
169  }
170 
171  // pass the shallow copy to the tools
172  for (const auto& tool : m_tauIDTools) {
173  ATH_CHECK( tool->execute(*tau) );
174  }
175 
176  // copy over the relevant decorations (scores and working points)
177  const xAOD::TauJet* xTau = tauContainer->at(tau->index());
178  for (const std::string& score : m_scores) {
179  SG::Decorator<float> scoreDec (score);
180  SG::ConstAccessor<float> scoreAcc (score);
181  scoreDec(*xTau) = scoreAcc(*tau);
182  }
183  for (const std::string& WP : m_WPs) {
184  SG::Decorator<char> WPDec (WP);
185  SG::ConstAccessor<char> WPAcc (WP);
186  WPDec(*xTau) = WPAcc(*tau);
187  }
188  }
189 
190  delete shallowCopy.first;
191  delete shallowCopy.second;
192 
193  return StatusCode::SUCCESS;
194  }
195 }
ShallowCopy.h
TauIDDecoratorWrapper.h
DerivationFramework::TauIDDecoratorWrapper::TauIDDecoratorWrapper
TauIDDecoratorWrapper(const std::string &t, const std::string &n, const IInterface *p)
Definition: TauIDDecoratorWrapper.cxx:12
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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:66
DerivationFramework::TauIDDecoratorWrapper::m_tauIDTools
ToolHandleArray< TauRecToolBase > m_tauIDTools
Definition: TauIDDecoratorWrapper.h:39
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
DerivationFramework::TauIDDecoratorWrapper::m_WPs
std::vector< std::string > m_WPs
Definition: TauIDDecoratorWrapper.h:42
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:755
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
SG::ConstAccessor< float >
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
DerivationFramework::TauIDDecoratorWrapper::m_doEvetoWP
bool m_doEvetoWP
Definition: TauIDDecoratorWrapper.h:40
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:58
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
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:36
DerivationFramework::TauIDDecoratorWrapper::m_scores
std::vector< std::string > m_scores
Definition: TauIDDecoratorWrapper.h:41
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:581
DerivationFramework::TauIDDecoratorWrapper::initialize
StatusCode initialize() override
Definition: TauIDDecoratorWrapper.cxx:18
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?
DerivationFramework::TauIDDecoratorWrapper::m_decorKeys
SG::WriteDecorHandleKeyArray< xAOD::TauJetContainer > m_decorKeys
Definition: TauIDDecoratorWrapper.h:37
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.
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:72
makeComparison.deltaR
float deltaR
Definition: makeComparison.py:36
Decorator.h
Helper class to provide type-safe access to aux data.
DerivationFramework::TauIDDecoratorWrapper::finalize
StatusCode finalize() override
Definition: TauIDDecoratorWrapper.cxx:67
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.