The Gaudi Framework  v32r2 (46d42edc)
EventSelector.cpp
Go to the documentation of this file.
1 // Include files
8 #include "GaudiKernel/IToolSvc.h"
9 #include "GaudiKernel/Incident.h"
10 #include "GaudiKernel/MsgStream.h"
12 #include "GaudiKernel/SmartIF.h"
13 
14 #include "EventIterator.h"
15 #include "EventSelector.h"
16 #include <climits>
17 
19 
20 StatusCode EventSelector::resetCriteria( const std::string& /* criteria */, Context& /* context */ ) const {
21  return StatusCode::FAILURE;
22 }
23 
24 // Progress report
26  if ( iter ) {
27  long count = iter->numEvent();
28  // Print an message every m_evtPrintFrequency events
29  if ( 0 == iter->context() ) {
30  info() << "End of event input reached." << endmsg;
31  } else if ( iter->numStreamEvent() == -1 ) {
32  // Intial value for this stream
33  } else if ( m_evtPrintFrequency != -1 && ( count % m_evtPrintFrequency == 0 ) ) {
34  always() << "Reading Event record " << count + 1 << ". Record number within stream " << iter->ID() + 1 << ": "
35  << iter->numStreamEvent() + 1 << endmsg;
36  }
37  } else {
38  info() << "End of event input reached." << endmsg;
39  }
40 }
41 
42 // IEvtSelector::first()
45  IDataStreamTool::size_type iter_id = ( m_reconfigure ) ? 0 : iter.ID() + 1;
46  if ( m_reconfigure ) const_cast<EventSelector*>( this )->m_reconfigure = false;
47  if ( shutDown ) {
48  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
50  if ( s->isInitialized() ) {
51  EventSelector* thisPtr = const_cast<EventSelector*>( this );
52  if ( s->selector() && iter.context() ) {
53  Context* ctxt = iter.context();
54  s->selector()->releaseContext( ctxt ).ignore();
55  iter.set( 0, 0 );
56  }
57  status = thisPtr->m_streamtool->finalizeStream( const_cast<EventSelectorDataStream*>( s ) );
58  iter.set( 0, 0 );
59  }
60  }
61  }
62 
63  const EventSelectorDataStream* s = nullptr;
64  status = m_streamtool->getNextStream( s, iter_id );
65 
66  if ( status.isSuccess() ) {
67 
68  if ( s ) {
69  if ( !s->isInitialized() ) {
70  EventSelector* thisPtr = const_cast<EventSelector*>( this );
71  status = thisPtr->m_streamtool->initializeStream( const_cast<EventSelectorDataStream*>( s ) );
72  }
73 
74  if ( status.isSuccess() ) {
75  const IEvtSelector* sel = s->selector();
76  if ( sel ) {
77  Context* ctxt = nullptr;
78  status = sel->createContext( ctxt );
79  if ( status.isSuccess() ) {
80  status = sel->resetCriteria( s->criteria(), *ctxt );
81  if ( status.isSuccess() ) {
82  iter.set( this, iter_id, ctxt, 0 );
83  info() << *s << endmsg;
84  m_incidentSvc->fireIncident( Incident( s->dbName(), IncidentType::BeginInputFile ) );
85  return StatusCode::SUCCESS;
86  }
87  }
88  delete ctxt;
89  }
90  }
91  m_incidentSvc->fireIncident( Incident( s->dbName(), IncidentType::FailInputFile ) );
92  }
93  }
94 
95  iter.set( this, -1, 0, 0 );
96  status.setChecked();
97  // m_incidentSvc->fireIncident(Incident(s->dbName(),IncidentType::FailInputFile));
98  return StatusCode::FAILURE;
99 }
100 
101 // IEvtSelector::first()
104  if ( shutDown ) {
105  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
106  const EventSelectorDataStream* s = m_streamtool->getStream( iter.ID() );
107  if ( s->isInitialized() ) {
108  EventSelector* thisPtr = const_cast<EventSelector*>( this );
109  if ( s->selector() && iter.context() ) {
110  Context* ctxt = iter.context();
111  s->selector()->releaseContext( ctxt );
112  iter.set( 0, 0 );
113  }
114  status = thisPtr->m_streamtool->finalizeStream( const_cast<EventSelectorDataStream*>( s ) );
115  iter.set( 0, 0 );
116  }
117  }
118  }
119 
120  IDataStreamTool::size_type iter_id = iter.ID() - 1;
121  const EventSelectorDataStream* s = nullptr;
122  status = m_streamtool->getPreviousStream( s, iter_id );
123 
124  if ( status.isSuccess() ) {
125 
126  if ( !s->isInitialized() ) {
127  EventSelector* thisPtr = const_cast<EventSelector*>( this );
128  status = thisPtr->m_streamtool->initializeStream( const_cast<EventSelectorDataStream*>( s ) );
129  }
130  if ( status.isSuccess() ) {
131  const IEvtSelector* sel = s->selector();
132  if ( sel ) {
133  Context* ctxt = nullptr;
134  status = sel->createContext( ctxt );
135  if ( status.isSuccess() ) {
136  status = sel->resetCriteria( s->criteria(), *ctxt );
137  if ( status.isSuccess() ) {
138  iter.set( this, iter_id, ctxt, 0 );
139  info() << *s << endmsg;
140  return StatusCode::SUCCESS;
141  }
142  }
143  }
144  }
145  }
146 
147  iter.set( this, -1, 0, 0 );
148  return StatusCode::FAILURE;
149 }
150 
152 StatusCode EventSelector::createContext( Context*& refpCtxt ) const {
153  // Max event is zero. Return begin = end
154  refpCtxt = nullptr;
155  if ( m_firstEvent < 0 ) {
156  error() << "First Event = " << m_firstEvent << " not valid" << endmsg;
157  error() << "It should be > 0 " << endmsg;
158  return StatusCode::FAILURE; // if failure => iterators = end();
159  }
160  auto ctxt = new EvtSelectorContext( this );
161  refpCtxt = ctxt;
162  ctxt->set( 0, -1, 0, 0 );
163  firstOfNextStream( true, *ctxt ).ignore();
164  long nskip = m_firstEvent;
165  while ( --nskip > 0 ) {
166  StatusCode sc = next( *refpCtxt );
167  if ( sc.isFailure() ) {
168  error() << " createContext() failed to start with event number " << m_firstEvent << endmsg;
169  releaseContext( refpCtxt );
170  refpCtxt = nullptr;
171  return StatusCode::FAILURE;
172  }
173  }
174  return StatusCode::SUCCESS;
175 }
176 
178 StatusCode EventSelector::next( Context& refCtxt ) const { return next( refCtxt, 1 ); }
179 
181 StatusCode EventSelector::next( Context& refCtxt, int /* jump */ ) const {
182  EvtSelectorContext* pIt = dynamic_cast<EvtSelectorContext*>( &refCtxt );
183  if ( pIt ) {
184  if ( pIt->ID() != -1 ) {
185  const EventSelectorDataStream* s = m_streamtool->getStream( pIt->ID() );
186  Context* it = pIt->context();
187  IEvtSelector* sel = s->selector();
188  if ( it && sel ) { // First exploit the current stream
189  StatusCode sc = sel->next( *it ); // This stream is empty: advance to the next stream
190  if ( !sc.isSuccess() ) {
191  m_incidentSvc->fireIncident( Incident( s->dbName(), IncidentType::EndInputFile ) );
192  sc = firstOfNextStream( true, *pIt );
193  if ( sc.isSuccess() ) sc = next( *pIt );
194  } else {
195  pIt->increaseCounters( false );
196  pIt->set( it, 0 );
197  printEvtInfo( pIt );
198  }
199  return sc;
200  } else if ( m_reconfigure ) {
201  StatusCode sc = firstOfNextStream( false, *pIt );
202  printEvtInfo( pIt );
203  return sc;
204  }
205  } else if ( m_reconfigure ) {
206  StatusCode sc = firstOfNextStream( false, *pIt );
207  printEvtInfo( pIt );
208  return sc;
209  }
210  pIt->increaseCounters( false );
211  }
212  printEvtInfo( pIt );
213  return StatusCode::FAILURE;
214 }
215 
217 StatusCode EventSelector::previous( Context& refCtxt ) const { return previous( refCtxt, 1 ); }
218 
220 StatusCode EventSelector::previous( Context& refCtxt, int jump ) const {
221  EvtSelectorContext* pIt = dynamic_cast<EvtSelectorContext*>( &refCtxt );
222  if ( pIt && jump > 0 ) {
224  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
225  const EventSelectorDataStream* s = m_streamtool->getStream( pIt->ID() );
226  Context* it = pIt->context();
227  IEvtSelector* sel = s->selector();
228  if ( it && sel ) { // First exploit the current stream
229  // This stream is empty: advance to the next stream
230  sc = sel->previous( *it ); // This stream is empty: advance to the next stream
231  if ( !sc.isSuccess() ) {
232  sc = lastOfPreviousStream( true, *pIt );
233  } else {
234  pIt->increaseCounters( false );
235  pIt->set( it, 0 );
236  }
237  printEvtInfo( pIt );
238  if ( !sc.isSuccess() ) { return sc; }
239  }
240  pIt->increaseCounters( false );
241  }
242  return sc;
243  }
244  printEvtInfo( pIt );
245  return StatusCode::FAILURE;
246 }
247 
249 StatusCode EventSelector::last( Context& refCtxt ) const {
250  EvtSelectorContext* pIt = dynamic_cast<EvtSelectorContext*>( &refCtxt );
251  if ( pIt ) {}
252  return StatusCode::FAILURE;
253 }
254 
256 StatusCode EventSelector::rewind( Context& refCtxt ) const {
257  EvtSelectorContext* ctxt = dynamic_cast<EvtSelectorContext*>( &refCtxt );
258  if ( ctxt ) {
259  ctxt->set( 0, -1, 0, 0 );
260  firstOfNextStream( true, *ctxt );
261  long nskip = m_firstEvent;
262  while ( --nskip > 0 ) {
263  StatusCode sc = next( *ctxt );
264  if ( sc.isFailure() ) {
265  error() << "rewind() failed to start with event number " << m_firstEvent << endmsg;
266  return StatusCode::FAILURE;
267  }
268  }
269  return StatusCode::SUCCESS;
270  }
271  return StatusCode::FAILURE;
272 }
273 
275 StatusCode EventSelector::createAddress( const Context& refCtxt, IOpaqueAddress*& refpAddr ) const {
276  auto cpIt = dynamic_cast<const EvtSelectorContext*>( &refCtxt );
277  auto pIt = const_cast<EvtSelectorContext*>( cpIt );
278  refpAddr = nullptr;
280  if ( pIt ) {
281  auto s = m_streamtool->getStream( pIt->ID() );
282  auto it = pIt->context();
283  auto sel = s->selector();
284  if ( it && sel ) {
285  IOpaqueAddress* pAddr = nullptr;
286  sc = sel->createAddress( *it, pAddr );
287  if ( sc.isSuccess() ) { refpAddr = pAddr; }
288  pIt->set( it, pAddr );
289  }
290  }
291  return sc;
292 }
293 
294 // Release existing event iteration context
295 StatusCode EventSelector::releaseContext( Context*& refCtxt ) const {
297  auto cpIt = dynamic_cast<const EvtSelectorContext*>( refCtxt );
298  std::unique_ptr<EvtSelectorContext> pIt{const_cast<EvtSelectorContext*>( cpIt )};
299  if ( pIt && pIt->ID() >= 0 && pIt->ID() < (long)m_streamtool->size() ) {
300  const auto s = m_streamtool->getStream( pIt->ID() );
301  auto it = pIt->context();
302  auto sel = s->selector();
303  if ( it && sel ) { sc = sel->releaseContext( it ); }
304  }
305  refCtxt = nullptr; // std::unique_ptr always deletes object, so always set to NULL
306  return sc;
307 }
308 
311  // Initialize base class
312  StatusCode status = Service::initialize();
313  if ( !status.isSuccess() ) {
314  error() << "Error initializing base class Service!" << endmsg;
315  return status;
316  }
317  // Get the references to the services that are needed by the ApplicationMgr itself
318  m_incidentSvc = serviceLocator()->service( "IncidentSvc" );
319  if ( !m_incidentSvc ) {
320  fatal() << "Error retrieving IncidentSvc." << endmsg;
321  return StatusCode::FAILURE;
322  }
323  if ( m_evtMax != INT_MAX ) {
324  error() << "EvtMax is an obsolete property of the event selector." << endmsg;
325  error() << "Please set \"ApplicationMgr.EvtMax = " << m_evtMax << ";\" to process the requested number of events."
326  << endmsg;
327  return StatusCode::FAILURE;
328  }
329 
330  m_toolSvc = serviceLocator()->service( "ToolSvc" );
331  if ( !m_toolSvc ) {
332  error() << " Could not locate the Tool Service! " << endmsg;
333  return StatusCode::FAILURE;
334  }
335  // make sure we finalize _prior_ to ToolSvc... we are about to get a
336  // a pointer to a tool which gets finalized and released by the ToolSvc
337  // during ToolSvc::finalize, and we don't want dangling pointers...
339  auto prio = mgr->getPriority( "ToolSvc" );
340  mgr->setPriority( name(), prio + 1 ).ignore();
341 
343 
344  if ( status.isFailure() ) {
345  error() << "Error initializing " << m_streamManager << endmsg;
346  return status;
347  }
348 
349  status = m_streamtool->clear();
350  if ( status.isFailure() ) {
351  // Message already printed by the tool
352  return status;
353  }
354 
356 
358 
359  m_streamID = 0;
360 
361  return status;
362 }
363 
364 // Re-initialize
367  error() << "Cannot reinitialize: service not in state initialized" << endmsg;
368  return StatusCode::FAILURE;
369  }
370 
371  if ( m_streamSpecsLast != m_streamSpecs ) {
372  StatusCode status = m_streamtool->clear();
373  if ( status.isFailure() ) return status;
375  m_reconfigure = true;
377  }
378 
379  return StatusCode::SUCCESS;
380 }
381 
382 //
384 
385  if ( msgLevel( MSG::DEBUG ) ) { debug() << "finalize()" << endmsg; }
386 
387  m_incidentSvc = nullptr;
388 
389  if ( m_streamtool ) {
390  if ( m_toolSvc ) {
392  } else {
393  // It should not be possible to get here
395  }
396  m_streamtool = nullptr;
397  }
398  m_toolSvc.reset();
399 
400  return Service::finalize();
401 }
SmartIF< IIncidentSvc > m_incidentSvc
Reference to the indicent service.
Definition: EventSelector.h:63
StatusCode initialize() override
Definition: Service.cpp:60
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:277
virtual StatusCode finalizeStream(EventSelectorDataStream *)=0
StatusCode finalize() override
Definition: Service.cpp:164
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:52
IEvtSelector::Context * context() const
Access "real" iterator.
Definition: EventIterator.h:70
The Event Selector Interface.
Definition: IEvtSelector.h:18
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
IDataStreamTool * m_streamtool
Definition: EventSelector.h:67
sel
Definition: IOTest.py:94
long int m_streamID
Definition: EventSelector.h:59
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
STL namespace.
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
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:138
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
StreamSpecs m_streamSpecsLast
Input stream specifiers (last used)
Definition: EventSelector.h:72
#define DECLARE_COMPONENT(type)
virtual StatusCode addStreams(const StreamSpecs &)=0
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Gaudi::Property< int > m_evtPrintFrequency
Definition: EventSelector.h:82
virtual void fireIncident(const Incident &incident)=0
Fire an Incident.
Gaudi::Property< std::string > m_streamManager
Definition: EventSelector.h:83
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.
virtual StatusCode clear()=0
Definition of class EventIterator.
Definition: EventIterator.h:32
long numEvent() const
Access counter.
Definition: EventIterator.h:91
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
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...
virtual void printEvtInfo(const EvtSelectorContext *iter) const
Progress report.
virtual EventSelectorDataStream * getStream(size_type)=0
Gaudi::Property< int > m_evtMax
Definition: EventSelector.h:81
StatusCode firstOfNextStream(bool shutDown, EvtSelectorContext &it) const
Retrieve first entry of the next data stream.
StatusCode initialize() override
IService implementation: Db event selector override.
Gaudi::Property< StreamSpecs > m_streamSpecs
Definition: EventSelector.h:79
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Gaudi::Property< int > m_firstEvent
Definition: EventSelector.h:80
bool isSuccess() const
Definition: StatusCode.h:267
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
StatusCode lastOfPreviousStream(bool shutDown, EvtSelectorContext &it) const
Retrieve last entry of the previous data stream.
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
virtual unsigned long release()=0
Release Interface instance.
virtual int getPriority(const std::string &name) const =0
STL class.
void set(const IEvtSelector *sel, IDataStreamTool::size_type id, IEvtSelector::Context *it, IOpaqueAddress *pA)
Set the address of the iterator.
Definition: EventIterator.h:55
Definition of class EventSelectorDataStream.
Base class for all Incidents (computing events).
Definition: Incident.h:17
string s
Definition: gaudirun.py:318
constexpr static const auto FAILURE
Definition: StatusCode.h:86
virtual IDataStreamTool::size_type ID() const
Stream identifier.
Definition: EventIterator.h:89
long numStreamEvent() const
Access counter within stream.
Definition: EventIterator.h:93
virtual StatusCode releaseTool(IAlgTool *tool)=0
Release the tool.
IDataStreamTool::size_type increaseCounters(bool reset=false)
Increase counters.
Definition: EventIterator.h:72
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:86
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
bool isFailure() const
Definition: StatusCode.h:130
bool m_reconfigure
Reconfigure occurred.
Definition: EventSelector.h:70
Opaque address interface definition.
StatusCode last(Context &c) const override
Access last item in the iteration.
virtual StatusCode getNextStream(const EventSelectorDataStream *&, size_type &)=0
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
const StatusCode & setChecked(bool checked=true) const
Check/uncheck StatusCode.
Definition: StatusCode.h:143
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192