Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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