| # Gaudi Framework, version v20r3
Generated: 24 Nov 2008

# 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
    00019 #include "EventSelector.h"
    00020 #include "EventIterator.h"
    00021 #include <climits>
    00022
    00023 DECLARE_SERVICE_FACTORY(EventSelector)
    00024
    00025 // Standard constructor
    00026 EventSelector::EventSelector(const std::string& name, ISvcLocator* svcloc )
    00027   : Service( 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 // Standard destructor
    00044 EventSelector::~EventSelector()
    00045 {
    00046 }
    00047
    00048 StatusCode
    00049 EventSelector::resetCriteria(const std::string& /* criteria */,
    00050                              Context&           /* context  */)  const
    00051 {
    00052   return StatusCode::FAILURE;
    00053 }
    00054
    00055 // Progress report
    00056 void EventSelector::printEvtInfo(const EvtSelectorContext* iter) const {
    00057   if ( 0 != iter )  {
    00058     long count = iter->numEvent();
    00059     // Print an message every m_evtPrintFrequency events
    00060     if ( 0 == iter->context() )   {
    00061       MsgStream log(messageService(), name());
    00062       log << MSG::INFO << "End of event input reached." << endreq;
    00063     }
    00064     else if( iter->numStreamEvent() == -1 ) {
    00065       // Intial value for this stream
    00066     }
    00067     else if( m_evtPrintFrequency != -1 && (count % m_evtPrintFrequency == 0))   {
    00068       MsgStream log(messageService(), name());
    00069       log << MSG::ALWAYS << "Reading Event record " << count+1
    00070           << ". Record number within stream " << iter->ID()+1
    00071           << ": " << iter->numStreamEvent()+1 << endreq;
    00072     }
    00073   }
    00074   else  {
    00075     MsgStream log(messageService(), name());
    00076     log << MSG::INFO << "End of event input reached." << endreq;
    00077   }
    00078 }
    00079
    00080 // IEvtSelector::first()
    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(messageService(), name());
    00121             iter.set(this, iter_id, ctxt, 0);
    00122             log << MSG::INFO << *s << endreq;
    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 // IEvtSelector::first()
    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(messageService(), name());
    00174             iter.set(this, iter_id, ctxt, 0);
    00175             log << MSG::INFO << *s << endreq;
    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   // Max event is zero. Return begin = end
    00191   refpCtxt = 0;
    00192   if ( m_firstEvent < 0 ) {
    00193     MsgStream log(messageService(), name());
    00194     log << MSG::ERROR  << "First Event = " << m_firstEvent << " not valid" << endreq;
    00195     log << MSG::ERROR  << "It should be > 0 " << endreq;
    00196     return StatusCode::FAILURE;    // if failure => iterators = end();
    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(messageService(), name());
    00207       log << MSG::ERROR << " createContext() failed to start with event number "
    00208           << m_firstEvent << endreq;
    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 /* jump */ ) 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 )    { // First exploit the current stream
    00231         StatusCode sc = sel->next(*it);  // This stream is empty: advance to the next stream
    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 )    { // First exploit the current stream
    00275                             // This stream is empty: advance to the next stream
    00276         sc = sel->previous(*it);  // This stream is empty: advance to the next stream
    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(messageService(), name());
    00316         log << MSG::ERROR << "rewind() failed to start with event number "
    00317             << m_firstEvent << endreq;
    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 // Release existing event iteration context
    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::queryInterface(const InterfaceID& riid, void** ppvInterface)  {
    00376   if ( riid == IID_IEvtSelector )  {
    00377     *ppvInterface = (IEvtSelector*)this;
    00378     addRef();
    00379     return SUCCESS;
    00380   }
    00381   return Service::queryInterface( riid, ppvInterface );
    00382 }
    00383
    00385 StatusCode EventSelector::initialize()    {
    00386   // Initialize base class
    00387   StatusCode status = Service::initialize();
    00388   MsgStream logger(messageService(), name());
    00389   if ( !status.isSuccess() )    {
    00390     logger << MSG::ERROR << "Error initializing base class Service!" << endreq;
    00391     return status;
    00392   }
    00393   // Get the references to the services that are needed by the ApplicationMgr itself
    00394   StatusCode sc = serviceLocator()->service("IncidentSvc", m_incidentSvc, true);
    00395   if( !sc.isSuccess() )  {
    00396     logger << MSG::FATAL << "Error retrieving IncidentSvc." << endreq;
    00397     return sc;
    00398   }
    00399   if ( m_evtMax != INT_MAX )   {
    00400     logger << MSG::ERROR << "EvtMax is an obsolete property of the event selector." << endreq;
    00401     logger << MSG::ERROR << "Please set \"ApplicationMgr.EvtMax = " << m_evtMax
    00402            << ";\" to process the requested number of events." << endreq;
    00403     return StatusCode::FAILURE;
    00404   }
    00405
    00406   m_toolSvc = 0 ;
    00407   sc = serviceLocator()->service( "ToolSvc" , m_toolSvc , true );
    00408   if ( sc.isFailure() )
    00409   { logger << MSG::ERROR << " Could not locate the Tool Service! " << endreq ;
    00410   return StatusCode::FAILURE ;}
    00411
    00412   sc = m_toolSvc->retrieveTool(m_streamManager.c_str(), m_streamtool, this );
    00413
    00414   if( sc.isFailure() ) {
    00415     logger << MSG::ERROR << "Error initializing "
    00416            << m_streamManager << endreq;
    00417     return sc;
    00418   }
    00419
    00420   sc = m_streamtool->clear();
    00421   if( sc.isFailure() ) {
    00422     // Message already printed by the tool
    00423     return sc;
    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(messageService(), name());
    00439     logger << MSG::ERROR << "Cannot reinitialize: service not in state initialized" << endreq;
    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()" << endreq;
    00460
    00461   if( m_incidentSvc ) {
    00462     m_incidentSvc->release();
    00463     m_incidentSvc = 0 ;
    00464   }
    00465
    00466   if( m_streamtool ) {
    00467     m_streamtool->release();
    00468     m_streamtool = 0 ;
    00469   }
    00470
    00471   if( m_toolSvc ) {
    00472     m_toolSvc->release();
    00473     m_toolSvc = 0 ;
    00474   }
    00475
    00476   return Service::finalize();
    00477 }
    00478