Gaudi Framework, version v21r7p1

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

Generated at Mon Feb 15 17:27:30 2010 for Gaudi Framework, version v21r7p1 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004