CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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=defaultDict.keys()
67  keys.sort()
68  if len(sys.argv)==1:
69  print "Default bad (run,events) are:"
70  for i in keys:
71  print i
72  print "\nA total of %i bad events"%len(keys)
73  sys.exit()
74  newlines=[]
75  for i in sys.argv[1:]:
76  if not os.path.isfile(i):
77  print "Error, file '%s' does not exist"%i
78  continue
79  lines=open(i,'r').readlines()
80  for i in lines:
81  newlines.append(i)
82  newBadDict=ReadNewList(newlines)
83  # At some point, add ability to append/replace.
84  # For now, new list is just the output from newBadDict
85 
86  newkeys=newBadDict.keys()
87  newkeys.sort()
88  notInOld={}
89  notInNew={}
90 
91  out=open("badEvents.py",'w')
92 
93  thistime=time.time()
94  thistime=time.strftime("%H:%M:%S %d %h %Y")
95  out.write("# File last updated on %s\n"%thistime)
96  out.write("# A total of %i bad events\n\n"%len(newkeys))
97 
98  out.write("badEvents=[\n")
99  for i in newkeys:
100  #print newBadDict[i]
101  out.write("%s\n"%newBadDict[i])
102  if i not in keys:
103  notInOld[i]=newBadDict[i]
104  out.write("]\n")
105  out.close()
106 
107 
108  for i in keys:
109  if i not in newkeys:
110  notInNew[i]=defaultDict[i]
111 
112 
113  print "Total bad events in new file = ",len(newkeys)
114 
115  if len(notInOld.keys())>0:
116  print
117  print "A total of %i bad events found"%len(notInOld.keys())
118  for k in notInOld.keys():
119  print k
120 
121  if len(notInNew.keys())>0:
122  print
123  print "A total of %i events aren't in NEW list!"%len(notInNew.keys())
124  for k in notInNew.keys():
125  print k