The Gaudi Framework  v30r4 (9b837755)
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 // Initialisation. Check parameters
75 //=============================================================================
77 {
79 
80  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialise" << endmsg;
81 
82  StatusCode status = decodeNames();
83  if ( !status.isSuccess() ) return status;
84 
85  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
86  if ( m_timerTool->globalTiming() ) m_measureTime = true;
87 
88  if ( m_measureTime ) {
89  m_timer = m_timerTool->addTimer( name() );
90  m_timerTool->increaseIndent();
91  } else {
92  release( m_timerTool );
93  m_timerTool = nullptr;
94  }
95 
96  //== Initialize the algorithms
97  for ( auto& entry : m_entries ) {
98  if ( m_measureTime ) {
99  entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) );
100  }
101 
102  status = entry.algorithm()->sysInitialize();
103  if ( !status.isSuccess() ) {
104  return Error( "Can not initialize " + entry.algorithm()->name(), status );
105  }
106  }
107  if ( m_measureTime ) m_timerTool->decreaseIndent();
108 
109  return StatusCode::SUCCESS;
110 }
111 
112 //=============================================================================
113 // Main execution
114 //=============================================================================
116 {
117 
118  if ( m_measureTime ) m_timerTool->start( m_timer );
119 
120  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
121 
122  StatusCode result = StatusCode( StatusCode::SUCCESS, true );
123 
124  bool seqPass = !m_modeOR; // for OR, result will be false, unless (at least) one is true
125  // for AND, result will be true, unless (at least) one is false
126  // also see comment below ....
127 
128  for ( auto& entry : m_entries ) {
129  Algorithm* myAlg = entry.algorithm();
130  if ( !myAlg->isEnabled() ) continue;
131  if ( !myAlg->isExecuted() ) {
132 
133  if ( m_measureTime ) m_timerTool->start( entry.timer() );
134  result = myAlg->sysExecute( getContext() );
135  if ( m_measureTime ) m_timerTool->stop( entry.timer() );
136  if ( !result.isSuccess() ) break; //== Abort and return bad status
137  }
138  //== Check the returned status
139  if ( !m_ignoreFilter ) {
140  bool passed = myAlg->filterPassed();
141  if ( msgLevel( MSG::VERBOSE ) )
142  verbose() << "Algorithm " << myAlg->name() << " returned filter passed " << ( passed ? "true" : "false" )
143  << endmsg;
144  if ( entry.reverse() ) passed = !passed;
145 
146  //== indicate our own result. For OR, exit as soon as true.
147  // If no more, will exit with false.
148  //== for AND, exit as soon as false. Else, will be true (default)
149 
150  // if not short-circuiting, make sure we latch iPass to 'true' in
151  // OR mode (i.e. it is sufficient for one item to be true in order
152  // to be true at the end, and thus we start out at 'false'), and latch
153  // to 'false' in AND mode (i.e. it is sufficient for one item to
154  // be false to the false in the end, and thus we start out at 'true')
155  // -- i.e. we should not just blindly return the 'last' passed status!
156 
157  // or to put it another way: in OR mode, we don't care about things
158  // which are false, as they leave our current state alone (provided
159  // we stared as 'false'!), and in AND mode, we keep our current
160  // state until someone returns 'false' (provided we started as 'true')
161  if ( m_modeOR ? passed : !passed ) {
162  seqPass = passed;
163  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is now " << ( seqPass ? "true" : "false" ) << endmsg;
164  if ( m_shortCircuit ) break;
165  }
166  }
167  }
168  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is " << ( seqPass ? "true" : "false" ) << endmsg;
169  if ( !m_ignoreFilter && !m_entries.empty() ) setFilterPassed( m_invert ? !seqPass : seqPass );
170  setExecuted( true );
171 
172  if ( m_measureTime ) m_timerTool->stop( m_timer );
173 
174  return m_returnOK ? ( result.ignore(), StatusCode::SUCCESS ) : result;
175 }
176 
177 //=========================================================================
178 // Decode the input names and fills the m_algs vector.
179 //=========================================================================
181 {
183  m_entries.clear();
184 
185  //== Get the "Context" option if in the file...
186  auto jos = service<IJobOptionsSvc>( "JobOptionsSvc" );
187 
188  //= Get the Application manager, to see if algorithm exist
189  auto appMgr = service<IAlgManager>( "ApplicationMgr" );
190  for ( const auto& item : m_names.value() ) {
192  const std::string& theName = typeName.name();
193  const std::string& theType = typeName.type();
194 
195  //== Check wether the specified algorithm already exists. If not, create it
197  SmartIF<IAlgorithm> myIAlg = appMgr->algorithm( typeName, false ); // do not create it now
198  if ( !myIAlg ) {
199  // ensure some magic properties are set while we create the subalgorithm so
200  // that it effectively inherites 'our' settings -- if they have non-default
201  // values... and are not set explicitly already.
202  populate_JobOptionsSvc_t populate_guard{theName, jos, std::forward_as_tuple( "Context", context() ),
203  std::forward_as_tuple( "RootInTES", rootInTES() )};
204  Algorithm* myAlg = nullptr;
205  result = createSubAlgorithm( theType, theName, myAlg );
206  myIAlg = myAlg; // ensure that myIAlg.isValid() from here onwards!
207  } else {
208  Algorithm* myAlg = dynamic_cast<Algorithm*>( myIAlg.get() );
209  if ( myAlg ) {
210  subAlgorithms()->push_back( myAlg );
211  // when the algorithm is not created, the ref count is short by one, so we have to fix it.
212  myAlg->addRef();
213  }
214  }
215 
216  // propagate the sub-algorithm into own state.
217  if ( result.isSuccess() && Gaudi::StateMachine::INITIALIZED <= FSMState() && myIAlg &&
218  Gaudi::StateMachine::INITIALIZED > myIAlg->FSMState() ) {
219  StatusCode sc = myIAlg->sysInitialize();
220  if ( sc.isFailure() ) result = sc;
221  }
222 
223  // propagate the sub-algorithm into own state.
224  if ( result.isSuccess() && Gaudi::StateMachine::RUNNING <= FSMState() && myIAlg &&
225  Gaudi::StateMachine::RUNNING > myIAlg->FSMState() ) {
226  StatusCode sc = myIAlg->sysStart();
227  if ( sc.isFailure() ) result = sc;
228  }
229 
230  //== Is it an Algorithm ? Strange test...
231  if ( result.isSuccess() ) {
232  // TODO: (MCl) it is possible to avoid the dynamic_cast in most of the
233  // cases by keeping the result of createSubAlgorithm.
234  Algorithm* myAlg = dynamic_cast<Algorithm*>( myIAlg.get() );
235  if ( myAlg ) {
236  // Note: The reference counting is kept by the system of sub-algorithms
237  m_entries.emplace_back( myAlg );
238  if ( msgLevel( MSG::DEBUG ) ) debug() << "Added algorithm " << theName << endmsg;
239  } else {
240  warning() << theName << " is not an Algorithm - failed dynamic_cast" << endmsg;
241  final = StatusCode::FAILURE;
242  }
243  } else {
244  warning() << "Unable to find or create " << theName << endmsg;
245  final = result;
246  }
247  }
248 
249  //== Print the list of algorithms
250  MsgStream& msg = info();
251  if ( m_modeOR ) msg << "OR ";
252  msg << "Member list: ";
253  GaudiUtils::details::ostream_joiner( msg, m_entries, ", ",
254  []( auto& os, const AlgorithmEntry& e ) -> decltype( auto ) {
255  Algorithm* alg = e.algorithm();
256  std::string typ = System::typeinfoName( typeid( *alg ) );
257  os << typ;
258  if ( alg->name() != typ ) os << "/" << alg->name();
259  return os;
260  } );
261  if ( !isDefault( context() ) ) msg << ", with context '" << context() << "'";
262  if ( !isDefault( rootInTES() ) ) msg << ", with rootInTES '" << rootInTES() << "'";
263  msg << endmsg;
264 
265  return final;
266 }
267 
268 //=========================================================================
269 // Interface for the Property manager
270 //=========================================================================
272 {
273  // no action for not-yet initialized sequencer
274  if ( Gaudi::StateMachine::INITIALIZED > FSMState() ) return; // RETURN
275 
276  decodeNames().ignore();
277 
278  if ( !m_measureTime ) return; // RETURN
279 
280  // add the entries into timer table:
281 
282  if ( !m_timerTool ) {
283  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
284  }
285 
286  if ( m_timerTool->globalTiming() ) m_measureTime = true;
287 
288  m_timer = m_timerTool->addTimer( name() );
289  m_timerTool->increaseIndent();
290 
291  for ( auto& entry : m_entries ) {
292  entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) );
293  }
294 
295  m_timerTool->decreaseIndent();
296 }
297 
299 {
300  if ( m_invert ) os << "~";
301  // the default filterpass value for an empty sequencer depends on ModeOR
302  if ( m_entries.empty() ) return os << ( ( !m_modeOR ) ? "CFTrue" : "CFFalse" );
303 
304  // if we have only one element, we do not need a name
305  if ( m_entries.size() > 1 ) os << "seq(";
306 
307  const auto op = m_modeOR ? " | " : " & ";
308  const auto last = end( m_entries );
309  const auto first = begin( m_entries );
310  for ( auto iterator = first; iterator != last; ++iterator ) {
311  if ( iterator != first ) os << op;
312  if ( iterator->reverse() ) os << "~";
313  iterator->algorithm()->toControlFlowExpression( os );
314  }
315 
316  if ( m_entries.size() > 1 ) os << ")";
317  return os;
318 }
319 //=============================================================================
constexpr static const auto FAILURE
Definition: StatusCode.h:88
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode execute() override
Algorithm execution.
T empty(T...args)
T forward_as_tuple(T...args)
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:747
Implementation of property with value of concrete type.
Definition: Property.h:383
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
bool isExecuted() const override
Has this algorithm been executed since the last reset?
Definition: Algorithm.cpp:757
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:332
StatusCode initialize() override
standard initialization method
bool isSuccess() const
Definition: StatusCode.h:287
bool PyHelper() addPropertyToCatalogue(IInterface *p, char *comp, char *name, char *value)
Definition: Bootstrap.cpp:258
class MergingTransformer< Out(const vector_of_const_< In > void
Algorithm * algorithm() const
bool isFailure() const
Definition: StatusCode.h:139
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
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
T push_back(T...args)
bool isEnabled() const override
Is this algorithm enabled or disabled?
Definition: Algorithm.cpp:769
Helper class to parse a string of format "type/name".
Main interface for the JobOptions service.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
virtual const std::vector< const Gaudi::Details::PropertyBase * > * getProperties(const std::string &client) const =0
Get the properties associated to a given client.
T move(T...args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
void membershipHandler()
for asynchronous changes in the list of algorithms
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.
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:165
bool filterPassed() const override
Did this algorithm pass or fail its filter criterion for the last event?
Definition: Algorithm.cpp:771
appMgr
Definition: IOTest.py:94
string s
Definition: gaudirun.py:253
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:182
AttribStringParser::Iterator begin(const AttribStringParser &parser)
const std::string & name() const
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:23
T for_each(T...args)
STL class.
T forward(T...args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
StatusCode sysExecute(const EventContext &ctx) override
The actions to be performed by the algorithm on an event.
Definition: Algorithm.cpp:528