ATLAS Offline Software
Loading...
Searching...
No Matches
ReadFromCoolCompare.py
Go to the documentation of this file.
1#!/bin/env python
2
3# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4#
5# ReadFromCoolCompare.py
6# (Based on A. Solodkov's script ReadCsFromCool.py)
7# Andrei Artamonov 2010-09-22
8# Note: this is a low level tool to be used only for tests
9# It reads conditions from two sources (COOL and/or sqlite),
10# compares values and dumps to the file difference, if it is above
11# specified threshould
12# Update: Yuri Smirnov 01/28/2016: folder tag protection added including
13# not only short (reduced) tag names, but also full tag names/paths
14#
15#=== examples of folder/tag names
16#folderPath = "/TILE/OFL02/CALIB/CES"
17#folderPath = "/TILE/OFL02/TIME/CHANNELOFFSET/PHY"
18#folderPath = "/TILE/OFL02/NOISE/SAMPLE"
19#tag = "RUN2-HLT-UPD1-01"
20#folderPath = "/TILE/ONL01/CALIB/CIS/LIN"
21#folderPath = "/TILE/ONL01/MUID"
22#folderPath = "/TILE/ONL01/FRAG1"
23#folderTag = ""
24#==================================================
25
26from TileCalibBlobPython import TileCalibTools
27from TileCalibBlobObjs.Classes import TileCalibUtils
28import os, sys, getopt
29from builtins import input
30os.environ['TERM'] = 'linux'
31
32# main defaults are here - can be modified from command line
33run=999999999
34run2=0 # will be set to "run" if not on command line
35lumi=0
36lumi2=-1 # will be set to "lumi" if not on command line
37maxdiff=-1.0 # dump all values
38maxdiffpercent=-1.0
39folderPath = "/TILE/OFL02/CALIB/CES"
40folderPath2 = "none" # will be set to "folderPath" if not on command line
41tag = "RUN2-HLT-UPD1-01"
42tag2 = "none"
43schema = "COOLOFL_TILE"
44schema2 = "none"
45instance = "CONDBR2"
46instance2 = "none"
47server = "FRONTIER"
48server2 = "none"
49sqlfn = "none"
50sqlfn2 = "none"
51help = 0
52
53#------------------------------- parse arguments and change defaults
54# print ('ARGV :', sys.argv[1:])
55options, remainder = getopt.getopt(sys.argv[1:], 'h', ['run=','run2=','lumi=','lumi2=','maxdiff=','maxdiffpercent=','folder=','folder2=','tag=','tag2=','schema=','schema2=','instance=','instance2=','server=','server2=','sqlfn=','sqlfn2='])
56# print ('OPTIONS :', options)
57for opt, arg in options:
58 arg = arg.strip()
59 if opt in ('--h'):
60 help = 1
61 elif opt in ('--run'):
62 run = int(arg)
63 elif opt in ('--run2'):
64 run2 = int(arg)
65 elif opt in ('--lumi'):
66 lumi = int(arg)
67 elif opt in ('--lumi2'):
68 lumi2 = int(arg)
69 elif opt in ('--maxdiff'):
70 maxdiff = float(arg)
71 elif opt in ('--maxdiffpercent'):
72 maxdiffpercent = float(arg)
73 elif opt in ('--folder'):
74 folderPath=arg
75 elif opt in ('--folder2'):
76 folderPath2=arg
77 elif opt in ('--tag'):
78 tag=arg
79 elif opt in ('--tag2'):
80 tag2=arg
81 elif opt in ('--schema'):
82 schema=arg
83 elif opt in ('--schema2'):
84 schema2=arg
85 elif opt in ('--instance'):
86 instance=arg
87 elif opt in ('--instance2'):
88 instance2=arg
89 elif opt in ('--server'):
90 server=arg
91 elif opt in ('--server2'):
92 server2=arg
93 elif opt in ('--sqlfn'):
94 sqlfn=arg
95 elif opt in ('--sqlfn2'):
96 sqlfn2=arg
97
98if not run2:
99 run2=run
100if lumi2<0:
101 lumi2=lumi
102if folderPath2=="none":
103 folderPath2=folderPath
104if tag2=="none":
105 tag2=tag
106if schema2=="none":
107 schema2=schema
108if instance2=="none":
109 instance2=instance
110if server2=="none":
111 server2=server
112if sqlfn2=="same":
113 sqlfn2=sqlfn
114
115#if maxdiffpercent>-1:
116# maxdiff=-1;
117
118print ('run ',run, 'lumi ',lumi, 'run2 ',run2, 'lumi2 ',lumi2)
119print ('maxdiff ',maxdiff)
120print ('maxdiffpercent ',maxdiffpercent)
121print ('folder ',folderPath, 'folder2 ',folderPath2)
122print ('tag ',tag, 'tag2 ',tag2)
123print ('schema ', schema, 'schema2 ', schema2)
124print ('instance ', instance, 'instance2 ', instance2)
125print ('server ', server, 'server2 ', server2)
126print ('sqlfn ',sqlfn, 'sqlfn2 ',sqlfn2)
127
128if help:
129 print ('******** Above, the defaults are shown, one can change them')
130 print (' using command-line options: ReadFromCoolCompare.py --option=value')
131 print (' Any number of options in random order can be given')
132 print (' If run2,lumi2,schema2,tag2 or instance2 are not given, the values')
133 print (' of run,lumi,schema,tag and instance will be assigned to them')
134 print ('For singleversion folders (ONL01) there should be option with empty tag: --tag=')
135 print ('conditions with options maxdiffpercent and maxdiff are used')
136 print (' in logical AND, so, both have to be TRUE to print the data')
137 print ('\n Examples:')
138 print ('\npython ReadFromCoolCompare.py --maxdiff=100 --run=100012 --folder=/TILE/ONL01/CALIB/CES --tag= --schema=COOLONL_TILE --tag2=HLT-REPC-004 --sqlfn2=tileSqlite.db --folder2=/TILE/OFL02/CALIB/CES --schema2=COOLOFL_TILE')
139 print ('\npython ReadFromCoolCompare.py --folder=/TILE/ONL01/FRAG1 --schema=COOLONL_TILE --tag= --maxdiffpercent=5.5 --run=160000 --run2=999999999')
140 sys.exit()
141
142
143connStr=schema+'/'+instance if ':' not in schema and ';' not in schema and '/' not in schema else schema
144connStr2=schema2+'/'+instance2 if ':' not in schema2 and ';' not in schema2 and '/' not in schema2 else schema2
145
146#===================================================================
147#====================== FILL DB parameters BELOW ===================
148#===================================================================
149#--- Read from COOL server or from local sqlite file:
150
151if sqlfn == 'none':
152 db = TileCalibTools.openDbConn(connStr, server)
153else:
154 db = TileCalibTools.openDb('SQLITE', instance, 'READONLY',schema,sqlfn)
155if sqlfn2 == 'none':
156 db2 = TileCalibTools.openDbConn(connStr2, server2)
157else:
158 db2 = TileCalibTools.openDb('SQLITE', instance2, 'READONLY',schema2,sqlfn2)
159
160#if tag:
161# folderTag = TileCalibUtils.getFullTag(folderPath, tag)
162#else:
163# folderTag = ""
164#if tag2:
165# folderTag2 = TileCalibUtils.getFullTag(folderPath2, tag2)
166#else:
167# folderTag2 = ""
168
169if tag.startswith('TileO'):
170 folderTag = tag
171elif tag:
172 folderTag = TileCalibUtils.getFullTag(folderPath, tag)
173else:
174 folderTag = ""
175
176if tag2.startswith('TileO'):
177 folderTag2 = tag2
178elif tag2:
179 folderTag2 = TileCalibUtils.getFullTag(folderPath2, tag2)
180else:
181 folderTag2 = ""
182
183#==================================================
184
185from TileCalibBlobPython.TileCalibLogger import getLogger
186log = getLogger("readFromCool")
187import logging
188log.setLevel(logging.DEBUG)
189f=open('output.ascii', 'w')
190if run2!=run and tag2==tag and folderPath2==folderPath and folderPath.startswith("/TILE/OFL02/TIME"):
191 fd=open('from_%d_to_%d.dif'%(run2,run), 'w')
192 writedif=True
193else:
194 writedif=False
195
196log.info("Initializing folder %s with tag %s", folderPath, folderTag)
197log.info("Initializing folder %s with tag %s", folderPath2, folderTag2)
198
199blobReader = TileCalibTools.TileBlobReader(db,folderPath, folderTag)
200blobReader2 = TileCalibTools.TileBlobReader(db2,folderPath2, folderTag2)
201#blobReader.log().setLevel(logging.DEBUG)
202
203#=== get drawer with status at given run
204
205ros = 1 # ros 0 sometimes contains obsolete header !
206drawer = 0
207log.info("Initializing for run1 %d lumi %d run2 %d lumi2 %d maxdiff %f maxdiffpercent %f", run, lumi, run2, lumi2, maxdiff,maxdiffpercent)
208log.info("Comment1: %s", blobReader.getComment((run,lumi)))
209log.info("Comment2: %s", blobReader2.getComment((run2,lumi2)))
210
211flt=None
212r=5
213d=0
214while not flt:
215 d-=1
216 if d<0:
217 r-=1
218 if r<0:
219 log.error("No valid drawers in first database")
220 sys.exit()
222 flt = blobReader.getDrawer(r, d, (run,lumi), False, False)
223#flt.dump()
224ot = flt.getObjType()
225ov = flt.getObjVersion()
226os = flt.getObjSizeByte()//4
227no = flt.getNObjs()
228nc = flt.getNChans()
229ng = flt.getNGains()
230
231flt2=None
232r=5
233d=0
234while not flt2:
235 d-=1
236 if d<0:
237 r-=1
238 if r<0:
239 log.error("No valid drawers in second database")
240 sys.exit()
242 flt2 = blobReader2.getDrawer(r, d, (run2,lumi2), False, False)
243ot2 = flt2.getObjType()
244os2 = flt2.getObjSizeByte()//4
245
246if (os != os2) or (ot != ot2):
247 log.error("Object sizes (%s vs %s) or types (%s vs %s) are different", os, os2, ot, ot2)
248 answ=input(' continue anyway? (y/n)')
249 if (answ != 'y'):
250 sys.exit()
251
252v =[]
253v2=[]
254for ind in range(0,os):
255 v.append(0)
256 v2.append(0)
257
258f.write("Command line parameters used: \n %s \n" % sys.argv[1:])
259f.write("---- Object header for ros=%d drawer=%d \n" % (r,d))
260f.write("ObjType : %d \n" % ot)
261f.write("ObjVersion : %d \n" % ov)
262f.write("ObjSize[4bytes]: %d \n" % os)
263f.write("NObjs : %d \n" % no)
264f.write("NChannels : %d \n" % nc)
265f.write("NGains : %d \n" % ng)
266
267answ='n'
268#=== get value for a gived channel
269for ros in range(0,5):
270 for mod in range(0, min(64,TileCalibUtils.getMaxDrawer(ros))):
272 #log.info("ros %d, drawer %s at run %d" % (ros, modName, run))
273 flt = blobReader.getDrawer(ros, mod,(run,lumi), False, False)
274 flt2 = blobReader2.getDrawer(ros, mod,(run2,lumi2), False, False)
275 if flt and flt2:
276 osc = flt.getObjSizeByte()//4
277 os2c = flt2.getObjSizeByte()//4
278 oscMax = max(osc,os2c)
279 if (((os != osc) or (os2 != os2c)) and answ != 'y'):
280 if (ros==0 and osc==os2c and os==os2):
281 log.warning("Object sizes are different for last drawer in DB (%s) and default drawer %s (%s)", os, modName, osc)
282 else:
283 log.error("Object sizes are different for last drawer in DB (%s and %s) and drawer %s (%s and %s)", os, os2, modName, osc, os2c)
284 answ=input(' continue anyway? (y/n)')
285 if (answ != 'y'):
286 sys.exit()
287 else:
288 for ind in range(0,oscMax):
289 v.append(0)
290 v2.append(0)
291
292
293 for chn in range(TileCalibUtils.max_chan()):
294 chnName = " %2i" % chn
295 for adc in range(ng):
296 for ind in range(0,oscMax):
297 if (ind<osc):
298 v[ind] = flt.getData(chn, adc, ind)
299 if (ind<os2c):
300 v2[ind] = flt2.getData(chn, adc, ind)
301 dv12 = v[ind] - v2[ind]
302 if v2[ind] == 0:
303 if v[ind] == 0:
304 dv12percent=0
305 else:
306 dv12percent=dv12*100./v[ind]
307 else:
308 dv12percent=dv12*100./v2[ind]
309 #print ( modName, ' chann ', repr(chn), ' adc ', repr(adc), ' ind ', repr(ind), ' val1 ', repr(v[ind]),' val2 ', repr(v2[ind]), ' diff ', repr(dv12), 'percent ', repr(dv12percent))
310 if abs(dv12) > maxdiff and abs(dv12percent) > maxdiffpercent:
311 if ot==30: # integers
312 f.write('%s chann %2d adc %d ind %d val1 %d val2 %d diff %d \n' % (modName,chn,adc,ind,v[ind],v2[ind],dv12))
313 elif ot==20: # bad channels
314 f.write('%s chann %2d adc %d ind %d val1 %s val2 %s diff %f \n' % (modName,chn,adc,ind,hex(int(v[ind])),hex(int(v2[ind])),dv12))
315 else: # floats
316 f.write('%s chann %2d adc %d ind %d val1 %.4f val2 %.4f diff %.4f %.2f%%\n' % (modName,chn,adc,ind,v[ind],v2[ind],dv12,dv12percent))
317 if writedif and adc==0 and ind==0:
318 fd.write("%s ch %2d %.4f\n" % (modName,chn,dv12))
319
320 #f.write(s + "\n")
321
322#=== close DB and output file
323db.closeDatabase()
324db2.closeDatabase()
325f.close()
326if writedif:
327 fd.close()
#define min(a, b)
Definition cfImp.cxx:40
#define max(a, b)
Definition cfImp.cxx:41
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.
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.