ATLAS Offline Software
AMITagHelperConfig.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
2 """Utilities to get and set the AMITag in the metadata
3 
4 The input AMITag is contained in the AMITag attribute of the /TagInfo in-file
5 metadata object.
6 
7 The AMITag of the current processing step is provided by command line argument
8 to the transform.
9 
10 The tool provides a function to get and check the AMITags from the input and
11 add the output AMITag, configuring the TagInfoMgr with the information.
12 
13  Usage:
14  from PyUtils.AMITagHelperConfig import AMITagCfg
15  cfg.merge(AMITagCfg(ConfigFlags, runArgs))
16 """
17 from AthenaCommon.Logging import logging
18 log = logging.getLogger('AMITagHelper')
19 
20 import re
21 amitagRegex = re.compile('^[a-z][0-9]+')
22 
23 
24 def inputAMITagsFromEnvironment(silent=False):
25  tagsFromINDS = []
26  # from INDS environmental variable
27  from os import environ
28  varINDS = environ.get('INDS', '')
29  if varINDS:
30  # try extracting the AMITag of the input dataset from its name
31  tagsFromINDS = varINDS.split('.')[-1].split('_')
32  tagsFromINDS = [tag for tag in tagsFromINDS if amitagRegex.match(tag)]
33  if not silent:
34  log.debug('AMITag from input dataset name: {}'.format(tagsFromINDS))
35  return tagsFromINDS
36 
37 
38 def inputAMITags(flags, fixBroken=False, silent=False):
39  """Returns AMITag of input, split in a list, e.g. ['e6337','s3681','r13145']
40 
41  Looks up AMITag of input from ConfigFlag input file's in-file
42  metadta. If nothing can be retrieved from the in-file metadata return empty
43  list, otherwise return value from metadata. Inform about differences.
44  """
45  if flags.Input.SecondaryFiles and not flags.Overlay.DataOverlay:
46  files = flags.Input.SecondaryFiles
47  else:
48  files = flags.Input.Files
49 
50  from AthenaConfiguration.AutoConfigFlags import GetFileMD
51  tags = GetFileMD(files).get('AMITag', '')
52  tagsFromINDS = inputAMITagsFromEnvironment(silent)
53 
54  if tags:
55  tags = tags.split('_')
56  # remove duplicates
57  from itertools import groupby
58  tags = [key for key, _group in groupby(tags)]
59  if not silent:
60  log.info(f'Read AMITag from metadata: {tags}')
61  else:
62  log.info("Cannot access /TagInfo/AMITag from in-file metadata")
63  # Set it to an empty list or the value from INDS
64  return tagsFromINDS
65 
66  if tagsFromINDS and tags != tagsFromINDS:
67  if not silent:
68  log.warning("AMITag mismatch, check metadata of input dataset")
69  if fixBroken:
70  if not silent:
71  log.warning(f"Will change {tags} to {tagsFromINDS} based on INDS environment variable")
72  tags = tagsFromINDS
73 
74  return tags
75 
76 
77 def AMITagCfg(flags, runArgs=None, fixBroken=False):
78  """Add input and output AMITag values and set result in in-file metadata
79 
80  The AMITags will be combined with '_' as delimiters. The result is set in
81  the in-file metadata.
82  """
83  tags = inputAMITags(flags, fixBroken)
84  if runArgs is not None:
85  if hasattr(runArgs, 'AMITag') and runArgs.AMITag not in tags:
86  tags += [runArgs.AMITag]
87  log.info(f'Adding AMITag from execution: {runArgs.AMITag}')
88  elif hasattr(runArgs, 'AMIConfig') and runArgs.AMIConfig not in tags:
89  tags += [runArgs.AMIConfig]
90  log.info(f'Adding AMITag from execution: {runArgs.AMIConfig}')
91 
92  if not tags:
93  from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
94  return ComponentAccumulator()
95 
96  valueAMITag = '_'.join(tags)
97 
98  from EventInfoMgt.TagInfoMgrConfig import TagInfoMgrCfg
99  return TagInfoMgrCfg(flags, tagValuePairs={'AMITag': valueAMITag})
python.JetAnalysisCommon.ComponentAccumulator
ComponentAccumulator
Definition: JetAnalysisCommon.py:302
vtune_athena.format
format
Definition: vtune_athena.py:14
python.AutoConfigFlags.GetFileMD
def GetFileMD(filenames, allowEmpty=True)
Definition: AutoConfigFlags.py:51
python.TagInfoMgrConfig.TagInfoMgrCfg
def TagInfoMgrCfg(flags, tagValuePairs={})
Definition: TagInfoMgrConfig.py:6
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.AMITagHelperConfig.inputAMITagsFromEnvironment
def inputAMITagsFromEnvironment(silent=False)
Definition: AMITagHelperConfig.py:24
python.AMITagHelperConfig.AMITagCfg
def AMITagCfg(flags, runArgs=None, fixBroken=False)
Definition: AMITagHelperConfig.py:77
python.AMITagHelperConfig.inputAMITags
def inputAMITags(flags, fixBroken=False, silent=False)
Definition: AMITagHelperConfig.py:38
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
Trk::split
@ split
Definition: LayerMaterialProperties.h:38