ATLAS Offline Software
Loading...
Searching...
No Matches
TriggerMenuRW.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5
6
18#include "TrigConfData/L1Menu.h"
22
23#include <cstdlib>
24#include <vector>
25#include <format> //for format error
26
27using namespace std;
28
29struct Config {
30public:
31
32 std::vector<std::string> knownParameters {
33 "file", "f", "smk", "l1psk", "hltpsk", "bgsk", "db", "crest-db", "crest-server", "crest-api", "write", "w",
34 "Write", "W", "help", "h", "detail", "d", "ctp", "c"
35 };
36
37 // parameters
38 // input
39 std::vector<std::string> inputFiles {};
40 unsigned int smk { 0 };
41 unsigned int l1psk { 0 };
42 unsigned int hltpsk { 0 };
43 unsigned int bgsk { 0 };
44 std::string dbalias { "TRIGGERDB_RUN3" };
45 std::string crestDb { "" };
46 const std::string crestServerDefault{ "http://crest-04.cern.ch/" };
48 std::string crestApi { "" };
49 bool doCtp { false }; // flag to read CTP files
50
51 // output
52 bool write { false }; // flag to enable writing
53 bool writeFromDataStructure { false }; // flag to enable writing of the L1Menu structure (if available)
54 std::string base { "" };
55
56 // other
57 bool help { false };
58 bool detail { false };
59
60 // to keep track of configuration errors
61 vector<string> error;
62
63 // parses the commandline
64 void parseProgramOptions(int argc, char* argv[]);
65
66 // help
67 void usage();
68
69};
70
71
73
74 cout << "The program needs to be run with the following specifications:\n\n";
75 cout << "TriggerMenuRW <options>\n";
76 cout << "\n";
77 cout << "[Input options]\n";
78 cout << " -f|--file file1 [file2 [file3 ...]] ... one or multiple json files\n";
79 cout << " --smk smk ... smk \n";
80 cout << " --l1psk l1psk ... the L1 prescale key \n";
81 cout << " --hltpsk hltpsk ... the HLT prescale key \n";
82 cout << " --bgsk bgsk ... the bunchgroup key \n";
83 cout << " --db dbalias ... dbalias for oracle/frontier access (default " << dbalias << ") \n";
84 cout << " --crest-db crestdb ... crest_db for Crest access, if specified Crest will be used (default '') \n";
85 cout << " possible values: CONF_DATA_RUN3, CONF_MC_RUN3, CONF_REPR_RUN3\n";
86 cout << " --crest-server server ... crest server (default " << crestServerDefault << ")\n";
87 cout << " --crest-api version ... crest api version (default taken from CrestApi)";
88 cout << " -c|--ctp ... if provided together with the SMK and DB then will read only CTP files from the DB and not the rest of the menu\n";
89 cout << "[Output options]\n";
90 cout << " -w|--write [base] ... to write out json files, e.g. L1menu[_<base>].json. base is optional.\n";
91 cout << " -W|--Write [base] ... to write out json files from the internal structure (only for L1Menu), e.g. L1menu[_<base>].json. base is optional.\n";
92 cout << "[Other options]\n";
93 cout << " -h|--help ... this help\n";
94 cout << " -d|--detail ... prints detailed job options\n";
95 cout << "\n\n";
96 cout << "Examples\n";
97 cout << " --file L1menu.json HLTMenu.json ... read L1Menu.json and HLTMenu.json and show some basic statistics\n";
98 cout << " --db TRIGGERDB_RUN3 --smk 3205 ... read the L1 menu, HLT menu, HLT job options, and HLT monitoring groups for SMK 3205\n";
99 cout << " --db TRIGGERDB_RUN3 --smk 3205 -w ... read the information and write DB content directly as json files\n";
100}
101
102void
103Config::parseProgramOptions(int argc, char* argv[]) {
104
105 std::string currentParameter("");
106 std::string listofUnknownParameters = "";
107
108 for(int i=1; i<argc; i++) {
109
110 std::string currentWord(argv[i]);
111 bool isParam = currentWord[0]=='-'; // string starts with a '-', so it is a parameter name
112
113 // get the parameter name
114 int firstChar = currentWord.find_first_not_of('-');
115 string paramName = currentWord.substr(firstChar);
116
117 // check if the parameter is known
118 if ( isParam && std::find(knownParameters.begin(), knownParameters.end(), paramName) == knownParameters.end() ) {
119 listofUnknownParameters += " " + currentWord;
120 continue;
121 }
122
123 if(isParam) {
124 currentParameter = "";
125 // check the boolean parameters
126 if(paramName == "h" || paramName == "help" ) { help = true; continue; }
127 if(paramName == "d" || paramName == "detail" ) { detail = true; continue; }
128 if(paramName == "w" || paramName == "write" ) { write = true; }
129 if(paramName == "W" || paramName == "Write" ) { writeFromDataStructure = true; }
130 if(paramName == "c" || paramName == "ctp" ) { doCtp = true; }
131 currentParameter = std::move(paramName);
132 continue;
133 }
134
135 // now treat the parameter values
136
137 // inputs
138 if(currentParameter == "file" || currentParameter == "f") {
139 inputFiles.push_back(std::move(currentWord));
140 continue;
141 }
142 if(currentParameter == "smk") {
143 smk = stoul(currentWord);
144 continue;
145 }
146 if(currentParameter == "l1psk") {
147 l1psk = stoul(currentWord);
148 continue;
149 }
150 if(currentParameter == "hltpsk") {
151 hltpsk = stoul(currentWord);
152 continue;
153 }
154 if(currentParameter == "bgsk") {
155 bgsk = stoul(currentWord);
156 continue;
157 }
158 if(currentParameter == "db") {
159 dbalias = std::move(currentWord);
160 continue;
161 }
162 if(currentParameter == "crest-db") {
163 crestDb = std::move(currentWord);
164 continue;
165 }
166 if(currentParameter == "crest-server") {
167 crestServer = std::move(currentWord);
168 continue;
169 }
170 if(currentParameter == "crest-api") {
171 crestApi = std::move(currentWord);
172 continue;
173 }
174 // output
175 if(currentParameter == "write" || currentParameter == "w" || currentParameter == "Write" || currentParameter == "W") {
176 base = std::move(currentWord);
177 continue;
178 }
179
180 }
181
182 // some sanity checks
183 if ( inputFiles.size() == 0 and smk == 0 and l1psk == 0 and hltpsk == 0 and bgsk == 0 ) {
184 error.push_back("No input specified! Please provide either one of the following: input file(s), smk, l1psk, hltpsk, or bgsk");
185 }
186
187 if ( listofUnknownParameters.size() > 0 ) {
188 error.push_back( string("Unknown parameter(s):") + listofUnknownParameters);
189 }
190
191}
192
193namespace {
194 bool
195 writeJsonFile(const TrigConf::DataStructure & ds, const std::string & kind, const Config & cfg) {
196 if( cfg.write ) {
197 std::string filename = kind;
198 if ( cfg.base != "" ) {
199 filename += "_" + cfg.base;
200 }
201 filename += ".json";
202 TrigConf::JsonFileLoader fileLoader;
203 return fileLoader.saveFile(filename, ds);
204 } else if ( cfg.writeFromDataStructure ) {
205 std::string filename = kind;
206 if ( cfg.base != "" ) {
207 filename += "_" + cfg.base;
208 }
209 filename += ".fromDS.json";
210 if ( kind=="L1Menu" ) {
212 const auto & l1menu = dynamic_cast<const TrigConf::L1Menu &>(ds);
213 return fileWriter.writeJsonFile(filename, l1menu);
214 } else if ( kind == "HLTMenu") {
216 const auto & hltmenu = dynamic_cast<const TrigConf::HLTMenu &>(ds);
217 return fileWriter.writeJsonFile(filename, hltmenu);
218 }
219 }
220 return true;
221 }
222
223 std::string
224 outputFileName(const std::string & kind, const Config & cfg) {
225 if( ! cfg.write )
226 return "";
227 std::string filename = kind;
228 if ( cfg.base != "" ) {
229 filename += "_" + cfg.base;
230 }
231 filename += ".json";
232 return filename;
233 }
234
235}
236//coverity[root_function]
237int main(int argc, char** argv) {
238
239 Config cfg;
240 cfg.parseProgramOptions(argc, argv);
241 if(cfg.help) {
242 cfg.usage();
243 return 0;
244 }
245
246 if(cfg.error.size()!=0) {
247 for(const string & e: cfg.error)
248 cerr << e << endl;
249 cfg.usage();
250 return 1;
251 }
252
253 if( cfg.inputFiles.size()>0 ) {
254 // load config from files
255 TrigConf::JsonFileLoader fileLoader;
256 for (const std::string & fn : cfg.inputFiles) {
257 // check if the file is L1 or HLT
258 std::string filetype = fileLoader.getFileType( fn );
259 if(filetype == "l1menu") {
260 TrigConf::L1Menu l1menu;
261 fileLoader.loadFile( fn, l1menu);
262 cout << "Loaded L1 menu " << l1menu.name() << " with " << l1menu.size() << " items from " << fn << endl;
263 l1menu.printMenu(cfg.detail);
264 writeJsonFile(l1menu, "L1Menu", cfg);
265 } else if(filetype == "hltmenu" ) {
266 TrigConf::HLTMenu hltmenu;
267 fileLoader.loadFile( fn, hltmenu);
268 cout << "Loaded HLT menu " << hltmenu.name() << " with " << hltmenu.size() << " chains from " << fn << endl;
269 hltmenu.printMenu(cfg.detail);
270 writeJsonFile(hltmenu, "HLTMenu", cfg);
271 } else if(filetype == "l1prescale" ) {
273 fileLoader.loadFile( fn, l1pss);
274 cout << "Loaded L1 prescales set file " << fn << " with " << l1pss.size() << " prescales from " << fn << endl;
275 writeJsonFile(l1pss, "L1PrescalesSet", cfg);
276 } else if(filetype == "hltprescale" ) {
278 fileLoader.loadFile( fn, hltpss);
279 cout << "Loaded HLT prescales set file " << fn << " with " << hltpss.size() << " prescales from " << fn << endl;
280 hltpss.printPrescaleSet(cfg.detail);
281 writeJsonFile(hltpss, "HLTPrescalesSet", cfg);
282 } else if(filetype == "bunchgroupset" ) {
284 fileLoader.loadFile( fn, bgs);
285 cout << "Loaded L1 BunchGroup set file " << fn << " with " << bgs.sizeNonEmpty() << " non-empty bunchgroups from " << fn << endl;
286 bgs.printSummary(cfg.detail);
287 writeJsonFile(bgs, "BunchGroupSet", cfg);
288 } else if(filetype == "joboptions" ) {
290 fileLoader.loadFile( fn, jo);
291 cout << "Loaded job options with " << jo.getObject("properties").getKeys().size() << " properties from " << fn << endl;
292 if( cfg.detail ) {
293 TrigConf::DataStructure ds = jo.getObject("properties");
294 //cppcheck-suppress throwInEntryPoint
295 for( const auto& alg : ds.data()) {
296 std::cout << alg.first << std::endl;
297 for( const auto& prop : alg.second ) {
298 std::cout << " " << prop.first << " -> " << prop.second.data() << std::endl;
299 }
300 }
301 }
302 writeJsonFile(jo, "HLTJobOptions", cfg);
303 } else if(filetype == "hltmonitoringsummary" ) {
305 fileLoader.loadFile( fn, mon);
306 cout << "Loaded HLT monnitoring with " << mon.size() << " signatures from " << fn << endl;
307 mon.printMonConfig(cfg.detail);
308 writeJsonFile(mon, "HLTMonitoring", cfg);
309 } else {
310 cerr << "File " << fn << " not recognized as being an L1 or HLT menu or prescale set or bunchgroup set" << endl;
311 }
312 }
313 }
314
315 if( cfg.smk != 0 && !cfg.doCtp ) {
316 // load config from DB
317 {
318 // db menu loader
319 TrigConf::TrigDBMenuLoader dbloader(cfg.dbalias);
320 if(!cfg.crestDb.empty()) {
321 dbloader.setCrestTrigDB(cfg.crestDb);
322 dbloader.setCrestConnection(cfg.crestServer, cfg.crestApi);
323 }
324
325 // L1 menu
326 {
327 TrigConf::L1Menu l1menu;
328 try {
329 dbloader.loadL1Menu( cfg.smk, l1menu, outputFileName("L1Menu", cfg) );
330 }
331 catch(TrigConf::IOException & ex) {
332 cout << "Could not load L1 menu. An exception occurred: " << ex.what() << endl;
333 }
334 if(l1menu) {
335 cout << "Loaded L1 menu " << l1menu.name() << " with " << l1menu.size() << " items from " << cfg.dbalias << " with SMK " << cfg.smk << endl;
336 if( cfg.detail ) {
337 l1menu.printMenu(true);
338 }
339 }
340 cout << endl;
341 }
342
343 // HLT menu
344 {
345 TrigConf::HLTMenu hltmenu;
346 try {
347 dbloader.loadHLTMenu( cfg.smk, hltmenu, outputFileName("HLTMenu", cfg));
348 }
349 catch(TrigConf::IOException & ex) {
350 cout << "Could not load HLT menu. An exception occurred: " << ex.what() << endl;
351 }
352 if (hltmenu) {
353 cout << "Loaded HLT menu " << hltmenu.name() << " with " << hltmenu.size() << " chains from " << cfg.dbalias << " with SMK " << cfg.smk << endl;
354 if( cfg.detail ) {
355 hltmenu.printMenu(true);
356 }
357 }
358 cout << endl;
359 }
360 }
361
362 // Job options
363 {
364 TrigConf::TrigDBJobOptionsLoader dbloader(cfg.dbalias);
365 if(!cfg.crestDb.empty()) {
366 dbloader.setCrestTrigDB(cfg.crestDb);
367 dbloader.setCrestConnection(cfg.crestServer, cfg.crestApi);
368 }
370 try {
371 dbloader.loadJobOptions( cfg.smk, jo, outputFileName("HLTJobOptions", cfg) );
372 }
373 catch(TrigConf::IOException & ex) {
374 cout << "Could not load HLT job options. An exception occurred: " << ex.what() << endl;
375 }
376 if (jo) {
377 cout << "Loaded job options with " << jo.getObject("properties").getKeys().size() << " entries from " << cfg.dbalias << " with SMK " << cfg.smk << endl;
378 if( cfg.detail ) {
379 TrigConf::DataStructure ds = jo.getObject("properties");
380 for( const auto& alg : ds.data()) {
381 std::cout << alg.first << std::endl;
382 for( const auto& prop : alg.second ) {
383 std::cout << " " << prop.first << " -> " << prop.second.data() << std::endl;
384 }
385 }
386 }
387 }
388 cout << endl;
389 }
390
391 // HLT monitoring groups
392 {
393 TrigConf::TrigDBMonitoringLoader dbloader(cfg.dbalias);
394 if(!cfg.crestDb.empty()) {
395 dbloader.setCrestTrigDB(cfg.crestDb);
396 dbloader.setCrestConnection(cfg.crestServer, cfg.crestApi);
397 }
399 try {
400 dbloader.loadHLTMonitoring( cfg.smk, hltmon, outputFileName("HLTMonitoring", cfg));
401 }
402 catch(TrigConf::IOException & ex) {
403 cout << "Could not load HLT monitoring. An exception occurred: " << ex.what() << endl;
404 }
405 if (hltmon) {
406 cout << "Loaded HLT monitoring with " << hltmon.size() << " signatures rom " << cfg.dbalias << " with SMK " << cfg.smk << endl;
407 if( cfg.detail ) {
408 hltmon.printMonConfig(true);
409 }
410 }
411 cout << endl;
412 }
413
414 }
415
416 if( cfg.smk != 0 && cfg.doCtp ) {
417 TrigConf::TrigDBCTPFilesLoader dbloader(cfg.dbalias);
418 TrigConf::L1CTPFiles ctpfiles;
419 try{
420 dbloader.loadHardwareFiles(cfg.smk, ctpfiles, 0x0F, outputFileName("CTPFiles", cfg));
421 } catch (std::format_error & e){
422 cout << " format_error "<<e.what()<<" thrown in TriggerMenuRW.\n";
423 return 1;
424 }
425 ctpfiles.print();
426 }
427
428 if( cfg.l1psk != 0 ) {
429 // load L1 prescales set from DB
430 TrigConf::TrigDBL1PrescalesSetLoader dbloader(cfg.dbalias);
431 if(!cfg.crestDb.empty()) {
432 dbloader.setCrestTrigDB(cfg.crestDb);
433 dbloader.setCrestConnection(cfg.crestServer, cfg.crestApi);
434 }
436 try {
437 dbloader.loadL1Prescales( cfg.l1psk, l1pss, outputFileName("L1PrescalesSet", cfg) );
438 }
439 catch(TrigConf::IOException & ex) {
440 cout << "Could not load L1 prescales. An exception occurred: " << ex.what() << endl;
441 }
442 if (l1pss) {
443 cout << "Loaded L1 prescales set " << l1pss.name() << " with " << l1pss.size() << " prescales from " << cfg.dbalias << " with L1 PSK " << cfg.l1psk << endl;
444 }
445 }
446
447 if( cfg.hltpsk != 0 ) {
448 // load L1 prescales set from DB
449 TrigConf::TrigDBHLTPrescalesSetLoader dbloader(cfg.dbalias);
450 if(!cfg.crestDb.empty()) {
451 dbloader.setCrestTrigDB(cfg.crestDb);
452 dbloader.setCrestConnection(cfg.crestServer, cfg.crestApi);
453 }
455 try {
456 dbloader.loadHLTPrescales( cfg.hltpsk, hltpss, outputFileName("HLTPrescalesSet", cfg) );
457 }
458 catch(TrigConf::IOException & ex) {
459 cout << "Could not load HLT prescales. An exception occurred: " << ex.what() << endl;
460 }
461 if (hltpss) {
462 cout << "Loaded HLT prescales set " << hltpss.name() << " with " << hltpss.size() << " prescales from " << cfg.dbalias << " with HLT PSK " << cfg.hltpsk << endl;
463 }
464 }
465
466 if( cfg.bgsk != 0 ) {
467 // load L1 prescales set from DB
468 TrigConf::TrigDBL1BunchGroupSetLoader dbloader(cfg.dbalias);
469 if(!cfg.crestDb.empty()) {
470 dbloader.setCrestTrigDB(cfg.crestDb);
471 dbloader.setCrestConnection(cfg.crestServer, cfg.crestApi);
472 }
474 try {
475 dbloader.loadBunchGroupSet( cfg.bgsk, bgs, outputFileName("BunchGroupSet", cfg) );
476 }
477 catch(TrigConf::IOException & ex) {
478 cout << "Could not load bunchgroup set. An exception occurred: " << ex.what() << endl;
479 }
480 if (bgs) {
481 cout << "Loaded L1 bunchgroup set " << bgs.name() << " with " << bgs.size() << " bunchgroups from " << cfg.dbalias << " with BGSK " << cfg.bgsk << endl;
482 if( cfg.detail ) {
483 bgs.printSummary(true);
484 }
485 }
486 }
487
488 return 0;
489}
Loader class for Trigger configuration from Json.
static const std::string outputFileName
Loader class for Trigger configuration (L1 hardware files) from the Trigger DB.
Loader class for Trigger configuration (HLT prescales set) from the Trigger DB.
Loader class for Trigger configuration from the Trigger DB.
Loader class for Trigger configuration (L1 prescales set) from the Trigger DB.
Loader class for Trigger configuration (L1 prescales set) from the Trigger DB.
Base class for Trigger configuration data and wrapper around underlying representation.
virtual const std::string & name() const final
std::vector< std::string > getKeys() const
Access to the keys of an DataStructure which presents a dictionary.
DataStructure getObject(const std::string &pathToChild, bool ignoreIfMissing=false) const
Access to configuration object.
HLT menu configuration.
Definition HLTMenu.h:21
std::size_t size() const
Accessor to the number of HLT chains.
Definition HLTMenu.cxx:35
void printMenu(bool full=false) const
print overview of L1 Menu
Definition HLTMenu.cxx:96
HLT monitoring configuration.
std::size_t size() const
Accessor to the number of HLT monitoring chains.
void printMonConfig(bool full=false) const
print overview of L1 Menu
HLT menu configuration.
std::size_t size() const
number of HLT prescales
void printPrescaleSet(bool full) const
Loader of trigger configurations from Json files.
bool saveFile(const std::string &filename, const DataStructure &data) const
Save content of DataStructure (underlying ptree) to a file.
std::string getFileType(const std::string &filename) const
Checks the trigger level of a given json file.
bool loadFile(const std::string &filename, boost::property_tree::ptree &data, const std::string &pathToChild="") const
Load content of json file into a ptree.
Loader of trigger configurations from Json files.
bool writeJsonFile(const std::string &filename, const HLTMenu &menu) const
Loader of trigger configurations from Json files.
bool writeJsonFile(const std::string &filename, const L1Menu &l1menu) const
L1 board configuration.
std::size_t size() const
Accessor to the number of defined bunchgroups.
std::size_t sizeNonEmpty() const
Accessor to the number of non-empty bunchgroups.
void printSummary(bool detailed=false) const
print a more or less detailed summary
L1 menu configuration.
Definition L1CTPFiles.h:29
L1 menu configuration.
Definition L1Menu.h:28
L1 menu configuration.
std::size_t size() const
number of L1 prescales
Loader of trigger configurations from Json files.
bool loadHardwareFiles(unsigned int smk, L1CTPFiles &ctpfiles, uint8_t loadMask=0x0F, const std::string &outFileName="") const
Load content from the Trigger DB into an L1CTPFiles object for a given super master key (SMK)
Loader of trigger configurations from Json files.
bool loadHLTPrescales(unsigned int hltpsk, HLTPrescalesSet &hltpss, const std::string &outFileName="") const
Load content from the Trigger DB into an HLTPrescalesSet for a given HLTPrescaleKey (HLTPSK)
Loader of trigger configurations from Json files.
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)
Loader of trigger configurations from Json files.
bool loadBunchGroupSet(unsigned int bgsk, L1BunchGroupSet &bgs, const std::string &outFileName="") const
Load content from the Trigger DB into an L1PrescalesSet for a given L1PrescaleKey (L1PSK)
Loader of trigger configurations from Json files.
bool loadL1Prescales(unsigned int l1psk, L1PrescalesSet &l1pss, const std::string &outFileName="") const
Load content from the Trigger DB into an L1PrescalesSet for a given L1PrescaleKey (L1PSK)
void setCrestTrigDB(const std::string &crestTrigDB)
set trigger db for the crest connection
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
Loader of trigger menu configurations from the database.
bool loadHLTMenu(unsigned int smk, boost::property_tree::ptree &hltmenu, const std::string &outFileName="") const
Load HLT menu content from the Trigger DB into two ptrees for a given SuperMasterKey (SMK)
bool loadL1Menu(unsigned int smk, boost::property_tree::ptree &l1menu, const std::string &outFileName="") const
Load L1 menu content from the Trigger DB into a ptree for a given SuperMasterKey (SMK)
Loader of trigger menu configurations from the database.
bool loadHLTMonitoring(unsigned int smk, boost::property_tree::ptree &hltmonitoring, const std::string &outFileName="") const
Load HLT menu content from the Trigger DB into two ptrees for a given SuperMasterKey (SMK)
int main()
Definition hello.cxx:18
STL namespace.
std::string crestDb
std::string dbalias
void usage()
std::vector< std::string > knownParameters
unsigned int hltpsk
std::string crestApi
bool writeFromDataStructure
const std::string crestServerDefault
std::string base
unsigned int smk
void parseProgramOptions(int argc, char *argv[])
unsigned int bgsk
std::vector< std::string > inputFiles
std::string crestServer
unsigned int l1psk