EventLoopMgr.cpp
Go to the documentation of this file.00001
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/AppReturnCode.h"
00015
00016 #include "HistogramAgent.h"
00017 #include "EventLoopMgr.h"
00018
00019
00020
00021 DECLARE_SERVICE_FACTORY(EventLoopMgr)
00022
00023
00024
00025
00026 EventLoopMgr::EventLoopMgr(const std::string& nam, ISvcLocator* svcLoc)
00027 : MinimalEventLoopMgr(nam, svcLoc)
00028 {
00029 m_histoDataMgrSvc = 0;
00030 m_histoPersSvc = 0;
00031 m_evtDataMgrSvc = 0;
00032 m_evtDataSvc = 0;
00033 m_evtSelector = 0;
00034 m_evtContext = 0;
00035 m_endEventFired = true;
00036
00037
00038 declareProperty("HistogramPersistency", m_histPersName = "");
00039 declareProperty("EvtSel", m_evtsel );
00040 declareProperty("Warnings",m_warnings=true,
00041 "Set this property to false to suppress warning messages");
00042 }
00043
00044
00045
00046
00047 EventLoopMgr::~EventLoopMgr() {
00048 if( m_histoDataMgrSvc ) m_histoDataMgrSvc->release();
00049 if( m_histoPersSvc ) m_histoPersSvc->release();
00050 if( m_evtDataMgrSvc ) m_evtDataMgrSvc->release();
00051 if( m_evtDataSvc ) m_evtDataSvc->release();
00052 if( m_evtSelector ) m_evtSelector->release();
00053 if( m_evtContext ) delete m_evtContext;
00054 }
00055
00056
00057
00058
00059 StatusCode EventLoopMgr::initialize() {
00060
00061 StatusCode sc = MinimalEventLoopMgr::initialize();
00062 MsgStream log(msgSvc(), name());
00063 if( !sc.isSuccess() ) {
00064 log << MSG::DEBUG << "Error Initializing base class MinimalEventLoopMgr." << endmsg;
00065 return sc;
00066 }
00067
00068
00069 m_evtDataMgrSvc = serviceLocator()->service("EventDataSvc");
00070 if( !m_evtDataMgrSvc.isValid() ) {
00071 log << MSG::FATAL << "Error retrieving EventDataSvc interface IDataManagerSvc." << endmsg;
00072 return StatusCode::FAILURE;
00073 }
00074 m_evtDataSvc = serviceLocator()->service("EventDataSvc");
00075 if( !m_evtDataSvc.isValid() ) {
00076 log << MSG::FATAL << "Error retrieving EventDataSvc interface IDataProviderSvc." << endmsg;
00077 return StatusCode::FAILURE;
00078 }
00079
00080
00081 m_appMgrProperty = serviceLocator();
00082 if ( ! m_appMgrProperty.isValid() ) {
00083 log << MSG::FATAL << "IProperty interface not found in ApplicationMgr." << endmsg;
00084 return StatusCode::FAILURE;
00085 }
00086
00087
00088 setProperty(m_appMgrProperty->getProperty("EvtSel")).ignore();
00089
00090 if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
00091 m_evtSelector = serviceLocator()->service("EventSelector");
00092 if( m_evtSelector.isValid() ) {
00093
00094 sc=m_evtSelector->createContext(m_evtContext);
00095 if( !sc.isSuccess() ) {
00096 log << MSG::FATAL << "Can not create the event selector Context." << endmsg;
00097 return sc;
00098 }
00099 }
00100 else {
00101 log << MSG::FATAL << "EventSelector not found." << endmsg;
00102 return sc;
00103 }
00104 }
00105 else {
00106 m_evtSelector = 0;
00107 m_evtContext = 0;
00108 if ( m_warnings ) {
00109 log << MSG::WARNING << "Unable to locate service \"EventSelector\" " << endmsg;
00110 log << MSG::WARNING << "No events will be processed from external input." << endmsg;
00111 }
00112 }
00113
00114
00115 m_histoDataMgrSvc = serviceLocator()->service("HistogramDataSvc");
00116 if( !m_histoDataMgrSvc.isValid() ) {
00117 log << MSG::FATAL << "Error retrieving HistogramDataSvc." << endmsg;
00118 return sc;
00119 }
00120
00121 m_histoPersSvc = serviceLocator()->service("HistogramPersistencySvc");
00122 if( !m_histoPersSvc.isValid() ) {
00123 log << MSG::WARNING << "Histograms cannot not be saved - though required." << endmsg;
00124 return sc;
00125 }
00126
00127 return StatusCode::SUCCESS;
00128 }
00129
00130
00131
00132 StatusCode EventLoopMgr::reinitialize() {
00133 MsgStream log(msgSvc(), name());
00134
00135
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
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
00149 if ( m_evtSelector.get() && m_evtContext ) {
00150
00151 m_evtSelector->releaseContext(m_evtContext);
00152 m_evtContext = 0;
00153 }
00154 m_evtSelector = theEvtSel;
00155 if (theSvc->FSMState() == Gaudi::StateMachine::INITIALIZED) {
00156 sc = theSvc->reinitialize();
00157 if( !sc.isSuccess() ) {
00158 log << MSG::ERROR << "Failure Reinitializing EventSelector "
00159 << theSvc->name( ) << endmsg;
00160 return sc;
00161 }
00162 }
00163 else {
00164 sc = theSvc->sysInitialize();
00165 if( !sc.isSuccess() ) {
00166 log << MSG::ERROR << "Failure Initializing EventSelector "
00167 << theSvc->name( ) << endmsg;
00168 return sc;
00169 }
00170 }
00171 sc = m_evtSelector->createContext(m_evtContext);
00172 if( !sc.isSuccess() ) {
00173 log << MSG::ERROR << "Can not create Context "
00174 << theSvc->name( ) << endmsg;
00175 return sc;
00176 }
00177 log << MSG::INFO << "EventSelector service changed to "
00178 << theSvc->name( ) << endmsg;
00179 }
00180 else if ( m_evtSelector.isValid() ) {
00181 if ( m_evtContext ) {
00182 m_evtSelector->releaseContext(m_evtContext);
00183 m_evtContext = 0;
00184 }
00185 sc = m_evtSelector->createContext(m_evtContext);
00186 if( !sc.isSuccess() ) {
00187 log << MSG::ERROR << "Can not create Context "
00188 << theSvc->name( ) << endmsg;
00189 return sc;
00190 }
00191 }
00192 }
00193 else if ( m_evtSelector.isValid() && m_evtContext ) {
00194 m_evtSelector->releaseContext(m_evtContext);
00195 m_evtSelector = 0;
00196 m_evtContext = 0;
00197 }
00198 return StatusCode::SUCCESS;
00199 }
00200
00201
00202
00203
00204
00205 StatusCode EventLoopMgr::stop() {
00206 if ( ! m_endEventFired ) {
00207
00208 m_incidentSvc->fireIncident(Incident(name(),IncidentType::EndEvent));
00209 m_endEventFired = true;
00210 }
00211 return MinimalEventLoopMgr::stop();
00212 }
00213
00214
00215
00216
00217 StatusCode EventLoopMgr::finalize() {
00218 StatusCode sc;
00219 MsgStream log(msgSvc(), name());
00220
00221
00222 sc = MinimalEventLoopMgr::finalize();
00223 if (! sc.isSuccess()) {
00224 log << MSG::ERROR << "Error finalizing base class" << endmsg;
00225 return sc;
00226 }
00227
00228
00229 if ( m_histoPersSvc != 0 ) {
00230 HistogramAgent agent;
00231 sc = m_histoDataMgrSvc->traverseTree( &agent );
00232 if( sc.isSuccess() ) {
00233 IDataSelector* objects = agent.selectedObjects();
00234
00235 if ( objects->size() > 0 ) {
00236 IDataSelector::iterator i;
00237 for ( i = objects->begin(); i != objects->end(); i++ ) {
00238 IOpaqueAddress* pAddr = 0;
00239 StatusCode iret = m_histoPersSvc->createRep(*i, pAddr);
00240 if ( iret.isSuccess() ) {
00241 (*i)->registry()->setAddress(pAddr);
00242 }
00243 else {
00244 sc = iret;
00245 }
00246 }
00247 for ( i = objects->begin(); i != objects->end(); i++ ) {
00248 IRegistry* reg = (*i)->registry();
00249 StatusCode iret = m_histoPersSvc->fillRepRefs(reg->address(), *i);
00250 if ( !iret.isSuccess() ) {
00251 sc = iret;
00252 }
00253 }
00254 }
00255 if ( sc.isSuccess() ) {
00256 log << MSG::INFO << "Histograms converted successfully according to request." << endmsg;
00257 }
00258 else {
00259 log << MSG::ERROR << "Error while saving Histograms." << endmsg;
00260 }
00261 }
00262 else {
00263 log << MSG::ERROR << "Error while traversing Histogram data store" << endmsg;
00264 }
00265 }
00266
00267
00268 if ( m_evtSelector && m_evtContext ) {
00269 m_evtSelector->releaseContext(m_evtContext).ignore();
00270 m_evtContext = 0;
00271 }
00272
00273
00274 m_histoDataMgrSvc = 0;
00275 m_histoPersSvc = 0;
00276
00277 m_evtSelector = 0;
00278 m_evtDataSvc = 0;
00279 m_evtDataMgrSvc = 0;
00280
00281 return StatusCode::SUCCESS;
00282 }
00283
00284
00285
00286
00287
00288 StatusCode EventLoopMgr::executeEvent(void* par) {
00289
00290
00291 m_incidentSvc->fireIncident(Incident(name(),IncidentType::BeginEvent));
00292
00293 if ( m_scheduledStop ) {
00294 MsgStream log( msgSvc(), name() );
00295 log << MSG::ALWAYS << "Terminating event processing loop due to a stop scheduled by an incident listener" << endmsg;
00296 return StatusCode::SUCCESS;
00297 }
00298
00299
00300 StatusCode sc = MinimalEventLoopMgr::executeEvent(par);
00301
00302
00303 if( !sc.isSuccess() ){
00304 MsgStream log( msgSvc(), name() );
00305 log << MSG::ERROR << "Terminating event processing loop due to errors" << endmsg;
00306 }
00307 return sc;
00308 }
00309
00310
00311
00312
00313 StatusCode EventLoopMgr::executeRun( int maxevt ) {
00314 StatusCode sc;
00315
00316 sc = MinimalEventLoopMgr::executeRun(maxevt);
00317 return sc;
00318 }
00319
00320
00321
00322
00323 StatusCode EventLoopMgr::nextEvent(int maxevt) {
00324 static int total_nevt = 0;
00325 DataObject* pObject = 0;
00326 StatusCode sc(StatusCode::SUCCESS, true);
00327 MsgStream log( msgSvc(), name() );
00328
00329
00330 Gaudi::setAppReturnCode(m_appMgrProperty, Gaudi::ReturnCode::Success, true).ignore();
00331
00332
00333
00334 for( int nevt = 0; (maxevt == -1 ? true : nevt < maxevt); nevt++, total_nevt++) {
00335
00336
00337 if ( m_scheduledStop ) {
00338 m_scheduledStop = false;
00339 log << MSG::ALWAYS << "Terminating event processing loop due to scheduled stop" << endmsg;
00340 break;
00341 }
00342
00343 if( 0 != total_nevt ) {
00344
00345 if ( ! m_endEventFired ) {
00346
00347 m_incidentSvc->fireIncident(Incident(name(),IncidentType::EndEvent));
00348 m_endEventFired = true;
00349 }
00350 sc = m_evtDataMgrSvc->clearStore();
00351 if( !sc.isSuccess() ) {
00352 log << MSG::DEBUG << "Clear of Event data store failed" << endmsg;
00353 }
00354 }
00355
00356
00357 if( m_evtContext ) {
00358 IOpaqueAddress* addr = 0;
00359
00360 sc = getEventRoot(addr);
00361 if( !sc.isSuccess() ) {
00362 log << MSG::INFO << "No more events in event selection " << endmsg;
00363 break;
00364 }
00365
00366 sc = m_evtDataMgrSvc->setRoot ("/Event", addr);
00367 if( !sc.isSuccess() ) {
00368 log << MSG::WARNING << "Error declaring event root address." << endmsg;
00369 continue;
00370 }
00371 sc = m_evtDataSvc->retrieveObject("/Event", pObject);
00372 if( !sc.isSuccess() ) {
00373 log << MSG::WARNING << "Unable to retrieve Event root object" << endmsg;
00374 break;
00375 }
00376 }
00377 else {
00378 sc = m_evtDataMgrSvc->setRoot ("/Event", new DataObject());
00379 if( !sc.isSuccess() ) {
00380 log << MSG::WARNING << "Error declaring event root DataObject" << endmsg;
00381 }
00382 }
00383
00384 sc = executeEvent(NULL);
00385 m_endEventFired = false;
00386 if( !sc.isSuccess() ){
00387 log << MSG::ERROR << "Terminating event processing loop due to errors" << endmsg;
00388 Gaudi::setAppReturnCode(m_appMgrProperty, Gaudi::ReturnCode::AlgorithmFailure).ignore();
00389 return sc;
00390 }
00391 }
00392 return StatusCode::SUCCESS;
00393 }
00394
00396 StatusCode EventLoopMgr::getEventRoot(IOpaqueAddress*& refpAddr) {
00397 refpAddr = 0;
00398 StatusCode sc = m_evtSelector->next(*m_evtContext);
00399 if ( !sc.isSuccess() ) {
00400 return sc;
00401 }
00402
00403 sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
00404 if( !sc.isSuccess() ) {
00405 sc = m_evtSelector->next(*m_evtContext);
00406 if ( sc.isSuccess() ) {
00407 sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
00408 if ( !sc.isSuccess() ) {
00409 MsgStream log( msgSvc(), name() );
00410 log << MSG::WARNING << "Error creating IOpaqueAddress." << endmsg;
00411 }
00412 }
00413 }
00414 return sc;
00415 }