CMS 3D CMS Logo

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