ATLAS Offline Software
Loading...
Searching...
No Matches
ReadCalibFromCool.py
Go to the documentation of this file.
1#!/bin/env python
2
3# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
4#
5# ReadCalibFromCool.py
6# Andrei Artamonov 2009-11-03
7
8import getopt,sys,os
9os.environ['TERM'] = 'linux'
10
11def usage():
12 print ("Usage: ",sys.argv[0]," [OPTION] ... ")
13 print ("Dumps the TileCal constants from various schemas / folders / tags")
14 print ("")
15 print ("-h, --help shows this help")
16 print ("-f, --folder= specify status folder to use f.i. /TILE/OFL02/CALIB/CIS/LIN ")
17 print ("-t, --tag= specify tag to use, f.i. UPD1 or UPD4 or full suffix like RUN2-HLT-UPD1-00")
18 print ("-r, --run= specify run number, by default uses latest iov")
19 print ("-l, --lumi= specify lumi block number, default is 0")
20 print ("-b, --begin= specify run number of first iov in multi-iov mode, by default uses very first iov")
21 print ("-e, --end= specify run number of last iov in multi-iov mode, by default uses latest iov")
22 print ("-m, --module= specify module to use, default is not set")
23 print ("-N, --chmin= specify minimal channel to use, default is 0")
24 print ("-X, --chmax= specify maximal channel to use, default is 47")
25 print ("-c, --chan= specify channel to use , default is all channels from chmin to chmax")
26 print ("-g, --gain=, -a, --adc= specify adc(gain) to print or number of adcs to print with - sign, default is -2")
27 print ("-n, --nval= specify number of values to output, default is all")
28 print ("-C, --comment print comment for every IOV")
29 print ("-i, --iov print IOVs only for every module")
30 print ("-I, --IOV print IOVs only")
31 print ("-d, --default print also default values stored in AUX01-AUX20")
32 print ("-B, --blob print additional blob info")
33 print ("-H, --hex print frag id instead of module name")
34 print ("-P, --pmt print pmt number in addition to channel number")
35 print ("-p, --prefix= print some prefix on every line ")
36 print ("-k, --keep= field numbers or channel numbers to ignore, e.g. '0,2,3,EBch0,EBch1,EBch12,EBch13,EBspD4ch18,EBspD4ch19,EBspC10ch4,EBspC10ch5' ")
37 print ("-o, --double print values with double precision")
38 print ("-s, --schema= specify schema to use, like 'COOLONL_TILE/CONDBR2' or 'sqlite://;schema=tileSqlite.db;dbname=CONDBR2' or tileSqlite.db")
39 print ("-D, --dbname= specify dbname part of schema if schema only contains file name, default is CONDBR2")
40 print ("-S, --server= specify server - ORACLE or FRONTIER, default is FRONTIER")
41
42letters = "hr:l:s:t:f:D:S:n:b:e:m:N:X:c:a:g:p:dBCiIHPk:o"
43keywords = ["help","run=","lumi=","schema=","tag=","folder=","dbname=","server=","module=","begin=","end=","chmin=","chmax=","gain=","adc=","chan=","nval=","prefix=","default","blob","hex","pmt","keep=","comment","iov","IOV","double"]
44
45try:
46 opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
47except getopt.GetoptError as err:
48 print (str(err))
49 usage()
50 sys.exit(2)
51
52# defaults
53run = 2147483647
54lumi = 0
55schema = 'COOLOFL_TILE/CONDBR2'
56dbname = ''
57server = ''
58folderPath = "/TILE/OFL02/CALIB/CIS/LIN"
59tag = "UPD4"
60nval = 0
61nadc = -1
62rosmin = 1
63rosmax = 5
64blob = False
65hexid = False
66pmt = False
67prefix = None
68modmin = 0
69modmax = 99999
70modulename="AUX-1"
71partname=""
72one_mod = False
73mod = -1
74ros = -1
75chan_n= -1
76chanmin = -1
77chanmax = -1
78gainmin = -1
79gainmax = -1
80begin = -1
81end = 2147483647
82iov = False
83iovonly = False
84IOVONLY = False
85comment = False
86keep=[]
87doubl = False
88
89for o, a in opts:
90 a = a.strip()
91 if o in ("-f","--folder"):
92 folderPath = a
93 elif o in ("-t","--tag"):
94 tag = a
95 elif o in ("-s","--schema"):
96 schema = a
97 elif o in ("-D","--dbname"):
98 dbname = a
99 elif o in ("-S","--server"):
100 server = a
101 elif o in ("-n","--nval"):
102 nval = int(a)
103 elif o in ("-b","--begin"):
104 begin = int(a)
105 iov = True
106 one_mod = True
107 elif o in ("-e","--end"):
108 end = int(a)
109 iov = True
110 one_mod = True
111 elif o in ("-i","--iov"):
112 iov = True
113 iovonly = True
114 if modulename=='AUX-1':
115 modulename='ALL00'
116 elif o in ("-I","--IOV"):
117 iov = True
118 IOVONLY = True
119 if modulename=='AUX-1':
120 modulename='ALL00'
121 elif o in ("-a","--adc","-g","--gain"):
122 nadc = int(a)
123 elif o in ("-m","--module"):
124 modulename = a
125 one_mod = True
126 elif o in ("-c","--chan"):
127 chan_n = int(a)
128 elif o in ("-N","--chmin"):
129 chanmin = int(a)
130 elif o in ("-X","--chmax"):
131 chanmax = int(a)
132 elif o in ("-C","--comment"):
133 comment = True
134 elif o in ("-r","--run"):
135 run = int(a)
136 elif o in ("-l","--lumi"):
137 lumi = int(a)
138 elif o in ("-d","--default"):
139 rosmin = 0
140 elif o in ("-B","--blob"):
141 blob = True
142 elif o in ("-H","--hex"):
143 hexid = True
144 elif o in ("-P","--pmt"):
145 pmt = True
146 elif o in ("-p","--prefix"):
147 prefix = a
148 elif o in ("-k","--keep"):
149 keep = a.split(",")
150 elif o in ("-o","--double"):
151 doubl = True
152 elif o in ("-h","--help"):
153 usage()
154 sys.exit(2)
155 else:
156 raise RuntimeError("unhandled option")
157
158
159from TileCalibBlobPython import TileCalibTools
160from TileCalibBlobPython.TileCalibTools import MAXRUN, MAXLBK
161from TileCalibBlobObjs.Classes import TileCalibUtils, TileCalibType
162
163
164from TileCalibBlobPython.TileCalibLogger import getLogger
165log = getLogger("ReadCalibFrCool")
166import logging
167logLevel=logging.DEBUG
168log.setLevel(logLevel)
169log1 = getLogger("TileCalibTools")
170log1.setLevel(logLevel)
171
172
173#=== check parameters
174if len(dbname)<7 and run!=2147483647:
175 dbname = 'COMP200' if run<232000 else 'CONDBR2'
176
177if 'COOLO' not in schema and ':' not in schema and ';' not in schema:
178 schema='sqlite://;schema='+schema+';dbname='+(dbname if len(dbname) else 'CONDBR2')
179
180if schema=='COOLONL_TILE/COMP200':
181 if not (folderPath.startswith('/TILE/ONL01/') or folderPath.startswith('/TILE/OFL01/')):
182 print ("Folder %s doesn't exist in schema %s " % (folderPath,schema))
183 sys.exit(2)
184
185if schema=='COOLONL_TILE/CONDBR2':
186 if not folderPath.startswith('/TILE/ONL01/'):
187 print ("Folder %s doesn't exist in schema %s, only /TILE/ONL01 " % (folderPath,schema))
188 sys.exit(2)
189
190if schema=='COOLOFL_TILE/COMP200' or schema=='COOLOFL_TILE/CONDBR2':
191 if not folderPath.startswith('/TILE/OFL02/'):
192 print ("Folder %s doesn't exist in schema %s " % (folderPath,schema))
193 sys.exit(2)
194
195if iov:
196 run=end
197 lumi=0
198
199#=== set database
200db = TileCalibTools.openDbConn(schema,server)
201folderTag = TileCalibTools.getFolderTag(schema if 'COMP200' in schema or 'OFLP200' in schema else db, folderPath, tag)
202log.info("Initializing folder %s with tag %s", folderPath, folderTag)
203
204#=== initialize blob reader
205blobReader = TileCalibTools.TileBlobReader(db,folderPath, folderTag)
206#blobReader.log().setLevel(logging.DEBUG)
207
208#=== get drawer with status at given run
209log.info("Initializing for run %d, lumiblock %d", run,lumi)
210flt=None
211r=5
212d=0
215while not flt:
216 d-=1
217 if d<0:
218 r-=1
219 if r<0:
220 break
222 flt = blobReader.getDrawer(r, d, (run,lumi), False, False)
223if flt:
224 blobT=flt.getObjType()
225 blobV=flt.getObjVersion()
226 mchan=flt.getNChans()
227 mgain=flt.getNGains()
228 mval=flt.getObjSizeUint32()
229 log.info( "Blob type: %d Version: %d Nchannels: %d Ngains: %d Nval: %d", blobT,blobV,mchan,mgain,mval)
230 if nadc<-mgain:
231 nadc=-mgain
232 if nchan<mchan:
233 nchan=mchan
234 if ngain<mgain:
235 ngain=mgain
236else:
237 mgain=1
238if nadc==-1:
239 nadc=-ngain
240
241log.info("Comment: %s", blobReader.getComment((run,lumi)))
242
243#=== check ROS and module numbers
244if one_mod:
245 partname = modulename[:3]
246 mod = int(modulename[3:]) -1
247
248part_dict = {'AUX':0,'LBA':1,'LBC':2,'EBA':3,'EBC':4}
249if partname in part_dict:
250 ros = part_dict[partname]
251 rosmin = ros
252 rosmax = ros+1
253else:
254 ros = -1
255
256if mod >= 0:
257 modmin = mod
258 modmax = mod+1
259elif mod < -1:
260 modmax = modmin
261
262if chan_n >= 0 and chan_n < nchan:
263 chanmin = chan_n
264 chanmax = chan_n+1
265else:
266 if chanmin<0:
267 chanmin = 0
268 if chanmax<0:
269 chanmax = nchan
270 else:
271 chanmax += 1
272
273if nadc >= 0 and nadc < ngain:
274 gainmin = nadc
275 gainmax = nadc+1
276else:
277 gainmin = 0
278 if nadc<0:
279 gainmax = -nadc
280 else:
281 gainmax = ngain
282
283
284#=== IOV only option
285if iovonly or IOVONLY:
286 if iovonly:
287 print("")
288 if begin<0:
289 begin = end
290 lastRun = -1
291 allsince = {}
292 roslist = list(range(rosmin,rosmax))
293 if comment:
294 roslist += [-1]
295 for ros in roslist:
296 if ros==-1:
297 modlist = [1000]
298 modName = "Comment"
299 else:
300 modlist=range(modmin, min(modmax,TileCalibUtils.getMaxDrawer(ros)))
301 for mod in modlist:
302 if mod<1000:
303 if hexid:
304 modName = "0x%x" % ((ros<<8)+mod)
305 else:
306 modName = TileCalibUtils.getDrawerString(ros,mod)
307 iovs = ["None"]
308 dbobjs = blobReader.getDBobjsWithinRange(ros,mod)
309 while dbobjs.goToNext():
310 obj = dbobjs.currentRef()
311 objsince = obj.since()
312 sinceRun = objsince >> 32
313 sinceLum = objsince & 0xFFFFFFFF
314 since = "(%d,%d)" % (sinceRun, sinceLum)
315 if sinceRun>lastRun:
316 lastRun = sinceRun
317 if sinceRun>end:
318 break
319 elif sinceRun>=begin:
320 if iovs[0]!="None":
321 iovs += [since]
322 else:
323 iovs[0] = since
324 if since in allsince:
325 allsince[since] += [modName]
326 else:
327 allsince[since] = [modName]
328 else:
329 iovs[0] = since
330 if iovonly:
331 print("%s %s" % (modName, " ".join(iovs)) )
332 if IOVONLY:
333 print("")
334 all=[]
335 for since in allsince:
336 all += ["%s %s" % (since,TileCalibTools.moduleListToString(allsince[since],(rosmin==0),True,comment))]
337 if len(all)>0:
338 all.sort()
339 for s in all:
340 print(s)
341 else:
342 print("No new IOVs after run",lastRun)
343 #=== close DB
344 db.closeDatabase()
345 sys.exit(0)
346
347
348#=== Filling the iovList
349iovList = []
350if iov:
351 if mod>=0:
352 COOL_part = ros
353 COOL_chan = mod
354 else:
355 COOL_part = -1
356 COOL_chan = 1000
357
358 try:
359 dbobjs = blobReader.getDBobjsWithinRange(COOL_part,COOL_chan)
360 if (dbobjs is None):
361 raise Exception("No DB objects retrieved when building IOV list!")
362 while dbobjs.goToNext():
363 obj = dbobjs.currentRef()
364 objsince = obj.since()
365 sinceRun = objsince >> 32
366 sinceLum = objsince & 0xFFFFFFFF
367 since = (sinceRun, sinceLum)
368 objuntil = obj.until()
369 untilRun = objuntil >> 32
370 untilLum = objuntil & 0xFFFFFFFF
371 until = (untilRun, untilLum)
372 iovList.append((since, until))
373 except Exception:
374 log.warning( "Warning: can not read IOVs from input DB file" )
375 sys.exit(2)
376
377 be=iovList[0][0][0]
378 en=iovList[-1][0][0]
379
380 if begin != be or end != en:
381 ib=0
382 ie=len(iovList)
383 for i,iovs in enumerate(iovList):
384 run = iovs[0][0]
385 lumi = iovs[0][1]
386 if (run<begin and run>be) or (run==begin and lumi==0) :
387 be=run
388 ib=i
389 if run>=end and run<en:
390 en=run
391 ie=i+1
392 log.info( "" )
393 if be != begin:
394 log.info( "Changing begin run from %d to %d (start of IOV)", begin,be)
395 begin=be
396 if en != end:
397 if en>end:
398 log.info( "Changing end run from %d to %d (start of next IOV)", end,en)
399 else:
400 log.info( "Changing end run from %d to %d (start of last IOV)", end,en)
401 end=en
402 iovList=iovList[ib:ie]
403 if COOL_chan == 1000:
404 log.info( "%d IOVs in total for comment field", len(iovList) )
405 else:
406 log.info( "%d IOVs in total for %s", len(iovList),modulename)
407else:
408 iovList.append(((run,lumi),(MAXRUN, MAXLBK)))
409
410log.info( "\n" )
411
412
413
416dummy = [0]*48
417barrel = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
418 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
419 27, 26, 25, 30, 29, 28,-33,-32, 31, 36, 35, 34,
420 39, 38, 37, 42, 41, 40, 45,-44, 43, 48, 47, 46]
421extbar = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
422 13, 14, 15, 16, 17, 18,-19,-20, 21, 22, 23, 24,
423 -27,-26,-25,-31,-32,-28, 33, 29, 30,-36,-35, 34,
424 44, 38, 37, 43, 42, 41,-45,-39,-40,-48,-47,-46]
425
426ch2pmt = [ dummy, barrel, barrel, extbar, extbar ]
427gname=[]
428if mgain!=2:
429 for i in range(mgain+1):
430 gname+=[ "g "+str(i) ]
431else:
432 gname = [ "LG", "HG" ]
433
434#=== loop over all partitions,modules,channels
435pref = ""
436for iovs in iovList:
437 if iov:
438 pref = "(%i,%i) " % (iovs[0][0],iovs[0][1])
439 if prefix:
440 pref = prefix + " " + pref
441 if comment:
442 log.info( pref + blobReader.getComment(iovs[0]) )
443 if prefix and prefix.startswith("Write"):
444 comm = blobReader.getComment(iovs[0])
445 if ": " in comm:
446 comm = comm[comm.find(": ")+2:]
447 print ('%s --update --folder=%s --tag=%s --run=%i --lumi=%i --comment="%s"' % (prefix,folderPath,folderTag,iovs[0][0],iovs[0][1],comm))
448 miss=0
449 good=0
450 for ros in range(rosmin,rosmax):
451 for mod in range(modmin, min(modmax,TileCalibUtils.getMaxDrawer(ros))):
452 if hexid:
453 modName = "0x%x" % ((ros<<8)+mod)
454 else:
455 modName = TileCalibUtils.getDrawerString(ros,mod)
456 if modName in ['EBA39','EBA40','EBA41','EBA42','EBA55','EBA56','EBA57','EBA58',
457 'EBC39','EBC40','EBC41','EBC42','EBC55','EBC56','EBC57','EBC58' ]:
458 modSpec = 'EBspC10'
459 elif modName in ['EBA15','EBC18']:
460 modSpec = 'EBspD4'
461 elif modName in ['EBC29','EBC32','EBC34','EBC37']:
462 modSpec = 'EBspE4'
463 elif modName in ['EBA07', 'EBA25', 'EBA44', 'EBA53',
464 'EBC07', 'EBC25', 'EBC44', 'EBC53',
465 'EBC28', 'EBC31', 'EBC35', 'EBC38' ]:
466 modSpec = 'EBspE1'
467 elif modName in ['EBA08', 'EBA24', 'EBA43', 'EBA54',
468 'EBC08', 'EBC24', 'EBC43', 'EBC54' ]:
469 modSpec = 'EBMBTS'
470 else:
471 modSpec = modName
472 try:
473 flt = blobReader.getDrawer(ros, mod,iovs[0], False, False)
474 if flt is None or isinstance(flt, (int)):
475 miss+=1
476 print ("%s is missing in DB" % modName)
477 else:
478 good+=1
479 if blob:
480 print ("%s Blob type: %d Version: %d Nchannels: %d Ngains: %d Nval: %d" % (modName, flt.getObjType(), flt.getObjVersion(), flt.getNChans(), flt.getNGains(), flt.getObjSizeUint32()))
481 typeName = TileCalibType.getClassName(flt.getObjType())[-3:]
482 mval0 = 0
483 mval = flt.getObjSizeUint32()
484 if nval<0 and -nval<=mval:
485 mval=-nval
486 mval0=mval-1
487 elif nval!=0 and nval<mval:
488 mval = nval
489 mchan=flt.getNChans()
490 mgain=flt.getNGains()
491 chmin = chanmin if chanmin<mchan else mchan
492 chmax = chanmax if chanmax<mchan else mchan
493 gnmin = gainmin if gainmin<mgain else mgain
494 gnmax = gainmax if gainmax<mgain else mgain
495 for chn in range(chmin,chmax):
496 for adc in range(gnmin,gnmax):
497 if pmt:
498 msg = "%s pm %02i ch %02i %s " % ( modName, abs(ch2pmt[ros][chn]), chn, gname[adc] )
499 else:
500 msg = "%s %2i %1i " % ( modName, chn, adc )
501 for val in range(mval0,mval):
502 if str(val) in keep or modName in keep or modSpec in keep or modName[:3] in keep or modName[:2] in keep \
503 or ("%sch%i"% (modName,chn)) in keep or ("%sch%i"% (modSpec,chn)) in keep or ("%sch%i"% (modName[:3],chn)) in keep or ("%sch%i"% (modName[:2],chn)) in keep \
504 or ("%sch%ig%i"% (modName,chn,adc)) in keep or ("%sch%ig%i"% (modSpec,chn,adc)) in keep or ("%sch%ig%i"% (modName[:3],chn,adc)) in keep or ("%sch%ig%i"% (modName[:2],chn,adc)) in keep:
505 msg += " keep "
506 else:
507 if typeName=='Int':
508 v = flt.getData(chn, adc, val)
509 if v>0xff:
510 msg += " 0x%x" % v
511 else:
512 msg += " %3d" % v
513 elif typeName=='Bch':
514 msg += " %d" % flt.getData(chn, adc, val)
515 elif doubl:
516 msg += " %s" % flt.getData(chn, adc, val)
517 else:
518 msg += " %f" % flt.getData(chn, adc, val)
519 print (pref+msg)
520 except Exception as e:
521 print (e)
522 if miss:
523 if iovs[0][0]!=2147483647:
524 print ("%3i drawers are present in DB for run %d lb %d" % (good,iovs[0][0],iovs[0][1]))
525 print ("%3i drawers are missing in DB for run %d lb %d" % (miss,iovs[0][0],iovs[0][1]))
526 else:
527 print ("%3i drawers are present in DB" % (good))
528 print ("%3i drawers are missing in DB" % (miss))
529 if good==0:
530 print ("Please, check that you are using correct schema and correct tag")
531
532#=== close DB
533db.closeDatabase()
void print(char *figname, TCanvas *c1)
#define min(a, b)
Definition cfImp.cxx:40
static std::string getClassName(TileCalibType::TYPE type)
Returns the class name.
static unsigned int max_gain()
Python compatibility function.
static std::string getDrawerString(unsigned int ros, unsigned int drawer)
Return the drawer name, e.g.
static unsigned int max_chan()
Python compatibility function.
static unsigned int getMaxDrawer(unsigned int ros)
Returns the maximal channel number for a given drawer.