ATLAS Offline Software
AlgorithmStateModule.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 
9 //
10 // includes
11 //
12 
14 
17 #include <AsgTools/SgTEvent.h>
18 #include <EventLoop/MessageCheck.h>
19 #include <EventLoop/ModuleData.h>
20 #include <EventLoop/Worker.h>
21 #include <RootCoreUtils/Assert.h>
22 #include <TTree.h>
23 #include <exception>
24 
25 //
26 // method implementations
27 //
28 
29 namespace EL
30 {
31  namespace Detail
32  {
33  namespace
34  {
35  template<typename F> StatusCode
36  forAllAlgorithms (MsgStream& msg, ModuleData& data, const char *funcName, F&& func)
37  {
38  for (AlgorithmData& alg : data.m_algs)
39  {
40  try
41  {
42  typedef typename std::decay<decltype(func(alg))>::type scType__;
44  {
45  msg << MSG::ERROR << "executing " << funcName << " on algorithm " << alg->getName() << endmsg;
46  return StatusCode::FAILURE;
47  }
48  } catch (...)
49  {
50  report_exception (std::current_exception());
51  msg << MSG::ERROR << "executing " << funcName << " on algorithm " << alg->getName() << endmsg;
52  return StatusCode::FAILURE;
53  }
54  }
55  return StatusCode::SUCCESS;
56  }
57  }
58 
59 
60 
63  {
64  if (m_initialized)
65  {
66  ANA_MSG_ERROR ("getting second initialize call");
67  return StatusCode::FAILURE;
68  }
69  m_initialized = true;
70  AlgorithmWorkerData workerData;
71  workerData.m_histogramWorker = data.m_worker;
72  workerData.m_treeWorker = data.m_worker;
73  workerData.m_filterWorker = data.m_worker;
74  workerData.m_wk = data.m_worker;
75  workerData.m_evtStore = data.m_evtStore;
76  return forAllAlgorithms (msg(), data, "initialize", [&] (AlgorithmData& alg) {
77  return alg->initialize (workerData);});
78  }
79 
80 
81 
84  {
85  if (!m_initialized)
86  return StatusCode::SUCCESS;
87  if (forAllAlgorithms (msg(), data, "finalize", [&] (AlgorithmData& alg) {
88  return alg->finalize ();}).isFailure())
89  return StatusCode::FAILURE;
90  return StatusCode::SUCCESS;
91  }
92 
93 
94 
97  {
98  return forAllAlgorithms (msg(), data, "endInputFile", [&] (AlgorithmData& alg) {
99  return alg->endInputFile ();});
100  }
101 
102 
103 
106  {
107  if (!m_initialized)
108  {
109  ANA_MSG_ERROR ("algorithms have not been initialized yet");
110  return StatusCode::FAILURE;
111  }
112 
113  if (data.m_inputTree == nullptr ||
114  data.m_inputTree->GetEntries() == 0)
115  return StatusCode::SUCCESS;
116 
117  if (forAllAlgorithms (msg(), data, "changeInput", [&] (AlgorithmData& alg) {
118  return alg->beginInputFile ();}).isFailure())
119  return StatusCode::FAILURE;
120  return StatusCode::SUCCESS;
121  }
122 
123 
124 
127  {
128  return forAllAlgorithms (msg(), data, "fileExecute", [&] (AlgorithmData& alg) {
129  return alg->fileExecute ();});
130  }
131 
132 
133 
136  {
137  RCU_CHANGE_INVARIANT (this);
138 
139  data.m_skipEvent = false;
140  auto iter = data.m_algs.begin();
141  try
142  {
143  for (auto end = data.m_algs.end();
144  iter != end; ++ iter)
145  {
146  iter->m_executeCount += 1;
147  if (iter->m_algorithm->execute() == StatusCode::FAILURE)
148  {
149  ANA_MSG_ERROR ("while calling execute() on algorithm " << iter->m_algorithm->getName());
150  return StatusCode::FAILURE;
151  }
152 
153  if (data.m_skipEvent)
154  {
155  iter->m_skipCount += 1;
156  return StatusCode::SUCCESS;
157  }
158  }
159  } catch (...)
160  {
161  Detail::report_exception (std::current_exception());
162  ANA_MSG_ERROR ("while calling execute() on algorithm " << iter->m_algorithm->getName());
163  return StatusCode::FAILURE;
164  }
165 
168  try
169  {
170  for (auto jter = data.m_algs.begin(), end = iter;
171  jter != end && !data.m_skipEvent; ++ jter)
172  {
173  if (jter->m_algorithm->postExecute() == StatusCode::FAILURE)
174  {
175  ANA_MSG_ERROR ("while calling postExecute() on algorithm " << iter->m_algorithm->getName());
176  return StatusCode::FAILURE;
177  }
178  }
179  } catch (...)
180  {
181  Detail::report_exception (std::current_exception());
182  ANA_MSG_ERROR ("while calling postExecute() on algorithm " << iter->m_algorithm->getName());
183  return StatusCode::FAILURE;
184  }
185 
186  return StatusCode::SUCCESS;
187  }
188  }
189 }
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
EL::AlgorithmWorkerData::m_evtStore
asg::SgTEvent * m_evtStore
Definition: AlgorithmWorkerData.h:35
SGout2dot.alg
alg
Definition: SGout2dot.py:243
EL::Detail::AlgorithmStateModule::onNewInputFile
virtual StatusCode onNewInputFile(ModuleData &data) override
actions after opening a new input file
Definition: AlgorithmStateModule.cxx:105
asg::CheckHelper
this is an internal traits class for status codes used by the ANA_CHECK* macros
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:174
EL::Detail::ModuleData
the data the EventLoop core classes are sharing with the Module implementation
Definition: ModuleData.h:64
EL::AlgorithmWorkerData::m_wk
IWorker * m_wk
Definition: AlgorithmWorkerData.h:39
EL::AlgorithmWorkerData::m_filterWorker
IFilterWorker * m_filterWorker
Definition: AlgorithmWorkerData.h:38
EL::Detail::AlgorithmData
all the data a worker tracks for an individual algorithm
Definition: AlgorithmData.h:28
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
Assert.h
MessageCheck.h
AlgorithmStateModule.h
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
EL::Detail::AlgorithmStateModule::onInitialize
virtual StatusCode onInitialize(ModuleData &data) override
action just before algorithms are initialized
Definition: AlgorithmStateModule.cxx:62
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
EL
This module defines the arguments passed from the BATCH driver to the BATCH worker.
Definition: AlgorithmWorkerData.h:24
EL::Detail::AlgorithmStateModule::m_initialized
bool m_initialized
whether Algorithm::initialize has been called
Definition: AlgorithmStateModule.h:61
AlgorithmWorkerData.h
xAOD::DiTauJetParameters::Detail
Detail
Definition: DiTauDefs.h:38
EL::AlgorithmWorkerData::m_treeWorker
ITreeWorker * m_treeWorker
Definition: AlgorithmWorkerData.h:37
EL::AlgorithmWorkerData
all the external components an algorithm needs before initialization (in EventLoop)
Definition: AlgorithmWorkerData.h:34
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
Worker.h
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
ModuleData.h
EL::AlgorithmWorkerData::m_histogramWorker
IHistogramWorker * m_histogramWorker
Definition: AlgorithmWorkerData.h:36
EL::Detail::AlgorithmStateModule::onFileExecute
virtual StatusCode onFileExecute(ModuleData &data) override
actions just before fileExecute is called on algorithms
Definition: AlgorithmStateModule.cxx:126
EL::Detail::AlgorithmStateModule::onFinalize
virtual StatusCode onFinalize(ModuleData &data) override
actions just before algorithms are finalized
Definition: AlgorithmStateModule.cxx:83
EL::Detail::report_exception
void report_exception(std::exception_ptr eptr)
print out the currently evaluated exception
Definition: PhysicsAnalysis/D3PDTools/EventLoop/Root/MessageCheck.cxx:27
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
IAlgorithmWrapper.h
EL::Detail::AlgorithmStateModule::onExecute
virtual StatusCode onExecute(ModuleData &data) override
actions just before execute is called on algorithms
Definition: AlgorithmStateModule.cxx:135
SgTEvent.h
EL::Detail::AlgorithmStateModule::onCloseInputFile
virtual StatusCode onCloseInputFile(ModuleData &data) override
actions before closing an input file
Definition: AlgorithmStateModule.cxx:96