Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
EventSelector.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "EventSelector.h"
12 #include "EventIterator.h"
19 #include <GaudiKernel/IToolSvc.h>
20 #include <GaudiKernel/Incident.h>
21 #include <GaudiKernel/MsgStream.h>
23 #include <GaudiKernel/SmartIF.h>
24 #include <climits>
25 
27 
28 StatusCode EventSelector::resetCriteria( const std::string& /* criteria */, Context& /* context */ ) const {
29  return StatusCode::FAILURE;
30 }
31 
32 // Progress report
34  if ( iter ) {
35  long count = iter->numEvent();
36  // Print an message every m_evtPrintFrequency events
37  if ( 0 == iter->context() ) {
38  info() << "End of event input reached." << endmsg;
39  } else if ( iter->numStreamEvent() == -1 ) {
40  // Intial value for this stream
41  } else if ( m_evtPrintFrequency > 0 && ( count % m_evtPrintFrequency == 0 ) ) {
42  always() << "Reading Event record " << count + 1 << ". Record number within stream " << iter->ID() + 1 << ": "
43  << iter->numStreamEvent() + 1 << endmsg;
44  }
45  } else {
46  info() << "End of event input reached." << endmsg;
47  }
48 }
49 
50 // IEvtSelector::first()
53  IDataStreamTool::size_type iter_id = ( m_reconfigure ) ? 0 : iter.ID() + 1;
54  if ( m_reconfigure ) const_cast<EventSelector*>( this )->m_reconfigure = false;
55  if ( shutDown ) {
56  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
58  if ( s->isInitialized() ) {
59  EventSelector* thisPtr = const_cast<EventSelector*>( this );
60  if ( s->selector() && iter.context() ) {
61  Context* ctxt = iter.context();
62  s->selector()->releaseContext( ctxt ).ignore();
63  iter.set( 0, 0 );
64  }
65  status = thisPtr->m_streamtool->finalizeStream( const_cast<EventSelectorDataStream*>( s ) );
66  iter.set( 0, 0 );
67  }
68  }
69  }
70 
71  const EventSelectorDataStream* s = nullptr;
72  status = m_streamtool->getNextStream( s, iter_id );
73 
74  if ( status.isSuccess() ) {
75 
76  if ( s ) {
77  if ( !s->isInitialized() ) {
78  EventSelector* thisPtr = const_cast<EventSelector*>( this );
79  status = thisPtr->m_streamtool->initializeStream( const_cast<EventSelectorDataStream*>( s ) );
80  }
81 
82  if ( status.isSuccess() ) {
83  const IEvtSelector* sel = s->selector();
84  if ( sel ) {
85  Context* ctxt = nullptr;
86  status = sel->createContext( ctxt );
87  if ( status.isSuccess() ) {
88  status = sel->resetCriteria( s->criteria(), *ctxt );
89  if ( status.isSuccess() ) {
90  iter.set( this, iter_id, ctxt, 0 );
91  info() << *s << endmsg;
92  m_incidentSvc->fireIncident( Incident( s->dbName(), IncidentType::BeginInputFile ) );
93  return StatusCode::SUCCESS;
94  }
95  }
96  delete ctxt;
97  }
98  }
99  m_incidentSvc->fireIncident( Incident( s->dbName(), IncidentType::FailInputFile ) );
100  }
101  }
102 
103  iter.set( this, -1, 0, 0 );
104  // m_incidentSvc->fireIncident(Incident(s->dbName(),IncidentType::FailInputFile));
105  return StatusCode::FAILURE;
106 }
107 
108 // IEvtSelector::first()
111  if ( shutDown ) {
112  if ( iter.ID() >= 0 && iter.ID() < (long)m_streamtool->size() ) {
113  const EventSelectorDataStream* s = m_streamtool->getStream( iter.ID() );
114  if ( s->isInitialized() ) {
115  EventSelector* thisPtr = const_cast<EventSelector*>( this );
116  if ( s->selector() && iter.context() ) {
117  Context* ctxt = iter.context();
118  if ( status = s->selector()->releaseContext( ctxt ); !status ) return status;
119  iter.set( 0, 0 );
120  }
121  status = thisPtr->m_streamtool->finalizeStream( const_cast<EventSelectorDataStream*>( s ) );
122  iter.set( 0, 0 );
123  }
124  }
125  }
126 
127  IDataStreamTool::size_type iter_id = iter.ID() - 1;
128  const EventSelectorDataStream* s = nullptr;
129  status = m_streamtool->getPreviousStream( s, iter_id );
130 
131  if ( status.isSuccess() ) {
132 
133  if ( !s->isInitialized() ) {
134  EventSelector* thisPtr = const_cast<EventSelector*>( this );
135  status = thisPtr->m_streamtool->initializeStream( const_cast<EventSelectorDataStream*>( s ) );
136  }
137  if ( status.isSuccess() ) {
138  const IEvtSelector* sel = s->selector();
139  if ( sel ) {
140  Context* ctxt = nullptr;
141  status = sel->createContext( ctxt );
142  if ( status.isSuccess() ) {
143  status = sel->resetCriteria( s->criteria(), *ctxt );
144  if ( status.isSuccess() ) {
145  iter.set( this, iter_id, ctxt, 0 );
146  info() << *s << endmsg;
147  return StatusCode::SUCCESS;
148  }
149  }
150  }
151  }
152  }
153 
154  iter.set( this, -1, 0, 0 );
155  return StatusCode::FAILURE;
156 }
157 
159 StatusCode EventSelector::createContext( Context*& refpCtxt ) const {
160  // Max event is zero. Return begin = end
161  refpCtxt = nullptr;
162  if ( m_firstEvent < 0 ) {
163  error() << "First Event = " << m_firstEvent << " not valid" << endmsg;
164  error() << "It should be > 0 " << endmsg;
165  return StatusCode::FAILURE; // if failure => iterators = end();
166  }
167  auto ctxt = new EvtSelectorContext( this );
168  refpCtxt = ctxt;
169  ctxt->set( 0, -1, 0, 0 );
170  firstOfNextStream( true, *ctxt ).ignore();
171  long nskip = m_firstEvent;
172  while ( --nskip > 0 ) {
173  StatusCode sc = next( *refpCtxt );
174  if ( sc.isFailure() ) {
175  error() << " createContext() failed to start with event number " << m_firstEvent << endmsg;
176  if ( sc = releaseContext( refpCtxt ); !sc ) return sc;
177  refpCtxt = nullptr;
178  return StatusCode::FAILURE;
179  }
180  }
181  return StatusCode::SUCCESS;
182 }
183 
185 StatusCode EventSelector::next( Context& refCtxt ) const { return next( refCtxt, 1 ); }
186 
188 StatusCode EventSelector::next( Context& refCtxt, int /* jump */ ) const {
189  EvtSelectorContext* pIt = dynamic_cast<EvtSelectorContext*>( &refCtxt );
190  if ( pIt ) {
191  if ( pIt->ID() != -1 ) {
192  const EventSelectorDataStream* s = m_streamtool->getStream( pIt->ID() );
193  Context* it = pIt->context();
194  IEvtSelector* sel = s->selector();
195  if ( it && sel ) { // First exploit the current stream
196  StatusCode sc = sel->next( *it ); // This stream is empty: advance to the next stream
197  if ( !sc.isSuccess() ) {
198  m_incidentSvc->fireIncident( Incident( s->dbName(), IncidentType::EndInputFile ) );
199  sc = firstOfNextStream( true, *pIt );
200  if ( sc.isSuccess() ) sc = next( *pIt );
201  } else {
202  pIt->increaseCounters( false );
203  pIt->set( it, 0 );
204  printEvtInfo( pIt );
205  }
206  return sc;
207  } else if ( m_reconfigure ) {
208  StatusCode sc = firstOfNextStream( false, *pIt );
209  printEvtInfo( pIt );
210  return sc;
211  }
212  } else if ( m_reconfigure ) {
213  StatusCode sc = firstOfNextStream( false, *pIt );
214  printEvtInfo( pIt );
215  return sc;
216  }
217  pIt->increaseCounters( false );
218  }
219  printEvtInfo( pIt );
220  return StatusCode::FAILURE;
221 }
222 
224 StatusCode EventSelector::previous( Context& refCtxt ) const { return previous( refCtxt, 1 ); }
225 
227 StatusCode EventSelector::previous( Context& refCtxt, int jump ) const {
228  EvtSelectorContext* pIt = dynamic_cast<EvtSelectorContext*>( &refCtxt );
229  if ( pIt && jump > 0 ) {
231  for ( int i = 0; i < jump && sc.isSuccess(); ++i ) {
232  const EventSelectorDataStream* s = m_streamtool->getStream( pIt->ID() );
233  Context* it = pIt->context();
234  IEvtSelector* sel = s->selector();
235  if ( it && sel ) { // First exploit the current stream
236  // This stream is empty: advance to the next stream
237  sc = sel->previous( *it ); // This stream is empty: advance to the next stream
238  if ( !sc.isSuccess() ) {
239  sc = lastOfPreviousStream( true, *pIt );
240  } else {
241  pIt->increaseCounters( false );
242  pIt->set( it, 0 );
243  }
244  printEvtInfo( pIt );
245  if ( !sc.isSuccess() ) { return sc; }
246  }
247  pIt->increaseCounters( false );
248  }
249  return sc;
250  }
251  printEvtInfo( pIt );
252  return StatusCode::FAILURE;
253 }
254 
256 StatusCode EventSelector::last( Context& refCtxt ) const {
257  EvtSelectorContext* pIt = dynamic_cast<EvtSelectorContext*>( &refCtxt );
258  if ( pIt ) {}
259  return StatusCode::FAILURE;
260 }
261 
263 StatusCode EventSelector::rewind( Context& refCtxt ) const {
265  EvtSelectorContext* ctxt = dynamic_cast<EvtSelectorContext*>( &refCtxt );
266  if ( ctxt ) {
267  ctxt->set( 0, -1, 0, 0 );
268  if ( sc = firstOfNextStream( true, *ctxt ); !sc ) return sc;
269  long nskip = m_firstEvent;
270  while ( --nskip > 0 ) {
271  sc = next( *ctxt );
272  if ( sc.isFailure() ) { error() << "rewind() failed to start with event number " << m_firstEvent << endmsg; }
273  }
274  }
275  return sc;
276 }
277 
279 StatusCode EventSelector::createAddress( const Context& refCtxt, IOpaqueAddress*& refpAddr ) const {
280  auto cpIt = dynamic_cast<const EvtSelectorContext*>( &refCtxt );
281  auto pIt = const_cast<EvtSelectorContext*>( cpIt );
282  refpAddr = nullptr;
284  if ( pIt ) {
285  auto s = m_streamtool->getStream( pIt->ID() );
286  auto it = pIt->context();
287  auto sel = s->selector();
288  if ( it && sel ) {
289  IOpaqueAddress* pAddr = nullptr;
290  sc = sel->createAddress( *it, pAddr );
291  if ( sc.isSuccess() ) { refpAddr = pAddr; }
292  pIt->set( it, pAddr );
293  }
294  }
295  return sc;
296 }
297 
298 // Release existing event iteration context
299 StatusCode EventSelector::releaseContext( Context*& refCtxt ) const {
301  auto cpIt = dynamic_cast<const EvtSelectorContext*>( refCtxt );
302  std::unique_ptr<EvtSelectorContext> pIt{ const_cast<EvtSelectorContext*>( cpIt ) };
303  if ( pIt && pIt->ID() >= 0 && pIt->ID() < (long)m_streamtool->size() ) {
304  const auto s = m_streamtool->getStream( pIt->ID() );
305  auto it = pIt->context();
306  auto sel = s->selector();
307  if ( it && sel ) { sc = sel->releaseContext( it ); }
308  }
309  refCtxt = nullptr; // std::unique_ptr always deletes object, so always set to NULL
310  return sc;
311 }
312 
315  // Initialize base class
316  StatusCode status = Service::initialize();
317  if ( !status.isSuccess() ) {
318  error() << "Error initializing base class Service!" << endmsg;
319  return status;
320  }
321  // Get the references to the services that are needed by the ApplicationMgr itself
322  m_incidentSvc = serviceLocator()->service( "IncidentSvc" );
323  if ( !m_incidentSvc ) {
324  fatal() << "Error retrieving IncidentSvc." << endmsg;
325  return StatusCode::FAILURE;
326  }
327  if ( m_evtMax != INT_MAX ) {
328  error() << "EvtMax is an obsolete property of the event selector." << endmsg;
329  error() << "Please set \"ApplicationMgr.EvtMax = " << m_evtMax << ";\" to process the requested number of events."
330  << endmsg;
331  return StatusCode::FAILURE;
332  }
333 
334  m_toolSvc = serviceLocator()->service( "ToolSvc" );
335  if ( !m_toolSvc ) {
336  error() << " Could not locate the Tool Service! " << endmsg;
337  return StatusCode::FAILURE;
338  }
339  // make sure we finalize _prior_ to ToolSvc... we are about to get a
340  // a pointer to a tool which gets finalized and released by the ToolSvc
341  // during ToolSvc::finalize, and we don't want dangling pointers...
343  auto prio = mgr->getPriority( "ToolSvc" );
344  mgr->setPriority( name(), prio + 1 ).ignore();
345 
346  status = m_toolSvc->retrieveTool( m_streamManager, m_streamtool, this );
347 
348  if ( status.isFailure() ) {
349  error() << "Error initializing " << m_streamManager << endmsg;
350  return status;
351  }
352 
353  status = m_streamtool->clear();
354  if ( status.isFailure() ) {
355  // Message already printed by the tool
356  return status;
357  }
358 
360 
362 
363  m_streamID = 0;
364 
365  return status;
366 }
367 
368 // Re-initialize
371  error() << "Cannot reinitialize: service not in state initialized" << endmsg;
372  return StatusCode::FAILURE;
373  }
374 
375  if ( m_streamSpecsLast != m_streamSpecs ) {
376  StatusCode status = m_streamtool->clear();
377  if ( status.isFailure() ) return status;
379  m_reconfigure = true;
381  }
382 
383  return StatusCode::SUCCESS;
384 }
385 
386 //
388 
389  if ( msgLevel( MSG::DEBUG ) ) { debug() << "finalize()" << endmsg; }
390 
391  m_incidentSvc = nullptr;
392 
393  if ( m_streamtool ) {
394  if ( m_toolSvc ) {
395  m_toolSvc->releaseTool( m_streamtool ).ignore();
396  } else {
397  // It should not be possible to get here
399  }
400  m_streamtool = nullptr;
401  }
402  m_toolSvc.reset();
403 
404  return Service::finalize();
405 }
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:22
EventCollectionSelector::resetCriteria
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...
Definition: EventCollectionSelector.cpp:295
IDataStreamTool::getStream
virtual EventSelectorDataStream * getStream(size_type)=0
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
EventSelector::firstOfNextStream
StatusCode firstOfNextStream(bool shutDown, EvtSelectorContext &it) const
Retrieve first entry of the next data stream.
Definition: EventSelector.cpp:51
IDataStreamTool::size
virtual size_type size()=0
EventSelectorDataStream.h
EventSelector::lastOfPreviousStream
StatusCode lastOfPreviousStream(bool shutDown, EvtSelectorContext &it) const
Retrieve last entry of the previous data stream.
Definition: EventSelector.cpp:109
EventSelector::m_firstEvent
Gaudi::Property< int > m_firstEvent
Definition: EventSelector.h:68
EventSelector::m_toolSvc
SmartIF< IToolSvc > m_toolSvc
Definition: EventSelector.h:53
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
EvtSelectorContext::increaseCounters
IDataStreamTool::size_type increaseCounters(bool reset=false)
Increase counters.
Definition: EventIterator.h:70
EventSelector::releaseContext
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
Definition: EventSelector.cpp:299
gaudirun.s
string s
Definition: gaudirun.py:346
IOpaqueAddress
Definition: IOpaqueAddress.h:28
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
IAddressCreator.h
IEvtSelector
Definition: IEvtSelector.h:26
EvtSelectorContext::context
IEvtSelector::Context * context() const
Access "real" iterator.
Definition: EventIterator.h:68
EventSelector::printEvtInfo
virtual void printEvtInfo(const EvtSelectorContext *iter) const
Progress report.
Definition: EventSelector.cpp:33
EventSelector::reinitialize
StatusCode reinitialize() override
Service override: Reinitialize service.
Definition: EventSelector.cpp:369
EvtSelectorContext::ID
virtual IDataStreamTool::size_type ID() const
Stream identifier.
Definition: EventIterator.h:87
EventSelector::next
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
Definition: EventSelector.cpp:185
IDataStreamTool::initializeStream
virtual StatusCode initializeStream(EventSelectorDataStream *)=0
EventSelector::finalize
StatusCode finalize() override
IService implementation: Service finalization.
Definition: EventSelector.cpp:387
CommonMessaging< implements< IService, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:147
EventSelector::previous
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
Definition: EventSelector.cpp:224
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:223
Service::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:55
EvtSelectorContext
Definition of class EventIterator.
Definition: EventIterator.h:30
IIncidentSvc.h
IToolSvc.h
EventSelector::m_streamtool
IDataStreamTool * m_streamtool
Definition: EventSelector.h:55
EventSelector.h
IDataStreamTool::clear
virtual StatusCode clear()=0
SmartIF.h
EvtSelectorContext::numEvent
long numEvent() const
Access counter.
Definition: EventIterator.h:89
EventSelector::m_evtPrintFrequency
Gaudi::Property< int > m_evtPrintFrequency
Definition: EventSelector.h:70
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:333
StatusCode
Definition: StatusCode.h:64
EventSelector::createAddress
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
Definition: EventSelector.cpp:279
EvtSelectorContext::numStreamEvent
long numStreamEvent() const
Access counter within stream.
Definition: EventIterator.h:91
EventSelector::m_incidentSvc
SmartIF< IIncidentSvc > m_incidentSvc
Reference to the indicent service.
Definition: EventSelector.h:51
IDataStreamTool::getNextStream
virtual StatusCode getNextStream(const EventSelectorDataStream *&, size_type &)=0
SmartIF< ISvcManager >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:198
EventIterator.h
EventSelector::m_evtMax
Gaudi::Property< int > m_evtMax
Definition: EventSelector.h:69
IOTest.sel
sel
Definition: IOTest.py:106
IDataStreamTool::finalizeStream
virtual StatusCode finalizeStream(EventSelectorDataStream *)=0
EventSelector::initialize
StatusCode initialize() override
IService implementation: Db event selector override.
Definition: EventSelector.cpp:314
EventSelector::m_streamManager
Gaudi::Property< std::string > m_streamManager
Definition: EventSelector.h:71
EventSelector::last
StatusCode last(Context &c) const override
Access last item in the iteration.
Definition: EventSelector.cpp:256
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
IDataStreamTool::getPreviousStream
virtual StatusCode getPreviousStream(const EventSelectorDataStream *&, size_type &)=0
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
IDataStreamTool::size_type
long size_type
Definition: IDataStreamTool.h:33
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
EventSelector::m_streamID
long int m_streamID
Definition: EventSelector.h:47
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:45
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:24
EventSelector::m_streamSpecs
Gaudi::Property< StreamSpecs > m_streamSpecs
Definition: EventSelector.h:67
EventSelector::createContext
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
Definition: EventSelector.cpp:159
EventSelectorDataStream
Definition of class EventSelectorDataStream.
Definition: EventSelectorDataStream.h:39
EventSelector::m_reconfigure
bool m_reconfigure
Reconfigure occurred.
Definition: EventSelector.h:58
IDataStreamTool.h
EventSelector::m_streamSpecsLast
StreamSpecs m_streamSpecsLast
Input stream specifiers (last used)
Definition: EventSelector.h:60
EvtSelectorContext::set
void set(const IEvtSelector *sel, IDataStreamTool::size_type id, IEvtSelector::Context *it, IOpaqueAddress *pA)
Set the address of the iterator.
Definition: EventIterator.h:53
IDataStreamTool::addStreams
virtual StatusCode addStreams(const StreamSpecs &)=0
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
EventSelector
Definition of class EventSelector.
Definition: EventSelector.h:41
ISvcLocator.h
Incident.h
Incident
Definition: Incident.h:24
PropertyHolder.h
IInterface::release
virtual unsigned long release() const =0
Release Interface instance.
ISvcManager.h
MsgStream.h
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:336
EventSelector::rewind
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
Definition: EventSelector.cpp:263