ATLAS Offline Software
Loading...
Searching...
No Matches
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"""
5Utility module for dataquality specific things
6"""
7
8
9from sys import stdout
10
11BUILTIN_NAMES = {"True", "False"}
12
13def 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
21def 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
34WHITE = None
35GREY = 0
36
37def 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
49def 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
55def 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
63def 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
93def 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
99def 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
128def 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
137def 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]
void print(char *figname, TCanvas *c1)
STL class.
STL class.
best(iterable, priorities=[3, 2, 1, -1, 0])
make_functor(expression, location, global_env={}, input_translation=None)
iovs_in_time(wanted_start_time, wanted_end_time)
worst_status(iovs, *dummy_args)
pprint_objects(objects, where=stdout)