ATLAS Offline Software
AMITagHelper.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2021 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. In the grid environment this may be checked against the AMITag
6 in the name of the input dataset, to be looked up from the INDS environmental
7 variable.
8 
9 The AMITag of the current processing step is provided by command line argument
10 to the transform. In the reconstruction environment it may be taken from the
11 RecFlags.
12 
13 The tool provides a function to get and check the AMITags from the input and
14 add the output AMITag. In addition a script can do all this and set the value
15 for the output /TagInfo object.
16 
17  Usage:
18  AMITagHelper.SetOutputAMITag(runargs.AMITag)
19 """
20 import os
21 import re
22 from AthenaCommon.AppMgr import ServiceMgr
23 from AthenaCommon.Logging import logging
24 try:
25  from PyJobTransformsCore.runargs import RunArguments
26 except ModuleNotFoundError:
27  from PyJobTransforms.trfJobOptions import RunArguments
28 from PyUtils.MetaReaderPeeker import metadata
29 try:
30  from RecExConfig.RecFlags import rec
31 except ModuleNotFoundError:
32  pass
33 
34 log = logging.getLogger('AMITagHelper')
35 amitagRegex = re.compile('^[a-z][0-9]+')
36 
37 
39  """Returns AMITag of input, e.g. e6337_s3681_r13145
40 
41  Looks up AMITag of input from INDS environmental variable and in-file
42  metadta. If nothing can be retrieved from the in-file metadata return value
43  from INDS, otherwise return value from metadata. Inform about differences.
44  """
45  tags = []
46  tagsFromINDS = []
47  # from INDS environmental variable
48  inputDataSetName = os.environ.get('INDS')
49  if inputDataSetName:
50  # try extracting the AMITag of the input dataset from its name
51  tagsFromINDS = inputDataSetName.split('.')[-1].split('_')
52  tagsFromINDS = [tag for tag in tagsFromINDS if amitagRegex.match(tag)]
53  log.debug('AMITag from input dataset name: {}'.format(tagsFromINDS))
54 
55  # from in-file metadata
56  try:
57  tags = metadata['AMITag']
58  if isinstance(tags, str):
59  tags = tags.split('_')
60  except KeyError:
61  log.info("Cannot access /TagInfo/AMITag from in-file metadata")
62  return tagsFromINDS
63  except NameError:
64  log.info("Cannot access in-file metadata")
65  return tagsFromINDS
66 
67  if tagsFromINDS and tags != tagsFromINDS:
68  log.warning("AMITag mismatch, check metadata of input dataset")
69 
70  # Well that is weird, evgen?
71  return tags
72 
73 
74 def OutputAMITag(runArgs=None):
75  """Return value of new AMITag for this step in the workflow
76 
77  Look up AMITag for this processing step from the command line arugments and
78  the reconstruction flags. Return the value from the CLI arguments if it is
79  set, otherwise try the value from the confi flags. Inform about differences.
80  """
81  if not runArgs:
82  runArgs = RunArguments()
83 
84  flagTag = ''
85  try:
86  flagTag = rec.AMITag()
87  except (NameError, AttributeError,):
88  pass
89 
90  cliTag = ''
91  try:
92  cliTag = runArgs.AMITag
93  except AttributeError:
94  pass
95 
96  if flagTag and flagTag != cliTag:
97  log.info('AMITag from recFlags does not match value form command line')
98 
99  if cliTag:
100  return cliTag
101  else:
102  return flagTag
103 
104 
105 def SetAMITag(outputTag=None, inputTags=None, runArgs=None):
106  """Add input and output AMITag values and set result in in-file metadata
107 
108  If no values are provided for the input and output AMITags we look the up.
109  The AMITags will be combined with '_' as delimiters. The result is set in
110  the in-file metadata.
111  """
112  if not outputTag:
113  outputTag = OutputAMITag(runArgs)
114  if not inputTags:
115  inputTags = InputAMITags()
116 
117  tags = inputTags
118  if outputTag and outputTag not in inputTags:
119  tags += [outputTag]
120  tags = [tag for tag in tags if amitagRegex.match(tag)]
121  amitag = '_'.join(tags)
122  try:
123  if amitag:
124  ServiceMgr.TagInfoMgr.ExtraTagValuePairs.update( {'AMITag': amitag})
125  log.info("Output AMITag in in-file metadata set to {}".format(amitag))
126  else:
127  log.debug("Not adding empty AMITag to /TagInfo")
128  except NameError:
129  log.warning("TagInfoMgr not available to set output AMITag")
130  except TypeError:
131  log.info("AMITags could not be converted to well-known format")
vtune_athena.format
format
Definition: vtune_athena.py:14
PyJobTransforms.trfJobOptions
Contains functions related Athena Job Options files.
python.AMITagHelper.SetAMITag
def SetAMITag(outputTag=None, inputTags=None, runArgs=None)
Definition: AMITagHelper.py:105
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.AMITagHelper.InputAMITags
def InputAMITags()
Definition: AMITagHelper.py:38
python.AMITagHelper.OutputAMITag
def OutputAMITag(runArgs=None)
Definition: AMITagHelper.py:74
Trk::split
@ split
Definition: LayerMaterialProperties.h:38