| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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