CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/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     parser.add_option ('--bx', dest='bx', type='int',
00020                        default='0',
00021                        help="Bunch crossing to check (0 = in-time)" )
00022     (options, args) = parser.parse_args()
00023     import ROOT # stupid ROOT takes the arugments error
00024     from DataFormats.FWLite import Events, Handle
00025 
00026     listOfFiles = args[:]
00027     for filename in options.loadFromFile:
00028         source = open (filename, 'r')
00029         for line in source:            
00030             line = commentRE.sub ('', line).strip() # get rid of comments
00031             if not line:
00032                 # don't bother with blank lines
00033                 continue
00034             listOfFiles.append (line)
00035         source.close()
00036     if options.prefix:
00037         oldList = listOfFiles
00038         listOfFiles = []
00039         for name in oldList:
00040             listOfFiles.append( options.prefix + name )
00041 
00042     if not listOfFiles:
00043         raise RuntimeError, "You have not provided any files"
00044 
00045     events = Events (listOfFiles)
00046 
00047     handle = Handle('vector<PileupSummaryInfo>')
00048     label  = ('addPileupInfo')
00049 
00050     ROOT.gROOT.SetBatch()        # don't pop up canvases
00051 
00052     # loop over events
00053     countDict = {}
00054     total = 0.
00055     for event in events:
00056         event.getByLabel (label, handle)
00057         pileups = handle.product()
00058         for pileup in pileups:
00059             if pileup.getBunchCrossing() == options.bx:
00060                 break
00061             if pileup == pileups[-1] and len(pileups)>1 :
00062                 raise RuntimeError, "Requested BX not found in file"
00063 
00064         num = pileup.getPU_NumInteractions()
00065         total += 1
00066         if not countDict.has_key (num):
00067             countDict[num] = 1
00068         else:
00069             countDict[num] += 1
00070 
00071     print "total", int(total), "\ncounts:"
00072     pprint (countDict, width=1)
00073     print "normalized:"
00074 
00075     renormDict = {}
00076     for key, count in countDict.iteritems():
00077         renormDict[key] = count / total
00078     pprint (renormDict)
00079