The Gaudi Framework  v29r0 (ff2e7097)
GaudiSequencer.cpp
Go to the documentation of this file.
1 // Include files
2 
3 // from Gaudi
8 
9 namespace
10 {
11 
12  // TODO: this adds C++14 'make_unique'... remove once we move to C++14...
13  template <typename T, typename... Args>
14  std::unique_ptr<T> make_unique_( Args&&... args )
15  {
16  return std::unique_ptr<T>( new T( std::forward<Args>( args )... ) );
17  }
18 
19  bool isDefault( const std::string& s ) { return s.empty(); }
20 
21  // utility class to populate some properties in the job options service
22  // for a given instance name in case those options are not explicitly
23  // set a-priori (effectively inheriting their values from the GaudiSequencer)
24  class populate_JobOptionsSvc_t
25  {
27  IJobOptionsSvc* m_jos;
28  std::string m_name;
29 
30  template <typename T>
31  void process( T&& t )
32  {
33  static_assert( std::tuple_size<T>::value == 2, "Expecting an std::tuple key-value pair" );
35  using prop_t = Gaudi::Property<type>;
36  if ( !isDefault( std::get<1>( t ) ) )
37  m_props.push_back( make_unique_<prop_t>( std::get<0>( t ), std::get<1>( t ) ) );
38  }
39  template <typename T, typename... Args>
40  void process( T&& t, Args&&... args )
41  {
42  process( std::forward<T>( t ) );
43  process( std::forward<Args>( args )... );
44  }
45  void check_veto()
46  { // avoid changing properties expliclty present in the JOS...
47  const auto* props = m_jos->getProperties( m_name );
48  if ( !props ) return;
49  for ( const auto& i : *props ) {
50  auto j = std::find_if(
51  std::begin( m_props ), std::end( m_props ),
52  [&i]( const std::unique_ptr<Gaudi::Details::PropertyBase>& prop ) { return prop->name() == i->name(); } );
53  if ( j == std::end( m_props ) ) continue;
54  m_props.erase( j );
55  if ( m_props.empty() ) break; // done!
56  }
57  }
58 
59  public:
60  template <typename... Args>
61  populate_JobOptionsSvc_t( std::string name, IJobOptionsSvc* jos, Args&&... args )
62  : m_jos{jos}, m_name{std::move( name )}
63  {
64  process( std::forward<Args>( args )... );
65  if ( !m_props.empty() ) check_veto();
66  std::for_each( std::begin( m_props ), std::end( m_props ),
68  m_jos->addPropertyToCatalogue( m_name, *i ).ignore();
69  } );
70  }
71  ~populate_JobOptionsSvc_t()
72  {
73  std::for_each( std::begin( m_props ), std::end( m_props ),
75  m_jos->removePropertyFromCatalogue( m_name, i->name() ).ignore();
76  } );
77  }
78  };
79 
80  template <typename Stream, typename Container, typename Separator, typename Transform>
81  Stream& ostream_joiner( Stream& os, const Container& c, Separator sep, Transform trans )
82  {
83  auto first = std::begin( c );
84  auto last = std::end( c );
85  if ( first != last ) {
86  os << trans( *first );
87  ++first;
88  }
89  for ( ; first != last; ++first ) os << sep << trans( *first );
90  return os;
91  }
92 }
93 
94 //-----------------------------------------------------------------------------
95 // Implementation file for class : GaudiSequencer
96 //
97 // 2004-05-13 : Olivier Callot
98 //-----------------------------------------------------------------------------
99 
100 //=============================================================================
101 // Standard constructor, initializes variables
102 //=============================================================================
104  : GaudiAlgorithm( name, pSvcLocator )
105 {
106  m_names.declareUpdateHandler( &GaudiSequencer::membershipHandler, this );
107 }
108 //=============================================================================
109 // Initialisation. Check parameters
110 //=============================================================================
112 {
114 
115  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialise" << endmsg;
116 
117  StatusCode status = decodeNames();
118  if ( !status.isSuccess() ) return status;
119 
120  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
121  if ( m_timerTool->globalTiming() ) m_measureTime = true;
122 
123  if ( m_measureTime ) {
126  } else {
127  release( m_timerTool );
128  m_timerTool = nullptr;
129  }
130 
131  //== Initialize the algorithms
132  for ( auto& entry : m_entries ) {
133  if ( m_measureTime ) {
134  entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) );
135  }
136 
137  status = entry.algorithm()->sysInitialize();
138  if ( !status.isSuccess() ) {
139  return Error( "Can not initialize " + entry.algorithm()->name(), status );
140  }
141  }
143 
144  return StatusCode::SUCCESS;
145 }
146 
147 //=============================================================================
148 // Main execution
149 //=============================================================================
151 {
152 
154 
155  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
156 
157  StatusCode result = StatusCode( StatusCode::SUCCESS, true );
158 
159  bool seqPass = !m_modeOR; // for OR, result will be false, unless (at least) one is true
160  // for AND, result will be true, unless (at least) one is false
161  // also see comment below ....
162 
163  for ( auto& entry : m_entries ) {
164  Algorithm* myAlg = entry.algorithm();
165  if ( !myAlg->isEnabled() ) continue;
166  if ( !myAlg->isExecuted() ) {
167 
168  if ( m_measureTime ) m_timerTool->start( entry.timer() );
169  result = myAlg->sysExecute( getContext() );
170  if ( m_measureTime ) m_timerTool->stop( entry.timer() );
171  myAlg->setExecuted( true );
172  if ( !result.isSuccess() ) break; //== Abort and return bad status
173  }
174  //== Check the returned status
175  if ( !m_ignoreFilter ) {
176  bool passed = myAlg->filterPassed();
177  if ( msgLevel( MSG::VERBOSE ) )
178  verbose() << "Algorithm " << myAlg->name() << " returned filter passed " << ( passed ? "true" : "false" )
179  << endmsg;
180  if ( entry.reverse() ) passed = !passed;
181 
182  //== indicate our own result. For OR, exit as soon as true.
183  // If no more, will exit with false.
184  //== for AND, exit as soon as false. Else, will be true (default)
185 
186  // if not short-circuiting, make sure we latch iPass to 'true' in
187  // OR mode (i.e. it is sufficient for one item to be true in order
188  // to be true at the end, and thus we start out at 'false'), and latch
189  // to 'false' in AND mode (i.e. it is sufficient for one item to
190  // be false to the false in the end, and thus we start out at 'true')
191  // -- i.e. we should not just blindly return the 'last' passed status!
192 
193  // or to put it another way: in OR mode, we don't care about things
194  // which are false, as they leave our current state alone (provided
195  // we stared as 'false'!), and in AND mode, we keep our current
196  // state until someone returns 'false' (provided we started as 'true')
197  if ( m_modeOR ? passed : !passed ) {
198  seqPass = passed;
199  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is now " << ( seqPass ? "true" : "false" ) << endmsg;
200  if ( m_shortCircuit ) break;
201  }
202  }
203  }
204  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is " << ( seqPass ? "true" : "false" ) << endmsg;
205  if ( !m_ignoreFilter && !m_entries.empty() ) setFilterPassed( m_invert ? !seqPass : seqPass );
206  setExecuted( true );
207 
209 
210  return m_returnOK ? ( result.ignore(), StatusCode::SUCCESS ) : result;
211 }
212 
213 //=============================================================================
214 // Finalize
215 //=============================================================================
217 {
218 
219  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
220  return GaudiAlgorithm::finalize();
221 }
222 
223 //=========================================================================
224 // Execute the beginRun of every algorithm
225 //=========================================================================
227 {
228 
229  if ( !isEnabled() ) return StatusCode::SUCCESS;
230  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> beginRun" << endmsg;
231  return StatusCode::SUCCESS;
232 }
233 
234 //=========================================================================
235 // Execute the endRun() of every algorithm
236 //=========================================================================
238 {
239 
240  if ( !isEnabled() ) return StatusCode::SUCCESS;
241  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> endRun" << endmsg;
242  return StatusCode::SUCCESS;
243 }
244 
245 //=========================================================================
246 // Decode the input names and fills the m_algs vector.
247 //=========================================================================
249 {
250 
252  m_entries.clear();
253 
254  //== Get the "Context" option if in the file...
255  auto jos = service<IJobOptionsSvc>( "JobOptionsSvc" );
256 
257  //= Get the Application manager, to see if algorithm exist
258  auto appMgr = service<IAlgManager>( "ApplicationMgr" );
259  for ( const auto& item : m_names.value() ) {
261  const std::string& theName = typeName.name();
262  const std::string& theType = typeName.type();
263 
264  //== Check wether the specified algorithm already exists. If not, create it
266  SmartIF<IAlgorithm> myIAlg = appMgr->algorithm( typeName, false ); // do not create it now
267  if ( !myIAlg ) {
268  // ensure some magic properties are set while we create the subalgorithm so
269  // that it effectively inherites 'our' settings -- if they have non-default
270  // values... and are not set explicitly already.
271  populate_JobOptionsSvc_t populate_guard{theName, jos, std::forward_as_tuple( "Context", context() ),
272  std::forward_as_tuple( "RootInTES", rootInTES() )};
273  Algorithm* myAlg = nullptr;
274  result = createSubAlgorithm( theType, theName, myAlg );
275  myIAlg = myAlg; // ensure that myIAlg.isValid() from here onwards!
276  } else {
277  Algorithm* myAlg = dynamic_cast<Algorithm*>( myIAlg.get() );
278  if ( myAlg ) {
279  subAlgorithms()->push_back( myAlg );
280  // when the algorithm is not created, the ref count is short by one, so we have to fix it.
281  myAlg->addRef();
282  }
283  }
284 
285  // propagate the sub-algorithm into own state.
286  if ( result.isSuccess() && Gaudi::StateMachine::INITIALIZED <= FSMState() && myIAlg &&
287  Gaudi::StateMachine::INITIALIZED > myIAlg->FSMState() ) {
288  StatusCode sc = myIAlg->sysInitialize();
289  if ( sc.isFailure() ) {
290  result = sc;
291  }
292  }
293 
294  // propagate the sub-algorithm into own state.
295  if ( result.isSuccess() && Gaudi::StateMachine::RUNNING <= FSMState() && myIAlg &&
296  Gaudi::StateMachine::RUNNING > myIAlg->FSMState() ) {
297  StatusCode sc = myIAlg->sysStart();
298  if ( sc.isFailure() ) {
299  result = sc;
300  }
301  }
302 
303  //== Is it an Algorithm ? Strange test...
304  if ( result.isSuccess() ) {
305  // TODO: (MCl) it is possible to avoid the dynamic_cast in most of the
306  // cases by keeping the result of createSubAlgorithm.
307  Algorithm* myAlg = dynamic_cast<Algorithm*>( myIAlg.get() );
308  if ( myAlg ) {
309  // Note: The reference counting is kept by the system of sub-algorithms
310  m_entries.emplace_back( myAlg );
311  if ( msgLevel( MSG::DEBUG ) ) debug() << "Added algorithm " << theName << endmsg;
312  } else {
313  warning() << theName << " is not an Algorithm - failed dynamic_cast" << endmsg;
314  final = StatusCode::FAILURE;
315  }
316  } else {
317  warning() << "Unable to find or create " << theName << endmsg;
318  final = result;
319  }
320  }
321 
322  //== Print the list of algorithms
323  MsgStream& msg = info();
324  if ( m_modeOR ) msg << "OR ";
325  msg << "Member list: ";
326  ostream_joiner( msg, m_entries, ", ", []( const AlgorithmEntry& e ) {
327  Algorithm* alg = e.algorithm();
328  std::string typ = System::typeinfoName( typeid( *alg ) );
329  return ( alg->name() == typ ) ? alg->name() : ( typ + "/" + alg->name() );
330  } );
331  if ( !isDefault( context() ) ) msg << ", with context '" << context() << "'";
332  if ( !isDefault( rootInTES() ) ) msg << ", with rootInTES '" << rootInTES() << "'";
333  msg << endmsg;
334 
335  return final;
336 }
337 
338 //=========================================================================
339 // Interface for the Property manager
340 //=========================================================================
342 {
343  // no action for not-yet initialized sequencer
345  return;
346  } // RETURN
347 
348  decodeNames().ignore();
349 
350  if ( !m_measureTime ) {
351  return;
352  } // RETURN
353 
354  // add the entries into timer table:
355 
356  if ( !m_timerTool ) {
357  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
358  }
359 
360  if ( m_timerTool->globalTiming() ) m_measureTime = true;
361 
364 
365  for ( auto& entry : m_entries ) {
366  entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) );
367  }
368 
370 }
371 
373 {
374  if ( m_invert ) os << "~";
375  // the default filterpass value for an empty sequencer depends on ModeOR
376  if ( m_entries.empty() ) return os << ( ( !m_modeOR ) ? "CFTrue" : "CFFalse" );
377 
378  // if we have only one element, we do not need a name
379  if ( m_entries.size() > 1 ) os << "seq(";
380 
381  const auto op = m_modeOR ? " | " : " & ";
382  const auto first = begin( m_entries );
383  const auto last = end( m_entries );
384  auto iterator = first;
385  while ( iterator != last ) {
386  if ( iterator != first ) os << op;
387  if ( iterator->reverse() ) os << "~";
388  iterator->algorithm()->toControlFlowExpression( os );
389  ++iterator;
390  }
391 
392  if ( m_entries.size() > 1 ) os << ")";
393  return os;
394 }
395 //=============================================================================
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:837
T forward_as_tuple(T...args)
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:731
void setFilterPassed(bool state) const override
Set the filter passed flag to the specified state.
Definition: Algorithm.cpp:768
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:708
bool isExecuted() const override
Has this algorithm been executed since the last reset?
Definition: Algorithm.cpp:741
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:329
virtual int addTimer(const std::string &name)=0
add a timer entry with the specified name
StatusCode initialize() override
standard initialization method
const std::string name() const
property name
Definition: Property.h:40
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
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)
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:704
virtual void increaseIndent()=0
Increase the indentation of the name.
T end(T...args)
Algorithm * algorithm() const
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
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:760
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:28
StatusCode finalize() override
standard finalization method
const EventContext & getContext() const override
get the context
Definition: Algorithm.h:436
T erase(T...args)
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:776
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
T find_if(T...args)
STL class.
STL class.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T begin(T...args)
const std::string & type() const
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:762
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:747
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:109
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:23
MSG::Level msgLevel() const
get the output level 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.
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:502
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:191
StatusCode beginRun() override
Algorithm beginRun.