All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HistorySvc.cpp
Go to the documentation of this file.
1 #include "HistorySvc.h"
2 
5 
11 
13 
14 #include "GaudiKernel/System.h"
15 #include "GaudiKernel/Bootstrap.h"
17 #include "GaudiKernel/IAlgorithm.h"
18 #include "GaudiKernel/Algorithm.h"
19 #include "GaudiKernel/IAlgTool.h"
20 #include "GaudiKernel/AlgTool.h"
21 #include "GaudiKernel/IService.h"
23 #include "GaudiKernel/IAppMgrUI.h"
25 #include "GaudiKernel/IToolSvc.h"
27 
29 
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <limits>
34 
35 #define ON_DEBUG if (UNLIKELY(outputLevel() <= MSG::DEBUG))
36 #define ON_VERBOSE if (UNLIKELY(outputLevel() <= MSG::VERBOSE))
37 
38 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
40 
41 using namespace std;
42 
43 //
45 //
46 
47 struct DHH {
49  std::string key;
50 
51  DHH(const CLID& i, const std::string& k):id(i), key(k) {}
52 
53  bool operator < ( DHH const &rhs ) const {
54  if (id != rhs.id) {
55  return (id < rhs.id);
56  } else {
57  return (key < rhs.key);
58  }
59  }
60 };
61 
62 void fenv_dummy(char**) {}
63 
64 HistorySvc::HistorySvc( const std::string& name, ISvcLocator* svc )
65  : base_class( name, svc ),
66  m_isInitialized(false),
67  m_dump(false),
68  p_algCtxSvc(0),
69  m_jobHistory(0),
70  m_outputFile(""),
71  m_incidentSvc(0),
72  m_log(msgSvc(), name ),
73  m_outputFileTypeXML(false)
74 
75 {
76  declareProperty("Dump",m_dump);
77  declareProperty("Activate", m_activate=true);
78  declareProperty("OutputFile",m_outputFile);
79 
80  // hack to bring in environ
81  vector<string> envtmp = System::getEnv();
82  envtmp.size(); // prevent icc remark #177: X declared but never referenced
83 }
84 
85 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
86 
88  delete m_jobHistory;
89 }
90 
91 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
92 
94 
95  clearState();
96 
98 
99  return initialize();
100 
101 }
102 
103 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
104 
106  m_algs.clear();
107  std::map<const Algorithm*, AlgorithmHistory*>::iterator algitr;
108  for (algitr = m_algmap.begin(); algitr != m_algmap.end(); ++algitr) {
109  AlgorithmHistory* h = algitr->second;
110  (const_cast<Algorithm*> (algitr->first))->release();
111  delete h;
112  }
113  m_algmap.clear();
114 
115  m_ialgtools.clear();
116  m_algtools.clear();
117  std::map<const AlgTool*, AlgToolHistory*>::iterator atitr;
118  for (atitr=m_algtoolmap.begin(); atitr != m_algtoolmap.end(); ++atitr) {
119  (const_cast<AlgTool*> (atitr->first))->release();
120  delete atitr->second;
121  }
122  m_algtoolmap.clear();
123 
124  m_svcs.clear();
125  std::map<const IService*, ServiceHistory*>::iterator svitr;
126  for (svitr = m_svcmap.begin(); svitr != m_svcmap.end(); ++svitr) {
127  (const_cast<IService*> (svitr->first))->release();
128  delete svitr->second;
129  }
130  m_svcmap.clear();
131 }
132 
133 
134 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
135 
137 
138  StatusCode status = Service::initialize();
140 
141  if (status.isFailure()) {
142 
143  ON_DEBUG
144  m_log << MSG::DEBUG << "Failed to initialize the base class (Service)"
145  << endmsg;
146  return status;
147  }
148 
149  ON_DEBUG
150  m_log << MSG::DEBUG << "Initializing HistorySvc" << endmsg;
151 
152  if (!m_activate) return StatusCode::SUCCESS;
153 
154  static const bool CREATEIF(true);
155 
156  if ( service("AlgContextSvc",p_algCtxSvc,CREATEIF).isFailure() ) {
157  m_log << MSG::ERROR << "unable to get the AlgContextSvc" << endmsg;
158  return StatusCode::FAILURE;
159  }
160 
161  if (service("IncidentSvc", m_incidentSvc, CREATEIF).isFailure()) {
162  m_log << MSG::ERROR << "unable to get the IncidentSvc" << endmsg;
163  return StatusCode::FAILURE;
164  }
165 
166  // create a weak dependency on the ToolSvc, so that it doesn't get deleted
167  // before we're done with it in finalize
168  m_toolSvc = serviceLocator()->service("ToolSvc");
169  if (! m_toolSvc) {
170  m_log << MSG::ERROR << "could not retrieve the ToolSvc handle !"
171  << endmsg;
172  return StatusCode::FAILURE;
173  }
174 
175  // add listener to be triggered by first BeginEvent with low priority
176  // so it gets called first
177  const bool rethrow = false;
178  const bool oneShot = true; // make the listener called only once
180  std::numeric_limits<long>::min(),rethrow,oneShot);
181 
182  if (m_outputFile.find(".XML") != string::npos || m_outputFile.find(".xml") != string::npos) {
183  ON_DEBUG
184  m_log << MSG::DEBUG << "output format is XML" << endmsg;
185  m_outputFileTypeXML = true;
186  }
187 
188  m_isInitialized = true;
189 
190  return StatusCode::SUCCESS;
191 
192 }
193 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
194 
196 
197  if (m_jobHistory == 0) {
198  m_jobHistory = new JobHistory;
199  IJobOptionsSvc *jo;
200  if (service("JobOptionsSvc",jo).isFailure()) {
201  m_log << MSG::ERROR
202  << "Could not get jobOptionsSvc - "
203  << "not adding properties to JobHistory" << endmsg;
204  } else {
205 
206  bool foundAppMgr(false);
207 
208  std::vector<std::string> clients = jo->getClients();
209  std::vector<std::string>::const_iterator it;
210  std::vector<const Property*>::const_iterator itr;
211  for (it=clients.begin(); it!=clients.end(); ++it) {
212  if (*it == "ApplicationMgr") {
213  foundAppMgr = true;
214  }
215  const std::vector<const Property*> *props = jo->getProperties(*it);
216  for (itr=props->begin(); itr != props->end(); ++itr) {
217  m_jobHistory->addProperty( *it, *itr );
218  }
219  }
220 
221  if (!foundAppMgr) {
222  IProperty *ap;
223  if (service("ApplicationMgr",ap).isFailure()) {
224  m_log << MSG::ERROR << "could not get the ApplicationMgr" << endmsg;
225  } else {
226  std::vector<Property*>::const_iterator itr2;
227  const std::vector<Property*> props = ap->getProperties();
228  for (itr2=props.begin(); itr2 != props.end(); ++itr2) {
229  m_jobHistory->addProperty( "ApplicationMgr", *itr2 );
230  }
231  }
232  }
233 
234  }
235  }
236 
238 
239  StatusCode sc;
240  IAlgManager* algMgr = 0;
242  pp_cast<void>(&algMgr) );
243  if (sc.isFailure()) {
244  m_log << MSG::ERROR << "Could not get AlgManager" << endmsg;
245  return StatusCode::FAILURE;
246  } else {
247 
248  std::list<IAlgorithm*> algs;
249  algs = algMgr->getAlgorithms();
250  std::list<IAlgorithm*>::const_iterator itr;
251  for (itr=algs.begin(); itr!=algs.end(); ++itr) {
252  Algorithm* alg = dynamic_cast<Algorithm*> (*itr);
253  if (alg == 0) {
254  m_log << MSG::WARNING << "Algorithm " << (*itr)->name()
255  << " does not inherit from Algorithm. Not registering it."
256  << endmsg;
257  } else {
258  registerAlg( *alg ).ignore();
259  }
260  }
261 
262  m_log << MSG::INFO;
263  m_log << "Registered " << algs.size() << " Algorithms" << endmsg;
264 
265  }
266 
268 
269  m_isInitialized = true;
270  std::set<const IAlgTool*>::const_iterator itra;
271  for (itra = m_ialgtools.begin(); itra != m_ialgtools.end(); ++itra) {
272  (const_cast<IAlgTool*>(*itra))->addRef();
273  registerAlgTool(**itra).ignore();
274  }
275 
276  m_log << MSG::INFO << "Registered " << m_algtools.size() << " AlgTools"
277  << endmsg;
278 
280 
281  std::list<IService*> svcs = Gaudi::svcLocator()->getServices();
282 
283  std::list<IService*>::const_iterator itrs;
284  for (itrs=svcs.begin(); itrs!=svcs.end(); ++itrs) {
285  (*itrs)->addRef();
286  registerSvc(**itrs).ignore();
287  }
288 
289  m_log << MSG::INFO;
290  m_log << "Registered " << svcs.size() << " Services" << endmsg;
291 
292  return StatusCode::SUCCESS;
293 
294 
295 }
296 
297 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
298 
300 
301  if (!m_activate) return StatusCode::SUCCESS;
302 
303  if (m_dump) {
305  }
306 
307  if (m_outputFile != "") {
308  std::ofstream ofs;
309  ofs.open(m_outputFile.c_str());
310  if (!ofs) {
311  m_log << MSG::ERROR << "Unable to open output file \"m_outputFile\""
312  << endmsg;
313  } else {
314 
315  // dumpProperties(ofs);
316  dumpState(ofs);
317 
318  ofs.close();
319  }
320  }
321 
322  clearState();
323 
324  return StatusCode::SUCCESS;
325 
326 }
327 
328 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
329 
331 
332  ON_VERBOSE
333  m_log << MSG::VERBOSE << "HistorySvc::finalize()" << endmsg;
334 
335 
336  clearState();
337 
338  StatusCode status = Service::finalize();
339 
340  if ( status.isSuccess() )
341  m_log << MSG::INFO << "Service finalised successfully" << endmsg;
342 
343  return status;
344 
345 }
346 
347 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
348 
349 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
350 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
351 
354 
355  JobHistory *job = getJobHistory();
356  if (m_algmap.find(&alg) != m_algmap.end()) {
357  m_log << MSG::WARNING << "Algorithm " << alg.name()
358  << " already registered with HistorySvc" << endmsg;
359  return StatusCode::SUCCESS;
360  }
361 
362  (const_cast<Algorithm*>(&alg))->addRef();
363 
364  m_algs.insert(&alg);
365 
366  AlgorithmHistory *algHist = new AlgorithmHistory(alg, job);
367  m_algmap[&alg] = algHist;
368 
369  ON_DEBUG {
370  m_log << MSG::DEBUG << "Registering algorithm: ";
372  m_log << alg.name() << endmsg;
373  m_log.resetColor();
374  }
375 
376  return StatusCode(StatusCode::SUCCESS,true);
377 
378 }
379 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
380 
383 
384  m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
385 
386  AlgorithmHistory *hist = getAlgHistory( alg );
387 
388  if (hist == 0) {
389  return StatusCode::FAILURE;
390  }
391 
392  ostringstream ost;
393  ost << *hist;
394 
395  std::string str = ost.str();
396 
397  m_log << MSG::INFO << alg.name() << " --> " << endl << str << endmsg;
398 
399  return StatusCode(StatusCode::SUCCESS,true);
400 
401 }
402 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
403 
404 void
405 HistorySvc::dumpProperties(const Algorithm &alg, std::ofstream& ofs) const {
406 
407  AlgorithmHistory *hist = getAlgHistory( alg );
408 
409  if (hist == 0) {
410  return;
411  }
412 
413  PropertyList::const_iterator itr;
414  for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
415  ofs << alg.name() << " " << dumpProp(*itr) << std::endl;
416  }
417 
418 }
419 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
420 
423 
424  const Algorithm *palg = &alg;
425  set<const Algorithm*>::const_iterator itr = m_algs.find(palg);
426  if ( itr == m_algs.end() ) {
427  m_log << MSG::WARNING << "Algorithm " << alg.name() << " not registered"
428  << endmsg;
429  return 0;
430  }
431 
432  map<const Algorithm*, AlgorithmHistory*>::const_iterator itr2;
433  itr2 = m_algmap.find( *itr );
434 
435  return ( itr2->second );
436 
437 }
438 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
439 
440 void
441 HistorySvc::getAlgHistory(std::set<AlgorithmHistory*>& algs) const {
442 
443  set<const Algorithm*>::const_iterator itr;
444  for (itr=m_algs.begin(); itr!=m_algs.end(); ++itr) {
445  AlgorithmHistory *ah = m_algmap.find(*itr)->second;
446 
447  algs.insert(ah);
448  }
449 
450 }
451 
452 
453 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
456 
457 
458  return StatusCode(StatusCode::SUCCESS,true);
459 
460 
461 }
462 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
463 
464 
467 
468  m_log << MSG::INFO;
470  m_log << "Dumping properties for all Algorithms (" << m_algmap.size()
471  << ")" << endmsg;
472 
473  std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
474  for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
475  const Algorithm* alg = itr->first;
476 
477  listProperties( *alg ).ignore();
478 
479  }
480 
481  m_log << MSG::INFO;
483  m_log << "Dumping properties for all AlgTools (" << m_algtoolmap.size()
484  << ")" << endmsg;
485 
486  std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
487  for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
488  ON_DEBUG
489  m_log << MSG::DEBUG << " --> " << itr_a->second->algtool_name() << endmsg;
490  const AlgTool* alg = itr_a->first;
491 
492  listProperties( *alg ).ignore();
493 
494  }
495 
496  m_log << MSG::INFO;
498  m_log << "Dumping properties for all Services (" << m_svcmap.size()
499  << ")" << endmsg;
500 
501  std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
502  for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
503  const IService* svc = itr_s->first;
504 
505  listProperties( *svc ).ignore();
506 
507  }
508 
509  m_log << MSG::INFO;
511  m_log << "Dumping properties for Job";
512  m_log.resetColor();
513 
514  ostringstream ost;
515  ost << *m_jobHistory;
516  std::string str = ost.str();
517 
518  m_log << std::endl << str << endmsg;
519 
520  return StatusCode(StatusCode::SUCCESS,true);
521 
522 }
523 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
524 
525 void
526 HistorySvc::dumpProperties(std::ofstream& ofs) const {
527 
528 
529  ofs << "GLOBAL" << std::endl;
531  JobHistory::PropertyPairList::const_iterator itrj;
532  for (itrj=props.begin(); itrj != props.end(); ++itrj) {
533  std::string client = itrj->first;
534  const Property* prp = itrj->second;
535  ofs << client << " " << dumpProp(prp) << std::endl;
536  }
537 
538  ofs << std::endl << "SERVICES" << std::endl;
539  std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
540  for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
541  const IService* svc = itr_s->first;
542 
543  dumpProperties( *svc, ofs );
544 
545  }
546 
547  ofs << std::endl << "ALGORITHMS" << std::endl;
548  std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
549  for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
550  const Algorithm* alg = itr->first;
551 
552  dumpProperties( *alg, ofs );
553 
554  }
555 
556  ofs << std::endl << "ALGTOOLS" << std::endl;
557  std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
558  for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
559  const AlgTool* alg = itr_a->first;
560 
561  dumpProperties( *alg, ofs );
562 
563  }
564 
565 
566 }
567 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
568 
569 JobHistory*
571 
572  return m_jobHistory;
573 
574 }
575 
576 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
577 IAlgorithm*
579  if (p_algCtxSvc == 0) {
580  m_log << MSG::WARNING << "trying to create DataHistoryObj before "
581  << "HistorySvc has been initialized" << endmsg;
582  return 0;
583 
584  } else {
585  return p_algCtxSvc->currentAlg();
586  }
587 }
588 
589 
590 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
591 
593 HistorySvc::createDataHistoryObj(const CLID& id, const std::string& key,
594  const std::string& /* storeName */) {
595 
596  if (!m_activate) return 0;
597 
598 
599  AlgorithmHistory *algHist;
600 
601  IAlgorithm* ialg = getCurrentIAlg();
602  if (ialg == 0) {
603  ON_DEBUG
604  m_log << MSG::DEBUG
605  << "Could not discover current Algorithm:" << endl
606  << " object CLID: " << id << " key: \"" << key
607  << "\"" << endmsg;
608  algHist = 0;
609  } else {
610  Algorithm* alg = dynamic_cast<Algorithm*>(ialg);
611  if (alg != 0) {
612  algHist = getAlgHistory( *alg );
613  } else {
615  << "Could not extract concrete Algorithm:"
616  << endl
617  << " object CLID: " << id << " key: \"" << key
618  << "\"" << endmsg;
619  algHist = 0;
620  }
621  }
622 
623  DataHistory *hist = new DataHistory(id, key, algHist);
624 
625  return hist;
626 
627 }
628 
629 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
631 HistorySvc::registerDataHistory(const CLID& id, const std::string& key,
632  const std::string& storeName) {
633 
634  DHH dhh(id,key);
635 
636  pair<DHMitr,DHMitr> mitr = m_datMap.equal_range(dhh);
637 
638  if (mitr.first == mitr.second) {
639  // not in the map
640  DataHistory *dh = createDataHistoryObj(id,key,storeName);
641  m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
642  } else {
643  // found at least one
644  bool match(false);
645 
646  std::string algName;
647  IAlgorithm *ialg = getCurrentIAlg();
648  if (ialg != 0) {
649  algName = ialg->name();
650  } else {
651  algName = "UNKNOWN";
652  }
653 
654  for (DHMitr itr = mitr.first; itr != mitr.second; ++itr) {
655  DataHistory *dh = itr->second;
656  if (dh->algorithmHistory()->algorithm_name() == algName) {
657  match = true;
658  break;
659  }
660  }
661 
662  if (! match) {
663  DataHistory *dh = createDataHistoryObj(id,key,storeName);
664  m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
665  }
666  }
667 
668  return StatusCode::SUCCESS;
669 
670 }
671 
672 
673 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
675 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
676  const std::string& /*storeName*/) const {
677 
678  DHH dhh(id,key);
679 
680  pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
681 
682  if(mitr.first == mitr.second) {
683  return 0;
684  }
685 
686  return mitr.first->second;
687 
688 }
689 
690 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
691 
692 int
693 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
694  const std::string& /*storeName*/,
695  std::list<DataHistory*>& dhlist) const {
696 
697  DHH dhh(id,key);
698 
699  int n(0);
700 
701  pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
702 
703  for (DHMCitr itr=mitr.first; itr != mitr.second; ++itr) {
704  dhlist.push_back(itr->second);
705  n++;
706  }
707 
708  return n;
709 
710 }
711 
712 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
713 
716 
717  if ( svc.name() == "HistoryStore" ) {
718  // m_log << MSG::WARNING << "not registering store" << endmsg;
719  return StatusCode(StatusCode::SUCCESS,true);
720  }
721 
722  JobHistory *job = getJobHistory();
723  const IService* psvc = &svc;
724  map<const IService*, ServiceHistory*>::const_iterator itr =
725  m_svcmap.find(psvc);
726  if (itr == m_svcmap.end()) {
727 
728  if (m_log.level() <= MSG::DEBUG) {
729  m_log << MSG::DEBUG << "Registering Service: ";
731  m_log << svc.name() << endmsg;
732  }
733 
734  m_svcs.insert(psvc);
735 
736  ServiceHistory *svcHist = new ServiceHistory(&svc, job);
737  m_svcmap[psvc] = svcHist;
738 
739  (const_cast<IService*>(psvc))->addRef();
740 
741  }
742 
743  m_log.resetColor();
744  return StatusCode(StatusCode::SUCCESS,true);
745 
746 }
747 
748 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
749 
752 
753  const IService *psvc = &svc;
754  map<const IService*, ServiceHistory*>::const_iterator itr =
755  m_svcmap.find(psvc);
756  if ( itr == m_svcmap.end() ) {
757  m_log << MSG::WARNING << "Service " << svc.name() << " not registered"
758  << endmsg;
759  return 0;
760  }
761 
762  return ( itr->second );
763 
764 }
765 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
766 
767 void
768 HistorySvc::getServiceHistory(std::set<ServiceHistory*>& svcs) const {
769 
770  set<const IService*>::const_iterator itr;
771  for (itr=m_svcs.begin(); itr!=m_svcs.end(); ++itr) {
772  ServiceHistory *sh = m_svcmap.find( *itr )->second;
773 
774  svcs.insert(sh);
775  }
776 
777 }
778 
779 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
780 
783 
784  m_log << MSG::INFO << "Dumping properties for " << svc.name() << endl;
785 
786  ServiceHistory *hist = getServiceHistory( svc );
787 
788  if (hist == 0) {
789  return StatusCode::FAILURE;
790  }
791 
792  ostringstream ost;
793  ost << *hist;
794 
795  std::string str = ost.str();
796 
797  m_log << MSG::INFO << svc.name() << " --> " << endl << str << endmsg;
798 
799 
800 
801  return StatusCode(StatusCode::SUCCESS,true);
802 
803 }
804 
805 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
806 
807 void
808 HistorySvc::dumpProperties(const IService &svc, std::ofstream &ofs) const {
809 
810  ServiceHistory *hist = getServiceHistory( svc );
811 
812  if (hist == 0) {
813  return;
814  }
815 
816  PropertyList::const_iterator itr;
817  for (itr=hist->properties().begin(); itr != hist->properties().end();++itr) {
818  ofs << svc.name() << " " << dumpProp(*itr) << std::endl;
819  }
820 
821 }
822 
823 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
824 
827 
828  if (! m_isInitialized) {
829  if (p_algCtxSvc == 0) {
830  if ( service("AlgContextSvc",p_algCtxSvc,true).isFailure() ) {
831  m_log << MSG::ERROR << "unable to get the AlgContextSvc" << endmsg;
832  return StatusCode::FAILURE;
833  }
834  }
835 
836  m_ialgtools.insert(&ialg);
837  return StatusCode::SUCCESS;
838  }
839 
840  const AlgTool *alg = dynamic_cast<const AlgTool*>( &ialg );
841  if ( alg == 0 ) {
842  m_log << MSG::ERROR << "Could not dcast IAlgTool \"" << ialg.name()
843  << "\" to an AlgTool" << endmsg;
844  return StatusCode::FAILURE;
845  }
846 
847  if (m_algtools.find(alg) != m_algtools.end()) {
848  m_log << MSG::WARNING << "AlgTool " << ialg.name()
849  << " already registered in HistorySvc" << endmsg;
850  return StatusCode::SUCCESS;
851  }
852 
853  m_algtools.insert(alg);
854  (const_cast<AlgTool*>(alg))->addRef();
855 
856  const JobHistory *job = getJobHistory();
857  AlgToolHistory *algHist = new AlgToolHistory(*alg, job);
858  m_algtoolmap[alg] = algHist;
859 
860  if (m_log.level() <= MSG::DEBUG) {
861  m_log << MSG::DEBUG << "Registering algtool: ";
863  m_log << alg->name() << endmsg;
864  m_log.resetColor();
865  }
866 
867  return StatusCode::SUCCESS;
868 
869 }
870 
871 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
872 
875 
876  m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
877 
878  AlgToolHistory *hist = getAlgToolHistory( alg );
879 
880  if (hist == 0) {
881  return StatusCode::FAILURE;
882  }
883 
884  ostringstream ost;
885  ost << *hist;
886 
887  std::string str = ost.str();
888 
889  m_log << MSG::INFO << alg.name() << " --> " << endl << str << endmsg;
890 
891  return StatusCode::SUCCESS;
892 
893 }
894 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
895 
896 void
897 HistorySvc::dumpProperties(const IAlgTool& alg, std::ofstream &ofs) const {
898 
899  AlgToolHistory *hist = getAlgToolHistory( alg );
900 
901  if (hist == 0) {
902  return;
903  }
904 
905  PropertyList::const_iterator itr;
906  for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
907  ofs << alg.name() << " " << dumpProp(*itr) << std::endl;
908  }
909 
910 }
911 
912 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
913 
916 
917  const AlgTool *palg = dynamic_cast<const AlgTool*>(&alg);
918  set<const AlgTool*>::const_iterator itr = m_algtools.find(palg);
919  if ( itr == m_algtools.end() ) {
920  m_log << MSG::WARNING << "AlgTool " << alg.name() << " not registered"
921  << endmsg;
922  return 0;
923  }
924 
925  map<const AlgTool*, AlgToolHistory*>::const_iterator itr2;
926  itr2 = m_algtoolmap.find( *itr );
927 
928  return ( itr2->second );
929 
930 }
931 
932 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
933 
934 void
935 HistorySvc::getAlgToolHistory(std::set<AlgToolHistory*>& algs) const {
936 
937  set<const AlgTool*>::const_iterator itr;
938  for (itr=m_algtools.begin(); itr!=m_algtools.end(); ++itr) {
939  AlgToolHistory *ah = m_algtoolmap.find(*itr)->second;
940 
941  algs.insert(ah);
942  }
943 
944 }
945 
946 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
947 
948 void
949 HistorySvc::handle(const Incident& incident) {
950 
951  if (incident.type() == IncidentType::BeginEvent) {
952  if (captureState().isFailure()) {
953  m_log << MSG::WARNING << "Error capturing state." << endl
954  << "Will try again at next BeginEvent incident" << endmsg;
955  }
956  }
957 
958 }
959 
960 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
961 
962 
963 std::string
964 HistorySvc::dumpProp(const Property* prop, bool isXML, int ind) const {
965  std::ostringstream ost;
966  if (isXML) {
967  while (ind > 0) {
968  ost << " ";
969  ind --;
970  }
971  ost << "<PROPERTY name=\"" << prop->name()
972  << "\" value=\"" << HistoryObj::convert_string(prop->toString())
973  << "\" documentation=\"" << HistoryObj::convert_string(prop->documentation())
974  << "\">";
975  } else {
976  prop->fillStream(ost);
977  }
978  return ost.str();
979 }
980 
981 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
982 
983 void
984 HistorySvc::dumpState(std::ofstream& ofs) const {
985 
986 
987  if(m_outputFileTypeXML) {
988  //xml header
989  ofs << "<?xml version=\"1.0\" ?> " << std::endl;
990  ofs << "<!--Test-xml-->" << std::endl;
991  ofs << "<SETUP>" << std::endl;
992  ofs << " <GLOBAL>" << std::endl;
993  } else {
994  ofs << "GLOBAL" << std::endl;
995  }
996 
998  JobHistory::PropertyPairList::const_iterator itrj;
999  std::string client_currently_open = "start";
1000  for (itrj=props.begin(); itrj != props.end(); ++itrj) {
1001  // client is the name of the component of the current property
1002  std::string client = itrj->first;
1003  const Property* prp = itrj->second;
1004 
1005  if (m_outputFileTypeXML) {
1006 
1007  if (client != client_currently_open) {
1008  if(client_currently_open!="start") ofs << " </COMPONENT>" << endl;
1009  ofs << " <COMPONENT name=\""
1010  << client << "\" class=\"undefined\">" << std::endl;
1011  }
1012  } else {
1013  ofs << client << " ";
1014  }
1015 
1016  ofs << dumpProp(prp,m_outputFileTypeXML,6) << endl;
1017 
1018  client_currently_open = client;
1019 
1020  if (m_outputFileTypeXML)
1021  ofs << " </COMPONENT>" << endl;
1022  }
1023 
1024 
1025  if(m_outputFileTypeXML) {
1026  ofs << "</GLOBAL>" << endl << "<SERVICES>" << endl;
1027  } else {
1028  ofs << "SERVICES" << std::endl;
1029  }
1030 
1031  std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
1032  for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
1033  const IService* svc = itr_s->first;
1034 
1035  dumpState(svc,ofs);
1036 
1037  }
1038 
1039  if(m_outputFileTypeXML) {
1040  ofs << "</SERVICES>" << endl << "<ALGORITHMS> " << endl;
1041  } else {
1042  ofs << "ALGORITHMS" << std::endl;
1043  }
1044 
1045  std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
1046  for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
1047  const Algorithm* alg = itr->first;
1048 
1049  dumpState(alg,ofs);
1050 
1051  }
1052 
1053 
1054  if(m_outputFileTypeXML) {
1055  ofs << "</ALGORITHMS>" << endl << "<ALGTOOLS> " << endl;
1056  } else {
1057  ofs << "ALGTOOLS" << std::endl;
1058  }
1059 
1060  std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
1061  for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
1062  const AlgTool* alg = itr_a->first;
1063 
1064  dumpState( alg, ofs);
1065 
1066  }
1067 
1068  if(m_outputFileTypeXML) {
1069  ofs << "</ALGTOOLS>" << endl << "</SETUP>" << endl;
1070  }
1071 
1072 }
1073 
1074 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1075 
1076 void
1077 HistorySvc::dumpState(const INamedInterface *in, std::ofstream& ofs) const {
1078 
1079  HistoryObj *hist(0);
1080  IVersHistoryObj *vhist(0);
1081 
1082  const IService* is(0);
1083  const Algorithm* ia(0);
1084  const IAlgTool* it(0);
1085  if ( (is=dynamic_cast<const IService*>(in)) != 0) {
1086  ON_VERBOSE
1087  m_log << MSG::VERBOSE << in->name() << " is Service" << endmsg;
1088  ServiceHistory* o = getServiceHistory( *is );
1089  hist = dynamic_cast<HistoryObj*>( o );
1090  vhist = dynamic_cast<IVersHistoryObj*>( o );
1091  } else if ( (ia = dynamic_cast<const Algorithm*>(in)) != 0 ) {
1092  ON_VERBOSE
1093  m_log << MSG::VERBOSE << in->name() << " is Alg" << endmsg;
1094  AlgorithmHistory *o = getAlgHistory( *ia );
1095  hist = dynamic_cast<HistoryObj*>( o );
1096  vhist = dynamic_cast<IVersHistoryObj*>( o );
1097  } else if ( (it = dynamic_cast<const IAlgTool*>(in)) != 0 ) {
1098  ON_VERBOSE
1099  m_log << MSG::VERBOSE << in->name() << " is AlgTool" << endmsg;
1100  AlgToolHistory *o = getAlgToolHistory( *it );
1101  hist = dynamic_cast<HistoryObj*>( o );
1102  vhist = dynamic_cast<IVersHistoryObj*>( o );
1103  } else {
1104  m_log << MSG::ERROR
1105  << "Could not dcast interface to accepted History Obj type for "
1106  << in->name() << endmsg;
1107  return;
1108  }
1109 
1110  if (hist == 0 || vhist == 0) {
1111  m_log << MSG::ERROR << "Could not dcast recognized object to HistoryObj or IVersHistoryObj. This should never happen."
1112  << endmsg;
1113  return;
1114  }
1115 
1116  if (m_outputFileTypeXML) {
1117  hist->dump(ofs,true);
1118  } else {
1119  ofs << ">> " << vhist->name() << endl << *hist << endl;
1120  }
1121 
1122 }
const std::string BeginEvent
Processing of a new event has started.
Definition: Incident.h:60
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:608
virtual StatusCode stop()
Stop (from RUNNING to INITIALIZED).
Definition: HistorySvc.cpp:299
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:243
virtual const std::vector< const Property * > * getProperties(const std::string &client) const =0
Get the properties associated to a given client.
virtual ServiceHistory * getServiceHistory(const IService &) const
Definition: HistorySvc.cpp:751
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
const std::string & type() const
Access to the incident type.
Definition: Incident.h:34
virtual const std::vector< Property * > & getProperties() const =0
Get list of properties.
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:245
SmartIF< IToolSvc > m_toolSvc
Definition: HistorySvc.h:142
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:117
AlgorithmHistory * algorithmHistory() const
Definition: DataHistory.h:67
virtual std::string toString() const =0
value -> string
MSG::Level level()
Retrieve output level.
Definition: MsgStream.h:112
const std::string & name() const
property name
Definition: Property.h:47
std::vector< std::pair< std::string, const Property * > > PropertyPairList
Definition: JobHistory.h:29
CLID id
Definition: HistorySvc.cpp:48
virtual void dump(std::ostream &, const bool isXML=false, int indent=0) const =0
AlgorithmHistory class definition.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
virtual StatusCode registerDataHistory(const CLID &id, const std::string &key, const std::string &store)
Definition: HistorySvc.cpp:631
virtual StatusCode listProperties() const
Definition: HistorySvc.cpp:466
The IAlgManager is the interface implemented by the Algorithm Factory in the Application Manager to s...
Definition: IAlgManager.h:28
virtual StatusCode registerJob()
Definition: HistorySvc.cpp:455
virtual std::vector< std::string > getClients() const =0
Get the list of clients.
Base class for History Objects.
Definition: HistoryObj.h:24
HistorySvc class definition.
Definition: HistorySvc.h:43
std::multimap< DHH, DataHistory * > m_datMap
Definition: HistorySvc.h:120
std::map< const AlgTool *, AlgToolHistory * > m_algtoolmap
Definition: HistorySvc.h:115
std::string key
Definition: HistorySvc.cpp:49
const std::string & documentation() const
property documentation
Definition: Property.h:49
JobHistory class definition.
Definition: JobHistory.h:25
void fenv_dummy(char **)
Definition: HistorySvc.cpp:62
std::string m_outputFile
Definition: HistorySvc.h:126
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:72
virtual std::ostream & fillStream(std::ostream &) const
the printout of the property value
Definition: Property.cpp:182
virtual ~HistorySvc()
Definition: HistorySvc.cpp:87
IAlgorithm * getCurrentIAlg() const
Definition: HistorySvc.cpp:578
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
virtual const std::string & name() const =0
Retrieve the name of the instance.
virtual const std::list< IAlgorithm * > & getAlgorithms() const =0
Return the list of Algorithms.
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:90
virtual AlgToolHistory * getAlgToolHistory(const IAlgTool &) const
Definition: HistorySvc.cpp:915
virtual void handle(const Incident &inc)
Inform that a new incident has occurred.
Definition: HistorySvc.cpp:949
bool m_activate
Definition: HistorySvc.h:106
virtual unsigned long addRef()
Add reference to object.
Definition: DataObject.cpp:53
std::set< const Algorithm * > m_algs
Definition: HistorySvc.h:110
Main interface for the JobOptions service.
DHH(const CLID &i, const std::string &k)
Definition: HistorySvc.cpp:51
GAUDI_API ISvcLocator * svcLocator()
bool m_isInitialized
Definition: HistorySvc.h:104
const std::string & algorithm_name() const
General service interface definition.
Definition: IService.h:19
virtual StatusCode registerSvc(const IService &)
Definition: HistorySvc.cpp:715
#define ON_DEBUG
Definition: HistorySvc.cpp:35
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
HistorySvc(const std::string &name, ISvcLocator *svc)
Definition: HistorySvc.cpp:64
bool m_outputFileTypeXML
Definition: HistorySvc.h:145
virtual StatusCode registerAlgTool(const IAlgTool &)
Definition: HistorySvc.cpp:826
DataHistory class definition.
Definition: DataHistory.h:25
IAlgContextSvc * p_algCtxSvc
Definition: HistorySvc.h:108
virtual const std::string & name() const
The identifying name of the algorithm object.
Definition: Algorithm.cpp:837
JobHistory * m_jobHistory
Definition: HistorySvc.h:124
virtual StatusCode registerAlg(const Algorithm &)
Definition: HistorySvc.cpp:353
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: HistorySvc.cpp:136
virtual AlgorithmHistory * getAlgHistory(const Algorithm &) const
Definition: HistorySvc.cpp:422
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: HistorySvc.cpp:330
const PropertyList & properties() const
unsigned int CLID
Class ID definition.
Definition: ClassID.h:9
void addProperty(const std::string &, const Property *)
Definition: JobHistory.cpp:87
DataHistMap::const_iterator DHMCitr
Definition: HistorySvc.h:100
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:20
MsgStream m_log
Definition: HistorySvc.h:144
const TYPE & value() const
explicit conversion
Definition: Property.h:355
const PropertyPairList & propertyPairs() const
Definition: JobHistory.h:80
std::string dumpProp(const Property *, const bool isXML=false, int indent=0) const
Definition: HistorySvc.cpp:964
AlgToolHistory class definition.
void dumpState(std::ofstream &) const
Definition: HistorySvc.cpp:984
#define min(a, b)
const PropertyList & properties() const
IIncidentSvc * m_incidentSvc
Definition: HistorySvc.h:141
virtual DataHistory * createDataHistoryObj(const CLID &id, const std::string &key, const std::string &store)
Definition: HistorySvc.cpp:593
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:61
IInterface compliant class extending IInterface with the name() method.
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:43
virtual DataHistory * getDataHistory(const CLID &id, const std::string &key, const std::string &store) const
Definition: HistorySvc.cpp:675
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:74
const PropertyList & properties() const
void dumpProperties(std::ofstream &) const
Definition: HistorySvc.cpp:526
static std::string convert_string(const std::string &)
Definition: HistoryObj.cpp:29
Base class for all Incidents (computing events).
Definition: Incident.h:16
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:34
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
virtual void addListener(IIncidentListener *lis, const std::string &type="", long priority=0, bool rethrow=false, bool singleShot=false)=0
Add listener.
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:106
Templated class to add the standard messaging functionalities.
ServiceHistory class definition.
virtual const std::string & name() const =0
std::map< const Algorithm *, AlgorithmHistory * > m_algmap
Definition: HistorySvc.h:111
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
bool m_dump
Definition: HistorySvc.h:105
#define ON_VERBOSE
Definition: HistorySvc.cpp:36
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:142
std::set< const IService * > m_svcs
Definition: HistorySvc.h:117
virtual const std::list< IService * > & getServices() const =0
Get a reference to a service and create it if it does not exists.
std::set< const IAlgTool * > m_ialgtools
Definition: HistorySvc.h:113
void ignore() const
Definition: StatusCode.h:94
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:211
Interface for Versioned History Objects.
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:22
bool operator<(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:233
virtual const std::string & name() const
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:51
DataHistMap::iterator DHMitr
Definition: HistorySvc.h:99
virtual IAlgorithm * currentAlg() const =0
accessor to current algorithm:
void clearState()
Definition: HistorySvc.cpp:105
list i
Definition: ana.py:128
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:171
virtual StatusCode captureState()
Definition: HistorySvc.cpp:195
std::map< const IService *, ServiceHistory * > m_svcmap
Definition: HistorySvc.h:118
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:199
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual StatusCode reinitialize()
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: HistorySvc.cpp:93
std::set< const AlgTool * > m_algtools
Definition: HistorySvc.h:114
virtual JobHistory * getJobHistory() const
Definition: HistorySvc.cpp:570
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:336
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.