ATLAS Offline Software
Loading...
Searching...
No Matches
TauAODRunnerAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "TauAODRunnerAlg.h"
6
7TauAODRunnerAlg::TauAODRunnerAlg(const std::string &name, ISvcLocator *pSvcLocator) :
8 AthReentrantAlgorithm(name, pSvcLocator) {}
9
10
12 ATH_CHECK(m_tauContainer.initialize());
14 ATH_CHECK(m_tauOutContainer.initialize());
21
23 ATH_CHECK(m_officialTools.retrieve());
24
25 if(!m_modificationTools.empty()) {
26 ATH_MSG_INFO("List of modification tools in execution sequence:");
27 ATH_MSG_INFO("------------------------------------");
28 for (const auto &tool : m_modificationTools) {
29 ATH_MSG_INFO(tool->type() << " - " << tool->name());
30 }
31 ATH_MSG_INFO("------------------------------------");
32 } else {
33 ATH_MSG_INFO("Running without modification tools");
34 }
35
36 if(!m_officialTools.empty()) {
37 ATH_MSG_INFO("List of official tools in execution sequence:");
38 ATH_MSG_INFO("------------------------------------");
39 for (const auto &tool : m_officialTools) {
40 ATH_MSG_INFO(tool->type() << " - " << tool->name());
41
42 if((tool->type() == "TauPi0ClusterCreator" && (m_neutralPFOOutputContainer.empty() || m_hadronicPFOOutputContainer.empty() || m_pi0ClusterInputContainer.empty()))
43 || (tool->type() == "TauVertexVariables" && m_vertexOutputContainer.empty())
44 || (tool->type() == "TauPi0ClusterScaler" && (m_neutralPFOOutputContainer.empty() || m_chargedPFOOutputContainer.empty()))
45 || (tool->type() == "TauPi0ScoreCalculator" && m_neutralPFOOutputContainer.empty())
46 || (tool->type() == "TauPi0Selector" && m_neutralPFOOutputContainer.empty())
47 || (tool->type() == "PanTau::PanTauProcessor" && (m_neutralPFOOutputContainer.empty() || m_pi0Container.empty()))
48 || (tool->type() == "tauRecTools::TauTrackRNNClassifier" && m_tauTrackOutputContainer.empty())) {
49 ATH_MSG_ERROR("Missing input/output containers required for tool " << tool->name() << " (" << tool->type() << ")");
50 return StatusCode::FAILURE;
51 }
52 }
53 ATH_MSG_INFO("------------------------------------");
54 } else {
55 ATH_MSG_INFO("Running without official tools");
56 }
57
58 if(m_modificationTools.empty() && m_officialTools.empty()) {
59 ATH_MSG_ERROR("Could not allocate any tool!");
60 return StatusCode::FAILURE;
61 }
62
63 return StatusCode::SUCCESS;
64}
65
66
67StatusCode TauAODRunnerAlg::execute (const EventContext& ctx) const {
68 // Input TauJets
70 if (!tauInputHandle.isValid()) {
71 ATH_MSG_ERROR("Could not retrieve TauJetContainer with key " << tauInputHandle.key());
72 return StatusCode::FAILURE;
73 }
74 const xAOD::TauJetContainer *pTauContainer = tauInputHandle.cptr();
75
76
77 // Output TauTracks
78 xAOD::TauTrackContainer* newTauTrkCon = nullptr;
80 if(!m_tauTrackOutputContainer.empty()) {
81 outputTauTrackHandle = SG::makeHandle(m_tauTrackOutputContainer, ctx);
82 ATH_CHECK(outputTauTrackHandle.record(std::make_unique<xAOD::TauTrackContainer>(), std::make_unique<xAOD::TauTrackAuxContainer>()));
83 newTauTrkCon = outputTauTrackHandle.ptr();
84 }
85
86 // Output TauJets
88 ATH_CHECK(outputTauHandle.record(std::make_unique<xAOD::TauJetContainer>(), std::make_unique<xAOD::TauJetAuxContainer>()));
89 xAOD::TauJetContainer *newTauCon = outputTauHandle.ptr();
90
91 static const SG::Accessor<ElementLink<xAOD::TauJetContainer>> acc_ori_tau_link("originalTauJet");
92 static const SG::Accessor<char> acc_modified("ModifiedInAOD");
93
94 for (const xAOD::TauJet *tau : *pTauContainer) {
95 // Deep copy the tau container
96 xAOD::TauJet* newTau = newTauCon->push_back(std::make_unique<xAOD::TauJet>());
97 *newTau = *tau;
98
99 // Link the original tau to the deepcopy
101 link_to_ori_tau.toContainedElement(*pTauContainer, tau);
102 acc_ori_tau_link(*newTau) = link_to_ori_tau;
103
104 // If the output TauTrackContainer is declared, create a new copy of all existing TauTracks
105 // otherwise, reuse the same TauTracks (e.g. if only new TauID is being ran on a deep copy
106 // of the input TauJet container).
107 if(newTauTrkCon) {
108 // Clear the tautrack links to allow relinking.
109 newTau->clearTauTrackLinks();
110 for(const xAOD::TauTrack *tauTrk : tau->allTracks()) {
111 // Deep copy the tau track
112 xAOD::TauTrack* newTauTrk = newTauTrkCon->push_back(std::make_unique<xAOD::TauTrack>());
113 *newTauTrk = *tauTrk;
114
115 // Relink the tautrack
117 linkToTauTrack.toContainedElement(*newTauTrkCon, newTauTrk);
118 newTau->addTauTrackLink(linkToTauTrack);
119 }
120 }
121
122 // 'ModifiedInAOD' will be overriden by modification tools for relevant candidates
123 acc_modified(*newTau) = static_cast<char>(false);
124
125 // Execute all the modification tools (if provided)
126 for(const ToolHandle<ITauToolBase> &tool : m_modificationTools) {
127 ATH_MSG_DEBUG("RunnerAlg Invoking tool " << tool->name());
128 if(tool->execute(*newTau).isFailure()) break;
129 }
130
131 // If tau candidate was not modified and we ran modification tools, remove it from container
132 // The track cleanup is performed by the thinning algorithm downstream
133 if(!m_modificationTools.empty() && !acc_modified(*newTau)) {
134 newTauCon->pop_back();
135 }
136 }
137
138
139 // Read the CaloClusterContainer
140 const xAOD::CaloClusterContainer* pi0ClusterContainer = nullptr;
142 if(!m_pi0ClusterInputContainer.empty()) {
143 pi0ClusterInHandle = SG::makeHandle(m_pi0ClusterInputContainer, ctx);
144 if(!pi0ClusterInHandle.isValid()) {
145 ATH_MSG_ERROR ("Could not retrieve HiveDataObj with key " << pi0ClusterInHandle.key());
146 return StatusCode::FAILURE;
147 }
148 pi0ClusterContainer = pi0ClusterInHandle.cptr();
149 }
150
151 // Write charged PFO container
152 xAOD::PFOContainer* chargedPFOContainer = nullptr;
154 if(!m_chargedPFOOutputContainer.empty()) {
155 chargedPFOHandle = SG::makeHandle(m_chargedPFOOutputContainer, ctx);
156 ATH_CHECK(chargedPFOHandle.record(std::make_unique<xAOD::PFOContainer>(), std::make_unique<xAOD::PFOAuxContainer>()));
157 chargedPFOContainer = chargedPFOHandle.ptr();
158 }
159
160 // Write neutral PFO container
161 xAOD::PFOContainer* neutralPFOContainer = nullptr;
163 if(!m_neutralPFOOutputContainer.empty()) {
164 neutralPFOHandle = SG::makeHandle(m_neutralPFOOutputContainer, ctx);
165 ATH_CHECK(neutralPFOHandle.record(std::make_unique<xAOD::PFOContainer>(), std::make_unique<xAOD::PFOAuxContainer>()));
166 neutralPFOContainer = neutralPFOHandle.ptr();
167 }
168
169 // Write pi0 container
170 xAOD::ParticleContainer* pi0Container = nullptr;
172 if(!m_pi0Container.empty()) {
173 pi0Handle = SG::makeHandle(m_pi0Container, ctx);
174 ATH_CHECK(pi0Handle.record(std::make_unique<xAOD::ParticleContainer>(), std::make_unique<xAOD::ParticleAuxContainer>()));
175 pi0Container = pi0Handle.ptr();
176 }
177
178 // Write hadronic cluster PFO container
179 xAOD::PFOContainer* hadronicClusterPFOContainer = nullptr;
180 SG::WriteHandle<xAOD::PFOContainer> hadronicPFOHandle;
181 if(!m_hadronicPFOOutputContainer.empty()) {
182 hadronicPFOHandle = SG::makeHandle(m_hadronicPFOOutputContainer, ctx);
183 ATH_CHECK(hadronicPFOHandle.record(std::make_unique<xAOD::PFOContainer>(), std::make_unique<xAOD::PFOAuxContainer>()));
184 hadronicClusterPFOContainer = hadronicPFOHandle.ptr();
185 }
186
187 // Write secondary vertices
188 xAOD::VertexContainer* pSecVtxContainer = nullptr;
190 if(!m_vertexOutputContainer.empty()) {
191 vertOutHandle = SG::makeHandle(m_vertexOutputContainer, ctx);
192 ATH_CHECK(vertOutHandle.record(std::make_unique<xAOD::VertexContainer>(), std::make_unique<xAOD::VertexAuxContainer>()));
193 pSecVtxContainer = vertOutHandle.ptr();
194 }
195
196
197 // Execute all post-modification (_official_) tools
198 for (xAOD::TauJet *pTau : *newTauCon) {
199 StatusCode sc = StatusCode::SUCCESS;
200 for (const ToolHandle<ITauToolBase> &tool : m_officialTools) {
201 ATH_MSG_DEBUG("RunnerAlg Invoking tool " << tool->name());
202 if (tool->type() == "TauPi0ClusterCreator")
203 sc = tool->executePi0ClusterCreator(*pTau, *neutralPFOContainer, *hadronicClusterPFOContainer, *pi0ClusterContainer);
204 else if (tool->type() == "TauVertexVariables")
205 sc = tool->executeVertexVariables(*pTau, *pSecVtxContainer);
206 else if (tool->type() == "TauPi0ClusterScaler")
207 sc = tool->executePi0ClusterScaler(*pTau, *neutralPFOContainer, *chargedPFOContainer);
208 else if (tool->type() == "TauPi0ScoreCalculator")
209 sc = tool->executePi0nPFO(*pTau, *neutralPFOContainer);
210 else if (tool->type() == "TauPi0Selector")
211 sc = tool->executePi0nPFO(*pTau, *neutralPFOContainer);
212 else if (tool->type() == "PanTau::PanTauProcessor")
213 sc = tool->executePanTau(*pTau, *pi0Container, *neutralPFOContainer);
214 else if (tool->type() == "tauRecTools::TauTrackRNNClassifier")
215 sc = tool->executeTrackClassifier(*pTau, *newTauTrkCon);
216 else
217 sc = tool->execute(*pTau);
218
219 if (sc.isFailure()) break;
220 }
221 if (sc.isSuccess()) ATH_MSG_VERBOSE("The tau candidate has been modified successfully by the invoked official tools.");
222 }
223
224 ATH_MSG_VERBOSE("The tau candidate container has been modified by the rest of the tools");
225 ATH_MSG_DEBUG(newTauCon->size() << " / " << pTauContainer->size() <<" taus were modified");
226
227 return StatusCode::SUCCESS;
228}
229
230
231// Helper
233 static const SG::ConstAccessor<char> acc_modified("ModifiedInAOD");
234 return acc_modified(*newtau);
235}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
static Double_t sc
An algorithm that can be simultaneously executed in multiple threads.
void pop_back()
Remove the last element from the collection.
value_type push_back(value_type pElem)
Add an element to the end of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
Helper class to provide type-safe access to aux data.
Helper class to provide constant type-safe access to aux data.
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.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
pointer_type ptr()
Dereference the pointer.
virtual StatusCode initialize() override
virtual StatusCode execute(const EventContext &ctx) const override
ToolHandleArray< ITauToolBase > m_modificationTools
SG::ReadHandleKey< xAOD::TauJetContainer > m_tauContainer
SG::WriteHandleKey< xAOD::TauJetContainer > m_tauOutContainer
SG::WriteHandleKey< xAOD::PFOContainer > m_neutralPFOOutputContainer
SG::WriteHandleKey< xAOD::ParticleContainer > m_pi0Container
TauAODRunnerAlg(const std::string &name, ISvcLocator *)
ToolHandleArray< ITauToolBase > m_officialTools
SG::WriteHandleKey< xAOD::PFOContainer > m_chargedPFOOutputContainer
SG::WriteHandleKey< xAOD::PFOContainer > m_hadronicPFOOutputContainer
static bool isTauModified(const xAOD::TauJet *newtau)
SG::ReadHandleKey< xAOD::CaloClusterContainer > m_pi0ClusterInputContainer
SG::WriteHandleKey< xAOD::VertexContainer > m_vertexOutputContainer
SG::WriteHandleKey< xAOD::TauTrackContainer > m_tauTrackOutputContainer
void clearTauTrackLinks()
Remove all tracks from the tau.
void addTauTrackLink(const ElementLink< TauTrackContainer > &tr)
add a TauTrack to the tau
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
PFOContainer_v1 PFOContainer
Definition of the current "pfo container version".
ParticleContainer_v1 ParticleContainer
Define the latest version of the particle class.
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
TauTrack_v1 TauTrack
Definition of the current version.
Definition TauTrack.h:16
TauJet_v3 TauJet
Definition of the current "tau version".
TauTrackContainer_v1 TauTrackContainer
Definition of the current TauTrack container version.
CaloClusterContainer_v1 CaloClusterContainer
Define the latest version of the calorimeter cluster container.
TauJetContainer_v3 TauJetContainer
Definition of the current "taujet container version".