| # Gaudi Framework, version v23r1 |

Home |  | Generated: Wed Feb 29 2012

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines