def updatedb(schema,runmap,lumitype): ''' update lumidata set starttime=:rstart,stoptime=:rstop,nls=:nls where runnum=:runnum and data_id=:lumidataid ''' lumidatatableName='' if lumitype=='HF': lumitableName=nameDealer.lumidataTableName() elif lumitype == 'PIXEL': lumitableName = nameDealer.pixellumidataTableName() else: assert False, "ERROR Unknown lumitype '%s'" % lumitype t=lumiTime.lumiTime() setClause='STARTTIME=:runstarttime,STOPTIME=:runstoptime,NLS=:nls' updateCondition='RUNNUM=:runnum AND DATA_ID=:lumidataid' inputData=coral.AttributeList() inputData.extend('runstarttime','time stamp') inputData.extend('runstoptime','time stamp') inputData.extend('nls','unsigned int') inputData.extend('runnum','unsigned int') inputData.extend('lumidataid','unsigned long long') db=dbUtil.dbUtil(schema) for (run,lumidataid) in runmap.keys(): [runstarttime,runstoptime,nls]=runmap[(run,lumidataid)] runstartT=coral.TimeStamp(runstarttime.year,runstarttime.month,runstarttime.day,runstarttime.hour,runstarttime.minute,runstarttime.second,runstarttime.microsecond*1000) runstopT=coral.TimeStamp(runstoptime.year,runstoptime.month,runstoptime.day,runstoptime.hour,runstoptime.minute,runstoptime.second,runstoptime.microsecond*1000) inputData['runstarttime'].setData(runstartT) inputData['runstoptime'].setData(runstopT) inputData['nls'].setData(nls) inputData['runnum'].setData(int(run)) inputData['lumidataid'].setData(int(lumidataid)) db.singleUpdate(lumitableName,setClause,updateCondition,inputData)
def addindb(session, datatag, normtag, lumidata, bulksize): ''' input : [fill,run,lumils,cmsls,lstime,beamstatus,beamenergy,delivered,recorded,avgpu] ''' hfresultDefDict = [('RUNNUM', 'unsigned int'), ('LS', 'unsigned int'), ('CMSLS', 'unsigned int'), ('FILLNUM', 'unsigned int'), ('TIME', 'time stamp'), ('BEAM_STATUS', 'string'), ('ENERGY', 'unsigned int'), ('DELIVERED', 'float'), ('RECORDED', 'float'), ('AVG_PU', 'float'), ('DATA_VERSION', 'string'), ('NORM_VERSION', 'string'), ('INSERT_TIME', 'time stamp')] committedrows = 0 nrows = 0 bulkvalues = [] lute = lumiTime.lumiTime() try: for datum in lumidata: [ fillnum, runnum, lumils, cmsls, lstime_char, beamstatus, beamenergy, delivered, recorded, avgpu ] = datum inserttime = coral.TimeStamp() lstime = lute.StrToDatetime(lstime_char, customfm='%m/%d/%y %H:%M:%S') corallstime = coral.TimeStamp(lstime.year, lstime.month, lstime.day, lstime.hour, lstime.minute, lstime.second, 0) bulkvalues.append([('RUNNUM', runnum), ('LS', lumils), ('CMSLS', cmsls), ('FILLNUM', fillnum), ('TIME', corallstime), ('BEAM_STATUS', beamstatus), ('ENERGY', beamenergy), ('DELIVERED', delivered), ('RECORDED', recorded), ('AVG_PU', avgpu), ('DATA_VERSION', datatag), ('NORM_VERSION', normtag), ('INSERT_TIME', inserttime)]) nrows += 1 committedrows += 1 if nrows == bulksize: print 'committing trg in LS chunck ', nrows db = dbUtil.dbUtil(session.nominalSchema()) session.transaction().start(False) db.bulkInsert('HFLUMIRESULT', hfresultDefDict, bulkvalues) session.transaction().commit() nrows = 0 bulkvalues = [] elif committedrows == len(lumidata): print 'committing at the end ' db = dbUtil.dbUtil(session.nominalSchema()) session.transaction().start(False) db.bulkInsert('HFLUMIRESULT', hfresultDefDict, bulkvalues) session.transaction().commit() except: print 'error in addindb' raise
def generateLumiRundata(filename,runsummaryData,runlist): ''' input: runsummaryData {runnum: (datasource(0),nominalegev(1),ncollidingbunches(2),starttime(3),stoptime(4)} output: {runnum: [nominalenergy,ncollidingbunches,starttime,stoptime,nls] } ''' result={} t=lumiTime.lumiTime() for run in runlist: summary=runsummaryData[run] start=datetime.strptime(summary[3],'%m/%d/%y %H:%M:%S') stop=datetime.strptime(summary[4],'%m/%d/%y %H:%M:%S') starttime=coral.TimeStamp(start.year,start.month,start.day,start.hour,start.minute,start.second,0) stoptime=coral.TimeStamp(stop.year,stop.month,stop.day,stop.hour,stop.minute,stop.second,0) result[run]=[filename,summary[1],summary[2],starttime,stoptime,0] return result
def main(*args): runnum = 0 try: runnum = args[1] report = csvReporter.csvReporter('instlumibytime-' + str(runnum) + '.csv') msg = coral.MessageStream('') msg.setMsgVerbosity(coral.message_Level_Error) os.environ['CORAL_AUTH_PATH'] = '/afs/cern.ch/cms/lumi' svc = coral.ConnectionService() connectstr = 'oracle://cms_orcoff_prod/cms_lumi_prod' session = svc.connect(connectstr, accessMode=coral.access_ReadOnly) session.typeConverter().setCppTypeForSqlType("unsigned int", "NUMBER(10)") session.typeConverter().setCppTypeForSqlType("unsigned long long", "NUMBER(20)") session.transaction().start(True) schema = session.nominalSchema() q = schema.newQuery() runsummaryOut = lumiQueryAPI.runsummaryByrun(q, runnum) del q q = schema.newQuery() lumisummaryOut = lumiQueryAPI.lumisummaryByrun(q, runnum, '0001') del q session.transaction().commit() del session del svc #print runsummaryOut starttimestr = runsummaryOut[3] t = lumiTime.lumiTime() report.writeRow(['cmslsnum', 'utctime', 'unixtimestamp', 'instlumi']) for dataperls in lumisummaryOut: cmslsnum = dataperls[0] instlumi = dataperls[1] startorbit = dataperls[3] orbittime = t.OrbitToTime(starttimestr, startorbit) orbittimestamp = time.mktime( orbittime.timetuple()) + orbittime.microsecond / 1e6 report.writeRow([cmslsnum, orbittime, orbittimestamp, instlumi]) except IndexError: print 'runnumber should be provided' return 1 except Exception as er: print str(er) return 2 else: return 0
def main(*args): runnum=0 try: runnum=args[1] report=csvReporter.csvReporter('instlumibytime-'+str(runnum)+'.csv') msg=coral.MessageStream('') msg.setMsgVerbosity(coral.message_Level_Error) os.environ['CORAL_AUTH_PATH']='/afs/cern.ch/cms/lumi' svc = coral.ConnectionService() connectstr='oracle://cms_orcoff_prod/cms_lumi_prod' session=svc.connect(connectstr,accessMode=coral.access_ReadOnly) session.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)") session.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)") session.transaction().start(True) schema=session.nominalSchema() q=schema.newQuery() runsummaryOut=lumiQueryAPI.runsummaryByrun(q,runnum) del q q=schema.newQuery() lumisummaryOut=lumiQueryAPI.lumisummaryByrun(q,runnum,'0001') del q session.transaction().commit() del session del svc #print runsummaryOut starttimestr=runsummaryOut[3] t=lumiTime.lumiTime() report.writeRow(['cmslsnum','utctime','unixtimestamp','instlumi']) for dataperls in lumisummaryOut: cmslsnum=dataperls[0] instlumi=dataperls[1] startorbit=dataperls[3] orbittime=t.OrbitToTime(starttimestr,startorbit) orbittimestamp=time.mktime(orbittime.timetuple())+orbittime.microsecond/1e6 report.writeRow([cmslsnum,orbittime,orbittimestamp,instlumi]) except IndexError: print('runnumber should be provided') return 1 except Exception as er: print(str(er)) return 2 else: return 0
def totalLumivstimeLastweek(c,p='.',i='',o='.',selectionfile=None,beamstatus=None,beamenergy=None,beamfluctuation=None,dryrun=False,withTextOutput=False,withFineCorrection=False): ''' input: c connect string p authenticaion path i input selection file name o output path ##fix me: year boundary is not considered! ''' t=lumiTime.lumiTime() plotoutname='totallumivstime-weekly.png' textoutname='totallumivstime-weekly.csv' nowTime=datetime.datetime.now() lastMondayStr=' '.join([str(nowTime.isocalendar()[0]),str(nowTime.isocalendar()[1]-1),str(nowTime.isocalendar()[2])]) lastweekMonday=datetime.datetime(*time.strptime(lastMondayStr,'%Y %W %w')[0:5]) lastweekEndSunday=lastweekMonday+datetime.timedelta(days=7,hours=24) elements=['lumiSumPlot.py','-c',c,'-P',p,'-begin','"'+t.DatetimeToStr(lastweekMonday)+'"','-end','"'+t.DatetimeToStr(lastweekEndSunday)+'"','-batch',os.path.join(o,plotoutname),'time'] if withTextOutput: elements.append('-o') elements.append(os.path.join(o,textoutname)) if beamstatus: elements.append('-beamstatus') if beamstatus=='stable': elements.append('"STABLE BEAMS"') if beamenergy: elements.append('-beamenergy') elements.append(str(beamenergy)) if beamfluctuation: elements.append('-beamfluctuation') elements.append(str(beamfluctuation)) if withFineCorrection: elements.append('--with-correction') command=' '.join(elements) print command if not dryrun: statusAndOutput=commands.getstatusoutput(command) print statusAndOutput[1]
def addindb(session,datatag,normtag,lumidata,bulksize): ''' input : [fill,run,lumils,cmsls,lstime,beamstatus,beamenergy,delivered,recorded,avgpu] ''' hfresultDefDict=[('RUNNUM','unsigned int'),('LS','unsigned int'),('CMSLS','unsigned int'),('FILLNUM','unsigned int'),('TIME','time stamp'),('BEAM_STATUS','string'),('ENERGY','unsigned int'),('DELIVERED','float'),('RECORDED','float'),('AVG_PU','float'),('DATA_VERSION','string'),('NORM_VERSION','string'),('INSERT_TIME','time stamp')] committedrows=0 nrows=0 bulkvalues=[] lute=lumiTime.lumiTime() try: for datum in lumidata: [fillnum,runnum,lumils,cmsls,lstime_char,beamstatus,beamenergy,delivered,recorded,avgpu]=datum inserttime=coral.TimeStamp() lstime=lute.StrToDatetime(lstime_char,customfm='%m/%d/%y %H:%M:%S') corallstime=coral.TimeStamp(lstime.year,lstime.month,lstime.day,lstime.hour,lstime.minute,lstime.second,0) bulkvalues.append([('RUNNUM',runnum),('LS',lumils),('CMSLS',cmsls),('FILLNUM',fillnum),('TIME',corallstime),('BEAM_STATUS',beamstatus),('ENERGY',beamenergy),('DELIVERED',delivered),('RECORDED',recorded),('AVG_PU',avgpu),('DATA_VERSION',datatag),('NORM_VERSION',normtag),('INSERT_TIME',inserttime)]) nrows+=1 committedrows+=1 if nrows==bulksize: print 'committing trg in LS chunck ',nrows db=dbUtil.dbUtil(session.nominalSchema()) session.transaction().start(False) db.bulkInsert('HFLUMIRESULT',hfresultDefDict,bulkvalues) session.transaction().commit() nrows=0 bulkvalues=[] elif committedrows==len(lumidata): print 'committing at the end ' db=dbUtil.dbUtil(session.nominalSchema()) session.transaction().start(False) db.bulkInsert('HFLUMIRESULT',hfresultDefDict,bulkvalues) session.transaction().commit() except : print 'error in addindb' raise
def updatedb(schema, runmap, lumitype): ''' update lumidata set starttime=:rstart,stoptime=:rstop,nls=:nls where runnum=:runnum and data_id=:lumidataid ''' lumidatatableName = '' if lumitype == 'HF': lumitableName = nameDealer.lumidataTableName() elif lumitype == 'PIXEL': lumitableName = nameDealer.pixellumidataTableName() else: assert False, "ERROR Unknown lumitype '%s'" % lumitype t = lumiTime.lumiTime() setClause = 'STARTTIME=:runstarttime,STOPTIME=:runstoptime,NLS=:nls' updateCondition = 'RUNNUM=:runnum AND DATA_ID=:lumidataid' inputData = coral.AttributeList() inputData.extend('runstarttime', 'time stamp') inputData.extend('runstoptime', 'time stamp') inputData.extend('nls', 'unsigned int') inputData.extend('runnum', 'unsigned int') inputData.extend('lumidataid', 'unsigned long long') db = dbUtil.dbUtil(schema) for (run, lumidataid) in runmap.keys(): [runstarttime, runstoptime, nls] = runmap[(run, lumidataid)] runstartT = coral.TimeStamp(runstarttime.year, runstarttime.month, runstarttime.day, runstarttime.hour, runstarttime.minute, runstarttime.second, runstarttime.microsecond * 1000) runstopT = coral.TimeStamp(runstoptime.year, runstoptime.month, runstoptime.day, runstoptime.hour, runstoptime.minute, runstoptime.second, runstoptime.microsecond * 1000) inputData['runstarttime'].setData(runstartT) inputData['runstoptime'].setData(runstopT) inputData['nls'].setData(nls) inputData['runnum'].setData(int(run)) inputData['lumidataid'].setData(int(lumidataid)) db.singleUpdate(lumitableName, setClause, updateCondition, inputData)
def plotPeakPerday_Time(self, rawdata={}, resultlines=[], minTime=None, maxTime=None, nticks=6, withannotation=False, yscale='linear', referenceLabel='Delivered', labels=['Delivered'], textoutput=None): ''' THIS PLOT IS DELIVERED ONLY Input: rawdata={'Delivered':[(day,run,ls,instlumi)]} resultlines=[[day,run,ls,maxinstlum],[]] minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S withannotation: wheather the boundary points should be annotated referenceLabel: the one variable that decides the total unit and the plot x-axis range labels: labels of the variables to plot ''' xpoints = [] ypoints = {} legendlist = [] maxinfo = '' ymax = {} lut = lumiTime.lumiTime() if not minTime: minTime = '03/01/10 00:00:00' minTime = lut.StrToDatetime(minTime, customfm='%m/%d/%y %H:%M:%S') if not maxTime: maxTime = datetime.datetime.utcnow() else: maxTime = lut.StrToDatetime(maxTime, customfm='%m/%d/%y %H:%M:%S') for r in resultlines: day = int(r[0]) runnumber = int(r[1]) lsnum = int(r[2].split('.')[0]) if rawdata and day in [int(t[0]) for t in rawdata[referenceLabel]]: continue if day < minTime.date().toordinal(): continue if day > maxTime.date().toordinal(): continue for i, lab in enumerate(labels): v = float(r[-(len(labels) - i) - 1]) rawdata.setdefault(lab, []).append((day, runnumber, lsnum, v)) if not rawdata: print('[WARNING]: no data, do nothing') return maxlum = max([t[3] for t in rawdata[referenceLabel]]) minlum = min([t[3] for t in rawdata[referenceLabel] if t[3] > 0 ]) #used only for log scale, fin the non-zero bottom (unitstring, denomitor) = guessInstLumiUnit(maxlum) csvreport = None rows = [] flat = [] MinDay = minTime.date().toordinal() MaxDay = maxTime.date().toordinal() fulldays = range(MinDay, MaxDay + 1) for label in rawdata.keys(): yvalues = sorted(rawdata[label]) alldays = [t[0] for t in yvalues] alldates = [str(datetime.date.fromordinal(t)) for t in alldays] ypoints[label] = [] lumivals = [t[3] for t in yvalues] flat.append(lumivals) for d in fulldays: if not d in alldays: ypoints[label].append(0.0) else: thisdaylumi = [t[3] for t in yvalues if t[0] == d][0] if yscale == 'log': if thisdaylumi < minlum: thisdaylumi = minlum / denomitor else: thisdaylumi = thisdaylumi / denomitor else: thisdaylumi = thisdaylumi / denomitor ypoints[label].append(thisdaylumi) ymax[label] = max(lumivals) / denomitor 'ymax ', max(lumivals) xpoints = fulldays if textoutput: csvreport = csvReporter.csvReporter(textoutput) head = ['#day', 'run', 'lsnum', 'maxinstlumi', 'date'] csvreport.writeRow(head) flat.insert(0, alldays) allruns = [t[1] for t in rawdata[referenceLabel]] allls = [t[2] for t in rawdata[referenceLabel]] flat.insert(1, allruns) flat.insert(2, allls) flat.append(alldates) rows = list(zip(*flat)) csvreport.writeRows([list(t) for t in rows]) yearStrMin = minTime.strftime('%Y') yearStrMax = maxTime.strftime('%Y') if yearStrMin == yearStrMax: dateFmt = matplotlib.dates.DateFormatter('%d/%m') else: dateFmt = matplotlib.dates.DateFormatter('%d/%m/%y') ax = self.__fig.add_subplot(111) if yscale == 'linear': ax.set_yscale('linear') elif yscale == 'log': ax.set_yscale('log') else: raise 'unsupported yscale ', yscale majorLoc = matplotlib.ticker.LinearLocator(numticks=nticks) minorLoc = matplotlib.ticker.LinearLocator(numticks=nticks * 4) ax.xaxis.set_major_formatter(dateFmt) ax.set_xlabel(r'Date', position=(0.84, 0)) ax.set_ylabel(r'L ' + unitstring, position=(0, 0.9)) ax.xaxis.set_major_locator(majorLoc) ax.xaxis.set_minor_locator(minorLoc) xticklabels = ax.get_xticklabels() for tx in xticklabels: tx.set_horizontalalignment('right') ax.grid(True) cl = self.colormap['Max Inst'] textsummaryhead = ['#TotalRunningDays'] textsummaryline = ['#' + str(len(alldays))] for ylabel in labels: cl = 'k' if ylabel in self.colormap: cl = self.colormap[ylabel] ax.plot(xpoints, ypoints[ylabel], label='Max Inst', color=cl, drawstyle='steps') legendlist.append('Max Inst %.3f' % (ymax[ylabel]) + ' ' + unitstring) textsummaryhead.append('Max Inst' + ylabel) textsummaryline.append('%.3f' % (ymax[ylabel]) + ' ' + unitstring) if textoutput: csvreport.writeRow(textsummaryhead) csvreport.writeRow(textsummaryline) ax.legend(tuple(legendlist), loc='upper left') ax.set_xbound(lower=matplotlib.dates.date2num(minTime), upper=matplotlib.dates.date2num(maxTime)) if withannotation: #annotations trans = matplotlib.transforms.BlendedGenericTransform( ax.transData, ax.transAxes) ax.text(xpoints[0], 1.025, beginfo, transform=trans, horizontalalignment='left', size='x-small', color='green', bbox=dict(facecolor='white')) ax.text(xpoints[-1], 1.025, endinfo, transform=trans, horizontalalignment='left', size='x-small', color='green', bbox=dict(facecolor='white')) ax.annotate(maxinfo, xy=(xmax, ymax), xycoords='data', xytext=(0, 13), textcoords='offset points', arrowprops=dict(facecolor='green', shrink=0.05), size='x-small', horizontalalignment='center', color='green', bbox=dict(facecolor='white')) firstday = datetime.date.fromordinal(rawdata[referenceLabel][0][0]) lastday = datetime.date.fromordinal(rawdata[referenceLabel][-1][0]) firstdayStr = firstday.strftime('%Y %b %d') lastdayStr = lastday.strftime('%Y %b %d') ax.set_title('CMS Peak Luminosity/Day (' + firstdayStr + ' - ' + lastdayStr + ')', size='small') #ax.autoscale(tight=True) ax.autoscale_view(tight=True, scalex=True, scaley=False) #ax.set_xmargin(0.015) self.__fig.autofmt_xdate(bottom=0.18, rotation=15, ha='right') self.__fig.subplots_adjust(bottom=0.2, left=0.15)
def plotInst_RunLS(self,rawxdata,rawydata,nticks=6,textoutput=None): ''' Input: rawxdata [run,fill,starttime,stoptime,totalls,ncmsls] rawydata {label:[lumi]} ''' lslength=23.357 lut=lumiTime.lumiTime() runnum=rawxdata[0] fill=rawxdata[1] starttime=lut.DatetimeToStr(rawxdata[2],customfm='%m/%d/%y %H:%M:%S') stoptime=lut.DatetimeToStr(rawxdata[3],customfm='%m/%d/%y %H:%M:%S') totalls=rawxdata[-2] ncmsls=rawxdata[-1] peakinst=max(rawydata['Delivered'])/lslength totaldelivered=sum(rawydata['Delivered']) totalrecorded=sum(rawydata['Recorded']) xpoints=range(1,totalls+1) #print len(xpoints) ypoints={} ymax={} for ylabel,yvalue in rawydata.items(): ypoints[ylabel]=[y/lslength for y in yvalue] ymax[ylabel]=max(yvalue)/lslength left=0.15 width=0.7 bottom=0.1 height=0.65 bottom_h=bottom+height rect_scatter=[left,bottom,width,height] rect_table=[left,bottom_h,width,0.25] nullfmt=matplotlib.ticker.NullFormatter() nullloc=matplotlib.ticker.NullLocator() axtab=self.__fig.add_axes(rect_table,frameon=False) axtab.set_axis_off() axtab.xaxis.set_major_formatter(nullfmt) axtab.yaxis.set_major_formatter(nullfmt) axtab.xaxis.set_major_locator(nullloc) axtab.yaxis.set_major_locator(nullloc) ax=self.__fig.add_axes(rect_scatter) majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks) minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4) ax.set_xlabel(r'LS',position=(0.96,0)) ax.set_ylabel(r'L $\mu$b$^{-1}$s$^{-1}$',position=(0,0.9)) ax.xaxis.set_major_locator(majorLoc) ax.xaxis.set_minor_locator(minorLoc) ax.set_xbound(lower=xpoints[0],upper=xpoints[-1]) xticklabels=ax.get_xticklabels() for tx in xticklabels: tx.set_horizontalalignment('right') ax.grid(True) keylist=ypoints.keys() keylist.sort() legendlist=[] for ylabel in keylist: cl='k' if self.colormap.has_key(ylabel): cl=self.colormap[ylabel] ax.plot(xpoints,ypoints[ylabel],'.',label=ylabel,color=cl) legendlist.append(ylabel) #ax.axhline(0,color='green',linewidth=0.2) ax.axvline(xpoints[ncmsls-1],color='green',linewidth=0.2) (unitstring,denomitor)=guessLumiUnit(totaldelivered) colLabels=('run','fill','max inst(/$\mu$b/s)','delivered('+unitstring+')','recorded('+unitstring+')') cellText=[[str(runnum),str(fill),'%.3f'%(peakinst),'%.3f'%(totaldelivered/denomitor),'%.3f'%(totalrecorded/denomitor)]] sumtable=axtab.table(cellText=cellText,colLabels=colLabels,colWidths=[0.12,0.1,0.27,0.27,0.27],cellLoc='center',loc='center') trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes) axtab.add_table(sumtable) ax.text(xpoints[0],1.02,starttime[0:17],transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white')) ax.text(xpoints[ncmsls-1],1.02,stoptime[0:17],transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white')) ax.legend(tuple(legendlist),loc='upper right',numpoints=1)
def generateLumidata(lumirundatafromfile,lsdatafromfile,rundatafromdb,lsdatafromdb,replacelsMin,replacelsMax): ''' input: perrunresultfromfile=[]#source,starttime,stoptime,nls perlsresultfromfile={} #{lumilsnum:instlumiub} lumirundatafromdb=[] #[source,nominalegev,ncollidingbunches,starttime,stoptime,nls] lumilsdatafromdb={}#{lumilsnum:[cmslsnum(0),instlumi(1),instlumierror(2),instlumiquality(3),beamstatus(4),beamenergy(5),numorbit(6),startorbit(7),cmsbxindexblob(8),beamintensityblob_1(9),beamintensityblob_2(10),bxlumivalue_occ1(11),bxlumierror_occ1(12),bxlumiquality_occ1(13),bxlumivalue_occ2(14),bxlumierror_occ2(15),bxlumiquality_occ2(16),bxlumivalue_et(17),bxlumierror_et(18),bxlumiquality_et(19)]} ''' lumip=lumiParameters.ParametersObject() numorbit=lumip.numorbit startorbit=0 fakebeamenergy=4000. fakebeamstatus='STABLE BEAMS' fakefloatArray=array.array('f') fakeidxArray=array.array('h') fakeshortArray=array.array('h') for bxidx in range(1,3565): fakeidxArray.append(bxidx) fakefloatArray.append(0.) fakeshortArray.append(0) lumirundata=[] lumilsdata={} if rundatafromdb: lumirundata=rundatafromdb lumirundata[0]=rundatafromdb[0]+'+file:'+lumirundatafromfile[0] else: lu=lumiTime.lumiTime() source='+file:'+lumirundatafromfile[0] nominalegev=fakebeamenergy ncollidingbunches=72 starttime=lumirundatafromfile[1] stoptime=lumirundatafromfile[2] starttimeT=lu.timestampTodatetimeUTC(starttime) stoptimeT=lu.timestampTodatetimeUTC(stoptime) print(starttimeT.day,starttimeT.month,starttimeT.year) starttimeT_coral=coral.TimeStamp(starttimeT.year,starttimeT.month,starttimeT.day,starttimeT.hour,starttimeT.minute,starttimeT.second,0) stoptimeT_coral=coral.TimeStamp(stoptimeT.year,stoptimeT.month,stoptimeT.day,stoptimeT.hour,stoptimeT.minute,stoptimeT.second,0) nls=lumirundatafromfile[3] lumirundata=[source,nominalegev,ncollidingbunches,starttimeT_coral,stoptimeT_coral,nls] if lsdatafromdb: lumilsdata=lsdatafromdb if replacelsMin>len(lsdatafromdb): print('[INFO]Operation: extend an existing run from LS=',replacelsMin) lumirundata[5]+=len(lsdatafromfile) else: print('[INFO]Operation: replace instlumi in an existing run LS range=',replacelsMin,replacelsMax) else: print('[INFO]Operation: insert a new fake run') for lumilsnum in range(replacelsMin,replacelsMax+1): instlumi=lsdatafromfile[lumilsnum] if lumilsnum in lsdatafromdb.keys(): #if this is a hole lumilsdata[lumilsnum][1]=instlumi else: #if this is an extension instlumierror=0.0 instlumiquality=0 startorbit=(lumilsnum-1)*numorbit cmsbxindexblob=CommonUtil.packArraytoBlob(fakeshortArray) beamintensityblob_1=CommonUtil.packArraytoBlob(fakefloatArray) beamintensityblob_2=CommonUtil.packArraytoBlob(fakefloatArray) bxlumivalue_occ1=CommonUtil.packArraytoBlob(fakefloatArray) bxlumierror_occ1=CommonUtil.packArraytoBlob(fakefloatArray) bxlumiquality_occ1=CommonUtil.packArraytoBlob(fakeshortArray) bxlumivalue_occ2=CommonUtil.packArraytoBlob(fakefloatArray) bxlumierror_occ2=CommonUtil.packArraytoBlob(fakefloatArray) bxlumiquality_occ2=CommonUtil.packArraytoBlob(fakeshortArray) bxlumivalue_et=CommonUtil.packArraytoBlob(fakefloatArray) bxlumierror_et=CommonUtil.packArraytoBlob(fakefloatArray) bxlumiquality_et=CommonUtil.packArraytoBlob(fakeshortArray) lumilsdata[lumilsnum]=[0,instlumi,instlumierror,instlumiquality,fakebeamstatus,fakebeamenergy,numorbit,startorbit,cmsbxindexblob,beamintensityblob_1,beamintensityblob_2,bxlumivalue_occ1,bxlumierror_occ1,bxlumiquality_occ1,bxlumivalue_occ2,bxlumierror_occ2,bxlumiquality_occ2,bxlumivalue_et,bxlumierror_et,bxlumiquality_et] return (lumirundata,lumilsdata)
def getSpecificLumi(schema,fillnum,inputdir,dataidmap,normmap,xingMinLum=0.0,amodetag='PROTPHYS',bxAlgo='OCC1'): ''' specific lumi in 1e-30 (ub-1s-1) unit lumidetail occlumi in 1e-27 1309_lumi_401_CMS.txt time(in seconds since January 1,2011,00:00:00 UTC) stab(fraction of time spent in stable beams for this time bin) l(lumi in Hz/ub) dl(point-to-point error on lumi in Hz/ub) sl(specific lumi in Hz/ub) dsl(error on specific lumi) 20800119.0 1 -0.889948 0.00475996848729 0.249009 0.005583287562 -0.68359 6.24140208607 0.0 0.0 0.0 0.0 0.0 0.0 0.0383576 0.00430892097862 0.0479095 0.00430892097862 66.6447 4.41269758764 0.0 0.0 0.0 result [(time,beamstatusfrac,lumi,lumierror,speclumi,speclumierror)] ''' t=lumiTime.lumiTime() fillbypos={}#{bxidx:[[ts,beamstatusfrac,lumi,lumierror,spec1,specerror],[]]} runtimesInFill=getFillFromDB(schema,fillnum)#{runnum:starttimestr} runlist=runtimesInFill.keys() if not runlist: return fillbypos irunlsdict=dict(list(zip(runlist,[None]*len(runlist)))) #prirunlsdict GrunsummaryData=lumiCalcAPI.runsummaryMap(session.nominalSchema(),irunlsdict) lumidetails=lumiCalcAPI.deliveredLumiForIds(schema,irunlsdict,dataidmap,GrunsummaryData,beamstatusfilter=None,normmap=normmap,withBXInfo=True,bxAlgo=bxAlgo,xingMinLum=xingMinLum,withBeamIntensity=True,lumitype='HF') # #output: {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),calibratedlumierr(6),(bxvalues,bxerrs)(7),(bxidx,b1intensities,b2intensities)(8),fillnum(9)]} # totalstablebeamls=0 orderedrunlist=sorted(lumidetails) for run in orderedrunlist: perrundata=lumidetails[run] for perlsdata in perrundata: beamstatus=perlsdata[3] if beamstatus=='STABLE BEAMS': totalstablebeamls+=1 #print 'totalstablebeamls in fill ',totalstablebeamls if totalstablebeamls<10:#less than 10 LS in a fill has 'stable beam', it's no a good fill print('fill ',fillnum,' , having less than 10 stable beam lS, is not good, skip') return fillbypos lumiparam=lumiParameters.ParametersObject() for run in orderedrunlist: perrundata=lumidetails[run] for perlsdata in perrundata: beamstatusfrac=0.0 tsdatetime=perlsdata[2] ts=calendar.timegm(tsdatetime.utctimetuple()) beamstatus=perlsdata[3] if beamstatus=='STABLE BEAMS': beamstatusfrac=1.0 (bxidxlist,bxvaluelist,bxerrolist)=perlsdata[7] #instbxvaluelist=[x/lumiparam.lslengthsec() for x in bxvaluelist if x] instbxvaluelist=[x for x in bxvaluelist if x] maxlumi=0.0 if len(instbxvaluelist)!=0: maxlumi=max(instbxvaluelist) avginstlumi=0.0 if len(instbxvaluelist)!=0: avginstlumi=sum(instbxvaluelist) (intbxidxlist,b1intensities,b2intensities)=perlsdata[8]#contains only non-zero bx for bxidx in bxidxlist: idx=bxidxlist.index(bxidx) instbxvalue=bxvaluelist[idx] bxerror=bxerrolist[idx] if instbxvalue<max(xingMinLum,maxlumi*0.2): continue bintensityPos=-1 try: bintensityPos=intbxidxlist.index(bxidx) except ValueError: pass if bintensityPos<=0: fillbypos.setdefault(bxidx,[]).append([ts,beamstatusfrac,instbxvalue,bxerror,0.0,0.0]) continue b1intensity=b1intensities[bintensityPos] b2intensity=b2intensities[bintensityPos] speclumi=calculateSpecificLumi(instbxvalue,bxerror,b1intensity,0.0,b2intensity,0.0) fillbypos.setdefault(bxidx,[]).append([ts,beamstatusfrac,instbxvalue,bxerror,speclumi[0],speclumi[1]]) return fillbypos
def plotSumX_Time(self,rawdata={},resultlines=[],minTime=None,maxTime=None,nticks=6,yscale='linear',withannotation=False,referenceLabel='Delivered',labels=['Delivered','Recorded'],textoutput=None): ''' input: rawdata = {'Delivered':[(runnumber,starttimestamp,stoptimestamp,lumiperrun)],'Recorded':[(runnumber,starttimestamp,stoptimestamp,lumiperrun)]} resultlines = [[runnumber,starttimestampStr,stoptimestampStr,dellumiperrun,reclumiperrun],[runnumber,starttimestampStr,stoptimestampStr,dellumiperrun,reclumiperrun],] minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S yscale: linear,log or both withannotation: wheather the boundary points should be annotated referenceLabel: the one variable that decides the total unit and the plot x-axis range labels: labels of the variables to plot ''' xpoints=[] ypoints={} ytotal={} lut=lumiTime.lumiTime() if not minTime: minTime='03/01/10 00:00:00' minTime=lut.StrToDatetime(minTime,customfm='%m/%d/%y %H:%M:%S') if not maxTime: maxTime=datetime.datetime.utcnow() else: maxTime=lut.StrToDatetime(maxTime,customfm='%m/%d/%y %H:%M:%S') for r in resultlines: runnumber=int(r[0]) starttimeStr=r[1].split('.')[0] starttime=lut.StrToDatetime(starttimeStr,customfm='%Y-%m-%d %H:%M:%S') stoptimeStr=r[2].split('.')[0] stoptime=lut.StrToDatetime(stoptimeStr,customfm='%Y-%m-%d %H:%M:%S') if rawdata and runnumber in [t[0] for t in rawdata[referenceLabel]]:continue if starttime<minTime:continue if starttime>maxTime:continue for i,lab in enumerate(labels): v=float(r[-(len(labels)-i)]) rawdata.setdefault(lab,[]).append((runnumber,starttime,stoptime,v)) if not rawdata: print '[WARNING]: no data, do nothing' return tot=sum([t[3] for t in rawdata[referenceLabel]]) (unitstring,denomitor)=guessLumiUnit(tot) csvreport=None rows=[] flat=[] for label,yvalues in rawdata.items(): yvalues.sort() flat.append([t[3] for t in yvalues]) if label==referenceLabel: minTime=yvalues[0][1] maxTime=yvalues[-1][1] ypoints[label]=[] lumivals=[t[3] for t in yvalues] for i,val in enumerate(lumivals): ypoints[label].append(sum(lumivals[0:i+1])/denomitor) ytotal[label]=sum(lumivals)/denomitor xpoints=[matplotlib.dates.date2num(t[1]) for t in rawdata[referenceLabel]] ax=self.__fig.add_subplot(111) ax.set_yscale(yscale) yearStrMin=minTime.strftime('%Y') yearStrMax=maxTime.strftime('%Y') if yearStrMin==yearStrMax: dateFmt=matplotlib.dates.DateFormatter('%d/%m') else: dateFmt=matplotlib.dates.DateFormatter('%d/%m/%y') majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks) ax.xaxis.set_major_locator(majorLoc) minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4) ax.xaxis.set_major_formatter(dateFmt) ax.set_xlabel(r'Date',position=(0.84,0)) ax.set_ylabel(r'L '+unitstring,position=(0,0.9)) ax.xaxis.set_minor_locator(minorLoc) ax.set_xbound(lower=xpoints[0],upper=xpoints[-1]) xticklabels=ax.get_xticklabels() for tx in xticklabels: tx.set_horizontalalignment('left') ax.grid(True) keylist=ypoints.keys() keylist.sort() keylist.insert(0,keylist.pop(keylist.index(referenceLabel)))#move refereceLabel to front from now on legendlist=[] head=['#Run','StartTime','StopTime'] textsummaryhead=['#TotalRun'] textsummaryline=['#'+str(len(xpoints))] for ylabel in keylist: cl='k' if self.colormap.has_key(ylabel): cl=self.colormap[ylabel] ax.plot(xpoints,ypoints[ylabel],label=ylabel,color=cl,drawstyle='steps') legendlist.append(ylabel+' '+'%.3f'%(ytotal[ylabel])+' '+unitstring) textsummaryhead.append('Total'+ylabel) textsummaryline.append('%.3f'%(ytotal[ylabel])+' '+unitstring) head.append(ylabel) if textoutput: csvreport=csvReporter.csvReporter(textoutput) csvreport.writeRow(head) allruns=[int(t[0]) for t in rawdata[referenceLabel]] allstarts=[ lut.DatetimeToStr(t[1],customfm='%Y-%m-%d %H:%M:%S') for t in rawdata[referenceLabel] ] allstops=[ lut.DatetimeToStr(t[2],customfm='%Y-%m-%d %H:%M:%S') for t in rawdata[referenceLabel] ] flat.insert(0,allruns) flat.insert(1,allstarts) flat.insert(2,allstops) rows=zip(*flat) csvreport.writeRows([list(t) for t in rows]) csvreport.writeRow(textsummaryhead) csvreport.writeRow(textsummaryline) #annotations trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes) #print 'run boundary ',runs[0],runs[-1] #print 'xpoints boundary ',xpoints[0],xpoints[-1] #annotation if withannotation: runs=[t[0] for t in rawdata[referenceLabel]] ax.text(xpoints[0],1.025,str(runs[0]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white')) ax.text(xpoints[-1],1.025,str(runs[-1]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white')) if yearStrMin==yearStrMax: firsttimeStr=rawdata[referenceLabel][1][1].strftime('%b %d %H:%M') #time range(start) in the title is the first run beg time lasttimeStr=rawdata[referenceLabel][-1][2].strftime('%b %d %H:%M') #time range(stop) in the tile is the last run stop time #firstimeStr=minTime.strftime('%b %d %H:%M') #lasttimeStr=maxTime.strftime('%b %d %H:%M') #ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+' ('+firstimeStr+' - '+lasttimeStr+' UTC)',size='small',family='fantasy') ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+' ('+firsttimeStr+' - '+lasttimeStr+' UTC)',size='small') else: #ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+'-'+yearStrMax,size='small',family='fantasy') ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+'-'+yearStrMax,size='small') ax.legend(tuple(legendlist),loc='upper left') ax.autoscale_view(tight=True,scalex=True,scaley=False) self.__fig.autofmt_xdate(bottom=0.18,rotation=15,ha='right') self.__fig.subplots_adjust(bottom=0.2,left=0.15)
outtextfilename = outplotfilename + '.csv' if options.withoutTextoutput: outtextfilename = None lumip = lumiParameters.ParametersObject() svc = sessionManager.sessionManager(options.connect, authpath=options.authpath, siteconfpath=options.siteconfpath, debugON=options.debug) session = svc.openSession(isReadOnly=True, cpp2sqltype=[('unsigned int', 'NUMBER(10)'), ('unsigned long long', 'NUMBER(20)') ]) lslength = lumip.lslengthsec() begtime = options.begintime endtime = options.endtime lut = lumiTime.lumiTime() if not endtime: endtime = lut.DatetimeToStr(datetime.datetime.utcnow(), customfm='%m/%d/%y %H:%M:%S') pbeammode = None if options.beamstatus == 'stable': pbeammode = 'STABLE BEAMS' resultlines = [] inplots = [] ####################################################################### ##process old plot csv files,if any, skipping #commentlines ####################################################################### inplot = options.inplot if inplot: inplots = inplot.split('+') for ip in inplots:
if options.action == 'beambyls': session.transaction().start(True) dumpbeamintensity=False if options.outputfile and options.verbose: dumpbeamintensity=True result=lumiCalcAPI.beamForRange(session.nominalSchema(),irunlsdict,withBeamIntensity=options.withbeamintensity,minIntensity=options.minintensity) session.transaction().commit() if not options.outputfile: lumiReport.toScreenLSBeam(result,iresults=iresults,dumpIntensity=False) else: lumiReport.toCSVLSBeam(result,options.outputfile,resultlines=iresults,dumpIntensity=options.withbeamintensity,isverbose=options.verbose) if options.action == 'runsummary': session.transaction().start(True) result=lumiCalcAPI.runsummary(session.nominalSchema(),irunlsdict) session.transaction().commit() c=lumiTime.lumiTime() for r in result: run=r[0] fill='n/a' if r[5]: fill=str(r[5]) starttime=c.StrToDatetime(r[7]) starttime=starttime.strftime('%m/%d/%y %H:%M:%S') stoptime=c.StrToDatetime(r[8]) stoptime=stoptime.strftime('%m/%d/%y %H:%M:%S') l1key=r[1] hltkey=r[4] amodetag=r[2] egev='n/a' if r[3]: egev=str(r[3])
def generateLumidata(lumirundatafromfile, lsdatafromfile, rundatafromdb, lsdatafromdb, replacelsMin, replacelsMax): ''' input: perrunresultfromfile=[]#source,starttime,stoptime,nls perlsresultfromfile={} #{lumilsnum:instlumiub} lumirundatafromdb=[] #[source,nominalegev,ncollidingbunches,starttime,stoptime,nls] lumilsdatafromdb={}#{lumilsnum:[cmslsnum(0),instlumi(1),instlumierror(2),instlumiquality(3),beamstatus(4),beamenergy(5),numorbit(6),startorbit(7),cmsbxindexblob(8),beamintensityblob_1(9),beamintensityblob_2(10),bxlumivalue_occ1(11),bxlumierror_occ1(12),bxlumiquality_occ1(13),bxlumivalue_occ2(14),bxlumierror_occ2(15),bxlumiquality_occ2(16),bxlumivalue_et(17),bxlumierror_et(18),bxlumiquality_et(19)]} ''' lumip = lumiParameters.ParametersObject() numorbit = lumip.numorbit startorbit = 0 fakebeamenergy = 4000. fakebeamstatus = 'STABLE BEAMS' fakefloatArray = array.array('f') fakeidxArray = array.array('h') fakeshortArray = array.array('h') for bxidx in range(1, 3565): fakeidxArray.append(bxidx) fakefloatArray.append(0.) fakeshortArray.append(0) lumirundata = [] lumilsdata = {} if rundatafromdb: lumirundata = rundatafromdb lumirundata[0] = rundatafromdb[0] + '+file:' + lumirundatafromfile[0] else: lu = lumiTime.lumiTime() source = '+file:' + lumirundatafromfile[0] nominalegev = fakebeamenergy ncollidingbunches = 72 starttime = lumirundatafromfile[1] stoptime = lumirundatafromfile[2] starttimeT = lu.timestampTodatetimeUTC(starttime) stoptimeT = lu.timestampTodatetimeUTC(stoptime) print(starttimeT.day, starttimeT.month, starttimeT.year) starttimeT_coral = coral.TimeStamp(starttimeT.year, starttimeT.month, starttimeT.day, starttimeT.hour, starttimeT.minute, starttimeT.second, 0) stoptimeT_coral = coral.TimeStamp(stoptimeT.year, stoptimeT.month, stoptimeT.day, stoptimeT.hour, stoptimeT.minute, stoptimeT.second, 0) nls = lumirundatafromfile[3] lumirundata = [ source, nominalegev, ncollidingbunches, starttimeT_coral, stoptimeT_coral, nls ] if lsdatafromdb: lumilsdata = lsdatafromdb if replacelsMin > len(lsdatafromdb): print('[INFO]Operation: extend an existing run from LS=', replacelsMin) lumirundata[5] += len(lsdatafromfile) else: print( '[INFO]Operation: replace instlumi in an existing run LS range=', replacelsMin, replacelsMax) else: print('[INFO]Operation: insert a new fake run') for lumilsnum in range(replacelsMin, replacelsMax + 1): instlumi = lsdatafromfile[lumilsnum] if lumilsnum in lsdatafromdb.keys(): #if this is a hole lumilsdata[lumilsnum][1] = instlumi else: #if this is an extension instlumierror = 0.0 instlumiquality = 0 startorbit = (lumilsnum - 1) * numorbit cmsbxindexblob = CommonUtil.packArraytoBlob(fakeshortArray) beamintensityblob_1 = CommonUtil.packArraytoBlob(fakefloatArray) beamintensityblob_2 = CommonUtil.packArraytoBlob(fakefloatArray) bxlumivalue_occ1 = CommonUtil.packArraytoBlob(fakefloatArray) bxlumierror_occ1 = CommonUtil.packArraytoBlob(fakefloatArray) bxlumiquality_occ1 = CommonUtil.packArraytoBlob(fakeshortArray) bxlumivalue_occ2 = CommonUtil.packArraytoBlob(fakefloatArray) bxlumierror_occ2 = CommonUtil.packArraytoBlob(fakefloatArray) bxlumiquality_occ2 = CommonUtil.packArraytoBlob(fakeshortArray) bxlumivalue_et = CommonUtil.packArraytoBlob(fakefloatArray) bxlumierror_et = CommonUtil.packArraytoBlob(fakefloatArray) bxlumiquality_et = CommonUtil.packArraytoBlob(fakeshortArray) lumilsdata[lumilsnum] = [ 0, instlumi, instlumierror, instlumiquality, fakebeamstatus, fakebeamenergy, numorbit, startorbit, cmsbxindexblob, beamintensityblob_1, beamintensityblob_2, bxlumivalue_occ1, bxlumierror_occ1, bxlumiquality_occ1, bxlumivalue_occ2, bxlumierror_occ2, bxlumiquality_occ2, bxlumivalue_et, bxlumierror_et, bxlumiquality_et ] return (lumirundata, lumilsdata)
def instLumiForRange(schema,inputRange,beamstatusfilter=None,withBXInfo=False,bxAlgo=None,xingMinLum=0.0,withBeamIntensity=False,lumitype='HF',branchName=None): ''' DIRECTLY FROM ROOT FIME NO CORRECTION AT ALL lumi raw data. beofore normalization and time integral input: inputRange {run:[cmsls]} (required) beamstatus: LS filter on beamstatus (optional) withBXInfo: get per bunch info (optional) bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET'] xingMinLum: cut on bx lumi value (optional) withBeamIntensity: get beam intensity info (optional) branchName: data version output: result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),instlumi(5),instlumierr(6),startorbit(7),numorbit(8),(bxidx,bxvalues,bxerrs)(9),(bxidx,b1intensities,b2intensities)(10),fillnum(11)]}} lumi unit: HZ/ub ''' if lumitype not in ['HF','PIXEL']: raise ValueError('unknown lumitype '+lumitype) lumitableName='' lumilstableName='' if lumitype=='HF': lumitableName=nameDealer.lumidataTableName() lumilstableName=nameDealer.lumisummaryv2TableName() else: lumitableName=nameDealer.pixellumidataTableName() lumilstableName=nameDealer.pixellumisummaryv2TableName() result={} for run in inputRange.keys(): lslist=inputRange[run] if lslist is not None and len(lslist)==0: result[run]=[]#if no LS is selected for a run continue runsummary=dataDML.runsummary(schema,run) if len(runsummary)==0:#if run not found in runsummary result[run]=None continue fillnum=runsummary[4] runstarttimeStr=runsummary[6] lumidataid=dataDML.guessLumiDataIdByRun(schema,run,lumitableName) if lumidataid is None: #if run not found in lumidata result[run]=None continue (lumirunnum,perlsresult)=dataDML.lumiLSById(schema,lumidataid,beamstatusfilter,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName) lsresult=[] c=lumiTime.lumiTime() for lumilsnum in perlsresult.keys(): perlsdata=perlsresult[lumilsnum] cmslsnum=perlsdata[0] if lslist is not None and lumilsnum not in lslist: cmslsnum=0 numorbit=perlsdata[6] startorbit=perlsdata[7] orbittime=c.OrbitToTime(runstarttimeStr,startorbit,0) instlumi=perlsdata[1] instlumierr=perlsdata[2] beamstatus=perlsdata[4] beamenergy=perlsdata[5] bxidxlist=[] bxvaluelist=[] bxerrorlist=[] bxdata=None beamdata=None if withBXInfo: bxinfo=perlsdata[8] bxvalueArray=None bxerrArray=None if bxinfo: bxvalueArray=bxinfo[0] bxerrArray=bxinfo[1] for idx,bxval in enumerate(bxvalueArray): if bxval>xingMinLum: bxidxlist.append(idx) bxvaluelist.append(bxval) bxerrorlist.append(bxerrArray[idx]) del bxvalueArray[:] del bxerrArray[:] bxdata=(bxidxlist,bxvaluelist,bxerrorlist) if withBeamIntensity: beaminfo=perlsdata[9] bxindexlist=[] b1intensitylist=[] b2intensitylist=[] if beaminfo[0] and beaminfo[1] and beaminfo[2]: bxindexarray=beaminfo[0] beam1intensityarray=beaminfo[1] beam2intensityarray=beaminfo[2] bxindexlist=bxindexarray.tolist() b1intensitylist=beam1intensityarray.tolist() b2intensitylist=beam2intensityarray.tolist() del bxindexarray[:] del beam1intensityarray[:] del beam2intensityarray[:] beamdata=(bxindexlist,b1intensitylist,b2intensitylist) lsresult.append([lumilsnum,cmslsnum,orbittime,beamstatus,beamenergy,instlumi,instlumierr,startorbit,numorbit,bxdata,beamdata,fillnum]) del perlsdata[:] result[run]=lsresult return result
def lumiForRange(schema,inputRange,beamstatus=None,amodetag=None,egev=None,withBXInfo=False,bxAlgo=None,xingMinLum=0.0,withBeamIntensity=False,norm=None,datatag='DATA',finecorrections=None,driftcorrections=None,usecorrectionv2=False,lumitype='HF',branchName=None): ''' delivered/recorded lumi input: inputRange {run:[cmsls]} (required) beamstatus: LS filter on beamstatus (optional) amodetag: amodetag for picking norm(optional) egev: beamenergy for picking norm(optional) withBXInfo: get per bunch info (optional) bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET'] xingMinLum: cut on bx lumi value (optional) withBeamIntensity: get beam intensity info (optional) normname: norm factor name to use (optional) branchName: data version output: result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),(bxidx,bxvalues,bxerrs)(8),(bxidx,b1intensities,b2intensities)(9),fillnum(10)]} lumi unit: 1/ub ''' if lumitype not in ['HF','PIXEL']: raise ValueError('unknown lumitype '+lumitype) #if branchName is None: # branchName='DATA' lumip=lumiParameters.ParametersObject() lumitableName='' lumilstableName='' if lumitype=='HF': lumitableName=nameDealer.lumidataTableName() lumilstableName=nameDealer.lumisummaryv2TableName() else: lumitableName=nameDealer.pixellumidataTableName() lumilstableName=nameDealer.pixellumisummaryv2TableName() numbx=lumip.NBX result = {} normval=None perbunchnormval=None if norm: normval=_getnorm(schema,norm) perbunchnormval=float(normval)/float(1000) elif amodetag and egev: normval=_decidenormFromContext(schema,amodetag,egev) perbunchnormval=float(normval)/float(1000) c=lumiTime.lumiTime() for run in inputRange.keys():#loop over run lslist=inputRange[run] if lslist is not None and len(lslist)==0:#no selected ls, do nothing for this run result[run]=[] continue cmsrunsummary=dataDML.runsummary(schema,run) if len(cmsrunsummary)==0:#non existing run result[run]=None continue startTimeStr=cmsrunsummary[6] fillnum=cmsrunsummary[4] lumidataid=None trgdataid=None lumidataid=dataDML.guessLumiDataIdByRun(schema,run,lumitableName) if lumidataid is None : result[run]=None continue trgdataid=dataDML.guessTrgDataIdByRun(schema,run) (lumirunnum,lumidata)=dataDML.lumiLSById(schema,lumidataid,beamstatus=beamstatus,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName) if trgdataid is None : trgdata={} else: (trgrunnum,trgdata)=dataDML.trgLSById(schema,trgdataid) if not normval:#if norm cannot be decided , look for it according to context per run normval=_decidenormForRun(schema,run) perbunchnormval=float(normval)/float(1000) if not normval:#still not found? resort to global default (should never come here) normval=6370 perbunchnormval=6.37 print '[Warning] using default normalization '+str(normval) perrunresult=[] for lumilsnum,perlsdata in lumidata.items(): cmslsnum=perlsdata[0] triggeredls=perlsdata[0] if lslist is not None and cmslsnum not in lslist: #cmslsnum=0 triggeredls=0 recordedlumi=0.0 instlumi=perlsdata[1] instlumierror=perlsdata[2] avglumi=instlumi*normval calibratedlumi=avglumi if lumitype=='HF' and finecorrections and finecorrections[run]: if usecorrectionv2: if driftcorrections and driftcorrections[run]: calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run]) else: calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0) else: calibratedlumi=lumiCorrections.applyfinecorrection(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2]) if lumitype=='PIXEL' and finecorrections is not None: calibratedlumi=finecorrections[run]*avglumi calibratedlumierror=instlumierror*normval bstatus=perlsdata[4] begev=perlsdata[5] numorbit=perlsdata[6] startorbit=perlsdata[7] timestamp=c.OrbitToTime(startTimeStr,startorbit,0) lslen=lumip.lslengthsec() deliveredlumi=calibratedlumi*lslen recordedlumi=0.0 if triggeredls!=0: if not trgdata.has_key(cmslsnum): # triggeredls=0 #if no trigger, set back to non-cms-active ls recordedlumi=0.0 # no trigger->nobeam recordedlumi=None else: deadcount=trgdata[cmslsnum][0] ##subject to change !! bitzerocount=trgdata[cmslsnum][1] bitzeroprescale=trgdata[cmslsnum][2] deadfrac=trgdata[cmslsnum][3] if deadfrac<0 or deadfrac>1.0: deadfrac=1.0 #if float(bitzerocount)*float(bitzeroprescale)==0.0: # deadfrac=1.0 #else: # deadfrac=float(deadcount)/(float(bitzerocount)*float(bitzeroprescale)) #if deadfrac>1.0: # deadfrac=1.0 #artificial correction in case of deadfrac>1 recordedlumi=deliveredlumi*(1.0-deadfrac) del trgdata[cmslsnum][:] bxdata=None if withBXInfo: bxinfo=perlsdata[8] bxvalueArray=None bxerrArray=None bxidxlist=[] bxvaluelist=[] bxerrorlist=[] if bxinfo: bxvalueArray=bxinfo[0] bxerrArray=bxinfo[1] #if cmslsnum==1: # print 'bxvalueArray ',bxvalueArray for idx,bxval in enumerate(bxvalueArray): if finecorrections and finecorrections[run]: if usecorrectionv2: if driftcorrections and driftcorrections[run]: mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run]) else: mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0) else: mybxval=lumiCorrections.applyfinecorrectionBX(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2]) else: mybxval=bxval*perbunchnormval if mybxval>xingMinLum: bxidxlist.append(idx) bxvaluelist.append(mybxval) bxerrorlist.append(bxerrArray[idx]*perbunchnormval)#no correciton on errors del bxvalueArray[:] del bxerrArray[:] bxdata=(bxidxlist,bxvaluelist,bxerrorlist) beamdata=None if withBeamIntensity: beaminfo=perlsdata[9] bxindexlist=[] b1intensitylist=[] b2intensitylist=[] if beaminfo: bxindexarray=beaminfo[0] beam1intensityarray=beaminfo[1] beam2intensityarray=beaminfo[2] bxindexlist=bxindexarray.tolist() b1intensitylist=beam1intensityarray.tolist() b2intensitylist=beam2intensityarray.tolist() del bxindexarray[:] del beam1intensityarray[:] del beam2intensityarray[:] beamdata=(bxindexlist,b1intensitylist,b2intensitylist) perrunresult.append([lumilsnum,triggeredls,timestamp,bstatus,begev,deliveredlumi,recordedlumi,calibratedlumierror,bxdata,beamdata,fillnum]) del perlsdata[:] result[run]=perrunresult return result
def HALOParams_ForRun(dbsession,start,stop,runnum): ''' Method retrieving the CSC halo rates between two time stamps select t."TIMESTAMP", rate.num_asp, rate.halo_rate from CSCTF_RATES_T rate, CMS_CSC_TF_MON.CSCTF_CSCTF_RATES t where t.uniqueid =rate.csctf_csctf_rates_uniqueid ''' HALOParams='' try: dbsession.transaction().start(True) schema=dbsession.schema('CMS_CSC_TF_MON') if not schema: raise Exception, 'cannot connect to schema CMS_CSC_TF_MON' if not schema.existsTable('CSCTF_RATES_T'): raise Exception, 'non-existing table CSCTF_RATES_T' start_str=time.strftime("%Y%m%d%H%M",time.localtime(start)) stop_str=time.strftime("%Y%m%d%H%M",time.localtime(stop)) #print start_str,stop_str t=lumiTime.lumiTime () query_START=schema.newQuery() query_START.addToTableList('CSCTF_RATES_T','rate') query_START.addToTableList('CSCTF_CSCTF_RATES','t') query_START.addToOutputList ('TO_CHAR (t.TIMESTAMP, \''+t.coraltimefm+'\')', 'time') query_START.addToOutputList('rate.NUM_ASP','nasp') query_START.addToOutputList('rate.HALO_RATE','hrate') query_STARTOutput=coral.AttributeList() query_STARTOutput.extend('time','string') query_STARTOutput.extend('nasp','int') query_STARTOutput.extend('hrate','float') query_STARTCond=coral.AttributeList() query_START.setCondition("t.UNIQUEID=rate.CSCTF_CSCTF_RATES_UNIQUEID and t.TIMESTAMP between TO_DATE(\'%s\', \'YYYYMMDDHH24MI\') and TO_DATE(\'%s\',\'YYYYMMDDHH24MI\')"%(start_str,stop_str),query_STARTCond) query_START.defineOutput(query_STARTOutput) cursor=query_START.execute() # Cursor contains the list of runs # We then loop on them and apply some selection criteria rates_raw=[] while cursor.next(): split = cursor.currentRow()['time'].data().split(' ')[0:] d = time.strptime(split[0], "%m/%d/%y") h = split[1].split(':')[0:] t = time.mktime(d)+int(h[0])*3600+int(h[1])*60+int(h[2].split('.')[0:][0]) rates_raw.append(CSC_rate(t,cursor.currentRow()['nasp'].data(),cursor.currentRow()['hrate'].data())) if len(rates_raw)==0: return rates_sorted = sorted(rates_raw, key=attrgetter('sector','time')) rates_skimmed = [] for obj in rates_sorted: if obj in rates_skimmed: continue rates_skimmed.append(obj) rates_skimmed = sorted(rates_skimmed, key=attrgetter('time','sector')) filename="CSC_rates_%d.txt"%(int(runnum)) file = open(filename,"w") for obj in rates_skimmed: if obj.sector==0: text = "%d "%obj.time file.write(text) text = "%f "%obj.rate file.write(text) if obj.sector==12: file.write("\n") file.close() del query_START dbsession.transaction().commit() return except Exception,e: print str(e) dbsession.transaction().rollback() del dbsession
def effectiveLumiForRange(schema,inputRange,hltpathname=None,hltpathpattern=None,amodetag=None,beamstatus=None,egev=None,withBXInfo=False,xingMinLum=0.0,bxAlgo=None,withBeamIntensity=False,norm=None,finecorrections=None,driftcorrections=None,usecorrectionv2=False,lumitype='HF',branchName=None): ''' input: inputRange {run:[cmsls]} (required) hltpathname: selected hltpathname hltpathpattern: regex select hltpaths amodetag: amodetag for picking norm(optional) egev: beamenergy for picking norm(optional) withBXInfo: get per bunch info (optional) bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET'] xingMinLum: cut on bx lumi value (optional) withBeamIntensity: get beam intensity info (optional) normname: norm factor name to use (optional) branchName: data version output: result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),{hltpath:[l1name,l1prescale,hltprescale,efflumi]},bxdata,beamdata,fillnum]} lumi unit: 1/ub ''' if lumitype not in ['HF','PIXEL']: raise ValueError('unknown lumitype '+lumitype) if branchName is None: branchName='DATA' lumitableName='' lumilstableName='' if lumitype=='HF': lumitableName=nameDealer.lumidataTableName() lumilstableName=nameDealer.lumisummaryv2TableName() else: lumitableName=nameDealer.pixellumidataTableName() lumilstableName=nameDealer.pixellumisummaryv2TableName() numbx=3564 result = {} normval=None perbunchnormval=None if norm: normval=_getnorm(schema,norm) perbunchnormval=float(normval)/float(1000) elif amodetag and egev: normval=_decidenormFromContext(schema,amodetag,egev) perbunchnormval=float(normval)/float(1000) c=lumiTime.lumiTime() lumip=lumiParameters.ParametersObject() for run in inputRange.keys(): lslist=inputRange[run] if lslist is not None and len(lslist)==0:#no selected ls, do nothing for this run result[run]=[] continue cmsrunsummary=dataDML.runsummary(schema,run) if len(cmsrunsummary)==0:#non existing run result[run]=None continue startTimeStr=cmsrunsummary[6] fillnum=cmsrunsummary[4] lumidataid=None trgdataid=None hltdataid=None lumidataid=dataDML.guessLumiDataIdByRun(schema,run,lumitableName) trgdataid=dataDML.guessTrgDataIdByRun(schema,run) hltdataid=dataDML.guessHltDataIdByRun(schema,run) if lumidataid is None or trgdataid is None or hltdataid is None: result[run]=None continue (lumirunnum,lumidata)=dataDML.lumiLSById(schema,lumidataid,beamstatus,tableName=lumilstableName) (trgrunnum,trgdata)=dataDML.trgLSById(schema,trgdataid,withPrescale=True) (hltrunnum,hltdata)=dataDML.hltLSById(schema,hltdataid,hltpathname=hltpathname,hltpathpattern=hltpathpattern) hlttrgmap=dataDML.hlttrgMappingByrun(schema,run) if not normval:#if norm cannot be decided , look for it according to context per run normval=_decidenormForRun(schema,run) perbunchnormval=float(normval)/float(1000) if not normval:#still not found? resort to global default (should never come here) normval=6370 perbunchnormval=6.37 print '[Warning] using default normalization '+str(normval) perrunresult=[] for lumilsnum,perlsdata in lumidata.items(): cmslsnum=perlsdata[0] triggeredls=perlsdata[0] if lslist is not None and cmslsnum not in lslist: #cmslsnum=0 triggeredls=0 recordedlumi=0.0 instlumi=perlsdata[1] instlumierror=perlsdata[2] avglumi=instlumi*normval calibratedlumi=avglumi if lumitype=='HF' and finecorrections and finecorrections[run]: if usecorrectionv2: if driftcorrections and driftcorrections[run]: calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run]) else: calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0) else: calibratedlumi=lumiCorrections.applyfinecorrection(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2]) if lumitype=='PIXEL' and finecorrections is not None: calibratedlumi=finecorrections[run]*avglumi calibratedlumierror=instlumierror*normval bstatus=perlsdata[4] begev=perlsdata[5] numorbit=perlsdata[6] startorbit=perlsdata[7] timestamp=c.OrbitToTime(startTimeStr,startorbit,0) lslen=lumip.lslengthsec() deliveredlumi=calibratedlumi*lslen recordedlumi=0.0 trgprescalemap={}#trgprescalemap for this ls efflumidict={} if triggeredls!=0: if not trgdata.has_key(cmslsnum): #triggeredls=0 #if no trigger, set back to non-cms-active ls recordedlumi=0.0 # no trigger->nobeam recordedlumi=None else: deadcount=trgdata[cmslsnum][0] ##subject to change !! bitzerocount=trgdata[cmslsnum][1] bitzeroprescale=trgdata[cmslsnum][2] deadfrac=trgdata[cmslsnum][3] if deadfrac<0 or deadfrac>1.0: deadfrac=1.0 #if float(bitzerocount)*float(bitzeroprescale)==0.0: # deadfrac=1.0 #else: # deadfrac=float(deadcount)/(float(bitzerocount)*float(bitzeroprescale)) #if deadfrac>1.0: # deadfrac=1.0 #artificial correction in case of deadfrac>1 recordedlumi=deliveredlumi*(1.0-deadfrac) l1bitinfo=trgdata[cmslsnum][4] if l1bitinfo: for thisbitinfo in l1bitinfo: thisbitname=thisbitinfo[0] thisbitprescale=thisbitinfo[2] #trgprescalemap['"'+thisbitname+'"']=thisbitprescale#note:need to double quote bit name! trgprescalemap['"'+thisbitname+'"']=thisbitprescale del trgdata[cmslsnum][:] if hltdata.has_key(cmslsnum): hltpathdata=hltdata[cmslsnum] #print 'hltpathdata ',hltpathdata for pathidx,thispathinfo in enumerate(hltpathdata): efflumi=0.0 thispathname=thispathinfo[0] thisprescale=thispathinfo[1] thisl1seed=None l1bitname=None l1prescale=None try: thisl1seed=hlttrgmap[thispathname] except KeyError: thisl1seed=None # print 'hltpath, l1seed, hltprescale ',thispathname,thisl1seed,thisprescale if thisl1seed: try: l1bitname=hltTrgSeedMapper.findUniqueSeed(thispathname,thisl1seed) if l1bitname : l1prescale=trgprescalemap[l1bitname]#need to match double quoted string! else: l1prescale=None except KeyError: l1prescale=None if l1prescale and thisprescale :#normal both prescaled efflumi=recordedlumi/(float(l1prescale)*float(thisprescale)) efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi] elif l1prescale and thisprescale==0: #hltpath in menu but masked efflumi=0.0 efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi] else: efflumi=0.0 efflumidict[thispathname]=[None,0,thisprescale,efflumi] bxvaluelist=[] bxerrorlist=[] bxdata=None beamdata=None if withBXInfo: bxinfo=lumidata[8] bxvalueArray=None bxerrArray=None if bxinfo: bxvalueArray=bxinfo[0] bxerrArray=bxinfo[1] for idx,bxval in enumerate(bxvalueArray): if finecorrections and finecorrections[run]: if usecorrectionv2: if driftcorrections and driftcorrections[run]: mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run]) else: mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0) else: mybxval=lumiCorrections.applyfinecorrectionBX(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2]) else: mybxval=bxval*perbunchnormval if mybxval>xingMinLum: bxidxlist.append(idx) bxvaluelist.append(bxval) bxerrorlist.append(bxerrArray[idx]) del bxvalueArray[:] del bxerrArray[:] bxdata=(bxidxlist,bxvaluelist,bxerrorlist) if withBeamIntensity: beaminfo=perlsdata[9] bxindexlist=[] b1intensitylist=[] b2intensitylist=[] if beaminfo: bxindexarray=beaminfo[0] beam1intensityarray=beaminfo[1] beam2intensityarray=beaminfo[2] bxindexlist=bxindexarray.tolist() b1intensitylist=beam1intensityarray.tolist() b2intensitylist=beam2intensityarray.tolist() del bxindexarray[:] del beam1intensityarray[:] del beam2intensityarray[:] beamdata=(bxindexlist,b1intensitylist,b2intensitylist) # print cmslsnum,deliveredlumi,recordedlumi,efflumidict perrunresult.append([lumilsnum,triggeredls,timestamp,bstatus,begev,deliveredlumi,recordedlumi,calibratedlumierror,efflumidict,bxdata,beamdata,fillnum]) del perlsdata[:] result[run]=perrunresult #print result return result
minIntensity=options.minintensity) session.transaction().commit() if not options.outputfile: lumiReport.toScreenLSBeam(result, iresults=iresults, dumpIntensity=False) else: lumiReport.toScreenLSBeam(result, iresults=iresults, dumpIntensity=options.withbeamintensity, toFile=options.outputfile) if options.action == 'runsummary': session.transaction().start(True) result = lumiCalcAPI.runsummary(session.nominalSchema(), irunlsdict) session.transaction().commit() c = lumiTime.lumiTime() for r in result: run = r[0] fill = 'n/a' if r[5]: fill = str(r[5]) starttime = c.StrToDatetime(r[7]) starttime = starttime.strftime('%m/%d/%y %H:%M:%S') stoptime = c.StrToDatetime(r[8]) stoptime = stoptime.strftime('%m/%d/%y %H:%M:%S') l1key = r[1] hltkey = r[4] amodetag = r[2] egev = 'n/a' if r[3]: egev = str(r[3])
if options.beammode=='stable': pbeammode = 'STABLE BEAMS' if options.action=='trgbyls': reqTrg=True if options.action=='hltbyls': reqHlt=True if options.runnumber: reqrunmax=options.runnumber reqrunmin=options.runnumber if options.fillnum: reqfillmin=options.fillnum reqfillmax=options.fillnum if options.begin: (reqrunmin,reqfillmin,reqtimemin)=CommonUtil.parseTime(options.begin) if reqtimemin: lute=lumiTime.lumiTime() reqtimeminT=lute.StrToDatetime(reqtimemin,customfm='%m/%d/%y %H:%M:%S') timeFilter[0]=reqtimeminT if options.end: (reqrunmax,reqfillmax,reqtimemax)=CommonUtil.parseTime(options.end) if reqtimemax: lute=lumiTime.lumiTime() reqtimemaxT=lute.StrToDatetime(reqtimemax,customfm='%m/%d/%y %H:%M:%S') timeFilter[1]=reqtimemaxT sname=options.name isdetail=False spattern=None if sname is not None: isdetail=True if sname=='*' or sname=='all': sname=None
def plotInst_RunLS(self, rawxdata, rawydata, nticks=6, textoutput=None): ''' Input: rawxdata [run,fill,starttime,stoptime,totalls,ncmsls] rawydata {label:[lumi]} ''' lslength = 23.357 lut = lumiTime.lumiTime() runnum = rawxdata[0] fill = rawxdata[1] starttime = lut.DatetimeToStr(rawxdata[2], customfm='%m/%d/%y %H:%M:%S') stoptime = lut.DatetimeToStr(rawxdata[3], customfm='%m/%d/%y %H:%M:%S') totalls = rawxdata[-2] ncmsls = rawxdata[-1] peakinst = max(rawydata['Delivered']) / lslength totaldelivered = sum(rawydata['Delivered']) totalrecorded = sum(rawydata['Recorded']) xpoints = range(1, totalls + 1) #print len(xpoints) ypoints = {} ymax = {} for ylabel, yvalue in rawydata.items(): ypoints[ylabel] = [y / lslength for y in yvalue] ymax[ylabel] = max(yvalue) / lslength left = 0.15 width = 0.7 bottom = 0.1 height = 0.65 bottom_h = bottom + height rect_scatter = [left, bottom, width, height] rect_table = [left, bottom_h, width, 0.25] nullfmt = matplotlib.ticker.NullFormatter() nullloc = matplotlib.ticker.NullLocator() axtab = self.__fig.add_axes(rect_table, frameon=False) axtab.set_axis_off() axtab.xaxis.set_major_formatter(nullfmt) axtab.yaxis.set_major_formatter(nullfmt) axtab.xaxis.set_major_locator(nullloc) axtab.yaxis.set_major_locator(nullloc) ax = self.__fig.add_axes(rect_scatter) majorLoc = matplotlib.ticker.LinearLocator(numticks=nticks) minorLoc = matplotlib.ticker.LinearLocator(numticks=nticks * 4) ax.set_xlabel(r'LS', position=(0.96, 0)) ax.set_ylabel(r'L $\mu$b$^{-1}$s$^{-1}$', position=(0, 0.9)) ax.xaxis.set_major_locator(majorLoc) ax.xaxis.set_minor_locator(minorLoc) ax.set_xbound(lower=xpoints[0], upper=xpoints[-1]) xticklabels = ax.get_xticklabels() for tx in xticklabels: tx.set_horizontalalignment('right') ax.grid(True) keylist = sorted(ypoints.keys()) legendlist = [] for ylabel in keylist: cl = 'k' if ylabel in self.colormap: cl = self.colormap[ylabel] ax.plot(xpoints, ypoints[ylabel], '.', label=ylabel, color=cl) legendlist.append(ylabel) #ax.axhline(0,color='green',linewidth=0.2) ax.axvline(xpoints[ncmsls - 1], color='green', linewidth=0.2) (unitstring, denomitor) = guessLumiUnit(totaldelivered) colLabels = ('run', 'fill', 'max inst(/$\mu$b/s)', 'delivered(' + unitstring + ')', 'recorded(' + unitstring + ')') cellText = [[ str(runnum), str(fill), '%.3f' % (peakinst), '%.3f' % (totaldelivered / denomitor), '%.3f' % (totalrecorded / denomitor) ]] sumtable = axtab.table(cellText=cellText, colLabels=colLabels, colWidths=[0.12, 0.1, 0.27, 0.27, 0.27], cellLoc='center', loc='center') trans = matplotlib.transforms.BlendedGenericTransform( ax.transData, ax.transAxes) axtab.add_table(sumtable) ax.text(xpoints[0], 1.02, starttime[0:17], transform=trans, horizontalalignment='left', size='x-small', color='green', bbox=dict(facecolor='white')) ax.text(xpoints[ncmsls - 1], 1.02, stoptime[0:17], transform=trans, horizontalalignment='left', size='x-small', color='green', bbox=dict(facecolor='white')) ax.legend(tuple(legendlist), loc='upper right', numpoints=1)
def getLumiInfoForRuns(dbsession,c,runList,selectionDict,hltpath='',beamstatus=None,beamenergy=None,beamfluctuation=0.0,finecorrections=None): ''' input: runList[runnum], selectionDict{runnum:[ls]} output:{runnumber:[delivered,recorded,recordedinpath] } ''' t=lumiTime.lumiTime() result={}#runnumber:[lumisumoverlumils,lumisumovercmsls-deadtimecorrected,lumisumovercmsls-deadtimecorrected*hltcorrection_hltpath] #print 'selectionDict seen ',selectionDict dbsession.transaction().start(True) for runnum in runList: totallumi=0.0 delivered=0.0 recorded=0.0 recordedinpath=0.0 if len(selectionDict)!=0 and not selectionDict.has_key(runnum): if runnum<max(selectionDict.keys()): result[runnum]=[0.0,0.0,0.0] continue q=dbsession.nominalSchema().newQuery() if finecorrections and finecorrections[runnum]: totallumi=lumiQueryAPI.lumisumByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation,finecorrections=finecorrections[runnum]) #q1 else: totallumi=lumiQueryAPI.lumisumByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation) del q if not totallumi: result[runnum]=[0.0,0.0,0.0] if c.VERBOSE: print 'run ',runnum,' does not exist or has no lumi, skip' continue lumitrginfo={} hltinfo={} hlttrgmap={} q=dbsession.nominalSchema().newQuery() if finecorrections and finecorrections[runnum]: lumitrginfo=lumiQueryAPI.lumisummarytrgbitzeroByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation,finecorrections=finecorrections[runnum]) #q2 else: lumitrginfo=lumiQueryAPI.lumisummarytrgbitzeroByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation) del q if len(lumitrginfo)==0: q=dbsession.nominalSchema().newQuery() if finecorrections and finecorrections[runnum]: lumiinfobyrun=lumiQueryAPI.lumisummaryByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation,finecorrections=finecorrections[runnum]) #q3 else: lumiinfobyrun=lumiQueryAPI.lumisummaryByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation) del q if len(lumiinfobyrun)!=0: print 'warning request run ',runnum,' has no trigger data, calculate delivered only' for perlsdata in lumiinfobyrun: cmslsnum=perlsdata[0] instlumi=perlsdata[1] norbit=perlsdata[2] lslength=t.bunchspace_s*t.nbx*norbit delivered+=instlumi*lslength result[runnum]=[delivered,0.0,0.0] #result[runnum]=[0.0,0.0,0.0] #if c.VERBOSE: print 'request run ',runnum,' has no trigger, skip' else: norbits=lumitrginfo.values()[0][1] lslength=t.bunchspace_s*t.nbx*norbits delivered=totallumi*lslength hlttrgmap={} trgbitinfo={} if len(hltpath)!=0 and hltpath!='all': q=dbsession.nominalSchema().newQuery() #optional q3, initiated only if you ask for a hltpath hlttrgmap=lumiQueryAPI.hlttrgMappingByrun(q,runnum) del q if hlttrgmap.has_key(hltpath): l1bitname=hltTrgSeedMapper.findUniqueSeed(hltpath,hlttrgmap[hltpath]) q=dbsession.nominalSchema().newQuery() #optional q4, initiated only if you ask for a hltpath and it exists hltinfo=lumiQueryAPI.hltBypathByrun(q,runnum,hltpath) del q q=dbsession.nominalSchema().newQuery() trgbitinfo=lumiQueryAPI.trgBybitnameByrun(q,runnum,l1bitname) #optional q5, initiated only if you ask for a hltpath and it has a unique l1bit del q #done all possible queries. process result for cmslsnum,valuelist in lumitrginfo.items(): if len(selectionDict)!=0 and not (cmslsnum in selectionDict[runnum]): #if there's a selection list but cmslsnum is not selected,skip continue if valuelist[5]==0:#bitzero==0 means no beam,do nothing continue trgprescale=valuelist[8] deadfrac=float(valuelist[6])/float(float(valuelist[5])*float(trgprescale)) if(deadfrac<1.0): recorded=recorded+valuelist[0]*(1.0-deadfrac)*lslength if hlttrgmap.has_key(hltpath) and hltinfo.has_key(cmslsnum): hltprescale=hltinfo[cmslsnum][2] trgprescale=trgbitinfo[cmslsnum][3] recordedinpath=recordedinpath+valuelist[0]*(1.0-deadfrac)*lslength*hltprescale*trgprescale else: if deadfrac<0.0: print 'warning deadfraction negative in run',runnum,' ls ',cmslsnum if c.VERBOSE: print runnum,cmslsnum,valuelist[0]*lslength,valuelist[0]*(1.0-deadfrac)*lslength,lslength,deadfrac result[runnum]=[delivered,recorded,recordedinpath] dbsession.transaction().commit() #if c.VERBOSE: # print result return result
def getSpecificLumi(dbsession,parameters,fillnum,inputdir,finecorrections=None): ''' specific lumi in 1e-30 (ub-1s-1) unit lumidetail occlumi in 1e-27 1309_lumi_401_CMS.txt time(in seconds since January 1,2011,00:00:00 UTC) stab(fraction of time spent in stable beams for this time bin) l(lumi in Hz/ub) dl(point-to-point error on lumi in Hz/ub) sl(specific lumi in Hz/ub) dsl(error on specific lumi) 20800119.0 1 -0.889948 0.00475996848729 0.249009 0.005583287562 -0.68359 6.24140208607 0.0 0.0 0.0 0.0 0.0 0.0 0.0383576 0.00430892097862 0.0479095 0.00430892097862 66.6447 4.41269758764 0.0 0.0 0.0 result [(time,beamstatusfrac,lumi,lumierror,speclumi,speclumierror)] ''' #result=[] runtimesInFill=getFillFromFile(fillnum,inputdir)#{runnum:starttimestr} beamstatusDict={}#{runnum:{(startorbit,cmslsnum):beamstatus}} t=lumiTime.lumiTime() fillbypos={}#{bxidx:(lstime,beamstatusfrac,lumi,lumierror,specificlumi,specificlumierror)} #referencetime=time.mktime(datetime.datetime(2010,1,1,0,0,0).timetuple()) referencetime=0 if fillnum and len(runtimesInFill)==0: runtimesInFill=getFillFromDB(dbsession,parameters,fillnum)#{runnum:starttimestr} #precheck totalstablebeamLS=0 for runnum in runtimesInFill.keys(): q=dbsession.nominalSchema().newQuery() runinfo=lumiQueryAPI.lumisummaryByrun(q,runnum,'0001',beamstatus=None) del q lsbeamstatusdict={} for perlsdata in runinfo: cmslsnum=perlsdata[0] startorbit=perlsdata[3] beamstatus=perlsdata[4] lsbeamstatusdict[(startorbit,cmslsnum)]=beamstatus #print (startorbit,cmslsnum),beamstatus if beamstatus=='STABLE BEAMS': totalstablebeamLS+=1 beamstatusDict[runnum]=lsbeamstatusdict if totalstablebeamLS<10:#less than 10 LS in a fill has 'stable beam', it's no a good fill print 'fill ',fillnum,' , having less than 10 stable beam lS, is not good, skip' return fillbypos for runnum,starttime in runtimesInFill.items(): #if not runtimesInFill.has_key(runnum): # print 'run '+str(runnum)+' does not exist' # continue q=dbsession.nominalSchema().newQuery() if finecorrections and finecorrections[runnum]: occlumidata=lumiQueryAPI.calibratedDetailForRunLimitresult(q,parameters,runnum,finecorrection=finecorrections[runnum])#{(startorbit,cmslsnum):[(bxidx,lumivalue,lumierr)]} #values after cut else: occlumidata=lumiQueryAPI.calibratedDetailForRunLimitresult(q,parameters,runnum) del q #print occlumidata q=dbsession.nominalSchema().newQuery() beamintensitydata=lumiQueryAPI.beamIntensityForRun(q,parameters,runnum)#{startorbit:[(bxidx,beam1intensity,beam2intensity),()]} #print 'beamintensity for run ',runnum,beamintensitydata del q for (startorbit,cmslsnum),lumilist in occlumidata.items(): if len(lumilist)==0: continue beamstatusflag=beamstatusDict[runnum][(startorbit,cmslsnum)] beamstatusfrac=0.0 if beamstatusflag=='STABLE BEAMS': beamstatusfrac=1.0 lstimestamp=t.OrbitToUTCTimestamp(starttime,startorbit) for lumidata in lumilist:#loop over bx bxidx=lumidata[0] lumi=lumidata[1] lumierror=lumidata[2] speclumi=(0.0,0.0) if not fillbypos.has_key(bxidx): fillbypos[bxidx]=[] if beamintensitydata.has_key(startorbit): beaminfo=beamintensitydata[startorbit] for beamintensitybx in beaminfo: if beamintensitybx[0]==bxidx: beam1intensity=beamintensitybx[1] beam2intensity=beamintensitybx[2] if beam1intensity<0: beam1intensity=0 if beam2intensity<0: beam2intensity=0 speclumi=calculateSpecificLumi(lumi,lumierror,beam1intensity,0.0,beam2intensity,0.0) break fillbypos[bxidx].append((lstimestamp-referencetime,beamstatusfrac,lumi,lumierror,speclumi[0],speclumi[1])) #print 'fillbypos.keys ',fillbypos.keys() return fillbypos
def getSpecificLumi(schema,fillnum,inputdir,xingMinLum=0.0,norm='pp7TeV',withcorrection=True,amodetag='PROTPHYS',bxAlgo='OCC1'): ''' specific lumi in 1e-30 (ub-1s-1) unit lumidetail occlumi in 1e-27 1309_lumi_401_CMS.txt time(in seconds since January 1,2011,00:00:00 UTC) stab(fraction of time spent in stable beams for this time bin) l(lumi in Hz/ub) dl(point-to-point error on lumi in Hz/ub) sl(specific lumi in Hz/ub) dsl(error on specific lumi) 20800119.0 1 -0.889948 0.00475996848729 0.249009 0.005583287562 -0.68359 6.24140208607 0.0 0.0 0.0 0.0 0.0 0.0 0.0383576 0.00430892097862 0.0479095 0.00430892097862 66.6447 4.41269758764 0.0 0.0 0.0 result [(time,beamstatusfrac,lumi,lumierror,speclumi,speclumierror)] ''' t=lumiTime.lumiTime() fillbypos={}#{bxidx:[[ts,beamstatusfrac,lumi,lumierror,spec1,specerror],[]]} runtimesInFill=getFillFromDB(schema,fillnum)#{runnum:starttimestr} runlist=runtimesInFill.keys() if not runlist: return fillbypos irunlsdict=dict(zip(runlist,[None]*len(runlist))) #print irunlsdict finecorrections=None driftcorrections=None if withcorrection : cterms=lumiCorrections.nonlinearV2() finecorrections=lumiCorrections.correctionsForRangeV2(schema,runlist,cterms)#constant+nonlinear corrections driftcorrections=lumiCorrections.driftcorrectionsForRange(schema,runlist,cterms) lumidetails=lumiCalcAPI.instCalibratedLumiForRange(schema,irunlsdict,beamstatus=None,amodetag=amodetag,withBXInfo=True,withBeamIntensity=True,bxAlgo=bxAlgo,xingMinLum=xingMinLum,norm=norm,finecorrections=finecorrections,driftcorrections=driftcorrections,usecorrectionv2=True) session.transaction().commit() # #output: {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),calibratedlumi(5),calibratedlumierr(6),startorbit(7),numorbit(8),(bxvalues,bxerrs)(9),(bxidx,b1intensities,b2intensities)(10)]}} # totalstablebeamls=0 orderedrunlist=sorted(lumidetails) for run in orderedrunlist: perrundata=lumidetails[run] for perlsdata in perrundata: beamstatus=perlsdata[3] if beamstatus=='STABLE BEAMS': totalstablebeamls+=1 #print 'totalstablebeamls in fill ',totalstablebeamls if totalstablebeamls<10:#less than 10 LS in a fill has 'stable beam', it's no a good fill print 'fill ',fillnum,' , having less than 10 stable beam lS, is not good, skip' return fillbypos for run in orderedrunlist: perrundata=lumidetails[run] for perlsdata in perrundata: beamstatusfrac=0.0 tsdatetime=perlsdata[2] ts=calendar.timegm(tsdatetime.utctimetuple()) beamstatus=perlsdata[3] if beamstatus=='STABLE BEAMS': beamstatusfrac=1.0 (bxidxlist,bxvaluelist,bxerrolist)=perlsdata[9] maxlumi=0.0 if len(bxvaluelist)!=0: maxlumi=max(bxvaluelist) avginstlumi=0.0 if len(bxvaluelist)!=0: avginstlumi=sum(bxvaluelist) (intbxidxlist,b1intensities,b2intensities)=perlsdata[10]#contains only non-zero bx for bxidx in bxidxlist: idx=bxidxlist.index(bxidx) bxvalue=bxvaluelist[idx] bxerror=bxerrolist[idx] if bxvalue<max(xingMinLum,maxlumi*0.2): continue bintensityPos=-1 try: bintensityPos=intbxidxlist.index(bxidx) except ValueError: pass if bintensityPos<=0: fillbypos.setdefault(bxidx,[]).append([ts,beamstatusfrac,bxvalue,bxerror,0.0,0.0]) continue b1intensity=b1intensities[bintensityPos] b2intensity=b2intensities[bintensityPos] speclumi=calculateSpecificLumi(bxvalue,bxerror,b1intensity,0.0,b2intensity,0.0) fillbypos.setdefault(bxidx,[]).append([ts,beamstatusfrac,bxvalue,bxerror,speclumi[0],speclumi[1]]) return fillbypos
os.environ['CORAL_AUTH_PATH'] = options.authpath svc=sessionManager.sessionManager(options.connect,authpath=options.authpath,debugON=options.debug) session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')]) qResult=coral.AttributeList() qResult.extend('timestr','string') qResult.extend('DELIVERED','float') session.transaction().start(True) lumiquery=session.nominalSchema().newQuery() lumiquery.addToTableList('HFLUMIRESULT') qCondition=coral.AttributeList() qCondition.extend('begintime','time stamp') qCondition.extend('endtime','time stamp') lumiquery.addToOutputList('TO_CHAR(TIME,\'MM/DD/YY HH24:MI:SS\')','timestr') lumiquery.addToOutputList('DELIVERED') lumiquery.defineOutput(qResult) lute=lumiTime.lumiTime() begtimeStr='01/01/12 00:00:00' reqtimemaxT=datetime.datetime.now() print options.begin,options.end if options.begin: begtimeStr=options.begin reqtimeminT=lute.StrToDatetime(options.begin,customfm='%m/%d/%y %H:%M:%S') if options.end: reqtimemaxT=lute.StrToDatetime(options.end,customfm='%m/%d/%y %H:%M:%S') qCondition['begintime'].setData(coral.TimeStamp(reqtimeminT.year,reqtimeminT.month,reqtimeminT.day,reqtimeminT.hour,reqtimeminT.minute,reqtimeminT.second,0)) qCondition['endtime'].setData(coral.TimeStamp(reqtimemaxT.year,reqtimemaxT.month,reqtimemaxT.day,reqtimemaxT.hour,reqtimemaxT.minute,reqtimemaxT.second,0)) lumiquery.setCondition('TIME>=:begintime AND TIME<=:endtime',qCondition) cursor=lumiquery.execute() result={}#{ordinalnumber:delivered} while next(cursor): timeStr=cursor.currentRow()['timestr'].data()
def main(): allowedscales=['linear','log','both'] c=constants() parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="Plot integrated luminosity as function of the time variable of choice",formatter_class=argparse.ArgumentDefaultsHelpFormatter) # add required arguments parser.add_argument('-c',dest='connect',action='store',required=True,help='connect string to lumiDB') # add optional arguments parser.add_argument('-P',dest='authpath',action='store',help='path to authentication file') parser.add_argument('-n',dest='normfactor',action='store',help='normalization factor (optional, default to 1.0)') parser.add_argument('-i',dest='inputfile',action='store',help='lumi range selection file (optional)') parser.add_argument('-o',dest='outputfile',action='store',help='csv outputfile name (optional)') parser.add_argument('-lumiversion',dest='lumiversion',default='0001',action='store',required=False,help='lumi data version') parser.add_argument('-begin',dest='begin',action='store',help='begin value of x-axi (required)') parser.add_argument('-end',dest='end',action='store',help='end value of x-axi (optional). Default to the maximum exists DB') parser.add_argument('-beamenergy',dest='beamenergy',action='store',type=float,required=False,help='beamenergy (in GeV) selection criteria,e.g. 3.5e3') parser.add_argument('-beamfluctuation',dest='beamfluctuation',action='store',type=float,required=False,help='allowed fraction of beamenergy to fluctuate, e.g. 0.1') parser.add_argument('-beamstatus',dest='beamstatus',action='store',required=False,help='selection criteria beam status,e.g. STABLE BEAMS') parser.add_argument('-yscale',dest='yscale',action='store',required=False,default='linear',help='y_scale') parser.add_argument('-hltpath',dest='hltpath',action='store',help='specific hltpath to calculate the recorded luminosity. If specified aoverlays the recorded luminosity for the hltpath on the plot') parser.add_argument('-batch',dest='batch',action='store',help='graphical mode to produce PNG file. Specify graphical file here. Default to lumiSum.png') parser.add_argument('--annotateboundary',dest='annotateboundary',action='store_true',help='annotate boundary run numbers') parser.add_argument('--interactive',dest='interactive',action='store_true',help='graphical mode to draw plot in a TK pannel.') parser.add_argument('-timeformat',dest='timeformat',action='store',help='specific python timeformat string (optional). Default mm/dd/yy hh:min:ss.00') parser.add_argument('-siteconfpath',dest='siteconfpath',action='store',help='specific path to site-local-config.xml file, default to $CMS_PATH/SITECONF/local/JobConfig, if path undefined, fallback to cern proxy&server') parser.add_argument('action',choices=['run','fill','time','perday'],help='x-axis data type of choice') #graphical mode options parser.add_argument('--with-correction',dest='withFineCorrection',action='store_true',help='with fine correction') parser.add_argument('--verbose',dest='verbose',action='store_true',help='verbose mode, print result also to screen') parser.add_argument('--debug',dest='debug',action='store_true',help='debug') # parse arguments batchmode=True args=parser.parse_args() connectstring=args.connect begvalue=args.begin endvalue=args.end beamstatus=args.beamstatus beamenergy=args.beamenergy beamfluctuation=args.beamfluctuation xaxitype='run' connectparser=connectstrParser.connectstrParser(connectstring) connectparser.parse() usedefaultfrontierconfig=False cacheconfigpath='' if connectparser.needsitelocalinfo(): if not args.siteconfpath: cacheconfigpath=os.environ['CMS_PATH'] if cacheconfigpath: cacheconfigpath=os.path.join(cacheconfigpath,'SITECONF','local','JobConfig','site-local-config.xml') else: usedefaultfrontierconfig=True else: cacheconfigpath=args.siteconfpath cacheconfigpath=os.path.join(cacheconfigpath,'site-local-config.xml') p=cacheconfigParser.cacheconfigParser() if usedefaultfrontierconfig: p.parseString(c.defaultfrontierConfigString) else: p.parse(cacheconfigpath) connectstring=connectparser.fullfrontierStr(connectparser.schemaname(),p.parameterdict()) #print 'connectstring',connectstring runnumber=0 svc = coral.ConnectionService() hltpath='' if args.hltpath: hltpath=args.hltpath if args.debug : msg=coral.MessageStream('') msg.setMsgVerbosity(coral.message_Level_Debug) ifilename='' ofilename='integratedlumi.png' timeformat='' if args.authpath and len(args.authpath)!=0: os.environ['CORAL_AUTH_PATH']=args.authpath if args.normfactor: c.NORM=float(args.normfactor) if args.lumiversion: c.LUMIVERSION=args.lumiversion if args.verbose: c.VERBOSE=True if args.inputfile: ifilename=args.inputfile if args.batch: opicname=args.batch if args.outputfile: ofilename=args.outputfile if args.timeformat: timeformat=args.timeformat session=svc.connect(connectstring,accessMode=coral.access_Update) session.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)") session.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)") inputfilecontent='' fileparsingResult='' runList=[] runDict={} fillDict={} selectionDict={} minTime='' maxTime='' startRunTime='' stopRunTime='' #if len(ifilename)!=0 : # f=open(ifilename,'r') # inputfilecontent=f.read() # sparser=selectionParser.selectionParser(inputfilecontent) # runsandls=sparser.runsandls() # keylist=runsandls.keys() # keylist.sort() # for run in keylist: # if selectionDict.has_key(run): # lslist=runsandls[run] # lslist.sort() # selectionDict[run]=lslist if len(ifilename)!=0: ifparser=inputFilesetParser.inputFilesetParser(ifilename) runsandls=ifparser.runsandls() keylist=runsandls.keys() keylist.sort() for run in keylist: if not selectionDict.has_key(run): lslist=runsandls[run] lslist.sort() selectionDict[run]=lslist if args.action == 'run': if not args.end: session.transaction().start(True) schema=session.nominalSchema() lastrun=max(lumiQueryAPI.allruns(schema,requireRunsummary=True,requireLumisummary=True,requireTrg=True,requireHlt=False)) session.transaction().commit() else: lastrun=int(args.end) for r in range(int(args.begin),lastrun+1): runList.append(r) runList.sort() elif args.action == 'fill': session.transaction().start(True) maxfill=None if not args.end: qHandle=session.nominalSchema().newQuery() maxfill=max(lumiQueryAPI.allfills(qHandle,filtercrazy=True)) del qHandle else: maxfill=int(args.end) qHandle=session.nominalSchema().newQuery() fillDict=lumiQueryAPI.runsByfillrange(qHandle,int(args.begin),maxfill) del qHandle session.transaction().commit() #print 'fillDict ',fillDict for fill in range(int(args.begin),maxfill+1): if fillDict.has_key(fill): #fill exists for run in fillDict[fill]: runList.append(run) runList.sort() elif args.action == 'time' or args.action == 'perday': session.transaction().start(True) t=lumiTime.lumiTime() minTime=t.StrToDatetime(args.begin,timeformat) if not args.end: maxTime=datetime.datetime.utcnow() #to now else: maxTime=t.StrToDatetime(args.end,timeformat) #print minTime,maxTime qHandle=session.nominalSchema().newQuery() runDict=lumiQueryAPI.runsByTimerange(qHandle,minTime,maxTime)#xrawdata session.transaction().commit() runList=runDict.keys() del qHandle runList.sort() if len(runList)!=0: runmin=min(runList) runmax=max(runList) startRunTime=runDict[runmin][0] stopRunTime=runDict[runmax][1] #print 'run list: ',runDict else: print 'unsupported action ',args.action exit finecorrections=None if args.withFineCorrection: schema=session.nominalSchema() session.transaction().start(True) finecorrections=lumiCorrections.correctionsForRange(schema,runList) session.transaction().commit() #print 'runList ',runList #print 'runDict ', runDict fig=Figure(figsize=(7.2,5.4),dpi=120) m=matplotRender.matplotRender(fig) logfig=Figure(figsize=(7.5,5.7),dpi=135) mlog=matplotRender.matplotRender(logfig) if args.action == 'run': result={} result=getLumiInfoForRuns(session,c,runList,selectionDict,hltpath,beamstatus=beamstatus,beamenergy=beamenergy,beamfluctuation=beamfluctuation,finecorrections=finecorrections) xdata=[] ydata={} ydata['Delivered']=[] ydata['Recorded']=[] keylist=result.keys() keylist.sort() #must be sorted in order if args.outputfile: reporter=csvReporter.csvReporter(ofilename) fieldnames=['run','delivered','recorded'] reporter.writeRow(fieldnames) for run in keylist: xdata.append(run) delivered=result[run][0] recorded=result[run][1] ydata['Delivered'].append(delivered) ydata['Recorded'].append(recorded) if args.outputfile and (delivered!=0 or recorded!=0): reporter.writeRow([run,result[run][0],result[run][1]]) m.plotSumX_Run(xdata,ydata,yscale='linear') mlog.plotSumX_Run(xdata,ydata,yscale='log') elif args.action == 'fill': lumiDict={} lumiDict=getLumiInfoForRuns(session,c,runList,selectionDict,hltpath,beamstatus=beamstatus,beamenergy=beamenergy,beamfluctuation=beamfluctuation,finecorrections=finecorrections) xdata=[] ydata={} ydata['Delivered']=[] ydata['Recorded']=[] #keylist=lumiDict.keys() #keylist.sort() if args.outputfile: reporter=csvReporter.csvReporter(ofilename) fieldnames=['fill','run','delivered','recorded'] reporter.writeRow(fieldnames) fills=fillDict.keys() fills.sort() for fill in fills: runs=fillDict[fill] runs.sort() for run in runs: xdata.append(run) ydata['Delivered'].append(lumiDict[run][0]) ydata['Recorded'].append(lumiDict[run][1]) if args.outputfile : reporter.writeRow([fill,run,lumiDict[run][0],lumiDict[run][1]]) #print 'input fillDict ',len(fillDict.keys()),fillDict m.plotSumX_Fill(xdata,ydata,fillDict,yscale='linear') mlog.plotSumX_Fill(xdata,ydata,fillDict,yscale='log') elif args.action == 'time' : lumiDict={} lumiDict=getLumiInfoForRuns(session,c,runList,selectionDict,hltpath,beamstatus=beamstatus,beamenergy=beamenergy,beamfluctuation=beamfluctuation,finecorrections=finecorrections) #lumiDict=getLumiInfoForRuns(session,c,runList,selectionDict,hltpath,beamstatus='STABLE BEAMS') xdata={}#{run:[starttime,stoptime]} ydata={} ydata['Delivered']=[] ydata['Recorded']=[] keylist=lumiDict.keys() keylist.sort() if args.outputfile: reporter=csvReporter.csvReporter(ofilename) fieldnames=['run','starttime','stoptime','delivered','recorded'] reporter.writeRow(fieldnames) for run in keylist: ydata['Delivered'].append(lumiDict[run][0]) ydata['Recorded'].append(lumiDict[run][1]) starttime=runDict[run][0] stoptime=runDict[run][1] xdata[run]=[starttime,stoptime] if args.outputfile : reporter.writeRow([run,starttime,stoptime,lumiDict[run][0],lumiDict[run][1]]) m.plotSumX_Time(xdata,ydata,startRunTime,stopRunTime,hltpath=hltpath,annotateBoundaryRunnum=args.annotateboundary,yscale='linear') mlog.plotSumX_Time(xdata,ydata,startRunTime,stopRunTime,hltpath=hltpath,annotateBoundaryRunnum=args.annotateboundary,yscale='log') elif args.action == 'perday': daydict={}#{day:[[run,cmslsnum,lsstarttime,delivered,recorded]]} lumibyls=getLumiOrderByLS(session,c,runList,selectionDict,hltpath,beamstatus=beamstatus,beamenergy=beamenergy,beamfluctuation=beamfluctuation,finecorrections=finecorrections) #lumibyls [[runnumber,runstarttime,lsnum,lsstarttime,delivered,recorded,recordedinpath]] if args.outputfile: reporter=csvReporter.csvReporter(ofilename) fieldnames=['day','begrunls','endrunls','delivered','recorded'] reporter.writeRow(fieldnames) beginfo=[lumibyls[0][3],str(lumibyls[0][0])+':'+str(lumibyls[0][2])] endinfo=[lumibyls[-1][3],str(lumibyls[-1][0])+':'+str(lumibyls[-1][2])] for perlsdata in lumibyls: lsstarttime=perlsdata[3] delivered=perlsdata[4] recorded=perlsdata[5] day=lsstarttime.toordinal() if not daydict.has_key(day): daydict[day]=[] daydict[day].append([delivered,recorded]) days=daydict.keys() days.sort() daymin=days[0] daymax=days[-1] #alldays=range(daymin,daymax+1) resultbyday={} resultbyday['Delivered']=[] resultbyday['Recorded']=[] #for day in days: #print 'day min ',daymin #print 'day max ',daymax for day in range(daymin,daymax+1): if not daydict.has_key(day): delivered=0.0 recorded=0.0 else: daydata=daydict[day] mytransposed=CommonUtil.transposed(daydata,defaultval=0.0) delivered=sum(mytransposed[0]) recorded=sum(mytransposed[1]) resultbyday['Delivered'].append(delivered) resultbyday['Recorded'].append(recorded) if args.outputfile: reporter.writeRow([day,beginfo[1],endinfo[1],delivered,recorded]) #print 'beginfo ',beginfo #print 'endinfo ',endinfo #print resultbyday m.plotPerdayX_Time( range(daymin,daymax+1) ,resultbyday,startRunTime,stopRunTime,boundaryInfo=[beginfo,endinfo],annotateBoundaryRunnum=args.annotateboundary,yscale='linear') mlog.plotPerdayX_Time( range(daymin,daymax+1),resultbyday,startRunTime,stopRunTime,boundaryInfo=[beginfo,endinfo],annotateBoundaryRunnum=args.annotateboundary,yscale='log') else: raise Exception,'must specify the type of x-axi' del session del svc if args.batch and args.yscale=='linear': m.drawPNG(args.batch) elif args.batch and args.yscale=='log': mlog.drawPNG(args.batch) elif args.batch and args.yscale=='both': m.drawPNG(args.batch) basename,extension=os.path.splitext(args.batch) logfilename=basename+'_log'+extension mlog.drawPNG(logfilename) else: if not args.interactive: return if args.interactive is True and args.yscale=='linear': m.drawInteractive() elif args.interactive is True and args.yscale=='log': mlog.drawInteractive() else: raise Exception('unsupported yscale for interactive mode : '+args.yscale)
if options.fillnum: reqfillmin=options.fillnum reqfillmax=options.fillnum if options.begin: (runbeg,fillbeg,timebeg)=CommonUtil.parseTime(options.begin) if runbeg: #there's --begin runnum #priority run,fill,time if not reqrunmin:# there's no -r, then take this reqrunmin=runbeg elif fillbeg: if not reqfillmin: reqfillmin=fillbeg elif timebeg: reqtimemin=timebeg if reqtimemin: lute=lumiTime.lumiTime() reqtimeminT=lute.StrToDatetime(reqtimemin,customfm='%m/%d/%y %H:%M:%S') timeFilter[0]=reqtimeminT if options.end: (runend,fillend,timeend)=CommonUtil.parseTime(options.end) if runend: if not reqrunmax:#priority run,fill,time reqrunmax=runend elif fillend: if not reqfillmax: reqfillmax=fillend elif timeend: reqtimemax=timeend if reqtimemax: lute=lumiTime.lumiTime() reqtimemaxT=lute.StrToDatetime(reqtimemax,customfm='%m/%d/%y %H:%M:%S')
def instLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=None,timeFilter=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF'): ''' FROM ROOT FILE NO CORRECTION AT ALL input: irunlsdict: {run:[cmsls]} dataidmap: {run:(lumiid,trgid,hltid)} runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]} beamstatus: LS filter on beamstatus (optional) timeFilter: (minLSBegTime,maxLSBegTime) withBXInfo: get per bunch info (optional) bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET'] xingMinLum: None means apply no cut withBeamIntensity: get beam intensity info (optional) lumitype: luminosity measurement source output: result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),instlumi(5),instlumierr(6),startorbit(7),numorbit(8),(bxidx,bxvalues,bxerrs)(9),(bxidx,b1intensities,b2intensities)(10),fillnum(11)]}} special meanings: {run:None} None means selected run not in lumiDB, {run:[]} [] means no lumi data for this run in lumiDB {run:cmslsnum(1)==0} means either not cmslsnum or iscms but not selected instlumi unit in Hz/ub ''' if lumitype not in ['HF','PIXEL']: raise ValueError('unknown lumitype '+lumitype) lumitableName='' lumilstableName='' if lumitype=='HF': lumitableName=nameDealer.lumidataTableName() lumilstableName=nameDealer.lumisummaryv2TableName() else: lumitableName=nameDealer.pixellumidataTableName() lumilstableName=nameDealer.pixellumisummaryv2TableName() result={} for run in irunlsdict.keys(): #for run,(lumidataid,trgid,hltid ) in dataidmap.items(): lslist=irunlsdict[run] if lslist is not None and len(lslist)==0: result[run]=[]#no lumi data for this run in lumiDB continue fillnum=runsummaryMap[run][4] runstarttimeStr=runsummaryMap[run][6] if run not in dataidmap: result[run]=[]#no lumi data for this run in lumiDB continue (lumidataid,trgid,hltid )=dataidmap[run] if lumidataid is None: #selected run not in lumiDB result[run]=None continue (lumirunnum,perlsresult)=dataDML.lumiLSById(schema,lumidataid,beamstatus=beamstatusfilter,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName) lsresult=[] c=lumiTime.lumiTime() for lumilsnum in perlsresult.keys(): perlsdata=perlsresult[lumilsnum] cmslsnum=perlsdata[0] if lslist is not None and cmslsnum not in lslist: #ls exists but not selected cmslsnum=0 numorbit=perlsdata[6] startorbit=perlsdata[7] orbittime=c.OrbitToTime(runstarttimeStr,startorbit,begorbit=0,customfm='%m/%d/%y %H:%M:%S') if timeFilter: if timeFilter[0]: if orbittime<timeFilter[0]: continue if timeFilter[1]: if orbittime>timeFilter[1]: continue if lumitype=='HF': instlumi=perlsdata[1]*1000.0 #HF db avg values are in Hz/mb,change it to Hz/ub instlumierr=perlsdata[2]*1000.0 else: instlumi=perlsdata[1] #PIXEL avg values are in Hz/ub, need no conversion instlumierr=perlsdata[2] beamstatus=perlsdata[4] beamenergy=perlsdata[5] bxidxlist=[] bxvaluelist=[] bxerrorlist=[] bxdata=None beamdata=None if withBXInfo: bxinfo=perlsdata[8] bxvalueArray=None bxerrArray=None if bxinfo: bxvalueArray=bxinfo[0] bxerrArray=bxinfo[1] if xingMinLum : for idx,bxval in enumerate(bxvalueArray): if bxval>xingMinLum: bxidxlist.append(idx) bxvaluelist.append(bxval) bxerrorlist.append(bxerrArray[idx]) else: bxidxlist=range(0,len(bxvalueArray)) bxvaluelist=bxvalueArray.tolist() bxerrorlist=bxerrArray.tolist() del bxvalueArray[:] del bxerrArray[:] bxdata=(bxidxlist,bxvaluelist,bxerrorlist) if withBeamIntensity: beaminfo=perlsdata[9] bxindexlist=[] b1intensitylist=[] b2intensitylist=[] if beaminfo[0] and beaminfo[1] and beaminfo[2]: bxindexarray=beaminfo[0] beam1intensityarray=beaminfo[1] beam2intensityarray=beaminfo[2] bxindexlist=bxindexarray.tolist() b1intensitylist=beam1intensityarray.tolist() b2intensitylist=beam2intensityarray.tolist() del bxindexarray[:] del beam1intensityarray[:] del beam2intensityarray[:] beamdata=(bxindexlist,b1intensitylist,b2intensitylist) lsresult.append([lumilsnum,cmslsnum,orbittime,beamstatus,beamenergy,instlumi,instlumierr,startorbit,numorbit,bxdata,beamdata,fillnum]) del perlsdata[:] result[run]=lsresult return result
outplotfilename = options.outplot if not outplotfilename: outplotfilename=actiontofilebasemap[options.action] outtextfilename = outplotfilename+'.csv' if options.withoutTextoutput: outtextfilename=None lumip=lumiParameters.ParametersObject() svc=sessionManager.sessionManager(options.connect, authpath=options.authpath, siteconfpath=options.siteconfpath, debugON=options.debug) session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')]) lslength=lumip.lslengthsec() begtime=options.begintime endtime=options.endtime lut=lumiTime.lumiTime() if not endtime: endtime=lut.DatetimeToStr(datetime.datetime.utcnow(),customfm='%m/%d/%y %H:%M:%S') pbeammode=None normfactor=options.norm if options.beamstatus=='stable': pbeammode='STABLE BEAMS' resultlines=[] inplots=[] # ##process old plot csv files,if any, skipping #commentlines # inplot=options.inplot if inplot: inplots=inplot.split('+') for ip in inplots:
def getLumiOrderByLS(dbsession,c,runList,selectionDict,hltpath='',beamstatus=None,beamenergy=None,beamfluctuation=None,finecorrections=None): ''' input: runList[runnum], selectionDict{runnum:[ls]} output: [[runnumber,runstarttime,lsnum,lsstarttime,delivered,recorded,recordedinpath]] ''' #print 'getLumiOrderByLS selectionDict seen ',selectionDict t=lumiTime.lumiTime() result=[]#[[runnumber,runstarttime,lsnum,lsstarttime,delivered,recorded]] dbsession.transaction().start(True) sortedresult=[] #print 'runlist ',runList for runnum in runList: delivered=0.0 recorded=0.0 #print 'looking for run ',runnum q=dbsession.nominalSchema().newQuery() runsummary=lumiQueryAPI.runsummaryByrun(q,runnum) del q runstarttimeStr=runsummary[3] if len(runstarttimeStr)==0: if c.VERBOSE: print 'warning request run ',runnum,' has no runsummary, skip' continue if len(selectionDict)!=0 and not selectionDict.has_key(runnum): if runnum<max(selectionDict.keys()): result.append([runnum,runstarttimeStr,1,t.StrToDatetime(runstarttimeStr),0.0,0.0]) continue #print 'runsummary ',runsummary lumitrginfo={} q=dbsession.nominalSchema().newQuery() if finecorrections and finecorrections[runnum]: lumitrginfo=lumiQueryAPI.lumisummarytrgbitzeroByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation,finecorrections=finecorrections[runnum]) #q2 else: lumitrginfo=lumiQueryAPI.lumisummarytrgbitzeroByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation) #q2 del q #print 'lumitrginfo ',lumitrginfo if len(lumitrginfo)==0: #if no qualified cross lumi-trg found, try lumionly #result.append([runnum,runstarttimeStr,1,t.StrToDatetime(runstarttimeStr),0.0,0.0]) q=dbsession.nominalSchema().newQuery() if finecorrections and finecorrections[runnum]: lumiinfobyrun=lumiQueryAPI.lumisummaryByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation,finecorrections=finecorrections[runnum]) #q3 else: lumiinfobyrun=lumiQueryAPI.lumisummaryByrun(q,runnum,c.LUMIVERSION,beamstatus,beamenergy,beamfluctuation) del q if len(lumiinfobyrun)!=0: #if lumionly has qualified data means trg has no data print 'warning request run ',runnum,' has no trigger data, calculate delivered only' for perlsdata in lumiinfobyrun: cmslsnum=perlsdata[0] instlumi=perlsdata[1] norbit=perlsdata[2] startorbit=perlsdata[3] lsstarttime=t.OrbitToTime(runstarttimeStr,startorbit) lslength=t.bunchspace_s*t.nbx*norbit delivered=instlumi*lslength result.append([runnum,runstarttimeStr,cmslsnum,lsstarttime,delivered,0.0]) else: #print 'run '+str(runnum)+' has no qualified data ' lsstarttime=t.OrbitToTime(runstarttimeStr,0) result.append([runnum,runstarttimeStr,1,lsstarttime,0.0,0.0]) else: norbits=lumitrginfo.values()[0][1] lslength=t.bunchspace_s*t.nbx*norbits trgbitinfo={} for cmslsnum,valuelist in lumitrginfo.items(): instlumi=valuelist[0] startorbit=valuelist[2] bitzero=valuelist[5] deadcount=valuelist[6] prescale=valuelist[-1] lsstarttime=t.OrbitToTime(runstarttimeStr,startorbit) if len(selectionDict)!=0 and not (cmslsnum in selectionDict[runnum]): #if there's a selection list but cmslsnum is not selected,set to 0 result.append([runnum,runstarttimeStr,cmslsnum,lsstarttime,0.0,0.0]) continue delivered=instlumi*lslength if valuelist[5]==0:#bitzero==0 means no beam,do nothing recorded=0.0 else: deadfrac=float(deadcount)/float(float(bitzero)*float(prescale)) if(deadfrac<1.0): recorded=delivered*(1.0-deadfrac) result.append([runnum,runstarttimeStr,cmslsnum,lsstarttime,delivered,recorded]) #print 'result : ',result dbsession.transaction().commit() transposedResult=CommonUtil.transposed(result) lstimes=transposedResult[3] lstimes.sort() for idx,lstime in enumerate(lstimes): sortedresult.append(result[idx]) if c.VERBOSE: print sortedresult return sortedresult
def plotPeakPerday_Time(self,rawdata={},resultlines=[],minTime=None,maxTime=None,nticks=6,withannotation=False,yscale='linear',referenceLabel='Delivered',labels=['Delivered'],textoutput=None): ''' THIS PLOT IS DELIVERED ONLY Input: rawdata={'Delivered':[(day,run,ls,instlumi)]} resultlines=[[day,run,ls,maxinstlum],[]] minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S withannotation: wheather the boundary points should be annotated referenceLabel: the one variable that decides the total unit and the plot x-axis range labels: labels of the variables to plot ''' xpoints=[] ypoints={} legendlist=[] maxinfo='' ymax={} lut=lumiTime.lumiTime() if not minTime: minTime='03/01/10 00:00:00' minTime=lut.StrToDatetime(minTime,customfm='%m/%d/%y %H:%M:%S') if not maxTime: maxTime=datetime.datetime.utcnow() else: maxTime=lut.StrToDatetime(maxTime,customfm='%m/%d/%y %H:%M:%S') for r in resultlines: day=int(r[0]) runnumber=int(r[1]) lsnum=int(r[2].split('.')[0]) if rawdata and day in [int(t[0]) for t in rawdata[referenceLabel]]:continue if day < minTime.date().toordinal():continue if day > maxTime.date().toordinal():continue for i,lab in enumerate(labels): v=float(r[-(len(labels)-i)-1]) rawdata.setdefault(lab,[]).append((day,runnumber,lsnum,v)) if not rawdata: print '[WARNING]: no data, do nothing' return maxlum=max([t[3] for t in rawdata[referenceLabel]]) minlum=min([t[3] for t in rawdata[referenceLabel] if t[3]>0]) #used only for log scale, fin the non-zero bottom (unitstring,denomitor)=guessInstLumiUnit(maxlum) csvreport=None rows=[] flat=[] MinDay=minTime.date().toordinal() MaxDay=maxTime.date().toordinal() fulldays=range(MinDay,MaxDay+1) for label in rawdata.keys(): yvalues=rawdata[label] yvalues.sort()#sort by day alldays=[t[0] for t in yvalues] alldates=[str(datetime.date.fromordinal(t)) for t in alldays] ypoints[label]=[] lumivals=[t[3] for t in yvalues] flat.append(lumivals) for d in fulldays: if not d in alldays: ypoints[label].append(0.0) else: thisdaylumi=[t[3] for t in yvalues if t[0]==d][0] if yscale=='log': if thisdaylumi<minlum: thisdaylumi=minlum/denomitor else: thisdaylumi=thisdaylumi/denomitor else: thisdaylumi=thisdaylumi/denomitor ypoints[label].append(thisdaylumi) ymax[label]=max(lumivals)/denomitor 'ymax ',max(lumivals) xpoints=fulldays if textoutput: csvreport=csvReporter.csvReporter(textoutput) head=['#day','run','lsnum','maxinstlumi','date'] csvreport.writeRow(head) flat.insert(0,alldays) allruns=[ t[1] for t in rawdata[referenceLabel]] allls=[ t[2] for t in rawdata[referenceLabel]] flat.insert(1,allruns) flat.insert(2,allls) flat.append(alldates) rows=zip(*flat) csvreport.writeRows([list(t) for t in rows]) yearStrMin=minTime.strftime('%Y') yearStrMax=maxTime.strftime('%Y') if yearStrMin==yearStrMax: dateFmt=matplotlib.dates.DateFormatter('%d/%m') else: dateFmt=matplotlib.dates.DateFormatter('%d/%m/%y') ax=self.__fig.add_subplot(111) if yscale=='linear': ax.set_yscale('linear') elif yscale=='log': ax.set_yscale('log') else: raise 'unsupported yscale ',yscale majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks) minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4) ax.xaxis.set_major_formatter(dateFmt) ax.set_xlabel(r'Date',position=(0.84,0)) ax.set_ylabel(r'L '+unitstring,position=(0,0.9)) ax.xaxis.set_major_locator(majorLoc) ax.xaxis.set_minor_locator(minorLoc) xticklabels=ax.get_xticklabels() for tx in xticklabels: tx.set_horizontalalignment('right') ax.grid(True) cl=self.colormap['Max Inst'] textsummaryhead=['#TotalRunningDays'] textsummaryline=['#'+str(len(alldays))] for ylabel in labels: cl='k' if self.colormap.has_key(ylabel): cl=self.colormap[ylabel] ax.plot(xpoints,ypoints[ylabel],label='Max Inst',color=cl,drawstyle='steps') legendlist.append('Max Inst %.3f'%(ymax[ylabel])+' '+unitstring) textsummaryhead.append('Max Inst'+ylabel) textsummaryline.append('%.3f'%(ymax[ylabel])+' '+unitstring) if textoutput: csvreport.writeRow(textsummaryhead) csvreport.writeRow(textsummaryline) ax.legend(tuple(legendlist),loc='upper left') ax.set_xbound(lower=matplotlib.dates.date2num(minTime),upper=matplotlib.dates.date2num(maxTime)) if withannotation: #annotations trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes) ax.text(xpoints[0],1.025,beginfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white')) ax.text(xpoints[-1],1.025,endinfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white')) ax.annotate(maxinfo,xy=(xmax,ymax),xycoords='data',xytext=(0,13),textcoords='offset points',arrowprops=dict(facecolor='green',shrink=0.05),size='x-small',horizontalalignment='center',color='green',bbox=dict(facecolor='white')) firstday=datetime.date.fromordinal(rawdata[referenceLabel][0][0]) lastday=datetime.date.fromordinal(rawdata[referenceLabel][-1][0]) firstdayStr=firstday.strftime('%Y %b %d') lastdayStr=lastday.strftime('%Y %b %d') ax.set_title('CMS Peak Luminosity/Day ('+firstdayStr+' - '+lastdayStr+')',size='small') #ax.autoscale(tight=True) ax.autoscale_view(tight=True,scalex=True,scaley=False) #ax.set_xmargin(0.015) self.__fig.autofmt_xdate(bottom=0.18,rotation=15,ha='right') self.__fig.subplots_adjust(bottom=0.2,left=0.15)
def plotSumX_Time(self, rawdata={}, resultlines=[], minTime=None, maxTime=None, nticks=6, yscale='linear', withannotation=False, referenceLabel='Delivered', labels=['Delivered', 'Recorded'], textoutput=None): ''' input: rawdata = {'Delivered':[(runnumber,starttimestamp,stoptimestamp,lumiperrun)],'Recorded':[(runnumber,starttimestamp,stoptimestamp,lumiperrun)]} resultlines = [[runnumber,starttimestampStr,stoptimestampStr,dellumiperrun,reclumiperrun],[runnumber,starttimestampStr,stoptimestampStr,dellumiperrun,reclumiperrun],] minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S yscale: linear,log or both withannotation: wheather the boundary points should be annotated referenceLabel: the one variable that decides the total unit and the plot x-axis range labels: labels of the variables to plot ''' xpoints = [] ypoints = {} ytotal = {} lut = lumiTime.lumiTime() if not minTime: minTime = '03/01/10 00:00:00' minTime = lut.StrToDatetime(minTime, customfm='%m/%d/%y %H:%M:%S') if not maxTime: maxTime = datetime.datetime.utcnow() else: maxTime = lut.StrToDatetime(maxTime, customfm='%m/%d/%y %H:%M:%S') for r in resultlines: runnumber = int(r[0]) starttimeStr = r[1].split('.')[0] starttime = lut.StrToDatetime(starttimeStr, customfm='%Y-%m-%d %H:%M:%S') stoptimeStr = r[2].split('.')[0] stoptime = lut.StrToDatetime(stoptimeStr, customfm='%Y-%m-%d %H:%M:%S') if rawdata and runnumber in [ t[0] for t in rawdata[referenceLabel] ]: continue if starttime < minTime: continue if starttime > maxTime: continue for i, lab in enumerate(labels): v = float(r[-(len(labels) - i)]) rawdata.setdefault(lab, []).append( (runnumber, starttime, stoptime, v)) if not rawdata: print('[WARNING]: no data, do nothing') return tot = sum([t[3] for t in rawdata[referenceLabel]]) (unitstring, denomitor) = guessLumiUnit(tot) csvreport = None rows = [] flat = [] for label, yvalues in rawdata.items(): yvalues.sort() flat.append([t[3] for t in yvalues]) if label == referenceLabel: minTime = yvalues[0][1] maxTime = yvalues[-1][1] ypoints[label] = [] lumivals = [t[3] for t in yvalues] for i, val in enumerate(lumivals): ypoints[label].append(sum(lumivals[0:i + 1]) / denomitor) ytotal[label] = sum(lumivals) / denomitor xpoints = [ matplotlib.dates.date2num(t[1]) for t in rawdata[referenceLabel] ] ax = self.__fig.add_subplot(111) ax.set_yscale(yscale) yearStrMin = minTime.strftime('%Y') yearStrMax = maxTime.strftime('%Y') if yearStrMin == yearStrMax: dateFmt = matplotlib.dates.DateFormatter('%d/%m') else: dateFmt = matplotlib.dates.DateFormatter('%d/%m/%y') majorLoc = matplotlib.ticker.LinearLocator(numticks=nticks) ax.xaxis.set_major_locator(majorLoc) minorLoc = matplotlib.ticker.LinearLocator(numticks=nticks * 4) ax.xaxis.set_major_formatter(dateFmt) ax.set_xlabel(r'Date', position=(0.84, 0)) ax.set_ylabel(r'L ' + unitstring, position=(0, 0.9)) ax.xaxis.set_minor_locator(minorLoc) ax.set_xbound(lower=xpoints[0], upper=xpoints[-1]) xticklabels = ax.get_xticklabels() for tx in xticklabels: tx.set_horizontalalignment('left') ax.grid(True) keylist = sorted(ypoints.keys()) keylist.insert(0, keylist.pop(keylist.index( referenceLabel))) #move refereceLabel to front from now on legendlist = [] head = ['#Run', 'StartTime', 'StopTime'] textsummaryhead = ['#TotalRun'] textsummaryline = ['#' + str(len(xpoints))] for ylabel in keylist: cl = 'k' if ylabel in self.colormap: cl = self.colormap[ylabel] ax.plot(xpoints, ypoints[ylabel], label=ylabel, color=cl, drawstyle='steps') legendlist.append(ylabel + ' ' + '%.3f' % (ytotal[ylabel]) + ' ' + unitstring) textsummaryhead.append('Total' + ylabel) textsummaryline.append('%.3f' % (ytotal[ylabel]) + ' ' + unitstring) head.append(ylabel) if textoutput: csvreport = csvReporter.csvReporter(textoutput) csvreport.writeRow(head) allruns = [int(t[0]) for t in rawdata[referenceLabel]] allstarts = [ lut.DatetimeToStr(t[1], customfm='%Y-%m-%d %H:%M:%S') for t in rawdata[referenceLabel] ] allstops = [ lut.DatetimeToStr(t[2], customfm='%Y-%m-%d %H:%M:%S') for t in rawdata[referenceLabel] ] flat.insert(0, allruns) flat.insert(1, allstarts) flat.insert(2, allstops) rows = list(zip(*flat)) csvreport.writeRows([list(t) for t in rows]) csvreport.writeRow(textsummaryhead) csvreport.writeRow(textsummaryline) #annotations trans = matplotlib.transforms.BlendedGenericTransform( ax.transData, ax.transAxes) #print 'run boundary ',runs[0],runs[-1] #print 'xpoints boundary ',xpoints[0],xpoints[-1] #annotation if withannotation: runs = [t[0] for t in rawdata[referenceLabel]] ax.text(xpoints[0], 1.025, str(runs[0]), transform=trans, horizontalalignment='left', size='x-small', color='green', bbox=dict(facecolor='white')) ax.text(xpoints[-1], 1.025, str(runs[-1]), transform=trans, horizontalalignment='left', size='x-small', color='green', bbox=dict(facecolor='white')) if yearStrMin == yearStrMax: firsttimeStr = rawdata[referenceLabel][1][1].strftime( '%b %d %H:%M' ) #time range(start) in the title is the first run beg time lasttimeStr = rawdata[referenceLabel][-1][2].strftime( '%b %d %H:%M' ) #time range(stop) in the tile is the last run stop time #firstimeStr=minTime.strftime('%b %d %H:%M') #lasttimeStr=maxTime.strftime('%b %d %H:%M') #ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+' ('+firstimeStr+' - '+lasttimeStr+' UTC)',size='small',family='fantasy') ax.set_title('CMS Total Integrated Luminosity ' + yearStrMin + ' (' + firsttimeStr + ' - ' + lasttimeStr + ' UTC)', size='small') else: #ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+'-'+yearStrMax,size='small',family='fantasy') ax.set_title('CMS Total Integrated Luminosity ' + yearStrMin + '-' + yearStrMax, size='small') ax.legend(tuple(legendlist), loc='upper left') ax.autoscale_view(tight=True, scalex=True, scaley=False) self.__fig.autofmt_xdate(bottom=0.18, rotation=15, ha='right') self.__fig.subplots_adjust(bottom=0.2, left=0.15)
def main(): allowedscales=['linear','log','both'] c=constants() parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="Plot integrated luminosity as function of the time variable of choice") # add required arguments parser.add_argument('-c',dest='connect',action='store',required=True,help='connect string to lumiDB') # add optional arguments parser.add_argument('-P',dest='authpath',action='store',help='path to authentication file') parser.add_argument('-n',dest='normfactor',action='store',help='normalization factor (optional, default to 1.0)') parser.add_argument('-i',dest='inputfile',action='store',help='lumi range selection file (optional)') parser.add_argument('-o',dest='outputfile',action='store',help='csv outputfile name (optional)') parser.add_argument('-b',dest='beammode',action='store',help='beam mode, optional, no default') parser.add_argument('-lumiversion',dest='lumiversion',action='store',help='lumi data version, optional for all, default 0001') parser.add_argument('-begin',dest='begin',action='store',help='begin xvalue (required)') parser.add_argument('-end',dest='end',action='store',help='end xvalue(optional). Default to the maximum exists DB') parser.add_argument('-batch',dest='batch',action='store',help='graphical mode to produce PNG file. Specify graphical file here. Default to lumiSum.png') parser.add_argument('-yscale',dest='yscale',action='store',required=False,default='linear',help='y_scale') parser.add_argument('--interactive',dest='interactive',action='store_true',help='graphical mode to draw plot in a TK pannel.') parser.add_argument('-timeformat',dest='timeformat',action='store',help='specific python timeformat string (optional). Default mm/dd/yy hh:min:ss.00') parser.add_argument('-siteconfpath',dest='siteconfpath',action='store',help='specific path to site-local-config.xml file, default to $CMS_PATH/SITECONF/local/JobConfig, if path undefined, fallback to cern proxy&server') parser.add_argument('action',choices=['peakperday','run'],help='plot type of choice') #graphical mode options parser.add_argument('--annotateboundary',dest='annotateboundary',action='store_true',help='annotate boundary run numbers') parser.add_argument('--verbose',dest='verbose',action='store_true',help='verbose mode, print result also to screen') parser.add_argument('--with-correction',dest='withFineCorrection',action='store_true',help='with fine correction') parser.add_argument('--debug',dest='debug',action='store_true',help='debug') # parse arguments args=parser.parse_args() connectstring=args.connect connectparser=connectstrParser.connectstrParser(connectstring) connectparser.parse() usedefaultfrontierconfig=False cacheconfigpath='' if connectparser.needsitelocalinfo(): if not args.siteconfpath: cacheconfigpath=os.environ['CMS_PATH'] if cacheconfigpath: cacheconfigpath=os.path.join(cacheconfigpath,'SITECONF','local','JobConfig','site-local-config.xml') else: usedefaultfrontierconfig=True else: cacheconfigpath=args.siteconfpath cacheconfigpath=os.path.join(cacheconfigpath,'site-local-config.xml') p=cacheconfigParser.cacheconfigParser() if usedefaultfrontierconfig: p.parseString(c.defaultfrontierConfigString) else: p.parse(cacheconfigpath) connectstring=connectparser.fullfrontierStr(connectparser.schemaname(),p.parameterdict()) runnumber=0 svc = coral.ConnectionService() if args.debug : msg=coral.MessageStream('') msg.setMsgVerbosity(coral.message_Level_Debug) ifilename='' ofilename='instlumi.csv' beammode='stable' timeformat='' selectionDict={} if args.authpath and len(args.authpath)!=0: os.environ['CORAL_AUTH_PATH']=args.authpath if args.normfactor: c.NORM=float(args.normfactor) if args.lumiversion: c.LUMIVERSION=args.lumiversion if args.beammode: c.BEAMMODE=args.beammode if args.verbose: c.VERBOSE=True if args.inputfile: ifilename=args.inputfile if args.batch: opicname=args.batch if args.outputfile: ofilename=args.outputfile if args.timeformat: timeformat=args.timeformat session=svc.connect(connectstring,accessMode=coral.access_Update) session.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)") session.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)") startRunTime='' stopRunTime='' if ifilename: ifparser=inputFilesetParser(ifilename) runsandls=ifparser.runsandls() keylist=runsandls.keys() keylist.sort() for run in keylist: if selectionDict.has_key(run): lslist=runsandls[run] lslist.sort() selectionDict[run]=lslist if args.action == 'run': minRun=int(args.begin) if not args.end: maxRun=minRun else: maxRun=int(args.end) runList=range(minRun,maxRun+1) elif args.action == 'peakperday': session.transaction().start(True) t=lumiTime.lumiTime() minTime=t.StrToDatetime(args.begin,timeformat) if not args.end: maxTime=datetime.datetime.utcnow() #to now else: maxTime=t.StrToDatetime(args.end,timeformat) #print minTime,maxTime qHandle=session.nominalSchema().newQuery() runDict=lumiQueryAPI.runsByTimerange(qHandle,minTime,maxTime)#xrawdata session.transaction().commit() runList=runDict.keys() del qHandle runList.sort() if len(runList)!=0: runmin=min(runList) runmax=max(runList) startRunTime=runDict[runmin][0] stopRunTime=runDict[runmax][1] else: print 'unsupported action ',args.action exit #print 'runList ',runList #print 'runDict ', runDict finecorrections=None if args.withFineCorrection: schema=session.nominalSchema() session.transaction().start(True) finecorrections=lumiCorrections.correctionsForRange(schema,runList) session.transaction().commit() fig=Figure(figsize=(6,4.5),dpi=100) m=matplotRender.matplotRender(fig) logfig=Figure(figsize=(6,4.5),dpi=100) mlog=matplotRender.matplotRender(logfig) if args.action == 'peakperday': l=lumiTime.lumiTime() lumiperls=getInstLumiPerLS(session,c,runList,selectionDict,finecorrections=finecorrections) if args.outputfile: reporter=csvReporter.csvReporter(ofilename) fieldnames=['day','run','lsnum','maxinstlumi'] reporter.writeRow(fieldnames) #minDay=minTime.toordinal() #maxDay=maxTime.toordinal() daydict={}#{day:[[run,lsnum,instlumi]]} result={}#{day:[maxrun,maxlsnum,maxinstlumi]} for lsdata in lumiperls: runnumber=lsdata[0] lsnum=lsdata[1] runstarttimeStr=lsdata[-2]#note: it is a string!! startorbit=lsdata[5] deliveredInst=lsdata[2] lsstarttime=l.OrbitToTime(runstarttimeStr,startorbit) day=lsstarttime.toordinal() if not daydict.has_key(day): daydict[day]=[] daydict[day].append([runnumber,lsnum,deliveredInst]) days=daydict.keys() days.sort() for day in days: daydata=daydict[day] transposeddata=CommonUtil.transposed(daydata,defaultval=0.0) todaysmaxinst=max(transposeddata[2]) todaysmaxidx=transposeddata[2].index(todaysmaxinst) todaysmaxrun=transposeddata[0][todaysmaxidx] todaysmaxls=transposeddata[1][todaysmaxidx] result[day]=[todaysmaxrun,todaysmaxls,todaysmaxinst] if args.outputfile : reporter.writeRow([day,todaysmaxrun,todaysmaxls,todaysmaxinst]) m.plotPeakPerday_Time(result,startRunTime,stopRunTime,annotateBoundaryRunnum=args.annotateboundary,yscale='linear') mlog.plotPeakPerday_Time(result,startRunTime,stopRunTime,annotateBoundaryRunnum=args.annotateboundary,yscale='log') if args.action == 'run': runnumber=runList[0] if finecorrections and finecorrections[runnumber]: lumiperrun=getLumiPerRun(session,c,runnumber,finecorrections=finecorrections[runnumber])#[[lsnumber,deliveredInst,recordedInst,norbit,startorbit,fillnum,runstarttime,runstoptime]] else: lumiperrun=getLumiPerRun(session,c,runnumber) #print 'lumiperrun ',lumiperrun xdata=[]#[runnumber,fillnum,norbit,stattime,stoptime,totalls,ncmsls] ydata={}#{label:[instlumi]} ydata['Delivered']=[] ydata['Recorded']=[] norbit=lumiperrun[0][3] fillnum=lumiperrun[0][-3] starttime=lumiperrun[0][-2] stoptime=lumiperrun[0][-1] ncmsls=0 totalls=len(lumiperrun) for lsdata in lumiperrun: lsnumber=lsdata[0] if lsnumber!=0: ncmsls+=1 deliveredInst=lsdata[1] recordedInst=lsdata[2] ydata['Delivered'].append(deliveredInst) ydata['Recorded'].append(recordedInst) xdata=[runnumber,fillnum,norbit,starttime,stoptime,totalls,ncmsls] m.plotInst_RunLS(xdata,ydata) del session del svc if args.batch and args.yscale=='linear': m.drawPNG(args.batch) elif args.batch and args.yscale=='log': mlog.drawPNG(args.batch) elif args.batch and args.yscale=='both': m.drawPNG(args.batch) basename,extension=os.path.splitext(args.batch) logfilename=basename+'_log'+extension mlog.drawPNG(logfilename) else: raise Exception('unsupported yscale for batch mode : '+args.yscale) if not args.interactive: return if args.interactive is True and args.yscale=='linear': m.drawInteractive() elif args.interactive is True and args.yscale=='log': mlog.drawInteractive() else: raise Exception('unsupported yscale for interactive mode : '+args.yscale)
def getSpecificLumi(schema, fillnum, inputdir, dataidmap, normmap, xingMinLum=0.0, amodetag='PROTPHYS', bxAlgo='OCC1'): ''' specific lumi in 1e-30 (ub-1s-1) unit lumidetail occlumi in 1e-27 1309_lumi_401_CMS.txt time(in seconds since January 1,2011,00:00:00 UTC) stab(fraction of time spent in stable beams for this time bin) l(lumi in Hz/ub) dl(point-to-point error on lumi in Hz/ub) sl(specific lumi in Hz/ub) dsl(error on specific lumi) 20800119.0 1 -0.889948 0.00475996848729 0.249009 0.005583287562 -0.68359 6.24140208607 0.0 0.0 0.0 0.0 0.0 0.0 0.0383576 0.00430892097862 0.0479095 0.00430892097862 66.6447 4.41269758764 0.0 0.0 0.0 result [(time,beamstatusfrac,lumi,lumierror,speclumi,speclumierror)] ''' t = lumiTime.lumiTime() fillbypos = { } #{bxidx:[[ts,beamstatusfrac,lumi,lumierror,spec1,specerror],[]]} runtimesInFill = getFillFromDB(schema, fillnum) #{runnum:starttimestr} runlist = runtimesInFill.keys() if not runlist: return fillbypos irunlsdict = dict(zip(runlist, [None] * len(runlist))) #prirunlsdict GrunsummaryData = lumiCalcAPI.runsummaryMap(session.nominalSchema(), irunlsdict) lumidetails = lumiCalcAPI.deliveredLumiForIds(schema, irunlsdict, dataidmap, GrunsummaryData, beamstatusfilter=None, normmap=normmap, withBXInfo=True, bxAlgo=bxAlgo, xingMinLum=xingMinLum, withBeamIntensity=True, lumitype='HF') # #output: {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),calibratedlumierr(6),(bxvalues,bxerrs)(7),(bxidx,b1intensities,b2intensities)(8),fillnum(9)]} # totalstablebeamls = 0 orderedrunlist = sorted(lumidetails) for run in orderedrunlist: perrundata = lumidetails[run] for perlsdata in perrundata: beamstatus = perlsdata[3] if beamstatus == 'STABLE BEAMS': totalstablebeamls += 1 #print 'totalstablebeamls in fill ',totalstablebeamls if totalstablebeamls < 10: #less than 10 LS in a fill has 'stable beam', it's no a good fill print 'fill ', fillnum, ' , having less than 10 stable beam lS, is not good, skip' return fillbypos lumiparam = lumiParameters.ParametersObject() for run in orderedrunlist: perrundata = lumidetails[run] for perlsdata in perrundata: beamstatusfrac = 0.0 tsdatetime = perlsdata[2] ts = calendar.timegm(tsdatetime.utctimetuple()) beamstatus = perlsdata[3] if beamstatus == 'STABLE BEAMS': beamstatusfrac = 1.0 (bxidxlist, bxvaluelist, bxerrolist) = perlsdata[7] #instbxvaluelist=[x/lumiparam.lslengthsec() for x in bxvaluelist if x] instbxvaluelist = [x for x in bxvaluelist if x] maxlumi = 0.0 if len(instbxvaluelist) != 0: maxlumi = max(instbxvaluelist) avginstlumi = 0.0 if len(instbxvaluelist) != 0: avginstlumi = sum(instbxvaluelist) (intbxidxlist, b1intensities, b2intensities) = perlsdata[8] #contains only non-zero bx for bxidx in bxidxlist: idx = bxidxlist.index(bxidx) instbxvalue = bxvaluelist[idx] bxerror = bxerrolist[idx] if instbxvalue < max(xingMinLum, maxlumi * 0.2): continue bintensityPos = -1 try: bintensityPos = intbxidxlist.index(bxidx) except ValueError: pass if bintensityPos <= 0: fillbypos.setdefault(bxidx, []).append( [ts, beamstatusfrac, instbxvalue, bxerror, 0.0, 0.0]) continue b1intensity = b1intensities[bintensityPos] b2intensity = b2intensities[bintensityPos] speclumi = calculateSpecificLumi(instbxvalue, bxerror, b1intensity, 0.0, b2intensity, 0.0) fillbypos.setdefault(bxidx, []).append([ ts, beamstatusfrac, instbxvalue, bxerror, speclumi[0], speclumi[1] ]) return fillbypos