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 m_incidentSvc->fireIncident(Incident(s->dbName(),IncidentType::BeginInputFile));
00124 return StatusCode::SUCCESS;
00125 }
00126 }
00127 }
00128 }
00129 if(s!=NULL) m_incidentSvc->fireIncident(Incident(s->dbName(),IncidentType::FailInputFile));
00130 }
00131
00132 iter.set(this, -1, 0, 0);
00133 status.setChecked();
00134
00135 return StatusCode::FAILURE;
00136 }
00137
00138
00139 StatusCode
00140 EventSelector::lastOfPreviousStream(bool shutDown, EvtSelectorContext& iter) const {
00141 StatusCode status = StatusCode::SUCCESS;
00142 if ( shutDown ) {
00143 if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
00144 const EventSelectorDataStream* s = m_streamtool->getStream(iter.ID());
00145 if ( s->isInitialized() ) {
00146 EventSelector* thisPtr = const_cast<EventSelector*>(this);
00147 if ( s->selector() && iter.context() ) {
00148 Context* ctxt = iter.context();
00149 s->selector()->releaseContext(ctxt);
00150 iter.set(0,0);
00151 }
00152 status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
00153 iter.set(0,0);
00154 }
00155 }
00156 }
00157
00158 IDataStreamTool::size_type iter_id = iter.ID()-1;
00159 const EventSelectorDataStream* s ;
00160 status = m_streamtool->getPreviousStream( s , iter_id );
00161
00162 if ( status.isSuccess() ) {
00163
00164 if ( !s->isInitialized() ) {
00165 EventSelector* thisPtr = const_cast<EventSelector*>(this);
00166 status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
00167 }
00168 if ( status.isSuccess() ) {
00169 const IEvtSelector* sel = s->selector();
00170 if ( sel ) {
00171 Context* ctxt = 0;
00172 status = sel->createContext(ctxt);
00173 if ( status.isSuccess() ) {
00174 status = sel->resetCriteria(s->criteria(), *ctxt);
00175 if ( status.isSuccess() ) {
00176 MsgStream log(msgSvc(), name());
00177 iter.set(this, iter_id, ctxt, 0);
00178 log << MSG::INFO << *s << endmsg;
00179 return StatusCode::SUCCESS;
00180 }
00181 }
00182 }
00183 }
00184 }
00185
00186 iter.set(this, -1, 0, 0);
00187 return StatusCode::FAILURE;
00188 }
00189
00191 StatusCode EventSelector::createContext(Context*& refpCtxt) const
00192 {
00193
00194 refpCtxt = 0;
00195 if ( m_firstEvent < 0 ) {
00196 MsgStream log(msgSvc(), name());
00197 log << MSG::ERROR << "First Event = " << m_firstEvent << " not valid" << endmsg;
00198 log << MSG::ERROR << "It should be > 0 " << endmsg;
00199 return StatusCode::FAILURE;
00200 }
00201 EvtSelectorContext* ctxt = new EvtSelectorContext(this);
00202 ctxt->set(0, -1, 0, 0);
00203 firstOfNextStream(true, *ctxt).ignore();
00204 refpCtxt = ctxt;
00205 long nskip = m_firstEvent;
00206 while( --nskip > 0 ) {
00207 StatusCode sc = next(*refpCtxt);
00208 if ( sc.isFailure() ) {
00209 MsgStream log(msgSvc(), name());
00210 log << MSG::ERROR << " createContext() failed to start with event number "
00211 << m_firstEvent << endmsg;
00212 releaseContext(refpCtxt);
00213 refpCtxt = 0;
00214 return StatusCode::FAILURE;
00215 }
00216 }
00217 return StatusCode::SUCCESS;
00218 }
00219
00221 StatusCode EventSelector::next(Context& refCtxt) const {
00222 return next(refCtxt, 1);
00223 }
00224
00226 StatusCode EventSelector::next(Context& refCtxt, int ) const {
00227 EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
00228 if ( pIt ) {
00229 if ( pIt->ID() != -1 ) {
00230 const EventSelectorDataStream* s = m_streamtool->getStream(pIt->ID());
00231 Context* it = pIt->context();
00232 IEvtSelector* sel = s->selector();
00233 if ( it && sel ) {
00234 StatusCode sc = sel->next(*it);
00235 if ( !sc.isSuccess() ) {
00236 if(s!=NULL) m_incidentSvc->fireIncident(Incident(s->dbName(),IncidentType::EndInputFile));
00237 sc = firstOfNextStream(true, *pIt);
00238 if (sc.isSuccess() ) sc = next(*pIt);
00239 }
00240 else {
00241 pIt->increaseCounters(false);
00242 pIt->set(it, 0);
00243 printEvtInfo(pIt);
00244 }
00245 return sc;
00246 }
00247 else if ( m_reconfigure ) {
00248 StatusCode sc = firstOfNextStream(false, *pIt);
00249 printEvtInfo(pIt);
00250 return sc;
00251 }
00252 }
00253 else if ( m_reconfigure ) {
00254 StatusCode sc = firstOfNextStream(false, *pIt);
00255 printEvtInfo(pIt);
00256 return sc;
00257 }
00258 pIt->increaseCounters(false);
00259 }
00260 printEvtInfo(pIt);
00261 return StatusCode::FAILURE;
00262 }
00263
00265 StatusCode EventSelector::previous(Context& refCtxt) const {
00266 return previous(refCtxt, 1);
00267 }
00268
00270 StatusCode EventSelector::previous(Context& refCtxt, int jump) const {
00271 EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
00272 if ( pIt && jump > 0 ) {
00273 StatusCode sc = StatusCode::SUCCESS;
00274 for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
00275 const EventSelectorDataStream* s = m_streamtool->getStream(pIt->ID());
00276 Context* it = pIt->context();
00277 IEvtSelector* sel = s->selector();
00278 if ( it && sel ) {
00279
00280 sc = sel->previous(*it);
00281 if ( !sc.isSuccess() ) {
00282 sc = lastOfPreviousStream(true, *pIt);
00283 }
00284 else {
00285 pIt->increaseCounters(false);
00286 pIt->set(it, 0);
00287 }
00288 printEvtInfo(pIt);
00289 if ( !sc.isSuccess() ) {
00290 return sc;
00291 }
00292 }
00293 pIt->increaseCounters(false);
00294 }
00295 return sc;
00296 }
00297 printEvtInfo(pIt);
00298 return StatusCode::FAILURE;
00299 }
00300
00302 StatusCode EventSelector::last(Context& refCtxt) const {
00303 EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
00304 if ( pIt ) {
00305 }
00306 return StatusCode::FAILURE;
00307 }
00308
00310 StatusCode EventSelector::rewind(Context& refCtxt) const {
00311 EvtSelectorContext *ctxt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
00312 if ( ctxt ) {
00313 ctxt->set(0, -1, 0, 0);
00314 firstOfNextStream(true, *ctxt);
00315 long nskip = m_firstEvent;
00316 while( --nskip > 0 ) {
00317 StatusCode sc = next(*ctxt);
00318 if ( sc.isFailure() ) {
00319 MsgStream log(msgSvc(), name());
00320 log << MSG::ERROR << "rewind() failed to start with event number "
00321 << m_firstEvent << endmsg;
00322 return StatusCode::FAILURE;
00323 }
00324 }
00325 return StatusCode::SUCCESS;
00326 }
00327 return StatusCode::FAILURE;
00328 }
00329
00331 StatusCode
00332 EventSelector::createAddress(const Context& refCtxt,
00333 IOpaqueAddress*& refpAddr) const
00334 {
00335 const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(&refCtxt);
00336 EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
00337 refpAddr = 0;
00338 if ( pIt ) {
00339 const EventSelectorDataStream* s = m_streamtool->getStream(pIt->ID());
00340 Context* it = pIt->context();
00341 IEvtSelector* sel = s->selector();
00342 if ( it && sel ) {
00343 IOpaqueAddress* pAddr = 0;
00344 StatusCode sc = sel->createAddress(*it, pAddr);
00345 if ( sc.isSuccess() ) {
00346 refpAddr = pAddr;
00347 }
00348 pIt->set(it, pAddr);
00349 return sc;
00350 }
00351 }
00352 return StatusCode::FAILURE;
00353 }
00354
00355
00356 StatusCode EventSelector::releaseContext(Context*& refCtxt) const {
00357 const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(refCtxt);
00358 EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
00359 if ( pIt && pIt->ID() >= 0 && pIt->ID() < (long)m_streamtool->size() ) {
00360 const EventSelectorDataStream* s = m_streamtool->getStream(pIt->ID());
00361 Context* it = pIt->context();
00362 IEvtSelector* sel = s->selector();
00363 if ( it && sel ) {
00364 StatusCode sc = sel->releaseContext(it);
00365 if ( sc.isSuccess() ) {
00366 refCtxt = 0;
00367 delete pIt;
00368 return sc;
00369 }
00370 }
00371 }
00372 if ( pIt ) {
00373 delete pIt;
00374 }
00375 return StatusCode::SUCCESS;
00376 }
00377
00379 StatusCode EventSelector::initialize() {
00380
00381 StatusCode status = Service::initialize();
00382 MsgStream logger(msgSvc(), name());
00383 if ( !status.isSuccess() ) {
00384 logger << MSG::ERROR << "Error initializing base class Service!" << endmsg;
00385 return status;
00386 }
00387
00388 m_incidentSvc = serviceLocator()->service("IncidentSvc");
00389 if( !m_incidentSvc.isValid() ) {
00390 logger << MSG::FATAL << "Error retrieving IncidentSvc." << endmsg;
00391 return StatusCode::FAILURE;
00392 }
00393 if ( m_evtMax != INT_MAX ) {
00394 logger << MSG::ERROR << "EvtMax is an obsolete property of the event selector." << endmsg;
00395 logger << MSG::ERROR << "Please set \"ApplicationMgr.EvtMax = " << m_evtMax
00396 << ";\" to process the requested number of events." << endmsg;
00397 return StatusCode::FAILURE;
00398 }
00399
00400 m_toolSvc = serviceLocator()->service("ToolSvc");
00401 if ( !m_toolSvc.isValid() ) {
00402 logger << MSG::ERROR << " Could not locate the Tool Service! " << endmsg;
00403 return StatusCode::FAILURE;
00404 }
00405
00406 status = m_toolSvc->retrieveTool(m_streamManager.c_str(), m_streamtool, this);
00407
00408 if( status.isFailure() ) {
00409 logger << MSG::ERROR << "Error initializing "
00410 << m_streamManager << endmsg;
00411 return status;
00412 }
00413
00414 status = m_streamtool->clear();
00415 if( status.isFailure() ) {
00416
00417 return status;
00418 }
00419
00420 status = m_streamtool->addStreams(m_streamSpecs);
00421
00422 m_streamSpecsLast = m_streamSpecs;
00423
00424 m_streamID = 0;
00425
00426 return status;
00427 }
00428
00429
00430 StatusCode EventSelector::reinitialize() {
00431 if ( FSMState() != Gaudi::StateMachine::INITIALIZED ) {
00432 MsgStream logger(msgSvc(), name());
00433 logger << MSG::ERROR << "Cannot reinitialize: service not in state initialized" << endmsg;
00434 return StatusCode::FAILURE;
00435 }
00436
00437 if( m_streamSpecsLast != m_streamSpecs ) {
00438 StatusCode status = m_streamtool->clear();
00439 if ( status.isFailure() ) return status;
00440 m_streamSpecsLast = m_streamSpecs;
00441 m_reconfigure = true;
00442 return m_streamtool->addStreams(m_streamSpecs);
00443 }
00444
00445 return StatusCode::SUCCESS;
00446 }
00447
00448
00449 StatusCode EventSelector::finalize() {
00450
00451 MsgStream log(msgSvc(), name());
00452
00453 log << MSG::DEBUG << "finalize()" << endmsg;
00454
00455 m_incidentSvc = 0;
00456
00457 if (m_streamtool) {
00458 if (m_toolSvc.isValid()) {
00459 m_toolSvc->releaseTool(m_streamtool).ignore();
00460 } else {
00461
00462 m_streamtool->release();
00463 }
00464 m_streamtool = 0;
00465 }
00466
00467 m_toolSvc = 0;
00468
00469 return Service::finalize();
00470 }
00471