41 #include "boost/algorithm/string/predicate.hpp"
42 namespace ba = boost::algorithm;
49 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
50 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
62 template <
typename MAP,
typename SET>
63 inline void map_to_set(
const MAP&
m,
SET&
s ) {
77 return (
id < rhs.
id );
96 constexpr
struct clear_t {
97 template <
typename T1,
typename T2>
99 const_cast<T1*
>( p.first )->
release();
103 template <
typename M>
113 clear( m_algtoolmap );
125 debug() <<
"Failed to initialize the base class (Service)" <<
endmsg;
130 debug() <<
"Initializing HistorySvc" <<
endmsg;
134 static const bool CREATEIF(
true );
136 if ( service(
"AlgContextSvc", p_algCtxSvc, CREATEIF ).isFailure() ) {
137 error() <<
"unable to get the AlgContextSvc" <<
endmsg;
141 if ( service(
"IncidentSvc", m_incidentSvc, CREATEIF ).isFailure() ) {
142 error() <<
"unable to get the IncidentSvc" <<
endmsg;
148 m_toolSvc = serviceLocator()->service(
"ToolSvc" );
150 error() <<
"could not retrieve the ToolSvc handle !" <<
endmsg;
156 const bool rethrow =
false;
157 const bool oneShot =
true;
160 m_outputFileTypeXML = ba::iends_with( m_outputFile.value(),
".xml" );
161 ON_DEBUG if ( m_outputFileTypeXML ) { debug() <<
"output format is XML" <<
endmsg; }
163 m_isInitialized =
true;
171 if ( !m_jobHistory ) {
172 m_jobHistory = std::make_unique<JobHistory>();
174 bool foundAppMgr(
false );
175 for (
const auto& item : serviceLocator()->getOptsSvc().
items() ) {
176 m_jobHistory->addProperty( get<0>( item ), get<1>( item ) );
177 foundAppMgr |= get<0>( item ).compare( 0, 15,
"ApplicationMgr." ) == 0;
180 if ( !foundAppMgr ) {
181 auto ap = service<IProperty>(
"ApplicationMgr" );
183 error() <<
"could not get the ApplicationMgr" <<
endmsg;
185 for (
auto prop :
ap->getProperties() ) {
186 m_jobHistory->addProperty(
"ApplicationMgr." + prop->name(), prop->toString() );
196 error() <<
"Could not get AlgManager" <<
endmsg;
201 for (
auto ialg : algMgr->getAlgorithms() ) {
204 warning() <<
"Algorithm " << ialg->
name() <<
" does not inherit from Gaudi::Algorithm. Not registering it."
208 registerAlg( *alg ).ignore();
212 info() <<
"Registered " <<
count <<
" Algorithms" <<
endmsg;
216 m_isInitialized =
true;
217 for (
auto algtool : m_ialgtools ) {
218 (
const_cast<IAlgTool*
>( algtool ) )->addRef();
219 registerAlgTool( *algtool ).ignore();
222 info() <<
"Registered " << m_algtoolmap.size() <<
" AlgTools" <<
endmsg;
228 registerSvc( *svc ).ignore();
242 if ( m_dump ) { listProperties().ignore(); }
244 if ( !m_outputFile.empty() ) {
247 error() <<
"Unable to open output file \"m_outputFile\"" <<
endmsg;
270 if ( status.
isSuccess() ) info() <<
"Service finalised successfully" <<
endmsg;
283 if ( m_algmap.find( &
alg ) != m_algmap.end() ) {
284 warning() <<
"Algorithm " <<
alg.name() <<
" already registered with HistorySvc" <<
endmsg;
294 log <<
"Registering algorithm: ";
306 info() <<
"Dumping properties for " <<
alg.name() <<
endl;
322 if ( !hist ) {
return; }
330 auto itr = m_algmap.find( &
alg );
331 if ( itr == m_algmap.end() ) {
332 warning() <<
"Algorithm " <<
alg.name() <<
" not registered" <<
endmsg;
350 log <<
"Dumping properties for all Algorithms (" << m_algmap.size() <<
")" <<
endmsg;
352 for (
auto&
alg : m_algmap ) { listProperties( *
alg.first ).ignore(); }
356 log <<
"Dumping properties for all AlgTools (" << m_algtoolmap.size() <<
")" <<
endmsg;
358 for (
auto& algtool : m_algtoolmap ) {
360 debug() <<
" --> " << algtool.second->algtool_name() <<
endmsg;
361 listProperties( *algtool.first ).ignore();
366 log <<
"Dumping properties for all Services (" << m_svcmap.size() <<
")" <<
endmsg;
368 for (
auto& svc : m_svcmap ) { listProperties( *svc.first ).ignore(); }
372 log <<
"Dumping properties for Job";
384 for (
const auto& prop : m_jobHistory->propertyPairs() ) {
385 ofs << prop.first <<
" " << dumpProp( prop.second.get() ) <<
std::endl;
389 for (
const auto&
s : m_svcmap ) dumpProperties( *
s.first, ofs );
392 for (
const auto&
alg : m_algmap ) dumpProperties( *
alg.first, ofs );
395 for (
const auto&
tool : m_algtoolmap ) dumpProperties( *
tool.first, ofs );
403 if ( p_algCtxSvc )
return p_algCtxSvc->currentAlg();
404 warning() <<
"trying to create DataHistoryObj before "
405 <<
"HistorySvc has been initialized" <<
endmsg;
414 if ( !m_activate )
return nullptr;
421 debug() <<
"Could not discover current Algorithm:" <<
endl
422 <<
" object CLID: " <<
id <<
" key: \"" <<
key <<
"\"" <<
endmsg;
427 algHist = getAlgHistory( *
alg );
429 warning() <<
"Could not extract concrete Algorithm:" <<
endl
430 <<
" object CLID: " <<
id <<
" key: \"" <<
key <<
"\"" <<
endmsg;
442 auto boundaries = m_datMap.equal_range( dhh );
443 auto match = boundaries.second;
445 if ( boundaries.first != boundaries.second ) {
451 algName = ialg->name();
456 match =
std::find_if( boundaries.first, boundaries.second,
457 [&algName]( decltype( boundaries )::first_type::reference p ) ->
bool {
458 return p.second->algorithmHistory()->algorithm_name() == algName;
462 if ( match == boundaries.second ) {
476 auto mitr = m_datMap.equal_range( dhh );
477 return ( mitr.first != mitr.second ) ? mitr.first->second :
nullptr;
489 auto mitr = m_datMap.equal_range( dhh );
490 for (
auto itr = mitr.first; itr != mitr.second; ++itr ) {
505 auto itr = m_svcmap.find( psvc );
506 if ( itr == m_svcmap.end() ) {
510 log <<
"Registering Service: ";
518 (
const_cast<IService*
>( psvc ) )->addRef();
529 auto itr = m_svcmap.find( psvc );
530 if ( itr != m_svcmap.end() )
return itr->second;
532 warning() <<
"Service " << svc.name() <<
" not registered" <<
endmsg;
543 info() <<
"Dumping properties for " << svc.name() <<
endl;
549 info() << svc.name() <<
" --> " <<
endl << *hist <<
endmsg;
562 for (
auto& prop : hist->
properties() ) { ofs << svc.name() <<
" " << dumpProp( prop ) <<
std::endl; }
569 if ( !m_isInitialized ) {
570 if ( !p_algCtxSvc ) {
571 if ( service(
"AlgContextSvc", p_algCtxSvc,
true ).isFailure() ) {
572 error() <<
"unable to get the AlgContextSvc" <<
endmsg;
576 m_ialgtools.insert( &ialg );
582 error() <<
"Could not dcast IAlgTool \"" << ialg.
name() <<
"\" to an AlgTool" <<
endmsg;
586 if ( m_algtoolmap.find(
alg ) != m_algtoolmap.end() ) {
587 warning() <<
"AlgTool " << ialg.
name() <<
" already registered in HistorySvc" <<
endmsg;
596 log <<
"Registering algtool: ";
609 info() <<
"Dumping properties for " <<
alg.name() <<
endl;
635 auto itr = m_algtoolmap.find( palg );
636 if ( itr == m_algtoolmap.end() ) {
637 warning() <<
"AlgTool " <<
alg.name() <<
" not registered" <<
endmsg;
651 if ( incident.
type() == IncidentType::BeginEvent ) {
652 if ( captureState().isFailure() ) {
653 warning() <<
"Error capturing state." <<
endl <<
"Will try again at next BeginEvent incident" <<
endmsg;
679 if ( m_outputFileTypeXML ) {
681 ofs <<
"<?xml version=\"1.0\" ?> " <<
std::endl;
690 for (
auto& item : m_jobHistory->propertyPairs() ) {
692 const auto& client = item.first;
693 const auto& prp = item.second;
695 if ( m_outputFileTypeXML ) {
697 if ( client != client_currently_open ) {
698 if ( client_currently_open !=
"start" ) ofs <<
" </COMPONENT>" <<
endl;
699 ofs <<
" <COMPONENT name=\"" << client <<
"\" class=\"undefined\">" <<
std::endl;
702 ofs << client <<
" ";
705 ofs << dumpProp( prp.get(), m_outputFileTypeXML, 6 ) <<
endl;
707 client_currently_open = client;
709 if ( m_outputFileTypeXML ) ofs <<
" </COMPONENT>" <<
endl;
712 if ( m_outputFileTypeXML ) {
713 ofs <<
"</GLOBAL>" <<
endl <<
"<SERVICES>" <<
endl;
719 auto sortedDump = [&ofs,
this](
const auto&
map ) {
721 for (
const auto& item :
map ) sorted[item.first->name()] = item.first;
722 for (
const auto& item : sorted ) dumpState( item.second, ofs );
725 sortedDump( m_svcmap );
727 if ( m_outputFileTypeXML ) {
728 ofs <<
"</SERVICES>" <<
endl <<
"<ALGORITHMS> " <<
endl;
733 sortedDump( m_algmap );
735 if ( m_outputFileTypeXML ) {
736 ofs <<
"</ALGORITHMS>" <<
endl <<
"<ALGTOOLS> " <<
endl;
741 sortedDump( m_algtoolmap );
743 if ( m_outputFileTypeXML ) { ofs <<
"</ALGTOOLS>" <<
endl <<
"</SETUP>" <<
endl; }
756 if ( ( is =
dynamic_cast<const IService*
>( in ) ) !=
nullptr ) {
762 }
else if ( ( ia =
dynamic_cast<const Gaudi::Algorithm*
>( in ) ) !=
nullptr ) {
768 }
else if ( ( it =
dynamic_cast<const IAlgTool*
>( in ) ) !=
nullptr ) {
775 error() <<
"Could not dcast interface to accepted History Obj type for " << in->
name() <<
endmsg;
779 if ( !hist || !vhist ) {
780 error() <<
"Could not dcast recognized object to HistoryObj or IVersHistoryObj. This should never happen."
785 if ( m_outputFileTypeXML ) {
786 hist->
dump( ofs,
true );