ATLAS Offline Software
TrigConfJobOptionsSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <boost/algorithm/string.hpp>
6 #include <fstream>
7 #include <nlohmann/json.hpp>
8 
9 #include "GaudiKernel/IProperty.h"
10 #include "Gaudi/Property.h"
11 
14 
15 #include "TrigConfJobOptionsSvc.h"
16 
17 TrigConf::JobOptionsSvc::JobOptionsSvc(const std::string& name, ISvcLocator* pSvcLocator) :
18  base_class(name, pSvcLocator),
19  m_optsvc("JobOptionsSvc/TrigConfWrapped_JobOptionsSvc", name)
20 {}
21 
23 {
24  ATH_MSG_INFO("Initializing TrigConf::JobOptionsSvc");
25 
26  // Configure the wrapped JobOptionsSvc
27  ATH_CHECK(m_optsvc.retrieve());
28  m_optsvc->set( m_optsvc.name() + ".TYPE", "NONE" );
29 
30  if (m_sourceType == "FILE") {
31  ATH_MSG_INFO("Reading joboptions from " << m_sourcePath.value());
32  ATH_CHECK(readOptionsJson(m_sourcePath));
33  }
34  else if (m_sourceType == "DB") {
35  parseDBString(m_sourcePath);
36  ATH_MSG_INFO("Reading SMK " << m_smk << " from '" << m_server << "'");
37  ATH_CHECK(readOptionsDB(m_server, m_smk));
38  }
39  else if (m_sourceType == "PYTHON") {
40  /* "PYTHON" refers to loading properties directly from Python files
41  which we do not use in ATLAS. Configuration via athena.py is of
42  course supported by the default "NONE" mode. */
43  ATH_MSG_ERROR("Not supported " << m_sourceType);
44  return StatusCode::FAILURE;
45  }
46 
47  return StatusCode::SUCCESS;
48 }
49 
54 void TrigConf::JobOptionsSvc::parseDBString(const std::string& s)
55 {
56  std::string key, val;
57  std::istringstream iss(s);
58  while (std::getline(std::getline(iss, key, '='), val, ';')) {
61  if (key == "smkey")
62  m_smk = std::stoi(val);
63  else if (key == "server")
64  m_server = val;
65  else if (key == "lvl1key")
66  m_l1psk = std::stoi(val);
67  else if (key == "hltkey")
68  m_hltpsk = std::stoi(val);
69  }
70 }
71 
73 {
74  // Dump job options to JSON file if configured (for debugging only!)
75  if (!m_dump.empty()) {
76  ATH_MSG_INFO("Writing job options to " << m_dump.value());
77  dumpOptions(m_dump).ignore();
78  }
79  return StatusCode::SUCCESS;
80 }
81 
83 {
84  std::ifstream f(file);
85  if (!f) {
86  ATH_MSG_ERROR("Cannot open file " << file);
87  return StatusCode::FAILURE;
88  }
89 
91  f >> json;
92 
93  for (const auto& [client, props] : json["properties"].items()) {
94  for (const auto& [name, value] : props.items()) {
95  set(client + "." + name, value.get<std::string>());
96  }
97  }
98 
99  return StatusCode::SUCCESS;
100 }
101 
102 StatusCode TrigConf::JobOptionsSvc::readOptionsDB(const std::string& db_server, int smk)
103 {
104  // db job options loader
105  TrigConf::TrigDBJobOptionsLoader jodbloader(db_server);
106 
108  jodbloader.loadJobOptions( smk, jo );
109  if (jo) {
110  unsigned int nClients(0), nProps(0);
111  TrigConf::DataStructure ds = jo.getObject("properties");
112  for( const auto & client : ds.data()) {
113  nClients++;
114  for( const auto & property : client.second ) {
115  nProps++;
116  set(client.first + "." + property.first, property.second.data());
117  }
118  }
119  ATH_MSG_INFO("Loaded job options from " << nClients << " clients with " << nProps << " in total");
120  } else {
121  ATH_MSG_FATAL("Could not load job options from database " << db_server << " with SMK " << smk);
122  return StatusCode::FAILURE;
123  }
124  return StatusCode::SUCCESS;
125 }
126 
127 
133 {
134  // JSON filetype identifier
135  nlohmann::json json_file;
136  json_file["filetype"] = "joboptions";
137 
138  // Properties
139  auto& json = json_file["properties"] = {};
140  for (const auto& [name, value] : items()) {
141  const size_t idot = name.rfind('.');
142  const std::string client = name.substr(0, idot);
143  const std::string propname = name.substr(idot+1);
144  json[client][propname] = value;
145  }
146 
147  // Write JSON to file
148  std::ofstream o(file);
149  if (!o) {
150  ATH_MSG_ERROR("Cannot write to file " << file);
151  return StatusCode::FAILURE;
152  }
153  o << std::setw(4) << json_file << std::endl;
154 
155  return StatusCode::SUCCESS;
156 }
TrigConf::DataStructure::getObject
DataStructure getObject(const std::string &pathToChild, bool ignoreIfMissing=false) const
Access to configuration object.
Definition: DataStructure.cxx:207
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
checkxAOD.ds
ds
Definition: Tools/PyUtils/bin/checkxAOD.py:257
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
json
nlohmann::json json
Definition: HistogramDef.cxx:9
trim
std::string trim(const std::string &str, const std::string &whitespace=" \t")
Definition: BTaggingTruthTaggingTool.cxx:1149
RunEBWeightsComputation.smk
smk
Definition: RunEBWeightsComputation.py:87
TrigConf::JobOptionsSvc::readOptionsJson
StatusCode readOptionsJson(const std::string &file)
Definition: TrigConfJobOptionsSvc.cxx:82
TrigConf::JobOptionsSvc::initialize
virtual StatusCode initialize() override
Definition: TrigConfJobOptionsSvc.cxx:22
TrigConf::JobOptionsSvc::readOptionsDB
StatusCode readOptionsDB(const std::string &db_server, int smk)
Definition: TrigConfJobOptionsSvc.cxx:102
TrigDBJobOptionsLoader.h
Loader class for Trigger configuration from the Trigger DB.
athena.value
value
Definition: athena.py:122
rerun_display.client
client
Definition: rerun_display.py:31
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TrigConf::JobOptionsSvc::JobOptionsSvc
JobOptionsSvc(const std::string &name, ISvcLocator *pSvcLocator)
Definition: TrigConfJobOptionsSvc.cxx:17
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
file
TFile * file
Definition: tile_monitor.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TrigConf::name
Definition: HLTChainList.h:35
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:224
TrigConf::TrigDBJobOptionsLoader
Loader of trigger configurations from Json files.
Definition: TrigDBJobOptionsLoader.h:28
TrigConf::JobOptionsSvc::start
virtual StatusCode start() override
Definition: TrigConfJobOptionsSvc.cxx:72
TrigConf::JobOptionsSvc::dumpOptions
StatusCode dumpOptions(const std::string &file)
This is mainly for debugging purposes and to compare the JobOptions as seen by the JobOptionSvc to th...
Definition: TrigConfJobOptionsSvc.cxx:132
TrigConf::DataStructure
Base class for Trigger configuration data and wrapper around underlying representation.
Definition: DataStructure.h:37
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
TrigConfJobOptionsSvc.h
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
TrigConf::TrigDBJobOptionsLoader::loadJobOptions
bool loadJobOptions(unsigned int smk, boost::property_tree::ptree &jobOptions, const std::string &outFileName="") const
Load content from the Trigger DB into two ptrees for a given SuperMasterKey (SMK)
Definition: TrigDBJobOptionsLoader.cxx:51
DataStructure.h
TrigConf::JobOptionsSvc::parseDBString
void parseDBString(const std::string &s)
Parse DB connection string and fill private members.
Definition: TrigConfJobOptionsSvc.cxx:54
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37