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 dividebybinsize(hist, **kwargs): """Divide each bin by its bin width. If a histogram has assymmetric errors (e.g. data with Poisson), return a TGraphAsymmErrors instead.""" verbosity = LOG.getverbosity(kwargs) LOG.verbose('dividebybinsize: "%s"' % (hist.GetName()), verbosity, 2) zero = kwargs.get('zero', True) # include bins that are zero in TGraph zeroerrs = kwargs.get('zeroerrs', True) # include errors for zero bins errorX = kwargs.get('errorX', gStyle.GetErrorX()) # horizontal error bars nbins = hist.GetXaxis().GetNbins() TAB = LOG.table("%5s %8.6g %8.6g %10.3f %9.4f %8.4f %8.4f %10.4f", verb=verbosity, level=3) TAB.printheader("ibin", "xval", "width", "yval", "yerr", "yupp", "ylow", "yerr/width") if hist.GetBinErrorOption( ) == TH1.kPoisson: # make asymmetric Poisson errors (like for data) graph = TGraphAsymmErrors() graph.SetName(hist.GetName() + "_graph") graph.SetTitle(hist.GetTitle()) copystyle(graph, hist) ip = 0 # skip zero bins if not zero for ibin in xrange(1, nbins + 1): xval = hist.GetXaxis().GetBinCenter(ibin) width = hist.GetXaxis().GetBinWidth(ibin) xerr = width / 2 if errorX else 0 yval = hist.GetBinContent(ibin) yerr = hist.GetBinError(ibin) yupp = hist.GetBinErrorUp(ibin) ylow = hist.GetBinErrorLow(ibin) TAB.printrow(ibin, xval, width, yval, yerr, yupp, ylow, yval / width) hist.SetBinContent(ibin, yval / width) hist.SetBinError(ibin, yerr / width) if yval != 0 or zero: graph.SetPoint(ip, xval, yval / width) if yval != 0 or zeroerrs: graph.SetPointError(ip, xerr, xerr, ylow / width, yupp / width) else: graph.SetPointError(ip, xerr, xerr, 0, 0) ip += 1 return graph else: for ibin in xrange(0, nbins + 2): xval = hist.GetXaxis().GetBinCenter(ibin) width = hist.GetXaxis().GetBinWidth(ibin) yval = hist.GetBinContent(ibin) yerr = hist.GetBinError(ibin) hist.SetBinContent(ibin, yval / width) hist.SetBinError(ibin, yerr / width) TAB.printrow(ibin, xval, width, yval, yerr, hist.GetBinErrorUp(ibin), hist.GetBinErrorLow(ibin), yval / width) return hist
def obsOverPredWithDataStat(data_hist, bkg_total): obs_over_pred = TGraphAsymmErrors(data_hist) for b in range(1, data_hist.GetNbinsX() + 1): #divide data by background bkg_bin = bkg_total.GetBinContent(b) if abs(bkg_bin) > 1e-8: obs_over_pred.GetY()[b - 1] /= bkg_bin obs_over_pred.SetPointError(b - 1, 0., 0., data_hist.GetBinErrorLow(b) / bkg_bin, data_hist.GetBinErrorUp(b) / bkg_bin) else: obs_over_pred.GetY()[b - 1] = 0 obs_over_pred.SetPointError(b - 1, 0., 0., 0, 0) return obs_over_pred
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 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 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
def MakeErrorGraphForSource(self, source): result = TGraphAsymmErrors() counter = 0 for point in sorted(self._pointlist): result.SetPoint(counter, point.GetX(), point.GetY()) result.SetPointError(counter, point.GetDX(), point.GetDX(), point.GetLowerErrorForSource(source), point.GetUpperErrorForSource(source)) counter += 1 return result
def fit(x, y): from ROOT import TF1, TGraphAsymmErrors g = TGraphAsymmErrors(len(x)) for i in xrange(len(x)): g.SetPoint(i, x[i], y[i].nominal_value) g.SetPointError(i, 0, 0, y[i].std_dev, y[i].std_dev) fit = TF1("fit", "-[1]*x*x+[0]", -40, 120) fit.SetParNames("z0", "a") g.Fit(fit) return fit
def getDataPoissonErrors(hist, kPoisson=False, drawZeroBins=False, drawXbars=False, centerBin=True): '''Make data poisson errors for a histogram with two different methods: - TH1.kPoisson - chi-squared quantile ''' # https://github.com/DESY-CMS-SUS/cmgtools-lite/blob/8_0_25/TTHAnalysis/python/plotter/mcPlots.py#L70-L102 # https://github.com/DESY-CMS-SUS/cmgtools-lite/blob/8_0_25/TTHAnalysis/python/plotter/susy-1lep/RcsDevel/plotDataPredictWithSyst.py#L12-L21 if kPoisson: hist.SetBinErrorOption(TH1D.kPoisson) Nbins = hist.GetNbinsX() xaxis = hist.GetXaxis() alpha = (1 - 0.6827) / 2. graph = TGraphAsymmErrors(Nbins) graph.SetName(hist.GetName() + "_graph") graph.SetTitle(hist.GetTitle()) for i in xrange(1, Nbins + 1): N = hist.GetBinContent(i) if N <= 0 and not drawZeroBins: continue dN = hist.GetBinError(i) yscale = 1 if centerBin: x = xaxis.GetBinCenter(i) else: x = xaxis.GetBinLowEdge(i) if N > 0 and dN > 0 and abs(dN**2 / N - 1) > 1e-4: # check is error is Poisson yscale = (dN**2 / N) N = (N / dN)**2 if kPoisson: EYlow = hist.GetBinErrorLow(i) EYup = hist.GetBinErrorUp(i) else: EYlow = (N - Math.chisquared_quantile_c(1 - alpha, 2 * N) / 2.) if N > 0 else 0 EYup = Math.chisquared_quantile_c(alpha, 2 * (N + 1)) / 2. - N y = yscale * N EXup = xaxis.GetBinUpEdge(i) - x if drawXbars else 0 EXlow = x - xaxis.GetBinLowEdge(i) if drawXbars else 0 graph.SetPoint(i - 1, x, y) graph.SetPointError(i - 1, EXlow, EXup, EYlow, EYup) #print ">>> getDataPoissonErrors - bin %2d: (x,y) = ( %3.1f - %4.2f + %4.2f, %4.2f - %4.2f + %4.2f )"%(i,x,EXlow,EXup,y,EYlow,EYup) graph.SetLineWidth(hist.GetLineWidth()) graph.SetLineColor(hist.GetLineColor()) graph.SetLineStyle(hist.GetLineStyle()) graph.SetMarkerSize(hist.GetMarkerSize()) graph.SetMarkerColor(hist.GetMarkerColor()) graph.SetMarkerStyle(hist.GetMarkerStyle()) return graph
def obsOverPredWithDataStat(data_hist, bkg_total): obs_over_pred = TGraphAsymmErrors(data_hist) for b in range(1, data_hist.GetNbinsX() + 1): #divide data by background bkg_bin = bkg_total.GetBinContent(b) if abs(bkg_bin) > 1e-8: obs_over_pred.GetY()[b - 1] /= bkg_bin obs_over_pred.SetPointError(b - 1, 0., 0., data_hist.GetBinErrorLow(b) / bkg_bin, data_hist.GetBinErrorUp(b) / bkg_bin) else: obs_over_pred.GetY()[b - 1] = 0 obs_over_pred.SetPointError(b - 1, 0., 0., 0, 0) #in case we plot just the expected distribution data will be 0 #in this case we don't want to plot the data points in the ratio if data_hist.GetSumOfWeights() <= 0: for b in range(1, data_hist.GetNbinsX() + 1): obs_over_pred.GetY()[b - 1] += 1e30 return obs_over_pred
def createGraph(x, y, exh, exl, eyh, eyl): """ Create a TGraphAsymmErrors from the passed (lists) of values """ from ROOT import TGraphAsymmErrors nPoints = len(x) # simply assume that all lists have the same length graph = TGraphAsymmErrors(nPoints) for i in range(0, nPoints): # "blinding" of signal region # if x[i] > 5.2 and x[i] < 5.4: continue graph.SetPoint(i, x[i], y[i]) graph.SetPointError(i, exl[i], exh[i], eyl[i], eyh[i]) return graph
def getGraph(result, label): masses = result["mass"] massErr = result["massErr"] sigma = result["sigma"] sigmaErr = result["sigmaErr"] res = TGraphAsymmErrors(len(masses)) res.SetName(label) for i, mass in enumerate(masses): res.SetPoint(i, mass, sigma[i]) res.SetPointError(i, massErr[i], massErr[i], sigmaErr[i], sigmaErr[i]) return res
def getGraphFromHistos(histoCent, histoMin, histoMax): ''' Method that takes in input 3 histos and returns a graph ''' nPoints = histoCent.GetNbinsX() graph = TGraphAsymmErrors(nPoints) for iPt in range(nPoints): centC = histoCent.GetBinContent(iPt+1) minC = histoMin.GetBinContent(iPt+1) maxC = histoMax.GetBinContent(iPt+1) width = histoCent.GetBinWidth(iPt+1)/2 graph.SetPoint(iPt, histoCent.GetBinCenter(iPt+1), centC) graph.SetPointError(iPt, width, width, centC-minC, maxC-centC) return graph
def getGraph(result, label, Data=False): ptda = result["ptda"] if Data: sigma = result["da_nChi2"] else: sigma = result["mc_nChi2"] sigmaErr = 0 res = TGraphAsymmErrors(len(ptda)) res.SetName(label) for i, pt in enumerate(ptda): res.SetPoint(i, pt, sigma[i]) res.SetPointError(i, ptda[i] - ptbins[i], ptbins[i + 1] - ptda[i], 0, 0) return res
def convertHistToGraph(hist): graph = TGraphAsymmErrors(hist.GetNbinsX()) for i in range(hist.GetNbinsX()): graph.SetPoint(i, hist.GetXaxis().GetBinCenter(i), hist.GetBinContent(i)) graph.SetPointError(i, hist.GetXaxis().GetBinWidth(i) / 2., hist.GetXaxis().GetBinWidth(i) / 2., hist.GetBinError(i), hist.GetBinError(i)) 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 ComputeRatioGraph(gNum, gDen, useDenUnc=True): ''' Helper method to divide two TGraph (assuming same binning) Parameters ---------- - gNum: graph to divide (numerator) - gDen: graph to divide (denominator) Returns ---------- - gRatio: resulting graph ''' if gNum.GetN() != gDen.GetN(): print('ERROR: only graphs with same number of bins can be divided!') return None gRatio = TGraphAsymmErrors(1) for iPt in range(gNum.GetN()): x, num = ctypes.c_double(), ctypes.c_double() xd, den = ctypes.c_double(), ctypes.c_double() gNum.GetPoint(iPt, x, num) xUncLow = gNum.GetErrorXlow(iPt) xUncHigh = gNum.GetErrorXhigh(iPt) numUncLow = gNum.GetErrorYlow(iPt) numUncHigh = gNum.GetErrorYhigh(iPt) gDen.GetPoint(iPt, xd, den) denUncLow = gDen.GetErrorYlow(iPt) denUncHigh = gDen.GetErrorYhigh(iPt) ratio, ratioUncLow, ratioUncHigh = 0., 0., 0. if num.value != 0. and den.value != 0.: ratio = num.value/den.value if useDenUnc: ratioUncLow = np.sqrt((numUncLow/num.value)**2 + (denUncLow/den.value)**2) * ratio ratioUncHigh = np.sqrt((numUncHigh/num.value)**2 + (denUncHigh/den.value)**2) * ratio else: ratioUncLow = numUncLow / num.value * ratio ratioUncHigh = numUncHigh / num.value * ratio gRatio.SetPoint(iPt, x.value, ratio) gRatio.SetPointError(iPt, xUncLow, xUncHigh, ratioUncLow, ratioUncHigh) return gRatio
def prepareData(data): #set poison errors data.SetBinErrorOption(ROOT.TH1.kPoisson) #build TGraphAsymmErrors for plotting data_graph = TGraphAsymmErrors(data) for b in range(1, data.GetNbinsX() + 1): bin_content = data.GetBinContent(b) data_graph.SetPointError( b - 1, 0, 0, data.GetBinErrorLow(b) if bin_content >= 0. else 0., data.GetBinErrorUp(b) if bin_content >= 0. else 0.) #hack for not displaying points at zero maximum = (data.GetBinContent(data.GetMaximumBin()) + data.GetBinErrorUp(data.GetMaximumBin())) for b in range(1, data.GetNbinsX() + 1): if data.GetBinContent(b) < 1e-8: data_graph.GetY()[b - 1] += (maximum * 1e8) return data, data_graph
def efficiencyRatioSF( eff1, eff2, ): newEff = TGraphAsymmErrors(eff1.GetN()) for i in range(0, eff1.GetN()): pointX1 = ROOT.Double(0.) pointX2 = ROOT.Double(0.) pointY1 = ROOT.Double(0.) pointY2 = ROOT.Double(0.) isSuccesful1 = eff1.GetPoint(i, pointX1, pointY1) isSuccesful2 = eff2.GetPoint(i, pointX2, pointY2) errY1Up = eff1.GetErrorYhigh(i) errY1Down = eff1.GetErrorYlow(i) errY2Up = eff2.GetErrorYhigh(i) errY2Down = eff2.GetErrorYlow(i) errX = eff1.GetErrorX(i) if pointY1 != 0 and pointY2 != 0: yValue = pointY1 / pointY2 xValue = pointX1 xError = errX yErrorUp = math.sqrt((1. / pointY2 * errY1Up)**2 + (pointY1 / pointY2**2 * errY2Down)**2) yErrorDown = math.sqrt((1. / pointY2 * errY1Down)**2 + (pointY1 / pointY2**2 * errY2Up)**2) else: yValue = 0 xValue = pointX1 xError = errX yErrorUp = 0 yErrorDown = 0 newEff.SetPoint(i, xValue, yValue) newEff.SetPointError(i, xError, xError, yErrorDown, yErrorUp) return newEff
def getErrHist(plot,ofHist,rSFOFErr): hist = TH1F("errHist","errHist",plot.nBins,plot.firstBin,plot.lastBin) histUp = TH1F("errHist","errHist",plot.nBins,plot.firstBin,plot.lastBin) histDown = TH1F("errHist","errHist",plot.nBins,plot.firstBin,plot.lastBin) graph = TGraphAsymmErrors() for i in range(1,hist.GetNbinsX()+1): hist.SetBinContent(i,1) hist.SetBinError(i,ofHist.GetBinContent(i)*rSFOFErr) for i in range(0,hist.GetNbinsX()+1): graph.SetPoint(i,plot.firstBin - ((plot.lastBin-plot.firstBin)/plot.nBins)*0.5 +(i)*((plot.lastBin-plot.firstBin)/plot.nBins),ofHist.GetBinContent(i)) graph.SetPointError(i,((plot.firstBin-plot.lastBin)/plot.nBins)*0.5,((plot.firstBin-plot.lastBin)/plot.nBins)*0.5,(hist.GetBinError(i)**2 + ofHist.GetBinContent(i))**0.5,(hist.GetBinError(i)**2 + ofHist.GetBinContent(i))**0.5) for i in range(1,hist.GetNbinsX()+1): histUp.SetBinContent(i,ofHist.GetBinContent(i) + hist.GetBinError(i)) histDown.SetBinContent(i,ofHist.GetBinContent(i) - hist.GetBinError(i)) if ofHist.GetBinContent(i) > 0: hist.SetBinError(i,hist.GetBinError(i) / (ofHist.GetBinContent(i))) else: hist.SetBinError(i,0) return graph, histUp, histDown
def efficiencyRatio(eff1, eff2): newEff = TGraphAsymmErrors(eff1.GetN()) for i in range(0, eff1.GetN()): pointX1 = Double(0.) pointX2 = Double(0.) pointY1 = Double(0.) pointY2 = Double(0.) isSuccesful1 = eff1.GetPoint(i, pointX1, pointY1) isSuccesful2 = eff2.GetPoint(i, pointX2, pointY2) errY1Up = eff1.GetErrorYhigh(i) errY1Low = eff1.GetErrorYlow(i) errY2Up = eff2.GetErrorYhigh(i) errY2Low = eff2.GetErrorYlow(i) errX = eff1.GetErrorX(i) if pointY2 != 0: yValue = pointY1 / pointY2 xValue = pointX1 xError = errX #~ yErrorUp = math.sqrt(((1/pointY2)*errY1Up)**2+((pointY1/pointY2**2)*errY2Up)**2) yErrorUp = math.sqrt(((1 / pointY2) * errY1Up)**2 + ((pointY1 / pointY2**2) * errY2Up)**2) yErrorDown = math.sqrt(((1 / pointY2) * errY1Low)**2 + ((pointY1 / pointY2**2) * errY2Low)**2) else: yValue = 0 xValue = pointX1 xError = errX yErrorUp = 0 yErrorDown = 0 #~ print i newEff.SetPoint(i, xValue, yValue) newEff.SetPointError(i, xError, xError, yErrorDown, yErrorUp) return newEff
def getRatio(result, result2, label, Data=False): ptda = result["ptda"] ptda2 = result2["ptda"] if Data: sigma = result["da_nChi2"] sigma2 = result2["da_nChi2"] else: sigma = result["mc_nChi2"] sigma2 = result2["mc_nChi2"] print(sigma) print(sigma2) ratio = TGraphAsymmErrors(len(ptda)) ratio.SetName(label) for i, pt in enumerate(ptda): ratio.SetPoint(i, pt, sigma[i] / sigma2[i]) ratio.SetPointError(i, ptda[i] - ptbins[i], ptbins[i + 1] - ptda[i], 0, 0) return ratio
def averageGraphs(graphs): """ Create a new TGraphAsymmErrors from the passed list of TGraphAsymmErrors where the central values are the average values of all graphs. The uncertainties are computed from adding in quadrature the average of the uncertainties and the std-deviation of the central values """ from ROOT import TGraphAsymmErrors from math import sqrt # simply assume that all graphs have the same number of points nPoints = graphs[0].GetN() graph = TGraphAsymmErrors(nPoints) for i in range(0, nPoints): [centralVal, cValSig] = getAvgVal(graphs, i) meanErr = getAvgErr(graphs, i) uncer = sqrt(cValSig**2 + meanErr**2) [x, exl, exh] = getXInfo(graphs[0], i) # simply assume it is the same for all graph.SetPoint(i, x, centralVal) graph.SetPointError(i, exl, exh, uncer, uncer) return graph
def DivideGraphByHisto(gNum, hDen, useHistoUnc=True): ''' Helper method to divide a TGraph by a TH1 (assuming same binning) Parameters ---------- - gNum: graph to divide (numerator) - hDen: histogram (denominator) Returns ---------- - gRatio: resulting graph ''' if gNum.GetN() != hDen.GetNbinsX(): print('ERROR: only graphs and histos with same number of bins can be divided!') return None gRatio = TGraphAsymmErrors(0) for iPt in range(gNum.GetN()): x, num = ctypes.c_double(), ctypes.c_double() gNum.GetPoint(iPt, x, num) xUncLow = gNum.GetErrorXlow(iPt) xUncHigh = gNum.GetErrorXhigh(iPt) numUncLow = gNum.GetErrorYlow(iPt) numUncHigh = gNum.GetErrorYhigh(iPt) ptBinHisto = hDen.GetXaxis().FindBin(x.value) den = hDen.GetBinContent(ptBinHisto) if useHistoUnc: ratioUncLow = np.sqrt((numUncLow/num.value)**2 + (hDen.GetBinError(ptBinHisto)/den)**2) * num.value/den ratioUncHigh = np.sqrt((numUncHigh/num.value)**2 + (hDen.GetBinError(ptBinHisto)/den)**2) * num.value/den else: ratioUncLow = numUncLow/num.value * num.value/den ratioUncHigh = numUncHigh/num.value * num.value/den gRatio.SetPoint(iPt, x.value, num.value/den) gRatio.SetPointError(iPt, xUncLow, xUncHigh, ratioUncLow, ratioUncHigh) return gRatio
def fixPlots_withErrors(middleGraph): thisBandsGraph = TGraphAsymmErrors() thisBandsGraph.SetName(middleGraph.GetName()) thisBandsGraph.SetMarkerStyle(middleGraph.GetMarkerStyle()) thisBandsGraph.SetMarkerSize(middleGraph.GetMarkerSize()) thisBandsGraph.SetMarkerColor(middleGraph.GetMarkerColor()) thisBandsGraph.SetLineColor(middleGraph.GetLineColor()) for iPoint in xrange(0, middleGraph.GetN()): # Retrieve middle graph for (x,y) coordinates dataPointX = Double(0) dataPointY = Double(0) dataErrorX = Double(0) middleGraph.GetPoint(iPoint, dataPointX, dataPointY) dataErrorX = middleGraph.GetErrorX(iPoint) dataErrorY = middleGraph.GetErrorY(iPoint) dataPointY = fabs(dataPointY - 1) if (iPoint < middleGraph.GetN()): #if(dataPointY != 0 and iPoint < middleGraph.GetN()): print "fixPlots:", dataPointX thisBandsGraph.SetPoint(iPoint, dataPointX, dataPointY) thisBandsGraph.SetPointError(iPoint, dataErrorX, dataErrorX, dataErrorY, dataErrorY) #if (dataPointY == 0) : #thisBandsGraph.SetPoint(iPoint, dataPointX, dataPointY) #thisBandsGraph.SetPointError(iPoint, dataErrorX, dataErrorX, dataErrorY, dataErrorY) return thisBandsGraph
def readFile(fname, maxenergy=4.8): infile = file(fname, 'r') inlines = infile.readlines() graph = TGraphAsymmErrors() for iline in range(0, len(inlines)): line = inlines[iline] if '*' in line: continue lineparts = ' '.join(line.split()).strip().split(' ') if not check_number(lineparts[0:5]): continue energy = float(lineparts[0]) R = float(lineparts[3]) if (energy > maxenergy): continue statupp = abs(float(lineparts[4])) statlow = abs(float(lineparts[5])) (systlow, systupp) = systematics(iline, inlines) ipoint = graph.GetN() graph.SetPoint(ipoint, energy, R) graph.SetPointError( ipoint, 0, 0, math.sqrt(statlow * statlow + 1e-4 * systlow * systlow * R * R), math.sqrt(statupp * statupp + 1e-4 * systupp * systupp * R * R)) return graph
def pullsVertical_noBonly(fileName): content = filterPullFile(fileName) nbins, off = len(content), 0.10 # Graphs h_pulls = TH2F("pulls", "", 6, -3., 3., nbins, 0, nbins) S_pulls = TGraphAsymmErrors(nbins) boxes = [] canvas = TCanvas("canvas", "Pulls", 720, 300 + nbins * 18) #nbins*20) canvas.cd() canvas.SetGrid(0, 1) canvas.SetTopMargin(0.01) canvas.SetRightMargin(0.01) canvas.SetBottomMargin(0.10) canvas.SetLeftMargin(0.40) canvas.SetTicks(1, 1) for i, s in enumerate(content): l = s.split() h_pulls.GetYaxis().SetBinLabel(i + 1, l[0]) S_pulls.SetPoint(i, float(l[3]), float(i + 1) - 0.5) S_pulls.SetPointError(i, float(l[4]), float(l[4]), 0., 0.) h_pulls.GetXaxis().SetTitle("(#hat{#theta} - #theta_{0}) / #Delta#theta") h_pulls.GetXaxis().SetLabelOffset(0.0) h_pulls.GetXaxis().SetTitleOffset(0.8) h_pulls.GetXaxis().SetLabelSize(0.045) h_pulls.GetXaxis().SetTitleSize(0.050) h_pulls.GetYaxis().SetLabelSize(0.046) h_pulls.GetYaxis().SetNdivisions(nbins, 0, 0) S_pulls.SetFillColor(kBlack) S_pulls.SetLineColor(kBlack) S_pulls.SetMarkerColor(kBlack) S_pulls.SetLineWidth(2) S_pulls.SetMarkerStyle(20) S_pulls.SetMarkerSize(1) box1 = TBox(-1., 0., 1., nbins) #box1.SetFillStyle(3001) # 3001 checkered #box1.SetFillStyle(0) box1.SetFillColor(kGreen + 1) # 417 box1.SetLineWidth(2) box1.SetLineStyle(2) box1.SetLineColor(kGreen + 1) # 417 box2 = TBox(-2., 0., 2., nbins) #box2.SetFillStyle(3001) # 3001 checkered #box2.SetFillStyle(0) box2.SetFillColor(kOrange) # 800 box2.SetLineWidth(2) box2.SetLineStyle(2) box2.SetLineColor(kOrange) # 800 leg = TLegend(0.01, 0.01, 0.3, 0.15) leg.SetTextSize(0.05) leg.SetBorderSize(0) leg.SetFillStyle(0) leg.SetFillColor(0) #leg.SetNColumns(2) leg.AddEntry(S_pulls, "S+B fit", "lp") if text: leg.AddEntry(0, text, "") h_pulls.Draw("") box2.Draw() box1.Draw() S_pulls.Draw("P6SAME") leg.Draw() canvas.RedrawAxis() canvas.Print(outName + ".png") canvas.Print(outName + ".pdf") if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
def pullsVertical(fileName): content = filterPullFile(fileName) nbins, off = len(content), 0.10 b_pulls = TH1F("b_pulls", ";;Pulls", nbins, 0. - off, nbins - off) s_pulls = TH1F("s_pulls", ";;Pulls", nbins, 0. + off, nbins + off) # for i, s in enumerate(content): l = s.split() b_pulls.GetXaxis().SetBinLabel(i + 1, l[0]) s_pulls.GetXaxis().SetBinLabel(i + 1, l[0]) b_pulls.SetBinContent(i + 1, float(l[1])) b_pulls.SetBinError(i + 1, float(l[2])) s_pulls.SetBinContent(i + 1, float(l[3])) s_pulls.SetBinError(i + 1, float(l[4])) b_pulls.SetFillStyle(3005) b_pulls.SetFillColor(923) b_pulls.SetLineColor(923) b_pulls.SetLineWidth(1) b_pulls.SetMarkerStyle(20) b_pulls.SetMarkerSize(1.25) s_pulls.SetLineColor(602) s_pulls.SetMarkerColor(602) s_pulls.SetMarkerStyle(24) #24 s_pulls.SetLineWidth(1) b_pulls.GetYaxis().SetRangeUser(-2.5, 2.5) # Graphs h_pulls = TH2F("pulls", "", 6, -3., 3., nbins, 0, nbins) B_pulls = TGraphAsymmErrors(nbins) S_pulls = TGraphAsymmErrors(nbins) boxes = [] canvas = TCanvas("canvas", "Pulls", 600, 150 + nbins * 10) #nbins*20) canvas.cd() canvas.SetGrid(0, 1) canvas.GetPad(0).SetTopMargin(0.01) canvas.GetPad(0).SetRightMargin(0.01) canvas.GetPad(0).SetBottomMargin(0.05) canvas.GetPad(0).SetLeftMargin(0.25) #(0.25)#(0.065) canvas.GetPad(0).SetTicks(1, 1) for i, s in enumerate(content): l = s.split() if "1034h" in l[0]: l[0] = "CMS_PDF_13TeV" h_pulls.GetYaxis().SetBinLabel(i + 1, l[0].replace('CMS2016_', '')) #C #y1 = gStyle.GetPadBottomMargin() #y2 = 1. - gStyle.GetPadTopMargin() #h = (y2 - y1) / float(nbins) #y1 = y1 + float(i) * h #y2 = y1 + h #box = TPaveText(0, y1, 1, y2, 'NDC') #box.SetFillColor(0) #box.SetTextSize(0.02) #box.SetBorderSize(0) #box.SetTextAlign(12) #box.SetMargin(0.005) #if i % 2 == 0: # box.SetFillColor(18) #box.Draw() #boxes.append(box) B_pulls.SetPoint(i + 1, float(l[1]), float(i + 1) - 0.3) #C B_pulls.SetPointError(i + 1, float(l[2]), float(l[2]), 0., 0.) #C for i, s in enumerate(content): l = s.split() S_pulls.SetPoint(i + 1, float(l[3]), float(i + 1) - 0.7) #C S_pulls.SetPointError(i + 1, float(l[4]), float(l[4]), 0., 0.) #C h_pulls.GetXaxis().SetTitle("(#hat{#theta} - #theta_{0}) / #Delta#theta") h_pulls.GetXaxis().SetLabelOffset(-0.01) h_pulls.GetXaxis().SetTitleOffset(.6) h_pulls.GetYaxis().SetNdivisions(nbins, 0, 0) B_pulls.SetFillColor(1) B_pulls.SetLineColor(1) B_pulls.SetLineStyle(1) B_pulls.SetLineWidth(2) B_pulls.SetMarkerColor(1) B_pulls.SetMarkerStyle(20) B_pulls.SetMarkerSize(1) #(0.75) S_pulls.SetFillColor(629) S_pulls.SetLineColor(629) S_pulls.SetMarkerColor(629) S_pulls.SetLineWidth(2) S_pulls.SetMarkerStyle(20) S_pulls.SetMarkerSize(1) box1 = TBox(-1., 0., 1., nbins) box1.SetFillStyle(3001) #box1.SetFillStyle(0) box1.SetFillColor(417) box1.SetLineWidth(2) box1.SetLineStyle(2) box1.SetLineColor(417) box2 = TBox(-2., 0., 2., nbins) box2.SetFillStyle(3001) #box2.SetFillStyle(0) box2.SetFillColor(800) box2.SetLineWidth(2) box2.SetLineStyle(2) box2.SetLineColor(800) leg = TLegend(0.1, -0.05, 0.7, 0.08) leg.SetBorderSize(0) leg.SetFillStyle(0) leg.SetFillColor(0) leg.SetNColumns(2) leg.AddEntry(B_pulls, "B-only fit", "lp") leg.AddEntry(S_pulls, "S+B fit", "lp") if text: leg.AddEntry(0, text, "") h_pulls.Draw("") box2.Draw() box1.Draw() B_pulls.Draw("P6SAME") S_pulls.Draw("P6SAME") leg.Draw() # drawCMS(35867, "Preliminary") # drawAnalysis("VH") # drawRegion(outName) canvas.Print(outName + ".png") canvas.Print(outName + ".pdf") if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
hMassSB = GetSideBandHisto( hMassData, mean, sigma, 'hMassSB_pT_%0.f_%0.f' % (cutVars['Pt']['min'][iPt], cutVars['Pt']['max'][iPt])) B = GetExpectedBackgroundFromSB(hMassSB, mean, sigma, Nexp, nEvBkg) if inputCfg['PredForFprompt']['estimateFprompt']: fprompt, fpromptmin, fpromptmax = ComputeExpectedFprompt( ptmin, ptmax, effPrompt, hPredPrompt, hPredPromptMin, hPredPromptMax, effFD, hPredFD, hPredFDMin, hPredFDMax, RatioRaaFDPrompt) else: fprompt, fpromptmin, fpromptmax = (inputCfg['fprompt'] for iF in range(3)) gFprompt.SetPoint(iPt, PtCent, fprompt) gFprompt.SetPointError(iPt, PtUnc, PtUnc, fprompt - fpromptmin, fpromptmax - fprompt) SignifTAMU, SignifPHSD, SignifGossiaux, SignifCatania = ( {} for iDic in range(4)) SignifUncTAMU, SignifUncPHSD, SignifUncGossiaux, SignifUncCatania = ( {} for iDic in range(4)) CorrYieldTAMU, CorrYieldPHSD, CorrYieldGossiaux, CorrYieldCatania = ( {} for iDic in range(4)) RawYieldTAMU, RawYieldPHSD, RawYieldGossiaux, RawYieldCatania = ( {} for iDic in range(4)) for iFONLL in FONLL: if iFONLL == 'Cent' or iFONLL == 'Min' or iFONLL == 'Max': SignifTAMU[iFONLL], SignifUncTAMU[iFONLL], CorrYieldTAMU[ iFONLL], RawYieldTAMU[iFONLL] = (dict(RaaTAMU) for iDic in range(4))
fakeTrackBkgdEstimate.addChannel( "DisTrkInvertD0NHits5", "DisTrkSelectionSidebandD0CutNHits5", "MET_2016" + runPeriod, dirs['Andrew'] + "2016_final_prompt/fakeTrackSystematic_d0Sideband_new_v2") fakeTrackBkgdEstimate.addChannel( "DisTrkInvertD0NHits6", "DisTrkSelectionSidebandD0CutNHits6", "MET_2016" + runPeriod, dirs['Andrew'] + "2016_final_prompt/fakeTrackSystematic_d0Sideband_new_v2") fakeTrackBkgdEstimate.addChannel( "Basic", "BasicSelection", "MET_2016" + runPeriod, dirs['Andrew'] + "2016_final_prompt/basicSelection_new") nEst = fakeTrackBkgdEstimate.printNest() g0.SetPoint(i, minD0, nEst[0]) g0.SetPointError(i, D / 2.0, D / 2.0, min(nEst[1], nEst[0]), nEst[1]) zToMuMuEstimate = FakeTrackBkgdEstimate() zToMuMuEstimate.addLuminosityInInvPb(lumi["SingleMuon_2016" + runPeriod]) zToMuMuEstimate.addMinD0(minD0) zToMuMuEstimate.addChannel( "Basic3hits", "ZtoMuMuDisTrkNoD0CutNHits3", "SingleMu_2016" + runPeriod, dirs['Andrew'] + "2016_final_prompt/fakeTrackBackground_d0Sideband_new") zToMuMuEstimate.addChannel( "DisTrkInvertD0", "ZtoMuMuDisTrkSidebandD0Cut", "SingleMu_2016" + runPeriod, dirs['Andrew'] + "2016_final_prompt/fakeTrackBackground_d0Sideband_new") zToMuMuEstimate.addChannel( "DisTrkInvertD0NHits3", "ZtoMuMuDisTrkSidebandD0CutNHits3",
def significanceSB(cutlist, labellist): basecut = labellist[0] dim = len(cutlist) significance = [0] * (dim + 1) file = {} tree = {} effs = {} hist = {} GrAsym = {} yErrorUp = {} yErrorDown = {} totEve = 0 GrAsym = TGraphAsymmErrors() cuts = "" for j, c in enumerate(cutlist): s = 0. b = 0. cuts += cutlist[0] if j == 0 else " && " + cutlist[j] print "cuts = ", cuts for num1, v in enumerate(signals): #print "Signal = ", v for num2, filename in enumerate(samples[v]['files']): #print "Signal rootfile read = ", filename file[filename] = TFile(NTUPLESIG + filename + ".root", "READ") # Read TFile tree[filename] = file[filename].Get("Events") # Read TTree nevents = float(sample[filename]['nevents']) xs = float(sample[filename]['xsec']) * float( sample[filename]['kfactor']) LumiMC = nevents / xs Weight = float(LUMI) / float(LumiMC) sig_entries = tree[filename].GetEntries(cuts) #print "s = ", float(sig_entries) * float(Weight) s += float(sig_entries) * float(Weight) print "TOT SIG = ", s for num1, k in enumerate(back): #print "backgrounds = ", k for num2, filename in enumerate(samples[k]['files']): #print "backgrounds rootfile read = ", filename file[filename] = TFile(NTUPLEDIR + filename + ".root", "READ") # Read TFile tree[filename] = file[filename].Get("Events") # Read TTree nevents = float(sample[filename]['nevents']) xs = float(sample[filename]['xsec']) * float( sample[filename]['kfactor']) LumiMC = nevents / xs Weight = float(LUMI) / float(LumiMC) bkg_entries = tree[filename].GetEntries(cuts) #print "b = ", float(bkg_entries) * float(Weight) b += float(bkg_entries) * float(Weight) print "TOT BKG = ", b ##End of cutlist #COMPUTE #print "s = ", s #print "b = ", b #print "sqrt(b) = ", math.sqrt(b) #print "significance = ", float(s/math.sqrt(b)) significance[j] = float(s / math.sqrt(b)) yErrorUp[j] = float( TEfficiency.ClopperPearson(math.sqrt(b), s, 0.68, True) - significance[j]) yErrorDown[j] = float( significance[j] - TEfficiency.ClopperPearson(math.sqrt(b), s, 0.68, False)) GrAsym.SetPoint(j, j + 0.5, significance[j]) GrAsym.SetPointError(j, 0, 0, yErrorUp[j], yErrorDown[j]) for k, cs in enumerate(labellist): GrAsym.GetHistogram().GetXaxis().Set(dim, 0, dim) GrAsym.GetHistogram().GetXaxis().SetBinLabel(k + 1, "%s" % labellist[k]) GrAsym.SetLineColor(2) GrAsym.SetLineWidth(3) GrAsym.SetMarkerStyle(8) GrAsym.SetMarkerColor(2) c1 = TCanvas("c1", "Signals Acceptance", 800, 600) c1.cd() c1.GetPad(0).SetTopMargin(0.06) c1.GetPad(0).SetRightMargin(0.05) c1.GetPad(0).SetTicks(1, 1) gStyle.SetOptStat(0) #GrAsym.SetMaximum(1.3) #GrAsym.SetMinimum(0.) GrAsym.GetHistogram().GetXaxis().SetTitle("") GrAsym.GetHistogram().GetYaxis().SetTitle("Significance (S/#sqrt{B})") GrAsym.Draw("pa") drawCMS(LUMI, "Work In Progress") drawRegion(basecut) if not os.path.exists('plots/Signal/Significance/'): os.system('mkdir -p plots/Signal/Significance/') c1.Print("plots/Signal/Significance/Sigf_SB_" + basecut + ".png") c1.Print("plots/Signal/Significance/Sigf_SB_" + basecut + ".pdf") #if not options.runBash: raw_input("Press Enter to continue...") pass