/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Control/OfflineEventLoopMgr/OfflineEventLoopMgr-00-00-15/src/OfflineEventLoopMgr.cxx

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

Generated on Tue Nov 29 22:57:59 2016 for BOSS_7.0.2 by  doxygen 1.4.7