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 if ( p_algCtxSvc.retrieve().isFailure() ) {
135 error() <<
"unable to get the AlgContextSvc" <<
endmsg;
139 auto incidentSvc = service<IIncidentSvc>(
"IncidentSvc",
true );
140 if ( !incidentSvc ) {
141 error() <<
"unable to get the IncidentSvc" <<
endmsg;
147 m_toolSvc = serviceLocator()->service(
"ToolSvc" );
149 error() <<
"could not retrieve the ToolSvc handle !" <<
endmsg;
155 const bool rethrow =
false;
156 const bool oneShot =
true;
159 m_outputFileTypeXML = ba::iends_with( m_outputFile.value(),
".xml" );
160 ON_DEBUG if ( m_outputFileTypeXML ) { debug() <<
"output format is XML" <<
endmsg; }
162 m_isInitialized =
true;
170 if ( !m_jobHistory ) {
171 m_jobHistory = std::make_unique<JobHistory>();
173 bool foundAppMgr(
false );
174 for (
const auto& item : serviceLocator()->getOptsSvc().items() ) {
175 m_jobHistory->addProperty( get<0>( item ), get<1>( item ) );
176 foundAppMgr |= get<0>( item ).compare( 0, 15,
"ApplicationMgr." ) == 0;
179 if ( !foundAppMgr ) {
180 auto ap = service<IProperty>(
"ApplicationMgr" );
182 error() <<
"could not get the ApplicationMgr" <<
endmsg;
184 for (
auto prop : ap->getProperties() ) {
185 m_jobHistory->addProperty(
"ApplicationMgr." + prop->name(), prop->toString() );
195 error() <<
"Could not get AlgManager" <<
endmsg;
200 for (
auto ialg : algMgr->getAlgorithms() ) {
203 warning() <<
"Algorithm " << ialg->
name() <<
" does not inherit from Gaudi::Algorithm. Not registering it."
207 registerAlg( *alg ).ignore();
211 info() <<
"Registered " <<
count <<
" Algorithms" <<
endmsg;
215 m_isInitialized =
true;
216 for (
auto algtool : m_ialgtools ) {
217 (
const_cast<IAlgTool*
>( algtool ) )->addRef();
218 registerAlgTool( *algtool ).ignore();
221 info() <<
"Registered " << m_algtoolmap.size() <<
" AlgTools" <<
endmsg;
227 registerSvc( *svc ).ignore();
241 if ( m_dump ) { listProperties().ignore(); }
243 if ( !m_outputFile.empty() ) {
246 error() <<
"Unable to open output file \"m_outputFile\"" <<
endmsg;
269 if ( status.
isSuccess() ) info() <<
"Service finalised successfully" <<
endmsg;
282 if ( m_algmap.find( &
alg ) != m_algmap.end() ) {
283 warning() <<
"Algorithm " <<
alg.name() <<
" already registered with HistorySvc" <<
endmsg;
293 log <<
"Registering algorithm: ";
305 info() <<
"Dumping properties for " <<
alg.name() <<
endl;
321 if ( !hist ) {
return; }
329 auto itr = m_algmap.find( &
alg );
330 if ( itr == m_algmap.end() ) {
331 warning() <<
"Algorithm " <<
alg.name() <<
" not registered" <<
endmsg;
349 log <<
"Dumping properties for all Algorithms (" << m_algmap.size() <<
")" <<
endmsg;
351 for (
auto&
alg : m_algmap ) { listProperties( *
alg.first ).ignore(); }
355 log <<
"Dumping properties for all AlgTools (" << m_algtoolmap.size() <<
")" <<
endmsg;
357 for (
auto& algtool : m_algtoolmap ) {
359 debug() <<
" --> " << algtool.second->algtool_name() <<
endmsg;
360 listProperties( *algtool.first ).ignore();
365 log <<
"Dumping properties for all Services (" << m_svcmap.size() <<
")" <<
endmsg;
367 for (
auto& svc : m_svcmap ) { listProperties( *svc.first ).ignore(); }
371 log <<
"Dumping properties for Job";
383 for (
const auto& prop : m_jobHistory->propertyPairs() ) {
384 ofs << prop.first <<
" " << dumpProp( prop.second.get() ) <<
std::endl;
388 for (
const auto&
s : m_svcmap ) dumpProperties( *
s.first, ofs );
391 for (
const auto&
alg : m_algmap ) dumpProperties( *
alg.first, ofs );
394 for (
const auto&
tool : m_algtoolmap ) dumpProperties( *
tool.first, ofs );
402 if ( p_algCtxSvc.isSet() )
return p_algCtxSvc->currentAlg();
403 warning() <<
"trying to create DataHistoryObj before "
404 <<
"HistorySvc has been initialized" <<
endmsg;
413 if ( !m_activate )
return nullptr;
420 debug() <<
"Could not discover current Algorithm:" <<
endl
421 <<
" object CLID: " <<
id <<
" key: \"" <<
key <<
"\"" <<
endmsg;
426 algHist = getAlgHistory( *
alg );
428 warning() <<
"Could not extract concrete Algorithm:" <<
endl
429 <<
" object CLID: " <<
id <<
" key: \"" <<
key <<
"\"" <<
endmsg;
441 auto boundaries = m_datMap.equal_range( dhh );
442 auto match = boundaries.second;
444 if ( boundaries.first != boundaries.second ) {
450 algName = ialg->name();
455 match =
std::find_if( boundaries.first, boundaries.second,
457 return p.second->algorithmHistory()->algorithm_name() == algName;
461 if ( match == boundaries.second ) {
475 auto mitr = m_datMap.equal_range( dhh );
476 return ( mitr.first != mitr.second ) ? mitr.first->second :
nullptr;
488 auto mitr = m_datMap.equal_range( dhh );
489 for (
auto itr = mitr.first; itr != mitr.second; ++itr ) {
504 auto itr = m_svcmap.find( psvc );
505 if ( itr == m_svcmap.end() ) {
509 log <<
"Registering Service: ";
517 (
const_cast<IService*
>( psvc ) )->addRef();
528 auto itr = m_svcmap.find( psvc );
529 if ( itr != m_svcmap.end() )
return itr->second;
531 warning() <<
"Service " << svc.name() <<
" not registered" <<
endmsg;
542 info() <<
"Dumping properties for " << svc.name() <<
endl;
548 info() << svc.name() <<
" --> " <<
endl << *hist <<
endmsg;
561 for (
auto& prop : hist->
properties() ) { ofs << svc.name() <<
" " << dumpProp( prop ) <<
std::endl; }
568 if ( !m_isInitialized ) {
569 if ( !p_algCtxSvc.isSet() ) {
570 if ( p_algCtxSvc.retrieve().isFailure() ) {
571 error() <<
"unable to get the AlgContextSvc" <<
endmsg;
575 m_ialgtools.insert( &ialg );
581 error() <<
"Could not dcast IAlgTool \"" << ialg.
name() <<
"\" to an AlgTool" <<
endmsg;
585 if ( m_algtoolmap.find(
alg ) != m_algtoolmap.end() ) {
586 warning() <<
"AlgTool " << ialg.
name() <<
" already registered in HistorySvc" <<
endmsg;
595 log <<
"Registering algtool: ";
608 info() <<
"Dumping properties for " <<
alg.name() <<
endl;
634 auto itr = m_algtoolmap.find( palg );
635 if ( itr == m_algtoolmap.end() ) {
636 warning() <<
"AlgTool " <<
alg.name() <<
" not registered" <<
endmsg;
650 if ( incident.
type() == IncidentType::BeginEvent ) {
651 if ( captureState().isFailure() ) {
652 warning() <<
"Error capturing state." <<
endl <<
"Will try again at next BeginEvent incident" <<
endmsg;
678 if ( m_outputFileTypeXML ) {
680 ofs <<
"<?xml version=\"1.0\" ?> " <<
std::endl;
689 for (
auto& item : m_jobHistory->propertyPairs() ) {
691 const auto& client = item.first;
692 const auto& prp = item.second;
694 if ( m_outputFileTypeXML ) {
696 if ( client != client_currently_open ) {
697 if ( client_currently_open !=
"start" ) ofs <<
" </COMPONENT>" <<
endl;
698 ofs <<
" <COMPONENT name=\"" << client <<
"\" class=\"undefined\">" <<
std::endl;
701 ofs << client <<
" ";
704 ofs << dumpProp( prp.get(), m_outputFileTypeXML, 6 ) <<
endl;
706 client_currently_open = client;
708 if ( m_outputFileTypeXML ) ofs <<
" </COMPONENT>" <<
endl;
711 if ( m_outputFileTypeXML ) {
712 ofs <<
"</GLOBAL>" <<
endl <<
"<SERVICES>" <<
endl;
718 auto sortedDump = [&ofs,
this](
const auto&
map ) {
720 for (
const auto& item :
map ) sorted[item.first->name()] = item.first;
721 for (
const auto& item : sorted ) dumpState( item.second, ofs );
724 sortedDump( m_svcmap );
726 if ( m_outputFileTypeXML ) {
727 ofs <<
"</SERVICES>" <<
endl <<
"<ALGORITHMS> " <<
endl;
732 sortedDump( m_algmap );
734 if ( m_outputFileTypeXML ) {
735 ofs <<
"</ALGORITHMS>" <<
endl <<
"<ALGTOOLS> " <<
endl;
740 sortedDump( m_algtoolmap );
742 if ( m_outputFileTypeXML ) { ofs <<
"</ALGTOOLS>" <<
endl <<
"</SETUP>" <<
endl; }
755 if ( ( is =
dynamic_cast<const IService*
>( in ) ) !=
nullptr ) {
761 }
else if ( ( ia =
dynamic_cast<const Gaudi::Algorithm*
>( in ) ) !=
nullptr ) {
767 }
else if ( ( it =
dynamic_cast<const IAlgTool*
>( in ) ) !=
nullptr ) {
774 error() <<
"Could not dcast interface to accepted History Obj type for " << in->
name() <<
endmsg;
778 if ( !hist || !vhist ) {
779 error() <<
"Could not dcast recognized object to HistoryObj or IVersHistoryObj. This should never happen."
784 if ( m_outputFileTypeXML ) {
785 hist->
dump( ofs,
true );