Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:50

0001 #!/usr/bin/env python3
0002 from builtins import range
0003 from optparse import OptionParser
0004 import json
0005 
0006 def root2map(dir,ana,treename):
0007     import ROOT
0008     tfile = ROOT.TFile.Open("%s/%s/%s.root"%(dir,ana,treename))
0009     if not tfile:
0010         print("Error: dir %s does not contain %s/%s.root" % (dir,ana,treename))
0011         return None
0012     tree = tfile.Get(treename)
0013     if not tree:
0014         print("Error: rootfile %s/%s/%s.root does not contain a TTree %s" % (dir,ana,treename,treename))
0015         return None
0016     jsonind = {}
0017     for e in range(tree.GetEntries()):
0018         tree.GetEntry(e)
0019         run,lumi = tree.run, tree.lumi
0020         if run not in jsonind:
0021             jsonind[run] = [lumi]
0022         else:
0023             jsonind[run].append(lumi)
0024     # remove duplicates
0025     for run in jsonind:
0026         jsonind[run] =  list(set(jsonind[run]))
0027 
0028     nruns = len(jsonind)
0029     nlumis = sum(len(v) for v in jsonind.values())
0030     jsonmap = {}
0031     for r,lumis in jsonind.items():
0032         if len(lumis) == 0: continue # shouldn't happen
0033         lumis.sort()
0034         ranges = [ [ lumis[0], lumis[0] ] ]
0035         for lumi in lumis[1:]:
0036             if lumi == ranges[-1][1] + 1:
0037                 ranges[-1][1] = lumi
0038             else:
0039                 ranges.append([lumi,lumi])
0040         jsonmap[r] = ranges
0041     return (jsonmap, nruns, nlumis)
0042 
0043 if __name__ == '__main__':
0044     parser = OptionParser(usage='%prog <target_directories> [options]',
0045                           description='Check the output of the JSONAnalyzer and produce a json file of the processed runs and lumisections')
0046     parser.add_option("-a", "--analyzer", dest="jsonAnalyzer", default="JSONAnalyzer", help="Name of the JSONAnalyzer")
0047     parser.add_option("-t", "--tree", dest="treeName", default="RLTInfo", help="Name of the TTree produced by the JSONAnalyzer")
0048     parser.add_option("-o", "--out", dest="outputFile", default="lumiSummary.json", help="Name of the output file")
0049     (options,args) = parser.parse_args()
0050     if len(args)==0:
0051         print('provide at least one directory in argument. Use -h to display help')
0052         exit()
0053     for a in args:
0054         summary = root2map(a,options.jsonAnalyzer,options.treeName)
0055         if summary:
0056             oname = "%s/%s" % (a,options.outputFile)
0057             jmap, runs, lumis = summary
0058             json.dump(jmap,open(oname,'w'))
0059             print("Saved %s (%d runs, %d lumis)" % (oname, runs, lumis))