ATLAS Offline Software
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 
26 from TileCalibBlobPython import TileCalibTools
27 from TileCalibBlobObjs.Classes import TileCalibUtils
28 import os, sys, getopt
29 from builtins import input
30 os.environ['TERM'] = 'linux'
31 
32 # main defaults are here - can be modified from command line
33 run=999999999
34 run2=0 # will be set to "run" if not on command line
35 lumi=0
36 lumi2=-1 # will be set to "lumi" if not on command line
37 maxdiff=-1.0 # dump all values
38 maxdiffpercent=-1.0
39 folderPath = "/TILE/OFL02/CALIB/CES"
40 folderPath2 = "none" # will be set to "folderPath" if not on command line
41 tag = "RUN2-HLT-UPD1-01"
42 tag2 = "none"
43 schema = "COOLOFL_TILE"
44 schema2 = "none"
45 instance = "CONDBR2"
46 instance2 = "none"
47 server = "FRONTIER"
48 server2 = "none"
49 sqlfn = "none"
50 sqlfn2 = "none"
51 help = 0
52 
53 #------------------------------- parse arguments and change defaults
54 # print ('ARGV :', sys.argv[1:])
55 options, 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)
57 for 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 
98 if not run2:
99  run2=run
100 if lumi2<0:
101  lumi2=lumi
102 if folderPath2=="none":
103  folderPath2=folderPath
104 if tag2=="none":
105  tag2=tag
106 if schema2=="none":
107  schema2=schema
108 if instance2=="none":
109  instance2=instance
110 if server2=="none":
111  server2=server
112 if sqlfn2=="same":
113  sqlfn2=sqlfn
114 
115 #if maxdiffpercent>-1:
116 # maxdiff=-1;
117 
118 print ('run ',run, 'lumi ',lumi, 'run2 ',run2, 'lumi2 ',lumi2)
119 print ('maxdiff ',maxdiff)
120 print ('maxdiffpercent ',maxdiffpercent)
121 print ('folder ',folderPath, 'folder2 ',folderPath2)
122 print ('tag ',tag, 'tag2 ',tag2)
123 print ('schema ', schema, 'schema2 ', schema2)
124 print ('instance ', instance, 'instance2 ', instance2)
125 print ('server ', server, 'server2 ', server2)
126 print ('sqlfn ',sqlfn, 'sqlfn2 ',sqlfn2)
127 
128 if 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 
143 connStr=schema+'/'+instance if ':' not in schema and ';' not in schema and '/' not in schema else schema
144 connStr2=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 
151 if sqlfn == 'none':
152  db = TileCalibTools.openDbConn(connStr, server)
153 else:
154  db = TileCalibTools.openDb('SQLITE', instance, 'READONLY',schema,sqlfn)
155 if sqlfn2 == 'none':
156  db2 = TileCalibTools.openDbConn(connStr2, server2)
157 else:
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 
169 if tag.startswith('TileO'):
170  folderTag = tag
171 elif tag:
172  folderTag = TileCalibUtils.getFullTag(folderPath, tag)
173 else:
174  folderTag = ""
175 
176 if tag2.startswith('TileO'):
177  folderTag2 = tag2
178 elif tag2:
179  folderTag2 = TileCalibUtils.getFullTag(folderPath2, tag2)
180 else:
181  folderTag2 = ""
182 
183 #==================================================
184 
185 from TileCalibBlobPython.TileCalibLogger import getLogger
186 log = getLogger("readFromCool")
187 import logging
188 log.setLevel(logging.DEBUG)
189 f=open('output.ascii', 'w')
190 if 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
193 else:
194  writedif=False
195 
196 log.info("Initializing folder %s with tag %s", folderPath, folderTag)
197 log.info("Initializing folder %s with tag %s", folderPath2, folderTag2)
198 
199 blobReader = TileCalibTools.TileBlobReader(db,folderPath, folderTag)
200 blobReader2 = TileCalibTools.TileBlobReader(db2,folderPath2, folderTag2)
201 #blobReader.log().setLevel(logging.DEBUG)
202 
203 #=== get drawer with status at given run
204 
205 ros = 1 # ros 0 sometimes contains obsolete header !
206 drawer = 0
207 log.info("Initializing for run1 %d lumi %d run2 %d lumi2 %d maxdiff %f maxdiffpercent %f", run, lumi, run2, lumi2, maxdiff,maxdiffpercent)
208 log.info("Comment1: %s", blobReader.getComment((run,lumi)))
209 log.info("Comment2: %s", blobReader2.getComment((run2,lumi2)))
210 
211 flt=None
212 r=5
213 d=0
214 while 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()
224 ot = flt.getObjType()
225 ov = flt.getObjVersion()
226 os = flt.getObjSizeByte()//4
227 no = flt.getNObjs()
228 nc = flt.getNChans()
229 ng = flt.getNGains()
230 
231 flt2=None
232 r=5
233 d=0
234 while 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)
243 ot2 = flt2.getObjType()
244 os2 = flt2.getObjSizeByte()//4
245 
246 if (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 
252 v =[]
253 v2=[]
254 for ind in range(0,os):
255  v.append(0)
256  v2.append(0)
257 
258 f.write("Command line parameters used: \n %s \n" % sys.argv[1:])
259 f.write("---- Object header for ros=%d drawer=%d \n" % (r,d))
260 f.write("ObjType : %d \n" % ot)
261 f.write("ObjVersion : %d \n" % ov)
262 f.write("ObjSize[4bytes]: %d \n" % os)
263 f.write("NObjs : %d \n" % no)
264 f.write("NChannels : %d \n" % nc)
265 f.write("NGains : %d \n" % ng)
266 
267 answ='n'
268 #=== get value for a gived channel
269 for 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
323 db.closeDatabase()
324 db2.closeDatabase()
325 f.close()
326 if writedif:
327  fd.close()
TileCalibUtils::getMaxDrawer
static unsigned int getMaxDrawer(unsigned int ros)
Returns the maximal channel number for a given drawer.
Definition: TileCalibUtils.cxx:136
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
Trk::open
@ open
Definition: BinningType.h:40
TileCalibUtils::getDrawerString
static std::string getDrawerString(unsigned int ros, unsigned int drawer)
Return the drawer name, e.g.
Definition: TileCalibUtils.cxx:145
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
TileCalibUtils::max_chan
static unsigned int max_chan()
Python compatibility function.
Definition: TileCalibUtils.h:112
python.CaloCondLogger.getLogger
def getLogger(name="CaloCond")
Definition: CaloCondLogger.py:16
TileCalibUtils::getFullTag
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.
Definition: TileCalibUtils.cxx:33
python.LArMinBiasAlgConfig.float
float
Definition: LArMinBiasAlgConfig.py:65