| # Gaudi Framework, version v22r4 |

Home |  | Generated: Fri Sep 2 2011

# 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   MsgStream log(msgSvc(), name());
    00160   IService* isvc = ROOT::Reflex::PluginService::Create<IService*>(typ, nam, serviceLocator());
    00161   if ( isvc ) {
    00162     StatusCode status = isvc->queryInterface(IEvtSelector::interfaceID(), (void**)&sel);
    00163     if ( status.isSuccess() ) {
    00164       return status;
    00165     }
    00166     sel = 0;
    00167     isvc->release();
    00168   }
    00169   return StatusCode::FAILURE;
    00170 }
    00171
    00172
    00173 StatusCode DataStreamTool::finalizeStream(EventSelectorDataStream* s)   {
    00174   if ( s )    {
    00175     IEvtSelector* sel = const_cast<IEvtSelector*>(s->selector());
    00176     if ( sel )    {
    00177       SmartIF<IService> isvc(sel);
    00178       if ( isvc.isValid() )   {
    00179         isvc->finalize().ignore();
    00180         s->finalize().ignore();
    00181         // Fire EndStream "Incident"
    00182         m_incidentSvc->fireIncident(Incident(name(),IncidentType::EndStream));
    00183         return StatusCode::SUCCESS;
    00184       }
    00185       // Failed to get service interface of sub-event selector
    00186       return StatusCode::FAILURE;
    00187     }
    00188     // No selector (yet) attached - no need to finalize it!
    00189     return StatusCode::SUCCESS;
    00190   }
    00191   return StatusCode::FAILURE;
    00192 }
    00193
    00194
    00195 StatusCode DataStreamTool::eraseStream ( const std::string& info )   {
    00196
    00197   Streams::iterator i = getStreamIterator(info);
    00198
    00199   if ( m_streams.end() != i )   {
    00200     (*i)->release();
    00201     m_streams.erase(i);
    00202     return StatusCode::SUCCESS;
    00203   }
    00204
    00205   return StatusCode::FAILURE;
    00206 }
    00207
    00208 StatusCode DataStreamTool::createStream(const std::string& nam, const std::string& info,
    00209                                         EventSelectorDataStream*& stream)  {
    00210   stream = new EventSelectorDataStream(nam, info, serviceLocator());
    00211
    00212   return StatusCode::SUCCESS;
    00213 }
    00214
    00215
    00216 EventSelectorDataStream * DataStreamTool::getStream( const std::string& info ) {
    00217   Streams::iterator i = getStreamIterator(info);
    00218   if ( m_streams.end() == i ) return NULL;
    00219   return *i;
    00220 }
    00221
    00222 DataStreamTool::Streams::iterator DataStreamTool::getStreamIterator ( const std::string& info ) {
    00223   for ( Streams::iterator i = m_streams.begin(); i != m_streams.end(); i++ )    {
    00224     if ( (*i)->definition() == info )    {
    00225       return i;
    00226     }
    00227   }
    00228   return m_streams.end();
    00229 }
    00230
    00231 EventSelectorDataStream * DataStreamTool::getStream( size_type pos ) {
    00232   if ( (pos >= 0) && ((size_t)pos < m_streams.size()) ) // pos has to point inside the vector
    00233     return m_streams[pos];
    00234   else
    00235     return 0;
    00236 }
    00237
    00238 EventSelectorDataStream * DataStreamTool::lastStream()
    00239 {
    00240   if (m_streams.size() > 1 )
    00241     return *(--m_streams.end());
    00242   else return *m_streams.begin();
    00243
    00244 }
    00245
    00246
    00247
    00248 StatusCode DataStreamTool::clear()
    00249 {
    00250
    00251   StatusCode iret, status = StatusCode::SUCCESS;
    00252   iret.ignore();
    00253
    00254   MsgStream log(msgSvc(), name());
    00255
    00256   // disconnect the streams
    00257   for ( StreamSpecs::const_iterator il = m_streamSpecs.begin(); il != m_streamSpecs.end(); il++ ) {
    00258     EventSelectorDataStream* s = getStream(*il);
    00259     if ( NULL != s )   {
    00260       if ( s->isInitialized() )    {
    00261         iret = finalizeStream(s);
    00262         if ( !iret.isSuccess() )  {
    00263           log << MSG::ERROR << "Error finalizing Stream" << *il << endmsg;
    00264           status = iret;
    00265         }
    00266       }
    00267       iret = eraseStream( *il );
    00268       if ( !iret.isSuccess() )    {
    00269         log << MSG::ERROR << "Error diconnecting Stream" << *il << endmsg;
    00270         status = iret;
    00271       }
    00272     }
    00273   }
    00274
    00275   m_streamSpecs.clear();
    00276
    00277   return status;
    00278 }
    00279
    00280
    00281 StatusCode DataStreamTool::connectStream( EventSelectorDataStream *s)
    00282 {
    00283
    00284   if ( 0 != s )   {
    00285     s->addRef();
    00286     m_streams.push_back(s);
    00287     return StatusCode::SUCCESS;
    00288   }
    00289
    00290   return StatusCode::FAILURE;
    00291
    00292 }
    00293
    00294 StatusCode DataStreamTool::connectStream( const std::string & info )
    00295 {
    00296
    00297   if ( NULL != getStream(info) )   {
    00298     MsgStream log(msgSvc(), name());
    00299     log << MSG::WARNING << "Input stream " << info << "already in use" << endmsg;
    00300   }
    00301   std::ostringstream nam;
    00302   nam << name() << '_' << ++m_streamCount;
    00303   EventSelectorDataStream* s = 0;
    00304   StatusCode status = createStream(nam.str(), info, s);
    00305   if ( status.isSuccess() )   {
    00306     return connectStream(s);
    00307   }
    00308   s->release();
    00309   return status;
    00310
    00311
    00312 }
    00313
    00314 /*
    00315
    00316   Taking control over Streams and return them to EventSelector
    00317
    00318 */
    00319
    00320
    00321 StatusCode DataStreamTool::getNextStream( const EventSelectorDataStream * & esds, size_type & dsid )
    00322 {
    00323
    00324   EventSelectorDataStream * nextStream = getStream(dsid);
    00325   if ( NULL == nextStream ) return StatusCode::FAILURE; //<-end of streams reached
    00326
    00327   esds = nextStream;
    00328   ++m_streamID;
    00329
    00330   return StatusCode::SUCCESS;
    00331
    00332 }
    00333
    00334 StatusCode DataStreamTool::getPreviousStream( const EventSelectorDataStream * & esds, size_type & dsid )
    00335 {
    00336
    00337   EventSelectorDataStream * previousStream = getStream(dsid);
    00338   if (  NULL == previousStream ) return StatusCode::FAILURE; //<-begin of streams reached
    00339
    00340   esds = previousStream;
    00341   --m_streamID;
    00342
    00343   return StatusCode::SUCCESS;
    00344
    00345 }
    00346
    00347
    00348

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