| # Gaudi Framework, version v20r2
Generated: 18 Jul 2008

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- Search for |

- File List
- File Members

# DataStreamTool.cpp

Go to the documentation of this file.

    00001 // $Id: DataStreamTool.cpp,v 1.5 2008/04/04 15:12:19 marcocle Exp $
    00002 // Include files
    00003
    00004 // from Gaudi
    00005 #include "GaudiKernel/MsgStream.h"
    00006 #include "GaudiKernel/xtoa.h"
    00007 #include "GaudiKernel/SmartIF.h"
    00008 #include "GaudiKernel/Incident.h"
    00009 #include "GaudiKernel/Tokenizer.h"
    00010 #include "GaudiKernel/MsgStream.h"
    00011 #include "GaudiKernel/IIncidentSvc.h"
    00012 #include "GaudiKernel/ISvcLocator.h"
    00013 #include "GaudiKernel/ISvcManager.h"
    00014 #include "GaudiKernel/IAddressCreator.h"
    00015 #include "GaudiKernel/PropertyMgr.h"
    00016 #include "GaudiKernel/EventSelectorDataStream.h"
    00017 #include "GaudiKernel/DataStreamTool.h"
    00018 #include "GaudiKernel/ToolFactory.h"
    00019 #include "GaudiKernel/SvcFactory.h"
    00020
    00021 //-----------------------------------------------------------------------------
    00022 // Implementation file for class : DataStreamTool
    00023 //
    00024 // 2006-09-21 : Andres Felipe Osorio Oliveros
    00025 //-----------------------------------------------------------------------------
    00026
    00027 // Declaration of the Tool Factory
    00028 // Now the declaration is done in GaudiSvc
    00029 //DECLARE_TOOL_FACTORY( DataStreamTool )
    00030
    00031 //=============================================================================
    00032 // Standard constructor, initializes variables
    00033 //=============================================================================
    00034 DataStreamTool::DataStreamTool( const std::string& type,
    00035                                 const std::string& name,
    00036                                 const IInterface* parent )
    00037   : AlgTool ( type, name , parent )
    00038 {
    00039   declareInterface<IDataStreamTool>(this);
    00040
    00041   m_incidentSvc       = 0;
    00042   m_streamCount       = 0;
    00043   m_streamID          = 0;
    00044
    00045 }
    00046 //=============================================================================
    00047 // Destructor
    00048 //=============================================================================
    00049 DataStreamTool::~DataStreamTool() {
    00050 }
    00051
    00052 //=============================================================================
    00053 StatusCode DataStreamTool::initialize() {
    00054
    00055   MsgStream logger(msgSvc(), name());
    00056
    00057   StatusCode status = AlgTool::initialize();
    00058   if( !status.isSuccess() )  {
    00059     logger << MSG::FATAL << "Error. Cannot initialize base class." << endreq;
    00060     return status;
    00061   }
    00062
    00063   // Get the references to the services that are needed by the ApplicationMgr itself
    00064   status = serviceLocator()->service("IncidentSvc", m_incidentSvc, true);
    00065   if( !status.isSuccess() )  {
    00066     logger << MSG::FATAL << "Error retrieving IncidentSvc." << endreq;
    00067     return status;
    00068   }
    00069
    00070   return StatusCode::SUCCESS;
    00071
    00072 }
    00073
    00074 StatusCode DataStreamTool::addStream(const std::string & input) {
    00075
    00076   if ( NULL != getStream(input) )   {
    00077     MsgStream log(msgSvc(), name());
    00078     log << MSG::WARNING << "Input stream " << input << "already in use" << endmsg;
    00079   }
    00080
    00081   m_streamSpecs.push_back(input);
    00082
    00083   char txt[32];
    00084
    00085   std::string strname = name() + "_" + ::_itoa(++m_streamCount, txt, 10);
    00086
    00087   EventSelectorDataStream* s = 0;
    00088
    00089   StatusCode status = createStream(strname, input , s );
    00090
    00091   if( status.isSuccess() && 0 != s ) {
    00092     s->addRef();
    00093     m_streams.push_back(s);
    00094     status = StatusCode::SUCCESS;
    00095   }
    00096   else {
    00097     MsgStream log(msgSvc(), name());
    00098     if (s) {
    00099       s->release();
    00100       log << MSG::ERROR << "Error connecting/creating Stream: " << s << endreq;
    00101     }
    00102     log << MSG::ERROR << "Error connecting/creating Stream: " << input << endreq;
    00103     status = StatusCode::FAILURE;
    00104   }
    00105
    00106   return status;
    00107
    00108 }
    00109
    00110 StatusCode DataStreamTool::addStreams(const StreamSpecs & inputs) {
    00111
    00112   StatusCode status = StatusCode::SUCCESS;
    00113
    00114   for ( StreamSpecs::const_iterator itr = inputs.begin(); itr != inputs.end() && status.isSuccess(); ++itr )    {
    00115
    00116     status = addStream(*itr);
    00117
    00118   }
    00119
    00120   return status;
    00121
    00122 }
    00123
    00124 StatusCode DataStreamTool::finalize() {
    00125
    00126   StatusCode status = StatusCode::SUCCESS;
    00127
    00128   clear().ignore();
    00129
    00130   if( m_incidentSvc ) {
    00131     m_incidentSvc->release();
    00132     m_incidentSvc = 0 ;
    00133   }
    00134
    00135   status = AlgTool::finalize();
    00136
    00137   return status;
    00138
    00139 }
    00140
    00141 StatusCode DataStreamTool::initializeStream(EventSelectorDataStream* s)   {
    00142   IEvtSelector* sel = 0;
    00143   StatusCode status = s->initialize();
    00144   if ( status.isSuccess() )   {
    00145     status = createSelector(s->name(), s->selectorType(), sel);
    00146     if ( status.isSuccess() )   {
    00147       SmartIF<IProperty> prop(IID_IProperty, sel); //Att:IID_IProperty, IID_IService used to point to EventSelector
    00148       SmartIF<IService>  isvc(IID_IService, sel);
    00149       s->setSelector(sel);
    00150       sel->release();  // No need of this interface anymore, it is passed to the stream
    00151       if ( prop.isValid( ) && isvc.isValid( ) )   {
    00152         const Properties& p = s->properties();
    00153         for(Properties::const_iterator i=p.begin(); i!=p.end(); i++)   {
    00154           prop->setProperty((*i)).ignore();
    00155         }
    00156         int output_level = this->outputLevel();
    00157         prop->setProperty(IntegerProperty("OutputLevel",output_level)).ignore();
    00158         // FIXME: (MCl) Why do we have to initialize the selector here?
    00159         return isvc->sysInitialize();
    00160       }
    00161     }
    00162   }
    00163   return StatusCode::FAILURE;
    00164 }
    00165
    00166 // Create (sub-) Event selector service
    00167 StatusCode DataStreamTool::createSelector(const std::string& nam, const std::string& typ, IEvtSelector*& sel) {
    00168   MsgStream log(msgSvc(), name());
    00169   IService* isvc = ROOT::Reflex::PluginService::Create<IService*>(typ, nam, serviceLocator());
    00170   if ( isvc ) {
    00171     StatusCode status = isvc->queryInterface(IID_IEvtSelector, (void**)&sel);
    00172     if ( status.isSuccess() ) {
    00173       return status;
    00174     }
    00175     sel = 0;
    00176     isvc->release();
    00177   }
    00178   return StatusCode::FAILURE;
    00179 }
    00180
    00181
    00182 StatusCode DataStreamTool::finalizeStream(EventSelectorDataStream* s)   {
    00183   if ( s )    {
    00184     IEvtSelector* sel = const_cast<IEvtSelector*>(s->selector());
    00185     if ( sel )    {
    00186       SmartIF<IService>  isvc(IID_IService, sel);
    00187       if ( isvc.isValid() )   {
    00188         isvc->finalize().ignore();
    00189         s->finalize().ignore();
    00190         // Fire EndStream "Incident"
    00191         m_incidentSvc->fireIncident(Incident(name(),IncidentType::EndStream));
    00192         return StatusCode::SUCCESS;
    00193       }
    00194       // Failed to get service interface of sub-event selector
    00195       return StatusCode::FAILURE;
    00196     }
    00197     // No selector (yet) attached - no need to finalize it!
    00198     return StatusCode::SUCCESS;
    00199   }
    00200   return StatusCode::FAILURE;
    00201 }
    00202
    00203
    00204 StatusCode DataStreamTool::eraseStream ( const std::string& info )   {
    00205
    00206   Streams::iterator i = getStreamIterator(info);
    00207
    00208   if ( m_streams.end() != i )   {
    00209     (*i)->release();
    00210     m_streams.erase(i);
    00211     return StatusCode::SUCCESS;
    00212   }
    00213
    00214   return StatusCode::FAILURE;
    00215 }
    00216
    00217 StatusCode DataStreamTool::createStream(const std::string& nam, const std::string& info,
    00218                                         EventSelectorDataStream*& stream)  {
    00219   stream = new EventSelectorDataStream(nam, info, serviceLocator());
    00220
    00221   return StatusCode::SUCCESS;
    00222 }
    00223
    00224
    00225 EventSelectorDataStream * DataStreamTool::getStream( const std::string& info ) {
    00226   Streams::iterator i = getStreamIterator(info);
    00227   if ( m_streams.end() == i ) return NULL;
    00228   return *i;
    00229 }
    00230
    00231 DataStreamTool::Streams::iterator DataStreamTool::getStreamIterator ( const std::string& info ) {
    00232   for ( Streams::iterator i = m_streams.begin(); i != m_streams.end(); i++ )    {
    00233     if ( (*i)->definition() == info )    {
    00234       return i;
    00235     }
    00236   }
    00237   return m_streams.end();
    00238 }
    00239
    00240 EventSelectorDataStream * DataStreamTool::getStream( size_type pos ) {
    00241   if ( (pos >= 0) && ((size_t)pos < m_streams.size()) ) // pos has to point inside the vector
    00242     return m_streams[pos];
    00243   else
    00244     return 0;
    00245 }
    00246
    00247 EventSelectorDataStream * DataStreamTool::lastStream()
    00248 {
    00249   if (m_streams.size() > 1 )
    00250     return *(--m_streams.end());
    00251   else return *m_streams.begin();
    00252
    00253 }
    00254
    00255
    00256
    00257 StatusCode DataStreamTool::clear()
    00258 {
    00259
    00260   StatusCode iret, status = StatusCode::SUCCESS;
    00261   iret.ignore();
    00262
    00263   MsgStream log(msgSvc(), name());
    00264
    00265   // disconnect the streams
    00266   for ( StreamSpecs::const_iterator il = m_streamSpecs.begin(); il != m_streamSpecs.end(); il++ ) {
    00267     EventSelectorDataStream* s = getStream(*il);
    00268     if ( NULL != s )   {
    00269       if ( s->isInitialized() )    {
    00270         iret = finalizeStream(s);
    00271         if ( !iret.isSuccess() )  {
    00272           log << MSG::ERROR << "Error finalizing Stream" << *il << endreq;
    00273           status = iret;
    00274         }
    00275       }
    00276       iret = eraseStream( *il );
    00277       if ( !iret.isSuccess() )    {
    00278         log << MSG::ERROR << "Error diconnecting Stream" << *il << endreq;
    00279         status = iret;
    00280       }
    00281     }
    00282   }
    00283
    00284   m_streamSpecs.clear();
    00285
    00286   return status;
    00287 }
    00288
    00289
    00290 StatusCode DataStreamTool::connectStream( EventSelectorDataStream *s)
    00291 {
    00292
    00293   if ( 0 != s )   {
    00294     s->addRef();
    00295     m_streams.push_back(s);
    00296     return StatusCode::SUCCESS;
    00297   }
    00298
    00299   return StatusCode::FAILURE;
    00300
    00301 }
    00302
    00303 StatusCode DataStreamTool::connectStream( const std::string & info )
    00304 {
    00305
    00306   if ( NULL != getStream(info) )   {
    00307     MsgStream log(msgSvc(), name());
    00308     log << MSG::WARNING << "Input stream " << info << "already in use" << endmsg;
    00309   }
    00310   char txt[32];
    00311   std::string nam = name() + "_" + ::_itoa(++m_streamCount, txt, 10);
    00312   EventSelectorDataStream* s = 0;
    00313   StatusCode status = createStream(nam, info, s);
    00314   if ( status.isSuccess() )   {
    00315     return connectStream(s);
    00316   }
    00317   s->release();
    00318   return status;
    00319
    00320
    00321 }
    00322
    00323 /*
    00324
    00325   Taking control over Streams and return them to EventSelector
    00326
    00327 */
    00328
    00329
    00330 StatusCode DataStreamTool::getNextStream( const EventSelectorDataStream * & esds, size_type & dsid )
    00331 {
    00332
    00333   EventSelectorDataStream * nextStream = getStream(dsid);
    00334   if ( NULL == nextStream ) return StatusCode::FAILURE; //<-end of streams reached
    00335
    00336   esds = nextStream;
    00337   ++m_streamID;
    00338
    00339   return StatusCode::SUCCESS;
    00340
    00341 }
    00342
    00343 StatusCode DataStreamTool::getPreviousStream( const EventSelectorDataStream * & esds, size_type & dsid )
    00344 {
    00345
    00346   EventSelectorDataStream * previousStream = getStream(dsid);
    00347   if (  NULL == previousStream ) return StatusCode::FAILURE; //<-begin of streams reached
    00348
    00349   esds = previousStream;
    00350   --m_streamID;
    00351
    00352   return StatusCode::SUCCESS;
    00353
    00354 }
    00355
    00356
    00357