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