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