ATLAS Offline Software
Loading...
Searching...
No Matches
AthHLT.py
Go to the documentation of this file.
1# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2#
3# Utilities used in athenaHLT.py
4#
5from typing import Any
6from AthenaCommon.Logging import logging
7from pycrest.api.crest_api import CrestApi
8log = logging.getLogger('athenaHLT')
9
10from functools import cache
11import os
12import sys
13
14class CondDB:
15 _run2 = 236108
16 def __init__(self, run):
17 self.run = run
18 def db_instance(self):
19 if self.run>=self._run2:
20 return 'CONDBR2'
21 else:
22 return 'COMP200'
23 def sor_folder(self):
24 if self.run>=self._run2:
25 return '/TDAQ/RunCtrl/SOR'
26 else:
27 return '/TDAQ/RunCtrl/SOR_Params'
28
29@cache
30def get_sor_params(run_number) -> dict[str, Any] | None:
31 from CoolConvUtilities import AtlCoolLib
32
33 log.info('Reading SOR record for run %s from COOL', run_number)
34
35 cdb = CondDB(run_number)
36 dbcon = AtlCoolLib.readOpen('COOLONL_TDAQ/%s' % cdb.db_instance())
37 folder = dbcon.getFolder(cdb.sor_folder()) # type: ignore
38
39 # need to keep sor variable while using payload (cannot do the following in
40 # one single line nor overwrite sor). Otherwise: 1) GC comes into play;
41 # 2) the object is deleted; 3) since it's a shared_ptr, the internal
42 # cool::IObject also gets deleted; 4) payload is not valid any longer
43 try:
44 sor = folder.findObject(run_number << 32, 0)
45 except Exception:
46 return None # This can happen for unknown run numbers
47
48 payload = sor.payload()
49 d = {k: payload[k] for k in payload}
50 return d
51
52@cache
53def get_eor_params_crest(run_number, crest_server:str) -> dict[str, Any] | None:
54 from TrigConfStorage.TriggerCrestUtil import TriggerCrestUtil
55 log.info('Reading EOR record for run %s from Crest', run_number)
56 return TriggerCrestUtil.getEORParams(run_number, server=crest_server)
57
58@cache
59def get_trigconf_keys(run_number, lb_number):
60 """Read Trigger keys from COOL"""
61
62 from TrigConfStorage.TriggerCoolUtil import TriggerCoolUtil
63 confKeys: dict[str, Any] = TriggerCoolUtil.getTrigConfKeys(run_number, lb_number)
64 confKeys['DBAlias'] = confKeys.pop('DB', None)
65 return confKeys
66
67
68@cache
69def get_trigconf_keys_crest(run_number, lb_number, crest_server):
70 """Read Trigger keys from CREST"""
71 from TrigConfStorage.TriggerCrestUtil import TriggerCrestUtil
72 log.info("Using CREST server %s", crest_server)
73 api: CrestApi = TriggerCrestUtil.getCrestApi(server=crest_server)
74 confKeys: dict[str, Any] = TriggerCrestUtil.getTrigConfKeys(run_number, lb_number, api=api)
75 confKeys['DBAlias'] = confKeys.pop('DB', None)
76 return confKeys
77
78def getCACfg(jopath):
79 """Return the CA Cfg function based on joboptions path.
80 The format is MODULE[.FNC]. If no FNC is given, 'main' will be tried."""
81
82 import importlib
83
84 sys.path.append('.') # temporarily add local directory to search path
85
86 # try to import module as given:
87 try:
88 module = importlib.import_module(jopath)
89 except ModuleNotFoundError:
90 if '.' not in jopath:
91 raise
92 # or interpret as module.fnc:
93 mod_name, fnc_name = jopath.rsplit('.', maxsplit=1)
94 module = importlib.import_module(mod_name)
95 else:
96 # if the first import worked we are using the 'main(flags)' function:
97 fnc_name = 'main'
98
99 sys.path.pop()
100
101 log.info("Loading %s.%s", module.__name__, fnc_name)
102 return getattr(module, fnc_name)
103
104
105def reload_from_json(filename, suppress_args=[]):
106 """Re-launch athenaHLT from the given json file. Optionally suppress
107 the list of command line args (e.g. flags)."""
108
109 # Remove all command line args that are not compatible with running from JSON:
110 argv = []
111 for arg_index, arg in enumerate(sys.argv):
112 if arg == '--dump-config-reload':
113 continue
114 if arg in ['--precommand', '-c', '--postcommand', '-C']:
115 continue
116 if arg_index > 0 and sys.argv[arg_index-1] in ['--precommand', '-c', '--postcommand', '-C']:
117 continue
118 if arg.startswith('--precommand') or arg.startswith('--postcommand'):
119 continue
120 if arg in suppress_args:
121 continue
122 argv.append(arg)
123
124 argv[-1] = filename
125 log.info('Restarting %s from %s ...', argv[0], argv[-1])
126 sys.stdout.flush()
127 sys.stderr.flush()
128 os.execvp(argv[0], argv)
129
130
131#
132# Testing (used as ctest)
133#
134if __name__=='__main__':
135 # Can be used as script, e.g.: python -m TrigCommon.AthHLT 327265
136 if len(sys.argv)>1:
137 log.info('SOR parameters: %s', get_sor_params(int(sys.argv[1])))
138 sys.exit(0)
139
140 # Unit testing case:
141 d = get_sor_params(327265) # Run-2
142 assert(d is not None)
143 if d is not None:
144 print(d)
145 assert(d['DetectorMask']=='0000000000000000c10069fffffffff7')
146
147 d = get_eor_params_crest(327265, "https://crest.cern.ch/api-v5.0") # Run-2
148 assert(d is not None)
149 if d is not None:
150 print(d)
151 assert(d['DetectorMask']=='0000000000000000c10069fffffffff7')
152
153 d = get_sor_params(216416) # Run-1
154 assert(d is not None)
155 if d is not None:
156 print(d)
157 assert(d['DetectorMask']==281474976710647)
158
159 # Config keys
160 d = get_trigconf_keys(360026, 1)
161 print(d)
162 assert(d['SMK']==2749)
163 assert(d['LVL1PSK']==15186)
164 assert(d['HLTPSK']==17719)
165
166 # Config keys crest
167 d = get_trigconf_keys_crest(360026, 1, "https://crest.cern.ch/api-v5.0")
168 print(d)
169 assert(d['SMK']==2749)
170 assert(d['LVL1PSK']==15186)
171 assert(d['HLTPSK']==17719)
172
173 d = get_trigconf_keys(360026, 100)
174 print(d)
175 assert(d['SMK']==2749)
176 assert(d['LVL1PSK']==23504)
177 assert(d['HLTPSK']==17792)
void print(char *figname, TCanvas *c1)
__init__(self, run)
Definition AthHLT.py:16
dict[str, Any]|None get_eor_params_crest(run_number, str crest_server)
Definition AthHLT.py:53
get_trigconf_keys_crest(run_number, lb_number, crest_server)
Definition AthHLT.py:69
getCACfg(jopath)
Definition AthHLT.py:78
get_trigconf_keys(run_number, lb_number)
Definition AthHLT.py:59
reload_from_json(filename, suppress_args=[])
Definition AthHLT.py:105
dict[str, Any]|None get_sor_params(run_number)
Definition AthHLT.py:30