| The Gaudi Framework v32r1 (f65d50dc)

JobOptionsSvc.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Local:
3 // ============================================================================
4 #include "JobOptionsSvc.h"
5 #include "Analyzer.h"
6 #include "Catalog.h"
7 #include "Messages.h"
8 #include "Node.h"
9 #include "PragmaOptions.h"
10 #include "PythonConfig.h"
11 #include "Units.h"
12 // ============================================================================
13 // Gaudi:
14 // ============================================================================
15 #include "GaudiKernel/MsgStream.h"
16 #include "GaudiKernel/System.h"
17 // ============================================================================
18 DECLARE_COMPONENT( JobOptionsSvc )
19 // ============================================================================
20 // Namespace aliases:
21 // ============================================================================
22 namespace gp = Gaudi::Parsers;
23 // ============================================================================
24 JobOptionsSvc::JobOptionsSvc( const std::string& name, ISvcLocator* svc ) : base_class( name, svc ) {
25 if ( System::isEnvSet( "JOBOPTSEARCHPATH" ) ) m_dir_search_path = System::getEnv( "JOBOPTSEARCHPATH" );
26 if ( System::isEnvSet( "JOBOPTSDUMPFILE" ) ) m_dump = System::getEnv( "JOBOPTSDUMPFILE" );
27 }
28 // ============================================================================
29 StatusCode JobOptionsSvc::initialize() {
30 // Call base class initializer
31 StatusCode sc = Service::initialize();
32 // Read the job options if needed
33 if ( sc ) {
34 if ( m_source_type == "NONE" ) {
35 return sc;
36 } else if ( m_source_type == "PYTHON" ) {
37 PythonConfig conf( this );
38 return conf.evaluateConfig( m_source_path, m_pythonParams, m_pythonAction );
39 } else {
40 return readOptions( m_source_path, m_dir_search_path );
41 }
42 }
43 return sc;
44 }
45
46 // ============================================================================
47 StatusCode JobOptionsSvc::addPropertyToCatalogue( const std::string& client,
48 const Gaudi::Details::PropertyBase& property ) {
49 auto p = std::make_unique<Gaudi::Property<std::string>>( property.name(), "" );
50 return property.load( *p ) ? m_svc_catalog.addProperty( client, p.release() ) : StatusCode::FAILURE;
51 }
52 // ============================================================================
53 StatusCode JobOptionsSvc::removePropertyFromCatalogue( const std::string& client, const std::string& name ) {
54 return m_svc_catalog.removeProperty( client, name );
55 }
56 // ============================================================================
57 const JobOptionsSvc::PropertiesT* JobOptionsSvc::getProperties( const std::string& client ) const {
58 return m_svc_catalog.getProperties( client );
59 }
60 // ============================================================================
61 StatusCode JobOptionsSvc::setMyProperties( const std::string& client, IProperty* myInt ) {
62 const auto* props = m_svc_catalog.getProperties( client );
63 if ( !props ) { return StatusCode::SUCCESS; }
64
65 bool fail = false;
66 for ( const auto& cur : *props ) {
67 StatusCode sc = myInt->setProperty( *cur );
68 if ( sc.isFailure() ) {
69 error() << "Unable to set the property '" << cur->name() << "'"
70 << " of '" << client << "'. "
71 << "Check option and algorithm names, type and bounds." << endmsg;
72 fail = true;
73 }
74 }
75 return fail ? StatusCode::FAILURE : StatusCode::SUCCESS;
76 }
77
79 std::vector<std::string> JobOptionsSvc::getClients() const { return m_svc_catalog.getClients(); }
80
81 void JobOptionsSvc::dump( const std::string& file, const gp::Catalog& catalog ) const {
82 std::ofstream out( file, std::ios_base::out | std::ios_base::trunc );
83 if ( !out ) {
84 error() << "Unable to open dump-file \"" + file + "\"" << endmsg;
85 return; // RETURN
86 }
87 info() << "Properties are dumped into \"" + file + "\"" << endmsg;
88 // perform the actual dump:
89 out << catalog;
90 }
91
92 void JobOptionsSvc::fillServiceCatalog( const gp::Catalog& catalog ) {
93 for ( const auto& client : catalog ) {
94 for ( const auto& current : client.second ) {
95 addPropertyToCatalogue( client.first,
96 Gaudi::Property<std::string>{current.NameInClient(), current.ValueAsString()} );
97 }
98 }
99 }
100
101 StatusCode JobOptionsSvc::readOptions( const std::string& file, const std::string& path ) {
102 std::string search_path = path;
103 if ( search_path.empty() && !m_dir_search_path.empty() ) { search_path = m_dir_search_path; }
104 //
105 if ( msgLevel( MSG::DEBUG ) )
106 debug() << "Reading options from the file "
107 << "'" << file << "'" << endmsg;
108 gp::Messages messages( msgStream() );
109 gp::Catalog catalog;
110 gp::Units units;
111 gp::PragmaOptions pragma;
112 gp::Node ast;
113 StatusCode sc = gp::ReadOptions( file, path, &messages, &catalog, &units, &pragma, &ast ) ? StatusCode::SUCCESS
114 : StatusCode::FAILURE;
115
116 // --------------------------------------------------------------------------
117 if ( sc.isSuccess() ) {
118 if ( pragma.IsPrintOptions() ) { info() << "Print options" << std::endl << catalog << endmsg; }
119 if ( pragma.IsPrintTree() ) { info() << "Print tree:" << std::endl << ast.ToString() << endmsg; }
120 if ( pragma.HasDumpFile() ) dump( pragma.dumpFile(), catalog );
121 info() << "Job options successfully read in from " << file << endmsg;
122 fillServiceCatalog( catalog );
123 } else {
124 fatal() << "Job options errors." << endmsg;
125 }
126 // ----------------------------------------------------------------------------
127 return sc;
128 }
PythonConfig.h
System::getEnv
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:369
Analyzer.h
PragmaOptions.h
System.h
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:60
JobOptionsSvc::m_source_path
Gaudi::Property< std::string > m_source_path
Definition: JobOptionsSvc.h:69
std::string::empty
T empty(T... args)
JobOptionsSvc::fillServiceCatalog
void fillServiceCatalog(const Gaudi::Parsers::Catalog &catalog)
Definition: JobOptionsSvc.cpp:92
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
JobOptionsSvc::getClients
std::vector< std::string > getClients() const override
Get the list of clients.
Definition: JobOptionsSvc.cpp:79
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:932
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
SvcCatalog::getProperties
const PropertiesT * getProperties(const std::string &client) const
Definition: SvcCatalog.cpp:59
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:352
MsgStream.h
JobOptionsSvc::m_pythonAction
Gaudi::Property< std::string > m_pythonAction
Definition: JobOptionsSvc.h:72
Messages.h
JobOptionsSvc::m_svc_catalog
SvcCatalog m_svc_catalog
Definition: JobOptionsSvc.h:75
MSG::DEBUG
Definition: IMessageSvc.h:15
std::endl
T endl(T... args)
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
SvcCatalog::addProperty
StatusCode addProperty(const std::string &client, const Gaudi::Details::PropertyBase *property)
Definition: SvcCatalog.cpp:36
JobOptionsSvc.h
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:104
Gaudi::Parsers::ReadOptions
bool ReadOptions(const std::string &filename, const std::string &search_path, Messages *messages, Catalog *catalog, Units *units, PragmaOptions *pragma, Node *root)
Parse and analyze filename, save all messages and properties.
Definition: Analyzer.cpp:377
PythonConfig::evaluateConfig
StatusCode evaluateConfig(const std::string &filename, const std::string &preAction, const std::string &postAction)
Definition: PythonConfig.cpp:8
SvcCatalog::removeProperty
StatusCode removeProperty(const std::string &client, const std::string &name)
Definition: SvcCatalog.cpp:47
JobOptionsSvc::m_dump
Gaudi::Property< std::string > m_dump
Definition: JobOptionsSvc.h:71
CommonMessaging< implements< IService, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:138
Catalog.h
std::string
STL class.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Service::name
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
PythonConfig
Definition: PythonConfig.h:23
std::ofstream
STL class.
IProperty::setProperty
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
JobOptionsSvc::setMyProperties
StatusCode setMyProperties(const std::string &client, IProperty *me) override
Override default properties of the calling client.
Definition: JobOptionsSvc.cpp:61
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:98
JobOptionsSvc
Definition: JobOptionsSvc.h:21
JobOptionsSvc::m_pythonParams
Gaudi::Property< std::string > m_pythonParams
Definition: JobOptionsSvc.h:73
JobOptionsSvc::m_dir_search_path
Gaudi::Property< std::string > m_dir_search_path
Definition: JobOptionsSvc.h:70
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Node.h
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:107
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
CommonMessagingBase::msgStream
MsgStream & msgStream() const
Return an uninitialized MsgStream.
Definition: CommonMessaging.h:71
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:267
SvcCatalog::getClients
std::vector< std::string > getClients() const
Definition: SvcCatalog.cpp:63
System::isEnvSet
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:389
std::vector
STL class.
Units.h
JobOptionsSvc::readOptions
StatusCode readOptions(const std::string &file, const std::string &path="") override
look for file 'file' into search path 'path' and read it to update existing JobOptionsCatalogue
Definition: JobOptionsSvc.cpp:101
Gaudi::Parsers
Definition: DODBasicMapper.cpp:7
JobOptionsSvc::removePropertyFromCatalogue
StatusCode removePropertyFromCatalogue(const std::string &client, const std::string &name) override
Remove a property from the JobOptions catalog.
Definition: JobOptionsSvc.cpp:53
JobOptionsSvc::initialize
StatusCode initialize() override
Definition: JobOptionsSvc.cpp:29
CommonMessaging
Definition: CommonMessaging.h:56
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
JobOptionsSvc::m_source_type
Gaudi::Property< std::string > m_source_type
dump the content of Properties catalog to the predefined file
Definition: JobOptionsSvc.h:68
JobOptionsSvc::dump
void dump(const std::string &file, const Gaudi::Parsers::Catalog &catalog) const
Definition: JobOptionsSvc.cpp:81
JobOptionsSvc::JobOptionsSvc
JobOptionsSvc(const std::string &name, ISvcLocator *svc)
Definition: JobOptionsSvc.cpp:24
PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >::getProperties
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
Definition: PropertyHolder.h:251
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
JobOptionsSvc::addPropertyToCatalogue
StatusCode addPropertyToCatalogue(const std::string &client, const Gaudi::Details::PropertyBase &property) override
Add a property into the JobOptions catalog.
Definition: JobOptionsSvc.cpp:47
IProperty
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:92
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192