ATLAS Offline Software
InfileMetaDataConfig.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
3 from dataclasses import dataclass, field
4 from functools import wraps
5 
6 from AthenaCommon.Logging import logging
7 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
8 from AthenaConfiguration.ComponentFactory import CompFactory
9 from AthenaConfiguration.Enums import Format, MetadataCategory, ProductionStep
10 from 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 
33 def metadata_creator(func):
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
52 def createCutFlowMetaData(tools, result, flags):
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
63 def createByteStreamMetaData(tools, result, flags):
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
72 def createLumiBlockMetaData(tools, result, flags):
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
86 def createTriggerMenuMetaData(tools, result, flags):
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
99 def createTruthMetaData(tools, result, flags):
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
108 def createIOVMetaData(tools, result, flags):
109  tools.mdItems += ["IOVMetaDataContainer#*"]
110 
111 
112 def propagateMetaData(flags, streamName="", category=None, *args, **kwargs):
113  """
114  Returns the tuple of MetaDataHelperLists and ComponentAccumulator.
115  The former combines the lists needed to setup given metadata category
116  for the output stream configuration and metadata service.
117  The latter contains the CA needed for a given metadata category.
118  """
119  tools = MetaDataHelperLists()
120  result = ComponentAccumulator()
121  log = logging.getLogger("SetupMetaDataForStreamCfg")
122 
123  if category == MetadataCategory.FileMetaData:
124  tools.mdToolNames.append("xAODMaker::FileMetaDataTool")
125  tools.mdItems += [
126  "xAOD::FileMetaData#FileMetaData",
127  "xAOD::FileMetaDataAuxInfo#FileMetaDataAux.",
128  ]
129  tools.helperTools.append(
130  CompFactory.xAODMaker.FileMetaDataCreatorTool(
131  f"{outputStreamName(streamName)}_FileMetaDataCreatorTool",
132  OutputKey="FileMetaData",
133  StreamName=outputStreamName(streamName),
134  )
135  )
136  elif category == MetadataCategory.EventStreamInfo:
137  esiTool = CompFactory.MakeEventStreamInfo(
138  f"{outputStreamName(streamName)}_MakeEventStreamInfo",
139  Key=outputStreamName(streamName),
140  DataHeaderKey=outputStreamName(streamName),
141  EventInfoKey=f"{flags.Overlay.BkgPrefix}EventInfo"
142  if flags.Common.ProductionStep
143  in [ProductionStep.PileUpPresampling, ProductionStep.PileUpPretracking]
144  else "EventInfo",
145  )
146  tools.mdItems += [
147  f"EventStreamInfo#{outputStreamName(streamName)}",
148  ]
149  tools.helperTools.append(esiTool)
150 
151  # copy EventStreamInfo for merging jobs
152  if kwargs.get("mergeJob", False):
153  tools.mdTools += [
154  CompFactory.CopyEventStreamInfo(
155  f"{outputStreamName(streamName)}_CopyEventStreamInfo"
156  ),
157  ]
158 
159  elif category == MetadataCategory.EventFormat:
160  efTool = CompFactory.xAODMaker.EventFormatStreamHelperTool(
161  f"{outputStreamName(streamName)}_EventFormatStreamHelperTool",
162  Key=f"EventFormat{outputStreamName(streamName)}",
163  DataHeaderKey=outputStreamName(streamName),
164  )
165  tools.mdItems += [
166  f"xAOD::EventFormat#EventFormat{outputStreamName(streamName)}",
167  ]
168  tools.helperTools.append(efTool)
169  tools.mdToolNames.append("xAODMaker::EventFormatMetaDataTool")
170  elif category == MetadataCategory.CutFlowMetaData:
171  if "CutBookkeepers" in flags.Input.MetadataItems:
172  from EventBookkeeperTools.EventBookkeeperToolsConfig import (
173  CutFlowOutputList,
174  )
175 
176  tools.mdToolNames.append("BookkeeperTool")
177  tools.mdItems += CutFlowOutputList(flags)
178 
179  elif category == MetadataCategory.TriggerMenuMetaData:
180  if any("TriggerMenu" in item for item in flags.Input.MetadataItems):
181  _tools, _ = createTriggerMenuMetaData(flags)
182  tools.mdTools = _tools.mdTools
183  tools.mdItems = _tools.mdItems
184 
185  elif category == MetadataCategory.TruthMetaData:
186  if "TruthMetaData" in flags.Input.MetadataItems:
187  tools.mdItems += [
188  "xAOD::TruthMetaDataContainer#TruthMetaData",
189  "xAOD::TruthMetaDataAuxContainer#TruthMetaDataAux.",
190  ]
191  tools.mdTools.append(
192  CompFactory.xAODMaker.TruthMetaDataTool("TruthMetaDataTool")
193  )
194  elif category == MetadataCategory.ByteStreamMetaData:
195  if "ByteStreamMetadata" in flags.Input.MetadataItems:
196  tools.mdItems += ["ByteStreamMetadataContainer#*"]
197  elif category == MetadataCategory.LumiBlockMetaData:
198  if any(
199  lb in flags.Input.MetadataItems
200  for lb in ["SuspectLumiBlocks", "IncompleteLumiBlocks", "LumiBlocks"]
201  ):
202  tools.mdToolNames.append("LumiBlockMetaDataTool")
203  tools.mdItems += [
204  "xAOD::LumiBlockRangeContainer#*",
205  "xAOD::LumiBlockRangeAuxContainer#*",
206  ]
207  elif category == MetadataCategory.IOVMetaData:
208  if "IOVMetaDataContainer" in flags.Input.MetadataItems.values():
209  tools.mdItems += ["IOVMetaDataContainer#*"]
210 
211  else:
212  log.warning(f"Requested metadata category: {category} could not be configured")
213  return tools, result
214 
215 
217  flags,
218  streamName="",
219  AcceptAlgs=None,
220  createMetadata=None,
221  propagateMetadataFromInput=True,
222  *args,
223  **kwargs,
224 ):
225  """
226  Set up metadata for the stream named streamName
227 
228  It takes optional arguments: createMetadata to specify a list of metadata
229  categories to create (empty by default) and propagateMetadataFromInput (bool)
230  to propagate metadata existing in the input (True by default).
231 
232  The additional argument, AcceptAlgs, is needed for workflows with custom kernels.
233 
234  Returns CA to be merged
235  """
236  log = logging.getLogger("SetupMetaDataForStreamCfg")
237  result = ComponentAccumulator()
238  if not isinstance(streamName, str) or not streamName:
239  return result
240  if AcceptAlgs is None:
241  AcceptAlgs = []
242  if createMetadata is None:
243  createMetadata = []
244 
245  helperLists = MetaDataHelperLists()
246 
247  if propagateMetadataFromInput:
248  for mdCategory in MetadataCategory:
249  lists, caConfig = propagateMetaData(
250  flags,
251  streamName,
252  mdCategory,
253  *args,
254  **kwargs,
255  )
256  helperLists += lists
257  result.merge(caConfig)
258 
259  for md in createMetadata:
260  try:
261  lists, caConfig = globals()[f"create{md.name}"](
262  flags,
263  )
264  except KeyError:
265  log.warning(
266  f"Requested metadata category: {md.name} could not be configured"
267  )
268  continue
269  helperLists += lists
270  result.merge(caConfig)
271 
272  # Configure the relevant output stream
273  result.merge(
275  flags,
276  streamName=streamName,
277  itemOrList=helperLists.mdItems,
278  AcceptAlgs=AcceptAlgs,
279  HelperTools=helperLists.helperTools,
280  **kwargs,
281  )
282  )
283  # Configure the MetaDataSvc and pass the relevant tools
284  from AthenaServices.MetaDataSvcConfig import MetaDataSvcCfg
285 
286  result.merge(
288  flags, tools=helperLists.mdTools, toolNames=helperLists.mdToolNames
289  )
290  )
291  return result
python.CreateLumiBlockCollectionFromFileConfig.CreateLumiBlockCollectionFromFileCfg
def CreateLumiBlockCollectionFromFileCfg(configFlags)
Definition: CreateLumiBlockCollectionFromFileConfig.py:6
AthenaPoolExample_WriteCond.outputStreamName
string outputStreamName
Definition: AthenaPoolExample_WriteCond.py:21
python.OutputStreamConfig.addToMetaData
def addToMetaData(flags, streamName, itemOrList, AcceptAlgs=[], HelperTools=[], **kwargs)
Definition: OutputStreamConfig.py:153
python.JetAnalysisCommon.ComponentAccumulator
ComponentAccumulator
Definition: JetAnalysisCommon.py:302
InfileMetaDataConfig.createTruthMetaData
def createTruthMetaData(tools, result, flags)
Definition: InfileMetaDataConfig.py:99
InfileMetaDataConfig.createByteStreamMetaData
def createByteStreamMetaData(tools, result, flags)
Definition: InfileMetaDataConfig.py:63
InfileMetaDataConfig.createCutFlowMetaData
def createCutFlowMetaData(tools, result, flags)
Definition: InfileMetaDataConfig.py:52
InfileMetaDataConfig.createTriggerMenuMetaData
def createTriggerMenuMetaData(tools, result, flags)
Definition: InfileMetaDataConfig.py:86
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
python.EventBookkeeperToolsConfig.CutFlowOutputList
def CutFlowOutputList(flags, output_name='CutBookkeepers')
Definition: EventBookkeeperToolsConfig.py:44
python.ByteStreamConfig.ByteStreamReadCfg
def ByteStreamReadCfg(flags, type_names=None)
Definition: Event/ByteStreamCnvSvc/python/ByteStreamConfig.py:25
InfileMetaDataConfig.propagateMetaData
def propagateMetaData(flags, streamName="", category=None, *args, **kwargs)
Definition: InfileMetaDataConfig.py:112
InfileMetaDataConfig.createIOVMetaData
def createIOVMetaData(tools, result, flags)
Definition: InfileMetaDataConfig.py:108
InfileMetaDataConfig.MetaDataHelperLists.__iadd__
def __iadd__(self, helperLists)
Definition: InfileMetaDataConfig.py:25
InfileMetaDataConfig.createLumiBlockMetaData
def createLumiBlockMetaData(tools, result, flags)
Definition: InfileMetaDataConfig.py:72
python.MetaDataSvcConfig.MetaDataSvcCfg
def MetaDataSvcCfg(flags, toolNames=[], tools=[])
Definition: MetaDataSvcConfig.py:6
InfileMetaDataConfig.metadata_creator
def metadata_creator(func)
Definition: InfileMetaDataConfig.py:33
InfileMetaDataConfig.MetaDataHelperLists
Definition: InfileMetaDataConfig.py:14
InfileMetaDataConfig.SetupMetaDataForStreamCfg
def SetupMetaDataForStreamCfg(flags, streamName="", AcceptAlgs=None, createMetadata=None, propagateMetadataFromInput=True, *args, **kwargs)
Definition: InfileMetaDataConfig.py:216
python.EventBookkeeperToolsConfig.CutFlowSvcCfg
def CutFlowSvcCfg(flags, **kwargs)
Definition: EventBookkeeperToolsConfig.py:24