| The Gaudi Framework v32r1 (f65d50dc)

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