| # Gaudi Framework, version v21r10p1 |

Home |  | Generated: 29 Jul 2010

# 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   : base_class ( 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." << endmsg;
    00060     return status;
    00061   }
    00062
    00063   // Get the references to the services that are needed by the ApplicationMgr itself
    00064   m_incidentSvc = serviceLocator()->service("IncidentSvc");
    00065   if( !m_incidentSvc.isValid() )  {
    00066     logger << MSG::FATAL << "Error retrieving IncidentSvc." << endmsg;
    00067     return StatusCode::FAILURE;
    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 << endmsg;
    00101     }
    00102     log << MSG::ERROR << "Error connecting/creating Stream: " << input << endmsg;
    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   clear().ignore();
    00126   m_incidentSvc = 0; // release
    00127
    00128   return AlgTool::finalize();
    00129 }
    00130
    00131 StatusCode DataStreamTool::initializeStream(EventSelectorDataStream* s)   {
    00132   IEvtSelector* sel = 0;
    00133   StatusCode status = s->initialize();
    00134   if ( status.isSuccess() )   {
    00135     status = createSelector(s->name(), s->selectorType(), sel);
    00136     if ( status.isSuccess() )   {
    00137       SmartIF<IProperty> prop(sel); //Att: IProperty, IService used to point to EventSelector
    00138       SmartIF<IService>  isvc(sel);
    00139       s->setSelector(sel);
    00140       sel->release();  // No need of this interface anymore, it is passed to the stream
    00141       if ( prop.isValid( ) && isvc.isValid( ) )   {
    00142         const Properties& p = s->properties();
    00143         for(Properties::const_iterator i=p.begin(); i!=p.end(); i++)   {
    00144           prop->setProperty((*i)).ignore();
    00145         }
    00146         int output_level = this->outputLevel();
    00147         prop->setProperty(IntegerProperty("OutputLevel",output_level)).ignore();
    00148         // FIXME: (MCl) Why do we have to initialize the selector here?
    00149         return isvc->sysInitialize();
    00150       }
    00151     }
    00152   }
    00153   return StatusCode::FAILURE;
    00154 }
    00155
    00156 // Create (sub-) Event selector service
    00157 StatusCode DataStreamTool::createSelector(const std::string& nam, const std::string& typ, IEvtSelector*& sel) {
    00158   MsgStream log(msgSvc(), name());
    00159   IService* isvc = ROOT::Reflex::PluginService::Create<IService*>(typ, nam, serviceLocator());
    00160   if ( isvc ) {
    00161     StatusCode status = isvc->queryInterface(IEvtSelector::interfaceID(), (void**)&sel);
    00162     if ( status.isSuccess() ) {
    00163       return status;
    00164     }
    00165     sel = 0;
    00166     isvc->release();
    00167   }
    00168   return StatusCode::FAILURE;
    00169 }
    00170
    00171
    00172 StatusCode DataStreamTool::finalizeStream(EventSelectorDataStream* s)   {
    00173   if ( s )    {
    00174     IEvtSelector* sel = const_cast<IEvtSelector*>(s->selector());
    00175     if ( sel )    {
    00176       SmartIF<IService> isvc(sel);
    00177       if ( isvc.isValid() )   {
    00178         isvc->finalize().ignore();
    00179         s->finalize().ignore();
    00180         // Fire EndStream "Incident"
    00181         m_incidentSvc->fireIncident(Incident(name(),IncidentType::EndStream));
    00182         return StatusCode::SUCCESS;
    00183       }
    00184       // Failed to get service interface of sub-event selector
    00185       return StatusCode::FAILURE;
    00186     }
    00187     // No selector (yet) attached - no need to finalize it!
    00188     return StatusCode::SUCCESS;
    00189   }
    00190   return StatusCode::FAILURE;
    00191 }
    00192
    00193
    00194 StatusCode DataStreamTool::eraseStream ( const std::string& info )   {
    00195
    00196   Streams::iterator i = getStreamIterator(info);
    00197
    00198   if ( m_streams.end() != i )   {
    00199     (*i)->release();
    00200     m_streams.erase(i);
    00201     return StatusCode::SUCCESS;
    00202   }
    00203
    00204   return StatusCode::FAILURE;
    00205 }
    00206
    00207 StatusCode DataStreamTool::createStream(const std::string& nam, const std::string& info,
    00208                                         EventSelectorDataStream*& stream)  {
    00209   stream = new EventSelectorDataStream(nam, info, serviceLocator());
    00210
    00211   return StatusCode::SUCCESS;
    00212 }
    00213
    00214
    00215 EventSelectorDataStream * DataStreamTool::getStream( const std::string& info ) {
    00216   Streams::iterator i = getStreamIterator(info);
    00217   if ( m_streams.end() == i ) return NULL;
    00218   return *i;
    00219 }
    00220
    00221 DataStreamTool::Streams::iterator DataStreamTool::getStreamIterator ( const std::string& info ) {
    00222   for ( Streams::iterator i = m_streams.begin(); i != m_streams.end(); i++ )    {
    00223     if ( (*i)->definition() == info )    {
    00224       return i;
    00225     }
    00226   }
    00227   return m_streams.end();
    00228 }
    00229
    00230 EventSelectorDataStream * DataStreamTool::getStream( size_type pos ) {
    00231   if ( (pos >= 0) && ((size_t)pos < m_streams.size()) ) // pos has to point inside the vector
    00232     return m_streams[pos];
    00233   else
    00234     return 0;
    00235 }
    00236
    00237 EventSelectorDataStream * DataStreamTool::lastStream()
    00238 {
    00239   if (m_streams.size() > 1 )
    00240     return *(--m_streams.end());
    00241   else return *m_streams.begin();
    00242
    00243 }
    00244
    00245
    00246
    00247 StatusCode DataStreamTool::clear()
    00248 {
    00249
    00250   StatusCode iret, status = StatusCode::SUCCESS;
    00251   iret.ignore();
    00252
    00253   MsgStream log(msgSvc(), name());
    00254
    00255   // disconnect the streams
    00256   for ( StreamSpecs::const_iterator il = m_streamSpecs.begin(); il != m_streamSpecs.end(); il++ ) {
    00257     EventSelectorDataStream* s = getStream(*il);
    00258     if ( NULL != s )   {
    00259       if ( s->isInitialized() )    {
    00260         iret = finalizeStream(s);
    00261         if ( !iret.isSuccess() )  {
    00262           log << MSG::ERROR << "Error finalizing Stream" << *il << endmsg;
    00263           status = iret;
    00264         }
    00265       }
    00266       iret = eraseStream( *il );
    00267       if ( !iret.isSuccess() )    {
    00268         log << MSG::ERROR << "Error diconnecting Stream" << *il << endmsg;
    00269         status = iret;
    00270       }
    00271     }
    00272   }
    00273
    00274   m_streamSpecs.clear();
    00275
    00276   return status;
    00277 }
    00278
    00279
    00280 StatusCode DataStreamTool::connectStream( EventSelectorDataStream *s)
    00281 {
    00282
    00283   if ( 0 != s )   {
    00284     s->addRef();
    00285     m_streams.push_back(s);
    00286     return StatusCode::SUCCESS;
    00287   }
    00288
    00289   return StatusCode::FAILURE;
    00290
    00291 }
    00292
    00293 StatusCode DataStreamTool::connectStream( const std::string & info )
    00294 {
    00295
    00296   if ( NULL != getStream(info) )   {
    00297     MsgStream log(msgSvc(), name());
    00298     log << MSG::WARNING << "Input stream " << info << "already in use" << endmsg;
    00299   }
    00300   char txt[32];
    00301   std::string nam = name() + "_" + ::_itoa(++m_streamCount, txt, 10);
    00302   EventSelectorDataStream* s = 0;
    00303   StatusCode status = createStream(nam, info, s);
    00304   if ( status.isSuccess() )   {
    00305     return connectStream(s);
    00306   }
    00307   s->release();
    00308   return status;
    00309
    00310
    00311 }
    00312
    00313 /*
    00314
    00315   Taking control over Streams and return them to EventSelector
    00316
    00317 */
    00318
    00319
    00320 StatusCode DataStreamTool::getNextStream( const EventSelectorDataStream * & esds, size_type & dsid )
    00321 {
    00322
    00323   EventSelectorDataStream * nextStream = getStream(dsid);
    00324   if ( NULL == nextStream ) return StatusCode::FAILURE; //<-end of streams reached
    00325
    00326   esds = nextStream;
    00327   ++m_streamID;
    00328
    00329   return StatusCode::SUCCESS;
    00330
    00331 }
    00332
    00333 StatusCode DataStreamTool::getPreviousStream( const EventSelectorDataStream * & esds, size_type & dsid )
    00334 {
    00335
    00336   EventSelectorDataStream * previousStream = getStream(dsid);
    00337   if (  NULL == previousStream ) return StatusCode::FAILURE; //<-begin of streams reached
    00338
    00339   esds = previousStream;
    00340   --m_streamID;
    00341
    00342   return StatusCode::SUCCESS;
    00343
    00344 }
    00345
    00346
    00347