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