ATLAS Offline Software
Loading...
Searching...
No Matches
ReadCalibFromCrest.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# File: ReadCalibFromCrest.py
6# Sanya Solodkov <Sanya.Solodkov@cern.ch>, 2025-02-04
7#
8# Purpose: Read calibration constants from CREST DB or from JSON file
9#
10
11import getopt,sys,os
12os.environ['TERM'] = 'linux'
13
14def usage():
15 print ("Usage: ",sys.argv[0]," [OPTION] ... ")
16 print ("Dumps the TileCal constants from various schemas / folders / tags")
17 print ("")
18 print ("-h, --help shows this help")
19 print ("-f, --folder= specify status folder to use f.i. /TILE/OFL02/CALIB/CIS/LIN ")
20 print ("-t, --tag= specify tag to use, f.i. UPD1 or UPD4 or full suffix like RUN2-HLT-UPD1-00")
21 print ("-H, --hash= specify hash to use, run/lumi will be ignored if hash is provided")
22 print ("-r, --run= specify run number, by default uses latest iov")
23 print ("-l, --lumi= specify lumi block number, default is 0")
24 print ("-b, --begin= specify run number of first iov in multi-iov mode, by default uses very first iov")
25 print ("-e, --end= specify run number of last iov in multi-iov mode, by default uses latest iov")
26 print ("-m, --module= specify module to use, default is not set")
27 print ("-N, --chmin= specify minimal channel to use, default is 0")
28 print ("-X, --chmax= specify maximal channel to use, default is 47")
29 print ("-c, --chan= specify channel to use , default is all channels from chmin to chmax")
30 print ("-g, --gain=, -a, --adc= specify adc(gain) to print or number of adcs to print with - sign, default is -2")
31 print ("-n, --nval= specify number of values to output, default is all")
32 print ("-C, --comment print comment for every IOV")
33 print ("-i, --iov print IOVs only for every module")
34 print ("-I, --IOV print IOVs only")
35 print ("-A, --all print all records for every IOV, including those which were overwritten")
36 print ("-d, --default print also default values stored in AUX01-AUX20")
37 print ("-B, --blob print additional blob info")
38 print ("-x, --hex print frag id instead of module name")
39 print ("-P, --pmt print pmt number in addition to channel number")
40 print ("-p, --prefix= print some prefix on every line ")
41 print ("-k, --keep= field numbers or channel numbers to ignore, e.g. '0,2,3,EBch0,EBch1,EBch12,EBch13,EBspD4ch18,EBspD4ch19,EBspC10ch4,EBspC10ch5' ")
42 print ("-o, --double print values with double precision")
43 print ("-s, --schema= specify name of input JSON file or CREST_SERVER_PATH")
44
45letters = "hH:r:l:s:t:f:n:b:e:m:N:X:c:a:g:p:dBCiIAxPk:o:"
46keywords = ["help","hash=","run=","lumi=","schema=","tag=","folder=","module=","begin=","end=","chmin=","chmax=","gain=","adc=","chan=","nval=","prefix=","default","blob","hex","pmt","keep=","comment","iov","IOV","all","double"]
47
48try:
49 opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
50except getopt.GetoptError as err:
51 print (str(err))
52 usage()
53 sys.exit(2)
54
55# defaults
56run = 2147483647
57lumi = 0
58schema = 'CREST'
59folderPath = "/TILE/OFL02/CALIB/CIS/LIN"
60tag = "UPD4"
61hash = None
62nval = 0
63nadc = -1
64rosmin = 1
65rosmax = 5
66blob = False
67hexid = False
68pmt = False
69prefix = None
70modmin = 0
71modmax = 99999
72modulename="AUX-1"
73partname=""
74one_mod = False
75mod = -1
76ros = -1
77chan_n= -1
78chanmin = -1
79chanmax = -1
80gainmin = -1
81gainmax = -1
82begin = -1
83end = 2147483647
84all = False
85iov = False
86iovonly = False
87IOVONLY = False
88comment = False
89keep=[]
90doubl = False
91
92for o, a in opts:
93 a = a.strip()
94 if o in ("-f","--folder"):
95 folderPath = a
96 elif o in ("-t","--tag"):
97 tag = a
98 elif o in ("-s","--schema"):
99 schema = a
100 elif o in ("-H","--hash"):
101 hash = a
102 elif o in ("-n","--nval"):
103 nval = int(a)
104 elif o in ("-b","--begin"):
105 begin = int(a)
106 iov = True
107 one_mod = True
108 elif o in ("-e","--end"):
109 end = int(a)
110 iov = True
111 one_mod = True
112 elif o in ("-A","--all"):
113 all = True
114 elif o in ("-i","--iov"):
115 iov = True
116 iovonly = True
117 if modulename=='AUX-1':
118 modulename='ALL00'
119 elif o in ("-I","--IOV"):
120 iov = True
121 IOVONLY = True
122 if modulename=='AUX-1':
123 modulename='ALL00'
124 elif o in ("-a","--adc","-g","--gain"):
125 nadc = int(a)
126 elif o in ("-m","--module"):
127 modulename = a
128 one_mod = True
129 elif o in ("-c","--chan"):
130 chan_n = int(a)
131 elif o in ("-N","--chmin"):
132 chanmin = int(a)
133 elif o in ("-X","--chmax"):
134 chanmax = int(a)
135 elif o in ("-C","--comment"):
136 comment = True
137 elif o in ("-r","--run"):
138 run = int(a)
139 elif o in ("-l","--lumi"):
140 lumi = int(a)
141 elif o in ("-d","--default"):
142 rosmin = 0
143 elif o in ("-B","--blob"):
144 blob = True
145 elif o in ("-x","--hex"):
146 hexid = True
147 elif o in ("-P","--pmt"):
148 pmt = True
149 elif o in ("-p","--prefix"):
150 prefix = a
151 elif o in ("-k","--keep"):
152 keep = a.split(",")
153 elif o in ("-o","--double"):
154 doubl = True
155 elif o in ("-h","--help"):
156 usage()
157 sys.exit(2)
158 else:
159 raise RuntimeError("unhandled option")
160
161if hash is not None:
162 iov = False
163 run = None
164 lumi = hash
165
166
167from TileCalibBlobPython import TileCalibCrest
168from TileCalibBlobObjs.Classes import TileCalibUtils, TileCalibType
169
170from TileCalibBlobPython.TileCalibLogger import getLogger
171log = getLogger("ReadCalibFrCrest")
172import logging
173logLevel=logging.DEBUG
174log.setLevel(logLevel)
175log1 = getLogger("TileCalibCrest")
176log1.setLevel(logLevel)
177
178#=== check ROS and module numbers
179if one_mod:
180 partname = modulename[:3]
181 mod = int(modulename[3:]) -1
182
183part_dict = {'AUX':0,'LBA':1,'LBC':2,'EBA':3,'EBC':4}
184if partname in part_dict:
185 ros = part_dict[partname]
186 rosmin = ros
187 rosmax = ros+1
188else:
189 ros = -1
190
191if mod >= 0:
192 modmin = mod
193 modmax = mod+1
194elif mod < -1:
195 modmax = modmin
196
197if iov:
198 run=end
199 lumi=0
200
201if tag.upper().endswith('HEAD'):
202 tag=tag.upper()
203if len(tag)==0 or tag.endswith('HEAD'):
204 folderPath=folderPath.replace('OFL02','ONL01')
205 log.info("tag is %s, using %s folder", tag if tag else 'empty', folderPath)
206 if tag=='HEAD':
207 tag=''
208
209folderTag = tag
210tag = tag.upper()
211tag1 = tag.split('_')[1][:4] if '_' in tag else tag[:4]
212if tag1 == "TILE" or tag1 == "CALO" or tag.startswith("TILE") or tag.startswith("CALO"):
213 folderPath=""
214if not os.path.isfile(schema):
215 log.info("Initializing folder %s with tag %s", folderPath, folderTag)
216
217blobReader = TileCalibCrest.TileBlobReaderCrest(schema,folderPath, folderTag, run, lumi,
218 TileCalibUtils.getDrawerIdx(max(rosmin,0),max(modmin,0)),
219 TileCalibUtils.getDrawerIdx(min(rosmax-1,4),max(0,min(modmax-1,TileCalibUtils.getMaxDrawer(min(rosmax-1,4))-1))))
220
221#=== get drawer with status at given run
222flt=None
223r=5
224d=0
227while not flt:
228 d-=1
229 if d<0:
230 r-=1
231 if r<0:
232 break
234 flt = blobReader.getDrawer(r, d, (run,lumi), True, False)
235if flt:
236 blobT=flt.getObjType()
237 blobV=flt.getObjVersion()
238 mchan=flt.getNChans()
239 mgain=flt.getNGains()
240 mval=flt.getObjSizeUint32()
241 log.info( "Blob type: %d Version: %d Nchannels: %d Ngains: %d Nval: %d", blobT,blobV,mchan,mgain,mval)
242 if nadc<-mgain:
243 nadc=-mgain
244 if nchan<mchan:
245 nchan=mchan
246 if ngain<mgain:
247 ngain=mgain
248else:
249 mgain=1
250if nadc==-1:
251 nadc=-ngain
252
253log.info("Comment: %s", blobReader.getComment((run,lumi)))
254
255if chan_n >= 0 and chan_n < nchan:
256 chanmin = chan_n
257 chanmax = chan_n+1
258else:
259 if chanmin<0:
260 chanmin = 0
261 if chanmax<0:
262 chanmax = nchan
263 else:
264 chanmax += 1
265
266if nadc >= 0 and nadc < ngain:
267 gainmin = nadc
268 gainmax = nadc+1
269else:
270 gainmin = 0
271 if nadc<0:
272 gainmax = -nadc
273 else:
274 gainmax = ngain
275
276
277#=== Filling the iovList
278iovList = []
279if iov:
280 if begin<0:
281 if iovonly or IOVONLY:
282 begin = end
283 else:
284 begin=0
285 iovList = blobReader.getIovs((begin,0),(end,0))
286
287 be=iovList[0][0]
288 en=iovList[-1][0]
289
290 if begin != be or end != en:
291 log.info( "" )
292 if be != begin:
293 log.info( "Changing begin run from %d to %d (start of IOV)", begin,be)
294 begin=be
295 if en != end:
296 if en>end:
297 log.info( "Changing end run from %d to %d (start of next IOV)", end,en)
298 else:
299 log.info( "Changing end run from %d to %d (start of last IOV)", end,en)
300 end=en
301 log.info( "%d IOVs in total", len(iovList))
302
303 #=== IOV only option
304 if iovonly or IOVONLY:
305 if comment:
306 for iovs in iovList:
307 blobReader.readIov(iovs)
308 if all:
309 phash = blobReader.getPayloadHash()
310 itime = blobReader.getInsertionTime()
311 for i in range(len(phash)):
312 blobReader.getPayloadByHash(i)
313 log.info("(%i,%i) Insertion time %s Hash %s", iovs[0], iovs[1], itime[i], phash[i] )
314 log.info("(%i,%i) %s", iovs[0], iovs[1], blobReader.getComment(iovs) )
315 else:
316 log.info("(%i,%i) %s", iovs[0], iovs[1], blobReader.getComment(iovs) )
317 option = 1 if iovonly else 0
318 option += (2 if IOVONLY else 0)
319 blobReader.dumpIovs(iovList,rosmin,rosmax,modmin,modmax,option,(rosmin<=0),True)
320 sys.exit(0)
321else:
322 iovList.append((run,lumi))
323
324log.info( "\n" )
325
326
327
330dummy = [0]*48
331barrel = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
332 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
333 27, 26, 25, 30, 29, 28,-33,-32, 31, 36, 35, 34,
334 39, 38, 37, 42, 41, 40, 45,-44, 43, 48, 47, 46]
335extbar = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
336 13, 14, 15, 16, 17, 18,-19,-20, 21, 22, 23, 24,
337 -27,-26,-25,-31,-32,-28, 33, 29, 30,-36,-35, 34,
338 44, 38, 37, 43, 42, 41,-45,-39,-40,-48,-47,-46]
339
340ch2pmt = [ dummy, barrel, barrel, extbar, extbar ]
341gname=[]
342if mgain!=2:
343 for i in range(mgain+1):
344 gname+=[ "g "+str(i) ]
345else:
346 gname = [ "LG", "HG" ]
347
348#=== loop over all partitions,modules,channels
349oneModule = ((rosmax-rosmin==1) and (modmax-modmin==1) and iov)
350oldBlob = None
351pref = ""
352for iovs in iovList:
353 blobReader.readIov(iovs)
354 phash = blobReader.getPayloadHash()
355 itime = blobReader.getInsertionTime()
356 hashmax = len(phash) if all else 1
357 for hashind in range(hashmax):
358 if iov:
359 pref = "(%i,%i) " % (iovs[0],iovs[1])
360 if prefix:
361 pref = prefix + " " + pref
362 if all:
363 blobReader.getPayloadByHash(hashind)
364 log.info("(%s,%s) Insertion time %s Hash %s", 0 if iovs[0] is None else iovs[0], iovs[1], itime[hashind], phash[hashind] )
365 if comment:
366 log.info( pref + str(blobReader.getComment(iovs)) )
367 if prefix and prefix.startswith("Write"):
368 comm = blobReader.getComment(iovs)
369 if ": " in comm:
370 comm = comm[comm.find(": ")+2:]
371 print ('%s --update --folder=%s --tag=%s --run=%i --lumi=%i --comment="%s"' % (prefix,folderPath,folderTag,iovs[0],iovs[1],comm))
372 miss=0
373 good=0
374 for ros in range(rosmin,rosmax):
375 for mod in range(modmin, min(modmax,TileCalibUtils.getMaxDrawer(ros))):
376 if hexid:
377 modName = "0x%x" % ((ros<<8)+mod)
378 else:
379 modName = TileCalibUtils.getDrawerString(ros,mod)
380 if modName in ['EBA39','EBA40','EBA41','EBA42','EBA55','EBA56','EBA57','EBA58',
381 'EBC39','EBC40','EBC41','EBC42','EBC55','EBC56','EBC57','EBC58' ]:
382 modSpec = 'EBspC10'
383 elif modName in ['EBA15','EBC18']:
384 modSpec = 'EBspD4'
385 else:
386 modSpec = modName
387 try:
388 if oneModule:
389 newBlob = blobReader.getBlob(ros, mod, iovs, False)
390 if oldBlob == newBlob:
391 if hashind==0:
392 log.info( f'{modName} in IOV {iovs} is identical to previous IOV')
393 else:
394 log.info( f'{modName} in payload {hashind+1} of IOV {iovs} is identical to previous payload')
395 continue
396 else:
397 if oldBlob is None:
398 log.info( f'Reading IOV {iovs} for module {modName}')
399 else:
400 if hashind==0:
401 log.info( f'{modName} was updated in IOV {iovs}')
402 else:
403 log.info( f'{modName} was updated in payload {hashind+1} of IOV {iovs}')
404 oldBlob = newBlob
405 flt = blobReader.getDrawer(ros, mod,iovs, False, False)
406 if flt is None or isinstance(flt, (int)):
407 miss+=1
408 print ("%s is missing in DB" % modName)
409 else:
410 good+=1
411 if blob:
412 print ("%s Blob type: %d Version: %d Nchannels: %d Ngains: %d Nval: %d" % (modName, flt.getObjType(), flt.getObjVersion(), flt.getNChans(), flt.getNGains(), flt.getObjSizeUint32()))
413 typeName = TileCalibType.getClassName(flt.getObjType())[-3:]
414 mval0 = 0
415 mval = flt.getObjSizeUint32()
416 if nval<0 and -nval<=mval:
417 mval=-nval
418 mval0=mval-1
419 elif nval!=0 and nval<mval:
420 mval = nval
421 mchan=flt.getNChans()
422 mgain=flt.getNGains()
423 chmin = chanmin if chanmin<mchan else mchan
424 chmax = chanmax if chanmax<mchan else mchan
425 gnmin = gainmin if gainmin<mgain else mgain
426 gnmax = gainmax if gainmax<mgain else mgain
427 for chn in range(chmin,chmax):
428 for adc in range(gnmin,gnmax):
429 if pmt:
430 msg = "%s pm %02i ch %02i %s " % ( modName, abs(ch2pmt[ros][chn]), chn, gname[adc] )
431 else:
432 msg = "%s %2i %1i " % ( modName, chn, adc )
433 for val in range(mval0,mval):
434 if str(val) in keep or modName in keep or modSpec in keep or modName[:3] in keep or modName[:2] in keep \
435 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 \
436 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:
437 msg += " keep "
438 else:
439 if typeName=='Int':
440 v = flt.getData(chn, adc, val)
441 if v>0xff:
442 msg += " 0x%x" % v
443 else:
444 msg += " %3d" % v
445 elif typeName=='Bch':
446 msg += " %d" % flt.getData(chn, adc, val)
447 elif doubl:
448 msg += " %s" % flt.getData(chn, adc, val)
449 else:
450 msg += " %f" % flt.getData(chn, adc, val)
451 print (pref+msg)
452 except Exception as e:
453 print (e)
454 if miss:
455 if hash is not None:
456 print ("%3i drawers are present in DB for hash %s" % (good,hash))
457 print ("%3i drawers are missing in DB for hash %s" % (miss,hash))
458 elif iovs[0]!=2147483647:
459 print ("%3i drawers are present in DB for run %d lb %d" % (good,iovs[0],iovs[1]))
460 print ("%3i drawers are missing in DB for run %d lb %d" % (miss,iovs[0],iovs[1]))
461 else:
462 print ("%3i drawers are present in DB" % (good))
463 print ("%3i drawers are missing in DB" % (miss))
464 if good==0:
465 print ("Please, check that you are using correct schema and correct tag")
#define min(a, b)
Definition cfImp.cxx:40
#define max(a, b)
Definition cfImp.cxx:41
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 getDrawerIdx(unsigned int ros, unsigned int drawer)
Returns a drawer hash.
static unsigned int getMaxDrawer(unsigned int ros)
Returns the maximal channel number for a given drawer.