| # Gaudi Framework, version v23r0 |

Home |  | Generated: Mon Jan 30 2012

# EventLoopMgr.cpp

Go to the documentation of this file.

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

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