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
13  def __init__(self,filename):
14  self.__result={}
15  self.__strresult={}
16  self.__filename=filename
17  csvReader=csv.reader(open(filename),delimiter=',')
18  oldRun=0
19  runnumber=0
20  ldict = {}
21  llist = []
22  NonValidLumi = 0
23  lastLumi = 0
24  for row in csvReader:
25  field0=str(row[0]).strip()
26  fieldsplit=re.split(':',field0)
27  runstring = fieldsplit[0]
28  try:
29  field1=str(row[1]).strip()
30  fieldsplit=re.split(':',field1)
31  lsstring = fieldsplit[0]
32  except Exception,e:
33  lsstring='1' # for list with run number only, fake lsnum
34  if not is_intstr(runstring) or not is_intstr(lsstring):
35  continue
36  runnumber=int(runstring)
37  lsnumber=int(lsstring)
38 
39  if runnumber != oldRun:
40  if oldRun>0:
41  self.__result[oldRun]=ldict
42  ldict = {}
43  oldRun = runnumber
44  lastLumi = 0
45  NonValidLumi = 0
46  else:
47  oldRun = runnumber
48 
49  try:
50  delivered, recorded = float( row[5] ), float( row[6] )
51  except:
52  print 'Record not parsed, Run = %d, LS = %d' % (runnumber, lsnumber)
53 
54 # Commented out... If there is no value, there is no interpolation now...
55 # if recorded>0 :
56 # lastLumi = recorded
57 # if NonValidLumi>0:
58 # # have to put real values in lumi list
59 # for lnum in llist:
60 # elems = [delivered, recorded]
61 # ldict[lnum] = elems
62 # NonValidLumi=0
63 # llist = []
64 # else:
65 # if lastLumi>0:
66 # recorded = lastLumi
67 # else:
68 # # have to save lumi sections to fill once we get a non-zero lumi value
69 # llist.append(lsnumber)
70 # NonValidLumi=1
71 
72  elems = [ delivered,recorded ]
73  ldict[lsnumber]=elems
74 
75  self.__result[runnumber]=ldict #catch the last one
76 
77  def runs(self):
78  return self.__result.keys()
79  def runsandls(self):
80  '''return {run:lslist}
81  '''
82  return self.__result
83 # def runsandlsStr(self):
84 # '''return {'run':lslist}
85 # '''
86 # return self.__strresult
87  def numruns(self):
88  return len(self.__result.keys())
89  def numls(self,run):
90  return len(self.__result[run])
91 
92 if __name__ == '__main__':
93  result={}
94  #filename='../test/lumi_by_LS_all.csv'
95  filename='test.csv'
96  s=csvLumibyLSParser(filename)
97  print 'runs : ',s.runs()
98  print 'full result : ',s.runsandls()
99  #print 'str result : ',s.runsandlsStr()
100  print 'num runs : ',s.numruns()
101  #print 'numls in run : ',s.numls(135175)
102 
void strip(std::string &input, const std::string &blanks=" \n\t")
Definition: stringTools.cc:16
list object
Definition: dbtoconf.py:77