Esempio n. 1
0
def _do_raw_1d(data, prelim=False):

    colors = [ROOT.kBlack, ROOT.kRed, ROOT.kBlue]

    for npart, layer in itertools.product(range(1,4), [n for n, _ in LCONDS]):
        ldata = data[layer]['Output_number'][
            np.where(data[layer]['Output_number_true'] == npart)
        ]
        ldata /= np.sum(ldata, axis=1).reshape((ldata.shape[0], 1))

        canvas = ROOT.TCanvas(
            'c_raw_{}_{}'.format(npart, layer),
            '',
            0,
            0,
            800,
            600
        )
        canvas.SetLogy()

        leg = ROOT.TLegend(0.7, 0.7, 0.9, 0.85)
        leg.SetBorderSize(0)

        for i_out in range(3):
            hist = ROOT.TH1D(
                'h_raw_{}_{}_{}'.format(npart, layer, i_out),
                '',
                30,
                0,
                1
            )
            ROOT.SetOwnership(hist, False)
            root_numpy.fill_hist(hist, ldata[:, i_out])
            hist.Scale(1.0 / hist.Integral())
            hist.SetLineColor(colors[i_out])
            hist.SetMaximum(10)
            hist.Draw(('' if i_out == 0 else 'same') + 'HIST')

            leg.AddEntry(hist, '{}-particle bin'.format(i_out + 1), 'L')

        leg.Draw()
        figures.draw_atlas_label(preliminary=prelim)
        txt = ROOT.TLatex()
        txt.SetNDC()
        txt.SetTextSize(txt.GetTextSize() * 0.75)
        txt.DrawLatex(0.2, 0.82, 'PYTHIA8 dijet, 1.8 < p_{T}^{jet} < 2.5 TeV')
        txt.DrawText(
            0.2,
            0.77,
            '{}-particle clusters'.format(npart)
        )
        txt.DrawText(0.2, 0.72, layer)

        canvas.SaveAs('number_raw_{}_{}.pdf'.format(npart, layer))
Esempio n. 2
0
def _plot_2d(hsdict, variable, nparticle, layer, preliminary):

    name = '_'.join([variable, str(nparticle), '2D', layer])
    key = name
    if key not in hsdict:
        return
    th2 = hsdict[name]

    _set_palette()
    ROOT.gStyle.SetNumberContours(255)

    canvas = ROOT.TCanvas('canvas_' + name, '', 0, 0, 800, 600)

    th2.Rebin2D(2, 2)

    th2.GetZaxis().SetTitle("Particles / {x} x {y} {u}".format(
        x=th2.GetXaxis().GetBinWidth(1),
        y=th2.GetYaxis().GetBinWidth(1),
        u='mm^{2}' if 'residual' in variable else ''))
    th2.GetZaxis().SetLabelSize(0.04)
    ROOT.TGaxis.SetMaxDigits(4)

    if 'residual' in variable:
        th2.GetXaxis().SetRangeUser(-0.05, 0.05)
        th2.GetYaxis().SetRangeUser(-0.5, 0.5)
    else:
        th2.GetXaxis().SetRangeUser(-5, 5)
        th2.GetYaxis().SetRangeUser(-5, 5)

    th2.GetXaxis().SetLabelSize(0.04)
    th2.GetYaxis().SetLabelSize(0.04)

    th2.SetTitle(';Truth hit local x {v} {u};Truth hit local y {v} {u}'.format(
        v=variable.replace('corr_', '').rstrip('s'),
        u='[mm]' if 'residual' in variable else ''))

    th2.Draw('COLZ')

    figures.draw_atlas_label(preliminary)

    txt = ROOT.TLatex()
    txt.SetNDC()
    txt.SetTextSize(txt.GetTextSize() * 0.75)
    txt.DrawLatex(0.2, 0.82, 'PYTHIA8 dijet, 1.8 < p_{T}^{jet} < 2.5 TeV')
    txt.DrawText(
        0.2, 0.77,
        '{}-particle{} clusters'.format(nparticle,
                                        '' if nparticle == 1 else 's'))

    txt.DrawText(
        0.2, 0.72,
        layer.upper() if layer == 'ibl' else layer[0].upper() + layer[1:])

    canvas.SaveAs(name + '.pdf')
Esempio n. 3
0
def _roc_graph(data, classes, prelim=False):
    # pylint: disable=too-many-locals
    pos, neg = classes

    canvas = ROOT.TCanvas('c_{}vs{}.format(pos, neg)', '', 0, 0, 800, 600)
    canvas.SetLogx()

    graphs = ROOT.TMultiGraph()
    leg = ROOT.TLegend(0.63, 0.7, 0.9, 0.88)

    for layer in data:
        if pos == 3:
            pos_sel = data[layer]['Output_number_true'] >= pos
        else:
            pos_sel = data[layer]['Output_number_true'] == pos

        if neg == 3:
            neg_sel = data[layer]['Output_number_true'] >= neg
        else:
            neg_sel = data[layer]['Output_number_true'] == neg

        isel = np.where(
            np.logical_or(
                pos_sel,
                neg_sel,
            )
        )[0]

        fpr, tpr, _ = sklearn.metrics.roc_curve(
            data[layer]['Output_number_true'][isel],
            data[layer]['Output_number'][isel][:, pos - 1],
            pos_label=pos
        )
        auc = sklearn.metrics.auc(fpr, tpr)

        graph = ROOT.TGraph(fpr.size)
        _set_graph_style(graph, layer)
        root_numpy.fill_graph(graph, np.column_stack((fpr, tpr)))
        graphs.Add(graph)
        leg.AddEntry(graph, '{}, AUC: {:.2f}'.format(layer, auc), 'L')

    graphs.SetTitle(
        ';Pr(Estimated: {pos}-particle{ps} | True: {neg}-particle{ns});Pr(Estimated: {pos}-particle{ps} | True: {pos}-particle{ps})'.format(  # noqa
            pos=pos,
            neg=neg,
            ps=('' if pos == 1 else 's'),
            ns=('' if neg == 1 else 's')
        )
    )
    graphs.SetMaximum(1.5)
    graphs.Draw('AL')

    line = ROOT.TF1("line", "x", 0, 1)
    line.SetLineStyle(3)
    line.SetLineColor(ROOT.kGray + 2)
    line.Draw("same")
    leg.AddEntry(line, 'Random, AUC: 0.50', 'L')

    leg.SetTextSize(leg.GetTextSize() * 0.65)
    leg.SetBorderSize(0)
    leg.Draw()

    figures.draw_atlas_label(prelim)
    txt = ROOT.TLatex()
    txt.SetNDC()
    txt.SetTextSize(txt.GetTextSize() * 0.75)
    txt.DrawLatex(0.2, 0.82, 'PYTHIA8 dijet, 1.8 < p_{T}^{jet} < 2.5 TeV')

    canvas.SaveAs('ROC_{}vs{}.pdf'.format(pos, neg))
Esempio n. 4
0
def _do_raw_2d(data, x=2, y=3, prelim=False):

    x_min, x_max = 0, 1
    y_min, y_max = 0, 1.4
    z_min, z_max = 1e-6, 1
    x_n_bins = 50
    y_n_bins = int(x_n_bins * (y_max - y_min) / (x_max - x_min))

    oldmargin = ROOT.gStyle.GetPadRightMargin()
    ROOT.gStyle.SetPadRightMargin(0.15)
    _set_palette()
    ROOT.gStyle.SetNumberContours(255)

    for npart in range(1, 4):

        canvas = ROOT.TCanvas('c', '', 0, 0, 800, 600)
        canvas.SetLogz()

        hist = ROOT.TH2D(
            'h_raw_2D_{}'.format(npart),
            '',
            x_n_bins,
            x_min,
            x_max,
            y_n_bins,
            y_min,
            y_max
        )
        hist.GetYaxis().SetRangeUser(0, y_max)
        hist.GetZaxis().SetLabelSize(0.04)
        hist.GetZaxis().SetTitle(
            "Cluster density / {x} x {y}".format(
                x=hist.GetXaxis().GetBinWidth(1),
                y=hist.GetYaxis().GetBinWidth(1),
            )
        )
        hist.SetTitle(
            ';{x}-particle score;{y}-particle score'.format(
                x=x if x !=3 else '#geq {}'.format(x),
                y=y if y !=3 else '#geq {}'.format(y),
            )
        )
        ROOT.TGaxis.SetMaxDigits(4)

        for layer in [n for n, _ in LCONDS]:
            layer_data = data[layer]['Output_number'][
                np.where(data[layer]['Output_number_true'] == npart)
            ]
            root_numpy.fill_hist(hist, layer_data[:,[x-1,y-1]])

        hist.Scale(1.0 / hist.Integral())
        hist.SetMinimum(z_min)
        hist.SetMaximum(z_max)
        hist.Draw('COLZ')

        # Plot the classification region
        if x == 2 and y == 3:
            gr1 = ROOT.TGraph(4, array.array('d', [0, 0, 0.6, 0.6]), array.array('d', [0, 0.2, 0.2, 0]))
            gr1.SetFillColorAlpha(ROOT.kBlack, 0.2)
            gr1.SetLineWidth(0)
            gr1.Draw('F')

            # Add legend
            leg = ROOT.TLegend(0.5, 0.7, 0.8, 0.8)
            leg.SetTextSize(0.02)
            leg.SetBorderSize(0)
            leg.AddEntry(gr1, "1-particle classification region", "F")
            leg.Draw()

        # Add text
        figures.draw_atlas_label(preliminary=prelim)
        txt = ROOT.TLatex()
        txt.SetNDC()
        txt.SetTextSize(txt.GetTextSize() * 0.75)
        txt.DrawLatex(0.2, 0.82, 'PYTHIA8 dijet, 1.8 < p_{T}^{jet} < 2.5 TeV')
        txt.DrawText(
            0.2,
            0.77,
            '{}-particle clusters'.format(npart)
        )

        canvas.SaveAs('number_raw_{}_{}_2D_{}.pdf'.format(x, y, npart))

    ROOT.gStyle.SetPadRightMargin(oldmargin)
Esempio n. 5
0
def _tpr_fnr(data, pos, cond, preliminary=False):

    ROOT.gStyle.SetErrorX(0.5)

    colors = {1: ROOT.kBlack, 2: ROOT.kRed, 3: ROOT.kBlue}
    markers = {1: 8, 2: 21,  3: 23}

    oldmargin = ROOT.gStyle.GetPadRightMargin()
    ROOT.gStyle.SetPadRightMargin(0.15)

    if pos == 1:
        fnrs = [2, 3]
    elif pos == 2:
        fnrs = [1, 3]
    else:
        fnrs = [1, 2]

    # define bins in eta
    bins = _get_bins(cond)

    bin_data_1 = np.zeros(len(bins) - 1)
    bin_data_2or3 = np.zeros(len(bins) - 1)
    e_bin_data_1 = np.zeros(len(bins) - 1)
    e_bin_data_2or3 = np.zeros(len(bins) - 1)

    for i in range(1, len(bins)):

        data_1 = np.zeros(3)
        data_2or3 = np.zeros(3)
        e_data_1 = np.zeros(3)
        e_data_2or3 = np.zeros(3)
        stats = np.zeros(3)
        for j, layer in enumerate(data.keys()):
            # get the indices corresponding to eta bins
            i_bins = np.digitize(data[layer][cond], bins)
            bin_data = data[layer][np.where(i_bins == i)]
            data_1[j], e_data_1[j] = _calc_rate(bin_data, true=pos, est=[1])
            data_2or3[j], e_data_2or3[j] = _calc_rate(bin_data, true=pos, est=[2,3])
            stats[j] = bin_data.shape[0]

        if np.all(stats == 0):
            weights = 0
        else:
            weights = stats / float(np.sum(stats))
        bin_data_1[i-1] = np.sum(weights * data_1)
        bin_data_2or3[i-1] = np.sum(weights * data_2or3)
        e_bin_data_1[i-1] = np.sqrt(np.sum(weights*weights*e_data_1*e_data_1))
        e_bin_data_2or3[i-1] = np.sqrt(np.sum(weights*weights*e_data_2or3*e_data_2or3))

    hist_1 = ROOT.TH1F(
        'h',
        '',
        len(bins) - 1,
        array.array('f', bins)
    )
    hist_2or3 = ROOT.TH1F(
        'ha',
        '',
        len(bins) - 1,
        array.array('f', bins)
    )

    root_numpy.fill_hist(hist_1, bins[:-1], weights=bin_data_1)
    root_numpy.fill_hist(hist_2or3, bins[:-1], weights=bin_data_2or3)
    for i in range(1, bins.shape[0]):
        hist_1.SetBinError(i, e_bin_data_1[i-1])
        hist_2or3.SetBinError(i, e_bin_data_2or3[i-1])

    cnv = ROOT.TCanvas('c', '', 0, 0, 800, 600)
    # cnv.SetLogy()

    hist_1.SetTitle(';{};Pr(Estimated | true: {} particle)'.format(
        _get_xlabel(cond),
        pos,
    ))

    xmax_1 = 1.6

    hist_1.SetMaximum(xmax_1)
    hist_1.SetMinimum(0.0)
    hist_1.SetLineColor(colors[1])
    hist_1.SetMarkerColor(colors[1])
    hist_1.SetMarkerStyle(markers[1])
    hist_1.Draw('p e')
    cnv.Update()

    hist_2or3.SetLineColor(colors[2])
    hist_2or3.SetMarkerColor(colors[2])
    hist_2or3.SetMarkerStyle(markers[2])
    hist_2or3.Draw('p e same')

    if cond == 'cluster_size':
        hist_1.GetXaxis().SetNdivisions(4, False)
        for k,v in {1: 1, 2: 5, 3: 10, 4: 15, 5: 20}.iteritems():
            hist_1.GetXaxis().ChangeLabel(k, -1, -1, -1, -1, -1, str(v))
    if cond in ['cluster_size_X', 'cluster_size_Y']:
        hist_1.GetXaxis().SetNdivisions(7, False)
        hist_1.GetXaxis().CenterLabels()

    figures.draw_atlas_label(preliminary)
    txt = ROOT.TLatex()
    txt.SetNDC()
    txt.SetTextSize(txt.GetTextSize() * 0.75)
    txt.DrawLatex(0.2, 0.82, 'PYTHIA8 dijet, 1.8 < p_{T}^{jet} < 2.5 TeV')
    txt.DrawText(
        0.2,
        0.77,
        '{}-particle{} clusters'.format(pos, '' if pos == 1 else 's')
    )

    leg = ROOT.TLegend(0.6, 0.7, 0.84, 0.82)
    leg.SetBorderSize(0)
    leg.AddEntry(hist_1, 'Estimated = 1', 'PL')
    leg.AddEntry(hist_2or3, 'Estimated = 2 or 3', 'PL')
    leg.Draw()

    cnv.SaveAs('tpr_fnr_{}_{}.pdf'.format(pos, cond))

    ROOT.gStyle.SetPadRightMargin(oldmargin)
Esempio n. 6
0
def _plot_2d_cond(hsdict, variable, cond, direction, prelim):

    if direction is not None:
        name = '_'.join([variable, direction, '2D', cond])
    else:
        name = '_'.join([variable, '2D', cond])

    canvas = ROOT.TCanvas("canvas_" + name, '', 0, 0, 800, 600)
    legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.85)
    legend.SetBorderSize(0)

    colors = {1: ROOT.kBlack, 2: ROOT.kRed, 3: ROOT.kBlue}

    for nparticle in [1, 2, 3]:

        # First, build an inclusive histogram with all the layers
        hist_incl = None
        for lyr in LAYERS:
            if direction is not None:
                hname = '_'.join(
                    [variable,
                     str(nparticle), direction, '2D', cond, lyr])
            else:
                hname = '_'.join([variable, str(nparticle), '2D', cond, lyr])
            if hname in hsdict:
                if hist_incl is None:
                    hist_incl = hsdict[hname]
                else:
                    hist_incl.Add(hsdict[hname])

        if hist_incl is None:
            continue

        # Now, iterate through bins of the conditional variables in
        # the right range
        bins = []
        binvals = []
        binerrs = []
        vmin, vmax = _get_range(cond)
        for i in range(hist_incl.GetXaxis().FindBin(vmin),
                       hist_incl.GetXaxis().FindBin(vmax)):
            proj = hist_incl.ProjectionY("proj%d%d" % (i, nparticle), i, i)
            if 'residuals' in variable:
                sigma, dsigma = _fwhm(proj, err=True)
            else:
                (_, _, sigma), (_, _, dsigma) = _fit(proj, err=True)
            binvals.append(sigma)
            binerrs.append(dsigma)
            bins.append(hist_incl.GetXaxis().GetBinLowEdge(i))
        bins.append(hist_incl.GetXaxis().GetBinLowEdge(i + 1))

        bins, binvals, binerrs = _rebin(bins, binvals, binerrs, cond)

        hist = ROOT.TH1D(
            'h_%d_%s_%s_%s' % (nparticle, direction, cond, variable), '',
            len(bins) - 1, array.array('d', bins))
        ROOT.SetOwnership(hist, False)
        root_numpy.fill_hist(hist, bins[:-1], binvals)
        for i in range(1, bins.shape[0]):
            hist.SetBinError(i, binerrs[i - 1])
        hist.SetMarkerColor(colors[nparticle])
        hist.SetLineColor(colors[nparticle])
        hist.SetMarkerStyle(MARKERS[nparticle - 1])

        _set_range(hist, variable, direction, cond)

        if nparticle == 1:
            _set_range(hist, variable, direction, cond)
            hist.SetTitle(';{};{}'.format(
                _varlabel(cond)[0],
                'Residual fwhm [mm]'
                if 'residuals' in variable else 'Pull #sigma',
            ))

        if cond == 'cluster_size':
            hist.GetXaxis().SetNdivisions(4, False)
            for k, v in {1: 1, 2: 5, 3: 10, 4: 15, 5: 20}.iteritems():
                hist.GetXaxis().ChangeLabel(k, -1, -1, -1, -1, -1, str(v))

        if cond in ['cluster_size_X', 'cluster_size_Y']:
            hist.GetXaxis().SetNdivisions(7, False)
            hist.GetXaxis().CenterLabels()

        hist.Draw(('' if nparticle == 1 else 'same') + ' P E')

        legend.AddEntry(
            hist,
            "{}-particle{} clusters".format(nparticle,
                                            's' if nparticle > 1 else ''))

    legend.Draw()
    figures.draw_atlas_label(prelim)
    txt = ROOT.TLatex()
    txt.SetNDC()
    txt.SetTextSize(txt.GetTextSize() * 0.75)
    txt.DrawLatex(0.2, 0.82, 'PYTHIA8 dijet, 1.8 < p_{T}^{jet} < 2.5 TeV')
    if direction is not None:
        txt.DrawText(0.2, 0.77, 'Local {} direction'.format(direction.lower()))

    # TODO add text for residuals1{X,Y} plots

    canvas.SaveAs(name + '.pdf')
Esempio n. 7
0
def _plot_1d(hsdict, variable, nparticle, direction, preliminary):
    # pylint: disable=too-many-locals

    name = '_'.join([variable, str(nparticle), direction])

    canvas = ROOT.TCanvas('canvas_' + name, '', 0, 0, 800, 600)
    legend = ROOT.TLegend(0.61, 0.6, 0.91, 0.85)
    legend.SetBorderSize(0)
    stack = ROOT.THStack('stk_' + name, '')

    gausses = []
    for layer, color, marker in zip(LAYERS, COLORS, MARKERS):
        LOG.debug((name, layer))
        key = name + '_' + layer
        if key not in hsdict:
            continue
        hist = hsdict[key]
        if layer == 'endcap' and nparticle == 1:
            hist.Rebin(4)
            hist.Scale(0.5 / hist.Integral())
        else:
            hist.Rebin(2)
            hist.Scale(1.0 / hist.Integral())
            width = hist.GetBinWidth(1)
        hist.SetLineColor(color)
        hist.SetMarkerColor(color)
        hist.SetMarkerStyle(marker)

        if 'pull' in variable:
            hist.SetLineStyle(2)
            const, mean, sigma = _fit(hist)
            gausses.append(
                ROOT.TF1('f' + layer, '[0]*TMath::Gaus(x, [1], [2])', -5, 5))
            gausses[-1].SetLineColor(color)
            gausses[-1].SetLineStyle(2)
            gausses[-1].SetParameter(0, const)
            gausses[-1].SetParameter(1, mean)
            gausses[-1].SetParameter(2, sigma)
        else:
            mean = hist.GetMean()
            sigma = _fwhm(hist)

        stack.Add(hist)

        legend.AddEntry(
            hist, '#splitline{%s clusters}'
            '{#mu = %.2f %s, %s = %.2f %s}' % (
                _layer_name(layer),
                mean,
                'mm' if 'residuals' in variable else '',
                "fwhm" if 'residuals' in variable else "#sigma",
                sigma,
                'mm' if 'residuals' in variable else '',
            ), 'LP')

    stack.SetTitle(';Truth hit {v} {bu};Particle density / {w} {wu}'.format(
        v=variable.replace('corr_', '').rstrip('s'),
        bu='[mm]' if 'residuals' in variable else '',
        w=width,
        wu='mm' if 'residuals' in variable else ''))

    if 'pull' in variable:
        stack.Draw('p nostack')
        for g in gausses:
            g.Draw('same')
    else:
        stack.Draw('hist nostack')
        stack.Draw('p same nostack')

    scaley = 1.3
    if nparticle > 1:
        scaley = 1.7

    if 'pull' in variable:
        rangex = 5.0
    else:
        if direction == 'X':
            if nparticle == 1:
                rangex = 0.04
            else:
                rangex = 0.05
        else:
            rangex = 0.4

    print 'SETTING RANGE AT ' + str(rangex)
    stack.GetXaxis().SetRangeUser(-rangex, rangex)
    stack.SetMaximum(0.25)
    figures.draw_atlas_label(preliminary)
    legend.Draw()

    txt = ROOT.TLatex()
    txt.SetNDC()
    txt.SetTextSize(txt.GetTextSize() * 0.75)
    txt.DrawLatex(0.2, 0.79, 'PYTHIA8 dijet, 1.8 < p_{T}^{jet} < 2.5 TeV')
    txt.DrawText(
        0.2, 0.74,
        '{}-particle{} clusters'.format(nparticle,
                                        '' if nparticle == 1 else 's'))
    txt.DrawText(0.2, 0.69, 'local {} direction'.format(direction.lower()))

    canvas.SaveAs(name + '.pdf')