| # Gaudi Framework, version v21r9 |

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