ATLAS Offline Software
Loading...
Searching...
No Matches
python.Utils Namespace Reference

Namespaces

namespace  unixtools

Classes

class  Campaign

Functions

 loadDefaultComps (allcomps)
 exposeHandles (allcomps)
 setupLoggingLevels (flags, ca)
 getRunFromName (name, default='', asInt=False)
 fileListSnippet (files, dsName, taskName, jobDir=None)
 blankIfNone (s)
 getUserName (default='UNKNOWN')
 getHostName ()
 getMCCampaign (files)
 getDataYear (flags)
 getEDMVersionFromBS (filename)
 edmDictToList (edmDict)
 edmListToDict (edmList)
 getEDMListFromWriteHandles (configurables)

Variables

str __version__ = '1.0.0'
str __author__ = 'Wim Lavrijsen (WLavrijsen@lbl.gov)'
list __all__ = [ 'unixtools' ]
dict campaign_runs
 log = logging.getLogger('TrigEDMConfig')

Detailed Description

This module defines miscellaneous utility functions.

Function Documentation

◆ blankIfNone()

python.Utils.blankIfNone ( s)

Definition at line 41 of file InnerDetector/InDetExample/InDetBeamSpotExample/python/Utils.py.

41def blankIfNone(s):
42 if s:
43 return s
44 else:
45 return ''
46
47

◆ edmDictToList()

python.Utils.edmDictToList ( edmDict)
Convert EDM dictionary in the format:
    {'type1': ['key1','key2'], 'type2': ['key3']}
to a flat list in the format:
    ['type1#key1', 'type1#key2', 'type2#key3']

Definition at line 68 of file Trigger/TriggerCommon/TrigEDMConfig/python/Utils.py.

68def edmDictToList(edmDict):
69 '''
70 Convert EDM dictionary in the format:
71 {'type1': ['key1','key2'], 'type2': ['key3']}
72 to a flat list in the format:
73 ['type1#key1', 'type1#key2', 'type2#key3']
74 '''
75 return [ f"{type}#{name}" for type, names in edmDict.items() for name in names ]
76
77

◆ edmListToDict()

python.Utils.edmListToDict ( edmList)
Convert EDM list in the format:
    ['type1#key1', 'type1#key2', 'type2#key3']
to a dictionary in the format:
    {'type1': ['key1','key2'], 'type2': ['key3']}

Definition at line 78 of file Trigger/TriggerCommon/TrigEDMConfig/python/Utils.py.

78def edmListToDict(edmList):
79 '''
80 Convert EDM list in the format:
81 ['type1#key1', 'type1#key2', 'type2#key3']
82 to a dictionary in the format:
83 {'type1': ['key1','key2'], 'type2': ['key3']}
84 '''
85 edmDict = defaultdict(list)
86 for typeName in edmList:
87 edmType, edmKey = typeName.split('#')
88 edmDict[edmType].append(edmKey)
89 return edmDict
90
91

◆ exposeHandles()

python.Utils.exposeHandles ( allcomps)
Sets all handle keys explicitly

Definition at line 36 of file Control/AthenaConfiguration/python/Utils.py.

36def exposeHandles(allcomps):
37 """Sets all handle keys explicitly"""
38 def __getDefault(d):
39 if isinstance(d, collections.abc.Sequence):
40 return [el for el in d]
41 else:
42 return d.Path
43
44 for comp in allcomps:
45 for propName, value in comp._descriptors.items():
46 if propName in comp._properties: continue
47 if "HandleKey" in value.cpp_type:
48 comp._properties[propName] = __getDefault(value.default)
49

◆ fileListSnippet()

python.Utils.fileListSnippet ( files,
dsName,
taskName,
jobDir = None )

Definition at line 30 of file InnerDetector/InDetExample/InDetBeamSpotExample/python/Utils.py.

30def fileListSnippet(files,dsName,taskName,jobDir=None):
31 s = ''
32 if files:
33 for f in files.split():
34 if jobDir and not os.path.exists('%s/%s/%s/%s' % (jobDir,dsName,taskName,f)):
35 s += '%s (archived)<br>' % f
36 else:
37 s += '<a href="/jobfiles/%s/%s/%s">%s</a><br>' % (dsName,taskName,f,f)
38 return s
39
40

◆ getDataYear()

python.Utils.getDataYear ( flags)

Definition at line 71 of file Tools/Campaigns/python/Utils.py.

71def getDataYear(flags):
72 if flags.Input.isMC:
73 campaign = getMCCampaign(flags.Input.Files)
74 dataYear = {
75 Campaign.MC20a: 2016, # prefer over 2015
76 Campaign.MC20d: 2017,
77 Campaign.MC20e: 2018,
78 Campaign.MC21a: 2022,
79 Campaign.MC23a: 2022,
80 Campaign.MC23c: 2023,
81 Campaign.MC23d: 2023,
82 Campaign.MC23e: 2024,
83 Campaign.MC23g: 2025,
84 Campaign.PhaseII: 2030,
85 }[campaign]
86 else:
87 dataYear = flags.Input.DataYear
88 return dataYear

◆ getEDMListFromWriteHandles()

python.Utils.getEDMListFromWriteHandles ( configurables)
Build OutputStream ItemList from all WriteHandles in a list of components (configurables),
for example a list of AlgTools. The output is in flat list format:
    ['type1#key1', 'type1#key2', 'type2#key3']

Definition at line 92 of file Trigger/TriggerCommon/TrigEDMConfig/python/Utils.py.

92def getEDMListFromWriteHandles(configurables):
93 '''
94 Build OutputStream ItemList from all WriteHandles in a list of components (configurables),
95 for example a list of AlgTools. The output is in flat list format:
96 ['type1#key1', 'type1#key2', 'type2#key3']
97 '''
98
99 def getWriteHandles(comp):
100 properties = [getattr(comp,propName) for propName in comp.getDefaultProperties().keys()]
101 return [prop for prop in properties if isinstance(prop,DataHandle) and prop.mode()=='W']
102
103 def formatItem(containerType, containerKey):
104 auxType = containerType.replace('Container','AuxContainer')
105 return [f'{containerType}#{containerKey}',
106 f'{auxType}#{containerKey}Aux.-']
107
108 def containerTypedef(containerType):
109 if containerType.startswith('xAOD::') and containerType.endswith('Container'):
110 # Already the right typedef
111 return containerType
112 m = re.match(r'DataVector<xAOD::(\w+)_v[0-9]+,', containerType)
113 if m and len(m.groups()) > 0:
114 return f'xAOD::{m.group(1)}Container'
115 raise RuntimeError(f'Failed to convert type {containerType} into a container typedef')
116
117 def itemListFromConfigurable(comp):
118 items = []
119 for handle in getWriteHandles(comp):
120 sgKey = handle.Path.replace('StoreGateSvc+','')
121 if not sgKey:
122 continue
123 items += formatItem(containerTypedef(handle.Type), handle.Path)
124 return items
125
126 itemList = []
127 for comp in configurables:
128 itemList += itemListFromConfigurable(comp)
129 return itemList

◆ getEDMVersionFromBS()

python.Utils.getEDMVersionFromBS ( filename)
Determine Trigger EDM version based on the input ByteStream file.

Run-3 EDM is indicated by HLT ROD version > 1.0. For Run 1 and 2 the
HLT ROD version was 0.0 and the run number is used to disambiguate between them.

Definition at line 11 of file Trigger/TriggerCommon/TrigEDMConfig/python/Utils.py.

11def getEDMVersionFromBS(filename):
12 """Determine Trigger EDM version based on the input ByteStream file.
13
14 Run-3 EDM is indicated by HLT ROD version > 1.0. For Run 1 and 2 the
15 HLT ROD version was 0.0 and the run number is used to disambiguate between them.
16 """
17
18 boundary_run12 = 230000
19 boundary_run23 = 368000
20
21 import eformat
22 from libpyeformat_helper import SubDetector
23 bs = eformat.istream(filename)
24
25 rodVersionM = None
26 rodVersionL = None
27 # Find the first HLT ROBFragment in the first event
28 for robf in bs[0]:
29 if robf.rob_source_id().subdetector_id()==SubDetector.TDAQ_HLT:
30 rodVersionM = robf.rod_minor_version() >> 8
31 rodVersionL = robf.rod_minor_version() & 0xFF
32 log.debug("HLT ROD minor version from input file is %d.%d", rodVersionM, rodVersionL)
33 break
34
35 # Case 1: failed to read ROD version
36 if rodVersionM is None or rodVersionL is None:
37 log.warning("Cannot determine HLT ROD version from input file, falling back to run-number-based decision")
38 # Case 2: ROD version indicating Run 3
39 elif rodVersionM >= 1:
40 log.info("Determined EDMVersion to be 3, because running on BS file with HLT ROD version %d.%d",
41 rodVersionM, rodVersionL)
42 return 3
43
44 # Case 3: ROD version indicating Run 1 or 2 - use run number to disambiguate
45 runNumber = bs[0].run_no()
46 log.debug("Read run number %s", runNumber)
47
48 if not runNumber or runNumber <= 0:
49 log.warning("Cannot determine EDM version because run number %s is invalid. ", runNumber)
50 return None
51 elif runNumber < boundary_run12:
52 # Run-1 data
53 log.info("Determined EDMVersion to be 1 based on BS file run number (runNumber < %d)",
54 boundary_run12)
55 return 1
56 elif runNumber < boundary_run23:
57 # Run-2 data
58 log.info("Determined EDMVersion to be 2 based on BS file run number (%d < runNumber < %d)",
59 boundary_run12, boundary_run23)
60 return 2
61 else:
62 # Run-3 data
63 log.info("Determined EDMVersion to be 3 based on BS file run number (runNumber > %d)",
64 boundary_run23)
65 return 3
66
67

◆ getHostName()

python.Utils.getHostName ( )

Definition at line 60 of file InnerDetector/InDetExample/InDetBeamSpotExample/python/Utils.py.

60def getHostName():
61 import platform
62 return platform.node()

◆ getMCCampaign()

python.Utils.getMCCampaign ( files)

Definition at line 38 of file Tools/Campaigns/python/Utils.py.

38def getMCCampaign(files):
39 # Auto-configure from file
40 from AthenaConfiguration.AutoConfigFlags import GetFileMD
41 metadata = GetFileMD(files)
42 mc_campaign = Campaign(metadata.get('mc_campaign', ''))
43 project_name = metadata.get('project_name', '')
44 run_numbers = metadata.get('runNumbers', [])
45
46 if mc_campaign is not Campaign.Unknown:
47 return mc_campaign
48
49 if run_numbers:
50 mc_campaign = campaign_runs.get(run_numbers[0], Campaign.Unknown)
51
52 # MC-equivalent projects for data
53 if 'data25' in project_name:
54 return Campaign.MC23g
55 elif 'data24' in project_name:
56 return Campaign.MC23e
57 elif 'data23' in project_name:
58 return Campaign.MC23c
59 elif 'data22' in project_name:
60 return Campaign.MC21a
61 elif 'data18' in project_name:
62 return Campaign.MC20e
63 elif 'data17' in project_name:
64 return Campaign.MC20d
65 elif 'data16' in project_name or 'data15' in project_name:
66 return Campaign.MC20a
67
68 return mc_campaign
69
70# Get data year corresponding to MC campaign

◆ getRunFromName()

python.Utils.getRunFromName ( name,
default = '',
asInt = False )
Extract the run number from a file name whose first part is a standard dataset name.
   If the run number cannot be determined, returns the default value, which, by
   default is an empty string.

Definition at line 13 of file InnerDetector/InDetExample/InDetBeamSpotExample/python/Utils.py.

13def getRunFromName(name,default='',asInt=False):
14 """Extract the run number from a file name whose first part is a standard dataset name.
15 If the run number cannot be determined, returns the default value, which, by
16 default is an empty string."""
17 name = os.path.basename(name)
18 try:
19 run = re.sub('^0*','',name.split('.')[1])
20 if not re.search(r'^\d+$',run):
21 # Probably wasn't the run number, so use the default instead
22 run = default
23 else:
24 run = int(run) if asInt else run
25 except Exception:
26 run = default
27 return run
28
29

◆ getUserName()

python.Utils.getUserName ( default = 'UNKNOWN')
Get login name in a platform-independent manner.

Definition at line 48 of file InnerDetector/InDetExample/InDetBeamSpotExample/python/Utils.py.

48def getUserName(default='UNKNOWN'):
49 """Get login name in a platform-independent manner."""
50 user = ''
51 try:
52 user = os.getlogin() # this doesn't seem to work with acrontab
53 except Exception:
54 pass
55 if not user:
56 user = os.getenv('USER',default)
57 return user
58
59

◆ loadDefaultComps()

python.Utils.loadDefaultComps ( allcomps)
Attempts to load all default components (those that are not actually configured)

Definition at line 10 of file Control/AthenaConfiguration/python/Utils.py.

10def loadDefaultComps(allcomps):
11 """Attempts to load all default components (those that are not actually configured)"""
12 loadedSome = False
13 def __load(comp, prop):
14 descr = comp._descriptors[prop]
15 if descr.default.getType() == "":
16 return
17 try:
18 childComp = CompFactory.getComp(descr.default.getType())(descr.default.getName())
19 comp._properties[prop] = childComp
20 nonlocal loadedSome
21 loadedSome = True
22 except Exception:
23 msg=logging.getLogger('loadDefaultComps')
24 msg.warning("Default component %s can not be loaded", descr.default.typeAndName )
25 pass
26
27 for comp in allcomps:
28 for propName,value in comp._descriptors.items():
29 if propName in comp._properties: continue
30 if isinstance(value.default, GaudiHandle):
31 __load(comp, propName )
32
33 if loadedSome:
34 loadDefaultComps(allcomps)
35

◆ setupLoggingLevels()

python.Utils.setupLoggingLevels ( flags,
ca )
Read the Exec.*MessageComponents flags and modify OutputLevel of component(s).

The specification of components uses the Python `fnmatch` library and resembles UNIX paths.
An event algorithm MyAlgo/MyInstance has the following path:
    MasterSeq/AthAllAlgSeq/AthAlgSeq/MyAlgo/MyInstance
A private tool MyTool of name ToolInstance used by that algorithm:
    MasterSeq/AthAllAlgSeq/AthAlgSeq/MyAlgo/MyInstance/MyTool/ToolInstance
A public tool:
    ToolSvc/MyTool/ToolInstance

The path specification can take the following forms:
    '*/ToolInstance'      : all tools that have matching instance name
    '*/MyTool/*'          : all instances of type MyTool
    '*/MyAlgo/MyInstance' : specific algorithm instance
    '*/MyAlgo/*'          : all instances of the specific algorithm class
    '*/AthAlgSeq/*'       : all algorithms of the given sequence
    'ToolSvc/My*/*'       : all public tools with instance name starting with "My"

The modifications to the OutputLevel are applied in the order ERROR to VERBOSE, i.e.
it is possible to set higher verbosities with more specific selections.

Each setting can be either a string or a list of strings. If the component path contains
no '/' it is assumed to be a plain component name. In this case, the OutputLevel is set
using the property MessageSvc.setDebug or equivalent. This works also for converters, which
do not have any properties.

Definition at line 50 of file Control/AthenaConfiguration/python/Utils.py.

50def setupLoggingLevels(flags, ca):
51 """Read the Exec.*MessageComponents flags and modify OutputLevel of component(s).
52
53 The specification of components uses the Python `fnmatch` library and resembles UNIX paths.
54 An event algorithm MyAlgo/MyInstance has the following path:
55 MasterSeq/AthAllAlgSeq/AthAlgSeq/MyAlgo/MyInstance
56 A private tool MyTool of name ToolInstance used by that algorithm:
57 MasterSeq/AthAllAlgSeq/AthAlgSeq/MyAlgo/MyInstance/MyTool/ToolInstance
58 A public tool:
59 ToolSvc/MyTool/ToolInstance
60
61 The path specification can take the following forms:
62 '*/ToolInstance' : all tools that have matching instance name
63 '*/MyTool/*' : all instances of type MyTool
64 '*/MyAlgo/MyInstance' : specific algorithm instance
65 '*/MyAlgo/*' : all instances of the specific algorithm class
66 '*/AthAlgSeq/*' : all algorithms of the given sequence
67 'ToolSvc/My*/*' : all public tools with instance name starting with "My"
68
69 The modifications to the OutputLevel are applied in the order ERROR to VERBOSE, i.e.
70 it is possible to set higher verbosities with more specific selections.
71
72 Each setting can be either a string or a list of strings. If the component path contains
73 no '/' it is assumed to be a plain component name. In this case, the OutputLevel is set
74 using the property MessageSvc.setDebug or equivalent. This works also for converters, which
75 do not have any properties.
76 """
77
78 def __tolist(d):
79 return ([d] if d else []) if isinstance(d, str) else d
80
81 for flag_val, level in ((flags.Exec.ErrorMessageComponents, 'ERROR'),
82 (flags.Exec.WarningMessageComponents, 'WARNING'),
83 (flags.Exec.InfoMessageComponents, 'INFO'),
84 (flags.Exec.DebugMessageComponents, 'DEBUG'),
85 (flags.Exec.VerboseMessageComponents, 'VERBOSE')):
86 for c in __tolist(flag_val):
87 if "/" in c:
88 ca.foreach_component(c).OutputLevel = getattr(Constants, level)
89
90 # a plain component name is given
91 else:
92 msgSvc = ca.getService("MessageSvc") # should exist by the time this code runs
93 getattr(msgSvc, f'set{level.title()}').append(c) # e.g. setDebug property

Variable Documentation

◆ __all__

list python.Utils.__all__ = [ 'unixtools' ]
private

Definition at line 9 of file Control/AthenaCommon/python/Utils/__init__.py.

◆ __author__

str python.Utils.__author__ = 'Wim Lavrijsen (WLavrijsen@lbl.gov)'
private

Definition at line 7 of file Control/AthenaCommon/python/Utils/__init__.py.

◆ __version__

str python.Utils.__version__ = '1.0.0'
private

Definition at line 6 of file Control/AthenaCommon/python/Utils/__init__.py.

◆ campaign_runs

dict python.Utils.campaign_runs
Initial value:
1= {
2 284500: Campaign.MC20a,
3 300000: Campaign.MC20d,
4 310000: Campaign.MC20e,
5 410000: Campaign.MC21a,
6}

Definition at line 30 of file Tools/Campaigns/python/Utils.py.

◆ log

python.Utils.log = logging.getLogger('TrigEDMConfig')