DataStreamTool.cpp
Go to the documentation of this file.
1 // Include files
2 
3 // from Gaudi
4 #include "GaudiKernel/MsgStream.h"
5 #include "GaudiKernel/xtoa.h"
6 #include "GaudiKernel/SmartIF.h"
7 #include "GaudiKernel/Incident.h"
8 #include "GaudiKernel/MsgStream.h"
9 #include "GaudiKernel/IIncidentSvc.h"
10 #include "GaudiKernel/ISvcLocator.h"
11 #include "GaudiKernel/ISvcManager.h"
12 #include "GaudiKernel/IAddressCreator.h"
13 #include "GaudiKernel/PropertyMgr.h"
14 #include "GaudiKernel/EventSelectorDataStream.h"
15 #include "GaudiKernel/DataStreamTool.h"
16 #include "GaudiKernel/ToolFactory.h"
17 #include "GaudiKernel/Service.h"
18 
19 #include <sstream>
20 
21 //-----------------------------------------------------------------------------
22 // Implementation file for class : DataStreamTool
23 //
24 // 2006-09-21 : Andres Felipe Osorio Oliveros
25 //-----------------------------------------------------------------------------
26 
27 //=============================================================================
28 // Standard constructor, initializes variables
29 //=============================================================================
31  const std::string& name,
32  const IInterface* parent )
33  : base_class ( type, name , parent )
34 {
35  //declareInterface<IDataStreamTool>(this);
36 
37  m_incidentSvc = 0;
38  m_streamCount = 0;
39  m_streamID = 0;
40 
41 }
42 //=============================================================================
43 // Destructor
44 //=============================================================================
46 }
47 
48 //=============================================================================
50 
52 
54  if( !status.isSuccess() ) {
55  logger << MSG::FATAL << "Error. Cannot initialize base class." << endmsg;
56  return status;
57  }
58 
59  // Get the references to the services that are needed by the ApplicationMgr itself
60  m_incidentSvc = serviceLocator()->service("IncidentSvc");
61  if( !m_incidentSvc.isValid() ) {
62  logger << MSG::FATAL << "Error retrieving IncidentSvc." << endmsg;
63  return StatusCode::FAILURE;
64  }
65 
66  return StatusCode::SUCCESS;
67 
68 }
69 
70 StatusCode DataStreamTool::addStream(const std::string & input) {
71 
72  if ( NULL != getStream(input) ) {
73  MsgStream log(msgSvc(), name());
74  log << MSG::WARNING << "Input stream " << input << "already in use" << endmsg;
75  }
76 
77  m_streamSpecs.push_back(input);
78 
79  std::ostringstream strname;
80  strname << name() << '_' << ++m_streamCount;
81 
83 
84  StatusCode status = createStream(strname.str(), input , s );
85 
86  if( status.isSuccess() && 0 != s ) {
87  s->addRef();
88  m_streams.push_back(s);
89  status = StatusCode::SUCCESS;
90  }
91  else {
92  MsgStream log(msgSvc(), name());
93  if (s) {
94  s->release();
95  log << MSG::ERROR << "Error connecting/creating Stream: " << s << endmsg;
96  }
97  log << MSG::ERROR << "Error connecting/creating Stream: " << input << endmsg;
98  status = StatusCode::FAILURE;
99  }
100 
101  return status;
102 
103 }
104 
106 
108 
109  for ( StreamSpecs::const_iterator itr = inputs.begin(); itr != inputs.end() && status.isSuccess(); ++itr ) {
110 
111  status = addStream(*itr);
112 
113  }
114 
115  return status;
116 
117 }
118 
120  clear().ignore();
121  m_incidentSvc = 0; // release
122 
123  return AlgTool::finalize();
124 }
125 
127  IEvtSelector* sel = 0;
128  StatusCode status = s->initialize();
129  if ( status.isSuccess() ) {
130  status = createSelector(s->name(), s->selectorType(), sel);
131  if ( status.isSuccess() ) {
132  SmartIF<IProperty> prop(sel); //Att: IProperty, IService used to point to EventSelector
133  SmartIF<IService> isvc(sel);
134  s->setSelector(sel);
135  sel->release(); // No need of this interface anymore, it is passed to the stream
136  if ( prop.isValid( ) && isvc.isValid( ) ) {
137  const Properties& p = s->properties();
138  for(Properties::const_iterator i=p.begin(); i!=p.end(); i++) {
139  prop->setProperty((*i)).ignore();
140  }
141  int output_level = this->outputLevel();
142  prop->setProperty(IntegerProperty("OutputLevel",output_level)).ignore();
143  // FIXME: (MCl) Why do we have to initialize the selector here?
144  return isvc->sysInitialize();
145  }
146  }
147  }
148  return StatusCode::FAILURE;
149 }
150 
151 // Create (sub-) Event selector service
152 StatusCode DataStreamTool::createSelector(const std::string& nam, const std::string& typ, IEvtSelector*& sel) {
153  IService* isvc = Service::Factory::create(typ, nam, serviceLocator());
154  if ( isvc ) {
155  StatusCode status = isvc->queryInterface(IEvtSelector::interfaceID(), (void**)&sel);
156  if ( status.isSuccess() ) {
157  return status;
158  }
159  sel = 0;
160  isvc->release();
161  }
162  MsgStream log(msgSvc(), name());
163  log << MSG::ERROR << "Failed to create IEvtSelector " << typ << "/" << nam << endmsg;
164  return StatusCode::FAILURE;
165 }
166 
167 
169  if ( s ) {
170  IEvtSelector* sel = const_cast<IEvtSelector*>(s->selector());
171  if ( sel ) {
172  SmartIF<IService> isvc(sel);
173  if ( isvc.isValid() ) {
174  isvc->finalize().ignore();
175  s->finalize().ignore();
176  // Fire EndStream "Incident"
178  return StatusCode::SUCCESS;
179  }
180  // Failed to get service interface of sub-event selector
181  return StatusCode::FAILURE;
182  }
183  // No selector (yet) attached - no need to finalize it!
184  return StatusCode::SUCCESS;
185  }
186  return StatusCode::FAILURE;
187 }
188 
189 
190 StatusCode DataStreamTool::eraseStream ( const std::string& info ) {
191 
192  Streams::iterator i = getStreamIterator(info);
193 
194  if ( m_streams.end() != i ) {
195  (*i)->release();
196  m_streams.erase(i);
197  return StatusCode::SUCCESS;
198  }
199 
200  return StatusCode::FAILURE;
201 }
202 
203 StatusCode DataStreamTool::createStream(const std::string& nam, const std::string& info,
204  EventSelectorDataStream*& stream) {
205  stream = new EventSelectorDataStream(nam, info, serviceLocator());
206 
207  return StatusCode::SUCCESS;
208 }
209 
210 
212  Streams::iterator i = getStreamIterator(info);
213  if ( m_streams.end() == i ) return NULL;
214  return *i;
215 }
216 
217 DataStreamTool::Streams::iterator DataStreamTool::getStreamIterator ( const std::string& info ) {
218  for ( Streams::iterator i = m_streams.begin(); i != m_streams.end(); i++ ) {
219  if ( (*i)->definition() == info ) {
220  return i;
221  }
222  }
223  return m_streams.end();
224 }
225 
227  if ( (pos >= 0) && ((size_t)pos < m_streams.size()) ) // pos has to point inside the vector
228  return m_streams[pos];
229  else
230  return 0;
231 }
232 
234 {
235  if (m_streams.size() > 1 )
236  return *(--m_streams.end());
237  else return *m_streams.begin();
238 
239 }
240 
241 
242 
244 {
245 
246  StatusCode iret, status = StatusCode::SUCCESS;
247  iret.ignore();
248 
249  MsgStream log(msgSvc(), name());
250 
251  // disconnect the streams
252  for ( StreamSpecs::const_iterator il = m_streamSpecs.begin(); il != m_streamSpecs.end(); il++ ) {
254  if ( NULL != s ) {
255  if ( s->isInitialized() ) {
256  iret = finalizeStream(s);
257  if ( !iret.isSuccess() ) {
258  log << MSG::ERROR << "Error finalizing Stream" << *il << endmsg;
259  status = iret;
260  }
261  }
262  iret = eraseStream( *il );
263  if ( !iret.isSuccess() ) {
264  log << MSG::ERROR << "Error diconnecting Stream" << *il << endmsg;
265  status = iret;
266  }
267  }
268  }
269 
270  m_streamSpecs.clear();
271 
272  return status;
273 }
274 
275 
277 {
278 
279  if ( 0 != s ) {
280  s->addRef();
281  m_streams.push_back(s);
282  return StatusCode::SUCCESS;
283  }
284 
285  return StatusCode::FAILURE;
286 
287 }
288 
289 StatusCode DataStreamTool::connectStream( const std::string & info )
290 {
291 
292  if ( NULL != getStream(info) ) {
293  MsgStream log(msgSvc(), name());
294  log << MSG::WARNING << "Input stream " << info << "already in use" << endmsg;
295  }
296  std::ostringstream nam;
297  nam << name() << '_' << ++m_streamCount;
299  StatusCode status = createStream(nam.str(), info, s);
300  if ( status.isSuccess() ) {
301  return connectStream(s);
302  }
303  s->release();
304  return status;
305 
306 
307 }
308 
309 /*
310 
311  Taking control over Streams and return them to EventSelector
312 
313 */
314 
315 
317 {
318 
319  EventSelectorDataStream * nextStream = getStream(dsid);
320  if ( NULL == nextStream ) return StatusCode::FAILURE; //<-end of streams reached
321 
322  esds = nextStream;
323  ++m_streamID;
324 
325  return StatusCode::SUCCESS;
326 
327 }
328 
330 {
331 
332  EventSelectorDataStream * previousStream = getStream(dsid);
333  if ( NULL == previousStream ) return StatusCode::FAILURE; //<-begin of streams reached
334 
335  esds = previousStream;
336  --m_streamID;
337 
338  return StatusCode::SUCCESS;
339 
340 }
341 
342 
343 
bool isInitialized() const
Check initialization status.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Streams::iterator getStreamIterator(const std::string &)
const std::string & name() const
Retrieve stream name.
virtual StatusCode createStream(const std::string &, const std::string &, EventSelectorDataStream *&)
std::vector< std::string > StreamSpecs
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: AlgTool.cpp:438
virtual EventSelectorDataStream * getStream(const std::string &)
Retrieve stream by name.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
The Event Selector Interface.
Definition: IEvtSelector.h:19
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
IEvtSelector * selector() const
Retrieve event selector object.
virtual StatusCode initializeStream(EventSelectorDataStream *)
Initialize newly opened stream.
virtual StatusCode eraseStream(const std::string &)
GAUDIPS_API Logger & logger()
Return the current logger instance.
virtual StatusCode getNextStream(const EventSelectorDataStream *&, size_type &)
std::vector< StringProperty > Properties
ISvcLocator * serviceLocator() const
Retrieve pointer to service locator.
Definition: AlgTool.cpp:72
virtual StatusCode createSelector(const std::string &, const std::string &, IEvtSelector *&)
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
virtual StatusCode sysInitialize()=0
Initialize Service.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:82
virtual StatusCode initialize()
Parse input criteria.
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
IMessageSvc * msgSvc() const
Retrieve pointer to message service.
Definition: AlgTool.cpp:79
virtual StatusCode getPreviousStream(const EventSelectorDataStream *&, size_type &)
General service interface definition.
Definition: IService.h:19
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Definition of the basic interface.
Definition: IInterface.h:160
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
virtual StatusCode setProperty(const Property &p)=0
Set the property by property.
virtual StatusCode finalize()
Finalize stream and release resources.
SimpleProperty< int > IntegerProperty
Definition: Property.h:733
StatusCode connectStream(EventSelectorDataStream *)
Connect single stream by reference.
virtual StatusCode finalize()=0
Finalize (from INITIALIZED to CONFIGURED).
DataStreamTool(const std::string &type, const std::string &name, const IInterface *parent)
Standard constructor.
virtual StatusCode addStreams(const StreamSpecs &)
SmartIF< IIncidentSvc > m_incidentSvc
Reference to the incident service.
virtual unsigned long release()=0
Release Interface instance.
virtual EventSelectorDataStream * lastStream()
size_type m_streamID
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
Definition of class EventSelectorDataStream.
const std::string & selectorType() const
Retrieve event selector type.
Base class for all Incidents (computing events).
Definition: Incident.h:16
int outputLevel() const
get tool's output level
Definition: AlgTool.h:306
size_type m_streamCount
string s
Definition: gaudirun.py:244
virtual StatusCode finalizeStream(EventSelectorDataStream *)
Finalize no longer needed stream.
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
const Properties & properties()
Access properties.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: AlgTool.cpp:311
void ignore() const
Definition: StatusCode.h:107
virtual ~DataStreamTool()
Destructor.
virtual const std::string & name() const
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:51
list i
Definition: ana.py:128
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:171
virtual StatusCode clear()
const std::string EndStream
Processing of the stream has finished.
Definition: Incident.h:64
virtual void setSelector(IEvtSelector *pSelector)
Attach event selector object.
StreamSpecs m_streamSpecs
string type
Definition: gaudirun.py:151
virtual StatusCode addStream(const std::string &)
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.