CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/RecoLuminosity/LumiDB/python/csvLumibyLSParser.py

Go to the documentation of this file.
00001 # Note: this is specifically to parse a .csv file generated from a command like
00002 # pixelLumiCalc.py lumibyls -i json_DCSONLY_pp.txt --hltpath "HLT_Photon75_CaloIdVL_IsoL*" -o myHLTtest.out
00003 # format: Run,LS,HLTpath,L1bit,HLTpresc,L1presc,Recorded(/ub),Effective(/ub)
00004 import csv
00005 import re
00006 def is_intstr(s):
00007     try:
00008         int(s)
00009         return True
00010     except ValueError:
00011         return False
00012 class csvLumibyLSParser(object):
00013     def __init__(self,filename):
00014         self.__result={}
00015         self.__strresult={}
00016         self.__filename=filename
00017         csvReader=csv.reader(open(filename),delimiter=',')
00018         oldRun=0
00019         runnumber=0
00020         ldict = {}
00021         llist = []
00022         NonValidLumi = 0
00023         lastLumi = 0
00024         for row in csvReader:
00025             field0=str(row[0]).strip()
00026             fieldsplit=re.split(':',field0)
00027             runstring = fieldsplit[0]
00028             try:
00029                 field1=str(row[1]).strip()
00030                 fieldsplit=re.split(':',field1)
00031                 lsstring = fieldsplit[0]
00032             except Exception,e:
00033                 lsstring='1' # for list with run number only, fake lsnum
00034             if not is_intstr(runstring) or not  is_intstr(lsstring):
00035                 continue
00036             runnumber=int(runstring)
00037             lsnumber=int(lsstring)
00038 
00039             if runnumber != oldRun:
00040                 if oldRun>0:
00041                     self.__result[oldRun]=ldict
00042                     ldict = {}
00043                     oldRun = runnumber
00044                     lastLumi = 0
00045                     NonValidLumi = 0
00046                 else:
00047                     oldRun = runnumber
00048 
00049             try:
00050                 delivered, recorded = float( row[5] ), float( row[6] )
00051             except:
00052                 print 'Record not parsed, Run = %d, LS = %d' % (runnumber, lsnumber)                
00053 
00054 # Commented out... If there is no value, there is no interpolation now...
00055 #            if recorded>0 :
00056 #                lastLumi = recorded
00057 #                if NonValidLumi>0:
00058 #                    # have to put real values in lumi list
00059 #                    for lnum in llist:
00060 #                        elems = [delivered, recorded]
00061 #                        ldict[lnum] = elems
00062 #                    NonValidLumi=0
00063 #                    llist = []
00064 #            else:
00065 #                if lastLumi>0:
00066 #                    recorded = lastLumi
00067 #                else:
00068 #                   # have to save lumi sections to fill once we get a non-zero lumi value
00069 #                   llist.append(lsnumber)
00070 #                   NonValidLumi=1
00071                     
00072             elems = [ delivered,recorded ]
00073             ldict[lsnumber]=elems
00074 
00075         self.__result[runnumber]=ldict #catch the last one
00076 
00077     def runs(self):
00078         return self.__result.keys()
00079     def runsandls(self):
00080         '''return {run:lslist}
00081         '''
00082         return self.__result
00083 #    def runsandlsStr(self):
00084 #        '''return {'run':lslist}
00085 #        '''
00086 #        return self.__strresult
00087     def numruns(self):
00088         return len(self.__result.keys())
00089     def numls(self,run):
00090         return len(self.__result[run])
00091         
00092 if __name__ == '__main__':
00093     result={}
00094     #filename='../test/lumi_by_LS_all.csv'
00095     filename='test.csv'
00096     s=csvLumibyLSParser(filename)
00097     print 'runs : ',s.runs()
00098     print 'full result : ',s.runsandls()
00099     #print 'str result : ',s.runsandlsStr()
00100     print 'num runs : ',s.numruns()
00101     #print 'numls in run : ',s.numls(135175)
00102