00001
00002
00003
00004 #include "GaudiKernel/xtoa.h"
00005 #include "GaudiKernel/SmartIF.h"
00006 #include "GaudiKernel/Incident.h"
00007 #include "GaudiKernel/SvcFactory.h"
00008 #include "GaudiKernel/Tokenizer.h"
00009 #include "GaudiKernel/MsgStream.h"
00010 #include "GaudiKernel/IIncidentSvc.h"
00011 #include "GaudiKernel/ISvcLocator.h"
00012 #include "GaudiKernel/ISvcManager.h"
00013 #include "GaudiKernel/IToolSvc.h"
00014 #include "GaudiKernel/IDataStreamTool.h"
00015 #include "GaudiKernel/IAddressCreator.h"
00016 #include "GaudiKernel/PropertyMgr.h"
00017 #include "GaudiKernel/EventSelectorDataStream.h"
00018
00019 #include "EventSelector.h"
00020 #include "EventIterator.h"
00021 #include <climits>
00022
00023 DECLARE_SERVICE_FACTORY(EventSelector)
00024
00025
00026 EventSelector::EventSelector(const std::string& name, ISvcLocator* svcloc )
00027 : base_class( name, svcloc)
00028 {
00029 m_incidentSvc = 0;
00030 m_toolSvc = 0;
00031 m_streamCount = 0;
00032 m_firstEvent = 0;
00033 m_evtPrintFrequency = 10;
00034 m_evtMax = INT_MAX;
00035 declareProperty( "Input", m_streamSpecs);
00036 declareProperty( "FirstEvent", m_firstEvent);
00037 declareProperty( "EvtMax", m_evtMax);
00038 declareProperty( "PrintFreq", m_evtPrintFrequency);
00039 declareProperty( "StreamManager", m_streamManager="DataStreamTool");
00040 m_reconfigure = false;
00041 }
00042
00043
00044 EventSelector::~EventSelector()
00045 {
00046 }
00047
00048 StatusCode
00049 EventSelector::resetCriteria(const std::string& ,
00050 Context& ) const
00051 {
00052 return StatusCode::FAILURE;
00053 }
00054
00055
00056 void EventSelector::printEvtInfo(const EvtSelectorContext* iter) const {
00057 if ( 0 != iter ) {
00058 long count = iter->numEvent();
00059
00060 if ( 0 == iter->context() ) {
00061 MsgStream log(msgSvc(), name());
00062 log << MSG::INFO << "End of event input reached." << endmsg;
00063 }
00064 else if( iter->numStreamEvent() == -1 ) {
00065
00066 }
00067 else if( m_evtPrintFrequency != -1 && (count % m_evtPrintFrequency == 0)) {
00068 MsgStream log(msgSvc(), name());
00069 log << MSG::ALWAYS << "Reading Event record " << count+1
00070 << ". Record number within stream " << iter->ID()+1
00071 << ": " << iter->numStreamEvent()+1 << endmsg;
00072 }
00073 }
00074 else {
00075 MsgStream log(msgSvc(), name());
00076 log << MSG::INFO << "End of event input reached." << endmsg;
00077 }
00078 }
00079
00080
00081 StatusCode
00082 EventSelector::firstOfNextStream(bool shutDown, EvtSelectorContext& iter) const {
00083 StatusCode status = StatusCode::SUCCESS;
00084 IDataStreamTool::size_type iter_id = (m_reconfigure) ? 0 : iter.ID()+1;
00085 if ( m_reconfigure ) const_cast<EventSelector*>(this)->m_reconfigure = false;
00086 if ( shutDown ) {
00087 if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
00088 const EventSelectorDataStream* s = m_streamtool->getStream(iter.ID());
00089 if ( s->isInitialized() ) {
00090 EventSelector* thisPtr = const_cast<EventSelector*>(this);
00091 if ( s->selector() && iter.context() ) {
00092 Context* ctxt = iter.context();
00093 s->selector()->releaseContext(ctxt).ignore();
00094 iter.set(0,0);
00095 }
00096 status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
00097 iter.set(0,0);
00098 }
00099 }
00100 }
00101
00102 const EventSelectorDataStream* s ;
00103 status = m_streamtool->getNextStream( s , iter_id );
00104
00105 if ( status.isSuccess() ) {
00106
00107 if ( !s->isInitialized() ) {
00108 EventSelector* thisPtr = const_cast<EventSelector*>(this);
00109 status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
00110 }
00111
00112 if ( status.isSuccess() ) {
00113 const IEvtSelector* sel = s->selector();
00114 if ( sel ) {
00115 Context* ctxt = 0;
00116 status = sel->createContext(ctxt);
00117 if ( status.isSuccess() ) {
00118 status = sel->resetCriteria(s->criteria(), *ctxt);
00119 if ( status.isSuccess() ) {
00120 MsgStream log(msgSvc(), name());
00121 iter.set(this, iter_id, ctxt, 0);
00122 log << MSG::INFO << *s << endmsg;
00123 return StatusCode::SUCCESS;
00124 }
00125 }
00126 }
00127 }
00128 }
00129
00130 iter.set(this, -1, 0, 0);
00131 status.setChecked();
00132 return StatusCode::FAILURE;
00133 }
00134
00135
00136 StatusCode
00137 EventSelector::lastOfPreviousStream(bool shutDown, EvtSelectorContext& iter) const {
00138 StatusCode status = StatusCode::SUCCESS;
00139 if ( shutDown ) {
00140 if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
00141 const EventSelectorDataStream* s = m_streamtool->getStream(iter.ID());
00142 if ( s->isInitialized() ) {
00143 EventSelector* thisPtr = const_cast<EventSelector*>(this);
00144 if ( s->selector() && iter.context() ) {
00145 Context* ctxt = iter.context();
00146 s->selector()->releaseContext(ctxt);
00147 iter.set(0,0);
00148 }
00149 status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
00150 iter.set(0,0);
00151 }
00152 }
00153 }
00154
00155 IDataStreamTool::size_type iter_id = iter.ID()-1;
00156 const EventSelectorDataStream* s ;
00157 status = m_streamtool->getPreviousStream( s , iter_id );
00158
00159 if ( status.isSuccess() ) {
00160
00161 if ( !s->isInitialized() ) {
00162 EventSelector* thisPtr = const_cast<EventSelector*>(this);
00163 status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
00164 }
00165 if ( status.isSuccess() ) {
00166 const IEvtSelector* sel = s->selector();
00167 if ( sel ) {
00168 Context* ctxt = 0;
00169 status = sel->createContext(ctxt);
00170 if ( status.isSuccess() ) {
00171 status = sel->resetCriteria(s->criteria(), *ctxt);
00172 if ( status.isSuccess() ) {
00173 MsgStream log(msgSvc(), name());
00174 iter.set(this, iter_id, ctxt, 0);
00175 log << MSG::INFO << *s << endmsg;
00176 return StatusCode::SUCCESS;
00177 }
00178 }
00179 }
00180 }
00181 }
00182
00183 iter.set(this, -1, 0, 0);
00184 return StatusCode::FAILURE;
00185 }
00186
00188 StatusCode EventSelector::createContext(Context*& refpCtxt) const
00189 {
00190
00191 refpCtxt = 0;
00192 if ( m_firstEvent < 0 ) {
00193 MsgStream log(msgSvc(), name());
00194 log << MSG::ERROR << "First Event = " << m_firstEvent << " not valid" << endmsg;
00195 log << MSG::ERROR << "It should be > 0 " << endmsg;
00196 return StatusCode::FAILURE;
00197 }
00198 EvtSelectorContext* ctxt = new EvtSelectorContext(this);
00199 ctxt->set(0, -1, 0, 0);
00200 firstOfNextStream(true, *ctxt).ignore();
00201 refpCtxt = ctxt;
00202 long nskip = m_firstEvent;
00203 while( --nskip > 0 ) {
00204 StatusCode sc = next(*refpCtxt);
00205 if ( sc.isFailure() ) {
00206 MsgStream log(msgSvc(), name());
00207 log << MSG::ERROR << " createContext() failed to start with event number "
00208 << m_firstEvent << endmsg;
00209 releaseContext(refpCtxt);
00210 refpCtxt = 0;
00211 return StatusCode::FAILURE;
00212 }
00213 }
00214 return StatusCode::SUCCESS;
00215 }
00216
00218 StatusCode EventSelector::next(Context& refCtxt) const {
00219 return next(refCtxt, 1);
00220 }
00221
00223 StatusCode EventSelector::next(Context& refCtxt, int ) const {
00224 EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
00225 if ( pIt ) {
00226 if ( pIt->ID() != -1 ) {
00227 const EventSelectorDataStream* s = m_streamtool->getStream(pIt->ID());
00228 Context* it = pIt->context();
00229 IEvtSelector* sel = s->selector();
00230 if ( it && sel ) {
00231 StatusCode sc = sel->next(*it);
00232 if ( !sc.isSuccess() ) {
00233 sc = firstOfNextStream(true, *pIt);
00234 if (sc.isSuccess() ) sc = next(*pIt);
00235 }
00236 else {
00237 pIt->increaseCounters(false);
00238 pIt->set(it, 0);
00239 printEvtInfo(pIt);
00240 }
00241 return sc;
00242 }
00243 else if ( m_reconfigure ) {
00244 StatusCode sc = firstOfNextStream(false, *pIt);
00245 printEvtInfo(pIt);
00246 return sc;
00247 }
00248 }
00249 else if ( m_reconfigure ) {
00250 StatusCode sc = firstOfNextStream(false, *pIt);
00251 printEvtInfo(pIt);
00252 return sc;
00253 }
00254 pIt->increaseCounters(false);
00255 }
00256 printEvtInfo(pIt);
00257 return StatusCode::FAILURE;
00258 }
00259
00261 StatusCode EventSelector::previous(Context& refCtxt) const {
00262 return previous(refCtxt, 1);
00263 }
00264
00266 StatusCode EventSelector::previous(Context& it,int jump) const {
00267 EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&it);
00268 if ( pIt && jump > 0 ) {
00269 StatusCode sc = StatusCode::SUCCESS;
00270 for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
00271 const EventSelectorDataStream* s = m_streamtool->getStream(pIt->ID());
00272 Context* it = pIt->context();
00273 IEvtSelector* sel = s->selector();
00274 if ( it && sel ) {
00275
00276 sc = sel->previous(*it);
00277 if ( !sc.isSuccess() ) {
00278 sc = lastOfPreviousStream(true, *pIt);
00279 }
00280 else {
00281 pIt->increaseCounters(false);
00282 pIt->set(it, 0);
00283 }
00284 printEvtInfo(pIt);
00285 if ( !sc.isSuccess() ) {
00286 return sc;
00287 }
00288 }
00289 pIt->increaseCounters(false);
00290 }
00291 return sc;
00292 }
00293 printEvtInfo(pIt);
00294 return StatusCode::FAILURE;
00295 }
00296
00298 StatusCode EventSelector::last(Context& refCtxt) const {
00299 EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
00300 if ( pIt ) {
00301 }
00302 return StatusCode::FAILURE;
00303 }
00304
00306 StatusCode EventSelector::rewind(Context& refCtxt) const {
00307 EvtSelectorContext *ctxt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
00308 if ( ctxt ) {
00309 ctxt->set(0, -1, 0, 0);
00310 firstOfNextStream(true, *ctxt);
00311 long nskip = m_firstEvent;
00312 while( --nskip > 0 ) {
00313 StatusCode sc = next(*ctxt);
00314 if ( sc.isFailure() ) {
00315 MsgStream log(msgSvc(), name());
00316 log << MSG::ERROR << "rewind() failed to start with event number "
00317 << m_firstEvent << endmsg;
00318 return StatusCode::FAILURE;
00319 }
00320 }
00321 return StatusCode::SUCCESS;
00322 }
00323 return StatusCode::FAILURE;
00324 }
00325
00327 StatusCode
00328 EventSelector::createAddress(const Context& refCtxt,
00329 IOpaqueAddress*& refpAddr) const
00330 {
00331 const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(&refCtxt);
00332 EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
00333 refpAddr = 0;
00334 if ( pIt ) {
00335 const EventSelectorDataStream* s = m_streamtool->getStream(pIt->ID());
00336 Context* it = pIt->context();
00337 IEvtSelector* sel = s->selector();
00338 if ( it && sel ) {
00339 IOpaqueAddress* pAddr = 0;
00340 StatusCode sc = sel->createAddress(*it, pAddr);
00341 if ( sc.isSuccess() ) {
00342 refpAddr = pAddr;
00343 }
00344 pIt->set(it, pAddr);
00345 return sc;
00346 }
00347 }
00348 return StatusCode::FAILURE;
00349 }
00350
00351
00352 StatusCode EventSelector::releaseContext(Context*& refCtxt) const {
00353 const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(refCtxt);
00354 EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
00355 if ( pIt && pIt->ID() >= 0 && pIt->ID() < (long)m_streamtool->size() ) {
00356 const EventSelectorDataStream* s = m_streamtool->getStream(pIt->ID());
00357 Context* it = pIt->context();
00358 IEvtSelector* sel = s->selector();
00359 if ( it && sel ) {
00360 StatusCode sc = sel->releaseContext(it);
00361 if ( sc.isSuccess() ) {
00362 refCtxt = 0;
00363 delete pIt;
00364 return sc;
00365 }
00366 }
00367 }
00368 if ( pIt ) {
00369 delete pIt;
00370 }
00371 return StatusCode::SUCCESS;
00372 }
00373
00375 StatusCode EventSelector::initialize() {
00376
00377 StatusCode status = Service::initialize();
00378 MsgStream logger(msgSvc(), name());
00379 if ( !status.isSuccess() ) {
00380 logger << MSG::ERROR << "Error initializing base class Service!" << endmsg;
00381 return status;
00382 }
00383
00384 m_incidentSvc = serviceLocator()->service("IncidentSvc");
00385 if( !m_incidentSvc.isValid() ) {
00386 logger << MSG::FATAL << "Error retrieving IncidentSvc." << endmsg;
00387 return StatusCode::FAILURE;
00388 }
00389 if ( m_evtMax != INT_MAX ) {
00390 logger << MSG::ERROR << "EvtMax is an obsolete property of the event selector." << endmsg;
00391 logger << MSG::ERROR << "Please set \"ApplicationMgr.EvtMax = " << m_evtMax
00392 << ";\" to process the requested number of events." << endmsg;
00393 return StatusCode::FAILURE;
00394 }
00395
00396 m_toolSvc = serviceLocator()->service("ToolSvc");
00397 if ( !m_toolSvc.isValid() ) {
00398 logger << MSG::ERROR << " Could not locate the Tool Service! " << endmsg;
00399 return StatusCode::FAILURE;
00400 }
00401
00402 status = m_toolSvc->retrieveTool(m_streamManager.c_str(), m_streamtool, this);
00403
00404 if( status.isFailure() ) {
00405 logger << MSG::ERROR << "Error initializing "
00406 << m_streamManager << endmsg;
00407 return status;
00408 }
00409
00410 status = m_streamtool->clear();
00411 if( status.isFailure() ) {
00412
00413 return status;
00414 }
00415
00416 status = m_streamtool->addStreams(m_streamSpecs);
00417
00418 m_streamSpecsLast = m_streamSpecs;
00419
00420 m_streamID = 0;
00421
00422 return status;
00423 }
00424
00425
00426 StatusCode EventSelector::reinitialize() {
00427 if ( FSMState() != Gaudi::StateMachine::INITIALIZED ) {
00428 MsgStream logger(msgSvc(), name());
00429 logger << MSG::ERROR << "Cannot reinitialize: service not in state initialized" << endmsg;
00430 return StatusCode::FAILURE;
00431 }
00432
00433 if( m_streamSpecsLast != m_streamSpecs ) {
00434 StatusCode status = m_streamtool->clear();
00435 if ( status.isFailure() ) return status;
00436 m_streamSpecsLast = m_streamSpecs;
00437 m_reconfigure = true;
00438 return m_streamtool->addStreams(m_streamSpecs);
00439 }
00440
00441 return StatusCode::SUCCESS;
00442 }
00443
00444
00445 StatusCode EventSelector::finalize() {
00446
00447 MsgStream log(msgSvc(), name());
00448
00449 log << MSG::DEBUG << "finalize()" << endmsg;
00450
00451 m_incidentSvc = 0;
00452
00453 if (m_streamtool) {
00454 if (m_toolSvc.isValid()) {
00455 m_toolSvc->releaseTool(m_streamtool).ignore();
00456 } else {
00457
00458 m_streamtool->release();
00459 }
00460 m_streamtool = 0;
00461 }
00462
00463 m_toolSvc = 0;
00464
00465 return Service::finalize();
00466 }
00467