All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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"
10 #include "GaudiKernel/IToolSvc.h"
16 
17 #include "EventSelector.h"
18 #include "EventIterator.h"
19 #include <climits>
20 
22 
23 // Standard constructor
24 EventSelector::EventSelector(const std::string& name, ISvcLocator* svcloc )
25  : base_class( name, svcloc)
26 {
27  m_incidentSvc = 0;
28  m_toolSvc = 0;
29  m_streamCount = 0;
30  m_firstEvent = 0;
31  m_evtPrintFrequency = 10;
32  m_evtMax = INT_MAX;
33  declareProperty( "Input", m_streamSpecs);
34  declareProperty( "FirstEvent", m_firstEvent);
35  declareProperty( "EvtMax", m_evtMax);
36  declareProperty( "PrintFreq", m_evtPrintFrequency);
37  declareProperty( "StreamManager", m_streamManager="DataStreamTool");
38  m_reconfigure = false;
39 }
40 
41 // Standard destructor
43 {
44 }
45 
47 EventSelector::resetCriteria(const std::string& /* criteria */,
48  Context& /* context */) const
49 {
50  return StatusCode::FAILURE;
51 }
52 
53 // Progress report
55  if ( 0 != iter ) {
56  long count = iter->numEvent();
57  // Print an message every m_evtPrintFrequency events
58  if ( 0 == iter->context() ) {
59  MsgStream log(msgSvc(), name());
60  log << MSG::INFO << "End of event input reached." << endmsg;
61  }
62  else if( iter->numStreamEvent() == -1 ) {
63  // Intial value for this stream
64  }
65  else if( m_evtPrintFrequency != -1 && (count % m_evtPrintFrequency == 0)) {
66  MsgStream log(msgSvc(), name());
67  log << MSG::ALWAYS << "Reading Event record " << count+1
68  << ". Record number within stream " << iter->ID()+1
69  << ": " << iter->numStreamEvent()+1 << endmsg;
70  }
71  }
72  else {
73  MsgStream log(msgSvc(), name());
74  log << MSG::INFO << "End of event input reached." << endmsg;
75  }
76 }
77 
78 // IEvtSelector::first()
82  IDataStreamTool::size_type iter_id = (m_reconfigure) ? 0 : iter.ID()+1;
83  if ( m_reconfigure ) const_cast<EventSelector*>(this)->m_reconfigure = false;
84  if ( shutDown ) {
85  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
87  if ( s->isInitialized() ) {
88  EventSelector* thisPtr = const_cast<EventSelector*>(this);
89  if ( s->selector() && iter.context() ) {
90  Context* ctxt = iter.context();
91  s->selector()->releaseContext(ctxt).ignore();
92  iter.set(0,0);
93  }
94  status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
95  iter.set(0,0);
96  }
97  }
98  }
99 
100  const EventSelectorDataStream* s ;
101  status = m_streamtool->getNextStream( s , iter_id );
102 
103  if ( status.isSuccess() ) {
104 
105  if(s!=NULL) {
106  if ( !s->isInitialized() ) {
107  EventSelector* thisPtr = const_cast<EventSelector*>(this);
108  status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
109  }
110 
111  if ( status.isSuccess() ) {
112  const IEvtSelector* sel = s->selector();
113  if ( sel ) {
114  Context* ctxt = 0;
115  status = sel->createContext(ctxt);
116  if ( status.isSuccess() ) {
117  status = sel->resetCriteria(s->criteria(), *ctxt);
118  if ( status.isSuccess() ) {
119  MsgStream log(msgSvc(), name());
120  iter.set(this, iter_id, ctxt, 0);
121  log << MSG::INFO << *s << endmsg;
123  return StatusCode::SUCCESS;
124  }
125  }
126  }
127  }
129  }
130  }
131 
132  iter.set(this, -1, 0, 0);
133  status.setChecked();
134  //m_incidentSvc->fireIncident(Incident(s->dbName(),IncidentType::FailInputFile));
135  return StatusCode::FAILURE;
136 }
137 
138 // IEvtSelector::first()
142  if ( shutDown ) {
143  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
145  if ( s->isInitialized() ) {
146  EventSelector* thisPtr = const_cast<EventSelector*>(this);
147  if ( s->selector() && iter.context() ) {
148  Context* ctxt = iter.context();
149  s->selector()->releaseContext(ctxt);
150  iter.set(0,0);
151  }
152  status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
153  iter.set(0,0);
154  }
155  }
156  }
157 
158  IDataStreamTool::size_type iter_id = iter.ID()-1;
159  const EventSelectorDataStream* s ;
160  status = m_streamtool->getPreviousStream( s , iter_id );
161 
162  if ( status.isSuccess() ) {
163 
164  if ( !s->isInitialized() ) {
165  EventSelector* thisPtr = const_cast<EventSelector*>(this);
166  status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
167  }
168  if ( status.isSuccess() ) {
169  const IEvtSelector* sel = s->selector();
170  if ( sel ) {
171  Context* ctxt = 0;
172  status = sel->createContext(ctxt);
173  if ( status.isSuccess() ) {
174  status = sel->resetCriteria(s->criteria(), *ctxt);
175  if ( status.isSuccess() ) {
176  MsgStream log(msgSvc(), name());
177  iter.set(this, iter_id, ctxt, 0);
178  log << MSG::INFO << *s << endmsg;
179  return StatusCode::SUCCESS;
180  }
181  }
182  }
183  }
184  }
185 
186  iter.set(this, -1, 0, 0);
187  return StatusCode::FAILURE;
188 }
189 
192 {
193  // Max event is zero. Return begin = end
194  refpCtxt = 0;
195  if ( m_firstEvent < 0 ) {
196  MsgStream log(msgSvc(), name());
197  log << MSG::ERROR << "First Event = " << m_firstEvent << " not valid" << endmsg;
198  log << MSG::ERROR << "It should be > 0 " << endmsg;
199  return StatusCode::FAILURE; // if failure => iterators = end();
200  }
201  EvtSelectorContext* ctxt = new EvtSelectorContext(this);
202  ctxt->set(0, -1, 0, 0);
203  firstOfNextStream(true, *ctxt).ignore();
204  refpCtxt = ctxt;
205  long nskip = m_firstEvent;
206  while( --nskip > 0 ) {
207  StatusCode sc = next(*refpCtxt);
208  if ( sc.isFailure() ) {
209  MsgStream log(msgSvc(), name());
210  log << MSG::ERROR << " createContext() failed to start with event number "
211  << m_firstEvent << endmsg;
212  releaseContext(refpCtxt);
213  refpCtxt = 0;
214  return StatusCode::FAILURE;
215  }
216  }
217  return StatusCode::SUCCESS;
218 }
219 
222  return next(refCtxt, 1);
223 }
224 
226 StatusCode EventSelector::next(Context& refCtxt, int /* jump */ ) const {
227  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
228  if ( pIt ) {
229  if ( pIt->ID() != -1 ) {
231  Context* it = pIt->context();
232  IEvtSelector* sel = s->selector();
233  if ( it && sel ) { // First exploit the current stream
234  StatusCode sc = sel->next(*it); // This stream is empty: advance to the next stream
235  if ( !sc.isSuccess() ) {
237  sc = firstOfNextStream(true, *pIt);
238  if (sc.isSuccess() ) sc = next(*pIt);
239  }
240  else {
241  pIt->increaseCounters(false);
242  pIt->set(it, 0);
243  printEvtInfo(pIt);
244  }
245  return sc;
246  }
247  else if ( m_reconfigure ) {
248  StatusCode sc = firstOfNextStream(false, *pIt);
249  printEvtInfo(pIt);
250  return sc;
251  }
252  }
253  else if ( m_reconfigure ) {
254  StatusCode sc = firstOfNextStream(false, *pIt);
255  printEvtInfo(pIt);
256  return sc;
257  }
258  pIt->increaseCounters(false);
259  }
260  printEvtInfo(pIt);
261  return StatusCode::FAILURE;
262 }
263 
266  return previous(refCtxt, 1);
267 }
268 
270 StatusCode EventSelector::previous(Context& refCtxt, int jump) const {
271  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
272  if ( pIt && jump > 0 ) {
274  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
276  Context* it = pIt->context();
277  IEvtSelector* sel = s->selector();
278  if ( it && sel ) { // First exploit the current stream
279  // This stream is empty: advance to the next stream
280  sc = sel->previous(*it); // This stream is empty: advance to the next stream
281  if ( !sc.isSuccess() ) {
282  sc = lastOfPreviousStream(true, *pIt);
283  }
284  else {
285  pIt->increaseCounters(false);
286  pIt->set(it, 0);
287  }
288  printEvtInfo(pIt);
289  if ( !sc.isSuccess() ) {
290  return sc;
291  }
292  }
293  pIt->increaseCounters(false);
294  }
295  return sc;
296  }
297  printEvtInfo(pIt);
298  return StatusCode::FAILURE;
299 }
300 
303  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
304  if ( pIt ) {
305  }
306  return StatusCode::FAILURE;
307 }
308 
311  EvtSelectorContext *ctxt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
312  if ( ctxt ) {
313  ctxt->set(0, -1, 0, 0);
314  firstOfNextStream(true, *ctxt);
315  long nskip = m_firstEvent;
316  while( --nskip > 0 ) {
317  StatusCode sc = next(*ctxt);
318  if ( sc.isFailure() ) {
319  MsgStream log(msgSvc(), name());
320  log << MSG::ERROR << "rewind() failed to start with event number "
321  << m_firstEvent << endmsg;
322  return StatusCode::FAILURE;
323  }
324  }
325  return StatusCode::SUCCESS;
326  }
327  return StatusCode::FAILURE;
328 }
329 
333  IOpaqueAddress*& refpAddr) const
334 {
335  const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(&refCtxt);
336  EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
337  refpAddr = 0;
338  if ( pIt ) {
340  Context* it = pIt->context();
341  IEvtSelector* sel = s->selector();
342  if ( it && sel ) {
343  IOpaqueAddress* pAddr = 0;
344  StatusCode sc = sel->createAddress(*it, pAddr);
345  if ( sc.isSuccess() ) {
346  refpAddr = pAddr;
347  }
348  pIt->set(it, pAddr);
349  return sc;
350  }
351  }
352  return StatusCode::FAILURE;
353 }
354 
355 // Release existing event iteration context
357  const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(refCtxt);
358  EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
359  if ( pIt && pIt->ID() >= 0 && pIt->ID() < (long)m_streamtool->size() ) {
361  Context* it = pIt->context();
362  IEvtSelector* sel = s->selector();
363  if ( it && sel ) {
364  StatusCode sc = sel->releaseContext(it);
365  if ( sc.isSuccess() ) {
366  refCtxt = 0;
367  delete pIt;
368  return sc;
369  }
370  }
371  }
372  if ( pIt ) {
373  delete pIt;
374  }
375  return StatusCode::SUCCESS;
376 }
377 
380  // Initialize base class
381  StatusCode status = Service::initialize();
382  MsgStream logger(msgSvc(), name());
383  if ( !status.isSuccess() ) {
384  logger << MSG::ERROR << "Error initializing base class Service!" << endmsg;
385  return status;
386  }
387  // Get the references to the services that are needed by the ApplicationMgr itself
388  m_incidentSvc = serviceLocator()->service("IncidentSvc");
389  if( !m_incidentSvc.isValid() ) {
390  logger << MSG::FATAL << "Error retrieving IncidentSvc." << endmsg;
391  return StatusCode::FAILURE;
392  }
393  if ( m_evtMax != INT_MAX ) {
394  logger << MSG::ERROR << "EvtMax is an obsolete property of the event selector." << endmsg;
395  logger << MSG::ERROR << "Please set \"ApplicationMgr.EvtMax = " << m_evtMax
396  << ";\" to process the requested number of events." << endmsg;
397  return StatusCode::FAILURE;
398  }
399 
400  m_toolSvc = serviceLocator()->service("ToolSvc");
401  if ( !m_toolSvc.isValid() ) {
402  logger << MSG::ERROR << " Could not locate the Tool Service! " << endmsg;
403  return StatusCode::FAILURE;
404  }
405 
406  status = m_toolSvc->retrieveTool(m_streamManager.c_str(), m_streamtool, this);
407 
408  if( status.isFailure() ) {
409  logger << MSG::ERROR << "Error initializing "
410  << m_streamManager << endmsg;
411  return status;
412  }
413 
414  status = m_streamtool->clear();
415  if( status.isFailure() ) {
416  // Message already printed by the tool
417  return status;
418  }
419 
421 
423 
424  m_streamID = 0;
425 
426  return status;
427 }
428 
429 // Re-initialize
432  MsgStream logger(msgSvc(), name());
433  logger << MSG::ERROR << "Cannot reinitialize: service not in state initialized" << endmsg;
434  return StatusCode::FAILURE;
435  }
436 
438  StatusCode status = m_streamtool->clear();
439  if ( status.isFailure() ) return status;
441  m_reconfigure = true;
443  }
444 
445  return StatusCode::SUCCESS;
446 }
447 
448 //
450 
451  if (msgLevel(MSG::DEBUG)) {
452  MsgStream log(msgSvc(), name());
453  log << MSG::DEBUG << "finalize()" << endmsg;
454  }
455 
456  m_incidentSvc = 0;
457 
458  if (m_streamtool) {
459  if (m_toolSvc.isValid()) {
460  m_toolSvc->releaseTool(m_streamtool).ignore();
461  } else {
462  // It should not be possible to get here
464  }
465  m_streamtool = 0;
466  }
467 
468  m_toolSvc = 0;
469 
470  return Service::finalize();
471 }
472 
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
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.
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...
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.
GAUDIPS_API Logger & logger()
Return the current logger instance.
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
StreamSpecs m_streamSpecsLast
Input stream specifiers (last used)
Definition: EventSelector.h:74
virtual StatusCode addStreams(const StreamSpecs &)=0
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual StatusCode finalize()
IService implementation: Service finalization.
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:331
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:74
void set(const IEvtSelector *sel, IDataStreamTool::size_type id, IEvtSelector::Context *it, IOpaqueAddress *pA)
Set the address of the iterator.
Definition: EventIterator.h:61
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:210
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...
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:211
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:199
const std::string & dbName() const
Retrieve stream dbName.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
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:336
MSG::Level msgLevel() const
get the output level from the embedded MsgStream