def writeFullUnc(pred_file): ''' Update the input root file, add a hist with total prediction and full uncertainty. ''' f = rt.TFile(pred_file, 'UPDATE') h = TGraphAsymmErrors(f.Get(pred_total_name).Clone('bkgtotal_unc_sr')) h_pieces = {} for hname, sample in zip(graph_names, all_samples): h_pieces[sample] = TGraphAsymmErrors( f.Get(hname).Clone(sample + '_unc_sr')) print "%30s %10s %16s" % ('bin', 'total pred', 'total unc.') for ibin in xrange(0, h.GetN()): bin = binlist[ibin] val = h.GetY()[ibin] e_low, e_up = fullUnc[bin] h.SetPointEYlow(ibin, e_low) h.SetPointEYhigh(ibin, e_up) print "%30s %10.2f +%8.2f -%8.2f" % (bin, val, e_up, e_low) allVals[bin] = {'bkg': (val, e_low, e_up)} for sample in all_samples: val = yields[bin][sample] e_low, e_up = fullUnc_pieces[sample][bin] h_pieces[sample].SetPointEYlow(ibin, e_low) h_pieces[sample].SetPointEYhigh(ibin, e_up) allVals[bin][sample] = (val, e_low, e_up) h.Write('bkgtotal_unc_sr', rt.TObject.kOverwrite) for sample in all_samples: h_pieces[sample].Write(sample + '_unc_sr', rt.TObject.kOverwrite) f.Close()
def make_tgrapherrors(name, title, color=1, marker=20, marker_size=1, width=1, asym_err=False, style=1, x=None, y=None): if (x and y) is None: gr = TGraphErrors() if not asym_err else TGraphAsymmErrors() else: gr = TGraphErrors(len(x), array(x, 'd'), array( y, 'd')) if not asym_err else TGraphAsymmErrors( len(x), array(x, 'd'), array(y), 'd') gr.SetTitle(title) gr.SetName(name) gr.SetMarkerStyle(marker) gr.SetMarkerColor(color) gr.SetLineColor(color) gr.SetMarkerSize(marker_size) gr.SetLineWidth(width) gr.SetLineStyle(style) return gr
def getGraph(self): from array import array from ROOT import TMultiGraph, TLegend, TGraphAsymmErrors n = len(self.__x) if n != len(self.__y) or n != len(self.__yErrLow) or n != len( self.__yErrHigh): raise StandardError, "The length of the x(%s), y(%s) and y error(%s,%s) lists does not match" % ( len(self.__x), len(self.__y), len( self.__yErrLow), len(self.__yErrHigh)) result = TMultiGraph() legendPosition = [ float(i) for i in self.__getStyleOption("legendPosition").split() ] legend = TLegend(*legendPosition) legend.SetFillColor(0) result.SetTitle("%s;%s;%s" % (self.__title, self.__xTitle, self.__yTitle)) #(refArrays, refLabel) = self.__getRefernceGraphArrays() #refGraph = TGraphAsymmErrors(*refArrays) #refGraph.SetLineWidth(2) #refGraph.SetLineColor(int(self.__config.get("reference","lineColor"))) #refGraph.SetFillColor(int(self.__config.get("reference","fillColor"))) #result.Add(refGraph,"L3") #legend.AddEntry(refGraph,self.__config.get("reference","name")) xErr = array("d", [0 for i in range(n)]) print "__x = ", self.__x print "__y = ", self.__y graph = TGraphAsymmErrors(n, self.__x, self.__y, xErr, xErr, self.__yErrLow, self.__yErrHigh) graph.SetLineWidth(2) graph.SetFillColor(0) graph.SetLineColor(int(self.__getStyleOption("lineColor"))) graph.SetMarkerColor(int(self.__getStyleOption("markerColor"))) graph.SetMarkerStyle(int(self.__getStyleOption("markerStyle"))) graph.SetMarkerSize(float(self.__getStyleOption("markerSize"))) sysGraph = TGraphAsymmErrors(n, self.__x, self.__y, xErr, xErr, self.__ySysErrLow, self.__ySysErrHigh) sysGraph.SetLineWidth(1) sysGraph.SetFillColor(0) sysGraph.SetLineColor(int(self.__getStyleOption("lineColor"))) sysGraph.SetMarkerColor(int(self.__getStyleOption("markerColor"))) sysGraph.SetMarkerStyle(int(self.__getStyleOption("markerStyle"))) sysGraph.SetMarkerSize(float(self.__getStyleOption("markerSize"))) result.Add(sysGraph, "[]") result.Add(graph, "P") # result.SetName("MultiPlots") # result.SetTitle("%s;%s;%s"%(self.__title,self.__xTitle,self.__yTitle)) result.SetName("MG_%s" % (self.__title)) legend.AddEntry(graph, self.__getStyleOption("name")) #for (x,y,yErr) in zip(self.__x, self.__y, zip(self.__yErrLow,self.__yErrHigh)): # self.__addAnnotaion("hallo",x,y,yErr) return (result, legend)
def __init_histograms(self): """Initialize the histograms for the simulation""" self._specgraph = TGraphAsymmErrors(1) self._ifluxgraph = TGraphAsymmErrors(1) # integral flux and its error self._excessgraph = TGraphAsymmErrors( 1) # distribution of excess events self._gammaExp = TH1D() # distribution of excess events self._bkgExp = TH1D() # distribution of excess events return
def __init__( self, process_list, sf = None, df = None ): self.__processes = process_list if sf and df: self.__sf = sf self.__df = df alpha = 1 - 0.6827 self.__sfCRGraph = TGraphAsymmErrors(self.__sf) self.__dfCRGraph = TGraphAsymmErrors(self.__df) for g in [self.__sfCRGraph, self.__dfCRGraph]: for i in range(g.GetN()): N = g.GetY()[i] LowErr = 0 if N == 0 else Math.gamma_quantile(alpha/2, N, 1.) UpErr = Math.gamma_quantile_c(alpha/2, N+1, 1.) # recommended by StatComm (1.8) # Math.gamma_quantile_c(alpha, N+1, 1.) # gives 1.2, strictly 68% one-sided g.SetPointEYlow(i, N-LowErr) g.SetPointEYhigh(i, UpErr-N) for p in self.__processes: if p.name() in ['nonpromptSF', 'nonpromptDF']: nom, low, high = [], [], [] for ib in range(p.nominal().GetXaxis().GetNbins()): if p.name() in ['nonpromptDF']: hnp = self.__dfCRGraph else: hnp = self.__sfCRGraph npcr = hnp.GetY()[ib] npcrErrLow = hnp.GetEYlow()[ib] npcrErrHigh = hnp.GetEYhigh()[ib] np = p.nominal().GetBinContent(ib+1) npErr = p.nominal().GetBinError(ib+1) alpha = np/npcr if npcr > 0 else npErr nom.append(npcr*alpha) low.append(npcrErrLow*alpha) high.append(npcrErrHigh*alpha) hnom = p.nominal().Clone() for ib in range(hnom.GetXaxis().GetNbins()): hnom.SetBinContent(ib+1, nom[ib]) npGraph = TGraphAsymmErrors(hnom) for ib in range(hnom.GetXaxis().GetNbins()): npGraph.SetPointEYlow(ib, low[ib]) npGraph.SetPointEYhigh(ib, high[ib]) if p.name() in ['nonpromptDF']: self.__dfGraph = npGraph else: self.__sfGraph = npGraph self.__uncertainties = set() for p in self.__processes: self.__uncertainties = self.__uncertainties.union( set( p.uncertaintySources() ) )
def writeFullUnc(pred_file): ''' Update the input root file, add a hist with total prediction and full uncertainty. ''' f = rt.TFile(pred_file, 'UPDATE') h = TGraphAsymmErrors(f.Get(pred_total_name).Clone('bkgtotal_unc_sr')) h_syst = TGraphAsymmErrors( f.Get(pred_total_name).Clone('bkgtotal_syst_unc_sr')) h_pieces = {} h_syst_pieces = {} for hname, sample in zip(graph_names, all_samples): h_pieces[sample] = TGraphAsymmErrors( f.Get(hname).Clone(sample + '_unc_sr')) h_syst_pieces[sample + "_up"] = TH1F(sample + '_syst_up', sample + '_syst_up', 183, 0, 183) h_syst_pieces[sample + "_dn"] = TH1F(sample + '_syst_dn', sample + '_syst_dn', 183, 0, 183) print "%30s %10s %16s" % ('bin', 'total pred', 'total unc.') for ibin in xrange(0, h.GetN()): bin = binlist[ibin] val = h.GetY()[ibin] e_low, e_up = fullUnc[bin] h.SetPointEYlow(ibin, e_low) h.SetPointEYhigh(ibin, e_up) print "%30s %10.4f +%8.4f -%8.4f" % (bin, val, e_up, e_low) allVals[bin] = {'bkg': (val, e_low, e_up)} # Test for only syst histograms val = h_syst.GetY()[ibin] e_low, e_up = systUnc[bin] h_syst.SetPointEYlow(ibin, e_low) h_syst.SetPointEYhigh(ibin, e_up) for sample in all_samples: val = yields[bin][sample] e_low, e_up = fullUnc_pieces[sample][bin] #print "%11s %30s %10.4f +%8.4f -%8.4f" % (sample, bin, val, e_up, e_low) h_pieces[sample].SetPointEYlow(ibin, e_low) h_pieces[sample].SetPointEYhigh(ibin, e_up) allVals[bin][sample] = (val, e_low, e_up) # Test for only syst histograms e_low, e_up = systUnc_rel_pieces[sample][bin] #if sample in test_samp and bin in test_bin and type in test_type and debug: #if 'TTZ' in sample and e_up > 8: # print("%11s %30s %10.4f +%8.4f -%8.4f" % (sample, bin, val, e_up, e_low)) h_syst_pieces[sample + "_up"].SetBinContent(ibin + 1, e_up) h_syst_pieces[sample + "_dn"].SetBinContent(ibin + 1, e_low) h_syst_pieces[sample + "_up"].SetBinError(ibin + 1, 0) h_syst_pieces[sample + "_dn"].SetBinError(ibin + 1, 0) h.Write('bkgtotal_unc_sr', rt.TObject.kOverwrite) h.Write('bkgtotal_syst_unc_sr', rt.TObject.kOverwrite) for sample in all_samples: h_pieces[sample].Write(sample + '_unc_sr', rt.TObject.kOverwrite) h_syst_pieces[sample + "_up"].Write(sample + '_syst_up', rt.TObject.kOverwrite) h_syst_pieces[sample + "_dn"].Write(sample + '_syst_dn', rt.TObject.kOverwrite) f.Close()
def draw_limits_per_category(nchannels, xmin, xmax, obs, expect, upper1sig, lower1sig, upper2sig, lower2sig): channel = np.array( [3. * nchannels - 1.5 - 3. * i for i in range(0, nchannels)]) ey = np.array([0.8 for i in range(0, nchannels)]) zero = np.zeros(nchannels) gexpect1sig = TGraphAsymmErrors(nchannels, expect, channel, lower1sig, upper1sig, ey, ey) gexpect1sig.SetFillColor(kGreen) gexpect1sig.SetLineWidth(2) gexpect1sig.SetLineStyle(2) gexpect2sig = TGraphAsymmErrors(nchannels, expect, channel, lower2sig, upper2sig, ey, ey) gexpect2sig.SetFillColor(kYellow) gexpect2sig.SetLineWidth(2) gexpect2sig.SetLineStyle(2) gexpect2sig.Draw("2") gexpect1sig.Draw("2") gobs = TGraphErrors(nchannels, obs, channel, zero, ey) gobs.SetMarkerStyle(21) gobs.SetMarkerSize(1.5) gobs.SetLineWidth(2) gobs.Draw("pz") # dashed line at median expected limits l = TLine() l.SetLineStyle(2) l.SetLineWidth(2) for bin in range(nchannels): l.DrawLine(expect[bin], channel[bin] - ey[bin], expect[bin], channel[bin] + ey[bin]) # line to separate individual and combined limits l.SetLineStyle(1) l.SetLineWidth(1) l.DrawLine(xmin, 0, xmax, 0) # legend x1 = gStyle.GetPadLeftMargin() + 0.01 y2 = 1 - gStyle.GetPadTopMargin() - 0.01 leg = TLegend(x1, y2 - 0.17, x1 + 0.25, y2) leg.SetFillColor(4000) leg.AddEntry(gexpect1sig, "Expected #pm1#sigma", "FL") leg.AddEntry(gexpect2sig, "Expected #pm2#sigma", "FL") leg.AddEntry(gobs, "Observed", "pl") leg.Draw() return gobs, gexpect1sig, gexpect2sig, leg
def draw_limits_per_category(nchannels, xmin, xmax, obs, expect, upper1sig, lower1sig, upper2sig, lower2sig): channel = np.array( [nchannels - 1.5 - float(i) for i in range(0, nchannels)]) ey = np.full(nchannels, 0.494) zero = np.zeros(nchannels) gexpect1sig = TGraphAsymmErrors(nchannels, expect, channel, lower1sig, upper1sig, ey, ey) gexpect1sig.SetFillColor(kGreen) gexpect1sig.SetLineWidth(2) gexpect1sig.SetLineStyle(2) gexpect2sig = TGraphAsymmErrors(nchannels, expect, channel, lower2sig, upper2sig, ey, ey) gexpect2sig.SetFillColor(kYellow) gexpect2sig.SetLineWidth(2) gexpect2sig.SetLineStyle(2) gexpect2sig.Draw("2") gexpect1sig.Draw("2") gobs = TGraphErrors(nchannels, obs, channel, zero, ey) gobs.SetMarkerStyle(21) gobs.SetMarkerSize(1.5) gobs.SetLineWidth(2) #gobs.Draw("pz") # dashed line at median expected limits l = TLine() l.SetLineStyle(2) l.SetLineWidth(2) for bin in range(nchannels): l.DrawLine(expect[bin], channel[bin] - ey[bin], expect[bin], channel[bin] + ey[bin]) # line to separate individual and combined limits l.SetLineStyle(1) l.SetLineWidth(1) l.DrawLine(xmin, 0, xmax, 0) # legend leg = TLegend(0.75, 0.75, 0.95, 0.9) leg.SetFillColor(4000) leg.AddEntry(gexpect1sig, "Expected #pm1#sigma", "FL") leg.AddEntry(gexpect2sig, "Expected #pm2#sigma", "FL") #leg.AddEntry( gobs, "Observed", "pl" ) leg.Draw() return gobs, gexpect1sig, gexpect2sig, leg
def TextFileToRootGraphs(self, limit_text_file,med_idx=0):#, limit_text_filename): filename = limit_text_file #limit_text_filename limit_root_file = limit_text_file.replace(".txt",".root") f = open(filename,"r") med=array('f') mchi=array('f') expm2=array('f') expm1=array('f') expmed=array('f') expp1=array('f') expp2=array('f') obs=array('f') errx=array('f') for line in f: if len(line.rsplit())<7: continue med.append(float(line.rstrip().split()[1])) mchi.append(float(line.rstrip().split()[0])) expm2.append(float(line.rstrip().split()[4]) - float(line.rstrip().split()[2]) ) expm1.append(float(line.rstrip().split()[4]) - float(line.rstrip().split()[3]) ) expmed.append(float(line.rstrip().split()[4])) expp1.append(float(line.rstrip().split()[5]) - float(line.rstrip().split()[4]) ) expp2.append(float(line.rstrip().split()[6]) - float(line.rstrip().split()[4]) ) obs.append(float(line.rstrip().split()[7])) errx.append(0.0) print ('expm2: ', expm2) print ('expm1: ', expm1) print ('expmed: ', expmed) print ('expp1: ', expp1) print ('expp2: ', expp2) g_exp2 = TGraphAsymmErrors(int(len(med)), med, expmed, errx, errx, expm2, expp2 ) ; g_exp2.SetName("exp2") g_exp1 = TGraphAsymmErrors(int(len(med)), med, expmed, errx, errx, expm1, expp1 ) ; g_exp1.SetName("exp1") g_expmed = TGraphAsymmErrors(int(len(med)), med, expmed) ; g_expmed.SetName("expmed") g_obs = TGraphAsymmErrors(int(len(med)), med, obs ) ; g_obs.SetName("obs") f1 = TFile(limit_root_file,'RECREATE') g_exp2.Write() g_exp1.Write() g_expmed.Write() g_obs.Write() f1.Write() f1.Close() return limit_root_file
def eff(wrps, option='cl=0.683 b(1,1) mode'): """ Applies to HistoWrappers only. Returns GraphWrapper. Takes lumi from first. >>> from ROOT import TH1I >>> h1 = TH1I("h1", "", 2, .5, 2.5) >>> h1.Fill(1) 1 >>> h1.Fill(1) 1 >>> w1 = wrappers.HistoWrapper(h1, lumi=2) >>> h2 = TH1I("h2", "", 2, .5, 2.5) >>> h2.Sumw2() >>> h2.Fill(1) 1 >>> h2.Fill(1) 1 >>> h2.Fill(1) 1 >>> h2.Fill(2) 2 >>> w2 = wrappers.HistoWrapper(h2, lumi=3) >>> w3 = eff([w1, w2]) >>> w3.graph.GetN() 2 >>> hi = w3.graph.GetErrorYhigh(0) >>> lo = w3.graph.GetErrorYlow(0) >>> abs(hi - 0.189572034007) < 1e-10 True >>> abs(lo - 0.236650832868) < 1e-10 True """ wrps = iterableize(wrps) wrps = iter(wrps) try: nominator = next(wrps) denominator = next(wrps) except StopIteration: raise TooFewWrpsError("eff needs exactly two Wrappers.") try: wrps.next() raise TooManyWrpsError("eff needs exactly two Wrappers.") except StopIteration: pass if not isinstance(nominator, wrappers.HistoWrapper): raise WrongInputError( "eff needs nominator to be of type HistoWrapper. nominator: " + str(nominator) ) if not isinstance(denominator, wrappers.HistoWrapper): raise WrongInputError( "eff needs denominator to be of type HistoWrapper. denominator: " + str(denominator) ) graph = TGraphAsymmErrors(nominator.histo, denominator.histo, option) graph.GetXaxis().SetTitle(nominator.histo.GetXaxis().GetTitle()) graph.GetYaxis().SetTitle('efficiency') info = nominator.all_info() return wrappers.GraphWrapper(graph, **info)
def getGraphSimple(self): from array import array from ROOT import TMultiGraph, TLegend, TGraphAsymmErrors n = len(self.__x) if n != len(self.__y) or n != len(self.__yErrLow) or n != len(self.__yErrHigh): raise StandardError, "The length of the x(%s), y(%s) and y error(%s,%s) lists does not match"%(len(self.__x), len(self.__y), len(self.__yErrLow), len(self.__yErrHigh)) legendPosition = [float(i) for i in self.__getStyleOption("legendPosition").split()] legend = TLegend(*legendPosition) legend.SetFillColor(0) xErr = array("d",[0 for i in range(n)]) print "__x = ", self.__x print "__y = ", self.__y graph = TGraphAsymmErrors(n, self.__x, self.__y, xErr, xErr, self.__yErrLow,self.__yErrHigh) graph.SetTitle("%s;%s;%s"%(self.__title,self.__xTitle,self.__yTitle)) graph.SetLineWidth(2) graph.SetFillColor(0) graph.SetLineColor(int(self.__getStyleOption("lineColor"))) graph.SetMarkerColor(int(self.__getStyleOption("markerColor"))) graph.SetMarkerStyle(int(self.__getStyleOption("markerStyle"))) graph.SetMarkerSize(float(self.__getStyleOption("markerSize"))) graph.SetDrawOption("AP") return (graph, legend)
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 MakePoissonConfidenceLevelErrors(hist): x_val = array('f') y_val = array('f') x_errU = array('f') x_errL = array('f') y_errU = array('f') y_errL = array('f') for b in xrange(1, hist.GetNbinsX() + 1): binEntries = hist.GetBinContent(b) if binEntries > 0.: binErrUp = calcPoissonCLUpper(binEntries) - binEntries binErrLow = binEntries - calcPoissonCLLower(binEntries) x_val.append(hist.GetXaxis().GetBinCenter(b)) y_val.append(binEntries) y_errU.append(binErrUp) y_errL.append(binErrLow) x_errU.append(0.) #hist.GetXaxis().GetBinWidth( b )/2.0 ) x_errL.append(0.) #hist.GetXaxis().GetBinWidth( b )/2.0 ) pass pass #print len(x_val), x_val, y_val, x_errL, x_errU, y_errL, y_errU if len(x_val) > 0: dataGraph = TGraphAsymmErrors(len(x_val), x_val, y_val, x_errL, x_errU, y_errL, y_errU) return dataGraph else: return None
def resolutionGraph( tree, xVar, yVar, title, measure, cut='', weightExpression='' ): ## Create and fill a resolution graph for this dataset, i.e. resolution of y vs. x # @ param tree TTree object used to create the histogram # @ param xVar Variable object defining the xAxis and the draw command # @ param yVar Variable object defining the xAxis and the draw command # @ param title histogram title # @param measure Measure object to evaluate the resolution # @ param cut Cut object (optional) # @ param weightExpression weight expression (optional) # @ return the generated histogram bins = xVar.binning.bins title = '%s vs %s' % (yVar.title, xVar.title) if not title else title name = 'h%s_%s' % ( title.replace(' ', '_').replace('(', '').replace(')',''), uuid.uuid1() ) from ROOT import TGraphAsymmErrors graph = TGraphAsymmErrors( xVar.binning.nBins ) graph.SetNameTitle( name, '%s;%s;%s' % (title, xVar.axisLabel, measure.getDecoratedLabel( yVar ) ) ) # create a histogram of y for each bin in x and evaluate measure for iBin in xrange( xVar.binning.nBins ): low = bins[iBin] up = bins[iBin+1] xMean = low + 0.5 * (up - low) xErr = xMean - low myCut = (Cut( '%s >= %s && %s < %s' % (xVar.command, low, xVar.command, up) ) + cut) * weightExpression values, weights = getValuesFromTree( tree, yVar.command, myCut.cut ) yMean, yErrLow, yErrUp = measure.calculateFromValues( values, weights ) graph.SetPoint( iBin, xMean, yMean ) graph.SetPointError( iBin, xErr, xErr, yErrLow, yErrUp ) return graph
def makeResidHist(data, bkg): pulls = TGraphAsymmErrors(data.GetN()) pulls.SetName("Pulls") pulls.SetLineWidth(data.GetLineWidth()) pulls.SetLineStyle(data.GetLineStyle()) pulls.SetLineColor(data.GetLineColor()) pulls.SetMarkerSize(data.GetMarkerSize()) pulls.SetMarkerStyle(data.GetMarkerStyle()) pulls.SetMarkerColor(data.GetMarkerColor()) pulls.SetFillStyle(data.GetFillStyle()) pulls.SetFillColor(data.GetFillColor()) # Add histograms, calculate Poisson confidence interval on sum value for i in range(data.GetN()): x = data.GetX()[i] dyl = data.GetErrorYlow(i) dyh = data.GetErrorYhigh(i) yy = data.GetY()[i] - bkg.Interpolate(x) #bkg.GetBinContent(i+1) norm = dyl if yy > 0. else dyh if norm == 0.: yy, dyh, dyl = 0., 0., 0. else: yy /= norm dyh /= norm dyl /= norm pulls.SetPoint(i, x, yy) pulls.SetPointEYhigh(i, dyh) pulls.SetPointEYlow(i, dyl) return pulls
def divide(h, g, name=""): """ Divide TGraph h by TGraph g, returns a new graph NOTE: currently does no sort of "bin-matching" or anything of that kind """ from ROOT import TGraphAsymmErrors n = h.Clone() d = g.Clone() nPoints = collectPoints(n) dPoints = collectPoints(d) if len(nPoints) != len(dPoints): print("Cannot divide Graphs with unequal number of points: " "{} vs. {}".format(len(nPoints), len(dPoints))) return TGraphAsymmErrors() nPoints.sort(key=lambda p: p.x) dPoints.sort(key=lambda p: p.x) ratioPoints = [] for i in range(0, len(nPoints)): ratioPoints.append(dividePoint(nPoints[i], dPoints[i])) graph = createGraphFromPoints(ratioPoints) if name: graph.SetName(name) return graph
def tg_sys(central, variations): shapebins_centres = [] shapebins_contents = [] shapebins_widths_up = [] shapebins_widths_down = [] shapebins_error_up = [] shapebins_error_down = [] for i in range(central.GetNbinsX()): shapebins_centres.append(central.GetBinCenter(i+1)) shapebins_contents.append(central.GetBinContent(i+1)) shapebins_widths_up.append(central.GetBinWidth(i+1)*0.5) shapebins_widths_down.append(central.GetBinWidth(i+1)*0.5) error_up = 0 error_down = 0 for j, _ in enumerate(variations): error = variations[j].GetBinContent(i+1)-central.GetBinContent(i+1) if error > 0 and error > error_up: error_up = error if error < 0 and abs(error) > error_down: error_down = abs(error) shapebins_error_up.append(error_up) shapebins_error_down.append(error_down) shapebins_centres_array = array('d', shapebins_centres) shapebins_contents_array = array('d', shapebins_contents) shapebins_widths_up_array = array('d', shapebins_widths_up) shapebins_widths_down_array = array('d', shapebins_widths_down) shapebins_error_up_array = array('d', shapebins_error_up) shapebins_error_down_array = array('d', shapebins_error_down) tg = TGraphAsymmErrors(central.GetNbinsX(), shapebins_centres_array, shapebins_contents_array, shapebins_widths_down_array, shapebins_widths_up_array, shapebins_error_down_array, shapebins_error_up_array) return tg
def efficiency(total, passed): name = uuid4().hex if total.GetDimension() == 1: # Reset bin content for bins with 0 in the total to total = 1, passed # = 0. Due to negative weights and low stats some bins can have # passed > total; set the content for such bins to passed = total. _make_consistent(passed, total) # Divide(pass,total,"cl=0.683 b(1,1) mode") rep = TGraphAsymmErrors(passed, total, "cl=0.683 b(1,1) mode") #rep = TGraphAsymmErrors(passed, total) #passed_bins = get_bins(passed, True) #total_bins = get_bins(total, True) #rep_bins = get_bins(rep) #if all([x == 0.0 for x,y in rep_bins]): #print('{0} {1}'.format(process._label, filter)) #print(['{0:7.2f}'.format(y) for x,y in passed_bins]) #print(['{0:7.2f}'.format(y) for x,y in total_bins]) #print(['{0:7.2f}'.format(y) for x,y in rep_bins]) #print(['({0:.0f}, {1:.2f})'.format(x, y) for x,y in rep_bins]) else: rep = passed.Clone(name) rep.Divide(total) rep.SetName(name) return rep
def getRateInPb (self, seed): if seed not in numerator[self._label]: print "ETM" + seed + " not found in results!" return self._denominatorHist = TH1D ("total", "", 1, -0.5, 0.5) self._numeratorHist = TH1D ("pass", "", 1, -0.5, 0.5) self._denominatorHist.SetBinContent (1, self._denominator) self._denominatorHist.SetBinError (1, math.sqrt (self._denominator)) self._numeratorHist.SetBinContent (1, numerator[self._label][seed]) self._numeratorHist.SetBinError (1, math.sqrt (numerator[self._label][seed])) self._rateGraph = TGraphAsymmErrors (self._numeratorHist, self._denominatorHist) x = Double (0.0) y = Double (0.0) self._rateGraph.GetPoint (0, x, y) eLow = self._rateGraph.GetErrorYlow (0) eHigh = self._rateGraph.GetErrorYhigh (0) y *= self._crossSectionInPb eLow *= self._crossSectionInPb eHigh *= self._crossSectionInPb return (y, eLow, eHigh)
def createAssymSFFile(filename, sftable, name): """Create histogram from table.""" print(">>> Creating '%s'..." % filename) file = TFile(filename, 'RECREATE') file.cd() x = [float(i) + 0.5 for i in range(len(sftable.keys()))] ex = [0.0] * len(sftable.keys()) x_names = sorted(sftable.keys()) y = [sftable[k]['val'] for k in x_names] eyl = [sftable[k]['down'] for k in x_names] eyh = [sftable[k]['up'] for k in x_names] g = TGraphAsymmErrors(len(x), np.array(x), np.array(y), np.array(ex), np.array(ex), np.array(eyl), np.array(eyh)) g.GetXaxis().SetNdivisions(len(x) * 2) g.GetXaxis().ChangeLabel(0, -1, 0, -1, -1, -1, "") for i in x: print(1 + int(i), x_names[int(i)]) g.GetXaxis().ChangeLabel(2 + int(i) * 2, -1, 0) g.GetXaxis().ChangeLabel(1 + int(i) * 2, -1, -1, -1, -1, -1, x_names[int(i)]) g.Write(name) # file.ls() ; g.Draw("A*") ; raw_input() file.Close() return file
def readDataIntoGraph(): f = open('HESSj1745_290.dat', "r") lines = f.readlines() x, y, yHigh, yLow = array('d'), array('d'), array('d'), array('d') # yScale = 10e12 yScale = 1 xScale = 1000 for iLine in lines: if iLine.isspace() == True or iLine[0] == '#': continue tmpList = iLine.split() tmpX = xScale * float(tmpList[0]) * float(tmpList[0]) x.append(xScale * float(tmpList[0])) y.append(yScale * float(tmpList[1]) * tmpX) yLow.append(yScale * (float(tmpList[1]) - float(tmpList[2])) * tmpX) yHigh.append(yScale * (float(tmpList[3]) - float(tmpList[1])) * tmpX) f.close() listOfZeros = array("d", [0] * len(x)) gr = TGraphAsymmErrors(len(x), x, y, listOfZeros, listOfZeros, yLow, yHigh) # gr.SetLineColor( color ) gr.SetLineWidth(2) # gr.SetMarkerColor( color ) gr.SetMarkerStyle(21) gr.GetXaxis().SetTitle('Energy [GeV]') gr.GetYaxis().SetTitle('E^{2} x Flux [GeV cm^{-2}s^{-1}]') gr.SetTitle('') return gr
def getEff(name, den_input, num_input, rebin=0, xtitle='', ytitle=''): c = TCanvas(name + '_Canvas') legend = TLegend(0.8, 0.1, 0.999, 0.6) legend.SetFillColor(kWhite) den = den_input.Clone() num = num_input.Clone() if rebin != 0: den.Rebin(rebin) num.Rebin(rebin) error_bars = TGraphAsymmErrors() error_bars.Divide(num, den, "cl=0.683 b(1,1) mode") error_bars.SetLineWidth(3) if xtitle == '': error_bars.GetXaxis().SetTitle(num.GetXaxis().GetTitle()) else: error_bars.GetXaxis().SetTitle(xtitle) if ytitle == '': error_bars.GetYaxis().SetTitle(num.GetYaxis().GetTitle()) else: error_bars.GetYaxis().SetTitle(ytitle) error_bars.GetXaxis().SetRangeUser(400, 2000) error_bars.SetLineColor(kBlack) error_bars.SetMaximum(1.01) error_bars.SetMinimum(0.) if ytitle == '': error_bars.GetYaxis().SetTitle("Trigger rate") else: error_bars.GetYaxis().SetTitle(ytitle) error_bars.SetTitle('') error_bars.Draw('AP') c.SaveAs('pdf/' + name + '.pdf') c.Write(name + '_Canvas') error_bars.Write(name)
def convertHistToGraph(hist, useGarwood=False): alpha = 1 - 0.6827 graph = TGraphAsymmErrors(hist.GetNbinsX()) if useGarwood: lastEvent = False for i in reversed(range(hist.GetNbinsX())): N = hist.GetBinContent(i + 1) if not lastEvent and N > 0: lastEvent = True if lastEvent and N <= 0.: N = 1.e-6 L = 0 if N == 0 else ROOT.Math.gamma_quantile(alpha / 2, N, 1.) U = ROOT.Math.gamma_quantile_c(alpha / 2, N + 1, 1) graph.SetPoint(i, hist.GetXaxis().GetBinCenter(i + 1), N if not N == 0 else -1.e99) graph.SetPointError(i, 0., 0., N - L, U - N) else: for i in range(hist.GetNbinsX()): graph.SetPoint(i, hist.GetXaxis().GetBinCenter(i + 1), hist.GetBinContent(i + 1)) graph.SetPointError(i, hist.GetXaxis().GetBinWidth(i + 1) / 2., hist.GetXaxis().GetBinWidth(i + 1) / 2., hist.GetBinError(i + 1), hist.GetBinError(i + 1)) graph.SetLineWidth(hist.GetLineWidth()) graph.SetLineStyle(hist.GetLineStyle()) graph.SetLineColor(hist.GetLineColor()) graph.SetMarkerSize(hist.GetMarkerSize()) graph.SetMarkerStyle(hist.GetMarkerStyle()) graph.SetMarkerColor(hist.GetMarkerColor()) graph.SetFillStyle(hist.GetFillStyle()) graph.SetFillColor(hist.GetFillColor()) 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 bestfit(): xmin = -10 xmax = 6 c, h = draw_canvas_histo( xmin, xmax, "Best fit #mu = #sigma/#sigma_{SM} at m_{H} = 125 GeV") # line at SM expectation of mu = 1 l = TLine() l.SetLineStyle(2) l.DrawLine(1.0, -1.0, 1.0, -1.0 + nchannels) mu = np.array([-0.4, -4.734, -2.014]) upper = np.array([2.12, 3.688, 1.80]) lower = np.array([2.08, 3.754, 1.8224]) channel = np.array([1.5, 0.5, -0.5]) zero = np.zeros(nchannels) gmu = TGraphAsymmErrors(nchannels, mu, channel, lower, upper, zero, zero) gmu.SetMarkerStyle(21) gmu.SetMarkerSize(1.5) gmu.SetLineColor(2) gmu.SetLineWidth(2) gmu.Draw("pz") #draw_disclaimer() c.RedrawAxis() c.Modified() c.Update() c.SaveAs("bestfit.pdf")
def ReadHepDataROOT(HepDataFileName, tablenum): infile = TFile(HepDataFileName) table = infile.Get('Table %d' % tablenum) Hist1D_y1 = table.Get('Hist1D_y1') Hist1D_y1_e1 = table.Get('Hist1D_y1_e1') Hist1D_y1_e2plus = table.Get('Hist1D_y1_e2plus') Hist1D_y1_e2minus = table.Get('Hist1D_y1_e2minus') Hist1D_y1_e3 = table.Get('Hist1D_y1_e3') Hist1D_y1.SetDirectory(0) Hist1D_y1_e1.SetDirectory(0) Hist1D_y1_e2plus.SetDirectory(0) Hist1D_y1_e2minus.SetDirectory(0) Hist1D_y1_e3.SetDirectory(0) infile.Close() graphSyst = TGraphAsymmErrors() histoStat = Hist1D_y1.Clone('histoStat') for iPt in range(Hist1D_y1.GetNbinsX()): histoStat.SetBinContent(iPt + 1, Hist1D_y1.GetBinContent(iPt + 1)) histoStat.SetBinError(iPt + 1, Hist1D_y1_e1.GetBinContent(iPt + 1)) systlow = math.sqrt( Hist1D_y1_e2minus.GetBinContent(iPt + 1)**2 + Hist1D_y1_e3.GetBinContent(iPt + 1)**2) systhigh = math.sqrt( Hist1D_y1_e2plus.GetBinContent(iPt + 1)**2 + Hist1D_y1_e3.GetBinContent(iPt + 1)**2) graphSyst.SetPoint(iPt, histoStat.GetBinCenter(iPt + 1), histoStat.GetBinContent(iPt + 1)) graphSyst.SetPointError(iPt, histoStat.GetBinWidth(iPt + 1) / 2, histoStat.GetBinWidth(iPt + 1) / 2, systlow, systhigh) return histoStat, graphSyst
def MakePoissonConfidenceLevelErrors_ratio(hist, hden): x_val = array('f') y_val = array('f') x_errU = array('f') x_errL = array('f') y_errU = array('f') y_errL = array('f') if hist.GetNbinsX() != hden.GetNbinsX(): print "error: hist and hden have different number of bins" return None for b in xrange(1, hist.GetNbinsX() + 1): binEntries = hist.GetBinContent(b) den = hden.GetBinContent(b) if binEntries > 0. and den != 0.0: binErrUp = (calcPoissonCLUpper(binEntries) - binEntries) / den binErrLow = (binEntries - calcPoissonCLLower(binEntries)) / den x_val.append(hist.GetXaxis().GetBinCenter(b)) y_val.append(binEntries / den) y_errU.append(binErrUp) y_errL.append(binErrLow) x_errU.append(hist.GetXaxis().GetBinWidth(b) / 2.0) x_errL.append(hist.GetXaxis().GetBinWidth(b) / 2.0) #print "ratio:",hist.GetXaxis().GetBinCenter( b ), binEntries/den, binErrUp, binErrLow pass pass #print len(x_val), x_val, y_val, x_errL, x_errU, y_errL, y_errU if len(x_val) > 0: dataGraph = TGraphAsymmErrors(len(x_val), x_val, y_val, x_errL, x_errU, y_errL, y_errU) return dataGraph else: return None
def HistToAsymmErrs(name,hist) : X = [] EXlow = [] EXhigh = [] Y = [] EYlow = [] EYhigh = [] for i in range(hist.GetNbinsX()) : X .append(hist.GetBinCenter (i+1)) EXlow .append(hist.GetBinWidth (i+1)/2.) EXhigh.append(hist.GetBinWidth (i+1)/2.) Y .append(hist.GetBinContent(i+1)) EYlow .append(hist.GetBinError (i+1)) EYhigh.append(hist.GetBinError (i+1)) a_X = array('d',X ) a_EXlow = array('d',EXlow ) a_EXhigh = array('d',EXhigh ) a_Y = array('d',Y ) a_EYlow = array('d',EYlow ) a_EYhigh = array('d',EYhigh ) asymm = TGraphAsymmErrors(len(a_X),a_X,a_Y,a_EXlow,a_EXhigh,a_EYlow,a_EYhigh) asymm.GetXaxis().SetTitle(hist.GetXaxis().GetTitle()) asymm.GetYaxis().SetTitle(hist.GetYaxis().GetTitle()) asymm.SetLineWidth(2) asymm.SetName(name) return asymm
def QuadratureUpDown(asymlist, hists=[], doFlatUnc=False, FlatUp=0.0, FlatDown=0.0): # These are TGraphAsymmErrors total_unc_up_sq = [] total_unc_dn_sq = [] for i in range(asymlist[0].GetN()): total_unc_up_sq.append(0) total_unc_dn_sq.append(0) # # asym err hists # for i in range(asymlist[0].GetN()): for j in range(len(asymlist)): E_up = asymlist[j].GetEYhigh()[i] E_dn = asymlist[j].GetEYlow()[i] total_unc_up_sq[i] += math.pow(E_up, 2) total_unc_dn_sq[i] += math.pow(E_dn, 2) # # hists # for i in range(len(total_unc_up_sq)): for j in range(len(hists)): E_up = hists[j].GetBinError(i + 1) E_dn = hists[j].GetBinError(i + 1) total_unc_up_sq[i] += math.pow(E_up, 2) total_unc_dn_sq[i] += math.pow(E_dn, 2) # # flat uncertainty # if doFlatUnc: for i in range(len(total_unc_up_sq)): total_unc_up_sq[i] += math.pow(FlatUp * asymlist[0].GetY()[i], 2) total_unc_dn_sq[i] += math.pow(FlatDown * asymlist[0].GetY()[i], 2) # # sqrt # for i in range(len(total_unc_up_sq)): total_unc_up_sq[i] = math.sqrt(total_unc_up_sq[i]) total_unc_dn_sq[i] = math.sqrt(total_unc_dn_sq[i]) #print 'Setting up unc to ',total_unc_up_sq[i] #print 'Setting dn unc to ',total_unc_dn_sq[i] #print total_unc_up_sq #print total_unc_dn_sq yup = array('d', total_unc_up_sq) ydn = array('d', total_unc_dn_sq) #print yup #print ydn result = TGraphAsymmErrors(asymlist[0].GetN(), asymlist[0].GetX(), asymlist[0].GetY(), asymlist[0].GetEXlow(), asymlist[0].GetEXhigh(), ydn, yup) result.GetXaxis().SetTitle(asymlist[0].GetXaxis().GetTitle()) result.GetYaxis().SetTitle(asymlist[0].GetYaxis().GetTitle()) result.SetLineWidth(2) result.SetName(asymlist[0].GetName() + ' Total Error') return result
def geterrorband(*hists, **kwargs): """Make an error band histogram for a list of histograms, or stack. Returns an TGraphAsymmErrors object.""" verbosity = LOG.getverbosity(kwargs) hists = unwraplistargs(hists) hists = [ h.GetStack().Last() if isinstance(h, THStack) else h for h in hists ] sysvars = kwargs.get( 'sysvars', []) # list of tuples with up/cent/down variation histograms name = kwargs.get('name', None) or "error_" + hists[0].GetName() title = kwargs.get( 'title', None) or ("Sys. + stat. unc." if sysvars else "Stat. unc.") color = kwargs.get('color', kBlack) hist0 = hists[0] nbins = hist0.GetNbinsX() if sysvars and isinstance(sysvars, dict): sysvars = [v for k, v in sysvars.iteritems()] error = TGraphAsymmErrors() error.SetName(name) error.SetTitle(title) LOG.verb("geterrorband: Making error band for %s" % (hists), verbosity, 2) TAB = LOG.table( "%5s %7s %6s %10s %11s %-20s %-20s %-20s", "%5d %7.6g %6.6g %10.2f %11.2f +%8.2f -%8.2f +%8.2f -%8.2f +%8.2f -%8.2f", verb=verbosity, level=3) TAB.printheader("ibin", "xval", "xerr", "nevts", "sqrt(nevts)", "statistical unc.", "systematical unc.", "total unc.") ip = 0 for ibin in range(1, nbins + 1): xval = hist0.GetXaxis().GetBinCenter(ibin) xerr = 0 if ibin in [0, nbins + 1] else hist0.GetXaxis().GetBinWidth(ibin) / 2 yval = 0 statlow2, statupp2 = 0, 0 syslow2, sysupp2 = 0, 0 for hist in hists: # STATISTICS yval += hist.GetBinContent(ibin) statlow2 += hist.GetBinErrorLow(ibin)**2 statupp2 += hist.GetBinErrorUp(ibin)**2 for histup, hist, histdown in sysvars: # SYSTEMATIC VARIATIONS syslow2 += (hist.GetBinContent(ibin) - histdown.GetBinContent(ibin))**2 sysupp2 += (hist.GetBinContent(ibin) - histup.GetBinContent(ibin))**2 ylow2, yupp2 = statlow2 + syslow2, statupp2 + sysupp2, error.SetPoint(ip, xval, yval) error.SetPointError(ip, xerr, xerr, sqrt(ylow2), sqrt(yupp2)) TAB.printrow(ibin, xval, xerr, yval, sqrt(abs(yval)), sqrt(statupp2), sqrt(statlow2), sqrt(sysupp2), sqrt(syslow2), sqrt(yupp2), sqrt(ylow2)) ip += 1 seterrorbandstyle(error, color=color) #error.SetLineColor(hist0.GetLineColor()) error.SetLineWidth(hist0.GetLineWidth()) # use draw option 'E2 SAME' return error