EventSelector.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/xtoa.h"
3 #include "GaudiKernel/SmartIF.h"
4 #include "GaudiKernel/Incident.h"
5 #include "GaudiKernel/MsgStream.h"
6 #include "GaudiKernel/IIncidentSvc.h"
7 #include "GaudiKernel/ISvcLocator.h"
8 #include "GaudiKernel/ISvcManager.h"
9 #include "GaudiKernel/IToolSvc.h"
10 #include "GaudiKernel/IDataStreamTool.h"
11 #include "GaudiKernel/IAddressCreator.h"
12 #include "GaudiKernel/PropertyMgr.h"
13 #include "GaudiKernel/EventSelectorDataStream.h"
14 #include "GaudiKernel/AppReturnCode.h"
15 
16 #include "EventSelector.h"
17 #include "EventIterator.h"
18 #include <climits>
19 
21 
22 // Standard constructor
23 EventSelector::EventSelector(const std::string& name, ISvcLocator* svcloc )
24  : base_class( name, svcloc)
25 {
26  m_incidentSvc = 0;
27  m_toolSvc = 0;
28  m_streamCount = 0;
29  m_firstEvent = 0;
30  m_evtPrintFrequency = 10;
31  m_evtMax = INT_MAX;
32  declareProperty( "Input", m_streamSpecs);
33  declareProperty( "FirstEvent", m_firstEvent);
34  declareProperty( "EvtMax", m_evtMax);
35  declareProperty( "PrintFreq", m_evtPrintFrequency);
36  declareProperty( "StreamManager", m_streamManager="DataStreamTool");
37  m_reconfigure = false;
38 }
39 
40 // Standard destructor
42 {
43 }
44 
46 EventSelector::resetCriteria(const std::string& /* criteria */,
47  Context& /* context */) const
48 {
49  return StatusCode::FAILURE;
50 }
51 
52 // Progress report
54  if ( 0 != iter ) {
55  long count = iter->numEvent();
56  // Print an message every m_evtPrintFrequency events
57  if ( 0 == iter->context() ) {
58  MsgStream log(msgSvc(), name());
59  log << MSG::INFO << "End of event input reached." << endmsg;
60  }
61  else if( iter->numStreamEvent() == -1 ) {
62  // Intial value for this stream
63  }
64  else if( m_evtPrintFrequency != -1 && (count % m_evtPrintFrequency == 0)) {
65  MsgStream log(msgSvc(), name());
66  log << MSG::ALWAYS << "Reading Event record " << count+1
67  << ". Record number within stream " << iter->ID()+1
68  << ": " << iter->numStreamEvent()+1 << endmsg;
69  }
70  }
71  else {
72  MsgStream log(msgSvc(), name());
73  log << MSG::INFO << "End of event input reached." << endmsg;
74  }
75 }
76 
77 // IEvtSelector::first()
81  IDataStreamTool::size_type iter_id = (m_reconfigure) ? 0 : iter.ID()+1;
82  if ( m_reconfigure ) const_cast<EventSelector*>(this)->m_reconfigure = false;
83  if ( shutDown ) {
84  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
86  if ( s->isInitialized() ) {
87  EventSelector* thisPtr = const_cast<EventSelector*>(this);
88  if ( s->selector() && iter.context() ) {
89  Context* ctxt = iter.context();
90  s->selector()->releaseContext(ctxt).ignore();
91  iter.set(0,0);
92  }
93  status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
94  iter.set(0,0);
95  }
96  }
97  }
98 
99  const EventSelectorDataStream* s ;
100  status = m_streamtool->getNextStream( s , iter_id );
101 
102  if ( status.isSuccess() ) {
103 
104  if(s!=NULL) {
105  if ( !s->isInitialized() ) {
106  EventSelector* thisPtr = const_cast<EventSelector*>(this);
107  status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
108  }
109 
110  if ( status.isSuccess() ) {
111  const IEvtSelector* sel = s->selector();
112  if ( sel ) {
113  Context* ctxt = 0;
114  status = sel->createContext(ctxt);
115  if ( status.isSuccess() ) {
116  status = sel->resetCriteria(s->criteria(), *ctxt);
117  if ( status.isSuccess() ) {
118  MsgStream log(msgSvc(), name());
119  iter.set(this, iter_id, ctxt, 0);
120  log << MSG::INFO << *s << endmsg;
122  return StatusCode::SUCCESS;
123  }
124  }
125  }
126  }
128  }
129  }
130 
131  iter.set(this, -1, 0, 0);
132  status.setChecked();
133  //m_incidentSvc->fireIncident(Incident(s->dbName(),IncidentType::FailInputFile));
134  return StatusCode::FAILURE;
135 }
136 
137 // IEvtSelector::first()
141  if ( shutDown ) {
142  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
144  if ( s->isInitialized() ) {
145  EventSelector* thisPtr = const_cast<EventSelector*>(this);
146  if ( s->selector() && iter.context() ) {
147  Context* ctxt = iter.context();
148  s->selector()->releaseContext(ctxt);
149  iter.set(0,0);
150  }
151  status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
152  iter.set(0,0);
153  }
154  }
155  }
156 
157  IDataStreamTool::size_type iter_id = iter.ID()-1;
158  const EventSelectorDataStream* s ;
159  status = m_streamtool->getPreviousStream( s , iter_id );
160 
161  if ( status.isSuccess() ) {
162 
163  if ( !s->isInitialized() ) {
164  EventSelector* thisPtr = const_cast<EventSelector*>(this);
165  status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
166  }
167  if ( status.isSuccess() ) {
168  const IEvtSelector* sel = s->selector();
169  if ( sel ) {
170  Context* ctxt = 0;
171  status = sel->createContext(ctxt);
172  if ( status.isSuccess() ) {
173  status = sel->resetCriteria(s->criteria(), *ctxt);
174  if ( status.isSuccess() ) {
175  MsgStream log(msgSvc(), name());
176  iter.set(this, iter_id, ctxt, 0);
177  log << MSG::INFO << *s << endmsg;
178  return StatusCode::SUCCESS;
179  }
180  }
181  }
182  }
183  }
184 
185  iter.set(this, -1, 0, 0);
186  return StatusCode::FAILURE;
187 }
188 
191 {
192  // Max event is zero. Return begin = end
193  refpCtxt = 0;
194  if ( m_firstEvent < 0 ) {
195  MsgStream log(msgSvc(), name());
196  log << MSG::ERROR << "First Event = " << m_firstEvent << " not valid" << endmsg;
197  log << MSG::ERROR << "It should be > 0 " << endmsg;
198  return StatusCode::FAILURE; // if failure => iterators = end();
199  }
200  EvtSelectorContext* ctxt = new EvtSelectorContext(this);
201  ctxt->set(0, -1, 0, 0);
202  firstOfNextStream(true, *ctxt).ignore();
203  refpCtxt = ctxt;
204  long nskip = m_firstEvent;
205  while( --nskip > 0 ) {
206  StatusCode sc = next(*refpCtxt);
207  if ( sc.isFailure() ) {
208  MsgStream log(msgSvc(), name());
209  log << MSG::ERROR << " createContext() failed to start with event number "
210  << m_firstEvent << endmsg;
211  releaseContext(refpCtxt);
212  refpCtxt = 0;
213  return StatusCode::FAILURE;
214  }
215  }
216  return StatusCode::SUCCESS;
217 }
218 
221  return next(refCtxt, 1);
222 }
223 
225 StatusCode EventSelector::next(Context& refCtxt, int /* jump */ ) const {
226  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
227  if ( pIt ) {
228  if ( pIt->ID() != -1 ) {
230  Context* it = pIt->context();
231  IEvtSelector* sel = s->selector();
232  if ( it && sel ) { // First exploit the current stream
233  StatusCode sc = sel->next(*it); // This stream is empty: advance to the next stream
234  if ( !sc.isSuccess() ) {
236  sc = firstOfNextStream(true, *pIt);
237  if (sc.isSuccess() ) sc = next(*pIt);
238  }
239  else {
240  pIt->increaseCounters(false);
241  pIt->set(it, 0);
242  printEvtInfo(pIt);
243  }
244  return sc;
245  }
246  else if ( m_reconfigure ) {
247  StatusCode sc = firstOfNextStream(false, *pIt);
248  printEvtInfo(pIt);
249  return sc;
250  }
251  }
252  else if ( m_reconfigure ) {
253  StatusCode sc = firstOfNextStream(false, *pIt);
254  printEvtInfo(pIt);
255  return sc;
256  }
257  pIt->increaseCounters(false);
258  }
259  printEvtInfo(pIt);
260  return StatusCode::FAILURE;
261 }
262 
265  return previous(refCtxt, 1);
266 }
267 
269 StatusCode EventSelector::previous(Context& refCtxt, int jump) const {
270  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
271  if ( pIt && jump > 0 ) {
273  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
275  Context* it = pIt->context();
276  IEvtSelector* sel = s->selector();
277  if ( it && sel ) { // First exploit the current stream
278  // This stream is empty: advance to the next stream
279  sc = sel->previous(*it); // This stream is empty: advance to the next stream
280  if ( !sc.isSuccess() ) {
281  sc = lastOfPreviousStream(true, *pIt);
282  }
283  else {
284  pIt->increaseCounters(false);
285  pIt->set(it, 0);
286  }
287  printEvtInfo(pIt);
288  if ( !sc.isSuccess() ) {
289  return sc;
290  }
291  }
292  pIt->increaseCounters(false);
293  }
294  return sc;
295  }
296  printEvtInfo(pIt);
297  return StatusCode::FAILURE;
298 }
299 
302  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
303  if ( pIt ) {
304  }
305  return StatusCode::FAILURE;
306 }
307 
310  EvtSelectorContext *ctxt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
311  if ( ctxt ) {
312  ctxt->set(0, -1, 0, 0);
313  firstOfNextStream(true, *ctxt);
314  long nskip = m_firstEvent;
315  while( --nskip > 0 ) {
316  StatusCode sc = next(*ctxt);
317  if ( sc.isFailure() ) {
318  MsgStream log(msgSvc(), name());
319  log << MSG::ERROR << "rewind() failed to start with event number "
320  << m_firstEvent << endmsg;
321  return StatusCode::FAILURE;
322  }
323  }
324  return StatusCode::SUCCESS;
325  }
326  return StatusCode::FAILURE;
327 }
328 
332  IOpaqueAddress*& refpAddr) const
333 {
334  const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(&refCtxt);
335  EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
336  refpAddr = 0;
337  if ( pIt ) {
339  Context* it = pIt->context();
340  IEvtSelector* sel = s->selector();
341  if ( it && sel ) {
342  IOpaqueAddress* pAddr = 0;
343  StatusCode sc = sel->createAddress(*it, pAddr);
344  if ( sc.isSuccess() ) {
345  refpAddr = pAddr;
346  }
347  pIt->set(it, pAddr);
348  return sc;
349  }
350  }
351  return StatusCode::FAILURE;
352 }
353 
354 // Release existing event iteration context
356  const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(refCtxt);
357  EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
358  if ( pIt && pIt->ID() >= 0 && pIt->ID() < (long)m_streamtool->size() ) {
360  Context* it = pIt->context();
361  IEvtSelector* sel = s->selector();
362  if ( it && sel ) {
363  StatusCode sc = sel->releaseContext(it);
364  if ( sc.isSuccess() ) {
365  refCtxt = 0;
366  delete pIt;
367  return sc;
368  }
369  }
370  }
371  if ( pIt ) {
372  delete pIt;
373  }
374  return StatusCode::SUCCESS;
375 }
376 
379  // Initialize base class
380  StatusCode status = Service::initialize();
381  MsgStream logger(msgSvc(), name());
382  if ( !status.isSuccess() ) {
383  logger << MSG::ERROR << "Error initializing base class Service!" << endmsg;
384  return status;
385  }
386  // Get the references to the services that are needed by the ApplicationMgr itself
387  m_incidentSvc = serviceLocator()->service("IncidentSvc");
388  if( !m_incidentSvc.isValid() ) {
389  logger << MSG::FATAL << "Error retrieving IncidentSvc." << endmsg;
390  return StatusCode::FAILURE;
391  }
392  if ( m_evtMax != INT_MAX ) {
393  logger << MSG::ERROR << "EvtMax is an obsolete property of the event selector." << endmsg;
394  logger << MSG::ERROR << "Please set \"ApplicationMgr.EvtMax = " << m_evtMax
395  << ";\" to process the requested number of events." << endmsg;
396  return StatusCode::FAILURE;
397  }
398 
399  m_toolSvc = serviceLocator()->service("ToolSvc");
400  if ( !m_toolSvc.isValid() ) {
401  logger << MSG::ERROR << " Could not locate the Tool Service! " << endmsg;
402  return StatusCode::FAILURE;
403  }
404 
405  status = m_toolSvc->retrieveTool(m_streamManager.c_str(), m_streamtool, this);
406 
407  if( status.isFailure() ) {
408  logger << MSG::ERROR << "Error initializing "
409  << m_streamManager << endmsg;
410  return status;
411  }
412 
413  status = m_streamtool->clear();
414  if( status.isFailure() ) {
415  // Message already printed by the tool
416  return status;
417  }
418 
420 
422 
423  m_streamID = 0;
424 
425  return status;
426 }
427 
428 // Re-initialize
431  MsgStream logger(msgSvc(), name());
432  logger << MSG::ERROR << "Cannot reinitialize: service not in state initialized" << endmsg;
433  return StatusCode::FAILURE;
434  }
435 
437  StatusCode status = m_streamtool->clear();
438  if ( status.isFailure() ) return status;
440  m_reconfigure = true;
442  }
443 
444  return StatusCode::SUCCESS;
445 }
446 
447 //
449 
450  if (msgLevel(MSG::DEBUG)) {
451  MsgStream log(msgSvc(), name());
452  log << MSG::DEBUG << "finalize()" << endmsg;
453  }
454 
455  m_incidentSvc = 0;
456 
457  if (m_streamtool) {
458  if (m_toolSvc.isValid()) {
460  } else {
461  // It should not be possible to get here
463  }
464  m_streamtool = 0;
465  }
466 
467  m_toolSvc = 0;
468 
469  return Service::finalize();
470 }
471 
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
IEvtSelector::Context * context() const
Access "real" iterator.
Definition: EventIterator.h:78
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:26
virtual StatusCode finalizeStream(EventSelectorDataStream *)=0
int m_firstEvent
First event to be processed.
Definition: EventSelector.h:80
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
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
The Event Selector Interface.
Definition: IEvtSelector.h:19
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
IDataStreamTool * m_streamtool
Definition: EventSelector.h:67
IEvtSelector * selector() const
Retrieve event selector object.
long int m_streamID
Definition: EventSelector.h:59
virtual StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const
Create new Opaque address corresponding to the current record.
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
virtual StatusCode rewind(Context &refCtxt) const
Rewind the dataset.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
virtual StatusCode createContext(Context *&refpCtxt) const
Create a new event loop context.
StreamSpecs m_streamSpecsLast
Input stream specifiers (last used)
Definition: EventSelector.h:74
virtual StatusCode addStreams(const StreamSpecs &)=0
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:82
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
virtual StatusCode finalize()
IService implementation: Service finalization.
#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:30
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 StatusCode last(Context &c) const
Access last item in the iteration.
virtual EventSelectorDataStream * getStream(size_type)=0
virtual StatusCode releaseContext(Context *&) const =0
Release the Context object.
StreamSpecs m_streamSpecs
Input stream specifiers (for job options)
Definition: EventSelector.h:72
long numStreamEvent() const
Access counter within stream.
virtual ~EventSelector()
Standard Destructor.
virtual Gaudi::StateMachine::State FSMState() const
Get the current state.
Definition: Service.h:56
virtual StatusCode next(Context &refCtxt) const
Get next iteration item from the event loop context.
virtual StatusCode releaseContext(Context *&refCtxt) const
Release existing event iteration context.
virtual StatusCode getPreviousStream(const EventSelectorDataStream *&, size_type &)=0
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:329
SmartIF< IToolSvc > m_toolSvc
Definition: EventSelector.h:65
virtual unsigned long release()=0
Release Interface instance.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:72
void set(const IEvtSelector *sel, IDataStreamTool::size_type id, IEvtSelector::Context *it, IOpaqueAddress *pA)
Set the address of the iterator.
Definition: EventIterator.h:61
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
Definition of class EventSelectorDataStream.
Base class for all Incidents (computing events).
Definition: Incident.h:16
virtual IDataStreamTool::size_type ID() const
Stream identifier.
virtual StatusCode initialize()
IService implementation: Db event selector override.
string s
Definition: gaudirun.py:244
Templated class to add the standard messaging functionalities.
virtual StatusCode resetCriteria(const std::string &cr, Context &c) const
Will set a new criteria for the selection of the next list of events and will change the state of the...
virtual StatusCode releaseTool(IAlgTool *tool)=0
Release the tool.
IDataStreamTool::size_type increaseCounters(bool reset=false)
Increase counters.
Definition: EventIterator.h:82
virtual StatusCode previous(Context &c) const =0
Fetch the previous event.
Definition of class EventSelector.
Definition: EventSelector.h:53
virtual size_type size()=0
virtual StatusCode previous(Context &refCtxt) const
Get previous iteration item from the event loop context.
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:107
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:212
virtual StatusCode reinitialize()
Service override: Reinitialize service.
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.
list i
Definition: ana.py:128
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:197
const std::string & dbName() const
Retrieve stream dbName.
StatusCode retrieveTool(const std::string &type, T *&tool, const IInterface *parent=0, bool createIf=true)
Retrieve specified tool sub-type with tool dependent part of the name automatically assigned...
Definition: IToolSvc.h:141
void setChecked() const
Ignore the checking code;.
Definition: StatusCode.h:104
const std::string BeginInputFile
a new input file has been started
Definition: Incident.h:77
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:334
MSG::Level msgLevel() const
get the output level from the embedded MsgStream