Ejemplo n.º 1
0
def get_uncertainty_graph(hnom, curve_uncert):
    """
    Convert an histogram and a RooCurve
    into a TGraphAsymmError

    Parameters
    ----------
    hnom: TH1F, TH1D, ...
        The histogram of nominal values
    curve_uncert: RooCurve
        The uncertainty band around the nominal value

    TODO: Improve the handling of the underflow and overflow bins
    """
    graph = Graph(hnom.GetNbinsX())
    for ibin in xrange(1, hnom.GetNbinsX() + 1):
        uncerts = []
        for ip in xrange(3, curve_uncert.GetN() - 3):
            x, y = ROOT.Double(0.), ROOT.Double(0.)
            curve_uncert.GetPoint(ip, x, y)
            if hnom.GetBinLowEdge(ibin) <= x < hnom.GetBinLowEdge(ibin + 1):
                uncerts.append(y)
        log.debug('{0}, bin {1}: {2}'.format(hnom.name, ibin, uncerts))
        low, high = min(uncerts), max(uncerts)
        bin_center = 0.5 * (hnom.GetBinLowEdge(ibin + 1) +
                            hnom.GetBinLowEdge(ibin))
        e_x_low = bin_center - hnom.GetBinLowEdge(ibin)
        e_x_high = hnom.GetBinLowEdge(ibin + 1) - bin_center
        bin_content = hnom.GetBinContent(ibin)
        e_y_low = hnom.GetBinContent(ibin) - low
        e_y_high = high - hnom.GetBinContent(ibin)
        graph.SetPoint(ibin - 1, bin_center, bin_content)
        graph.SetPointError(ibin - 1, e_x_low, e_x_high, e_y_low, e_y_high)
    return graph
Ejemplo n.º 2
0
def spread_x( histograms, bin_edges ):
    """
        Usually when plotting multiple histograms with same x-values and
        similar y-values their markers will overlap. This function spreads
        the data points across a bin. It creates a set of graphs with the
        same y-values but different x.

        @param histograms: list of histograms with same binning
        @param bin_edges: the bin edges of the histograms
    """
    # construct bins from the bin edges
    bins = [( bin_lower, bin_upper ) for bin_lower, bin_upper in izip( bin_edges[:-1], bin_edges[1:] )]
    # now get the bin widths
    bin_widths = [abs( bin_i[1] - bin_i[0] ) for bin_i in bins]
    # number of histograms
    number_of_hists = len( histograms )
    # and divide the bins into equidistant bits leaving some space to the bin edges
    x_locations = []
    add_locations = x_locations.append
    for bin_lower, width in izip( bin_edges, bin_widths ):
        x_step = width / ( 1.0 * number_of_hists + 1 )  # +1 due to spacing to bin edge
        add_locations( [bin_lower + n * x_step for n in range( 1, number_of_hists + 1 )] )

    # transpose
    x_locations = map( list, zip( *x_locations ) )

    graphs = []
    for histogram, x_coordinates in zip( histograms, x_locations ):
        g = Graph( histogram )
        for i, ( x, y ) in enumerate( zip( x_coordinates, histogram.y() ) ):
            g.SetPoint( i, x, y )

        graphs.append( g )

    return graphs
Ejemplo n.º 3
0
def tau_from_L_curve(unfoldingObject):
    '''
    Get best tau via l curve method
    Not tested
    '''
    lCurve = TGraph()
    logTauX = TSpline3()
    logTauY = TSpline3()
    iBest = unfoldingObject.ScanLcurve(500, 0., 0., lCurve, logTauX, logTauY)

    # Additional info, plots
    t = Double(0)
    x = Double(0)
    y = Double(0)
    logTauX.GetKnot(iBest, t, x)
    logTauY.GetKnot(iBest, t, y)

    bestLcurve = Graph(1)
    bestLcurve.SetPoint(1, x, y)
    lCurve.SetMarkerColor(600)
    lCurve.SetMarkerSize(1)
    lCurve.SetMarkerStyle(5)

    # lCurve.Draw("AP");
    bestLcurve.markercolor = 'red'
    # bestLcurve.Draw("*");

    return unfoldingObject.GetTau()
Ejemplo n.º 4
0
def old_working_points(ana, category, wp_level):
    log.info('create the workers')

    names = ['loose', 'medium', 'tight']

    cuts = [
        wp_level + '_is_loose == 1', wp_level + '_is_medium == 1',
        wp_level + '_is_tight == 1'
    ]

    workers = [FuncWorker(get_sig_bkg, ana, category, cut) for cut in cuts]
    run_pool(workers, n_jobs=-1)
    yields = [w.output for w in workers]

    log.info('--> Calculate the total yields')
    sig_tot = ana.tau.events(category)[1].value
    bkg_tot = ana.jet.events(category, weighted=True)[1].value
    gr = Graph(len(cuts))
    wps = []
    for i, (val, yields, name) in enumerate(zip(cuts, yields, names)):
        eff_sig = yields[0] / sig_tot
        eff_bkg = yields[1] / bkg_tot
        rej_bkg = 1. / eff_bkg if eff_bkg != 0 else 0
        wps.append(working_point(val, eff_sig, eff_bkg, name=name))
        gr.SetPoint(i, eff_sig, rej_bkg)
    return gr, wps
Ejemplo n.º 5
0
def grrScale(gr1, scale):
    x1 = Double()
    x_ = Double()
    x1e = Double()

    y1 = Double()
    y1e = Double()

    y = []
    ex = []
    ey = []

    NC = gr1.GetN()
    x = [0 for i in range(NC)]

    for ii in range(NC):
        x1e = gr1.GetErrorX(ii)
        y1e = gr1.GetErrorY(ii)
        gr1.GetPoint(ii, x_, y1)
        x1 = x_ * 1.0
        x[ii] = x1
        y.append(y1 * scale)
        ex.append(x1e)
        ey.append(y1e * scale)

    gr = Graph(NC)
    for x0, y0, x0e, y0e, i in zip(x, y, ex, ey, range(NC)):
        gr.SetPoint(i, x0, y0)
        gr.SetPointError(i, x0e, x0e, y0e, y0e)
    return gr
Ejemplo n.º 6
0
def roc(ana, category, discr_var):
    """
    Calculates the ROC curve
    Returns the sorted list of wp and a TGraph
    """
    h_template = Hist(1000, 0, 1)

    h_sig = ana.tau.get_hist_array({discr_var: h_template}, category=category)
    h_sig = h_sig[discr_var]

    h_bkg = ana.jet.get_hist_array({discr_var: h_template}, category=category)
    h_bkg = h_bkg[discr_var]

    roc_gr = Graph(h_sig.GetNbinsX())
    roc_list = []
    for i in range(1, h_sig.GetNbinsX()):
        eff_sig_i = (h_sig.Integral() -
                     h_sig.Integral(0, i)) / h_sig.Integral()
        eff_bkg_i = (h_bkg.Integral() -
                     h_bkg.Integral(0, i)) / h_bkg.Integral()
        rej_bkg_i = 1. / eff_bkg_i if eff_bkg_i != 0 else 0.
        roc_list.append(
            working_point(h_sig.GetBinLowEdge(i), eff_sig_i, eff_bkg_i))

        roc_gr.SetPoint(i, eff_sig_i, rej_bkg_i)
    return roc_gr, roc_list
Ejemplo n.º 7
0
def efficiency_graph(pass_function,
                     function_inputs,
                     xs,
                     bins=None,
                     error=0.005):
    pass_results = pass_function(function_inputs)
    if bins is None:  # Automatic binning
        # Compute the number of bins such that the error on the efficiency is equal to 'error' in each bin
        # The calculation is based on binomial errors and assumes that the efficiency is flat (that the distributions of all and selected events are the same)
        k = float(np.count_nonzero(pass_results))
        n = float(len(pass_results))
        percentiles = [0., 100.]
        if k > 0:
            nbins = (error * n)**2 / k / (1 - k / n)
            # Compute the bin bounaries with the same number of events in all bins
            percentiles = np.arange(0., 100., 100. / nbins)
            percentiles[-1] = 100.
        bins = np.unique(np.percentile(xs, percentiles))
    # Fill histograms of selected and all events and compute efficiency
    histo_pass = Hist(bins)
    histo_total = Hist(bins)
    fill_hist(histo_pass, xs, pass_results)
    fill_hist(histo_total, xs)
    efficiency = Graph()
    efficiency.Divide(histo_pass, histo_total)
    return efficiency
def luminosity_vs_time(timing_list, luminosity_list, style, run_name):
    '''

    :param timing_list: Python list containing the timing data
    :param luminosity_list: Python list containing the luminosity data
    :param style: ROOT style in string, such as 'ATLAS'
    :return:
    '''
    # Set ROOT graph style
    set_style(str(style))

    # create graph
    graph = Graph(len(timing_list))
    for i, (xx, yy) in enumerate(zip(timing_list, luminosity_list)):
        graph.SetPoint(i, xx, yy)

        # set visual attributes

    graph.markercolor = 'blue'
    graph.xaxis.SetTitle("Time")
    graph.yaxis.SetTitle("Luminosity")
    graph.xaxis.SetRangeUser(min(timing_list), max(timing_list))
    graph.yaxis.SetRangeUser(min(luminosity_list), max(luminosity_list))

    # plot with ROOT
    canvas = Canvas()
    graph.Draw("AP")
    label = ROOT.TText(0.8, 0.9, str(run_name))
    label.SetTextFont(43)
    label.SetTextSize(25)
    label.SetNDC()
    label.Draw()
    canvas.Modified()
    canvas.Update()
    wait(True)
Ejemplo n.º 9
0
def tau_from_L_curve( unfoldingObject ):
    '''
    Get best tau via l curve method
    Not tested
    '''
    lCurve = TGraph()
    logTauX = TSpline3()
    logTauY = TSpline3()
    iBest = unfoldingObject.ScanLcurve(500, 0., 0., lCurve, logTauX, logTauY);

    # Additional info, plots
    t = Double(0)
    x = Double(0)
    y = Double(0)
    logTauX.GetKnot(iBest,t,x);
    logTauY.GetKnot(iBest,t,y);

    bestLcurve = Graph(1);
    bestLcurve.SetPoint(1,x,y);
    lCurve.SetMarkerColor(600)
    lCurve.SetMarkerSize(1)
    lCurve.SetMarkerStyle(5)

    # lCurve.Draw("AP");
    bestLcurve.markercolor = 'red'
    # bestLcurve.Draw("*");

    return unfoldingObject.GetTau()
Ejemplo n.º 10
0
def remap_x_values(hist, corr_hist):
    """
    Map the x values of hist to the y values of map_hist.
    In order to do so, it is necessary that the x values of hist are also present as x-values in map_hist.

    Parameters
    ----------
    hist : Hist1D
    corr_hist : Hist2D
                Correlations between the quantity on hist's x-axis (also corr_hist's xaxis) and the new
                quantity to plot agains (on corr_hist's y-axis.

    Returns
    -------
    Graph
            Graph of the remapped hist. Errors are ??? TODO
    """
    hist = asrootpy(hist)
    corr_hist = asrootpy(corr_hist)
    profx = asrootpy(corr_hist.ProfileX(gen_random_name()))

    rt_graph = Graph()
    for i, (nch_ref_bin,
            counter_bin) in enumerate(zip(profx.bins(), hist.bins())):
        rt_graph.SetPoint(i, nch_ref_bin.value, counter_bin.value)
        xerr, yerr = nch_ref_bin.error / 2.0, counter_bin.error / 2.0
        rt_graph.SetPointError(i, xerr, xerr, yerr, yerr)
    return rt_graph
def plot_raw_detector_vs_detector(detector_one_data, detector_two_data, style, name):
    # Set ROOT graph style
    set_style(str(style))
    detector_one_list = []
    detector_two_list = []
    for block in range(len(detector_one_data)):
        for bcid in range(len(detector_one_data[block])):
            detector_one_list.append(detector_one_data[block][bcid])
            detector_two_list.append(detector_two_data[block][bcid])
    # create graph
    graph = Graph(len(detector_one_list))
    for i, (xx, yy) in enumerate(zip(detector_one_list, detector_two_list)):
        graph.SetPoint(i, float(xx), float(yy))

    # set visual attributes

    graph.markercolor = 'blue'
    graph.xaxis.SetTitle("BCM V [Raw Rate]")
    graph.yaxis.SetTitle("LUCID BI [Raw Rate]")
    graph.xaxis.SetRangeUser(0, 1)
    graph.yaxis.SetRangeUser(0, 1)

    # plot with ROOT
    canvas = Canvas()
    graph.Draw("AP")
    label = ROOT.TText(0.6, 0.9, str(name))
    label.SetTextFont(43)
    label.SetTextSize(25)
    label.SetNDC()
    label.Draw()
    canvas.Modified()
    canvas.Update()
    wait(True)
def plot_percent_luminosity_ratio(detector_one_data, detector_two_data, style, run_name):
    '''

    :param detector_one_data: Data from one detector type, such as ATLAS' LUCID, in a list of lists, every entry is one
    luminsoity block, with the luminosity block being a list of BCID data, assumed to be same
    length as detector_two_data
    :param detector_two_data: Data from another detector type, such as ATLAS' LUCID, in a list of lists, every entry is
    one luminosity block, with the luminosity block being a list of BCID data, assumed to be same
    length as detector_one_data
    :param style: The ROOT style for the graph, generally 'ATLAS'
    :return: ROOT plots of the ratio of luminosities over the luminosity, as percetnage difference from first data point
    '''
    # Set ROOT graph style
    set_style(str(style))

    print("Number of Luminosity Blocks included: " + str(len(detector_one_data)))
    # Get ratio of the detectors
    luminosity_ratio = []
    lumi_blocks = []
    for block in range(len(detector_one_data)):
        for bcid in range(len(detector_one_data[block])):
            detector_one_point = detector_one_data[block][bcid]
            detector_two_point = detector_two_data[block][bcid]
            # Check if the blocks are zero
            if detector_one_point != 0.0 and detector_two_point != 0.0:
                ratio = -math.log(1 - detector_one_point) / -math.log(1 - detector_two_point)
                luminosity_ratio.append(ratio)
                lumi_blocks.append(block)

    # Get percentage difference based off the first block and BCID
    first_point = luminosity_ratio[0]

    for index in range(len(luminosity_ratio)):
        luminosity_ratio[index] = (luminosity_ratio[index] / first_point) - 1

    # create graph
    graph = Graph(len(lumi_blocks), title=run_name)
    for i, (xx, yy) in enumerate(zip(lumi_blocks, luminosity_ratio)):
        graph.SetPoint(i, float(xx), float(yy))

    # set visual attributes

    graph.markercolor = 'blue'
    graph.xaxis.SetTitle("Luminosity Block")
    graph.yaxis.SetTitle("Luminosity [Percent Ratio]")
    graph.xaxis.SetRangeUser(min(lumi_blocks), max(lumi_blocks))
    graph.yaxis.SetRangeUser(min(luminosity_ratio), max(luminosity_ratio))

    # plot with ROOT
    canvas = Canvas()
    graph.Draw("AP")
    label = ROOT.TText(0.8, 0.9, str(run_name))
    label.SetTextFont(43)
    label.SetTextSize(25)
    label.SetNDC()
    label.Draw()
    canvas.Modified()
    canvas.Update()
    wait(True)
Ejemplo n.º 13
0
def rejection(eff):
    htot = asrootpy(eff.GetTotalHistogram()).Clone()
    hpass = asrootpy(eff.GetPassedHistogram())
    if hpass.Integral != 0:
        rej = htot / hpass
    else:
        rej = htot
    rej = Graph(rej)
    name = '_'.join(eff.name.split('_')[1:])
    rej.name = 'rej_{0}'.format(name)
    rej.title = eff.title
    return rej
Ejemplo n.º 14
0
def rejection(eff):
    htot = asrootpy(eff.GetTotalHistogram()).Clone()
    hpass = asrootpy(eff.GetPassedHistogram())
    if hpass.Integral !=0:
        rej = htot/hpass
    else:
        rej = htot
    rej = Graph(rej)
    name = '_'.join(eff.name.split('_')[1:])
    rej.name = 'rej_{0}'.format(name)
    rej.title = eff.title
    return rej
Ejemplo n.º 15
0
def test_xerr():
    g = Graph(10)
    list(g.xerr())

    g = Graph(10, type='errors')
    list(g.xerr())

    g = Graph(10, type='asymm')
    list(g.xerr())
Ejemplo n.º 16
0
def tau_from_scan( unfoldingObject, regularisation_settings ):
    variable = regularisation_settings.variable

    # Plots that get outputted by the scan
    lCurve = TGraph()
    scanResult = TSpline3()
    d = 'signal'
    a = ''

    # Parameters of scan
    # Number of points to scan, and min/max tau
    nScan = 1000
    minTau = 1.E-6
    maxTau = 1.E-0

    if variable == 'abs_lepton_eta':
        minTau = 1.E-8
        maxTau = 1.E-3
    elif variable == 'lepton_pt':
        minTau = 1.E-6
        maxTau = 1.E-2
    elif variable == 'NJets':
        minTau = 1.E-6
        maxTau = 1.E-2

    # Scan is performed here    
    iBest = unfoldingObject.ScanTau(nScan, minTau, maxTau, scanResult, TUnfoldDensity.kEScanTauRhoSquareAvg);

    # Plot the scan result
    # Correlation as function of log tau
    canvas = TCanvas()
    scanResult.SetMarkerColor(600)
    scanResult.SetMarkerSize(1)
    scanResult.SetMarkerStyle(5)
    scanResult.Draw('LP')

    # Add point corresponding to optimum tau
    t = Double(0)
    x = Double(0)
    scanResult.GetKnot(iBest,t,x);
    bestTau = Graph(1)
    bestTau.SetPoint(1,t,x)
    bestTau.markercolor = 'red'
    bestTau.SetMarkerSize(1.25)
    bestTau.Draw('*')

    # Write to file
    output_dir = regularisation_settings.output_folder
    make_folder_if_not_exists(output_dir)
    canvas.SaveAs(output_dir + '/{0}.png'.format(variable) )

    return unfoldingObject.GetTau()
Ejemplo n.º 17
0
def draw_curve_8(_func, name, title, ylow, yhigh, num_errs=1):
    """ Draw 8TeV trigger efficiency curves """
    graphs = []
    for (trigger, color) in triggers:
        tool = ROOT.TrigTauEfficiency()
        tool.loadInputFile(os.path.join(base, 'triggerSF_{0}.root'.format(trigger)))
        func = getattr(tool, _func)
        eff = np.array(map(lambda x: func(x, eta, 0, period, prong, wpflag, eveto), pt))
        errs_low = []
        errs_high = []
        for ierr in xrange(num_errs):
            eff_low = np.array(map(lambda x: func(x, eta, -1, period, prong, wpflag, eveto), pt))
            eff_high = np.array(map(lambda x: func(x, eta, 1, period, prong, wpflag, eveto), pt))
            errs_low.append(eff_low)
            errs_high.append(eff_high)
        # quadrature sum of error
        eff_low = np.sqrt(np.sum([np.power(err, 2) for err in errs_low], axis=0))
        eff_high = np.sqrt(np.sum([np.power(err, 2) for err in errs_high], axis=0))
        graph = Graph(len(pt), name=trigger)
        for i, (p, e, e_low, e_high) in enumerate(zip(pt, eff, eff_low, eff_high)):
            graph.SetPoint(i, p / 1000, e)
            graph.SetPointError(i, 0.4, 0.4, e_low, e_high)
        graph.linecolor = color
        graph.linewidth = 2
        graph.fillstyle = '/'
        graph.fillcolor = color
        graphs.append(graph)
    c = Canvas()
    leg = Legend(len(graphs),
        pad=c, topmargin=0.6, leftmargin=0.3,
        textsize=25, margin=0.2)
    for i, g in enumerate(graphs):
        if i == 0:
            g.Draw('3AL')
            g.xaxis.title = '#font[52]{p}_{T} [GeV]'
            g.xaxis.SetLimits(20, 100)
            g.yaxis.SetLimits(ylow, yhigh)
            g.yaxis.SetRangeUser(ylow, yhigh)
            g.yaxis.title = title
        else:
            g.Draw('3L SAME')
        leg.AddEntry(g, g.name, 'L')
    leg.Draw()
    lines = []
    for thresh in (25, 35):
        line = Line(thresh, ylow, thresh, yhigh)
        line.linestyle = 'dashed'
        line.linewidth = 2
        line.Draw()
        lines.append(line)
    c.SaveAs('trigger_{0}.png'.format(name))
    c.SaveAs('trigger_{0}.eps'.format(name))
Ejemplo n.º 18
0
def test_init_from_file_1d():
    with tempfile.NamedTemporaryFile() as f:
        for i in xrange(100):
            f.write('{0:.3f},{1:.3f}\n'.format(random(), random()))
        f.flush()
        g = Graph.from_file(f.name, sep=',')
        assert_equal(len(g), 100)
Ejemplo n.º 19
0
def test_errorbar():
    from rootpy.plotting import root2matplotlib as rplt
    h = Hist(100, -5, 5)
    h.FillRandom('gaus')
    g = Graph(h)
    rplt.errorbar(g)
    rplt.errorbar(h)
Ejemplo n.º 20
0
def rate(et, thresholds, total_events):
    pass_thresholds = pass_threshold(et, thresholds)
    # fill pass and total numbers of events
    bins = (pass_thresholds[:-1, 0] + pass_thresholds[1:, 0]) / 2.
    bins = np.append([-bins[0] + 2. * pass_thresholds[0, 0]], bins)
    bins = np.append(bins, [2. * pass_thresholds[-1, 0] - bins[-1]])
    histo_pass = Hist(bins)
    histo_total = Hist(bins)
    for b, n in zip(histo_pass[1:-1], pass_thresholds[:, 1]):
        b.value = n
        b.error = np.sqrt(n)
    for b in histo_total[1:-1]:
        b.value = total_events
        b.error = np.sqrt(total_events)
    rates = Graph()
    rates.Divide(histo_pass, histo_total)
    return rates
Ejemplo n.º 21
0
def get_rebinned_graph(graph_origin, binning=None):
    if binning is None:
        return graph_origin
    graph_rebin = Graph(len(binning) - 1)
    if len(graph_origin) != len(graph_rebin):
        log.warning('uniform: {0} bins != rebinned: {1} bins'.format(
            len(graph_origin), len(graph_rebin)))
        raise RuntimeError('wrong binning')
    for ipoint, (y,
                 yerr) in enumerate(zip(graph_origin.y(),
                                        graph_origin.yerr())):
        x_rebin_err = 0.5 * (binning[ipoint + 1] - binning[ipoint])
        x_rebin = binning[ipoint] + x_rebin_err
        graph_rebin.SetPoint(ipoint, x_rebin, y)
        graph_rebin.SetPointError(ipoint, x_rebin_err, x_rebin_err, yerr[0],
                                  yerr[1])
    return graph_rebin
Ejemplo n.º 22
0
def test_init_from_file_1d():
    with tempfile.NamedTemporaryFile() as f:
        for i in xrange(100):
            f.write('{0:.3f},{1:.3f}\n'.format(
                random(), random()))
        f.flush()
        g = Graph.from_file(f.name, sep=',')
        assert_equal(len(g), 100)
Ejemplo n.º 23
0
    def double_profile(tprofile_x, tprofile_y):
        """creates a graph with points whose x and y values and errors are taken from the bins of two profiles with identical binning"""
        ## Note: underflow and overflow bins are discarded

        if len(tprofile_x) != len(tprofile_y):
            raise ValueError(
                "Cannot build double profile: x and y profiles "
                "have different number or bins ({} and {})".format(
                    len(tprofile_x) - 2,
                    len(tprofile_y) - 2))

        _dp_graph = Graph(len(tprofile_x) - 2,
                          type='errors')  # symmetric errors

        _i_point = 0
        for _i_bin, (_bin_proxy_x,
                     _bin_proxy_y) in enumerate(zip(tprofile_x, tprofile_y)):
            # disregard overflow/underflow bins
            if _i_bin == 0 or _i_bin == len(tprofile_x) - 1:
                continue

            if _bin_proxy_y.value:
                _dp_graph.SetPoint(_i_point, _bin_proxy_x.value,
                                   _bin_proxy_y.value)
                _dp_graph.SetPointError(_i_point, _bin_proxy_x.error,
                                        _bin_proxy_y.error)
                _i_point += 1

        # remove "unfilled" points
        while (_dp_graph.GetN() > _i_point):
            _dp_graph.RemovePoint(_dp_graph.GetN() - 1)

        return _dp_graph
Ejemplo n.º 24
0
def UncertGraph(hnom, curve_uncert):
    """
    Convert an histogram and a RooCurve
    into a TGraphAsymmError

    Parameters
    ----------
    hnom: TH1F, TH1D,...
        The histogram of nominal values
    curve_uncert: RooCurve
        The uncertainty band around the nominal value
    curve_uncert: RooCurve
    TODO: Improve the handling of the underflow and overflow bins
    """

    graph = Graph(hnom.GetNbinsX())
    # ---------------------------------------------
    for ibin in xrange(1, hnom.GetNbinsX() + 1):
        uncerts = []
        for ip in xrange(3, curve_uncert.GetN() - 3):
            x, y = ROOT.Double(0.), ROOT.Double(0.)
            curve_uncert.GetPoint(ip, x, y)
            if int(x) == int(hnom.GetBinLowEdge(ibin)):
                uncerts.append(y)
        uncerts.sort()
        log.info('{0}: {1}'.format(hnom.name, uncerts))
        if len(uncerts) != 2:
            for val in uncerts:
                if val in uncerts:
                    uncerts.remove(val)
        if len(uncerts) != 2:
            raise RuntimeError(
                'Need exactly two error values and got {0}'.format(uncerts))

        bin_center = 0.5 * (hnom.GetBinLowEdge(ibin + 1) +
                            hnom.GetBinLowEdge(ibin))
        e_x_low = bin_center - hnom.GetBinLowEdge(ibin)
        e_x_high = hnom.GetBinLowEdge(ibin + 1) - bin_center
        bin_content = hnom.GetBinContent(ibin)
        e_y_low = hnom.GetBinContent(ibin) - uncerts[0]
        e_y_high = uncerts[1] - hnom.GetBinContent(ibin)
        graph.SetPoint(ibin - 1, bin_center, bin_content)
        graph.SetPointError(ibin - 1, e_x_low, e_x_high, e_y_low, e_y_high)
    # ---------------------------------------------
    return graph
Ejemplo n.º 25
0
def test_operators():
    h = Hist(10, -2, 2).FillRandom('gaus')
    g = Graph(h)
    g *= 2
    g /= 2
    g += 2
    g -= 2
    for point in g:
        assert_equal(point.y.value, h[point.idx_ + 1].value)
Ejemplo n.º 26
0
def draw_curve_7(_func, name, title, ylow, yhigh):
    """ Draw 7TeV trigger efficiency curves """
    graphs = []
    for (trigger, color) in triggers:
        tool = ROOT.TauTriggerCorrections(os.path.join(base, 'triggerSF_%s.root' % trigger))
        func = getattr(tool, _func)
        eff = map(lambda x: func(x, 0), pt)
        eff_low = map(lambda x: func(x, -1), pt)
        eff_high = map(lambda x: func(x, 1), pt)
        graph = Graph(len(pt), name=trigger)
        for i, (p, e, e_low, e_high) in enumerate(zip(pt, eff, eff_low, eff_high)):
            graph.SetPoint(i, p / 1000, e)
            graph.SetPointError(i, 0.4, 0.4, e - e_low, e_high - e)
        graph.linecolor = color
        graph.linewidth = 2
        graph.fillstyle = '/'
        graph.fillcolor = color
        graphs.append(graph)
    c = Canvas()
    leg = Legend(len(graphs),
        pad=c, topmargin=0.4, leftmargin=0.3, textsize=25, margin=0.2)
    for i, g in enumerate(graphs):
        if i == 0:
            g.Draw('3AC')
            g.xaxis.title = '#font[52]{p}_{T} [GeV]'
            g.xaxis.SetLimits(20, 100)
            g.yaxis.SetLimits(ylow, yhigh)
            g.yaxis.SetRangeUser(ylow, yhigh)
            g.yaxis.title = title
        else:
            g.Draw('3C SAME')
        leg.AddEntry(g, g.name, 'L')
    leg.Draw()
    lines = []
    for thresh in (25, 35):
        line = Line(thresh, ylow, thresh, yhigh)
        line.linestyle = 'dashed'
        line.linewidth = 2
        line.Draw()
        lines.append(line)
    c.SaveAs('trigger_{0}.png'.format(name))
    c.SaveAs('trigger_{0}.eps'.format(name))
Ejemplo n.º 27
0
def scaleGraph(gr1, sc):
    x1 = ROOT.Double()
    x_ = ROOT.Double()
    y1 = ROOT.Double()
    test = []
    y = []

    NC = gr1.GetN()
    x = [0 for i in range(NC)]

    for ii in range(NC):
        gr1.GetPoint(ii, x_, y1)
        x1 = x_ * 1.0
        x[ii] = x1
        y.append(y1 * sc)

    gr = Graph(NC)
    for x0, y0, i in zip(x, y, range(NC)):
        gr.SetPoint(i, x0, y0)
    return gr
def get_ratios(results_7TeV, results_8TeV):
    ratios = {}
    for key in results_7TeV.keys():
        ratio = None
        if 'Graph' in str(type(results_7TeV[key])):
            ratio = Graph.divide(results_7TeV[key], results_8TeV[key], False)
        else:
            ratio = results_7TeV[key].Clone( 'ratio_' + key )
            ratio.Divide(results_8TeV[key])
        ratios[key] = ratio
    return ratios
Ejemplo n.º 29
0
def get_ratios(results_7TeV, results_8TeV):
    ratios = {}
    for key in results_7TeV.keys():
        ratio = None
        if 'Graph' in str(type(results_7TeV[key])):
            ratio = Graph.divide(results_7TeV[key], results_8TeV[key], False)
        else:
            ratio = results_7TeV[key].Clone('ratio_' + key)
            ratio.Divide(results_8TeV[key])
        ratios[key] = ratio
    return ratios
Ejemplo n.º 30
0
def efficiency_bdt(pass_function, function_inputs, xs):
    pass_results = pass_function(function_inputs)
    xs_train = xs.reshape(-1, 1)
    clf = GradientBoostingClassifier()
    clf.fit(xs_train, pass_results)
    graph = Graph(100)
    xs_graph = np.linspace(np.amin(xs), np.amax(xs), num=100)
    probas = clf.predict_proba(xs_graph.reshape(-1, 1))[:, [1]]
    print probas
    fill_graph(graph, np.column_stack((xs_graph, probas)))
    print np.column_stack((xs_graph, probas))
    return graph
Ejemplo n.º 31
0
def makeSystError(gr1, gr2, **kwargs):
    x_ = Double()
    x1e = Double()
    x2 = Double()
    x2e = Double()
    y1 = Double()
    y1e = Double()
    y2 = Double()
    y2e = Double()
    NC = gr1.GetN()
    print("Number of points: {}".format(NC))
    y = []
    ex = []
    ey = []
    x = [0 for i in range(NC)]

    for ii in range(NC):
        x1e = gr1.GetErrorX(ii)
        y1e = gr1.GetErrorY(ii)
        gr1.GetPoint(ii, x_, y1)
        x1 = x_ * 1.0
        gr2.GetPoint(ii, x2, y2)
        x2e = gr2.GetErrorX(ii)
        y2e = gr2.GetErrorY(ii)
        x[ii] = x1
        if (kwargs.get('abs', False)):
            y.append(math.fabs(y1 - y2))
        elif (kwargs.get('rel', False)):
            y.append((y1 - y2) / (y1 + y2))
        else:
            y.append(y1 - y2)
        ex.append(x1e)
        ey.append(math.sqrt(y1e**2 + y2e**2))
    gr = Graph(NC)
    for x0, y0, x0e, y0e, i in zip(x, y, ex, ey, range(NC)):
        gr.SetPoint(i, x0, y0)
        print("Set point {} {},{}".format(i, x0, y0))
        gr.SetPointError(i, x0e, x0e, y0e, y0e)
    return gr
Ejemplo n.º 32
0
def grrDivide(gr1, gr2):
    x1 = Double()
    x_ = Double()
    x1e = Double()
    x2 = Double()
    x2e = Double()
    y1 = Double()
    y1e = Double()
    y2 = Double()
    y2e = Double()
    test = []
    y = []
    ex = []
    ey = []

    NC = gr1.GetN()
    x = [0 for i in range(NC)]
    test = [0 for i in range(NC)]

    for ii in range(NC):
        x1e = gr1.GetErrorX(ii)
        y1e = gr1.GetErrorY(ii)
        gr1.GetPoint(ii, x_, y1)
        x1 = x_ * 1.0
        gr2.GetPoint(ii, x2, y2)
        x2e = gr2.GetErrorX(ii)
        y2e = gr2.GetErrorY(ii)
        x[ii] = x1
        y.append(y1 / y2 if y2 != 0 else 0)
        ex.append(x2e)
        ey.append(
            math.sqrt(pow(y1e / y2, 2) + pow(y1 * y2e /
                                             (y2 * y2), 2)) if y2 != 0 else 0)

    gr = Graph(NC)
    for x0, y0, x0e, y0e, i in zip(x, y, ex, ey, range(NC)):
        gr.SetPoint(i, x0, y0)
        gr.SetPointError(i, x0e, x0e, y0e, y0e)
    return gr
Ejemplo n.º 33
0
def find_best_working_point(effs, signal_efficiencies,
                            background_efficiencies):
    # Compute efficiency gradients
    effs_diff = np.ediff1d(effs)
    signal_efficiencies_diff = np.ediff1d(signal_efficiencies)
    background_efficiencies_diff = np.ediff1d(background_efficiencies)
    signal_efficiencies_diff = np.divide(signal_efficiencies_diff, effs_diff)
    background_efficiencies_diff = np.divide(background_efficiencies_diff,
                                             effs_diff)
    # Interpolate and find points where background efficiency gradient > signal efficiency gradient (with some tolerance)
    interp_x = np.linspace(np.amin(effs[1:]), np.amax(effs[1:]), 1000)
    interp_signal = np.interp(interp_x, effs[1:], signal_efficiencies_diff)
    interp_background = np.interp(interp_x, effs[1:],
                                  background_efficiencies_diff)
    optimal_points = np.argwhere(
        np.greater(interp_background - 0.05, interp_signal)).ravel(
        )  # Use a tolerance of 0.02 in case of fluctuations
    if len(optimal_points) == 0:
        print 'WARNING: no working point found where the efficiency gradient is larger for background than for signal'
    # Find optimal point with smallest efficiency
    ## Compute spacing between points, and select those with an efficiency separation > 2%
    optimal_discontinuities = np.argwhere(
        np.ediff1d(interp_x[optimal_points]) > 0.02).ravel()
    ## Select the point with the largest efficiency
    optimal_index = np.amax(optimal_discontinuities) + 1 if len(
        optimal_discontinuities) > 0 else 0
    optimal_point = interp_x[optimal_points[optimal_index]]
    # Create graphs
    signal_efficiencies_diff_graph = Graph(len(effs) - 1)
    background_efficiencies_diff_graph = Graph(len(effs) - 1)
    optimal_points_graph = Graph(len(optimal_points))
    fill_graph(signal_efficiencies_diff_graph,
               np.column_stack((effs[1:], signal_efficiencies_diff)))
    fill_graph(background_efficiencies_diff_graph,
               np.column_stack((effs[1:], background_efficiencies_diff)))
    fill_graph(
        optimal_points_graph,
        np.column_stack(
            (interp_x[optimal_points], interp_signal[optimal_points])))
    signal_efficiencies_diff_graph.SetName('efficiencies_signal')
    background_efficiencies_diff_graph.SetName('efficiencies_background')
    optimal_points_graph.SetName('signal_background_optimal_points')
    return signal_efficiencies_diff_graph, background_efficiencies_diff_graph, optimal_points_graph, optimal_point
Ejemplo n.º 34
0
def draw_curve_7(_func, name, title, ylow, yhigh):
    """ Draw 7TeV trigger efficiency curves """
    graphs = []
    for (trigger, color) in triggers:
        tool = ROOT.TauTriggerCorrections(
            os.path.join(base, 'triggerSF_%s.root' % trigger))
        func = getattr(tool, _func)
        eff = map(lambda x: func(x, 0), pt)
        eff_low = map(lambda x: func(x, -1), pt)
        eff_high = map(lambda x: func(x, 1), pt)
        graph = Graph(len(pt), name=trigger)
        for i, (p, e, e_low,
                e_high) in enumerate(zip(pt, eff, eff_low, eff_high)):
            graph.SetPoint(i, p / 1000, e)
            graph.SetPointError(i, 0.4, 0.4, e - e_low, e_high - e)
        graph.linecolor = color
        graph.linewidth = 2
        graph.fillstyle = '/'
        graph.fillcolor = color
        graphs.append(graph)
    c = Canvas()
    leg = Legend(len(graphs),
                 pad=c,
                 topmargin=0.4,
                 leftmargin=0.3,
                 textsize=25,
                 margin=0.2)
    for i, g in enumerate(graphs):
        if i == 0:
            g.Draw('3AC')
            g.xaxis.title = '#font[52]{p}_{T} [GeV]'
            g.xaxis.SetLimits(20, 100)
            g.yaxis.SetLimits(ylow, yhigh)
            g.yaxis.SetRangeUser(ylow, yhigh)
            g.yaxis.title = title
        else:
            g.Draw('3C SAME')
        leg.AddEntry(g, g.name, 'L')
    leg.Draw()
    lines = []
    for thresh in (25, 35):
        line = Line(thresh, ylow, thresh, yhigh)
        line.linestyle = 'dashed'
        line.linewidth = 2
        line.Draw()
        lines.append(line)
    c.SaveAs('trigger_{0}.png'.format(name))
    c.SaveAs('trigger_{0}.eps'.format(name))
Ejemplo n.º 35
0
def test_xerr():
    g = Graph(10)
    list(g.xerr())

    g = Graph(10, type='errors')
    list(g.xerr())

    g = Graph(10, type='asymm')
    list(g.xerr())
def luminosity_block_log_time(luminosity_list, style):
    '''

    :param timing_list: Python list containing the timing data
    :param luminosity_list: Python list containing the luminosity data
    :param style: ROOT style in string, such as 'ATLAS'
    :return: Graph of the luminosity over a single block
    '''
    # Set ROOT graph style
    set_style(str(style))

    luminosity_ratio = []
    for bcid in range(len(luminosity_list)):
        detector_one_point = luminosity_list[bcid]
        # Check if the blocks are zero
        if detector_one_point != 0.0:
            ratio = -math.log(1 - detector_one_point)
            luminosity_ratio.append(ratio)

    # create graph
    graph = Graph(len(luminosity_ratio))
    for i, (xx, yy) in enumerate(zip(range(len(luminosity_ratio)), luminosity_ratio)):
        graph.SetPoint(i, xx, yy)

        # set visual attributes

    graph.markercolor = 'blue'
    graph.xaxis.SetTitle("Time")
    graph.yaxis.SetTitle("-Ln(1 - Rate) [Single Detector]")
    graph.xaxis.SetRangeUser(0, 3564)
    graph.yaxis.SetRangeUser(min(luminosity_ratio), max(luminosity_ratio))

    # plot with ROOT
    canvas = Canvas()
    graph.Draw("AP")
    wait(True)
Ejemplo n.º 37
0
def get_mean_rms(category, var):
    gr_mean = Graph(len(SIGNALS_14TEV))
    gr_rms = Graph(len(SIGNALS_14TEV))
    for ip, signal in enumerate(SIGNALS_14TEV):
        with root_open('efficiencies/eff_presel_{0}_v{1}.root'.format(
                signal, VERSION)) as fsig:
            h_s = fsig[category].Get('h_' + category + '_' + var['name'])
            gr_mean.SetPoint(ip, DATASETS[signal]['mu'], h_s.GetMean())
            gr_mean.SetPointError(ip, 0, 0, h_s.GetMeanError(),
                                  h_s.GetMeanError())
            gr_rms.SetPoint(ip, DATASETS[signal]['mu'], h_s.GetRMS())
            gr_rms.SetPointError(ip, 0, 0, h_s.GetRMSError(),
                                 h_s.GetRMSError())
    gr_mean.xaxis.title = 'Average Interactions Per Bunch Crossing'
    gr_mean.yaxis.title = 'Mean of ' + get_label(var)
    gr_rms.xaxis.title = 'Average Interactions Per Bunch Crossing'
    gr_rms.yaxis.title = 'RMS of ' + get_label(var)
    return gr_mean, gr_rms
Ejemplo n.º 38
0
def addConstantError(gr1, error, **kwargs):
    x_ = Double()
    x1e = Double()
    y1_ = Double()
    y1e = Double()
    NC = gr1.GetN()
    y2 = [0 for i in range(NC)]
    ex = []
    ey = []
    x = [0 for i in range(NC)]
    gr = Graph(NC)
    for ii in range(NC):
        x1e = gr1.GetErrorX(ii)
        y1e = gr1.GetErrorY(ii)
        gr1.GetPoint(ii, x_, y1_)
        print("{} {} {}".format(i, x_, y1_))
        x1 = x_ * 1.0

        x[ii] = x1
        y1 = y1_ * 1.0
        y2[ii] = y1
        print("Append y with {}".format(y1))
        print(y2)
        ex.append(x1e)
        if (kwargs.get('rel'), False):
            ey.append(math.sqrt(y1e**2 + (error * y1)**2))
            print("Old error: {}, New Error: {}".format(
                y1e, math.sqrt(y1e**2 + (error * y1)**2)))
        else:
            ey.append(math.sqrt(y1e**2 + error**2))


#     gr.SetPoint(i,x1,y1)
#     print("Set point {} {},{}".format(i,x1,y1))
#
#     if(kwargs.get('rel'),False):
#       gr.SetPointError(i,x1e,x1e,math.sqrt(y1e**2+(error*y1)**2),math.sqrt(y1e**2+(error*y1)**2))
#       print("Set point Error {} {},{}".format(i,x1e,math.sqrt(y1e**2+(error*y1)**2)))
#     else:
#       gr.SetPointError(i,x1e,x1e,math.sqrt(y1e**2+error**2),math.sqrt(y1e**2+error**2))

    gr = Graph(NC)
    print(x)
    print(y2)
    print(ex)
    print(ey)
    for x0, y0, x0e, y0e, i in zip(x, y2, ex, ey, range(NC)):
        gr.SetPoint(i, x0, y0)
        print("Set point {} {},{}".format(i, x0, y0))
        gr.SetPointError(i, x0e, x0e, y0e, y0e)
        print("Set point Error {} {},{}".format(i, x0e, y0e))

    print(gr)
    return gr
Ejemplo n.º 39
0
    Hist(100, -5, 5, color='salmon',
         drawstyle='hist').FillRandom(F1('TMath::Gaus(x, 2, 1)'), 500))
stack.Add(
    Hist(100, -5, 5, color='powderblue',
         drawstyle='hist').FillRandom(F1('TMath::Gaus(x, 2, 0.6)'), 300))
objects.append(stack)

# create some random histograms
for i, (mu, sigma, n, c,
        s) in enumerate(zip(mus, sigmas, events, colors, styles)):
    hist = Hist(100,
                -5,
                5,
                color=c,
                fillstyle=s,
                drawstyle='hist' if i % 2 == 0 else '')
    hist.FillRandom(F1('TMath::Gaus(x,{0},{1})'.format(mu, sigma)), n)
    objects.append(hist)

# create a graph
graph = Graph(10, drawstyle='P')
for i in range(10):
    x = -2 + i * 4 / 10.
    graph.SetPoint(i, x, 40 + 10 * sin(x))
objects.append(graph)

draw(objects, xtitle='Some Variable [Units]', ytitle='Events', ypadding=0.05)
# see rootpy.plotting.utils.get_limits for details on what arguments are
# supported for setting the axes ranges.
wait()
def plot_normalized_luminosity_ratio(detector_one_data, detector_two_data, style, run_name):
    '''

    :param detector_one_data: Data from one detector type, such as ATLAS' LUCID, in a list of lists, every entry is one
    luminsoity block, with the luminosity block being a list of BCID data, assumed to be same
    length as detector_two_data
    :param detector_two_data: Data from another detector type, such as ATLAS' LUCID, in a list of lists, every entry is
    one luminosity block, with the luminosity block being a list of BCID data, assumed to be same
    length as detector_one_data
    :param style: The ROOT style for the graph, generally 'ATLAS'
    :return: ROOT plots of the ratio of luminosities over the luminosity, normalized to one
    '''

    # Set ROOT graph style
    set_style(str(style))

    print("Number of Luminosity Blocks included: " + str(len(detector_one_data)))

    # Get ratio of the detectors
    luminosity_ratio = []
    lumi_blocks = []
    for block in range(len(detector_one_data)):
        for bcid in range(len(detector_one_data[block])):
            detector_one_point = detector_one_data[block][bcid]
            detector_two_point = detector_two_data[block][bcid]
            # Check if the blocks are zero
            if detector_one_point != 0.0 and detector_two_point != 0.0:
                ratio = -math.log(1 - detector_one_point) / -math.log(1 - detector_two_point)
                luminosity_ratio.append(ratio)
                lumi_blocks.append(block)

    # Normalize the ratios
    def normalize(x, x_min, x_max):
        top = x - x_min
        bottom = x_max - x_min
        return top / bottom

    max_ratio = max(luminosity_ratio)
    min_ratio = min(luminosity_ratio)
    print("Max ratio: " + str(max_ratio))
    print("Min Ratio: " + str(min_ratio))
    for ratio_entry in range(len(luminosity_ratio)):
        luminosity_ratio[ratio_entry] = normalize(luminosity_ratio[ratio_entry], x_max=max_ratio, x_min=min_ratio)

    # create graph
    graph = Graph(len(lumi_blocks), title=run_name)
    for i, (xx, yy) in enumerate(zip(lumi_blocks, luminosity_ratio)):
        graph.SetPoint(i, float(xx), float(yy))

    # set visual attributes

    graph.markercolor = 'blue'
    graph.xaxis.SetTitle("Luminosity Block")
    graph.yaxis.SetTitle("Luminosity [Normalized Ratio]")
    graph.xaxis.SetRangeUser(min(lumi_blocks), max(lumi_blocks))
    graph.yaxis.SetRangeUser(min(luminosity_ratio), max(luminosity_ratio))

    # plot with ROOT
    canvas = Canvas()
    graph.Draw("AP")
    label = ROOT.TText(0.8, 0.9, str(run_name))
    label.SetTextFont(43)
    label.SetTextSize(25)
    label.SetNDC()
    label.Draw()
    canvas.Modified()
    canvas.Update()
    wait(True)
def plot_percent_luminosity_ratio_sum(detector_one_data, detector_two_data, style, run_name):
    '''

    :param detector_one_data: Data from one detector type, such as ATLAS' LUCID, in a list of lists, every entry is one
    luminsoity block, with the luminosity block being a list of BCID data, assumed to be same
    length as detector_two_data
    :param detector_two_data: Data from another detector type, such as ATLAS' LUCID, in a list of lists, every entry is
    one luminosity block, with the luminosity block being a list of BCID data, assumed to be same
    length as detector_one_data
    :param style: The ROOT style for the graph, generally 'ATLAS'
    :return: ROOT plots of the ratio of luminosity sums over the luminosity, as percentage difference from first data point
    '''
    # Set ROOT graph style
    set_style(str(style))

    print("Number of Luminosity Blocks included: " + str(len(detector_one_data)))

    # Get average value of the rate for each luminosity block
    temp_detector_one = [[] for _ in xrange(len(detector_one_data))]
    temp_detector_two = [[] for _ in xrange(len(detector_one_data))]
    for block in range(len(detector_one_data)):
        detector_one_avg = 0
        one_count = 0
        detector_two_avg = 0
        two_count = 0
        for bcid in range(len(detector_one_data[block])):
            detector_one_point = detector_one_data[block][bcid]
            detector_two_point = detector_two_data[block][bcid]
            detector_one_avg += detector_one_point
            one_count += 1
            detector_two_avg += detector_two_point
            two_count += 1
        if one_count != 0:
            detector_one_avg = detector_one_avg / one_count
            detector_two_avg = detector_two_avg / two_count
            temp_detector_one[block].append(detector_one_avg)
            temp_detector_two[block].append(detector_two_avg)

    # Reassign temp to the original lists
    detector_one_data = temp_detector_one
    detector_two_data = temp_detector_two

    # Get ratio of the detectors
    luminosity_ratio = []
    lumi_blocks = []
    for block in range(len(detector_one_data)):
        for bcid in range(len(detector_one_data[block])):
            detector_one_point = detector_one_data[block][bcid]
            detector_two_point = detector_two_data[block][bcid]
            # Check if the blocks are zero
            if detector_one_point != 0.0 and detector_two_point != 0.0:
                ratio = -math.log(1 - detector_one_point) / -math.log(1 - detector_two_point)
                luminosity_ratio.append(ratio)
                lumi_blocks.append(block)

    # Get percentage difference based off the first block and BCID
    first_point = luminosity_ratio[0]

    for index in range(len(luminosity_ratio)):
        luminosity_ratio[index] = (luminosity_ratio[index] / first_point) - 1

    # Delete last point, when the beams are being shut down and there are massive spikes
    luminosity_ratio = luminosity_ratio[:-1]
    lumi_blocks = lumi_blocks[:-1]
    # create graph
    graph = Graph(len(lumi_blocks), title=run_name)
    for i, (xx, yy) in enumerate(zip(lumi_blocks, luminosity_ratio)):
        graph.SetPoint(i, float(xx), float(yy))

    # set visual attributes

    graph.markercolor = 'blue'
    graph.xaxis.SetTitle("Luminosity Block")
    graph.yaxis.SetTitle("Luminosity [Average Percent Ratio]")
    graph.xaxis.SetRangeUser(min(lumi_blocks), max(lumi_blocks))
    graph.yaxis.SetRangeUser(min(luminosity_ratio), max(luminosity_ratio))

    # plot with ROOT
    canvas = Canvas()
    graph.Draw("AP")
    label = ROOT.TText(0.8, 0.9, str(run_name))
    label.SetTextFont(43)
    label.SetTextSize(25)
    label.SetNDC()
    label.Draw()
    canvas.Modified()
    canvas.Update()
    wait(True)
Ejemplo n.º 42
0
	# remove the errorbars
	tmphandles = []
	tmplabels = []
	for a,b in zip(handles,labels):
		if type(a)==Line2D:
			continue
		tmphandles.append(a[0])
		tmplabels.append(b)
	# use them in the legend
	axes.legend(tmphandles, tmplabels, loc='best',numpoints=1)


	if 'Data' in hists:
		# print list(stack.sum.y())
		# ratioplot = Graph.divide(  Graph(hists['Data']), stack.sum  )
		ratioplot = Graph()
		ratioplot.Divide(  hists['Data'], stack.sum , 'pois'  )
		ratioplot.color = "black"

		if hists["Data"].Integral():
			tmpyerror,tmpyerror2 = zip(*list(ratioplot.yerr()) )
			tmpx = list(ratioplot.x())
			tmpy = list(ratioplot.y())
			tmpxy = zip(tmpx,tmpy,tmpyerror)
			# print tmpxy
			tmpxy = [tmp for tmp in tmpxy if tmp[1]!=0  ]
			# print tmpxy
			tmpx,tmpy,tmpyerror = zip(*tmpxy)
			# print tmpyerror
			axes_ratio.errorbar(tmpx, tmpy, 
								# list(ratioplot.y()), 
Ejemplo n.º 43
0
def draw_ratio(a, b, field, category,
               textsize=22,
               ratio_range=(0, 2),
               ratio_line_values=[0.5, 1, 1.5],
               optional_label_text=None,
               normalize=True,
               logy=False):
    """
    Draw a canvas with two Hists normalized to unity on top
    and a ratio plot between the two hist
    Parameters:
    - a: Nominal Hist (denominator in the ratio)
    - b: Shifted Hist (numerator in the ratio)
    - field: variable field (see variables.py)
    - category: analysis category (see categories/*)
    """
    xtitle = get_xtitle(field)

    plot = RatioPlot(xtitle=xtitle,
                     ytitle='{0}Events'.format(
                         'Normalized ' if normalize else ''),
                     ratio_title='A / B',
                     ratio_limits=ratio_range,
                     ratio_line_values=ratio_line_values,
                     logy=logy)
    if normalize:
        a_integral = a.integral()
        if a_integral != 0:
            a /= a_integral
        b_integral = b.integral()
        if b_integral != 0:
            b /= b_integral
    a.title = 'A: ' + a.title
    b.title = 'B: ' + b.title
    a.color = 'black'
    b.color = 'red'
    a.legendstyle = 'L'
    b.legendstyle = 'L'
    a.markersize = 0
    b.markersize = 0
    a.linewidth = 2
    b.linewidth = 2
    a.fillstyle = 'hollow'
    b.fillstyle = 'hollow'
    a.linestyle = 'solid'
    b.linestyle = 'dashed'
    a.drawstyle='hist E0'
    b.drawstyle='hist E0'
    plot.draw('main', [a, b], ypadding=(0.3, 0.))
    ratio = Hist.divide(a, b, fill_value=-1)
    ratio.drawstyle = 'hist'
    ratio.color = 'black'
    ratio_band = Graph(ratio, fillstyle='/', fillcolor='black', linewidth=0)
    ratio_band.drawstyle = '20'
    plot.draw('ratio', [ratio_band, ratio])
    with plot.pad('main') as pad:
        # legend
        #         leg = Legend([a, b], 0.2, 0.2, 0.45,
        #                      margin=0.35, textsize=textsize)
        leg = Legend([a, b])
        leg.Draw()
        # draw the category label
        if category is not None:
            label = ROOT.TLatex(
                pad.GetLeftMargin() + 0.04, 0.87,
                category.label)
            label.SetNDC()
            label.SetTextFont(43)
            label.SetTextSize(textsize)
            label.Draw()
        # show p-value and chi^2
        if a.integral() != 0 and b.integral() != 0:
            pvalue = a.Chi2Test(b, 'WW')
            chi2 = a.Chi2Test(b, 'WW CHI2/NDF')
        else:
            pvalue = -9999.
            chi2 = -9999.
        pvalue_label = ROOT.TLatex(
            pad.GetLeftMargin() + 0.04, 0.8,
            "p-value={0:.2f}".format(pvalue))
        pvalue_label.SetNDC(True)
        pvalue_label.SetTextFont(43)
        pvalue_label.SetTextSize(textsize)
        pvalue_label.Draw()
        
        chi2_label = ROOT.TLatex(
            pad.GetLeftMargin() + 0.04, 0.72,
            "#frac{{#chi^{{2}}}}{{ndf}}={0:.2f}".format(chi2))
        chi2_label.SetNDC(True)
        chi2_label.SetTextFont(43)
        chi2_label.SetTextSize(textsize)
        chi2_label.Draw()
        if optional_label_text is not None:
            optional_label = ROOT.TLatex(pad.GetLeftMargin()+0.01, 1 - pad.GetTopMargin() + 0.005,
                                         optional_label_text )
            optional_label.SetNDC(True)
            optional_label.SetTextFont(43)
            optional_label.SetTextSize(textsize)
            optional_label.Draw()
        if ATLAS_LABEL.lower() == 'internal':
            x = 0.67
            y = 1-pad.GetTopMargin()+0.005
        else:
            x = (1. - pad.GetRightMargin() - 0.03) - len(ATLAS_LABEL) * 0.025
            y = 1-pad.GetTopMargin()+0.01
        ATLAS_label(x, y,
                    sep=0.132, pad=pad, sqrts=None,
                    text=ATLAS_LABEL,
                    textsize=textsize)
    return plot
def plot_multiple_all_luminosity_block_ratio(all_detector_one_data, all_detector_two_data, all_detector_three_data,
                                             background_list, style, name):
    '''

    :param all_detector_one_data: A dictionary of the run name to a list of lists of luminosity blocks
    :param all_detector_two_data: A dictionary of the run name to a list of lists of luminosity blocks
    :param style: a string corresponding to a ROOT graphing style, e.g. 'ATLAS'
    :return: Plot of all the detector data on a graph chronologically according to run number
    '''
    # Set ROOT graph style
    set_style(str(style))

    # Get average value of the rate for each luminosity block
    temp_detector_one = copy.deepcopy(all_detector_one_data)
    temp_detector_two = copy.deepcopy(all_detector_two_data)
    temp_detector_three = copy.deepcopy(all_detector_three_data)
    print(sorted(all_detector_one_data.keys()))
    for run in sorted(all_detector_one_data.keys()):
        block_count = 0
        for block in range(len(all_detector_one_data.get(run)) - 1):
            del temp_detector_one.get(run)[block][:]
            del temp_detector_two.get(run)[block][:]
            del temp_detector_three.get(run)[block][:]
            block_count += 1
            detector_one_avg = 0
            one_count = 0
            detector_two_avg = 0
            two_count = 0
            detector_three_avg = 0
            three_count = 0
            for bcid in range(len(all_detector_one_data.get(run)[block])):
                # Gets the previous BCID luminosity to subtract as the background
                if run == "286282":
                    detector_one_point_background = all_detector_one_data.get(run)[block][bcid - 1]
                    detector_two_point_background = all_detector_two_data.get(run)[block][bcid - 1]
                    detector_three_point_background = all_detector_three_data.get(run)[block][bcid - 1]
                    print("BCID [N-1]: " + str(detector_one_point_background))
                    print("BCID [N]: " + str(all_detector_one_data.get(run)[block][bcid]))
                    detector_one_point = -math.log(1 - all_detector_one_data.get(run)[block][bcid]) \
                                         + math.log(1 - detector_one_point_background)
                    detector_three_point = -math.log(1 - all_detector_three_data.get(run)[block][bcid]) \
                                           + math.log(1 - detector_three_point_background)
                    print("Detector 1 Point: " + str(detector_one_point))
                    detector_two_point = -math.log(1 - all_detector_two_data.get(run)[block][bcid]) \
                                         + math.log(1 - detector_two_point_background)
                    detector_one_avg += detector_one_point
                    one_count += 1
                    detector_two_avg += detector_two_point
                    two_count += 1
                    detector_three_avg += detector_three_point
                    three_count += 1
                else:
                    detector_one_point = -math.log(1 - all_detector_one_data.get(run)[block][bcid])
                    detector_two_point = -math.log(1 - all_detector_two_data.get(run)[block][bcid])
                    detector_three_point = -math.log(1 - all_detector_three_data.get(run)[block][bcid])
                    detector_one_avg += detector_one_point
                    one_count += 1
                    detector_two_avg += detector_two_point
                    two_count += 1
                    detector_three_avg += detector_three_point
                    three_count += 1
            if one_count != 0:
                detector_one_avg = detector_one_avg / one_count
                detector_two_avg = detector_two_avg / two_count
                detector_three_avg = detector_three_avg / three_count
                temp_detector_one.get(run)[block_count - 1].append(detector_one_avg)
                temp_detector_two.get(run)[block_count - 1].append(detector_two_avg)
                temp_detector_three.get(run)[block_count - 1].append(detector_three_avg)
        # Remove the last luminosity block from each run, the one that generally spikes
        temp_detector_one[run] = temp_detector_one[run][:-10]
        temp_detector_two[run] = temp_detector_two[run][:-10]
        temp_detector_three[run] = temp_detector_three[run][:-10]
    # Reassign temp to the original lists
    all_detector_one_data = temp_detector_one
    all_detector_two_data = temp_detector_two
    all_detector_three_data = temp_detector_three

    # Get ratio of the detectors 1 and 2
    luminosity_ratio = []
    lumi_blocks = []
    block_count1 = 0
    for run in sorted(all_detector_one_data.keys()):
        for block in range(len(all_detector_one_data.get(run))):
            block_count1 += 1
            for bcid in range(len(all_detector_one_data.get(run)[block])):
                detector_one_point = all_detector_one_data.get(run)[block][bcid]
                detector_two_point = all_detector_two_data.get(run)[block][bcid]
                # Check if the blocks are zero
                if detector_one_point != 0.0 and detector_two_point != 0.0:
                    ratio = detector_one_point / detector_two_point
                    luminosity_ratio.append(ratio)
                    lumi_blocks.append(block_count1)

    # Get ratio of the detectors 1 and 3
    luminosity_ratio_1 = []
    lumi_blocks_1 = []
    block_count2 = 0
    for run in sorted(all_detector_one_data.keys()):
        for block in range(len(all_detector_one_data.get(run))):
            block_count2 += 1
            for bcid in range(len(all_detector_one_data.get(run)[block])):
                detector_one_point = all_detector_one_data.get(run)[block][bcid]
                detector_three_point = all_detector_three_data.get(run)[block][bcid]
                # Check if the blocks are zero
                if detector_one_point != 0.0 and detector_three_point != 0.0:
                    ratio = detector_one_point / detector_three_point
                    luminosity_ratio_1.append(ratio)
                    lumi_blocks_1.append(block_count2)

    # Get percentage difference based off the first block and BCID
    first_point = luminosity_ratio[0]
    first_point_1 = luminosity_ratio_1[0]

    for index in range(len(luminosity_ratio)):
        luminosity_ratio[index] = 100 * ((luminosity_ratio[index] / first_point) - 1)

    for index in range(len(luminosity_ratio_1)):
        luminosity_ratio_1[index] = 100 * ((luminosity_ratio_1[index] / first_point_1) - 1)

    # create graph
    graph = Graph(len(lumi_blocks))
    for i, (xx, yy) in enumerate(zip(lumi_blocks, luminosity_ratio)):
        graph.SetPoint(i, float(xx), float(yy))

    # set visual attributes

    graph.markercolor = 'blue'
    graph.xaxis.SetTitle("Luminosity Block")
    graph.yaxis.SetTitle("Luminosity [Average Percent Ratio]")
    graph.xaxis.SetRangeUser(min(lumi_blocks), max(lumi_blocks))
    graph.yaxis.SetRangeUser(min(luminosity_ratio), max(luminosity_ratio))

    # plot with ROOT
    canvas = Canvas()

    graph.Draw("AP")
    canvas.Update()

    # add points from detectors 1 and 3
    # create graph
    graph1 = Graph(len(lumi_blocks_1))
    for i, (xx, yy) in enumerate(zip(lumi_blocks_1, luminosity_ratio_1)):
        graph1.SetPoint(i, float(xx), float(yy))

    # set visual attributes

    graph1.linecolor = 'white'  # Hides the lines at this time
    graph1.markercolor = 'red'
    # graph1.xaxis.SetRangeUser(min(lumi_blocks_1), max(lumi_blocks_1))
    # graph1.yaxis.SetRangeUser(min(luminosity_ratio_1), max(luminosity_ratio_1))

    graph1.Draw("P")
    canvas.Update()
    # Draw lines for different runs
    run_length = 0
    for run in sorted(all_detector_one_data.keys()):
        run_length += len(all_detector_one_data.get(run))
        line = ROOT.TLine(run_length, min(luminosity_ratio),
                          run_length, max(luminosity_ratio))
        line.Draw()
        line_label = ROOT.TText(run_length - 30, max(luminosity_ratio) - 1.5, str(run))
        line_label.SetTextAngle(90)
        line_label.SetTextSize(18)
        line_label.SetTextFont(43)
        line_label.Draw()
    label = ROOT.TText(0.7, 0.8, str(name))
    label.SetTextFont(43)
    label.SetTextSize(25)
    label.SetNDC()
    label.Draw()
    canvas.Modified()
    canvas.Update()
    wait(True)
Ejemplo n.º 45
0
            print '[%d]' % i, entry
        idx = raw_input('which one should I use? ')
        idx = int(idx)
        _, to_use = matches[idx]

    print to_use.start.str, '-->', time.str
    points.append((to_use.start.time, time.time))


#linear fit
from rootpy import ROOT
ROOT.gROOT.SetBatch()
from rootpy.plotting import Graph, F1, Canvas

fcn = F1(args.fitfunc)
graph = Graph(len(points))
for idx, point in enumerate(points):
    x, y = point
    graph.SetPoint(idx, x, y)
graph.fit(fcn)

canvas = Canvas()
graph.Draw()
canvas.SaveAs("shifts.png")

with open(args.out, 'w') as out:
    for i, entry in enumerate(entries):
        entry.remap(fcn)
        out.write('%d\n' % i)
        out.write('%s\n\n' % entry.string)
Ejemplo n.º 46
0
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator, MultipleLocator

# set the random seed
ROOT.gRandom.SetSeed(42)
np.random.seed(42)

# points
x = np.sort(np.random.random(10)) * 3500
y = np.random.random(10)

# set style for ROOT
set_style('ATLAS')

# create graph
graph = Graph(x.shape[0])
for i, (xx, yy) in enumerate(zip(x, y)):
    graph.SetPoint(i, xx, yy)

# set visual attributes
graph.linecolor = 'blue'
graph.markercolor = 'blue'
graph.xaxis.SetTitle("E_{T} [GeV]")
graph.yaxis.SetTitle("d#sigma_{jet}/dE_{T,jet} [fb/GeV]")
graph.xaxis.SetRangeUser(0, 3500)
graph.yaxis.SetRangeUser(0, 1)

# plot with ROOT
canvas = Canvas()
graph.Draw("APL")
Ejemplo n.º 47
0

	axislabel = ""
	for chunk in histogramName.split("_"):
		if "SR" in chunk or "minus" in chunk:
			continue
		axislabel += chunk
	# axislabel = " ".join(histogramName.split("_")[2:])
	axislabel = axislabel.split("<")[0].split(">")[0]




	if 'DataMain' in hists:
		if hists["DataMain"].Integral() and plotWithMPL:
			ratioplot = Graph()
			ratioplot.Divide(  hists['DataMain'], stack.sum , 'pois'  )
			# ratioplot.color = "green"
			tmpyerror,tmpyerror2 = zip(*list(ratioplot.yerr()) )
			tmpx = list(ratioplot.x())
			tmpy = list(ratioplot.y())
			tmpxy = zip(tmpx,tmpy,tmpyerror)
			# print tmpxy
			tmpxy = [tmp for tmp in tmpxy if tmp[1]!=0  ]
			# print tmpxy
			tmpx,tmpy,tmpyerror = zip(*tmpxy)
			# print tmpyerror
			# axes_ratio.errorbar(tmpx,tmpy, yerr = tmpyerror,xerr=False, emptybins=False, marker='o', lw=1, color="black")
			axes_ratio.errorbar(tmpx, tmpy,
								# list(ratioplot.y()),
								yerr = tmpyerror,
Ejemplo n.º 48
0

	# axes_ratio.set_xlabel(histogramName.replace("_"," ").replace(">","$>$").replace("<","$<$") )



	# get handles
	handles, labels = axes.get_legend_handles_labels()
	# remove the errorbars
	handles = [h[0] for h in handles]
	# use them in the legend
	axes.legend(handles, labels, loc='best',numpoints=1)


	if 'Data' in hists:
		ratioplot = Graph.divide(  Graph(hists['Data']), stack.sum , 'pois'  )
		ratioplot.color = "black"
		axes_ratio.errorbar(list(ratioplot.x()) , 
							list(ratioplot.y()), 
							yerr=[ x[0] for x in list(ratioplot.yerr() ) ] , 
							# xerr=list(ratioplot.y()), 
							fmt='o',
							color="black")

		yticks(arange(0,2.0,0.2))
		ylim([0,2])

	axes_ratio.set_ylabel('Data/MC')


def plot_all_integrated_luminosity(all_detector_one_data, all_detector_two_data, all_detector_three_data, block_length,
                                   bcid_status, background_list, style, name, detector_one_calibration,
                                   detector_two_calibration, detector_three_calibration):
    '''
    Take all the luminosity ratio for each luminosity block and multiply by the time to get the integrated luminosity
    :param all_detector_one_data: A dictionary of the run name to a list of lists of luminosity blocks
    :param all_detector_two_data: A dictionary of the run name to a list of lists of luminosity blocks
    :param block_length: A dictionary the same length as the detector arrays containing the luminosity block length (time)
    :param style: a string corresponding to a ROOT graphing style, e.g. 'ATLAS'
    :return: Plot of all the detector data on a graph chronologically according to run number
    '''
    # Set ROOT graph style
    set_style(str(style))

    # Get average value of the rate for each luminosity block
    temp_detector_one = copy.deepcopy(all_detector_one_data)
    temp_detector_two = copy.deepcopy(all_detector_two_data)
    temp_detector_three = copy.deepcopy(all_detector_three_data)
    print(sorted(all_detector_one_data.keys()))
    for run in sorted(all_detector_one_data.keys()):
        block_count = 0
        for block in range(len(all_detector_one_data.get(run)) - 1):
            del temp_detector_one.get(run)[block][:]
            del temp_detector_two.get(run)[block][:]
            del temp_detector_three.get(run)[block][:]
            block_count += 1
            detector_one_avg = 0
            one_count = 0
            detector_two_avg = 0
            two_count = 0
            detector_three_avg = 0
            three_count = 0
            for bcid in range(len(all_detector_one_data.get(run)[block])):
                # Gets the previous BCID luminosity to subtract as the background
                if run in background_list:
                    if bcid_status.get(run)[block][bcid - 1] == 0:
                        detector_one_point_background = all_detector_one_data.get(run)[block][bcid - 1]
                        detector_two_point_background = all_detector_two_data.get(run)[block][bcid - 1]
                        detector_three_point_background = all_detector_three_data.get(run)[block][bcid - 1]
                        print("BCID [N-1] Stability: " + str(bcid_status.get(run)[block][bcid - 1]))
                        print("BCID [N] Stability: " + str(bcid_status.get(run)[block][bcid]))
                        detector_one_point = -math.log(1 - all_detector_one_data.get(run)[block][bcid]) \
                                             + math.log(1 - detector_one_point_background)
                        print("Detector 1 Point: " + str(detector_one_point))
                        detector_two_point = -math.log(1 - all_detector_two_data.get(run)[block][bcid]) \
                                             + math.log(1 - detector_two_point_background)
                        print("Detector 2 Point: " + str(detector_two_point))
                        detector_three_point = -math.log(1 - all_detector_three_data.get(run)[block][bcid]) \
                                               + math.log(1 - detector_three_point_background)
                        detector_one_avg += detector_one_point
                        one_count += 1
                        detector_two_avg += detector_two_point
                        two_count += 1
                        detector_three_avg += detector_three_point
                        three_count += 1
                    else:
                        print("No empty BCID to subtract background from")
                        '''
                        print("BCID [N] Stability: " + str(bcid_status.get(run)[block][bcid]))
                        detector_one_point = -math.log(1 - all_detector_one_data.get(run)[block][bcid]) #\
                                             #+ math.log(1 - detector_one_point_background)
                        print("Detector 1 Point: " + str(detector_one_point))
                        detector_two_point = -math.log(1 - all_detector_two_data.get(run)[block][bcid]) #\
                                             #+ math.log(1 - detector_two_point_background)
                        print("Detector 2 Point: " + str(detector_two_point))
                        detector_three_point = -math.log(1 - all_detector_three_data.get(run)[block][bcid]) #\
                                               #+ math.log(1 - detector_three_point_background)
                        detector_one_avg += detector_one_point
                        one_count += 1
                        detector_two_avg += detector_two_point
                        two_count += 1
                        detector_three_avg += detector_three_point
                        three_count += 1
                        '''
                if bcid_status.get(run)[block][bcid] > 0.0:
                    detector_one_point = -math.log(1 - all_detector_one_data.get(run)[block][bcid])
                    detector_two_point = -math.log(1 - all_detector_two_data.get(run)[block][bcid])
                    detector_three_point = -math.log(1 - all_detector_three_data.get(run)[block][bcid])
                    detector_one_avg += detector_one_point
                    one_count += 1
                    detector_two_avg += detector_two_point
                    two_count += 1
                    detector_three_avg += detector_three_point
                    three_count += 1
            if one_count != 0:
                detector_one_avg = detector_one_avg / one_count
                detector_two_avg = detector_two_avg / two_count
                detector_three_avg = detector_three_avg / three_count
                temp_detector_one.get(run)[block_count - 1].append(detector_one_avg)
                temp_detector_two.get(run)[block_count - 1].append(detector_two_avg)
                temp_detector_three.get(run)[block_count - 1].append(detector_three_avg)
        # Remove the last luminosity block from each run, the one that generally spikes
        temp_detector_one[run] = temp_detector_one[run][:-10]
        temp_detector_two[run] = temp_detector_two[run][:-10]
        temp_detector_three[run] = temp_detector_three[run][:-10]
    # Reassign temp to the original lists
    all_detector_one_data = temp_detector_one
    all_detector_two_data = temp_detector_two
    all_detector_three_data = temp_detector_three

    # Get integrated luminosity of the detectors
    integrated_luminosity_one = []
    integrated_luminosity_two = []
    integrated_luminosity_three = []
    luminosity_ratio_two = []
    luminosity_ratio_three = []
    lumi_blocks = []
    block_count1 = 0
    lumi_total = 0
    lumi_total_two = 0
    lumi_total_three = 0
    # To keep track of how long each run actually is when plotting
    run_length_dict = {}
    for run in sorted(all_detector_one_data.keys()):
        run_length_dict[run] = 0
        for block in range(len(all_detector_one_data.get(run))):
            block_count1 += 1
            for bcid in range(len(all_detector_one_data.get(run)[block])):
                detector_one_point = all_detector_one_data.get(run)[block][bcid]
                detector_two_point = all_detector_two_data.get(run)[block][bcid]
                detector_three_point = all_detector_three_data.get(run)[block][bcid]
                if detector_one_point != 0.0 and detector_two_point != 0.0 and detector_three_point != 0.0:
                    # Use conversion factor
                    converted_point_one = convert_to_raw_luminosity(11.245, 20.8, detector_one_point)
                    converted_point_two = convert_to_raw_luminosity(11.245, 20.8, detector_two_point)
                    converted_point_three = convert_to_raw_luminosity(11.245, 20.8, detector_three_point)
                    ratio_one_two = converted_point_one / converted_point_two
                    ratio_one_three = converted_point_one / converted_point_three
                    luminosity_ratio_two.append(ratio_one_two)
                    luminosity_ratio_three.append(ratio_one_three)
                    length = block_length.get(run)[block][bcid]
                    lumi_total += converted_point_one * length
                    lumi_total_two += converted_point_two * length
                    lumi_total_three += converted_point_three * length
                    integrated_luminosity_one.append(lumi_total)
                    integrated_luminosity_two.append(lumi_total_two)
                    integrated_luminosity_three.append(lumi_total_three)
                    lumi_blocks.append(block_count1)
                    run_length_dict[run] += 1

    # Get percentage difference based off the first block and BCID
    first_point = detector_one_calibration / detector_two_calibration
    third_point = detector_one_calibration / detector_three_calibration

    for index in range(len(integrated_luminosity_one)):
        luminosity_ratio_two[index] = 100 * ((luminosity_ratio_two[index] / first_point) - 1)
        luminosity_ratio_three[index] = 100 * ((luminosity_ratio_three[index] / third_point) - 1)

    # create graph
    graph = Graph(len(integrated_luminosity_one))
    for i, (xx, yy) in enumerate(zip(integrated_luminosity_one, luminosity_ratio_two)):
        graph.SetPoint(i, float(xx), float(yy))

    # set visual attributes

    # Set temp list for the min and max functions
    luminosity_ratio = luminosity_ratio_two + luminosity_ratio_three
    integrated_luminosity = integrated_luminosity_one
    #integrated_luminosity = lumi_blocks

    graph.markercolor = 'blue'
    graph.yaxis.SetTitle("Luminosity Ratio [Percent]")
    graph.xaxis.SetTitle("Luminosity [Integrated]")
    graph.yaxis.SetRangeUser(min(luminosity_ratio),
                             max(luminosity_ratio))
    graph.xaxis.SetRangeUser(min(integrated_luminosity),
                             max(integrated_luminosity))

    # plot with ROOT
    canvas = Canvas()

    graph.Draw("AP")
    canvas.Update()

    # add points from detectors 1 and 3
    # create graph
    graph1 = Graph(len(integrated_luminosity))
    for i, (xx, yy) in enumerate(zip(integrated_luminosity, luminosity_ratio_three)):
        graph1.SetPoint(i, float(xx), float(yy))

    # set visual attributes

    graph1.linecolor = 'white'  # Hides the lines at this time
    graph1.markercolor = 'red'

    graph1.Draw("P")
    canvas.Update()
    # Draw lines for different runs
    run_length = 0
    total_length = 0
    num_run = 0
    print"Integrated Luminosity length: ",len(integrated_luminosity)
    for run in sorted(all_detector_one_data.keys()):
        print str(run)
        total_length += run_length_dict[run]
        print"Total Length: ",total_length
        #run_length = integrated_luminosity[total_length - 1]
        print"Run Length", run_length
        line = ROOT.TLine(run_length, min(luminosity_ratio),
                          run_length, max(luminosity_ratio))
        line.Draw()
        line_label = ROOT.TText(run_length - 30, max(luminosity_ratio) - 1.5, str(run))
        line_label.SetTextAngle(90)
        line_label.SetTextSize(18)
        line_label.SetTextFont(43)
        line_label.Draw()
    label = ROOT.TText(0.2, 0.9, str(name))
    label.SetTextFont(43)
    label.SetTextSize(25)
    label.SetNDC()
    label.Draw()
    canvas.Modified()
    canvas.Update()
    wait(True)
Ejemplo n.º 50
0
def qqplot(h1, h2, quantiles=None):
    """
    Return a Graph of a QQ plot and confidence band
    """
    if quantiles is None:
        quantiles = max(min(len(h1), len(h2)) / 2, 1)
    nq = quantiles
    xq = array('d',
               [0.] * nq)  # position where to compute the quantiles in [0,1]
    yq1 = array('d', [0.] * nq)  # array to contain the quantiles
    yq2 = array('d', [0.] * nq)  # array to contain the quantiles

    for i in xrange(nq):
        xq[i] = float(i + 1) / nq

    h1.GetQuantiles(nq, yq1, xq)
    h2.GetQuantiles(nq, yq2, xq)

    xq_plus = array('d', [0.] * nq)
    xq_minus = array('d', [0.] * nq)
    yq2_plus = array('d', [0.] * nq)
    yq2_minus = array('d', [0.] * nq)
    """
    KS_cv: KS critical value

               1.36
    KS_cv = -----------
             sqrt( N )

    Where 1.36 is for alpha = 0.05 (confidence level 1-5%=95%, about 2 sigma)

    For 1 sigma (alpha=0.32, CL=68%), the value in the nominator is 0.9561,
    it is gotten by GetCriticalValue(1, 1 - 0.68).

    NOTE:
    * For 1-sample KS test (data and theoretic), N should be n
    * For 2-sample KS test (2 data set), N should be sqrt(m*n/(m+n))! Here is the case
      m or n (size of samples) should be effective size for a histogram
    * Critical value here is valid for only for sample size >= 80 (some references say 35)
      which means, for example, for a unweighted histogram, it must have more than 80 (or 35)
      entries filled and then confidence band is reliable.
    """

    esum1 = effective_sample_size(h1)
    esum2 = effective_sample_size(h2)

    # one sigma band
    KS_cv = (critical_value(1, 1 - 0.68) / sqrt(
        (esum1 * esum2) / (esum1 + esum2)))

    for i in xrange(nq):
        xq_plus[i] = float(xq[i] + KS_cv)  # upper limit
        xq_minus[i] = float(xq[i] - KS_cv)  # lower limit

    h2.GetQuantiles(nq, yq2_plus, xq_plus)
    h2.GetQuantiles(nq, yq2_minus, xq_minus)

    yq2_err_plus = array('d', [0.] * nq)
    yq2_err_minus = array('d', [0.] * nq)
    for i in xrange(nq):
        yq2_err_plus[i] = yq2_plus[i] - yq2[i]
        yq2_err_minus[i] = yq2[i] - yq2_minus[i]

    #forget the last point, so number of points: (nq-1)
    gr = Graph(nq - 1)
    for i in xrange(nq - 1):
        gr[i] = (yq1[i], yq2[i])
        # confidence level band
        gr.SetPointEYlow(i, yq2_err_minus[i])
        gr.SetPointEYhigh(i, yq2_err_plus[i])

    return gr
Ejemplo n.º 51
0
def pvalue_plot(poi, pvalues, pad=None,
                xtitle='X', ytitle='P_{0}',
                linestyle=None,
                linecolor=None,
                yrange=None,
                verbose=False):
    """
    Draw a pvalue plot

    Parameters
    ----------
    poi : list
        List of POI values tested
    pvalues : list
        List of p-values or list of lists of p-values to overlay
        multiple p-value curves
    pad : Canvas or Pad, optional (default=None)
        Pad to draw onto. Create new pad if None.
    xtitle : str, optional (default='X')
        The x-axis label (POI name)
    ytitle : str, optional (default='P_{0}')
        The y-axis label
    linestyle : str or list, optional (default=None)
        Line style for the p-value graph or a list of linestyles for
        multiple p-value graphs.
    linecolor : str or list, optional (default=None)
        Line color for the p-value graph or a list of linestyles for
        multiple p-value graphs.

    Returns
    -------
    pad : Canvas
        The pad.
    graphs : list of Graph
        The p-value graphs

    """
    if not pvalues:
        raise ValueError("pvalues is empty")
    if not poi:
        raise ValueError("poi is empty")
    # determine if pvalues is list or list of lists
    if not isinstance(pvalues[0], (list, tuple)):
        pvalues = [pvalues]
    if linecolor is not None:
        if not isinstance(linecolor, list):
            linecolor = [linecolor]
        linecolor = cycle(linecolor)
    if linestyle is not None:
        if not isinstance(linestyle, list):
            linestyle = [linestyle]
        linestyle = cycle(linestyle)

    with preserve_current_canvas():
        if pad is None:
            pad = Canvas()
        pad.cd()
        pad.SetLogy()

        # create the axis
        min_poi, max_poi = min(poi), max(poi)
        haxis = Hist(1000, min_poi, max_poi)
        xaxis = haxis.xaxis
        yaxis = haxis.yaxis
        xaxis.SetRangeUser(min_poi, max_poi)
        haxis.Draw('AXIS')

        min_pvalue = float('inf')
        graphs = []
        for ipv, pv in enumerate(pvalues):
            graph = Graph(len(poi), linestyle='dashed',
                          drawstyle='L', linewidth=2)
            for idx, (point, pvalue) in enumerate(zip(poi, pv)):
                graph.SetPoint(idx, point, pvalue)
            if linestyle is not None:
                graph.linestyle = linestyle.next()
            if linecolor is not None:
                graph.linecolor = linecolor.next()
            graphs.append(graph)
            curr_min_pvalue = min(pv)
            if curr_min_pvalue < min_pvalue:
                min_pvalue = curr_min_pvalue

        if verbose:
            for graph in graphs:
                log.info(['{0:1.1f}'.format(xval) for xval in list(graph.x())])
                log.info(['{0:0.3f}'.format(yval) for yval in list(graph.y())])

        # automatically handles axis limits
        axes, bounds = draw(graphs, pad=pad, same=True, logy=True,
             xtitle=xtitle, ytitle=ytitle,
             xaxis=xaxis, yaxis=yaxis, ypadding=(0.2, 0.1),
             logy_crop_value=1E-300)

        if yrange is not None:
            xaxis, yaxis = axes
            yaxis.SetLimits(*yrange)
            yaxis.SetRangeUser(*yrange)
            min_pvalue = yrange[0]

        # draw sigma levels up to minimum of pvalues
        line = Line()
        line.SetLineStyle(2)
        line.SetLineColor(2)
        latex = ROOT.TLatex()
        latex.SetNDC(False)
        latex.SetTextSize(20)
        latex.SetTextColor(2)
        sigma = 0
        while True:
            pvalue = gaussian_cdf_c(sigma)
            if pvalue < min_pvalue:
                break
            keepalive(pad, latex.DrawLatex(max_poi, pvalue, " {0}#sigma".format(sigma)))
            keepalive(pad, line.DrawLine(min_poi, pvalue, max_poi, pvalue))
            sigma += 1

        pad.RedrawAxis()
        pad.Update()
    return pad, graphs
Ejemplo n.º 52
0
def test_divide():
    Graph.divide(Graph(Hist(10, 0, 1).FillRandom('gaus')),
                 Hist(10, 0, 1).FillRandom('gaus'), 'pois')
Ejemplo n.º 53
0
def main(args):

    fileList = glob('/data/nawoods/lepsForSIP/*.root')
    files = {int(f.split('_M')[1].split('.')[0]) : f for f in fileList}

    checkers = {m : MuonSIPChecker('m={}'.format(m), [f]) for m,f in files.iteritems()}
    sipRMS = Graph(len(fileList), type='errors')
    sipHists = []
    dxyRMS = Graph(len(fileList), type='errors')
    dxyHists = []
    dzRMS = Graph(len(fileList), type='errors')
    dzHists = []

    ipHists = []
    ipErrHists = []

    fracFailing = Graph(len(fileList), type='errors')
    for i, m in enumerate(sorted(files.keys())):

        checkers[m].processSample()

        checkers[m].hists['sip'].Sumw2()
        sipRMS.SetPoint(i, float(m), checkers[m].hists['sip'].GetRMS())
        sipRMS.SetPointError(i, 0., checkers[m].hists['sip'].GetRMSError())
        sipHists.append(checkers[m].hists['sip'])
        sipHists[-1].color = getColor(m)
        sipHists[-1].title = "m_{{H}} = {}".format(m)
        sipHists[-1].drawstyle = 'hist'
        sipHists[-1].legendstyle = 'L'
        sipHists[-1].linewidth = 2
        sipHists[-1].scale(1./sipHists[-1].integral())

        ipHists.append(checkers[m].hists['ip'])
        ipHists[-1].color = getColor(m)
        ipHists[-1].title = "m_{{H}} = {}".format(m)
        ipHists[-1].drawstyle = 'hist'
        ipHists[-1].legendstyle = 'L'
        ipHists[-1].linewidth = 2
        ipHists[-1].scale(1./ipHists[-1].integral())

        ipErrHists.append(checkers[m].hists['ipErr'])
        ipErrHists[-1].color = getColor(m)
        ipErrHists[-1].title = "m_{{H}} = {}".format(m)
        ipErrHists[-1].drawstyle = 'hist'
        ipErrHists[-1].legendstyle = 'L'
        ipErrHists[-1].linewidth = 2
        ipErrHists[-1].scale(1./ipErrHists[-1].integral())

        checkers[m].hists['dxy'].Sumw2()
        dxyRMS.SetPoint(i, float(m), checkers[m].hists['dxy'].GetRMS())
        dxyRMS.SetPointError(i, 0., checkers[m].hists['dxy'].GetRMSError())
        dxyHists.append(checkers[m].hists['dxy'])
        dxyHists[-1].color = getColor(m)
        dxyHists[-1].title = "m_{{H}} = {}".format(m)
        dxyHists[-1].drawstyle = 'hist'
        dxyHists[-1].legendstyle = 'L'
        dxyHists[-1].linewidth = 2
        dxyHists[-1].scale(1./dxyHists[-1].integral())
        
        checkers[m].hists['dz'].Sumw2()
        dzRMS.SetPoint(i, float(m), checkers[m].hists['dz'].GetRMS())
        dzRMS.SetPointError(i, 0., checkers[m].hists['dz'].GetRMSError())
        dzHists.append(checkers[m].hists['dz'])
        dzHists[-1].color = getColor(m)
        dzHists[-1].title = "m_{{H}} = {}".format(m)
        dzHists[-1].drawstyle = 'hist'
        dzHists[-1].legendstyle = 'L'
        dzHists[-1].linewidth = 2
        dzHists[-1].scale(1./dzHists[-1].integral())

        fracFailing.SetPoint(i, float(m),
                             1. - float(checkers[m].nPassSIP) / checkers[m].nTot)

    
    cSIP = Canvas(1000,1000)
    draw(sipHists, cSIP, xtitle='#frac{IP_{3D}}{#sigma_{IP_{3D}}}',
         ytitle='arb.')
    legSIP = Legend(sipHists, cSIP, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04)
    legSIP.Draw("same")
    cSIP.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/sips.png')
    cSIPLog = Canvas(1000,1000)
    draw(sipHists, cSIPLog, xtitle='#frac{IP_{3D}}{#sigma_{IP_{3D}}}',
         ytitle='arb.', logy=True)
    legSIPLog = Legend(sipHists, cSIPLog, textsize=.027, entrysep=0.012, leftmargin=0.6, entryheight=0.03)
    legSIPLog.Draw("same")
    cSIPLog.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/sipsLog.png')
    
    cSIPRMS = Canvas(1000, 1000)
    sipRMS.color = 'b'
    sipRMS.drawstyle = 'PE'
    sipRMS.legendstyle = 'PE'
    draw(sipRMS, cSIPRMS, xtitle="m_{H}", ytitle="RMS(SIP_{3D})", xlimits=(0.,2600.))
    cSIPRMS.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/sipRMS.png')
    
    cIP = Canvas(1000,1000)
    draw(ipHists, cIP, xtitle='IP_{3D}',
         ytitle='arb.')
    legIP = Legend(ipHists, cIP, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04)
    legIP.Draw("same")
    cIP.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/ips.png')
    cIPLog = Canvas(1000,1000)
    draw(ipHists, cIPLog, xtitle='#frac{IP_{3D}}{#sigma_{IP_{3D}}}',
         ytitle='arb.', logy=True)
    legIPLog = Legend(ipHists, cIPLog, textsize=.027, entrysep=0.012, leftmargin=0.6, entryheight=0.03)
    legIPLog.Draw("same")
    cIPLog.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/ipsLog.png')
    
    cIPErr = Canvas(1000,1000)
    draw(ipErrHists, cIPErr, xtitle='#sigma_{IP_{3D}}',
         ytitle='arb.')
    legIPErr = Legend(ipErrHists, cIPErr, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04)
    legIPErr.Draw("same")
    cIPErr.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/ipErrs.png')
    cIPErrLog = Canvas(1000,1000)
    draw(ipErrHists, cIPErrLog, xtitle='#sigma_{IP_{3D}}',
         ytitle='arb.', logy=True)
    legIPErrLog = Legend(ipErrHists, cIPErrLog, textsize=.027, entrysep=0.012, leftmargin=0.6, entryheight=0.03)
    legIPErrLog.Draw("same")
    cIPErrLog.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/ipErrsLog.png')
    
    cFail = Canvas(1000, 1000)
    fracFailing.color = 'b'
    fracFailing.drawstyle = 'PE'
    fracFailing.legendstyle = 'PE'
    draw(fracFailing, cSIPRMS, xtitle="m_{H}", ytitle="Fraction failing SIP", xlimits=(0.,2600.))
    cSIPRMS.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/fracFailing.png')
    
    cDXY = Canvas(1000,1000)
    draw(dxyHists, cDXY, xtitle='#Delta_{xy}',
         ytitle='arb.')
    legDXY = Legend(dxyHists, cDXY, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04)
    legDXY.Draw("same")
    cDXY.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/dxys.png')

    cDXYRMS = Canvas(1000, 1000)
    dxyRMS.color = 'b'
    dxyRMS.drawstyle = 'PE'
    dxyRMS.legendstyle = 'PE'
    draw(dxyRMS, cDXYRMS, xtitle="m_{H}", ytitle="RMS(#Delta_{xy})", xlimits=(0.,2600.))
    cDXYRMS.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/dxyRMS.png')
    
    cDZ = Canvas(1000,1000)
    draw(dzHists, cDZ, xtitle='#Delta_{z}',
         ytitle='arb.')
    legDZ = Legend(dzHists, cDZ, textsize=.03, entrysep=0.015, leftmargin=0.6, entryheight=0.04)
    legDZ.Draw("same")
    cDZ.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/dzs.png')

    cDZRMS = Canvas(1000, 1000)
    dzRMS.color = 'b'
    dzRMS.drawstyle = 'PE'
    dzRMS.legendstyle = 'PE'
    draw(dzRMS, cDZRMS, xtitle="m_{H}", ytitle="RMS(#Delta_{z})", xlimits=(0.,2600.))
    cDZRMS.Print('/afs/cern.ch/user/n/nawoods/www/sipStudy/dzRMS.png')
Ejemplo n.º 54
0
    def __init__(self, filename):
        self.parameters = {}
        sys_name = re.search('Output/(\w+)/', filename).group(1)
        file_list = glob.glob(filename + '/*.txt')

        # Get parameter names and corresponding line numbers
        with open(file_list[1]) as f:
            lines = [line.rstrip('\n') for line in f]
            candidates = [[item.split()[0], i] for i, item in enumerate(lines)
                          if re.search(u"\+\/\-.*|Chi2", item)]
        for cand in candidates:
            self.parameters[cand[0]] = [cand[1], []]

        print(self.parameters)

        for ifile in file_list:
            [iy, ipt] = re.search(r"y(\d)_pt(\d+)", ifile).groups()
            iy = int(iy)
            ipt = int(ipt)

            with open(ifile) as ftmp:
                lines = [line.rstrip('\n') for line in ftmp]
                mean_pt = float(lines[0].split()[1])

                for par in self.parameters:
                    graphs = self.parameters[par][1]
                    parno = self.parameters[par][0]

                    try:
                        graph = next(igraph for igraph in graphs
                                     if igraph.name == 'g_{0}_y{1}_{2}'.format(
                                         sys_name, iy, par))
                    except StopIteration:
                        graph = Graph(len(bins_pt) - 1,
                                      name='g_{0}_y{1}_{2}'.format(
                                          sys_name, iy, par),
                                      type='asymm')
                        graphs.append(graph)

                    try:
                        data = lines[parno].split()
                    except IndexError:
                        continue

                    if par == 'Chi2/nDOF:':
                        par_value = float(data[1])
                        par_error = [0, 0]
                    else:
                        par_value = float(data[2])
                        if (data[3] == "+/-"):
                            if (data[4][0] == "("):
                                par_error = [
                                    float(data[4][2:-1]),
                                    float(data[5][:-1])
                                ]
                            else:
                                if float(data[4]) > 0.3 * par_value:
                                    par_error = [
                                        0.3 * par_value, 0.3 * par_value
                                    ]
                                else:
                                    par_error = [
                                        float(data[4]),
                                        float(data[4])
                                    ]
                        else:
                            par_error = [999, 999]

                    graph[ipt] = (mean_pt, par_value)
                    graph[ipt].x.error_hi = bins_pt[ipt + 1] - mean_pt
                    graph[ipt].x.error_low = -bins_pt[ipt] + mean_pt
                    graph[ipt].y.error_low = par_error[0]
                    graph[ipt].y.error_hi = par_error[1]

        for par in self.parameters:
            graphs = self.parameters[par][1]
            for graph, color in zip(graphs, colors):
                graph.color = color
Ejemplo n.º 55
0
    'mmmm' : lambda row: getattr(row, 'm3_m4_%s'%massVar),
}

def selectLowMass(row):
    return row.MassDREtFSR < 110.

for ana in ['full', 'z4l']:
    
    if ana == 'z4l':
        selector = selectLowMass
    else:
        selector = lambda *args: True

    g = {}
    for ch in plotter.channels:
        g[ch] = Graph(plotter.ntuples['data']['data'][ch].GetEntries(), title=titles[ch])
        g[ch].color = colors[ch]
        g[ch].markerstyle = markers[ch]
        g[ch].drawstyle = 'P'
        g[ch].SetMarkerSize(g[ch].GetMarkerSize()*1.5)
        if ch == 'mmmm':
            g[ch].SetMarkerSize(g[ch].GetMarkerSize()*1.18)
    
    for ch in plotter.channels:
        #nWithFSR = 0
        for i, row in enumerate(plotter.ntuples['data']['data'][ch]):
            if selector(row):
                g[ch].SetPoint(i, getMZ1[ch](row), getMZ2[ch](row))
            #if row.Mass != row.MassDREtFSR:
            #    nWithFSR += 1
        #print "%s: %d / %d"%(ch, nWithFSR, int(plotter.ntuples['data']['data'][ch].GetEntries()))
Ejemplo n.º 56
0
def main():
    print('Number of arguments: ', len(sys.argv), 'arguments.')
    print('Argument list:', str(sys.argv))
    filename = sys.argv[1]
    if len(sys.argv) > 2:
        fileData = sys.argv[2]
    else:
        fileData = None
    print("Input file: ")
    print(filename)
    Njets = 9

    if fileData is not None:
        fD = root_open(fileData, 'read')
        iS = 0
        gGausRMSData = fD.Get("gGausRMS{:02d}".format(iS))
        gGausRMSerrData = fD.Get("gGausRMS{:02d}_Systematics".format(iS))
        gGausYieldData = fD.Get("gGausYield{:02d}".format(iS))
        gGausYielderrData = fD.Get("gGausYield{:02d}_Systematics".format(iS))
        gGammaRMSData = fD.Get("gGammaRMS{:02d}".format(iS))
        gGammaRMSerrData = fD.Get("gGammaRMS{:02d}_Systematics".format(iS))
        gGammaYieldData = fD.Get("gGammaYield{:02d}".format(iS))
        gGammaYielderrData = fD.Get("gGammaYield{:02d}_Systematics".format(iS))

    with root_open(filename, 'read') as f:
        FullJets_jT = []
        colors = (1, 2, 3)
        for iF, c in zip((8, 6, 7), colors):
            jT = [
                f.get(
                    'AliJJetJtTask/AliJJetJtHistManager/JetConeJtWeightBin/JetConeJtWeightBinNFin{:02d}JetPt{:02d}'
                    .format(iF, ij)) for ij in range(Njets)
            ]
            bgJt = [
                f.get(
                    'AliJJetJtTask/AliJJetJtHistManager/BgJtWeightBin/BgJtWeightBinNFin{:02d}JetPt{:02d}'
                    .format(iF, ij)) for ij in range(Njets)
            ]
            jetPtBin = [
                f.get(
                    'AliJJetJtTask/AliJJetJtHistManager/JetPtBin/JetPtBinNFin{:02d}JetPt{:02d}'
                    .format(iF, ij)) for ij in range(Njets)
            ]
            nJets = [h.Integral() for h in jetPtBin]
            nBgs = [
                f.get(
                    'AliJJetJtTask/AliJJetJtHistManager/BgTrkNumberBin/BgTrkNumberBinNFin{:02d}JetPt{:02d}'
                    .format(iF, ij)).Integral() for ij in range(Njets)
            ]
            FullJets_jT.append(jT)
            #FullJets_bgJt.append(bgJt)
            #FullJets_jetPtBin.append(jetPtBin)
            for j, b, nj, nb in zip(jT, bgJt, nJets, nBgs):
                j.Rebin(2)
                b.Rebin(2)
                j.Scale(1.0 / nj, "width")
                b.Scale(1.0 / nb, "width")
                j.SetMarkerColor(c)
                b.SetMarkerColor(c)
                j.Add(b, -1.0)

        jetPt = [
            (int(
                re.search(r'p_{T,jet} : ([\d]*)\.[\d] - ([\d]*).[\d]*',
                          h.GetTitle(), re.M | re.I).group(1)),
             int(
                 re.search(r'p_{T,jet} : ([\d]*)\.[\d] - ([\d]*).[\d]*',
                           h.GetTitle(), re.M | re.I).group(2)))
            for h in FullJets_jT[0]
        ]  #Use regular expressions to extract jet pT range from histogram titles
        jetPtCenter = array('d', [(a + b) / 2.0 for a, b in jetPt])
        jetPtErrors = array('d', [(b - a) / 2.0 for a, b in jetPt])
        print(jetPt, type(jetPt))
        print(jetPtCenter, type(jetPtCenter))
        print(jetPtErrors, type(jetPtErrors))

        FullJets_fit = []
        FullJets_parameters = []
        FullJets_gausRMS = []
        FullJets_gammaRMS = []
        FullJets_gausYield = []
        FullJets_gammaYield = []
        for jT in FullJets_jT:
            gausRMS = []
            gammaRMS = []
            gausRMSe = []
            gammaRMSe = []
            gausYield = []
            gammaYield = []
            gausYielde = []
            gammaYielde = []
            fits = []
            parameters = []
            for h, i in zip(jT, range(Njets)):
                fit, d = fitJtHisto(h, '', 1, i, 8)
                fits.append(fit)
                parameters.append(d)
                gausRMS.append(d['gausRMS'])
                gausRMSe.append(d['gausRMSe'])
                gammaRMS.append(d['gammaRMS'])
                gammaRMSe.append(d['gammaRMSe'])
                gausYield.append(d['gausYield'])
                gausYielde.append(d['gausYielde'])
                gammaYield.append(d['gammaYield'])
                gammaYielde.append(d['gammaYielde'])

            gausRMSg = Graph(len(gausRMS) - 2)
            gammaRMSg = Graph(len(gammaRMS) - 2)
            gausYieldg = Graph(len(gausYield) - 2)
            gammaYieldg = Graph(len(gammaYield) - 2)
            for h, he, g in zip((gausYield, gammaYield),
                                (gausYielde, gammaYielde),
                                (gausYieldg, gammaYieldg)):
                for x, xe, a, e, i in zip(jetPtCenter[2:],
                                          jetPtErrors[2:], h[2:], he[2:],
                                          range(len(gausRMS) - 2)):
                    g.SetPoint(i, x, a)
                    g.SetPointError(i, xe, xe, e, e)

            for a, b, c, d, e, f, i in zip(gausRMS[2:], gammaRMS[2:],
                                           gausRMSe[2:], gammaRMSe[2:],
                                           jetPtCenter[2:], jetPtErrors[2:],
                                           range(len(gausRMS) - 2)):
                gausRMSg.SetPoint(i, e, a)
                gausRMSg.SetPointError(i, f, f, c, c)
                gammaRMSg.SetPoint(i, e, b)
                gammaRMSg.SetPointError(i, f, f, d, d)

            FullJets_gausRMS.append(gausRMSg)
            FullJets_gammaRMS.append(gammaRMSg)
            FullJets_gausYield.append(gausYieldg)
            FullJets_gammaYield.append(gammaYieldg)
            FullJets_fit.append(fits)
            FullJets_parameters.append(parameters)

        if fileData is not None:
            FullJets_gausRMS.append(gGausRMSData)
            FullJets_gammaRMS.append(gGammaRMSData)
            FullJets_gausYield.append(gGausYieldData)
            FullJets_gammaYield.append(gGammaYieldData)

        fig, axs = plt.subplots(2, 1, figsize=(7, 7))
        ax = axs[0]
        ax.set_xlim([0.1, 15])
        ax.set_ylim([5e-6, 2e3])
        ax.set_xlabel(r'$j_{T}\left(GeV/c\right)$', fontsize=labelsize)
        ax.set_ylabel(r'$\frac{1}{N_{jets}}\frac{dN}{j_{T}dj_{T}}$',
                      fontsize=labelsize)
        ratios = []
        ax.set_xscale('log')
        ax.set_yscale('log')
        for h, c, R in zip(FullJets_jT, (0, 1, 2, 3), Rs):
            h[6].SetMarkerColor(colors[c])
            h[6].SetMarkerStyle(styles[c])
            h[6].SetLineColor(colors[c])
            ratio = h[6].Clone()
            ratio.Divide(FullJets_jT[1][6])
            ratios.append(ratio)
            plot = rplt.errorbar(h[6],
                                 xerr=False,
                                 emptybins=False,
                                 label='R = {:.1f}'.format(R),
                                 axes=ax,
                                 fmt='+')
            line = plot.get_children()[0]
            line.set_markersize(mSize)
            if (styles[c] > 23):
                line.set_markerfacecolor('none')
                #line.set_markeredgecolor(color)
                line.set_color(colors[c])

        ax.text(
            0.15,
            0.001,
            'Pythia \n Full Jets\n' + r'Anti-$k_T$' + '\n' +
            r'$p_{{T,\mathrm{{jet}}}}$: {:02d}-{:02d} GeV/c'.format(60, 80),
            fontsize=10)
        handles, labels = ax.get_legend_handles_labels()
        handles = [
            container.ErrorbarContainer(h, has_xerr=False, has_yerr=True)
            if isinstance(h, container.ErrorbarContainer) else h
            for h in handles
        ]
        ax.legend(handles,
                  labels,
                  loc='upper right',
                  numpoints=1,
                  prop={'family': 'monospace'})
        #ax.legend(loc = 'upper right')
        ax.set_xlim([0.1, 15])
        ax.set_ylim([5e-6, 2e3])
        ax.grid(True)

        ax = axs[1]
        ax.grid(True)
        ax.set_xlabel(r'$j_{T}\left[GeV\right]$', fontsize=labelsize)
        ax.set_ylabel('Ratio',
                      fontsize=labelsize)  #Add x-axis labels for bottom row
        for ratio, c in zip(ratios, (0, 1, 2, 3)):
            #ratio.SetMarkerColor(c)
            #ratio.SetMarkerStyle(24)
            plot = rplt.errorbar(ratio, xerr=False, emptybins=False, axes=ax)
            line = plot.get_children()[0]
            line.set_markersize(mSize)
            if (styles[c] > 23):
                line.set_markerfacecolor('none')
                #line.set_markeredgecolor(color)
                line.set_color(colors[c])
        ax.set_xlim([0.1, 15])
        ax.set_ylim([0.1, 3])
        ax.set_xscale('log')
        plt.tight_layout()
        plt.subplots_adjust(wspace=0,
                            hspace=0)  #Set space between subfigures to 0
        plt.savefig("PythonFigures/RcomparisonSignalPt6080.pdf".format(file),
                    format='pdf')  #Save figure

        plt.show()  #Draw figure on screen

        drawWithErrors2Combined(FullJets_gausRMS,
                                FullJets_gammaRMS,
                                15,
                                500,
                                1,
                                0,
                                1.65,
                                0,
                                r'jet $p_T (GeV/c)$',
                                r'$\sqrt{\left<j_T^2\right>}$',
                                'Pythia',
                                'PythonFigures/RcomparisonRMS',
                                separate=True)
        return
        drawWithErrors2Combined(
            FullJets_gausYield,
            FullJets_gammaYield,
            15,
            500,
            1,
            0,
            10,
            0,
            r'jet $p_T$',
            r'Yield',
            'Pythia',
            'PythonFigures/RcomparisonYield',
        )

        ratios = []
        for hists in FullJets_jT:
            if hists is not None:
                ratio = []
                for h, true in zip(hists, FullJets_jT[1]):
                    h2 = h.Clone()
                    h2.Divide(true)
                    ratio.append(h2)
            else:
                ratio = None
            ratios.append(ratio)

        fig, axs = defs.makegrid(4,
                                 2,
                                 xlog=True,
                                 ylog=False,
                                 d=d,
                                 shareY=False)
        axs = axs.reshape(8)
        axs[4].set_ylabel("Ratio to R = 0.4", fontsize=labelsize)
        axs[7].set_ylabel("Ratio to R = 0.4", fontsize=labelsize)

        axs[1].text(
            0.02,
            0.005, 'Pythia\n'
            r'pPb $\sqrt{s_{NN}} = 5.02 \mathrm{TeV}$'
            '\n Full jets \n'
            r'Anti-$k_T$',
            fontsize=7
        )  #Add text to second subfigure, first parameters are coordinates in the drawn scale/units
        for hists, R in zip(FullJets_jT, Rs):
            for jT, ax, i, pT in zip(hists[2:], axs[0:4], range(0, 9),
                                     jetPt[2:]):
                rplt.errorbar(jT,
                              emptybins=False,
                              xerr=False,
                              label="R = {:.1f}".format(R),
                              axes=ax,
                              fmt='o')  #Plot jT histogram,
                ax.text(
                    0.3, 1e2, r'$p_{{T,\mathrm{{jet}}}}$:'
                    '\n'
                    r' {:02d}-{:02d} GeV'.format(pT[0], pT[1]))
                ax.set_xlim([0.01, 20])  #Set x-axis limits
                ax.set_ylim([5e-4, 2e3])  #Set y-axis limits
                ax.set_yscale('log')
                ax.grid(True)

        for ratio in ratios:
            for r, ax in zip(ratio[2:], axs[4:8]):
                rplt.errorbar(r, axes=ax, emptybins=False, xerr=False)
                ax.set_xlim([0.01, 20])
                ax.set_ylim([0.1, 3])
                ax.grid(True)

        axs[0].legend(loc='lower left')

        plt.savefig("PythonFigures/RcomparisonSignal.pdf",
                    format='pdf')  #Save figure
        plt.show()  #Draw figure on screen
def tau_from_scan( unfoldingObject, regularisation_settings ):
    variable = regularisation_settings.variable

    # Plots that get outputted by the scan
    lCurve = TGraph()
    scanResult = TSpline3()
    d = 'signal'
    a = ''

    # Parameters of scan
    # Number of points to scan, and min/max tau
    nScan = 200
    minTau = 1.E-6
    maxTau = 1.E-0

    if variable == 'abs_lepton_eta':
        minTau = 1.E-8
        maxTau = 1.E-3
    elif variable == 'lepton_pt':
        minTau = 1.E-6
        maxTau = 1.E-2
    elif variable == 'NJets':
        minTau = 1.E-6
        maxTau = 1.E-2

    # Scan is performed here    
    iBest = unfoldingObject.ScanTau(nScan, minTau, maxTau, scanResult, TUnfoldDensity.kEScanTauRhoSquareAvg);

    # Plot the scan result
    # Correlation as function of log tau
    canvas = TCanvas()


    # Add point corresponding to optimum tau
    t = Double(0)
    x = Double(0)
    scanResult.GetKnot(iBest,t,x);

    bestTau = Graph(1)
    bestTau.SetPoint(1,t,x)
    bestTau.markercolor = 'red'
    bestTau.SetMarkerSize(1.5)
    bestTau.SetMarkerStyle(34)
    bestTau.GetXaxis().SetTitle('log(#tau)')
    bestTau.GetYaxis().SetTitle('Average global correlation coefficient squared')
    bestTau.SetTitle('{0} {1}'.format(variable, regularisation_settings.channel))
    bestTau.GetYaxis().SetRangeUser(x*0.8,0.95)
    bestTau.GetXaxis().SetLimits(log(minTau, 10), log(maxTau, 10))

    bestTau.Draw('AP')

    scanResult.SetMarkerColor(600)
    scanResult.SetMarkerSize(0.5)
    scanResult.SetMarkerStyle(20)
    scanResult.Draw('LPSAME')
    # Redraw to get it to appear on top of TSpline3...
    bestTau.Draw('PSAME')



    # Write to file
    output_dir = regularisation_settings.output_folder
    make_folder_if_not_exists(output_dir)
    canvas.SaveAs(output_dir + '/{0}_{1}.png'.format(variable, regularisation_settings.channel) )

    return unfoldingObject.GetTau()
def plot_all_luminosity_block_ratio(all_detector_one_data, all_detector_two_data, background_list, status_data, style, name,
                                    detector_one_calibration, detector_two_calibration):
    '''

    :param all_detector_one_data: A dictionary of the run name to a list of lists of luminosity blocks
    :param all_detector_two_data: A dictionary of the run name to a list of lists of luminosity blocks
    :param style: a string corresponding to a ROOT graphing style, e.g. 'ATLAS'
    :return: Plot of all the detector data on a graph chronologically according to run number
    '''
    # Set ROOT graph style
    set_style(str(style))

    # Get average value of the rate for each luminosity block
    temp_detector_one = copy.deepcopy(all_detector_one_data)
    temp_detector_two = copy.deepcopy(all_detector_two_data)
    # print(sorted(all_detector_one_data.keys()))
    for run in sorted(all_detector_one_data.keys()):
        block_count = 0
        print"Starting run", run
        for block in range(len(all_detector_one_data.get(run)) - 1):
            del temp_detector_one.get(run)[block][:]
            del temp_detector_two.get(run)[block][:]
            block_count += 1
            detector_one_avg = 0
            one_count = 0
            detector_two_avg = 0
            two_count = 0
            for bcid in range(len(all_detector_one_data.get(run)[block])):
                # Gets the previous BCID luminosity to subtract as the background
                if run in background_list:
                    if status_data.get(run)[block][bcid - 1] <= 0.0:
                        detector_one_point_background = all_detector_one_data.get(run)[block][bcid - 1]
                        detector_two_point_background = all_detector_two_data.get(run)[block][bcid - 1]
                        print("BCID [N-1] Stability: " + str(status_data.get(run)[block][bcid - 1]))
                    else:
                        print("No empty BCID to subtract background from")
                    if all_detector_one_data.get(run)[block][bcid] < 1 and all_detector_two_data.get(run)[block][bcid] < 1:
                        print("BCID [N] Stability: " + str(status_data.get(run)[block][bcid]))
                        detector_one_point = -math.log(1 - all_detector_one_data.get(run)[block][bcid]) \
                                             + math.log(1 - detector_one_point_background)
                        print("Detector 1 Point: " + str(detector_one_point))
                        detector_two_point = -math.log(1 - all_detector_two_data.get(run)[block][bcid]) \
                                             + math.log(1 - detector_two_point_background)
                        print("Detector 2 Point: " + str(detector_two_point))
                        detector_one_avg += detector_one_point
                        one_count += 1
                        detector_two_avg += detector_two_point
                        two_count += 1
                else:
                    #print("         RUN:     " + str(run) + "                    END")
                    #print(all_detector_one_data.get(run)[block][bcid])
                    # Checking if the status is stable
                    if 1 > all_detector_one_data.get(run)[block][bcid] > 0.0 and 1 > all_detector_two_data.get(run)[block][bcid] > 0.0\
                            and status_data.get(run)[block][bcid] > 0.0:
                        #print("Value of Block, BCID: " + str(block) + " " + str(bcid) + " " + str(all_detector_one_data.get(run)[block][bcid]))
                        detector_one_point = -math.log(1 - all_detector_one_data.get(run)[block][bcid])
                        detector_two_point = -math.log(1 - all_detector_two_data.get(run)[block][bcid])
                        detector_one_avg += detector_one_point
                        one_count += 1
                        detector_two_avg += detector_two_point
                        two_count += 1
            if one_count != 0:
                detector_one_avg = detector_one_avg / one_count
                detector_two_avg = detector_two_avg / two_count
                if run == "286282":
                    print("One Average: " + str(detector_one_avg))
                temp_detector_one.get(run)[block_count - 1].append(detector_one_avg)
                temp_detector_two.get(run)[block_count - 1].append(detector_two_avg)
        # Remove the last luminosity block from each run, the one that generally spikes
        temp_detector_one[run] = temp_detector_one[run][:-10]
        temp_detector_two[run] = temp_detector_two[run][:-10]
    # Reassign temp to the original lists
    all_detector_one_data = temp_detector_one
    all_detector_two_data = temp_detector_two

    # Get ratio of the detectors
    luminosity_ratio = []
    lumi_blocks = []
    block_count1 = 0
    for run in sorted(all_detector_one_data.keys()):
        for block in range(len(all_detector_one_data.get(run))):
            block_count1 += 1
            for bcid in range(len(all_detector_one_data.get(run)[block])):
                detector_one_point = all_detector_one_data.get(run)[block][bcid]
                detector_two_point = all_detector_two_data.get(run)[block][bcid]
                # Check if the blocks are zero
                if detector_one_point != 0.0 or detector_two_point != 0.0:
                    ratio = detector_one_point / detector_two_point
                    luminosity_ratio.append(ratio)
                    lumi_blocks.append(block_count1)
                else:
                    print("Run", str(run), " Block:", str(block), "One:", str(detector_one_point),
                          "Two:", str(detector_two_point))

    print("Length lumi_blocks: " + str(len(lumi_blocks)))
    print("length lumi_ratio: " + str((len(luminosity_ratio))))

    # Get percentage difference based off the first block and BCID
    first_point = detector_one_calibration / detector_two_calibration

    for index in range(len(luminosity_ratio)):
        luminosity_ratio[index] = 100 * ((luminosity_ratio[index] / first_point) - 1)

    # create graph
    graph = Graph(len(lumi_blocks))
    for i, (xx, yy) in enumerate(zip(lumi_blocks, luminosity_ratio)):
        #print (xx, yy)
        graph.SetPoint(i, float(xx), float(yy))

    # set visual attributes

    graph.markercolor = 'blue'
    graph.xaxis.SetTitle("Luminosity Block")
    graph.yaxis.SetTitle("Luminosity [Average Percent Ratio]")
    graph.xaxis.SetRangeUser(min(lumi_blocks), max(lumi_blocks))
    #graph.yaxis.SetRangeUser(min(luminosity_ratio), max(luminosity_ratio))
    graph.yaxis.SetRangeUser(-5, 5)

    # plot with ROOT
    canvas = Canvas()

    graph.Draw("AP")
    # Draw lines for different runs
    run_length = 0
    for run in sorted(all_detector_one_data.keys()):
        #print("Length of Run " + str(run) + ": " + str(len(all_detector_one_data.get(run))))
        run_length += len(all_detector_one_data.get(run))
        line = ROOT.TLine(run_length, -5, #min(luminosity_ratio),
                          run_length, 5,)  # max(luminosity_ratio))
        line.Draw()
        line_label = ROOT.TText(run_length - 30, 5 -1.5, str(run)) #max(luminosity_ratio) - 1.5, str(run))
        line_label.SetTextAngle(90)
        line_label.SetTextSize(18)
        line_label.SetTextFont(43)
        line_label.Draw()
    label = ROOT.TText(0.7, 0.8, str(name))
    label.SetTextFont(43)
    label.SetTextSize(25)
    label.SetNDC()
    label.Draw()
    canvas.Modified()
    canvas.Update()
    wait(True)
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator, MultipleLocator

# set the random seed
ROOT.gRandom.SetSeed(42)
np.random.seed(42)

# points
x = np.sort(np.random.random(10)) * 3500
y = np.random.random(10)

# set style for ROOT
set_style('ATLAS')

# create graph
graph = Graph(x.shape[0])
for i, (xx, yy) in enumerate(zip(x, y)):
    graph.SetPoint(i, xx, yy)

# set visual attributes
graph.linecolor = 'blue'
graph.markercolor = 'blue'
graph.markerstyle = 'diamond'
graph.xaxis.SetTitle("E_{T} [GeV]")
graph.yaxis.SetTitle("d#sigma_{jet}/dE_{T,jet} [fb/GeV]")
graph.xaxis.SetRangeUser(0, 3500)
graph.yaxis.SetRangeUser(0, 1)

# plot with ROOT
canvas = Canvas()
graph.Draw("APL")