3 from typing
import Any, Optional
4 from AthenaConfiguration.AccumulatorCache
import AccumulatorCache
5 from AthenaCommon.Logging
import logging
6 log = logging.getLogger(
"TriggerConfigAccess.py" )
8 from .TrigConfigSvcCfg
import getTrigConfigFromFlag, getL1MenuFileName, getHLTMenuFileName, getL1PrescalesSetFileName, getHLTPrescalesSetFileName, getBunchGroupSetFileName, getHLTJobOptionsFileName, getHLTMonitoringFileName
10 from TrigConfIO.L1TriggerConfigAccess
import L1MenuAccess, L1PrescalesSetAccess, BunchGroupSetAccess
11 from TrigConfIO.HLTTriggerConfigAccess
import HLTMenuAccess, HLTPrescalesSetAccess, HLTJobOptionsAccess, HLTMonitoringAccess
13 from AthenaConfiguration.AutoConfigFlags
import GetFileMD
14 from AthenaConfiguration.Enums
import Format
16 from functools
import lru_cache
19 Access to the trigger configuration in python is provided depending on
20 the trigger configuration source
22 The tc source is taken from the TriggerFlag triggerConfig
24 1) tc source is set to INFILE
26 This is usually the case when running on ESD, AOD, and dAOD files and
27 only in this case. An exception is RDO with trigger information in
28 MC. The menu and prescales are taken from the pool file, from the
32 2) tc source is set to FILE
34 This is the case when executing the trigger with the configuration
35 taken from file. The filename is provided by the function
38 3) tc source is set to DB
40 This is the case when executing the trigger from the DB. The DB
41 connection and keys are provided by the triggerConfig flag
45 This is the case when reconstructing the data. From COOL the
46 configuration keys and db alias are taken, the configurations
47 are then loaded from the DB.
51 @lru_cache(maxsize=
None)
53 """Return dictionary of trigger keys for given run and lumiblock number
55 from TrigConfStorage.TriggerCoolUtil
import TriggerCoolUtil
56 condb =
"CONDBR2" if runNr > 236108
else "COMP200"
57 db = TriggerCoolUtil.GetConnection(condb)
58 run_range = [[runNr,runNr]]
59 d = { k: TriggerCoolUtil.getHLTConfigKeys(db, run_range)[runNr][k]
for k
in [
'SMK',
'DB'] }
60 for ( key, lbfirst, lblast)
in TriggerCoolUtil.getBunchGroupKey(db, run_range)[runNr][
'BGKey']:
61 if lbNr>=lbfirst
and (lbNr<=lblast
or lblast==-1):
64 for ( key, lbfirst, lblast)
in TriggerCoolUtil.getL1ConfigKeys(db, run_range)[runNr][
'LVL1PSK']:
65 if lbNr>=lbfirst
and (lbNr<=lblast
or lblast==-1):
68 for ( key, lbfirst, lblast)
in TriggerCoolUtil.getHLTPrescaleKeys(db, run_range)[runNr][
'HLTPSK2']:
69 if lbNr>=lbfirst
and (lbNr<=lblast
or lblast==-1):
74 dbaliasMapping = {
"TRIGGERDBR2R" :
"TRIGGERDB",
75 "TRIGGERDBV2" :
"TRIGGERDB_RUN1" }
76 if d[
"DB"]
in dbaliasMapping:
77 d[
"DB"] = dbaliasMapping[ d[
"DB"] ]
82 """Provides access to the database keys from the in-file metadata
84 Gets the database keys from the in-file metadata which are stored together with the json representation
85 If the keys are in the file, then usually SMK, L1PSK and HLTPSK are present.
86 The bunchgroupset is not stored in the metadata, so 0 is returned for the BGS key for completeness.
88 @returns: dictionary with the DB keys. Returns 'None' if information is not present.
91 keys = metadata.get(
"TriggerConfigInfo",
None)
95 'SMK': keys[
'HLT'][
'key']
if 'HLT' in keys
else 0,
96 'L1PSK': keys[
'L1PS'][
'key']
if 'L1PS' in keys
else 0,
97 'HLTPSK': keys[
'HLTPS'][
'key']
if 'HLTPSK' in keys
else 0,
102 Returns a string-serialised JSON object from the metadata store.
103 Checks AOD syntax first, then fully-qualified ESD syntax
106 if flags.Input.Format != Format.POOL:
107 raise RuntimeError(
"Cannot read trigger configuration (%s) from input type %s", key, flags.Input.Format)
108 from AthenaConfiguration.AutoConfigFlags
import GetFileMD
110 menu_json = metadata.get(key,
None)
111 if menu_json
is None:
112 menu_json = metadata.get(
'DataVector<xAOD::TriggerMenuJson_v1>_%s' % key,
None)
113 if menu_json
is None:
114 if key ==
'TriggerMenuJson_HLTMonitoring':
116 log.info(
"Trigger metadata with key 'TriggerMenuJson_HLTMonitoring' is not available in this file. This feature is currently being introduced.")
119 raise RuntimeError(
"Cannot read trigger configuration (%s) from input file metadata" % key)
131 if tc[
"SOURCE"] ==
"FILE":
133 elif tc[
"SOURCE"] ==
"COOL":
134 """This is the case when reconstructing the data."""
135 if len(flags.Input.RunNumbers) == 0:
136 raise RuntimeError(
"No run number available in input metadata")
138 cfg = L1MenuAccess( dbalias = keysFromCool[
"DB"], smkey = keysFromCool[
'SMK'] )
139 elif tc[
"SOURCE"] ==
"DB":
140 cfg = L1MenuAccess( dbalias = tc[
"DBCONN"], smkey = tc[
"SMK"] )
141 elif tc[
"SOURCE"] ==
"INFILE":
144 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
151 if tc[
"SOURCE"] ==
"FILE":
153 elif tc[
"SOURCE"] ==
"COOL":
154 """This is the case when reconstructing the data."""
155 if len(flags.Input.RunNumbers) == 0:
156 raise RuntimeError(
"No run number available in input metadata")
158 cfg = L1PrescalesSetAccess( dbalias = keysFromCool[
"DB"], l1pskey = keysFromCool[
'L1PSK'] )
159 elif tc[
"SOURCE"] ==
"DB":
160 cfg = L1PrescalesSetAccess( dbalias = tc[
"DBCONN"], l1pskey = tc[
"L1PSK"] )
161 elif tc[
"SOURCE"] ==
"INFILE":
164 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
171 if tc[
"SOURCE"] ==
"FILE":
173 elif tc[
"SOURCE"] ==
"COOL":
174 """This is the case when reconstructing the data."""
175 if len(flags.Input.RunNumbers) == 0:
176 raise RuntimeError(
"No run number available in input metadata")
178 cfg = BunchGroupSetAccess( dbalias = keysFromCool[
"DB"], bgskey = keysFromCool[
'BGSK'] )
179 elif tc[
"SOURCE"] ==
"DB":
180 cfg = BunchGroupSetAccess( dbalias = tc[
"DBCONN"], bgskey = tc[
"BGSK"] )
181 elif tc[
"SOURCE"] ==
"INFILE":
182 if flags.Input.Format != Format.POOL:
183 raise RuntimeError(f
"Cannot read trigger configuration (Bunchgroup Set) from input type {flags.Input.Format}")
184 raise NotImplementedError(
"Python access to the trigger configuration (Bunchgroup Set) from in-file metadata not yet implemented")
186 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
198 if tc[
"SOURCE"] ==
"FILE":
200 elif tc[
"SOURCE"] ==
"COOL":
201 """This is the case when reconstructing the data."""
202 if len(flags.Input.RunNumbers) == 0:
203 raise RuntimeError(
"No run number available in input metadata")
205 cfg = HLTMenuAccess( dbalias = keysFromCool[
"DB"], smkey = keysFromCool[
'SMK'] )
206 elif tc[
"SOURCE"] ==
"DB":
207 cfg = HLTMenuAccess( dbalias = tc[
"DBCONN"], smkey = tc[
"SMK"] )
208 elif tc[
"SOURCE"] ==
"INFILE":
211 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
218 if tc[
"SOURCE"] ==
"FILE":
220 elif tc[
"SOURCE"] ==
"COOL":
221 """This is the case when reconstructing the data."""
222 if len(flags.Input.RunNumbers) == 0:
223 raise RuntimeError(
"No run number available in input metadata")
225 cfg = HLTPrescalesSetAccess( dbalias = keysFromCool[
"DB"], hltpskey = keysFromCool[
'HLTPSK'] )
226 elif tc[
"SOURCE"] ==
"DB":
227 cfg = HLTPrescalesSetAccess( dbalias = tc[
"DBCONN"], hltpskey = tc[
"HLTPSK"] )
228 elif tc[
"SOURCE"] ==
"INFILE":
229 cfg = HLTPrescalesSetAccess(jsonString=
_getJSONFromMetadata(flags, key=
'TriggerMenuJson_HLTPS'))
231 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
238 if tc[
"SOURCE"] ==
"FILE":
240 elif tc[
"SOURCE"] ==
"COOL":
241 """This is the case when reconstructing the data."""
242 if len(flags.Input.RunNumbers) == 0:
243 raise RuntimeError(
"No run number available in input metadata")
245 cfg = HLTJobOptionsAccess( dbalias = keysFromCool[
"DB"], smkey = keysFromCool[
'SMK'] )
246 elif tc[
"SOURCE"] ==
"DB":
247 cfg = HLTJobOptionsAccess( dbalias = tc[
"DBCONN"], smkey = tc[
"SMK"] )
248 elif tc[
"SOURCE"] ==
"INFILE":
249 raise NotImplementedError(
"Python access to the HLT Job Options configuration from in-file metadata is NOT SUPPORTED (this file is huge!)")
251 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
258 if tc[
"SOURCE"] ==
"FILE":
260 elif tc[
"SOURCE"] ==
"COOL":
261 """This is the case when reconstructing the data."""
262 if len(flags.Input.RunNumbers) == 0:
263 raise RuntimeError(
"No run number available in input metadata")
265 cfg = HLTMonitoringAccess( dbalias = keysFromCool[
"DB"], smkey = keysFromCool[
'SMK'] )
266 elif tc[
"SOURCE"] ==
"DB":
267 cfg = HLTMonitoringAccess( dbalias = tc[
"DBCONN"], smkey = tc[
"SMK"] )
268 elif tc[
"SOURCE"] ==
"INFILE":
270 if jsonHLTMon
is not None:
271 cfg = HLTMonitoringAccess(jsonString=jsonHLTMon)
274 smkey = keysFromInfileMD[
'SMK']
if keysFromInfileMD
is not None else 0
275 if smkey < 3000
and not flags.Input.isMC:
277 log.info(
"Trigger metadata with key 'TriggerMenuJson_HLTMonitoring' is not available for Run 2 data. Returning empty dummy.")
278 jsonHLTMon =
'{"filetype": "hltmonitoringsummary","name": "EmptyDefault", "signatures": {}}'
279 cfg = HLTMonitoringAccess(jsonString=jsonHLTMon)
283 log.info(
"Falling back on reading the HLTMonitoring from the TRIGGERDB_RUN3 for SMK %i.", smkey)
284 cfg = HLTMonitoringAccess( dbalias =
"TRIGGERDB_RUN3", smkey = smkey )
287 log.info(
"Trigger HLTMonitoring is not available for SMK %i. Returning empty dummy.", smkey)
288 jsonHLTMon =
'{"filetype": "hltmonitoringsummary","name": "EmptyDefault", "signatures": {}}'
289 cfg = HLTMonitoringAccess(jsonString=jsonHLTMon)
292 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])