| # Gaudi Framework, version v20r2
Generated: 18 Jul 2008

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

- Search for |

- File List
- File Members

# HistorySvc.cpp

Go to the documentation of this file.

    00001 // $Id: HistorySvc.cpp,v 1.18 2008/06/04 12:35:15 marcocle Exp $
    00002
    00003 #ifndef GAUDISVC_FASTHISTORYSVC_H
    00004  #include "HistorySvc.h"
    00005 #endif
    00006
    00007 #include "GaudiKernel/HistoryObj.h"
    00008
    00009 #include "GaudiKernel/JobHistory.h"
    00010 #include "GaudiKernel/AlgorithmHistory.h"
    00011 #include "GaudiKernel/AlgToolHistory.h"
    00012 #include "GaudiKernel/DataHistory.h"
    00013 #include "GaudiKernel/ServiceHistory.h"
    00014
    00015 #ifndef KERNEL_SVCFACTORY_H
    00016  #include "GaudiKernel/SvcFactory.h"
    00017 #endif
    00018 #ifndef GAUDIKERNEL_ISVCLOCATOR_H
    00019  #include "GaudiKernel/ISvcLocator.h"
    00020 #endif
    00021
    00022 #include "GaudiKernel/System.h"
    00023 #include "GaudiKernel/Bootstrap.h"
    00024 #include "GaudiKernel/IAlgManager.h"
    00025 #include "GaudiKernel/IAlgorithm.h"
    00026 #include "GaudiKernel/Algorithm.h"
    00027 #include "GaudiKernel/IAlgTool.h"
    00028 #include "GaudiKernel/AlgTool.h"
    00029 #include "GaudiKernel/IService.h"
    00030 #include "GaudiKernel/IJobOptionsSvc.h"
    00031 #include "GaudiKernel/IAppMgrUI.h"
    00032 #include "GaudiKernel/IIncidentSvc.h"
    00033 #include "GaudiKernel/IToolSvc.h"
    00034 #include "GaudiKernel/ServiceHandle.h"
    00035
    00036 #include "GaudiKernel/IAlgContextSvc.h"
    00037
    00038 #include <iostream>
    00039 #include <fstream>
    00040 #include <sstream>
    00041 #include <limits>
    00042
    00043 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00044 DECLARE_SERVICE_FACTORY(HistorySvc)
    00045
    00046 using namespace std;
    00047
    00048 //
    00050 //
    00051
    00052 struct DHH {
    00053   CLID id;
    00054   std::string key;
    00055
    00056   DHH(const CLID& i, const std::string& k):id(i), key(k) {}
    00057
    00058   bool operator < ( DHH const &rhs ) const {
    00059     if (id != rhs.id) {
    00060       return (id < rhs.id);
    00061     } else {
    00062       return (key < rhs.key);
    00063     }
    00064   }
    00065 };
    00066
    00067 void fenv_dummy(char**) {}
    00068
    00069 HistorySvc::HistorySvc( const std::string& name, ISvcLocator* svc )
    00070   : Service( name, svc ),
    00071     m_isInitialized(false),
    00072     m_dump(false),
    00073     p_algCtxSvc(0),
    00074     m_jobHistory(0),
    00075     m_outputFile(""),
    00076     m_incidentSvc(0),
    00077     m_log(msgSvc(), name )
    00078
    00079 {
    00080     declareProperty("Dump",m_dump);
    00081     declareProperty("Activate", m_activate=true);
    00082     declareProperty("OutputFile",m_outputFile);
    00083
    00084     // hack to bring in environ
    00085     vector<string> envtmp = System::getEnv();
    00086
    00087 }
    00088
    00089 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00090
    00091 HistorySvc::~HistorySvc() {
    00092   delete m_jobHistory;
    00093 }
    00094
    00095 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00096
    00097 StatusCode HistorySvc::reinitialize() {
    00098
    00099   clearState();
    00100
    00101   m_state = Gaudi::StateMachine::OFFLINE;
    00102
    00103   return initialize();
    00104
    00105 }
    00106
    00107 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00108
    00109 void HistorySvc::clearState() {
    00110   m_algs.clear();
    00111   std::map<const Algorithm*, AlgorithmHistory*>::iterator algitr;
    00112   for (algitr = m_algmap.begin(); algitr != m_algmap.end(); ++algitr) {
    00113     AlgorithmHistory* h = algitr->second;
    00114     (const_cast<Algorithm*> (algitr->first))->release();
    00115     delete h;
    00116   }
    00117   m_algmap.clear();
    00118
    00119   m_ialgtools.clear();
    00120   m_algtools.clear();
    00121   std::map<const AlgTool*, AlgToolHistory*>::iterator atitr;
    00122   for (atitr=m_algtoolmap.begin(); atitr != m_algtoolmap.end(); ++atitr) {
    00123     (const_cast<AlgTool*> (atitr->first))->release();
    00124     delete atitr->second;
    00125   }
    00126   m_algtoolmap.clear();
    00127
    00128   m_svcs.clear();
    00129   std::map<const IService*, ServiceHistory*>::iterator svitr;
    00130   for (svitr = m_svcmap.begin(); svitr != m_svcmap.end(); ++svitr) {
    00131     (const_cast<IService*> (svitr->first))->release();
    00132     delete svitr->second;
    00133   }
    00134   m_svcmap.clear();
    00135 }
    00136
    00137
    00138 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00139
    00140 StatusCode HistorySvc::initialize() {
    00141
    00142   StatusCode status = Service::initialize();
    00143   m_log.setLevel( m_outputLevel.value() );
    00144
    00145   if (status.isFailure()) {
    00146 #ifndef NDEBUG
    00147     m_log << MSG::DEBUG << "Failed to initialize the base class (Service)"
    00148           << endreq;
    00149 #endif
    00150     return status;
    00151   }
    00152
    00153 #ifndef NDEBUG
    00154   m_log << MSG::DEBUG << "Initializing HistorySvc" << endreq;
    00155 #endif
    00156
    00157   if (!m_activate) return StatusCode::SUCCESS;
    00158
    00159   static const bool CREATEIF(true);
    00160
    00161   if ( service("AlgContextSvc",p_algCtxSvc,CREATEIF).isFailure() ) {
    00162     m_log << MSG::ERROR << "unable to get the AlgContextSvc" << endreq;
    00163     return StatusCode::FAILURE;
    00164   }
    00165
    00166   if (service("IncidentSvc", m_incidentSvc, CREATEIF).isFailure()) {
    00167     m_log << MSG::ERROR << "unable to get the IncidentSvc" << endreq;
    00168     return StatusCode::FAILURE;
    00169   }
    00170
    00171   // create a weak dependency on the ToolSvc, so that it doesn't get deleted
    00172   // before we're done with it in finalize
    00173   if (ServiceHandle<IToolSvc>("ToolSvc/ToolSvc",name()).retrieve().isFailure()) {
    00174     m_log << MSG::ERROR << "could not retrieve the ToolSvc handle !"
    00175           << endreq;
    00176     return StatusCode::FAILURE;
    00177   }
    00178
    00179   // add listener to be triggered by first BeginEvent with low priority
    00180   // so it gets called first
    00181   const bool rethrow = false;
    00182   const bool oneShot = true; // make the listener called only once
    00183   m_incidentSvc->addListener(this,IncidentType::BeginEvent,
    00184                              std::numeric_limits<long>::min(),rethrow,oneShot);
    00185
    00186   m_isInitialized = true;
    00187
    00188   return StatusCode::SUCCESS;
    00189
    00190 }
    00191 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00192
    00193 StatusCode HistorySvc::captureState() {
    00194
    00195   if (m_jobHistory == 0) {
    00196     m_jobHistory = new JobHistory;
    00197     IJobOptionsSvc *jo;
    00198     if (service("JobOptionsSvc",jo).isFailure()) {
    00199        m_log << MSG::ERROR
    00200              << "Could not get jobOptionsSvc - "
    00201              << "not adding properties to JobHistory" << endreq;
    00202     } else {
    00203
    00204       bool foundAppMgr(false);
    00205
    00206       std::vector<std::string> clients = jo->getClients();
    00207       std::vector<std::string>::const_iterator it;
    00208       std::vector<const Property*>::const_iterator itr;
    00209       for (it=clients.begin(); it!=clients.end(); ++it) {
    00210         if (*it == "ApplicationMgr") {
    00211           foundAppMgr = true;
    00212         }
    00213         const std::vector<const Property*> *props = jo->getProperties(*it);
    00214         for (itr=props->begin(); itr != props->end(); ++itr) {
    00215           m_jobHistory->addProperty( *it, *itr );
    00216         }
    00217       }
    00218
    00219       if (!foundAppMgr) {
    00220         IProperty *ap;
    00221         if (service("ApplicationMgr",ap).isFailure()) {
    00222           m_log << MSG::ERROR << "could not get the ApplicationMgr" << endreq;
    00223         } else {
    00224           std::vector<Property*>::const_iterator itr2;
    00225           const std::vector<Property*> props = ap->getProperties();
    00226           for (itr2=props.begin(); itr2 != props.end(); ++itr2) {
    00227             m_jobHistory->addProperty( "ApplicationMgr", *itr2 );
    00228           }
    00229         }
    00230       }
    00231
    00232     }
    00233   }
    00234
    00236
    00237   StatusCode sc;
    00238   IAlgManager* algMgr = 0;
    00239   sc = Gaudi::svcLocator()->queryInterface( IID_IAlgManager,
    00240                                             pp_cast<void>(&algMgr) );
    00241   if (sc.isFailure()) {
    00242     m_log << MSG::ERROR << "Could not get AlgManager" << endreq;
    00243     return StatusCode::FAILURE;
    00244   } else {
    00245
    00246     std::list<IAlgorithm*> algs;
    00247     algs = algMgr->getAlgorithms();
    00248     std::list<IAlgorithm*>::const_iterator itr;
    00249     for (itr=algs.begin(); itr!=algs.end(); ++itr) {
    00250       Algorithm* alg = dynamic_cast<Algorithm*> (*itr);
    00251       if (alg == 0) {
    00252         m_log << MSG::WARNING << "Algorithm " << (*itr)->name()
    00253               << " does not inherit from Algorithm. Not registering it."
    00254               << endreq;
    00255       } else {
    00256         registerAlg( *alg ).ignore();
    00257       }
    00258     }
    00259
    00260     m_log << MSG::INFO;
    00261     m_log << "Registered " << algs.size() << " Algorithms" << endreq;
    00262
    00263   }
    00264
    00266
    00267   m_isInitialized = true;
    00268   std::set<const IAlgTool*>::const_iterator itra;
    00269   for (itra = m_ialgtools.begin(); itra != m_ialgtools.end(); ++itra) {
    00270     (const_cast<IAlgTool*>(*itra))->addRef();
    00271     registerAlgTool(**itra).ignore();
    00272   }
    00273
    00274   m_log << MSG::INFO << "Registered " << m_algtools.size() << " AlgTools"
    00275         << endreq;
    00276
    00278
    00279   std::list<IService*> svcs = Gaudi::svcLocator()->getServices();
    00280
    00281   std::list<IService*>::const_iterator itrs;
    00282   for (itrs=svcs.begin(); itrs!=svcs.end(); ++itrs) {
    00283     (*itrs)->addRef();
    00284     registerSvc(**itrs).ignore();
    00285   }
    00286
    00287   m_log << MSG::INFO;
    00288   m_log << "Registered " << svcs.size() << " Services" << endreq;
    00289
    00290   return StatusCode::SUCCESS;
    00291
    00292
    00293 }
    00294
    00295 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00296
    00297 StatusCode HistorySvc::finalize() {
    00298
    00299   if (!m_activate) return StatusCode::SUCCESS;
    00300
    00301   if (m_dump) {
    00302     listProperties().ignore();
    00303   }
    00304
    00305   if (m_outputFile != "") {
    00306     std::ofstream ofs;
    00307     ofs.open(m_outputFile.c_str());
    00308     if (!ofs) {
    00309       m_log << MSG::ERROR << "Unable to open output file \"m_outputFile\""
    00310             << endreq;
    00311     } else {
    00312
    00313       //      dumpProperties(ofs);
    00314       dumpState(ofs);
    00315
    00316       ofs.close();
    00317     }
    00318   }
    00319
    00320   clearState();
    00321
    00322   StatusCode status = Service::finalize();
    00323
    00324   if ( status.isSuccess() )
    00325     m_log << MSG::INFO << "Service finalised successfully" << endreq;
    00326
    00327   return status;
    00328
    00329 }
    00330
    00331 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00332
    00333 StatusCode HistorySvc::queryInterface(const InterfaceID& riid,
    00334                                       void** ppvInterface)
    00335 {
    00336   StatusCode sc = StatusCode::FAILURE;
    00337   if ( ppvInterface ) {
    00338     *ppvInterface = 0;
    00339
    00340     if ( IHistorySvc::interfaceID().versionMatch(riid) )    {
    00341       *ppvInterface = static_cast<IHistorySvc*>(this);
    00342       sc = StatusCode::SUCCESS;
    00343       addRef();
    00344     }
    00345     else
    00346       sc = Service::queryInterface( riid, ppvInterface );
    00347   }
    00348   return sc;
    00349 }
    00350
    00351 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00352
    00353 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00354 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00355
    00356 StatusCode
    00357 HistorySvc::registerAlg(const Algorithm &alg) {
    00358
    00359   JobHistory *job = getJobHistory();
    00360   if (m_algmap.find(&alg) != m_algmap.end()) {
    00361     m_log << MSG::WARNING << "Algorithm " << alg.name()
    00362           << " already registered with HistorySvc" << endreq;
    00363     return StatusCode::SUCCESS;
    00364   }
    00365
    00366   (const_cast<Algorithm*>(&alg))->addRef();
    00367
    00368   m_algs.insert(&alg);
    00369
    00370   AlgorithmHistory *algHist = new AlgorithmHistory(alg, job);
    00371   m_algmap[&alg] = algHist;
    00372
    00373 #ifndef NDEBUG
    00374   m_log << MSG::DEBUG << "Registering algorithm: ";
    00375   m_log.setColor(MSG::CYAN);
    00376   m_log << alg.name() << endreq;
    00377   m_log.resetColor();
    00378 #endif
    00379
    00380   return StatusCode(StatusCode::SUCCESS,true);
    00381
    00382 }
    00383 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00384
    00385 StatusCode
    00386 HistorySvc::listProperties(const Algorithm &alg) const {
    00387
    00388   m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
    00389
    00390   AlgorithmHistory *hist = getAlgHistory( alg );
    00391
    00392   if (hist == 0) {
    00393     return StatusCode::FAILURE;
    00394   }
    00395
    00396   ostringstream ost;
    00397   ost << *hist;
    00398
    00399   std::string str = ost.str();
    00400
    00401   m_log << MSG::INFO << alg.name() << " --> " << endl << str << endreq;
    00402
    00403   return StatusCode(StatusCode::SUCCESS,true);
    00404
    00405 }
    00406 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00407
    00408 void
    00409 HistorySvc::dumpProperties(const Algorithm &alg, std::ofstream& ofs) const {
    00410
    00411   AlgorithmHistory *hist = getAlgHistory( alg );
    00412
    00413   if (hist == 0) {
    00414     return;
    00415   }
    00416
    00417   AlgorithmHistory::PropertyList::const_iterator itr;
    00418   for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
    00419     ofs << alg.name() << "  " << dumpProp(*itr) << std::endl;
    00420   }
    00421
    00422 }
    00423 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00424
    00425 AlgorithmHistory*
    00426 HistorySvc::getAlgHistory(const Algorithm &alg) const {
    00427
    00428   const Algorithm *palg = &alg;
    00429   set<const Algorithm*>::const_iterator itr = m_algs.find(palg);
    00430   if ( itr == m_algs.end() ) {
    00431     m_log << MSG::WARNING << "Algorithm " << alg.name() << " not registered"
    00432           << endreq;
    00433     return 0;
    00434   }
    00435
    00436   map<const Algorithm*, AlgorithmHistory*>::const_iterator itr2;
    00437   itr2 = m_algmap.find( *itr );
    00438
    00439   return ( itr2->second );
    00440
    00441 }
    00442 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00443
    00444 void
    00445 HistorySvc::getAlgHistory(std::set<AlgorithmHistory*>& algs) const {
    00446
    00447   set<const Algorithm*>::const_iterator itr;
    00448   for (itr=m_algs.begin(); itr!=m_algs.end(); ++itr) {
    00449     AlgorithmHistory *ah = m_algmap.find(*itr)->second;
    00450
    00451     algs.insert(ah);
    00452   }
    00453
    00454 }
    00455
    00456
    00457 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00458 StatusCode
    00459 HistorySvc::registerJob() {
    00460
    00461
    00462   return StatusCode(StatusCode::SUCCESS,true);
    00463
    00464
    00465 }
    00466 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00467
    00468
    00469 StatusCode
    00470 HistorySvc::listProperties() const {
    00471
    00472   m_log << MSG::INFO;
    00473   m_log.setColor(MSG::CYAN);
    00474   m_log << "Dumping properties for all Algorithms" << endreq;
    00475
    00476   std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
    00477   for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
    00478     const Algorithm* alg = itr->first;
    00479
    00480     listProperties( *alg ).ignore();
    00481
    00482   }
    00483
    00484   m_log << MSG::INFO;
    00485   m_log.setColor(MSG::CYAN);
    00486   m_log << "Dumping properties for all AlgTools" << endreq;
    00487
    00488   std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
    00489   for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
    00490     const AlgTool* alg = itr_a->first;
    00491
    00492     listProperties( *alg ).ignore();
    00493
    00494   }
    00495
    00496   m_log << MSG::INFO;
    00497   m_log.setColor(MSG::CYAN);
    00498   m_log << "Dumping properties for all Services" << endreq;
    00499
    00500   std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
    00501   for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
    00502     const IService* svc = itr_s->first;
    00503
    00504     listProperties( *svc ).ignore();
    00505
    00506   }
    00507
    00508   m_log << MSG::INFO;
    00509   m_log.setColor(MSG::CYAN);
    00510   m_log << "Dumping properties for Job";
    00511   m_log.resetColor();
    00512
    00513   ostringstream ost;
    00514   ost << *m_jobHistory;
    00515   std::string str = ost.str();
    00516
    00517   m_log << std::endl << str << endreq;
    00518
    00519   return StatusCode(StatusCode::SUCCESS,true);
    00520
    00521 }
    00522 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00523
    00524 void
    00525 HistorySvc::dumpProperties(std::ofstream& ofs) const {
    00526
    00527
    00528   ofs << "GLOBAL" << std::endl;
    00529   const JobHistory::PropertyList props = m_jobHistory->properties();
    00530   JobHistory::PropertyList::const_iterator itrj;
    00531   for (itrj=props.begin(); itrj != props.end(); ++itrj) {
    00532     std::string client = itrj->first;
    00533     const Property* prp = itrj->second;
    00534     ofs << client << "  " << dumpProp(prp) << std::endl;
    00535   }
    00536
    00537   ofs << std::endl << "SERVICES" << std::endl;
    00538   std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
    00539   for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
    00540     const IService* svc = itr_s->first;
    00541
    00542     dumpProperties( *svc, ofs );
    00543
    00544   }
    00545
    00546   ofs << std::endl << "ALGORITHMS" << std::endl;
    00547   std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
    00548   for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
    00549     const Algorithm* alg = itr->first;
    00550
    00551     dumpProperties( *alg, ofs );
    00552
    00553   }
    00554
    00555   ofs << std::endl << "ALGTOOLS" << std::endl;
    00556   std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
    00557   for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
    00558     const AlgTool* alg = itr_a->first;
    00559
    00560     dumpProperties( *alg, ofs );
    00561
    00562   }
    00563
    00564
    00565 }
    00566 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00567
    00568 JobHistory*
    00569 HistorySvc::getJobHistory() const {
    00570
    00571   return m_jobHistory;
    00572
    00573 }
    00574
    00575 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00576 IAlgorithm*
    00577 HistorySvc::getCurrentIAlg() const {
    00578   if (p_algCtxSvc == 0) {
    00579     m_log << MSG::WARNING << "trying to create DataHistoryObj before "
    00580           << "HistorySvc has been initialized" << endreq;
    00581     return 0;
    00582
    00583   } else {
    00584     return p_algCtxSvc->currentAlg();
    00585   }
    00586 }
    00587
    00588
    00589 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00590
    00591 DataHistory*
    00592 HistorySvc::createDataHistoryObj(const CLID& id, const std::string& key,
    00593                                  const std::string& /* storeName */) {
    00594
    00595   if (!m_activate) return 0;
    00596
    00597
    00598   AlgorithmHistory *algHist;
    00599
    00600   IAlgorithm* ialg = getCurrentIAlg();
    00601   if (ialg == 0) {
    00602 #ifndef NDEBUG
    00603     m_log << MSG::DEBUG
    00604           << "Could not discover current Algorithm:" << endl
    00605           << "          object CLID: " << id << "  key: \"" << key
    00606           << "\"" << endreq;
    00607 #endif
    00608     algHist = 0;
    00609   } else {
    00610     Algorithm* alg = dynamic_cast<Algorithm*>(ialg);
    00611     if (alg != 0) {
    00612       algHist = getAlgHistory( *alg );
    00613     } else {
    00614       m_log << MSG::WARNING
    00615             << "Could not extract concerete Algorithm:"
    00616             << endl
    00617             << "          object CLID: " << id << "  key: \"" << key
    00618             << "\"" << endreq;
    00619       algHist = 0;
    00620     }
    00621   }
    00622
    00623   DataHistory *hist = new DataHistory(id, key, algHist);
    00624
    00625   return hist;
    00626
    00627 }
    00628
    00629 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00630 StatusCode
    00631 HistorySvc::registerDataHistory(const CLID& id, const std::string& key,
    00632                                 const std::string& storeName) {
    00633
    00634   DHH dhh(id,key);
    00635
    00636   pair<DHMitr,DHMitr> mitr = m_datMap.equal_range(dhh);
    00637
    00638   if (mitr.first == mitr.second) {
    00639     // not in the map
    00640     DataHistory *dh = createDataHistoryObj(id,key,storeName);
    00641     m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
    00642   } else {
    00643     // found at least one
    00644     bool match(false);
    00645
    00646     std::string algName;
    00647     IAlgorithm *ialg = getCurrentIAlg();
    00648     if (ialg != 0) {
    00649       algName = ialg->name();
    00650     } else {
    00651       algName = "UNKNOWN";
    00652     }
    00653
    00654     for (DHMitr itr = mitr.first; itr != mitr.second; ++itr) {
    00655       DataHistory *dh = itr->second;
    00656       if (dh->algorithmHistory()->algorithm_name() == algName) {
    00657         match = true;
    00658         break;
    00659       }
    00660     }
    00661
    00662     if (! match) {
    00663       DataHistory *dh = createDataHistoryObj(id,key,storeName);
    00664       m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
    00665     }
    00666   }
    00667
    00668   return StatusCode::SUCCESS;
    00669
    00670 }
    00671
    00672
    00673 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00674 DataHistory*
    00675 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
    00676                            const std::string& /*storeName*/) const {
    00677
    00678   DHH dhh(id,key);
    00679
    00680   pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
    00681
    00682   if(mitr.first == mitr.second) {
    00683     return 0;
    00684   }
    00685
    00686   return mitr.first->second;
    00687
    00688 }
    00689
    00690 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00691
    00692 int
    00693 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
    00694                            const std::string& /*storeName*/,
    00695                            std::list<DataHistory*>& dhlist) const {
    00696
    00697   DHH dhh(id,key);
    00698
    00699   int n(0);
    00700
    00701   pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
    00702
    00703   for (DHMCitr itr=mitr.first; itr != mitr.second; ++itr) {
    00704     dhlist.push_back(itr->second);
    00705     n++;
    00706   }
    00707
    00708   return n;
    00709
    00710 }
    00711
    00712 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00713
    00714 StatusCode
    00715 HistorySvc::registerSvc(const IService &svc) {
    00716
    00717   if ( svc.name() == "HistoryStore" ) {
    00718     //    m_log << MSG::WARNING << "not registering store" << endreq;
    00719     return StatusCode(StatusCode::SUCCESS,true);
    00720   }
    00721
    00722   JobHistory *job = getJobHistory();
    00723   const IService* psvc = &svc;
    00724   map<const IService*, ServiceHistory*>::const_iterator itr =
    00725     m_svcmap.find(psvc);
    00726   if (itr == m_svcmap.end()) {
    00727
    00728 #ifndef NDEBUG
    00729     m_log << MSG::DEBUG << "Registering Service: ";
    00730     m_log.setColor(MSG::CYAN);
    00731     m_log << svc.name() << endreq;
    00732 #endif
    00733
    00734     m_svcs.insert(psvc);
    00735
    00736     ServiceHistory *svcHist = new ServiceHistory(&svc, job);
    00737     m_svcmap[psvc] = svcHist;
    00738
    00739     (const_cast<IService*>(psvc))->addRef();
    00740
    00741   }
    00742
    00743   m_log.resetColor();
    00744   return StatusCode(StatusCode::SUCCESS,true);
    00745
    00746 }
    00747
    00748 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00749
    00750 ServiceHistory*
    00751 HistorySvc::getServiceHistory(const IService &svc) const {
    00752
    00753   const IService *psvc = &svc;
    00754   map<const IService*, ServiceHistory*>::const_iterator itr =
    00755     m_svcmap.find(psvc);
    00756   if ( itr == m_svcmap.end() ) {
    00757     m_log << MSG::WARNING << "Service " << svc.name() << " not registered"
    00758           << endreq;
    00759     return 0;
    00760   }
    00761
    00762   return ( itr->second );
    00763
    00764 }
    00765 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00766
    00767 void
    00768 HistorySvc::getServiceHistory(std::set<ServiceHistory*>& svcs) const {
    00769
    00770   set<const IService*>::const_iterator itr;
    00771   for (itr=m_svcs.begin(); itr!=m_svcs.end(); ++itr) {
    00772     ServiceHistory *sh = m_svcmap.find( *itr )->second;
    00773
    00774     svcs.insert(sh);
    00775   }
    00776
    00777 }
    00778
    00779 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00780
    00781 StatusCode
    00782 HistorySvc::listProperties(const IService &svc) const {
    00783
    00784   m_log << MSG::INFO << "Dumping properties for " << svc.name() << endl;
    00785
    00786   ServiceHistory *hist = getServiceHistory( svc );
    00787
    00788   if (hist == 0) {
    00789     return StatusCode::FAILURE;
    00790   }
    00791
    00792   ostringstream ost;
    00793   ost << *hist;
    00794
    00795   std::string str = ost.str();
    00796
    00797   m_log << MSG::INFO << svc.name() << " --> " << endl << str << endreq;
    00798
    00799
    00800
    00801   return StatusCode(StatusCode::SUCCESS,true);
    00802
    00803 }
    00804
    00805 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00806
    00807 void
    00808 HistorySvc::dumpProperties(const IService &svc, std::ofstream &ofs) const {
    00809
    00810   ServiceHistory *hist = getServiceHistory( svc );
    00811
    00812   if (hist == 0) {
    00813     return;
    00814   }
    00815
    00816   ServiceHistory::PropertyList::const_iterator itr;
    00817   for (itr=hist->properties().begin(); itr != hist->properties().end();++itr) {
    00818     ofs << svc.name() << "  " << dumpProp(*itr) << std::endl;
    00819   }
    00820
    00821 }
    00822
    00823 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00824
    00825 StatusCode
    00826 HistorySvc::registerAlgTool(const IAlgTool& ialg) {
    00827
    00828   if (! m_isInitialized) {
    00829     if (p_algCtxSvc == 0) {
    00830       if ( service("AlgContextSvc",p_algCtxSvc,true).isFailure() ) {
    00831         m_log << MSG::ERROR << "unable to get the AlgContextSvc" << endreq;
    00832         return StatusCode::FAILURE;
    00833       }
    00834     }
    00835
    00836     m_ialgtools.insert(&ialg);
    00837     return StatusCode::SUCCESS;
    00838   }
    00839
    00840   const AlgTool *alg = dynamic_cast<const AlgTool*>( &ialg );
    00841   if ( alg == 0 ) {
    00842     m_log << MSG::ERROR << "Could not dcast IAlgTool \"" << ialg.name()
    00843           << "\" to an AlgTool" << endreq;
    00844     return StatusCode::FAILURE;
    00845   }
    00846
    00847   if (m_algtools.find(alg) != m_algtools.end()) {
    00848     m_log << MSG::WARNING << "AlgTool " << ialg.name()
    00849           << " already registered in HistorySvc" << endreq;
    00850     return StatusCode::SUCCESS;
    00851   }
    00852
    00853   m_algtools.insert(alg);
    00854   (const_cast<AlgTool*>(alg))->addRef();
    00855
    00856   const JobHistory *job = getJobHistory();
    00857   AlgToolHistory *algHist = new AlgToolHistory(*alg, job);
    00858   m_algtoolmap[alg] = algHist;
    00859
    00860 #ifndef NDEBUG
    00861   m_log << MSG::DEBUG << "Registering algtool: ";
    00862   m_log.setColor(MSG::CYAN);
    00863   m_log << alg->name() << endreq;
    00864   m_log.resetColor();
    00865 #endif
    00866
    00867   return StatusCode::SUCCESS;
    00868
    00869 }
    00870
    00871 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00872
    00873 StatusCode
    00874 HistorySvc::listProperties(const IAlgTool& alg) const {
    00875
    00876   m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
    00877
    00878   AlgToolHistory *hist = getAlgToolHistory( alg );
    00879
    00880   if (hist == 0) {
    00881     return StatusCode::FAILURE;
    00882   }
    00883
    00884   ostringstream ost;
    00885   ost << *hist;
    00886
    00887   std::string str = ost.str();
    00888
    00889   m_log << MSG::INFO << alg.name() << " --> " << endl << str << endreq;
    00890
    00891   return StatusCode::SUCCESS;
    00892
    00893 }
    00894 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00895
    00896 void
    00897 HistorySvc::dumpProperties(const IAlgTool& alg, std::ofstream &ofs) const {
    00898
    00899   AlgToolHistory *hist = getAlgToolHistory( alg );
    00900
    00901   if (hist == 0) {
    00902     return;
    00903   }
    00904
    00905   AlgToolHistory::PropertyList::const_iterator itr;
    00906   for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
    00907     ofs << alg.name() << "  " << dumpProp(*itr) << std::endl;
    00908   }
    00909
    00910 }
    00911
    00912 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00913
    00914 AlgToolHistory*
    00915 HistorySvc::getAlgToolHistory(const IAlgTool& alg) const {
    00916
    00917   const AlgTool *palg = dynamic_cast<const AlgTool*>(&alg);
    00918   set<const AlgTool*>::const_iterator itr = m_algtools.find(palg);
    00919   if ( itr == m_algtools.end() ) {
    00920     m_log << MSG::WARNING << "AlgTool " << alg.name() << " not registered"
    00921           << endreq;
    00922     return 0;
    00923   }
    00924
    00925   map<const AlgTool*, AlgToolHistory*>::const_iterator itr2;
    00926   itr2 = m_algtoolmap.find( *itr );
    00927
    00928   return ( itr2->second );
    00929
    00930 }
    00931
    00932 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00933
    00934 void
    00935 HistorySvc::getAlgToolHistory(std::set<AlgToolHistory*>& algs) const {
    00936
    00937   set<const AlgTool*>::const_iterator itr;
    00938   for (itr=m_algtools.begin(); itr!=m_algtools.end(); ++itr) {
    00939     AlgToolHistory *ah = m_algtoolmap.find(*itr)->second;
    00940
    00941     algs.insert(ah);
    00942   }
    00943
    00944 }
    00945
    00946 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00947
    00948 void
    00949 HistorySvc::handle(const Incident& incident) {
    00950
    00951   if (incident.type() == IncidentType::BeginEvent) {
    00952     if (captureState().isFailure()) {
    00953       m_log << MSG::WARNING << "Error capturing state." << endl
    00954             << "Will try again at next BeginEvent incident" << endreq;
    00955     }
    00956   }
    00957
    00958 }
    00959
    00960 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00961
    00962
    00963 std::string
    00964 HistorySvc::dumpProp(const Property* prop) const {
    00965   std::ostringstream ost;
    00966   prop->fillStream(ost);
    00967   return ost.str();
    00968 }
    00969
    00970 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00971
    00972 void
    00973 HistorySvc::dumpState(std::ofstream& ofs) const {
    00974
    00975
    00976   ofs << "GLOBAL" << std::endl;
    00977   const JobHistory::PropertyList props = m_jobHistory->properties();
    00978   JobHistory::PropertyList::const_iterator itrj;
    00979   for (itrj=props.begin(); itrj != props.end(); ++itrj) {
    00980     std::string client = itrj->first;
    00981     const Property* prp = itrj->second;
    00982     ofs << client << "  " << dumpProp(prp) << std::endl;
    00983   }
    00984
    00985
    00986   ofs << "SERVICES" << std::endl;
    00987   std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
    00988   for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
    00989     const IService* svc = itr_s->first;
    00990
    00991     dumpState( *svc, ofs );
    00992
    00993   }
    00994
    00995   ofs << "ALGORITHMS" << std::endl;
    00996   std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
    00997   for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
    00998     const Algorithm* alg = itr->first;
    00999
    01000     dumpState( *alg, ofs );
    01001
    01002   }
    01003
    01004
    01005   ofs << "ALGTOOLS" << std::endl;
    01006   std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
    01007   for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
    01008     const AlgTool* alg = itr_a->first;
    01009
    01010     dumpState( *alg, ofs );
    01011
    01012   }
    01013
    01014 }
    01015
    01016 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    01017
    01018 void
    01019 HistorySvc::dumpState(const Algorithm &alg, std::ofstream& ofs) const {
    01020
    01021   AlgorithmHistory *hist = getAlgHistory( alg );
    01022
    01023   if (hist == 0) {
    01024     return;
    01025   }
    01026
    01027   ofs << ">> " << alg.name() << endl << *hist << endl;
    01028
    01029 }
    01030
    01031 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    01032
    01033 void
    01034 HistorySvc::dumpState(const IService &svc, std::ofstream& ofs) const {
    01035
    01036   ServiceHistory *hist = getServiceHistory( svc );
    01037
    01038   if (hist == 0) {
    01039     return;
    01040   }
    01041
    01042   ofs << ">> " << svc.name() << endl << *hist << endl;
    01043
    01044 }
    01045
    01046 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    01047
    01048 void
    01049 HistorySvc::dumpState(const IAlgTool &alg, std::ofstream& ofs) const {
    01050
    01051   AlgToolHistory *hist = getAlgToolHistory( alg );
    01052
    01053   if (hist == 0) {
    01054     return;
    01055   }
    01056
    01057   ofs << ">> " << alg.name() << endl << *hist << endl;
    01058
    01059 }