The Gaudi Framework  v30r0 (c919700c)
GaudiSequencer.cpp
Go to the documentation of this file.
1 // Include files
2 #include <initializer_list>
3 #include <tuple>
4 
5 // from Gaudi
10 
11 namespace
12 {
13 
14  bool isDefault( const std::string& s ) { return s.empty(); }
15 
16  template <typename Container>
17  bool veto( const Container* props, const char* name )
18  { // avoid changing properties explicitly present in the JOS...
19  return props &&
20  std::any_of( begin( *props ), end( *props ), [name]( const auto* prop ) { return prop->name() == name; } );
21  }
22 
23  template <typename F, typename... Args>
24  void for_each_arg( F&& f, Args&&... args )
25  {
26  // the std::initializer_list only exists to provide a 'context' in which to
27  // expand the variadic pack
28  (void)std::initializer_list<int>{( f( std::forward<Args>( args ) ), 0 )...};
29  }
30 
31  // utility class to populate some properties in the job options service
32  // for a given instance name in case those options are not explicitly
33  // set a-priori (effectively inheriting their values from the GaudiSequencer)
34  class populate_JobOptionsSvc_t
35  {
37  IJobOptionsSvc* m_jos;
38  std::string m_name;
39 
40  template <typename Properties, typename Key, typename Value>
41  void addPropertyToCatalogue( const Properties* props, const std::tuple<Key, Value>& arg )
42  {
43  const auto& key = std::get<0>( arg );
44  const auto& value = std::get<1>( arg );
45  if ( isDefault( value ) || veto( props, key ) ) return;
46  m_jos->addPropertyToCatalogue( m_name, Gaudi::Property<std::decay_t<Value>>{key, value} ).ignore();
47  m_props.push_back( key );
48  }
49 
50  public:
51  template <typename... Args>
52  populate_JobOptionsSvc_t( std::string name, IJobOptionsSvc* jos, Args&&... args )
53  : m_jos{jos}, m_name{std::move( name )}
54  {
55  const auto* props = m_jos->getProperties( m_name );
56  for_each_arg( [&]( auto&& arg ) { this->addPropertyToCatalogue( props, std::forward<decltype( arg )>( arg ) ); },
57  std::forward<Args>( args )... );
58  }
59  ~populate_JobOptionsSvc_t()
60  {
61  std::for_each( begin( m_props ), end( m_props ),
62  [&]( const std::string& key ) { m_jos->removePropertyFromCatalogue( m_name, key ).ignore(); } );
63  }
64  };
65 }
66 
67 //-----------------------------------------------------------------------------
68 // Implementation file for class : GaudiSequencer
69 //
70 // 2004-05-13 : Olivier Callot
71 //-----------------------------------------------------------------------------
72 
73 //=============================================================================
74 // Standard constructor, initializes variables
75 //=============================================================================
77  : GaudiAlgorithm( name, pSvcLocator )
78 {
79  m_names.declareUpdateHandler( &GaudiSequencer::membershipHandler, this );
80 }
81 //=============================================================================
82 // Initialisation. Check parameters
83 //=============================================================================
85 {
87 
88  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialise" << endmsg;
89 
90  StatusCode status = decodeNames();
91  if ( !status.isSuccess() ) return status;
92 
93  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
94  if ( m_timerTool->globalTiming() ) m_measureTime = true;
95 
96  if ( m_measureTime ) {
99  } else {
100  release( m_timerTool );
101  m_timerTool = nullptr;
102  }
103 
104  //== Initialize the algorithms
105  for ( auto& entry : m_entries ) {
106  if ( m_measureTime ) {
107  entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) );
108  }
109 
110  status = entry.algorithm()->sysInitialize();
111  if ( !status.isSuccess() ) {
112  return Error( "Can not initialize " + entry.algorithm()->name(), status );
113  }
114  }
116 
117  return StatusCode::SUCCESS;
118 }
119 
120 //=============================================================================
121 // Main execution
122 //=============================================================================
124 {
125 
127 
128  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
129 
130  StatusCode result = StatusCode( StatusCode::SUCCESS, true );
131 
132  bool seqPass = !m_modeOR; // for OR, result will be false, unless (at least) one is true
133  // for AND, result will be true, unless (at least) one is false
134  // also see comment below ....
135 
136  for ( auto& entry : m_entries ) {
137  Algorithm* myAlg = entry.algorithm();
138  if ( !myAlg->isEnabled() ) continue;
139  if ( !myAlg->isExecuted() ) {
140 
141  if ( m_measureTime ) m_timerTool->start( entry.timer() );
142  result = myAlg->sysExecute( getContext() );
143  if ( m_measureTime ) m_timerTool->stop( entry.timer() );
144  myAlg->setExecuted( true );
145  if ( !result.isSuccess() ) break; //== Abort and return bad status
146  }
147  //== Check the returned status
148  if ( !m_ignoreFilter ) {
149  bool passed = myAlg->filterPassed();
150  if ( msgLevel( MSG::VERBOSE ) )
151  verbose() << "Algorithm " << myAlg->name() << " returned filter passed " << ( passed ? "true" : "false" )
152  << endmsg;
153  if ( entry.reverse() ) passed = !passed;
154 
155  //== indicate our own result. For OR, exit as soon as true.
156  // If no more, will exit with false.
157  //== for AND, exit as soon as false. Else, will be true (default)
158 
159  // if not short-circuiting, make sure we latch iPass to 'true' in
160  // OR mode (i.e. it is sufficient for one item to be true in order
161  // to be true at the end, and thus we start out at 'false'), and latch
162  // to 'false' in AND mode (i.e. it is sufficient for one item to
163  // be false to the false in the end, and thus we start out at 'true')
164  // -- i.e. we should not just blindly return the 'last' passed status!
165 
166  // or to put it another way: in OR mode, we don't care about things
167  // which are false, as they leave our current state alone (provided
168  // we stared as 'false'!), and in AND mode, we keep our current
169  // state until someone returns 'false' (provided we started as 'true')
170  if ( m_modeOR ? passed : !passed ) {
171  seqPass = passed;
172  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is now " << ( seqPass ? "true" : "false" ) << endmsg;
173  if ( m_shortCircuit ) break;
174  }
175  }
176  }
177  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is " << ( seqPass ? "true" : "false" ) << endmsg;
178  if ( !m_ignoreFilter && !m_entries.empty() ) setFilterPassed( m_invert ? !seqPass : seqPass );
179  setExecuted( true );
180 
182 
183  return m_returnOK ? ( result.ignore(), StatusCode::SUCCESS ) : result;
184 }
185 
186 //=============================================================================
187 // Finalize
188 //=============================================================================
190 {
191  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
192  return GaudiAlgorithm::finalize();
193 }
194 
195 //=========================================================================
196 // Execute the beginRun of every algorithm
197 //=========================================================================
199 {
200  if ( !isEnabled() ) return StatusCode::SUCCESS;
201  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> beginRun" << endmsg;
202  return StatusCode::SUCCESS;
203 }
204 
205 //=========================================================================
206 // Execute the endRun() of every algorithm
207 //=========================================================================
209 {
210  if ( !isEnabled() ) return StatusCode::SUCCESS;
211  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> endRun" << endmsg;
212  return StatusCode::SUCCESS;
213 }
214 
215 //=========================================================================
216 // Decode the input names and fills the m_algs vector.
217 //=========================================================================
219 {
221  m_entries.clear();
222 
223  //== Get the "Context" option if in the file...
224  auto jos = service<IJobOptionsSvc>( "JobOptionsSvc" );
225 
226  //= Get the Application manager, to see if algorithm exist
227  auto appMgr = service<IAlgManager>( "ApplicationMgr" );
228  for ( const auto& item : m_names.value() ) {
230  const std::string& theName = typeName.name();
231  const std::string& theType = typeName.type();
232 
233  //== Check wether the specified algorithm already exists. If not, create it
235  SmartIF<IAlgorithm> myIAlg = appMgr->algorithm( typeName, false ); // do not create it now
236  if ( !myIAlg ) {
237  // ensure some magic properties are set while we create the subalgorithm so
238  // that it effectively inherites 'our' settings -- if they have non-default
239  // values... and are not set explicitly already.
240  populate_JobOptionsSvc_t populate_guard{theName, jos, std::forward_as_tuple( "Context", context() ),
241  std::forward_as_tuple( "RootInTES", rootInTES() )};
242  Algorithm* myAlg = nullptr;
243  result = createSubAlgorithm( theType, theName, myAlg );
244  myIAlg = myAlg; // ensure that myIAlg.isValid() from here onwards!
245  } else {
246  Algorithm* myAlg = dynamic_cast<Algorithm*>( myIAlg.get() );
247  if ( myAlg ) {
248  subAlgorithms()->push_back( myAlg );
249  // when the algorithm is not created, the ref count is short by one, so we have to fix it.
250  myAlg->addRef();
251  }
252  }
253 
254  // propagate the sub-algorithm into own state.
255  if ( result.isSuccess() && Gaudi::StateMachine::INITIALIZED <= FSMState() && myIAlg &&
256  Gaudi::StateMachine::INITIALIZED > myIAlg->FSMState() ) {
257  StatusCode sc = myIAlg->sysInitialize();
258  if ( sc.isFailure() ) result = sc;
259  }
260 
261  // propagate the sub-algorithm into own state.
262  if ( result.isSuccess() && Gaudi::StateMachine::RUNNING <= FSMState() && myIAlg &&
263  Gaudi::StateMachine::RUNNING > myIAlg->FSMState() ) {
264  StatusCode sc = myIAlg->sysStart();
265  if ( sc.isFailure() ) result = sc;
266  }
267 
268  //== Is it an Algorithm ? Strange test...
269  if ( result.isSuccess() ) {
270  // TODO: (MCl) it is possible to avoid the dynamic_cast in most of the
271  // cases by keeping the result of createSubAlgorithm.
272  Algorithm* myAlg = dynamic_cast<Algorithm*>( myIAlg.get() );
273  if ( myAlg ) {
274  // Note: The reference counting is kept by the system of sub-algorithms
275  m_entries.emplace_back( myAlg );
276  if ( msgLevel( MSG::DEBUG ) ) debug() << "Added algorithm " << theName << endmsg;
277  } else {
278  warning() << theName << " is not an Algorithm - failed dynamic_cast" << endmsg;
279  final = StatusCode::FAILURE;
280  }
281  } else {
282  warning() << "Unable to find or create " << theName << endmsg;
283  final = result;
284  }
285  }
286 
287  //== Print the list of algorithms
288  MsgStream& msg = info();
289  if ( m_modeOR ) msg << "OR ";
290  msg << "Member list: ";
292  []( auto& os, const AlgorithmEntry& e ) -> decltype( auto ) {
293  Algorithm* alg = e.algorithm();
294  std::string typ = System::typeinfoName( typeid( *alg ) );
295  os << typ;
296  if ( alg->name() != typ ) os << "/" << alg->name();
297  return os;
298  } );
299  if ( !isDefault( context() ) ) msg << ", with context '" << context() << "'";
300  if ( !isDefault( rootInTES() ) ) msg << ", with rootInTES '" << rootInTES() << "'";
301  msg << endmsg;
302 
303  return final;
304 }
305 
306 //=========================================================================
307 // Interface for the Property manager
308 //=========================================================================
310 {
311  // no action for not-yet initialized sequencer
312  if ( Gaudi::StateMachine::INITIALIZED > FSMState() ) return; // RETURN
313 
314  decodeNames().ignore();
315 
316  if ( !m_measureTime ) return; // RETURN
317 
318  // add the entries into timer table:
319 
320  if ( !m_timerTool ) {
321  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
322  }
323 
324  if ( m_timerTool->globalTiming() ) m_measureTime = true;
325 
328 
329  for ( auto& entry : m_entries ) {
330  entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) );
331  }
332 
334 }
335 
337 {
338  if ( m_invert ) os << "~";
339  // the default filterpass value for an empty sequencer depends on ModeOR
340  if ( m_entries.empty() ) return os << ( ( !m_modeOR ) ? "CFTrue" : "CFFalse" );
341 
342  // if we have only one element, we do not need a name
343  if ( m_entries.size() > 1 ) os << "seq(";
344 
345  const auto op = m_modeOR ? " | " : " & ";
346  const auto last = end( m_entries );
347  const auto first = begin( m_entries );
348  for ( auto iterator = first; iterator != last; ++iterator ) {
349  if ( iterator != first ) os << op;
350  if ( iterator->reverse() ) os << "~";
351  iterator->algorithm()->toControlFlowExpression( os );
352  }
353 
354  if ( m_entries.size() > 1 ) os << ")";
355  return os;
356 }
357 //=============================================================================
Gaudi::Property< bool > m_measureTime
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode execute() override
Algorithm execution.
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
T empty(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
virtual void decreaseIndent()=0
Decrease the indentation of the name.
StatusCode createSubAlgorithm(const std::string &type, const std::string &name, Algorithm *&pSubAlg)
Create a sub algorithm.
Definition: Algorithm.cpp:842
T forward_as_tuple(T...args)
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:737
void setFilterPassed(bool state) const override
Set the filter passed flag to the specified state.
Definition: Algorithm.cpp:774
Implementation of property with value of concrete type.
Definition: Property.h:319
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: GaudiCommon.h:721
bool isExecuted() const override
Has this algorithm been executed since the last reset?
Definition: Algorithm.cpp:747
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
StatusCode initialize() override
Algorithm initialization.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:336
virtual int addTimer(const std::string &name)=0
add a timer entry with the specified name
StatusCode initialize() override
standard initialization method
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:50
WARN_UNUSED StatusCode Error(const std::string &msg, const StatusCode st=StatusCode::FAILURE, const size_t mx=10) const
Print the error message and return with the given StatusCode.
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
bool PyHelper() addPropertyToCatalogue(IInterface *p, char *comp, char *name, char *value)
Definition: Bootstrap.cpp:261
class MergingTransformer< Out(const vector_of_const_< In > void
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:717
virtual void increaseIndent()=0
Increase the indentation of the name.
Algorithm * algorithm() const
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:61
virtual bool globalTiming()=0
returns the flag telling that global timing is wanted
StatusCode endRun() override
Algorithm endRun.
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:58
std::ostream & toControlFlowExpression(std::ostream &os) const override
Produce string represention of the control flow expression.
STL class.
virtual StatusCode addPropertyToCatalogue(const std::string &client, const Gaudi::Details::PropertyBase &property)=0
Add a property into the JobOptions catalog.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
ISequencerTimerTool * m_timerTool
Pointer to the timer tool.
T push_back(T...args)
bool isEnabled() const override
Is this algorithm enabled or disabled?
Definition: Algorithm.cpp:766
Gaudi::Property< bool > m_ignoreFilter
Helper class to parse a string of format "type/name".
Gaudi::Property< std::vector< std::string > > m_names
Gaudi::Property< bool > m_invert
Main interface for the JobOptions service.
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
StatusCode finalize() override
standard finalization method
const EventContext & getContext() const override
get the context
Definition: Algorithm.h:438
The useful base class for data processing algorithms.
void membershipHandler(Gaudi::Details::PropertyBase &theProp)
for asynchronous changes in the list of algorithms
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
StatusCode finalize() override
Algorithm finalization.
virtual const std::vector< const Gaudi::Details::PropertyBase * > * getProperties(const std::string &client) const =0
Get the properties associated to a given client.
Gaudi::Property< bool > m_modeOR
T move(T...args)
StatusCode decodeNames()
Decode a vector of string.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:40
Gaudi::Property< bool > m_shortCircuit
virtual void start(int index)=0
start the counter, i.e.
const std::vector< Algorithm * > * subAlgorithms() const
List of sub-algorithms. Returns a pointer to a vector of (sub) Algorithms.
Definition: Algorithm.cpp:782
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
const std::string & type() const
T any_of(T...args)
virtual StatusCode removePropertyFromCatalogue(const std::string &client, const std::string &name)=0
Remove a property from the JobOptions catalog.
bool filterPassed() const override
Did this algorithm pass or fail its filter criterion for the last event?
Definition: Algorithm.cpp:768
appMgr
Definition: IOTest.py:94
string s
Definition: gaudirun.py:253
void setExecuted(bool state) const override
Set the executed flag to the specified state.
Definition: Algorithm.cpp:753
Gaudi::Property< bool > m_returnOK
GaudiSequencer(const std::string &name, ISvcLocator *pSvcLocator)
Standard constructor.
const std::string & name() const
void ignore() const
Definition: StatusCode.h:84
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:23
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
T for_each(T...args)
virtual double stop(int index)=0
stop the counter, return the elapsed time
StatusCode release(const IInterface *interface) const
Manual forced (and &#39;safe&#39;) release of the active tool or service.
STL class.
T forward(T...args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
int m_timer
Timer number for the sequencer.
StatusCode sysExecute(const EventContext &ctx) override
The actions to be performed by the algorithm on an event.
Definition: Algorithm.cpp:508
std::vector< AlgorithmEntry > m_entries
List of algorithms to process.
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition: Algorithm.h:193
StatusCode beginRun() override
Algorithm beginRun.