00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 import os,os.path,sys,math,array,datetime,time,re
00011
00012 from RecoLuminosity.LumiDB import argparse,lumiTime,lumiCalcAPI,sessionManager,lumiParameters
00013 MINFILL=1800
00014 MAXFILL=9999
00015 allfillname='allfills.txt'
00016 runtofilldqmfile='runtofill_dqm.txt'
00017
00018 def listfilldir(indir):
00019 fillnamepat=r'^[0-9]{4}$'
00020 p=re.compile(fillnamepat)
00021 processedfills=[]
00022 dirList=os.listdir(indir)
00023 for fname in dirList:
00024 if p.match(fname) and os.path.isdir(os.path.join(indir,fname)):
00025 allfs=os.listdir(os.path.join(indir,fname))
00026 for myfile in allfs:
00027 sumfilenamepat=r'^[0-9]{4}_bxsum_CMS.txt$'
00028 s=re.compile(sumfilenamepat)
00029 if s.match(myfile):
00030
00031 processedfills.append(int(fname))
00032 return processedfills
00033
00034 def lastcompleteFill(infile):
00035 lastfill=None
00036 hlinepat=r'(LASTCOMPLETEFILL )([0-9]{4})'
00037 h=re.compile(hlinepat)
00038 dqmfile=open(infile,'r')
00039 for line in dqmfile:
00040 result=h.match(line)
00041 if result:
00042 lastfill=result.group(2)
00043 break
00044 return int(lastfill)
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 if __name__ == '__main__':
00055 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Dump Fill",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
00056
00057 parser.add_argument('-c',
00058 dest='connect',
00059 action='store',
00060 required=False,
00061 help='connect string to lumiDB,optional',
00062 default='oracle://cms_orcon_adg/cms_lumi_prod')
00063 parser.add_argument('-P',
00064 dest='authpath',
00065 action='store',
00066 required=True,
00067 help='authentication.xml dir')
00068 parser.add_argument('-i',
00069 dest='inputdir',
00070 action='store',
00071 required=False,
00072 help='input dir to runtofill_dqm.txt',
00073 default='.')
00074 parser.add_argument('-o',
00075 dest='outputdir',
00076 action='store',
00077 required=False,
00078 help='output dir',
00079 default='.')
00080 parser.add_argument('-f','--fill',
00081 dest='fillnum',
00082 action='store',
00083 required=False,
00084 help='specific fill',
00085 default=None)
00086 parser.add_argument('--datatag',
00087 dest='datatag',
00088 action='store',
00089 required=False,
00090 help='datatag',
00091 default=None)
00092 parser.add_argument('--normtag',
00093 dest='normtag',
00094 action='store',
00095 required=False,
00096 help='normtag',
00097 default=None)
00098 parser.add_argument('--minfill',
00099 dest='minfill',
00100 action='store',
00101 required=False,
00102 help='minimal fillnumber',
00103 default=None)
00104 parser.add_argument('--maxfill',
00105 dest='maxfill',
00106 action='store',
00107 required=False,
00108 help='maximum fillnumber ',
00109 default=MAXFILL)
00110 parser.add_argument('--amodetag',
00111 dest='amodetag',
00112 action='store',
00113 required=False,
00114 help='specific accelerator mode choices [PROTOPHYS,IONPHYS,PAPHYS] (optional)')
00115 parser.add_argument('--beamenergy',
00116 dest='beamenergy',
00117 action='store',
00118 type=float,
00119 default=None,
00120 help='nominal beam energy in GeV')
00121 parser.add_argument('--beamfluctuation',
00122 dest='beamfluctuation',
00123 type=float,action='store',
00124 default=0.2,
00125 required=False,
00126 help='fluctuation in fraction allowed to nominal beam energy, default 0.2, to be used together with -beamenergy (optional)')
00127 parser.add_argument('--debug',
00128 dest='debug',
00129 action='store_true',
00130 help='debug')
00131 parser.add_argument('--without-stablebeam',
00132 dest='withoutStablebeam',
00133 action='store_true',
00134 required=False,
00135 help='without requirement on stable beams')
00136 parser.add_argument('--without-correction',
00137 dest='withoutFineCorrection',
00138 action='store_true',
00139 required=False,
00140 help='without fine correction')
00141 options=parser.parse_args()
00142 if options.minfill:
00143 MINFILL=int(options.minfill)
00144 fillstoprocess=[]
00145 maxfillnum=options.maxfill
00146 summaryfilenameTMP='_summary_CMS.txt'
00147 dbname=options.connect
00148 authdir=options.authpath
00149 if options.fillnum is not None:
00150 fillstoprocess.append(int(options.fillnum))
00151 else:
00152 svc=sessionManager.sessionManager(options.connect,authpath=options.authpath,debugON=options.debug)
00153 session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00154 session.transaction().start(True)
00155 schema=session.nominalSchema()
00156 allfillsFromDB=lumiCalcAPI.fillInRange(schema,fillmin=MINFILL,fillmax=maxfillnum,amodetag=options.amodetag)
00157 session.transaction().commit()
00158 processedfills=listfilldir(options.outputdir)
00159 lastcompletedFill=lastcompleteFill(os.path.join(options.inputdir,'runtofill_dqm.txt'))
00160 for pf in processedfills:
00161 if pf>lastcompletedFill:
00162 print '\tremove unfinished fill from processed list ',pf
00163 processedfills.remove(pf)
00164 for fill in allfillsFromDB:
00165 if fill not in processedfills :
00166 if int(fill)<=lastcompletedFill:
00167 if int(fill)>MINFILL:
00168 fillstoprocess.append(fill)
00169 else:
00170 print 'ongoing fill...',fill
00171 print 'fills to process : ',fillstoprocess
00172 if len(fillstoprocess)==0:
00173 print 'no fill to process, exit '
00174 exit(0)
00175 lumip=lumiParameters.ParametersObject()
00176 lslength=lumip.lslengthsec()
00177 import commands,os,RecoLuminosity.LumiDB.lumiTime,datetime,time
00178 for fillnum in fillstoprocess:
00179 clineElements=['lumiCalc2.py','lumibyls','-c',dbname,'-P',authdir,'-f',str(fillnum),'-o','tmp.out','--without-checkforupdate','--nowarning']
00180 if not options.withoutStablebeam:
00181 clineElements.append('-b stable')
00182 if options.withoutFineCorrection:
00183 clineElements.append('--without-correction')
00184 if options.datatag:
00185 clineElements.append('--datatag '+options.datatag)
00186 if options.normtag:
00187 clineElements.append('--normtag '+options.normtag)
00188 if options.beamenergy:
00189 clineElements.append('--beamenergy '+str(options.beamenergy))
00190 if options.beamfluctuation:
00191 clineElements.append('--beamfluctuation '+str(options.beamfluctuation))
00192
00193 finalcmmd=' '.join(clineElements)
00194 print 'cmmd executed:',finalcmmd
00195 (exestat,resultStr)=commands.getstatusoutput(finalcmmd)
00196 if exestat!=0:
00197 print 'lumiCalc2.py execution error ',resultStr
00198 exit(exestat)
00199 f=open('tmp.out','r')
00200 lcount=0
00201 lines=f.readlines()
00202 stablefillmap={}
00203 for line in lines:
00204 lcount=lcount+1
00205 if lcount==1:
00206 continue
00207
00208 line=line.strip()
00209 lineList=line.split(',')
00210 runnum=int(lineList[0].split(':')[0])
00211 if not stablefillmap.has_key(runnum):
00212 stablefillmap[runnum]=([],[])
00213 timestamp=lineList[2]
00214 bstatus=lineList[3]
00215 t=lumiTime.lumiTime()
00216 pydate=t.StrToDatetime(timestamp,'%m/%d/%y %H:%M:%S')
00217
00218 os.environ['TZ']='UTC'
00219 time.tzset()
00220 unixts=int(time.mktime(pydate.timetuple()))
00221 deliveredintl=float(lineList[5])
00222 if bstatus=='STABLE BEAMS':
00223 stablefillmap[runnum][0].append(unixts)
00224 stablefillmap[runnum][1].append(deliveredintl)
00225 filloutdir=os.path.join(options.outputdir,str(fillnum))
00226 if not os.path.exists(filloutdir):
00227 os.mkdir(filloutdir)
00228
00229
00230
00231 summaryfilename=os.path.join(options.outputdir,str(fillnum),str(fillnum)+summaryfilenameTMP)
00232
00233 ofile=open(summaryfilename,'w')
00234 if len(stablefillmap)==0:
00235 print >>ofile,'%s'%('#no stable beams')
00236 else:
00237 for r in sorted(stablefillmap):
00238 rundata=stablefillmap[r]
00239 print >>ofile,'%d\t%d\t%.6e\t%.6e'%(min(rundata[0]),max(rundata[0]), max(rundata[1])/lslength,sum(rundata[1]))
00240 ofile.close()
00241 os.remove('tmp.out')
00242 f.close()
00243