ATLAS Offline Software
Loading...
Searching...
No Matches
CopyBlobFromCool.py
Go to the documentation of this file.
1#!/bin/env python
2
3# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4#
5# CopyBlobFromCool.py
6# Sanya Solodkov <Sanya.Solodkov@cern.ch>, 2025-02-04
7#
8# Purpose: Read blobs from COOL and write them to JSON file
9#
10
11import getopt,sys,os,json
12os.environ['TERM'] = 'linux'
13
14def usage():
15 print ("Usage: ",sys.argv[0]," [OPTION] ... ")
16 print ("Read TileCal blobs from COOL and convert them to JSON format for CREST")
17 print ("")
18 print ("-h, --help shows this help")
19 print ("-s, --schema= specify schema to use, ONL or OFL for RUN1 or ONL2 or OFL2 for RUN2 or MC")
20 print ("-S, --server= specify server - ORACLE or FRONTIER, default is FRONTIER")
21 print ("-d, --dbname= specify the database name e.g. CONDBR2")
22 print ("-f, --folder= specify folder to use e.g. /TILE/OFL02/STATUS/ADC")
23 print ("-t, --tag= specify tag to use, f.i. UPD1 or UPD4 or tag suffix like RUN2-UPD4-04")
24 print ("-r, --run= specify run number, by default uses latest iov")
25 print ("-l, --lumi= specify lumi block number, default is 0")
26 print ("-c, --channel= specify COOL channel, by default COOL channels 0-275 and 1000 are used")
27 print ("-f, --full if sqlite file doesn't contain everything, try to find missing COOL channels in Oracle DB")
28 print ("-o, --output= specify the prefix for output json file")
29
30letters = "hS:s:d:t:f:r:l:c:o:f"
31keywords = ["help","server=","schema=","dbname=","tag=","folder=","run=","lumi=","channel=","output=","full"]
32
33try:
34 opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
35except getopt.GetoptError as err:
36 print (str(err))
37 usage()
38 sys.exit(2)
39
40# defaults
41run = 2147483647
42lumi = 0
43server = ''
44schema = 'COOLOFL_TILE/CONDBR2'
45folderPath = '/TILE/OFL02/CALIB/CIS/LIN'
46dbName = 'CONDBR2'
47tag = 'UPD4'
48channels = list(range(276)) + [1000]
49output = ""
50full = False
51
52for o, a in opts:
53 a = a.strip()
54 if o in ("-s","--schema"):
55 schema = a
56 elif o in ("-S","--server"):
57 server = a
58 elif o in ("-d","--dbname"):
59 dbName = a
60 elif o in ("-f","--folder"):
61 folderPath = a
62 elif o in ("-t","--tag"):
63 tag = a
64 elif o in ("-r","--run"):
65 run = int(a)
66 elif o in ("-l","--lumi"):
67 lumi = int(a)
68 elif o in ("-c","--channel"):
69 channels = [int(a)]
70 elif o in ("-o","--output"):
71 output = a
72 elif o in ("-f","--full"):
73 full = True
74 elif o in ("-h","--help"):
75 usage()
76 sys.exit(2)
77 else:
78 usage()
79 sys.exit(2)
80
81import base64
82import cppyy
83from PyCool import cool
84from TileCalibBlobPython import TileCalibTools
85from TileCalibBlobObjs.Classes import TileCalibDrawerCmt
86
87Blob = cppyy.gbl.coral.Blob
88
89def make_blob(string):
90 b = Blob()
91 b.write(string)
92 b.seek(0)
93 return b
94
95from TileCalibBlobPython.TileCalibLogger import getLogger
96log = getLogger("CopyBlob")
97import logging
98logLevel=logging.DEBUG
99log.setLevel(logLevel)
100
101#=== Read from COOL server:
102
103if os.path.isfile(schema):
104 schema = 'sqlite://;schema='+schema+';dbname='+dbName
105else:
106 log.info("File %s was not found, assuming it's full schema string" , schema)
107
108db = TileCalibTools.openDbConn(schema,server)
109folderTag = TileCalibTools.getFolderTag(db if 'CONDBR2' in schema else schema, folderPath, tag)
110log.info("Initializing folder %s with tag %s", folderPath, folderTag)
111
112folder = db.getFolder(folderPath)
113
114log.info( "\n" )
115
116payloadSpec = cool.RecordSpecification()
117payloadSpec.extend( 'TileCalibBlob', cool.StorageType.Blob64k )
118folderMode = cool.FolderVersioning.MULTI_VERSION
119folderSpec = cool.FolderSpecification(folderMode, payloadSpec)
120
121#=== loop over all COOL channels
122obj = None
123suff = ""
124since = (run<<32)+lumi
125maxSince = 0
126jdata={}
127missingChannels = []
128if full and 'sqlite' in schema:
129 maxIter = 2
130else:
131 maxIter = 1
132for iter in range(maxIter):
133 if missingChannels:
134 dbOra = TileCalibTools.openDbOracle(server, schema, folderPath)
135 folder = dbOra.getFolder(folderPath)
136 channels = missingChannels
137 missingChannels = []
138 for chan in channels:
139 try:
140 obj = folder.findObject( since, chan, folderTag )
141 objsince = obj.since()
142 objuntil = obj.until()
143 (sinceRun,sinceLum) = (objsince>>32,objsince&0xFFFFFFFF)
144 (untilRun,untilLum) = (objuntil>>32,objuntil&0xFFFFFFFF)
145 if (objsince>maxSince):
146 maxSince=objsince
147 coralblob = obj.payload()[0]
148 blob = coralblob.read()
149 b64string = str(base64.b64encode(blob),'ascii')
150 jdata[chan] = [b64string]
151 if chan==1000:
153 fullcmt = cmt.getFullComment()
154 log.info(fullcmt+"\n")
155 # back conversion - just for test
156 blob1 = base64.decodebytes(bytes(b64string,'ascii'))
157 coralblob = make_blob(blob1)
159 fullcmt1 = cmt1.getFullComment()
160 if(fullcmt!=fullcmt1): log.error(fullcmt1+"\n")
161 except Exception:
162 if iter==1:
163 log.warning( "Warning: can not read COOL channel %d from Oracle" , chan)
164 else:
165 if maxIter==2:
166 log.info( "Info: COOL channel %d is missing in input DB, it will be taken from Oracle" , chan)
167 else:
168 log.warning( "Warning: can not read COOL channel %d from input DB" , chan)
169 missingChannels.append(chan)
170 if len(missingChannels)==0:
171 break
172if iter>0:
173 jdata = dict(sorted(jdata.items(), key=lambda item: int(item[0])))
174
175if output=="":
176 output = folderTag
177if "." not in output:
178 (sinceRun,sinceLumi) = (maxSince>>32,maxSince&0xFFFFFFFF)
179 suff = "." + str(sinceRun) + "." + str(sinceLumi) + ".json"
180 ofile = output + suff
181else:
182 ofile = output
183with open(ofile, 'w') as the_file:
184 json.dump(jdata,the_file)
185 the_file.write('\n')
186
187print("see file",ofile)
188
189#=== close DB
190db.closeDatabase()
if(febId1==febId2)
void print(char *figname, TCanvas *c1)
static const TileCalibDrawerCmt * getInstance(const coral::Blob &blob)
Returns a pointer to a const TileCalibDrawerCmt.