def add_hist(total, hist): if args.combination_sideways: nx = hist.GetNbinsX() first, last = nx * isource, nx * (isource + 1) tc = rnp.hist2array(total, copy=False) tc[first:last] += rnp.hist2array(hist, copy=False) te2 = rnp.array(total.GetSumw2(), copy=False)[1:-1] te2[first:last] += rnp.array(hist.GetSumw2(), copy=False)[1:-1] else: total.Add(hist)
def getDataTGraph2D(g2D, round_xy=True, decPts=3): array_fPx = rp.array(TArrayD(g2D.GetN(), g2D.GetX())) array_fPy = rp.array(TArrayD(g2D.GetN(), g2D.GetY())) array_fPz = rp.array(TArrayD(g2D.GetN(), g2D.GetZ())) if round_xy: array_fPx = np.round(array_fPx, decimals=decPts) array_fPy = np.round(array_fPy, decimals=decPts) return np.column_stack((array_fPx, array_fPy, array_fPz))
def test_array(): for copy in (True, False): for cls in (getattr(ROOT, 'TArray{0}'.format(atype)) for atype in 'DFLIS'): a = cls(10) a[2] = 2 b = rnp.array(a, copy=copy) assert_equal(b[2], 2) assert_equal(b.shape[0], 10) a = ROOT.TArrayC(10) b = rnp.array(a, copy=copy) assert_equal(b.shape[0], 10) assert_raises(TypeError, rnp.array, object)
def on_epoch_end(self, epoch, logs=None): if input_format == 'xn': pred = self.model.predict([self.x, self.n]) else: pred = self.model.predict( [self.x[:, :, :3], self.x[:, :, 3], self.n]) pred = np.squeeze(pred) pcont = rnp.array(self.parr, copy=False) pcont[:] = np.squeeze(pred) pcont *= 100. self.canvas.Clear() self.gr_pred.DrawGraph(self.x.shape[0], self.tarr.GetArray(), self.parr.GetArray(), 'AP') self.canvas.Update()
def binWidthNorm(shape): if args.variable == 'fitshape' and isSR: mllbins, mthbins = mllVSmthBins[vname] binarea = np.array([(mllbins[x + 1] - mllbins[x]) * (mthbins[y + 1] - mthbins[y]) for y in range(len(mthbins) - 1) for x in range(len(mllbins) - 1)], dtype=np.float) cont = rnp.hist2array(shape, copy=False) err2 = rnp.array(shape.GetSumw2(), copy=False) cont /= binarea err2[1:-1] /= np.square(binarea) else: shape.Scale(1., 'width')
def set_postfit_uncertainty(hist, ws, fitresult, func, norm=None, randomized_parameters=None, fold=None): x = ws.var('CMS_th1x') ch = ws.cat('CMS_channel') err2 = rnp.array(hist.GetSumw2(), copy=False) err2 *= 0. anom = rnp.hist2array(hist, copy=False, include_overflow=True) fparams = func.getParameters(ROOT.RooArgSet(x, ch)) fcentral = fparams.snapshot() if norm is not None: nparams = norm.getParameters(ROOT.RooArgSet(x, ch)) ncentral = nparams.snapshot() # evaluate postfit uncertainties for itoy in range(n_postfit_unc_toys): if randomized_parameters is None: randomized = fitresult.randomizePars() fparams.assignValueOnly(randomized) else: fparams.assignValueOnly(randomized_parameters.get(itoy)) htemp = func.createHistogram('tmp', x, ROOT.RooFit.Extended(False)) if norm is not None: nparams.assignValueOnly(randomized) htemp.Scale(norm.getVal()) if fold is not None: htemp = fold_hist(htemp, fold) atemp = rnp.hist2array(htemp, copy=False, include_overflow=True) err2 += np.square(atemp - anom) / n_postfit_unc_toys htemp.Delete() fparams.assignValueOnly(fcentral) if norm is not None: nparams.assignValueOnly(ncentral)
def make_sideways_combination(name, temp): ncopy = len(args.sourcePaths) if temp.GetXaxis().GetXbins().GetSize() == 0: # uniform binning xmin = temp.GetXaxis().GetXmin() xmax = temp.GetXaxis().GetXmax() return ROOT.TH1D(name, '', temp.GetNbinsX() * ncopy, xmin, xmin + (xmax - xmin) * ncopy) else: # variable binning xbins = rnp.array(temp.GetXaxis().GetXbins(), copy=False) nx = temp.GetNbinsX() binning = np.zeros(nx * ncopy + 1, dtype=np.double) binning[:nx + 1] = xbins for icopy in range(1, ncopy): binning[nx * icopy + 1:nx * (icopy + 1) + 1] = (xbins[1:] - xbins[0] + binning[nx * icopy]) return ROOT.TH1D(name, '', nx * ncopy, array.array('d', binning))
def get_fiducial_histograms(source, obs, prods, add_stat_only=False, variations=None, scaling={}): if '%s/fiducial' % confdir not in sys.path: sys.path.append('%s/fiducial' % confdir) from nuisances import nuisances nominals = {} for prod in prods: phist = source.Get('fiducial/%s/histo_%s' % (obs, prod)) try: phist.Scale(scaling[prod]) except KeyError: pass nominals[prod] = phist phist.SetDirectory(0) htotal = nominals[prods[0]].Clone('total_%s' % obs) htotal.SetDirectory(0) for prod in prods[1:]: htotal.Add(nominals[prod]) if add_stat_only: statonly = htotal.Clone() if variations is not None: for prod in prods: variations[prod] = {} uncert = rnp.array(htotal.GetSumw2()) # stat uncert squared for nuis in nuisances.itervalues(): up = np.zeros_like(uncert) down = np.zeros_like(uncert) if nuis['type'] == 'shape': for prod in nuis['samples']: if prod not in prods: continue hup = source.Get('fiducial/%s/histo_%s_%sUp' % (obs, prod, nuis['name'])) dup = rnp.hist2array(hup, include_overflow=True, copy=False) hdown = source.Get('fiducial/%s/histo_%s_%sDown' % (obs, prod, nuis['name'])) ddown = rnp.hist2array(hdown, include_overflow=True, copy=False) try: dup *= scaling[prod] ddown *= scaling[prod] except KeyError: pass up += dup down += ddown if variations is not None: hup.SetDirectory(0) hdown.SetDirectory(0) variations[prod][nuis['name']] = (hup, hdown) elif nuis['type'] == 'lnN': for prod, value in nuis['samples'].iteritems(): if prod not in prods: continue nom = rnp.hist2array(nominals[prod], include_overflow=True, copy=False) if '/' in value: vdown, vup = map(float, value.split('/')) else: vup = float(value) vdown = 1. / vup up += nom * vup down += nom * vdown if variations is not None: hup = nominals[prod].Clone('histo_%s_%sUp' % (prod, nuis['name'])) hdown = nominals[prod].Clone('histo_%s_%sDown' % (prod, nuis['name'])) hup.SetDirectory(0) hdown.SetDirectory(0) hup.Scale(vup) hdown.Scale(vdown) variations[prod][nuis['name']] = (hup, hdown) up -= down up *= 0.5 uncert += np.square(up) htotal.GetSumw2().Set(len(uncert), array.array('d', uncert)) if add_stat_only: return nominals, htotal, statonly else: return nominals, htotal
def plotstack(stack, uncert, gobs, legend): del _temporaries[:] uncert.SetFillColor(ROOT.kBlack) uncert.SetFillStyle(3003) uncert.SetLineColor(ROOT.kBlack) uncert.SetLineWidth(1) distpad = canvas.cd(1) distpad.SetLogy(False) frame = uncert.Clone('frame') _temporaries.append(frame) frame.SetTitle('') frame.Reset() frame.GetXaxis().SetLabelSize(0.) frame.GetXaxis().SetTitle('') obsmax = max(gobs.GetY()[ip] + gobs.GetErrorYhigh(ip) for ip in range(gobs.GetN())) frame.SetMinimum(0.) frame.SetMaximum(max(obsmax, uncert.GetMaximum()) * 1.8) frame.Draw('HIST') stack.Draw('SAME HIST') uncert.Draw('SAME E2') gobs.Draw('PZ') legend.Draw() distpad.Update() ## Make ratio plot rmax = canvas.raxis.GetWmax() rmin = canvas.raxis.GetWmin() ratiopad = canvas.cd(2) ratiopad.SetGridy(True) runcert = uncert.Clone('runcert') _temporaries.append(runcert) runcert.SetTitle('') norm = root_numpy.hist2array(runcert, copy=False) idx_nonpositive = np.where(norm <= 0.)[0] # just set to 1 norm[idx_nonpositive] = 1. err2s = root_numpy.array(runcert.GetSumw2(), copy=False)[1:-1] err2s /= np.square(norm) err2s[idx_nonpositive] = 0. obsarray = root_numpy.array(ROOT.TArrayD(gobs.GetN(), gobs.GetY())) obsarray /= norm obserrhi = root_numpy.array(ROOT.TArrayD(gobs.GetN(), gobs.GetEYhigh())) obserrhi /= norm obserrlo = root_numpy.array(ROOT.TArrayD(gobs.GetN(), gobs.GetEYlow())) obserrlo /= norm robs = gobs.Clone('robs') robs.Set(gobs.GetN() - len(idx_nonpositive)) _temporaries.append(robs) irp = 0 for ip in range(gobs.GetN()): if ip in idx_nonpositive: continue robs.SetPoint(irp, gobs.GetX()[ip], obsarray[ip]) robs.SetPointEYhigh(irp, obserrhi[ip]) robs.SetPointEYlow(irp, obserrlo[ip]) irp += 1 root_numpy.array2hist(np.ones_like(norm), runcert) runcert.SetTickLength(0., 'X') runcert.SetTickLength(0., 'Y') runcert.GetXaxis().SetLabelSize(0.) runcert.GetXaxis().SetTitle('') runcert.GetYaxis().SetLabelSize(0.) runcert.GetYaxis().SetTitle('') runcert.SetMinimum(rmin) runcert.SetMaximum(rmax) runcert.Draw('E2') one = ROOT.TLine(runcert.GetXaxis().GetXmin(), 1., runcert.GetXaxis().GetXmax(), 1.) _temporaries.append(one) one.SetLineColor(ROOT.kBlack) one.SetLineWidth(1) one.SetLineStyle(ROOT.kSolid) one.Draw() robs.Draw('PZ') # draw out-of-bounds error bars and arrows arrow = ROOT.TArrow(0., 0., 0., 0., robs.GetMarkerSize() * 0.015, '|>') arrow.SetLineColor(ROOT.kBlack) arrow.SetLineWidth(1) errbar = ROOT.TLine(0., 0., 0., 0.) errbar.SetLineColor(ROOT.kBlack) errbar.SetLineWidth(1) for ip in range(robs.GetN()): x = robs.GetX()[ip] y = robs.GetY()[ip] errhi = robs.GetErrorYhigh(ip) errlo = robs.GetErrorYlow(ip) if y > rmax: if y - errlo > rmax: _temporaries.append(arrow.DrawArrow(x, 1., x, rmax - 0.05)) else: _temporaries.append(errbar.DrawLine(x, y - errlo, x, rmax)) elif y < rmin: if y + errhi > rmin: _temporaries.append(arrow.DrawArrow(x, 1., x, rmin + 0.05)) else: _temporaries.append(errbar.DrawLine(x, y + errhi, x, rmin)) # hatch mask bins with no base box = ROOT.TBox(0., rmin, 0., rmax) box.SetFillColor(ROOT.kRed) box.SetFillStyle(3003) box.SetLineWidth(0) for ip in idx_nonpositive: xmin = runcert.GetXaxis().GetBinLowEdge(ip + 1) xmax = runcert.GetXaxis().GetBinUpEdge(ip + 1) _temporaries.append(box.DrawBox(xmin, box.GetY1(), xmax, box.GetY2())) ratiopad.Update()
def plotstack(stacks, signals, uncerts, gobss, irow): ymax = 0. rmin = 0. rmax = 0. for icol in range(ncol): gobs = gobss[icol] uncert = uncerts[icol] signal = signals[icol] obsmax = max(gobs.GetY()[ip] + gobs.GetErrorYhigh(ip) for ip in range(gobs.GetN())) uncmax = max( uncert.GetBinContent(ix) + uncert.GetBinError(ix) for ix in range(1, uncert.GetNbinsX() + 1)) ymax = max(ymax, obsmax, uncmax) obsarray = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetY())) bkg = rnp.hist2array(uncert) bkg -= rnp.hist2array(signal, copy=False) obsarray -= bkg mm = obsarray.min() - gobs.GetErrorYlow(np.argmin(obsarray)) while rmin > mm * 1.1: rmin -= 2. mm = obsarray.max() + gobs.GetErrorYhigh(np.argmax(obsarray)) while rmax < mm * 1.1: rmax += 2. if int(rmax) % 10 == 0: # just to avoid axis label clashes rmax -= 1. canvas.yaxes[irow].SetNdivisions(405) canvas.yaxes[irow].SetTitle('events / GeV') canvas.raxes[irow].SetNdivisions(204) for icol in range(ncol): stack = stacks[icol] signal = signals[icol] uncert = uncerts[icol] gobs = gobss[icol] frame = uncert.Clone('frame') _temporaries.append(frame) frame.SetTitle('') frame.Reset() frame.SetTickLength(0.05, 'X') frame.SetTickLength(0.03, 'Y') frame.GetXaxis().SetNdivisions(canvas.xaxis.GetNdiv()) frame.GetXaxis().SetLabelSize(0.) frame.GetXaxis().SetTitle('') frame.GetYaxis().SetNdivisions(canvas.yaxes[irow].GetNdiv()) frame.GetYaxis().SetLabelSize(0.) frame.SetMinimum(0.) frame.SetMaximum(ymax * 1.4) distpad = canvas.cd((irow * ncol + icol) * 2 + 1) distpad.SetLogy(False) distpad.SetTicky(1) frame.Draw('HIST') stack.Draw('SAME HIST') uncert.Draw('SAME E2') gobs.Draw('PZ') distpad.Update() runcert = uncert.Clone('runcert') _temporaries.append(runcert) bkg = rnp.hist2array(runcert) bkg -= rnp.hist2array(signal, copy=False) obsarray = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetY())) obsarray -= bkg robs = gobs.Clone('robs') _temporaries.append(robs) for ip in range(gobs.GetN()): robs.SetPoint(ip, gobs.GetX()[ip], obsarray[ip]) rnp.array2hist(np.zeros_like(bkg), runcert) rframe = frame.Clone('rframe') _temporaries.append(rframe) rframe.GetXaxis().SetTickLength(0.15) rframe.GetYaxis().SetNdivisions(canvas.raxes[irow].GetNdiv()) rframe.SetMinimum(rmin) rframe.SetMaximum(rmax) ratiopad = canvas.cd((irow * ncol + icol) * 2 + 2) ratiopad.SetLogy(False) ratiopad.SetGridy(False) ratiopad.SetTicky(1) rframe.Draw('HIST') runcert.Draw('SAME E2') zero.DrawLine(rframe.GetXaxis().GetXmin(), 0., rframe.GetXaxis().GetXmax(), 0.) signal.Draw('HIST SAME') robs.Draw('PZ') ratiopad.Update()
def plotstack(stack, signal, uncert, gobs, ivert): uncert.SetTickLength(0., 'X') uncert.SetTickLength(0., 'Y') uncert.SetFillColor(ROOT.kBlack) uncert.SetFillStyle(3003) uncert.SetLineWidth(0) uncert.GetXaxis().SetNdivisions(120) uncert.GetYaxis().SetNdivisions(110) distpad = canvas.cd(ivert * 2 + 1) distpad.SetLogy(False) frame = uncert.Clone('frame') _temporaries.append(frame) frame.SetTitle('') frame.Reset() frame.GetXaxis().SetLabelSize(0.) frame.GetXaxis().SetTitle('') obsmax = max(gobs.GetY()[ip] + gobs.GetErrorYhigh(ip) for ip in range(gobs.GetN())) frame.SetMinimum(0.) frame.SetMaximum(max(obsmax, uncert.GetMaximum()) * 1.4) frame.Draw('HIST') stack.Draw('SAME HIST') uncert.Draw('SAME E2') gobs.Draw('PZ') distpad.Update() ratiopad = canvas.cd(ivert * 2 + 2) ratiopad.SetGridy(False) runcert = uncert.Clone('runcert') _temporaries.append(runcert) runcert.SetTitle('') bkg = rnp.hist2array(runcert) bkg -= rnp.hist2array(signal, copy=False) obsarray = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetY())) obsarray -= bkg robs = gobs.Clone('robs') _temporaries.append(robs) for ip in range(gobs.GetN()): robs.SetPoint(ip, gobs.GetX()[ip], obsarray[ip]) rnp.array2hist(np.zeros_like(bkg), runcert) runcert.SetTickLength(0., 'X') runcert.SetTickLength(0., 'Y') runcert.GetXaxis().SetLabelSize(0.) runcert.GetXaxis().SetTitle('') runcert.GetYaxis().SetLabelSize(0.) runcert.GetYaxis().SetTitle('') runcert.GetYaxis().SetNdivisions(502) runcert.Draw('E2') zero = ROOT.TLine(runcert.GetXaxis().GetXmin(), 0., runcert.GetXaxis().GetXmax(), 0.) _temporaries.append(zero) zero.SetLineColor(ROOT.kBlack) zero.SetLineWidth(1) zero.SetLineStyle(ROOT.kSolid) zero.Draw() signal.Draw('HIST SAME') robs.Draw('PZ') rmin = 0. while rmin > obsarray.min() * 1.1: rmin -= 10. rmax = 0. while rmax < obsarray.max() * 1.1: rmax += 10. runcert.SetMaximum(rmax) runcert.SetMinimum(rmin) ratiopad.Update()
def plotstack(stack, uncert, gobs, prefit=None, ivert=0): #del _temporaries[:] uncert.SetTickLength(0.05, 'X') uncert.SetTickLength(0., 'Y') uncert.SetFillColor(ROOT.kBlack) uncert.SetFillStyle(3003) uncert.SetLineColor(ROOT.kBlack) uncert.SetLineWidth(1) uncert.GetXaxis().SetNdivisions(120) uncert.GetYaxis().SetNdivisions(110) distpad = canvas.cd(ivert * 2 + 1) distpad.SetLogy(False) frame = uncert.Clone('frame') _temporaries.append(frame) frame.SetTitle('') frame.Reset() frame.GetXaxis().SetLabelSize(0.) frame.GetXaxis().SetTitle('') obsmax = max(gobs.GetY()[ip] + gobs.GetErrorYhigh(ip) for ip in range(gobs.GetN())) frame.SetMinimum(0.) frame.SetMaximum(max(obsmax, uncert.GetMaximum()) * 1.22) frame.Draw('HIST') stack.Draw('SAME HIST') uncert.Draw('SAME E2') gobs.Draw('PZ') distpad.Update() ## Make ratio plot canvas.raxes[ivert].SetTitle('obs. / pred.') canvas.raxes[ivert].SetNdivisions(108) ratiopad = canvas.cd(ivert * 2 + 2) ratiopad.SetGridy(True) runcert = uncert.Clone('runcert') _temporaries.append(runcert) runcert.SetTitle('') if prefit: norm = rnp.hist2array(prefit, copy=False) idx_nonpositive = np.where(norm <= 0.)[0] # just set to 1 norm[idx_nonpositive] = 1. obsarray = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetY())) obsarray /= norm obserrhi = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetEYhigh())) obserrhi /= norm obserrlo = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetEYlow())) obserrlo /= norm prefit_robs = gobs.Clone('prefit_robs') _temporaries.append(prefit_robs) prefit_robs.Set(gobs.GetN() - len(idx_nonpositive)) irp = 0 for ip in range(gobs.GetN()): if ip in idx_nonpositive: continue prefit_robs.SetPoint(irp, gobs.GetX()[ip], obsarray[ip]) prefit_robs.SetPointEYhigh(irp, obserrhi[ip]) prefit_robs.SetPointEYlow(irp, obserrlo[ip]) irp += 1 prefit_robs.SetMarkerStyle(4) prefit_robs.SetMarkerSize(1.) prefit_robs.SetMarkerColor(ROOT.kAzure) prefit_robs.SetLineColor(ROOT.kAzure) norm = rnp.hist2array(runcert, copy=False) idx_nonpositive = np.where(norm <= 0.)[0] # just set to 1 norm[idx_nonpositive] = 1. err2s = rnp.array(runcert.GetSumw2(), copy=False)[1:-1] err2s /= np.square(norm) err2s[idx_nonpositive] = 0. obsarray = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetY())) obsarray /= norm obserrhi = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetEYhigh())) obserrhi /= norm obserrlo = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetEYlow())) obserrlo /= norm robs = gobs.Clone('robs') robs.Set(gobs.GetN() - len(idx_nonpositive)) _temporaries.append(robs) irp = 0 for ip in range(gobs.GetN()): if ip in idx_nonpositive: continue robs.SetPoint(irp, gobs.GetX()[ip], obsarray[ip]) robs.SetPointEYhigh(irp, obserrhi[ip]) robs.SetPointEYlow(irp, obserrlo[ip]) irp += 1 rnp.array2hist(np.ones_like(norm), runcert) rmin = 0.5 rmax = 1.5 runcert.SetTickLength(0.1, 'X') runcert.SetTickLength(0., 'Y') runcert.GetXaxis().SetLabelSize(0.) runcert.GetXaxis().SetTitle('') runcert.GetYaxis().SetLabelSize(0.) runcert.GetYaxis().SetTitle('') runcert.GetYaxis().SetNdivisions(502) runcert.SetMinimum(rmin) runcert.SetMaximum(rmax) runcert.Draw('E2') one = ROOT.TLine(runcert.GetXaxis().GetXmin(), 1., runcert.GetXaxis().GetXmax(), 1.) _temporaries.append(one) one.SetLineColor(ROOT.kBlack) one.SetLineWidth(1) one.SetLineStyle(ROOT.kSolid) one.Draw() if prefit: prefit_robs.Draw('PZ') robs.Draw('PZ') # draw out-of-bounds error bars and arrows common.showOvershoots(robs, rmin, rmax) # hatch mask bins with no base box = ROOT.TBox(0., rmin, 0., rmax) box.SetFillColor(ROOT.kRed) box.SetFillStyle(3003) box.SetLineWidth(0) for ip in idx_nonpositive: xmin = runcert.GetXaxis().GetBinLowEdge(ip + 1) xmax = runcert.GetXaxis().GetBinUpEdge(ip + 1) _temporaries.append(box.DrawBox(xmin, box.GetY1(), xmax, box.GetY2())) ratiopad.Update()
if 'hww' in proc: funcname = 'shapeSig_%s_%s_morph_wrapper' % (bin, proc) else: funcname = 'shapeBkg_%s_%s_morph_wrapper' % (bin, proc) normname = 'n_exp_final_bin%s_proc_%s' % (bin, proc) hist = common.make_roofit_histogram('%s_postfit' % proc, ws, funcname, normname=normname) if hist is None: continue hist.SetBins(nbins, 0., float(nbins)) err2 = rnp.array(hist.GetSumw2(), copy=False) err2[:] = 0. postfits[proc] = hist # get the uncertainty variations !!!not accounting for lnN variations!!! for key in plotsdir.GetListOfKeys(): proc_var = key.GetName().replace('histo_', '') if proc_var == 'DATA': continue if not proc_var.endswith('Up'): continue shape = next(hist for proc, hist in prefits.iteritems() if proc_var.startswith(proc + '_'))
common.confdir) for obs in ['ptH', 'njet']: nominals, htotal = common.get_fiducial_histograms( source, obs, allprods) htotals[obs] = htotal for prods, title in productions: histograms[(obs, title)] = nominals[prods[0]].Clone('%s_%s' % (obs, title)) histograms[(obs, title)].SetDirectory(0) for prod in prods[1:]: histograms[(obs, title)].Add(nominals[prod]) # zero out the bin errors err2s = root_numpy.array(histograms[(obs, title)].GetSumw2(), copy=False) err2s *= 0. ytitles = {'ptH': 'fb / GeV', 'njet': 'fb'} rebins = {} if config == 'postfit': altprods = [ 'ggH_hwwalt', 'qqH_hwwalt', 'WH_hww', 'ZH_hww', 'ggZH_hww', 'ttH_hww' ] althtotals = {} for obs in ['ptH', 'njet']: _, htotal = common.get_fiducial_histograms(source, obs, altprods)
def __init__(self, data_path, input_type, n_vert_max, features=None, input_name='events'): super(PlotDistribution, self).__init__() if input_type == 'h5': import h5py data = h5py.File(data_path) self.x = data['x'] self.n = data['n'] y = data['y'] elif input_type == 'root': import uproot data = uproot.open(data_path)[input_name].arrays( ['x', 'n', 'y'], namedecode='ascii') self.x = data['x'] self.n = data['n'] y = data['y'] elif input_type == 'root-sparse': import uproot from generators.utils import to_dense data = uproot.open(data_path)[input_name].arrays( ['x', 'n', 'y'], namedecode='ascii') self.x = to_dense(data['n'], data['x'].content, n_vert_max=n_vert_max) self.n = data['n'] y = data['y'][:, [0]] if features is not None: self.x = self.x[:, :, features] self.tarr = ROOT.TArrayD(y.shape[0]) tcont = rnp.array(self.tarr, copy=False) tcont[:] = np.squeeze(y) self.parr = ROOT.TArrayD(y.shape[0]) self.gr_pred = ROOT.TGraph(y.shape[0]) self.gr_pred.SetMarkerSize(0.1) self.gr_pred.SetMarkerStyle(8) self.canvas = ROOT.TCanvas('plot_distribution', 'live', 600, 600) rarr = ROOT.TArrayD(y.shape[0]) rcont = rnp.array(rarr, copy=False) rcont[:] = np.squeeze(np.sum(self.x[:, :, 3], axis=1)) gr_raw = ROOT.TGraph(y.shape[0]) gr_raw.SetMarkerSize(0.1) gr_raw.SetMarkerStyle(8) self.canvas_raw = ROOT.TCanvas('plot_distribution_raw', 'raw', 600, 600) gr_raw.DrawGraph(self.x.shape[0], self.tarr.GetArray(), rarr.GetArray(), 'AP') self.canvas_raw.Update()
else: shape.SetFillStyle(0) shape.SetLineColor(color) shape.SetLineWidth(2) stack.Add(shape) shapes[procgroup] = shape signal = shapes['smH'] uncert = template.Clone('Total') _temporaries.append(uncert) uncert.SetDirectory(0) cont = rnp.hist2array(uncert, copy=False) err2 = rnp.array(uncert.GetSumw2(), copy=False)[1:-1] for ibin, bin_name in enumerate(bin_names): cont[ibin * nmll:(ibin + 1) * nmll] = rnp.hist2array(totals[bin_name], copy=False) err2[ibin * nmll:(ibin + 1) * nmll] = rnp.array( totals[bin_name].GetSumw2(), copy=False)[1:-1] uncert.Scale(1., 'width') canvas.yaxes[ivert].SetTitle('events / GeV') canvas.yaxes[ivert].SetTitleOffset(2.4) canvas.raxes[ivert].SetTitle('bkg. subt.') canvas.raxes[ivert].SetTitleOffset(2.4) obs = template.Clone('Observed') obs.SetDirectory(0)
bin = bin[:bin.find('_cat')] ibin = binnames.index(bin) hist.SetBinContent(ibin + 1, hist.GetBinContent(ibin + 1) + n) nhist = postfit_norms[(dataset, proc)] nhist.SetBinContent(normbin, nhist.GetBinContent(normbin) + n) hist.Divide(prefit_rnorms[(fit, proc)]) for ix in range(1, hist.GetNbinsX() + 1): if prefit_rnorms[(fit, proc)].GetBinContent(ix) == 0.: hist.SetBinContent(ix, -10.) # give a small uncert so that EP renders a line err = root_numpy.array(hist.GetSumw2(), copy=False) err[:] = 1.e-6 if proc == 'DY' and DYseparate: if fit == 'combination': for dataset in ['2016', '2017', '2018']: hist = postfit_rates[(fit, proc + dataset)] for ibin, bin in enumerate(binnames): rate = w.var('CMS_hww_%s%snorm_%s' % (proc, dataset, bin)) hist.SetBinContent(ibin + 1, rate.getVal()) if rate.getError() == 0.: hist.SetBinError(ibin + 1, 1.e-3) else: hist.SetBinError(ibin + 1, rate.getError())
shape.SetLineWidth(2) else: shape.SetFillStyle(0) shape.SetLineColor(color) shape.SetLineWidth(2) stack.Add(shape) shapes[procgroup] = shape signals.append(shapes['smH']) uncert = template.Clone('Total') uncerts.append(uncert) uncert.SetDirectory(0) cont = rnp.hist2array(uncert, copy=False) err2 = rnp.array(uncert.GetSumw2(), copy=False)[1:-1] cont[:] = rnp.hist2array(totals[bin_name], copy=False) err2[:] = rnp.array(totals[bin_name].GetSumw2(), copy=False)[1:-1] uncert.Scale(1., 'width') uncert.SetFillColor(ROOT.kBlack) uncert.SetFillStyle(3003) uncert.SetLineWidth(0) obs = template.Clone('Observed') obs.SetDirectory(0) cont = rnp.hist2array(obs, copy=False) cont[:] = rnp.hist2array(observed[bin_name], copy=False)
def get_fiducial_histograms(source, obs, prods): if '%s/fiducial' % confdir not in sys.path: sys.path.append('%s/fiducial' % confdir) from nuisances import nuisances nominals = {} for prod in prods: phist = source.Get('fiducial/%s/histo_%s' % (obs, prod)) nominals[prod] = phist phist.SetDirectory(0) htotal = nominals[prods[0]].Clone('total_%s' % obs) htotal.SetDirectory(0) for prod in prods[1:]: htotal.Add(nominals[prod]) uncert = root_numpy.array(htotal.GetSumw2()) # stat uncert squared for nuis in nuisances.itervalues(): up = np.zeros_like(uncert) down = np.zeros_like(uncert) if nuis['type'] == 'shape': for prod in nuis['samples']: if prod not in prods: continue up += root_numpy.hist2array(source.Get( 'fiducial/%s/histo_%s_%sUp' % (obs, prod, nuis['name'])), include_overflow=True, copy=False) down += root_numpy.hist2array(source.Get( 'fiducial/%s/histo_%s_%sDown' % (obs, prod, nuis['name'])), include_overflow=True, copy=False) elif nuis['type'] == 'lnN': for prod, value in nuis['samples'].iteritems(): if prod not in prods: continue nom = root_numpy.hist2array(nominals[prod], include_overflow=True, copy=False) if '/' in value: vdown, vup = map(float, value.split('/')) up += nom * vup down += nom * vdown else: value = float(value) up += nom * value down += nom / value up -= down up *= 0.5 uncert += np.square(up) htotal.GetSumw2().Set(len(uncert), array.array('d', uncert)) return nominals, htotal
histograms = {} if args.config in ['fiducial', 'postfit']: source = ROOT.TFile.Open('%s/fiducial/rootFile/plots_Fiducial.root' % common.confdir) nominals, htotal = common.get_fiducial_histograms(source, args.observable, allprods) for prods, title in productions: histograms[title] = nominals[prods[0]].Clone(title) histograms[title].SetDirectory(0) for prod in prods[1:]: histograms[title].Add(nominals[prod]) # zero out the bin errors err2s = rnp.array(histograms[title].GetSumw2(), copy=False) err2s *= 0. if args.observable == 'ptH': ytitle = 'd#sigma/d#font[12]{p}_{#lower[-0.18]{#kern[-0.2]{T}}}^{H} (fb/GeV)' elif args.observable == 'njet': ytitle = '#sigma(#font[12]{N}_{#kern[-0.3]{jet}}) (fb)' rebins = {} if args.config == 'postfit': altprods = ['ggH_hwwalt', 'qqH_hwwalt', 'WH_hww', 'ZH_hww', 'ggZH_hww', 'ttH_hww'] _, althtotal = common.get_fiducial_histograms(source, args.observable, altprods) source.Close()
def check_array(cls, copy): a = cls(10) a[2] = 2 b = rnp.array(a, copy=copy) assert_equal(b[2], 2) assert_equal(b.shape[0], 10)
def makePlot(self, inputFile, outputDirPlots, variables, cuts, samples, plot, nuisances, legend, groupPlot): print "==================" print "==== makePlot ====" print "==================" self.defineStyle() self._variables = variables self._samples = samples self._cuts = cuts self._outputDirPlots = outputDirPlots os.system("mkdir " + outputDirPlots + "/") # # prepare plotter.html # text_file_html = open(self._outputDirPlots + "/" + "plotter.html", "w") text_file_html.write( "<script type=\"text/javascript\" src=\"https://root.cern.ch/js/latest/scripts/JSRootCore.js?gui\"></script>\n" ) text_file_html.write("<div id=\"simpleGUI\" path=\"./\" files=\"\n") ROOT.TH1.SetDefaultSumw2(True) dataColor = 1 ROOT.gROOT.cd() list_thsData = {} list_thsSignal = {} list_thsBackground = {} list_thsSignal_grouped = {} list_thsBackground_grouped = {} list_thsSignal_grouped_normalized = {} list_thsBackground_grouped_normalized = {} list_tcanvas = {} list_tcanvasRatio = {} list_weight_X_tcanvasRatio = {} list_tcanvasDifference = {} list_weight_X_tcanvasDifference = {} list_tcanvasSigVsBkg = {} list_tcanvasSigVsBkgTHstack = {} generalCounter = 0 if os.path.isdir(inputFile): # ONLY COMPATIBLE WITH OUTPUTS MERGED TO SAMPLE LEVEL!! fileIn = {} allFiles = os.listdir(inputFile) for sampleName in self._samples: fileIn[sampleName] = ROOT.TFile.Open(inputFile + '/plots_%s_ALL_%s.root' % (self._tag, sampleName)) if not fileIn[sampleName]: raise RuntimeError('Input file for sample ' + sampleName + ' missing') if os.path.exists(inputFile + '/plots_total.root'): fileIn['total'] = ROOT.TFile.Open(inputFile + '/plots_total.root') else: fileIn = ROOT.TFile(inputFile, "READ") #---- save one TCanvas for every cut and every variable for cutName in self._cuts: if not "incl" in cutName or not "ResolvedSB" in cutName: continue print "cut =", cutName for variableName, variable in self._variables.iteritems(): if not "WhadMass" in variableName: continue if 'cuts' in variable and cutName not in variable['cuts']: continue if type(fileIn) is not dict and not fileIn.GetDirectory( cutName + "/" + variableName): continue print "variableName =", variableName if not "divideByBinWidth" in variable.keys(): variable["divideByBinWidth"] = 0 tcanvas = ROOT.TCanvas("cc" + cutName + "_" + variableName, "cc", 800, 600) tcanvasRatio = ROOT.TCanvas( "ccRatio" + cutName + "_" + variableName, "ccRatio", 800, 800) weight_X_tcanvasRatio = ROOT.TCanvas( "weight_X_tcanvasRatio" + cutName + "_" + variableName, "weight_X_tcanvasRatio", 800, 800) tcanvasDifference = ROOT.TCanvas( "ccDifference" + cutName + "_" + variableName, "ccDifference", 800, 800) weight_X_tcanvasDifference = ROOT.TCanvas( "weight_X_tcanvasDifference" + cutName + "_" + variableName, "weight_X_tcanvasDifference", 800, 800) if self._plotNormalizedDistributions: tcanvasSigVsBkg = ROOT.TCanvas( "ccSigVsBkg" + cutName + "_" + variableName, "cc", 800, 600) if self._plotNormalizedDistributionsTHstack: tcanvasSigVsBkgTHstack = ROOT.TCanvas( "ccTHstackSigVsBkg" + cutName + "_" + variableName, "cc", 800, 600) list_tcanvas[generalCounter] = tcanvas list_tcanvasRatio[generalCounter] = tcanvasRatio list_weight_X_tcanvasRatio[ generalCounter] = weight_X_tcanvasRatio list_tcanvasDifference[generalCounter] = tcanvasDifference list_weight_X_tcanvasDifference[ generalCounter] = weight_X_tcanvasDifference if self._plotNormalizedDistributions: list_tcanvasSigVsBkg[generalCounter] = tcanvasSigVsBkg if self._plotNormalizedDistributionsTHstack: list_tcanvasSigVsBkgTHstack[ generalCounter] = tcanvasSigVsBkgTHstack histos = {} histos_grouped = {} canvasNameTemplateRatio = 'ccRatio_' + cutName + "_" + variableName canvasNameTemplateDifference = 'ccDifference_' + cutName + "_" + variableName #tcanvasRatio = ROOT.TCanvas( canvasNameTemplateRatio, variableName, 800, 800 ) canvasNameTemplate = 'c_' + cutName + "_" + variableName #tcanvas = ROOT.TCanvas( canvasNameTemplate, variableName , 800, 600 ) tcanvas.cd() #print " and now this ..." tgrData_vx = array('f') tgrData_evx = array('f') tgrData_vy = array('f') tgrData_evy_up = array('f') tgrData_evy_do = array('f') # at least 1 "MC" should be around ... otherwise what are we plotting? Only data? tgrMC_vx = array('f') tgrMC_evx = array('f') #these vectors are needed for nuisances accounting nuisances_vy_up = {} nuisances_vy_do = {} ROOT.gROOT.cd() thsData = ROOT.THStack( "thsData_" + cutName + "_" + variableName, "thsData_" + cutName + "_" + variableName) #print 'really before thstack ... one' thsSignal = ROOT.THStack( "thsSignal_" + cutName + "_" + variableName, "thsSignal_" + cutName + "_" + variableName) #print 'really before thstack ... two' thsBackground = ROOT.THStack( "thsBackground_" + cutName + "_" + variableName, "thsBackground_" + cutName + "_" + variableName) thsBackgroundNoWjets = ROOT.THStack( "thsBackgroundNoWjets_" + cutName + "_" + variableName, "thsBackground_" + cutName + "_" + variableName) thsBackgroundOnlyWjets = ROOT.THStack( "thsBackgroundOnlyWjets_" + cutName + "_" + variableName, "thsBackground_" + cutName + "_" + variableName) #print 'really before thstack ... three' thsSignal_grouped = ROOT.THStack( "thsSignal_grouped_" + cutName + "_" + variableName, "thsSignal_grouped_" + cutName + "_" + variableName) #print 'really before thstack ... four' thsBackground_grouped = ROOT.THStack( "thsBackground_grouped_" + cutName + "_" + variableName, "thsBackground_grouped_" + cutName + "_" + variableName) list_thsData[generalCounter] = thsData list_thsSignal[generalCounter] = thsSignal list_thsBackground[generalCounter] = thsBackground list_thsSignal_grouped[generalCounter] = thsSignal_grouped list_thsBackground_grouped[ generalCounter] = thsBackground_grouped generalCounter += 1 #print '... after thstack ...' sigSupList = [] sigSupList_grouped = [] # list of additional histograms to be used in the ratio plot sigForAdditionalRatioList = {} sigForAdditionalDifferenceList = {} # enhanced list of nuisances, including bin-by-bin mynuisances = {} nexpected = 0 for sampleName, plotdef in plot.iteritems(): if 'samples' in variable and sampleName not in variable[ 'samples']: continue shapeName = cutName + "/" + variableName + '/histo_' + sampleName print ' -> shapeName = ', shapeName if type(fileIn) is dict: histo = fileIn[sampleName].Get(shapeName) else: histo = fileIn.Get(shapeName) print ' --> ', histo print 'new_histo_' + sampleName + '_' + cutName + '_' + variableName histos[sampleName] = histo.Clone('new_histo_' + sampleName + '_' + cutName + '_' + variableName) # allow arbitrary scaling in MC (and DATA??), if needed # for example to "see" a signal if 'scale' in plotdef.keys(): histos[sampleName].Scale(plotdef['scale']) #print " >> scale ", sampleName, " to ", plotdef['scale'] # apply cut dependent scale factors # for example when plotting different phase spaces if 'cuts' in plotdef.keys() and cutName in plotdef['cuts']: histos[sampleName].Scale( float(plotdef['cuts'][cutName])) # data style if plotdef['isData'] == 1: if variable['divideByBinWidth'] == 1: histos[sampleName].Scale(1, "width") #print ' plot[', sampleName, '][color] = ' , plotdef['color'] histos[sampleName].SetMarkerColor(plotdef['color']) histos[sampleName].SetMarkerSize(1) histos[sampleName].SetMarkerStyle(20) histos[sampleName].SetLineColor( self._getColor(plotdef['color'])) # blind data if 'isBlind' in plotdef.keys( ) and plotdef['isBlind'] == 1: histos[sampleName].Reset() # Per variable blinding if 'blind' in variable: if cutName in variable['blind']: blind_range = variable['blind'][cutName] if blind_range == "full": for iBin in range( 1, histos[sampleName].GetNbinsX() + 1): histos[sampleName].SetBinContent( iBin, 0) histos[sampleName].SetBinError(iBin, 0) histos[sampleName].Reset() elif type(blind_range) in [ list, tuple ] and len(blind_range) == 2: b0 = histos[sampleName].FindBin( blind_range[0]) b1 = histos[sampleName].FindBin( blind_range[1]) for iBin in range( 1, histos[sampleName].GetNbinsX() + 1): if iBin >= b0 and iBin <= b1: histos[sampleName].SetBinContent( iBin, 0) histos[sampleName].SetBinError( iBin, 0) thsData.Add(histos[sampleName]) # first time fill vectors X axis if len(tgrData_vx) == 0: dataColor = histos[sampleName].GetMarkerColor() for iBin in range( 1, histos[sampleName].GetNbinsX() + 1): tgrData_vx.append( histos[sampleName].GetBinCenter(iBin)) tgrData_evx.append( histos[sampleName].GetBinWidth(iBin) / 2.) tgrData_vy.append( histos[sampleName].GetBinContent(iBin)) #print " plot[", sampleName, "].keys() = ", plotdef.keys() if ('isSignal' not in plotdef.keys() or plotdef['isSignal'] != 3 ) and not ('isBlind' in plotdef.keys() and plotdef['isBlind'] == 1): if variable['divideByBinWidth'] == 1: tgrData_evy_up.append( self.GetPoissError( histos[sampleName]. GetBinContent(iBin) * histos[sampleName].GetBinWidth( iBin), 0, 1) / histos[sampleName].GetBinWidth( iBin)) tgrData_evy_do.append( self.GetPoissError( histos[sampleName]. GetBinContent(iBin) * histos[sampleName].GetBinWidth( iBin), 1, 0) / histos[sampleName].GetBinWidth( iBin)) else: tgrData_evy_up.append( self.GetPoissError( histos[sampleName]. GetBinContent(iBin), 0, 1)) tgrData_evy_do.append( self.GetPoissError( histos[sampleName]. GetBinContent(iBin), 1, 0)) else: tgrData_evy_up.append(0) tgrData_evy_do.append(0) else: for iBin in range( 1, histos[sampleName].GetNbinsX() + 1): tgrData_vx[iBin - 1] = ( histos[sampleName].GetBinCenter(iBin)) tgrData_evx.append( histos[sampleName].GetBinWidth(iBin) / 2.) tgrData_vy[iBin - 1] += histos[ sampleName].GetBinContent(iBin) if 'isSignal' not in plotdef.keys( ) or plotdef['isSignal'] == 3: if variable['divideByBinWidth'] == 1: tgrData_evy_up[iBin - 1] = SumQ( tgrData_evy_up[iBin - 1], self.GetPoissError( histos[sampleName]. GetBinContent(iBin) * histos[sampleName].GetBinWidth( iBin), 0, 1) / histos[sampleName].GetBinWidth( iBin)) tgrData_evy_do[iBin - 1] = SumQ( tgrData_evy_do[iBin - 1], self.GetPoissError( histos[sampleName]. GetBinContent(iBin) * histos[sampleName].GetBinWidth( iBin), 1, 0) / histos[sampleName].GetBinWidth( iBin)) else: tgrData_evy_up[iBin - 1] = SumQ( tgrData_evy_up[iBin - 1], self.GetPoissError( histos[sampleName]. GetBinContent(iBin), 0, 1)) tgrData_evy_do[iBin - 1] = SumQ( tgrData_evy_do[iBin - 1], self.GetPoissError( histos[sampleName]. GetBinContent(iBin), 1, 0)) # if plotdef['isData'] == 1: else: # MC style # only background "filled" histogram if plotdef['isSignal'] == 0: histos[sampleName].SetFillColor( self._getColor(plotdef['color'])) if 'fill' in plotdef: histos[sampleName].SetFillStype( plotdef['fill']) else: histos[sampleName].SetFillStyle(3001) else: histos[sampleName].SetFillStyle(0) histos[sampleName].SetLineWidth(2) histos[sampleName].SetLineColor( self._getColor(plotdef['color'])) if plotdef['isSignal'] == 1: if variable['divideByBinWidth'] == 1: histos[sampleName].Scale(1, "width") thsSignal.Add(histos[sampleName]) elif plotdef['isSignal'] == 2 or plotdef[ 'isSignal'] == 3: #print "SigSup histo: ", histos[sampleName] if variable['divideByBinWidth'] == 1: histos[sampleName].Scale(1, "width") sigSupList.append(histos[sampleName]) if plotdef['isSignal'] == 3: #print "sigForAdditionalRatio histo: ", histos[sampleName] sigForAdditionalRatioList[sampleName] = histos[ sampleName] sigForAdditionalDifferenceList[ sampleName] = histos[sampleName] else: nexpected += histos[sampleName].Integral( 1, histos[sampleName].GetNbinsX() ) # it was (-1, -1) in the past, correct now if variable['divideByBinWidth'] == 1: histos[sampleName].Scale(1, "width") thsBackground.Add(histos[sampleName]) if sampleName != "Wjets": thsBackgroundNoWjets.Add(histos[sampleName]) else: thsBackgroundOnlyWjets.Add(histos[sampleName]) #print " adding to background: ", sampleName # handle 'stat' nuisance to create the bin-by-bin list of nuisances # "massage" the list of nuisances accordingly for nuisanceName, nuisance in nuisances.iteritems(): if 'cuts' in nuisance and cutName not in nuisance[ 'cuts']: continue # run only if this nuisance will affect the phase space defined in "cut" #print " nuisanceName = ", nuisanceName if nuisanceName == 'stat': # 'stat' has a separate treatment, it's the MC/data statistics #print " nuisance = ", nuisance if 'samples' in nuisance.keys(): if sampleName in nuisance['samples'].keys( ): #print " stat nuisances for ", sampleName if nuisance['samples'][sampleName][ 'typeStat'] == 'uni': # unified approach print 'In principle nothing to be done here ... just wait' if nuisance['samples'][sampleName][ 'typeStat'] == 'bbb': # bin-by-bin # add N ad hoc nuisances, one for each bin for iBin in range( 1, histos[sampleName]. GetNbinsX() + 1): if ( 'ibin_' + str(iBin) + '_stat' ) not in mynuisances.keys( ): # if new, add the new nuisance # Name of the histogram: histo_" + sampleName + "_ibin_" + str(iBin) + "_statUp" # Then the nuisance is "ibin_" + str(iBin) + "_stat" mynuisances[ 'ibin_' + str(iBin) + '_stat'] = { 'samples': { sampleName: '1.00', }, } else: # otherwise just add the new sample in the list of samples to be considered mynuisances[ 'ibin_' + str(iBin) + '_stat']['samples'][ sampleName] = '1.00' else: if nuisanceName not in mynuisances.keys(): if 'type' in nuisance.keys() and ( nuisance['type'] == 'rateParam' or nuisance['type'] == 'lnU'): pass #print "skip this nuisance since 100 percent uncertainty :: ", nuisanceName else: mynuisances[nuisanceName] = nuisances[ nuisanceName] nuisanceHistos = ({}, {}) for nuisanceName, nuisance in mynuisances.iteritems(): # is this nuisance to be considered for this background? if 'samples' in nuisance: if sampleName not in nuisance['samples']: continue elif 'all' not in nuisance or nuisance['all'] != 1: continue if 'cuts' in nuisance and cutName not in nuisance[ 'cuts']: continue if 'name' in nuisance: shapeNameVars = tuple( cutName + "/" + variableName + '/histo_' + sampleName + "_" + nuisance['name'] + var for var in ['Up', 'Down']) else: shapeNameVars = tuple( cutName + "/" + variableName + '/histo_' + sampleName + "_" + nuisanceName + var for var in ['Up', 'Down']) if 'type' in nuisance and nuisance['type'] == 'lnN': if 'samples' in nuisance: values = nuisance['samples'][sampleName] # example: # 'samples' : { # 'WW' : '1.00', # 'ggH': '1.23/0.97' # }, else: # 'all' values = nuisance['value'] if '/' in values: variations = map(float, values.split('/')) else: variations = (float(values), 2. - float(values)) # don't use histos[sampleName], or the second "scale" will fail!!! for ivar, shapeNameVar in enumerate( shapeNameVars): histoVar = histo.Clone( shapeNameVar.replace('/', '__')) histoVar.Scale(variations[ivar]) nuisanceHistos[ivar][ nuisanceName] = histoVar else: for ivar, shapeNameVar in enumerate( shapeNameVars): if type(fileIn) is dict: histoVar = fileIn[sampleName].Get( shapeNameVar) else: histoVar = fileIn.Get(shapeNameVar) nuisanceHistos[ivar][ nuisanceName] = histoVar for ivar, nuisances_vy in enumerate( [nuisances_vy_up, nuisances_vy_do]): for nuisanceName, nuisance in mynuisances.iteritems( ): try: histoVar = nuisanceHistos[ivar][ nuisanceName] except KeyError: # now, even if not considered this nuisance, I need to add it, # so that in case is "empty" it will add the nominal value # for this sample that is not affected by the nuisance histoVar = histos[sampleName] else: if 'scale' in plotdef: histoVar.Scale(plotdef['scale']) # apply cut dependent scale factors # for example when plotting different phase spaces if 'cuts' in plotdef and cutName in plotdef[ 'cuts']: histoVar.Scale( float(plotdef['cuts'][cutName])) if variable["divideByBinWidth"] == 1: histoVar.Scale(1., "width") try: vy = nuisances_vy[nuisanceName] except KeyError: vy = nuisances_vy[ nuisanceName] = np.zeros_like( rnp.hist2array(histo, copy=False)) # get the background sum if plotdef[ 'isSignal'] == 0: # ---> add the signal too????? See ~ 20 lines below vy += rnp.hist2array(histoVar, copy=False) # create the group of histograms to plot # this has to be done after the scaling of the previous lines # andl also after all the rest, so that we inherit the style of the histograms for sampleNameGroup, sampleConfiguration in groupPlot.iteritems( ): if sampleName in sampleConfiguration['samples']: if sampleNameGroup in histos_grouped.keys(): histos_grouped[sampleNameGroup].Add( histos[sampleName]) else: histos_grouped[sampleNameGroup] = histos[ sampleName].Clone('new_histo_group_' + sampleNameGroup + '_' + cutName + '_' + variableName) # end sample loop # set the colors for the groups of samples for sampleNameGroup, sampleConfiguration in groupPlot.iteritems( ): if sampleNameGroup in histos_grouped.keys(): histos_grouped[sampleNameGroup].SetLineColor( self._getColor(sampleConfiguration['color'])) if sampleConfiguration['isSignal'] == 0: histos_grouped[sampleNameGroup].SetFillColor( self._getColor(sampleConfiguration['color'])) if 'fill' in sampleConfiguration: histos_grouped[sampleNameGroup].SetFillStyle( sampleConfiguration['fill']) else: histos_grouped[sampleNameGroup].SetFillStyle( 3001) else: histos_grouped[sampleNameGroup].SetFillStyle(0) histos_grouped[sampleNameGroup].SetLineWidth(2) # fill the reference distribution with the background only distribution # save the central values of the bkg sum for use for the nuisance band # # How could this be ==0 ? # At least one MC sample should be defined ... # but still, let's leave the possibility # if thsBackground.GetNhists() != 0: last = thsBackground.GetStack().Last() tgrMC_vy = rnp.hist2array(last, copy=True) for iBin in range( 1, thsBackground.GetStack().Last().GetNbinsX() + 1): tgrMC_vx.append( thsBackground.GetStack().Last().GetBinCenter(iBin)) tgrMC_evx.append( thsBackground.GetStack().Last().GetBinWidth(iBin) / 2.) nuisances_err2_up = rnp.array(last.GetSumw2())[1:-1] nuisances_err2_do = rnp.array(last.GetSumw2())[1:-1] else: tgrMC_vy = np.zeros((0, )) nuisances_err2_up = np.zeros((0, )) nuisances_err2_do = np.zeros((0, )) # # and now let's add the signal on top of the background stack # It is important to do this after setting (without signal) tgrMC_vy # for sampleName, plotdef in plot.iteritems(): if 'samples' in variable and sampleName not in variable[ 'samples']: continue # MC style if plotdef['isData'] == 0: if plotdef['isSignal'] == 1: thsBackground.Add(histos[sampleName]) # # you need to add the signal as well, since the signal was considered in the nuisances vector # otherwise you would introduce an uncertainty as big as the signal itself!!! # for nuisanceName in mynuisances.keys(): # now we need to tell wthether the variation is actually up or down ans sum in quadrature those with the same sign up = nuisances_vy_up[nuisanceName] do = nuisances_vy_do[nuisanceName] up_is_up = (up > tgrMC_vy) dup2 = np.square(up - tgrMC_vy) ddo2 = np.square(do - tgrMC_vy) nuisances_err2_up += np.where(up_is_up, dup2, ddo2) nuisances_err2_do += np.where(up_is_up, ddo2, dup2) nuisances_err_up = np.sqrt(nuisances_err2_up) nuisances_err_do = np.sqrt(nuisances_err2_do) tgrData = ROOT.TGraphAsymmErrors( thsBackground.GetStack().Last().GetNbinsX()) for iBin in range(0, len(tgrData_vx)): tgrData.SetPoint(iBin, tgrData_vx[iBin], tgrData_vy[iBin]) tgrData.SetPointError(iBin, tgrData_evx[iBin], tgrData_evx[iBin], tgrData_evy_do[iBin], tgrData_evy_up[iBin]) tgrData.SetMarkerColor(dataColor) tgrData.SetLineColor(dataColor) ## Default: --postFit 0 --> No additional line is drawn ## ---------------------------------------------------- ## --postFit 1 --> line is prefit if self._postFit == 'p': tgrDataOverPF = tgrData.Clone( "tgrDataOverPF") # use this for ratio with Post-Fit MC histoPF = fileIn.Get(cutName + "/" + variableName + '/histo_total_prefit') ## --postFit 2 --> line is (S+B) postfit if self._postFit == 's': tgrDataOverPF = tgrData.Clone( "tgrDataOverPF") # use this for ratio with Post-Fit MC histoPF = fileIn.Get(cutName + "/" + variableName + '/histo_total_postfit_s') ## --postFit 3 --> line is B-only postfit if self._postFit == 'b': tgrDataOverPF = tgrData.Clone( "tgrDataOverPF") # use this for ratio with Post-Fit MC histoPF = fileIn.Get(cutName + "/" + variableName + '/histo_total_postfit_b') last = thsBackground.GetStack().Last() lastNoWjets = thsBackgroundNoWjets.GetStack().Last() lastOnlyWjets = thsBackgroundOnlyWjets.GetStack().Last() tgrDataOverMC = tgrData.Clone("tgrDataOverMC") tgrRemainingOverWjets = ROOT.TGraphAsymmErrors(tgrData) tgrRemainingOverWjets.SetName("tgrRemainingOverWjets") removed = 0 for iBin in range(0, len(tgrData_vx)): tgrDataOverMC.SetPoint( iBin - removed, tgrData_vx[iBin], self.Ratio(tgrData_vy[iBin], last.GetBinContent(iBin + 1))) tgrDataOverMC.SetPointError( iBin - removed, tgrData_evx[iBin], tgrData_evx[iBin], self.Ratio(tgrData_evy_do[iBin], last.GetBinContent(iBin + 1)), self.Ratio(tgrData_evy_up[iBin], last.GetBinContent(iBin + 1))) dataMinusNonWjets = self.Difference( tgrData_vy[iBin], lastNoWjets.GetBinContent(iBin + 1)) value = self.Ratio(dataMinusNonWjets, lastOnlyWjets.GetBinContent(iBin + 1)) print(value) tgrRemainingOverWjets.SetPoint(iBin - removed, tgrData_vx[iBin], value) tgrRemainingOverWjets.SetPointError( iBin - removed, tgrData_evx[iBin], tgrData_evx[iBin], self.Ratio(tgrData_evy_do[iBin], lastOnlyWjets.GetBinContent(iBin + 1)), self.Ratio(tgrData_evy_up[iBin], lastOnlyWjets.GetBinContent(iBin + 1))) if value < 0.1: print("removed?") tgrRemainingOverWjets.RemovePoint(iBin - removed) removed += 1 ## do linear fit tcanvas = ROOT.TCanvas("mycanvas", "mycanvas", 800, 600) newFile = ROOT.TFile("newfile.root", "RECREATE") tgrDataOverMC.Write() linearFunc = ROOT.TF1("pol1", "pol1", 40, 250) linearFunc.SetParameter(0, 1.3) linearFunc.SetParameter(1, -0.002) tgrRemainingOverWjets.Fit("pol1", "R") tgrRemainingOverWjets.Draw("AP") self._saveCanvas(tcanvas, "myfit") tcanvas.Draw() tcanvas.Show() tgrRemainingOverWjets.Write() newFile.Close() raw_input("Press enter to continue") print " >> end:", variableName print " >> all end" print " >> all but really all " # # close plotter.html # text_file_html.write(" \"></div> \"\n") text_file_html.close() #sys.exit(0) #quit() #raise SystemExit() os._exit(0)