The Gaudi Framework  v30r3 (a5ef0a68)
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 //=============================================================================
23 {
24 
26  if ( !status.isSuccess() ) {
27  fatal() << "Error. Cannot initialize base class." << endmsg;
28  return status;
29  }
30 
31  // Get the references to the services that are needed by the ApplicationMgr itself
32  m_incidentSvc = serviceLocator()->service( "IncidentSvc" );
33  if ( !m_incidentSvc ) {
34  fatal() << "Error retrieving IncidentSvc." << endmsg;
35  return StatusCode::FAILURE;
36  }
37 
38  return StatusCode::SUCCESS;
39 }
40 
42 {
43 
44  if ( getStream( input ) ) {
45  warning() << "Input stream " << input << "already in use" << endmsg;
46  }
47 
48  m_streamSpecs.push_back( input );
49 
50  auto strname = name() + '_' + std::to_string( ++m_streamCount );
51  EventSelectorDataStream* s = nullptr;
52 
53  StatusCode status = createStream( strname, input, s );
54 
55  if ( status.isSuccess() && s ) {
56  s->addRef();
57  m_streams.push_back( s );
58  status = StatusCode::SUCCESS;
59  } else {
60  if ( s ) {
61  s->release();
62  error() << "Error connecting/creating Stream: " << s << endmsg;
63  }
64  error() << "Error connecting/creating Stream: " << input << endmsg;
65  status = StatusCode::FAILURE;
66  }
67  return status;
68 }
69 
70 StatusCode DataStreamTool::addStreams( const StreamSpecs& inputs )
71 {
72 
74  for ( auto& i : inputs ) {
75  status = addStream( i );
76  if ( !status.isSuccess() ) break;
77  }
78  return status;
79 }
80 
82 {
83  clear().ignore();
85  return AlgTool::finalize();
86 }
87 
89 {
90  IEvtSelector* sel = nullptr;
91  StatusCode status = s->initialize();
92  if ( status.isSuccess() ) {
93  status = createSelector( s->name(), s->selectorType(), sel );
94  if ( status.isSuccess() ) {
95  SmartIF<IProperty> prop( sel ); // Att: IProperty, IService used to point to EventSelector
96  SmartIF<IService> isvc( sel );
97  s->setSelector( sel );
98  sel->release(); // No need for this interface anymore, it is passed to the stream
99  if ( prop && isvc ) {
100  for ( const auto& i : s->properties() ) prop->setProperty( i ).ignore();
101  prop->setProperty( Gaudi::Property<int>( "OutputLevel", msgLevel() ) ).ignore();
102  // FIXME: (MCl) Why do we have to initialize the selector here?
103  return isvc->sysInitialize();
104  }
105  }
106  }
107  return StatusCode::FAILURE;
108 }
109 
110 // Create (sub-) Event selector service
112 {
113  auto isvc = make_SmartIF( Service::Factory::create( typ, nam, serviceLocator() ).release() );
114  if ( isvc ) {
115  auto isel = isvc.as<IEvtSelector>();
116  if ( isel ) {
117  sel = isel.get();
118  sel->addRef(); // make sure that sel is not left dangling once isel and isvc go out of scope...
119  return StatusCode::SUCCESS;
120  }
121  }
122  sel = nullptr;
123  error() << "Failed to create IEvtSelector " << typ << "/" << nam << endmsg;
124  return StatusCode::FAILURE;
125 }
126 
128 {
129  if ( s ) {
130  IEvtSelector* sel = s->selector();
131  if ( sel ) {
132  SmartIF<IService> isvc( sel );
133  if ( isvc ) {
134  isvc->finalize().ignore();
135  s->finalize().ignore();
136  // Fire EndStream "Incident"
137  m_incidentSvc->fireIncident( Incident( name(), IncidentType::EndStream ) );
138  return StatusCode::SUCCESS;
139  }
140  // Failed to get service interface of sub-event selector
141  return StatusCode::FAILURE;
142  }
143  // No selector (yet) attached - no need to finalize it!
144  return StatusCode::SUCCESS;
145  }
146  return StatusCode::FAILURE;
147 }
148 
150 {
151 
152  auto i = getStreamIterator( info );
153  if ( i != m_streams.end() ) {
154  ( *i )->release();
155  m_streams.erase( i );
156  return StatusCode::SUCCESS;
157  }
158  return StatusCode::FAILURE;
159 }
160 
162  EventSelectorDataStream*& stream )
163 {
164  stream = new EventSelectorDataStream( nam, info, serviceLocator() );
165  return StatusCode::SUCCESS;
166 }
167 
169 {
170  auto i = getStreamIterator( info );
171  return i != m_streams.end() ? *i : nullptr;
172 }
173 
174 DataStreamTool::Streams::iterator DataStreamTool::getStreamIterator( const std::string& info )
175 {
177  [&]( const EventSelectorDataStream* i ) { return i->definition() == info; } );
178 }
179 
181 {
182  // pos has to point inside the vector
183  return ( ( pos >= 0 ) && ( (size_t)pos < m_streams.size() ) ) ? m_streams[pos] : nullptr;
184 }
185 
187 
189 {
190 
191  StatusCode iret, status = StatusCode::SUCCESS;
192  iret.ignore();
193 
194  // disconnect the streams
195  for ( auto& il : m_streamSpecs ) {
197  if ( s ) {
198  if ( s->isInitialized() ) {
199  iret = finalizeStream( s );
200  if ( !iret.isSuccess() ) {
201  error() << "Error finalizing Stream" << il << endmsg;
202  status = iret;
203  }
204  }
205  iret = eraseStream( il );
206  if ( !iret.isSuccess() ) {
207  error() << "Error diconnecting Stream" << il << endmsg;
208  status = iret;
209  }
210  }
211  }
212 
213  m_streamSpecs.clear();
214 
215  return status;
216 }
217 
219 {
220 
221  if ( !s ) return StatusCode::FAILURE;
222  s->addRef();
223  m_streams.push_back( s );
224  return StatusCode::SUCCESS;
225 }
226 
228 {
229  if ( getStream( info ) ) {
230  warning() << "Input stream " << info << "already in use" << endmsg;
231  }
232  auto nam = name() + '_' + std::to_string( ++m_streamCount );
233  EventSelectorDataStream* s = nullptr;
234  StatusCode status = createStream( nam, info, s );
235  if ( status.isSuccess() ) return connectStream( s );
236  s->release();
237  return status;
238 }
239 
240 /*
241 
242  Taking control over Streams and return them to EventSelector
243 
244 */
245 
247 {
248 
249  EventSelectorDataStream* nextStream = getStream( dsid );
250  if ( !nextStream ) return StatusCode::FAILURE; //<-end of streams reached
251 
252  esds = nextStream;
253  ++m_streamID;
254 
255  return StatusCode::SUCCESS;
256 }
257 
259 {
260 
261  EventSelectorDataStream* previousStream = getStream( dsid );
262  if ( !previousStream ) return StatusCode::FAILURE; //<-begin of streams reached
263 
264  esds = previousStream;
265  --m_streamID;
266 
267  return StatusCode::SUCCESS;
268 }
bool isInitialized() const
Check initialization status.
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:83
Implementation of property with value of concrete type.
Definition: Property.h:381
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
StatusCode initialize() override
Definition: AlgTool.cpp:223
StatusCode clear() override
The Event Selector Interface.
Definition: IEvtSelector.h:18
bool isSuccess() const
Definition: StatusCode.h:287
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:293
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:39
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
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.
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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:62
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 StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:165
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
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
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:41
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)