CMS 3D CMS Logo

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