Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:33

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