ATLAS Offline Software
AlgorithmWrapper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 
9 //
10 // includes
11 //
12 
14 
17 #include <EventLoop/Algorithm.h>
18 #include <EventLoop/IWorker.h>
19 #include <RootCoreUtils/Assert.h>
20 #include <RootCoreUtils/ThrowMsg.h>
21 #include <TTree.h>
22 
23 //
24 // method implementations
25 //
26 
27 namespace EL
28 {
30  testInvariant () const
31  {
32  }
33 
34 
35 
37  AlgorithmWrapper (std::unique_ptr<Algorithm>&& val_algorithm)
38  : m_algorithm (std::move (val_algorithm))
39  {
40  RCU_NEW_INVARIANT (this);
41  }
42 
43 
44 
45  std::string_view AlgorithmWrapper ::
46  getName () const
47  {
48  RCU_READ_INVARIANT (this);
49  return m_algorithm->GetName();
50  }
51 
52 
53 
55  hasName (const std::string& name) const
56  {
57  RCU_READ_INVARIANT (this);
58  return m_algorithm->hasName (name);
59  }
60 
61 
62 
63  std::unique_ptr<IAlgorithmWrapper> AlgorithmWrapper ::
64  makeClone() const
65  {
66  using namespace msgAlgorithmConfig;
67  RCU_READ_INVARIANT (this);
68 
69  std::unique_ptr<Algorithm> myalgorithm
70  (dynamic_cast<Algorithm*>(m_algorithm->Clone ()));
71  if (myalgorithm == nullptr)
72  RCU_THROW_MSG ("failed to clone algorithm " + std::string (m_algorithm->GetName()));
73  return std::make_unique<AlgorithmWrapper> (std::move (myalgorithm));
74  }
75 
76 
77 
80  {
81  RCU_READ_INVARIANT (this);
82  return m_algorithm.get();
83  }
84 
85 
86 
88  initialize (const AlgorithmWorkerData& workerData)
89  {
90  using namespace msgAlgorithmConfig;
91  RCU_CHANGE_INVARIANT (this);
92 
93  m_algorithm->m_wk = workerData.m_wk;
94  m_algorithm->m_evtStorePtr = workerData.m_evtStore;
95  if (m_algorithm->histInitialize().isFailure())
96  {
97  ANA_MSG_ERROR ("failed to call histInitialize() on algorithm: " << m_algorithm->name());
98  return StatusCode::FAILURE;
99  }
100  return StatusCode::SUCCESS;
101  }
102 
103 
104 
106  execute ()
107  {
108  using namespace msgAlgorithmConfig;
109  RCU_CHANGE_INVARIANT (this);
110 
111  if (m_algorithm->execute().isFailure())
112  {
113  ANA_MSG_ERROR ("failed to call execute() on algorithm: " << m_algorithm->name());
114  return StatusCode::FAILURE;
115  }
116  return StatusCode::SUCCESS;
117  }
118 
119 
120 
122  postExecute ()
123  {
124  using namespace msgAlgorithmConfig;
125  RCU_CHANGE_INVARIANT (this);
126 
127  if (m_algorithm->postExecute().isFailure())
128  {
129  ANA_MSG_ERROR ("failed to call postExecute() on algorithm: " << m_algorithm->name());
130  return StatusCode::FAILURE;
131  }
132  return StatusCode::SUCCESS;
133  }
134 
135 
136 
138  finalize ()
139  {
140  using namespace msgAlgorithmConfig;
141  RCU_CHANGE_INVARIANT (this);
142 
143  if (m_isInitialized == true)
144  {
145  if (m_algorithm->finalize().isFailure())
146  {
147  ANA_MSG_ERROR ("failed to call finalize() on algorithm: " << m_algorithm->name());
148  return StatusCode::FAILURE;
149  }
150  }
151  if (m_algorithm->histFinalize().isFailure())
152  {
153  ANA_MSG_ERROR ("failed to call histFinalize() on algorithm: " << m_algorithm->name());
154  return StatusCode::FAILURE;
155  }
156  return StatusCode::SUCCESS;
157  }
158 
159 
160 
162  fileExecute ()
163  {
164  using namespace msgAlgorithmConfig;
165  RCU_CHANGE_INVARIANT (this);
166 
167  if (m_algorithm->fileExecute().isFailure())
168  {
169  ANA_MSG_ERROR ("failed to call fileExecute() on algorithm: " << m_algorithm->name());
170  return StatusCode::FAILURE;
171  }
172  return StatusCode::SUCCESS;
173  }
174 
175 
176 
179  {
180  using namespace msgAlgorithmConfig;
181  RCU_CHANGE_INVARIANT (this);
182 
183  if (m_algorithm->changeInput(m_firstFile).isFailure())
184  {
185  ANA_MSG_ERROR ("failed to call changeInput() on algorithm: " << m_algorithm->name());
186  return StatusCode::FAILURE;
187  }
188  m_firstFile = false;
189  if (m_isInitialized == false &&
190  m_algorithm->m_wk->tree() != nullptr &&
191  m_algorithm->m_wk->tree()->GetEntries() > 0)
192  {
193  if (m_algorithm->initialize().isFailure())
194  {
195  ANA_MSG_ERROR ("failed to call initialize() on algorithm: " << m_algorithm->name());
196  return StatusCode::FAILURE;
197  }
198  m_isInitialized = true;
199  }
200  return StatusCode::SUCCESS;
201  }
202 
203 
204 
206  endInputFile ()
207  {
208  using namespace msgAlgorithmConfig;
209  RCU_CHANGE_INVARIANT (this);
210 
211  if (m_algorithm->endOfFile().isFailure())
212  {
213  ANA_MSG_ERROR ("failed to call endOfFile() on algorithm: " << m_algorithm->name());
214  return StatusCode::FAILURE;
215  }
216  return StatusCode::SUCCESS;
217  }
218 }
EL::AlgorithmWrapper::initialize
virtual StatusCode initialize(const AlgorithmWorkerData &workerData) override
call initialize on the algorithm
Definition: AlgorithmWrapper.cxx:88
EL::AlgorithmWorkerData::m_evtStore
asg::SgTEvent * m_evtStore
Definition: AlgorithmWorkerData.h:35
EL::AlgorithmWrapper::makeClone
virtual std::unique_ptr< IAlgorithmWrapper > makeClone() const override
make a clone of this algorithm
Definition: AlgorithmWrapper.cxx:64
EL::AlgorithmWrapper::finalize
virtual StatusCode finalize() override
call finalize on the algorithm
Definition: AlgorithmWrapper.cxx:138
EL::AlgorithmWrapper::beginInputFile
virtual ::StatusCode beginInputFile() override
call beginInputFile on the algorithm
Definition: AlgorithmWrapper.cxx:178
EL::AlgorithmWorkerData::m_wk
IWorker * m_wk
Definition: AlgorithmWorkerData.h:39
EL::AlgorithmWrapper::m_firstFile
bool m_firstFile
whether this is the first file we encounter
Definition: AlgorithmWrapper.h:76
EL::AlgorithmWrapper::execute
virtual StatusCode execute() override
call execute on the algorithm
Definition: AlgorithmWrapper.cxx:106
EL::AlgorithmWrapper::getName
virtual std::string_view getName() const override
Definition: AlgorithmWrapper.cxx:46
EL::AlgorithmWrapper::fileExecute
virtual ::StatusCode fileExecute() override
call fileExecute on the algorithm
Definition: AlgorithmWrapper.cxx:162
EL::AlgorithmWrapper::testInvariant
void testInvariant() const
test the invariant of this object
Definition: AlgorithmWrapper.cxx:30
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
EL::AlgorithmWrapper::endInputFile
virtual ::StatusCode endInputFile() override
call endInputFile on the algorithm
Definition: AlgorithmWrapper.cxx:206
Assert.h
EL::AlgorithmWrapper::m_isInitialized
bool m_isInitialized
whether Algorithm::initialize has been called
Definition: AlgorithmWrapper.h:79
EL::AlgorithmWrapper::getLegacyAlg
virtual Algorithm * getLegacyAlg() override
get the legacy algorithm, if we wrap one
Definition: AlgorithmWrapper.cxx:79
EL::AlgorithmWrapper::m_algorithm
std::unique_ptr< Algorithm > m_algorithm
the actual algorithm
Definition: AlgorithmWrapper.h:73
EL::AlgorithmWrapper::postExecute
virtual StatusCode postExecute() override
call postExecute on the algorithm
Definition: AlgorithmWrapper.cxx:122
AlgorithmWrapper.h
EL::Algorithm
Definition: Algorithm.h:22
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Algorithm.h
EL
This module defines the arguments passed from the BATCH driver to the BATCH worker.
Definition: AlgorithmWorkerData.h:24
MessageCheck.h
AlgorithmWorkerData.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ThrowMsg.h
EL::AlgorithmWorkerData
all the external components an algorithm needs before initialization (in EventLoop)
Definition: AlgorithmWorkerData.h:34
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
RCU_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58
EL::AlgorithmWrapper::hasName
virtual bool hasName(const std::string &name) const override
whether this algorithm has the given name
Definition: AlgorithmWrapper.cxx:55
EL::AlgorithmWrapper::AlgorithmWrapper
AlgorithmWrapper()
standard default constructor for serialization
Definition: AlgorithmWrapper.h:33
IWorker.h
RCU_READ_INVARIANT
#define RCU_READ_INVARIANT(x)
Definition: Assert.h:229
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233