00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 """
00015 beam spot validation
00016
00017 A very simple script
00018
00019 usage: %prog -t <tag name>
00020 -o, --output = OUTPUT: filename of output html file.
00021 -p, --path = PATH: path to beam spot scripts.
00022 -w, --web = WEB: html path to website.
00023
00024 Francisco Yumiceva (yumiceva@fnal.gov)
00025 Fermilab 2010
00026
00027 """
00028
00029
00030 import os, string, re, sys, math
00031 import commands, time
00032
00033
00034 import optparse
00035
00036 USAGE = re.compile(r'(?s)\s*usage: (.*?)(\n[ \t]*\n|$)')
00037
00038 def nonzero(self):
00039 "True if options were given"
00040 for v in self.__dict__.itervalues():
00041 if v is not None: return True
00042 return False
00043
00044 optparse.Values.__nonzero__ = nonzero
00045
00046 class ParsingError(Exception): pass
00047
00048 optionstring=""
00049
00050 def exit(msg=""):
00051 raise SystemExit(msg or optionstring.replace("%prog",sys.argv[0]))
00052
00053 def parse(docstring, arglist=None):
00054 global optionstring
00055 optionstring = docstring
00056 match = USAGE.search(optionstring)
00057 if not match: raise ParsingError("Cannot find the option string")
00058 optlines = match.group(1).splitlines()
00059 try:
00060 p = optparse.OptionParser(optlines[0])
00061 for line in optlines[1:]:
00062 opt, help=line.split(':')[:2]
00063 short,long=opt.split(',')[:2]
00064 if '=' in opt:
00065 action='store'
00066 long=long.split('=')[0]
00067 else:
00068 action='store_true'
00069 p.add_option(short.strip(),long.strip(),
00070 action = action, help = help.strip())
00071 except (IndexError,ValueError):
00072 raise ParsingError("Cannot parse the option string correctly")
00073 return p.parse_args(arglist)
00074
00075
00076
00077 def cmp_tags(a,b):
00078
00079 yeara = int(a.split('_')[1])
00080 yearb = int(b.split('_')[1])
00081
00082 if yeara < yearb: return -1
00083 if yeara > yearb: return 1
00084
00085 suffix = "_offline"
00086 if a.find("_express") != -1:
00087 suffix = "_express"
00088 if a.find("_prompt") != -1:
00089 suffix = "_prompt"
00090
00091 tmpa = a.replace("BeamSpotObjects_2009_v","")
00092 tmpa = tmpa.replace(suffix,"")
00093
00094 tmpb = b.replace("BeamSpotObjects_2009_v","")
00095 tmpb = tmpb.replace(suffix,"")
00096
00097 na = int(tmpa)
00098 nb = int(tmpb)
00099 if na < nb: return -1
00100 if na == nb: return 0
00101 if na > nb: return 1
00102
00103
00104
00105 def dump_header(lines):
00106
00107 lines.append('''
00108 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>
00109 <head><title>Beam Spot Calibration Status</title></head>
00110
00111 <body>
00112
00113 <H1>Status of the Beam Spot Calibration</H1>
00114 <BR>
00115 <BR>
00116 This page is updated every 10 minutes:
00117 <BR>
00118 <strong><script src="datemod.js"
00119 type="text/javascript"></script></strong>
00120
00121 ''')
00122
00123
00124
00125 def dump_footer(lines):
00126
00127 lines.append('</body>\n</html>\n')
00128
00129
00130 def write_tags(tags, lines):
00131
00132 end = '\n'
00133 br = '<BR>'+end
00134
00135 lines.append('<tr>'+end)
00136 for i in tags.keys():
00137
00138 lines.append('<th>'+i)
00139 lines.append('</th>'+end)
00140 lines.append('</tr>'+end)
00141
00142 for ntags in range(0,len(tags['offline'])):
00143 lines.append('<tr>'+end)
00144 for i in tags.keys():
00145 alist = tags[i]
00146 if ntags < len(tags[i]):
00147 lines.append('<td> '+alist[ntags]+' </td>'+end)
00148 else:
00149 lines.append('<td> </td>')
00150 lines.append('</tr>'+end)
00151
00152
00153 def write_iovs(iovs, lines):
00154
00155 end = '\n'
00156 br = '<BR>'+end
00157
00158 lines.append('<tr>'+end)
00159 for i in iovs.keys():
00160 lines.append('<th>'+i)
00161 lines.append('</th>'+end)
00162 lines.append('</tr>'+end)
00163
00164 lines.append('<tr>'+end)
00165 for i in iovs.keys():
00166 aIOVlist = iovs[i]
00167 aIOV = IOV()
00168 if len(aIOVlist) > 0:
00169 aIOV = aIOVlist[0]
00170 lines.append('<td> '+aIOV.type+' </td>'+end)
00171 lines.append('</tr>'+end)
00172
00173 for niovs in range(0,len(iovs[iovs.keys()[0]])):
00174 lines.append('<tr>'+end)
00175 for i in iovs.keys():
00176 aIOVlist = iovs[i]
00177 aIOV = IOV()
00178 if len(aIOVlist) > niovs:
00179 aIOV = aIOVlist[niovs]
00180 lines.append('<td> '+aIOV.IOVfirst +' - '+aIOV.IOVlast+' </td>'+end)
00181 lines.append('</tr>'+end)
00182
00183
00184 def get_listoftags(dest, auth,):
00185
00186 queryTags_cmd = "cmscond_list_iov -c "+dest+" -P "+auth+" -a | grep BeamSpotObjects"
00187 print queryTags_cmd
00188 outcmd = commands.getstatusoutput( queryTags_cmd )
00189 print outcmd[1]
00190
00191 listtags = outcmd[1].split()
00192
00193 listtags_offline = []
00194 for itag in listtags:
00195 if itag[len(itag)-7:len(itag)] == "offline":
00196 listtags_offline.append(itag)
00197 listtags_express = []
00198 for itag in listtags:
00199 if itag[len(itag)-7:len(itag)] == "express":
00200 listtags_express.append(itag)
00201 listtags_prompt = []
00202 for itag in listtags:
00203 if itag[len(itag)-6:len(itag)] == "prompt":
00204 listtags_prompt.append(itag)
00205
00206 listtags_offline.sort( cmp = cmp_tags )
00207 listtags_offline.reverse()
00208 listtags_express.sort( cmp = cmp_tags )
00209 listtags_express.reverse()
00210 listtags_prompt.sort( cmp = cmp_tags )
00211 listtags_prompt.reverse()
00212
00213 result = {}
00214 result['offline'] = listtags_offline
00215 result['express'] = listtags_express
00216 result['prompt'] = listtags_prompt
00217
00218 return result
00219
00220
00221 class IOV:
00222 def __init__(self):
00223 self.type = ''
00224 self.IOVfirst = ''
00225 self.IOVlast = ''
00226
00227
00228 def get_lastIOVs( listoftags, dest, auth ):
00229
00230 dbtags = ['offline','express','prompt']
00231
00232 results = {}
00233 for itag in dbtags:
00234
00235 lasttag = listoftags[itag][0]
00236
00237 if itag != "offline":
00238 lasttag = listoftags[itag][1]
00239
00240 queryIOVs_cmd = "cmscond_list_iov -c "+dest+" -P "+auth+" -t "+ lasttag
00241 print queryIOVs_cmd
00242
00243 outcmd = commands.getstatusoutput( queryIOVs_cmd )
00244
00245 tmparr = outcmd[1].split('\n')
00246
00247 TimeType = tmparr[1].split()[1]
00248 listIOVs = []
00249
00250
00251 lastline = tmparr[len(tmparr)-1].split()
00252 npayloads = int( lastline[len(lastline)-1] )
00253
00254 maxIOVs = 3
00255 if npayloads < 3:
00256 maxIOVs = npayloads
00257
00258 for i in range(0,maxIOVs):
00259 tmpline = tmparr[len(tmparr) -2 -i]
00260 aIOV = IOV()
00261 aIOV.IOVfirst = tmpline.split()[0]
00262 aIOV.IOVlast = tmpline.split()[1]
00263 aIOV.type = TimeType
00264 listIOVs.append( aIOV )
00265
00266 results[lasttag] = listIOVs
00267
00268 return results
00269
00270
00271
00272 def pack(high,low):
00273 """pack high,low 32bit unsigned int to one unsigned 64bit long long
00274 Note:the print value of result number may appear signed, if the sign bit is used.
00275 """
00276 h=high<<32
00277 return (h|low)
00278
00279 def unpack(i):
00280 """unpack 64bit unsigned long long into 2 32bit unsigned int, return tuple (high,low)
00281 """
00282 high=i>>32
00283 low=i&0xFFFFFFFF
00284 return(high,low)
00285
00286 def unpackLumiid(i):
00287 """unpack 64bit lumiid to dictionary {'run','lumisection'}
00288 """
00289 j=unpack(i)
00290 return {'run':j[0],'lumisection':j[1]}
00291
00292
00293 def get_plots(path,output, iovs, tag):
00294
00295 initial=iovs[len(iovs)-1].IOVfirst
00296 final =iovs[0].IOVfirst
00297 if iovs[0].type == "lumiid":
00298 initial = str(unpack(initial)[0])+":"+str(unpack(initial)[1])
00299 final = str(unpack(final)[0])+":"+str(unpack(final)[1])
00300
00301 initial = str(int(initial) -100 )
00302 cmd = path+"/plotBeamSpotDB.py -b -P -t "+tag+" -i "+initial +" -f "+final
00303 print cmd
00304 outcmd = commands.getstatusoutput( cmd )
00305 print outcmd[1]
00306
00307 cmd = "ls *.png"
00308 outcmd = commands.getstatusoutput( cmd )
00309
00310 pngfiles = outcmd[1].split('\n')
00311 print pngfiles
00312
00313 cmd = "cp *.png "+os.path.dirname(output)
00314 outcmd = commands.getstatusoutput( cmd )
00315 cmd = "rm *.png"
00316 outcmd = commands.getstatusoutput( cmd )
00317
00318 pngfiles.sort()
00319 return pngfiles
00320
00321
00322 def write_plots(lines, plots,web):
00323
00324 end = '\n'
00325 br = '<BR>'+end
00326
00327 lines.append(br)
00328 lines.append('''
00329 <table border="1">
00330
00331 ''')
00332 for i in range(0,len(plots)):
00333 plot = plots[i]
00334 plot = os.path.basename(plot)
00335 if i%2 == 0:
00336 lines.append("<tr>"+end)
00337 lines.append("<td> <a href=\""+web+"/"+plot+"\"> <img src="+plot+" alt="+plot+" width='700' height='250' /> </a> </td>"+end)
00338 if i%2 == 1:
00339 lines.append("</tr>"+end)
00340
00341 lines.append('</table>'+end)
00342
00343
00344 def get_productionFiles( directory ):
00345
00346 list = commands.getstatusoutput('ls -t '+directory)
00347 list = list[1].split()
00348 newlist = []
00349 for i in list:
00350 if i.find('BeamFit_')!=-1:
00351 newlist.append(i)
00352
00353 return newlist
00354
00355 def get_productionIOVs( directory ):
00356
00357 files = get_productionFiles( directory )
00358 listofruns = []
00359 for f in files:
00360 ii = f.find('Run')
00361 arun = f[ii:len(f)-4]
00362 listofruns.append(arun)
00363 return listofruns
00364
00365
00366 if __name__ == '__main__':
00367
00368
00369
00370
00371 option,args = parse(__doc__)
00372 if not args and not option: exit()
00373
00374
00375 htmlwebsite = "https://yumiceva.web.cern.ch/yumiceva/beamspot/"
00376 if option.web: htmlwebsite = option.web
00377
00378
00379
00380 dest = "oracle://cms_orcoff_prod/CMS_COND_31X_BEAMSPOT"
00381 auth = "/afs/cern.ch/cms/DB/conddb"
00382
00383
00384 list_tags = get_listoftags( dest, auth)
00385
00386
00387 list_lastIOVs = get_lastIOVs( list_tags, dest, auth)
00388
00389
00390 processedruns = get_productionIOVs('/afs/cern.ch/cms/CAF/CMSCOMM/COMM_BSPOT/yumiceva/tmp_lumi_workflow/')
00391
00392
00393 lines = []
00394 end = '\n'
00395 br = '<BR>'+end
00396
00397 dump_header(lines)
00398
00399 lines.append('<h2>The three latest IOVs</h2>'+end)
00400 lines.append(br)
00401 lines.append('''
00402 <table border="1">
00403 ''')
00404 write_iovs( list_lastIOVs, lines )
00405 lines.append('</table>'+end)
00406
00407 lines.append('<h2>Latest data processed</h2>'+end)
00408 lines.append(br)
00409
00410 lines.append(processedruns[0]+', '+processedruns[1]+', '+processedruns[2])
00411 print processedruns
00412 lines.append(br)
00413
00414 lines.append('<h2>The Latest Tags</h2>'+end)
00415 lines.append(br)
00416 lines.append('''
00417 <table border="1">
00418 ''')
00419 write_tags( list_tags, lines)
00420 lines.append('</table>'+end)
00421
00422 lasttag = list_lastIOVs.keys()[0]
00423 lines.append('<h2>Plots of the latest IOVs from condDB tag: '+lasttag+' </h2>'+end)
00424 lines.append(br)
00425
00426 pngfiles = get_plots(option.path,option.output, list_lastIOVs[lasttag], lasttag)
00427 write_plots( lines, pngfiles, htmlwebsite )
00428
00429 dump_footer(lines)
00430
00431 outfile = open(option.output,'w')
00432
00433 outfile.writelines( lines )
00434
00435
00436
00437
00438
00439
00440
00441