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.
52 if flags.Trigger.useCrest:
53 confKeys: dict[str, str | int] =
getKeysFromCrest(runNr, lbNr, flags.Trigger.crestServer)
59 "TRIGGERDBR2R" :
"TRIGGERDB",
60 "TRIGGERDBV2" :
"TRIGGERDB_RUN1"
62 if confKeys[
"DB"]
in dbaliasMapping:
63 confKeys[
"DB"] = dbaliasMapping[ confKeys[
"DB"] ]
67 @lru_cache(maxsize=
None)
68 def getKeysFromCrest(runNr: int, lbNr: int, crest_server: str) -> dict[str, str | int]:
69 """Return dictionary of trigger keys for given run and lumiblock number from crest
71 from TrigConfStorage.TriggerCrestUtil
import TriggerCrestUtil
72 return TriggerCrestUtil.getTrigConfKeys(runNumber=runNr, lumiBlock=lbNr, server=crest_server)
74 @lru_cache(maxsize=
None)
76 """Return dictionary of trigger keys for given run and lumiblock number
78 from TrigConfStorage.TriggerCoolUtil
import TriggerCoolUtil
79 return TriggerCoolUtil.getTrigConfKeys(runNumber=runNr, lumiBlock=lbNr)
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 Example of the 'TriggerConfigInfo' metadata entry (run 453713, lb 1416):
90 'HLT': {'key': 3221, 'name': 'PhysicsP1_pp_run3_v1'},
91 'HLTPS': {'key': 6247, 'name': 'PhysicsP1_pp_run3_v1'},
92 'L1': {'key': 3221, 'name': 'Physics_pp_run3_v1'},
93 'L1PS': {'key': 7475, 'name': 'Physics_pp_run3_v1'}
95 @returns: dictionary with the DB keys. Returns 'None' if information is not present.
98 keys = metadata.get(
"TriggerConfigInfo",
None)
102 'SMK': keys[
'HLT'][
'key']
if 'HLT' in keys
else 0,
103 'LVL1PSK': keys[
'L1PS'][
'key']
if 'L1PS' in keys
else 0,
104 'HLTPSK': keys[
'HLTPS'][
'key']
if 'HLTPS' in keys
else 0,
109 Returns a string-serialised JSON object from the metadata store.
110 Checks AOD syntax first, then fully-qualified ESD syntax
113 if flags.Input.Format != Format.POOL:
114 raise RuntimeError(
"Cannot read trigger configuration (%s) from input type %s", key, flags.Input.Format)
115 from AthenaConfiguration.AutoConfigFlags
import GetFileMD
117 menu_json = metadata.get(key,
None)
118 if menu_json
is None:
119 menu_json = metadata.get(
'DataVector<xAOD::TriggerMenuJson_v1>_%s' % key,
None)
120 if menu_json
is None:
121 if key ==
'TriggerMenuJson_HLTMonitoring':
123 log.info(
"Trigger metadata with key 'TriggerMenuJson_HLTMonitoring' is not available in this file. This feature is currently being introduced.")
126 raise RuntimeError(
"Cannot read trigger configuration (%s) from input file metadata" % key)
138 if tc[
"SOURCE"] ==
"FILE":
140 elif tc[
"SOURCE"] ==
"COOL":
141 """This is the case when reconstructing the data."""
142 if len(flags.Input.RunNumbers) == 0:
143 raise RuntimeError(
"No run number available in input metadata")
145 cfg = L1MenuAccess( dbalias = keysFromConditions[
"DB"], smkey = keysFromConditions[
'SMK'] )
146 elif tc[
"SOURCE"] ==
"DB":
147 cfg = L1MenuAccess( dbalias = tc[
"DBCONN"], smkey = tc[
"SMK"] )
148 elif tc[
"SOURCE"] ==
"INFILE":
151 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
158 if tc[
"SOURCE"] ==
"FILE":
160 elif tc[
"SOURCE"] ==
"COOL":
161 """This is the case when reconstructing the data."""
162 if len(flags.Input.RunNumbers) == 0:
163 raise RuntimeError(
"No run number available in input metadata")
165 cfg = L1PrescalesSetAccess( dbalias = keysFromConditions[
"DB"], l1pskey = keysFromConditions[
'LVL1PSK'] )
166 elif tc[
"SOURCE"] ==
"DB":
167 cfg = L1PrescalesSetAccess( dbalias = tc[
"DBCONN"], l1pskey = tc[
"LVL1PSK"] )
168 elif tc[
"SOURCE"] ==
"INFILE":
171 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
178 if tc[
"SOURCE"] ==
"FILE":
180 elif tc[
"SOURCE"] ==
"COOL":
181 """This is the case when reconstructing the data."""
182 if len(flags.Input.RunNumbers) == 0:
183 raise RuntimeError(
"No run number available in input metadata")
185 cfg = BunchGroupSetAccess( dbalias = keysFromConditions[
"DB"], bgskey = keysFromConditions[
'BGSK'] )
186 elif tc[
"SOURCE"] ==
"DB":
187 cfg = BunchGroupSetAccess( dbalias = tc[
"DBCONN"], bgskey = tc[
"BGSK"] )
188 elif tc[
"SOURCE"] ==
"INFILE":
189 if flags.Input.Format != Format.POOL:
190 raise RuntimeError(f
"Cannot read trigger configuration (Bunchgroup Set) from input type {flags.Input.Format}")
191 raise NotImplementedError(
"Python access to the trigger configuration (Bunchgroup Set) from in-file metadata not yet implemented")
193 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
205 if tc[
"SOURCE"] ==
"FILE":
207 elif tc[
"SOURCE"] ==
"COOL":
208 """This is the case when reconstructing the data."""
209 if len(flags.Input.RunNumbers) == 0:
210 raise RuntimeError(
"No run number available in input metadata")
212 cfg = HLTMenuAccess( dbalias = keysFromConditions[
"DB"], smkey = keysFromConditions[
'SMK'] )
213 elif tc[
"SOURCE"] ==
"DB":
214 cfg = HLTMenuAccess( dbalias = tc[
"DBCONN"], smkey = tc[
"SMK"] )
215 elif tc[
"SOURCE"] ==
"INFILE":
218 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
225 if tc[
"SOURCE"] ==
"FILE":
227 elif tc[
"SOURCE"] ==
"COOL":
228 """This is the case when reconstructing the data."""
229 if len(flags.Input.RunNumbers) == 0:
230 raise RuntimeError(
"No run number available in input metadata")
232 cfg = HLTPrescalesSetAccess( dbalias = keysFromConditions[
"DB"], hltpskey = keysFromConditions[
'HLTPSK'] )
233 elif tc[
"SOURCE"] ==
"DB":
234 cfg = HLTPrescalesSetAccess( dbalias = tc[
"DBCONN"], hltpskey = tc[
"HLTPSK"] )
235 elif tc[
"SOURCE"] ==
"INFILE":
236 cfg = HLTPrescalesSetAccess(jsonString=
_getJSONFromMetadata(flags, key=
'TriggerMenuJson_HLTPS'))
238 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
245 if tc[
"SOURCE"] ==
"FILE":
247 elif tc[
"SOURCE"] ==
"COOL":
248 """This is the case when reconstructing the data."""
249 if len(flags.Input.RunNumbers) == 0:
250 raise RuntimeError(
"No run number available in input metadata")
252 cfg = HLTJobOptionsAccess( dbalias = keysFromConditions[
"DB"], smkey = keysFromConditions[
'SMK'] )
253 elif tc[
"SOURCE"] ==
"DB":
254 cfg = HLTJobOptionsAccess( dbalias = tc[
"DBCONN"], smkey = tc[
"SMK"] )
255 elif tc[
"SOURCE"] ==
"INFILE":
256 raise NotImplementedError(
"Python access to the HLT Job Options configuration from in-file metadata is NOT SUPPORTED (this file is huge!)")
258 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
265 if tc[
"SOURCE"] ==
"FILE":
267 elif tc[
"SOURCE"] ==
"COOL":
268 """This is the case when reconstructing the data."""
269 if len(flags.Input.RunNumbers) == 0:
270 raise RuntimeError(
"No run number available in input metadata")
272 cfg = HLTMonitoringAccess( dbalias = keysFromConditions[
"DB"], smkey = keysFromConditions[
'SMK'] )
273 elif tc[
"SOURCE"] ==
"DB":
274 cfg = HLTMonitoringAccess( dbalias = tc[
"DBCONN"], smkey = tc[
"SMK"] )
275 elif tc[
"SOURCE"] ==
"INFILE":
277 if jsonHLTMon
is not None:
278 cfg = HLTMonitoringAccess(jsonString=jsonHLTMon)
281 smkey = keysFromInfileMD[
'SMK']
if keysFromInfileMD
is not None else 0
282 if smkey < 3000
and not flags.Input.isMC:
284 log.info(
"Trigger metadata with key 'TriggerMenuJson_HLTMonitoring' is not available for Run 2 data. Returning empty dummy.")
285 jsonHLTMon =
'{"filetype": "hltmonitoringsummary","name": "EmptyDefault", "signatures": {}}'
286 cfg = HLTMonitoringAccess(jsonString=jsonHLTMon)
290 log.info(
"Falling back on reading the HLTMonitoring from the TRIGGERDB_RUN3 for SMK %i.", smkey)
291 cfg = HLTMonitoringAccess( dbalias =
"TRIGGERDB_RUN3", smkey = smkey )
294 log.info(
"Trigger HLTMonitoring is not available for SMK %i. Returning empty dummy.", smkey)
295 jsonHLTMon =
'{"filetype": "hltmonitoringsummary","name": "EmptyDefault", "signatures": {}}'
296 cfg = HLTMonitoringAccess(jsonString=jsonHLTMon)
299 raise RuntimeError(
"Unknown source of trigger configuration: %s" % tc[
"SOURCE"])
303 if filterOnActiveChains:
306 for sig
in cfg[
"signatures"]:
307 for chain
in list(cfg[
"signatures"][sig]):
308 if chain
in hltPs.chainNames()
and not hltPs.enabled(chain):
309 del cfg[
"signatures"][sig][chain]