CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pileupDistInMC.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import optparse
4 import re
5 from pprint import pprint
6 
7 commentRE = re.compile (r'#.*$')
8 
9 if __name__ == "__main__":
10  parser = optparse.OptionParser ("Usage: %prog file1.root [file2.root...]")
11  parser.add_option ('--loadFromFile', dest='loadFromFile', default=[],
12  type='string',
13  action='append',
14  help="Name of text file containing filenames" )
15  parser.add_option ('--prefix', dest='prefix', type='string',
16  default='',
17  help="Prefix to add to files" )
18 
19  (options, args) = parser.parse_args()
20  import ROOT # stupid ROOT takes the arugments error
21  from DataFormats.FWLite import Events, Handle
22 
23  listOfFiles = args[:]
24  for filename in options.loadFromFile:
25  source = open (filename, 'r')
26  for line in source:
27  line = commentRE.sub ('', line).strip() # get rid of comments
28  if not line:
29  # don't bother with blank lines
30  continue
31  listOfFiles.append (line)
32  source.close()
33  if options.prefix:
34  oldList = listOfFiles
35  listOfFiles = []
36  for name in oldList:
37  listOfFiles.append( options.prefix + name )
38 
39  if not listOfFiles:
40  raise RuntimeError, "You have not provided any files"
41 
42  events = Events (listOfFiles)
43 
44  handle = Handle('PileupSummaryInfo')
45  label = ('addPileupInfo')
46 
47  ROOT.gROOT.SetBatch() # don't pop up canvases
48 
49  # loop over events
50  countDict = {}
51  total = 0.
52  for event in events:
53  event.getByLabel (label, handle)
54  pileup = handle.product()
55  num = pileup.getPU_NumInteractions()
56  total += 1
57  if not countDict.has_key (num):
58  countDict[num] = 1
59  else:
60  countDict[num] += 1
61 
62  print "total", int(total), "\ncounts:"
63  pprint (countDict, width=1)
64  print "normalized:"
65 
66  renormDict = {}
67  for key, count in countDict.iteritems():
68  renormDict[key] = count / total
69  pprint (renormDict)
70 
void strip(std::string &input, const std::string &blanks=" \n\t")
Definition: stringTools.cc:16