CMS 3D CMS Logo

GenerateHcalLaserBadRunList.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys, os, string
4 import time
5 
6 import FWCore.ParameterSet.Config as cms
7 from hcalLaserEventFilter_cfi import hcalLaserEventFilter
8 
9 ''' Program reads existing bad run/event list from hcalLaserEventFilter_cfi.py, and an (optional) new list from a text file. If a text file is specified, this is assumed to be the desired new bad list, and its output will be sent to badEvents.py in the form needed by the cfi file.
10 
11 If no text file is provided, then the current bad events in the .py file will be displayed.
12 '''
13 
14 
15 def MakePair(startlist):
16  dict={}
17  for i in range(0,len(startlist),2):
18  key1=startlist[i]
19  key2=startlist[i+1]
20  runevent=(key1,key2)
21  dict[runevent]="%s,%s,"%(key1,key2)
22  return dict
23 
24 def ReadNewList(newlist):
25  ''' Read a new list of bad runs from an input file, and
26  creates a new list of output keys for the bad run/events.
27  '''
28  outlist=[]
29  for i in newlist:
30  temp=string.strip(i)
31  # Input is comma-separated
32  if temp.find(",")>-1:
33  temp=string.split(temp,",")
34  # Input is space-separated
35  else:
36  temp=string.split(temp)
37 
38  # Case 1: new input presented as "run lumi event" list
39  if len(temp)==3:
40  try:
41  run=string.atoi(temp[0])
42  evt=string.atoi(temp[2])
43  except:
44  print "Could not parse line '%s'"%i
45  # Case 2: new input presented as "run event" list
46  elif len(temp)==2:
47  try:
48  run=string.atoi(temp[0])
49  evt=string.atoi(temp[1])
50  except:
51  print "Could not parse line '%s'"%i
52  else:
53  print "Cannot parse line! ('%s')"%i
54  continue
55  outlist.append(run)
56  outlist.append(evt)
57  outDict=MakePair(outlist)
58  return outDict
59 
60 
61 #######################################################
62 
63 if __name__=="__main__":
64  defaultList=hcalLaserEventFilter.BadRunEventNumbers
65  defaultDict=MakePair(defaultList)
66  keys=sorted(defaultDict.keys())
67  if len(sys.argv)==1:
68  print "Default bad (run,events) are:"
69  for i in keys:
70  print i
71  print "\nA total of %i bad events"%len(keys)
72  sys.exit()
73  newlines=[]
74  for i in sys.argv[1:]:
75  if not os.path.isfile(i):
76  print "Error, file '%s' does not exist"%i
77  continue
78  lines=open(i,'r').readlines()
79  for i in lines:
80  newlines.append(i)
81  newBadDict=ReadNewList(newlines)
82  # At some point, add ability to append/replace.
83  # For now, new list is just the output from newBadDict
84 
85  newkeys=newBadDict.keys()
86  newkeys.sort()
87  notInOld={}
88  notInNew={}
89 
90  out=open("badEvents.py",'w')
91 
92  thistime=time.time()
93  thistime=time.strftime("%H:%M:%S %d %h %Y")
94  out.write("# File last updated on %s\n"%thistime)
95  out.write("# A total of %i bad events\n\n"%len(newkeys))
96 
97  out.write("badEvents=[\n")
98  for i in newkeys:
99  #print newBadDict[i]
100  out.write("%s\n"%newBadDict[i])
101  if i not in keys:
102  notInOld[i]=newBadDict[i]
103  out.write("]\n")
104  out.close()
105 
106 
107  for i in keys:
108  if i not in newkeys:
109  notInNew[i]=defaultDict[i]
110 
111 
112  print "Total bad events in new file = ",len(newkeys)
113 
114  if len(notInOld)>0:
115  print
116  print "A total of %i bad events found"%len(notInOld)
117  for k in notInOld.keys():
118  print k
119 
120  if len(notInNew)>0:
121  print
122  print "A total of %i events aren't in NEW list!"%len(notInNew)
123  for k in notInNew.keys():
124  print k