| # Gaudi Framework, version v22r2 |

Home |  | Generated: Tue May 10 2011

# EventLoopMgr.cpp

Go to the documentation of this file.

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines