Gaudi Framework, version v21r4

Home   Generated: 7 Sep 2009

Algorithm.cpp

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

Generated at Mon Sep 7 18:05:42 2009 for Gaudi Framework, version v21r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004