| # Gaudi Framework, version v21r10p1 |

Home |  | Generated: 29 Jul 2010

# 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
    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 // Standard Constructor
    00024 //--------------------------------------------------------------------------------------------
    00025 EventLoopMgr::EventLoopMgr(const std::string& nam, ISvcLocator* svcLoc)
    00026 : MinimalEventLoopMgr(nam, svcLoc)
    00027 {
    00028   m_histoDataMgrSvc   = 0;
    00029   m_histoPersSvc      = 0;
    00030   m_evtDataMgrSvc     = 0;
    00031   m_evtDataSvc        = 0;
    00032   m_evtSelector       = 0;
    00033   m_evtContext        = 0;
    00034   m_endEventFired     = true;
    00035
    00036   // Declare properties
    00037   declareProperty("HistogramPersistency", m_histPersName = "");
    00038   declareProperty("EvtSel", m_evtsel );
    00039   declareProperty("Warnings",m_warnings=true,
    00040                   "Set this property to false to suppress warning messages");
    00041 }
    00042
    00043 //--------------------------------------------------------------------------------------------
    00044 // Standard Destructor
    00045 //--------------------------------------------------------------------------------------------
    00046 EventLoopMgr::~EventLoopMgr()   {
    00047   if( m_histoDataMgrSvc ) m_histoDataMgrSvc->release();
    00048   if( m_histoPersSvc ) m_histoPersSvc->release();
    00049   if( m_evtDataMgrSvc ) m_evtDataMgrSvc->release();
    00050   if( m_evtDataSvc ) m_evtDataSvc->release();
    00051   if( m_evtSelector ) m_evtSelector->release();
    00052   if( m_evtContext ) delete m_evtContext;
    00053 }
    00054
    00055 //--------------------------------------------------------------------------------------------
    00056 // implementation of IAppMgrUI::initialize
    00057 //--------------------------------------------------------------------------------------------
    00058 StatusCode EventLoopMgr::initialize()    {
    00059   // Initialize the base class
    00060   StatusCode sc = MinimalEventLoopMgr::initialize();
    00061   MsgStream log(msgSvc(), name());
    00062   if( !sc.isSuccess() ) {
    00063     log << MSG::DEBUG << "Error Initializing base class MinimalEventLoopMgr." << endmsg;
    00064     return sc;
    00065   }
    00066
    00067   // Setup access to event data services
    00068   m_evtDataMgrSvc = serviceLocator()->service("EventDataSvc");
    00069   if( !m_evtDataMgrSvc.isValid() )  {
    00070     log << MSG::FATAL << "Error retrieving EventDataSvc interface IDataManagerSvc." << endmsg;
    00071     return StatusCode::FAILURE;
    00072   }
    00073   m_evtDataSvc = serviceLocator()->service("EventDataSvc");
    00074   if( !m_evtDataSvc.isValid() )  {
    00075     log << MSG::FATAL << "Error retrieving EventDataSvc interface IDataProviderSvc." << endmsg;
    00076     return StatusCode::FAILURE;
    00077   }
    00078
    00079   // Obtain the IProperty of the ApplicationMgr
    00080   SmartIF<IProperty> prpMgr(serviceLocator());
    00081   if ( ! prpMgr.isValid() )   {
    00082     log << MSG::FATAL << "IProperty interface not found in ApplicationMgr." << endmsg;
    00083     return StatusCode::FAILURE;
    00084   }
    00085   else {
    00086     m_appMgrProperty = prpMgr;
    00087   }
    00088
    00089   // We do not expect a Event Selector necessarily being declared
    00090   setProperty(m_appMgrProperty->getProperty("EvtSel")).ignore();
    00091
    00092   if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
    00093     m_evtSelector = serviceLocator()->service("EventSelector");
    00094     if( m_evtSelector.isValid() ) {
    00095       // Setup Event Selector
    00096       sc=m_evtSelector->createContext(m_evtContext);
    00097       if( !sc.isSuccess() )   {
    00098         log << MSG::FATAL << "Can not create the event selector Context." << endmsg;
    00099         return sc;
    00100       }
    00101     }
    00102     else {
    00103       log << MSG::FATAL << "EventSelector not found." << endmsg;
    00104       return sc;
    00105     }
    00106   }
    00107   else {
    00108     m_evtSelector = 0;
    00109     m_evtContext = 0;
    00110     if ( m_warnings ) {
    00111       log << MSG::WARNING << "Unable to locate service \"EventSelector\" " << endmsg;
    00112       log << MSG::WARNING << "No events will be processed from external input." << endmsg;
    00113     }
    00114   }
    00115
    00116   // Setup access to histogramming services
    00117   m_histoDataMgrSvc = serviceLocator()->service("HistogramDataSvc");
    00118   if( !m_histoDataMgrSvc.isValid() )  {
    00119     log << MSG::FATAL << "Error retrieving HistogramDataSvc." << endmsg;
    00120     return sc;
    00121   }
    00122   // Setup histogram persistency
    00123   m_histoPersSvc = serviceLocator()->service("HistogramPersistencySvc");
    00124   if( !m_histoPersSvc.isValid() ) {
    00125     log << MSG::WARNING << "Histograms cannot not be saved - though required." << endmsg;
    00126     return sc;
    00127   }
    00128
    00129   return StatusCode::SUCCESS;
    00130 }
    00131 //--------------------------------------------------------------------------------------------
    00132 // implementation of IService::reinitialize
    00133 //--------------------------------------------------------------------------------------------
    00134 StatusCode EventLoopMgr::reinitialize() {
    00135   MsgStream log(msgSvc(), name());
    00136
    00137   // Initialize the base class
    00138   StatusCode sc = MinimalEventLoopMgr::reinitialize();
    00139   if( !sc.isSuccess() ) {
    00140     log << MSG::DEBUG << "Error Initializing base class MinimalEventLoopMgr." << endmsg;
    00141     return sc;
    00142   }
    00143
    00144   // Check to see whether a new Event Selector has been specified
    00145   setProperty(m_appMgrProperty->getProperty("EvtSel"));
    00146   if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
    00147     SmartIF<IService> theSvc(serviceLocator()->service("EventSelector"));
    00148     SmartIF<IEvtSelector> theEvtSel(theSvc);
    00149     if( theEvtSel.isValid() && ( theEvtSel.get() != m_evtSelector.get() ) ) {
    00150       // Setup Event Selector
    00151       if ( m_evtSelector.get() && m_evtContext ) {
    00152         // Need to release context before switching to new event selector
    00153         m_evtSelector->releaseContext(m_evtContext);
    00154         m_evtContext = 0;
    00155       }
    00156       m_evtSelector = theEvtSel;
    00157       if (theSvc->FSMState() == Gaudi::StateMachine::INITIALIZED) {
    00158         sc = theSvc->reinitialize();
    00159         if( !sc.isSuccess() ) {
    00160           log << MSG::ERROR << "Failure Reinitializing EventSelector "
    00161               << theSvc->name( ) << endmsg;
    00162           return sc;
    00163         }
    00164       }
    00165       else {
    00166         sc = theSvc->sysInitialize();
    00167         if( !sc.isSuccess() ) {
    00168           log << MSG::ERROR << "Failure Initializing EventSelector "
    00169               << theSvc->name( ) << endmsg;
    00170           return sc;
    00171         }
    00172       }
    00173       sc = m_evtSelector->createContext(m_evtContext);
    00174       if( !sc.isSuccess() ) {
    00175         log << MSG::ERROR << "Can not create Context "
    00176             << theSvc->name( ) << endmsg;
    00177         return sc;
    00178       }
    00179       log << MSG::INFO << "EventSelector service changed to "
    00180           << theSvc->name( ) << endmsg;
    00181     }
    00182     else if ( m_evtSelector.isValid() ) {
    00183       if ( m_evtContext ) {
    00184         m_evtSelector->releaseContext(m_evtContext);
    00185         m_evtContext = 0;
    00186       }
    00187       sc = m_evtSelector->createContext(m_evtContext);
    00188       if( !sc.isSuccess() ) {
    00189         log << MSG::ERROR << "Can not create Context "
    00190             << 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   MsgStream log(msgSvc(), name());
    00222
    00223   // Finalize base class
    00224   sc = MinimalEventLoopMgr::finalize();
    00225   if (! sc.isSuccess()) {
    00226     log << MSG::ERROR << "Error finalizing base class" << endmsg;
    00227     return sc;
    00228   }
    00229
    00230   // Save Histograms Now
    00231   if ( m_histoPersSvc != 0 )    {
    00232     HistogramAgent agent;
    00233     sc = m_histoDataMgrSvc->traverseTree( &agent );
    00234     if( sc.isSuccess() )   {
    00235       IDataSelector* objects = agent.selectedObjects();
    00236       // skip /stat entry!
    00237       if ( objects->size() > 0 )    {
    00238         IDataSelector::iterator i;
    00239         for ( i = objects->begin(); i != objects->end(); i++ )    {
    00240           IOpaqueAddress* pAddr = 0;
    00241           StatusCode iret = m_histoPersSvc->createRep(*i, pAddr);
    00242           if ( iret.isSuccess() )     {
    00243             (*i)->registry()->setAddress(pAddr);
    00244           }
    00245           else  {
    00246             sc = iret;
    00247           }
    00248         }
    00249         for ( i = objects->begin(); i != objects->end(); i++ )    {
    00250           IRegistry* reg = (*i)->registry();
    00251           StatusCode iret = m_histoPersSvc->fillRepRefs(reg->address(), *i);
    00252           if ( !iret.isSuccess() )    {
    00253             sc = iret;
    00254           }
    00255         }
    00256       }
    00257       if ( sc.isSuccess() )    {
    00258         log << MSG::INFO << "Histograms converted successfully according to request." << endmsg;
    00259       }
    00260       else  {
    00261         log << MSG::ERROR << "Error while saving Histograms." << endmsg;
    00262       }
    00263     }
    00264     else {
    00265       log << MSG::ERROR << "Error while traversing Histogram data store" << endmsg;
    00266     }
    00267   }
    00268
    00269   // Release event selector context
    00270   if ( m_evtSelector && m_evtContext )   {
    00271     m_evtSelector->releaseContext(m_evtContext).ignore();
    00272     m_evtContext = 0;
    00273   }
    00274
    00275   // Release all interfaces...
    00276   m_histoDataMgrSvc = 0;
    00277   m_histoPersSvc    = 0;
    00278
    00279   m_evtSelector     = 0;
    00280   m_evtDataSvc      = 0;
    00281   m_evtDataMgrSvc   = 0;
    00282
    00283   return StatusCode::SUCCESS;
    00284 }
    00285
    00286
    00287 //--------------------------------------------------------------------------------------------
    00288 // executeEvent(void* par)
    00289 //--------------------------------------------------------------------------------------------
    00290 StatusCode EventLoopMgr::executeEvent(void* par)    {
    00291
    00292   // Fire BeginEvent "Incident"
    00293   m_incidentSvc->fireIncident(Incident(name(),IncidentType::BeginEvent));
    00294   // An incident may schedule a stop, in which case is better to exit before the actual execution.
    00295   if ( m_scheduledStop ) {
    00296     MsgStream  log( msgSvc(), name() );
    00297     log << MSG::ALWAYS << "Terminating event processing loop due to a stop scheduled by an incident listener" << endmsg;
    00298     return StatusCode::SUCCESS;
    00299   }
    00300
    00301   // Execute Algorithms
    00302   StatusCode sc = MinimalEventLoopMgr::executeEvent(par);
    00303
    00304   // Check if there was an error processing current event
    00305   if( !sc.isSuccess() ){
    00306     MsgStream log( msgSvc(), name() );
    00307     log << MSG::ERROR << "Terminating event processing loop due to errors" << endmsg;
    00308   }
    00309   return sc;
    00310 }
    00311
    00312 //--------------------------------------------------------------------------------------------
    00313 // IEventProcessing::executeRun
    00314 //--------------------------------------------------------------------------------------------
    00315 StatusCode EventLoopMgr::executeRun( int maxevt )    {
    00316   StatusCode  sc;
    00317   // initialize the base class
    00318   sc = MinimalEventLoopMgr::executeRun(maxevt);
    00319   return sc;
    00320 }
    00321
    00322 //--------------------------------------------------------------------------------------------
    00323 // implementation of IAppMgrUI::nextEvent
    00324 //--------------------------------------------------------------------------------------------
    00325 StatusCode EventLoopMgr::nextEvent(int maxevt)   {
    00326   static int        total_nevt = 0;
    00327   DataObject*       pObject = 0;
    00328   StatusCode        sc(StatusCode::SUCCESS, true);
    00329   MsgStream         log( msgSvc(), name() );
    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       log << MSG::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         log << MSG::DEBUG << "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         log << MSG::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         log << MSG::WARNING << "Error declaring event root address." << endmsg;
    00368         continue;
    00369       }
    00370       sc = m_evtDataSvc->retrieveObject("/Event", pObject);
    00371       if( !sc.isSuccess() ) {
    00372         log << MSG::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         log << MSG::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       log << MSG::ERROR << "Terminating event processing loop due to errors" << endmsg;
    00387       break;
    00388     }
    00389   }
    00390   return StatusCode::SUCCESS;
    00391 }
    00392
    00394 StatusCode EventLoopMgr::getEventRoot(IOpaqueAddress*& refpAddr)  {
    00395   refpAddr = 0;
    00396   StatusCode sc = m_evtSelector->next(*m_evtContext);
    00397   if ( !sc.isSuccess() )  {
    00398     return sc;
    00399   }
    00400   // Create root address and assign address to data service
    00401   sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
    00402   if( !sc.isSuccess() )  {
    00403     sc = m_evtSelector->next(*m_evtContext);
    00404     if ( sc.isSuccess() )  {
    00405       sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
    00406       if ( !sc.isSuccess() )  {
    00407         MsgStream log( msgSvc(), name() );
    00408         log << MSG::WARNING << "Error creating IOpaqueAddress." << endmsg;
    00409       }
    00410     }
    00411   }
    00412   return sc;
    00413 }