CMS 3D CMS Logo

pileupDistInMC.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 
3 from argparse import ArgumentParser
4 import re
5 from pprint import pprint
6 
7 commentRE = re.compile (r'#.*$')
8 
9 if __name__ == "__main__":
10  parser = ArgumentParser()
11  parser.add_argument('--loadFromFile', dest='loadFromFile', default=[],
12  type=str,
13  action='append',
14  help="Name of text file containing filenames" )
15  parser.add_argument('--prefix', dest='prefix', type=str,
16  default='',
17  help="Prefix to add to files" )
18  parser.add_argument('--bx', dest='bx', type=int,
19  default='0',
20  help="Bunch crossing to check (0 = in-time)" )
21  parser.add_argument("file", metavar="file.root", type=str, nargs='*')
22  options = parser.parse_args()
23  import ROOT # stupid ROOT takes the arguments error
24  from DataFormats.FWLite import Events, Handle
25 
26  listOfFiles = options.file
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.items():
77  renormDict[key] = count / total
78  pprint (renormDict)
79 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47