The Gaudi Framework  v30r3 (a5ef0a68)
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  if ( !result.isSuccess() ) break; //== Abort and return bad status
145  }
146  //== Check the returned status
147  if ( !m_ignoreFilter ) {
148  bool passed = myAlg->filterPassed();
149  if ( msgLevel( MSG::VERBOSE ) )
150  verbose() << "Algorithm " << myAlg->name() << " returned filter passed " << ( passed ? "true" : "false" )
151  << endmsg;
152  if ( entry.reverse() ) passed = !passed;
153 
154  //== indicate our own result. For OR, exit as soon as true.
155  // If no more, will exit with false.
156  //== for AND, exit as soon as false. Else, will be true (default)
157 
158  // if not short-circuiting, make sure we latch iPass to 'true' in
159  // OR mode (i.e. it is sufficient for one item to be true in order
160  // to be true at the end, and thus we start out at 'false'), and latch
161  // to 'false' in AND mode (i.e. it is sufficient for one item to
162  // be false to the false in the end, and thus we start out at 'true')
163  // -- i.e. we should not just blindly return the 'last' passed status!
164 
165  // or to put it another way: in OR mode, we don't care about things
166  // which are false, as they leave our current state alone (provided
167  // we stared as 'false'!), and in AND mode, we keep our current
168  // state until someone returns 'false' (provided we started as 'true')
169  if ( m_modeOR ? passed : !passed ) {
170  seqPass = passed;
171  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is now " << ( seqPass ? "true" : "false" ) << endmsg;
172  if ( m_shortCircuit ) break;
173  }
174  }
175  }
176  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is " << ( seqPass ? "true" : "false" ) << endmsg;
177  if ( !m_ignoreFilter && !m_entries.empty() ) setFilterPassed( m_invert ? !seqPass : seqPass );
178  setExecuted( true );
179 
181 
182  return m_returnOK ? ( result.ignore(), StatusCode::SUCCESS ) : result;
183 }
184 
185 //=========================================================================
186 // Decode the input names and fills the m_algs vector.
187 //=========================================================================
189 {
191  m_entries.clear();
192 
193  //== Get the "Context" option if in the file...
194  auto jos = service<IJobOptionsSvc>( "JobOptionsSvc" );
195 
196  //= Get the Application manager, to see if algorithm exist
197  auto appMgr = service<IAlgManager>( "ApplicationMgr" );
198  for ( const auto& item : m_names.value() ) {
200  const std::string& theName = typeName.name();
201  const std::string& theType = typeName.type();
202 
203  //== Check wether the specified algorithm already exists. If not, create it
205  SmartIF<IAlgorithm> myIAlg = appMgr->algorithm( typeName, false ); // do not create it now
206  if ( !myIAlg ) {
207  // ensure some magic properties are set while we create the subalgorithm so
208  // that it effectively inherites 'our' settings -- if they have non-default
209  // values... and are not set explicitly already.
210  populate_JobOptionsSvc_t populate_guard{theName, jos, std::forward_as_tuple( "Context", context() ),
211  std::forward_as_tuple( "RootInTES", rootInTES() )};
212  Algorithm* myAlg = nullptr;
213  result = createSubAlgorithm( theType, theName, myAlg );
214  myIAlg = myAlg; // ensure that myIAlg.isValid() from here onwards!
215  } else {
216  Algorithm* myAlg = dynamic_cast<Algorithm*>( myIAlg.get() );
217  if ( myAlg ) {
218  subAlgorithms()->push_back( myAlg );
219  // when the algorithm is not created, the ref count is short by one, so we have to fix it.
220  myAlg->addRef();
221  }
222  }
223 
224  // propagate the sub-algorithm into own state.
225  if ( result.isSuccess() && Gaudi::StateMachine::INITIALIZED <= FSMState() && myIAlg &&
226  Gaudi::StateMachine::INITIALIZED > myIAlg->FSMState() ) {
227  StatusCode sc = myIAlg->sysInitialize();
228  if ( sc.isFailure() ) result = sc;
229  }
230 
231  // propagate the sub-algorithm into own state.
232  if ( result.isSuccess() && Gaudi::StateMachine::RUNNING <= FSMState() && myIAlg &&
233  Gaudi::StateMachine::RUNNING > myIAlg->FSMState() ) {
234  StatusCode sc = myIAlg->sysStart();
235  if ( sc.isFailure() ) result = sc;
236  }
237 
238  //== Is it an Algorithm ? Strange test...
239  if ( result.isSuccess() ) {
240  // TODO: (MCl) it is possible to avoid the dynamic_cast in most of the
241  // cases by keeping the result of createSubAlgorithm.
242  Algorithm* myAlg = dynamic_cast<Algorithm*>( myIAlg.get() );
243  if ( myAlg ) {
244  // Note: The reference counting is kept by the system of sub-algorithms
245  m_entries.emplace_back( myAlg );
246  if ( msgLevel( MSG::DEBUG ) ) debug() << "Added algorithm " << theName << endmsg;
247  } else {
248  warning() << theName << " is not an Algorithm - failed dynamic_cast" << endmsg;
249  final = StatusCode::FAILURE;
250  }
251  } else {
252  warning() << "Unable to find or create " << theName << endmsg;
253  final = result;
254  }
255  }
256 
257  //== Print the list of algorithms
258  MsgStream& msg = info();
259  if ( m_modeOR ) msg << "OR ";
260  msg << "Member list: ";
262  []( auto& os, const AlgorithmEntry& e ) -> decltype( auto ) {
263  Algorithm* alg = e.algorithm();
264  std::string typ = System::typeinfoName( typeid( *alg ) );
265  os << typ;
266  if ( alg->name() != typ ) os << "/" << alg->name();
267  return os;
268  } );
269  if ( !isDefault( context() ) ) msg << ", with context '" << context() << "'";
270  if ( !isDefault( rootInTES() ) ) msg << ", with rootInTES '" << rootInTES() << "'";
271  msg << endmsg;
272 
273  return final;
274 }
275 
276 //=========================================================================
277 // Interface for the Property manager
278 //=========================================================================
280 {
281  // no action for not-yet initialized sequencer
282  if ( Gaudi::StateMachine::INITIALIZED > FSMState() ) return; // RETURN
283 
284  decodeNames().ignore();
285 
286  if ( !m_measureTime ) return; // RETURN
287 
288  // add the entries into timer table:
289 
290  if ( !m_timerTool ) {
291  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
292  }
293 
294  if ( m_timerTool->globalTiming() ) m_measureTime = true;
295 
298 
299  for ( auto& entry : m_entries ) {
300  entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) );
301  }
302 
304 }
305 
307 {
308  if ( m_invert ) os << "~";
309  // the default filterpass value for an empty sequencer depends on ModeOR
310  if ( m_entries.empty() ) return os << ( ( !m_modeOR ) ? "CFTrue" : "CFFalse" );
311 
312  // if we have only one element, we do not need a name
313  if ( m_entries.size() > 1 ) os << "seq(";
314 
315  const auto op = m_modeOR ? " | " : " & ";
316  const auto last = end( m_entries );
317  const auto first = begin( m_entries );
318  for ( auto iterator = first; iterator != last; ++iterator ) {
319  if ( iterator != first ) os << op;
320  if ( iterator->reverse() ) os << "~";
321  iterator->algorithm()->toControlFlowExpression( os );
322  }
323 
324  if ( m_entries.size() > 1 ) os << ")";
325  return os;
326 }
327 //=============================================================================
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:850
T forward_as_tuple(T...args)
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:765
void setFilterPassed(bool state) const override
Set the filter passed flag to the specified state.
Definition: Algorithm.cpp:791
Implementation of property with value of concrete type.
Definition: Property.h:381
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: GaudiCommon.h:752
bool isExecuted() const override
Has this algorithm been executed since the last reset?
Definition: Algorithm.cpp:775
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:332
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
Definition: StatusCode.h:287
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:258
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:748
virtual void increaseIndent()=0
Increase the indentation of the name.
Algorithm * algorithm() const
bool isFailure() const
Definition: StatusCode.h:139
virtual bool globalTiming()=0
returns the flag telling that global timing is wanted
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
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:787
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:51
const EventContext & getContext() const override
get the context
Definition: Algorithm.h:436
The useful base class for data processing algorithms.
void membershipHandler(Gaudi::Details::PropertyBase &theProp)
for asynchronous changes in the list of algorithms
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
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)
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
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:801
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
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.
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:789
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:780
Gaudi::Property< bool > m_returnOK
GaudiSequencer(const std::string &name, ISvcLocator *pSvcLocator)
Standard constructor.
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)
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:545
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
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)