13 #include "GaudiKernel/MsgStream.h"
14 #include "GaudiKernel/AttribStringParser.h"
15 #include "GaudiKernel/DataObject.h"
16 #include "GaudiKernel/IAlgorithm.h"
17 #include "GaudiKernel/ISvcLocator.h"
18 #include "GaudiKernel/IAlgManager.h"
19 #include "GaudiKernel/IIncidentSvc.h"
20 #include "GaudiKernel/DataIncident.h"
21 #include "GaudiKernel/IDataProviderSvc.h"
22 #include "GaudiKernel/IToolSvc.h"
23 #include "GaudiKernel/TypeNameString.h"
24 #include "GaudiKernel/ToStream.h"
25 #include "GaudiKernel/Chrono.h"
26 #include "GaudiKernel/LockedChrono.h"
27 #include "GaudiKernel/Property.h"
35 #include "boost/format.hpp"
36 #include "boost/algorithm/string/predicate.hpp"
48 "The type of handled Incident" ) ;
50 declareProperty (
"DataSvc" , m_dataSvcName ) ;
52 declareProperty (
"UsePreceedingPath" , m_partialPath ) ;
56 "Dump the configuration and stastics" ) ->
62 "Flag to (pre)initialize all algorithms" ) ;
64 (
"AllowPreInitializeFailure" ,
66 "Allow (pre)initialization of algorithms to fail without stopping the application" ) ;
68 declareProperty (
"Algorithms" , m_algMapping ) ->
70 declareProperty (
"Nodes" , m_nodeMapping ) ->
73 declareProperty (
"AlgMap" , m_algMap ) ->
75 declareProperty (
"NodeMap" , m_nodeMap ) ->
78 declareProperty (
"Prefix" , m_prefix ) ;
80 declareProperty(
"NodeMappingTools", m_nodeMapTools,
81 "List of tools of type IDODNodeMapper");
82 declareProperty(
"AlgMappingTools", m_algMapTools,
83 "List of tools of type IDODAlgMapper");
101 <<
"The property 'Nodes' is obsolete, switch to map-like 'NodeMap' "
102 <<
" = { 'data' : 'type' } "
113 <<
"The property 'Algorithms' is obsolete, switch to map-like 'AlgMap' "
114 <<
" = { 'data' : 'algorithm' } "
140 inline std::string no_prefix
141 (
const std::string&
value ,
142 const std::string& prefix )
144 return boost::algorithm::starts_with(value,prefix) ?
145 value.substr( prefix.size() ) : value ;
155 inline size_t add_prefix ( MAP& _map ,
const std::string& prefix )
158 if ( prefix.empty() ) {
return 0 ; }
160 auto it = std::find_if_not( _map.begin(), _map.end(),
161 [&](
typename MAP::const_reference
i) {
162 return boost::algorithm::starts_with(
i.first,prefix);
164 if ( it == _map.end() )
return 0 ;
165 std::string key = prefix + it->first ;
166 std::string value = std::move(it->second);
168 _map[ key ] = std::move(value);
169 return 1 + add_prefix ( _map , prefix ) ;
179 inline size_t get_dir (
const std::string&
object ,
SET& _set )
181 std::string::size_type ifind =
object.rfind(
'/') ;
183 if ( std::string::npos == ifind ) {
return 0 ; }
184 if ( 0 == ifind ) {
return 0 ; }
186 const std::string
top = std::string(
object , 0 , ifind) ;
188 return 1 + get_dir ( top , _set ) ;
196 template <
class MAP,
class SET>
197 inline size_t get_dirs (
const MAP& _map,
SET& _set )
199 size_t size = _set.size() ;
200 for (
const auto&
item : _map )
201 { get_dir (
item.first , _set ) ; }
202 return _set.size() - size ;
208 ClassH cl = TClass::GetClass(type.c_str()) ;
211 <<
"Failed to access dictionary class for "
212 << name <<
" of type:" << type <<
endmsg;
262 std::set<std::string> dirs ;
266 auto _e = dirs.find(
"/Event") ;
267 if ( dirs.end() != _e ) { dirs.erase( _e ) ; }
269 for (
const auto& dir : dirs )
321 <<
"Handled \"" <<
m_trapType <<
"\" incidents: "
329 (
"Algorithm timing: Mean(+-rms)/Min/Max:%3%(+-%4%)/%6%/%7%[ms] " ,
System::milliSec )
334 (
"Nodes timing: Mean(+-rms)/Min/Max:%3%(+-%4%)/%6%/%7%[ms] " ,
System::milliSec )
339 (
"Algs timing: Mean(+-rms)/Min/Max:%3%(+-%4%)/%6%/%7%[ms] " ,
System::milliSec )
344 (
"All timing: Mean(+-rms)/Min/Max:%3%(+-%4%)/%6%/%7%[ms] " ,
System::milliSec )
364 warning() <<
"ToolSvc already finalized: cannot release tools. Check options." <<
endmsg;
386 m_nodeMappers.clear();
388 m_algMappers.clear();
408 if ( !(
m_algMgr = serviceLocator()) )
410 error() <<
"Failed to retrieve the IAlgManager interface." <<
endmsg;
414 if ( !(
m_incSvc = serviceLocator()->service(
"IncidentSvc")) )
416 error() <<
"Failed to retrieve Incident service." <<
endmsg;
424 <<
"Failed to retrieve the data provider interface of "
431 if ( !(
m_toolSvc = serviceLocator()->service(
"ToolSvc")) )
433 error() <<
"Failed to retrieve ToolSvc" <<
endmsg;
458 std::string nam, typ, tag;
463 using Parser = Gaudi::Utils::AttribStringParser;
464 for (
auto attrib: Parser(node)) {
465 switch( ::
toupper(attrib.tag[0]) ) {
467 tag = std::move(attrib.value);
470 nam = std::move(attrib.value);
479 <<
"The obsolete property 'Nodes' redefines the action for '"
480 + tag +
"' to be '" +nam+
"'"
495 std::string typ, tag;
499 using Parser = Gaudi::Utils::AttribStringParser;
500 for (
auto attrib: Parser(alg)) {
501 switch( ::
toupper(attrib.tag[0]) ) {
503 tag = std::move(attrib.value);
506 typ = std::move(attrib.value);
516 <<
"The obsolete property 'Algorithms' redefines the action for '"
517 + tag +
"' to be '" +item.
type() +
"/"+item.
name()+
"'"
540 <<
"Failed to create algorithm "
552 <<
"Failed to initialize algorithm "
565 <<
"Failed to 'run' algorithm "
581 ToolGetter(std::string _path): path(
std::move(_path)) {}
594 inline bool isGood(
const std::string& r) {
return !r.empty();}
601 const ToolGetter getter;
602 const std::vector<IDODNodeMapper*> &nodes;
603 const std::vector<IDODAlgMapper*> &algs;
605 template <
class R,
class C>
606 R find(
const C&
l)
const {
608 auto result = getter(
i);
609 if (isGood(result))
return result;
615 Finder(std::string _path,
616 const std::vector<IDODNodeMapper*> &_nodes,
617 const std::vector<IDODAlgMapper*> &_algs)
618 : getter(
std::move(_path)), nodes(_nodes), algs(_algs) {
621 inline std::string node()
const {
622 return find<std::string>(nodes);
626 return find<Gaudi::Utils::TypeNameString>(algs);
645 if ( ! inc ) { return ; }
652 <<
"Incident: [" << incident.
type () <<
"] "
653 <<
" = " << incident.
source ()
654 <<
" Location:" << inc->tag() <<
endmsg;
678 verbose() <<
"Try to find mapping with mapping tools" <<
endmsg;
681 std::string node = finder.node();
685 verbose() <<
"Found Node handler: " << node <<
endmsg;
696 verbose() <<
"Found Algorithm handler: " << alg <<
endmsg;
717 std::unique_ptr<DataObject> object;
728 <<
"Failed to get dictionary for class '"
730 <<
"' for location:" << tag <<
endmsg;
734 object.reset( reinterpret_cast<DataObject*>(n.
clazz->New()) );
740 <<
"Failed to create an object of type:"
741 << n.
clazz->GetName() <<
" for location:" << tag
751 <<
MSG::ERROR <<
"Failed to register an object of type:"
752 << n.
name <<
" at location:" << tag
777 <<
"Failed to configure handler for: "
790 <<
"Failed to execute the algorithm:"
806 const bool mode )
const
808 if ( m_algs.empty() && m_nodes.empty() ) { return ; }
810 typedef std::pair<std::string,std::string> Pair ;
811 std::map<std::string,Pair> _m ;
812 for (
auto& alg : m_algs )
814 auto check = _m.find(alg.first) ;
815 if ( _m.end() !=
check )
819 <<
" The data item is activated for '"
822 const Leaf& l = alg.second ;
825 if ( !mode && 0 == l.
num ) { continue ; }
828 if ( mode ) { val = ( ! l.
algorithm ) ?
"F" :
"T" ; }
831 _m[ no_prefix ( alg.first , m_prefix ) ] = { nam , val } ;
834 for (
const auto& node : m_nodes )
836 auto check = _m.find(node.first) ;
837 if ( _m.end() !=
check )
841 <<
" The data item is already activated for '"
844 const Node&
n = node.second ;
845 std::string nam =
"'" + n.
name +
"'" ;
849 if ( !mode && 0 == n.
num ) { continue ; }
851 if ( mode ) { val = ( 0 == n.
clazz ) ?
"F" :
"T" ; }
854 _m[ no_prefix ( node.first , m_prefix ) ] = { nam , val } ;
857 if ( _m.empty() ) { return ; }
863 for (
const auto&
i : _m )
865 n1 = std::max ( n1 ,
i.first.size() ) ;
866 n2 = std::max ( n2 ,
i.second.first.size() ) ;
867 n3 = std::max ( n3 ,
i.second.second.size() ) ;
869 if ( 10 > n1 ) { n1 = 10 ; }
870 if ( 10 > n2 ) { n2 = 10 ; }
871 if ( 60 < n1 ) { n1 = 60 ; }
872 if ( 60 < n2 ) { n2 = 60 ; }
875 const std::string _f =
" | %%1$-%1%.%1%s | %%2$-%2%.%2%s | %%3$%3%.%3%s |" ;
879 const std::string _format = _ff.str() ;
883 if ( mode ) { msg <<
"Data-On-Demand Actions enabled for:" ; }
884 else { msg <<
"Data-On-Demand Actions has been used for:" ; }
887 fmt1 %
"Address" %
"Creator" % ( mode ?
"S" :
"#" ) ;
889 const std::string
header = fmt1.str() ;
890 std::string
line = std::string( header.size() ,
'-' ) ;
898 for (
const auto&
item : _m )
902 ( fmt %
item.first %
item.second.first %
item.second.second ) ;
905 msg <<
'\n' << line <<
endmsg ;
void i_setNodeHandler(const std::string &name, const std::string &type)
Internal method to initialize a node handler.
virtual SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
Definition of the MsgStream class used to transmit messages.
StatusCode initialize() override
string to_string(const T &value)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
const std::string & type() const
Access to the incident type.
The DataOnDemandSvc listens to incidents typically triggered by the data service of the configurable ...
StatusCode finalize() override
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
ChronoEntity m_timer_algs
const std::string & source() const
Access to the source of the incident.
std::vector< IDODNodeMapper * > m_nodeMappers
bool isSuccess() const
Test for a status code of SUCCESS.
MsgStream & stream() const
get the message stream
SmartIF< IIncidentSvc > m_incSvc
Incident service.
void handle(const Incident &incident) override
IIncidentListener interfaces overrides: incident handling.
std::unique_ptr< MsgStream > m_log
virtual StatusCode createAlgorithm(const std::string &algtype, const std::string &algname, IAlgorithm *&alg, bool managed=false, bool checkIfExists=true)=0
Create an instance of a algorithm type that has been declared beforehand and assigns to it a name...
std::vector< std::string > m_algMapTools
std::string m_trapType
Trap name.
std::string m_dataSvcName
Data service name.
Setup m_nodeMapping
Mapping to nodes.
A small utility class for chronometry of user codes.
Map m_algMap
the major configuration property { 'data' : 'algorithm' }
StatusCode initialize() override
Inherited Service overrides: Service initialization.
bool isFailure() const
Test for a status code of FAILURE.
The helper class to represent the efficient "key" for access.
virtual StatusCode sysExecute()=0
System execution. This method invokes the execute() method of a concrete algorithm.
SmartIF< IDataProviderSvc > m_dataSvc
Data provider reference.
ClassH clazz
the actual class
Interface of tools used by the DataOnDemandSvc to choose the type of node to be created at a path...
Setup m_algMapping
Mapping to algorithms.
Helper class to parse a string of format "type/name".
StatusCode setup()
Setup routine (called by (re-) initialize.
bool m_allowInitFailure
flag to allow DataOnDemand initialization to succeed even if the (pre)initialization of the algorithm...
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
StatusCode execHandler(const std::string &tag, Leaf &leaf)
Execute leaf handler (algorithm)
auto end(reverse_wrapper< T > &w)
This class is used for returning status codes from appropriate routines.
#define DECLARE_COMPONENT(type)
void update_dump(Property &)
update handler for 'Dump' property
StatusCode setupNodeHandlers()
Initialize node handlers.
iterator find(const key_type &key)
Helper class of the DataOnDemandSvc.
virtual Gaudi::Utils::TypeNameString algorithmForPath(const std::string &path)=0
For the given path, returns a TypeNameString object identifying the algorithm to be run to produce th...
std::vector< std::string > m_nodeMapTools
NodeMap m_nodes
Map of "empty" objects to be placed as intermediate nodes.
StatusCode reinitialize() override
void dump(const MSG::Level level, const bool mode=true) const
dump the content of DataOnDemand service
bool m_partialPath
Flag to allow for the creation of partial leaves.
std::string outputUserTime() const
print the chrono ;
std::vector< IDODAlgMapper * > m_algMappers
Interface of tools used by the DataOnDemandSvc to choose the algorithm to be run to produce the data ...
Property base class allowing Property* collections to be "homogeneous".
SmartIF< IAlgManager > m_algMgr
Algorithm manager.
SmartIF< IToolSvc > m_toolSvc
Data provider reference.
Helper object, useful for measurement of CPU-performance of highly-recursive structures, e.g.
AlgMap m_algs
Map of algorithms to handle incidents.
Base class used to extend a class implementing other interfaces.
bool m_dump
flag to force the printout
bool m_init
flag to warm up the configuration
const std::string & type() const
ChronoEntity m_timer_nodes
Base class for all Incidents (computing events).
virtual void addListener(IIncidentListener *lis, const std::string &type="", long priority=0, bool rethrow=false, bool singleShot=false)=0
Add listener.
bool dataObject
trivial object? DataObject?
DataOnDemandSvc(const std::string &name, ISvcLocator *svc)
Standard initializing service constructor.
StatusCode finalize() override
Inherited Service overrides: Service finalization.
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
virtual bool isInitialized() const =0
check if the algorithm is initialized properly
Map m_nodeMap
the major configuration property { 'data' : 'type' }
virtual void removeListener(IIncidentListener *lis, const std::string &type="")=0
Remove listener.
Data service incident class.
const std::string & name() const
StatusCode setupAlgHandlers()
Initialize leaf handlers.
Helper class of the DataOnDemandSvc.
virtual std::string nodeTypeForPath(const std::string &path)=0
For the given path, returns a the type name of the object to be created at the path.
virtual StatusCode registerObject(const std::string &fullPath, DataObject *pObject)=0
Register object with the data store.
StatusCode i_setAlgHandler(const std::string &name, const Gaudi::Utils::TypeNameString &alg)
Internal method to initialize an algorithm handler.
StatusCode configureHandler(Leaf &leaf)
Configure handler for leaf.
void update_2(Property &p)
void toupper(std::string &s)
void update_3(Property &p)
void update_1(Property &p)
StatusCode reinitialize() override
Inherited Service overrides: Service reinitialization.
StatusCode update()
update the handlers