The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
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"
23#include <GaudiKernel/SmartIF.h>
24#include <climits>
25
27
28StatusCode EventSelector::resetCriteria( const std::string& /* criteria */, Context& /* context */ ) const {
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() ) {
57 const EventSelectorDataStream* s = m_streamtool->getStream( iter.ID() );
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 ) );
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
159StatusCode 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
185StatusCode EventSelector::next( Context& refCtxt ) const { return next( refCtxt, 1 ); }
186
188StatusCode 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
224StatusCode EventSelector::previous( Context& refCtxt ) const { return previous( refCtxt, 1 ); }
225
227StatusCode 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
256StatusCode EventSelector::last( Context& refCtxt ) const {
257 EvtSelectorContext* pIt = dynamic_cast<EvtSelectorContext*>( &refCtxt );
258 if ( pIt ) {}
259 return StatusCode::FAILURE;
260}
261
263StatusCode 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
279StatusCode 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
299StatusCode 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
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
359 status = m_streamtool->addStreams( m_streamSpecs );
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
376 StatusCode status = m_streamtool->clear();
377 if ( status.isFailure() ) return status;
379 m_reconfigure = true;
380 return m_streamtool->addStreams( m_streamSpecs );
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
398 m_streamtool->release();
399 }
400 m_streamtool = nullptr;
401 }
402 m_toolSvc.reset();
403
404 return Service::finalize();
405}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
Definition of class EventSelectorDataStream.
Definition of class EventSelector.
bool m_reconfigure
Reconfigure occurred.
StatusCode next(Context &refCtxt) const override
Get next iteration item from the event loop context.
StatusCode finalize() override
IService implementation: Service finalization.
SmartIF< IIncidentSvc > m_incidentSvc
Reference to the indicent service.
StatusCode lastOfPreviousStream(bool shutDown, EvtSelectorContext &it) const
Retrieve last entry of the previous data stream.
StatusCode rewind(Context &refCtxt) const override
Rewind the dataset.
StatusCode reinitialize() override
Service override: Reinitialize service.
StreamSpecs m_streamSpecsLast
Input stream specifiers (last used)
StatusCode firstOfNextStream(bool shutDown, EvtSelectorContext &it) const
Retrieve first entry of the next data stream.
Gaudi::Property< int > m_evtMax
StatusCode initialize() override
IService implementation: Db event selector override.
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...
SmartIF< IToolSvc > m_toolSvc
StatusCode last(Context &c) const override
Access last item in the iteration.
StatusCode previous(Context &refCtxt) const override
Get previous iteration item from the event loop context.
StatusCode releaseContext(Context *&refCtxt) const override
Release existing event iteration context.
Gaudi::Property< StreamSpecs > m_streamSpecs
StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&refpAddr) const override
Create new Opaque address corresponding to the current record.
virtual void printEvtInfo(const EvtSelectorContext *iter) const
Progress report.
IDataStreamTool * m_streamtool
StatusCode createContext(Context *&refpCtxt) const override
Create a new event loop context.
long int m_streamID
Gaudi::Property< int > m_evtPrintFrequency
Gaudi::Property< std::string > m_streamManager
Gaudi::Property< int > m_firstEvent
Definition of class EventIterator.
long numEvent() const
Access counter.
virtual IDataStreamTool::size_type ID() const
Stream identifier.
IEvtSelector::Context * context() const
Access "real" iterator.
long numStreamEvent() const
Access counter within stream.
void set(const IEvtSelector *sel, IDataStreamTool::size_type id, IEvtSelector::Context *it, IOpaqueAddress *pA)
Set the address of the iterator.
IDataStreamTool::size_type increaseCounters(bool reset=false)
Increase counters.
virtual StatusCode finalizeStream(EventSelectorDataStream *)=0
virtual StatusCode initializeStream(EventSelectorDataStream *)=0
The Event Selector Interface.
Opaque address interface definition.
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
Base class for all Incidents (computing events).
Definition Incident.h:24
Gaudi::StateMachine::State FSMState() const override
Definition Service.h:55
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
StatusCode finalize() override
Definition Service.cpp:223
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
StatusCode initialize() override
Definition Service.cpp:118
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:129
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:139
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
@ DEBUG
Definition IMessageSvc.h:22
STL namespace.