All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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"
11 #include "GaudiKernel/MsgStream.h"
13 #include "GaudiKernel/Service.h"
14 #include "GaudiKernel/SmartIF.h"
15 
16 //-----------------------------------------------------------------------------
17 // Implementation file for class : DataStreamTool
18 //
19 // 2006-09-21 : Andres Felipe Osorio Oliveros
20 //-----------------------------------------------------------------------------
21 
22 //=============================================================================
23 // Standard constructor, initializes variables
24 //=============================================================================
26  : base_class( type, name, parent )
27 {
28  // declareInterface<IDataStreamTool>(this);
29 }
30 //=============================================================================
32 {
33 
35  if ( !status.isSuccess() ) {
36  fatal() << "Error. Cannot initialize base class." << endmsg;
37  return status;
38  }
39 
40  // Get the references to the services that are needed by the ApplicationMgr itself
41  m_incidentSvc = serviceLocator()->service( "IncidentSvc" );
42  if ( !m_incidentSvc ) {
43  fatal() << "Error retrieving IncidentSvc." << endmsg;
44  return StatusCode::FAILURE;
45  }
46 
47  return StatusCode::SUCCESS;
48 }
49 
51 {
52 
53  if ( getStream( input ) ) {
54  warning() << "Input stream " << input << "already in use" << endmsg;
55  }
56 
57  m_streamSpecs.push_back( input );
58 
59  auto strname = name() + '_' + std::to_string( ++m_streamCount );
60  EventSelectorDataStream* s = nullptr;
61 
62  StatusCode status = createStream( strname, input, s );
63 
64  if ( status.isSuccess() && s ) {
65  s->addRef();
66  m_streams.push_back( s );
67  status = StatusCode::SUCCESS;
68  } else {
69  if ( s ) {
70  s->release();
71  error() << "Error connecting/creating Stream: " << s << endmsg;
72  }
73  error() << "Error connecting/creating Stream: " << input << endmsg;
74  status = StatusCode::FAILURE;
75  }
76  return status;
77 }
78 
80 {
81 
83  for ( auto& i : inputs ) {
84  status = addStream( i );
85  if ( !status.isSuccess() ) break;
86  }
87  return status;
88 }
89 
91 {
92  clear().ignore();
94  return AlgTool::finalize();
95 }
96 
98 {
99  IEvtSelector* sel = nullptr;
100  StatusCode status = s->initialize();
101  if ( status.isSuccess() ) {
102  status = createSelector( s->name(), s->selectorType(), sel );
103  if ( status.isSuccess() ) {
104  SmartIF<IProperty> prop( sel ); // Att: IProperty, IService used to point to EventSelector
105  SmartIF<IService> isvc( sel );
106  s->setSelector( sel );
107  sel->release(); // No need for this interface anymore, it is passed to the stream
108  if ( prop && isvc ) {
109  for ( const auto& i : s->properties() ) prop->setProperty( i ).ignore();
110  prop->setProperty( Gaudi::Property<int>( "OutputLevel", msgLevel() ) ).ignore();
111  // FIXME: (MCl) Why do we have to initialize the selector here?
112  return isvc->sysInitialize();
113  }
114  }
115  }
116  return StatusCode::FAILURE;
117 }
118 
119 // Create (sub-) Event selector service
121 {
122  auto isvc = make_SmartIF( Service::Factory::create( typ, nam, serviceLocator() ) );
123  if ( isvc ) {
124  auto isel = isvc.as<IEvtSelector>();
125  if ( isel ) {
126  sel = isel.get();
127  sel->addRef(); // make sure that sel is not left dangling once isel and isvc go out of scope...
128  return StatusCode::SUCCESS;
129  }
130  }
131  sel = nullptr;
132  error() << "Failed to create IEvtSelector " << typ << "/" << nam << endmsg;
133  return StatusCode::FAILURE;
134 }
135 
137 {
138  if ( s ) {
139  IEvtSelector* sel = s->selector();
140  if ( sel ) {
141  SmartIF<IService> isvc( sel );
142  if ( isvc ) {
143  isvc->finalize().ignore();
144  s->finalize().ignore();
145  // Fire EndStream "Incident"
146  m_incidentSvc->fireIncident( Incident( name(), IncidentType::EndStream ) );
147  return StatusCode::SUCCESS;
148  }
149  // Failed to get service interface of sub-event selector
150  return StatusCode::FAILURE;
151  }
152  // No selector (yet) attached - no need to finalize it!
153  return StatusCode::SUCCESS;
154  }
155  return StatusCode::FAILURE;
156 }
157 
159 {
160 
161  auto i = getStreamIterator( info );
162  if ( i != m_streams.end() ) {
163  ( *i )->release();
164  m_streams.erase( i );
165  return StatusCode::SUCCESS;
166  }
167  return StatusCode::FAILURE;
168 }
169 
171  EventSelectorDataStream*& stream )
172 {
173  stream = new EventSelectorDataStream( nam, info, serviceLocator() );
174  return StatusCode::SUCCESS;
175 }
176 
178 {
179  auto i = getStreamIterator( info );
180  return i != m_streams.end() ? *i : nullptr;
181 }
182 
183 DataStreamTool::Streams::iterator DataStreamTool::getStreamIterator( const std::string& info )
184 {
186  [&]( const EventSelectorDataStream* i ) { return i->definition() == info; } );
187 }
188 
190 {
191  // pos has to point inside the vector
192  return ( ( pos >= 0 ) && ( (size_t)pos < m_streams.size() ) ) ? m_streams[pos] : nullptr;
193 }
194 
196 
198 {
199 
200  StatusCode iret, status = StatusCode::SUCCESS;
201  iret.ignore();
202 
203  // disconnect the streams
204  for ( auto& il : m_streamSpecs ) {
206  if ( s ) {
207  if ( s->isInitialized() ) {
208  iret = finalizeStream( s );
209  if ( !iret.isSuccess() ) {
210  error() << "Error finalizing Stream" << il << endmsg;
211  status = iret;
212  }
213  }
214  iret = eraseStream( il );
215  if ( !iret.isSuccess() ) {
216  error() << "Error diconnecting Stream" << il << endmsg;
217  status = iret;
218  }
219  }
220  }
221 
222  m_streamSpecs.clear();
223 
224  return status;
225 }
226 
228 {
229 
230  if ( !s ) return StatusCode::FAILURE;
231  s->addRef();
232  m_streams.push_back( s );
233  return StatusCode::SUCCESS;
234 }
235 
237 {
238  if ( getStream( info ) ) {
239  warning() << "Input stream " << info << "already in use" << endmsg;
240  }
241  auto nam = name() + '_' + std::to_string( ++m_streamCount );
242  EventSelectorDataStream* s = nullptr;
243  StatusCode status = createStream( nam, info, s );
244  if ( status.isSuccess() ) return connectStream( s );
245  s->release();
246  return status;
247 }
248 
249 /*
250 
251  Taking control over Streams and return them to EventSelector
252 
253 */
254 
256 {
257 
258  EventSelectorDataStream* nextStream = getStream( dsid );
259  if ( !nextStream ) return StatusCode::FAILURE; //<-end of streams reached
260 
261  esds = nextStream;
262  ++m_streamID;
263 
264  return StatusCode::SUCCESS;
265 }
266 
268 {
269 
270  EventSelectorDataStream* previousStream = getStream( dsid );
271  if ( !previousStream ) return StatusCode::FAILURE; //<-begin of streams reached
272 
273  esds = previousStream;
274  --m_streamID;
275 
276  return StatusCode::SUCCESS;
277 }
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:314
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
StatusCode initialize() override
Definition: AlgTool.cpp:227
StatusCode clear() override
The Event Selector Interface.
Definition: IEvtSelector.h:18
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
T to_string(T...args)
IEvtSelector * selector() const
Retrieve event selector object.
sel
Definition: IOTest.py:84
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:297
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:78
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:44
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Definition of the basic interface.
Definition: IInterface.h:234
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:245
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:143
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:88
void ignore() const
Definition: StatusCode.h:106
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:244
unsigned long release() override
Release Interface instance.
Definition: implements.h:46