9 Python helper module for managing CREST DB connections and TileCalibBlobs.
12 import os, cppyy, base64, json
14 from PyCool
import cool
15 Blob = cppyy.gbl.coral.Blob
17 from pycrest.api.crest_api
import CrestApi
19 from TileCalibBlobObjs.Classes
import TileCalibUtils, TileCalibDrawerCmt, \
20 TileCalibDrawerInt, TileCalibDrawerOfc, TileCalibDrawerBch, \
21 TileCalibDrawerFlt, TileCalibType
22 from TileCalibBlobPython
import TileCalibTools
25 from TileCalibBlobPython.TileCalibLogger
import TileCalibLogger, getLogger
42 MAXRUNLUMI = (MAXRUN<<32)+MAXLBK
49 TileCalibBlobReader is a helper class, managing the details of CREST interactions for
50 the user of TileCalibBlobs.
54 def __init__(self, db, folder='', tag='', run=None, lumi=0, modmin=0, modmax=275):
57 - db : server connection string or file name
58 - folder: full folder path
59 - tag : The folder tag, e.g. \"UPD4-24\" or full tag
60 - run : Run number (if known)
61 - lumi : Lumi block number
62 - modmin: Minimal module (COOL channel number)
63 - modmax: Maximal module (COOL channel number)
66 TileCalibLogger.__init__(self,
"TileBlobReader")
82 self.
__remote = ((
"http://" in db)
or (
"https://" in db)
or (
"CREST" in db))
84 if 'http' not in self.
__db:
85 self.
__db = os.getenv(db,os.getenv(
'CREST_HOST',os.getenv(
'CREST_SERVER_PATH',
'http://crest-j23.cern.ch:8080/api-v5.0')))
86 self.log().
info(
'Host %s' , (self.
__db))
88 socks = os.getenv(
'CREST_SOCKS',
'False')
91 if run
is not None and lumi
is not None:
92 log.info(
"Initializing for run %d, lumiblock %d", run,lumi)
95 self.log().
info(
'File %s' , (self.
__db))
99 with open(self.
__db,
'r')
as the_file:
100 jdata = json.load(the_file)
104 except Exception
as e:
105 self.log().critical( e )
110 if globalTag==
'CURRENT' or globalTag==
'UPD4' or globalTag==
'':
111 globalTag=TileCalibTools.getAliasFromFile(
'Current')
112 log.info(
"Resolved CURRENT globalTag to \'%s\'", globalTag)
113 elif globalTag==
'CURRENTES' or globalTag==
'UPD1':
114 globalTag=TileCalibTools.getAliasFromFile(
'CurrentES')
115 log.info(
"Resolved CURRENT ES globalTag to \'%s\'", globalTag)
116 elif globalTag==
'NEXT':
117 globalTag=TileCalibTools.getAliasFromFile(
'Next')
118 log.info(
"Resolved NEXT globalTag to \'%s\'", globalTag)
119 elif globalTag==
'NEXTES':
120 globalTag=TileCalibTools.getAliasFromFile(
'NextES')
121 log.info(
"Resolved NEXT ES globalTag to \'%s\'", globalTag)
122 globalTag=globalTag.replace(
'*',
'')
125 for f
in folder.split(
'/'):
126 prefix+=f.capitalize()
128 prefix=prefix.strip(
'-').
split(
'-')[0]
129 if prefix.startswith(
'Calo'):
130 prefix=
'CALO'+prefix[4:]
131 if 'UPD1' in globalTag
or 'UPD4' in globalTag
or 'COND' not in globalTag:
133 tag=prefix+
'-'+globalTag
134 self.log().
info(
"Resolved localTag \'%s\' to folderTag \'%s\'", globalTag,tag)
135 elif folder!=
'' and not (globalTag.startswith(
'Tile')
or globalTag.startswith(
'CALO')):
137 if tag.startswith(
'Calo'):
139 self.log().
info(
"Resolved localTag \'%s\' to folderTag \'%s\'", globalTag,tag)
142 self.log().
info(
"Use localTag \'%s\' as is", tag)
147 raise Exception(
"globalTag %s not found" % (globalTag) )
149 for i
in range(tags[
'size']):
150 t=tags[
'resources'][i][
'tag_name']
151 l=tags[
'resources'][i][
'label']
152 if (prefix!=
'' and t.startswith(prefix))
or l==folder:
154 self.log().
info(
"Resolved globalTag \'%s\' to folderTag \'%s\'", globalTag,tag)
161 run_lumi1=
str((since[0]<<32)+since[1]+1)
162 run_lumi2=
str((until[0]<<32)+until[1]+1)
163 MAXRUNLUMI1=
str(MAXRUNLUMI+1)
164 iovs1=self.
__api_instance.select_iovs(self.
__tag,
"0",run_lumi1,sort=
'id.since:DESC,id.insertionTime:DESC',size=1,snapshot=0)
165 iovs2=self.
__api_instance.select_iovs(self.
__tag,run_lumi2,MAXRUNLUMI1,sort=
'id.since:ASC,id.insertionTime:DESC',size=1,snapshot=0)
166 since1=0
if iovs1[
'size']==0
else iovs1[
'resources'][0][
'since']
167 until1=MAXRUNLUMI
if iovs2[
'size']==0
else iovs2[
'resources'][0][
'since']
168 iovs=self.
__api_instance.select_iovs(self.
__tag,
str(since1),
str(until1),sort=
'id.since:ASC,id.insertionTime:DESC',size=999999,snapshot=0)
171 raise Exception(
"IOV for tag %s IOV [%s,%s] - (%s,%s) not found" % (self.
__tag,since[0],since[1],until[0],until[1]) )
173 for i
in range(iovs[
'size']):
174 iov=iovs[
'resources'][i]
175 runS=iov[
'since']>>32
176 lumiS=iov[
'since']&0xFFFFFFFF
177 iovList.append((runS,lumiS))
182 run_lumi1=
str((runlumi[0]<<32)+runlumi[1]+1)
183 MAXRUNLUMI1=
str(MAXRUNLUMI+1)
184 iovs1=self.
__api_instance.select_iovs(self.
__tag,
"0",run_lumi1,sort=
'id.since:DESC,id.insertionTime:DESC',size=1,snapshot=0)
185 iovs2=self.
__api_instance.select_iovs(self.
__tag,run_lumi1,MAXRUNLUMI1,sort=
'id.since:ASC,id.insertionTime:DESC',size=1,snapshot=0)
187 raise Exception(
"IOV for tag %s run,lumi (%s,%s) not found" % (self.
__tag,runlumi[0],runlumi[1]) )
189 iov=iovs1[
'resources'][0]
192 lumiS=since&0xFFFFFFFF
193 until=MAXRUNLUMI
if iovs2[
'size']==0
else iovs2[
'resources'][0][
'since']
195 lumiU=until&0xFFFFFFFF
196 hash=iov[
'payload_hash']
199 self.log().
info(
'IOV [%d,%d] - (%d,%d)' , runS,lumiS,runU,lumiU)
200 self.log().
info(
'Insertion time %s' , iov[
'insertion_time'])
201 self.log().
info(
'Hash %s' , hash)
203 jdata=json.loads(payload)
214 blob=jdata[
str(chan)][0]
219 blob=jdata[
'1000'][0]
227 since = (runlumi[0][0]<<32) + runlumi[0][1]
228 until = (runlumi[1][0]<<32) + runlumi[1][1]
233 point = (runlumi[0]<<32) + runlumi[1]
234 inrange = point>=self.
__iov[0]
and point<self.
__iov[1]
246 if b64string
is None or len(b64string)==0:
250 blob1 = base64.decodebytes(bytes(b64string,
'ascii'))
257 if b64string
is None or len(b64string)==0:
261 blob1 = base64.decodebytes(bytes(b64string,
'ascii'))
267 if typeName==
'TileCalibDrawerFlt':
269 self.log().
debug(
"typeName = Flt " )
270 elif typeName==
'TileCalibDrawerInt':
272 self.log().
debug(
"typeName = Int " )
273 elif typeName==
'TileCalibDrawerBch':
275 self.log().
debug(
"typeName = Bch " )
276 elif typeName==
'TileCalibDrawerOfc':
278 self.log().
debug(
"typeName = Ofc " )
280 raise Exception(
"Invalid blob type requested: %s" % typeName )
284 def getDrawer(self,ros, mod, runlumi=None, dbg=False, useDefault=True):
294 if (chanNum>=0
and chanNum<len(self.
__drawer)):
296 if not useDefault
and drawer
is None:
298 while drawer
is None:
300 if ros==0
and drawer==0:
301 raise Exception(
'No default available')
307 elif (chanNum == 1000):
310 raise Exception(
"Invalid drawer requested: %s %s" % (ros,mod) )
320 return "<no comment found>"
325 Returns a default drawer number (among first 20 COOL channels) for any drawer in any partition
328 if drawer<=4
or drawer==12
or drawer>=20:
334 elif ros==1
or ros==2:
337 OffsetEBA = [ 0, 0, 0, 0, 0, 0, 3, 2,
338 0, 0, 0, 0, 7, 6, 5, 7,
339 7, 6, 6, 7, 0, 0, 0, 2,
340 3, 0, 0, 0, 0, 0, 0, 0,
341 0, 0, 0, 0, 0, 0, 1, 1,
342 1, 1, 2, 3, 0, 0, 0, 0,
343 0, 0, 0, 0, 3, 2, 1, 1,
344 1, 1, 0, 0, 0, 0, 0, 0]
345 drawer1 = 12 + OffsetEBA[drawer]
347 OffsetEBC = [ 0, 0, 0, 0, 0, 0, 3, 2,
348 0, 0, 0, 0, 7, 6, 6, 7,
349 7, 5, 6, 7, 0, 0, 0, 2,
350 3, 0, 0, 3, 4, 0, 3, 4,
351 0, 4, 3, 0, 4, 3, 1, 1,
352 1, 1, 2, 3, 0, 0, 0, 0,
353 0, 0, 0, 0, 3, 2, 1, 1,
354 1, 1, 0, 0, 0, 0, 0, 0]
355 drawer1 = 12 + OffsetEBC[drawer]