ATLAS Offline Software
SerializeAPI.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 __author__ = 'Javier Montejo'
4 __version__="$Revision: 2.0 $"
5 __doc__="Interface to retrieve lists of unprescaled triggers according to types and periods"
6 
7 import os.path
8 import json
9 from TriggerMenuMT.TriggerAPI.TriggerInfo import TriggerInfo, TriggerChain
10 from TriggerMenuMT.TriggerAPI.TriggerEnums import TriggerPeriod
11 from PathResolver import PathResolver
12 from AthenaCommon.Logging import logging
13 log = logging.getLogger(__name__)
14 privateJsonFile = "TriggerInfo.json"
15 
16 def dump(db, f=privateJsonFile):
17  dumpJson(db, f)
18 
19 def load(centralFile="TriggerMenu/TriggerInfo_20210302.json"):
20  if centralFile:
21  centralJsonFile = PathResolver.FindCalibFile(centralFile)
22  if centralJsonFile:
23  log.info("Found json file:"+centralJsonFile)
24  centralJsonFile = os.path.realpath(centralJsonFile)
25  else: log.error("Couldn't find central json file")
26 
27  if centralJsonFile:
28  try:
29  db = loadJson(centralJsonFile)
30  except (json.JSONDecodeError, ValueError):
31  log.info("Reading cached information failed")
32  db = {}
33  else:
34  db = {}
35  try:
36  privatedb = loadJson(privateJsonFile)
37  db.update(privatedb)
38  except (json.JSONDecodeError, ValueError):
39  log.error("Error loading the private json file")
40  except IOError:
41  pass
42  return db
43 
44 def dumpJson(db, jsonFile):
45  with open(jsonFile, "w") as f:
46  log.info("Dumping cached information to: "+jsonFile)
47  json.dump( toJsonDump(db) , f, cls=TriggerAPIEncoder)
48 
49 def loadJson(jsonFile):
50  with open(jsonFile) as f:
51  dct = json.load(f)
52  dct = fromJsonDump(dct)
53  return dct
54 
55 def fromJsonDump(thelist):
56  #Rebuild objects and dictionary structure from json
57  toret = {}
58  for key,thedict in thelist:
59  pk, grl = key
60  pk = TriggerPeriod.fromName( pk )
61  key = (pk, grl)
62  triggerinfo = TriggerInfo()
63  triggerinfo.period = TriggerPeriod.fromName( thedict["period"] )
64  #key and internal period value should be consistent
65  assert triggerinfo.period==pk, (triggerinfo.period, pk)
66  triggerinfo.totalLB = thedict["totalLB"]
67  triggerinfo.totalLBByRun = thedict.get("totalLBByRun",{})
68  triggerinfo.triggerChains = []
69  for tc in thedict["triggerChains"]:
70  tc = TriggerChain(tc["name"],tc["l1seed"],tc["livefraction"],tc["activeLB"],tc["hasRerun"],tc.get("activeLBByRun",{}))
71  triggerinfo.triggerChains.append(tc)
72  toret[key] = triggerinfo
73  return toret
74 
75 def toJsonDump(dct):
76  #tuple keys are not allowed, convert to list of pairs
77  #Dump the enum name instead of the value to allow flexibility in the future
78  return [ ((TriggerPeriod.toName(p) ,grl ),v) for (p,grl),v in dct.items()]
79 
80 class TriggerAPIEncoder(json.JSONEncoder):
81  def default(self, obj):
82  if hasattr(obj,'toJSON'):
83  return obj.toJSON()
84  else:
85  return json.JSONEncoder.default(self, obj)
PathResolver::FindCalibFile
static std::string FindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.h:108
python.TriggerAPI.SerializeAPI.dump
def dump(db, f=privateJsonFile)
Definition: SerializeAPI.py:16
python.TriggerAPI.SerializeAPI.load
def load(centralFile="TriggerMenu/TriggerInfo_20210302.json")
Definition: SerializeAPI.py:19
python.TriggerAPI.SerializeAPI.TriggerAPIEncoder
Definition: SerializeAPI.py:80
python.TriggerAPI.SerializeAPI.TriggerAPIEncoder.default
def default(self, obj)
Definition: SerializeAPI.py:81
Trk::open
@ open
Definition: BinningType.h:40
python.TriggerAPI.SerializeAPI.fromJsonDump
def fromJsonDump(thelist)
Definition: SerializeAPI.py:55
python.TriggerAPI.SerializeAPI.dumpJson
def dumpJson(db, jsonFile)
Definition: SerializeAPI.py:44
python.TriggerAPI.SerializeAPI.toJsonDump
def toJsonDump(dct)
Definition: SerializeAPI.py:75
python.TriggerAPI.SerializeAPI.loadJson
def loadJson(jsonFile)
Definition: SerializeAPI.py:49