EventSelector.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/SmartIF.h"
3 #include "GaudiKernel/Incident.h"
4 #include "GaudiKernel/MsgStream.h"
5 #include "GaudiKernel/IIncidentSvc.h"
6 #include "GaudiKernel/ISvcLocator.h"
7 #include "GaudiKernel/ISvcManager.h"
8 #include "GaudiKernel/IToolSvc.h"
9 #include "GaudiKernel/IDataStreamTool.h"
10 #include "GaudiKernel/IAddressCreator.h"
11 #include "GaudiKernel/PropertyMgr.h"
12 #include "GaudiKernel/EventSelectorDataStream.h"
13 
14 #include "EventSelector.h"
15 #include "EventIterator.h"
16 #include <climits>
17 
19 
20 // Standard constructor
21 EventSelector::EventSelector(const std::string& name, ISvcLocator* svcloc )
22  : base_class( name, svcloc)
23 {
24  declareProperty( "Input", m_streamSpecs);
25  declareProperty( "FirstEvent", m_firstEvent);
26  declareProperty( "EvtMax", m_evtMax);
27  declareProperty( "PrintFreq", m_evtPrintFrequency);
28  declareProperty( "StreamManager", m_streamManager);
29 }
30 
32 EventSelector::resetCriteria(const std::string& /* criteria */,
33  Context& /* context */) const
34 {
35  return StatusCode::FAILURE;
36 }
37 
38 // Progress report
40  if ( iter ) {
41  long count = iter->numEvent();
42  // Print an message every m_evtPrintFrequency events
43  if ( 0 == iter->context() ) {
44  MsgStream log(msgSvc(), name());
45  log << MSG::INFO << "End of event input reached." << endmsg;
46  }
47  else if( iter->numStreamEvent() == -1 ) {
48  // Intial value for this stream
49  }
50  else if( m_evtPrintFrequency != -1 && (count % m_evtPrintFrequency == 0)) {
51  MsgStream log(msgSvc(), name());
52  log << MSG::ALWAYS << "Reading Event record " << count+1
53  << ". Record number within stream " << iter->ID()+1
54  << ": " << iter->numStreamEvent()+1 << endmsg;
55  }
56  }
57  else {
58  MsgStream log(msgSvc(), name());
59  log << MSG::INFO << "End of event input reached." << endmsg;
60  }
61 }
62 
63 // IEvtSelector::first()
67  IDataStreamTool::size_type iter_id = (m_reconfigure) ? 0 : iter.ID()+1;
68  if ( m_reconfigure ) const_cast<EventSelector*>(this)->m_reconfigure = false;
69  if ( shutDown ) {
70  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
72  if ( s->isInitialized() ) {
73  EventSelector* thisPtr = const_cast<EventSelector*>(this);
74  if ( s->selector() && iter.context() ) {
75  Context* ctxt = iter.context();
76  s->selector()->releaseContext(ctxt).ignore();
77  iter.set(0,0);
78  }
79  status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
80  iter.set(0,0);
81  }
82  }
83  }
84 
85  const EventSelectorDataStream* s ;
86  status = m_streamtool->getNextStream( s , iter_id );
87 
88  if ( status.isSuccess() ) {
89 
90  if (s) {
91  if ( !s->isInitialized() ) {
92  EventSelector* thisPtr = const_cast<EventSelector*>(this);
93  status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
94  }
95 
96  if ( status.isSuccess() ) {
97  const IEvtSelector* sel = s->selector();
98  if ( sel ) {
99  Context* ctxt = nullptr;
100  status = sel->createContext(ctxt);
101  if ( status.isSuccess() ) {
102  status = sel->resetCriteria(s->criteria(), *ctxt);
103  if ( status.isSuccess() ) {
104  MsgStream log(msgSvc(), name());
105  iter.set(this, iter_id, ctxt, 0);
106  log << MSG::INFO << *s << endmsg;
108  return StatusCode::SUCCESS;
109  }
110  }
111  }
112  }
114  }
115  }
116 
117  iter.set(this, -1, 0, 0);
118  status.setChecked();
119  //m_incidentSvc->fireIncident(Incident(s->dbName(),IncidentType::FailInputFile));
120  return StatusCode::FAILURE;
121 }
122 
123 // IEvtSelector::first()
127  if ( shutDown ) {
128  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
130  if ( s->isInitialized() ) {
131  EventSelector* thisPtr = const_cast<EventSelector*>(this);
132  if ( s->selector() && iter.context() ) {
133  Context* ctxt = iter.context();
134  s->selector()->releaseContext(ctxt);
135  iter.set(0,0);
136  }
137  status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
138  iter.set(0,0);
139  }
140  }
141  }
142 
143  IDataStreamTool::size_type iter_id = iter.ID()-1;
144  const EventSelectorDataStream* s = nullptr;
145  status = m_streamtool->getPreviousStream( s , iter_id );
146 
147  if ( status.isSuccess() ) {
148 
149  if ( !s->isInitialized() ) {
150  EventSelector* thisPtr = const_cast<EventSelector*>(this);
151  status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
152  }
153  if ( status.isSuccess() ) {
154  const IEvtSelector* sel = s->selector();
155  if ( sel ) {
156  Context* ctxt = nullptr;
157  status = sel->createContext(ctxt);
158  if ( status.isSuccess() ) {
159  status = sel->resetCriteria(s->criteria(), *ctxt);
160  if ( status.isSuccess() ) {
161  MsgStream log(msgSvc(), name());
162  iter.set(this, iter_id, ctxt, 0);
163  log << MSG::INFO << *s << endmsg;
164  return StatusCode::SUCCESS;
165  }
166  }
167  }
168  }
169  }
170 
171  iter.set(this, -1, 0, 0);
172  return StatusCode::FAILURE;
173 }
174 
176 StatusCode EventSelector::createContext(Context*& refpCtxt) const
177 {
178  // Max event is zero. Return begin = end
179  refpCtxt = nullptr;
180  if ( m_firstEvent < 0 ) {
181  MsgStream log(msgSvc(), name());
182  log << MSG::ERROR << "First Event = " << m_firstEvent << " not valid" << endmsg;
183  log << MSG::ERROR << "It should be > 0 " << endmsg;
184  return StatusCode::FAILURE; // if failure => iterators = end();
185  }
186  EvtSelectorContext* ctxt = new EvtSelectorContext(this);
187  ctxt->set(0, -1, 0, 0);
188  firstOfNextStream(true, *ctxt).ignore();
189  refpCtxt = ctxt;
190  long nskip = m_firstEvent;
191  while( --nskip > 0 ) {
192  StatusCode sc = next(*refpCtxt);
193  if ( sc.isFailure() ) {
194  MsgStream log(msgSvc(), name());
195  log << MSG::ERROR << " createContext() failed to start with event number "
196  << m_firstEvent << endmsg;
197  releaseContext(refpCtxt);
198  refpCtxt = nullptr;
199  return StatusCode::FAILURE;
200  }
201  }
202  return StatusCode::SUCCESS;
203 }
204 
206 StatusCode EventSelector::next(Context& refCtxt) const {
207  return next(refCtxt, 1);
208 }
209 
211 StatusCode EventSelector::next(Context& refCtxt, int /* jump */ ) const {
212  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
213  if ( pIt ) {
214  if ( pIt->ID() != -1 ) {
216  Context* it = pIt->context();
217  IEvtSelector* sel = s->selector();
218  if ( it && sel ) { // First exploit the current stream
219  StatusCode sc = sel->next(*it); // This stream is empty: advance to the next stream
220  if ( !sc.isSuccess() ) {
222  sc = firstOfNextStream(true, *pIt);
223  if (sc.isSuccess() ) sc = next(*pIt);
224  }
225  else {
226  pIt->increaseCounters(false);
227  pIt->set(it, 0);
228  printEvtInfo(pIt);
229  }
230  return sc;
231  }
232  else if ( m_reconfigure ) {
233  StatusCode sc = firstOfNextStream(false, *pIt);
234  printEvtInfo(pIt);
235  return sc;
236  }
237  }
238  else if ( m_reconfigure ) {
239  StatusCode sc = firstOfNextStream(false, *pIt);
240  printEvtInfo(pIt);
241  return sc;
242  }
243  pIt->increaseCounters(false);
244  }
245  printEvtInfo(pIt);
246  return StatusCode::FAILURE;
247 }
248 
250 StatusCode EventSelector::previous(Context& refCtxt) const {
251  return previous(refCtxt, 1);
252 }
253 
255 StatusCode EventSelector::previous(Context& refCtxt, int jump) const {
256  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
257  if ( pIt && jump > 0 ) {
259  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
261  Context* it = pIt->context();
262  IEvtSelector* sel = s->selector();
263  if ( it && sel ) { // First exploit the current stream
264  // This stream is empty: advance to the next stream
265  sc = sel->previous(*it); // This stream is empty: advance to the next stream
266  if ( !sc.isSuccess() ) {
267  sc = lastOfPreviousStream(true, *pIt);
268  }
269  else {
270  pIt->increaseCounters(false);
271  pIt->set(it, 0);
272  }
273  printEvtInfo(pIt);
274  if ( !sc.isSuccess() ) {
275  return sc;
276  }
277  }
278  pIt->increaseCounters(false);
279  }
280  return sc;
281  }
282  printEvtInfo(pIt);
283  return StatusCode::FAILURE;
284 }
285 
287 StatusCode EventSelector::last(Context& refCtxt) const {
288  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
289  if ( pIt ) {
290  }
291  return StatusCode::FAILURE;
292 }
293 
295 StatusCode EventSelector::rewind(Context& refCtxt) const {
296  EvtSelectorContext *ctxt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
297  if ( ctxt ) {
298  ctxt->set(0, -1, 0, 0);
299  firstOfNextStream(true, *ctxt);
300  long nskip = m_firstEvent;
301  while( --nskip > 0 ) {
302  StatusCode sc = next(*ctxt);
303  if ( sc.isFailure() ) {
304  MsgStream log(msgSvc(), name());
305  log << MSG::ERROR << "rewind() failed to start with event number "
306  << m_firstEvent << endmsg;
307  return StatusCode::FAILURE;
308  }
309  }
310  return StatusCode::SUCCESS;
311  }
312  return StatusCode::FAILURE;
313 }
314 
317 EventSelector::createAddress(const Context& refCtxt,
318  IOpaqueAddress*& refpAddr) const
319 {
320  const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(&refCtxt);
321  EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
322  refpAddr = nullptr;
323  if ( pIt ) {
325  Context* it = pIt->context();
326  IEvtSelector* sel = s->selector();
327  if ( it && sel ) {
328  IOpaqueAddress* pAddr = nullptr;
329  StatusCode sc = sel->createAddress(*it, pAddr);
330  if ( sc.isSuccess() ) refpAddr = pAddr;
331  pIt->set(it, pAddr);
332  return sc;
333  }
334  }
335  return StatusCode::FAILURE;
336 }
337 
338 // Release existing event iteration context
339 StatusCode EventSelector::releaseContext(Context*& refCtxt) const {
340  const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(refCtxt);
341  std::unique_ptr<EvtSelectorContext> pIt{ const_cast<EvtSelectorContext*>(cpIt) };
342  if ( pIt && pIt->ID() >= 0 && pIt->ID() < (long)m_streamtool->size() ) {
343  const EventSelectorDataStream* s = m_streamtool->getStream(pIt->ID());
344  Context* it = pIt->context();
345  IEvtSelector* sel = s->selector();
346  if ( it && sel ) {
347  StatusCode sc = sel->releaseContext(it);
348  if ( sc.isSuccess() ) {
349  refCtxt = nullptr;
350  return sc;
351  }
352  }
353  }
354  return StatusCode::SUCCESS;
355 }
356 
359  // Initialize base class
360  StatusCode status = Service::initialize();
361  MsgStream logger(msgSvc(), name());
362  if ( !status.isSuccess() ) {
363  logger << MSG::ERROR << "Error initializing base class Service!" << endmsg;
364  return status;
365  }
366  // Get the references to the services that are needed by the ApplicationMgr itself
367  m_incidentSvc = serviceLocator()->service("IncidentSvc");
368  if( !m_incidentSvc ) {
369  logger << MSG::FATAL << "Error retrieving IncidentSvc." << endmsg;
370  return StatusCode::FAILURE;
371  }
372  if ( m_evtMax != INT_MAX ) {
373  logger << MSG::ERROR << "EvtMax is an obsolete property of the event selector." << endmsg;
374  logger << MSG::ERROR << "Please set \"ApplicationMgr.EvtMax = " << m_evtMax
375  << ";\" to process the requested number of events." << endmsg;
376  return StatusCode::FAILURE;
377  }
378 
379  m_toolSvc = serviceLocator()->service("ToolSvc");
380  if ( !m_toolSvc ) {
381  logger << MSG::ERROR << " Could not locate the Tool Service! " << endmsg;
382  return StatusCode::FAILURE;
383  }
384  // make sure we finalize _prior_ to ToolSvc... we are about to get a
385  // a pointer to a tool which gets finalized and released by the ToolSvc
386  // during ToolSvc::finalize, and we don't want dangling pointers...
387  SmartIF<ISvcManager> mgr(serviceLocator());
388  auto prio = mgr->getPriority("ToolSvc");
389  mgr->setPriority(name(),prio+1).ignore();
390 
391  status = m_toolSvc->retrieveTool(m_streamManager.c_str(), m_streamtool, this);
392 
393  if( status.isFailure() ) {
394  logger << MSG::ERROR << "Error initializing "
395  << m_streamManager << endmsg;
396  return status;
397  }
398 
399  status = m_streamtool->clear();
400  if( status.isFailure() ) {
401  // Message already printed by the tool
402  return status;
403  }
404 
406 
408 
409  m_streamID = 0;
410 
411  return status;
412 }
413 
414 // Re-initialize
416  if ( FSMState() != Gaudi::StateMachine::INITIALIZED ) {
417  MsgStream logger(msgSvc(), name());
418  logger << MSG::ERROR << "Cannot reinitialize: service not in state initialized" << endmsg;
419  return StatusCode::FAILURE;
420  }
421 
423  StatusCode status = m_streamtool->clear();
424  if ( status.isFailure() ) return status;
426  m_reconfigure = true;
428  }
429 
430  return StatusCode::SUCCESS;
431 }
432 
433 //
435 
436  if (msgLevel(MSG::DEBUG)) {
437  MsgStream log(msgSvc(), name());
438  log << MSG::DEBUG << "finalize()" << endmsg;
439  }
440 
441  m_incidentSvc = nullptr;
442 
443  if (m_streamtool) {
444  if (m_toolSvc) {
446  } else {
447  // It should not be possible to get here
448  m_streamtool->release();
449  }
450  m_streamtool = nullptr;
451  }
452  m_toolSvc.reset();
453 
454  return Service::finalize();
455 }
456 
StatusCode lastOfPreviousStream(bool shutDown, EvtSelectorContext &it) const
Retrieve last entry of the previous data stream.
SmartIF< IIncidentSvc > m_incidentSvc
Reference to the indicent service.
Definition: EventSelector.h:63
bool isInitialized() const
Check initialization status.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:62
IEvtSelector::Context * context() const
Access "real" iterator.
Definition: EventIterator.h:70
int m_evtPrintFrequency
Printout frequency.
Definition: EventSelector.h:84
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
virtual StatusCode finalizeStream(EventSelectorDataStream *)=0
int m_firstEvent
First event to be processed.
Definition: EventSelector.h:80
StatusCode finalize() override
Definition: Service.cpp:187
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
int m_evtMax
Maximum number of events to be processed.
Definition: EventSelector.h:82
The Event Selector Interface.
Definition: IEvtSelector.h:18
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
StatusCode resetCriteria(const std::string &cr, Context &c) const override
Will set a new criteria for the selection of the next list of events and will change the state of the...
IDataStreamTool * m_streamtool
Definition: EventSelector.h:67
IEvtSelector * selector() const
Retrieve event selector object.
long int m_streamID
Definition: EventSelector.h:59
virtual void printEvtInfo(const EvtSelectorContext *iter) const
Progress report.
STL namespace.
virtual StatusCode resetCriteria(const std::string &cr, Context &c) const =0
Will set a new criteria for the selection of the next list of events and will change the state of the...
GAUDIPS_API Logger & logger()
Return the current logger instance.
StatusCode firstOfNextStream(bool shutDown, EvtSelectorContext &it) const
Retrieve first entry of the next data stream.
const std::string FailInputFile
could not open or read from this file
Definition: Incident.h:78
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
StatusCode retrieveTool(const std::string &type, T *&tool, const IInterface *parent=nullptr, bool createIf=true)
Retrieve specified tool sub-type with tool dependent part of the name automatically assigned...
Definition: IToolSvc.h:141
StreamSpecs m_streamSpecsLast
Input stream specifiers (last used)
Definition: EventSelector.h:74
virtual StatusCode addStreams(const StreamSpecs &)=0
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
StatusCode reinitialize() override
Service override: Reinitialize service.
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
virtual StatusCode clear()=0
Definition of class EventIterator.
Definition: EventIterator.h:32
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
virtual StatusCode next(Context &c) const =0
Fetch the next event or the first event if it will be use soon after the creation of the context...
const std::string EndInputFile
an input file has been finished
Definition: Incident.h:79
virtual StatusCode createContext(Context *&c) const =0
Create and return a context object that will keep track of the state of selection.
virtual EventSelectorDataStream * getStream(size_type)=0
virtual StatusCode releaseContext(Context *&) const =0
Release the Context object.
StatusCode initialize() override
IService implementation: Db event selector override.
StreamSpecs m_streamSpecs
Input stream specifiers (for job options)
Definition: EventSelector.h:72
long numStreamEvent() const
Access counter within stream.
virtual StatusCode setPriority(const std::string &name, int pri)=0
StatusCode finalize() override
IService implementation: Service finalization.
virtual StatusCode getPreviousStream(const EventSelectorDataStream *&, size_type &)=0
SmartIF< IToolSvc > m_toolSvc
Definition: EventSelector.h:65
virtual int getPriority(const std::string &name) const =0
void set(const IEvtSelector *sel, IDataStreamTool::size_type id, IEvtSelector::Context *it, IOpaqueAddress *pA)
Set the address of the iterator.
Definition: EventIterator.h:53
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
Definition of class EventSelectorDataStream.
Base class for all Incidents (computing events).
Definition: Incident.h:16
virtual IDataStreamTool::size_type ID() const
Stream identifier.
Definition: EventIterator.h:95
string s
Definition: gaudirun.py:246
virtual StatusCode releaseTool(IAlgTool *tool)=0
Release the tool.
IDataStreamTool::size_type increaseCounters(bool reset=false)
Increase counters.
Definition: EventIterator.h:74
virtual StatusCode previous(Context &c) const =0
Fetch the previous event.
Definition of class EventSelector.
Definition: EventSelector.h:53
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
virtual size_type size()=0
virtual StatusCode initializeStream(EventSelectorDataStream *)=0
const std::string & criteria() const
Retrieve stream criteria.
bool m_reconfigure
Reconfigure occurred.
Definition: EventSelector.h:70
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:108
StatusCode last(Context &c) const override
Access last item in the iteration.
virtual StatusCode getNextStream(const EventSelectorDataStream *&, size_type &)=0
std::string m_streamManager
Definition: EventSelector.h:86
virtual StatusCode createAddress(const Context &c, IOpaqueAddress *&iop) const =0
Create an IOpaqueAddress object from the event fetched.
long numEvent() const
Access counter.
Definition: EventIterator.h:99
list i
Definition: ana.py:128
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
const std::string & dbName() const
Retrieve stream dbName.
void setChecked() const
Ignore the checking code;.
Definition: StatusCode.h:105
const std::string BeginInputFile
a new input file has been started
Definition: Incident.h:77