def DrawScat2XLine(graph, title, fname, ycoord1, ycoord2): #, grid): c = TCanvas("c", "c", 800, 600) graph.SetTitle(title) graph.GetXaxis().SetTitleSize(.04) graph.GetYaxis().SetTitleSize(.04) graph.GetXaxis().SetTitleOffset(1.2) graph.GetYaxis().SetTitleOffset(1.25) graph.GetXaxis().CenterTitle(1) graph.GetYaxis().CenterTitle(1) graph.GetYaxis().SetMaxDigits(4) graph.SetMarkerStyle(20) # Full circle graph.Draw() gPad.Update() line1 = TLine(gPad.GetUxmin(), ycoord1, gPad.GetUxmax(), ycoord1) line2 = TLine(gPad.GetUxmin(), ycoord2, gPad.GetUxmax(), ycoord2) # print(gPad.GetUymin(),gPad.GetUymax()) line1.SetLineStyle(2) line1.SetLineColor(2) line1.SetLineWidth(3) line2.SetLineStyle(2) line2.SetLineColor(2) line2.SetLineWidth(3) graph.Draw("AP") line1.Draw("same") line2.Draw("same") c.SaveAs(fname + ".C") c.SaveAs(fname + ".pdf") return
def get_rank_section(directory): # do Rank histo png imgname = "RankSummary.png" gStyle.SetPadTickY(0) c = TCanvas("ranks", "ranks", 500, 400) #gStyle.SetOptStat(0) c.cd() h = directory.rank_histo rank_histof = TH1F(h.GetName(), "", h.GetNbinsX(), h.GetXaxis().GetXmin(), h.GetXaxis().GetXmax()) rank_histof.SetLineWidth(2) for i in xrange(0, h.GetNbinsX() + 1): rank_histof.SetBinContent(i, h.GetBinContent(i)) h.SetTitle("Ranks Summary;Rank;Frequency") h.Draw("Hist") c.Update() rank_histof.ComputeIntegral() integral = rank_histof.GetIntegral() rank_histof.SetContent(integral) rightmax = 1.1 * rank_histof.GetMaximum() scale = gPad.GetUymax() / rightmax rank_histof.SetLineColor(kRed) rank_histof.Scale(scale) rank_histof.Draw("same") #draw an axis on the right side axis = TGaxis(gPad.GetUxmax(), gPad.GetUymin(), gPad.GetUxmax(), gPad.GetUymax(), 0, rightmax, 510, "+L") axis.SetTitle("Cumulative") axis.SetTitleColor(kRed) axis.SetLineColor(kRed) axis.SetLabelColor(kRed) axis.Draw() rank_histof.Draw("Same") c.Print(imgname) page_html = '<div class="span-20"><h2 class="alt"><a name="rank_summary">Ranks Summary</a></h2>' page_html += '<div class="span-19"><img src="%s"></div>' % imgname page_html += '</div> <a href="#top">Top...</a><hr>' return page_html
def plotGraph(self, x='vv', y='acc'): ''' Plot a graph with specified quantities on x and y axes , and saves the graph ''' if (x not in self.quantities.keys()) or (y not in self.quantities.keys()): raise RuntimeError('selected quantities not available, available quantities are: \n{}'.format(self.quantities.keys())) xq = self.quantities[x] yq = self.quantities[y] #graph = TGraphAsymmErrors() #graph = TGraph() graph = TGraphErrors() for i,s in enumerate(self.samples): graph.SetPoint(i,getattr(s, xq.name), getattr(s, yq.name) ) #if xq.err: # graph.SetPointEXhigh(i, getattr(s, xq.name+'_errup')) # errup errdn # graph.SetPointEXlow (i, getattr(s, xq.name+'_errdn')) if yq.err: graph.SetPointError(i, 0, getattr(s, yq.name+'_errup')) # graph.SetPointEYhigh(i, getattr(s, yq.name+'_errup')) # graph.SetPointEYlow (i, getattr(s, yq.name+'_errdn')) c = TCanvas() graph.SetLineWidth(2) graph.SetMarkerStyle(22) graph.SetTitle(';{x};{y}'.format(y=yq.title,x=xq.title)) graph.Draw('APLE') if yq.forceRange: graph.SetMinimum(yq.Range[0]) graph.SetMaximum(yq.Range[1]) gPad.Modified() gPad.Update() if yq.name=='expNevts': line = TLine(gPad.GetUxmin(),3,gPad.GetUxmax(),3) line.SetLineColor(ROOT.kBlue) line.Draw('same') #graph.SetMinimum(0.01) #graph.SetMaximum(1E06) if xq.log: c.SetLogx() if yq.log: c.SetLogy() c.SetGridx() c.SetGridy() c.SaveAs('./plots/{}{}/{}_{}VS{}.pdf'.format(self.label,suffix,self.name,yq.name,xq.name)) c.SaveAs('./plots/{}{}/{}_{}VS{}.C'.format(self.label,suffix,self.name,yq.name,xq.name)) c.SaveAs('./plots/{}{}/{}_{}VS{}.png'.format(self.label,suffix,self.name,yq.name,xq.name)) self.graphs['{}VS{}'.format(yq.name,xq.name)] = graph # add the graph container for memory? graph_saver.append(graph)
def __call__(self): h = gPad.GetSelected() if not h: return if not isinstance(h, TH2): return gPad.GetCanvas().FeedbackMode(kTRUE) # erase old position and draw a line at current position px = gPad.GetEventX() py = gPad.GetEventY() uxmin, uxmax = gPad.GetUxmin(), gPad.GetUxmax() uymin, uymax = gPad.GetUymin(), gPad.GetUymax() pxmin, pxmax = gPad.XtoAbsPixel(uxmin), gPad.XtoAbsPixel(uxmax) pymin, pymax = gPad.YtoAbsPixel(uymin), gPad.YtoAbsPixel(uymax) if self._old != None: gVirtualX.DrawLine(pxmin, self._old[1], pxmax, self._old[1]) gVirtualX.DrawLine(self._old[0], pymin, self._old[0], pymax) gVirtualX.DrawLine(pxmin, py, pxmax, py) gVirtualX.DrawLine(px, pymin, px, pymax) self._old = px, py upx = gPad.AbsPixeltoX(px) x = gPad.PadtoX(upx) upy = gPad.AbsPixeltoY(py) y = gPad.PadtoY(upy) padsav = gPad # create or set the display canvases if not self._cX: self._cX = TCanvas('c2', 'Projection Canvas in X', 730, 10, 700, 500) else: self._DestroyPrimitive('X') if not self._cY: self._cY = TCanvas('c3', 'Projection Canvas in Y', 10, 550, 700, 500) else: self._DestroyPrimitive('Y') self.DrawSlice(h, y, 'Y') self.DrawSlice(h, x, 'X') padsav.cd()
def __call__( self ): h = self.hname if not h: return if not isinstance( h, TH2 ): return gPad.GetCanvas().FeedbackMode( kTRUE ) event = gPad.GetEvent() if (event==1): # left mouse click self.projection = not self.projection elif (event==12): # middle mouse click self.logy = not self.logy # erase old position and draw a line at current position #(gPad coordinate system) px = gPad.GetEventX() py = gPad.GetEventY() # min/max x values visible on the pad (xmin and xmax of the x-axis) uxmin, uxmax = gPad.GetUxmin(), gPad.GetUxmax() uymin, uymax = gPad.GetUymin(), gPad.GetUymax() # same in pixels pxmin, pxmax = gPad.XtoAbsPixel( uxmin ), gPad.XtoAbsPixel( uxmax ) pymin, pymax = gPad.YtoAbsPixel( uymin ), gPad.YtoAbsPixel( uymax ) # if self.projection: # axis = h.GetYaxis() # else: # axis = h.GetXaxis() scaleY = abs((pymax-pymin) / (uymax-uymin)) scaleX = abs((pxmax-pxmin) / (uxmax-uxmin)) width = 0.0 # arbitrary default value [cm] if self._old: width = self.range[1] - self.range[0] ywidth = int(width * scaleY) xwidth = int(width * scaleX) if self._old != None: if self.projection: # gVirtualX.DrawBox( pxmin, self._old[1]-ywidth, pxmax, self._old[1], kSolid) gVirtualX.DrawLine( pxmin, self._old[1], pxmax, self._old[1]) gVirtualX.DrawLine( pxmin, self._old[1]-ywidth, pxmax, self._old[1]-ywidth) else: # gVirtualX.DrawBox( self._old[0], pymin, self._old[0]+ywidth, pymax, kSolid ) gVirtualX.DrawLine( self._old[0], pymin, self._old[0], pymax) gVirtualX.DrawLine( self._old[0]+xwidth, pymin, self._old[0]+xwidth, pymax) # Normally these calls remove old lines, but we do not need them since we update the pad in the end of DrawSlice # BUG: the lines disappear when the mouse does not move. This happens if this gPad.Update() is called. TODO: How to fix it? # if self.projection: # gVirtualX.DrawLine( pxmin, py, pxmax, py ) # gVirtualX.DrawLine( pxmin, py-ywidth, pxmax, py-ywidth ) # else: # gVirtualX.DrawLine( px, pymin, px, pymax) # gVirtualX.DrawLine( px+xwidth, pymin, px+xwidth, pymax) if self.projection: gPad.SetUniqueID(py) else: gPad.SetUniqueID(px) self._old = px, py upx = gPad.AbsPixeltoX( px ) x = gPad.PadtoX( upx ) upy = gPad.AbsPixeltoY( py ) y = gPad.PadtoY( upy ) padsav = gPad # create or set the display canvases if not self._cX: self._cX = gPad.GetCanvas().GetPad(2) else: self._DestroyPrimitive( 'X' ) if not self._cY: self._cY = gPad.GetCanvas().GetPad(2) else: self._DestroyPrimitive( 'Y' ) if self.projection: self.range = self.DrawSlice( h, y, 'Y' ) else: self.range = self.DrawSlice( h, x, 'X' ) padsav.cd()
def SAME3VsLumi(g1, g2, g3, title, ptype, lineMC1, lineDATA1, lineMC2, lineDATA2, lineMC3, lineDATA3, DoInclusive, dataPeriod): canvas = makeCMSCanvas(str(random.random()),"canvas",900,700) canvas.cd() graph2=copy.deepcopy(g2) graph3=copy.deepcopy(g3) graph2.SetMarkerColor(kBlue)#electrons graph3.SetMarkerColor(kRed)#muons multigraph = TMultiGraph() if(DoInclusive): graph1=copy.deepcopy(g1) multigraph.Add(graph1,"AP") multigraph.Add(graph2,"AP") multigraph.Add(graph3,"AP") multigraph.Draw("AP") TGaxis.SetMaxDigits(2) TGaxis.SetExponentOffset(-0.05, 0.02, "y") multigraph.GetXaxis().SetTitle("L [fb^{-1}]") multigraph.GetYaxis().SetTitleOffset(1.4) if(ptype == "Zmass"): multigraph.GetYaxis().SetTitle("M_{Z} [GeV]") # multigraph.GetYaxis().SetTitle("M_{l^{+}l^{-}} [GeV]") multigraph.SetMaximum(max(multigraph.GetHistogram().GetMaximum(),91.4)) multigraph.SetMinimum(min(multigraph.GetHistogram().GetMinimum(),89.6)) elif(ptype == "Zwidth"): multigraph.GetYaxis().SetTitle("#Gamma_{Z} [GeV]") elif(ptype == "Zmult"): multigraph.GetYaxis().SetTitle("#Z / fb^{-1}") if(not DoInclusive) : multigraph.SetMaximum(max(multigraph.GetHistogram().GetMaximum(),60000.)) # set y axis minimum at 60000. multigraph.SetMinimum(0.) # set y axis minimum at 0. # multigraph.SetMaximum(60000.) #second type: vs 2016 plots # multigraph.SetMinimum(25000.) printLumiPrelOut(canvas) # Draw legend legend = TLegend(0.93,0.84,0.99,0.93) if(DoInclusive): #legend.AddEntry(graph1,"inclusive","P") legend.AddEntry(graph1,"BB","P") legend.AddEntry(graph2,"BE","P") legend.AddEntry(graph3,"EE","P") else : legend.AddEntry(graph2,"e^{+}e^{-}","P") legend.AddEntry(graph3,"#mu^{+}#mu^{-}","P") legend.SetFillColor(kWhite) legend.SetLineColor(kBlack) legend.SetTextFont(43) legend.SetTextSize(20) legend.Draw() canvas.Update() # Draw letters for data-taking periods if(dataPeriod == "data2017"): textLetters = TLatex() textLetters.SetTextColor(kGray+1) textLetters.SetTextSize(0.03) if(ptype == "Zmass"): textLetters.DrawLatex(2., gPad.GetUymin()+0.2,"B") textLetters.DrawLatex(9.5, gPad.GetUymin()+0.2,"C") textLetters.DrawLatex(16., gPad.GetUymin()+0.2,"D") textLetters.DrawLatex(23., gPad.GetUymin()+0.2,"E") textLetters.DrawLatex(36., gPad.GetUymin()+0.2,"F") elif(ptype == "Zwidth"): textLetters.DrawLatex(2., gPad.GetUymin()+0.3,"B") textLetters.DrawLatex(9.5, gPad.GetUymin()+0.3,"C") textLetters.DrawLatex(16., gPad.GetUymin()+0.3,"D") textLetters.DrawLatex(23., gPad.GetUymin()+0.3,"E") textLetters.DrawLatex(36., gPad.GetUymin()+0.3,"F") elif(ptype == "Zmult") : textLetters.DrawLatex(2., 260000,"B") textLetters.DrawLatex(9.5, 260000,"C") textLetters.DrawLatex(16., 260000,"D") textLetters.DrawLatex(23., 260000,"E") textLetters.DrawLatex(36., 260000,"F") if(dataPeriod == "data2018"): textLetters = TLatex() textLetters.SetTextColor(kGray+1) textLetters.SetTextSize(0.03) if(ptype == "Zmass"): textLetters.DrawLatex(6., gPad.GetUymin() + 0.6,"A") textLetters.DrawLatex(16., gPad.GetUymin() + 0.6,"B") textLetters.DrawLatex(23., gPad.GetUymin() + 0.6,"C") textLetters.DrawLatex(43., gPad.GetUymin() + 0.6,"D") elif(ptype == "Zwidth"): textLetters.DrawLatex(6., gPad.GetUymin() +0.3,"A") textLetters.DrawLatex(16., gPad.GetUymin() +0.3,"B") textLetters.DrawLatex(23., gPad.GetUymin() +0.3,"C") textLetters.DrawLatex(43., gPad.GetUymin() +0.3,"D") elif(ptype == "Zmult") : textLetters.DrawLatex(6., 260000,"A") textLetters.DrawLatex(16., 260000,"B") textLetters.DrawLatex(23., 260000,"C") textLetters.DrawLatex(43., 260000,"D") # **** if(dataPeriod == "data2018"): # draw vertical lines that divide different data taking periods down = gPad.GetUymin() up = gPad.GetUymax() lineA = TLine(13.48, down, 13.48, up) # Run2018A up to 13.48 fb-1 lineA.SetLineColor(kBlack) lineA.SetLineStyle(2) lineA.Draw() lineB = TLine(20.265, down, 20.265, up) # Run2018B up to 20.265 fb-1 lineB.SetLineColor(kBlack) lineB.SetLineStyle(2) lineB.Draw() lineC = TLine(26.877, down, 26.877, up) # Run2018C up to 26.877 fb-1 lineC.SetLineColor(kBlack) lineC.SetLineStyle(2) lineC.Draw() if(dataPeriod == "data2017"): # draw vertical lines that divide different data taking periods down = gPad.GetUymin() up = gPad.GetUymax() lineB = TLine(4.793, down, 4.793, up) # Run2017B up to 4.793 fb-1 lineB.SetLineColor(kBlack) lineB.SetLineStyle(2) lineB.Draw() lineC = TLine(14.549, down, 14.549, up) # Run2017C up to 14.549 fb-1 lineC.SetLineColor(kBlack) lineC.SetLineStyle(2) lineC.Draw() lineD = TLine(18.868, down, 18.868, up) # Run2017D up to 18.868 fb-1 lineD.SetLineColor(kBlack) lineD.SetLineStyle(2) lineD.Draw() lineE = TLine(28.293, down, 28.293, up) # Run2017E up to 28.293 fb-1 lineE.SetLineColor(kBlack) lineE.SetLineStyle(2) lineE.Draw() if(dataPeriod == "data2016"): # draw vertical lines that divide different data taking periods down = gPad.GetUymin() up = gPad.GetUymax() lineB = TLine(5.789, down, 5.789, up) # Run2016B up to 5.789 fb-1 lineB.SetLineColor(kBlack) lineB.SetLineStyle(2) lineB.Draw() lineC = TLine(8.366, down, 8.366, up) # Run2016C up to 8.366 fb-1 lineC.SetLineColor(kBlack) lineC.SetLineStyle(2) lineC.Draw() lineD = TLine(12.616, down, 12.616, up) # Run2016D up to 12.616 fb-1 lineD.SetLineColor(kBlack) lineD.SetLineStyle(2) lineD.Draw() lineE = TLine(16.624, down, 16.624, up) # Run2016E up to 16.624 fb-1 lineE.SetLineColor(kBlack) lineE.SetLineStyle(2) lineE.Draw() lineF = TLine(19.725, down, 19.725, up) # Run2016F up to 19.725 fb-1 lineF.SetLineColor(kBlack) lineF.SetLineStyle(2) lineF.Draw() lineG = TLine(27.268, down, 27.268, up) # Run2016G up to 27.268 fb-1 lineG.SetLineColor(kBlack) lineG.SetLineStyle(2) lineG.Draw() # **** # draw orizontal lines for MC and DATA fit if(ptype == "Zmass" or ptype == "Zwidth") : leftEnd = gPad.GetUxmin() rightEnd = gPad.GetUxmax() if(DoInclusive): line1 = TLine(leftEnd,lineMC1,rightEnd,lineMC1) line1.SetLineColor(kBlack) line1.SetLineStyle(1) line1.Draw() line2 = TLine(leftEnd,lineDATA1,rightEnd,lineDATA1) line2.SetLineColor(kBlack) line2.SetLineStyle(2) line2.Draw() # line for graph 2: color blue line3 = TLine(leftEnd,lineMC2,rightEnd,lineMC2) line3.SetLineColor(kBlue) line3.SetLineStyle(1) line3.Draw() line4 = TLine(leftEnd,lineDATA2,rightEnd,lineDATA2) line4.SetLineColor(kBlue) line4.SetLineStyle(2) line4.Draw() # line for graph 3: color red line5 = TLine(leftEnd,lineMC3,rightEnd,lineMC3) line5.SetLineColor(kRed) line5.SetLineStyle(1) line5.Draw() line6 = TLine(leftEnd,lineDATA3,rightEnd,lineDATA3) line6.SetLineColor(kRed) line6.SetLineStyle(2) line6.Draw() # *** canvas.SaveAs(title + ".root") canvas.SaveAs(title + ".pdf") canvas.SaveAs(title + ".png") return;
all_graphs[0].SetMaximum(calo_init.args.axisMax) if calo_init.args.axisMin: all_graphs[0].SetMinimum(calo_init.args.axisMin) canv.Update() lines = [] for iLine, line in enumerate(avgSF): lines.append(TLine(0, avgSF[iLine], Nslices * sliceWidth, avgSF[iLine])) lines[iLine].SetLineColor(iLine + 1) lines[iLine].Draw('same') # add second axis canv.SetRightMargin(0.1) all_graphs[0].GetXaxis().SetRangeUser(0, 68) all_graphs[0].GetXaxis().SetLabelOffset(0.02) gPad.RangeAxis(0, gPad.GetUymin(), 68, gPad.GetUxmax()) axis = TGaxis(gPad.GetUxmin(), gPad.GetUymin(), gPad.GetUxmax(), gPad.GetUymin(), gPad.GetUxmin() * calo_init.args.X0density, gPad.GetUxmax() * calo_init.args.X0density, 506, "-") axis.SetLabelSize(0.05) axis.SetTitleSize(0.07) axis.SetTitleOffset(0.9) axis.SetLabelOffset(0.02) axis.Draw() axis.SetLabelFont(42) unit1 = draw_text(['(cm)'], [0.91, 0.09, 1, 0.14], 1, 0) unit1.SetTextSize(0.05) unit1.SetTextFont(42) unit2 = draw_text(['(X_{0})'], [0.91, 0.16, 1, 0.21], 1, 0) unit2.SetTextSize(0.05)
def __call__(self): h = self.hname if not h: return if not isinstance(h, TH2): return gPad.GetCanvas().FeedbackMode(kTRUE) event = gPad.GetEvent() if (event == 1): # left mouse click self.projection = not self.projection elif (event == 12): # middle mouse click self.logy = not self.logy # erase old position and draw a line at current position #(gPad coordinate system) px = gPad.GetEventX() py = gPad.GetEventY() uxmin, uxmax = gPad.GetUxmin(), gPad.GetUxmax() uymin, uymax = gPad.GetUymin(), gPad.GetUymax() pxmin, pxmax = gPad.XtoAbsPixel(uxmin), gPad.XtoAbsPixel(uxmax) pymin, pymax = gPad.YtoAbsPixel(uymin), gPad.YtoAbsPixel(uymax) # if self.projection: # axis = h.GetYaxis() # else: # axis = h.GetXaxis() width = 1 if self._old != None: if self.projection: gVirtualX.DrawBox(pxmin, self._old[1] - width, pxmax, self._old[1], kSolid) else: gVirtualX.DrawBox(self._old[0], pymin, self._old[0] + width, pymax, kSolid) if self.projection: gVirtualX.DrawBox(pxmin, py, pxmax, py + width, kSolid) else: gVirtualX.DrawBox(px, pymin, px, pymax + width, kSolid) self._old = px, py upx = gPad.AbsPixeltoX(px) x = gPad.PadtoX(upx) upy = gPad.AbsPixeltoY(py) y = gPad.PadtoY(upy) padsav = gPad # create or set the display canvases if not self._cX: self._cX = gPad.GetCanvas().GetPad(2) else: self._DestroyPrimitive('X') if not self._cY: self._cY = gPad.GetCanvas().GetPad(2) else: self._DestroyPrimitive('Y') if self.projection: self.DrawSlice(h, y, 'Y') else: self.DrawSlice(h, x, 'X') padsav.cd()
hRawYieldDistr.append( inFileMT.Get(f'hRawYield_pT_{ptMin*10:.0f}-{ptMax*10:.0f}')) rms = hRawYieldDistr[-1].GetRMS() / hRawYieldDistr[-1].GetMean() * 100 shift = TMath.Abs( hRawYields.GetBinContent(iPt) - hRawYieldDistr[-1].GetMean()) / hRawYieldDistr[-1].GetMean() * 100 syst = TMath.Sqrt(rms**2 + shift**2) hRMS.SetBinContent(iPt, rms) hMeanShift.SetBinContent(iPt, shift) hSyst.SetBinContent(iPt, syst) cRMS = TCanvas('cRMS', '', 800, 800) cRMS.DrawFrame(hRMS.GetBinLowEdge(1), 0.1, ptMax, 20., ';#it{p}_{T} (GeV/#it{c});RMS (%)') hRMS.DrawCopy('same') axisSoverB = TGaxis(gPad.GetUxmax(), gPad.GetUymin(), gPad.GetUxmax(), gPad.GetUymax(), 0.01, 20., 510, "+LG") axisSoverB.SetLineColor(kRed + 1) axisSoverB.SetLabelColor(kRed + 1) axisSoverB.SetLabelFont(42) axisSoverB.SetLabelSize(0.045) axisSoverB.SetTitle('S/B (3#sigma)') axisSoverB.SetTitleOffset(1.2) axisSoverB.SetLabelOffset(0.012) axisSoverB.SetTitleColor(kRed + 1) axisSoverB.SetTitleFont(42) axisSoverB.SetTitleSize(0.05) axisSoverB.SetMaxDigits(3) axisSoverB.Draw() hSoverB.DrawCopy('same') cRMS.Update()