Gaudi Framework, version v20r2

Generated: 18 Jul 2008

Algorithm.cpp

Go to the documentation of this file.
00001 //$Id: Algorithm.cpp,v 1.41 2008/06/02 14:20: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   // Default implementation is finalize+initialize
00731   StatusCode sc = finalize();
00732   if (sc.isFailure()) {
00733     MsgStream log ( msgSvc() , name() );
00734     log << MSG::ERROR << "reinitialize(): cannot be finalized" << endreq;
00735     return sc;
00736   }
00737   sc = initialize();
00738   if (sc.isFailure()) {
00739     MsgStream log ( msgSvc() , name() );
00740     log << MSG::ERROR << "reinitialize(): cannot be initialized" << endreq;
00741     return sc;
00742   }
00743   return StatusCode::SUCCESS;
00744 }
00745 
00746 StatusCode Algorithm::restart() {
00747   // Default implementation is stop+start
00748   StatusCode sc = stop();
00749   if (sc.isFailure()) {
00750     MsgStream log ( msgSvc() , name() );
00751     log << MSG::ERROR << "restart(): cannot be stopped" << endreq;
00752     return sc;
00753   }
00754   sc = start();
00755   if (sc.isFailure()) {
00756     MsgStream log ( msgSvc() , name() );
00757     log << MSG::ERROR << "restart(): cannot be started" << endreq;
00758     return sc;
00759   }
00760   return StatusCode::SUCCESS;
00761 }
00762 
00763 const std::string& Algorithm::name() const {
00764   return m_name;
00765 }
00766 
00767 const std::string& Algorithm::version() const {
00768   return m_version;
00769 }
00770 
00771 bool Algorithm::isExecuted() const {
00772   return m_isExecuted;
00773 }
00774 
00775 void Algorithm::setExecuted( bool state ) {
00776   m_isExecuted = state;
00777 }
00778 
00779 void Algorithm::resetExecuted() {
00780   m_isExecuted   = false;
00781   m_filterPassed = true;
00782 }
00783 
00784 bool Algorithm::isEnabled() const {
00785   return m_isEnabled;
00786 }
00787 
00788 bool Algorithm::filterPassed() const {
00789   return m_filterPassed;
00790 }
00791 
00792 void Algorithm::setFilterPassed( bool state ) {
00793   m_filterPassed = state;
00794 }
00795 
00796 std::vector<Algorithm*>* Algorithm::subAlgorithms( ) const {
00797   return m_subAlgms;
00798 }
00799 
00800 void Algorithm::setOutputLevel( int level ) {
00801   if ( 0 != msgSvc() )
00802   {
00803     if ( MSG::NIL != level )
00804     { msgSvc()->setOutputLevel( name(), level ) ; }
00805     m_outputLevel = msgSvc()->outputLevel( name() );
00806   }
00807 }
00808 
00809 IAuditorSvc* Algorithm::auditorSvc() const {
00810   if ( 0 == m_pAuditorSvc ) {
00811     StatusCode sc = service( "AuditorSvc", m_pAuditorSvc, true );
00812     if( sc.isFailure() ) {
00813       throw GaudiException("Service [AuditorSvc] not found", name(), sc);
00814     }
00815   }
00816   return m_pAuditorSvc;
00817 }
00818 
00819 IChronoStatSvc* Algorithm::chronoSvc() const {
00820   if ( 0 == m_CSS ) {
00821     StatusCode sc = service( "ChronoStatSvc", m_CSS, true );
00822     if( sc.isFailure() ) {
00823       throw GaudiException("Service [ChronoStatSvc] not found", name(), sc);
00824     }
00825   }
00826   return m_CSS;
00827 }
00828 
00829 // Obsoleted name, kept due to the backwards compatibility
00830 IChronoStatSvc* Algorithm::chronoStatService() const {
00831   return chronoSvc();
00832 }
00833 
00834 IDataProviderSvc* Algorithm::detSvc() const {
00835   if ( 0 == m_DDS ) {
00836     StatusCode sc = service( "DetectorDataSvc", m_DDS, true );
00837     if( sc.isFailure() ) {
00838       throw GaudiException("Service [DetectorDataSvc] not found", name(), sc);
00839     }
00840   }
00841   return m_DDS;
00842 }
00843 // Obsoleted name, kept due to the backwards compatibility
00844 IDataProviderSvc* Algorithm::detDataService() const {
00845   return detSvc();
00846 }
00847 
00848 IConversionSvc* Algorithm::detCnvSvc() const {
00849   if ( 0 == m_DCS ) {
00850     StatusCode sc = service( "DetectorPersistencySvc", m_DCS, true );
00851     if( sc.isFailure() ) {
00852       throw GaudiException("Service [DetectorPersistencySvc] not found",
00853                            name(), sc);
00854     }
00855   }
00856   return m_DCS;
00857 }
00858 // Obsoleted name, kept due to the backwards compatibility
00859 IConversionSvc* Algorithm::detDataCnvService() const {
00860   return detCnvSvc();
00861 }
00862 
00863 IDataProviderSvc* Algorithm::eventSvc() const {
00864   if ( 0 == m_EDS ) {
00865     StatusCode sc = service( "EventDataSvc", m_EDS, true );
00866     if( sc.isFailure() ) {
00867       throw GaudiException("Service [EventDataSvc] not found", name(), sc);
00868     }
00869   }
00870   return m_EDS;
00871 }
00872 // Obsoleted name, kept due to the backwards compatibility
00873 IDataProviderSvc* Algorithm::eventDataService() const {
00874   return eventSvc();
00875 }
00876 
00877 IConversionSvc* Algorithm::eventCnvSvc() const {
00878   if ( 0 == m_ECS ) {
00879     StatusCode sc = service( "EventPersistencySvc", m_ECS, true );
00880     if( sc.isFailure() ) {
00881       throw GaudiException("Service [EventPersistencySvc] not found",
00882                            name(), sc);
00883     }
00884   }
00885   return m_ECS;
00886 }
00887 // Obsoleted name, kept due to the backwards compatibility
00888 IConversionSvc* Algorithm::eventDataCnvService() const {
00889   return eventCnvSvc();
00890 }
00891 
00892 IHistogramSvc* Algorithm::histoSvc() const {
00893   if ( 0 == m_HDS ) {
00894     StatusCode sc = service( "HistogramDataSvc", m_HDS, true );
00895     if( sc.isFailure() ) {
00896       throw GaudiException("Service [HistogramDataSvc] not found", name(), sc);
00897     }
00898   }
00899   return m_HDS;
00900 }
00901 // Obsoleted name, kept due to the backwards compatibility
00902 IHistogramSvc* Algorithm::histogramDataService() const {
00903   return histoSvc();
00904 }
00905 
00906 IExceptionSvc* Algorithm::exceptionSvc() const {
00907   if ( 0 == m_EXS ) {
00908     StatusCode sc = service( "ExceptionSvc", m_EXS, true );
00909     if( sc.isFailure() ) {
00910       throw GaudiException("Service [ExceptionSvc] not found", name(), sc);
00911     }
00912   }
00913   return m_EXS;
00914 }
00915 
00916 IMessageSvc* Algorithm::msgSvc() const {
00917   if ( 0 == m_MS ) {
00918     //can not use service() method (infinite recursion!)
00919     StatusCode sc = serviceLocator()->service( "MessageSvc", m_MS, true );
00920     if( sc.isFailure() ) {
00921       throw GaudiException("Service [MessageSvc] not found", name(), sc);
00922     }
00923   }
00924   return m_MS;
00925 }
00926 
00927 // Obsoleted name, kept due to the backwards compatibility
00928 IMessageSvc* Algorithm::messageService() const {
00929   return msgSvc();
00930 }
00931 
00932 INTupleSvc* Algorithm::ntupleSvc() const {
00933   if ( 0 == m_NTS ) {
00934     StatusCode sc = service( "NTupleSvc", m_NTS, true );
00935     if( sc.isFailure() ) {
00936       throw GaudiException("Service [NTupleSvc] not found", name(), sc);
00937     }
00938   }
00939   return m_NTS;
00940 }
00941 
00942 // Obsoleted name, kept due to the backwards compatibility
00943 INTupleSvc* Algorithm::ntupleService() const {
00944   return ntupleSvc();
00945 }
00946 
00947 // AIDATupleSvc:
00948 // IAIDATupleSvc* Algorithm::atupleSvc() const {
00949 //   if ( 0 == m_ATS ) {
00950 //     StatusCode sc = service( "AIDATupleSvc", m_ATS, true );
00951 //     if( sc.isFailure() ) {
00952 //       throw GaudiException("Service [AIDATupleSvc] not found", name(), sc);
00953 //     }
00954 //   }
00955 //   return m_ATS;
00956 // }
00957 
00958 
00959 IRndmGenSvc* Algorithm::randSvc() const {
00960   if ( 0 == m_RGS ) {
00961     StatusCode sc = service( "RndmGenSvc", m_RGS, true );
00962     if( sc.isFailure() ) {
00963       throw GaudiException("Service [RndmGenSvc] not found", name(), sc);
00964     }
00965   }
00966   return m_RGS;
00967 }
00968 
00969 IToolSvc* Algorithm::toolSvc() const {
00970   if ( 0 == m_ptoolSvc ) {
00971     StatusCode sc = service( "ToolSvc", m_ptoolSvc, true );
00972     if( sc.isFailure() ) {
00973       throw GaudiException("Service [ToolSvc] not found", name(), sc);
00974     }
00975   }
00976   return m_ptoolSvc;
00977 }
00978 
00979 ISvcLocator * Algorithm::serviceLocator() const {
00980   return m_pSvcLocator;
00981 }
00982 
00983 // IInterface implementation
00984 unsigned long Algorithm::addRef() {
00985   return ++m_refCount;
00986 }
00987 
00988 unsigned long Algorithm::release() {
00989   long count = --m_refCount;
00990   if( count <= 0) {
00991     delete this;
00992   }
00993   return count;
00994 }
00995 
00996 // Retrieve the interface requested and check its version compatibility
00997 StatusCode Algorithm::queryInterface
00998 ( const InterfaceID& iid    ,
00999   void**             ppISvc )
01000 {
01001   if ( 0 == ppISvc ) { return StatusCode::FAILURE ; }   // RETURN
01002   //
01003   if      ( IAlgorithm ::interfaceID()      . versionMatch ( iid ) )
01004   { *ppISvc = static_cast<IAlgorithm*>      ( this ) ; }
01005   else if ( IProperty  ::interfaceID()      . versionMatch ( iid ) )
01006   { *ppISvc = static_cast<IProperty*>       ( this ) ; }
01007   else if ( IStateful  ::interfaceID()      . versionMatch ( iid ) )   
01008   { *ppISvc = static_cast<IStateful*>       ( this ) ; }
01009   else if ( INamedInterface ::interfaceID() . versionMatch ( iid ) )
01010   { *ppISvc = static_cast<INamedInterface*> ( this ) ; }
01011   else if ( IInterface ::interfaceID()      . versionMatch ( iid ) )
01012   { *ppISvc = static_cast<IInterface*>      ( this ) ; }
01013   else { *ppISvc = 0 ; return StatusCode::FAILURE; }    // RETURN
01014   // increment the reference
01015   addRef();
01016 
01017   return StatusCode::SUCCESS;
01018 }
01019 
01020 // Use the job options service to set declared properties
01021 StatusCode Algorithm::setProperties() {
01022   if( 0 != m_pSvcLocator )    {
01023     IJobOptionsSvc* jos = 0;
01024     StatusCode sc = m_pSvcLocator->getService
01025       ( "JobOptionsSvc", IID_IJobOptionsSvc, *pp_cast<IInterface>(&jos) );
01026     if( sc.isSuccess() )    {
01027       // set first generic Properties
01028       sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this );
01029       if( sc.isFailure() ) return StatusCode::FAILURE;
01030 
01031       // set specific Properties
01032       if (isGaudiThreaded(name())) {
01033         if(jos->setMyProperties( name(), this ).isFailure()) {
01034           return StatusCode::FAILURE;
01035         }
01036       }
01037       jos->release();
01038       return sc;
01039     }
01040   }
01041   return StatusCode::FAILURE;
01042 }
01043 
01044 StatusCode Algorithm::createSubAlgorithm(const std::string& type,
01045                                          const std::string& name,
01046                                          Algorithm*& pSubAlgorithm) {
01047   if( 0 == m_pSvcLocator ) return StatusCode::FAILURE;
01048 
01049   IAlgManager* am = 0;
01050   IAlgManager** ptr = &am;
01051   StatusCode sc = m_pSvcLocator->getService
01052     ( "", IID_IAlgManager,(IInterface*&)*ptr );
01053   if( sc.isFailure() ) return StatusCode::FAILURE;
01054 
01055   // Maybe modify the AppMgr interface to return Algorithm* ??
01056   IAlgorithm *tmp;
01057   sc = am->createAlgorithm
01058     (type, name+getGaudiThreadIDfromName(Algorithm::name()), tmp);
01059   if( sc.isFailure() ) return StatusCode::FAILURE;
01060   am->release();
01061 
01062   try{
01063     pSubAlgorithm = dynamic_cast<Algorithm*>(tmp);
01064     m_subAlgms->push_back(pSubAlgorithm);
01065   } catch(...){
01066     sc = StatusCode::FAILURE;
01067   }
01068   return sc;
01069 }
01070 
01071 // IProperty implementation
01072 // Delegate to the Property manager
01073 StatusCode Algorithm::setProperty(const Property& p) {
01074   return m_propertyMgr->setProperty(p);
01075 }
01076 StatusCode Algorithm::setProperty(const std::string& s) {
01077   return m_propertyMgr->setProperty(s);
01078 }
01079 StatusCode Algorithm::setProperty(const std::string& n, const std::string& v) {
01080   return m_propertyMgr->setProperty(n,v);
01081 }
01082 StatusCode Algorithm::getProperty(Property* p) const {
01083   return m_propertyMgr->getProperty(p);
01084 }
01085 const Property& Algorithm::getProperty( const std::string& name) const{
01086   return m_propertyMgr->getProperty(name);
01087 }
01088 StatusCode Algorithm::getProperty(const std::string& n, std::string& v ) const {
01089   return m_propertyMgr->getProperty(n,v);
01090 }
01091 const std::vector<Property*>& Algorithm::getProperties( ) const {
01092   return m_propertyMgr->getProperties();
01093 }
01094 
01098 void 
01099 Algorithm::initOutputLevel(Property& /*prop*/) 
01100 {
01101   // do nothing... yet ?
01102 }
01103 
01104 StatusCode
01105 Algorithm::service_i(const std::string& svcName,
01106                      bool createIf,
01107                      const InterfaceID& iid,
01108                      void** ppSvc) const {
01109   MsgStream log(msgSvc(), name());
01110   ServiceLocatorHelper helper(*serviceLocator(), log, name());
01111   return helper.getService(svcName, createIf, iid, ppSvc);
01112 }
01113 
01114 StatusCode
01115 Algorithm::service_i(const std::string& svcType,
01116                      const std::string& svcName,
01117                      const InterfaceID& iid,
01118                      void** ppSvc) const {
01119 
01120   MsgStream log(msgSvc(), name());
01121   ServiceLocatorHelper helper(*serviceLocator(), log, name());
01122   return  helper.createService(svcType, svcName, iid, ppSvc);
01123 }

Generated at Fri Jul 18 11:59:22 2008 for Gaudi Framework, version v20r2 by Doxygen version 1.5.1 written by Dimitri van Heesch, © 1997-2004