ATLAS Offline Software
Loading...
Searching...
No Matches
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
7import os.path
8import json
9from TriggerMenuMT.TriggerAPI.TriggerInfo import TriggerInfo, TriggerChain
10from TriggerMenuMT.TriggerAPI.TriggerEnums import TriggerPeriod
11from PathResolver import PathResolver
12from AthenaCommon.Logging import logging
13log = logging.getLogger(__name__)
14privateJsonFile = "TriggerInfo.json"
15
16def dump(db, f=privateJsonFile):
17 dumpJson(db, f)
18
19def 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
44def 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
49def loadJson(jsonFile):
50 with open(jsonFile) as f:
51 dct = json.load(f)
52 dct = fromJsonDump(dct)
53 return dct
54
55def 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
75def 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
80class 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)
static std::string FindCalibFile(const std::string &logical_file_name)
This class contains trigger related information.
Definition TriggerInfo.h:77
-event-from-file
load(centralFile="TriggerMenu/TriggerInfo_20210302.json")