Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:39

0001 ################################################
0002 ##
0003 ## Example usage: python plot2JsonTimeSeries.py --file json_vanilla_short.json --label 'master branch' --file2 json_short.json --label2 'this PR'
0004 ##
0005 ################################################
0006 
0007 from __future__ import print_function
0008 import json
0009 import ROOT
0010 from array import array
0011 from pprint import pprint
0012 from optparse import OptionParser
0013 
0014 ROOT.gROOT.SetBatch(True)
0015 ROOT.TGaxis.SetMaxDigits(6);
0016 
0017 parser = OptionParser()
0018 parser.add_option("-f", "--file", dest="filename",
0019                   help="open FILE and extract info", metavar="FILE")
0020 
0021 parser.add_option("-l", "--label", dest="label",
0022                   help="label for the first file", metavar="LABEL")
0023 
0024 parser.add_option("-g", "--file2", dest="filename2",
0025                   help="open FILE 2 and extract info", metavar="FILE2")
0026 
0027 parser.add_option("-m", "--label2", dest="label2",
0028                   help="label for the second file", metavar="LABEL2")
0029 
0030 parser.add_option("-q", "--quiet",
0031                   action="store_false", dest="verbose", default=False,
0032                   help="don't print status messages to stdout")
0033 
0034 (options, args) = parser.parse_args()
0035 
0036 with open(options.filename) as data_file:    
0037     data = json.load(data_file)
0038     values = data["data"]
0039     annotations = data["annotations"]
0040     title   = annotations["title"]
0041     x_label = annotations["x_label"]
0042     y_label = annotations["y_label"]
0043 
0044 
0045 with open(options.filename2) as data_file2:    
0046     data2 = json.load(data_file2)
0047     values2 = data2["data"]
0048     annotations2 = data2["annotations"]
0049     title2   = annotations2["title"]
0050     x_label2 = annotations2["x_label"]
0051     y_label2 = annotations2["y_label"]
0052 
0053 bins=len(values)
0054 bins2=len(values2)
0055 
0056 '''
0057 ahhh the magic of list comprehension
0058 $%#&!@ excepted it does not work!!!
0059 
0060 xvalues = [values[i]['x'] for i in range(0,bins)]
0061 yvalues = [values[i]['y'] for i in range(0,bins)]
0062 xvalues2 = [values2[i]['x'] for i in range(0,bins2)]
0063 yvalues2 = [values2[i]['y'] for i in range(0,bins2)]
0064 
0065 print(xvalues,yvalues)
0066 print(xvalues2,yvalues2)
0067 '''
0068 
0069 xvalues, yvalues = array( 'd' ), array( 'd' )
0070 xvalues2, yvalues2 = array( 'd' ), array( 'd' )
0071 
0072 for i in range (bins):
0073     xvalues.append(values[i]['x'])
0074     yvalues.append(values[i]['y'])
0075 
0076 for i in range (bins2):
0077     xvalues2.append(values2[i]['x'])
0078     yvalues2.append(values2[i]['y'])
0079 
0080 graph1=ROOT.TGraph(bins,xvalues,yvalues)
0081 graph2=ROOT.TGraph(bins2,xvalues2,yvalues2)
0082 
0083 canv=ROOT.TCanvas("c1","c1",1200,800)
0084 canv.SetGrid()
0085 canv.cd()
0086 
0087 graph1.SetTitle(title)
0088 graph1.GetXaxis().SetTitle(x_label)
0089 graph1.GetYaxis().SetTitle(y_label)
0090 
0091 graph1.SetMarkerSize(1.4)
0092 graph2.SetMarkerSize(1.5)
0093 
0094 graph1.SetMarkerStyle(ROOT.kOpenCircle)
0095 graph2.SetMarkerStyle(ROOT.kFullSquare)
0096 
0097 graph1.SetMarkerColor(ROOT.kRed)
0098 graph2.SetMarkerColor(ROOT.kBlue)
0099 
0100 graph1.SetLineColor(ROOT.kRed)
0101 graph2.SetLineColor(ROOT.kBlue)
0102 
0103 graph1.GetYaxis().SetRangeUser(-0.1,5.)
0104 
0105 graph1.Draw("APL")
0106 graph2.Draw("PLSame")
0107 
0108 TLegend = ROOT.TLegend(0.10,0.80,0.40,0.90)
0109 TLegend.AddEntry(graph1,options.label,"LP")
0110 TLegend.AddEntry(graph2,options.label2,"LP")
0111 
0112 TLegend.Draw("same")
0113 
0114 canv.Update()
0115 #canv.GetFrame().SetFillColor( 11 )
0116 canv.GetFrame().SetBorderSize( 12 )
0117 canv.Modified()
0118 canv.Update()
0119 
0120 outfilename="comparison_"+options.label+"_vs_"+options.label2+"_json.png"
0121 canv.SaveAs(outfilename.replace(" ",""))