00001
00002
00003
00004
00005
00006
00007
00008 #include "GaudiAlg/Sequencer.h"
00009
00010 #include "GaudiKernel/IAlgManager.h"
00011 #include "GaudiKernel/ISvcLocator.h"
00012 #include "GaudiKernel/AlgFactory.h"
00013 #include "GaudiKernel/MsgStream.h"
00014 #include "GaudiKernel/Chrono.h"
00015 #include "GaudiKernel/Stat.h"
00016 #include "GaudiKernel/GaudiException.h"
00017
00021 Sequencer::Sequencer( const std::string& name, ISvcLocator* pSvcLocator )
00022 : Algorithm( name, pSvcLocator ),
00023 m_branchFilterPassed( false )
00024 {
00025
00026
00027 m_branchAlgs = new std::vector<Algorithm*>();
00028
00029
00030 declareProperty( "Members", m_names );
00031 declareProperty( "BranchMembers", m_branchNames );
00032 declareProperty( "StopOverride", m_stopOverride=false );
00033
00034
00035 m_names.declareUpdateHandler ( &Sequencer::membershipHandler , this );
00036 m_branchNames.declareUpdateHandler( &Sequencer::branchMembershipHandler, this );
00037
00038 }
00039
00043 Sequencer::~Sequencer()
00044 {
00045 delete m_branchAlgs;
00046 }
00047
00048 StatusCode
00049 Sequencer::initialize()
00050 {
00051 StatusCode result = StatusCode::SUCCESS;
00052 MsgStream log( msgSvc( ), name( ) );
00053
00054 std::vector<Algorithm*>* theAlgs;
00055 std::vector<Algorithm*>::iterator it;
00056 std::vector<Algorithm*>::iterator itend;
00057
00058 result = decodeMemberNames();
00059 if( result.isFailure() ) {
00060 log << MSG::ERROR << "Unable to configure one or more sequencer members " << endmsg;
00061 return result;
00062 }
00063 result = decodeBranchMemberNames();
00064 if( result.isFailure() ) {
00065 log << MSG::ERROR << "Unable to configure one or more branch members " << endmsg;
00066 return result;
00067 }
00068
00069
00070 theAlgs = subAlgorithms( );
00071 itend = theAlgs->end( );
00072 for (it = theAlgs->begin(); it != itend; it++) {
00073 Algorithm* theAlgorithm = (*it);
00074 result = theAlgorithm->sysInitialize( );
00075 if( result.isFailure() ) {
00076 log << MSG::ERROR << "Unable to initialize Algorithm " << theAlgorithm->name() << endmsg;
00077 return result;
00078 }
00079 }
00080
00081
00082 theAlgs = branchAlgorithms( );
00083 itend = theAlgs->end( );
00084 for (it = theAlgs->begin(); it != itend; it++) {
00085 Algorithm* theAlgorithm = (*it);
00086 result = theAlgorithm->sysInitialize( );
00087 if( result.isFailure() ) {
00088 log << MSG::ERROR << "Unable to initialize Algorithm " << theAlgorithm->name() << endmsg;
00089 return result;
00090 }
00091 }
00092
00093 return result;
00094 }
00095
00096 StatusCode
00097 Sequencer::reinitialize()
00098 {
00099
00100 if ( isEnabled( ) ) {
00101
00102
00103
00104 std::vector<Algorithm*>* theAlgms = subAlgorithms( );
00105 std::vector<Algorithm*>::iterator it;
00106 std::vector<Algorithm*>::iterator itend = theAlgms->end( );
00107 for (it = theAlgms->begin(); it != itend; it++) {
00108 Algorithm* theAlgorithm = (*it);
00109 if ( ! theAlgorithm->isEnabled( ) ) {
00110 theAlgorithm->reinitialize( ).ignore();
00111 }
00112 }
00113
00114
00115 theAlgms = branchAlgorithms( );
00116 itend = theAlgms->end( );
00117 for (it = theAlgms->begin(); it != itend; it++) {
00118 Algorithm* theAlgorithm = (*it);
00119 if ( ! theAlgorithm->isEnabled( ) ) {
00120 theAlgorithm->reinitialize( ).ignore();
00121 }
00122 }
00123
00124 }
00125 return StatusCode::SUCCESS;
00126 }
00127
00128 StatusCode
00129 Sequencer::execute()
00130 {
00131 StatusCode result = StatusCode::SUCCESS;
00132 MsgStream log( msgSvc( ), name( ) );
00133
00134 log << MSG::DEBUG << name( ) << " Sequencer::execute( )" << endmsg;
00135
00136
00137 if ( isEnabled( ) && ! isExecuted( ) ) {
00138 Algorithm* lastAlgorithm;
00139 result = execute( subAlgorithms( ), m_isInverted, lastAlgorithm );
00140 if ( result.isSuccess( ) ) {
00141 bool passed = filterPassed( );
00142 if ( ! passed && ! isStopOverride( ) ) {
00143
00144
00145
00146
00147
00148
00149 std::vector<Algorithm*>* theAlgs = branchAlgorithms( );
00150 if ( theAlgs->size( ) > 0 ) {
00151 Algorithm* branchAlgorithm = (*theAlgs)[0];
00152 if ( lastAlgorithm == branchAlgorithm ) {
00153
00154
00155 result = execute( branchAlgorithms( ),
00156 m_isBranchInverted,
00157 lastAlgorithm, 1 );
00158 if ( result.isSuccess( ) ) {
00159
00160
00161
00162
00163
00164 setBranchFilterPassed( filterPassed( ) ).ignore();
00165 }
00166 }
00167 }
00168 }
00169 }
00170
00171
00172 setExecuted( true );
00173 }
00174 return result;
00175 }
00176
00177 StatusCode
00178 Sequencer::finalize()
00179 {
00180
00181
00182
00183 std::vector<Algorithm*>* theAlgs = branchAlgorithms( );
00184 std::vector<Algorithm*>::iterator it;
00185 std::vector<Algorithm*>::iterator itend = theAlgs->end( );
00186 for (it = theAlgs->begin(); it != itend; it++) {
00187 Algorithm* theAlgorithm = (*it);
00188 if (theAlgorithm->sysFinalize( ).isFailure()) {
00189 MsgStream log( msgSvc( ), name( ) );
00190 log << MSG::ERROR << "Unable to finalize Algorithm "
00191 << theAlgorithm->name() << endmsg;
00192 }
00193 }
00194 return StatusCode::SUCCESS;
00195 }
00196
00197 StatusCode
00198 Sequencer::start()
00199 {
00200 StatusCode result = StatusCode::SUCCESS;
00201 MsgStream log( msgSvc( ), name( ) );
00202
00203 std::vector<Algorithm*>* theAlgs;
00204 std::vector<Algorithm*>::iterator it;
00205 std::vector<Algorithm*>::iterator itend;
00206
00207
00208 theAlgs = subAlgorithms( );
00209 itend = theAlgs->end( );
00210 for (it = theAlgs->begin(); it != itend; it++) {
00211 Algorithm* theAlgorithm = (*it);
00212 result = theAlgorithm->sysStart( );
00213 if( result.isFailure() ) {
00214 log << MSG::ERROR << "Unable to start Algorithm " << theAlgorithm->name() << endmsg;
00215 return result;
00216 }
00217 }
00218
00219
00220 theAlgs = branchAlgorithms( );
00221 itend = theAlgs->end( );
00222 for (it = theAlgs->begin(); it != itend; it++) {
00223 Algorithm* theAlgorithm = (*it);
00224 result = theAlgorithm->sysStart( );
00225 if( result.isFailure() ) {
00226 log << MSG::ERROR << "Unable to start Algorithm " << theAlgorithm->name() << endmsg;
00227 return result;
00228 }
00229 }
00230
00231 return result;
00232 }
00233
00234 StatusCode
00235 Sequencer::stop()
00236 {
00237
00238
00239 std::vector<Algorithm*>* theAlgs;
00240 std::vector<Algorithm*>::iterator it;
00241 std::vector<Algorithm*>::iterator itend;
00242
00243 theAlgs = subAlgorithms( );
00244 itend = theAlgs->end( );
00245 for (it = theAlgs->begin(); it != itend; it++) {
00246 Algorithm* theAlgorithm = (*it);
00247 if (theAlgorithm->sysStop( ).isFailure()) {
00248 MsgStream log( msgSvc( ), name( ) );
00249 log << MSG::ERROR << "Unable to stop Algorithm "
00250 << theAlgorithm->name() << endmsg;
00251 }
00252 }
00253
00254 theAlgs = branchAlgorithms( );
00255 itend = theAlgs->end( );
00256 for (it = theAlgs->begin(); it != itend; it++) {
00257 Algorithm* theAlgorithm = (*it);
00258 if (theAlgorithm->sysStop( ).isFailure()) {
00259 MsgStream log( msgSvc( ), name( ) );
00260 log << MSG::ERROR << "Unable to stop Algorithm "
00261 << theAlgorithm->name() << endmsg;
00262 }
00263 }
00264 return StatusCode::SUCCESS;
00265 }
00266
00267 StatusCode
00268 Sequencer::beginRun()
00269 {
00270 StatusCode result = StatusCode::SUCCESS;
00271 MsgStream log( msgSvc( ), name( ) );
00272
00273
00274 if ( isEnabled( ) ) {
00275
00276
00277
00278
00279
00280 std::vector<Algorithm*>* theAlgs = subAlgorithms( );
00281 std::vector<Algorithm*>::iterator it;
00282 std::vector<Algorithm*>::iterator itend = theAlgs->end( );
00283 for (it = theAlgs->begin(); it != itend; it++) {
00284 Algorithm* theAlgorithm = (*it);
00285 result = theAlgorithm->sysInitialize( );
00286 if( result.isFailure() ) {
00287 log << MSG::ERROR << "Unable to initialize Algorithm " << theAlgorithm->name() << endmsg;
00288 break;
00289 }
00290 result = theAlgorithm->sysStart( );
00291 if( result.isFailure() ) {
00292 log << MSG::ERROR << "Unable to start Algorithm " << theAlgorithm->name() << endmsg;
00293 break;
00294 }
00295 }
00296
00297
00298
00299 for (it = theAlgs->begin(); it != itend; it++) {
00300 Algorithm* theAlgorithm = (*it);
00301 if ( ! theAlgorithm->isEnabled( ) ) {
00302 theAlgorithm->beginRun( ).ignore();
00303 }
00304 }
00305
00306
00307
00308
00309
00310 theAlgs = branchAlgorithms( );
00311 itend = theAlgs->end( );
00312 for (it = theAlgs->begin(); it != itend; it++) {
00313 Algorithm* theAlgorithm = (*it);
00314 result = theAlgorithm->sysInitialize( );
00315 if( result.isFailure() ) {
00316 log << MSG::ERROR << "Unable to initialize Algorithm " << theAlgorithm->name() << endmsg;
00317 break;
00318 }
00319 result = theAlgorithm->sysStart( );
00320 if( result.isFailure() ) {
00321 log << MSG::ERROR << "Unable to start Algorithm " << theAlgorithm->name() << endmsg;
00322 break;
00323 }
00324 }
00325
00326
00327
00328 for (it = theAlgs->begin(); it != itend; it++) {
00329 Algorithm* theAlgorithm = (*it);
00330 if ( ! theAlgorithm->isEnabled( ) ) {
00331 theAlgorithm->beginRun( ).ignore();
00332 }
00333 }
00334 }
00335 return StatusCode::SUCCESS;
00336 }
00337
00338 StatusCode
00339 Sequencer::endRun()
00340 {
00341
00342 if ( isEnabled( ) ) {
00343
00344
00345
00346 std::vector<Algorithm*>* theAlgms = subAlgorithms( );
00347 std::vector<Algorithm*>::iterator it;
00348 std::vector<Algorithm*>::iterator itend = theAlgms->end( );
00349 for (it = theAlgms->begin(); it != itend; it++) {
00350 Algorithm* theAlgorithm = (*it);
00351 if ( ! theAlgorithm->isEnabled( ) ) {
00352 theAlgorithm->endRun( ).ignore();
00353 }
00354 }
00355
00356
00357 theAlgms = branchAlgorithms( );
00358 itend = theAlgms->end( );
00359 for (it = theAlgms->begin(); it != itend; it++) {
00360 Algorithm* theAlgorithm = (*it);
00361 if ( ! theAlgorithm->isEnabled( ) ) {
00362 theAlgorithm->endRun( ).ignore();
00363 }
00364 }
00365 }
00366 return StatusCode::SUCCESS;
00367 }
00368
00369 void
00370 Sequencer::resetExecuted( )
00371 {
00372 Algorithm::resetExecuted( );
00373
00374
00375
00376 std::vector<Algorithm*>* subAlgms = subAlgorithms( );
00377 std::vector<Algorithm*>::iterator it;
00378 std::vector<Algorithm*>::iterator itend = subAlgms->end( );
00379 for (it = subAlgms->begin(); it != itend; it++) {
00380 Algorithm* theAlgorithm = (*it);
00381 theAlgorithm->resetExecuted( );
00382 }
00383
00384
00385
00386 subAlgms = branchAlgorithms( );
00387 itend = subAlgms->end( );
00388 for (it = subAlgms->begin(); it != itend; it++) {
00389 Algorithm* theAlgorithm = (*it);
00390 theAlgorithm->resetExecuted( );
00391 }
00392
00393
00394 m_branchFilterPassed = false;
00395 }
00396
00397 bool
00398 Sequencer::branchFilterPassed( ) const
00399 {
00400 return m_branchFilterPassed;
00401 }
00402
00403 StatusCode
00404 Sequencer::setBranchFilterPassed( bool state )
00405 {
00406 m_branchFilterPassed = state;
00407 return StatusCode::SUCCESS;
00408 }
00409
00410 bool
00411 Sequencer::isStopOverride( ) const
00412 {
00413 return m_stopOverride.value( );
00414 }
00415
00416 StatusCode
00417 Sequencer::append( Algorithm* pAlgorithm )
00418 {
00419 StatusCode result = append( pAlgorithm, subAlgorithms( ) );
00420 return result;
00421 }
00422
00423 StatusCode
00424 Sequencer::appendToBranch( Algorithm* pAlgorithm )
00425 {
00426 StatusCode result = append( pAlgorithm, branchAlgorithms( ) );
00427 return result;
00428 }
00429
00430 StatusCode
00431 Sequencer::createAndAppend( const std::string& type,
00432 const std::string& name,
00433 Algorithm*& pAlgorithm )
00434 {
00435 StatusCode result = createAndAppend( type, name, pAlgorithm, subAlgorithms( ) );
00436 return result;
00437 }
00438
00439 StatusCode
00440 Sequencer::createAndAppendToBranch( const std::string& type,
00441 const std::string& name,
00442 Algorithm*& pAlgorithm )
00443 {
00444 StatusCode result = createAndAppend( type, name, pAlgorithm, branchAlgorithms( ) );
00445 return result;
00446 }
00447
00448 StatusCode
00449 Sequencer::remove( Algorithm* pAlgorithm )
00450 {
00451 std::string theName = pAlgorithm->name( );
00452 StatusCode result = remove( theName );
00453 return result;
00454 }
00455
00456 StatusCode
00457 Sequencer::remove( const std::string& algname )
00458 {
00459 StatusCode result = remove( algname, subAlgorithms( ) );
00460 return result;
00461 }
00462
00463 StatusCode
00464 Sequencer::removeFromBranch( Algorithm* pAlgorithm )
00465 {
00466 std::string theName = pAlgorithm->name( );
00467 StatusCode result = removeFromBranch( theName );
00468 return result;
00469 }
00470
00471 StatusCode
00472 Sequencer::removeFromBranch( const std::string& algname )
00473 {
00474 StatusCode result = remove( algname, branchAlgorithms( ) );
00475 return result;
00476 }
00477
00478 std::vector<Algorithm*>*
00479 Sequencer::branchAlgorithms( ) const {
00480 return m_branchAlgs;
00481 }
00482
00483 StatusCode
00484 Sequencer::decodeMemberNames( )
00485 {
00486 StatusCode result = StatusCode::SUCCESS;
00487
00488
00489 result = decodeNames( m_names,
00490 subAlgorithms( ),
00491 m_isInverted );
00492
00493 return result;
00494 }
00495
00496 void
00497 Sequencer::membershipHandler( Property& )
00498 {
00499 if ( isInitialized() ) decodeMemberNames();
00500 }
00501
00502 StatusCode
00503 Sequencer::decodeBranchMemberNames( )
00504 {
00505 StatusCode result = StatusCode::SUCCESS;
00506
00507
00508 result = decodeNames( m_branchNames,
00509 branchAlgorithms( ),
00510 m_isBranchInverted );
00511
00512 return result;
00513 }
00514
00515 void
00516 Sequencer::branchMembershipHandler( Property& )
00517 {
00518 if ( isInitialized() ) decodeBranchMemberNames();
00519 }
00520
00525 StatusCode
00526 Sequencer::append( Algorithm* pAlgorithm,
00527 std::vector<Algorithm*>* theAlgs )
00528 {
00529 StatusCode result = StatusCode::SUCCESS;
00530
00531 std::vector<Algorithm*>::iterator it;
00532 std::vector<Algorithm*>::iterator itend = theAlgs->end( );
00533 for (it = theAlgs->begin(); it != itend; it++) {
00534 Algorithm* theAlgorithm = (*it);
00535 if ( theAlgorithm == pAlgorithm ) {
00536 result = StatusCode::FAILURE;
00537 break;
00538 }
00539 }
00540 if ( result.isSuccess( ) ) {
00541 theAlgs->push_back( pAlgorithm );
00542 pAlgorithm->addRef();
00543 }
00544 return result;
00545 }
00546
00547 StatusCode
00548 Sequencer::createAndAppend( const std::string& type,
00549 const std::string& algName,
00550 Algorithm*& pAlgorithm,
00551 std::vector<Algorithm*>* theAlgs )
00552 {
00553 StatusCode result = StatusCode::FAILURE;
00554 MsgStream log( msgSvc( ), name( ) );
00555 SmartIF<IAlgManager> theAlgMgr(serviceLocator()->service("ApplicationMgr"));
00556 if ( theAlgMgr.isValid() ) {
00557 IAlgorithm* tmp;
00558 result = theAlgMgr->createAlgorithm( type, algName, tmp );
00559 if ( result.isSuccess( ) ) {
00560 try{
00561 pAlgorithm = dynamic_cast<Algorithm*>(tmp);
00562 theAlgs->push_back( pAlgorithm );
00563 } catch(...){
00564 log << MSG::ERROR << "Unable to create Algorithm " << algName << endmsg;
00565 result = StatusCode::FAILURE;
00566 }
00567 }
00568 }
00569 return result;
00570 }
00571
00572 StatusCode
00573 Sequencer::decodeNames( StringArrayProperty& theNames,
00574 std::vector<Algorithm*>* theAlgs,
00575 std::vector<bool>& theLogic )
00576 {
00577 StatusCode result;
00578 MsgStream log( msgSvc( ), name( ) );
00579 SmartIF<IAlgManager> theAlgMgr(serviceLocator()->service("ApplicationMgr"));
00580 if ( theAlgMgr.isValid() ) {
00581
00582 theAlgs->clear( );
00583
00584
00585
00586 const std::vector<std::string>& theNameVector = theNames.value( );
00587 std::vector<std::string>::const_iterator it;
00588 std::vector<std::string>::const_iterator itend = theNameVector.end( );
00589 for (it = theNameVector.begin(); it != itend; it++) {
00590
00591
00592
00593
00594
00595
00596
00597 const Gaudi::Utils::TypeNameString typeName(*it);
00598 std::string theName = typeName.name();
00599 std::string theType = typeName.type();
00600
00601
00602
00603
00604
00605
00606
00607 bool isInverted = false;
00608 std::string::size_type invert = theName.find_first_of( ":" );
00609
00610 while ( std::string::npos != invert
00611 && invert < (theName.size() - 1) && theName[invert+1] == ':' )
00612 invert = theName.find_first_of( ":", invert+2 );
00613 if ( std::string::npos != invert ) {
00614 if ( theName == theType ) {
00615
00616
00617 theType = theType.substr( 0, invert );
00618 }
00619 theName = theName.substr( 0, invert );
00620 isInverted = true;
00621 }
00622
00623
00624 SmartIF<IAlgorithm>& theIAlg = theAlgMgr->algorithm(theName, false);
00625 Algorithm* theAlgorithm = 0;
00626 StatusCode status = StatusCode::SUCCESS;
00627 if ( theIAlg.isValid() ) {
00628 try{
00629 theAlgorithm = dynamic_cast<Algorithm*>(theIAlg.get());
00630 } catch(...){
00631 log << MSG::WARNING << theName << " is not an Algorithm - Failed dynamic cast" << endmsg;
00632 theAlgorithm = 0;
00633 }
00634 }
00635 if ( theAlgorithm ) {
00636
00637
00638 status = append( theAlgorithm, theAlgs );
00639 if ( status.isSuccess( ) ) {
00640 log << MSG::DEBUG << theName << " already exists - appended to member list" << endmsg;
00641 } else {
00642 log << MSG::WARNING << theName << " already exists - append failed!!!" << endmsg;
00643 result = StatusCode::FAILURE;
00644 }
00645 } else {
00646
00647
00648
00649 status = createAndAppend( theType, theName, theAlgorithm, theAlgs );
00650 if ( status.isSuccess( ) ) {
00651 log << MSG::DEBUG << theName << " doesn't exist - created and appended to member list" << endmsg;
00652 } else {
00653 log << MSG::WARNING << theName << " doesn't exist - creation failed!!!" << endmsg;
00654 result = StatusCode::FAILURE;
00655 }
00656 }
00657 if ( status.isSuccess( ) ) {
00658 theLogic.push_back( isInverted );
00659 }
00660 }
00661
00662 }
00663
00664 if ( result.isSuccess() && theAlgs->size() != 0 ) {
00665 log << MSG::INFO << "Member list: ";
00666 std::vector<Algorithm*>::iterator ai = theAlgs->begin();
00667 std::vector<bool>::iterator li = theLogic.begin();
00668 for ( ; ai != theAlgs->end(); ++ai, ++li ) {
00669
00670 if ( ai != theAlgs->begin() ) log << ", ";
00671
00672 if ( (*ai)->name() == System::typeinfoName(typeid(**ai)) )
00673 log << (*ai)->name();
00674 else
00675 log << System::typeinfoName(typeid(**ai)) << "/" << (*ai)->name();
00676
00677 if (*li) log << ":invert";
00678 }
00679 log << endmsg;
00680 }
00681 return result;
00682 }
00683
00684 StatusCode
00685 Sequencer::execute( std::vector<Algorithm*>* theAlgs,
00686 std::vector<bool>& theLogic,
00687 Algorithm*& lastAlgorithm,
00688 unsigned int first )
00689 {
00690 StatusCode result = StatusCode::SUCCESS;
00691
00692
00693
00694
00695
00696 unsigned int size = theAlgs->size( );
00697 for (unsigned int i = first; i < size; i++) {
00698 lastAlgorithm = (*theAlgs)[i];
00699 result = executeMember( lastAlgorithm );
00700 if ( result.isSuccess( ) ) {
00701
00702
00703
00704 bool passed = lastAlgorithm->filterPassed( );
00705 bool isInverted = theLogic[i];
00706 if ( isInverted ) {
00707 passed = ! passed;
00708 }
00709 setFilterPassed( passed );
00710
00711
00712
00713
00714 if ( ! isStopOverride( ) ) {
00715 if ( ! passed ) break;
00716 }
00717 } else {
00718 break;
00719 }
00720 }
00721 return result;
00722 }
00723
00724 StatusCode
00725 Sequencer::executeMember( Algorithm* theAlgorithm )
00726 {
00727 StatusCode result = StatusCode::SUCCESS;
00728 if ( theAlgorithm->isEnabled( ) ) {
00729 if ( ! theAlgorithm->isExecuted( ) ) {
00730 result = theAlgorithm->sysExecute( );
00731
00732
00733
00734 theAlgorithm->setExecuted( true );
00735 }
00736 }
00737 return result;
00738 }
00739
00740 StatusCode
00741 Sequencer::remove( const std::string& algname, std::vector<Algorithm*>* theAlgs )
00742 {
00743 MsgStream log( msgSvc( ), name( ) );
00744 StatusCode result = StatusCode::FAILURE;
00745
00746
00747 std::vector<Algorithm*>::iterator it;
00748 std::vector<Algorithm*>::iterator itend = theAlgs->end( );
00749 for (it = theAlgs->begin(); it != itend; it++) {
00750 Algorithm* theAlgorithm = (*it);
00751 if ( theAlgorithm->name( ) == algname ) {
00752
00753
00754
00755 log << MSG::INFO <<"Sequencer::remove( ) isn't implemented yet!!!!!" << endmsg;
00756 result = StatusCode::SUCCESS;
00757 break;
00758 }
00759 }
00760 return result;
00761 }