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