CMS 3D CMS Logo

GenerateHcalLaserBadRunList.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 import sys, os, string
5 import time
6 
7 import FWCore.ParameterSet.Config as cms
8 from hcalLaserEventFilter_cfi import hcalLaserEventFilter
9 
10 ''' 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.
11 
12 If no text file is provided, then the current bad events in the .py file will be displayed.
13 '''
14 
15 
16 def MakePair(startlist):
17  dict={}
18  for i in range(0,len(startlist),2):
19  key1=startlist[i]
20  key2=startlist[i+1]
21  runevent=(key1,key2)
22  dict[runevent]="%s,%s,"%(key1,key2)
23  return dict
24 
25 def ReadNewList(newlist):
26  ''' Read a new list of bad runs from an input file, and
27  creates a new list of output keys for the bad run/events.
28  '''
29  outlist=[]
30  for i in newlist:
31  temp=string.strip(i)
32  # Input is comma-separated
33  if temp.find(",")>-1:
34  temp=string.split(temp,",")
35  # Input is space-separated
36  else:
37  temp=string.split(temp)
38 
39  # Case 1: new input presented as "run lumi event" list
40  if len(temp)==3:
41  try:
42  run=string.atoi(temp[0])
43  evt=string.atoi(temp[2])
44  except:
45  print("Could not parse line '%s'"%i)
46  # Case 2: new input presented as "run event" list
47  elif len(temp)==2:
48  try:
49  run=string.atoi(temp[0])
50  evt=string.atoi(temp[1])
51  except:
52  print("Could not parse line '%s'"%i)
53  else:
54  print("Cannot parse line! ('%s')"%i)
55  continue
56  outlist.append(run)
57  outlist.append(evt)
58  outDict=MakePair(outlist)
59  return outDict
60 
61 
62 #######################################################
63 
64 if __name__=="__main__":
65  defaultList=hcalLaserEventFilter.BadRunEventNumbers
66  defaultDict=MakePair(defaultList)
67  keys=sorted(defaultDict.keys())
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)>0:
116  print()
117  print("A total of %i bad events found"%len(notInOld))
118  for k in notInOld.keys():
119  print(k)
120 
121  if len(notInNew)>0:
122  print()
123  print("A total of %i events aren't in NEW list!"%len(notInNew))
124  for k in notInNew.keys():
125  print(k)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65