test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
csvLumibyLSParser.py
Go to the documentation of this file.
1 # Note: this is specifically to parse a .csv file generated from a command like
2 # pixelLumiCalc.py lumibyls -i json_DCSONLY_pp.txt --hltpath "HLT_Photon75_CaloIdVL_IsoL*" -o myHLTtest.out
3 # format: Run,LS,HLTpath,L1bit,HLTpresc,L1presc,Recorded(/ub),Effective(/ub)
4 import csv
5 import re
6 def is_intstr(s):
7  try:
8  int(s)
9  return True
10  except ValueError:
11  return False
12 class csvLumibyLSParser(object):
13  def __init__(self,filename,RunX):
14  self.__result={}
15  self.__strresult={}
16  self.__filename=filename
17  self.__RunX=RunX
18  csvReader=csv.reader(open(filename),delimiter=',')
19  oldRun=0
20  runnumber=0
21  ldict = {}
22  llist = []
23  NonValidLumi = 0
24  lastLumi = 0
25  for row in csvReader:
26  field0=str(row[0]).strip()
27  fieldsplit=re.split(':',field0)
28  runstring = fieldsplit[0]
29  try:
30  field1=str(row[1]).strip()
31  fieldsplit=re.split(':',field1)
32  lsstring = fieldsplit[0]
33  except Exception as e:
34  lsstring='1' # for list with run number only, fake lsnum
35  if not is_intstr(runstring) or not is_intstr(lsstring):
36  continue
37  runnumber=int(runstring)
38  lsnumber=int(lsstring)
39 
40  if runnumber != oldRun:
41  if oldRun>0:
42  self.__result[oldRun]=ldict
43  ldict = {}
44  oldRun = runnumber
45  lastLumi = 0
46  NonValidLumi = 0
47  else:
48  oldRun = runnumber
49 
50  try:
51  if RunX=='Run2':
52  delivered, recorded = float( row[4] ), float( row[5] )
53  if RunX=='Run1':
54  delivered, recorded = float( row[5] ), float( row[6] )
55  except:
56  print 'Record not parsed, Run = %d, LS = %d' % (runnumber, lsnumber)
57 
58 # Commented out... If there is no value, there is no interpolation now...
59 # if recorded>0 :
60 # lastLumi = recorded
61 # if NonValidLumi>0:
62 # # have to put real values in lumi list
63 # for lnum in llist:
64 # elems = [delivered, recorded]
65 # ldict[lnum] = elems
66 # NonValidLumi=0
67 # llist = []
68 # else:
69 # if lastLumi>0:
70 # recorded = lastLumi
71 # else:
72 # # have to save lumi sections to fill once we get a non-zero lumi value
73 # llist.append(lsnumber)
74 # NonValidLumi=1
75 
76  elems = [ delivered,recorded ]
77  ldict[lsnumber]=elems
78 
79  self.__result[runnumber]=ldict #catch the last one
80 
81  def runs(self):
82  return self.__result.keys()
83  def runsandls(self):
84  '''return {run:lslist}
85  '''
86  return self.__result
87 # def runsandlsStr(self):
88 # '''return {'run':lslist}
89 # '''
90 # return self.__strresult
91  def numruns(self):
92  return len(self.__result.keys())
93  def numls(self,run):
94  return len(self.__result[run])
95 
96 if __name__ == '__main__':
97  result={}
98  #filename='../test/lumi_by_LS_all.csv'
99  filename='test.csv'
100  s=csvLumibyLSParser(filename)
101  print 'runs : ',s.runs()
102  print 'full result : ',s.runsandls()
103  #print 'str result : ',s.runsandlsStr()
104  print 'num runs : ',s.numruns()
105  #print 'numls in run : ',s.numls(135175)
106