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=[], jobOptions=None):
106 """Re-launch athenaHLT from the given json file. Optionally suppress
107 the list of command line args (e.g. flags).
108 jobOptions: the positional argument to be replaced by `filename`."""
109
110 # Remove all command line args that are not compatible with running from JSON:
111 argv = []
112 for arg_index, arg in enumerate(sys.argv):
113 if arg == '--dump-config-reload':
114 continue
115 if arg in ['--precommand', '-c', '--postcommand', '-C']:
116 continue
117 if arg_index > 0 and sys.argv[arg_index-1] in ['--precommand', '-c', '--postcommand', '-C']:
118 continue
119 if arg.startswith('--precommand') or arg.startswith('--postcommand'):
120 continue
121 if arg in suppress_args:
122 continue
123 argv.append(arg)
124
125 if jobOptions is not None and jobOptions in argv:
126 argv[argv.index(jobOptions)] = filename
127 else:
128 argv[-1] = filename
129 log.info('Restarting %s from %s ...', argv[0], filename)
130 sys.stdout.flush()
131 sys.stderr.flush()
132 os.execvp(argv[0], argv)
133
134
135#
136# Testing (used as ctest)
137#
138if __name__=='__main__':
139 # Can be used as script, e.g.: python -m TrigCommon.AthHLT 327265
140 if len(sys.argv)>1:
141 log.info('SOR parameters: %s', get_sor_params(int(sys.argv[1])))
142 sys.exit(0)
143
144 # Unit testing case:
145 d = get_sor_params(327265) # Run-2
146 assert(d is not None)
147 if d is not None:
148 print(d)
149 assert(d['DetectorMask']=='0000000000000000c10069fffffffff7')
150
151 d = get_eor_params_crest(327265, "https://crest.cern.ch/api-v5.0") # Run-2
152 assert(d is not None)
153 if d is not None:
154 print(d)
155 assert(d['DetectorMask']=='0000000000000000c10069fffffffff7')
156
157 d = get_sor_params(216416) # Run-1
158 assert(d is not None)
159 if d is not None:
160 print(d)
161 assert(d['DetectorMask']==281474976710647)
162
163 # Config keys
164 d = get_trigconf_keys(360026, 1)
165 print(d)
166 assert(d['SMK']==2749)
167 assert(d['LVL1PSK']==15186)
168 assert(d['HLTPSK']==17719)
169
170 # Config keys crest
171 d = get_trigconf_keys_crest(360026, 1, "https://crest.cern.ch/api-v5.0")
172 print(d)
173 assert(d['SMK']==2749)
174 assert(d['LVL1PSK']==15186)
175 assert(d['HLTPSK']==17719)
176
177 d = get_trigconf_keys(360026, 100)
178 print(d)
179 assert(d['SMK']==2749)
180 assert(d['LVL1PSK']==23504)
181 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
reload_from_json(filename, suppress_args=[], jobOptions=None)
Definition AthHLT.py:105
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
dict[str, Any]|None get_sor_params(run_number)
Definition AthHLT.py:30