The Gaudi Framework  v33r1 (b1225454)
Sequence.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
12 #include <Gaudi/Sequence.h>
13 
14 using Gaudi::Sequence;
15 
16 namespace {
17  template <StatusCode ( Gaudi::Algorithm::*f )(), typename C>
18  bool for_algorithms( C& c ) {
19  return std::accumulate( std::begin( c ), std::end( c ), true,
20  []( bool b, Gaudi::Algorithm* a ) { return ( a->*f )().isSuccess() && b; } );
21  }
22 } // namespace
23 
24 StatusCode Sequence::initialize() {
26 
27  // initialize sub-algorithms
28  if ( !for_algorithms<&Algorithm::sysInitialize>( m_subAlgms ) ) {
29  error() << "error initializing one or several sub-algorithms of Sequence " << name() << endmsg;
30  return StatusCode::FAILURE;
31  }
32 
33  return StatusCode::SUCCESS;
34 }
35 
37  // Bypass the finalialization if the algorithm hasn't been initilized.
38  // Note: this check is also in Gaudi::Algorithm::sysFinalize
40 
41  // finalize sub-algorithms
42  if ( !for_algorithms<&Algorithm::sysFinalize>( m_subAlgms ) ) {
43  error() << "error finalizing one or several sub-algorithms of Sequence " << name() << endmsg;
44  return StatusCode::FAILURE;
45  }
46 
47  // Release all sub-algorithms
48  for ( auto& it : m_subAlgms ) it->release();
49  m_subAlgms.clear();
50 
51  return Algorithm::finalize();
52 }
53 
55  if ( !Algorithm::start() ) return StatusCode::FAILURE;
56 
57  // start sub-algorithms
58  if ( !for_algorithms<&Algorithm::sysStart>( m_subAlgms ) ) {
59  error() << "error starting one or several sub-algorithms of Sequence " << name() << endmsg;
60  return StatusCode::FAILURE;
61  }
62 
63  return StatusCode::SUCCESS;
64 }
65 
67  // stop sub-algorithms
68  if ( !for_algorithms<&Algorithm::sysStop>( m_subAlgms ) ) {
69  error() << "error stopping one or several sub-algorithms of Sequence " << name() << endmsg;
70  return StatusCode::FAILURE;
71  }
72 
73  return Algorithm::stop();
74 }
75 
77  // re-initialize sub-algorithms
78  if ( !for_algorithms<&Algorithm::sysReinitialize>( m_subAlgms ) ) {
79  error() << "error re-initializing one or several sub-algorithms of Sequence " << name() << endmsg;
80  return StatusCode::FAILURE;
81  }
82 
83  return Algorithm::reinitialize();
84 }
85 
87  // re-start sub-algorithms
88  if ( !for_algorithms<&Algorithm::sysRestart>( m_subAlgms ) ) {
89  error() << "error re-restarting one or several sub-algorithms of Sequence " << name() << endmsg;
90  return StatusCode::FAILURE;
91  }
92 
93  return Algorithm::restart();
94 }
95 
98 
99  // loop through sub-algs
100  for ( auto alg : *subAlgorithms() ) vis->visit( alg );
101 }
102 
104 
106 
109  if ( !am ) return StatusCode::FAILURE;
110 
111  // Maybe modify the AppMgr interface to return Algorithm* ??
112  IAlgorithm* tmp;
113  StatusCode sc = am->createAlgorithm( type, name, tmp );
114  if ( sc.isFailure() ) return StatusCode::FAILURE;
115 
116  try {
117  pSubAlgorithm = dynamic_cast<Algorithm*>( tmp );
118  m_subAlgms.push_back( pSubAlgorithm );
119  } catch ( ... ) { sc = StatusCode::FAILURE; }
120  return sc;
121 }
StatusCode restart() override
the default (empty) implementation of IStateful::restart() method
Definition: Algorithm.cpp:534
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:181
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
T end(T... args)
SmartIF< ISvcLocator > & serviceLocator() const override
The standard service locator.
Definition: Algorithm.cpp:593
StatusCode stop() override
System stop.
Definition: Sequence.cpp:66
STL class.
StatusCode reinitialize() override
Reinitialization method invoked by the framework.
Definition: Sequence.cpp:76
virtual void visit(const IDataHandleHolder *)=0
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:179
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode createSubAlgorithm(const std::string &type, const std::string &name, Algorithm *&pSubAlg)
Create a sub algorithm.
Definition: Sequence.cpp:107
std::vector< Algorithm * > m_subAlgms
Sub algorithms.
Definition: Sequence.h:84
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
const std::string & type() const override
The type of the algorithm object.
Definition: Algorithm.h:166
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:185
StatusCode finalize() override
System finalization.
Definition: Sequence.cpp:36
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:38
StatusCode reinitialize() override
the default (empty) implementation of IStateful::reinitialize() method
Definition: Algorithm.cpp:514
void acceptDHVisitor(IDataHandleVisitor *) const override
Definition: Sequence.cpp:96
void acceptDHVisitor(IDataHandleVisitor *) const override
Definition: Algorithm.cpp:199
T begin(T... args)
const std::vector< Algorithm * > * subAlgorithms() const
List of sub-algorithms. Returns a pointer to a vector of (sub) Algorithms.
Definition: Sequence.cpp:103
StatusCode start() override
System start.
Definition: Sequence.cpp:54
StatusCode restart() override
Restart method invoked by the framework.
Definition: Sequence.cpp:86
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:89
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition: Algorithm.h:191
virtual StatusCode createAlgorithm(std::string algtype, std::string algname, IAlgorithm *&alg, bool managed=false, bool checkIfExists=true)=0
Create an instance of a algorithm type that has been declared beforehand and assigns to it a name.
bool isFailure() const
Definition: StatusCode.h:145
T accumulate(T... args)
bool isEnabled() const override
Is this algorithm enabled or disabled?
Definition: Algorithm.cpp:559
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:183
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:549