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