Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:25:33

0001 #!/usr/bin/env python
0002 
0003 from __future__ import print_function
0004 from __future__ import absolute_import
0005 from builtins import range
0006 import sys, os, string
0007 import time
0008 
0009 import FWCore.ParameterSet.Config as cms
0010 from .hcalLaserEventFilter_cfi import hcalLaserEventFilter
0011 
0012 ''' 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.
0013 
0014 If no text file is provided, then the current bad events in the .py file will be displayed.
0015 '''
0016 
0017 
0018 def MakePair(startlist):
0019     dict={}
0020     for i in range(0,len(startlist),2):
0021         key1=startlist[i]
0022         key2=startlist[i+1]
0023         runevent=(key1,key2)
0024         dict[runevent]="%s,%s,"%(key1,key2)
0025     return dict
0026 
0027 def ReadNewList(newlist):
0028     ''' Read a new list of bad runs from an input file, and
0029         creates a new list of output keys for the bad run/events.
0030         '''
0031     outlist=[]
0032     for i in newlist:
0033         temp=string.strip(i)
0034         # Input is comma-separated
0035         if temp.find(",")>-1:
0036             temp=string.split(temp,",")
0037         # Input is space-separated
0038         else:
0039             temp=string.split(temp)
0040 
0041         # Case 1:  new input presented as "run lumi event" list
0042         if len(temp)==3:
0043             try:
0044                 run=string.atoi(temp[0])
0045                 evt=string.atoi(temp[2])
0046             except:
0047                 print("Could not parse line '%s'"%i)
0048         # Case 2:  new input presented as "run event" list
0049         elif len(temp)==2:
0050             try:
0051                 run=string.atoi(temp[0])
0052                 evt=string.atoi(temp[1])
0053             except:
0054                 print("Could not parse line '%s'"%i)
0055         else:
0056             print("Cannot parse line! ('%s')"%i)
0057             continue
0058         outlist.append(run)
0059         outlist.append(evt)
0060     outDict=MakePair(outlist)
0061     return outDict
0062 
0063 
0064 #######################################################
0065 
0066 if __name__=="__main__":
0067     defaultList=hcalLaserEventFilter.BadRunEventNumbers
0068     defaultDict=MakePair(defaultList)
0069     keys=sorted(defaultDict.keys())
0070     if len(sys.argv)==1:
0071         print("Default bad (run,events) are:")
0072         for i in keys:
0073             print(i)
0074         print("\nA total of %i bad events"%len(keys))
0075         sys.exit()
0076     newlines=[]
0077     for i in sys.argv[1:]:
0078         if not os.path.isfile(i):
0079             print("Error, file '%s' does not exist"%i)
0080             continue
0081         lines=open(i,'r').readlines()
0082         for i in lines:
0083             newlines.append(i)
0084     newBadDict=ReadNewList(newlines)
0085     # At some point, add ability to append/replace.
0086     # For now, new list is just the output from newBadDict
0087 
0088     newkeys=newBadDict.keys()
0089     newkeys.sort()
0090     notInOld={}
0091     notInNew={}
0092 
0093     out=open("badEvents.py",'w')
0094     
0095     thistime=time.time()
0096     thistime=time.strftime("%H:%M:%S %d %h %Y")
0097     out.write("# File last updated on %s\n"%thistime)
0098     out.write("# A total of %i bad events\n\n"%len(newkeys))
0099 
0100     out.write("badEvents=[\n")
0101     for i in newkeys:
0102         #print newBadDict[i]
0103         out.write("%s\n"%newBadDict[i])
0104         if i not in keys:
0105             notInOld[i]=newBadDict[i]
0106     out.write("]\n")
0107     out.close()
0108     
0109     
0110     for i in keys:
0111         if i not in newkeys:
0112             notInNew[i]=defaultDict[i]
0113 
0114 
0115     print("Total bad events in new file = ",len(newkeys))
0116 
0117     if len(notInOld)>0:
0118         print()
0119         print("A total of %i bad events found"%len(notInOld))
0120         for k in notInOld.keys():
0121             print(k)
0122 
0123     if len(notInNew)>0:
0124         print()
0125         print("A total of %i events aren't in NEW list!"%len(notInNew))
0126         for k in notInNew.keys():
0127             print(k)