Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r0 (2143aa4c)
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 
76  auto timer = m_timerTool->scopedTimer( m_timer, m_measureTime );
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  auto entry_timer = m_timerTool->scopedTimer( entry.timer(), m_measureTime );
91  result = myAlg->sysExecute( ctx );
92  if ( !result.isSuccess() ) break; //== Abort and return bad status
93  }
94  //== Check the returned status
95  if ( !m_ignoreFilter ) {
96  bool passed = myAlg->execState( ctx ).filterPassed();
97  if ( msgLevel( MSG::VERBOSE ) )
98  verbose() << "Algorithm " << myAlg->name() << " returned filter passed " << ( passed ? "true" : "false" )
99  << endmsg;
100  if ( entry.reverse() ) passed = !passed;
101 
102  //== indicate our own result. For OR, exit as soon as true.
103  // If no more, will exit with false.
104  //== for AND, exit as soon as false. Else, will be true (default)
105 
106  // if not short-circuiting, make sure we latch iPass to 'true' in
107  // OR mode (i.e. it is sufficient for one item to be true in order
108  // to be true at the end, and thus we start out at 'false'), and latch
109  // to 'false' in AND mode (i.e. it is sufficient for one item to
110  // be false to the false in the end, and thus we start out at 'true')
111  // -- i.e. we should not just blindly return the 'last' passed status!
112 
113  // or to put it another way: in OR mode, we don't care about things
114  // which are false, as they leave our current state alone (provided
115  // we stared as 'false'!), and in AND mode, we keep our current
116  // state until someone returns 'false' (provided we started as 'true')
117  if ( m_modeOR ? passed : !passed ) {
118  seqPass = passed;
119  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is now " << ( seqPass ? "true" : "false" ) << endmsg;
120  if ( m_shortCircuit ) break;
121  }
122  }
123  }
124  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is " << ( seqPass ? "true" : "false" ) << endmsg;
125  auto& state = execState( ctx );
126  if ( !m_ignoreFilter && !m_entries.empty() ) state.setFilterPassed( m_invert ? !seqPass : seqPass );
127  state.setState( AlgExecState::State::Done );
128 
129  return m_returnOK ? ( result.ignore(), StatusCode::SUCCESS ) : result;
130 }
131 
132 //=========================================================================
133 // Decode the input names and fills the m_algs vector.
134 //=========================================================================
137  m_entries.clear();
138 
139  //== Get the "Context" option if in the file...
140 
141  //= Get the Application manager, to see if algorithm exist
142  auto appMgr = service<IAlgManager>( "ApplicationMgr" );
143  for ( const auto& item : m_names.value() ) {
145  const std::string& theName = typeName.name();
146  const std::string& theType = typeName.type();
147 
148  //== Check wether the specified algorithm already exists. If not, create it
150  SmartIF<IAlgorithm> myIAlg = appMgr->algorithm( typeName, false ); // do not create it now
151  if ( !myIAlg ) {
152  Gaudi::Algorithm* myAlg = nullptr;
153  result = createSubAlgorithm( theType, theName, myAlg );
154  if ( myAlg ) {
155  // Override the default values of the special properties Context and RootInTES,
156  // which will be superseded by the explicit value in options (if present).
157  if ( !isDefault( context() ) && myAlg->hasProperty( "Context" ) ) {
158  myAlg->setProperty( "Context", context() ).ignore();
159  }
160  if ( !isDefault( rootInTES() ) && myAlg->hasProperty( "RootInTES" ) ) {
161  myAlg->setProperty( "RootInTES", rootInTES() ).ignore();
162  }
163  }
164  myIAlg = myAlg; // ensure that myIAlg.isValid() from here onwards!
165  } else {
166  Gaudi::Algorithm* myAlg = dynamic_cast<Gaudi::Algorithm*>( myIAlg.get() );
167  if ( myAlg ) {
168  subAlgorithms()->push_back( myAlg );
169  // when the algorithm is not created, the ref count is short by one, so we have to fix it.
170  myAlg->addRef();
171  }
172  }
173 
174  // propagate the sub-algorithm into own state.
175  if ( result.isSuccess() && Gaudi::StateMachine::INITIALIZED <= FSMState() && myIAlg &&
176  Gaudi::StateMachine::INITIALIZED > myIAlg->FSMState() ) {
177  StatusCode sc = myIAlg->sysInitialize();
178  if ( sc.isFailure() ) result = sc;
179  }
180 
181  // propagate the sub-algorithm into own state.
182  if ( result.isSuccess() && Gaudi::StateMachine::RUNNING <= FSMState() && myIAlg &&
183  Gaudi::StateMachine::RUNNING > myIAlg->FSMState() ) {
184  StatusCode sc = myIAlg->sysStart();
185  if ( sc.isFailure() ) result = sc;
186  }
187 
188  //== Is it an Algorithm ? Strange test...
189  if ( result.isSuccess() ) {
190  // TODO: (MCl) it is possible to avoid the dynamic_cast in most of the
191  // cases by keeping the result of createSubAlgorithm.
192  Gaudi::Algorithm* myAlg = dynamic_cast<Gaudi::Algorithm*>( myIAlg.get() );
193  if ( myAlg ) {
194  // Note: The reference counting is kept by the system of sub-algorithms
195  m_entries.emplace_back( myAlg );
196  if ( msgLevel( MSG::DEBUG ) ) debug() << "Added algorithm " << theName << endmsg;
197  } else {
198  warning() << theName << " is not a Gaudi::Algorithm - failed dynamic_cast" << endmsg;
199  final = StatusCode::FAILURE;
200  }
201  } else {
202  warning() << "Unable to find or create " << theName << endmsg;
203  final = result;
204  }
205  }
206 
207  //== Print the list of algorithms
208  MsgStream& msg = info();
209  if ( m_modeOR ) msg << "OR ";
210  msg << "Member list: ";
212  []( auto& os, const AlgorithmEntry& e ) -> decltype( auto ) {
214  std::string typ = alg->type();
215  os << typ;
216  if ( alg->name() != typ ) os << "/" << alg->name();
217  return os;
218  } );
219  if ( !isDefault( context() ) ) msg << ", with context '" << context() << "'";
220  if ( !isDefault( rootInTES() ) ) msg << ", with rootInTES '" << rootInTES() << "'";
221  msg << endmsg;
222 
223  return final;
224 }
225 
226 //=========================================================================
227 // Interface for the Property manager
228 //=========================================================================
230  // no action for not-yet initialized sequencer
231  if ( Gaudi::StateMachine::INITIALIZED > FSMState() ) return; // RETURN
232 
233  decodeNames().ignore();
234 
235  if ( !m_measureTime ) return; // RETURN
236 
237  // add the entries into timer table:
238 
239  if ( !m_timerTool ) { m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" ); }
240 
241  if ( m_timerTool->globalTiming() ) m_measureTime = true;
242 
245 
246  for ( auto& entry : m_entries ) { entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) ); }
247 
249 }
250 
252  if ( m_invert ) os << "~";
253  // the default filterpass value for an empty sequencer depends on ModeOR
254  if ( m_entries.empty() ) return os << ( ( !m_modeOR ) ? "CFTrue" : "CFFalse" );
255 
256  // if we have only one element, we do not need a name
257  if ( m_entries.size() > 1 ) os << "seq(";
258 
259  const auto op = m_modeOR ? " | " : " & ";
260  const auto last = end( m_entries );
261  const auto first = begin( m_entries );
262  for ( auto iterator = first; iterator != last; ++iterator ) {
263  if ( iterator != first ) os << op;
264  if ( iterator->reverse() ) os << "~";
265  iterator->algorithm()->toControlFlowExpression( os );
266  }
267 
268  if ( m_entries.size() > 1 ) os << ")";
269  return os;
270 }
271 
272 // ============================================================================
275 
276  IAlgContextSvc* algCtx = nullptr;
277  if ( registerContext() ) { algCtx = contextSvc(); }
278  // Lock the context
279  Gaudi::Utils::AlgContext cnt( this, algCtx, ctx );
280 
281  // Do not execute if one or more of the m_vetoObjs exist in TES
282  const auto it = find_if( begin( m_vetoObjs ), end( m_vetoObjs ),
283  [&]( const std::string& loc ) { return this->exist<DataObject>( evtSvc(), loc ); } );
284  if ( it != end( m_vetoObjs ) ) {
285  if ( msgLevel( MSG::DEBUG ) ) debug() << *it << " found, skipping event " << endmsg;
286  return sc;
287  }
288 
289  // Execute if m_requireObjs is empty
290  // or if one or more of the m_requireObjs exist in TES
291  bool doIt = m_requireObjs.empty() ||
292  any_of( begin( m_requireObjs ), end( m_requireObjs ),
293  [&]( const std::string& loc ) { return this->exist<DataObject>( evtSvc(), loc ); } );
294 
295  // execute the generic method:
296  if ( doIt ) sc = GaudiCommon<Gaudi::Sequence>::sysExecute( ctx );
297  return sc;
298 }
299 //=============================================================================
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:135
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:273
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:346
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
ISequencerTimerTool::scopedTimer
auto scopedTimer(int index, bool enable=true)
Start timer by index. The timer stops on destruction of returned object.
Definition: ISequencerTimerTool.h:72
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:578
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:28
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
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:251
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:496
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:229
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
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 >