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