| # Gaudi Framework, version v21r11 |

Home |  | Generated: 30 Sep 2010

# EventSelector.cpp

Go to the documentation of this file.

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