ATLAS Offline Software
Loading...
Searching...
No Matches
InfileMetaDataConfig.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3from dataclasses import dataclass, field
4from functools import wraps
5
6from AthenaCommon.Logging import logging
7from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
8from AthenaConfiguration.ComponentFactory import CompFactory
9from AthenaConfiguration.Enums import Format, MetadataCategory, ProductionStep
10from OutputStreamAthenaPool.OutputStreamConfig import addToMetaData, outputStreamName
11
12
13defaultIOVFolders = [
14 "/Generation/Parameters",
15 "/GLOBAL/BField/Maps",
16 "/Simulation/Parameters",
17 "/Digitization/Parameters",
18 "/TagInfo",
19 "/TRIGGER/HLT/HltConfigKeys",
20 "/TRIGGER/HLT/Menu",
21 "/TRIGGER/HLT/PrescaleKey",
22 "/TRIGGER/HLT/Prescales",
23 "/TRIGGER/LVL1/ItemDef",
24 "/TRIGGER/LVL1/Lvl1ConfigKey",
25 "/TRIGGER/LVL1/Menu",
26 "/TRIGGER/LVL1/Prescales",
27 "/TRIGGER/LVL1/Thresholds",
28]
29
30excludeStreamToIOVFolders = {
31 "HITS": [
32 "/Digitization/Parameters",
33 ],
34}
35
36
37@dataclass
39 """
40 Helper class aggregating lists needed to setup metadata
41 for the output stream configuration and metadata service.
42 """
43
44 helperTools: list = field(default_factory=list)
45 mdTools: list = field(default_factory=list)
46 mdToolNames: list = field(default_factory=list)
47 mdItems: list = field(default_factory=list)
48
49 def __iadd__(self, helperLists):
50 self.helperTools += helperLists.helperTools
51 self.mdTools += helperLists.mdTools
52 self.mdToolNames += helperLists.mdToolNames
53 self.mdItems += helperLists.mdItems
54 return self
55
56
58 """
59 Decorator function which:
60 - creates default MetaDataHelperLists() and ComponentAccumulator() instances
61 - passes them to wrapped create*MetaData functions responsible for configuration of helper lists and CA, specific to metadata category
62 - returns configured instances of MetaDataHelperLists() (used by MetaDataSvc and AthenaOutputStream) and CA
63 """
64
65 @wraps(func)
66 def wrapper(*args, **kwargs):
67 tools = MetaDataHelperLists()
68 result = ComponentAccumulator()
69 func(tools, result, *args, **kwargs)
70 return tools, result
71
72 return wrapper
73
74
75@metadata_creator
76def createCutFlowMetaData(tools, result, flags, **kwargs):
77 from EventBookkeeperTools.EventBookkeeperToolsConfig import (
78 CutFlowOutputList,
79 CutFlowSvcCfg,
80 )
81
82 result.merge(CutFlowSvcCfg(flags))
83 tools.mdItems += CutFlowOutputList(flags)
84
85
86@metadata_creator
87def createByteStreamMetaData(tools, result, flags, **kwargs):
88 tools.mdItems += ["ByteStreamMetadataContainer#*"]
89 if flags.Input.Format == Format.BS and not flags.Common.isOnline:
90 from ByteStreamCnvSvc.ByteStreamConfig import ByteStreamReadCfg
91
92 result.merge(ByteStreamReadCfg(flags))
93
94
95@metadata_creator
96def createLumiBlockMetaData(tools, result, flags, **kwargs):
97 if flags.Input.Format == Format.BS and not flags.Common.isOnline:
98 from LumiBlockComps.CreateLumiBlockCollectionFromFileConfig import (
99 CreateLumiBlockCollectionFromFileCfg,
100 )
101
102 result.merge(CreateLumiBlockCollectionFromFileCfg(flags))
103 tools.mdItems += [
104 "xAOD::LumiBlockRangeContainer#*",
105 "xAOD::LumiBlockRangeAuxContainer#*",
106 ]
107
108
109@metadata_creator
110def createTriggerMenuMetaData(tools, result, flags, **kwargs):
111 tools.mdTools.append(
112 CompFactory.xAODMaker.TriggerMenuMetaDataTool("TriggerMenuMetaDataTool")
113 )
114 tools.mdItems += [
115 "xAOD::TriggerMenuContainer#*",
116 "xAOD::TriggerMenuAuxContainer#*",
117 "xAOD::TriggerMenuJsonContainer#*",
118 "xAOD::TriggerMenuJsonAuxContainer#*",
119 ]
120
121
122@metadata_creator
123def createTruthMetaData(tools, result, flags, **kwargs):
124 tools.mdItems += [
125 "xAOD::TruthMetaDataContainer#TruthMetaData",
126 "xAOD::TruthMetaDataAuxContainer#TruthMetaDataAux.",
127 ]
128 tools.mdTools.append(CompFactory.xAODMaker.TruthMetaDataTool("TruthMetaDataTool"))
129
130
131@metadata_creator
132def createIOVMetaData(tools, result, flags, **kwargs):
133 streamName = kwargs.get("streamName", "")
134 if streamName not in excludeStreamToIOVFolders:
135 tools.mdItems += ["IOVMetaDataContainer#*"]
136 else:
137 tools.mdItems += [
138 f"IOVMetaDataContainer#{folder}"
139 for folder in defaultIOVFolders
140 if folder not in excludeStreamToIOVFolders[streamName]
141 ]
142 from IOVDbSvc.IOVDbSvcConfig import IOVDbSvcCfg
143 result.merge(IOVDbSvcCfg(flags))
144
145
146@metadata_creator
147def createEventStreamInfo(tools, result, flags, **kwargs):
148 esiTool = CompFactory.MakeEventStreamInfo(
149 f"{outputStreamName(kwargs.get('streamName', ''))}_MakeEventStreamInfo",
150 Key=outputStreamName(kwargs.get('streamName', '')),
151 DataHeaderKey=outputStreamName(kwargs.get('streamName', '')),
152 EventInfoKey=f"{flags.Overlay.BkgPrefix}EventInfo"
153 if flags.Common.ProductionStep
154 in [ProductionStep.PileUpPresampling, ProductionStep.PileUpPretracking, ProductionStep.MinbiasPreprocessing]
155 else "EventInfo",
156 )
157 tools.mdItems += [
158 f"EventStreamInfo#{outputStreamName(kwargs.get('streamName', ''))}",
159 ]
160 tools.helperTools.append(esiTool)
161
162def propagateMetaData(flags, streamName="", category=None):
163 """
164 Returns the tuple of MetaDataHelperLists and ComponentAccumulator.
165 The former combines the lists needed to setup given metadata category
166 for the output stream configuration and metadata service.
167 The latter contains the CA needed for a given metadata category.
168 """
169 tools = MetaDataHelperLists()
170 result = ComponentAccumulator()
171 log = logging.getLogger("SetupMetaDataForStreamCfg")
172
173 if category == MetadataCategory.FileMetaData:
174 tools.mdToolNames.append("xAODMaker::FileMetaDataTool")
175 tools.mdItems += [
176 "xAOD::FileMetaData#FileMetaData",
177 "xAOD::FileMetaDataAuxInfo#FileMetaDataAux.",
178 ]
179 tools.helperTools.append(
180 CompFactory.xAODMaker.FileMetaDataCreatorTool(
181 f"{outputStreamName(streamName)}_FileMetaDataCreatorTool",
182 OutputKey="FileMetaData",
183 StreamName=outputStreamName(streamName),
184 EventInfoKey=f"{flags.Overlay.BkgPrefix}EventInfo"
185 if flags.Common.ProductionStep in [ProductionStep.PileUpPresampling, ProductionStep.PileUpPretracking, ProductionStep.MinbiasPreprocessing]
186 else "EventInfo",
187 )
188 )
189 elif category == MetadataCategory.EventStreamInfo:
190 tools.mdTools += [
191 CompFactory.CopyEventStreamInfo(
192 f"{outputStreamName(streamName)}_CopyEventStreamInfo",
193 Keys=[outputStreamName(streamName)],
194 ),
195 ]
196
197 elif category == MetadataCategory.EventFormat:
198 efTool = CompFactory.xAODMaker.EventFormatStreamHelperTool(
199 f"{outputStreamName(streamName)}_EventFormatStreamHelperTool",
200 Key=f"EventFormat{outputStreamName(streamName)}",
201 DataHeaderKey=outputStreamName(streamName),
202 )
203 tools.mdItems += [
204 f"xAOD::EventFormat#EventFormat{outputStreamName(streamName)}",
205 ]
206 tools.helperTools.append(efTool)
207 tools.mdToolNames.append("xAODMaker::EventFormatMetaDataTool")
208 elif category == MetadataCategory.CutFlowMetaData:
209 if "CutBookkeepers" in flags.Input.MetadataItems:
210 from EventBookkeeperTools.EventBookkeeperToolsConfig import (
211 CutFlowOutputList,
212 )
213
214 tools.mdToolNames.append("BookkeeperTool")
215 tools.mdItems += CutFlowOutputList(flags)
216
217 elif category == MetadataCategory.TriggerMenuMetaData:
218 if any("TriggerMenu" in item for item in flags.Input.MetadataItems):
219 _tools, _ = createTriggerMenuMetaData(flags)
220 tools.mdTools = _tools.mdTools
221 tools.mdItems = _tools.mdItems
222
223 elif category == MetadataCategory.TruthMetaData:
224 if "TruthMetaData" in flags.Input.MetadataItems:
225 tools.mdItems += [
226 "xAOD::TruthMetaDataContainer#TruthMetaData",
227 "xAOD::TruthMetaDataAuxContainer#TruthMetaDataAux.",
228 ]
229 tools.mdTools.append(
230 CompFactory.xAODMaker.TruthMetaDataTool("TruthMetaDataTool")
231 )
232 elif category == MetadataCategory.ByteStreamMetaData:
233 if "ByteStreamMetadata" in flags.Input.MetadataItems:
234 tools.mdItems += ["ByteStreamMetadataContainer#*"]
235 elif category == MetadataCategory.LumiBlockMetaData:
236 if any(
237 lb in flags.Input.MetadataItems
238 for lb in ["SuspectLumiBlocks", "IncompleteLumiBlocks", "LumiBlocks"]
239 ):
240 tools.mdToolNames.append("LumiBlockMetaDataTool")
241 tools.mdItems += [
242 "xAOD::LumiBlockRangeContainer#*",
243 "xAOD::LumiBlockRangeAuxContainer#*",
244 ]
245 elif category == MetadataCategory.IOVMetaData:
246 if "IOVMetaDataContainer" in flags.Input.MetadataItems.values():
247 if streamName not in excludeStreamToIOVFolders:
248 tools.mdItems += ["IOVMetaDataContainer#*"]
249 else:
250 tools.mdItems += [
251 f"IOVMetaDataContainer#{folder}"
252 for folder, container in flags.Input.MetadataItems.items()
253 if container == "IOVMetaDataContainer" and folder not in excludeStreamToIOVFolders[streamName]
254 ]
255
256 else:
257 log.warning(f"Requested metadata category: {category} could not be configured")
258 return tools, result
259
260
262 flags,
263 streamName="",
264 AcceptAlgs=None,
265 createMetadata=None,
266 propagateMetadataFromInput=True,
267 *args,
268 **kwargs,
269):
270 """
271 Set up metadata for the stream named streamName
272
273 It takes optional arguments: createMetadata to specify a list of metadata
274 categories to create (empty by default) and propagateMetadataFromInput (bool)
275 to propagate metadata existing in the input (True by default).
276
277 The additional argument, AcceptAlgs, is needed for workflows with custom kernels.
278
279 Returns CA to be merged
280 """
281 log = logging.getLogger("SetupMetaDataForStreamCfg")
282 result = ComponentAccumulator()
283 if not isinstance(streamName, str) or not streamName:
284 return result
285 if AcceptAlgs is None:
286 AcceptAlgs = []
287 if createMetadata is None:
288 createMetadata = []
289 createMetadata += [MetadataCategory.EventStreamInfo]
290
291 helperLists = MetaDataHelperLists()
292
293 if propagateMetadataFromInput:
294 for mdCategory in MetadataCategory:
295 lists, caConfig = propagateMetaData(
296 flags,
297 streamName,
298 mdCategory,
299 )
300 helperLists += lists
301 result.merge(caConfig)
302
303 for md in createMetadata:
304 try:
305 lists, caConfig = globals()[f"create{md.name}"](
306 flags,
307 streamName=streamName,
308 )
309 except KeyError:
310 log.warning(
311 f"Requested metadata category: {md.name} could not be configured"
312 )
313 continue
314 helperLists += lists
315 result.merge(caConfig)
316
317 # Configure the relevant output stream
318 result.merge(
319 addToMetaData(
320 flags,
321 streamName=streamName,
322 itemOrList=helperLists.mdItems,
323 AcceptAlgs=AcceptAlgs,
324 HelperTools=helperLists.helperTools,
325 **kwargs,
326 )
327 )
328 # Configure the MetaDataSvc and pass the relevant tools
329 from AthenaServices.MetaDataSvcConfig import MetaDataSvcCfg
330
331 result.merge(
332 MetaDataSvcCfg(
333 flags, tools=helperLists.mdTools, toolNames=helperLists.mdToolNames
334 )
335 )
336 return result
createTriggerMenuMetaData(tools, result, flags, **kwargs)
createByteStreamMetaData(tools, result, flags, **kwargs)
propagateMetaData(flags, streamName="", category=None)
createEventStreamInfo(tools, result, flags, **kwargs)
createCutFlowMetaData(tools, result, flags, **kwargs)
SetupMetaDataForStreamCfg(flags, streamName="", AcceptAlgs=None, createMetadata=None, propagateMetadataFromInput=True, *args, **kwargs)
createLumiBlockMetaData(tools, result, flags, **kwargs)
createTruthMetaData(tools, result, flags, **kwargs)
createIOVMetaData(tools, result, flags, **kwargs)