Gaudi Framework, version v23r6

Home   Generated: Wed Jan 30 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EventSelector.cpp
Go to the documentation of this file.
1 // $Id: EventSelector.cpp,v 1.46 2008/10/30 18:01:03 marcocle Exp $
2 
3 // Include files
4 #include "GaudiKernel/xtoa.h"
5 #include "GaudiKernel/SmartIF.h"
6 #include "GaudiKernel/Incident.h"
13 #include "GaudiKernel/IToolSvc.h"
19 
20 #include "EventSelector.h"
21 #include "EventIterator.h"
22 #include <climits>
23 
25 
26 // Standard constructor
27 EventSelector::EventSelector(const std::string& name, ISvcLocator* svcloc )
28  : base_class( name, svcloc)
29 {
30  m_incidentSvc = 0;
31  m_toolSvc = 0;
32  m_streamCount = 0;
33  m_firstEvent = 0;
34  m_evtPrintFrequency = 10;
35  m_evtMax = INT_MAX;
36  declareProperty( "Input", m_streamSpecs);
37  declareProperty( "FirstEvent", m_firstEvent);
38  declareProperty( "EvtMax", m_evtMax);
39  declareProperty( "PrintFreq", m_evtPrintFrequency);
40  declareProperty( "StreamManager", m_streamManager="DataStreamTool");
41  m_reconfigure = false;
42 }
43 
44 // Standard destructor
46 {
47 }
48 
51  Context& /* context */) const
52 {
53  return StatusCode::FAILURE;
54 }
55 
56 // Progress report
58  if ( 0 != iter ) {
59  long count = iter->numEvent();
60  // Print an message every m_evtPrintFrequency events
61  if ( 0 == iter->context() ) {
62  MsgStream log(msgSvc(), name());
63  log << MSG::INFO << "End of event input reached." << endmsg;
64  }
65  else if( iter->numStreamEvent() == -1 ) {
66  // Intial value for this stream
67  }
68  else if( m_evtPrintFrequency != -1 && (count % m_evtPrintFrequency == 0)) {
69  MsgStream log(msgSvc(), name());
70  log << MSG::ALWAYS << "Reading Event record " << count+1
71  << ". Record number within stream " << iter->ID()+1
72  << ": " << iter->numStreamEvent()+1 << endmsg;
73  }
74  }
75  else {
76  MsgStream log(msgSvc(), name());
77  log << MSG::INFO << "End of event input reached." << endmsg;
78  }
79 }
80 
81 // IEvtSelector::first()
85  IDataStreamTool::size_type iter_id = (m_reconfigure) ? 0 : iter.ID()+1;
86  if ( m_reconfigure ) const_cast<EventSelector*>(this)->m_reconfigure = false;
87  if ( shutDown ) {
88  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
90  if ( s->isInitialized() ) {
91  EventSelector* thisPtr = const_cast<EventSelector*>(this);
92  if ( s->selector() && iter.context() ) {
93  Context* ctxt = iter.context();
94  s->selector()->releaseContext(ctxt).ignore();
95  iter.set(0,0);
96  }
97  status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
98  iter.set(0,0);
99  }
100  }
101  }
102 
103  const EventSelectorDataStream* s ;
104  status = m_streamtool->getNextStream( s , iter_id );
105 
106  if ( status.isSuccess() ) {
107 
108  if ( !s->isInitialized() ) {
109  EventSelector* thisPtr = const_cast<EventSelector*>(this);
110  status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
111  }
112 
113  if ( status.isSuccess() ) {
114  const IEvtSelector* sel = s->selector();
115  if ( sel ) {
116  Context* ctxt = 0;
117  status = sel->createContext(ctxt);
118  if ( status.isSuccess() ) {
119  status = sel->resetCriteria(s->criteria(), *ctxt);
120  if ( status.isSuccess() ) {
121  MsgStream log(msgSvc(), name());
122  iter.set(this, iter_id, ctxt, 0);
123  log << MSG::INFO << *s << endmsg;
125  return StatusCode::SUCCESS;
126  }
127  }
128  }
129  }
130  if(s!=NULL) {
132  }
133  }
134 
135  iter.set(this, -1, 0, 0);
136  status.setChecked();
137  //m_incidentSvc->fireIncident(Incident(s->dbName(),IncidentType::FailInputFile));
138  return StatusCode::FAILURE;
139 }
140 
141 // IEvtSelector::first()
145  if ( shutDown ) {
146  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
148  if ( s->isInitialized() ) {
149  EventSelector* thisPtr = const_cast<EventSelector*>(this);
150  if ( s->selector() && iter.context() ) {
151  Context* ctxt = iter.context();
152  s->selector()->releaseContext(ctxt);
153  iter.set(0,0);
154  }
155  status = thisPtr->m_streamtool->finalizeStream(const_cast<EventSelectorDataStream*>(s));
156  iter.set(0,0);
157  }
158  }
159  }
160 
161  IDataStreamTool::size_type iter_id = iter.ID()-1;
162  const EventSelectorDataStream* s ;
163  status = m_streamtool->getPreviousStream( s , iter_id );
164 
165  if ( status.isSuccess() ) {
166 
167  if ( !s->isInitialized() ) {
168  EventSelector* thisPtr = const_cast<EventSelector*>(this);
169  status = thisPtr->m_streamtool->initializeStream(const_cast<EventSelectorDataStream*>(s));
170  }
171  if ( status.isSuccess() ) {
172  const IEvtSelector* sel = s->selector();
173  if ( sel ) {
174  Context* ctxt = 0;
175  status = sel->createContext(ctxt);
176  if ( status.isSuccess() ) {
177  status = sel->resetCriteria(s->criteria(), *ctxt);
178  if ( status.isSuccess() ) {
179  MsgStream log(msgSvc(), name());
180  iter.set(this, iter_id, ctxt, 0);
181  log << MSG::INFO << *s << endmsg;
182  return StatusCode::SUCCESS;
183  }
184  }
185  }
186  }
187  }
188 
189  iter.set(this, -1, 0, 0);
190  return StatusCode::FAILURE;
191 }
192 
195 {
196  // Max event is zero. Return begin = end
197  refpCtxt = 0;
198  if ( m_firstEvent < 0 ) {
199  MsgStream log(msgSvc(), name());
200  log << MSG::ERROR << "First Event = " << m_firstEvent << " not valid" << endmsg;
201  log << MSG::ERROR << "It should be > 0 " << endmsg;
202  return StatusCode::FAILURE; // if failure => iterators = end();
203  }
204  EvtSelectorContext* ctxt = new EvtSelectorContext(this);
205  ctxt->set(0, -1, 0, 0);
206  firstOfNextStream(true, *ctxt).ignore();
207  refpCtxt = ctxt;
208  long nskip = m_firstEvent;
209  while( --nskip > 0 ) {
210  StatusCode sc = next(*refpCtxt);
211  if ( sc.isFailure() ) {
212  MsgStream log(msgSvc(), name());
213  log << MSG::ERROR << " createContext() failed to start with event number "
214  << m_firstEvent << endmsg;
215  releaseContext(refpCtxt);
216  refpCtxt = 0;
217  return StatusCode::FAILURE;
218  }
219  }
220  return StatusCode::SUCCESS;
221 }
222 
225  return next(refCtxt, 1);
226 }
227 
229 StatusCode EventSelector::next(Context& refCtxt, int /* jump */ ) const {
230  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
231  if ( pIt ) {
232  if ( pIt->ID() != -1 ) {
234  Context* it = pIt->context();
235  IEvtSelector* sel = s->selector();
236  if ( it && sel ) { // First exploit the current stream
237  StatusCode sc = sel->next(*it); // This stream is empty: advance to the next stream
238  if ( !sc.isSuccess() ) {
240  sc = firstOfNextStream(true, *pIt);
241  if (sc.isSuccess() ) sc = next(*pIt);
242  }
243  else {
244  pIt->increaseCounters(false);
245  pIt->set(it, 0);
246  printEvtInfo(pIt);
247  }
248  return sc;
249  }
250  else if ( m_reconfigure ) {
251  StatusCode sc = firstOfNextStream(false, *pIt);
252  printEvtInfo(pIt);
253  return sc;
254  }
255  }
256  else if ( m_reconfigure ) {
257  StatusCode sc = firstOfNextStream(false, *pIt);
258  printEvtInfo(pIt);
259  return sc;
260  }
261  pIt->increaseCounters(false);
262  }
263  printEvtInfo(pIt);
264  return StatusCode::FAILURE;
265 }
266 
269  return previous(refCtxt, 1);
270 }
271 
273 StatusCode EventSelector::previous(Context& refCtxt, int jump) const {
274  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
275  if ( pIt && jump > 0 ) {
277  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
279  Context* it = pIt->context();
280  IEvtSelector* sel = s->selector();
281  if ( it && sel ) { // First exploit the current stream
282  // This stream is empty: advance to the next stream
283  sc = sel->previous(*it); // This stream is empty: advance to the next stream
284  if ( !sc.isSuccess() ) {
285  sc = lastOfPreviousStream(true, *pIt);
286  }
287  else {
288  pIt->increaseCounters(false);
289  pIt->set(it, 0);
290  }
291  printEvtInfo(pIt);
292  if ( !sc.isSuccess() ) {
293  return sc;
294  }
295  }
296  pIt->increaseCounters(false);
297  }
298  return sc;
299  }
300  printEvtInfo(pIt);
301  return StatusCode::FAILURE;
302 }
303 
306  EvtSelectorContext *pIt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
307  if ( pIt ) {
308  }
309  return StatusCode::FAILURE;
310 }
311 
314  EvtSelectorContext *ctxt = dynamic_cast<EvtSelectorContext*>(&refCtxt);
315  if ( ctxt ) {
316  ctxt->set(0, -1, 0, 0);
317  firstOfNextStream(true, *ctxt);
318  long nskip = m_firstEvent;
319  while( --nskip > 0 ) {
320  StatusCode sc = next(*ctxt);
321  if ( sc.isFailure() ) {
322  MsgStream log(msgSvc(), name());
323  log << MSG::ERROR << "rewind() failed to start with event number "
324  << m_firstEvent << endmsg;
325  return StatusCode::FAILURE;
326  }
327  }
328  return StatusCode::SUCCESS;
329  }
330  return StatusCode::FAILURE;
331 }
332 
336  IOpaqueAddress*& refpAddr) const
337 {
338  const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(&refCtxt);
339  EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
340  refpAddr = 0;
341  if ( pIt ) {
343  Context* it = pIt->context();
344  IEvtSelector* sel = s->selector();
345  if ( it && sel ) {
346  IOpaqueAddress* pAddr = 0;
347  StatusCode sc = sel->createAddress(*it, pAddr);
348  if ( sc.isSuccess() ) {
349  refpAddr = pAddr;
350  }
351  pIt->set(it, pAddr);
352  return sc;
353  }
354  }
355  return StatusCode::FAILURE;
356 }
357 
358 // Release existing event iteration context
360  const EvtSelectorContext *cpIt = dynamic_cast<const EvtSelectorContext*>(refCtxt);
361  EvtSelectorContext *pIt = const_cast<EvtSelectorContext*>(cpIt);
362  if ( pIt && pIt->ID() >= 0 && pIt->ID() < (long)m_streamtool->size() ) {
364  Context* it = pIt->context();
365  IEvtSelector* sel = s->selector();
366  if ( it && sel ) {
367  StatusCode sc = sel->releaseContext(it);
368  if ( sc.isSuccess() ) {
369  refCtxt = 0;
370  delete pIt;
371  return sc;
372  }
373  }
374  }
375  if ( pIt ) {
376  delete pIt;
377  }
378  return StatusCode::SUCCESS;
379 }
380 
383  // Initialize base class
384  StatusCode status = Service::initialize();
385  MsgStream logger(msgSvc(), name());
386  if ( !status.isSuccess() ) {
387  logger << MSG::ERROR << "Error initializing base class Service!" << endmsg;
388  return status;
389  }
390  // Get the references to the services that are needed by the ApplicationMgr itself
391  m_incidentSvc = serviceLocator()->service("IncidentSvc");
392  if( !m_incidentSvc.isValid() ) {
393  logger << MSG::FATAL << "Error retrieving IncidentSvc." << endmsg;
394  return StatusCode::FAILURE;
395  }
396  if ( m_evtMax != INT_MAX ) {
397  logger << MSG::ERROR << "EvtMax is an obsolete property of the event selector." << endmsg;
398  logger << MSG::ERROR << "Please set \"ApplicationMgr.EvtMax = " << m_evtMax
399  << ";\" to process the requested number of events." << endmsg;
400  return StatusCode::FAILURE;
401  }
402 
403  m_toolSvc = serviceLocator()->service("ToolSvc");
404  if ( !m_toolSvc.isValid() ) {
405  logger << MSG::ERROR << " Could not locate the Tool Service! " << endmsg;
406  return StatusCode::FAILURE;
407  }
408 
409  status = m_toolSvc->retrieveTool(m_streamManager.c_str(), m_streamtool, this);
410 
411  if( status.isFailure() ) {
412  logger << MSG::ERROR << "Error initializing "
413  << m_streamManager << endmsg;
414  return status;
415  }
416 
417  status = m_streamtool->clear();
418  if( status.isFailure() ) {
419  // Message already printed by the tool
420  return status;
421  }
422 
424 
426 
427  m_streamID = 0;
428 
429  return status;
430 }
431 
432 // Re-initialize
435  MsgStream logger(msgSvc(), name());
436  logger << MSG::ERROR << "Cannot reinitialize: service not in state initialized" << endmsg;
437  return StatusCode::FAILURE;
438  }
439 
441  StatusCode status = m_streamtool->clear();
442  if ( status.isFailure() ) return status;
444  m_reconfigure = true;
446  }
447 
448  return StatusCode::SUCCESS;
449 }
450 
451 //
453 
454  if (msgLevel(MSG::DEBUG)) {
455  MsgStream log(msgSvc(), name());
456  log << MSG::DEBUG << "finalize()" << endmsg;
457  }
458 
459  m_incidentSvc = 0;
460 
461  if (m_streamtool) {
462  if (m_toolSvc.isValid()) {
463  m_toolSvc->releaseTool(m_streamtool).ignore();
464  } else {
465  // It should not be possible to get here
467  }
468  m_streamtool = 0;
469  }
470 
471  m_toolSvc = 0;
472 
473  return Service::finalize();
474 }
475 

Generated at Wed Jan 30 2013 17:13:38 for Gaudi Framework, version v23r6 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004