CMS 3D CMS Logo

heppy_report.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from __future__ import print_function
3 from builtins import range
4 from optparse import OptionParser
5 import json
6 import six
7 
8 def root2map(dir,ana,treename):
9  import ROOT
10  tfile = ROOT.TFile.Open("%s/%s/%s.root"%(dir,ana,treename))
11  if not tfile:
12  print("Error: dir %s does not contain %s/%s.root" % (dir,ana,treename))
13  return None
14  tree = tfile.Get(treename)
15  if not tree:
16  print("Error: rootfile %s/%s/%s.root does not contain a TTree %s" % (dir,ana,treename,treename))
17  return None
18  jsonind = {}
19  for e in range(tree.GetEntries()):
20  tree.GetEntry(e)
21  run,lumi = tree.run, tree.lumi
22  if run not in jsonind:
23  jsonind[run] = [lumi]
24  else:
25  jsonind[run].append(lumi)
26  # remove duplicates
27  for run in jsonind:
28  jsonind[run] = list(set(jsonind[run]))
29 
30  nruns = len(jsonind)
31  nlumis = sum(len(v) for v in six.itervalues(jsonind))
32  jsonmap = {}
33  for r,lumis in six.iteritems(jsonind):
34  if len(lumis) == 0: continue # shouldn't happen
35  lumis.sort()
36  ranges = [ [ lumis[0], lumis[0] ] ]
37  for lumi in lumis[1:]:
38  if lumi == ranges[-1][1] + 1:
39  ranges[-1][1] = lumi
40  else:
41  ranges.append([lumi,lumi])
42  jsonmap[r] = ranges
43  return (jsonmap, nruns, nlumis)
44 
45 if __name__ == '__main__':
46  parser = OptionParser(usage='%prog <target_directories> [options]',
47  description='Check the output of the JSONAnalyzer and produce a json file of the processed runs and lumisections')
48  parser.add_option("-a", "--analyzer", dest="jsonAnalyzer", default="JSONAnalyzer", help="Name of the JSONAnalyzer")
49  parser.add_option("-t", "--tree", dest="treeName", default="RLTInfo", help="Name of the TTree produced by the JSONAnalyzer")
50  parser.add_option("-o", "--out", dest="outputFile", default="lumiSummary.json", help="Name of the output file")
51  (options,args) = parser.parse_args()
52  if len(args)==0:
53  print('provide at least one directory in argument. Use -h to display help')
54  exit()
55  for a in args:
56  summary = root2map(a,options.jsonAnalyzer,options.treeName)
57  if summary:
58  oname = "%s/%s" % (a,options.outputFile)
59  jmap, runs, lumis = summary
60  json.dump(jmap,open(oname,'w'))
61  print("Saved %s (%d runs, %d lumis)" % (oname, runs, lumis))
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
heppy_report.root2map
def root2map(dir, ana, treename)
Definition: heppy_report.py:8
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
mps_setup.append
append
Definition: mps_setup.py:85
beamvalidation.exit
def exit(msg="")
Definition: beamvalidation.py:53