| # 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
AlgTool.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/AlgTool.h"
3 #include "GaudiKernel/IMessageSvc.h"
4 #include "GaudiKernel/ISvcLocator.h"
5 #include "GaudiKernel/IJobOptionsSvc.h"
6
7 #include "GaudiKernel/Algorithm.h"
8 #include "GaudiKernel/Service.h"
9 #include "GaudiKernel/Auditor.h"
10 #include "GaudiKernel/System.h"
11 #include "GaudiKernel/GaudiException.h"
12 #include "GaudiKernel/ServiceLocatorHelper.h"
13 #include "GaudiKernel/ThreadGaudi.h"
14 #include "GaudiKernel/Guards.h"
15
16 //------------------------------------------------------------------------------
17 StatusCode AlgTool::queryInterface
18 ( const InterfaceID& riid ,
19 void** ppvi )
20 //------------------------------------------------------------------------------
21 {
22 if ( 0 == ppvi ) { return StatusCode::FAILURE ; } // RETURN
23 StatusCode sc = base_class::queryInterface(riid,ppvi);
24 if (sc.isSuccess()) {
25 return sc;
26 }
27 else {
28 for ( InterfaceList::iterator it = m_interfaceList.begin() ;
29 m_interfaceList.end() != it ; ++it )
30 {
31 if ( !it->first.versionMatch ( riid ) ) { continue ; }
32 // OK
33 *ppvi = it->second ;
34 addRef() ;
35 return SUCCESS ; // RETURN
36 }
37 *ppvi = 0 ;
38 return NO_INTERFACE ; // RETURN
39 }
40 // cannot reach this point
41 }
42 //------------------------------------------------------------------------------
43 void AlgTool::declInterface( const InterfaceID& iid, void* ii)
44 //------------------------------------------------------------------------------
45 {
46 m_interfaceList.push_back(std::make_pair(iid, ii));
47 }
48
49
50 //------------------------------------------------------------------------------
51 const std::string& AlgTool::name() const
52 //------------------------------------------------------------------------------
53 {
54 return m_name;
55 }
56
57 //------------------------------------------------------------------------------
58 const std::string& AlgTool::type() const
59 //------------------------------------------------------------------------------
60 {
61 return m_type;
62 }
63
64 //------------------------------------------------------------------------------
65 const IInterface* AlgTool::parent() const
66 //------------------------------------------------------------------------------
67 {
68 return m_parent;
69 }
70
71 //------------------------------------------------------------------------------
72 ISvcLocator* AlgTool::serviceLocator() const
73 //------------------------------------------------------------------------------
74 {
75 return m_svcLocator;
76 }
77
78 //------------------------------------------------------------------------------
79 IMessageSvc* AlgTool::msgSvc() const
80 //------------------------------------------------------------------------------
81 {
82 return m_messageSvc;
83 }
84
85 //------------------------------------------------------------------------------
86 IToolSvc* AlgTool::toolSvc() const
87 //------------------------------------------------------------------------------
88 {
89 if ( 0 == m_ptoolSvc ) {
90 StatusCode sc = service( "ToolSvc", m_ptoolSvc, true );
91 if( sc.isFailure() ) {
92 throw GaudiException("Service [ToolSvc] not found", name(), sc);
93 }
94 }
95 return m_ptoolSvc;
96 }
97
98 //------------------------------------------------------------------------------
99 StatusCode AlgTool::setProperty(const Property& p)
100 //------------------------------------------------------------------------------
101 {
102 return m_propertyMgr->setProperty(p);
103 }
104
105 //------------------------------------------------------------------------------
106 StatusCode AlgTool::setProperty(const std::string& s)
107 //------------------------------------------------------------------------------
108 {
109 return m_propertyMgr->setProperty(s);
110 }
111
112 //------------------------------------------------------------------------------
113 StatusCode AlgTool::setProperty(const std::string& n, const std::string& v)
114 //------------------------------------------------------------------------------
115 {
116 return m_propertyMgr->setProperty(n,v);
117 }
118
119 //------------------------------------------------------------------------------
120 StatusCode AlgTool::getProperty(Property* p) const
121 //------------------------------------------------------------------------------
122 {
123 return m_propertyMgr->getProperty(p);
124 }
125
126 //------------------------------------------------------------------------------
127 const Property& AlgTool::getProperty(const std::string& n) const
128 {
129 return m_propertyMgr->getProperty(n);
130 }
131
132 //------------------------------------------------------------------------------
133 StatusCode AlgTool::getProperty(const std::string& n, std::string& v ) const
134 //------------------------------------------------------------------------------
135 {
136 return m_propertyMgr->getProperty(n,v);
137 }
138
139 //------------------------------------------------------------------------------
140 const std::vector<Property*>& AlgTool::getProperties() const
141 //------------------------------------------------------------------------------
142 {
143 return m_propertyMgr->getProperties();
144 }
145
146 //------------------------------------------------------------------------------
147 StatusCode AlgTool::setProperties()
148 //------------------------------------------------------------------------------
149 {
150 if( m_svcLocator == 0) {
151 return StatusCode::FAILURE;
152 }
153 SmartIF<IJobOptionsSvc> jos(m_svcLocator->service("JobOptionsSvc"));
154 if( !jos.isValid() ) return StatusCode::FAILURE;
155
156 // set first generic Properties
157 StatusCode sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this );
158 if( sc.isFailure() ) return StatusCode::FAILURE;
159
160 // set specific Properties
161 if (isGaudiThreaded(name())) {
162 if(jos->setMyProperties( name(), this ).isFailure()) {
163 return StatusCode::FAILURE;
164 }
165 }
166
167 // Change my own outputlevel
168 if ( 0 != m_messageSvc )
169 {
170 if ( MSG::NIL != m_outputLevel )
171 { m_messageSvc -> setOutputLevel ( name () , m_outputLevel ) ; }
172 m_outputLevel = m_messageSvc -> outputLevel ( name () ) ;
173 }
174
175 return StatusCode::SUCCESS;
176 }
177
178 //------------------------------------------------------------------------------
179 AlgTool::AlgTool( const std::string& type,
180 const std::string& name,
181 const IInterface* parent)
182 //------------------------------------------------------------------------------
183 : m_outputLevel ( MSG::NIL )
184 , m_type ( type )
185 , m_name ( name )
186 , m_parent ( parent )
187 , m_svcLocator ( 0 )
188 , m_messageSvc ( 0 )
189 , m_ptoolSvc ( 0 )
190 , m_pMonitorSvc ( NULL )
191 , m_propertyMgr ( new PropertyMgr() )
192 , m_interfaceList ( )
193 , m_threadID ( )
194 , m_pAuditorSvc ( 0 )
195 , m_auditInit ( false )
196 , m_state ( Gaudi::StateMachine::CONFIGURED )
197 , m_targetState ( Gaudi::StateMachine::CONFIGURED )
198 {
199 addRef(); // Initial count set to 1
200
201 declareProperty( "MonitorService", m_monitorSvcName = "MonitorSvc" );
202
203 { // get the "OutputLevel" property from parent
204 const Property* _p = Gaudi::Utils::getProperty ( parent , "OutputLevel") ;
205 if ( 0 != _p ) { m_outputLevel.assign( *_p ) ; }
206 declareProperty ( "OutputLevel" , m_outputLevel ) ;
207 m_outputLevel.declareUpdateHandler(&AlgTool::initOutputLevel, this);
208 }
209
210 IInterface* _p = const_cast<IInterface*> ( parent ) ;
211
212 if ( Algorithm* _alg = dynamic_cast<Algorithm*> ( _p ) )
213 {
214 m_svcLocator = _alg -> serviceLocator () ;
215 m_messageSvc = _alg -> msgSvc () ;
216 m_threadID = getGaudiThreadIDfromName ( _alg -> name() ) ;
217 }
218 else if ( Service* _svc = dynamic_cast<Service*> ( _p ) )
219 {
220 m_svcLocator = _svc -> serviceLocator () ;
221 m_messageSvc = _svc -> msgSvc () ;
222 m_threadID = getGaudiThreadIDfromName ( _svc -> name() ) ;
223 }
224 else if ( AlgTool* _too = dynamic_cast<AlgTool*> ( _p ) )
225 {
226 m_svcLocator = _too -> m_svcLocator;
227 m_messageSvc = _too -> m_messageSvc;
228 m_threadID = getGaudiThreadIDfromName ( _too ->m_threadID ) ;
229 }
230 else if ( Auditor* _aud = dynamic_cast<Auditor*> ( _p ) )
231 {
232 m_svcLocator = _aud -> serviceLocator() ;
233 m_messageSvc = _aud -> msgSvc() ;
234 m_threadID = getGaudiThreadIDfromName ( _aud -> name() ) ;
235 }
236 else
237 {
238 throw GaudiException
239 ( "Failure to create tool '"
240 + type + "/" + name + "': illegal parent type '"
241 + System::typeinfoName(typeid(*_p)) + "'", "AlgTool", 0 );
242 }
243
244
245 { // audit tools
246 SmartIF<IProperty> appMgr(m_svcLocator->service("ApplicationMgr"));
247 if ( !appMgr.isValid() ) {
248 throw GaudiException("Could not locate ApplicationMgr","AlgTool",0);
249 }
250 const Property* p = Gaudi::Utils::getProperty( appMgr , "AuditTools");
251 if ( 0 != p ) { m_auditInit.assign ( *p ) ; }
252 declareProperty ( "AuditTools", m_auditInit );
253 bool audit = m_auditInit.value();
254 // Declare common AlgTool properties with their defaults
255 declareProperty ( "AuditInitialize" , m_auditorInitialize = audit ) ;
256 declareProperty ( "AuditStart" , m_auditorStart = audit ) ;
257 declareProperty ( "AuditStop" , m_auditorStop = audit ) ;
258 declareProperty ( "AuditFinalize" , m_auditorFinalize = audit ) ;
259 }
260
261 // check thread ID and try if tool name indicates thread ID
262 if ( m_threadID.empty() )
263 { m_threadID = getGaudiThreadIDfromName ( AlgTool::name() ) ; }
264 }
265
266 //-----------------------------------------------------------------------------
267 StatusCode AlgTool::sysInitialize() {
268 //-----------------------------------------------------------------------------
269 StatusCode sc;
270
271 try {
272 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::INITIALIZE,m_state);
273 Gaudi::Guards::AuditorGuard guard(this,
274 // check if we want to audit the initialize
275 (m_auditorInitialize) ? auditorSvc() : 0,
276 IAuditor::Initialize);
277 sc = initialize();
278 if (sc.isSuccess())
279 m_state = m_targetState;
280 return sc;
281 }
282 catch( const GaudiException& Exception ) {
283 MsgStream log ( msgSvc() , name() + ".sysInitialize()" );
284 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
285 << " is caught " << endmsg;
286 log << MSG::ERROR << Exception << endmsg;
287 }
288 catch( const std::exception& Exception ) {
289 MsgStream log ( msgSvc() , name() + ".sysInitialize()" );
290 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
291 log << MSG::ERROR << Exception.what() << endmsg;
292 }
293 catch( ... ) {
294 MsgStream log ( msgSvc() , name() + ".sysInitialize()" );
295 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
296 }
297 return StatusCode::FAILURE ;
298
299 }
300
301 //------------------------------------------------------------------------------
302 StatusCode AlgTool::initialize()
303 //------------------------------------------------------------------------------
304 {
305 // For the time being there is nothing to be done here.
306 // Setting the properties is done by the ToolSvc calling setProperties()
307 // explicitly.
308 return StatusCode::SUCCESS;
309 }
310
311 //-----------------------------------------------------------------------------
312 StatusCode AlgTool::sysStart() {
313 //-----------------------------------------------------------------------------
314 StatusCode sc;
315
316 try {
317 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::START,m_state);
318 Gaudi::Guards::AuditorGuard guard(this,
319 // check if we want to audit the initialize
320 (m_auditorStart) ? auditorSvc() : 0,
321 IAuditor::Start);
322 sc = start();
323 if (sc.isSuccess())
324 m_state = m_targetState;
325 return sc;
326 }
327 catch( const GaudiException& Exception ) {
328 MsgStream log ( msgSvc() , name() + ".sysStart()" );
329 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
330 << " is caught " << endmsg;
331 log << MSG::ERROR << Exception << endmsg;
332 }
333 catch( const std::exception& Exception ) {
334 MsgStream log ( msgSvc() , name() + ".sysStart()" );
335 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
336 log << MSG::ERROR << Exception.what() << endmsg;
337 }
338 catch( ... ) {
339 MsgStream log ( msgSvc() , name() + ".sysStart()" );
340 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
341 }
342 return StatusCode::FAILURE ;
343
344 }
345
346 //------------------------------------------------------------------------------
347 StatusCode AlgTool::start()
348 //------------------------------------------------------------------------------
349 {
350 // For the time being there is nothing to be done here.
351 return StatusCode::SUCCESS;
352 }
353
354 //-----------------------------------------------------------------------------
355 StatusCode AlgTool::sysStop() {
356 //-----------------------------------------------------------------------------
357 StatusCode sc;
358
359 try {
360 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::STOP,m_state);
361 Gaudi::Guards::AuditorGuard guard(this,
362 // check if we want to audit the initialize
363 (m_auditorStop) ? auditorSvc() : 0,
364 IAuditor::Stop);
365 sc = stop();
366 if (sc.isSuccess())
367 m_state = m_targetState;
368 return sc;
369 }
370 catch( const GaudiException& Exception ) {
371 MsgStream log ( msgSvc() , name() + ".sysStop()" );
372 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
373 << " is caught " << endmsg;
374 log << MSG::ERROR << Exception << endmsg;
375 }
376 catch( const std::exception& Exception ) {
377 MsgStream log ( msgSvc() , name() + ".sysStop()" );
378 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
379 log << MSG::ERROR << Exception.what() << endmsg;
380 }
381 catch( ... ) {
382 MsgStream log ( msgSvc() , name() + ".sysStop()" );
383 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
384 }
385 return StatusCode::FAILURE ;
386
387 }
388
389 //------------------------------------------------------------------------------
390 StatusCode AlgTool::stop()
391 //------------------------------------------------------------------------------
392 {
393 // For the time being there is nothing to be done here.
394 return StatusCode::SUCCESS;
395 }
396
397 //-----------------------------------------------------------------------------
398 StatusCode AlgTool::sysFinalize() {
399 //-----------------------------------------------------------------------------
400
401 StatusCode sc;
402
403 try {
404 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::FINALIZE,m_state);
405 Gaudi::Guards::AuditorGuard guard(this,
406 // check if we want to audit the initialize
407 (m_auditorFinalize) ? auditorSvc() : 0,
408 IAuditor::Finalize);
409 sc = finalize();
410 if (sc.isSuccess())
411 m_state = m_targetState;
412 return sc;
413 }
414 catch( const GaudiException& Exception ) {
415 MsgStream log ( msgSvc() , name() + ".sysFinalize()" );
416 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
417 << " is caught " << endmsg;
418 log << MSG::ERROR << Exception << endmsg;
419 }
420 catch( const std::exception& Exception ) {
421 MsgStream log ( msgSvc() , name() + ".sysFinalize()" );
422 log << MSG::FATAL << " Standard std::exception is caught " << endmsg;
423 log << MSG::ERROR << Exception.what() << endmsg;
424 }
425 catch( ... ) {
426 MsgStream log ( msgSvc() , name() + ".sysFinalize()" );
427 log << MSG::FATAL << "UNKNOWN Exception is caught " << endmsg;
428 }
429 return StatusCode::FAILURE;
430
431 }
432 //------------------------------------------------------------------------------
433 StatusCode AlgTool::finalize()
434 //------------------------------------------------------------------------------
435 {
436 // For the time being there is nothing to be done here.
437 return StatusCode::SUCCESS;
438 }
439
440 //-----------------------------------------------------------------------------
441 StatusCode AlgTool::sysReinitialize() {
442 //-----------------------------------------------------------------------------
443 StatusCode sc;
444
445 // Check that the current status is the correct one.
446 if ( Gaudi::StateMachine::INITIALIZED != FSMState() ) {
447 MsgStream log ( msgSvc() , name() );
448 log << MSG::ERROR
449 << "sysReinitialize(): cannot reinitialize tool not initialized"
450 << endmsg;
451 return StatusCode::FAILURE;
452 }
453
454 try {
455 Gaudi::Guards::AuditorGuard guard(this,
456 // check if we want to audit the initialize
457 (m_auditorReinitialize) ? auditorSvc() : 0,
458 IAuditor::ReInitialize);
459 sc = reinitialize();
460 return sc;
461 }
462 catch( const GaudiException& Exception ) {
463 MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
464 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
465 << " is caught" << endmsg;
466 log << MSG::ERROR << Exception << endmsg;
467 }
468 catch( const std::exception& Exception ) {
469 MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
470 log << MSG::FATAL << " Standard std::exception is caught" << endmsg;
471 log << MSG::ERROR << Exception.what() << endmsg;
472 }
473 catch( ... ) {
474 MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
475 log << MSG::FATAL << "UNKNOWN Exception is caught" << endmsg;
476 }
477 return StatusCode::FAILURE ;
478
479 }
480
481 //------------------------------------------------------------------------------
482 StatusCode AlgTool::reinitialize()
483 //------------------------------------------------------------------------------
484 {
485 /* @TODO
486 * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
487 * is causing too many problems
488 *
489 // Default implementation is finalize+initialize
490 StatusCode sc = finalize();
491 if (sc.isFailure()) {
492 MsgStream log ( msgSvc() , name() );
493 log << MSG::ERROR << "reinitialize(): cannot be finalized" << endmsg;
494 return sc;
495 }
496 sc = initialize();
497 if (sc.isFailure()) {
498 MsgStream log ( msgSvc() , name() );
499 log << MSG::ERROR << "reinitialize(): cannot be initialized" << endmsg;
500 return sc;
501 }
502 */
503 return StatusCode::SUCCESS;
504 }
505
506 //-----------------------------------------------------------------------------
507 StatusCode AlgTool::sysRestart() {
508 //-----------------------------------------------------------------------------
509 StatusCode sc;
510
511 // Check that the current status is the correct one.
512 if ( Gaudi::StateMachine::RUNNING != FSMState() ) {
513 MsgStream log ( msgSvc() , name() );
514 log << MSG::ERROR
515 << "sysRestart(): cannot reinitialize tool not started"
516 << endmsg;
517 return StatusCode::FAILURE;
518 }
519
520 try {
521 m_targetState = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::START,m_state);
522 Gaudi::Guards::AuditorGuard guard(this,
523 // check if we want to audit the initialize
524 (m_auditorRestart) ? auditorSvc() : 0,
525 IAuditor::ReStart);
526 sc = restart();
527 return sc;
528 }
529 catch( const GaudiException& Exception ) {
530 MsgStream log ( msgSvc() , name() + ".sysRestart()" );
531 log << MSG::FATAL << " Exception with tag=" << Exception.tag()
532 << " is caught" << endmsg;
533 log << MSG::ERROR << Exception << endmsg;
534 }
535 catch( const std::exception& Exception ) {
536 MsgStream log ( msgSvc() , name() + ".sysRestart()" );
537 log << MSG::FATAL << " Standard std::exception is caught" << endmsg;
538 log << MSG::ERROR << Exception.what() << endmsg;
539 }
540 catch( ... ) {
541 MsgStream log ( msgSvc() , name() + ".sysRestart()" );
542 log << MSG::FATAL << "UNKNOWN Exception is caught" << endmsg;
543 }
544 return StatusCode::FAILURE ;
545
546 }
547
548 //------------------------------------------------------------------------------
549 StatusCode AlgTool::restart()
550 //------------------------------------------------------------------------------
551 {
552 // Default implementation is stop+start
553 StatusCode sc = stop();
554 if (sc.isFailure()) {
555 MsgStream log ( msgSvc() , name() );
556 log << MSG::ERROR << "restart(): cannot be stopped" << endmsg;
557 return sc;
558 }
559 sc = start();
560 if (sc.isFailure()) {
561 MsgStream log ( msgSvc() , name() );
562 log << MSG::ERROR << "restart(): cannot be started" << endmsg;
563 return sc;
564 }
565 return StatusCode::SUCCESS;
566 }
567
568 //------------------------------------------------------------------------------
569 AlgTool::~AlgTool()
570 //------------------------------------------------------------------------------
571 {
572 delete m_propertyMgr;
573 if( m_ptoolSvc ) m_ptoolSvc->release();
574 if( m_pAuditorSvc ) m_pAuditorSvc->release();
575 if ( m_pMonitorSvc ) { m_pMonitorSvc->undeclareAll(this); m_pMonitorSvc->release(); }
576 }
577
578 //------------------------------------------------------------------------------
580 StatusCode
581 AlgTool::service_i(const std::string& svcName,
582 bool createIf,
583 const InterfaceID& iid,
584 void** ppSvc) const {
585 const ServiceLocatorHelper helper(*serviceLocator(), *this);
586 return helper.getService(svcName, createIf, iid, ppSvc);
587 }
588
589 //------------------------------------------------------------------------------
590 StatusCode
591 AlgTool::service_i(const std::string& svcType,
592 const std::string& svcName,
593 const InterfaceID& iid,
594 void** ppSvc) const {
595 const ServiceLocatorHelper helper(*serviceLocator(), *this);
596 return helper.createService(svcType, svcName, iid, ppSvc);
597 }
598
599 SmartIF<IService> AlgTool::service(const std::string& name, const bool createIf, const bool quiet) const {
600 const ServiceLocatorHelper helper(*serviceLocator(), *this);
601 return helper.service(name, quiet, createIf);
602 }
603
604 //-----------------------------------------------------------------------------
605 IAuditorSvc* AlgTool::auditorSvc() const {
606 //---------------------------------------------------------------------------
607 if ( 0 == m_pAuditorSvc ) {
608 StatusCode sc = service( "AuditorSvc", m_pAuditorSvc, true );
609 if( sc.isFailure() ) {
610 throw GaudiException("Service [AuditorSvc] not found", name(), sc);
611 }
612 }
613 return m_pAuditorSvc;
614 }
615
616
617 //-----------------------------------------------------------------------------
618 void AlgTool::initOutputLevel(Property& /*prop*/) {
619 //-----------------------------------------------------------------------------
620 // do nothing... yet ?
621 }
622