ATLAS Offline Software
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 
11 import getopt,sys,os,json
12 os.environ['TERM'] = 'linux'
13 
14 def 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 ("-o, --output= specify the prefix for output json file")
28 
29 letters = "hS:s:d:t:f:r:l:c:o:"
30 keywords = ["help","server=","schema=","dbname=","tag=","folder=","run=","lumi=","channel=","output="]
31 
32 try:
33  opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
34 except getopt.GetoptError as err:
35  print (str(err))
36  usage()
37  sys.exit(2)
38 
39 # defaults
40 run = 2147483647
41 lumi = 0
42 server = ''
43 schema = 'COOLOFL_TILE/CONDBR2'
44 folderPath = '/TILE/OFL02/CALIB/CIS/LIN'
45 dbName = 'CONDBR2'
46 tag = 'UPD4'
47 channels = list(range(276)) + [1000]
48 output = ""
49 
50 for o, a in opts:
51  a = a.strip()
52  if o in ("-s","--schema"):
53  schema = a
54  elif o in ("-S","--server"):
55  server = a
56  elif o in ("-d","--dbname"):
57  dbName = a
58  elif o in ("-f","--folder"):
59  folderPath = a
60  elif o in ("-t","--tag"):
61  tag = a
62  elif o in ("-r","--run"):
63  run = int(a)
64  elif o in ("-l","--lumi"):
65  lumi = int(a)
66  elif o in ("-c","--channel"):
67  channels = [int(a)]
68  elif o in ("-o","--output"):
69  output = a
70  elif o in ("-h","--help"):
71  usage()
72  sys.exit(2)
73  else:
74  usage()
75  sys.exit(2)
76 
77 import base64
78 import cppyy
79 from PyCool import cool
80 from TileCalibBlobPython import TileCalibTools
81 from TileCalibBlobPython import TileBchTools
82 from TileCalibBlobPython.TileCalibTools import MAXRUN, MAXLBK
83 from TileCalibBlobObjs.Classes import TileCalibUtils, TileCalibDrawerCmt
84 
85 Blob = cppyy.gbl.coral.Blob
86 
87 def make_blob(string):
88  b = Blob()
89  b.write(string)
90  b.seek(0)
91  return b
92 
93 from TileCalibBlobPython.TileCalibLogger import getLogger
94 log = getLogger("CopyBlob")
95 import logging
96 logLevel=logging.DEBUG
97 log.setLevel(logLevel)
98 
99 #=== Read from COOL server:
100 
101 if os.path.isfile(schema):
102  schema = 'sqlite://;schema='+schema+';dbname='+dbName
103 else:
104  log.info("File %s was not found, assuming it's full schema string" , schema)
105 
106 db = TileCalibTools.openDbConn(schema,server)
107 folderTag = TileCalibTools.getFolderTag(db if 'CONDBR2' in schema else schema, folderPath, tag)
108 log.info("Initializing folder %s with tag %s", folderPath, folderTag)
109 
110 folder = db.getFolder(folderPath)
111 
112 log.info( "\n" )
113 
114 payloadSpec = cool.RecordSpecification()
115 payloadSpec.extend( 'TileCalibBlob', cool.StorageType.Blob64k )
116 folderMode = cool.FolderVersioning.MULTI_VERSION
117 folderSpec = cool.FolderSpecification(folderMode, payloadSpec)
118 
119 #=== loop over all COOL channels
120 obj = None
121 suff = ""
122 since = (run<<32)+lumi
123 maxSince = 0
124 jdata={}
125 for chan in channels:
126  try:
127  obj = folder.findObject( since, chan, folderTag )
128  objsince = obj.since()
129  objuntil = obj.until()
130  (sinceRun,sinceLum) = (objsince>>32,objsince&0xFFFFFFFF)
131  (untilRun,untilLum) = (objuntil>>32,objuntil&0xFFFFFFFF)
132  if (objsince>maxSince):
133  maxSince=objsince
134  coralblob = obj.payload()[0]
135  blob = coralblob.read()
136  b64string = str(base64.b64encode(blob),'ascii')
137  jdata[chan] = [b64string]
138  if chan==1000:
140  fullcmt = cmt.getFullComment()
141  log.info(fullcmt+"\n")
142  # back conversion - just for test
143  blob1 = base64.decodebytes(bytes(b64string,'ascii'))
144  coralblob = make_blob(blob1)
146  fullcmt1 = cmt1.getFullComment()
147  if(fullcmt!=fullcmt1): log.error(fullcmt1+"\n")
148  except Exception:
149  log.warning( "Warning: can not read COOL channel %d from input DB" , chan)
150 
151 if output=="":
152  output = folderTag
153 if "." not in output:
154  (sinceRun,sinceLumi) = (maxSince>>32,maxSince&0xFFFFFFFF)
155  suff = "." + str(sinceRun) + "." + str(sinceLumi) + ".json"
156  ofile = output + suff
157 else:
158  ofile = output
159 with open(ofile, 'w') as the_file:
160  json.dump(jdata,the_file)
161  the_file.write('\n')
162 
163 print("see file",ofile)
164 
165 #=== close DB
166 db.closeDatabase()
CopyBlobFromCool.usage
def usage()
Definition: CopyBlobFromCool.py:14
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:26
CopyBlobFromCool.make_blob
def make_blob(string)
Definition: CopyBlobFromCool.py:87
Trk::open
@ open
Definition: BinningType.h:40
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
TileCalibDrawerCmt::getInstance
static const TileCalibDrawerCmt * getInstance(const coral::Blob &blob)
Returns a pointer to a const TileCalibDrawerCmt.
Definition: TileCalibDrawerCmt.cxx:24
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:567
str
Definition: BTagTrackIpAccessor.cxx:11
CopyBlobFromCool.Blob
Blob
Definition: CopyBlobFromCool.py:85
python.CaloCondLogger.getLogger
def getLogger(name="CaloCond")
Definition: CaloCondLogger.py:16