Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GaudiSequencer.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 \***********************************************************************************/
11 // Include files
12 #include <initializer_list>
13 #include <sstream>
14 #include <tuple>
15 
16 // from Gaudi
21 
22 #include "GaudiCommon.icpp"
23 template class GaudiCommon<Gaudi::Sequence>;
24 
25 namespace {
26 
27  bool isDefault( const std::string& s ) { return s.empty(); }
28 
29 } // namespace
30 
31 //-----------------------------------------------------------------------------
32 // Implementation file for class : GaudiSequencer
33 //
34 // 2004-05-13 : Olivier Callot
35 //-----------------------------------------------------------------------------
36 
37 //=============================================================================
38 // Initialisation. Check parameters
39 //=============================================================================
41  // Note: not calling base class initialize because we want to reimplement the loop over sub algs
42  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialise" << endmsg;
43 
44  auto status = decodeNames();
45  if ( !status.isSuccess() ) return status;
46 
47  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
48 
49  if ( m_timerTool->globalTiming() ) m_measureTime = true;
50 
51  if ( m_measureTime ) {
54  } else {
56  m_timerTool = nullptr;
57  }
58 
59  //== Initialize the algorithms
60  for ( auto& entry : m_entries ) {
61  if ( m_measureTime ) { entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) ); }
62 
63  status = entry.algorithm()->sysInitialize();
64  if ( !status.isSuccess() ) { return Error( "Can not initialize " + entry.algorithm()->name(), status ); }
65  }
67 
68  return StatusCode::SUCCESS;
69 }
70 
71 //=============================================================================
72 // Main execution
73 //=============================================================================
75 
77 
78  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
79 
81 
82  bool seqPass = !m_modeOR; // for OR, result will be false, unless (at least) one is true
83  // for AND, result will be true, unless (at least) one is false
84  // also see comment below ....
85 
86  for ( auto& entry : m_entries ) {
87  Gaudi::Algorithm* myAlg = entry.algorithm();
88  if ( !myAlg->isEnabled() ) continue;
89  if ( myAlg->execState( ctx ).state() != AlgExecState::State::Done ) {
90 
91  if ( m_measureTime ) m_timerTool->start( entry.timer() );
92  result = myAlg->sysExecute( ctx );
93  if ( m_measureTime ) m_timerTool->stop( entry.timer() );
94  if ( !result.isSuccess() ) break; //== Abort and return bad status
95  }
96  //== Check the returned status
97  if ( !m_ignoreFilter ) {
98  bool passed = myAlg->execState( ctx ).filterPassed();
99  if ( msgLevel( MSG::VERBOSE ) )
100  verbose() << "Algorithm " << myAlg->name() << " returned filter passed " << ( passed ? "true" : "false" )
101  << endmsg;
102  if ( entry.reverse() ) passed = !passed;
103 
104  //== indicate our own result. For OR, exit as soon as true.
105  // If no more, will exit with false.
106  //== for AND, exit as soon as false. Else, will be true (default)
107 
108  // if not short-circuiting, make sure we latch iPass to 'true' in
109  // OR mode (i.e. it is sufficient for one item to be true in order
110  // to be true at the end, and thus we start out at 'false'), and latch
111  // to 'false' in AND mode (i.e. it is sufficient for one item to
112  // be false to the false in the end, and thus we start out at 'true')
113  // -- i.e. we should not just blindly return the 'last' passed status!
114 
115  // or to put it another way: in OR mode, we don't care about things
116  // which are false, as they leave our current state alone (provided
117  // we stared as 'false'!), and in AND mode, we keep our current
118  // state until someone returns 'false' (provided we started as 'true')
119  if ( m_modeOR ? passed : !passed ) {
120  seqPass = passed;
121  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is now " << ( seqPass ? "true" : "false" ) << endmsg;
122  if ( m_shortCircuit ) break;
123  }
124  }
125  }
126  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is " << ( seqPass ? "true" : "false" ) << endmsg;
127  auto& state = execState( ctx );
128  if ( !m_ignoreFilter && !m_entries.empty() ) state.setFilterPassed( m_invert ? !seqPass : seqPass );
129  state.setState( AlgExecState::State::Done );
130 
132 
133  return m_returnOK ? ( result.ignore(), StatusCode::SUCCESS ) : result;
134 }
135 
136 //=========================================================================
137 // Decode the input names and fills the m_algs vector.
138 //=========================================================================
141  m_entries.clear();
142 
143  //== Get the "Context" option if in the file...
144 
145  //= Get the Application manager, to see if algorithm exist
146  auto appMgr = service<IAlgManager>( "ApplicationMgr" );
147  for ( const auto& item : m_names.value() ) {
149  const std::string& theName = typeName.name();
150  const std::string& theType = typeName.type();
151 
152  //== Check wether the specified algorithm already exists. If not, create it
154  SmartIF<IAlgorithm> myIAlg = appMgr->algorithm( typeName, false ); // do not create it now
155  if ( !myIAlg ) {
156  Gaudi::Algorithm* myAlg = nullptr;
157  result = createSubAlgorithm( theType, theName, myAlg );
158  if ( myAlg ) {
159  // Override the default values of the special properties Context and RootInTES,
160  // which will be superseded by the explicit value in options (if present).
161  if ( !isDefault( context() ) && myAlg->hasProperty( "Context" ) ) {
162  myAlg->setProperty( "Context", context() ).ignore();
163  }
164  if ( !isDefault( rootInTES() ) && myAlg->hasProperty( "RootInTES" ) ) {
165  myAlg->setProperty( "RootInTES", rootInTES() ).ignore();
166  }
167  }
168  myIAlg = myAlg; // ensure that myIAlg.isValid() from here onwards!
169  } else {
170  Gaudi::Algorithm* myAlg = dynamic_cast<Gaudi::Algorithm*>( myIAlg.get() );
171  if ( myAlg ) {
172  subAlgorithms()->push_back( myAlg );
173  // when the algorithm is not created, the ref count is short by one, so we have to fix it.
174  myAlg->addRef();
175  }
176  }
177 
178  // propagate the sub-algorithm into own state.
179  if ( result.isSuccess() && Gaudi::StateMachine::INITIALIZED <= FSMState() && myIAlg &&
180  Gaudi::StateMachine::INITIALIZED > myIAlg->FSMState() ) {
181  StatusCode sc = myIAlg->sysInitialize();
182  if ( sc.isFailure() ) result = sc;
183  }
184 
185  // propagate the sub-algorithm into own state.
186  if ( result.isSuccess() && Gaudi::StateMachine::RUNNING <= FSMState() && myIAlg &&
187  Gaudi::StateMachine::RUNNING > myIAlg->FSMState() ) {
188  StatusCode sc = myIAlg->sysStart();
189  if ( sc.isFailure() ) result = sc;
190  }
191 
192  //== Is it an Algorithm ? Strange test...
193  if ( result.isSuccess() ) {
194  // TODO: (MCl) it is possible to avoid the dynamic_cast in most of the
195  // cases by keeping the result of createSubAlgorithm.
196  Gaudi::Algorithm* myAlg = dynamic_cast<Gaudi::Algorithm*>( myIAlg.get() );
197  if ( myAlg ) {
198  // Note: The reference counting is kept by the system of sub-algorithms
199  m_entries.emplace_back( myAlg );
200  if ( msgLevel( MSG::DEBUG ) ) debug() << "Added algorithm " << theName << endmsg;
201  } else {
202  warning() << theName << " is not a Gaudi::Algorithm - failed dynamic_cast" << endmsg;
203  final = StatusCode::FAILURE;
204  }
205  } else {
206  warning() << "Unable to find or create " << theName << endmsg;
207  final = result;
208  }
209  }
210 
211  //== Print the list of algorithms
212  MsgStream& msg = info();
213  if ( m_modeOR ) msg << "OR ";
214  msg << "Member list: ";
216  []( auto& os, const AlgorithmEntry& e ) -> decltype( auto ) {
218  std::string typ = alg->type();
219  os << typ;
220  if ( alg->name() != typ ) os << "/" << alg->name();
221  return os;
222  } );
223  if ( !isDefault( context() ) ) msg << ", with context '" << context() << "'";
224  if ( !isDefault( rootInTES() ) ) msg << ", with rootInTES '" << rootInTES() << "'";
225  msg << endmsg;
226 
227  return final;
228 }
229 
230 //=========================================================================
231 // Interface for the Property manager
232 //=========================================================================
234  // no action for not-yet initialized sequencer
235  if ( Gaudi::StateMachine::INITIALIZED > FSMState() ) return; // RETURN
236 
237  decodeNames().ignore();
238 
239  if ( !m_measureTime ) return; // RETURN
240 
241  // add the entries into timer table:
242 
243  if ( !m_timerTool ) { m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" ); }
244 
245  if ( m_timerTool->globalTiming() ) m_measureTime = true;
246 
249 
250  for ( auto& entry : m_entries ) { entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) ); }
251 
253 }
254 
256  if ( m_invert ) os << "~";
257  // the default filterpass value for an empty sequencer depends on ModeOR
258  if ( m_entries.empty() ) return os << ( ( !m_modeOR ) ? "CFTrue" : "CFFalse" );
259 
260  // if we have only one element, we do not need a name
261  if ( m_entries.size() > 1 ) os << "seq(";
262 
263  const auto op = m_modeOR ? " | " : " & ";
264  const auto last = end( m_entries );
265  const auto first = begin( m_entries );
266  for ( auto iterator = first; iterator != last; ++iterator ) {
267  if ( iterator != first ) os << op;
268  if ( iterator->reverse() ) os << "~";
269  iterator->algorithm()->toControlFlowExpression( os );
270  }
271 
272  if ( m_entries.size() > 1 ) os << ")";
273  return os;
274 }
275 
276 // ============================================================================
279 
280  IAlgContextSvc* algCtx = nullptr;
281  if ( registerContext() ) { algCtx = contextSvc(); }
282  // Lock the context
283  Gaudi::Utils::AlgContext cnt( this, algCtx, ctx );
284 
285  // Do not execute if one or more of the m_vetoObjs exist in TES
286  const auto it = find_if( begin( m_vetoObjs ), end( m_vetoObjs ),
287  [&]( const std::string& loc ) { return this->exist<DataObject>( evtSvc(), loc ); } );
288  if ( it != end( m_vetoObjs ) ) {
289  if ( msgLevel( MSG::DEBUG ) ) debug() << *it << " found, skipping event " << endmsg;
290  return sc;
291  }
292 
293  // Execute if m_requireObjs is empty
294  // or if one or more of the m_requireObjs exist in TES
295  bool doIt = m_requireObjs.empty() ||
296  any_of( begin( m_requireObjs ), end( m_requireObjs ),
297  [&]( const std::string& loc ) { return this->exist<DataObject>( evtSvc(), loc ); } );
298 
299  // execute the generic method:
300  if ( doIt ) sc = GaudiCommon<Gaudi::Sequence>::sysExecute( ctx );
301  return sc;
302 }
303 //=============================================================================
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Gaudi::Algorithm::sysExecute
StatusCode sysExecute(const EventContext &ctx) override
The actions to be performed by the algorithm on an event.
Definition: Algorithm.cpp:327
GaudiSequencer::decodeNames
StatusCode decodeNames()
Decode a vector of string.
Definition: GaudiSequencer.cpp:139
IAlgManager.h
GaudiCommon.icpp
GaudiSequencer::AlgorithmEntry::algorithm
Gaudi::Algorithm * algorithm() const
Definition: GaudiSequencer.h:58
std::string
STL class.
GaudiSequencer::m_timerTool
ISequencerTimerTool * m_timerTool
Pointer to the timer tool.
Definition: GaudiSequencer.h:91
Gaudi::Algorithm::contextSvc
SmartIF< IAlgContextSvc > & contextSvc() const
get Algorithm Context Service
Definition: Algorithm.cpp:568
GaudiSequencer::sysExecute
StatusCode sysExecute(const EventContext &ctx) override
Definition: GaudiSequencer.cpp:277
AlgExecState::state
State state() const
Definition: IAlgExecStateSvc.h:42
GaudiSequencer::m_names
Gaudi::Property< std::vector< std::string > > m_names
Definition: GaudiSequencer.h:80
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:528
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
Gaudi::Sequence::createSubAlgorithm
StatusCode createSubAlgorithm(const std::string &type, const std::string &name, Algorithm *&pSubAlg)
Create a sub algorithm.
Definition: Sequence.cpp:107
gaudirun.s
string s
Definition: gaudirun.py:348
ISequencerTimerTool::globalTiming
virtual bool globalTiming()=0
returns the flag telling that global timing is wanted
PropertyHolder::setProperty
StatusCode setProperty(const std::string &name, const Gaudi::Details::PropertyBase &p) override
set the property from another property with a different name
Definition: PropertyHolder.h:157
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
GaudiSequencer::m_invert
Gaudi::Property< bool > m_invert
Definition: GaudiSequencer.h:88
ISequencerTimerTool::increaseIndent
virtual void increaseIndent()=0
Increase the indentation of the name.
ISequencerTimerTool::decreaseIndent
virtual void decreaseIndent()=0
Decrease the indentation of the name.
GaudiSequencer::m_timer
int m_timer
Timer number for the sequencer.
Definition: GaudiSequencer.h:92
GaudiSequencer::m_entries
std::vector< AlgorithmEntry > m_entries
List of algorithms to process.
Definition: GaudiSequencer.h:90
CommonMessaging< implements< IAlgorithm, IDataHandleHolder, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
basic.alg
alg
Definition: basic.py:15
GaudiSequencer::m_shortCircuit
Gaudi::Property< bool > m_shortCircuit
Definition: GaudiSequencer.h:87
Gaudi::Algorithm::isEnabled
bool isEnabled() const override
Is this algorithm enabled or disabled?
Definition: Algorithm.cpp:538
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:588
GaudiSequencer::execute
StatusCode execute(const EventContext &ctx) const override
Definition: GaudiSequencer.cpp:74
StatusCode
Definition: StatusCode.h:65
GaudiSequencer::m_modeOR
Gaudi::Property< bool > m_modeOR
Definition: GaudiSequencer.h:83
Gaudi::Algorithm::FSMState
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition: Algorithm.h:190
std::ostream
STL class.
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
Gaudi::Algorithm::execState
AlgExecState & execState(const EventContext &ctx) const override
reference to AlgExecState of Alg
Definition: Algorithm.cpp:540
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
Gaudi::Utils::AlgContext
Definition: IAlgContextSvc.h:84
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:239
GaudiSequencer::m_returnOK
Gaudi::Property< bool > m_returnOK
Definition: GaudiSequencer.h:86
SmartIF< IAlgorithm >
genconfuser.verbose
verbose
Definition: genconfuser.py:29
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
GaudiCommon< Gaudi::Sequence >::release
StatusCode release(const IInterface *interface) const
Manual forced (and 'safe') release of the active tool or service.
Definition: GaudiCommon.icpp:176
Gaudi::StateMachine::RUNNING
@ RUNNING
Definition: StateMachine.h:26
MsgStream
Definition: MsgStream.h:34
ISequencerTimerTool::start
virtual void start(int index)=0
start the counter, i.e.
GaudiCommon< Gaudi::Sequence >::context
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:676
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
ThreadLocalContext.h
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
Gaudi::Algorithm::evtSvc
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:248
IAlgTool::sysInitialize
virtual StatusCode sysInitialize()=0
Initialization of the Tool.
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
GaudiSequencer::m_requireObjs
Gaudi::Property< std::vector< std::string > > m_requireObjs
Definition: GaudiSequencer.h:77
GaudiSequencer::m_vetoObjs
Gaudi::Property< std::vector< std::string > > m_vetoObjs
Definition: GaudiSequencer.h:75
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:25
EventContext
Definition: EventContext.h:34
FixTESPath< Gaudi::Sequence >::rootInTES
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: FixTESPath.h:61
IAlgContextSvc
Definition: IAlgContextSvc.h:33
Gaudi::Sequence::subAlgorithms
const std::vector< Algorithm * > * subAlgorithms() const
List of sub-algorithms. Returns a pointer to a vector of (sub) Algorithms.
Definition: Sequence.cpp:103
AlgExecState::filterPassed
bool filterPassed() const
Definition: IAlgExecStateSvc.h:41
GaudiSequencer::AlgorithmEntry
Definition: GaudiSequencer.h:51
ISequencerTimerTool.h
GaudiSequencer::toControlFlowExpression
std::ostream & toControlFlowExpression(std::ostream &os) const override
Produce string represention of the control flow expression.
Definition: GaudiSequencer.cpp:255
IOTest.end
end
Definition: IOTest.py:123
GaudiSequencer::m_measureTime
Gaudi::Property< bool > m_measureTime
Definition: GaudiSequencer.h:85
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
GaudiSequencer::initialize
StatusCode initialize() override
Definition: GaudiSequencer.cpp:40
GaudiSequencer.h
compareRootHistos.state
state
Definition: compareRootHistos.py:498
Gaudi::Algorithm::registerContext
bool registerContext() const
register for Algorithm Context Service?
Definition: Algorithm.h:295
IOTest.appMgr
appMgr
Definition: IOTest.py:103
GaudiSequencer::membershipHandler
void membershipHandler()
for asynchronous changes in the list of algorithms
Definition: GaudiSequencer.cpp:233
GaudiCommon< Gaudi::Sequence >::Error
StatusCode Error(std::string_view msg, const StatusCode st=StatusCode::FAILURE, const size_t mx=10) const
Print the error message and return with the given StatusCode.
Definition: GaudiCommon.icpp:256
ISequencerTimerTool::stop
virtual double stop(int index)=0
stop the counter, return the elapsed time
GaudiUtils::details::ostream_joiner
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:73
GaudiSequencer::m_ignoreFilter
Gaudi::Property< bool > m_ignoreFilter
Definition: GaudiSequencer.h:84
PropertyHolder::hasProperty
bool hasProperty(std::string_view name) const override
Return true if we have a property with the given name.
Definition: PropertyHolder.h:227
ISequencerTimerTool::addTimer
virtual int addTimer(const std::string &name)=0
add a timer entry with the specified name
GaudiCommon< Gaudi::Sequence >