def plot_ratio(ih_,max_val_,xbins__,comb_ID_): # // Define two gaussian histograms. Note the X and Y title are defined # // at booking time using the convention "Hist_title ; X_title ; Y_title" # TH1F *h1 = new TH1F("h1", "Two gaussian plots and their ratio;x title; h1 and h2 gaussian histograms", 100, -5, 5); # TH1F *h2 = new TH1F("h2", "h2", 100, -5, 5); # h1.FillRandom("gaus"); # h2.FillRandom("gaus"); # // Define the Canvas #TCanvas *c = new TCanvas("c", "canvas", 800, 800); cc = TCanvas("cc", "canvas", 800, 800) h1 = ih_[0][0] h2 = ih_[1][0] #print 'h1 = ',h1 #print 'h2 = ',h2 #print'h1.GetMaximum() = ' ,h1.GetMaximum() #// Upper plot will be in pad1 pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0) pad1.SetBottomMargin(0) # Upper and lower plot are joined #pad1.SetGridx() #Vertical grid, dashed lines pad1.Draw() #Draw the upper pad: pad1 pad1.cd() # pad1 becomes the current pad h1.SetStats(0) # No statistics on upper plot h1.GetXaxis().SetNdivisions(xbins__) h1.Draw() # Draw h1 # pad1.Update() # lline = TLine(pad1.GetUxmin(),20,pad1.GetUxmax(),20) # #lline.SetNDC(1) # lline.SetLineStyle(3) # lline.Draw('same') h2.Draw("same") # Draw h2 on top of h1 # // Do not draw the Y axis label on the upper plot and redraw a small # // axis instead, in order to avoid the first label (0) to be clipped. h1.GetYaxis().SetLabelSize(0.) #TGaxis *axis = new TGaxis( -5, 20, -5, 220, 20,220,510,""); #axis = TGaxis( -5, 20, -5, 220, 20,220,510,"") #xmin ymin xmax ymax axis = TGaxis( 0, 0, 0, max_val_, 0.001,max_val_,510,"") axis.SetLabelFont(43) #Absolute font size in pixel (precision 3) axis.SetLabelSize(15) axis.Draw() #lower plot will be in pad cc.cd() # Go back to the main canvas before defining pad2 #TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3) pad2 = TPad("pad2", "pad2", 0, 0.05, 1, 0.3) pad2.SetTopMargin(0) # can change to separate top and bottom #pad2.SetBottomMargin(0.2) #pad2.SetBottomMargin(0) #pad2.SetGridx() # vertical grid, dashed lines pad2.Draw() pad2.cd() # pad2 becomes the current pad # Define the ratio plot #TH1F *h3 = (TH1F*)h1.Clone("h3"); h3 = h2.Clone("h3") h3.SetLineColor(1) h3.SetMinimum(0.5) # Define Y .. h3.SetMaximum(1.5) # .. range h3.Sumw2() h3.SetStats(0) # No statistics on lower plot h3.Divide(h1) h3.SetMarkerStyle(21) #gPad.Modified() #gPad.Update() h3.Draw("ep") # Draw the ratio plot pad2.Update() lline = TLine(pad2.GetUxmin(),1,pad2.GetUxmax(),1) #lline.SetNDC(1) lline.SetLineStyle(1) lline.Draw('same') #// h1 settings h1.SetLineColor(600+1) h1.SetLineWidth(2) #// Y axis h1 plot settings h1.GetYaxis().SetTitleSize(20) h1.GetYaxis().SetTitleFont(43) h1.GetYaxis().SetTitleOffset(1.55) #print 'xbins__ = ',xbins__ h1.GetXaxis().SetNdivisions(xbins__) #h1.GetXaxis().SetNdivisions(0) #// h2 settings h2.SetLineColor(632) h2.SetLineWidth(2) # // Ratio plot (h3) settings h3.SetTitle("") # Remove the ratio title #// Y axis ratio plot settings h3.GetYaxis().SetTitle("Reco/Gen") h3.GetYaxis().SetNdivisions(505) h3.GetYaxis().SetTitleSize(20) h3.GetYaxis().SetTitleFont(43) h3.GetYaxis().SetTitleOffset(1.55) h3.GetYaxis().SetLabelFont(43) #Absolute font size in pixel (precision 3) h3.GetYaxis().SetLabelSize(15) # // X axis ratio plot settings #h3.GetXaxis().SetNdivisions(xbins__) h3.GetXaxis().SetNdivisions(xbins__) #h3.GetXaxis().SetNdivisions(0) h3.GetXaxis().SetTitleSize(20) h3.GetXaxis().SetTitleFont(43) h3.GetXaxis().SetTitleOffset(4.) h3.GetXaxis().SetLabelFont(43) # Absolute font size in pixel (precision 3) h3.GetXaxis().SetLabelSize(15) pad1.cd() leg_ = TLegend(0.6, 0.75, 0.89, 0.89) #for hi_,h_ in enumerate(hists_): a = ih_[:] #print'a = ',a #for i,hist_info_ in enumerate(a): for hist_info_ in a: this_h = hist_info_[0] this_label = hist_info_[1] leg_.AddEntry(this_h,this_label,'lf') # histo object, ID #leg.SetTextSize(0.02) leg_.Draw('same') #cc.SaveAs(output_Loc + "Gen_Reco_" + comb_ID_ + ".png") cc.SaveAs(output_Loc + "Gen_Reco_" + comb_ID_ + ".pdf") cc.SaveAs(output_Loc + "Gen_Reco_" + comb_ID_ + ".png") return 0
def plotPerDirectionBx(options): """Save directional fit plots (per BX) to PDF files""" name = options['scan'] + '_' + options['name'] + options['fitted'] if 'method' in options: name += '_' + options['method'] name += '_collected' f = openRootFileR(name) for bx in options['crossings']: plotname = plotName(name + '_bx' + str(bx), timestamp=False) filename = plotName(name + '_bx' + str(bx), timestamp=True) filepath = plotPath(name + '_bx' + str(bx), timestamp=True) print '<<< Save plot:', filepath graphs = f.Get(plotname) residuals = f.Get(plotname + '_residuals') if 'final' in options: graphs.SetTitle('') residuals.SetTitle('') gStyle.SetOptFit(options['optfit']) canvas = TCanvas(plotname + '_canvas', '', 700, 600) canvas.cd() canvas.SetMargin(0.13, 0.03, 0.33, 0.05) graphs.Draw('AP') gPad.Update() text = TLatex() text.SetNDC() for j, graph in enumerate(graphs.GetListOfGraphs()): graph.SetMarkerStyle(21) graph.SetMarkerColor(2 + 2 * j) graph.GetFunction(options['fit']).SetLineColor(2 + 2 * j) stats = graph.GetListOfFunctions().FindObject('stats') stats.SetTextColor(2 + 2 * j) stats.SetBorderSize(0) stats.SetTextSize(0.04) inverted = graph.GetFunction( options['fit']).GetParameter('p1') < 0.0 text.SetTextFont(42) text.SetTextSize(0.04) text.SetTextColor(2 + 2 * j) if inverted and j == 0: text.DrawLatex(0.18, 0.54, options['scan'] + ' scan forward') stats.SetX1NDC(0.16) stats.SetX2NDC(0.53) stats.SetY1NDC(0.38) stats.SetY2NDC(0.53) elif inverted and j == 1: text.DrawLatex(0.61, 0.9, options['scan'] + ' scan backward') stats.SetX1NDC(0.59) stats.SetX2NDC(0.96) stats.SetY1NDC(0.74) stats.SetY2NDC(0.89) elif j == 0: text.DrawLatex(0.18, 0.9, options['scan'] + ' scan forward') stats.SetX1NDC(0.16) stats.SetX2NDC(0.53) stats.SetY1NDC(0.74) stats.SetY2NDC(0.89) else: text.DrawLatex(0.61, 0.54, options['scan'] + ' scan backward') stats.SetX1NDC(0.59) stats.SetX2NDC(0.96) stats.SetY1NDC(0.38) stats.SetY2NDC(0.53) graphs.GetXaxis().SetTitle('Nominal Position [#mum]') graphs.GetYaxis().SetTitle(options['ytitle']) graphs.GetYaxis().SetTitleOffset(1.3) if ('final' in options): text.SetTextColor(1) text.SetTextFont(42) text.SetTextSize(0.04) text.SetTextAlign(31) text.DrawLatex(0.97, 0.96, O['plotsig']) text.SetTextAlign(11) if options['final'] == 'wip': text.SetTextFont(52) text.SetTextSize(0.04) text.DrawLatex(0.13, 0.96, 'Work in Progress') else: text.SetTextFont(62) text.SetTextSize(0.05) text.DrawLatex(0.13, 0.96, 'CMS') text.SetTextFont(52) text.SetTextSize(0.04) text.DrawLatex(0.22, 0.96, 'Preliminary') for axis in [graphs.GetYaxis(), graphs.GetXaxis()]: axis.SetTitleSize(0.05) axis.SetLabelSize(0.04) axis.SetLabelOffset(0.01) axis.CenterTitle() pad = TPad('pad', 'pad', 0, 0, 1, 0.2) pad.Draw() pad.cd() pad.SetMargin(0.13, 0.03, 0.01, 0.01) for j, residual in enumerate(residuals.GetListOfGraphs()): residual.SetMarkerStyle(21) residual.SetMarkerColor(2 + 2 * j) residuals.Draw("AP") residuals.GetXaxis().SetTitle('') residuals.GetXaxis().SetLabelSize(0.0) residuals.GetXaxis().SetTickSize(0.151) residuals.GetYaxis().SetNdivisions(305) residuals.GetYaxis().SetTickSize(0.019) residuals.GetYaxis().SetLabelSize(0.2) residuals.GetYaxis().SetLabelOffset(0.01) pad.Update() line = TLine(pad.GetUxmin(), 0.0, pad.GetUxmax(), 0.0) line.SetLineColor(14) line.SetLineStyle(3) line.Draw() canvas.cd() text.SetTextFont(42) text.SetTextSize(0.05) text.SetTextAngle(90.0) text.DrawLatex(0.035, 0.0, 'Resid. ' + options['restitle']) if not 'final' in options: drawSignature(filename) # canvas.Modified() # canvas.Update() canvas.Print(filepath) canvas.Close() closeRootFile(f, name)
def makeTwoScalesGraph(file): print(file) ch1 = OPData.fromPath('../data/part3/%s' % file, 1) ch2 = OPData.fromPath('../data/part3/%s' % file, 2) print('make canvas + pad') c = TCanvas('c-%s' % file, '', 1280, 720) pad = TPad('pad-%s' % file, '', 0, 0, 1, 1) pad.Draw() pad.cd() print('frame') frame = pad.DrawFrame(0, min(ch1.getY()) * 1.1, 0.051, max(ch1.getY()) * 1.1) frame.SetXTitle('Zeit t / s') frame.GetXaxis().CenterTitle() frame.SetYTitle('Spannung Spule 2: U_{2} / V') frame.GetYaxis().CenterTitle() frame.GetYaxis().SetLabelColor(4) frame.GetYaxis().SetTitleColor(4) print('g1') g1 = ch1.makeGraph('g1-%s' % file) prepareGraph(g1) g1.Draw('PX') print('overlay') c.cd() overlay = TPad('overlay-%s' % file, '', 0, 0, 1, 1) overlay.SetFillStyle(4000) # transparent overlay.SetFillColor(0) # white overlay.SetFrameFillStyle(4000) # transparent overlay.Draw() overlay.cd() print('g2') g2 = ch2.makeGraph('g2-%s' % file) prepareGraph(g2, 2) g2ymin = min(ch2.getY()) xmin = pad.GetUxmin() xmax = pad.GetUxmax() ymin = 1.1 * g2ymin ymax = abs(ymin) if file == '07.tab': # same scale like 06.tab ymin, ymax = -0.07128, 0.07128 oframe = overlay.DrawFrame(xmin, ymin, xmax, ymax) oframe.GetXaxis().SetLabelOffset(99) oframe.GetYaxis().SetLabelOffset(99) oframe.GetYaxis().SetTickLength(0) g2.Draw('PX') print('axis') axis = TGaxis(xmax, ymin, xmax, ymax, ymin, ymax, 510, "+L") axis.SetTitle('Spannung Photodiode: U_{ph} / V') axis.CenterTitle() axis.SetTitleOffset(1.2) axis.Draw() print('print') c.Update() c.Print('../img/part3/%s.pdf' % file[:-4], 'pdf')