| # 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
Algorithm.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/Kernel.h"
2 #include "GaudiKernel/ISvcLocator.h"
3 #include "GaudiKernel/IMessageSvc.h"
4 #include "GaudiKernel/IJobOptionsSvc.h"
5 #include "GaudiKernel/IAlgManager.h"
6 #include "GaudiKernel/IAuditorSvc.h"
7 #include "GaudiKernel/IDataProviderSvc.h"
8 #include "GaudiKernel/IConversionSvc.h"
9 #include "GaudiKernel/IHistogramSvc.h"
10 #include "GaudiKernel/INTupleSvc.h"
11 #include "GaudiKernel/IRndmGenSvc.h"
12 #include "GaudiKernel/IToolSvc.h"
13 #include "GaudiKernel/IExceptionSvc.h"
14 #include "GaudiKernel/IAlgContextSvc.h"
15 #include "GaudiKernel/IProperty.h"
16
17 #include "GaudiKernel/Algorithm.h"
18 #include "GaudiKernel/PropertyMgr.h"
19 #include "GaudiKernel/MsgStream.h"
20 #include "GaudiKernel/Chrono.h"
21 #include "GaudiKernel/Stat.h"
22 #include "GaudiKernel/GaudiException.h"
23 #include "GaudiKernel/ServiceLocatorHelper.h"
24 #include "GaudiKernel/ThreadGaudi.h"
25 #include "GaudiKernel/Guards.h"
26
27 // Constructor
28 Algorithm::Algorithm( const std::string& name, ISvcLocator *pSvcLocator,
29 const std::string& version)
30 : m_name(name),
31 m_version(version),
32 m_registerContext ( false ) ,
33 m_pSvcLocator(pSvcLocator),
34 m_filterPassed(true),
35 m_isEnabled(true),
36 m_isExecuted(false),
37 m_state(Gaudi::StateMachine::CONFIGURED),
38 m_targetState(Gaudi::StateMachine::CONFIGURED)
39 {
40 m_propertyMgr = new PropertyMgr();
41 m_subAlgms = new std::vector<Algorithm *>();
42
43 // Declare common Algorithm properties with their defaults
44 declareProperty( "OutputLevel", m_outputLevel = MSG::NIL);
45 declareProperty( "Enable", m_isEnabled = true);
46 declareProperty( "ErrorMax", m_errorMax = 1);
47 declareProperty( "ErrorCount", m_errorCount = 0);
48 // Auditor monitoring properties
49
50 // Get the default setting for service auditing from the AppMgr
51 declareProperty( "AuditAlgorithms", m_auditInit );
52
53 bool audit(false);
54 SmartIF<IProperty> appMgr(serviceLocator()->service("ApplicationMgr"));
55 if (appMgr.isValid()) {
56 const Property& prop = appMgr->getProperty("AuditAlgorithms");
57 Property &pr = const_cast<Property&>(prop);
58 if (m_name != "IncidentSvc") {
59 setProperty( pr ).ignore();
60 }
61 audit = m_auditInit.value();
62 }
63
64 declareProperty( "AuditInitialize" , m_auditorInitialize = audit ) ;
65 declareProperty( "AuditReinitialize", m_auditorReinitialize = audit ) ;
66 declareProperty( "AuditRestart" , m_auditorRestart = audit ) ;
67 declareProperty( "AuditExecute" , m_auditorExecute = audit ) ;
68 declareProperty( "AuditFinalize" , m_auditorFinalize = audit ) ;
69 declareProperty( "AuditBeginRun" , m_auditorBeginRun = audit ) ;
70 declareProperty( "AuditEndRun" , m_auditorEndRun = audit ) ;
71 declareProperty( "AuditStart" , m_auditorStart = audit ) ;
72 declareProperty( "AuditStop" , m_auditorStop = audit ) ;
73
74 declareProperty( "MonitorService" , m_monitorSvcName = "MonitorSvc" );
75
76 declareProperty
77 ( "RegisterForContextService" ,
78 m_registerContext ,
79 "The flag to enforce the registration for Algorithm Context Service") ;
80
81 // update handlers.
82 m_outputLevel.declareUpdateHandler(&Algorithm::initOutputLevel, this);
83
84 }
85
86 // Default Destructor
87 Algorithm::~Algorithm() {
88 delete m_subAlgms;
89 delete m_propertyMgr;
90 }
91
92 // IAlgorithm implementation
93 StatusCode Algorithm::sysInitialize() {
94
95 // Bypass the initialization if the algorithm
96 // has already been initialized.
97 if ( Gaudi::StateMachine::INITIALIZED <= FSMState() ) return StatusCode::SUCCESS;
98
99 // Set the Algorithm's properties
100 StatusCode sc = setProperties();
101 if( sc.isFailure() ) return StatusCode::FAILURE;
102
103 // Bypass the initialization if the algorithm is disabled.
104 // Need to do this after setProperties.
105 if ( !isEnabled( ) ) return StatusCode::SUCCESS;
106
107 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::INITIALIZE,m_state);
108
109 // Check current outputLevel to eventually inform the MessagsSvc
110 //if( m_outputLevel != MSG::NIL ) {
111 setOutputLevel( m_outputLevel );
112 //}
113
114 // TODO: (MCl) where shoud we do this? initialize or start?
115 // Reset Error count
116 //m_errorCount = 0;
117
118 // lock the context service
119 Gaudi::Utils::AlgContext cnt
120 ( this , registerContext() ? contextSvc().get() : 0 ) ;
121
122 // Invoke initialize() method of the derived class inside a try/catch clause
123 try {
124
125 { // limit the scope of the guard
126 Gaudi::Guards::AuditorGuard guard
127 ( this,
128 // check if we want to audit the initialize
129 (m_auditorInitialize) ? auditorSvc().get() : 0,
130 IAuditor::Initialize);
131 // Invoke the initialize() method of the derived class
132 sc = initialize();
133 }
134 if( sc.isSuccess() ) {
135
136 // Now initialize care of any sub-algorithms
137 std::vector<Algorithm *>::iterator it;
138 bool fail(false);
139 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) {
140 if ((*it)->sysInitialize().isFailure()) fail = true;
141 }
142 if( fail ) {
143 sc = StatusCode::FAILURE;
144 MsgStream log ( msgSvc() , name() );
145 log << MSG::ERROR << " Error initializing one or several sub-algorithms"
146 << endmsg;
147 } else {
148 // Update the state.
149 m_state = m_targetState;
150 }
151 }
152 }
153 catch ( const GaudiException& Exception )
154 {
155 MsgStream log ( msgSvc() , name() ) ;
156 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
157 << " is caught " << endmsg;
158 log << MSG::ERROR << Exception << endmsg;
159 Stat stat( chronoSvc() , Exception.tag() );
160 sc = StatusCode::FAILURE;
161 }
162 catch( const std::exception& Exception )
163 {
164 MsgStream log ( msgSvc() , name() ) ;
165 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
166 log << MSG::ERROR << Exception.what() << endmsg;
167 Stat stat( chronoSvc() , "*std::exception*" );
168 sc = StatusCode::FAILURE;
169 }
170 catch(...)
171 {
172 MsgStream log ( msgSvc() , name() ) ;
173 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
174 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
175 sc = StatusCode::FAILURE;
176 }
177
178 return sc;
179 }
180
181 // IAlgorithm implementation
182 StatusCode Algorithm::sysStart() {
183
184 // Bypass the startup if already running or disabled.
185 if ( Gaudi::StateMachine::RUNNING == FSMState() ||
186 !isEnabled() ) return StatusCode::SUCCESS;
187
188 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::START,m_state);
189
190 // TODO: (MCl) where shoud we do this? initialize or start?
191 // Reset Error count
192 m_errorCount = 0;
193
194 // lock the context service
195 Gaudi::Utils::AlgContext cnt
196 ( this , registerContext() ? contextSvc().get() : 0 ) ;
197
198 StatusCode sc(StatusCode::FAILURE);
199 // Invoke start() method of the derived class inside a try/catch clause
200 try
201 {
202 { // limit the scope of the guard
203 Gaudi::Guards::AuditorGuard guard
204 (this,
205 // check if we want to audit the initialize
206 (m_auditorStart) ? auditorSvc().get() : 0,
207 IAuditor::Start);
208 // Invoke the start() method of the derived class
209 sc = start();
210 }
211 if( sc.isSuccess() ) {
212
213 // Now start any sub-algorithms
214 std::vector<Algorithm *>::iterator it;
215 bool fail(false);
216 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) {
217 if ((*it)->sysStart().isFailure()) fail = true;
218 }
219 if( fail ) {
220 sc = StatusCode::FAILURE;
221 MsgStream log ( msgSvc() , name() );
222 log << MSG::ERROR << " Error starting one or several sub-algorithms"
223 << endmsg;
224 } else {
225 // Update the state.
226 m_state = m_targetState;
227 }
228 }
229 }
230 catch ( const GaudiException& Exception )
231 {
232 MsgStream log ( msgSvc() , name() );
233 log << MSG::FATAL << "in sysStart(): exception with tag=" << Exception.tag()
234 << " is caught" << endmsg;
235 log << MSG::ERROR << Exception << endmsg;
236 Stat stat( chronoSvc() , Exception.tag() );
237 sc = StatusCode::FAILURE;
238 }
239 catch( const std::exception& Exception )
240 {
241 MsgStream log ( msgSvc() , name() );
242 log << MSG::FATAL << "in sysStart(): standard std::exception is caught" << endmsg;
243 log << MSG::ERROR << Exception.what() << endmsg;
244 Stat stat( chronoSvc() , "*std::exception*" );
245 sc = StatusCode::FAILURE;
246 }
247 catch(...)
248 {
249 MsgStream log ( msgSvc() , name() );
250 log << MSG::FATAL << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
251 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
252 sc = StatusCode::FAILURE;
253 }
254
255 return sc;
256 }
257
258 // IAlgorithm implementation
259 StatusCode Algorithm::sysReinitialize() {
260
261 // Bypass the initialization if the algorithm is disabled.
262 if ( !isEnabled( ) ) return StatusCode::SUCCESS;
263
264 // Check that the current status is the correct one.
265 if ( Gaudi::StateMachine::INITIALIZED != FSMState() ) {
266 MsgStream log ( msgSvc() , name() );
267 log << MSG::ERROR
268 << "sysReinitialize(): cannot reinitialize algorithm not initialized"
269 << endmsg;
270 return StatusCode::FAILURE;
271 }
272
273 // Check current outputLevel to evetually inform the MessagsSvc
274 //if( m_outputLevel != MSG::NIL ) {
275 setOutputLevel( m_outputLevel );
276 //}
277
278 // Reset Error count
279 // m_errorCount = 0; // done during start
280
281 // lock the context service
282 Gaudi::Utils::AlgContext cnt
283 ( this , registerContext() ? contextSvc().get() : 0 ) ;
284
285 StatusCode sc(StatusCode::SUCCESS);
286 // Invoke reinitialize() method of the derived class inside a try/catch clause
287 try {
288 { // limit the scope of the guard
289 Gaudi::Guards::AuditorGuard guard(this,
290 // check if we want to audit the initialize
291 (m_auditorReinitialize) ? auditorSvc().get() : 0,
292 IAuditor::ReInitialize);
293 // Invoke the reinitialize() method of the derived class
294 sc = reinitialize();
295 }
296 if( sc.isSuccess() ) {
297
298 // Now initialize care of any sub-algorithms
299 std::vector<Algorithm *>::iterator it;
300 bool fail(false);
301 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) {
302 if((*it)->sysReinitialize().isFailure()) fail = true;
303 }
304
305 if (fail) {
306 sc = StatusCode::FAILURE;
307 MsgStream log ( msgSvc() , name() );
308 log << MSG::ERROR
309 << "sysReinitialize(): Error reinitializing one or several "
310 << "sub-algorithms" << endmsg;
311 }
312 }
313 }
314 catch ( const GaudiException& Exception )
315 {
316 MsgStream log ( msgSvc() , name() );
317 log << MSG::FATAL << "sysReinitialize(): Exception with tag=" << Exception.tag()
318 << " is caught" << endmsg;
319 log << MSG::ERROR << Exception << endmsg;
320 Stat stat( chronoSvc() , Exception.tag() );
321 sc = StatusCode::FAILURE;
322 }
323 catch( const std::exception& Exception )
324 {
325 MsgStream log ( msgSvc() , name() );
326 log << MSG::FATAL << "sysReinitialize(): Standard std::exception is caught" << endmsg;
327 log << MSG::ERROR << Exception.what() << endmsg;
328 Stat stat( chronoSvc() , "*std::exception*" );
329 sc = StatusCode::FAILURE;
330 }
331 catch(...)
332 {
333 MsgStream log ( msgSvc() , name() );
334 log << MSG::FATAL << "sysReinitialize(): UNKNOWN Exception is caught" << endmsg;
335 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
336 sc = StatusCode::FAILURE;
337 }
338
339 return sc;
340 }
341
342 // IAlgorithm implementation
343 StatusCode Algorithm::sysRestart() {
344
345 // Bypass the initialization if the algorithm is disabled.
346 if ( !isEnabled( ) ) return StatusCode::SUCCESS;
347
348 // Check that the current status is the correct one.
349 if ( Gaudi::StateMachine::RUNNING != FSMState() ) {
350 MsgStream log ( msgSvc() , name() );
351 log << MSG::ERROR
352 << "sysRestart(): cannot restart algorithm not started"
353 << endmsg;
354 return StatusCode::FAILURE;
355 }
356
357 // Check current outputLevel to evetually inform the MessagsSvc
358 //if( m_outputLevel != MSG::NIL ) {
359 setOutputLevel( m_outputLevel );
360 //}
361
362 // Reset Error count
363 m_errorCount = 0;
364
365 // lock the context service
366 Gaudi::Utils::AlgContext cnt
367 ( this , registerContext() ? contextSvc().get() : 0 ) ;
368
369 StatusCode sc(StatusCode::FAILURE);
370 // Invoke reinitialize() method of the derived class inside a try/catch clause
371 try {
372 { // limit the scope of the guard
373 Gaudi::Guards::AuditorGuard guard(this,
374 // check if we want to audit the initialize
375 (m_auditorRestart) ? auditorSvc().get() : 0,
376 IAuditor::ReStart);
377 // Invoke the reinitialize() method of the derived class
378 sc = restart();
379 }
380 if( sc.isSuccess() ) {
381
382 // Now initialize care of any sub-algorithms
383 std::vector<Algorithm *>::iterator it;
384 bool fail(false);
385 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) {
386 if ((*it)->sysRestart().isFailure()) fail = true;
387 }
388 if( fail ) {
389 sc = StatusCode::FAILURE;
390 MsgStream log ( msgSvc() , name() );
391 log << MSG::ERROR
392 << "sysRestart(): Error restarting one or several sub-algorithms"
393 << endmsg;
394 }
395 }
396 }
397 catch ( const GaudiException& Exception )
398 {
399 MsgStream log ( msgSvc() , name() );
400 log << MSG::FATAL << "sysRestart(): Exception with tag=" << Exception.tag()
401 << " is caught" << endmsg;
402 log << MSG::ERROR << Exception << endmsg;
403 Stat stat( chronoSvc() , Exception.tag() );
404 sc = StatusCode::FAILURE;
405 }
406 catch( const std::exception& Exception )
407 {
408 MsgStream log ( msgSvc() , name() );
409 log << MSG::FATAL << "sysRestart(): Standard std::exception is caught" << endmsg;
410 log << MSG::ERROR << Exception.what() << endmsg;
411 Stat stat( chronoSvc() , "*std::exception*" );
412 sc = StatusCode::FAILURE;
413 }
414 catch(...)
415 {
416 MsgStream log ( msgSvc() , name() );
417 log << MSG::FATAL << "sysRestart(): UNKNOWN Exception is caught" << endmsg;
418 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
419 sc = StatusCode::FAILURE;
420 }
421
422 return sc;
423 }
424
425 // IAlgorithm implementation
426 StatusCode Algorithm::sysBeginRun() {
427
428 // Bypass the beginRun if the algorithm is disabled.
429 if ( !isEnabled( ) ) return StatusCode::SUCCESS;
430
431 // Check current outputLevel to evetually inform the MessagsSvc
432 //if( m_outputLevel != MSG::NIL ) {
433 setOutputLevel( m_outputLevel );
434 //}
435
436 // Reset Error count
437 m_errorCount = 0;
438
439 // lock the context service
440 Gaudi::Utils::AlgContext cnt
441 ( this , registerContext() ? contextSvc().get() : 0 ) ;
442
443 StatusCode sc(StatusCode::FAILURE);
444 // Invoke beginRun() method of the derived class inside a try/catch clause
445 try {
446 { // limit the scope of the guard
447 Gaudi::Guards::AuditorGuard guard(this,
448 // check if we want to audit the initialize
449 (m_auditorBeginRun) ? auditorSvc().get() : 0,
450 IAuditor::BeginRun);
451 // Invoke the beginRun() method of the derived class
452 sc = beginRun();
453 }
454 if( sc.isSuccess() ) {
455
456 // Now call beginRun for any sub-algorithms
457 std::vector<Algorithm *>::iterator it;
458 bool fail(false);
459 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) {
460 if((*it)->sysBeginRun().isFailure()) fail = true;
461 }
462 if( fail ) {
463 sc = StatusCode::FAILURE;
464 MsgStream log ( msgSvc() , name() );
465 log << MSG::ERROR << " Error executing BeginRun for one or several sub-algorithms"
466 << endmsg;
467 }
468 }
469 }
470 catch ( const GaudiException& Exception )
471 {
472 MsgStream log ( msgSvc() , name() );
473 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
474 << " is caught " << endmsg;
475 log << MSG::ERROR << Exception << endmsg;
476 Stat stat( chronoSvc() , Exception.tag() );
477 sc = StatusCode::FAILURE;
478 }
479 catch( const std::exception& Exception )
480 {
481 MsgStream log ( msgSvc() , name() );
482 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
483 log << MSG::ERROR << Exception.what() << endmsg;
484 Stat stat( chronoSvc() , "*std::exception*" );
485 sc = StatusCode::FAILURE;
486 }
487 catch(...)
488 {
489 MsgStream log ( msgSvc() , name() );
490 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
491 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
492 sc = StatusCode::FAILURE;
493 }
494 return sc;
495 }
496
497 StatusCode Algorithm::beginRun() {
498 return StatusCode::SUCCESS;
499 }
500
501 // IAlgorithm implementation
502 StatusCode Algorithm::sysEndRun() {
503
504 // Bypass the endRun if the algorithm is disabled.
505 if ( !isEnabled( ) ) return StatusCode::SUCCESS;
506
507 // Check current outputLevel to eventually inform the MessagsSvc
508 //if( m_outputLevel != MSG::NIL ) {
509 setOutputLevel( m_outputLevel );
510 //}
511
512 // Reset Error count
513 m_errorCount = 0;
514
515 // lock the context service
516 Gaudi::Utils::AlgContext cnt
517 ( this , registerContext() ? contextSvc().get() : 0 ) ;
518
519 // Invoke endRun() method of the derived class inside a try/catch clause
520 StatusCode sc(StatusCode::FAILURE);
521 try {
522 { // limit the scope of the guard
523 Gaudi::Guards::AuditorGuard guard(this,
524 // check if we want to audit the initialize
525 (m_auditorEndRun) ? auditorSvc().get() : 0,
526 IAuditor::EndRun);
527 // Invoke the endRun() method of the derived class
528 sc = endRun();
529 }
530 if( sc.isSuccess() ) {
531
532 // Now call endRun for any sub-algorithms
533 std::vector<Algorithm *>::iterator it;
534 bool fail(false);
535 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) {
536 if ((*it)->sysEndRun().isFailure()) fail = true;
537 }
538 if( fail ) {
539 sc = StatusCode::FAILURE;
540 MsgStream log ( msgSvc() , name() );
541 log << MSG::ERROR << " Error calling endRun for one or several sub-algorithms"
542 << endmsg;
543 }
544 }
545 }
546 catch ( const GaudiException& Exception )
547 {
548 MsgStream log ( msgSvc() , name() );
549 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
550 << " is caught " << endmsg;
551 log << MSG::ERROR << Exception << endmsg;
552 Stat stat( chronoSvc() , Exception.tag() );
553 sc = StatusCode::FAILURE;
554 }
555 catch( const std::exception& Exception )
556 {
557 MsgStream log ( msgSvc() , name() );
558 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
559 log << MSG::ERROR << Exception.what() << endmsg;
560 Stat stat( chronoSvc() , "*std::exception*" );
561 sc = StatusCode::FAILURE;
562 }
563 catch(...)
564 {
565 MsgStream log ( msgSvc() , name() );
566 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
567 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
568 sc = StatusCode::FAILURE;
569 }
570 return sc;
571 }
572
573 StatusCode Algorithm::endRun() {
574 return StatusCode::SUCCESS;
575 }
576
577
578 StatusCode Algorithm::sysExecute() {
579 if (!isEnabled()) {
580 if (UNLIKELY(m_outputLevel <= MSG::VERBOSE)) {
581 MsgStream log ( msgSvc() , name() );
582 log << MSG::VERBOSE << ".sysExecute(): is not enabled. Skip execution" << endmsg;
583 }
584 return StatusCode::SUCCESS;
585 }
586
587 StatusCode status;
588
589 // Should performance profile be performed ?
590 // invoke execute() method of Algorithm class
591 // and catch all uncaught exceptions
592
593 // lock the context service
594 Gaudi::Utils::AlgContext cnt
595 ( this , registerContext() ? contextSvc().get() : 0 ) ;
596
597 Gaudi::Guards::AuditorGuard guard(this,
598 // check if we want to audit the initialize
599 (m_auditorExecute) ? auditorSvc().get() : 0,
600 IAuditor::Execute,
601 status);
602 try {
603 status = execute();
604 setExecuted(true); // set the executed flag
605
606 if (status.isFailure()) {
607 status = exceptionSvc()->handleErr(*this,status);
608 }
609
610 }
611 catch( const GaudiException& Exception )
612 {
613 setExecuted(true); // set the executed flag
614
615 MsgStream log ( msgSvc() , name() );
616 if (Exception.code() == StatusCode::FAILURE) {
617 log << MSG::FATAL;
618 } else {
619 log << MSG::ERROR << " Recoverable";
620 }
621
622 log << " Exception with tag=" << Exception.tag()
623 << " is caught " << endmsg;
624
625 log << MSG::ERROR << Exception << endmsg;
626
627 Stat stat( chronoSvc() , Exception.tag() ) ;
628 status = exceptionSvc()->handle(*this,Exception);
629 }
630 catch( const std::exception& Exception )
631 {
632 setExecuted(true); // set the executed flag
633
634 MsgStream log ( msgSvc() , name() );
635 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
636 log << MSG::ERROR << Exception.what() << endmsg;
637 Stat stat( chronoSvc() , "*std::exception*" ) ;
638 status = exceptionSvc()->handle(*this,Exception);
639 }
640 catch(...)
641 {
642 setExecuted(true); // set the executed flag
643
644 MsgStream log ( msgSvc() , name() );
645 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
646 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
647
648 status = exceptionSvc()->handle(*this);
649 }
650
651 if( status.isFailure() ) {
652 MsgStream log ( msgSvc() , name() );
653 // Increment the error count
654 m_errorCount++;
655 // Check if maximum is exeeded
656 if( m_errorCount < m_errorMax ) {
657 log << MSG::WARNING << "Continuing from error (cnt=" << m_errorCount
658 << ", max=" << m_errorMax << ")" << endmsg;
659 // convert to success
660 status = StatusCode::SUCCESS;
661 }
662 }
663 return status;
664 }
665
666 // IAlgorithm implementation
667 StatusCode Algorithm::sysStop() {
668
669 // Bypass the startup if already running or disabled.
670 if ( Gaudi::StateMachine::INITIALIZED == FSMState() ||
671 !isEnabled() ) return StatusCode::SUCCESS;
672
673 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::STOP,m_state);
674
675 // lock the context service
676 Gaudi::Utils::AlgContext cnt
677 ( this , registerContext() ? contextSvc().get() : 0 ) ;
678
679 StatusCode sc(StatusCode::FAILURE);
680 // Invoke stop() method of the derived class inside a try/catch clause
681 try {
682 // Stop first any sub-algorithms (in reverse order)
683 std::vector<Algorithm *>::iterator it;
684 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) {
685 (*it)->sysStop().ignore();
686 }
687 { // limit the scope of the guard
688 Gaudi::Guards::AuditorGuard guard(this,
689 // check if we want to audit the initialize
690 (m_auditorStop) ? auditorSvc().get() : 0,
691 IAuditor::Stop);
692
693 // Invoke the stop() method of the derived class
694 sc = stop();
695 }
696 if( sc.isSuccess() ) {
697 // Update the state.
698 m_state = m_targetState;
699 }
700 }
701 catch ( const GaudiException& Exception ) {
702 MsgStream log ( msgSvc() , name() );
703 log << MSG::FATAL << "in sysStop(): exception with tag=" << Exception.tag()
704 << " is caught" << endmsg;
705 log << MSG::ERROR << Exception << endmsg;
706 Stat stat( chronoSvc() , Exception.tag() );
707 sc = StatusCode::FAILURE;
708 }
709 catch( const std::exception& Exception ) {
710 MsgStream log ( msgSvc() , name() );
711 log << MSG::FATAL << "in sysStop(): standard std::exception is caught" << endmsg;
712 log << MSG::ERROR << Exception.what() << endmsg;
713 Stat stat( chronoSvc() , "*std::exception*" );
714 sc = StatusCode::FAILURE;
715 }
716 catch(...) {
717 MsgStream log ( msgSvc() , name() );
718 log << MSG::FATAL << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
719 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
720 sc = StatusCode::FAILURE;
721 }
722
723 return sc;
724 }
725
726 StatusCode Algorithm::sysFinalize() {
727
728 // Bypass the finalialization if the algorithm hasn't been initilized.
729 if ( Gaudi::StateMachine::CONFIGURED == FSMState() ||
730 !isEnabled() ) return StatusCode::SUCCESS;
731
732 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::FINALIZE,m_state);
733
734 // lock the context service
735 Gaudi::Utils::AlgContext cnt
736 ( this , registerContext() ? contextSvc().get() : 0 ) ;
737
738 StatusCode sc(StatusCode::FAILURE);
739 // Invoke finalize() method of the derived class inside a try/catch clause
740 try {
741 // Order changed (bug #3903 overview: finalize and nested algorithms)
742 // Finalize first any sub-algoithms (it can be done more than once)
743 std::vector<Algorithm *>::iterator it;
744 bool fail(false);
745 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) {
746 if (!(*it)->sysFinalize().isSuccess()) {
747 fail = true;
748 }
749 }
750
751 { // limit the scope of the guard
752 Gaudi::Guards::AuditorGuard guard(this,
753 // check if we want to audit the initialize
754 (m_auditorFinalize) ? auditorSvc().get() : 0,
755 IAuditor::Finalize);
756 // Invoke the finalize() method of the derived class
757 sc = finalize();
758 }
759 if (fail) sc = StatusCode::FAILURE;
760
761 if( sc.isSuccess() ) {
762
763 // Release all sub-algorithms
764 for (it = m_subAlgms->begin(); it != m_subAlgms->end(); it++) {
765 (*it)->release();
766 }
767 // Indicate that this Algorithm has been finalized to prevent duplicate attempts
768 m_state = m_targetState;
769 }
770 }
771 catch( const GaudiException& Exception )
772 {
773 MsgStream log ( msgSvc() , name() );
774 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
775 << " is caught " << endmsg;
776 log << MSG::ERROR << Exception << endmsg;
777 Stat stat( chronoSvc() , Exception.tag() ) ;
778 sc = StatusCode::FAILURE;
779 }
780 catch( const std::exception& Exception )
781 {
782 MsgStream log ( msgSvc() , name() );
783 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
784 log << MSG::ERROR << Exception.what() << endmsg;
785 Stat stat( chronoSvc() , "*std::exception*" ) ;
786 sc = StatusCode::FAILURE;
787 }
788 catch( ... )
789 {
790 MsgStream log ( msgSvc() , name() );
791 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
792 Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
793 sc = StatusCode::FAILURE;
794 }
795 return sc;
796 }
797
798 StatusCode Algorithm::reinitialize() {
799 /* @TODO
800 * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
801 * is causing too many problems
802 *
803 // Default implementation is finalize+initialize
804 StatusCode sc = finalize();
805 if (sc.isFailure()) {
806 MsgStream log ( msgSvc() , name() );
807 log << MSG::ERROR << "reinitialize(): cannot be finalized" << endmsg;
808 return sc;
809 }
810 sc = initialize();
811 if (sc.isFailure()) {
812 MsgStream log ( msgSvc() , name() );
813 log << MSG::ERROR << "reinitialize(): cannot be initialized" << endmsg;
814 return sc;
815 }
816 */
817 return StatusCode::SUCCESS;
818 }
819
820 StatusCode Algorithm::restart() {
821 // Default implementation is stop+start
822 StatusCode sc = stop();
823 if (sc.isFailure()) {
824 MsgStream log ( msgSvc() , name() );
825 log << MSG::ERROR << "restart(): cannot be stopped" << endmsg;
826 return sc;
827 }
828 sc = start();
829 if (sc.isFailure()) {
830 MsgStream log ( msgSvc() , name() );
831 log << MSG::ERROR << "restart(): cannot be started" << endmsg;
832 return sc;
833 }
834 return StatusCode::SUCCESS;
835 }
836
837 const std::string& Algorithm::name() const {
838 return m_name;
839 }
840
841 const std::string& Algorithm::version() const {
842 return m_version;
843 }
844
845 bool Algorithm::isExecuted() const {
846 return m_isExecuted;
847 }
848
849 void Algorithm::setExecuted( bool state ) {
850 m_isExecuted = state;
851 }
852
853 void Algorithm::resetExecuted() {
854 m_isExecuted = false;
855 m_filterPassed = true;
856 }
857
858 bool Algorithm::isEnabled() const {
859 return m_isEnabled;
860 }
861
862 bool Algorithm::filterPassed() const {
863 return m_filterPassed;
864 }
865
866 void Algorithm::setFilterPassed( bool state ) {
867 m_filterPassed = state;
868 }
869
870 std::vector<Algorithm*>* Algorithm::subAlgorithms( ) const {
871 return m_subAlgms;
872 }
873
874 void Algorithm::setOutputLevel( int level ) {
875 if ( msgSvc() != 0 )
876 {
877 if ( MSG::NIL != level )
878 { msgSvc()->setOutputLevel( name(), level ) ; }
879 m_outputLevel = msgSvc()->outputLevel( name() );
880 }
881 }
882
883 #define serviceAccessor(METHOD,INTERFACE,NAME,MEMBER) \
884 SmartIF<INTERFACE>& Algorithm::METHOD() const { \
885 if ( !MEMBER.isValid() ) { \
886 MEMBER = service(NAME); \
887 if( !MEMBER.isValid() ) { \
888 throw GaudiException("Service [" NAME "] not found", name(), StatusCode::FAILURE); \
889 } \
890 } \
891 return MEMBER; \
892 }
893
894 //serviceAccessor(msgSvc, IMessageSvc, "MessageSvc", m_MS)
895 // Message service needs a special treatment to avoid infinite recursion
896 SmartIF<IMessageSvc>& Algorithm::msgSvc() const {
897 if ( !m_MS.isValid() ) {
898 //can not use service() method (infinite recursion!)
899 m_MS = serviceLocator(); // default message service
900 if( !m_MS.isValid() ) {
901 throw GaudiException("Service [MessageSvc] not found", name(), StatusCode::FAILURE);
902 }
903 }
904 return m_MS;
905 }
906
907 serviceAccessor(auditorSvc, IAuditorSvc, "AuditorSvc", m_pAuditorSvc)
908 serviceAccessor(chronoSvc, IChronoStatSvc, "ChronoStatSvc", m_CSS)
909 serviceAccessor(detSvc, IDataProviderSvc, "DetectorDataSvc", m_DDS)
910 serviceAccessor(detCnvSvc, IConversionSvc, "DetectorPersistencySvc", m_DCS)
911 serviceAccessor(eventSvc, IDataProviderSvc, "EventDataSvc", m_EDS)
912 serviceAccessor(eventCnvSvc, IConversionSvc, "EventPersistencySvc", m_ECS)
913 serviceAccessor(histoSvc, IHistogramSvc, "HistogramDataSvc", m_HDS)
914 serviceAccessor(exceptionSvc, IExceptionSvc, "ExceptionSvc", m_EXS)
915 serviceAccessor(ntupleSvc, INTupleSvc, "NTupleSvc", m_NTS)
916 //serviceAccessor(atupleSvc, IAIDATupleSvc, "AIDATupleSvc", m_ATS)
917 serviceAccessor(randSvc, IRndmGenSvc, "RndmGenSvc", m_RGS)
918 serviceAccessor(toolSvc, IToolSvc, "ToolSvc", m_ptoolSvc)
919 serviceAccessor(contextSvc, IAlgContextSvc,"AlgContextSvc", m_contextSvc)
920
921
922 // Obsoleted name, kept due to the backwards compatibility
923 SmartIF<IChronoStatSvc>& Algorithm::chronoStatService() const {
924 return chronoSvc();
925 }
926 // Obsoleted name, kept due to the backwards compatibility
927 SmartIF<IDataProviderSvc>& Algorithm::detDataService() const {
928 return detSvc();
929 }
930 // Obsoleted name, kept due to the backwards compatibility
931 SmartIF<IConversionSvc>& Algorithm::detDataCnvService() const {
932 return detCnvSvc();
933 }
934 // Obsoleted name, kept due to the backwards compatibility
935 SmartIF<IDataProviderSvc>& Algorithm::eventDataService() const {
936 return eventSvc();
937 }
938 // Obsoleted name, kept due to the backwards compatibility
939 SmartIF<IConversionSvc>& Algorithm::eventDataCnvService() const {
940 return eventCnvSvc();
941 }
942 // Obsoleted name, kept due to the backwards compatibility
943 SmartIF<IHistogramSvc>& Algorithm::histogramDataService() const {
944 return histoSvc();
945 }
946 // Obsoleted name, kept due to the backwards compatibility
947 SmartIF<IMessageSvc>& Algorithm::messageService() const {
948 return msgSvc();
949 }
950 // Obsoleted name, kept due to the backwards compatibility
951 SmartIF<INTupleSvc>& Algorithm::ntupleService() const {
952 return ntupleSvc();
953 }
954
955 #if 0
956 IAuditorSvc* Algorithm::auditorSvc() const {
957 if ( 0 == m_pAuditorSvc ) {
958 StatusCode sc = service( "AuditorSvc", m_pAuditorSvc, true );
959 if( sc.isFailure() ) {
960 throw GaudiException("Service [AuditorSvc] not found", name(), sc);
961 }
962 }
963 return m_pAuditorSvc;
964 }
965
966 IChronoStatSvc* Algorithm::chronoSvc() const {
967 if ( 0 == m_CSS ) {
968 StatusCode sc = service( "ChronoStatSvc", m_CSS, true );
969 if( sc.isFailure() ) {
970 throw GaudiException("Service [ChronoStatSvc] not found", name(), sc);
971 }
972 }
973 return m_CSS;
974 }
975
976 IDataProviderSvc* Algorithm::detSvc() const {
977 if ( 0 == m_DDS ) {
978 StatusCode sc = service( "DetectorDataSvc", m_DDS, true );
979 if( sc.isFailure() ) {
980 throw GaudiException("Service [DetectorDataSvc] not found", name(), sc);
981 }
982 }
983 return m_DDS;
984 }
985
986 IConversionSvc* Algorithm::detCnvSvc() const {
987 if ( 0 == m_DCS ) {
988 StatusCode sc = service( "DetectorPersistencySvc", m_DCS, true );
989 if( sc.isFailure() ) {
990 throw GaudiException("Service [DetectorPersistencySvc] not found",
991 name(), sc);
992 }
993 }
994 return m_DCS;
995 }
996
997 IDataProviderSvc* Algorithm::eventSvc() const {
998 if ( 0 == m_EDS ) {
999 StatusCode sc = service( "EventDataSvc", m_EDS, true );
1000 if( sc.isFailure() ) {
1001 throw GaudiException("Service [EventDataSvc] not found", name(), sc);
1002 }
1003 }
1004 return m_EDS;
1005 }
1006
1007 IConversionSvc* Algorithm::eventCnvSvc() const {
1008 if ( 0 == m_ECS ) {
1009 StatusCode sc = service( "EventPersistencySvc", m_ECS, true );
1010 if( sc.isFailure() ) {
1011 throw GaudiException("Service [EventPersistencySvc] not found",
1012 name(), sc);
1013 }
1014 }
1015 return m_ECS;
1016 }
1017
1018 IHistogramSvc* Algorithm::histoSvc() const {
1019 if ( 0 == m_HDS ) {
1020 StatusCode sc = service( "HistogramDataSvc", m_HDS, true );
1021 if( sc.isFailure() ) {
1022 throw GaudiException("Service [HistogramDataSvc] not found", name(), sc);
1023 }
1024 }
1025 return m_HDS;
1026 }
1027
1028 IExceptionSvc* Algorithm::exceptionSvc() const {
1029 if ( 0 == m_EXS ) {
1030 StatusCode sc = service( "ExceptionSvc", m_EXS, true );
1031 if( sc.isFailure() ) {
1032 throw GaudiException("Service [ExceptionSvc] not found", name(), sc);
1033 }
1034 }
1035 return m_EXS;
1036 }
1037
1038 IMessageSvc* Algorithm::msgSvc() const {
1039 if ( 0 == m_MS ) {
1040 //can not use service() method (infinite recursion!)
1041 StatusCode sc = serviceLocator()->service( "MessageSvc", m_MS, true );
1042 if( sc.isFailure() ) {
1043 throw GaudiException("Service [MessageSvc] not found", name(), sc);
1044 }
1045 }
1046 return m_MS;
1047 }
1048
1049 INTupleSvc* Algorithm::ntupleSvc() const {
1050 if ( 0 == m_NTS ) {
1051 StatusCode sc = service( "NTupleSvc", m_NTS, true );
1052 if( sc.isFailure() ) {
1053 throw GaudiException("Service [NTupleSvc] not found", name(), sc);
1054 }
1055 }
1056 return m_NTS;
1057 }
1058
1059 // AIDATupleSvc:
1060 // IAIDATupleSvc* Algorithm::atupleSvc() const {
1061 // if ( 0 == m_ATS ) {
1062 // StatusCode sc = service( "AIDATupleSvc", m_ATS, true );
1063 // if( sc.isFailure() ) {
1064 // throw GaudiException("Service [AIDATupleSvc] not found", name(), sc);
1065 // }
1066 // }
1067 // return m_ATS;
1068 // }
1069
1070 IRndmGenSvc* Algorithm::randSvc() const {
1071 if ( 0 == m_RGS ) {
1072 StatusCode sc = service( "RndmGenSvc", m_RGS, true );
1073 if( sc.isFailure() ) {
1074 throw GaudiException("Service [RndmGenSvc] not found", name(), sc);
1075 }
1076 }
1077 return m_RGS;
1078 }
1079
1080 IToolSvc* Algorithm::toolSvc() const {
1081 if ( 0 == m_ptoolSvc ) {
1082 StatusCode sc = service( "ToolSvc", m_ptoolSvc, true );
1083 if( sc.isFailure() ) {
1084 throw GaudiException("Service [ToolSvc] not found", name(), sc);
1085 }
1086 }
1087 return m_ptoolSvc;
1088 }
1089 #endif
1090
1091 SmartIF<ISvcLocator>& Algorithm::serviceLocator() const {
1092 return *const_cast<SmartIF<ISvcLocator>*>(&m_pSvcLocator);
1093 }
1094
1095 // Use the job options service to set declared properties
1096 StatusCode Algorithm::setProperties() {
1097 if( m_pSvcLocator != 0 ) {
1098 SmartIF<IJobOptionsSvc> jos(m_pSvcLocator->service("JobOptionsSvc"));
1099 if( jos.isValid() ) {
1100 // set first generic Properties
1101 StatusCode sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this );
1102 if( sc.isFailure() ) return StatusCode::FAILURE;
1103
1104 // set specific Properties
1105 if (isGaudiThreaded(name())) {
1106 if(jos->setMyProperties( name(), this ).isFailure()) {
1107 return StatusCode::FAILURE;
1108 }
1109 }
1110 return sc;
1111 }
1112 }
1113 return StatusCode::FAILURE;
1114 }
1115
1116 StatusCode Algorithm::createSubAlgorithm(const std::string& type,
1117 const std::string& name,
1118 Algorithm*& pSubAlgorithm) {
1119 if( m_pSvcLocator == 0 ) return StatusCode::FAILURE;
1120
1121 SmartIF<IAlgManager> am(m_pSvcLocator);
1122 if ( !am.isValid() ) return StatusCode::FAILURE;
1123
1124 // Maybe modify the AppMgr interface to return Algorithm* ??
1125 IAlgorithm *tmp;
1126 StatusCode sc = am->createAlgorithm
1127 (type, name+getGaudiThreadIDfromName(Algorithm::name()), tmp);
1128 if( sc.isFailure() ) return StatusCode::FAILURE;
1129
1130 try{
1131 pSubAlgorithm = dynamic_cast<Algorithm*>(tmp);
1132 m_subAlgms->push_back(pSubAlgorithm);
1133 } catch(...){
1134 sc = StatusCode::FAILURE;
1135 }
1136 return sc;
1137 }
1138
1139 // IProperty implementation
1140 // Delegate to the Property manager
1141 StatusCode Algorithm::setProperty(const Property& p) {
1142 return m_propertyMgr->setProperty(p);
1143 }
1144 StatusCode Algorithm::setProperty(const std::string& s) {
1145 return m_propertyMgr->setProperty(s);
1146 }
1147 StatusCode Algorithm::setProperty(const std::string& n, const std::string& v) {
1148 return m_propertyMgr->setProperty(n,v);
1149 }
1150 StatusCode Algorithm::getProperty(Property* p) const {
1151 return m_propertyMgr->getProperty(p);
1152 }
1153 const Property& Algorithm::getProperty( const std::string& name) const{
1154 return m_propertyMgr->getProperty(name);
1155 }
1156 StatusCode Algorithm::getProperty(const std::string& n, std::string& v ) const {
1157 return m_propertyMgr->getProperty(n,v);
1158 }
1159 const std::vector<Property*>& Algorithm::getProperties( ) const {
1160 return m_propertyMgr->getProperties();
1161 }
1162
1166 void
1167 Algorithm::initOutputLevel(Property& /*prop*/)
1168 {
1169 // do nothing... yet ?
1170 }
1171
1172 StatusCode
1173 Algorithm::service_i(const std::string& svcName,
1174 bool createIf,
1175 const InterfaceID& iid,
1176 void** ppSvc) const {
1177 const ServiceLocatorHelper helper(*serviceLocator(), *this);
1178 return helper.getService(svcName, createIf, iid, ppSvc);
1179 }
1180
1181 StatusCode
1182 Algorithm::service_i(const std::string& svcType,
1183 const std::string& svcName,
1184 const InterfaceID& iid,
1185 void** ppSvc) const {
1186 const ServiceLocatorHelper helper(*serviceLocator(), *this);
1187 return helper.createService(svcType, svcName, iid, ppSvc);
1188 }
1189
1190 SmartIF<IService> Algorithm::service(const std::string& name, const bool createIf, const bool quiet) const {
1191 const ServiceLocatorHelper helper(*serviceLocator(), *this);
1192 return helper.service(name, quiet, createIf);
1193 }