DataStreamTool.cpp
Go to the documentation of this file.
1 // Include files
2 
3 // from Gaudi
5 #include "GaudiKernel/SmartIF.h"
6 #include "GaudiKernel/Incident.h"
14 #include "GaudiKernel/Service.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  const std::string& name,
27  const IInterface* parent )
28  : base_class ( type, name , parent )
29 {
30  //declareInterface<IDataStreamTool>(this);
31 }
32 //=============================================================================
34 
35 
37  if( !status.isSuccess() ) {
38  fatal() << "Error. Cannot initialize base class." << endmsg;
39  return status;
40  }
41 
42  // Get the references to the services that are needed by the ApplicationMgr itself
43  m_incidentSvc = serviceLocator()->service("IncidentSvc");
44  if( !m_incidentSvc ) {
45  fatal() << "Error retrieving IncidentSvc." << endmsg;
46  return StatusCode::FAILURE;
47  }
48 
49  return StatusCode::SUCCESS;
50 
51 }
52 
54 
55  if ( getStream(input) ) {
56  warning() << "Input stream " << input << "already in use" << endmsg;
57  }
58 
59  m_streamSpecs.push_back(input);
60 
61  auto strname = name() + '_' + std::to_string( ++m_streamCount );
62  EventSelectorDataStream* s = nullptr;
63 
64  StatusCode status = createStream(strname, input , s );
65 
66  if( status.isSuccess() && s ) {
67  s->addRef();
69  status = StatusCode::SUCCESS;
70  } else {
71  if (s) {
72  s->release();
73  error() << "Error connecting/creating Stream: " << s << endmsg;
74  }
75  error() << "Error connecting/creating Stream: " << input << endmsg;
76  status = StatusCode::FAILURE;
77  }
78  return status;
79 }
80 
81 StatusCode DataStreamTool::addStreams(const StreamSpecs & inputs) {
82 
84  for ( auto& i : inputs ) {
85  status = addStream(i);
86  if (!status.isSuccess()) break;
87  }
88  return status;
89 
90 }
91 
93  clear().ignore();
95  return AlgTool::finalize();
96 }
97 
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(IntegerProperty("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  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 
135 
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 
157 
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  stream = new EventSelectorDataStream(nam, info, serviceLocator());
172  return StatusCode::SUCCESS;
173 }
174 
175 
177  auto i = getStreamIterator(info);
178  return i != m_streams.end() ? *i : nullptr;
179 }
180 
181 DataStreamTool::Streams::iterator DataStreamTool::getStreamIterator ( const std::string& info ) {
183  [&](const EventSelectorDataStream* i) {
184  return i->definition()==info;
185  });
186 }
187 
189  // pos has to point inside the vector
190  return ( (pos >= 0) && ((size_t)pos < m_streams.size()) ) ? m_streams[pos]
191  : nullptr;
192 }
193 
195 {
196  return m_streams.back();
197 }
198 
199 
200 
202 {
203 
204  StatusCode iret, status = StatusCode::SUCCESS;
205  iret.ignore();
206 
207  // disconnect the streams
208  for ( auto& il : m_streamSpecs ) {
210  if ( s ) {
211  if ( s->isInitialized() ) {
212  iret = finalizeStream(s);
213  if ( !iret.isSuccess() ) {
214  error() << "Error finalizing Stream" << il << endmsg;
215  status = iret;
216  }
217  }
218  iret = eraseStream( il );
219  if ( !iret.isSuccess() ) {
220  error() << "Error diconnecting Stream" << il << endmsg;
221  status = iret;
222  }
223  }
224  }
225 
226  m_streamSpecs.clear();
227 
228  return status;
229 }
230 
231 
233 {
234 
235  if ( !s ) return StatusCode::FAILURE;
236  s->addRef();
237  m_streams.push_back(s);
238  return StatusCode::SUCCESS;
239 
240 }
241 
243 {
244  if ( getStream(info) ) {
245  warning() << "Input stream " << info << "already in use" << endmsg;
246  }
247  auto nam = name() + '_' + std::to_string( ++m_streamCount);
248  EventSelectorDataStream* s = nullptr;
249  StatusCode status = createStream(nam, info, s);
250  if ( status.isSuccess() ) return connectStream(s);
251  s->release();
252  return status;
253 }
254 
255 /*
256 
257  Taking control over Streams and return them to EventSelector
258 
259 */
260 
261 
263 {
264 
265  EventSelectorDataStream * nextStream = getStream(dsid);
266  if ( !nextStream ) return StatusCode::FAILURE; //<-end of streams reached
267 
268  esds = nextStream;
269  ++m_streamID;
270 
271  return StatusCode::SUCCESS;
272 
273 }
274 
276 {
277 
278  EventSelectorDataStream * previousStream = getStream(dsid);
279  if ( !previousStream ) return StatusCode::FAILURE; //<-begin of streams reached
280 
281  esds = previousStream;
282  --m_streamID;
283 
284  return StatusCode::SUCCESS;
285 
286 }
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
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
StatusCode initialize() override
Definition: AlgTool.cpp:290
StatusCode clear() override
The Event Selector Interface.
Definition: IEvtSelector.h:18
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
T to_string(T...args)
IEvtSelector * selector() const
Retrieve event selector object.
StatusCode createStream(const std::string &, const std::string &, EventSelectorDataStream *&) override
T end(T...args)
StatusCode initializeStream(EventSelectorDataStream *) override
Initialize newly opened stream.
STL class.
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: AlgTool.cpp:86
StatusCode finalize() override
StatusCode finalize() override
Definition: AlgTool.cpp:360
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 initialize()
Parse input criteria.
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
string type
Definition: gaudirun.py:151
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 setProperty(const Property &p)=0
Set the property by property.
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.
SimpleProperty< int > IntegerProperty
Definition: Property.h:708
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)
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:108
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
list i
Definition: ana.py:128
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:65
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