![]() |
|
|
Generated: 8 Jan 2009 |
00001 //$Id: Algorithm.cpp,v 1.42 2008/10/23 15:57:37 marcocle Exp $ 00002 #include "GaudiKernel/Kernel.h" 00003 #include "GaudiKernel/ISvcLocator.h" 00004 #include "GaudiKernel/IMessageSvc.h" 00005 #include "GaudiKernel/IJobOptionsSvc.h" 00006 #include "GaudiKernel/IAlgManager.h" 00007 #include "GaudiKernel/IAuditorSvc.h" 00008 #include "GaudiKernel/IDataProviderSvc.h" 00009 #include "GaudiKernel/IConversionSvc.h" 00010 #include "GaudiKernel/IHistogramSvc.h" 00011 #include "GaudiKernel/INTupleSvc.h" 00012 #include "GaudiKernel/IRndmGenSvc.h" 00013 #include "GaudiKernel/IToolSvc.h" 00014 #include "GaudiKernel/IExceptionSvc.h" 00015 #include "GaudiKernel/IProperty.h" 00016 00017 #include "GaudiKernel/Algorithm.h" 00018 #include "GaudiKernel/PropertyMgr.h" 00019 #include "GaudiKernel/MsgStream.h" 00020 #include "GaudiKernel/Chrono.h" 00021 #include "GaudiKernel/Stat.h" 00022 #include "GaudiKernel/GaudiException.h" 00023 #include "GaudiKernel/ServiceLocatorHelper.h" 00024 #include "GaudiKernel/ThreadGaudi.h" 00025 #include "GaudiKernel/Guards.h" 00026 00027 // Constructor 00028 Algorithm::Algorithm( const std::string& name, ISvcLocator *pSvcLocator, 00029 const std::string& version) 00030 : m_refCount(0), 00031 m_name(name), 00032 m_version(version), 00033 m_MS(0), 00034 m_EDS(0), 00035 m_ECS(0), 00036 m_DDS(0), 00037 m_DCS(0), 00038 m_HDS(0), 00039 m_NTS(0), 00040 //AIDATupleSvc: 00041 // m_ATS(0), 00042 m_CSS(0), 00043 m_RGS(0), 00044 m_EXS(0), 00045 m_pAuditorSvc(0), 00046 m_ptoolSvc(0), 00047 m_pMonitorSvc(NULL), 00048 m_pSvcLocator(pSvcLocator), 00049 m_filterPassed(true), 00050 m_isEnabled(true), 00051 m_isExecuted(false), 00052 m_state(Gaudi::StateMachine::CONFIGURED), 00053 m_targetState(Gaudi::StateMachine::CONFIGURED) 00054 { 00055 m_propertyMgr = new PropertyMgr(); 00056 m_subAlgms = new std::vector<Algorithm *>(); 00057 00058 // Declare common Algorithm properties with their defaults 00059 declareProperty( "OutputLevel", m_outputLevel = MSG::NIL); 00060 declareProperty( "Enable", m_isEnabled = true); 00061 declareProperty( "ErrorMax", m_errorMax = 1); 00062 declareProperty( "ErrorCount", m_errorCount = 0); 00063 // Auditor monitoring properties 00064 00065 // Get the default setting for service auditing from the AppMgr 00066 declareProperty( "AuditAlgorithms", m_auditInit ); 00067 00068 IProperty *appMgr; 00069 bool audit(false); 00070 if (serviceLocator()->service("ApplicationMgr", appMgr, false).isSuccess()) { 00071 const Property& prop = appMgr->getProperty("AuditAlgorithms"); 00072 Property &pr = const_cast<Property&>(prop); 00073 if (m_name != "IncidentSvc") { 00074 setProperty( pr ).ignore(); 00075 } 00076 audit = m_auditInit.value(); 00077 } 00078 00079 declareProperty( "AuditInitialize" , m_auditorInitialize = audit ) ; 00080 declareProperty( "AuditReinitialize", m_auditorReinitialize = audit ) ; 00081 declareProperty( "AuditRestart" , m_auditorRestart = audit ) ; 00082 declareProperty( "AuditExecute" , m_auditorExecute = audit ) ; 00083 declareProperty( "AuditFinalize" , m_auditorFinalize = audit ) ; 00084 declareProperty( "AuditBeginRun" , m_auditorBeginRun = audit ) ; 00085 declareProperty( "AuditEndRun" , m_auditorEndRun = audit ) ; 00086 declareProperty( "AuditStart" , m_auditorStart = audit ) ; 00087 declareProperty( "AuditStop" , m_auditorStop = audit ) ; 00088 00089 declareProperty( "MonitorService" , m_monitorSvcName = "MonitorSvc" ); 00090 00091 // update handlers. 00092 m_outputLevel.declareUpdateHandler(&Algorithm::initOutputLevel, this); 00093 00094 } 00095 00096 // Default Destructor 00097 Algorithm::~Algorithm() { 00098 delete m_subAlgms; 00099 delete m_propertyMgr; 00100 if ( m_MS ) m_MS->release(); 00101 if ( m_EDS ) m_EDS->release(); 00102 if ( m_ECS ) m_ECS->release(); 00103 if ( m_DDS ) m_DDS->release(); 00104 if ( m_DCS ) m_DCS->release(); 00105 if ( m_HDS ) m_HDS->release(); 00106 if ( m_NTS ) m_NTS->release(); 00107 if ( m_CSS ) m_CSS->release(); 00108 if ( m_RGS ) m_RGS->release(); 00109 if ( m_pAuditorSvc ) m_pAuditorSvc->release(); 00110 if ( m_ptoolSvc ) m_ptoolSvc->release(); 00111 if ( m_pMonitorSvc ) { m_pMonitorSvc->undeclareAll(this); m_pMonitorSvc->release(); } 00112 } 00113 00114 // IAlgorithm implementation 00115 StatusCode Algorithm::sysInitialize() { 00116 00117 // Bypass the initialization if the algorithm 00118 // has already been initialized. 00119 if ( Gaudi::StateMachine::INITIALIZED <= FSMState() ) return StatusCode::SUCCESS; 00120 00121 // Set the Algorithm's properties 00122 StatusCode sc = setProperties(); 00123 if( sc.isFailure() ) return StatusCode::FAILURE; 00124 00125 // Bypass the initialization if the algorithm is disabled. 00126 // Need to do this after setProperties. 00127 if ( !isEnabled( ) ) return StatusCode::SUCCESS; 00128 00129 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::INITIALIZE,m_state); 00130 00131 // Check current outputLevel to evetually inform the MessagsSvc 00132 //if( m_outputLevel != MSG::NIL ) { 00133 setOutputLevel( m_outputLevel ); 00134 //} 00135 00136 // TODO: (MCl) where shoud we do this? initialize or start? 00137 // Reset Error count 00138 //m_errorCount = 0; 00139 00140 // Invoke initialize() method of the derived class inside a try/catch clause 00141 try { 00142 { // limit the scope of the guard 00143 Gaudi::Guards::AuditorGuard guard(this, 00144 // check if we want to audit the initialize 00145 (m_auditorInitialize) ? auditorSvc() : 0, 00146 IAuditor::Initialize); 00147 // Invoke the initialize() method of the derived class 00148 sc = initialize(); 00149 } 00150 if( sc.isFailure() ) return StatusCode::FAILURE; 00151 00152 // Now initialize care of any sub-algorithms 00153 std::vector<Algorithm *>::iterator it; 00154 StatusCode result = StatusCode::SUCCESS; 00155 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) { 00156 sc = (*it)->sysInitialize(); 00157 if( sc.isFailure() ) result = sc; 00158 } 00159 if( result.isFailure() ) { 00160 MsgStream log ( msgSvc() , name() + ".sysInitialize()" ); 00161 log << MSG::ERROR << " Error initializing one or several sub-algorithms" 00162 << endreq; 00163 return result; 00164 } 00165 // Update the state. 00166 m_state = m_targetState; 00167 return StatusCode::SUCCESS; 00168 } 00169 catch ( const GaudiException& Exception ) { 00170 MsgStream log ( msgSvc() , name() + ".sysInitialize()" ); 00171 log << MSG::FATAL << " Exception with tag=" << Exception.tag() 00172 << " is caught " << endreq; 00173 log << MSG::ERROR << Exception << endreq; 00174 Stat stat( chronoSvc() , Exception.tag() ); 00175 } 00176 catch( const std::exception& Exception ) { 00177 MsgStream log ( msgSvc() , name() + ".sysInitialize()" ); 00178 log << MSG::FATAL << " Standard std::exception is caught " << endreq; 00179 log << MSG::ERROR << Exception.what() << endreq; 00180 Stat stat( chronoSvc() , "*std::exception*" ); 00181 } 00182 catch(...) { 00183 MsgStream log ( msgSvc() , name() + ".sysInitialize()" ); 00184 log << MSG::FATAL << "UNKNOWN Exception is caught " << endreq; 00185 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00186 } 00187 00188 return StatusCode::FAILURE; 00189 } 00190 00191 // IAlgorithm implementation 00192 StatusCode Algorithm::sysStart() { 00193 00194 // Bypass the startup if already running or disabled. 00195 if ( Gaudi::StateMachine::RUNNING == FSMState() || 00196 !isEnabled() ) return StatusCode::SUCCESS; 00197 00198 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::START,m_state); 00199 00200 // TODO: (MCl) where shoud we do this? initialize or start? 00201 // Reset Error count 00202 m_errorCount = 0; 00203 00204 StatusCode sc; 00205 // Invoke start() method of the derived class inside a try/catch clause 00206 try { 00207 { // limit the scope of the guard 00208 Gaudi::Guards::AuditorGuard guard(this, 00209 // check if we want to audit the initialize 00210 (m_auditorStart) ? auditorSvc() : 0, 00211 IAuditor::Start); 00212 // Invoke the start() method of the derived class 00213 sc = start(); 00214 } 00215 if( sc.isFailure() ) return StatusCode::FAILURE; 00216 00217 // Now start any sub-algorithms 00218 std::vector<Algorithm *>::iterator it; 00219 StatusCode result = StatusCode::SUCCESS; 00220 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) { 00221 sc = (*it)->sysStart(); 00222 if( sc.isFailure() ) result = sc; 00223 } 00224 if( result.isFailure() ) { 00225 MsgStream log ( msgSvc() , name() + ".sysInitialize()" ); 00226 log << MSG::ERROR << " Error initializing one or several sub-algorithms" 00227 << endreq; 00228 return result; 00229 } 00230 // Update the state. 00231 m_state = m_targetState; 00232 return StatusCode::SUCCESS; 00233 } 00234 catch ( const GaudiException& Exception ) { 00235 MsgStream log ( msgSvc() , name() ); 00236 log << MSG::FATAL << "in sysStart(): exception with tag=" << Exception.tag() 00237 << " is caught" << endreq; 00238 log << MSG::ERROR << Exception << endreq; 00239 Stat stat( chronoSvc() , Exception.tag() ); 00240 } 00241 catch( const std::exception& Exception ) { 00242 MsgStream log ( msgSvc() , name() ); 00243 log << MSG::FATAL << "in sysStart(): standard std::exception is caught" << endreq; 00244 log << MSG::ERROR << Exception.what() << endreq; 00245 Stat stat( chronoSvc() , "*std::exception*" ); 00246 } 00247 catch(...) { 00248 MsgStream log ( msgSvc() , name() ); 00249 log << MSG::FATAL << "in sysStart(): UNKNOWN Exception is caught" << endreq; 00250 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00251 } 00252 00253 return StatusCode::FAILURE; 00254 } 00255 00256 // IAlgorithm implementation 00257 StatusCode Algorithm::sysReinitialize() { 00258 00259 // Bypass the initialization if the algorithm is disabled. 00260 if ( !isEnabled( ) ) return StatusCode::SUCCESS; 00261 00262 // Check that the current status is the correct one. 00263 if ( Gaudi::StateMachine::INITIALIZED != FSMState() ) { 00264 MsgStream log ( msgSvc() , name() ); 00265 log << MSG::ERROR 00266 << "sysReinitialize(): cannot reinitialize algorithm not initialized" 00267 << endreq; 00268 return StatusCode::FAILURE; 00269 } 00270 00271 // Check current outputLevel to evetually inform the MessagsSvc 00272 //if( m_outputLevel != MSG::NIL ) { 00273 setOutputLevel( m_outputLevel ); 00274 //} 00275 00276 // Reset Error count 00277 // m_errorCount = 0; // done during start 00278 00279 StatusCode sc(StatusCode::SUCCESS,true); 00280 // Invoke reinitialize() method of the derived class inside a try/catch clause 00281 try { 00282 { // limit the scope of the guard 00283 Gaudi::Guards::AuditorGuard guard(this, 00284 // check if we want to audit the initialize 00285 (m_auditorReinitialize) ? auditorSvc() : 0, 00286 IAuditor::ReInitialize); 00287 // Invoke the reinitialize() method of the derived class 00288 sc = reinitialize(); 00289 } 00290 if( sc.isFailure() ) return StatusCode::FAILURE; 00291 00292 // Now initialize care of any sub-algorithms 00293 std::vector<Algorithm *>::iterator it; 00294 StatusCode result = StatusCode::SUCCESS; 00295 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) { 00296 sc = (*it)->sysReinitialize(); 00297 if( sc.isFailure() ) result = sc; 00298 } 00299 if( result.isFailure() ) { 00300 MsgStream log ( msgSvc() , name() ); 00301 log << MSG::ERROR 00302 << "sysReinitialize(): Error reinitializing one or several sub-algorithms" 00303 << endreq; 00304 return result; 00305 } 00306 return StatusCode::SUCCESS; 00307 } 00308 catch ( const GaudiException& Exception ) { 00309 MsgStream log ( msgSvc() , name() ); 00310 log << MSG::FATAL << "sysReinitialize(): Exception with tag=" << Exception.tag() 00311 << " is caught" << endreq; 00312 log << MSG::ERROR << Exception << endreq; 00313 Stat stat( chronoSvc() , Exception.tag() ); 00314 } 00315 catch( const std::exception& Exception ) { 00316 MsgStream log ( msgSvc() , name() ); 00317 log << MSG::FATAL << "sysReinitialize(): Standard std::exception is caught" << endreq; 00318 log << MSG::ERROR << Exception.what() << endreq; 00319 Stat stat( chronoSvc() , "*std::exception*" ); 00320 } 00321 catch(...) { 00322 MsgStream log ( msgSvc() , name() ); 00323 log << MSG::FATAL << "sysReinitialize(): UNKNOWN Exception is caught" << endreq; 00324 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00325 } 00326 00327 return StatusCode::FAILURE; 00328 } 00329 00330 // IAlgorithm implementation 00331 StatusCode Algorithm::sysRestart() { 00332 00333 // Bypass the initialization if the algorithm is disabled. 00334 if ( !isEnabled( ) ) return StatusCode::SUCCESS; 00335 00336 // Check that the current status is the correct one. 00337 if ( Gaudi::StateMachine::RUNNING != FSMState() ) { 00338 MsgStream log ( msgSvc() , name() ); 00339 log << MSG::ERROR 00340 << "sysRestart(): cannot restart algorithm not started" 00341 << endreq; 00342 return StatusCode::FAILURE; 00343 } 00344 00345 // Check current outputLevel to evetually inform the MessagsSvc 00346 //if( m_outputLevel != MSG::NIL ) { 00347 setOutputLevel( m_outputLevel ); 00348 //} 00349 00350 // Reset Error count 00351 m_errorCount = 0; 00352 00353 StatusCode sc(StatusCode::SUCCESS,true); 00354 // Invoke reinitialize() method of the derived class inside a try/catch clause 00355 try { 00356 { // limit the scope of the guard 00357 Gaudi::Guards::AuditorGuard guard(this, 00358 // check if we want to audit the initialize 00359 (m_auditorRestart) ? auditorSvc() : 0, 00360 IAuditor::ReStart); 00361 // Invoke the reinitialize() method of the derived class 00362 sc = restart(); 00363 } 00364 if( sc.isFailure() ) return StatusCode::FAILURE; 00365 00366 // Now initialize care of any sub-algorithms 00367 std::vector<Algorithm *>::iterator it; 00368 StatusCode result = StatusCode::SUCCESS; 00369 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) { 00370 sc = (*it)->sysRestart(); 00371 if( sc.isFailure() ) result = sc; 00372 } 00373 if( result.isFailure() ) { 00374 MsgStream log ( msgSvc() , name() ); 00375 log << MSG::ERROR 00376 << "sysRestart(): Error restarting one or several sub-algorithms" 00377 << endreq; 00378 return result; 00379 } 00380 return StatusCode::SUCCESS; 00381 } 00382 catch ( const GaudiException& Exception ) { 00383 MsgStream log ( msgSvc() , name() ); 00384 log << MSG::FATAL << "sysRestart(): Exception with tag=" << Exception.tag() 00385 << " is caught" << endreq; 00386 log << MSG::ERROR << Exception << endreq; 00387 Stat stat( chronoSvc() , Exception.tag() ); 00388 } 00389 catch( const std::exception& Exception ) { 00390 MsgStream log ( msgSvc() , name() ); 00391 log << MSG::FATAL << "sysRestart(): Standard std::exception is caught" << endreq; 00392 log << MSG::ERROR << Exception.what() << endreq; 00393 Stat stat( chronoSvc() , "*std::exception*" ); 00394 } 00395 catch(...) { 00396 MsgStream log ( msgSvc() , name() ); 00397 log << MSG::FATAL << "sysRestart(): UNKNOWN Exception is caught" << endreq; 00398 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00399 } 00400 00401 return StatusCode::FAILURE; 00402 } 00403 00404 // IAlgorithm implementation 00405 StatusCode Algorithm::sysBeginRun() { 00406 00407 // Bypass the beginRun if the algorithm is disabled. 00408 if ( !isEnabled( ) ) return StatusCode::SUCCESS; 00409 00410 // Check current outputLevel to evetually inform the MessagsSvc 00411 //if( m_outputLevel != MSG::NIL ) { 00412 setOutputLevel( m_outputLevel ); 00413 //} 00414 00415 // Reset Error count 00416 m_errorCount = 0; 00417 00418 StatusCode sc; 00419 // Invoke beginRun() method of the derived class inside a try/catch clause 00420 try { 00421 { // limit the scope of the guard 00422 Gaudi::Guards::AuditorGuard guard(this, 00423 // check if we want to audit the initialize 00424 (m_auditorBeginRun) ? auditorSvc() : 0, 00425 IAuditor::BeginRun); 00426 // Invoke the beginRun() method of the derived class 00427 sc = beginRun(); 00428 } 00429 if( sc.isFailure() ) return StatusCode::FAILURE; 00430 00431 // Now call beginRun for any sub-algorithms 00432 std::vector<Algorithm *>::iterator it; 00433 StatusCode result = StatusCode::SUCCESS; 00434 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) { 00435 sc = (*it)->sysBeginRun(); 00436 if( sc.isFailure() ) result = sc; 00437 } 00438 if( result.isFailure() ) { 00439 MsgStream log ( msgSvc() , name() + ".sysBeginRun()" ); 00440 log << MSG::ERROR << " Error executing BeginRun for one or several sub-algorithms" 00441 << endreq; 00442 return result; 00443 } 00444 return StatusCode::SUCCESS; 00445 } 00446 catch ( const GaudiException& Exception ) { 00447 MsgStream log ( msgSvc() , name() + ".sysBeginRun()" ); 00448 log << MSG::FATAL << " Exception with tag=" << Exception.tag() 00449 << " is caught " << endreq; 00450 log << MSG::ERROR << Exception << endreq; 00451 Stat stat( chronoSvc() , Exception.tag() ); 00452 } 00453 catch( const std::exception& Exception ) { 00454 MsgStream log ( msgSvc() , name() + ".sysBeginRun()" ); 00455 log << MSG::FATAL << " Standard std::exception is caught " << endreq; 00456 log << MSG::ERROR << Exception.what() << endreq; 00457 Stat stat( chronoSvc() , "*std::exception*" ); 00458 } 00459 catch(...) { 00460 MsgStream log ( msgSvc() , name() + ".sysBeginRun()" ); 00461 log << MSG::FATAL << "UNKNOWN Exception is caught " << endreq; 00462 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00463 } 00464 return StatusCode::FAILURE; 00465 } 00466 00467 StatusCode Algorithm::beginRun() { 00468 return StatusCode::SUCCESS; 00469 } 00470 00471 // IAlgorithm implementation 00472 StatusCode Algorithm::sysEndRun() { 00473 00474 // Bypass the endRun if the algorithm is disabled. 00475 if ( !isEnabled( ) ) return StatusCode::SUCCESS; 00476 00477 // Check current outputLevel to eventually inform the MessagsSvc 00478 //if( m_outputLevel != MSG::NIL ) { 00479 setOutputLevel( m_outputLevel ); 00480 //} 00481 00482 // Reset Error count 00483 m_errorCount = 0; 00484 00485 // Invoke endRun() method of the derived class inside a try/catch clause 00486 StatusCode sc; 00487 try { 00488 { // limit the scope of the guard 00489 Gaudi::Guards::AuditorGuard guard(this, 00490 // check if we want to audit the initialize 00491 (m_auditorEndRun) ? auditorSvc() : 0, 00492 IAuditor::EndRun); 00493 // Invoke the endRun() method of the derived class 00494 sc = endRun(); 00495 } 00496 if( sc.isFailure() ) return StatusCode::FAILURE; 00497 00498 // Now call endRun for any sub-algorithms 00499 std::vector<Algorithm *>::iterator it; 00500 StatusCode result = StatusCode::SUCCESS; 00501 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) { 00502 sc = (*it)->sysEndRun(); 00503 if( sc.isFailure() ) result = sc; 00504 } 00505 if( result.isFailure() ) { 00506 MsgStream log ( msgSvc() , name() + ".sysEndRun()" ); 00507 log << MSG::ERROR << " Error calling endRun for one or several sub-algorithms" 00508 << endreq; 00509 return result; 00510 } 00511 return StatusCode::SUCCESS; 00512 } 00513 catch ( const GaudiException& Exception ) { 00514 MsgStream log ( msgSvc() , name() + ".sysEndRun()" ); 00515 log << MSG::FATAL << " Exception with tag=" << Exception.tag() 00516 << " is caught " << endreq; 00517 log << MSG::ERROR << Exception << endreq; 00518 Stat stat( chronoSvc() , Exception.tag() ); 00519 } 00520 catch( const std::exception& Exception ) { 00521 MsgStream log ( msgSvc() , name() + ".sysEndRun()" ); 00522 log << MSG::FATAL << " Standard std::exception is caught " << endreq; 00523 log << MSG::ERROR << Exception.what() << endreq; 00524 Stat stat( chronoSvc() , "*std::exception*" ); 00525 } 00526 catch(...) { 00527 MsgStream log ( msgSvc() , name() + ".sysEndRun()" ); 00528 log << MSG::FATAL << "UNKNOWN Exception is caught " << endreq; 00529 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00530 } 00531 return StatusCode::FAILURE; 00532 } 00533 00534 StatusCode Algorithm::endRun() { 00535 return StatusCode::SUCCESS; 00536 } 00537 00538 00539 StatusCode Algorithm::sysExecute() { 00540 if (!isEnabled()) { 00541 MsgStream log ( msgSvc() , name() ); 00542 log << MSG::VERBOSE << ".sysExecute(): is not enabled. Skip execution" <<endreq; 00543 return StatusCode::SUCCESS; 00544 } 00545 00546 StatusCode status; 00547 00548 // Should performance profile be performed ? 00549 // invoke execute() method of Algorithm class 00550 // and catch all uncaught exceptions 00551 00552 Gaudi::Guards::AuditorGuard guard(this, 00553 // check if we want to audit the initialize 00554 (m_auditorExecute) ? auditorSvc() : 0, 00555 IAuditor::Execute, 00556 status); 00557 try { 00558 status = execute(); 00559 setExecuted(true); // set the executed flag 00560 00561 if (status.isFailure()) { 00562 status = exceptionSvc()->handleErr(*this,status); 00563 } 00564 00565 } 00566 catch( const GaudiException& Exception ) { 00567 setExecuted(true); // set the executed flag 00568 00569 MsgStream log ( msgSvc() , name() + ".sysExecute()" ); 00570 if (Exception.code() == StatusCode::FAILURE) { 00571 log << MSG::FATAL; 00572 } else { 00573 log << MSG::ERROR << " Recoverable"; 00574 } 00575 00576 log << " Exception with tag=" << Exception.tag() 00577 << " is caught " << endreq; 00578 00579 log << MSG::ERROR << Exception << endreq; 00580 00581 Stat stat( chronoSvc() , Exception.tag() ) ; 00582 status = exceptionSvc()->handle(*this,Exception); 00583 } 00584 catch( const std::exception& Exception ) { 00585 setExecuted(true); // set the executed flag 00586 00587 MsgStream log ( msgSvc() , name() + ".sysExecute()" ); 00588 log << MSG::FATAL << " Standard std::exception is caught " << endreq; 00589 log << MSG::ERROR << Exception.what() << endreq; 00590 Stat stat( chronoSvc() , "*std::exception*" ) ; 00591 status = exceptionSvc()->handle(*this,Exception); 00592 } 00593 catch(...) { 00594 setExecuted(true); // set the executed flag 00595 00596 MsgStream log ( msgSvc() , name() + ".sysExecute()" ); 00597 log << MSG::FATAL << "UNKNOWN Exception is caught " << endreq; 00598 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00599 00600 status = exceptionSvc()->handle(*this); 00601 } 00602 00603 if( status.isFailure() ) { 00604 MsgStream log ( msgSvc() , name() ); 00605 // Increment the error count 00606 m_errorCount++; 00607 // Check if maximum is exeeded 00608 if( m_errorCount < m_errorMax ) { 00609 log << MSG::WARNING << "Continuing from error (cnt=" << m_errorCount 00610 << ", max=" << m_errorMax << ")" << endreq; 00611 // convert to success 00612 status = StatusCode::SUCCESS; 00613 } 00614 } 00615 return status; 00616 } 00617 00618 // IAlgorithm implementation 00619 StatusCode Algorithm::sysStop() { 00620 00621 // Bypass the startup if already running or disabled. 00622 if ( Gaudi::StateMachine::INITIALIZED == FSMState() || 00623 !isEnabled() ) return StatusCode::SUCCESS; 00624 00625 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::STOP,m_state); 00626 00627 StatusCode sc; 00628 // Invoke stop() method of the derived class inside a try/catch clause 00629 try { 00630 // Stop first any sub-algorithms (in reverse order) 00631 std::vector<Algorithm *>::reverse_iterator it; 00632 for (it = m_subAlgms->rbegin(); it != m_subAlgms->rend(); it++) { 00633 (*it)->sysStop().ignore(); 00634 } 00635 { // limit the scope of the guard 00636 Gaudi::Guards::AuditorGuard guard(this, 00637 // check if we want to audit the initialize 00638 (m_auditorStop) ? auditorSvc() : 0, 00639 IAuditor::Stop); 00640 00641 // Invoke the stop() method of the derived class 00642 sc = stop(); 00643 } 00644 if( sc.isFailure() ) return StatusCode::FAILURE; 00645 00646 // Update the state. 00647 m_state = m_targetState; 00648 return StatusCode::SUCCESS; 00649 } 00650 catch ( const GaudiException& Exception ) { 00651 MsgStream log ( msgSvc() , name() ); 00652 log << MSG::FATAL << "in sysStop(): exception with tag=" << Exception.tag() 00653 << " is caught" << endreq; 00654 log << MSG::ERROR << Exception << endreq; 00655 Stat stat( chronoSvc() , Exception.tag() ); 00656 } 00657 catch( const std::exception& Exception ) { 00658 MsgStream log ( msgSvc() , name() ); 00659 log << MSG::FATAL << "in sysStop(): standard std::exception is caught" << endreq; 00660 log << MSG::ERROR << Exception.what() << endreq; 00661 Stat stat( chronoSvc() , "*std::exception*" ); 00662 } 00663 catch(...) { 00664 MsgStream log ( msgSvc() , name() ); 00665 log << MSG::FATAL << "in sysStop(): UNKNOWN Exception is caught" << endreq; 00666 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00667 } 00668 00669 return StatusCode::FAILURE; 00670 } 00671 00672 StatusCode Algorithm::sysFinalize() { 00673 00674 // Bypass the finalialization if the algorithm hasn't been initilized. 00675 if ( Gaudi::StateMachine::CONFIGURED == FSMState() || 00676 !isEnabled() ) return StatusCode::SUCCESS; 00677 00678 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::FINALIZE,m_state); 00679 00680 // Invoke finalize() method of the derived class inside a try/catch clause 00681 StatusCode sc = StatusCode::SUCCESS; 00682 try { 00683 // Order changed (bug #3903 overview: finalize and nested algorithms) 00684 // Finalize first any sub-algoithms (it can be done more than once) 00685 std::vector<Algorithm *>::iterator it; 00686 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) { 00687 (*it)->sysFinalize().ignore(); 00688 } 00689 00690 { // limit the scope of the guard 00691 Gaudi::Guards::AuditorGuard guard(this, 00692 // check if we want to audit the initialize 00693 (m_auditorFinalize) ? auditorSvc() : 0, 00694 IAuditor::Finalize); 00695 // Invoke the finalize() method of the derived class 00696 sc = finalize(); 00697 } 00698 if( !sc.isSuccess() ) return StatusCode::FAILURE; 00699 00700 // Release all sub-algorithms 00701 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) { 00702 (*it)->release(); 00703 } 00704 // Indicate that this Algorithm has been finalized to prevent duplicate attempts 00705 m_state = m_targetState; 00706 return sc; 00707 } 00708 catch( const GaudiException& Exception ) { 00709 MsgStream log ( msgSvc() , name() + ".sysFinalize()" ); 00710 log << MSG::FATAL << " Exception with tag=" << Exception.tag() 00711 << " is caught " << endreq; 00712 log << MSG::ERROR << Exception << endreq; 00713 Stat stat( chronoSvc() , Exception.tag() ) ; 00714 } 00715 catch( const std::exception& Exception ) { 00716 MsgStream log ( msgSvc() , name() + ".sysFinalize()" ); 00717 log << MSG::FATAL << " Standard std::exception is caught " << endreq; 00718 log << MSG::ERROR << Exception.what() << endreq; 00719 Stat stat( chronoSvc() , "*std::exception*" ) ; 00720 } 00721 catch( ... ) { 00722 MsgStream log ( msgSvc() , name() + ".sysFinalize()" ); 00723 log << MSG::FATAL << "UNKNOWN Exception is caught " << endreq; 00724 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ; 00725 } 00726 return StatusCode::FAILURE ; 00727 } 00728 00729 StatusCode Algorithm::reinitialize() { 00730 /* @TODO 00731 * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize 00732 * is causing too many problems 00733 * 00734 // Default implementation is finalize+initialize 00735 StatusCode sc = finalize(); 00736 if (sc.isFailure()) { 00737 MsgStream log ( msgSvc() , name() ); 00738 log << MSG::ERROR << "reinitialize(): cannot be finalized" << endreq; 00739 return sc; 00740 } 00741 sc = initialize(); 00742 if (sc.isFailure()) { 00743 MsgStream log ( msgSvc() , name() ); 00744 log << MSG::ERROR << "reinitialize(): cannot be initialized" << endreq; 00745 return sc; 00746 } 00747 */ 00748 return StatusCode::SUCCESS; 00749 } 00750 00751 StatusCode Algorithm::restart() { 00752 // Default implementation is stop+start 00753 StatusCode sc = stop(); 00754 if (sc.isFailure()) { 00755 MsgStream log ( msgSvc() , name() ); 00756 log << MSG::ERROR << "restart(): cannot be stopped" << endreq; 00757 return sc; 00758 } 00759 sc = start(); 00760 if (sc.isFailure()) { 00761 MsgStream log ( msgSvc() , name() ); 00762 log << MSG::ERROR << "restart(): cannot be started" << endreq; 00763 return sc; 00764 } 00765 return StatusCode::SUCCESS; 00766 } 00767 00768 const std::string& Algorithm::name() const { 00769 return m_name; 00770 } 00771 00772 const std::string& Algorithm::version() const { 00773 return m_version; 00774 } 00775 00776 bool Algorithm::isExecuted() const { 00777 return m_isExecuted; 00778 } 00779 00780 void Algorithm::setExecuted( bool state ) { 00781 m_isExecuted = state; 00782 } 00783 00784 void Algorithm::resetExecuted() { 00785 m_isExecuted = false; 00786 m_filterPassed = true; 00787 } 00788 00789 bool Algorithm::isEnabled() const { 00790 return m_isEnabled; 00791 } 00792 00793 bool Algorithm::filterPassed() const { 00794 return m_filterPassed; 00795 } 00796 00797 void Algorithm::setFilterPassed( bool state ) { 00798 m_filterPassed = state; 00799 } 00800 00801 std::vector<Algorithm*>* Algorithm::subAlgorithms( ) const { 00802 return m_subAlgms; 00803 } 00804 00805 void Algorithm::setOutputLevel( int level ) { 00806 if ( 0 != msgSvc() ) 00807 { 00808 if ( MSG::NIL != level ) 00809 { msgSvc()->setOutputLevel( name(), level ) ; } 00810 m_outputLevel = msgSvc()->outputLevel( name() ); 00811 } 00812 } 00813 00814 IAuditorSvc* Algorithm::auditorSvc() const { 00815 if ( 0 == m_pAuditorSvc ) { 00816 StatusCode sc = service( "AuditorSvc", m_pAuditorSvc, true ); 00817 if( sc.isFailure() ) { 00818 throw GaudiException("Service [AuditorSvc] not found", name(), sc); 00819 } 00820 } 00821 return m_pAuditorSvc; 00822 } 00823 00824 IChronoStatSvc* Algorithm::chronoSvc() const { 00825 if ( 0 == m_CSS ) { 00826 StatusCode sc = service( "ChronoStatSvc", m_CSS, true ); 00827 if( sc.isFailure() ) { 00828 throw GaudiException("Service [ChronoStatSvc] not found", name(), sc); 00829 } 00830 } 00831 return m_CSS; 00832 } 00833 00834 // Obsoleted name, kept due to the backwards compatibility 00835 IChronoStatSvc* Algorithm::chronoStatService() const { 00836 return chronoSvc(); 00837 } 00838 00839 IDataProviderSvc* Algorithm::detSvc() const { 00840 if ( 0 == m_DDS ) { 00841 StatusCode sc = service( "DetectorDataSvc", m_DDS, true ); 00842 if( sc.isFailure() ) { 00843 throw GaudiException("Service [DetectorDataSvc] not found", name(), sc); 00844 } 00845 } 00846 return m_DDS; 00847 } 00848 // Obsoleted name, kept due to the backwards compatibility 00849 IDataProviderSvc* Algorithm::detDataService() const { 00850 return detSvc(); 00851 } 00852 00853 IConversionSvc* Algorithm::detCnvSvc() const { 00854 if ( 0 == m_DCS ) { 00855 StatusCode sc = service( "DetectorPersistencySvc", m_DCS, true ); 00856 if( sc.isFailure() ) { 00857 throw GaudiException("Service [DetectorPersistencySvc] not found", 00858 name(), sc); 00859 } 00860 } 00861 return m_DCS; 00862 } 00863 // Obsoleted name, kept due to the backwards compatibility 00864 IConversionSvc* Algorithm::detDataCnvService() const { 00865 return detCnvSvc(); 00866 } 00867 00868 IDataProviderSvc* Algorithm::eventSvc() const { 00869 if ( 0 == m_EDS ) { 00870 StatusCode sc = service( "EventDataSvc", m_EDS, true ); 00871 if( sc.isFailure() ) { 00872 throw GaudiException("Service [EventDataSvc] not found", name(), sc); 00873 } 00874 } 00875 return m_EDS; 00876 } 00877 // Obsoleted name, kept due to the backwards compatibility 00878 IDataProviderSvc* Algorithm::eventDataService() const { 00879 return eventSvc(); 00880 } 00881 00882 IConversionSvc* Algorithm::eventCnvSvc() const { 00883 if ( 0 == m_ECS ) { 00884 StatusCode sc = service( "EventPersistencySvc", m_ECS, true ); 00885 if( sc.isFailure() ) { 00886 throw GaudiException("Service [EventPersistencySvc] not found", 00887 name(), sc); 00888 } 00889 } 00890 return m_ECS; 00891 } 00892 // Obsoleted name, kept due to the backwards compatibility 00893 IConversionSvc* Algorithm::eventDataCnvService() const { 00894 return eventCnvSvc(); 00895 } 00896 00897 IHistogramSvc* Algorithm::histoSvc() const { 00898 if ( 0 == m_HDS ) { 00899 StatusCode sc = service( "HistogramDataSvc", m_HDS, true ); 00900 if( sc.isFailure() ) { 00901 throw GaudiException("Service [HistogramDataSvc] not found", name(), sc); 00902 } 00903 } 00904 return m_HDS; 00905 } 00906 // Obsoleted name, kept due to the backwards compatibility 00907 IHistogramSvc* Algorithm::histogramDataService() const { 00908 return histoSvc(); 00909 } 00910 00911 IExceptionSvc* Algorithm::exceptionSvc() const { 00912 if ( 0 == m_EXS ) { 00913 StatusCode sc = service( "ExceptionSvc", m_EXS, true ); 00914 if( sc.isFailure() ) { 00915 throw GaudiException("Service [ExceptionSvc] not found", name(), sc); 00916 } 00917 } 00918 return m_EXS; 00919 } 00920 00921 IMessageSvc* Algorithm::msgSvc() const { 00922 if ( 0 == m_MS ) { 00923 //can not use service() method (infinite recursion!) 00924 StatusCode sc = serviceLocator()->service( "MessageSvc", m_MS, true ); 00925 if( sc.isFailure() ) { 00926 throw GaudiException("Service [MessageSvc] not found", name(), sc); 00927 } 00928 } 00929 return m_MS; 00930 } 00931 00932 // Obsoleted name, kept due to the backwards compatibility 00933 IMessageSvc* Algorithm::messageService() const { 00934 return msgSvc(); 00935 } 00936 00937 INTupleSvc* Algorithm::ntupleSvc() const { 00938 if ( 0 == m_NTS ) { 00939 StatusCode sc = service( "NTupleSvc", m_NTS, true ); 00940 if( sc.isFailure() ) { 00941 throw GaudiException("Service [NTupleSvc] not found", name(), sc); 00942 } 00943 } 00944 return m_NTS; 00945 } 00946 00947 // Obsoleted name, kept due to the backwards compatibility 00948 INTupleSvc* Algorithm::ntupleService() const { 00949 return ntupleSvc(); 00950 } 00951 00952 // AIDATupleSvc: 00953 // IAIDATupleSvc* Algorithm::atupleSvc() const { 00954 // if ( 0 == m_ATS ) { 00955 // StatusCode sc = service( "AIDATupleSvc", m_ATS, true ); 00956 // if( sc.isFailure() ) { 00957 // throw GaudiException("Service [AIDATupleSvc] not found", name(), sc); 00958 // } 00959 // } 00960 // return m_ATS; 00961 // } 00962 00963 00964 IRndmGenSvc* Algorithm::randSvc() const { 00965 if ( 0 == m_RGS ) { 00966 StatusCode sc = service( "RndmGenSvc", m_RGS, true ); 00967 if( sc.isFailure() ) { 00968 throw GaudiException("Service [RndmGenSvc] not found", name(), sc); 00969 } 00970 } 00971 return m_RGS; 00972 } 00973 00974 IToolSvc* Algorithm::toolSvc() const { 00975 if ( 0 == m_ptoolSvc ) { 00976 StatusCode sc = service( "ToolSvc", m_ptoolSvc, true ); 00977 if( sc.isFailure() ) { 00978 throw GaudiException("Service [ToolSvc] not found", name(), sc); 00979 } 00980 } 00981 return m_ptoolSvc; 00982 } 00983 00984 ISvcLocator * Algorithm::serviceLocator() const { 00985 return m_pSvcLocator; 00986 } 00987 00988 // IInterface implementation 00989 unsigned long Algorithm::addRef() { 00990 return ++m_refCount; 00991 } 00992 00993 unsigned long Algorithm::release() { 00994 long count = --m_refCount; 00995 if( count <= 0) { 00996 delete this; 00997 } 00998 return count; 00999 } 01000 01001 // Retrieve the interface requested and check its version compatibility 01002 StatusCode Algorithm::queryInterface 01003 ( const InterfaceID& iid , 01004 void** ppISvc ) 01005 { 01006 if ( 0 == ppISvc ) { return StatusCode::FAILURE ; } // RETURN 01007 // 01008 if ( IAlgorithm ::interfaceID() . versionMatch ( iid ) ) 01009 { *ppISvc = static_cast<IAlgorithm*> ( this ) ; } 01010 else if ( IProperty ::interfaceID() . versionMatch ( iid ) ) 01011 { *ppISvc = static_cast<IProperty*> ( this ) ; } 01012 else if ( IStateful ::interfaceID() . versionMatch ( iid ) ) 01013 { *ppISvc = static_cast<IStateful*> ( this ) ; } 01014 else if ( INamedInterface ::interfaceID() . versionMatch ( iid ) ) 01015 { *ppISvc = static_cast<INamedInterface*> ( this ) ; } 01016 else if ( IInterface ::interfaceID() . versionMatch ( iid ) ) 01017 { *ppISvc = static_cast<IInterface*> ( this ) ; } 01018 else { *ppISvc = 0 ; return StatusCode::FAILURE; } // RETURN 01019 // increment the reference 01020 addRef(); 01021 01022 return StatusCode::SUCCESS; 01023 } 01024 01025 // Use the job options service to set declared properties 01026 StatusCode Algorithm::setProperties() { 01027 if( 0 != m_pSvcLocator ) { 01028 IJobOptionsSvc* jos = 0; 01029 StatusCode sc = m_pSvcLocator->getService 01030 ( "JobOptionsSvc", IID_IJobOptionsSvc, *pp_cast<IInterface>(&jos) ); 01031 if( sc.isSuccess() ) { 01032 // set first generic Properties 01033 sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this ); 01034 if( sc.isFailure() ) return StatusCode::FAILURE; 01035 01036 // set specific Properties 01037 if (isGaudiThreaded(name())) { 01038 if(jos->setMyProperties( name(), this ).isFailure()) { 01039 return StatusCode::FAILURE; 01040 } 01041 } 01042 jos->release(); 01043 return sc; 01044 } 01045 } 01046 return StatusCode::FAILURE; 01047 } 01048 01049 StatusCode Algorithm::createSubAlgorithm(const std::string& type, 01050 const std::string& name, 01051 Algorithm*& pSubAlgorithm) { 01052 if( 0 == m_pSvcLocator ) return StatusCode::FAILURE; 01053 01054 IAlgManager* am = 0; 01055 IAlgManager** ptr = &am; 01056 StatusCode sc = m_pSvcLocator->getService 01057 ( "", IID_IAlgManager,(IInterface*&)*ptr ); 01058 if( sc.isFailure() ) return StatusCode::FAILURE; 01059 01060 // Maybe modify the AppMgr interface to return Algorithm* ?? 01061 IAlgorithm *tmp; 01062 sc = am->createAlgorithm 01063 (type, name+getGaudiThreadIDfromName(Algorithm::name()), tmp); 01064 if( sc.isFailure() ) return StatusCode::FAILURE; 01065 am->release(); 01066 01067 try{ 01068 pSubAlgorithm = dynamic_cast<Algorithm*>(tmp); 01069 m_subAlgms->push_back(pSubAlgorithm); 01070 } catch(...){ 01071 sc = StatusCode::FAILURE; 01072 } 01073 return sc; 01074 } 01075 01076 // IProperty implementation 01077 // Delegate to the Property manager 01078 StatusCode Algorithm::setProperty(const Property& p) { 01079 return m_propertyMgr->setProperty(p); 01080 } 01081 StatusCode Algorithm::setProperty(const std::string& s) { 01082 return m_propertyMgr->setProperty(s); 01083 } 01084 StatusCode Algorithm::setProperty(const std::string& n, const std::string& v) { 01085 return m_propertyMgr->setProperty(n,v); 01086 } 01087 StatusCode Algorithm::getProperty(Property* p) const { 01088 return m_propertyMgr->getProperty(p); 01089 } 01090 const Property& Algorithm::getProperty( const std::string& name) const{ 01091 return m_propertyMgr->getProperty(name); 01092 } 01093 StatusCode Algorithm::getProperty(const std::string& n, std::string& v ) const { 01094 return m_propertyMgr->getProperty(n,v); 01095 } 01096 const std::vector<Property*>& Algorithm::getProperties( ) const { 01097 return m_propertyMgr->getProperties(); 01098 } 01099 01103 void 01104 Algorithm::initOutputLevel(Property& /*prop*/) 01105 { 01106 // do nothing... yet ? 01107 } 01108 01109 StatusCode 01110 Algorithm::service_i(const std::string& svcName, 01111 bool createIf, 01112 const InterfaceID& iid, 01113 void** ppSvc) const { 01114 MsgStream log(msgSvc(), name()); 01115 ServiceLocatorHelper helper(*serviceLocator(), log, name()); 01116 return helper.getService(svcName, createIf, iid, ppSvc); 01117 } 01118 01119 StatusCode 01120 Algorithm::service_i(const std::string& svcType, 01121 const std::string& svcName, 01122 const InterfaceID& iid, 01123 void** ppSvc) const { 01124 01125 MsgStream log(msgSvc(), name()); 01126 ServiceLocatorHelper helper(*serviceLocator(), log, name()); 01127 return helper.createService(svcType, svcName, iid, ppSvc); 01128 }