Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:25:19

0001 from __future__ import print_function
0002 # Note: this is specifically to parse a .csv file generated from a command like
0003 # pixelLumiCalc.py lumibyls -i json_DCSONLY_pp.txt --hltpath "HLT_Photon75_CaloIdVL_IsoL*" -o myHLTtest.out
0004 # format: Run,LS,HLTpath,L1bit,HLTpresc,L1presc,Recorded(/ub),Effective(/ub)
0005 import csv
0006 import re
0007 def is_intstr(s):
0008     try:
0009         int(s)
0010         return True
0011     except ValueError:
0012         return False
0013 class csvLumibyLSParser(object):
0014     def __init__(self,filename,RunX):
0015         self.__result={}
0016         self.__strresult={}
0017         self.__filename=filename
0018         self.__RunX=RunX
0019         csvReader=csv.reader(open(filename),delimiter=',')
0020         oldRun=0
0021         runnumber=0
0022         ldict = {}
0023         llist = []
0024         NonValidLumi = 0
0025         lastLumi = 0
0026         for row in csvReader:
0027             field0=str(row[0]).strip()
0028             fieldsplit=re.split(':',field0)
0029             runstring = fieldsplit[0]
0030             try:
0031                 field1=str(row[1]).strip()
0032                 fieldsplit=re.split(':',field1)
0033                 lsstring = fieldsplit[0]
0034             except Exception as e:
0035                 lsstring='1' # for list with run number only, fake lsnum
0036             if not is_intstr(runstring) or not  is_intstr(lsstring):
0037                 continue
0038             runnumber=int(runstring)
0039             lsnumber=int(lsstring)
0040 
0041             if runnumber != oldRun:
0042                 if oldRun>0:
0043                     self.__result[oldRun]=ldict
0044                     ldict = {}
0045                     oldRun = runnumber
0046                     lastLumi = 0
0047                     NonValidLumi = 0
0048                 else:
0049                     oldRun = runnumber
0050 
0051             try:
0052                 if RunX=='Run2':
0053                     delivered, recorded = float( row[4] ), float( row[5] )
0054                 if RunX=='Run1':
0055                     delivered, recorded = float( row[5] ), float( row[6] )
0056             except:
0057                 print('Record not parsed, Run = %d, LS = %d' % (runnumber, lsnumber))                
0058 
0059 # Commented out... If there is no value, there is no interpolation now...
0060 #            if recorded>0 :
0061 #                lastLumi = recorded
0062 #                if NonValidLumi>0:
0063 #                    # have to put real values in lumi list
0064 #                    for lnum in llist:
0065 #                        elems = [delivered, recorded]
0066 #                        ldict[lnum] = elems
0067 #                    NonValidLumi=0
0068 #                    llist = []
0069 #            else:
0070 #                if lastLumi>0:
0071 #                    recorded = lastLumi
0072 #                else:
0073 #                   # have to save lumi sections to fill once we get a non-zero lumi value
0074 #                   llist.append(lsnumber)
0075 #                   NonValidLumi=1
0076 
0077             elems = [ delivered,recorded ]
0078             ldict[lsnumber]=elems
0079 
0080         self.__result[runnumber]=ldict #catch the last one
0081 
0082     def runs(self):
0083         return self.__result.keys()
0084     def runsandls(self):
0085         '''return {run:lslist}
0086         '''
0087         return self.__result
0088 #    def runsandlsStr(self):
0089 #        '''return {'run':lslist}
0090 #        '''
0091 #        return self.__strresult
0092     def numruns(self):
0093         return len(self.__result)
0094     def numls(self,run):
0095         return len(self.__result[run])
0096 
0097 if __name__ == '__main__':
0098     result={}
0099     #filename='../test/lumi_by_LS_all.csv'
0100     filename='test.csv'
0101     s=csvLumibyLSParser(filename)
0102     print('runs : ',s.runs())
0103     print('full result : ',s.runsandls())
0104     #print 'str result : ',s.runsandlsStr()
0105     print('num runs : ',s.numruns())
0106     #print 'numls in run : ',s.numls(135175)
0107