00001
00002
00003 import os,os.path,sys,math,array,datetime,time,re
00004 import coral
00005
00006 from RecoLuminosity.LumiDB import argparse,lumiTime,CommonUtil,lumiCalcAPI,lumiCorrections,sessionManager,lumiParameters
00007 MINFILL=1800
00008 MAXFILL=9999
00009 allfillname='allfills.txt'
00010 runtofilldqmfile='runtofill_dqm.txt'
00011
00012 def listfilldir(indir):
00013 fillnamepat=r'^[0-9]{4}$'
00014 p=re.compile(fillnamepat)
00015 processedfills=[]
00016 dirList=os.listdir(indir)
00017 for fname in dirList:
00018 if p.match(fname) and os.path.isdir(os.path.join(indir,fname)):
00019 allfs=os.listdir(os.path.join(indir,fname))
00020 for myfile in allfs:
00021 sumfilenamepat=r'^[0-9]{4}_bxsum_CMS.txt$'
00022 s=re.compile(sumfilenamepat)
00023 if s.match(myfile):
00024
00025 processedfills.append(int(fname))
00026 return processedfills
00027
00028 def lastcompleteFill(infile):
00029 lastfill=None
00030 hlinepat=r'(LASTCOMPLETEFILL )([0-9]{4})'
00031 h=re.compile(hlinepat)
00032 dqmfile=open(infile,'r')
00033 for line in dqmfile:
00034 result=h.match(line)
00035 if result:
00036 lastfill=result.group(2)
00037 break
00038 return int(lastfill)
00039
00040 if __name__ == '__main__':
00041 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Dump Fill",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
00042
00043 parser.add_argument('-c',dest='connect',action='store',required=False,help='connect string to lumiDB,optional',default='oracle://cms_orcoff_prod/cms_lumi_prod')
00044 parser.add_argument('-P',dest='authpath',action='store',required=True,help='authentication.xml dir')
00045 parser.add_argument('-i',dest='inputdir',action='store',required=False,help='input dir to runtofill_dqm.txt',default='.')
00046 parser.add_argument('-o',dest='outputdir',action='store',required=False,help='output dir',default='.')
00047 parser.add_argument('-f','--fill',dest='fillnum',action='store',required=False,help='specific fill',default=None)
00048 parser.add_argument('--norm',dest='norm',action='store',required=False,help='norm',default='pp7TeV')
00049 parser.add_argument('--minfill',dest='minfill',action='store',required=False,help='minimal fillnumber ',default=None)
00050 parser.add_argument('--maxfill',dest='maxfill',action='store',required=False,help='maximum fillnumber ',default=MAXFILL)
00051 parser.add_argument('--amodetag',dest='amodetag',action='store',required=False,help='accelerator mode tag ',default='PROTPHYS')
00052 parser.add_argument('--debug',dest='debug',action='store_true',help='debug')
00053 parser.add_argument('--without-stablebeam',dest='withoutStablebeam',action='store_true',required=False,help='without requirement on stable beams')
00054 parser.add_argument('--without-correction',dest='withoutFineCorrection',action='store_true',required=False,help='without fine correction')
00055 options=parser.parse_args()
00056 if options.minfill:
00057 MINFILL=int(options.minfill)
00058 fillstoprocess=[]
00059 maxfillnum=options.maxfill
00060 summaryfilenameTMP='_summary_CMS.txt'
00061 dbname=options.connect
00062 authdir=options.authpath
00063 if options.fillnum is not None:
00064 fillstoprocess.append(int(options.fillnum))
00065 else:
00066 svc=sessionManager.sessionManager(options.connect,authpath=options.authpath,debugON=options.debug)
00067 session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00068 session.transaction().start(True)
00069 schema=session.nominalSchema()
00070 allfillsFromDB=lumiCalcAPI.fillInRange(schema,fillmin=MINFILL,fillmax=maxfillnum,amodetag=options.amodetag)
00071 session.transaction().commit()
00072 processedfills=listfilldir(options.outputdir)
00073 lastcompletedFill=lastcompleteFill(os.path.join(options.inputdir,'runtofill_dqm.txt'))
00074 for pf in processedfills:
00075 if pf>lastcompletedFill:
00076 print '\tremove unfinished fill from processed list ',pf
00077 processedfills.remove(pf)
00078 for fill in allfillsFromDB:
00079 if fill not in processedfills :
00080 if int(fill)<=lastcompletedFill:
00081 if int(fill)>MINFILL:
00082 fillstoprocess.append(fill)
00083 else:
00084 print 'ongoing fill...',fill
00085 print 'fills to process : ',fillstoprocess
00086 if len(fillstoprocess)==0:
00087 print 'no fill to process, exit '
00088 exit(0)
00089 lumip=lumiParameters.ParametersObject()
00090 lslength=lumip.lslengthsec()
00091 import commands,os,RecoLuminosity.LumiDB.lumiTime,datetime,time
00092 for fillnum in fillstoprocess:
00093 clineElements=['lumiCalc2.py','lumibyls','-c',dbname,'-P',authdir,'-f',str(fillnum),'-o','tmp.out']
00094 if not options.withoutStablebeam:
00095 clineElements.append('-b stable')
00096 if options.withoutFineCorrection:
00097 clineElements.append('--without-correction')
00098 clineElements.append('--norm '+options.norm)
00099 finalcmmd=' '.join(clineElements)
00100 print 'cmmd executed:',finalcmmd
00101 (exestat,resultStr)=commands.getstatusoutput(finalcmmd)
00102 if exestat!=0:
00103 print 'lumiCalc2.py execution error ',resultStr
00104 exit(exestat)
00105 f=open('tmp.out','r')
00106 lcount=0
00107 lines=f.readlines()
00108 stablefillmap={}
00109 for line in lines:
00110 lcount=lcount+1
00111 if lcount==1:
00112 continue
00113
00114 line=line.strip()
00115 lineList=line.split(',')
00116 runnum=int(lineList[0].split(':')[0])
00117 if not stablefillmap.has_key(runnum):
00118 stablefillmap[runnum]=([],[])
00119 timestamp=lineList[2]
00120 bstatus=lineList[3]
00121 t=lumiTime.lumiTime()
00122 pydate=t.StrToDatetime(timestamp,'%m/%d/%y %H:%M:%S')
00123 os.environ['TZ']='UTC'
00124 time.tzset()
00125 unixts=int(time.mktime(pydate.timetuple()))
00126 deliveredintl=float(lineList[5])
00127 if bstatus=='STABLE BEAMS':
00128 stablefillmap[runnum][0].append(unixts)
00129 stablefillmap[runnum][1].append(deliveredintl)
00130 filloutdir=os.path.join(options.outputdir,str(fillnum))
00131 if not os.path.exists(filloutdir):
00132 os.mkdir(filloutdir)
00133
00134
00135
00136 summaryfilename=os.path.join(options.outputdir,str(fillnum),str(fillnum)+summaryfilenameTMP)
00137
00138 ofile=open(summaryfilename,'w')
00139 if len(stablefillmap)==0:
00140 print >>ofile,'%s'%('#no stable beams')
00141 else:
00142 for r in sorted(stablefillmap):
00143 rundata=stablefillmap[r]
00144 print >>ofile,'%d\t%d\t%.6e\t%.6e'%(min(rundata[0]),max(rundata[0]), max(rundata[1])/lslength,sum(rundata[1]))
00145 ofile.close()
00146 os.remove('tmp.out')
00147 f.close()
00148