The Gaudi Framework  v36r7 (7f57a304)
JobOptionsSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2022 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>
25 #include <GaudiKernel/Service.h>
26 #include <GaudiKernel/StatusCode.h>
27 #include <GaudiKernel/System.h>
28 #include <algorithm>
29 #include <functional>
30 #include <memory>
31 #include <string>
32 #include <unordered_map>
33 #include <vector>
34 
35 #if __cplusplus >= 201703
36 # include <string_view>
37 #else
38 # include <experimental/string_view>
39 namespace std {
40  using experimental::string_view;
41 }
42 #endif
43 
44 namespace Gaudi {
45  namespace Parsers {
46  class Catalog;
47  }
48 } // namespace Gaudi
49 
50 class JobOptionsSvc : public extends<Service, Gaudi::Interfaces::IOptionsSvc> {
51 public:
53 
54 private:
57 
59 
62 
63 protected:
65  void set( const std::string& key, const std::string& value ) override { m_options[key] = value; }
66  std::string get( const std::string& key, const std::string& default_ = {} ) const override {
67  auto item = m_options.find( key );
68  return item != m_options.end() ? std::string{ item->second } : default_;
69  }
70  std::string pop( const std::string& key, const std::string& default_ = {} ) override {
71  std::string result = default_;
72 
73  auto item = m_options.find( key );
74  if ( item != m_options.end() ) {
75  result = std::move( item->second );
76  m_options.erase( item );
77  }
78  return result;
79  }
80  bool has( const std::string& key ) const override { return m_options.find( key ) != m_options.end(); }
83  v.reserve( m_options.size() );
84  std::for_each( begin( m_options ), end( m_options ), [&v]( const auto& item ) { v.emplace_back( item ); } );
85  std::sort( begin( v ), end( v ) );
86  return v;
87  }
88  bool isSet( const std::string& key ) const override {
89  auto item = m_options.find( key );
90  return item != m_options.end() && item->second.isSet();
91  }
92 
94 
95  void broadcast( const std::regex& filter, const std::string& value, OnlyDefaults defaults_only ) override;
97 
98 public:
99  // Constructor
100  JobOptionsSvc( const std::string& name, ISvcLocator* svc );
101 
102  StatusCode initialize() override;
103  StatusCode start() override;
104  StatusCode stop() override;
105 
112  StatusCode readOptions( std::string_view file, std::string_view path = "" ) override;
113 
114 private:
115  void fillServiceCatalog( const Gaudi::Parsers::Catalog& catalog );
116 
118  void dump( const std::string& file, const Gaudi::Parsers::Catalog& catalog ) const;
119  void dump( const std::string& file ) const;
120 
121 private:
128 
130  this,
131  "GlobalDefaults",
132  {},
133  "Allow definition of global defaults for properties as list of pairs (regex, value)" };
134 
135  Gaudi::Property<bool> m_reportUnused{ this, "ReportUnused", false, "Print report of properties set, but not used" };
136 
138 };
139 
140 // ============================================================================
142 // ============================================================================
143 // Namespace aliases:
144 // ============================================================================
145 namespace gp = Gaudi::Parsers;
146 // ============================================================================
148  if ( System::isEnvSet( "JOBOPTSEARCHPATH" ) ) m_dir_search_path = System::getEnv( "JOBOPTSEARCHPATH" );
149  if ( System::isEnvSet( "JOBOPTSDUMPFILE" ) ) m_dump = System::getEnv( "JOBOPTSDUMPFILE" );
150 
154  for ( const auto& p : m_globalDefaultsProp ) { m_globalDefaults.emplace_back( p.first, p.second ); }
155  } );
156 }
157 // ============================================================================
159  // Call base class initializer
161  // Read the job options if needed
162  if ( sc ) {
163  if ( m_source_type == "NONE" ) {
164  return sc;
165  } else if ( m_source_type == "PYTHON" ) {
166  PythonConfig conf( this );
167  return conf.evaluateConfig( m_source_path, m_pythonParams, m_pythonAction );
168  } else {
170  }
171  }
172  return sc;
173 }
175  if ( m_reportUnused ) {
177  unused.reserve( m_options.size() );
178 
179  for ( const auto& p : m_options ) {
180  if ( !p.second.isBound() ) unused.emplace_back( p.first );
181  }
182 
183  if ( !unused.empty() ) {
184  std::sort( unused.begin(), unused.end() );
185  auto& log = warning();
186  log << unused.size() << " unused properties:";
187  for ( const auto& k : unused ) log << "\n - " << k;
188  log << endmsg;
189  }
190  }
191  return Service::stop();
192 }
193 
194 // ============================================================================
196  if ( !m_dump.empty() ) { dump( m_dump ); }
197  return StatusCode::SUCCESS;
198 }
199 
200 void JobOptionsSvc::dump( const std::string& file, const gp::Catalog& catalog ) const {
201  std::ofstream out( file, std::ios_base::out | std::ios_base::trunc );
202  if ( !out ) {
203  error() << "Unable to open dump-file \"" + file + "\"" << endmsg;
204  return; // RETURN
205  }
206  info() << "Properties are dumped into \"" + file + "\"" << endmsg;
207  // perform the actual dump:
208  out << catalog;
209 }
210 
211 void JobOptionsSvc::dump( const std::string& file ) const {
212  std::ofstream out( file, std::ios_base::out | std::ios_base::trunc );
213  if ( !out ) {
214  error() << "Unable to open dump-file \"" + file + "\"" << endmsg;
215  } else {
216  info() << "Properties are dumped into \"" + file + "\"" << endmsg;
217  for ( const auto& [key, value] : items() ) {
218  out << key << " = " << value << ';';
219  if ( !m_options.find( key )->second.isBound() ) out << " // unused";
220  out << '\n';
221  }
222  }
223 }
224 
226  for ( const auto& client : catalog ) {
227  for ( const auto& current : client.second ) {
228  set( client.first + '.' + current.NameInClient(), current.ValueAsString() );
229  }
230  }
231 }
232 
233 StatusCode JobOptionsSvc::readOptions( std::string_view file, std::string_view path ) {
234  std::string search_path = std::string{ path };
235  if ( search_path.empty() && !m_dir_search_path.empty() ) { search_path = m_dir_search_path; }
236  //
237  if ( msgLevel( MSG::DEBUG ) )
238  debug() << "Reading options from the file "
239  << "'" << file << "'" << endmsg;
240  gp::Messages messages( msgStream() );
241  gp::Catalog catalog;
242  gp::Units units;
243  gp::PragmaOptions pragma;
244  gp::Node ast;
245  StatusCode sc = gp::ReadOptions( file, path, &messages, &catalog, &units, &pragma, &ast ) ? StatusCode::SUCCESS
247 
248  // --------------------------------------------------------------------------
249  if ( sc.isSuccess() ) {
250  if ( pragma.IsPrintOptions() ) { info() << "Print options" << std::endl << catalog << endmsg; }
251  if ( pragma.IsPrintTree() ) { info() << "Print tree:" << std::endl << ast.ToString() << endmsg; }
252  if ( pragma.HasDumpFile() ) dump( pragma.dumpFile(), catalog );
253  info() << "Job options successfully read in from " << file << endmsg;
254  fillServiceCatalog( catalog );
255  } else {
256  fatal() << "Job options errors." << endmsg;
257  }
258  // ----------------------------------------------------------------------------
259  return sc;
260 }
261 
263  const std::string key = prefix + '.' + property->name();
264 
265  std::tuple<bool, std::string_view> defaultValue{ false, "" };
266  if ( !has( key ) && !m_globalDefaults.empty() ) { // look for a global default only if it was not set
267  std::smatch match;
268  for ( const auto& p : m_globalDefaults ) {
269  if ( regex_match( key, match, p.first ) ) { defaultValue = { true, p.second }; }
270  }
271  }
272 
273  m_options[key] = *property;
274 
275  // at this point the property is bound, so we can set the default if needed
276  if ( std::get<0>( defaultValue ) ) set( key, std::string{ std::get<1>( defaultValue ) } );
277 }
278 
279 void JobOptionsSvc::broadcast( const std::regex& filter, const std::string& value, OnlyDefaults defaults_only ) {
280  std::smatch match;
281  for ( auto& p : m_options ) {
282  if ( !defaults_only || !p.second.isSet() ) {
283  const auto s = p.first.str();
284  if ( regex_match( s, match, filter ) ) { p.second = value; }
285  }
286  }
287 }
JobOptionsSvc::get
std::string get(const std::string &key, const std::string &default_={}) const override
Definition: JobOptionsSvc.cpp:66
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:135
std::move
T move(T... args)
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
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:61
System::getEnv
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:379
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
JobOptionsSvc::m_pythonAction
Gaudi::Property< std::string > m_pythonAction
Definition: JobOptionsSvc.cpp:126
JobOptionsSvc::m_source_path
Gaudi::Property< std::string > m_source_path
Definition: JobOptionsSvc.cpp:123
JobOptionsSvc::set
void set(const std::string &key, const std::string &value) override
Definition: JobOptionsSvc.cpp:65
JobOptionsSvc::bind
void bind(const std::string &prefix, Gaudi::Details::PropertyBase *property) override
Definition: JobOptionsSvc.cpp:262
std::tuple
gaudirun.prefix
string prefix
Definition: gaudirun.py:361
Gaudi::Parsers::Node
Definition: Node.h:35
JobOptionsSvc::m_options
StorageType m_options
Definition: JobOptionsSvc.cpp:58
JobOptionsSvc::m_pythonParams
Gaudi::Property< std::string > m_pythonParams
Definition: JobOptionsSvc.cpp:127
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:279
PropertyId.h
JobOptionsSvc::start
StatusCode start() override
Definition: JobOptionsSvc.cpp:195
Gaudi::Parsers::PragmaOptions
Definition: PragmaOptions.h:33
JobOptionsSvc::m_old_iface_compat
std::map< std::string, std::unique_ptr< Gaudi::Details::PropertyBase > > m_old_iface_compat
Definition: JobOptionsSvc.cpp:60
TimingHistograms.name
name
Definition: TimingHistograms.py:25
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
JobOptionsSvc::m_dump
Gaudi::Property< std::string > m_dump
Definition: JobOptionsSvc.cpp:125
JobOptionsSvc::stop
StatusCode stop() override
Definition: JobOptionsSvc.cpp:174
std::ofstream
STL class.
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
JobOptionsSvc::m_globalDefaultsProp
Gaudi::Property< std::vector< std::pair< std::string, std::string > > > m_globalDefaultsProp
Definition: JobOptionsSvc.cpp:129
CommonMessaging
Definition: CommonMessaging.h:66
Gaudi::Parsers::Messages
Definition: Messages.h:31
Gaudi::Property::declareUpdateHandler
Details::PropertyBase & declareUpdateHandler(std::function< void(Details::PropertyBase &)> fun) override
set new callback for update
Definition: Property.h:146
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:961
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:233
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
System::isEnvSet
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:399
std::map
STL class.
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
std::regex
Units.h
IOTest.end
def end
Definition: IOTest.py:128
Gaudi
Header file for std:chrono::duration-based Counters.
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:379
JobOptionsSvc::dump
void dump(const std::string &file, const Gaudi::Parsers::Catalog &catalog) const
dump properties catalog to file
Definition: JobOptionsSvc.cpp:200
Messages.h
JobOptionsSvc::m_dir_search_path
Gaudi::Property< std::string > m_dir_search_path
Definition: JobOptionsSvc.cpp:124
PythonConfig.h
Service.h
HistoDumpEx.v
v
Definition: HistoDumpEx.py:27
Factory.h
JobOptionsSvc::PropertiesT
std::vector< const Gaudi::Details::PropertyBase * > PropertiesT
Definition: JobOptionsSvc.cpp:52
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:72
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
JobOptionsSvc::initialize
StatusCode initialize() override
Definition: JobOptionsSvc.cpp:158
JobOptionsSvc::m_source_type
Gaudi::Property< std::string > m_source_type
Definition: JobOptionsSvc.cpp:122
Gaudi::Parsers::PragmaOptions::IsPrintOptions
bool IsPrintOptions()
Definition: PragmaOptions.h:49
JobOptionsSvc
Definition: JobOptionsSvc.cpp:50
std::vector::begin
T begin(T... args)
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
JobOptionsSvc::isSet
bool isSet(const std::string &key) const override
Definition: JobOptionsSvc.cpp:88
std::vector::empty
T empty(T... args)
Gaudi::Parsers::PragmaOptions::HasDumpFile
bool HasDumpFile()
Definition: PragmaOptions.h:51
Gaudi::Parsers
Definition: DODBasicMapper.cpp:17
IProperty.h
std::smatch
std::unordered_map::end
T end(T... args)
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:70
Catalog.h
JobOptionsSvc::fillServiceCatalog
void fillServiceCatalog(const Gaudi::Parsers::Catalog &catalog)
Definition: JobOptionsSvc.cpp:225
PropertyHolder.h
ProduceConsume.key
key
Definition: ProduceConsume.py:81
JobOptionsSvc::items
std::vector< std::tuple< std::string, std::string > > items() const override
Definition: JobOptionsSvc.cpp:81
std::unordered_map< PropertyId, Gaudi::Details::WeakPropertyRef >
Gaudi::Parsers::PragmaOptions::IsPrintTree
bool IsPrintTree()
Definition: PragmaOptions.h:50
IOptionsSvc.h
Gaudi::Parsers::PragmaOptions::dumpFile
const std::string & dumpFile() const
Definition: PragmaOptions.h:46
JobOptionsSvc::has
bool has(const std::string &key) const override
Definition: JobOptionsSvc.cpp:80
conf
Definition: conf.py:1
JobOptionsSvc::m_globalDefaults
std::vector< std::pair< std::regex, std::string > > m_globalDefaults
Definition: JobOptionsSvc.cpp:137
Gaudi::Property< std::string >
JobOptionsSvc::JobOptionsSvc
JobOptionsSvc(const std::string &name, ISvcLocator *svc)
Definition: JobOptionsSvc.cpp:147
Gaudi::Parsers::Catalog
Definition: Catalog.h:39
Property.h
MsgStream.h
PythonConfig
Definition: PythonConfig.h:33
PrepareBase.out
out
Definition: PrepareBase.py:20
Analyzer.h
PragmaOptions.h