4 import logging; log = logging.getLogger(
"DCSCalculator2.variable")
6 from DQUtils
import fetch_iovs
7 from DQUtils.general
import timer
8 from DQUtils.sugar
import define_iov_type, RunLumi, IOVSet, RANGEIOV_VAL
9 from DQUtils.events
import quantize_iovs_slow_mc
11 import DCSCalculator2.config
as config
12 from DCSCalculator2.libcore
import map_channels
13 from DCSCalculator2.consts
import ( RED, YELLOW, GREEN )
18 "Stores the state of a single input channel, whether it is working or not"
22 "Similar to GoodIOV but stores a DQ code instead of good/bad state"
29 def DefectIOVFull(channel, present, comment, recoverable=False, user='sys:defectcalculator
'):
30 "Stores a defect IOV with all fields"
34 Class which encapsulates logic behind an input variable.
36 This class is responsible for:
38 * Reading data from COOL / CoraCOOL
39 * Evaluating the 'good' state of a variable once read from the database
40 * Quantizing the state from time-based intervals of validity to lumiblock
42 It is subclassed for configuration variables and "global" variables.
47 is_config_variable =
False
48 timewise_folder =
True
50 def __init__(self, folder_name, evaluator, **kwargs):
51 if not hasattr(self,
'input_db'):
55 if not hasattr(self,
"fetch_args"):
57 self.__dict__.update(kwargs)
62 Useful for verifying if input variables have changed.
71 def read(self, query_range, folder_base, folder_name):
73 Read the relevant data from COOL for this variable
75 if folder_name.startswith(
"/"):
76 folder_path = folder_name
79 folder_path =
"/".
join((folder_base, folder_name))
81 log.info(
"Querying COOL folder %s", folder_path)
83 if config.opts.check_input_time:
88 newdbstring = self.
input_db.rsplit(
'/', 1)[0]
91 if config.opts.input_database.startswith(
'sqlite'):
92 self.
fetch_args[
'database'] = config.opts.input_database
94 self.
fetch_args[
'database'] = (
'%s/%s' % (newdbstring, config.opts.input_database))
96 log.debug(
"Fetching with args: %r", self.
fetch_args)
108 original_length=len(
list(iovs))
109 if folder_path==
'/TGC/DCS/PSHVCHSTATE':
110 for i
in range(original_length-1, -1, -1):
111 if list(iovs)[i].channel
in range(5504,5552)
or list(iovs)[i].channel
in range(7362,7411):
114 if config.opts.check_input_time:
117 if log.isEnabledFor(logging.INFO):
118 input_hash =
hash(iovs)
120 log.info(
" -> Input hash: % 09x (len=%i)", input_hash, len(iovs))
126 Logs the first and last insertion times of the IoVs, and their ranges.
128 first_value, last_value = iovs.range_iov
130 log.info(
"Times for %s:", self)
131 log.info(
" IoV : (first)%26s (last)%26s",
132 first_value.date, last_value.date)
134 if not hasattr(iovs.first,
"insertion_time"):
135 log.info(
"Insertion time not available")
137 insertion_times = [iov.insertion_time
for iov
in iovs]
138 log.info(
" Insertion: (first)%26s (last)%26s",
139 min(insertion_times),
max(insertion_times))
143 By default, do nothing. Overloaded by DCSC_Variable_With_Mapping.
149 Quantize "good state" timewise-iovs to lumiblocks.
150 OUT_OF_CONFIG gets priority over BAD if BAD and OUT_OF_CONFIG overlap
154 iovs = [iovs_
for c, iovs_
in sorted(iovs.by_channel.items())]
156 def quantizer (iovs):
157 return min(i.good
for i
in iovs)
if iovs
else None
162 for iov
in iovs
if iov[0].run == iov[1].run)
166 Determine if one input iov is good.
174 Determine whether each iov signifies a good or bad state.
181 Calculate LB-wise "good" states
187 query_range =
RANGEIOV_VAL(lbtime.first.since, lbtime.last.until)
189 a, b = lbtime.first, lbtime.last
194 iovs = self.
read(query_range, subdetector.folder_base, self.
folder_name)
208 with timer(
"Quantize %s (%i iovs over %i lbs)" %
226 A variable which needs channel ids to be remapped before further use
235 This class over-rides the behaviour for evaluating the "goodness" of an
236 input channel. It allows for an intermediate state (caution) between good
241 def __init__(self, folder_name, evaluator, caution_evaluator=None, **kwargs):
242 super(DCSC_Global_Variable, self).
__init__(folder_name, evaluator, **kwargs)
248 Determine DQ colour for this global variable iov.
260 return CodeIOV(iov.since, iov.until, iov.channel, state)
264 Needs a different quantizer. (The default DQ quantizer will do)
266 iovs = [iovs_
for c, iovs_
in sorted(iovs.by_channel.items())]
272 if iov[0].run == iov[1].run)
276 Global variable which emits defects
280 def __init__(self, folder_name, evaluator, **kwargs):
281 super(DCSC_Defect_Global_Variable, self).
__init__(folder_name, evaluator, **kwargs)
285 if len(current_events) == 0:
294 iovs = [iovs_
for c, iovs_
in sorted(iovs.by_channel.items())]
296 DCSC_Defect_Global_Variable.quantizing_function)
297 return IOVSet(
DefectIOV(*iov, comment=
'Automatically set')
300 if iov[0].run == iov[1].run)