Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:15:06

0001 #!/usr/bin/env python
0002 '''
0003 GetBadEvents.py
0004 v1.0
0005 Jeff Temple
0006 Oct. 19, 2012
0007 '''
0008 from __future__ import print_function
0009 
0010 import sys,os,string
0011 from optparse import OptionParser
0012 
0013 def GetBadCrabEvents(crabdir=None,prefix=None,verbose=False):
0014     ''' Searches the res/*stdout files in the specified crab output directory "crabdir".  For each file, each line is read, and parsed as run:LS:event.  If "prefix" is set, any lines not starting with "prefix" are skipped.  The list of events "badlist" is returned at the end of the function.'''
0015 
0016     badlist=[]
0017     if not os.path.isdir(crabdir):
0018         print("<GetBadEvents> Sorry, directory '%s' does not exist!"%crabdir)
0019         return badlist
0020     newdir=os.path.join(crabdir,'res')
0021     if not os.path.isdir(newdir):
0022         print("<GetBadEvents> Sorry, subdirectory '%s' does not exist!"%newdir)
0023         return badlist
0024     # Search stdout files
0025     allfiles=sorted(os.listdir(newdir))
0026     for f in allfiles:
0027         if not f.endswith(".stdout"):
0028             continue
0029         ###print f
0030         temp=open(os.path.join(newdir,f),'r').readlines()
0031         for line in temp:
0032             if prefix!=None and not line.startswith(prefix):
0033                 continue
0034             if prefix!=None:
0035                 thisline=string.strip(string.split(line,prefix)[1])
0036             else:
0037                 thisline=string.strip(line)
0038             try:
0039                 # Check to make sure that string can be parsed as run:ls:event
0040                 myevt=string.split(thisline,":")
0041                 run=string.atoi(myevt[0])
0042                 ls=string.atoi(myevt[1])
0043                 evt=string.atoi(myevt[2])
0044                 ##if (run<0 or ls<0 or evt<0):
0045                     ##print "<GetBadEvents> Error!  Run:LS:Event value less than 0 for '%s'!"%thisline
0046                     ##continue
0047             except:
0048                 print("<GetBadEvents>  Error!  Cannot understand string '%s'"%thisline)
0049                 continue
0050             if thisline not in badlist:
0051                 badlist.append(thisline)
0052             else:
0053                 if verbose:
0054                     print("<GetBadEvents> Warning!  Event %s already in list!"%thisline)
0055         
0056     return badlist
0057 
0058 
0059 ############################################################
0060 
0061 if __name__=="__main__":
0062     parser=OptionParser()
0063     parser.add_option("-v","--verbose",
0064                       dest="verbose",
0065                       action="store_true",
0066                       default=False,
0067                       help="If specified, extra debug information is printed.")
0068     parser.add_option("-p","--prefix",
0069                       default=None,
0070                       dest="prefix",
0071                       help="If prefix specified, only lines beginning with specified prefix are parsed as Run:LS:Event")
0072     parser.add_option("-o","--outfile",
0073                       dest="outfile",
0074                       default="badevents.txt",
0075                       help="Specify name of output file.  Default is badevents.txt")
0076     parser.add_option("-n","--nocrab",
0077                       dest="nocrab",
0078                       default=False,
0079                       action="store_true",
0080                       help="If specified, combines all command-line input files into a single output, instead of assuming a CRAB output directory and parsing subdirs for *.stdout files.  Default is False (meaning that a CRAB structure is assumed).")
0081     (opts,args)=parser.parse_args()
0082 
0083     allbadevents=[]
0084     if (opts.nocrab==False):
0085         for dir in args:
0086             badlist=GetBadCrabEvents(crabdir=dir,
0087                                      prefix=opts.prefix,
0088                                      verbose=opts.verbose)
0089             print("Total bad events with prefix '%s' in directory '%s' is %i"%(opts.prefix,dir,len(badlist)))
0090             # We could just all bad events, without first checking to see if already present, which would lead to double-counting, but wouldn't affect the actual filtering
0091             for b in badlist:
0092                 if b not in allbadevents:
0093                     allbadevents.append(b)
0094 
0095     else:
0096         for infile in args:
0097             if not os.path.isfile(infile):
0098                 print("Sorry, input file '%s' does not exist"%infile)
0099                 continue
0100             print("Reading input file '%s'"%infile)
0101             events=open(infile,'r').readlines()
0102             for e in events:
0103                 temp=string.strip(e)
0104                 if temp not in allbadevents:
0105                     allbadevents.append(temp)
0106 
0107     print("Total bad events found = ",len(allbadevents))
0108     outfile=open(opts.outfile,'w')
0109     print("Sorting list of %i bad events"%len(allbadevents))
0110     allbadevents.sort()
0111     for b in allbadevents:
0112         outfile.write("%s\n"%b)
0113     outfile.close()
0114