ATLAS Offline Software
Loading...
Searching...
No Matches
TileCalibCrest.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# TileCalibCrest.py
5# Sanya Solodkov <Sanya.Solodkov@cern.ch>, 2025-02-04
6# Siarhei Harkusha <Siarhei.Harkusha@cern.ch>, 2025-05-19
7# Laura Sargsyan <Laura.Sargsyan@cern.ch>, 2025-09-16
8# Sanya Solodkov <Sanya.Solodkov@cern.ch>, 2026-07-01 - migration to CHAI
9
10"""
11Python helper module for managing CREST DB connections and TileCalibBlobs.
12"""
13
14import os, re, cppyy, base64, json, time, datetime
15
16from PyCool import cool # noqa: F401
17Blob = cppyy.gbl.coral.Blob
18
19import chai
20chai.set_log_level(chai.LogLevel.WARNING)
21
22from TileCalibBlobObjs.Classes import TileCalibUtils, TileCalibDrawerCmt, \
23 TileCalibDrawerInt, TileCalibDrawerOfc, TileCalibDrawerBch, \
24 TileCalibDrawerFlt, TileCalibType
25from TileCalibBlobPython import TileCalibTools
26
27#=== get a logger
28from TileCalibBlobPython.TileCalibLogger import TileCalibLogger, getLogger
29log = getLogger("TileCalibCrest")
30
31CREST_HOST = os.getenv('CREST_HOST',os.getenv('CREST_SERVER',os.getenv('CHAI_CREST_SERVER',os.getenv('CREST_SERVER_PATH',os.getenv('CREST_API_URL','https://atlas-crest-dev.cern.ch:443/api-v6.4')))))
32
33#======================================================================
34#===
35#=== Global helper functions
36#===
37#======================================================================
38
39#
40#______________________________________________________________________
41#=== useful constants
42MINRUN = 0
43MINLBK = 0
44MAXRUN = (1<<31)-1
45MAXLBK = (1<<32)-1
46# empty Tile channel for storing laser partition variation. DO NOT CHANGE.
47LASPARTCHAN = 43
48
49
50class TileBlobReaderCrest(TileCalibLogger):
51 """
52 TileCalibBlobReader is a helper class, managing the details of CREST interactions for
53 the user of TileCalibBlobs.
54 """
55
56 #____________________________________________________________________
57 def __init__(self, db, folder='', tag='', run=None, lumi=0, modmin=0, modmax=275, copyBlob=False):
58 """
59 Input:
60 - db : server connection string or file name
61 - folder: full folder path
62 - tag : The folder tag, e.g. \"UPD4-24\" or full tag
63 - run : Run number (if known)
64 - lumi : Lumi block number
65 - modmin: Minimal module (COOL channel number)
66 - modmax: Maximal module (COOL channel number)
67 - copyBlob: save payload from CREST (Default:False, True to copy payload to json file)
68 """
69 #=== initialize base class
70 TileCalibLogger.__init__(self,"TileBlobReader")
71 self.payload = {}
72 if db is None or ('CREST' in db and 'http' not in db):
73 self.__db = CREST_HOST
74 else:
75 self.__db = db
76 self.__folder = folder
77 self.__tag = tag
78 self.__copyBlob = copyBlob
79
80 self.__iovList = []
81 self.__iov = (-1,0)
84 self.__commentBlob = None
85 self.__drawerBlob = [None]*276
86 self.__comment = None
87 self.__drawer = [None]*276
88 self.__modmin = modmin
89 self.__modmax = modmax+1
90
91 #=== try to open db
92 self.__remote = not self.__db.endswith(".json")
93 if self.__remote:
94 if 'http' in self.__db:
95 self.log().info('Host %s' , (self.__db))
96 self.__api_instance = chai.Database(f"crest:{self.__db}")
97 else:
98 self.log().info('Local db %s' , (self.__db))
99 self.__api_instance = chai.Database(f"crest_fs:{self.__db}")
100 self.__tag = self.getFolderTag(folder,None,tag)
101 try:
102 self.__tag_instance = self.__api_instance.get_tag(self.__tag)
103 except Exception:
104 self.__tag_instance = None
105 if run is not None and lumi is not None:
106 log.info("Initializing for run %d, lumiblock %d", run,lumi)
107 self.__getIov((run,lumi),True)
108 elif run is None and lumi is not None:
109 log.info("Initializing for hash %s", lumi)
110 self.__getIov(lumi,True)
111 else:
112 self.log().info('File %s' , (self.__db))
113 self.__api_instance = None
114 self.__tag_instance = None
115 self.__tag = 'unknown'
116 self.__iovList.append(((MINRUN,MINLBK),(MAXRUN, MAXLBK)))
117 self.__iov = self.__runlumi2iov(self.__iovList[-1])
118 with open(self.__db, 'r') as the_file:
119 jdata = json.load(the_file)
120 if self.__copyBlob:
121 self.payload = jdata
122 return
123 for chan in range(self.__modmin,self.__modmax):
124 try:
125 blob=jdata[str(chan)][0]
126 except Exception:
127 blob=None
128 self.__create_drawer(blob,chan)
129 try:
130 blob=jdata['1000'][0]
131 except Exception:
132 blob=None
133 self.__create_comment(blob)
134
135 #____________________________________________________________________
136 def getTag(self):
137 return self.__tag
138
139 #____________________________________________________________________
140 def getFolderTag(self, folder, prefix, globalTag, api=None):
141 if globalTag=='CURRENT' or globalTag=='UPD4' or globalTag=='' or globalTag=='HEAD':
142 globalTag=TileCalibTools.getAliasFromFile('Current')
143 log.info("Resolved CURRENT globalTag to \'%s\'", globalTag)
144 elif globalTag=='CURRENTES' or globalTag=='UPD1':
145 globalTag=TileCalibTools.getAliasFromFile('CurrentES')
146 log.info("Resolved CURRENT ES globalTag to \'%s\'", globalTag)
147 elif globalTag=='NEXT':
148 globalTag=TileCalibTools.getAliasFromFile('Next')
149 log.info("Resolved NEXT globalTag to \'%s\'", globalTag)
150 elif globalTag=='NEXTES':
151 globalTag=TileCalibTools.getAliasFromFile('NextES')
152 log.info("Resolved NEXT ES globalTag to \'%s\'", globalTag)
153 globalTag=globalTag.replace('*','')
154 if prefix is None:
155 prefix = ''
156 for f in folder.split('/'):
157 if re.findall('[a-z]+',f) != [] and f!='CellNoise':
158 prefix+=f
159 else:
160 prefix+=f.capitalize()
161 else:
162 prefix=prefix.strip('-').split('-')[0]
163 if prefix.startswith('Calo') and 'NoiseCell' not in prefix:
164 prefix='CALO'+prefix[4:]
165 prefix=prefix.replace('Pileupnoiselumi','PileUpNoiseLumi')
166 if 'UPD1' in globalTag or 'UPD4' in globalTag or ('COND' not in globalTag and 'CREST' not in globalTag):
167 if prefix != '':
168 if prefix in globalTag or prefix.upper() in globalTag:
169 tag=globalTag
170 else:
171 tag=prefix+'-'+globalTag
172 self.log().info("Resolved localTag \'%s\' to folderTag \'%s\'", globalTag,tag)
173 elif folder!='' and not (globalTag.upper().startswith('TILE') or globalTag.upper().startswith('CALO')):
174 tag = TileCalibUtils.getFullTag(folder, globalTag)
175 if tag.startswith('Calo') and 'NoiseCell' not in tag:
176 tag='CALO'+tag[4:]
177 tag=tag.replace('Pileupnoiselumi','PileUpNoiseLumi')
178 self.log().info("Resolved localTag \'%s\' to folderTag \'%s\'", globalTag,tag)
179 else:
180 tag=globalTag
181 self.log().info("Use localTag \'%s\' as is", tag)
182 else:
183 tag=None
184 try:
185 if api:
186 gtag = api.get_global_tag(globalTag)
187 elif self.__api_instance and 'http' in self.__db:
188 gtag = self.__api_instance.get_global_tag(globalTag)
189 else:
190 api = chai.Database(f"crest:{CREST_HOST}")
191 gtag = api.get_global_tag(globalTag)
192 tags = gtag.mapping
193 except Exception:
194 tags = {}
195 if len(tags)==0:
196 raise Exception( "globalTag %s not found" % (globalTag) )
197 else:
198 for l in tags:
199 t=tags[l][1]
200 if (prefix!='' and t.startswith(prefix)) or l==folder:
201 tag=t
202 self.log().info("Resolved globalTag \'%s\' to folderTag \'%s\'", globalTag,tag)
203 #taginfo = self.__api_instance.get_tag(tag)
204 #print(taginfo.to_json())
205 return tag
206
207 #____________________________________________________________________
208 def getIovs(self,since=(MINRUN,MINLBK),until=(MAXRUN,MAXLBK)):
209 if self.__tag_instance is None:
210 return [self.__iovList[0][0]]
211 else:
212 run_lumi1=(since[0]<<32)+since[1]
213 run_lumi2=(until[0]<<32)+until[1]
214 run_lumi0=self.__tag_instance.get_payload_at(run_lumi1).since
215 iovs=self.__tag_instance.get_iovs(run_lumi0,run_lumi2)
216 iovList=[]
217 for iov in iovs:
218 runS=iov.since>>32
219 lumiS=iov.since&0xFFFFFFFF
220 if (runS,lumiS) not in iovList:
221 iovList.append((runS,lumiS))
222 if len(iovList)==0:
223 raise Exception( "IOV for tag %s IOV [%s,%s] - (%s,%s) not found" % (self.__tag,since[0],since[1],until[0],until[1]) )
224 return iovList
225
226 #____________________________________________________________________
227 def getIov(self, option=0):
228 if option!=0:
229 return self.__iov
230 else:
231 return self.__iovList[-1]
232
233 #____________________________________________________________________
235 return len(self.__payload_hash)
236
237 #____________________________________________________________________
238 def getPayloadHash(self):
239 return self.__payload_hash
240
241 #____________________________________________________________________
243 return self.__insertion_time
244
245 #____________________________________________________________________
246 def getPayloadByHash(self,hash):
247 if self.__tag_instance is None:
248 pass
249 else:
250 if isinstance(hash,int) and hash>=0 and hash<len(self.__payload_hash):
251 hash = self.__payload_hash[hash]
252 try:
253 payload = self.__tag_instance.get_payload(hash)
254 except Exception:
255 raise Exception( "Payload for hash %s not found" % hash )
256 jdata = json.loads(payload.to_json())
257 if self.__copyBlob:
258 self.payload = jdata
259 return
260 for chan in range(self.__modmin,self.__modmax):
261 try:
262 blob=jdata[str(chan)][0]
263 except Exception:
264 blob=None
265 self.__create_drawer(blob,chan)
266 try:
267 blob=jdata['1000'][0]
268 except Exception:
269 blob=None
270 self.__create_comment(blob)
271 return
272
273 #____________________________________________________________________
274 def readIov(self,runlumi=None,dbg=False):
275 if self.__remote and runlumi is not None and not self.__checkIov(runlumi):
276 self.__getIov(runlumi,dbg)
277
278 #____________________________________________________________________
279 def __getIov(self,runlumi,dbg=False):
280 if self.__tag_instance is None:
281 pass
282 else:
283 usehash = not (isinstance(runlumi,list) or isinstance(runlumi,tuple))
284 if usehash:
285 res=self.__tag_instance.get_payload(runlumi)
286 else:
287 res=self.__tag_instance.get_payload_at((runlumi[0]<<32)+runlumi[1])
288 if not res:
289 if usehash:
290 raise Exception( "IOV for tag %s run,lumi (%s,%s) not found" % (self.__tag,runlumi[0],runlumi[1]) )
291 else:
292 raise Exception( "IOV for tag %s hash %s not found" % (self.__tag,runlumi) )
293 else:
294 if usehash:
295 (runS,lumiS,runU,lumiU) = (0,0,MAXRUN,MAXLBK)
296 self.__insertion_time.append(0)
297 self.__payload_hash.append(runlumi)
298 jdata = json.loads(res.to_json())
299 if dbg:
300 self.log().info('hash %s' , runlumi)
301 else:
302 runS=res.since>>32
303 lumiS=res.since&0xFFFFFFFF
304 if res.until is None:
305 runU=MAXRUN
306 lumiU=MAXLBK
307 else:
308 runU=res.until>>32
309 lumiU=res.until&0xFFFFFFFF
310 if dbg:
311 #self.log().info('Run,Lumi (%d,%d)' , runlumi)
312 self.log().info('IOV [%d,%d] - (%d,%d)' , runS,lumiS,runU,lumiU)
313 iovs=self.__tag_instance.get_iovs(res.since,res.since+1)
314 self.__payload_hash = []
315 self.__insertion_time = []
316 for iov in iovs:
317 if iov.since == res.since:
318 self.__insertion_time.append(iov.insertion_time)
319 self.__payload_hash.append(iov.payload_hash)
320 if dbg:
321 self.log().info('Insertion time %s Hash %s' , iov.insertion_time, iov.payload_hash)
322 self.__insertion_time.reverse()
323 self.__payload_hash.reverse()
324 jdata = json.loads(res.payload.to_json())
325 #with open("payload.json", 'w') as the_file:
326 # the_file.write(payload)
327 # the_file.write('\n')
328 self.__iovList.append(((runS,lumiS),(runU, lumiU)))
329 self.__iov = self.__runlumi2iov(self.__iovList[-1])
330 if self.__copyBlob:
331 self.payload = jdata
332 return
333 for chan in range(self.__modmin,self.__modmax):
334 try:
335 blob=jdata[str(chan)][0]
336 except Exception:
337 blob=None
338 self.__create_drawer(blob,chan)
339 try:
340 blob=jdata['1000'][0]
341 except Exception:
342 blob=None
343 self.__create_comment(blob)
344 return
345
346 #____________________________________________________________________
347 def __runlumi2iov(self,runlumi):
348 since = (runlumi[0][0]<<32) + runlumi[0][1]
349 until = (runlumi[1][0]<<32) + runlumi[1][1]
350 return (since,until)
351
352 #____________________________________________________________________
353 def __checkIov(self,runlumi):
354 if isinstance(runlumi,list) or isinstance(runlumi,tuple):
355 run = runlumi[0] if isinstance(runlumi[0],int) else 0
356 lumi = runlumi[1] if isinstance(runlumi[1],int) else 0
357 point = (run<<32) + lumi
358 else:
359 point = runlumi if isinstance(runlumi,int) else 0
360 inrange = point>=self.__iov[0] and point<self.__iov[1]
361 return inrange
362
363 #____________________________________________________________________
364 def __make_blob(self,string):
365 b = Blob()
366 b.write(string)
367 b.seek(0)
368 return b
369
370 #____________________________________________________________________
371 def __create_comment(self,b64string):
372 if b64string is None or len(b64string)==0:
373 if b64string is None:
374 self.__commentBlob = None
375 else:
376 self.__commentBlob = 0
377 self.__comment = None
378 else:
379 blob1 = base64.decodebytes(bytes(b64string,'ascii'))
380 self.__commentBlob = self.__make_blob(blob1)
382 return
383
384 #____________________________________________________________________
385 def __create_drawer(self,b64string,chan):
386 if b64string is None or isinstance(b64string, (int, float)) or len(b64string)==0:
387 if b64string is None:
388 self.__drawerBlob[chan] = None
389 else:
390 self.__drawerBlob[chan] = 0
391 self.__drawer[chan] = None
392 return
393 blob1 = base64.decodebytes(bytes(b64string,'ascii'))
394 self.__drawerBlob[chan] = self.__make_blob(blob1)
396 typeName = TileCalibType.getClassName(cmt.getObjType())
397 del cmt
398 #=== create calibDrawer depending on type
399 if typeName=='TileCalibDrawerFlt':
401 self.log().debug( "typeName = Flt " )
402 elif typeName=='TileCalibDrawerInt':
404 self.log().debug( "typeName = Int " )
405 elif typeName=='TileCalibDrawerBch':
407 self.log().debug( "typeName = Bch " )
408 elif typeName=='TileCalibDrawerOfc':
410 self.log().debug( "typeName = Ofc " )
411 elif typeName=='TileCalibDrawerCmt':
412 self.__drawer[chan] = cppyy.gbl.CaloCondBlobFlt.getInstance(self.__drawerBlob[chan])
413 self.log().debug( "typeName = CaloFlt " )
414 else:
415 self.__drawer[chan] = None
416 self.log().warn("Unknown blob type for chan %d - ignoring", chan)
417 return
418
419 #____________________________________________________________________
420 def getPayload(self, runlumi=None, dbg=False):
421
422 if self.__remote and runlumi is not None and not self.__checkIov(runlumi):
423 self.__getIov(runlumi,dbg)
424
425 return self.payload
426
427 #____________________________________________________________________
428 def getBlob(self,ros, mod, runlumi=None, dbg=False):
429
430 if self.__remote and runlumi is not None and not self.__checkIov(runlumi):
431 self.__getIov(runlumi,dbg)
432
433 chanNum = getDrawerIdx(ros,mod)
434
435 if (chanNum>=0 and chanNum<len(self.__drawer)):
436 return self.__drawerBlob[chanNum]
437 else:
438 raise Exception( "Invalid drawer requested: %s %s" % (ros,mod) )
439
440 #____________________________________________________________________
441 def getDrawer(self,ros, mod, runlumi=None, dbg=False, useDefault=True):
442
443 if self.__remote and runlumi is not None and not self.__checkIov(runlumi):
444 self.__getIov(runlumi,dbg)
445
446 chanNum = getDrawerIdx(ros,mod)
447
448 if (chanNum>=0 and chanNum<len(self.__drawer)):
449 drawer=self.__drawer[chanNum]
450 if not useDefault and drawer is None:
451 if self.__drawerBlob[chanNum] is None:
452 return None
453 else:
454 return 0
455 while drawer is None:
456 #=== no default at all?
457 if ros==0 and drawer==0:
458 raise Exception('No default available')
459 #=== follow default policy
460 ros,mod = self.getDefault(ros,mod)
461 chanNum = getDrawerIdx(ros,mod)
462 drawer=self.__drawer[chanNum]
463 return drawer
464 elif (chanNum == 1000):
465 return self.__comment
466 else:
467 raise Exception( "Invalid drawer requested: %s %s" % (ros,mod) )
468
469 #____________________________________________________________________
470 def getComment(self,runlumi=None,split=False):
471
472 if self.__remote and runlumi is not None and not self.__checkIov(runlumi):
473 self.__getIov(runlumi)
474 if self.__comment is not None:
475 if split:
476 return (self.__comment.getAuthor(),self.__comment.getComment(),self.__comment.getDate())
477 else:
478 return self.__comment.getFullComment()
479 else:
480 return "<no comment found>"
481
482 #____________________________________________________________________
483 def getDefault(self, ros, drawer):
484 """
485 Returns a default drawer number (among first 20 COOL channels) for any drawer in any partition
486 """
487 if ros==0:
488 if drawer<=4 or drawer==12 or drawer>=20:
489 drawer1=0
490 elif drawer<12:
491 drawer1=4
492 else:
493 drawer1=12
494 elif ros==1 or ros==2:
495 drawer1=4
496 elif ros==3:
497 OffsetEBA = [ 0, 0, 0, 0, 0, 0, 3, 2, #// Merged E+1: EBA07; Outer MBTS: EBA08
498 0, 0, 0, 0, 7, 6, 5, 7, #// D+4: EBA13, EBA16; Special D+4: EBA14; Special D+40: EBA15
499 7, 6, 6, 7, 0, 0, 0, 2, #// D+4: EBA17, EBA20; Special D+4: EBA18, EBA19; Outer MBTS: EBA24
500 3, 0, 0, 0, 0, 0, 0, 0, #// Merged E+1: EBA25
501 0, 0, 0, 0, 0, 0, 1, 1, #// Inner MBTS + special C+10: EBA39, EBA40
502 1, 1, 2, 3, 0, 0, 0, 0, #// Inner MBTS + special C+10: EBA41, EBA42; Outer MBTS: EBA43; Merged E+1: EBA44
503 0, 0, 0, 0, 3, 2, 1, 1, #// Merged E+1: EBA53; Outer MBTS: EBA54; Inner MBTS + special C+10: EBA55, EBA56
504 1, 1, 0, 0, 0, 0, 0, 0] #// Inner MBTS + special C+10: EBA57, EBA58
505 drawer1 = 12 + OffsetEBA[drawer]
506 elif ros==4:
507 OffsetEBC = [ 0, 0, 0, 0, 0, 0, 3, 2, #// Merged E-1: EBC07; Outer MBTS: EBC08
508 0, 0, 0, 0, 7, 6, 6, 7, # // D-4: EBC13, EBC16; Special D-4: EBC14, EBC15;
509 7, 5, 6, 7, 0, 0, 0, 2, #// D-4: EBC17, EBC20; Special D-40 EBC18; Special D-4: EBC19; Outer MBTS: EBC24
510 3, 0, 0, 3, 4, 0, 3, 4, #// Merged E-1: EBC25, EBC28, EBC31; E-4': EBC29, EBC32
511 0, 4, 3, 0, 4, 3, 1, 1, #// E-4': EBC34, EBC37; Merged E-1: EBC35, EBC38; Inner MBTS + special C-10: EBC39, EBC40
512 1, 1, 2, 3, 0, 0, 0, 0, #// Inner MBTS + special C-10: EBC41, EBC42; Outer MBTS: EBC43; Merged E-1: EBC44
513 0, 0, 0, 0, 3, 2, 1, 1, #// Merged E-1: EBC53; Outer MBTS: EBC54; Inner MBTS + special C-10: EBC55, EBC56
514 1, 1, 0, 0, 0, 0, 0, 0] #// Inner MBTS + special C-10: EBC57, EBC58
515 drawer1 = 12 + OffsetEBC[drawer]
516 else:
517 drawer1=0
518
519 return (0,drawer1)
520
521 #____________________________________________________________________
522 def dumpIovs(self, iovList, rosmin, rosmax, drawermin, drawermax, option=1, comment=False, usenames=True):
523 """
524 Dumps statistics - how many non-empty modules exists in different IOVs
525 """
526
527 if len(iovList)>0:
528 alliovs={}
529 allmods={}
530 zeroiovs={}
531 nmod=0
532 rosrange=list(range(rosmin,rosmax))
533 if comment:
534 rosrange+=[9999]
535 for since in iovList:
536 iov="(%s,%s)" % since
537 allmod=""
538 missmod=""
539 zeromod=""
540 zero=0
541 miss=0
542 for ros in rosrange:
543 if ros<0:
544 (dmin,dmax) = (drawermin,drawermax)
545 elif ros>4:
547 else:
548 (dmin,dmax) = (drawermin,min(drawermax,TileCalibUtils.getMaxDrawer(ros)))
549 for drawer in range(dmin,dmax):
550 flt = self.getDrawer(ros, drawer, since, False, False)
551 if ros<0 or ros>4:
553 mod = "Comment"
554 else:
555 mod = "CH_"+str(getDrawerIdx(ros,drawer))
556 elif usenames:
557 mod = TileCalibUtils.getDrawerString(ros,drawer)
558 else:
559 mod = str(getDrawerIdx(ros,drawer))
560 if mod not in allmods:
561 allmods[mod] = ""
562 nmod += 1
563 if flt is not None:
564 if flt==0:
565 zero += 1
566 zeromod += " " + mod
567 allmod += " " + mod + "_zero"
568 allmods[mod] += " " + iov + "_zero"
569 else:
570 allmod += " " + mod
571 allmods[mod] += " " + iov
572 else:
573 miss+=1
574 missmod += " " + mod
575 word = 'module' if usenames else 'COOL channel'
576 if miss==0 and nmod>1:
577 alliovs[iov] = " All %s" % plural(nmod,word)
578 elif miss>0 and miss<10:
579 alliovs[iov] = " %s present, %s missing:%s" % (plural(nmod-miss,word),plural(miss,word),missmod)
580 else:
581 alliovs[iov] = "%s ; %s present, %s missing" % (allmod,plural(nmod-miss,word),plural(miss,word))
582 zeroiovs[iov] = (zero,zeromod)
583
584 if (option&1)==1:
585 print("")
586 for key,value in allmods.items():
587 if value=="":
588 value=" None"
589 print("%s\t%s" % (key,value))
590 if (option&2)==2:
591 print("")
592 for key,value in alliovs.items():
593 if value=="":
594 value=" None"
595 if zeroiovs[key] and zeroiovs[key][0]>0:
596 if "_zero" not in value:
597 print("%s\t%s ; zero-sized blobs for %d modules:%s" % (key,value,zeroiovs[key][0],zeroiovs[key][1]))
598 else:
599 print("%s\t%s ; zero-sized blobs for %d modules" % (key,value,zeroiovs[key][0]))
600 else:
601 print("%s\t%s" % (key,value))
602 else:
603 print("\nNo IOVs found")
604
605
606class TileBlobWriterCrest(TileCalibLogger):
607 """
608 TileBlobWriterCrest is a helper class, managing the details of
609 CREST interactions for the user of TileCalibBlobs.
610 """
611
612 #____________________________________________________________________
613 def __init__(self, db, folderPath, calibDrawerType, payload=None):
614 """
615 Input:
616 - db : db should be a database connection
617 - folderPath: full folder path to create or update
618 """
619
620 #=== initialize base class
621 TileCalibLogger.__init__(self, "TileBlobWriter")
622
623 #=== store db
624 if db is not None and 'CREST' in db and 'http' not in db:
625 self.__db = CREST_HOST
626 else:
627 self.__db = db
628 self.__folderPath = folderPath
629 self.__payload = payload
630 if payload is not None:
631 return
632
633 #=== create default vectors based on calibDrawerType
634 self.__calibDrawerType = calibDrawerType
635 if calibDrawerType in ['TileCalibDrawerFlt', 'Flt']:
636 self.__TileCalibDrawer = TileCalibDrawerFlt
637 self.__defVec = cppyy.gbl.std.vector('std::vector<float>')()
638 elif calibDrawerType in ['TileCalibDrawerBch', 'Bch']:
639 self.__TileCalibDrawer = TileCalibDrawerBch
640 self.__defVec = cppyy.gbl.std.vector('std::vector<unsigned int>')()
641 elif calibDrawerType in ['TileCalibDrawerInt', 'Int']:
642 self.__TileCalibDrawer = TileCalibDrawerInt
643 self.__defVec = cppyy.gbl.std.vector('std::vector<unsigned int>')()
644 elif calibDrawerType in ['CaloCondBlobFlt', 'CaloFlt']:
645 self.__TileCalibDrawer = cppyy.gbl.CaloCondBlobFlt
646 self.__defVec = cppyy.gbl.std.vector('std::vector<float>')()
647 else:
648 raise Exception("Unknown calibDrawerType: %s" % calibDrawerType)
649
650 # Always all drawers should be written
651 self.__drawerBlob = {drawerIdx:Blob() for drawerIdx in range(0, TileCalibUtils.max_draweridx())}
652 self.__drawer = {}
653 #____________________________________________________________________
654 def register(self, since=(MINRUN,MINLBK), tag="", chan=-1, payload=None):
655 """
656 Registers the folder in the database.
657 - since: lower limit of IOV
658 - tag : The tag to write to
659 - chan : COOL channel to write, if negative - all COOL channels are written
660 Comment channel is always written
661
662 The interpretation of the 'since' inputs depends on their type:
663 - tuple(int,int) : run and lbk number
664 """
665
666 if self.__payload is None and payload is None:
667 jdata = {}
668 for drawerIdx,blob in self.__drawerBlob.items():
669 if chan<0 or drawerIdx==chan or drawerIdx==1000:
670 if blob is None or blob==0:
671 b64string = ''
672 else:
673 blob.seek(0)
674 b64string = str(base64.b64encode(blob.read()), 'ascii')
675 jdata[drawerIdx] = [b64string]
676 else:
677 if payload is not None:
678 self.__payload = payload
679 jdata = self.__payload
680
681 (sinceRun, sinceLumi) = since
682 fullTag = tag
683 if self.__folderPath and not (tag.upper().startswith('TILE') or tag.upper().startswith('CALO')):
684 fullTag = TileCalibUtils.getFullTag(self.__folderPath, tag)
685
686 if not self.__db or self.__db.endswith('.json'):
687 # Writting into the json file
688 fileName = f"{fullTag}.{sinceRun}.{sinceLumi}.json"
689 if self.__db:
690 fileName = f'{self.__db[:-5]}.{fileName}'
691
692 with open(fileName, 'w') as the_file:
693 json.dump(jdata, the_file, separators=(',', ':'), sort_keys=True)
694 else:
695 if 'http' in self.__db:
696 self.log().info('Host %s' , (self.__db))
697 self.__api_instance = chai.Database(f"crest:{self.__db}")
698 fileName = 'remote host'
699 else:
700 self.log().info('Local db %s' , (self.__db))
701 self.__api_instance = chai.Database(f"crest_fs:{self.__db}")
702 fileName = self.__db + '/' + fullTag
703
704 (payload_spec,container) = self.create_container_with_blob(jdata)
705
706 try:
707 new_tag = self.__api_instance.create_tag(
708 fullTag,
709 "", # tag description
710 payload_spec,
711 '<timeStamp>run-lumi</timeStamp><addrHeader><address_header service_type="71" clid="1238547719" /></addrHeader><typeName>CondAttrListCollection</typeName>',
712 chai.IovType.RunNumberLumiBlock,
713 "crest-json-single-iov",
714 chai.Synchronization.All,
715 chai.TagStatus.Unlocked
716 )
717 except Exception as e:
718 # Tag might already exist, try to get it
719 try:
720 new_tag = self.__api_instance.get_tag(fullTag)
721 except Exception as e2:
722 self.log().error(f"Failed to create or get tag: {e}")
723 self.log().error(f"Additional error: {e2}")
724 raise Exception( "Failed to write data to %s" % (self.__db) )
725
726 new_tag.add_payload(container, (sinceRun<<32)+sinceLumi)
727
728 #=== print info
729 self.log().info( 'Writting tag "%s"', fullTag)
730 self.log().info( '... since : [%s,%s]' , sinceRun, sinceLumi)
731 if self.__payload is None:
732 self.log().info( '... with comment field: "%s"', self.getComment())
733 self.log().info( '... into file : %s' , fileName)
734
735 #____________________________________________________________________
736 def create_container_with_blob(self, jdata, name=None, type=chai.Type.String):
737 """
738 Create a container with the blob data (base64 encoded).
739
740 Args:
741 jdata: all base64-encoded blob data in json format
742 name: name for field specification
743 type: type for field specification
744
745 Returns:
746 Payload specification with all channels found in jdata
747 Container with the base64-encoded blob data
748
749 This method uses String type with base64 encoding instead of Blob type
750 due to limitations in nanobind's handling of BlobData during serialization
751 """
752 if name is None:
753 if self.__calibDrawerType in ['CaloCondBlobFlt', 'CaloFlt']:
754 name='CaloCondBlob16M'
755 else:
756 name='TileCalibBlob'
757
758 field_spec = chai.FieldSpec([chai.Field(name,type)])
759
760 spec = []
761 for id in jdata.items():
762 spec += [chai.Channel(int(id[0]), "")]
763 channel_spec = chai.ChannelSpec(spec)
764
765 # Create payload specification
766 payload_spec = chai.PayloadSpec(field_spec, channel_spec)
767
768 # Create container
769 container = chai.Container(payload_spec)
770
771 for id in jdata.items():
772 container[int(id[0])].push_string(id[1][0])
773
774 return (payload_spec,container)
775
776 #____________________________________________________________________
777 def setComment(self, author, comment=None):
778 """
779 Sets a general comment in the comment channel.
780 """
782 commentBlob = self.__drawerBlob.get(drawerIdx, None)
783 if commentBlob:
784 commentBlob.resize(0)
785 else:
786 commentBlob = Blob()
787 self.__drawerBlob[drawerIdx] = commentBlob
788
789 if isinstance(author, tuple) and len(author) == 3:
790 tm = time.mktime(datetime.datetime.strptime(author[2], "%a %b %d %H:%M:%S %Y").timetuple())
791 self.__drawer[drawerIdx] = TileCalibDrawerCmt.getInstance(commentBlob, author[0], author[1], int(tm))
792 else:
793 self.__drawer[drawerIdx] = TileCalibDrawerCmt.getInstance(commentBlob, author, comment)
794
795 #____________________________________________________________________
796 def getComment(self, split=False):
797 """
798 Returns the general comment (default if none is set)
799 """
801 comment = self.__drawer.get(drawerIdx, None)
802 if comment:
803 if split:
804 return (comment.getAuthor(), self.__comment.getComment(), self.__comment.getDate())
805 else:
806 return comment.getFullComment()
807 else:
808 return "<no comment found>"
809
810 #____________________________________________________________________
811 def getDrawer(self, ros, drawer, calibDrawerTemplate=None):
812 """
813 Returns a TileCalibDrawer object of requested type
814 for the given ROS and drawer.
815 """
816
817 try:
818 drawerIdx = getDrawerIdx(ros, drawer)
819 calibDrawer = self.__drawer.get(drawerIdx, None)
820
821 if not calibDrawer:
822 if self.__calibDrawerType in ['CaloCondBlobFlt', 'CaloFlt']:
823 calibDrawer = self.__TileCalibDrawer.getInstance(self.__drawerBlob[drawerIdx])
824 else:
825 calibDrawer = self.__TileCalibDrawer.getInstance(self.__drawerBlob[drawerIdx], self.__defVec,0,0)
826 self.__drawer[drawerIdx] = calibDrawer
827
828 #=== clone if requested
829 if calibDrawerTemplate:
830 calibDrawer.clone(calibDrawerTemplate)
831
832 return calibDrawer
833
834 except Exception as e:
835 self.log().critical( e )
836 return None
837
838 #____________________________________________________________________
839 def zeroBlob(self, ros, drawer):
840 """
841 Resets blob size to zero
842 """
843 try:
844 drawerIdx = getDrawerIdx(ros, drawer)
845 blob = self.__drawerBlob[drawerIdx]
846 blob.resize(0)
847 except Exception as e:
848 self.log().critical( e )
849 return None
850
851
852#____________________________________________________________________
853def getDrawerIdx(ros, drawer):
854 """
855 Calculates TileCalibDrawer index
856 from ros number and drawer number
857 """
858
859 if ros<0 or ros>4:
860 drawerIdx = drawer
861 else:
862 drawerIdx = TileCalibUtils.getDrawerIdx(ros, drawer)
863
864 return drawerIdx
865
866#____________________________________________________________________
867def plural(n, txt):
868 """
869 merges integer number and text
870 and adds 's' at the end of text
871 if n is not equal 1
872 """
873
874 text = f'{n} {txt}'
875 if n!=1:
876 text += 's'
877 return text
const bool debug
void print(char *figname, TCanvas *c1)
#define min(a, b)
Definition cfImp.cxx:40
static const TileCalibDrawerBch * getInstance(const coral::Blob &blob)
Returns a pointer to a const TileCalibDrawerBch.
static const TileCalibDrawerCmt * getInstance(const coral::Blob &blob)
Returns a pointer to a const TileCalibDrawerCmt.
static const TileCalibDrawerFlt * getInstance(const coral::Blob &blob)
Returns a pointer to a const TileCalibDrawerFlt.
static const TileCalibDrawerInt * getInstance(const coral::Blob &blob)
Returns a pointer to a const TileCalibDrawerBch.
static TileCalibDrawerOfc * getInstance(coral::Blob &blob, uint16_t objVersion, uint32_t nSamples, int32_t nPhases, uint16_t nChans, uint16_t nGains, std::string_view author="", std::string_view comment="", uint64_t timeStamp=0)
Returns a pointer to a non-const TileCalibDrawerOfc.
static std::string getClassName(TileCalibType::TYPE type)
Returns the class name.
static std::string getDrawerString(unsigned int ros, unsigned int drawer)
Return the drawer name, e.g.
static unsigned int max_draweridx()
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.
static std::string getFullTag(const std::string &folder, const std::string &tag)
Returns the full tag string, composed of camelized folder name and tag part.
static unsigned int getCommentChannel()
Returns the COOL channel number for the comment channel.
getIovs(self, since=(MINRUN, MINLBK), until=(MAXRUN, MAXLBK))
readIov(self, runlumi=None, dbg=False)
getFolderTag(self, folder, prefix, globalTag, api=None)
getPayload(self, runlumi=None, dbg=False)
__init__(self, db, folder='', tag='', run=None, lumi=0, modmin=0, modmax=275, copyBlob=False)
getDrawer(self, ros, mod, runlumi=None, dbg=False, useDefault=True)
dumpIovs(self, iovList, rosmin, rosmax, drawermin, drawermax, option=1, comment=False, usenames=True)
getComment(self, runlumi=None, split=False)
getBlob(self, ros, mod, runlumi=None, dbg=False)
getDrawer(self, ros, drawer, calibDrawerTemplate=None)
register(self, since=(MINRUN, MINLBK), tag="", chan=-1, payload=None)
setComment(self, author, comment=None)
__init__(self, db, folderPath, calibDrawerType, payload=None)
create_container_with_blob(self, jdata, name=None, type=chai.Type.String)
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:132
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179