3from typing
import Any, Optional
4from AthenaConfiguration.AccumulatorCache
import AccumulatorCache
5from AthenaCommon.Logging
import logging
6log = logging.getLogger(
"TriggerConfigAccess.py" )
8from .TrigConfigSvcCfg
import getTrigConfigFromFlag, getL1MenuFileName, getHLTMenuFileName, getL1PrescalesSetFileName, getHLTPrescalesSetFileName, getBunchGroupSetFileName, getHLTJobOptionsFileName, getHLTMonitoringFileName
10from TrigConfIO.L1TriggerConfigAccess
import L1MenuAccess, L1PrescalesSetAccess, BunchGroupSetAccess
11from TrigConfIO.HLTTriggerConfigAccess
import HLTMenuAccess, HLTPrescalesSetAccess, HLTJobOptionsAccess, HLTMonitoringAccess
13from AthenaConfiguration.AutoConfigFlags
import GetFileMD
14from AthenaConfiguration.Enums
import Format
16from functools
import lru_cache
19Access to the trigger configuration in python is provided depending on
20the trigger configuration source
22The tc source is taken from the TriggerFlag triggerConfig
241) tc source is set to INFILE
26This is usually the case when running on ESD, AOD, and dAOD files and
27only in this case. An exception is RDO with trigger information in
28MC. The menu and prescales are taken from the pool file, from the
322) tc source is set to FILE
34This is the case when executing the trigger with the configuration
35taken from file. The filename is provided by the function
383) tc source is set to DB
40This is the case when executing the trigger from the DB. The DB
41connection and keys are provided by the triggerConfig flag
45This is the case when reconstructing the data. From COOL the
46configuration keys and db alias are taken, the configurations
47are 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)
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.
97 metadata = GetFileMD(flags.Input.Files)
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,
109Returns a string-serialised JSON object from the metadata store.
110Checks 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
116 metadata = GetFileMD(flags.Input.Files)
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)
137 tc = getTrigConfigFromFlag( flags )
138 if tc[
"SOURCE"] ==
"FILE":
139 cfg = L1MenuAccess( filename = getL1MenuFileName( flags ) )
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"])
157 tc = getTrigConfigFromFlag( flags )
158 if tc[
"SOURCE"] ==
"FILE":
159 cfg = L1PrescalesSetAccess( filename = getL1PrescalesSetFileName( flags ) )
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"])
177 tc = getTrigConfigFromFlag( flags )
178 if tc[
"SOURCE"] ==
"FILE":
179 cfg = BunchGroupSetAccess( filename = getBunchGroupSetFileName( flags ) )
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"])
204 tc = getTrigConfigFromFlag( flags )
205 if tc[
"SOURCE"] ==
"FILE":
206 cfg = HLTMenuAccess( filename = getHLTMenuFileName( flags ) )
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"])
224 tc = getTrigConfigFromFlag( flags )
225 if tc[
"SOURCE"] ==
"FILE":
226 cfg = HLTPrescalesSetAccess( filename = getHLTPrescalesSetFileName( flags ) )
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"])
244 tc = getTrigConfigFromFlag( flags )
245 if tc[
"SOURCE"] ==
"FILE":
246 cfg = HLTJobOptionsAccess( filename = getHLTJobOptionsFileName() )
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"])
264 tc = getTrigConfigFromFlag( flags )
265 if tc[
"SOURCE"] ==
"FILE":
266 cfg = HLTMonitoringAccess( filename = getHLTMonitoringFileName( flags ) )
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]
HLTMonitoringAccess getHLTMonitoringAccess(flags=None, filterOnActiveChains=False)
HLTMenuAccess getHLTMenuAccess(flags)
dict[str, str|int] getKeysFromCool(int runNr, int lbNr=0)
dict[str, str|int] getKeysFromConditions(int runNr, int lbNr, flags)
HLTPrescalesSetAccess getHLTPrescalesSetAccess(flags)
HLTJobOptionsAccess getHLTJobOptionsAccess(flags)
L1MenuAccess getL1MenuAccess(flags)
L1PrescalesSetAccess getL1PrescalesSetAccess(flags)
BunchGroupSetAccess getBunchGroupSetAccess(flags)
Optional[dict[str, Any]] getDBKeysFromMetadata(flags)
dict[str, str|int] getKeysFromCrest(int runNr, int lbNr, str crest_server)
Optional[dict[str, Any]] _getJSONFromMetadata(flags, key)