ATLAS Offline Software
DataQuality/DQUtils/python/utils.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
4 """
5 Utility module for dataquality specific things
6 """
7 
8 
9 from sys import stdout
10 
11 BUILTIN_NAMES = {"True", "False"}
12 
13 def worst_status(iovs, *dummy_args):
14  """
15  Return the worst status of multiple IoVs. (Different from worst_of).
16  """
17  if len(iovs) == 1: return iovs[0][2]
18  statuses = zip(*iovs)[2]
19  return worst(statuses)
20 
21 def worst(iterable):
22  states = set(iterable)
23 
24  if not states: return None
25  if -1 in states: return -1
26  elif 1 in states: return 1
27  elif 2 in states: return 2
28  elif 3 in states: return 3
29  elif 0 in states: return 0
30  elif None in states: return None
31 
32  raise RuntimeError("Invalid Code present on database?")
33 
34 WHITE = None
35 GREY = 0
36 
37 def first(iterable):
38  """
39  Return the first filled (and not grey) flag
40  """
41  result = WHITE
42  for element in iterable:
43  if element not in [WHITE, GREY]:
44  return element
45  elif element == GREY:
46  result = GREY
47  return result
48 
49 def best(iterable, priorities=[3, 2, 1, -1, 0]):
50  """
51  a min() function whose priorities can be chosen
52  """
53  return worst(iterable, [3, 2, 1, -1, 0])
54 
55 def best_status(iovs, *dummy_args):
56  """
57  Return the worst status. (Different from worst_of).
58  """
59  if len(iovs) == 1: return iovs[0][2]
60  statuses = zip(*iovs)[2]
61  return best(statuses)
62 
63 def iovs_in_time(wanted_start_time, wanted_end_time):
64  """
65  Return a function which selects iovs dependent on they occured
66  in the desired time
67  """
68 
69  # TODO: Test that this logic gives the same result as
70  # wanted_start < iov_end and wanted_end > iov_start
71 
72  def test_one(iov):
73  start_time, end_time, status = iov
74  # Start or end of IoV lies within desired region
75 
76  return ( # Desired start or end time lies within block
77  start_time <= wanted_start_time <= end_time or
78  start_time <= wanted_end_time <= end_time or
79  # Block lies within desired start or end time
80  wanted_start_time <= start_time <= wanted_end_time or
81  wanted_start_time <= end_time <= wanted_end_time)
82 
83  return test_one
84 
86  """
87  Return ROOT version tuple
88  """
89  from ROOT import gROOT
90  version_code = gROOT.RootVersionCode()
91  return (version_code >> 16, version_code >> 8 & 0xFF, version_code & 0xFF)
92 
93 def fix_iovkey(iovkey):
94  """
95  If an IoV starts on the 0th lumiblock, then move it forward one.
96  """
97  return iovkey if iovkey & 0xFFFFFFFF else iovkey+1
98 
99 def make_functor(expression, location, global_env={}, input_translation=None):
100  """
101  Compile an expression, returning the variables used and a functor which can
102  be called with the namespace in which the expression is run
103 
104  `expression` is a single python expression to be evaluated
105  `location` is a string describing where the snippet of code came from
106  (in case of exceptions, for debugging)
107  `global_env` is the global environment in which the expression is executed
108  `input_translation` is an optional function which is executed on the
109  functor's arguments before it is executed.
110  """
111  compiled = compile(expression, location, "eval")
112 
113  provided_vars = set(global_env.keys())
114  variables = sorted(set(compiled.co_names) - BUILTIN_NAMES - provided_vars)
115  if input_translation:
116  def functor(locals_={}):
117  return eval(compiled, global_env, input_translation(locals_))
118  else:
119  def functor(locals_={}):
120  return eval(compiled, global_env, locals_)
121  return variables, functor
122 
124  if isinstance(x, float):
125  return "%.3f" % x
126  return x
127 
128 def pprint_objects(objects, where=stdout):
129  """
130  Pretty print a list of IoV-results
131  """
132  for obj in objects:
133  since, until = obj[:2]
134  args = since, until, "(%s)" % ", ".join(map(str, map(make_floats_pretty, obj[2:])))
135  print("[%r -> %r) : %s" % args, file=where)
136 
137 def describe_colour(colour):
138  mapping = {
139  None: "White",
140  -1: "Black",
141  0: "Grey",
142  1: "Red",
143  2: "Yellow",
144  3: "Green",
145  }
146  if colour not in mapping:
147  raise RuntimeError("Unknown colour code: %s" % colour)
148  return mapping[colour]
python.utils.fix_iovkey
def fix_iovkey(iovkey)
Definition: DataQuality/DQUtils/python/utils.py:93
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename R::value_type > sorted(const R &r, PROJ proj={})
Helper function to create a sorted vector from an unsorted range.
python.utils.worst
def worst(iterable)
Definition: DataQuality/DQUtils/python/utils.py:21
python.utils.make_floats_pretty
def make_floats_pretty(x)
Definition: DataQuality/DQUtils/python/utils.py:123
python.utils.get_root_version
def get_root_version()
Definition: DataQuality/DQUtils/python/utils.py:85
python.utils.worst_status
def worst_status(iovs, *dummy_args)
Definition: DataQuality/DQUtils/python/utils.py:13
python.utils.describe_colour
def describe_colour(colour)
Definition: DataQuality/DQUtils/python/utils.py:137
python.utils.pprint_objects
def pprint_objects(objects, where=stdout)
Definition: DataQuality/DQUtils/python/utils.py:128
python.utils.first
def first(iterable)
Definition: DataQuality/DQUtils/python/utils.py:37
python.utils.best
def best(iterable, priorities=[3, 2, 1, -1, 0])
Definition: DataQuality/DQUtils/python/utils.py:49
python.utils.best_status
def best_status(iovs, *dummy_args)
Definition: DataQuality/DQUtils/python/utils.py:55
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
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.utils.iovs_in_time
def iovs_in_time(wanted_start_time, wanted_end_time)
Definition: DataQuality/DQUtils/python/utils.py:63
python.utils.make_functor
def make_functor(expression, location, global_env={}, input_translation=None)
Definition: DataQuality/DQUtils/python/utils.py:99