Gaudi Framework, version v21r7

Home   Generated: 22 Jan 2010

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

Generated at Fri Jan 22 20:27:51 2010 for Gaudi Framework, version v21r7 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004