MinimalEventLoopMgr.cpp
Go to the documentation of this file.00001
00002 #define GAUDIKERNEL_MINIMALEVENTLOOPMGR_CPP
00003
00004 #include "GaudiKernel/SmartIF.h"
00005 #include "GaudiKernel/MsgStream.h"
00006 #include "GaudiKernel/IAlgorithm.h"
00007 #include "GaudiKernel/IAlgManager.h"
00008 #include "GaudiKernel/TypeNameString.h"
00009 #include "GaudiKernel/GaudiException.h"
00010 #include "GaudiKernel/ThreadGaudi.h"
00011 #include "GaudiKernel/Incident.h"
00012 #include "GaudiKernel/IIncidentListener.h"
00013 #include "GaudiKernel/IIncidentSvc.h"
00014
00015 #include "GaudiKernel/MinimalEventLoopMgr.h"
00016
00017 namespace {
00018 class AbortEventListener: public implements1<IIncidentListener> {
00019 public:
00020 AbortEventListener(bool &flag, std::string &source):m_flag(flag),
00021 m_source(source){
00022 addRef();
00023 }
00024 virtual ~AbortEventListener() {}
00026 virtual void handle(const Incident& i) {
00027 if (i.type() == IncidentType::AbortEvent) {
00028 m_flag = true;
00029 m_source = i.source();
00030 }
00031 }
00032
00033 private:
00035 bool &m_flag;
00037 std::string &m_source;
00038 };
00039 }
00040
00041
00042
00043
00044 MinimalEventLoopMgr::MinimalEventLoopMgr(const std::string& nam, ISvcLocator* svcLoc)
00045 : base_class(nam, svcLoc), m_appMgrUI(svcLoc)
00046 {
00047 declareProperty("TopAlg", m_topAlgNames );
00048 declareProperty("OutStream", m_outStreamNames );
00049 declareProperty("OutStreamType", m_outStreamType = "OutputStream");
00050 m_topAlgNames.declareUpdateHandler ( &MinimalEventLoopMgr::topAlgHandler, this );
00051 m_outStreamNames.declareUpdateHandler( &MinimalEventLoopMgr::outStreamHandler, this );
00052 m_state = OFFLINE;
00053 m_scheduledStop = false;
00054 m_abortEvent = false;
00055 }
00056
00057
00058
00059
00060 MinimalEventLoopMgr::~MinimalEventLoopMgr() {
00061 m_state = OFFLINE;
00062 }
00063
00064
00065
00066
00067 StatusCode MinimalEventLoopMgr::initialize() {
00068
00069 MsgStream log(msgSvc(), name());
00070
00071 if ( !m_appMgrUI.isValid() ) {
00072 return StatusCode::FAILURE;
00073 }
00074
00075 StatusCode sc = Service::initialize();
00076 if ( !sc.isSuccess() ) {
00077 log << MSG::ERROR << "Failed to initialize Service Base class." << endmsg;
00078 return StatusCode::FAILURE;
00079 }
00080
00081 SmartIF<IProperty> prpMgr(serviceLocator());
00082 if ( ! prpMgr.isValid() ) {
00083 log << MSG::ERROR << "Error retrieving AppMgr interface IProperty." << endmsg;
00084 return StatusCode::FAILURE;
00085 }
00086 else {
00087 if ( m_topAlgNames.value().size() == 0 ) {
00088 setProperty(prpMgr->getProperty("TopAlg")).ignore();
00089 }
00090 if ( m_outStreamNames.value().size() == 0 ) {
00091 setProperty(prpMgr->getProperty("OutStream")).ignore();
00092 }
00093 }
00094
00095
00096 m_incidentSvc = serviceLocator()->service("IncidentSvc");
00097 if( !m_incidentSvc.isValid() ) {
00098 log << MSG::FATAL << "Error retrieving IncidentSvc." << endmsg;
00099 return StatusCode::FAILURE;
00100 }
00101
00102 m_abortEventListener = new AbortEventListener(m_abortEvent,m_abortEventSource);
00103 m_incidentSvc->addListener(m_abortEventListener,IncidentType::AbortEvent);
00104
00105
00106 m_state = INITIALIZED;
00107
00108
00109
00110
00111
00112
00113 sc = decodeOutStreams();
00114 if ( !sc.isSuccess() ) {
00115 log << MSG::ERROR << "Failed to initialize Output streams." << endmsg;
00116 m_state = CONFIGURED;
00117 return sc;
00118 }
00119
00120
00121
00122
00123
00124 sc = decodeTopAlgs();
00125 if ( !sc.isSuccess() ) {
00126 log << MSG::ERROR << "Failed to initialize Top Algorithms streams." << endmsg;
00127 m_state = CONFIGURED;
00128 return sc;
00129 }
00130
00131 ListAlg::iterator ita;
00132
00133
00134 for (ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00135 sc = (*ita)->sysInitialize();
00136 if( !sc.isSuccess() ) {
00137 log << MSG::ERROR << "Unable to initialize Algorithm: " << (*ita)->name() << endmsg;
00138 return sc;
00139 }
00140 }
00141 for (ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00142 sc = (*ita)->sysInitialize();
00143 if( !sc.isSuccess() ) {
00144 log << MSG::ERROR << "Unable to initialize Output Stream: " << (*ita)->name() << endmsg;
00145 return sc;
00146 }
00147 }
00148
00149 return StatusCode::SUCCESS;
00150 }
00151
00152
00153
00154 StatusCode MinimalEventLoopMgr::start() {
00155
00156 StatusCode sc = Service::start();
00157 if ( ! sc.isSuccess() ) return sc;
00158
00159 MsgStream log(msgSvc(), name());
00160
00161 ListAlg::iterator ita;
00162
00163
00164 for (ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00165 sc = (*ita)->sysStart();
00166 if( !sc.isSuccess() ) {
00167 log << MSG::ERROR << "Unable to start Algorithm: " << (*ita)->name() << endmsg;
00168 return sc;
00169 }
00170 }
00171 for (ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00172 sc = (*ita)->sysStart();
00173 if( !sc.isSuccess() ) {
00174 log << MSG::ERROR << "Unable to start Output Stream: " << (*ita)->name() << endmsg;
00175 return sc;
00176 }
00177 }
00178 return StatusCode::SUCCESS;
00179 }
00180
00181
00182
00183 StatusCode MinimalEventLoopMgr::stop() {
00184
00185 StatusCode sc = StatusCode::SUCCESS;
00186
00187 MsgStream log(msgSvc(), name());
00188
00189 ListAlg::iterator ita;
00190
00191
00192 for (ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00193 sc = (*ita)->sysStop();
00194 if( !sc.isSuccess() ) {
00195 log << MSG::ERROR << "Unable to stop Algorithm: " << (*ita)->name() << endmsg;
00196 return sc;
00197 }
00198 }
00199 for (ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00200 sc = (*ita)->sysStop();
00201 if( !sc.isSuccess() ) {
00202 log << MSG::ERROR << "Unable to stop Output Stream: " << (*ita)->name() << endmsg;
00203 return sc;
00204 }
00205 }
00206
00207 return Service::stop();
00208 }
00209
00210
00211
00212
00213 StatusCode MinimalEventLoopMgr::reinitialize() {
00214 MsgStream log( msgSvc(), name() );
00215 StatusCode sc = StatusCode::SUCCESS;
00216 ListAlg::iterator ita;
00217
00218
00219 for (ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00220 sc = (*ita)->sysReinitialize();
00221 if( !sc.isSuccess() ) {
00222 log << MSG::ERROR << "Unable to reinitialize Algorithm: " << (*ita)->name() << endmsg;
00223 return sc;
00224 }
00225 }
00226 for (ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00227 sc = (*ita)->sysReinitialize();
00228 if( !sc.isSuccess() ) {
00229 log << MSG::ERROR << "Unable to reinitialize Output Stream: " << (*ita)->name() << endmsg;
00230 return sc;
00231 }
00232 }
00233
00234 return sc;
00235 }
00236
00237
00238
00239 StatusCode MinimalEventLoopMgr::restart() {
00240 MsgStream log( msgSvc(), name() );
00241 StatusCode sc = StatusCode::SUCCESS;
00242 ListAlg::iterator ita;
00243
00244
00245 for (ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00246 sc = (*ita)->sysRestart();
00247 if( !sc.isSuccess() ) {
00248 log << MSG::ERROR << "Unable to restart Algorithm: " << (*ita)->name() << endmsg;
00249 return sc;
00250 }
00251 }
00252 for (ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00253 sc = (*ita)->sysRestart();
00254 if( !sc.isSuccess() ) {
00255 log << MSG::ERROR << "Unable to restart Output Stream: " << (*ita)->name() << endmsg;
00256 return sc;
00257 }
00258 }
00259
00260 return sc;
00261 }
00262
00263
00264
00265
00266 StatusCode MinimalEventLoopMgr::finalize() {
00267 MsgStream log( msgSvc(), name() );
00268 StatusCode sc = StatusCode::SUCCESS;
00269 StatusCode scRet = StatusCode::SUCCESS;
00270
00271 ListAlg::iterator ita;
00272 for ( ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00273 sc = (*ita)->sysFinalize();
00274 if( !sc.isSuccess() ) {
00275 scRet = StatusCode::FAILURE;
00276 log << MSG::WARNING << "Finalization of algorithm " << (*ita)->name() << " failed" << endmsg;
00277 }
00278 }
00279
00280 for ( ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00281 sc = (*ita)->sysFinalize();
00282 if( !sc.isSuccess() ) {
00283 scRet = StatusCode::FAILURE;
00284 log << MSG::WARNING << "Finalization of algorithm " << (*ita)->name() << " failed" << endmsg;
00285 }
00286 }
00287
00288 SmartIF<IAlgManager> algMan(serviceLocator());
00289 for ( ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00290 if (algMan->removeAlgorithm(*ita).isFailure()) {
00291 scRet = StatusCode::FAILURE;
00292 log << MSG::ERROR << "Problems removing Algorithm " << (*ita)->name()
00293 << endmsg;
00294 }
00295 (*ita)->release();
00296 }
00297 m_topAlgList.clear();
00298
00299
00300 for ( ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00301 (*ita)->release();
00302 }
00303 m_outStreamList.clear();
00304 if ( sc.isSuccess() ) m_state = FINALIZED;
00305
00306 m_incidentSvc->removeListener(m_abortEventListener, IncidentType::AbortEvent);
00307 m_abortEventListener = 0;
00308
00309 m_incidentSvc = 0;
00310 m_appMgrUI = 0;
00311
00312 sc = Service::finalize();
00313
00314 if (sc.isFailure()) {
00315 scRet = StatusCode::FAILURE;
00316 log << MSG::ERROR << "Problems finalizing Service base class" << endmsg;
00317 }
00318
00319 return scRet;
00320 }
00321
00322
00323
00324
00325 StatusCode MinimalEventLoopMgr::nextEvent(int ) {
00326 MsgStream log(msgSvc(), name());
00327 log << MSG::ERROR << "This method cannot be called on an object of type "
00328 << System::typeinfoName(typeid(*this)) << endmsg;
00329 return StatusCode::FAILURE;
00330 }
00331
00332
00333
00334
00335 StatusCode MinimalEventLoopMgr::executeRun( int maxevt ) {
00336 StatusCode sc;
00337 MsgStream log( msgSvc(), name() );
00338 ListAlg::iterator ita;
00339 bool eventfailed = false;
00340
00341
00342 for (ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00343 sc = (*ita)->sysBeginRun();
00344 if( !sc.isSuccess() ) {
00345 log << MSG::WARNING << "beginRun() of algorithm " << (*ita)->name() << " failed" << endmsg;
00346 eventfailed = true;
00347 }
00348 }
00349
00350
00351 sc = nextEvent(maxevt);
00352 if( !sc.isSuccess() ) {
00353 eventfailed = true;
00354 }
00355
00356
00357 for (ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00358 sc = (*ita)->sysEndRun();
00359 if( !sc.isSuccess() ) {
00360 log << MSG::WARNING << "endRun() of algorithm " << (*ita)->name() << " failed" << endmsg;
00361 eventfailed = true;
00362 }
00363 }
00364
00365 if( eventfailed ){
00366 return StatusCode::FAILURE;
00367 }
00368 else {
00369 return StatusCode::SUCCESS;
00370 }
00371 }
00372
00373
00374
00375
00376 StatusCode MinimalEventLoopMgr::executeEvent(void* ) {
00377 bool eventfailed = false;
00378
00379
00380
00381 SmartIF<IAlgManager> algMan(serviceLocator());
00382 if ( algMan.isValid() ) {
00383 const ListAlgPtrs& allAlgs = algMan->getAlgorithms() ;
00384 for( ListAlgPtrs::const_iterator ialg = allAlgs.begin() ; allAlgs.end() != ialg ; ++ialg ) {
00385 if ( 0 != *ialg ) (*ialg)->resetExecuted();
00386 }
00387 }
00388
00389
00390 for (ListAlg::iterator ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00391 StatusCode sc(StatusCode::FAILURE);
00392 try {
00393 if (m_abortEvent){
00394 MsgStream log ( msgSvc() , name() );
00395 log << MSG::DEBUG << "AbortEvent incident fired by "
00396 << m_abortEventSource << endmsg;
00397 m_abortEvent = false;
00398 sc.ignore();
00399 break;
00400 }
00401 sc = (*ita)->sysExecute();
00402 } catch ( const GaudiException& Exception ) {
00403 MsgStream log ( msgSvc() , "MinimalEventLoopMgr.executeEvent()" );
00404 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
00405 << " thrown by " << (*ita)->name() << endmsg;
00406 log << MSG::ERROR << Exception << endmsg;
00407 } catch ( const std::exception& Exception ) {
00408 MsgStream log ( msgSvc() , "MinimalEventLoopMgr.executeEvent()" );
00409 log << MSG::FATAL << " Standard std::exception thrown by "
00410 << (*ita)->name() << endmsg;
00411 log << MSG::ERROR << Exception.what() << endmsg;
00412 } catch(...) {
00413 MsgStream log ( msgSvc() , "MinimalEventLoopMgr.executeEvent()" );
00414 log << MSG::FATAL << "UNKNOWN Exception thrown by "
00415 << (*ita)->name() << endmsg;
00416 }
00417
00418 if( !sc.isSuccess() ) {
00419 MsgStream log( msgSvc(), name() );
00420 log << MSG::WARNING << "Execution of algorithm " << (*ita)->name() << " failed" << endmsg;
00421 eventfailed = true;
00422 }
00423 }
00424
00425
00426 if (m_abortEvent){
00427 if (outputLevel() <= MSG::DEBUG) {
00428 MsgStream log ( msgSvc() , name() );
00429 log << MSG::DEBUG << "AbortEvent incident fired by "
00430 << m_abortEventSource << endmsg;
00431 }
00432 m_abortEvent = false;
00433 }
00434
00435
00436 for (ListAlg::iterator ito = m_outStreamList.begin(); ito != m_outStreamList.end(); ito++ ) {
00437 (*ito)->resetExecuted();
00438 StatusCode sc;
00439 sc = (*ito)->sysExecute();
00440 if( !sc.isSuccess() ) {
00441 MsgStream log( msgSvc(), name() );
00442 log << MSG::WARNING << "Execution of output stream " << (*ito)->name() << " failed" << endmsg;
00443 eventfailed = true;
00444 }
00445 }
00446
00447
00448 if( eventfailed ){
00449 MsgStream log( msgSvc(), name() );
00450 log << MSG::ERROR << "Error processing event loop." << endmsg;
00451 return StatusCode(StatusCode::FAILURE,true);
00452 }
00453 return StatusCode(StatusCode::SUCCESS,true);
00454 }
00455
00456
00457
00458 StatusCode MinimalEventLoopMgr::stopRun() {
00459 m_scheduledStop = true;
00460 return StatusCode::SUCCESS;
00461 }
00462
00463
00464
00465
00466 void MinimalEventLoopMgr::topAlgHandler( Property& ) {
00467 if ( !(decodeTopAlgs( )).isSuccess() ) {
00468 throw GaudiException("Failed to initialize Top Algorithms streams.",
00469 "MinimalEventLoopMgr::topAlgHandler",
00470 StatusCode::FAILURE);
00471 }
00472 }
00473
00474
00475
00476
00477 StatusCode MinimalEventLoopMgr::decodeTopAlgs() {
00478 StatusCode sc = StatusCode::SUCCESS;
00479 if ( CONFIGURED == m_state || INITIALIZED == m_state ) {
00480 SmartIF<IAlgManager> algMan(serviceLocator());
00481 MsgStream log(msgSvc(), name());
00482 if ( algMan.isValid()) {
00483
00484 m_topAlgList.clear( );
00485 const std::vector<std::string>& algNames = m_topAlgNames.value( );
00486 for (VectorName::const_iterator it = algNames.begin(); it != algNames.end(); it++) {
00487 Gaudi::Utils::TypeNameString item(*it);
00488
00489 std::string item_name = item.name() + getGaudiThreadIDfromName(name());
00490 const bool CREATE = false;
00491 SmartIF<IAlgorithm> alg = algMan->algorithm(item_name, CREATE);
00492 if (alg.isValid()) {
00493 log << MSG::DEBUG << "Top Algorithm " << item_name << " already exists" << endmsg;
00494 }
00495 else {
00496 log << MSG::DEBUG << "Creating Top Algorithm " << item.type() << " with name " << item_name << endmsg;
00497 IAlgorithm *ialg = 0;
00498 StatusCode sc = algMan->createAlgorithm(item.type(), item_name, ialg);
00499 if( !sc.isSuccess() ) {
00500 log << MSG::ERROR << "Unable to create Top Algorithm " << item.type() << " with name " << item_name << endmsg;
00501 return sc;
00502 }
00503 alg = ialg;
00504 }
00505 m_topAlgList.push_back(alg);
00506 }
00507 return sc;
00508 }
00509 sc = StatusCode::FAILURE;
00510 }
00511 return sc;
00512 }
00513
00514
00515
00516
00517 void MinimalEventLoopMgr::outStreamHandler( Property& ) {
00518 if ( !(decodeOutStreams( )).isSuccess() ) {
00519 throw GaudiException("Failed to initialize output streams.",
00520 "MinimalEventLoopMgr::outStreamHandler",
00521 StatusCode::FAILURE);
00522 }
00523 }
00524
00525
00526
00527
00528 StatusCode MinimalEventLoopMgr::decodeOutStreams( ) {
00529 StatusCode sc = StatusCode::SUCCESS;
00530 if ( CONFIGURED == m_state || INITIALIZED == m_state ) {
00531 MsgStream log(msgSvc(), name());
00532 SmartIF<IAlgManager> algMan(serviceLocator());
00533 if ( algMan.isValid() ) {
00534
00535 m_outStreamList.clear();
00536 const std::vector<std::string>& algNames = m_outStreamNames.value( );
00537 for (VectorName::const_iterator it = algNames.begin(); it != algNames.end(); it++) {
00538 Gaudi::Utils::TypeNameString item(*it, m_outStreamType);
00539 log << MSG::DEBUG << "Creating " << m_outStreamType << (*it) << endmsg;
00540 const bool CREATE = false;
00541 SmartIF<IAlgorithm> os = algMan->algorithm( item, CREATE );
00542 if (os.isValid()) {
00543 log << MSG::DEBUG << "Output Stream " << item.name() << " already exists" << endmsg;
00544 }
00545 else {
00546 log << MSG::DEBUG << "Creating Output Stream " << (*it) << endmsg;
00547 IAlgorithm* ios = 0;
00548 StatusCode sc = algMan->createAlgorithm( item.type(), item.name(), ios );
00549 if( !sc.isSuccess() ) {
00550 log << MSG::ERROR << "Unable to create Output Stream " << (*it) << endmsg;
00551 return sc;
00552 }
00553 os = ios;
00554 }
00555 m_outStreamList.push_back( os );
00556 }
00557 return sc;
00558 }
00559 sc = StatusCode::FAILURE;
00560 }
00561 return sc;
00562 }
00563
00564