ATLAS Offline Software
Loading...
Searching...
No Matches
AlgorithmStateModule.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8
9//
10// includes
11//
12
14
18#include <AsgTools/SgEvent.h>
21#include <EventLoop/Worker.h>
23#include <TTree.h>
24#include <algorithm>
25#include <exception>
26
27//
28// method implementations
29//
30
31namespace EL
32{
33 namespace Detail
34 {
35 namespace
36 {
37 template<typename F> StatusCode
38 forAllAlgorithms (MsgStream& msg, ModuleData& data, const char *funcName, F&& func)
39 {
40 for (AlgorithmData& alg : data.m_algs)
41 {
42 try
43 {
44 typedef typename std::decay<decltype(func(alg))>::type scType__;
45 if (!::asg::CheckHelper<scType__>::isSuccess (func (alg)))
46 {
47 msg << MSG::ERROR << "executing " << funcName << " on algorithm " << alg->getName() << endmsg;
48 return StatusCode::FAILURE;
49 }
50 } catch (...)
51 {
52 report_exception (std::current_exception());
53 msg << MSG::ERROR << "executing " << funcName << " on algorithm " << alg->getName() << endmsg;
54 return StatusCode::FAILURE;
55 }
56 }
57 return StatusCode::SUCCESS;
58 }
59 }
60
61
62
63 StatusCode AlgorithmStateModule ::
64 onInitialize (ModuleData& data)
65 {
66 if (m_initialized)
67 {
68 ANA_MSG_ERROR ("getting second initialize call");
69 return StatusCode::FAILURE;
70 }
71 m_initialized = true;
72 AlgorithmWorkerData workerData;
73 workerData.m_histogramWorker = data.m_worker;
74 workerData.m_treeWorker = data.m_worker;
75 workerData.m_filterWorker = data.m_worker;
76 workerData.m_wk = data.m_worker;
77 workerData.m_evtStore = data.m_evtStore;
78 return forAllAlgorithms (msg(), data, "initialize", [&] (AlgorithmData& alg) {
79 return alg->initialize (workerData);});
80 }
81
82
83
84 StatusCode AlgorithmStateModule ::
85 onFinalize (ModuleData& data)
86 {
87 if (!m_initialized)
88 return StatusCode::SUCCESS;
89 if (forAllAlgorithms (msg(), data, "finalize", [&] (AlgorithmData& alg) {
90 return alg->finalize ();}).isFailure())
91 return StatusCode::FAILURE;
92 return StatusCode::SUCCESS;
93 }
94
95
96
97 StatusCode AlgorithmStateModule ::
98 onCloseInputFile (ModuleData& data)
99 {
100 // beginInputFile is skipped for files without events (see
101 // onNewInputFile), so skip the matching endInputFile as well to keep
102 // the calls paired.
103 if (!data.m_hasInputEvents) return StatusCode::SUCCESS;
104
105 return forAllAlgorithms (msg(), data, "endInputFile", [&] (AlgorithmData& alg) {
106 return alg->endInputFile ();});
107 }
108
109
110
111 StatusCode AlgorithmStateModule ::
112 onNewInputFile (ModuleData& data)
113 {
114 if (!m_initialized)
115 {
116 ANA_MSG_ERROR ("algorithms have not been initialized yet");
117 return StatusCode::FAILURE;
118 }
119
120 // Check that there are events on input
121 if (!data.m_hasInputEvents) return StatusCode::SUCCESS;
122
123 if (forAllAlgorithms (msg(), data, "changeInput", [&] (AlgorithmData& alg) {
124 return alg->beginInputFile ();}).isFailure())
125 return StatusCode::FAILURE;
126 return StatusCode::SUCCESS;
127 }
128
129
130
131 StatusCode AlgorithmStateModule ::
132 onFileExecute (ModuleData& data)
133 {
134 return forAllAlgorithms (msg(), data, "fileExecute", [&] (AlgorithmData& alg) {
135 return alg->fileExecute ();});
136 }
137
138
139
140 StatusCode AlgorithmStateModule ::
141 onExecute (ModuleData& data)
142 {
143 data.m_skipEvent = false;
144 bool sequenceSkip = false;
145 const EventContext& ctx = Gaudi::Hive::currentContext();
146 for (auto& algData : data.m_algs)
147 {
148 try
149 {
150 if (algData.m_sequenceStart)
151 sequenceSkip = false;
152 else if (sequenceSkip)
153 {
154 algData.m_wasSkipped = true;
155 continue;
156 }
157
158 algData.m_executeCount += 1;
159 if (algData.m_algorithm->execute(ctx).isFailure())
160 {
161 ANA_MSG_ERROR ("while calling execute() on algorithm " << algData.m_algorithm->getName());
162 return StatusCode::FAILURE;
163 }
164
165 if (data.m_skipEvent)
166 {
167 algData.m_skipCount += 1;
168 algData.m_wasSkipped = true;
169 sequenceSkip = true;
170 data.m_skipEvent = false;
171 }
172 } catch (...)
173 {
174 Detail::report_exception (std::current_exception());
175 ANA_MSG_ERROR ("while calling execute() on algorithm " << algData.m_algorithm->getName());
176 return StatusCode::FAILURE;
177 }
178 }
179
180 for (auto& algData : data.m_algs)
181 {
182 try
183 {
184 // This will skip `postExecute` for all algorithms that called
185 // `setFilterPassed(false)` or that were skipped because of a
186 // prior algorithm calling `setFilterPassed(false)`.
187 if (algData.m_wasSkipped)
188 {
189 algData.m_wasSkipped = false;
190 continue;
191 }
192 if (algData.m_algorithm->postExecute().isFailure())
193 {
194 ANA_MSG_ERROR ("while calling postExecute() on algorithm " << algData.m_algorithm->getName());
195 return StatusCode::FAILURE;
196 }
197 } catch (...)
198 {
199 Detail::report_exception (std::current_exception());
200 ANA_MSG_ERROR ("while calling postExecute() on algorithm " << algData.m_algorithm->getName());
201 return StatusCode::FAILURE;
202 }
203 }
204
205 return StatusCode::SUCCESS;
206 }
207 }
208}
#define endmsg
#define ANA_MSG_ERROR(xmsg,...)
Macro printing error messages.
#define F(x, y, z)
Definition MD5.cxx:112
bool m_initialized
whether Algorithm::initialize has been called
void report_exception(std::exception_ptr eptr)
print out the currently evaluated exception
This module defines the arguments passed from the BATCH driver to the BATCH worker.
::StatusCode StatusCode
StatusCode definition for legacy code.
all the external components an algorithm needs before initialization (in EventLoop)
IHistogramWorker * m_histogramWorker
all the data a worker tracks for an individual algorithm
the data the EventLoop core classes are sharing with the Module implementation
Definition ModuleData.h:64
MsgStream & msg
Definition testRead.cxx:32