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