Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GaudiSequencer.cpp
Go to the documentation of this file.
1 // Include files
2 #include <initializer_list>
3 #include <tuple>
4 
5 // from Gaudi
11 
12 #include "GaudiCommon.icpp"
13 template class GaudiCommon<Gaudi::Sequence>;
14 
15 namespace {
16 
17  bool isDefault( const std::string& s ) { return s.empty(); }
18 
19  template <typename Container>
20  bool veto( const Container* props, const char* name ) { // avoid changing properties explicitly present in the JOS...
21  return props &&
22  std::any_of( begin( *props ), end( *props ), [name]( const auto* prop ) { return prop->name() == name; } );
23  }
24 
25  template <typename F, typename... Args>
26  void for_each_arg( F&& f, Args&&... args ) {
27  // the std::initializer_list only exists to provide a 'context' in which to
28  // expand the variadic pack
29  (void)std::initializer_list<int>{( f( std::forward<Args>( args ) ), 0 )...};
30  }
31 
32  // utility class to populate some properties in the job options service
33  // for a given instance name in case those options are not explicitly
34  // set a-priori (effectively inheriting their values from the GaudiSequencer)
35  class populate_JobOptionsSvc_t {
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  const auto& key = std::get<0>( arg );
43  const auto& value = std::get<1>( arg );
44  if ( isDefault( value ) || veto( props, key ) ) return;
45  m_jos->addPropertyToCatalogue( m_name, Gaudi::Property<std::decay_t<Value>>{key, value} ).ignore();
46  m_props.push_back( key );
47  }
48 
49  public:
50  template <typename... Args>
51  populate_JobOptionsSvc_t( std::string name, IJobOptionsSvc* jos, Args&&... args )
52  : m_jos{jos}, m_name{std::move( name )} {
53  const auto* props = m_jos->getProperties( m_name );
54  for_each_arg( [&]( auto&& arg ) { this->addPropertyToCatalogue( props, std::forward<decltype( arg )>( arg ) ); },
55  std::forward<Args>( args )... );
56  }
57  ~populate_JobOptionsSvc_t() {
58  std::for_each( begin( m_props ), end( m_props ),
59  [&]( const std::string& key ) { m_jos->removePropertyFromCatalogue( m_name, key ).ignore(); } );
60  }
61  };
62 } // namespace
63 
64 //-----------------------------------------------------------------------------
65 // Implementation file for class : GaudiSequencer
66 //
67 // 2004-05-13 : Olivier Callot
68 //-----------------------------------------------------------------------------
69 
70 //=============================================================================
71 // Initialisation. Check parameters
72 //=============================================================================
74  // Note: not calling base class initialize because we want to reimplement the loop over sub algs
75  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialise" << endmsg;
76 
77  auto status = decodeNames();
78  if ( !status.isSuccess() ) return status;
79 
80  m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
81 
82  if ( m_timerTool->globalTiming() ) m_measureTime = true;
83 
84  if ( m_measureTime ) {
85  m_timer = m_timerTool->addTimer( name() );
86  m_timerTool->increaseIndent();
87  } else {
88  release( m_timerTool );
89  m_timerTool = nullptr;
90  }
91 
92  //== Initialize the algorithms
93  for ( auto& entry : m_entries ) {
94  if ( m_measureTime ) { entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) ); }
95 
96  status = entry.algorithm()->sysInitialize();
97  if ( !status.isSuccess() ) { return Error( "Can not initialize " + entry.algorithm()->name(), status ); }
98  }
99  if ( m_measureTime ) m_timerTool->decreaseIndent();
100 
101  return StatusCode::SUCCESS;
102 }
103 
104 //=============================================================================
105 // Main execution
106 //=============================================================================
108 
109  if ( m_measureTime ) m_timerTool->start( m_timer );
110 
111  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
112 
113  StatusCode result = StatusCode( StatusCode::SUCCESS, true );
114 
115  bool seqPass = !m_modeOR; // for OR, result will be false, unless (at least) one is true
116  // for AND, result will be true, unless (at least) one is false
117  // also see comment below ....
118 
119  for ( auto& entry : m_entries ) {
120  Gaudi::Algorithm* myAlg = entry.algorithm();
121  if ( !myAlg->isEnabled() ) continue;
122  if ( myAlg->execState( ctx ).state() != AlgExecState::State::Done ) {
123 
124  if ( m_measureTime ) m_timerTool->start( entry.timer() );
125  result = myAlg->sysExecute( ctx );
126  if ( m_measureTime ) m_timerTool->stop( entry.timer() );
127  if ( !result.isSuccess() ) break; //== Abort and return bad status
128  }
129  //== Check the returned status
130  if ( !m_ignoreFilter ) {
131  bool passed = myAlg->execState( ctx ).filterPassed();
132  if ( msgLevel( MSG::VERBOSE ) )
133  verbose() << "Algorithm " << myAlg->name() << " returned filter passed " << ( passed ? "true" : "false" )
134  << endmsg;
135  if ( entry.reverse() ) passed = !passed;
136 
137  //== indicate our own result. For OR, exit as soon as true.
138  // If no more, will exit with false.
139  //== for AND, exit as soon as false. Else, will be true (default)
140 
141  // if not short-circuiting, make sure we latch iPass to 'true' in
142  // OR mode (i.e. it is sufficient for one item to be true in order
143  // to be true at the end, and thus we start out at 'false'), and latch
144  // to 'false' in AND mode (i.e. it is sufficient for one item to
145  // be false to the false in the end, and thus we start out at 'true')
146  // -- i.e. we should not just blindly return the 'last' passed status!
147 
148  // or to put it another way: in OR mode, we don't care about things
149  // which are false, as they leave our current state alone (provided
150  // we stared as 'false'!), and in AND mode, we keep our current
151  // state until someone returns 'false' (provided we started as 'true')
152  if ( m_modeOR ? passed : !passed ) {
153  seqPass = passed;
154  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is now " << ( seqPass ? "true" : "false" ) << endmsg;
155  if ( m_shortCircuit ) break;
156  }
157  }
158  }
159  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "SeqPass is " << ( seqPass ? "true" : "false" ) << endmsg;
160  auto& state = execState( ctx );
161  if ( !m_ignoreFilter && !m_entries.empty() ) state.setFilterPassed( m_invert ? !seqPass : seqPass );
162  state.setState( AlgExecState::State::Done );
163 
164  if ( m_measureTime ) m_timerTool->stop( m_timer );
165 
166  return m_returnOK ? ( result.ignore(), StatusCode::SUCCESS ) : result;
167 }
168 
169 //=========================================================================
170 // Decode the input names and fills the m_algs vector.
171 //=========================================================================
174  m_entries.clear();
175 
176  //== Get the "Context" option if in the file...
177  auto jos = service<IJobOptionsSvc>( "JobOptionsSvc" );
178 
179  //= Get the Application manager, to see if algorithm exist
180  auto appMgr = service<IAlgManager>( "ApplicationMgr" );
181  for ( const auto& item : m_names.value() ) {
183  const std::string& theName = typeName.name();
184  const std::string& theType = typeName.type();
185 
186  //== Check wether the specified algorithm already exists. If not, create it
188  SmartIF<IAlgorithm> myIAlg = appMgr->algorithm( typeName, false ); // do not create it now
189  if ( !myIAlg ) {
190  // ensure some magic properties are set while we create the subalgorithm so
191  // that it effectively inherites 'our' settings -- if they have non-default
192  // values... and are not set explicitly already.
193  populate_JobOptionsSvc_t populate_guard{theName, jos, std::forward_as_tuple( "Context", context() ),
194  std::forward_as_tuple( "RootInTES", rootInTES() )};
195  Gaudi::Algorithm* myAlg = nullptr;
196  result = createSubAlgorithm( theType, theName, myAlg );
197  myIAlg = myAlg; // ensure that myIAlg.isValid() from here onwards!
198  } else {
199  Gaudi::Algorithm* myAlg = dynamic_cast<Gaudi::Algorithm*>( myIAlg.get() );
200  if ( myAlg ) {
201  subAlgorithms()->push_back( myAlg );
202  // when the algorithm is not created, the ref count is short by one, so we have to fix it.
203  myAlg->addRef();
204  }
205  }
206 
207  // propagate the sub-algorithm into own state.
208  if ( result.isSuccess() && Gaudi::StateMachine::INITIALIZED <= FSMState() && myIAlg &&
209  Gaudi::StateMachine::INITIALIZED > myIAlg->FSMState() ) {
210  StatusCode sc = myIAlg->sysInitialize();
211  if ( sc.isFailure() ) result = sc;
212  }
213 
214  // propagate the sub-algorithm into own state.
215  if ( result.isSuccess() && Gaudi::StateMachine::RUNNING <= FSMState() && myIAlg &&
216  Gaudi::StateMachine::RUNNING > myIAlg->FSMState() ) {
217  StatusCode sc = myIAlg->sysStart();
218  if ( sc.isFailure() ) result = sc;
219  }
220 
221  //== Is it an Algorithm ? Strange test...
222  if ( result.isSuccess() ) {
223  // TODO: (MCl) it is possible to avoid the dynamic_cast in most of the
224  // cases by keeping the result of createSubAlgorithm.
225  Gaudi::Algorithm* myAlg = dynamic_cast<Gaudi::Algorithm*>( myIAlg.get() );
226  if ( myAlg ) {
227  // Note: The reference counting is kept by the system of sub-algorithms
228  m_entries.emplace_back( myAlg );
229  if ( msgLevel( MSG::DEBUG ) ) debug() << "Added algorithm " << theName << endmsg;
230  } else {
231  warning() << theName << " is not a Gaudi::Algorithm - failed dynamic_cast" << endmsg;
232  final = StatusCode::FAILURE;
233  }
234  } else {
235  warning() << "Unable to find or create " << theName << endmsg;
236  final = result;
237  }
238  }
239 
240  //== Print the list of algorithms
241  MsgStream& msg = info();
242  if ( m_modeOR ) msg << "OR ";
243  msg << "Member list: ";
244  GaudiUtils::details::ostream_joiner( msg, m_entries, ", ",
245  []( auto& os, const AlgorithmEntry& e ) -> decltype( auto ) {
246  Gaudi::Algorithm* alg = e.algorithm();
247  std::string typ = System::typeinfoName( typeid( *alg ) );
248  os << typ;
249  if ( alg->name() != typ ) os << "/" << alg->name();
250  return os;
251  } );
252  if ( !isDefault( context() ) ) msg << ", with context '" << context() << "'";
253  if ( !isDefault( rootInTES() ) ) msg << ", with rootInTES '" << rootInTES() << "'";
254  msg << endmsg;
255 
256  return final;
257 }
258 
259 //=========================================================================
260 // Interface for the Property manager
261 //=========================================================================
263  // no action for not-yet initialized sequencer
264  if ( Gaudi::StateMachine::INITIALIZED > FSMState() ) return; // RETURN
265 
266  decodeNames().ignore();
267 
268  if ( !m_measureTime ) return; // RETURN
269 
270  // add the entries into timer table:
271 
272  if ( !m_timerTool ) { m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" ); }
273 
274  if ( m_timerTool->globalTiming() ) m_measureTime = true;
275 
276  m_timer = m_timerTool->addTimer( name() );
277  m_timerTool->increaseIndent();
278 
279  for ( auto& entry : m_entries ) { entry.setTimer( m_timerTool->addTimer( entry.algorithm()->name() ) ); }
280 
281  m_timerTool->decreaseIndent();
282 }
283 
285  if ( m_invert ) os << "~";
286  // the default filterpass value for an empty sequencer depends on ModeOR
287  if ( m_entries.empty() ) return os << ( ( !m_modeOR ) ? "CFTrue" : "CFFalse" );
288 
289  // if we have only one element, we do not need a name
290  if ( m_entries.size() > 1 ) os << "seq(";
291 
292  const auto op = m_modeOR ? " | " : " & ";
293  const auto last = end( m_entries );
294  const auto first = begin( m_entries );
295  for ( auto iterator = first; iterator != last; ++iterator ) {
296  if ( iterator != first ) os << op;
297  if ( iterator->reverse() ) os << "~";
298  iterator->algorithm()->toControlFlowExpression( os );
299  }
300 
301  if ( m_entries.size() > 1 ) os << ")";
302  return os;
303 }
304 
305 // ============================================================================
308 
309  IAlgContextSvc* algCtx = nullptr;
310  if ( registerContext() ) { algCtx = contextSvc(); }
311  // Lock the context
312  Gaudi::Utils::AlgContext cnt( this, algCtx, ctx );
313 
314  // Do not execute if one or more of the m_vetoObjs exist in TES
315  const auto it = find_if( begin( m_vetoObjs ), end( m_vetoObjs ),
316  [&]( const std::string& loc ) { return this->exist<DataObject>( evtSvc(), loc ); } );
317  if ( it != end( m_vetoObjs ) ) {
318  if ( msgLevel( MSG::DEBUG ) ) debug() << *it << " found, skipping event " << endmsg;
319  return sc;
320  }
321 
322  // Execute if m_requireObjs is empty
323  // or if one or more of the m_requireObjs exist in TES
324  bool doIt = m_requireObjs.empty() ||
325  any_of( begin( m_requireObjs ), end( m_requireObjs ),
326  [&]( const std::string& loc ) { return this->exist<DataObject>( evtSvc(), loc ); } );
327 
328  // execute the generic method:
329  if ( doIt ) sc = GaudiCommon<Gaudi::Sequence>::sysExecute( ctx );
330  return sc;
331 }
332 //=============================================================================
bool isEnabled() const override
Is this algorithm enabled or disabled?
Definition: Algorithm.cpp:645
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
T empty(T...args)
Gaudi::Algorithm * algorithm() const
T forward_as_tuple(T...args)
Helper "sentry" class to automatize the safe register/unregister the algorithm&#39;s context.
Implementation of property with value of concrete type.
Definition: Property.h:352
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:182
StatusCode initialize() override
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:309
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:635
bool isSuccess() const
Definition: StatusCode.h:267
bool PyHelper() addPropertyToCatalogue(IInterface *p, char *comp, char *name, char *value)
Definition: Bootstrap.cpp:245
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
class MergingTransformer< Out(const vector_of_const_< In > void
This class represents an entry point to all the event specific data.
Definition: EventContext.h:31
bool isFailure() const
Definition: StatusCode.h:130
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.
AlgExecState & execState(const EventContext &ctx) const override
reference to AlgExecState of Alg
Definition: Algorithm.cpp:647
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
T push_back(T...args)
StatusCode sysExecute(const EventContext &ctx) override
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:50
StatusCode execute(const EventContext &ctx) const override
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)
StatusCode decodeNames()
Decode a vector of string.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:37
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:153
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
appMgr
Definition: IOTest.py:92
string s
Definition: gaudirun.py:312
constexpr static const auto FAILURE
Definition: StatusCode.h:86
An abstract interface for Algorithm Context Service.
bool filterPassed() const
AttribStringParser::Iterator begin(const AttribStringParser &parser)
const std::string & name() const
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:21
T for_each(T...args)
STL class.
State state() const
T forward(T...args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
StatusCode sysExecute(const EventContext &ctx) override
The actions to be performed by the algorithm on an event.
Definition: Algorithm.cpp:438