def calculate_correct_x_coordinates(mc_truth, bins):
    mc_temp = rebin_asymmetric(mc_truth, bins)
    widths = calculate_bin_widths(bins)
    
    x_positions = []
    add_position = x_positions.append
    
    for bin_i, width in enumerate(widths):
        y = mc_temp.GetBinContent(bin_i + 1)/width
        #find closest y-distance on MC hist and get the x-value
        x_low = bins[bin_i]# + 1)
        x_high = x_low + width
        x = find_x_of_closest_approach(mc_truth, x_low, x_high, y)
        add_position(x)
        
    return x_positions
def do_shape_check(channel,
                   control_region_1,
                   control_region_2,
                   variable,
                   normalisation,
                   title,
                   x_title,
                   y_title,
                   x_limits,
                   y_limits,
                   name_region_1='conversions',
                   name_region_2='non-isolated electrons',
                   name_region_3='fit results',
                   rebin=1):
    global b_tag_bin
    # QCD shape comparison
    if channel == 'electron':
        histograms = get_histograms_from_files(
            [control_region_1, control_region_2], histogram_files)

        region_1 = histograms[channel][control_region_1].Clone(
        ) - histograms['TTJet'][control_region_1].Clone(
        ) - histograms['V+Jets'][control_region_1].Clone(
        ) - histograms['SingleTop'][control_region_1].Clone()
        region_2 = histograms[channel][control_region_2].Clone(
        ) - histograms['TTJet'][control_region_2].Clone(
        ) - histograms['V+Jets'][control_region_2].Clone(
        ) - histograms['SingleTop'][control_region_2].Clone()

        region_1.Rebin(rebin)
        region_2.Rebin(rebin)

        histogram_properties = Histogram_properties()
        histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_' + b_tag_bin
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[0]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = 'upper right'
        make_control_region_comparison(
            region_1,
            region_2,
            name_region_1=name_region_1,
            name_region_2=name_region_2,
            histogram_properties=histogram_properties,
            save_folder=output_folder)

        # QCD shape comparison to fit results
        histograms = get_histograms_from_files([control_region_1],
                                               histogram_files)

        region_1_tmp = histograms[channel][control_region_1].Clone(
        ) - histograms['TTJet'][control_region_1].Clone(
        ) - histograms['V+Jets'][control_region_1].Clone(
        ) - histograms['SingleTop'][control_region_1].Clone()
        region_1 = rebin_asymmetric(region_1_tmp, bin_edges[variable])

        fit_results_QCD = normalisation[variable]['QCD']
        region_2 = value_error_tuplelist_to_hist(fit_results_QCD,
                                                 bin_edges[variable])

        histogram_properties = Histogram_properties()
        histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_fits_with_conversions_' + b_tag_bin
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[1]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = 'upper right'
        make_control_region_comparison(
            region_1,
            region_2,
            name_region_1=name_region_1,
            name_region_2=name_region_3,
            histogram_properties=histogram_properties,
            save_folder=output_folder)

    histograms = get_histograms_from_files([control_region_2], histogram_files)

    region_1_tmp = histograms[channel][control_region_2].Clone(
    ) - histograms['TTJet'][control_region_2].Clone(
    ) - histograms['V+Jets'][control_region_2].Clone(
    ) - histograms['SingleTop'][control_region_2].Clone()
    region_1 = rebin_asymmetric(region_1_tmp, bin_edges[variable])

    fit_results_QCD = normalisation[variable]['QCD']
    region_2 = value_error_tuplelist_to_hist(fit_results_QCD,
                                             bin_edges[variable])

    histogram_properties = Histogram_properties()
    histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_fits_with_noniso_' + b_tag_bin
    histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
    histogram_properties.x_axis_title = x_title
    histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
    histogram_properties.x_limits = x_limits
    histogram_properties.y_limits = y_limits[1]
    histogram_properties.mc_error = 0.0
    histogram_properties.legend_location = 'upper right'
    make_control_region_comparison(region_1,
                                   region_2,
                                   name_region_1=name_region_2,
                                   name_region_2=name_region_3,
                                   histogram_properties=histogram_properties,
                                   save_folder=output_folder)
def do_shape_check(channel, control_region_1, control_region_2, variable, normalisation, title, x_title, y_title, x_limits, y_limits,
                   name_region_1='conversions' , name_region_2='non-isolated electrons', name_region_3='fit results', rebin=1):
    global b_tag_bin
    # QCD shape comparison
    if channel == 'electron':
        histograms = get_histograms_from_files([control_region_1, control_region_2], histogram_files)
        
        region_1 = histograms[channel][control_region_1].Clone() - histograms['TTJet'][control_region_1].Clone() - histograms['V+Jets'][control_region_1].Clone() - histograms['SingleTop'][control_region_1].Clone()
        region_2 = histograms[channel][control_region_2].Clone() - histograms['TTJet'][control_region_2].Clone() - histograms['V+Jets'][control_region_2].Clone() - histograms['SingleTop'][control_region_2].Clone()
        
        region_1.Rebin(rebin)
        region_2.Rebin(rebin)
        
        histogram_properties = Histogram_properties()
        histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_' + b_tag_bin
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[0]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = 'upper right'
        make_control_region_comparison(region_1, region_2,
                                       name_region_1=name_region_1, name_region_2=name_region_2,
                                       histogram_properties=histogram_properties, save_folder=output_folder)
        
        # QCD shape comparison to fit results
        histograms = get_histograms_from_files([control_region_1], histogram_files)
        
        region_1_tmp = histograms[channel][control_region_1].Clone() - histograms['TTJet'][control_region_1].Clone() - histograms['V+Jets'][control_region_1].Clone() - histograms['SingleTop'][control_region_1].Clone()
        region_1 = rebin_asymmetric(region_1_tmp, bin_edges[variable])
        
        fit_results_QCD = normalisation[variable]['QCD']
        region_2 = value_error_tuplelist_to_hist(fit_results_QCD, bin_edges_vis[variable])
        
        histogram_properties = Histogram_properties()
        histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_fits_with_conversions_' + b_tag_bin
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[1]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = 'upper right'
        make_control_region_comparison(region_1, region_2,
                                       name_region_1=name_region_1, name_region_2=name_region_3,
                                       histogram_properties=histogram_properties, save_folder=output_folder)
    
    histograms = get_histograms_from_files([control_region_2], histogram_files)
    
    region_1_tmp = histograms[channel][control_region_2].Clone() - histograms['TTJet'][control_region_2].Clone() - histograms['V+Jets'][control_region_2].Clone() - histograms['SingleTop'][control_region_2].Clone()
    region_1 = rebin_asymmetric(region_1_tmp, bin_edges_vis[variable])    
    
    fit_results_QCD = normalisation[variable]['QCD']
    region_2 = value_error_tuplelist_to_hist(fit_results_QCD, bin_edges[variable])
    
    histogram_properties = Histogram_properties()
    histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_fits_with_noniso_' + b_tag_bin
    histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
    histogram_properties.x_axis_title = x_title
    histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
    histogram_properties.x_limits = x_limits
    histogram_properties.y_limits = y_limits[1]
    histogram_properties.mc_error = 0.0
    histogram_properties.legend_location = 'upper right'
    make_control_region_comparison(region_1, region_2,
                                   name_region_1=name_region_2, name_region_2=name_region_3,
                                   histogram_properties=histogram_properties, save_folder=output_folder)
def do_shape_check(
    channel,
    control_region_1,
    control_region_2,
    variable,
    normalisation,
    title,
    x_title,
    y_title,
    x_limits,
    y_limits,
    name_region_1="conversions",
    name_region_2="non-isolated electrons",
    name_region_3="fit results",
    rebin=1,
):
    global b_tag_bin
    # QCD shape comparison
    if channel == "electron":
        histograms = get_histograms_from_files([control_region_1, control_region_2], histogram_files)

        region_1 = (
            histograms[channel][control_region_1].Clone()
            - histograms["TTJet"][control_region_1].Clone()
            - histograms["V+Jets"][control_region_1].Clone()
            - histograms["SingleTop"][control_region_1].Clone()
        )
        region_2 = (
            histograms[channel][control_region_2].Clone()
            - histograms["TTJet"][control_region_2].Clone()
            - histograms["V+Jets"][control_region_2].Clone()
            - histograms["SingleTop"][control_region_2].Clone()
        )

        region_1.Rebin(rebin)
        region_2.Rebin(rebin)

        histogram_properties = Histogram_properties()
        histogram_properties.name = "QCD_control_region_comparison_" + channel + "_" + variable + "_" + b_tag_bin
        histogram_properties.title = title + ", " + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = "arbitrary units/(0.1)"
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[0]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = "upper right"
        make_control_region_comparison(
            region_1,
            region_2,
            name_region_1=name_region_1,
            name_region_2=name_region_2,
            histogram_properties=histogram_properties,
            save_folder=output_folder,
        )

        # QCD shape comparison to fit results
        histograms = get_histograms_from_files([control_region_1], histogram_files)

        region_1_tmp = (
            histograms[channel][control_region_1].Clone()
            - histograms["TTJet"][control_region_1].Clone()
            - histograms["V+Jets"][control_region_1].Clone()
            - histograms["SingleTop"][control_region_1].Clone()
        )
        region_1 = rebin_asymmetric(region_1_tmp, bin_edges[variable])

        fit_results_QCD = normalisation[variable]["QCD"]
        region_2 = value_error_tuplelist_to_hist(fit_results_QCD, bin_edges[variable])

        histogram_properties = Histogram_properties()
        histogram_properties.name = (
            "QCD_control_region_comparison_" + channel + "_" + variable + "_fits_with_conversions_" + b_tag_bin
        )
        histogram_properties.title = title + ", " + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = "arbitrary units/(0.1)"
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[1]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = "upper right"
        make_control_region_comparison(
            region_1,
            region_2,
            name_region_1=name_region_1,
            name_region_2=name_region_3,
            histogram_properties=histogram_properties,
            save_folder=output_folder,
        )

    histograms = get_histograms_from_files([control_region_2], histogram_files)

    region_1_tmp = (
        histograms[channel][control_region_2].Clone()
        - histograms["TTJet"][control_region_2].Clone()
        - histograms["V+Jets"][control_region_2].Clone()
        - histograms["SingleTop"][control_region_2].Clone()
    )
    region_1 = rebin_asymmetric(region_1_tmp, bin_edges[variable])

    fit_results_QCD = normalisation[variable]["QCD"]
    region_2 = value_error_tuplelist_to_hist(fit_results_QCD, bin_edges[variable])

    histogram_properties = Histogram_properties()
    histogram_properties.name = (
        "QCD_control_region_comparison_" + channel + "_" + variable + "_fits_with_noniso_" + b_tag_bin
    )
    histogram_properties.title = title + ", " + b_tag_bins_latex[b_tag_bin]
    histogram_properties.x_axis_title = x_title
    histogram_properties.y_axis_title = "arbitrary units/(0.1)"
    histogram_properties.x_limits = x_limits
    histogram_properties.y_limits = y_limits[1]
    histogram_properties.mc_error = 0.0
    histogram_properties.legend_location = "upper right"
    make_control_region_comparison(
        region_1,
        region_2,
        name_region_1=name_region_2,
        name_region_2=name_region_3,
        histogram_properties=histogram_properties,
        save_folder=output_folder,
    )