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 
7 def root2map(dir,ana,treename):
8  import ROOT
9  tfile = ROOT.TFile.Open("%s/%s/%s.root"%(dir,ana,treename))
10  if not tfile:
11  print("Error: dir %s does not contain %s/%s.root" % (dir,ana,treename))
12  return None
13  tree = tfile.Get(treename)
14  if not tree:
15  print("Error: rootfile %s/%s/%s.root does not contain a TTree %s" % (dir,ana,treename,treename))
16  return None
17  jsonind = {}
18  for e in range(tree.GetEntries()):
19  tree.GetEntry(e)
20  run,lumi = tree.run, tree.lumi
21  if run not in jsonind:
22  jsonind[run] = [lumi]
23  else:
24  jsonind[run].append(lumi)
25  # remove duplicates
26  for run in jsonind:
27  jsonind[run] = list(set(jsonind[run]))
28 
29  nruns = len(jsonind)
30  nlumis = sum(len(v) for v in jsonind.values())
31  jsonmap = {}
32  for r,lumis in jsonind.items():
33  if len(lumis) == 0: continue # shouldn't happen
34  lumis.sort()
35  ranges = [ [ lumis[0], lumis[0] ] ]
36  for lumi in lumis[1:]:
37  if lumi == ranges[-1][1] + 1:
38  ranges[-1][1] = lumi
39  else:
40  ranges.append([lumi,lumi])
41  jsonmap[r] = ranges
42  return (jsonmap, nruns, nlumis)
43 
44 if __name__ == '__main__':
45  parser = OptionParser(usage='%prog <target_directories> [options]',
46  description='Check the output of the JSONAnalyzer and produce a json file of the processed runs and lumisections')
47  parser.add_option("-a", "--analyzer", dest="jsonAnalyzer", default="JSONAnalyzer", help="Name of the JSONAnalyzer")
48  parser.add_option("-t", "--tree", dest="treeName", default="RLTInfo", help="Name of the TTree produced by the JSONAnalyzer")
49  parser.add_option("-o", "--out", dest="outputFile", default="lumiSummary.json", help="Name of the output file")
50  (options,args) = parser.parse_args()
51  if len(args)==0:
52  print('provide at least one directory in argument. Use -h to display help')
53  exit()
54  for a in args:
55  summary = root2map(a,options.jsonAnalyzer,options.treeName)
56  if summary:
57  oname = "%s/%s" % (a,options.outputFile)
58  jmap, runs, lumis = summary
59  json.dump(jmap,open(oname,'w'))
60  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:7
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:52