Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

MinimalEventLoopMgr.cpp

Go to the documentation of this file.
00001 //===================================================================
00002 //      MinimalEventLoopMgr.cpp
00003 //--------------------------------------------------------------------
00004 //
00005 //      Package    : MinimalEventLoopMgr ( The LHCb Offline System)
00006 //
00007 //  Description: implementation of the Application's batch mode handler
00008 //
00009 //      Author     : M.Frank
00010 //  History    :
00011 // +---------+----------------------------------------------+---------
00012 // |    Date |                 Comment                      | Who     
00013 // +---------+----------------------------------------------+---------
00014 // | 29/10/98| Initial version                              | MF
00015 // +---------+----------------------------------------------+---------
00016 //
00017 //====================================================================
00018 #define  GAUDISVC_MINIMALEVENTLOOPMGR_CPP
00019 
00020 #include "GaudiKernel/SmartIF.h"
00021 #include "GaudiKernel/MsgStream.h"
00022 #include "GaudiKernel/SvcFactory.h"
00023 #include "GaudiKernel/IAlgorithm.h"
00024 #include "GaudiKernel/IAlgManager.h"
00025 
00026 #include "MinimalEventLoopMgr.h"
00027 #include "ListItem.h"
00028 
00029 // Instantiation of a static factory class used by clients to create instances of this service
00030 static SvcFactory<MinimalEventLoopMgr> s_MinimalEventLoopMgrFactory;
00031 const ISvcFactory& MinimalEventLoopMgrFactory = s_MinimalEventLoopMgrFactory;
00032 
00033 // Standard Constructor
00034 MinimalEventLoopMgr::MinimalEventLoopMgr(const std::string& nam, ISvcLocator* svcLoc)
00035 : Service(nam, svcLoc)
00036 {
00037   declareProperty("TopAlg",         m_topAlgNames );
00038   declareProperty("OutStream",      m_outStreamNames );
00039   m_topAlgNames.declareUpdateHandler   ( &MinimalEventLoopMgr::topAlgHandler, this );
00040   m_outStreamNames.declareUpdateHandler( &MinimalEventLoopMgr::outStreamHandler, this );
00041   svcLoc->queryInterface(IID_IAppMgrUI, (void**)&m_appMgrUI);
00042   m_state = OFFLINE;
00043 }
00044 
00045 // Standard Destructor
00046 MinimalEventLoopMgr::~MinimalEventLoopMgr()   {
00047   m_state = OFFLINE;
00048 }
00049 
00050 // implementation of IInterface: queryInterface
00051 StatusCode MinimalEventLoopMgr::queryInterface(const InterfaceID& riid, void** ppvInterface)   {
00052   if ( riid == IID_IAppMgrUI )  {
00053     *ppvInterface = (IAppMgrUI*)this;
00054   }
00055   else if ( riid == IID_IEventProcessor )  {
00056     *ppvInterface = (IEventProcessor*)this;
00057   }
00058   else  {
00059     return Service::queryInterface(riid, ppvInterface);
00060   }
00061   addRef();
00062   return StatusCode::SUCCESS;
00063 }
00064 
00065 // implementation of IAppMgrUI::run
00066 StatusCode MinimalEventLoopMgr::run()   {
00067   return StatusCode::FAILURE;
00068 }
00069 
00070 // implementation of IAppMgrUI::configure
00071 StatusCode MinimalEventLoopMgr::configure()   {
00072   if ( 0 != m_appMgrUI )    {
00073     StatusCode sc = Service::initialize();
00074     if ( sc.isSuccess() )   {
00075       MsgStream log(msgSvc(), name());
00076       SmartIF<IProperty> prpMgr(IID_IProperty, serviceLocator());
00077       if ( prpMgr.isValid() )   {
00078         if ( m_topAlgNames.value().size() == 0 )    {
00079           setProperty(prpMgr->getProperty("TopAlg"));
00080         }
00081         if ( m_outStreamNames.value().size() == 0 )   {
00082           setProperty(prpMgr->getProperty("OutStream"));
00083         }
00084         m_state = CONFIGURED;
00085         return StatusCode::SUCCESS;
00086       }
00087       log << MSG::ERROR << "Error retrieving AppMgr interface IProperty." << endreq;
00088       return StatusCode::FAILURE;
00089     }
00090     return sc;
00091   }
00092   return StatusCode::FAILURE;
00093 }
00094 
00095 // implementation of IAppMgrUI::initalize
00096 StatusCode MinimalEventLoopMgr::initialize()    {
00097   MsgStream log(msgSvc(), name());
00098   m_state = INITIALIZED;
00099   //--------------------------------------------------------------------------------------------
00100   // Create output streams. Do not initialize them yet.
00101   // The state is updated temporarily in order to enable the handler, which
00102   // is also triggered by a change to the "OutputStream" Property.
00103   //--------------------------------------------------------------------------------------------
00104   StatusCode sc = decodeOutStreams();
00105   if ( !sc.isSuccess() )    {
00106     log << MSG::ERROR << "Failed to initialize Output streams." << endreq;
00107     m_state = CONFIGURED;
00108     return sc;
00109   }
00110   //--------------------------------------------------------------------------------------------
00111   // Create all needed Top Algorithms. Do not initialize them yet.
00112   // The state is updated temporarily in order to enable the handler, which
00113   // is also triggered by a change to the "TopAlg" Property.
00114   //--------------------------------------------------------------------------------------------
00115   sc = decodeTopAlgs();
00116   if ( !sc.isSuccess() )    {
00117     log << MSG::ERROR << "Failed to initialize Top Algorithms streams." << endreq;
00118     m_state = CONFIGURED;
00119     return sc;
00120   }
00121   return sc;
00122 }
00123 
00124 // implementation of IAppMgrUI::initalize
00125 StatusCode MinimalEventLoopMgr::finalize()    {
00126   MsgStream log( msgSvc(), name() );
00127   StatusCode sc = StatusCode::SUCCESS;
00128   //--------------------------------------------------------------------------------------------
00129   // Call the finalize() method of all top algorithms
00130   //--------------------------------------------------------------------------------------------
00131   ListAlg::iterator ita;
00132   for ( ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00133     sc = (*ita)->sysFinalize();
00134     if( !sc.isSuccess() ) {
00135       log << MSG::WARNING << "Finalization of algorithm " << (*ita)->name() << " failed" << endreq;
00136     }
00137   }
00138   //--------------------------------------------------------------------------------------------
00139   // Call the finalize() method of all Output streams
00140   //--------------------------------------------------------------------------------------------
00141   for ( ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00142     sc = (*ita)->sysFinalize();
00143     if( !sc.isSuccess() ) {
00144       log << MSG::WARNING << "Finalization of algorithm " << (*ita)->name() << " failed" << endreq;
00145     }
00146   }
00147   //--------------------------------------------------------------------------------------------
00148   // release all top algorithms
00149   //--------------------------------------------------------------------------------------------
00150   for ( ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00151     (*ita)->release();
00152   }
00153   m_topAlgList.erase(m_topAlgList.begin(), m_topAlgList.end() );
00154 
00155   //--------------------------------------------------------------------------------------------
00156   // release all output streams
00157   //--------------------------------------------------------------------------------------------
00158   for ( ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00159     (*ita)->release();
00160   }
00161   m_outStreamList.erase(m_outStreamList.begin(), m_outStreamList.end() );
00162   if ( sc.isSuccess() ) m_state = FINALIZED;
00163   return sc;
00164 }
00165 
00166 // implementation of IAppMgrUI::terminate
00167 StatusCode MinimalEventLoopMgr::terminate()   {
00168   m_appMgrUI = releaseInterface(m_appMgrUI);
00169   return Service::finalize();
00170 }
00171 
00172 // implementation of IAppMgrUI::nextEvent
00173 StatusCode MinimalEventLoopMgr::nextEvent(int /* maxevt */)   {
00174   MsgStream log(msgSvc(), name());
00175   log << MSG::ERROR << "This method cannot be called on an object of type " 
00176       << System::typeinfoName(typeid(*this)) << endreq;
00177   return StatusCode::FAILURE;
00178 }
00179 
00180 // implementation of IAppMgrUI::state: current state of the application manager
00181 const std::string& MinimalEventLoopMgr::state() const   {
00182   return m_appMgrUI->state();
00183 }
00184 
00185 // implemenation of IEventProcessor::executeEvent(void* par)
00186 StatusCode MinimalEventLoopMgr::executeEvent(void* /* par */)    {
00187   bool eventfailed = false;
00188   StatusCode sc;
00189   //--------------------------------------------------------------------------------------------
00190   // Call the execute() method of all top algorithms
00191   //--------------------------------------------------------------------------------------------
00192   for (ListAlg::iterator ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00193     (*ita)->resetExecuted();
00194     sc = (*ita)->sysExecute();
00195     if( !sc.isSuccess() )  {
00196       MsgStream log( msgSvc(), name() );
00197       log << MSG::WARNING << "Execution of algorithm " << (*ita)->name() << " failed" << endreq;
00198       eventfailed = true;
00199     }
00200   }
00201   //--------------------------------------------------------------------------------------------
00202   // Call the execute() method of all output streams
00203   //--------------------------------------------------------------------------------------------
00204   for (ListAlg::iterator ito = m_outStreamList.begin(); ito != m_outStreamList.end(); ito++ ) {
00205     (*ito)->resetExecuted();
00206     sc = (*ito)->sysExecute();
00207     if( !sc.isSuccess() )  {
00208       MsgStream log( msgSvc(), name() );
00209       log << MSG::WARNING << "Execution of output stream " << (*ito)->name() << " failed" << endreq;
00210       eventfailed = true;
00211     }
00212   }
00213   //--------------------------------------------------------------------------------------------
00214   // Check if there was an error processing current event
00215   //--------------------------------------------------------------------------------------------
00216   if( eventfailed ){
00217     MsgStream log( msgSvc(), name() );
00218     log << MSG::ERROR << "Error processing event loop." << endreq;
00219     return StatusCode::FAILURE;
00220   }
00221   return StatusCode::SUCCESS;
00222 }
00223 
00224 //============================================================================================
00225 // Top algorithm List handler
00226 //============================================================================================
00227 void MinimalEventLoopMgr::topAlgHandler( Property& /* theProp */ )              {
00228   decodeTopAlgs( );
00229 }
00230 
00231 //============================================================================================
00232 // decodeTopAlgNameList & topAlgNameListHandler
00233 //============================================================================================
00234 StatusCode MinimalEventLoopMgr::decodeTopAlgs()    {
00235   StatusCode result = StatusCode::SUCCESS;
00236   if ( CONFIGURED == m_state || INITIALIZED == m_state ) {
00237     SmartIF<IAlgManager> algMan(IID_IAlgManager, serviceLocator());
00238     MsgStream log(msgSvc(), name());
00239     if ( algMan.isValid() )   {
00240       // Reset the existing Top Algorithm List
00241       m_topAlgList.clear( );
00242       const std::vector<std::string>& algNames = m_topAlgNames.value( );
00243       for (VectorName::const_iterator it = algNames.begin(); it != algNames.end(); it++) {
00244         ListItem item(*it);
00245         // Got the type and name. Now creating the algorithm, avoiding duplicate creation.
00246         IAlgorithm* ialg;
00247         result = algMan->getAlgorithm( item.name(), ialg );
00248         if ( result.isSuccess( ) ) {
00249           log << MSG::DEBUG << "Top Algorithm " << item.name() << " already exists" << endreq;
00250         } 
00251         else {
00252           log << MSG::DEBUG << "Creating Top Algorithm " << item.type() << endreq;
00253           result = algMan->createAlgorithm( item.type(), item.name(), ialg );
00254           if( result.isFailure() ) {
00255             log << MSG::ERROR << "Unable to create Top Algorithm " << item.type() << endreq;
00256             return result;
00257           }
00258         }
00259         m_topAlgList.push_back(ialg);
00260       }
00261       if ( INITIALIZED == m_state ) {
00262         // Initialize the new list of Algorihms. Note that existing Algorithms
00263         // are protected against multiple initialization attempts.
00264         for (ListAlg::iterator ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ita++ ) {
00265           result = (*ita)->sysInitialize();
00266           if( result.isFailure() ) {
00267             log << MSG::ERROR << "Unable to initialize Algorithm: " << (*ita)->name() << endreq;
00268             return result;
00269           }
00270         }
00271       }
00272       return result;
00273     }
00274     result = StatusCode::FAILURE;
00275   }
00276   return result;
00277 }
00278 
00279 //============================================================================================
00280 // Output stream List handler
00281 //============================================================================================
00282 void MinimalEventLoopMgr::outStreamHandler( Property& /* theProp */ )           {
00283   decodeOutStreams( );
00284 }
00285 
00286 //============================================================================================
00287 // decodeOutStreamNameList & outStreamNameListHandler
00288 //============================================================================================
00289 StatusCode MinimalEventLoopMgr::decodeOutStreams( )    {
00290   StatusCode result = StatusCode::SUCCESS;
00291   if ( CONFIGURED == m_state || INITIALIZED == m_state ) {
00292     MsgStream log(msgSvc(), name());
00293     SmartIF<IAlgManager> algMan(IID_IAlgManager, serviceLocator());
00294     if ( algMan.isValid() )   {
00295       // Reset the existing Top Algorithm List
00296       m_outStreamList.clear();
00297       const std::vector<std::string>& algNames = m_outStreamNames.value( );
00298       for (VectorName::const_iterator it = algNames.begin(); it != algNames.end(); it++) {
00299         ListItem item(*it, "OutputStream");
00300         IAlgorithm* ios;
00301         log << MSG::DEBUG << "Creating OutputStream " << (*it) << endreq;
00302         result = algMan->getAlgorithm( item.name(), ios );
00303         if ( result.isSuccess( ) ) {
00304           log << MSG::DEBUG << "Output Stream " << item.name() << " already exists" << endreq;
00305         } 
00306         else {
00307           log << MSG::DEBUG << "Creating Output Stream " << (*it) << endreq;
00308           result = algMan->createAlgorithm( item.type(), item.name(), ios );
00309           if( result.isFailure( ) )    {
00310             log << MSG::ERROR << "Unable to create Output Stream " << (*it) << endreq;
00311             return result;
00312           }
00313         }
00314         m_outStreamList.push_back( ios );
00315       }
00316       if ( INITIALIZED == m_state ) {
00317         // Initialize the new list of Output Streams. Note that existing Output Streams
00318         // are protected against multiple initialization attempts.
00319         for (ListAlg::iterator ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ita++ ) {
00320           result = (*ita)->sysInitialize();
00321           if( result.isFailure() ) {
00322             log << MSG::ERROR << "Unable to initialize Output Stream: " << (*ita)->name() << endreq;
00323             return result;
00324           }
00325         }
00326       }
00327       return result;
00328     }
00329     result = StatusCode::FAILURE;
00330   }
00331   return result;
00332 }
00333 
00334 

Generated at Wed Nov 21 12:22:30 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000