| # Gaudi Framework, version v20r2
Generated: 18 Jul 2008

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- Search for |

- File List
- File Members

# EventLoopMgr.cpp

Go to the documentation of this file.

    00001 // $Id: EventLoopMgr.cpp,v 1.25 2008/06/06 13:20:49 marcocle Exp $
    00002 #define  GAUDISVC_EVENTLOOPMGR_CPP
    00003
    00004 #include "GaudiKernel/SmartIF.h"
    00005 #include "GaudiKernel/Incident.h"
    00006 #include "GaudiKernel/MsgStream.h"
    00007 #include "GaudiKernel/SvcFactory.h"
    00008 #include "GaudiKernel/DataObject.h"
    00009 #include "GaudiKernel/IIncidentSvc.h"
    00010 #include "GaudiKernel/IEvtSelector.h"
    00011 #include "GaudiKernel/IDataManagerSvc.h"
    00012 #include "GaudiKernel/IDataProviderSvc.h"
    00013 #include "GaudiKernel/IConversionSvc.h"
    00014
    00015 #include "HistogramAgent.h"
    00016 #include "EventLoopMgr.h"
    00017
    00018 // Instantiation of a static factory class used by clients to create instances of this service
    00019 DECLARE_SERVICE_FACTORY(EventLoopMgr)
    00020
    00021 //--------------------------------------------------------------------------------------------
    00022 // Standard Constructor
    00023 //--------------------------------------------------------------------------------------------
    00024 EventLoopMgr::EventLoopMgr(const std::string& nam, ISvcLocator* svcLoc)
    00025 : MinimalEventLoopMgr(nam, svcLoc)
    00026 {
    00027   m_histoDataMgrSvc   = 0;
    00028   m_histoPersSvc      = 0;
    00029   m_evtDataMgrSvc     = 0;
    00030   m_evtDataSvc        = 0;
    00031   m_evtSelector       = 0;
    00032   m_evtContext        = 0;
    00033   m_endEventFired     = true;
    00034
    00035   // Declare properties
    00036   declareProperty("HistogramPersistency", m_histPersName = "");
    00037   declareProperty( "EvtSel", m_evtsel );
    00038 }
    00039
    00040 //--------------------------------------------------------------------------------------------
    00041 // Standard Destructor
    00042 //--------------------------------------------------------------------------------------------
    00043 EventLoopMgr::~EventLoopMgr()   {
    00044   if( m_histoDataMgrSvc ) m_histoDataMgrSvc->release();
    00045   if( m_histoPersSvc ) m_histoPersSvc->release();
    00046   if( m_evtDataMgrSvc ) m_evtDataMgrSvc->release();
    00047   if( m_evtDataSvc ) m_evtDataSvc->release();
    00048   if( m_evtSelector ) m_evtSelector->release();
    00049   if( m_evtContext ) delete m_evtContext;
    00050 }
    00051
    00052 //--------------------------------------------------------------------------------------------
    00053 // implementation of IAppMgrUI::initalize
    00054 //--------------------------------------------------------------------------------------------
    00055 StatusCode EventLoopMgr::initialize()    {
    00056   MsgStream log(msgSvc(), name());
    00057
    00058   // initilaize the base class
    00059   StatusCode sc = MinimalEventLoopMgr::initialize();
    00060   if( !sc.isSuccess() ) {
    00061     log << MSG::DEBUG << "Error Initializing base class MinimalEventLoopMgr." << endreq;
    00062     return sc;
    00063   }
    00064
    00065   // Setup access to event data services
    00066   sc = serviceLocator()->service("EventDataSvc", m_evtDataMgrSvc, true);
    00067   if( !sc.isSuccess() )  {
    00068     log << MSG::FATAL << "Error retrieving EventDataSvc interface IDataManagerSvc." << endreq;
    00069     return sc;
    00070   }
    00071   sc = serviceLocator()->service("EventDataSvc", m_evtDataSvc, true);
    00072   if( !sc.isSuccess() )  {
    00073     log << MSG::FATAL << "Error retrieving EventDataSvc interface IDataProviderSvc." << endreq;
    00074     return sc;
    00075   }
    00076
    00077   // Obtain the IProperty of the ApplicationMgr
    00078   SmartIF<IProperty> prpMgr(IID_IProperty, serviceLocator());
    00079   if ( ! prpMgr.isValid() )   {
    00080     log << MSG::FATAL << "IProperty interface not found in ApplicationMgr." << endreq;
    00081     return StatusCode::FAILURE;
    00082   }
    00083   else {
    00084     m_appMgrProperty = prpMgr;
    00085   }
    00086
    00087   // We do not expect a Event Selector necessarily being declared
    00088   setProperty(m_appMgrProperty->getProperty("EvtSel")).ignore();
    00089
    00090   if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
    00091     sc = serviceLocator()->service( "EventSelector", m_evtSelector, true );
    00092     if( sc.isSuccess() )     {
    00093       // Setup Event Selector
    00094       sc=m_evtSelector->createContext(m_evtContext);
    00095       if( !sc.isSuccess() )   {
    00096         log << MSG::FATAL << "Can not create the event selector Context." << endreq;
    00097         return sc;
    00098       }
    00099     }
    00100     else {
    00101       log << MSG::FATAL << "EventSelector not found." << endreq;
    00102       return sc;
    00103     }
    00104   }
    00105   else {
    00106     m_evtSelector = 0;
    00107     m_evtContext = 0;
    00108     log << MSG::WARNING << "Unable to locate service \"EventSelector\" " << endreq;
    00109     log << MSG::WARNING << "No events will be processed from external input." << endreq;
    00110   }
    00111
    00112   // Setup access to histogramming services
    00113   sc = serviceLocator()->service("HistogramDataSvc", m_histoDataMgrSvc, true);
    00114   if( !sc.isSuccess() )  {
    00115     log << MSG::FATAL << "Error retrieving HistogramDataSvc." << endreq;
    00116     return sc;
    00117   }
    00118   // Setup histogram persistency
    00119   sc = serviceLocator()->service("HistogramPersistencySvc", m_histoPersSvc, true );
    00120   if( !sc.isSuccess() ) {
    00121     log << MSG::WARNING << "Histograms cannot not be saved - though required." << endreq;
    00122     return sc;
    00123   }
    00124   return StatusCode::SUCCESS;
    00125 }
    00126 //--------------------------------------------------------------------------------------------
    00127 // implementation of IService::reinitialize
    00128 //--------------------------------------------------------------------------------------------
    00129 StatusCode EventLoopMgr::reinitialize() {
    00130   MsgStream log(msgSvc(), name());
    00131
    00132   // initilaize the base class
    00133   StatusCode sc = MinimalEventLoopMgr::reinitialize();
    00134   if( !sc.isSuccess() ) {
    00135     log << MSG::DEBUG << "Error Initializing base class MinimalEventLoopMgr." << endreq;
    00136     return sc;
    00137   }
    00138
    00139   // Check to see whether a new Event Selector has been specified
    00140   setProperty(m_appMgrProperty->getProperty("EvtSel"));
    00141   if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
    00142     IEvtSelector* theEvtSel;
    00143     IService*     theSvc;
    00144     sc = serviceLocator()->service( "EventSelector", theEvtSel );
    00145     sc = serviceLocator()->service( "EventSelector", theSvc );
    00146     if( sc.isSuccess() && ( theEvtSel != m_evtSelector ) ) {
    00147       // Setup Event Selector
    00148       m_evtSelector = theEvtSel;
    00149       if (theSvc->FSMState() == Gaudi::StateMachine::INITIALIZED) {
    00150         sc = theSvc->reinitialize();
    00151         if( !sc.isSuccess() ) {
    00152           log << MSG::ERROR << "Failure Reinitializing EventSelector "
    00153               << theSvc->name( ) << endreq;
    00154           return sc;
    00155         }
    00156       }
    00157       else {
    00158         sc = theSvc->sysInitialize();
    00159         if( !sc.isSuccess() ) {
    00160           log << MSG::ERROR << "Failure Initializing EventSelector "
    00161               << theSvc->name( ) << endreq;
    00162           return sc;
    00163         }
    00164       }
    00165       sc = m_evtSelector->createContext(m_evtContext);
    00166       if( !sc.isSuccess() ) {
    00167         log << MSG::ERROR << "Can not create Context "
    00168             << theSvc->name( ) << endreq;
    00169         return sc;
    00170       }
    00171       log << MSG::INFO << "EventSelector service changed to "
    00172           << theSvc->name( ) << endreq;
    00173     }
    00174     else if ( m_evtSelector && m_evtContext )   {
    00175       m_evtSelector->releaseContext(m_evtContext);
    00176       m_evtContext = 0;
    00177       sc = m_evtSelector->createContext(m_evtContext);
    00178       if( !sc.isSuccess() ) {
    00179         log << MSG::ERROR << "Can not create Context "
    00180             << theSvc->name( ) << endreq;
    00181         return sc;
    00182       }
    00183     }
    00184   }
    00185   else if ( m_evtSelector && m_evtContext )   {
    00186     m_evtSelector->releaseContext(m_evtContext);
    00187     m_evtSelector = 0;
    00188     m_evtContext = 0;
    00189   }
    00190   return StatusCode::SUCCESS;
    00191 }
    00192
    00193
    00194 //--------------------------------------------------------------------------------------------
    00195 // implementation of IService::stop
    00196 //--------------------------------------------------------------------------------------------
    00197 StatusCode EventLoopMgr::stop()    {
    00198   if ( ! m_endEventFired ) {
    00199     // Fire pending EndEvent incident
    00200     m_incidentSvc->fireIncident(Incident(name(),IncidentType::EndEvent));
    00201     m_endEventFired = true;
    00202   }
    00203   return MinimalEventLoopMgr::stop();
    00204 }
    00205
    00206 //--------------------------------------------------------------------------------------------
    00207 // implementation of IAppMgrUI::finalize
    00208 //--------------------------------------------------------------------------------------------
    00209 StatusCode EventLoopMgr::finalize()    {
    00210   StatusCode sc;
    00211   MsgStream log(msgSvc(), name());
    00212
    00213   // Finalize base class
    00214   sc = MinimalEventLoopMgr::finalize();
    00215   if (! sc.isSuccess()) {
    00216     log << MSG::ERROR << "Error finalizing base class" << endreq;
    00217     return sc;
    00218   }
    00219
    00220   // Save Histograms Now
    00221   if ( 0 != m_histoPersSvc )    {
    00222     HistogramAgent agent;
    00223     sc = m_histoDataMgrSvc->traverseTree( &agent );
    00224     if( sc.isSuccess() )   {
    00225       IDataSelector* objects = agent.selectedObjects();
    00226       // skip /stat entry!
    00227       if ( objects->size() > 0 )    {
    00228         IDataSelector::iterator i;
    00229         for ( i = objects->begin(); i != objects->end(); i++ )    {
    00230           IOpaqueAddress* pAddr = 0;
    00231           StatusCode iret = m_histoPersSvc->createRep(*i, pAddr);
    00232           if ( iret.isSuccess() )     {
    00233             (*i)->registry()->setAddress(pAddr);
    00234           }
    00235           else  {
    00236             sc = iret;
    00237           }
    00238         }
    00239         for ( i = objects->begin(); i != objects->end(); i++ )    {
    00240           IRegistry* reg = (*i)->registry();
    00241           StatusCode iret = m_histoPersSvc->fillRepRefs(reg->address(), *i);
    00242           if ( !iret.isSuccess() )    {
    00243             sc = iret;
    00244           }
    00245         }
    00246       }
    00247       if ( sc.isSuccess() )    {
    00248         log << MSG::INFO << "Histograms converted successfully according to request." << endreq;
    00249       }
    00250       else  {
    00251         log << MSG::ERROR << "Error while saving Histograms." << endreq;
    00252       }
    00253     }
    00254     else {
    00255       log << MSG::ERROR << "Error while traversing Histogram data store" << endreq;
    00256     }
    00257   }
    00258
    00259   // Release evemt selector context
    00260   if ( m_evtSelector && m_evtContext )   {
    00261     m_evtSelector->releaseContext(m_evtContext).ignore();
    00262     m_evtContext = 0;
    00263   }
    00264
    00265   // Release all interfaces...
    00266   m_histoDataMgrSvc = releaseInterface(m_histoDataMgrSvc);
    00267   m_histoPersSvc    = releaseInterface(m_histoPersSvc);
    00268
    00269   m_evtSelector     = releaseInterface(m_evtSelector);
    00270   m_evtDataSvc      = releaseInterface(m_evtDataSvc);
    00271   m_evtDataMgrSvc   = releaseInterface(m_evtDataMgrSvc);
    00272
    00273   return StatusCode::SUCCESS;
    00274 }
    00275
    00276
    00277 //--------------------------------------------------------------------------------------------
    00278 // executeEvent(void* par)
    00279 //--------------------------------------------------------------------------------------------
    00280 StatusCode EventLoopMgr::executeEvent(void* par)    {
    00281
    00282   // Fire BeginEvent "Incident"
    00283   m_incidentSvc->fireIncident(Incident(name(),IncidentType::BeginEvent));
    00284   // An incident may schedule a stop, in which case is better to exit before the actual execution.
    00285   if ( m_scheduledStop ) {
    00286     MsgStream  log( msgSvc(), name() );
    00287     log << MSG::ALWAYS << "Terminating event processing loop due to a stop scheduled by an incident listener" << endreq;
    00288     return StatusCode::SUCCESS;
    00289   }
    00290
    00291   // Execute Algorithms
    00292   StatusCode sc = MinimalEventLoopMgr::executeEvent(par);
    00293
    00294   // Check if there was an error processing current event
    00295   if( !sc.isSuccess() ){
    00296     MsgStream log( msgSvc(), name() );
    00297     log << MSG::ERROR << "Terminating event processing loop due to errors" << endreq;
    00298   }
    00299   return sc;
    00300 }
    00301
    00302 //--------------------------------------------------------------------------------------------
    00303 // IEventProcessing::executeRun
    00304 //--------------------------------------------------------------------------------------------
    00305 StatusCode EventLoopMgr::executeRun( int maxevt )    {
    00306   StatusCode  sc;
    00307   // initialize the base class
    00308   sc = MinimalEventLoopMgr::executeRun(maxevt);
    00309   return sc;
    00310 }
    00311
    00312 //--------------------------------------------------------------------------------------------
    00313 // implementation of IAppMgrUI::nextEvent
    00314 //--------------------------------------------------------------------------------------------
    00315 StatusCode EventLoopMgr::nextEvent(int maxevt)   {
    00316   static int        total_nevt = 0;
    00317   DataObject*       pObject = 0;
    00318   StatusCode        sc;
    00319   MsgStream         log( msgSvc(), name() );
    00320
    00321   // loop over events if the maxevt (received as input) if different from -1.
    00322   // if evtmax is -1 it means infinite loop
    00323   for( int nevt = 0; (maxevt == -1 ? true : nevt < maxevt);  nevt++, total_nevt++) {
    00324     // Check if there is a scheduled stop issued by some algorithm/sevice
    00325     if ( m_scheduledStop ) {
    00326       m_scheduledStop = false;
    00327       log << MSG::ALWAYS << "Terminating event processing loop due to scheduled stop" << endreq;
    00328       break;
    00329     }
    00330     // Clear the event store, if used in the event loop
    00331     if( 0 != total_nevt ) {
    00332
    00333       if ( ! m_endEventFired ) {
    00334         // Fire EndEvent "Incident" (it is considered part of the clearsing of the TS)
    00335         m_incidentSvc->fireIncident(Incident(name(),IncidentType::EndEvent));
    00336         m_endEventFired = true;
    00337       }
    00338       sc = m_evtDataMgrSvc->clearStore();
    00339       if( !sc.isSuccess() )  {
    00340         log << MSG::DEBUG << "Clear of Event data store failed" << endreq;
    00341       }
    00342     }
    00343
    00344     // Setup event in the event store
    00345     if( m_evtContext ) {
    00346       IOpaqueAddress* addr = 0;
    00347       // Only if there is a EventSelector
    00348       sc = getEventRoot(addr);
    00349       if( !sc.isSuccess() )  {
    00350         log << MSG::INFO << "No more events in event selection " << endreq;
    00351         break;
    00352       }
    00353       // Set root clears the event data store first
    00354       sc = m_evtDataMgrSvc->setRoot ("/Event", addr);
    00355       if( !sc.isSuccess() )  {
    00356         log << MSG::WARNING << "Error declaring event root address." << endreq;
    00357         continue;
    00358       }
    00359       sc = m_evtDataSvc->retrieveObject("/Event", pObject);
    00360       if( !sc.isSuccess() ) {
    00361         log << MSG::WARNING << "Unable to retrieve Event root object" << endreq;
    00362         break;
    00363       }
    00364     }
    00365     else {
    00366       sc = m_evtDataMgrSvc->setRoot ("/Event", new DataObject());
    00367       if( !sc.isSuccess() )  {
    00368         log << MSG::WARNING << "Error declaring event root DataObject" << endreq;
    00369       }
    00370     }
    00371     // Execute event for all required algorithms
    00372     sc = executeEvent(NULL);
    00373     m_endEventFired = false;
    00374     if( !sc.isSuccess() ){
    00375       log << MSG::ERROR << "Terminating event processing loop due to errors" << endreq;
    00376       break;
    00377     }
    00378   }
    00379   return StatusCode::SUCCESS;
    00380 }
    00381
    00383 StatusCode EventLoopMgr::getEventRoot(IOpaqueAddress*& refpAddr)  {
    00384   refpAddr = 0;
    00385   StatusCode sc = m_evtSelector->next(*m_evtContext);
    00386   if ( !sc.isSuccess() )  {
    00387     return sc;
    00388   }
    00389   // Create root address and assign address to data service
    00390   sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
    00391   if( !sc.isSuccess() )  {
    00392     sc = m_evtSelector->next(*m_evtContext);
    00393     if ( sc.isSuccess() )  {
    00394       sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
    00395       if ( !sc.isSuccess() )  {
    00396         MsgStream log( msgSvc(), name() );
    00397         log << MSG::WARNING << "Error creating IOpaqueAddress." << endreq;
    00398       }
    00399     }
    00400   }
    00401   return sc;
    00402 }