ATLAS Offline Software
JsonUtils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 #
4 # Module to collect JSON specific trigger configuration helpers
5 #
6 import pickle
7 import json
8 
9 def create_joboptions_json(pkl_file, json_file, createDBJson = True):
10  """Create the configuration JSON file from the properties in `pkl_file`
11  and save it into `json_file`. If `createDBJson` then also create the
12  JSON file for database upload at P1."""
13 
14  from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
15 
16  with open(pkl_file, "rb") as f:
17  cfg = pickle.load(f)
18  props = {}
19  if isinstance(cfg, ComponentAccumulator): # CA-based configuration
20  app_props, msg_props, comp_props = cfg.gatherProps()
21  props["ApplicationMgr"] = app_props
22  props["MessageSvc"] = msg_props
23  for comp, name, value in comp_props:
24  props.setdefault(comp, {})[name] = value
25  else: # legacy job options
26  svc = pickle.load(f) # some specialized services (only in legacy config)
27  props = cfg
28  props.update(svc) # merge the two dictionaries
29 
30  hlt_json = {'filetype' : 'joboptions'}
31  hlt_json['properties'] = props
32 
33  # write JSON file
34  with open(json_file, "w") as f:
35  json.dump(hlt_json, f, indent=4, sort_keys=True, ensure_ascii=True)
36 
37  if createDBJson:
38  # also create a configuration JSON file that can be uploaded
39  # to the triggerdb for running at P1
40  assert json_file[-5:] == ".json"
41  db_file = json_file.replace(".json", ".db.json")
42 
43  modifyConfigForP1(json_file, db_file)
44 
45 
46 def modifyConfigForP1(json_file, db_file):
47  """ modifies a number of job properties to run from the TriggerDB and writes out modified JSON
48  """
49  from AthenaCommon.Logging import logging
50  log = logging.getLogger("JsonUtils")
51 
52  with open(json_file, 'r') as f:
53  jocat = json.load(f)
54  properties = jocat['properties']
55 
56  def mod(props, alg, prop, fnc):
57  if alg not in props:
58  log.warning("Asked to modify property of %s but it does not exist", alg)
59  return
60 
61  origVal = props[alg].get(prop, "")
62  props[alg][prop] = fnc(origVal)
63 
64 
65  # L1 and HLT Config Svc must read from db
66  mod( properties, "LVL1ConfigSvc", "InputType", lambda x : "DB" )
67  mod( properties, "HLTConfigSvc", "InputType", lambda x : "DB" )
68  mod( properties, "HLTPrescaleCondAlg", "Source", lambda x : "COOL" ) # prescales will be read from COOL online
69  mod( properties, "HLTPrescaleCondAlg", "TriggerDB", lambda x : "JOSVC" ) # configuration will be taken from the JOSvc at P1
70  # remove filenames to avoid duplicates
71  mod( properties, "LVL1ConfigSvc", "HLTJsonFileName", lambda x : "None" )
72  mod( properties, "LVL1ConfigSvc", "L1JsonFileName", lambda x : "None" )
73  mod( properties, "TrigConf__BunchGroupCondAlg", "Filename", lambda x : "None" )
74  mod( properties, "HLTConfigSvc", "HLTJsonFileName", lambda x : "None" )
75  mod( properties, "HLTConfigSvc", "L1JsonFileName", lambda x : "None" )
76  mod( properties, "HLTConfigSvc", "MonitoringJsonFileName", lambda x : "None" )
77  mod( properties, "HLTPrescaleCondAlg", "Filename", lambda x : "None" )
78 
79  with open(db_file,'w') as f:
80  json.dump(jocat, f, indent=4, sort_keys=True, ensure_ascii=True)
81 
82 
83 if __name__ == "__main__":
84  import os, sys
85  if len(sys.argv)!=2:
86  print("Syntax: %s HLTJobOptions.[pkl,json]" % sys.argv[0].split("/")[-1])
87  print(" .pkl: convert to JSON and DB JSON")
88  print(" .json: convert to DB JSON")
89  sys.exit(1)
90 
91  fname = sys.argv[1]
92  ext = os.path.splitext(fname)[1]
93  if ext == '.json':
94  with open(fname) as fh:
95  hlt_json = json.load( fh )
96  properties = hlt_json["properties"]
97  modifyConfigForP1(fname, fname.replace("json", "db.json"))
98  elif ext == '.pkl':
99  create_joboptions_json(fname, fname.replace("pkl","json"))
100  else:
101  print("Unkown file format")
102  sys.exit(1)
maskDeadModules.mod
mod
Definition: maskDeadModules.py:36
python.JsonUtils.create_joboptions_json
def create_joboptions_json(pkl_file, json_file, createDBJson=True)
Definition: JsonUtils.py:9
python.JsonUtils.modifyConfigForP1
def modifyConfigForP1(json_file, db_file)
Definition: JsonUtils.py:46
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
Trk::open
@ open
Definition: BinningType.h:40
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
Trk::split
@ split
Definition: LayerMaterialProperties.h:38