Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (f31105fd)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
JobOptionsSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "Analyzer.h"
12 #include "Catalog.h"
13 #include "Messages.h"
14 #include "Node.h"
15 #include "PragmaOptions.h"
16 #include "PropertyId.h"
17 #include "PythonConfig.h"
18 #include "Units.h"
20 #include <Gaudi/Parsers/Factory.h>
21 #include <Gaudi/Property.h>
22 #include <GaudiKernel/IProperty.h>
23 #include <GaudiKernel/MsgStream.h>
26 #include <GaudiKernel/Service.h>
27 #include <GaudiKernel/StatusCode.h>
28 #include <GaudiKernel/System.h>
29 #include <algorithm>
30 #include <functional>
31 #include <memory>
32 #include <nlohmann/json.hpp>
33 #include <string>
34 #include <string_view>
35 #include <unordered_map>
36 #include <vector>
37 
38 namespace Gaudi {
39  namespace Parsers {
40  class Catalog;
41  }
42 } // namespace Gaudi
43 
44 class JobOptionsSvc : public extends<Service, Gaudi::Interfaces::IOptionsSvc> {
45 public:
47 
48 private:
51 
53 
56 
57 protected:
59  void set( const std::string& key, const std::string& value ) override { m_options[key] = value; }
60  std::string get( const std::string& key, const std::string& default_ = {} ) const override {
61  auto item = m_options.find( key );
62  return item != m_options.end() ? std::string{ item->second } : default_;
63  }
64  std::string pop( const std::string& key, const std::string& default_ = {} ) override {
65  std::string result = default_;
66 
67  auto item = m_options.find( key );
68  if ( item != m_options.end() ) {
69  result = std::move( item->second );
70  m_options.erase( item );
71  }
72  return result;
73  }
74  bool has( const std::string& key ) const override { return m_options.find( key ) != m_options.end(); }
77  v.reserve( m_options.size() );
78  std::for_each( begin( m_options ), end( m_options ), [&v]( const auto& item ) { v.emplace_back( item ); } );
79  std::sort( begin( v ), end( v ) );
80  return v;
81  }
82  bool isSet( const std::string& key ) const override {
83  auto item = m_options.find( key );
84  return item != m_options.end() && item->second.isSet();
85  }
86 
88 
89  void broadcast( const std::regex& filter, const std::string& value, OnlyDefaults defaults_only ) override;
91 
92 public:
93  // Constructor
94  JobOptionsSvc( const std::string& name, ISvcLocator* svc );
95 
96  StatusCode initialize() override;
97  StatusCode start() override;
98  StatusCode stop() override;
99 
106  StatusCode readOptions( std::string_view file, std::string_view path = "" ) override;
107 
108 private:
109  void fillServiceCatalog( const Gaudi::Parsers::Catalog& catalog );
110 
112  void dump( const std::string& file, const Gaudi::Parsers::Catalog& catalog ) const;
113  void dump( const std::string& file ) const;
114 
115 private:
122 
124  this,
125  "GlobalDefaults",
126  {},
127  "Allow definition of global defaults for properties as list of pairs (regex, value)" };
128 
129  Gaudi::Property<bool> m_reportUnused{ this, "ReportUnused", false, "Print report of properties set, but not used" };
130 
132 };
133 
134 // ============================================================================
136 // ============================================================================
137 // Namespace aliases:
138 // ============================================================================
139 namespace gp = Gaudi::Parsers;
140 // ============================================================================
142  if ( System::isEnvSet( "JOBOPTSEARCHPATH" ) ) m_dir_search_path = System::getEnv( "JOBOPTSEARCHPATH" );
143  if ( System::isEnvSet( "JOBOPTSDUMPFILE" ) ) m_dump = System::getEnv( "JOBOPTSDUMPFILE" );
144 
148  for ( const auto& p : m_globalDefaultsProp ) { m_globalDefaults.emplace_back( p.first, p.second ); }
149  } );
150 }
151 // ============================================================================
153  // Call base class initializer
155  // Read the job options if needed
156  if ( sc ) {
157  if ( m_source_type == "NONE" ) {
158  return sc;
159  } else if ( m_source_type == "PYTHON" ) {
160  PythonConfig conf( this );
161  return conf.evaluateConfig( m_source_path, m_pythonParams, m_pythonAction );
162  } else {
164  }
165  }
166  return sc;
167 }
169  if ( m_reportUnused ) {
171  unused.reserve( m_options.size() );
172 
173  for ( const auto& p : m_options ) {
174  if ( !p.second.isBound() ) unused.emplace_back( p.first );
175  }
176 
177  if ( !unused.empty() ) {
178  std::sort( unused.begin(), unused.end() );
179  auto& log = warning();
180  log << unused.size() << " unused properties:";
181  for ( const auto& k : unused ) log << "\n - " << k;
182  log << endmsg;
183  }
184  }
185  return Service::stop();
186 }
187 
188 // ============================================================================
190  if ( !m_dump.empty() ) { dump( m_dump ); }
191  return StatusCode::SUCCESS;
192 }
193 
194 void JobOptionsSvc::dump( const std::string& file, const gp::Catalog& catalog ) const {
195  std::ofstream out( file, std::ios_base::out | std::ios_base::trunc );
196  if ( !out ) {
197  error() << "Unable to open dump-file \"" + file + "\"" << endmsg;
198  return; // RETURN
199  }
200  info() << "Properties are dumped into \"" + file + "\"" << endmsg;
201  // perform the actual dump:
202  out << catalog;
203 }
204 
205 void JobOptionsSvc::dump( const std::string& file ) const {
206  std::ofstream out( file, std::ios_base::out | std::ios_base::trunc );
207  if ( !out ) {
208  error() << "Unable to open dump-file \"" + file + "\"" << endmsg;
209  } else {
210  info() << "Properties are dumped into \"" + file + "\"" << endmsg;
211  for ( const auto& [key, value] : items() ) {
212  out << key << " = " << value << ';';
213  if ( !m_options.find( key )->second.isBound() ) out << " // unused";
214  out << '\n';
215  }
216  }
217 }
218 
220  for ( const auto& client : catalog ) {
221  for ( const auto& current : client.second ) {
222  set( client.first + '.' + current.NameInClient(), current.ValueAsString() );
223  }
224  }
225 }
226 
227 StatusCode JobOptionsSvc::readOptions( std::string_view file, std::string_view path ) {
228  std::string search_path = std::string{ path };
229  if ( search_path.empty() && !m_dir_search_path.empty() ) { search_path = m_dir_search_path; }
230 
231  if ( msgLevel( MSG::DEBUG ) )
232  debug() << "Reading options from the file "
233  << "'" << file << "'" << endmsg;
234 
235  if ( file.size() >= 5 && file.substr( file.size() - 5 ) == ".json" ) {
238  input >> opts;
239  for ( auto item = opts.begin(); item != opts.end(); ++item ) { set( item.key(), item.value().get<std::string>() ); }
240  return StatusCode::SUCCESS;
241  }
242 
243  gp::Messages messages( msgStream() );
244  gp::Catalog catalog;
245  gp::Units units;
246  gp::PragmaOptions pragma;
247  gp::Node ast;
248  StatusCode sc = gp::ReadOptions( file, path, &messages, &catalog, &units, &pragma, &ast ) ? StatusCode::SUCCESS
250 
251  // --------------------------------------------------------------------------
252  if ( sc.isSuccess() ) {
253  if ( pragma.IsPrintOptions() ) { info() << "Print options" << std::endl << catalog << endmsg; }
254  if ( pragma.IsPrintTree() ) { info() << "Print tree:" << std::endl << ast.ToString() << endmsg; }
255  if ( pragma.HasDumpFile() ) dump( pragma.dumpFile(), catalog );
256  info() << "Job options successfully read in from " << file << endmsg;
257  fillServiceCatalog( catalog );
258  } else {
259  fatal() << "Job options errors." << endmsg;
260  }
261  // ----------------------------------------------------------------------------
262  return sc;
263 }
264 
266  const std::string key = prefix + '.' + property->name();
267 
268  std::tuple<bool, std::string_view> defaultValue{ false, "" };
269  if ( !has( key ) && !m_globalDefaults.empty() ) { // look for a global default only if it was not set
270  std::smatch match;
271  for ( const auto& p : m_globalDefaults ) {
272  if ( regex_match( key, match, p.first ) ) { defaultValue = { true, p.second }; }
273  }
274  }
275 
276  m_options[key] = *property;
277 
278  // at this point the property is bound, so we can set the default if needed
279  if ( std::get<0>( defaultValue ) ) set( key, std::string{ std::get<1>( defaultValue ) } );
280 }
281 
282 void JobOptionsSvc::broadcast( const std::regex& filter, const std::string& value, OnlyDefaults defaults_only ) {
283  std::smatch match;
284  for ( auto& p : m_options ) {
285  if ( !defaults_only || !p.second.isSet() ) {
286  const auto s = p.first.str();
287  if ( regex_match( s, match, filter ) ) { p.second = value; }
288  }
289  }
290 }
JobOptionsSvc::get
std::string get(const std::string &key, const std::string &default_={}) const override
Definition: JobOptionsSvc.cpp:60
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:35
std::for_each
T for_each(T... args)
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
std::string
STL class.
Gaudi.Configuration.log
log
Definition: Configuration.py:28
JobOptionsSvc::m_reportUnused
Gaudi::Property< bool > m_reportUnused
Definition: JobOptionsSvc.cpp:129
std::move
T move(T... args)
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:315
Gaudi::Parsers::Units
Definition: Units.h:28
System.h
std::vector::reserve
T reserve(T... args)
Gaudi::Details::WeakPropertyRef::isBound
bool isBound() const
Definition: PropertyBase.h:208
gaudirun.s
string s
Definition: gaudirun.py:346
JobOptionsSvc::m_old_iface_compat_2
std::map< std::string, PropertiesT > m_old_iface_compat_2
Definition: JobOptionsSvc.cpp:55
System::getEnv
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:390
std::vector
STL class.
std::unordered_map::find
T find(T... args)
std::unordered_map::size
T size(T... args)
ISvcLocator
Definition: ISvcLocator.h:46
jsonFromLHCbLog.json
json
Definition: jsonFromLHCbLog.py:86
JobOptionsSvc::m_pythonAction
Gaudi::Property< std::string > m_pythonAction
Definition: JobOptionsSvc.cpp:120
JobOptionsSvc::m_source_path
Gaudi::Property< std::string > m_source_path
Definition: JobOptionsSvc.cpp:117
JobOptionsSvc::set
void set(const std::string &key, const std::string &value) override
Definition: JobOptionsSvc.cpp:59
JobOptionsSvc::bind
void bind(const std::string &prefix, Gaudi::Details::PropertyBase *property) override
Definition: JobOptionsSvc.cpp:265
std::tuple
gaudirun.prefix
string prefix
Definition: gaudirun.py:361
Gaudi::Parsers::Node
Definition: Node.h:34
JobOptionsSvc::m_options
StorageType m_options
Definition: JobOptionsSvc.cpp:52
JobOptionsSvc::m_pythonParams
Gaudi::Property< std::string > m_pythonParams
Definition: JobOptionsSvc.cpp:121
StatusCode.h
std::sort
T sort(T... args)
CommonMessaging< implements< IService, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
std::vector::clear
T clear(T... args)
PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >::property
Gaudi::Details::PropertyBase * property(std::string_view name) const
\fixme property and bindPropertiesTo should be protected
Definition: PropertyHolder.h:238
Gaudi::Details::WeakPropertyRef::isSet
bool isSet() const
Definition: PropertyBase.h:209
JobOptionsSvc::broadcast
void broadcast(const std::regex &filter, const std::string &value, OnlyDefaults defaults_only) override
Definition: JobOptionsSvc.cpp:282
PropertyId.h
JobOptionsSvc::start
StatusCode start() override
Definition: JobOptionsSvc.cpp:189
Gaudi::Parsers::PragmaOptions
Definition: PragmaOptions.h:34
JobOptionsSvc::m_old_iface_compat
std::map< std::string, std::unique_ptr< Gaudi::Details::PropertyBase > > m_old_iface_compat
Definition: JobOptionsSvc.cpp:54
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
Gaudi::Parsers::Node::ToString
std::string ToString(int indent=0) const
Definition: Node.cpp:63
gaudirun.opts
opts
Definition: gaudirun.py:336
JobOptionsSvc::m_dump
Gaudi::Property< std::string > m_dump
Definition: JobOptionsSvc.cpp:119
JobOptionsSvc::stop
StatusCode stop() override
Definition: JobOptionsSvc.cpp:168
std::ofstream
STL class.
JobOptionsSvc::m_globalDefaultsProp
Gaudi::Property< std::vector< std::pair< std::string, std::string > > > m_globalDefaultsProp
Definition: JobOptionsSvc.cpp:123
CommonMessaging
Definition: CommonMessaging.h:66
Gaudi::Parsers::Messages
Definition: Messages.h:30
Gaudi::Property::declareUpdateHandler
Details::PropertyBase & declareUpdateHandler(std::function< void(Details::PropertyBase &)> fun) override
set new callback for update
Definition: Property.h:140
std::unordered_map::erase
T erase(T... args)
JobOptionsSvc::readOptions
StatusCode readOptions(std::string_view file, std::string_view path="") override
look for file 'file' into search path 'path' and read it to update existing JobOptionsCatalogue
Definition: JobOptionsSvc.cpp:227
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
System::isEnvSet
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:410
std::map
STL class.
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
std::regex
Units.h
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Parsers::ReadOptions
bool ReadOptions(std::string_view filename, std::string_view search_path, Messages *messages, Catalog *catalog, Units *units, PragmaOptions *pragma, Node *root)
Parse and analyze filename, save all messages and properties.
Definition: Analyzer.cpp:387
JobOptionsSvc::dump
void dump(const std::string &file, const Gaudi::Parsers::Catalog &catalog) const
dump properties catalog to file
Definition: JobOptionsSvc.cpp:194
Messages.h
JobOptionsSvc::m_dir_search_path
Gaudi::Property< std::string > m_dir_search_path
Definition: JobOptionsSvc.cpp:118
PythonConfig.h
Service.h
Factory.h
JobOptionsSvc::PropertiesT
std::vector< const Gaudi::Details::PropertyBase * > PropertiesT
Definition: JobOptionsSvc.cpp:46
std::vector::emplace_back
T emplace_back(T... args)
Node.h
Gaudi::Details::PropertyId
Helper to record a property identifier as a sequence of SharedString instances.
Definition: PropertyId.h:65
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
std::endl
T endl(T... args)
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
JobOptionsSvc::initialize
StatusCode initialize() override
Definition: JobOptionsSvc.cpp:152
JobOptionsSvc::m_source_type
Gaudi::Property< std::string > m_source_type
Definition: JobOptionsSvc.cpp:116
Gaudi::Parsers::PragmaOptions::IsPrintOptions
bool IsPrintOptions()
Definition: PragmaOptions.h:50
System::PathResolver::find_file
static std::string find_file(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
Definition: PathResolver.cpp:118
JobOptionsSvc
Definition: JobOptionsSvc.cpp:44
std::vector::begin
T begin(T... args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
JobOptionsSvc::isSet
bool isSet(const std::string &key) const override
Definition: JobOptionsSvc.cpp:82
std::vector::empty
T empty(T... args)
Properties.v
v
Definition: Properties.py:122
Gaudi::Parsers::PragmaOptions::HasDumpFile
bool HasDumpFile()
Definition: PragmaOptions.h:52
Gaudi::Parsers
Definition: DODBasicMapper.cpp:17
IProperty.h
std::smatch
std::unordered_map::end
T end(T... args)
IOTest.end
end
Definition: IOTest.py:125
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
JobOptionsSvc::pop
std::string pop(const std::string &key, const std::string &default_={}) override
Definition: JobOptionsSvc.cpp:64
PathResolver.h
Catalog.h
JobOptionsSvc::fillServiceCatalog
void fillServiceCatalog(const Gaudi::Parsers::Catalog &catalog)
Definition: JobOptionsSvc.cpp:219
PropertyHolder.h
ProduceConsume.key
key
Definition: ProduceConsume.py:84
JobOptionsSvc::items
std::vector< std::tuple< std::string, std::string > > items() const override
Definition: JobOptionsSvc.cpp:75
std::unordered_map< PropertyId, Gaudi::Details::WeakPropertyRef >
Gaudi::Parsers::PragmaOptions::IsPrintTree
bool IsPrintTree()
Definition: PragmaOptions.h:51
IOptionsSvc.h
Gaudi::Parsers::PragmaOptions::dumpFile
const std::string & dumpFile() const
Definition: PragmaOptions.h:47
JobOptionsSvc::has
bool has(const std::string &key) const override
Definition: JobOptionsSvc.cpp:74
conf
Definition: conf.py:1
JobOptionsSvc::m_globalDefaults
std::vector< std::pair< std::regex, std::string > > m_globalDefaults
Definition: JobOptionsSvc.cpp:131
Gaudi::Property< std::string >
JobOptionsSvc::JobOptionsSvc
JobOptionsSvc(const std::string &name, ISvcLocator *svc)
Definition: JobOptionsSvc.cpp:141
Gaudi::Parsers::Catalog
Definition: Catalog.h:38
Property.h
MsgStream.h
PythonConfig
Definition: PythonConfig.h:33
Gaudi::Functional::details::out
OptOut && out
Definition: details.h:174
Analyzer.h
std::ifstream
STL class.
PragmaOptions.h