The Gaudi Framework  v29r0 (ff2e7097)
DataStreamTool.cpp
Go to the documentation of this file.
1 // Include files
2 
3 // from Gaudi
9 #include "GaudiKernel/Incident.h"
10 #include "GaudiKernel/MsgStream.h"
12 #include "GaudiKernel/Service.h"
13 #include "GaudiKernel/SmartIF.h"
14 
15 //-----------------------------------------------------------------------------
16 // Implementation file for class : DataStreamTool
17 //
18 // 2006-09-21 : Andres Felipe Osorio Oliveros
19 //-----------------------------------------------------------------------------
20 
21 //=============================================================================
22 // Standard constructor, initializes variables
23 //=============================================================================
25  : base_class( type, name, parent )
26 {
27  // declareInterface<IDataStreamTool>(this);
28 }
29 //=============================================================================
31 {
32 
34  if ( !status.isSuccess() ) {
35  fatal() << "Error. Cannot initialize base class." << endmsg;
36  return status;
37  }
38 
39  // Get the references to the services that are needed by the ApplicationMgr itself
40  m_incidentSvc = serviceLocator()->service( "IncidentSvc" );
41  if ( !m_incidentSvc ) {
42  fatal() << "Error retrieving IncidentSvc." << endmsg;
43  return StatusCode::FAILURE;
44  }
45 
46  return StatusCode::SUCCESS;
47 }
48 
50 {
51 
52  if ( getStream( input ) ) {
53  warning() << "Input stream " << input << "already in use" << endmsg;
54  }
55 
56  m_streamSpecs.push_back( input );
57 
58  auto strname = name() + '_' + std::to_string( ++m_streamCount );
59  EventSelectorDataStream* s = nullptr;
60 
61  StatusCode status = createStream( strname, input, s );
62 
63  if ( status.isSuccess() && s ) {
64  s->addRef();
65  m_streams.push_back( s );
66  status = StatusCode::SUCCESS;
67  } else {
68  if ( s ) {
69  s->release();
70  error() << "Error connecting/creating Stream: " << s << endmsg;
71  }
72  error() << "Error connecting/creating Stream: " << input << endmsg;
73  status = StatusCode::FAILURE;
74  }
75  return status;
76 }
77 
79 {
80 
82  for ( auto& i : inputs ) {
83  status = addStream( i );
84  if ( !status.isSuccess() ) break;
85  }
86  return status;
87 }
88 
90 {
91  clear().ignore();
93  return AlgTool::finalize();
94 }
95 
97 {
98  IEvtSelector* sel = nullptr;
99  StatusCode status = s->initialize();
100  if ( status.isSuccess() ) {
101  status = createSelector( s->name(), s->selectorType(), sel );
102  if ( status.isSuccess() ) {
103  SmartIF<IProperty> prop( sel ); // Att: IProperty, IService used to point to EventSelector
104  SmartIF<IService> isvc( sel );
105  s->setSelector( sel );
106  sel->release(); // No need for this interface anymore, it is passed to the stream
107  if ( prop && isvc ) {
108  for ( const auto& i : s->properties() ) prop->setProperty( i ).ignore();
109  prop->setProperty( Gaudi::Property<int>( "OutputLevel", msgLevel() ) ).ignore();
110  // FIXME: (MCl) Why do we have to initialize the selector here?
111  return isvc->sysInitialize();
112  }
113  }
114  }
115  return StatusCode::FAILURE;
116 }
117 
118 // Create (sub-) Event selector service
120 {
121  auto isvc = make_SmartIF( Service::Factory::create( typ, nam, serviceLocator() ) );
122  if ( isvc ) {
123  auto isel = isvc.as<IEvtSelector>();
124  if ( isel ) {
125  sel = isel.get();
126  sel->addRef(); // make sure that sel is not left dangling once isel and isvc go out of scope...
127  return StatusCode::SUCCESS;
128  }
129  }
130  sel = nullptr;
131  error() << "Failed to create IEvtSelector " << typ << "/" << nam << endmsg;
132  return StatusCode::FAILURE;
133 }
134 
136 {
137  if ( s ) {
138  IEvtSelector* sel = s->selector();
139  if ( sel ) {
140  SmartIF<IService> isvc( sel );
141  if ( isvc ) {
142  isvc->finalize().ignore();
143  s->finalize().ignore();
144  // Fire EndStream "Incident"
145  m_incidentSvc->fireIncident( Incident( name(), IncidentType::EndStream ) );
146  return StatusCode::SUCCESS;
147  }
148  // Failed to get service interface of sub-event selector
149  return StatusCode::FAILURE;
150  }
151  // No selector (yet) attached - no need to finalize it!
152  return StatusCode::SUCCESS;
153  }
154  return StatusCode::FAILURE;
155 }
156 
158 {
159 
160  auto i = getStreamIterator( info );
161  if ( i != m_streams.end() ) {
162  ( *i )->release();
163  m_streams.erase( i );
164  return StatusCode::SUCCESS;
165  }
166  return StatusCode::FAILURE;
167 }
168 
170  EventSelectorDataStream*& stream )
171 {
172  stream = new EventSelectorDataStream( nam, info, serviceLocator() );
173  return StatusCode::SUCCESS;
174 }
175 
177 {
178  auto i = getStreamIterator( info );
179  return i != m_streams.end() ? *i : nullptr;
180 }
181 
182 DataStreamTool::Streams::iterator DataStreamTool::getStreamIterator( const std::string& info )
183 {
185  [&]( const EventSelectorDataStream* i ) { return i->definition() == info; } );
186 }
187 
189 {
190  // pos has to point inside the vector
191  return ( ( pos >= 0 ) && ( (size_t)pos < m_streams.size() ) ) ? m_streams[pos] : nullptr;
192 }
193 
195 
197 {
198 
199  StatusCode iret, status = StatusCode::SUCCESS;
200  iret.ignore();
201 
202  // disconnect the streams
203  for ( auto& il : m_streamSpecs ) {
205  if ( s ) {
206  if ( s->isInitialized() ) {
207  iret = finalizeStream( s );
208  if ( !iret.isSuccess() ) {
209  error() << "Error finalizing Stream" << il << endmsg;
210  status = iret;
211  }
212  }
213  iret = eraseStream( il );
214  if ( !iret.isSuccess() ) {
215  error() << "Error diconnecting Stream" << il << endmsg;
216  status = iret;
217  }
218  }
219  }
220 
221  m_streamSpecs.clear();
222 
223  return status;
224 }
225 
227 {
228 
229  if ( !s ) return StatusCode::FAILURE;
230  s->addRef();
231  m_streams.push_back( s );
232  return StatusCode::SUCCESS;
233 }
234 
236 {
237  if ( getStream( info ) ) {
238  warning() << "Input stream " << info << "already in use" << endmsg;
239  }
240  auto nam = name() + '_' + std::to_string( ++m_streamCount );
241  EventSelectorDataStream* s = nullptr;
242  StatusCode status = createStream( nam, info, s );
243  if ( status.isSuccess() ) return connectStream( s );
244  s->release();
245  return status;
246 }
247 
248 /*
249 
250  Taking control over Streams and return them to EventSelector
251 
252 */
253 
255 {
256 
257  EventSelectorDataStream* nextStream = getStream( dsid );
258  if ( !nextStream ) return StatusCode::FAILURE; //<-end of streams reached
259 
260  esds = nextStream;
261  ++m_streamID;
262 
263  return StatusCode::SUCCESS;
264 }
265 
267 {
268 
269  EventSelectorDataStream* previousStream = getStream( dsid );
270  if ( !previousStream ) return StatusCode::FAILURE; //<-begin of streams reached
271 
272  esds = previousStream;
273  --m_streamID;
274 
275  return StatusCode::SUCCESS;
276 }
bool isInitialized() const
Check initialization status.
Streams::iterator getStreamIterator(const std::string &)
const std::string & name() const
Retrieve stream name.
StatusCode getNextStream(const EventSelectorDataStream *&, size_type &) override
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: AlgTool.cpp:84
Implementation of property with value of concrete type.
Definition: Property.h:319
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
StatusCode initialize() override
Definition: AlgTool.cpp:241
StatusCode clear() override
The Event Selector Interface.
Definition: IEvtSelector.h:18
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
T to_string(T...args)
IEvtSelector * selector() const
Retrieve event selector object.
sel
Definition: IOTest.py:95
StatusCode createStream(const std::string &, const std::string &, EventSelectorDataStream *&) override
T end(T...args)
StatusCode initializeStream(EventSelectorDataStream *) override
Initialize newly opened stream.
STL class.
StatusCode finalize() override
StatusCode finalize() override
Definition: AlgTool.cpp:311
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:79
T push_back(T...args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
virtual StatusCode initialize()
Parse input criteria.
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
EventSelectorDataStream * lastStream() override
unsigned long addRef() override
Reference Interface instance.
Definition: implements.h:41
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
Definition of the basic interface.
Definition: IInterface.h:277
virtual StatusCode finalize()
Finalize stream and release resources.
StatusCode finalizeStream(EventSelectorDataStream *) override
Finalize no longer needed stream.
T erase(T...args)
StatusCode createSelector(const std::string &, const std::string &, IEvtSelector *&) override
SmartIF< IIncidentSvc > m_incidentSvc
Reference to the incident service.
StatusCode connectStream(EventSelectorDataStream *)
Connect single stream by reference.
DataStreamTool(const std::string &type, const std::string &name, const IInterface *parent)
Standard constructor.
EventSelectorDataStream * getStream(const std::string &) override
Retrieve stream by name.
StatusCode addStream(const std::string &) override
StatusCode eraseStream(const std::string &) override
const std::string & definition() const
Retrieve definition string.
T find_if(T...args)
T size(T...args)
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:63
virtual unsigned long release()=0
Release Interface instance.
StatusCode initialize() override
size_type m_streamID
T begin(T...args)
Definition of class EventSelectorDataStream.
StatusCode getPreviousStream(const EventSelectorDataStream *&, size_type &) override
const std::string & selectorType() const
Retrieve event selector type.
Base class for all Incidents (computing events).
Definition: Incident.h:17
size_type m_streamCount
T back(T...args)
string s
Definition: gaudirun.py:253
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
StatusCode addStreams(const StreamSpecs &) override
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:152
const Properties & properties()
Access properties.
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
void ignore() const
Definition: StatusCode.h:109
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
virtual void setSelector(IEvtSelector *pSelector)
Attach event selector object.
StreamSpecs m_streamSpecs
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
unsigned long release() override
Release Interface instance.
Definition: implements.h:43