2 """Utilities to get and set the AMITag in the metadata
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
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
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.
18 AMITagHelper.SetOutputAMITag(runargs.AMITag)
22 from AthenaCommon.AppMgr
import ServiceMgr
23 from AthenaCommon.Logging
import logging
25 from PyJobTransformsCore.runargs
import RunArguments
26 except ModuleNotFoundError:
28 from PyUtils.MetaReaderPeeker
import metadata
30 from RecExConfig.RecFlags
import rec
31 except ModuleNotFoundError:
34 log = logging.getLogger(
'AMITagHelper')
35 amitagRegex = re.compile(
'^[a-z][0-9]+')
39 """Returns AMITag of input, e.g. e6337_s3681_r13145
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.
48 inputDataSetName = os.environ.get(
'INDS')
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))
57 tags = metadata[
'AMITag']
58 if isinstance(tags, str):
59 tags = tags.split(
'_')
61 log.info(
"Cannot access /TagInfo/AMITag from in-file metadata")
64 log.info(
"Cannot access in-file metadata")
67 if tagsFromINDS
and tags != tagsFromINDS:
68 log.warning(
"AMITag mismatch, check metadata of input dataset")
75 """Return value of new AMITag for this step in the workflow
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.
82 runArgs = RunArguments()
86 flagTag = rec.AMITag()
87 except (NameError, AttributeError,):
92 cliTag = runArgs.AMITag
93 except AttributeError:
96 if flagTag
and flagTag != cliTag:
97 log.info(
'AMITag from recFlags does not match value form command line')
105 def SetAMITag(outputTag=None, inputTags=None, runArgs=None):
106 """Add input and output AMITag values and set result in in-file metadata
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.
118 if outputTag
and outputTag
not in inputTags:
120 tags = [tag
for tag
in tags
if amitagRegex.match(tag)]
121 amitag =
'_'.
join(tags)
124 ServiceMgr.TagInfoMgr.ExtraTagValuePairs.update( {
'AMITag': amitag})
125 log.info(
"Output AMITag in in-file metadata set to {}".
format(amitag))
127 log.debug(
"Not adding empty AMITag to /TagInfo")
129 log.warning(
"TagInfoMgr not available to set output AMITag")
131 log.info(
"AMITags could not be converted to well-known format")