CMS 3D CMS Logo

pileupDistInMC.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 import optparse
5 import re
6 from pprint import pprint
7 import six
8 
9 commentRE = re.compile (r'#.*$')
10 
11 if __name__ == "__main__":
12  parser = optparse.OptionParser ("Usage: %prog file1.root [file2.root...]")
13  parser.add_option ('--loadFromFile', dest='loadFromFile', default=[],
14  type='string',
15  action='append',
16  help="Name of text file containing filenames" )
17  parser.add_option ('--prefix', dest='prefix', type='string',
18  default='',
19  help="Prefix to add to files" )
20 
21  parser.add_option ('--bx', dest='bx', type='int',
22  default='0',
23  help="Bunch crossing to check (0 = in-time)" )
24  (options, args) = parser.parse_args()
25  import ROOT # stupid ROOT takes the arugments error
26  from DataFormats.FWLite import Events, Handle
27 
28  listOfFiles = args[:]
29  for filename in options.loadFromFile:
30  source = open (filename, 'r')
31  for line in source:
32  line = commentRE.sub ('', line).strip() # get rid of comments
33  if not line:
34  # don't bother with blank lines
35  continue
36  listOfFiles.append (line)
37  source.close()
38  if options.prefix:
39  oldList = listOfFiles
40  listOfFiles = []
41  for name in oldList:
42  listOfFiles.append( options.prefix + name )
43 
44  if not listOfFiles:
45  raise RuntimeError("You have not provided any files")
46 
47  events = Events (listOfFiles)
48 
49  handle = Handle('vector<PileupSummaryInfo>')
50  label = ('addPileupInfo')
51 
52  ROOT.gROOT.SetBatch() # don't pop up canvases
53 
54  # loop over events
55  countDict = {}
56  total = 0.
57  for event in events:
58  event.getByLabel (label, handle)
59  pileups = handle.product()
60  for pileup in pileups:
61  if pileup.getBunchCrossing() == options.bx:
62  break
63  if pileup == pileups[-1] and len(pileups)>1 :
64  raise RuntimeError("Requested BX not found in file")
65 
66  num = pileup.getPU_NumInteractions()
67  total += 1
68  if num not in countDict:
69  countDict[num] = 1
70  else:
71  countDict[num] += 1
72 
73  print("total", int(total), "\ncounts:")
74  pprint (countDict, width=1)
75  print("normalized:")
76 
77  renormDict = {}
78  for key, count in six.iteritems(countDict):
79  renormDict[key] = count / total
80  pprint (renormDict)
81 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65