4 from DQDefects
import DefectsDB
5 from DQUtils.sugar
import IOVSet, RunLumiType, RunLumi, define_iov_type
6 from DQUtils
import fetch_iovs, process_iovs
11 """Defect exception class."""
16 "IOV type to solidify upon with an extra present element"
19 class IDBSDefectWriter:
21 Class for writing BS defects to an sqlite file
24 def __init__(self,fileName, forceNew=False, dbName='IDBSDQ', tag='nominal', user='sys:dqBeamSpot
'):
26 Initialise database connection
34 if not fileName: fileName =
'tmp.'+
str(os.getpid())+
'.db'
36 self.
connect(fileName, forceNew, dbName, tag)
42 Delete db to clear connection
45 os.system(
'[[ -f tmp.%s.db ]] && rm tmp.%s.db' %(os.getpid(), os.getpid()))
50 def connect(self, fileName, forceNew=False, dbName='IDBSDQ', tag='nominal'):
52 Open connection to defect DB
55 connString =
'sqlite://;schema=%s;dbname=%s' % (fileName,dbName)
57 if forceNew
and os.path.exists(fileName):
60 self.
db = DefectsDB(connString, read_only=
False, create=
True, tag=(tag,
'HEAD'))
72 def add(self,runMin=0, runMax=(1 << 31)-1, lbMin=0, lbMax=(1 << 32)-1):
74 Add iovs which are NOT defective to the list
75 Note, lbMax is exclusive here (and inclusive when shown on defect web page).
79 since = RunLumiType((runMin << 32)+lbMin)
80 until = RunLumiType((runMax << 32)+lbMax)
89 Complete a list of IoVs to cover all LBs in a run, treating empty ones as having 'emptyState'
94 run_lbs =
fetch_iovs(
"EOR", runs=(runMin, runMax), what=[], with_channel=
False)
105 print (
"WARNING: No LBs in run according to EOR_Params - are we running before the run has finished?")
107 def lbsStartAtOne(iov):
108 "Change LBs starting at 0 to start at 1"
109 return iov._replace(since=
RunLumi(iov.since.run, 1))
112 run_lbs = [lbsStartAtOne(iov)
for iov
in run_lbs
if iov.until.lumi > 1]
124 for since, until, (run_lb, iov, dbDefect)
in process_iovs(run_lbs, self.
iovs.solidify(DEFECTIOV_VAL), defectsCurrentlyInDb):
125 if not run_lb:
continue
136 if dbDefect
and dbDefect.present:
139 iovs.add(since, until, self.
defect,
False)
142 iovs.add(since, until, self.
defect,
True)
149 Write all defects to the database. If 'nonpresent' is True then write the absent ones too
152 with self.
db.storage_buffer:
153 for since, until, defect, present
in self.
iovs:
154 if not present
and not nonpresent:
continue
156 self.
_writeDefect(defect, since, until, present = present)
158 def _writeDefect(self,defect, since, until, tag='nominal', description='', comment='', present=True, recoverable=False):
160 Write a single defect to the database
163 if defect
not in self.
db.defect_names:
164 self.
db.create_defect(defect, description)
166 self.
db.insert(defect, since, until, comment, self.
user, present, recoverable)
170 def dump(self, filename = None):
172 Dump defects to a file given by filename or stdout if no filename given
175 from DQUtils.utils
import pprint_objects
177 if filename
is not None:
178 f =
open(filename.replace(
'.db',
'.txt'),
"w")
187 print (
'\nNo DQ defects')
198 Container for beamspot DQ defects from COOL
201 defectBitPos = [
'UNKNOWN',
'ID_BS_2010YELLOW',
'ID_BS_RUNAVERAGE',
'ID_BS_PARAMETERSTEP',
202 'ID_BS_NOBEAMSPOT',
'ID_BS_2010RED',
'LUMI_VDM']
205 def __init__(self, database='COOLOFL_GLOBAL/CONDBR2', tag='HEAD', debug=False):
208 Initialise database connection
223 Delete db to clear connection
232 Open connection to defect DB
235 self.
db = DefectsDB(self.
database, read_only=
True, create=
False, tag=self.
tag)
236 self.
idbsDefects = [d
for d
in self.
db.defect_names
if d.startswith(
'ID_BS_')
or d ==
'LUMI_VDM']
245 List of all possible beamspot defects
250 def defect(self, run, lb, channels=None):
252 Get list of DQ defects for a particular run and lb, caching the result for the latest (succesful) run
254 from InDetBeamSpotExample.DQUtilities import IDBSDefectData
255 idbs = IDBSDefectData()
256 idbs.defect(167661,372)
258 channels is the list of defects to look for (defaults to all ID_BS defects)
267 since = (run << 32)+lbMin
268 until = (run << 32)+lbMax
274 print (run, lb, defects)
278 iovs = self.
db.
retrieve(since, until, channels=channels)
282 print (
"Unable to access folder with given parameters")
287 chans, self.
iovsets = iovs.chans_iovsets
294 print (run, lb, defects)
300 Get the DQ defects for the given LB from the full run info
305 if since.lumi <= lb < until.lumi:
307 defects = [state.channel
for state
in states
if state]
314 Return the maximal list of defects for a given range. lbEnd is exclusive
318 for lb
in range(lbStart, lbEnd):
319 defects.extend(self.
defect(run, lb, channels=channels))
325 Dump DQ info for a particular run (useful in reprocessing to compare new to old)
334 since = (run << 32)+lbMin
335 until = (run << 32)+lbMax
338 iovs = self.
db.
retrieve(since, until, channels=channels, nonpresent=
True)
342 print (
"Unable to access folder with given parameters")
351 Class to enocode and decode IDBS defects. No instances fo this class can be instanciated
352 and it bascially just acts as a poor man's namespace (should really be a module)
355 defectBitPos = [
'UNKNOWN',
'ID_BS_2010YELLOW',
'ID_BS_RUNAVERAGE',
'ID_BS_PARAMETERSTEP',
356 'ID_BS_NOBEAMSPOT',
'ID_BS_2010RED',
'LUMI_VDM']
360 """Encode defect as an int. If defect is unknown raise error"""
362 if defect
not in IDBSDefectEncoding.defectBitPos:
363 raise DefectError (
'ERROR: Unknown defect %s encountered' % defect)
365 return (1 << IDBSDefectEncoding.defectBitPos.index(defect))
369 """Encode list of defects as an int. If no defect (empty list) returns 0 for bakcward compatibility"""
374 """Decode int as list of defects. Raise error if unkown defect encountered"""
377 binStr =
bin(dint)[2:][::-1]
379 if len(binStr) > len(IDBSDefectEncoding.defectBitPos):
380 raise DefectError (
"ERROR: integer %s out of range" % dint)
384 raise DefectError (
'ERROR: Unknown defect encountered')
386 return [d[0]
for d
in zip(IDBSDefectEncoding.defectBitPos,binStr)
if d[1]==
'1']