def numericalInvertPlot(graph): #pick up point by point pl #loop on all data points nPoints = graph.GetN() newGraph = TGraphAsymmErrors(nPoints) #x=array('f') #y=array('f') #ey=array('f') for iPoint in xrange(0, nPoints): #get info on data point dataPointX = Double(0) dataPointY = Double(0) graph.GetPoint(iPoint, dataPointX, dataPointY) dataErrorX = graph.GetErrorX(iPoint) dataErrorY = graph.GetErrorY(iPoint) #numerical-invert the X newDataPointX = dataPointX * dataPointY newErrorXLeft = fabs(newDataPointX - (dataPointX - dataErrorX)) newErrorXRight = fabs(newDataPointX - (dataPointX + dataErrorX)) #setting the new graph (with asymm errors this time) newGraph.SetPoint(iPoint, newDataPointX, dataPointY) newGraph.SetPointEXhigh(iPoint, newErrorXRight) newGraph.SetPointEXlow(iPoint, newErrorXLeft) newGraph.SetPointEYhigh(iPoint, dataErrorY) newGraph.SetPointEYlow(iPoint, dataErrorY) print dataPointY, newDataPointX #check that the numerical-inverted X is still in the previous bin - otherwise, ERROR! binRangeLow = dataPointX - dataErrorX binRangeHigh = dataPointX + dataErrorX if newDataPointX < binRangeLow or newDataPointX > binRangeHigh: print "Warning! Data point should not be here!" print "Old data point: ", dataPointX print "New NI data point:", newDataPointX print "XLow: ", binRangeLow print "XHigh: ", binRangeHigh newGraph.SetPoint(iPoint, dataPointX, 0.0000000000001) newGraph.SetPointEXhigh(iPoint, dataErrorX) newGraph.SetPointEXlow(iPoint, dataErrorX) newGraph.SetPointEYhigh(iPoint, dataErrorY) newGraph.SetPointEYlow(iPoint, dataErrorY) return newGraph
def convertToPoisson(h): graph = TGraphAsymmErrors() q = (1-0.6827)/2. for i in range(1,h.GetNbinsX()+1): x=h.GetXaxis().GetBinCenter(i) xLow =h.GetXaxis().GetBinLowEdge(i) xHigh =h.GetXaxis().GetBinUpEdge(i) y=h.GetBinContent(i) yLow=0 yHigh=0 if y !=0.0: yLow = y-Math.chisquared_quantile_c(1-q,2*y)/2. yHigh = Math.chisquared_quantile_c(q,2*(y+1))/2.-y graph.SetPoint(i-1,x,y) graph.SetPointEYlow(i-1,yLow) graph.SetPointEYhigh(i-1,yHigh) graph.SetPointEXlow(i-1,0.0) graph.SetPointEXhigh(i-1,0.0) graph.SetMarkerStyle(20) graph.SetLineWidth(2) graph.SetMarkerSize(1.) graph.SetMarkerColor(kBlack) return graph
def apply_centers(hPt, hCen): #bin center points according to the data cen = get_centers_from_toyMC(hCen) gSig = TGraphAsymmErrors(hPt.GetNbinsX()) for i in xrange(hPt.GetNbinsX()): #center point #xcen = hPt.GetBinCenter(i+1) xcen = cen[i]["val"] #cross section value gSig.SetPoint(i, xcen, hPt.GetBinContent(i + 1)) #vertical error gSig.SetPointEYlow(i, hPt.GetBinErrorLow(i + 1)) gSig.SetPointEYhigh(i, hPt.GetBinErrorUp(i + 1)) #horizontal error gSig.SetPointEXlow(i, cen[i]["err"]) gSig.SetPointEXhigh(i, cen[i]["err"]) return gSig
def fixData(hist, useGarwood=True, cutGrass=False, maxPoisson=False): if hist == None: return varBins = False data = TGraphAsymmErrors() alpha = 1 - 0.6827 for i in list(reversed(range(0, hist.GetNbinsX()))): #print "bin", i, "x:", hist.GetX()[i], "y:", hist.GetY()[i] # X error bars to 0 - do not move this, otherwise the first bin will disappear, thanks Wouter and Rene! N = max(hist.GetBinContent(i + 1), 0.) # Avoid unphysical bins data.SetPoint(i, hist.GetXaxis().GetBinCenter(i + 1), N) if not varBins: data.SetPointEXlow(i, 0) data.SetPointEXhigh(i, 0) # Garwood confidence intervals if (useGarwood): L = ROOT.Math.gamma_quantile(alpha / 2, N, 1.) if N > 0 else 0. U = ROOT.Math.gamma_quantile_c(alpha / 2, N + 1, 1) # maximum between Poisson and Sumw2 error bars EL = N - L if not maxPoisson else max(N - L, hist.GetBinErrorLow(i)) EU = U - N if not maxPoisson else max(U - N, hist.GetBinErrorHigh(i)) data.SetPointEYlow(i, EL) data.SetPointEYhigh(i, EU) else: data.SetPointEYlow(i, math.sqrt(N)) data.SetPointEYhigh(i, math.sqrt(N)) # Cut grass if cutGrass and data.GetY()[i] > 0.: cutGrass = False # Treatment for 0 bins # if abs(hist.GetY()[i])<=1.e-6: # if cutGrass: hist.SetPointError(i, hist.GetErrorXlow(i), hist.GetErrorXhigh(i), 1.e-6, 1.e-6, ) # if (hist.GetX()[i]>65 and hist.GetX()[i]<135 and hist.GetY()[i]==0): hist.SetPointError(i, hist.GetErrorXlow(i), hist.GetErrorXhigh(i), 1.e-6, 1.e-6, ) # hist.SetPoint(i, hist.GetX()[i], -1.e-4) # X error bars #if hist.GetErrorXlow(i)<1.e-4: # binwidth = hist.GetX()[1]-hist.GetX()[0] # hist.SetPointEXlow(i, binwidth/2.) # hist.SetPointEXhigh(i, binwidth/2.) data.SetMarkerColor(hist.GetMarkerColor()) data.SetMarkerStyle(hist.GetMarkerStyle()) data.SetMarkerSize(hist.GetMarkerSize()) #data.SetLineSize(hist.GetLineSize()) return data
def fixed_centers(hPt): #fixed bin centers gSig = TGraphAsymmErrors(hPt.GetNbinsX()) for i in xrange(hPt.GetNbinsX()): #cross section value gSig.SetPoint(i, hPt.GetBinCenter(i + 1), hPt.GetBinContent(i + 1)) #vertical error gSig.SetPointEYlow(i, hPt.GetBinErrorLow(i + 1)) gSig.SetPointEYhigh(i, hPt.GetBinErrorUp(i + 1)) #horizontal error gSig.SetPointEXlow(i, hPt.GetBinWidth(i + 1) / 2.) gSig.SetPointEXhigh(i, hPt.GetBinWidth(i + 1) / 2.) return gSig
def histToGraph(hist, name='', keepErrors=True, poissonErrors=True): ## Helper method to convert a histogram to a corresponding graph # @hist TH1 object # @name name of the graph (default is name of histogram) # @keepErrors decide if the y-errors should be propagated to the graph # @poissonErrors decide if the y-errors should be calculated as Poisson errors # @return graph if not name: name = 'g%s' % (hist.GetName()) from ROOT import TGraphAsymmErrors nBins = hist.GetNbinsX() graph = TGraphAsymmErrors(nBins) graph.SetNameTitle(name, hist.GetTitle()) xAxis = hist.GetXaxis() for i in xrange(nBins): xVal = xAxis.GetBinCenter(i + 1) yVal = hist.GetBinContent(i + 1) graph.SetPoint(i, xVal, yVal) graph.SetPointEXlow(i, abs(xVal - xAxis.GetBinLowEdge(i + 1))) graph.SetPointEXhigh(i, abs(xVal - xAxis.GetBinUpEdge(i + 1))) if keepErrors: if poissonErrors: lo, hi = calculatePoissonErrors(yVal) graph.SetPointEYlow(i, lo) graph.SetPointEYhigh(i, hi) else: graph.SetPointEYlow(i, hist.GetBinErrorLow(i + 1)) graph.SetPointEYhigh(i, hist.GetBinErrorUp(i + 1)) # copy the style graph.SetMarkerStyle(hist.GetMarkerStyle()) graph.SetMarkerColor(hist.GetMarkerColor()) graph.SetMarkerSize(hist.GetMarkerSize()) graph.SetLineStyle(hist.GetLineStyle()) graph.SetLineColor(hist.GetLineColor()) graph.SetLineWidth(hist.GetLineWidth()) graph.SetFillColor(hist.GetFillColor()) graph.SetFillStyle(hist.GetFillStyle()) return graph
ey_lo_mu = grMu.GetErrorYlow(i) ey_lo_ele = grEle.GetErrorYlow(i) if y_mu < 1.e-9 or y_ele < 1.e-9: ey_lo_ratio = 0.0 else: ey_lo_ratio = y_ratio * math.hypot(ey_lo_mu / y_mu, ey_lo_ele / y_ele) ey_hi_mu = grMu.GetErrorYhigh(i) ey_hi_ele = grEle.GetErrorYhigh(i) if y_mu < 1.e-9 or y_ele < 1.e-9: ey_hi_ratio = 0.0 else: ey_hi_ratio = y_ratio * math.hypot(ey_hi_mu / y_mu, ey_hi_ele / y_ele) grRatio.SetPoint(i, x, y_ratio) grRatio.SetPointEXlow(i, ex_lo) grRatio.SetPointEXhigh(i, ex_hi) grRatio.SetPointEYlow(i, ey_lo_ratio) grRatio.SetPointEYhigh(i, ey_hi_ratio) if True: grRatio.SetMarkerStyle(20) grRatio.SetMarkerSize(1.5) grRatio.SetMarkerColor(1) grRatio.SetLineStyle(1) grRatio.SetLineColor(1) grRatio.SetLineWidth(1) grRatio.GetXaxis().SetLabelSize(0.04) grRatio.GetXaxis().SetTitleSize(0.04)
def nuisance_plot(input_file_, orien_, sticker_, oname_): pars = get_parameters_from_file(input_file_) #adjust_parameters(pars) N = len(pars) g68 = TGraph(2 * N + 7) g95 = TGraph(2 * N + 7) gPR = TGraphAsymmErrors(N) for i in range(0, N): x = pars[i].value y = i + 1.5 xerr = pars[i].error yerr = 0. if orien_ == 'h': x, y = y, x xerr, yerr = yerr, xerr gPR.SetPoint(i, x, y) gPR.SetPointEXlow(i, xerr) gPR.SetPointEYlow(i, yerr) gPR.SetPointEXhigh(i, xerr) gPR.SetPointEYhigh(i, yerr) for a in range(0, N + 3): if orien_ == 'h': g68.SetPoint(a, a, -1) g95.SetPoint(a, a, -2) g68.SetPoint(a + 1 + N + 2, N + 2 - a, 1) g95.SetPoint(a + 1 + N + 2, N + 2 - a, 2) else: g68.SetPoint(a, -1, a) g95.SetPoint(a, -2, a) g68.SetPoint(a + 1 + N + 2, 1, N + 2 - a) g95.SetPoint(a + 1 + N + 2, 2, N + 2 - a) gPR.SetLineStyle(1) gPR.SetLineWidth(1) gPR.SetLineColor(1) gPR.SetMarkerStyle(21) gPR.SetMarkerSize(1.25) g68.SetFillColor(kGreen) g95.SetFillColor(kYellow) if orien_ == 'h': text_TL = TPaveText(0.100, 0.925, 0.440, 0.995, 'NDC') text_TR = TPaveText(0.587, 0.925, 0.999, 0.993, 'NDC') else: text_TL = TPaveText(0.370, 0.945, 0.590, 0.995, 'NDC') text_TR = TPaveText(0.600, 0.945, 0.995, 0.993, 'NDC') text_TL.AddText(label_TL) text_TL.SetFillColor(0) text_TL.SetTextAlign(12) text_TL.SetTextSize(0.06) text_TL.SetTextFont(42) text_TR.AddText(sticker_) text_TR.SetFillColor(0) text_TR.SetTextAlign(32) text_TR.SetTextSize(0.05) text_TR.SetTextFont(42) if orien_ == 'h': c = TCanvas('c', 'c', 1000, 700) c.SetTopMargin(0.08) c.SetRightMargin(0.02) c.SetBottomMargin(0.16) c.SetLeftMargin(0.11) else: c = TCanvas('c', 'c', 700, 1000) c.SetTopMargin(0.06) c.SetRightMargin(0.02) c.SetBottomMargin(0.12) c.SetLeftMargin(0.35 * 700 / 650) c.SetTickx() c.SetTicky() c.Update() g95.Draw('AF') g68.Draw('F') gPR.Draw('P') text_TL.Draw('same') text_TR.Draw('same') ax_1 = g95.GetHistogram().GetYaxis() ax_2 = g95.GetHistogram().GetXaxis() if orien_ == 'h': ax_1, ax_2 = ax_2, ax_1 ax_1.Set(N + 2, 0, N + 2) ax_1.SetNdivisions(-414) for i in range(0, N): ax_1.SetBinLabel(i + 2, pars[i].name) g95.SetTitle('') ax_2.SetTitle('post-fit nuisance parameters values') #ax_2.SetTitle('deviation in units of #sigma') ax_1.SetTitleSize(0.050) ax_2.SetTitleSize(0.050) ax_1.SetTitleOffset(1.4) ax_2.SetTitleOffset(1.0) ax_1.SetLabelSize(0.05) #ax_2.SetLabelSize(0.05) ax_1.SetRangeUser(0, N + 2) ax_2.SetRangeUser(-2.4, 2.4) g95.GetHistogram().Draw('axis,same') c.SaveAs(oname_ + '.pdf') c.Close()