Gaudi Framework, version v20r4

Generated: 8 Jan 2009

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

Generated at Thu Jan 8 17:44:22 2009 for Gaudi Framework, version v20r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004