3 from __future__
import with_statement, division
5 import logging; log = logging.getLogger(
"DCSCalculator2.variable")
7 from DQUtils
import fetch_iovs
8 from DQUtils.general
import timer
9 from DQUtils.sugar
import define_iov_type, RunLumi, IOVSet, RANGEIOV_VAL
10 from DQUtils.events
import quantize_iovs_slow_mc
12 import DCSCalculator2.config
as config
13 from DCSCalculator2.libcore
import map_channels
14 from DCSCalculator2.consts
import ( RED, YELLOW, GREEN )
19 "Stores the state of a single input channel, whether it is working or not"
23 "Similar to GoodIOV but stores a DQ code instead of good/bad state"
30 def DefectIOVFull(channel, present, comment, recoverable=False, user='sys:defectcalculator
'):
31 "Stores a defect IOV with all fields"
35 Class which encapsulates logic behind an input variable.
37 This class is responsible for:
39 * Reading data from COOL / CoraCOOL
40 * Evaluating the 'good' state of a variable once read from the database
41 * Quantizing the state from time-based intervals of validity to lumiblock
43 It is subclassed for configuration variables and "global" variables.
48 is_config_variable =
False
49 timewise_folder =
True
51 def __init__(self, folder_name, evaluator, **kwargs):
52 if not hasattr(self,
'input_db'):
56 if not hasattr(self,
"fetch_args"):
58 self.__dict__.
update(kwargs)
63 Useful for verifying if input variables have changed.
72 def read(self, query_range, folder_base, folder_name):
74 Read the relevant data from COOL for this variable
76 if folder_name.startswith(
"/"):
77 folder_path = folder_name
80 folder_path =
"/".
join((folder_base, folder_name))
82 log.info(
"Querying COOL folder %s", folder_path)
84 if config.opts.check_input_time:
89 newdbstring = self.
input_db.rsplit(
'/', 1)[0]
92 if config.opts.input_database.startswith(
'sqlite'):
93 self.
fetch_args[
'database'] = config.opts.input_database
95 self.
fetch_args[
'database'] = (
'%s/%s' % (newdbstring, config.opts.input_database))
97 log.debug(
"Fetching with args: %r", self.
fetch_args)
109 original_length=len(
list(iovs))
110 if folder_path==
'/TGC/DCS/PSHVCHSTATE':
111 for i
in range(original_length-1, -1, -1):
112 if list(iovs)[i].channel
in range(5504,5552)
or list(iovs)[i].channel
in range(7362,7411):
115 if config.opts.check_input_time:
118 if log.isEnabledFor(logging.INFO):
119 input_hash =
hash(iovs)
121 log.info(
" -> Input hash: % 09x (len=%i)", input_hash, len(iovs))
127 Logs the first and last insertion times of the IoVs, and their ranges.
129 first_value, last_value = iovs.range_iov
131 log.info(
"Times for %s:", self)
132 log.info(
" IoV : (first)%26s (last)%26s",
133 first_value.date, last_value.date)
135 if not hasattr(iovs.first,
"insertion_time"):
136 log.info(
"Insertion time not available")
138 insertion_times = [iov.insertion_time
for iov
in iovs]
139 log.info(
" Insertion: (first)%26s (last)%26s",
140 min(insertion_times),
max(insertion_times))
144 By default, do nothing. Overloaded by DCSC_Variable_With_Mapping.
150 Quantize "good state" timewise-iovs to lumiblocks.
151 OUT_OF_CONFIG gets priority over BAD if BAD and OUT_OF_CONFIG overlap
155 iovs = [iovs_
for c, iovs_
in sorted(iovs.by_channel.items())]
157 def quantizer (iovs):
158 return min(i.good
for i
in iovs)
if iovs
else None
163 for iov
in iovs
if iov[0].run == iov[1].run)
167 Determine if one input iov is good.
175 Determine whether each iov signifies a good or bad state.
182 Calculate LB-wise "good" states
188 query_range =
RANGEIOV_VAL(lbtime.first.since, lbtime.last.until)
190 a, b = lbtime.first, lbtime.last
195 iovs = self.
read(query_range, subdetector.folder_base, self.
folder_name)
209 with timer(
"Quantize %s (%i iovs over %i lbs)" %
227 A variable which needs channel ids to be remapped before further use
236 This class over-rides the behaviour for evaluating the "goodness" of an
237 input channel. It allows for an intermediate state (caution) between good
242 def __init__(self, folder_name, evaluator, caution_evaluator=None, **kwargs):
243 super(DCSC_Global_Variable, self).
__init__(folder_name, evaluator, **kwargs)
249 Determine DQ colour for this global variable iov.
261 return CodeIOV(iov.since, iov.until, iov.channel, state)
265 Needs a different quantizer. (The default DQ quantizer will do)
267 iovs = [iovs_
for c, iovs_
in sorted(iovs.by_channel.items())]
273 if iov[0].run == iov[1].run)
277 Global variable which emits defects
281 def __init__(self, folder_name, evaluator, **kwargs):
282 super(DCSC_Defect_Global_Variable, self).
__init__(folder_name, evaluator, **kwargs)
286 if len(current_events) == 0:
295 iovs = [iovs_
for c, iovs_
in sorted(iovs.by_channel.items())]
297 DCSC_Defect_Global_Variable.quantizing_function)
298 return IOVSet(
DefectIOV(*iov, comment=
'Automatically set')
301 if iov[0].run == iov[1].run)