ATLAS Offline Software
channel_mapping.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 try:
4  from CoolRunQuery.utils.AtlRunQueryLookup import DQChannelDict
5 except ImportError:
6  from warnings import warn
7  import traceback
8  traceback.print_exc()
9  warn("Failed to import DQChannelDict from CoolRunQuery. "
10  "Can't perform channel conversions.")
11  channel_mapping = {}
12  channel_names = []
13 else:
14  # Need to decouple ourselves from CRQ by making a copy, else bad things.
15  channel_mapping = DQChannelDict.copy()
16  channel_names = channel_mapping.keys()
17 
18 cm_reversed = {value: key for key, value in channel_mapping.items()}
19 channel_mapping.update(cm_reversed)
20 
21 def convert_channel(name, want_id=True, channel_mapping=channel_mapping):
22  """
23  Given a channel, return a name detector or an integer (depending on want_id)
24  """
25  from builtins import int
26  if isinstance(name, int):
27  if name not in channel_mapping:
28  raise RuntimeError("ChannelID %r is not in channel_mapping" % name)
29  return name if want_id else channel_mapping[name]
30 
31  elif isinstance(name, str):
32  if name not in channel_mapping:
33  raise RuntimeError("ChannelID %r is not in channel_mapping" % name)
34  return name if not want_id else channel_mapping[name]
35 
36  raise RuntimeError("I don't know how to convert %r into "
37  "a ChannelSelection" % name)
38 
39 def list_to_channelselection(list_, convert_channel=convert_channel,
40  as_list=False):
41  """
42  Given a list, return a channelselection.
43 
44  Does the hard work of merging together adjacent channel numbers. Will also
45  convert channel names to numbers (if the folder is using DQ channels,
46  otherwise will give an incorrect result or fail to convert the strings to
47  channelids.)
48  """
49  from PyCool import cool
50  if not list_ and as_list: return []
51  if not list_: return cool.ChannelSelection()
52  if set(map(type, list_)) != set([int]) and convert_channel:
53  list_ = map(convert_channel, list_)
54 
55  list_.sort()
56 
57  start = None
58  ranges = []
59  for this, next in zip(list_, list_[1:] + [-1]):
60  if start is None:
61  start = this
62  if next - this != 1:
63  ranges.append((start, this))
64  start = None
65 
66  if as_list:
67  return ranges
68 
69  assert len(ranges) < 50, (
70  "Cool has a 50 channel selection limit (%s selected)" % ranges)
71 
72  selection = cool.ChannelSelection(*ranges.pop(0))
73  for start, end in ranges:
74  selection.addRange(start, end)
75 
76  return selection
77 
78 def make_channelselection(cs, mapping=None):
79  """
80  Helper function which can convert a channel id, name, or list of either into
81  a cool.ChannelSelection. Includes protections for invalid channels.
82  """
83  from PyCool import cool
84  if mapping is None:
85  mapping = channel_mapping
86  if cs is None or cs == []:
87  return cool.ChannelSelection()
88  elif isinstance(cs, (int, str)):
89  channel_id = convert_channel(cs, channel_mapping=mapping)
90  return cool.ChannelSelection(channel_id, channel_id)
91  elif hasattr(cs, "__iter__"):
92  cc = lambda cs: convert_channel(cs, channel_mapping=mapping)
93  return list_to_channelselection(cs, convert_channel=cc)
94  elif isinstance(cs, cool.ChannelSelection):
95  return cs
96  raise RuntimeError("I don't know how to convert %r into a "
97  "ChannelSelection" % cs)
98 
100  """
101  Given a `folder` name, retrieve a list of channel ids, names and return a
102  dictionary which will translate in either direction.
103  """
104  channel_ids = list(folder.listChannels())
105  channel_names = folder.listChannelsWithNames()
106  channel_names = list(map(channel_names.__getitem__, channel_ids))
107  channel_dict = dict(list(zip(channel_ids, channel_names))
108  +list(zip(channel_names, channel_ids)))
109  return channel_ids, channel_names, channel_dict
110 
python.channel_mapping.make_channelselection
def make_channelselection(cs, mapping=None)
Definition: channel_mapping.py:78
python.channel_mapping.get_channel_ids_names
def get_channel_ids_names(folder)
Definition: channel_mapping.py:99
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
python.channel_mapping.list_to_channelselection
def list_to_channelselection(list_, convert_channel=convert_channel, as_list=False)
Definition: channel_mapping.py:39
ReadBadBitsFromCool.warn
warn
Definition: ReadBadBitsFromCool.py:43
python.channel_mapping.convert_channel
def convert_channel(name, want_id=True, channel_mapping=channel_mapping)
Definition: channel_mapping.py:21