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  parser.add_option ('--bx', dest='bx', type='int',
20  default='0',
21  help="Bunch crossing to check (0 = in-time)" )
22  (options, args) = parser.parse_args()
23  import ROOT # stupid ROOT takes the arugments error
24  from DataFormats.FWLite import Events, Handle
25 
26  listOfFiles = args[:]
27  for filename in options.loadFromFile:
28  source = open (filename, 'r')
29  for line in source:
30  line = commentRE.sub ('', line).strip() # get rid of comments
31  if not line:
32  # don't bother with blank lines
33  continue
34  listOfFiles.append (line)
35  source.close()
36  if options.prefix:
37  oldList = listOfFiles
38  listOfFiles = []
39  for name in oldList:
40  listOfFiles.append( options.prefix + name )
41 
42  if not listOfFiles:
43  raise RuntimeError("You have not provided any files")
44 
45  events = Events (listOfFiles)
46 
47  handle = Handle('vector<PileupSummaryInfo>')
48  label = ('addPileupInfo')
49 
50  ROOT.gROOT.SetBatch() # don't pop up canvases
51 
52  # loop over events
53  countDict = {}
54  total = 0.
55  for event in events:
56  event.getByLabel (label, handle)
57  pileups = handle.product()
58  for pileup in pileups:
59  if pileup.getBunchCrossing() == options.bx:
60  break
61  if pileup == pileups[-1] and len(pileups)>1 :
62  raise RuntimeError("Requested BX not found in file")
63 
64  num = pileup.getPU_NumInteractions()
65  total += 1
66  if num not in countDict:
67  countDict[num] = 1
68  else:
69  countDict[num] += 1
70 
71  print "total", int(total), "\ncounts:"
72  pprint (countDict, width=1)
73  print "normalized:"
74 
75  renormDict = {}
76  for key, count in countDict.iteritems():
77  renormDict[key] = count / total
78  pprint (renormDict)
79