CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PhysicsTools/Utilities/scripts/pileupDistInMC.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import optparse
00004 import re
00005 from pprint import pprint
00006 
00007 commentRE = re.compile (r'#.*$')
00008 
00009 if __name__ == "__main__":
00010     parser = optparse.OptionParser ("Usage: %prog file1.root [file2.root...]")
00011     parser.add_option ('--loadFromFile', dest='loadFromFile', default=[],
00012                        type='string',
00013                        action='append', 
00014                        help="Name of text file containing filenames" )
00015     parser.add_option ('--prefix', dest='prefix', type='string',
00016                        default='',
00017                        help="Prefix to add to files" )
00018     
00019     (options, args) = parser.parse_args()
00020     import ROOT # stupid ROOT takes the arugments error
00021     from DataFormats.FWLite import Events, Handle
00022 
00023     listOfFiles = args[:]
00024     for filename in options.loadFromFile:
00025         source = open (filename, 'r')
00026         for line in source:            
00027             line = commentRE.sub ('', line).strip() # get rid of comments
00028             if not line:
00029                 # don't bother with blank lines
00030                 continue
00031             listOfFiles.append (line)
00032         source.close()
00033     if options.prefix:
00034         oldList = listOfFiles
00035         listOfFiles = []
00036         for name in oldList:
00037             listOfFiles.append( options.prefix + name )
00038 
00039     if not listOfFiles:
00040         raise RuntimeError, "You have not provided any files"
00041 
00042     events = Events (listOfFiles)
00043 
00044     handle = Handle('PileupSummaryInfo')
00045     label  = ('addPileupInfo')
00046 
00047     ROOT.gROOT.SetBatch()        # don't pop up canvases
00048 
00049     # loop over events
00050     countDict = {}
00051     total = 0.
00052     for event in events:
00053         event.getByLabel (label, handle)
00054         pileup = handle.product()
00055         num = pileup.getPU_NumInteractions()
00056         total += 1
00057         if not countDict.has_key (num):
00058             countDict[num] = 1
00059         else:
00060             countDict[num] += 1
00061 
00062     print "total", int(total), "\ncounts:"
00063     pprint (countDict, width=1)
00064     print "normalized:"
00065 
00066     renormDict = {}
00067     for key, count in countDict.iteritems():
00068         renormDict[key] = count / total
00069     pprint (renormDict)
00070