ATLAS Offline Software
TrigConfJobOptionsSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
5 #include "TrigConfSvcHelper.h"
6 
7 #include "GaudiKernel/IProperty.h"
8 #include "Gaudi/Property.h"
9 
12 
13 #include <nlohmann/json.hpp>
14 
15 #include <fstream>
16 #include <format>
17 
18 
19 namespace {
20  void trim_inplace(std::string& s) {
21  auto start = std::find_if_not(s.begin(), s.end(), ::isspace);
22  auto end = std::find_if_not(s.rbegin(), s.rend(), ::isspace).base();
23  s = (start < end) ? std::string(start, end) : "";
24  }
25 }
26 
27 TrigConf::JobOptionsSvc::JobOptionsSvc(const std::string& name, ISvcLocator* pSvcLocator) :
28  base_class(name, pSvcLocator),
29  m_optsvc("JobOptionsSvc/TrigConfWrapped_JobOptionsSvc", name)
30 {}
31 
33 {
34  ATH_MSG_INFO("Initializing TrigConf::JobOptionsSvc");
35 
36  // Configure the wrapped JobOptionsSvc
37  ATH_CHECK(m_optsvc.retrieve());
38  m_optsvc->set( m_optsvc.name() + ".TYPE", "NONE" );
39 
40  if (m_sourceType == "FILE") {
41  ATH_MSG_INFO("Reading joboptions from " << m_sourcePath.value());
42  ATH_CHECK(readOptionsJson(m_sourcePath));
43  }
44  else if (m_sourceType == "DB") {
45  parseDBString(m_sourcePath);
46  ATH_MSG_INFO("Reading SMK " << m_smk << " from '" << m_server << "'");
47  ATH_CHECK(readOptionsDB(m_server, m_smk));
48  }
49  else if (m_sourceType == "PYTHON") {
50  /* "PYTHON" refers to loading properties directly from Python files
51  which we do not use in ATLAS. Configuration via athena.py is of
52  course supported by the default "NONE" mode. */
53  ATH_MSG_ERROR("Not supported " << m_sourceType);
54  return StatusCode::FAILURE;
55  }
56 
57  return StatusCode::SUCCESS;
58 }
59 
65 void TrigConf::JobOptionsSvc::parseDBString(const std::string& s)
66 {
67  std::string key, val;
68  std::istringstream iss(s);
69  while (std::getline(std::getline(iss, key, '='), val, ';')) {
70  trim_inplace(key);
71  trim_inplace(val);
72  if (key == "smkey")
73  m_smk = std::stoi(val);
74  else if (key == "server")
75  m_server = val;
76  else if (key == "lvl1key")
77  m_l1psk = std::stoi(val);
78  else if (key == "hltkey")
79  m_hltpsk = std::stoi(val);
80  }
81 }
82 
84 {
85  // Dump job options to JSON file if configured (for debugging only!)
86  if (!m_dump.empty()) {
87  ATH_MSG_INFO("Writing job options to " << m_dump.value());
88  dumpOptions(m_dump).ignore();
89  }
90  return StatusCode::SUCCESS;
91 }
92 
94 {
95  std::ifstream f(file);
96  if (!f) {
97  ATH_MSG_ERROR("Cannot open file " << file);
98  return StatusCode::FAILURE;
99  }
100 
102  f >> json;
103 
104  for (const auto& [client, props] : json["properties"].items()) {
105  for (const auto& [name, value] : props.items()) {
106  set(client + "." + name, value.get<std::string>());
107  }
108  }
109 
110  return StatusCode::SUCCESS;
111 }
112 
113 StatusCode TrigConf::JobOptionsSvc::readOptionsDB(const std::string& db_server, int smk)
114 {
115  // db job options loader
116  TrigConf::TrigDBJobOptionsLoader jodbloader(db_server);
117  std::string crest_server("");
118  std::string crest_api("");
119  std::string dbname("");
120  if(isCrestConnection(db_server, crest_server, crest_api, dbname)) {
121  jodbloader.setCrestTrigDB(dbname);
122  jodbloader.setCrestConnection(crest_server, crest_api);
123  }
124 
126  jodbloader.loadJobOptions( smk, jo );
127  if (jo) {
128  unsigned int nClients(0), nProps(0);
129  TrigConf::DataStructure ds = jo.getObject("properties");
130  for( const auto & client : ds.data()) {
131  nClients++;
132  for( const auto & property : client.second ) {
133  nProps++;
134  set(client.first + "." + property.first, property.second.data());
135  }
136  }
137  ATH_MSG_INFO("Loaded job options from " << nClients << " clients with " << nProps << " in total");
138  } else {
139  ATH_MSG_FATAL("Could not load job options from database " << db_server << " with SMK " << smk);
140  return StatusCode::FAILURE;
141  }
142  return StatusCode::SUCCESS;
143 }
144 
145 
151 {
152  // JSON filetype identifier
153  nlohmann::json json_file;
154  json_file["filetype"] = "joboptions";
155 
156  // Properties
157  auto& json = json_file["properties"] = {};
158  for (const auto& [name, value] : items()) {
159  const size_t idot = name.rfind('.');
160  const std::string client = name.substr(0, idot);
161  const std::string propname = name.substr(idot+1);
162  json[client][propname] = value;
163  }
164 
165  // Write JSON to file
166  std::ofstream o(file);
167  if (!o) {
168  ATH_MSG_ERROR("Cannot write to file " << file);
169  return StatusCode::FAILURE;
170  }
171  o << std::setw(4) << json_file << std::endl;
172 
173  return StatusCode::SUCCESS;
174 }
TrigConf::DataStructure::getObject
DataStructure getObject(const std::string &pathToChild, bool ignoreIfMissing=false) const
Access to configuration object.
Definition: DataStructure.cxx:207
CaloNoise_fillDB.dbname
dbname
Definition: CaloNoise_fillDB.py:41
checkxAOD.ds
ds
Definition: Tools/PyUtils/bin/checkxAOD.py:260
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
json
nlohmann::json json
Definition: HistogramDef.cxx:9
RunEBWeightsComputation.smk
smk
Definition: RunEBWeightsComputation.py:87
TrigConf::JobOptionsSvc::readOptionsJson
StatusCode readOptionsJson(const std::string &file)
Definition: TrigConfJobOptionsSvc.cxx:93
TrigConf::JobOptionsSvc::initialize
virtual StatusCode initialize() override
Definition: TrigConfJobOptionsSvc.cxx:32
TrigConf::JobOptionsSvc::readOptionsDB
StatusCode readOptionsDB(const std::string &db_server, int smk)
Definition: TrigConfJobOptionsSvc.cxx:113
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:13
python.TriggerCrestUtil.crest_server
crest_server
Definition: TriggerCrestUtil.py:422
TrigDBJobOptionsLoader.h
Loader class for Trigger configuration from the Trigger DB.
athena.value
value
Definition: athena.py:124
TrigConf::TrigDBLoader::setCrestConnection
void setCrestConnection(const std::string &server, const std::string &version="")
declare CREST as the source of the configuration An empty crest server makes it use Oracle
Definition: TrigDBLoader.cxx:80
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:92
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:27
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TrigConf::TrigDBLoader::setCrestTrigDB
void setCrestTrigDB(const std::string &crestTrigDB)
set trigger db for the crest connection
Definition: TrigDBLoader.cxx:102
file
TFile * file
Definition: tile_monitor.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
hist_file_dump.f
f
Definition: hist_file_dump.py:140
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:232
TrigConf::TrigDBJobOptionsLoader
Loader of trigger configurations from Json files.
Definition: TrigDBJobOptionsLoader.h:28
TrigConf::JobOptionsSvc::start
virtual StatusCode start() override
Definition: TrigConfJobOptionsSvc.cxx:83
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:150
TrigConf::DataStructure
Base class for Trigger configuration data and wrapper around underlying representation.
Definition: DataStructure.h:37
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:71
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 job options from the Trigger DB into a ptree for a given SuperMasterKey (SMK)
Definition: TrigDBJobOptionsLoader.cxx:51
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
TrigConf::isCrestConnection
bool isCrestConnection(const std::string &db_connection_string, std::string &crest_server, std::string &crest_api, std::string &dbname)
Function to interpret the trigger connection string for CREST connections Format of the connections s...
Definition: TrigConfSvcHelper.cxx:9
TrigConfSvcHelper.h
DataStructure.h
TrigConf::JobOptionsSvc::parseDBString
void parseDBString(const std::string &s)
Parse DB connection string and fill private members.
Definition: TrigConfJobOptionsSvc.cxx:65
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37