ATLAS Offline Software
verify_menu_config.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 
5 '''
6 Script for verifying menu configurations.
7 
8 Can be run on individual files with the -hlt and/or -l1 options,
9 or on multiple files with the --folder option.
10 '''
11 
12 import json
13 import os
14 import sys
15 import argparse
16 
17 from collections import OrderedDict as odict
18 from AthenaCommon.Logging import logging
19 from TriggerMenuMT.menu_config_tests import TriggerLevel, menu_tests
20 
21 log = logging.getLogger("TriggerMenuConfigTest")
22 
23 def verify(config, trigger_level):
24  '''
25  Run all consistency checks for the trigger on the config.
26  '''
27  tests = menu_tests[trigger_level]
28  for test in tests:
29  test.run(config)
30 
31  successes = []
32  failures = []
33  for test in tests:
34  if test.failures:
35  failures.append(test)
36  else:
37  successes.append(test)
38  return { "successes": successes, "failures": failures }
39 
40 
41 def load_and_verify(file_path, trigger_level):
42  if not os.path.isfile(file_path):
43  log.error("'{}' does not exist".format(file_path))
44  elif not os.path.splitext(file_path)[1] == ".json":
45  log.error("'{}' is not a JSON file".format(file_path))
46  else:
47  with open(file_path, "r") as config_file:
48  config = json.load(config_file, object_pairs_hook = odict)
49 
50  return verify(config, trigger_level)
51 
52  return {"successes": [], "failures": []}
53 
54 
55 if __name__ == "__main__":
56  parser = argparse.ArgumentParser(
57  description="Check a generated JSON configuration is valid")
58  individual = parser.add_argument_group(
59  title="Individual file options")
60  individual.add_argument(
61  "-hlt", help="Path to HLT menu config file")
62  individual.add_argument(
63  "-l1", help="Path to L1 menu config file")
64  parser.add_argument("-f", "--folder", help="Path to a folder containing one or more configs to check")
65  args = parser.parse_args()
66 
67  results = {}
68  if args.folder:
69  if not os.path.isdir(args.folder):
70  log.error("Folder '{}' does not exist".format(args.folder))
71  sys.exit(1)
72 
73  json_files = [f for f in os.listdir(args.folder)
74  if f.endswith("json")]
75  def paths_starting_with(prefix):
76  return [args.folder + os.sep + f
77  for f in json_files if f.startswith(prefix)]
78 
79  for hlt_menu in paths_starting_with("HLTMenu"):
80  results[hlt_menu] = load_and_verify(
81  hlt_menu, TriggerLevel.HLT)
82 
83  for l1_menu in paths_starting_with("L1Menu"):
84  results[l1_menu] = load_and_verify(
85  l1_menu, TriggerLevel.L1)
86  else:
87  if args.hlt:
88  results[args.hlt] = load_and_verify(
89  args.hlt, TriggerLevel.HLT)
90  if args.l1:
91  results[args.l1] = load_and_verify(
92  args.l1, TriggerLevel.L1)
93 
94  any_failures = False
95  for filename, result in results.items():
96  success_plural = "" if len(result["successes"]) == 1 \
97  else "es"
98  failure_plural = "" if len(result["failures"]) == 1 \
99  else "s"
100  log.info(
101  "{} success{}, {} failure{} for {}".format(
102  len(result["successes"]), success_plural,
103  len(result["failures"]), failure_plural,
104  filename))
105  for failed_test in result["failures"]:
106  log.error("'{}' failed for: {}".format(
107  failed_test.description,
108  ", ".join([str(i) for i in failed_test.failures])))
109 
110  if result["failures"]:
111  any_failures = True
112  if any_failures:
113  sys.exit(1)
vtune_athena.format
format
Definition: vtune_athena.py:14
verify_menu_config.paths_starting_with
def paths_starting_with(prefix)
Definition: verify_menu_config.py:75
verify_menu_config.load_and_verify
def load_and_verify(file_path, trigger_level)
Definition: verify_menu_config.py:41
verify_menu_config.verify
def verify(config, trigger_level)
Definition: verify_menu_config.py:23
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11