| # Gaudi Framework, version v23r0 |

Home |  | Generated: Mon Jan 30 2012

# HistorySvc.cpp

Go to the documentation of this file.

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines