def print_xsections(xsections, channel, toFile = True, print_before_unfolding = False):
    global output_folder, variable, k_value, met_type, b_tag_bin
    printout = '=' * 60
    printout += '\n'
    printout += 'Results for %s variable, %s channel, k-value %s, met type %s, %s b-tag region\n' % (variable, channel, k_value, met_type, b_tag_bin)
    if print_before_unfolding:
        printout += 'BEFORE UNFOLDING\n'
    printout += '=' * 60
    printout += '\n'
    printout += '$%s$ bin & $\sigma_{meas}$' % variables_latex[variable]
    printout += '\\\\ \n\hline\n'
    scale = 100
    
    bins = variable_bins_ROOT[variable]
    assert(len(bins) == len(xsections['unfolded_with_systematics']))
    
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, error_up, error_down = xsections['measured_with_systematics'][bin_i]
        else:
            value, error_up, error_down = xsections['unfolded_with_systematics'][bin_i]
        relativeError_up = getRelativeError(value, error_up)
        relativeError_down = getRelativeError(value, error_down)
        if error_up == error_down:
            printout += '%s & ' % variable_bins_latex[variable_bin] + ' $(%.2f \pm %.2f ) \cdot 10^{-2} ' % (value * scale, error_up * scale) +\
                    '(%.2f' % (relativeError_up * 100) + '\%)$'
        else:
            printout += '%s & ' % variable_bins_latex[variable_bin] + ' $(%.2f^{+%.2f}_{-%.2f)} \cdot 10^{-2} ' % (value * scale, error_up * scale, error_down * scale) +\
                    '(^{+%.2f}_{-%.2f}' % (relativeError_up * 100, relativeError_down * 100) + '\%)$'
        printout += '\\\\ \n'

    printout += '\hline \n\n'
    
    if toFile:
        path = output_folder + '/'  + str(measurement_config.centre_of_mass) + 'TeV/'  + variable
        make_folder_if_not_exists(path)
        if print_before_unfolding:
            output_file = open(path + '/normalised_xsection_result_' + channel + '_' + met_type + '_kv' + str(k_value) + '_measured.tex', 'w')
        else:
            output_file = open(path + '/normalised_xsection_result_' + channel + '_' + met_type + '_kv' + str(k_value) + '_unfolded.tex', 'w')
        output_file.write(printout)
        output_file.close()
    else:
        print printout
示例#2
0
def print_xsections(xsections, channel, toFile=True):
    global savePath, variable, k_value, met_type, b_tag_bin
    printout = '\n'
    printout += '=' * 60
    printout = '\n'
    printout += 'Results for %s variable, %s channel, k-value %s, met type %s, %s b-tag region\n' % (
        variable, channel, k_value, met_type, b_tag_bin)
    printout += '=' * 60
    printout += '\n'
    rows = {}
    header = 'Measurement'
    scale = 100

    bins = variable_bins_ROOT[variable]
    assert (len(bins) == len(xsections['central']))

    for bin_i, variable_bin in enumerate(bins):
        header += '& $\sigma_{meas}$ %s bin %s~\GeV' % (variable, variable_bin)
        for source in categories:
            value, error = xsections[source][bin_i]
            relativeError = getRelativeError(value, error)
            text = ' $(%.2f \pm %.2f) \cdot 10^{-2}$ ' % (
                value * scale,
                error * scale) + '(%.2f' % (relativeError * 100) + '\%)'
            if rows.has_key(source):
                rows[source].append(text)
            else:
                rows[source] = [translateOptions[source], text]

    header += '\\\\ \n'
    printout += header
    printout += '\hline\n'
    for item in rows['central']:
        printout += item + '&'
    printout = printout.rstrip('&')
    printout += '\\\\ \n'

    for source in sorted(rows.keys()):
        if source == 'central':
            continue
        for item in rows[source]:
            printout += item + '&'
        printout = printout.rstrip('&')
        printout += '\\\\ \n'
    printout += '\hline \n\n'

    make_folder_if_not_exists(savePath + '/' + variable)
    if toFile:
        output_file = open(
            savePath + '/' + variable + '/normalised_xsection_result_' +
            channel + '_' + met_type + '_kv' + str(k_value) + '.tex', 'w')
        output_file.write(printout)
        output_file.close()
    else:
        print printout
def print_typical_systematics_table(central_values, errors, channel, toFile = True, print_before_unfolding = False):
    global output_folder, variable, met_type, b_tag_bin, all_measurements, phase_space, measurement_config
    bins = None
    if phase_space == 'VisiblePS':
        bins = variable_bins_visiblePS_ROOT[variable]
    elif phase_space == 'FullPS':
        bins = variable_bins_ROOT[variable]
    if print_before_unfolding:
        measurement = 'measured'
    else:
        measurement = 'unfolded'

    assert(len(bins) == len(errors['central']))
    assert(len(bins) == len(central_values[measurement]))
    typical_systematics = measurement_config.typical_systematics
    for s in typical_systematics:
        assert(errors.has_key(s))
    
    group_errors = {}
    for group in measurement_config.typical_systematics_summary:
        group_errors[group] = []

    for bin_i, _ in enumerate(bins):

        central_value = central_values[measurement][bin_i][0]
        uncertainties = {}
        # calculate all relative errors
        for systematic in typical_systematics:
            abs_error = errors[systematic][bin_i]
            relative_error = getRelativeError(central_value, abs_error)
            uncertainties[systematic] = relative_error
        # add errors in a group in quadrature
        for group, u_list in measurement_config.typical_systematics_summary.items():

            group_error_squared = 0
            for subgroup in u_list:
                # use the biggest of up and down
                subgroup_error = max(uncertainties[subgroup[0]], uncertainties[subgroup[1]])
                group_error_squared += pow(subgroup_error, 2)
            group_errors[group].append(math.sqrt(group_error_squared))

    summarised_typical_systematics = {}
    summarised_max_systematics = {}
    # calculate the median
    # x 100 to be in %
    for group, u_list in group_errors.items():
        summarised_typical_systematics[group] = median(u_list)*100
        summarised_max_systematics[group] = max(u_list) * 100

    for summary, errors in {'median':summarised_typical_systematics,'max':summarised_max_systematics}.iteritems():
        printout = '%% ' + '=' * 60
        printout += '\n'
        printout += '%% Typical systematics table for {0} channel, met type {1}, {2} b-tag region\n'.format(channel, met_type, b_tag_bin)
        if print_before_unfolding:
            printout += '%% BEFORE UNFOLDING\n'
        printout += '%% ' + '=' * 60
        printout += '\n'
        printout += '\\begin{table}[htbp]\n'
        printout += '\\centering\n'
        printout += '\\caption{Typical systematic uncertainties (median values) for the normalised \\ttbar cross section measurement \n'
        printout += 'at a centre-of-mass energy of {0} TeV '.format(measurement_config.centre_of_mass_energy)
        if channel == 'combined' or channel == 'combinedBeforeUnfolding':
            printout += '(combination of electron and muon channels).}\n'
        else:
            printout += '({0} channel).}\n'.format(channel)
        printout += '\\label{{tab:typical_systematics_{0}TeV_{1}}}\n'.format(measurement_config.centre_of_mass_energy, channel)
        printout += '\\resizebox{\\columnwidth}{!} {\n'
        printout += '\\begin{tabular}{l' + 'r'*len(bins) + '}\n'
        printout += '\\hline\n'

        header = 'Uncertainty source '
        header += '& {0}'.format(variables_latex[variable])

        header += ' '
        printout += header
        printout += '\n\\hline\n'
        for group, ts in errors.items():
            printout += group + ' (\\%) & {:.2f} \\\\ \n'.format(ts)
        printout += '\\hline \n'
        printout += '\\hline \n'
        printout += '\\end{tabular}\n'
        printout += '}\n'
        printout += '\\end{table}\n'

        if toFile:
            path = output_folder + '/'
            make_folder_if_not_exists(path)
            file_template = path + '/{0}_systematics_{1}TeV_{2}.tex'.format(summary,measurement_config.centre_of_mass_energy, channel)

            if print_before_unfolding:
                make_folder_if_not_exists(path + '/before_unfolding/')
                file_template = file_template.replace(path, path + '/before_unfolding/')
            if os.path.isfile(file_template): 
                with open(file_template, 'r+') as output_file:
                    lines = output_file.readlines()
                    for line_number, line in enumerate (lines):
                        if line.startswith("Uncertainty source"):
                            lines[line_number] = lines[line_number].strip() + "& " + variables_latex[variable] + "\n"
                        elif variable == "HT" and line.startswith("$E_{T}^{miss}$ uncertainties"):
                            lines[line_number] = lines[line_number].strip() + "& - \n"
                        else:
                            for group, ts in errors.items():
                                if line.startswith(group):
                                    new_line = line.replace('\\\\', '')
                                    new_line = new_line.strip()
                                    lines[line_number] = new_line + '& {:.2f} \\\\ \n'.format(ts)
                    output_file.seek(0)
                    for line in lines:
                        output_file.write(line)            
            else:
                output_file = open(file_template, 'w')
                output_file.write(printout)
            output_file.close()
        else:
            print printout
def print_error_table(central_values, errors, channel, toFile = True, print_before_unfolding = False):
    global output_folder, variable, met_type, b_tag_bin, all_measurements, phase_space
    bins = None
    bins_latex = None
    binEdges = None
    variable_latex = variables_latex[variable]
    if phase_space == 'VisiblePS':
        bins = variable_bins_visiblePS_ROOT[variable]
        bins_latex = variable_bins_visiblePS_latex[variable]
        binEdges = bin_edges_vis[variable]
    elif phase_space == 'FullPS':
        bins = variable_bins_ROOT[variable]
        bins_latex = variable_bins_latex[variable]
        binEdges = bin_edges_full[variable]
    printout = '%% ' + '=' * 60
    printout += '\n'
    printout += '%% Systematics table for %s variable, %s channel, met type %s, %s b-tag region\n' % (variable, channel, met_type, b_tag_bin)
    if print_before_unfolding:
        printout += '%% BEFORE UNFOLDING\n'
    printout += '%% ' + '=' * 60
    printout += '\n'

    printout += '\\begin{table}[htbp]\n'
    printout += '\\centering\n'
    printout += '\\caption{Systematic uncertainties for the normalised \\ttbar cross section measurement with respect to %s variable\n' % variable_latex
    printout += 'at a centre-of-mass energy of %d TeV ' % measurement_config.centre_of_mass_energy
    if channel == 'combined' or channel == "combinedBeforeUnfolding":
        printout += '(combination of electron and muon channels).}\n'
    else:
        printout += '(%s channel).}\n' % channel
    printout += '\\label{tab:%s_systematics_%dTeV_%s}\n' % (variable, measurement_config.centre_of_mass_energy, channel)
    if variable == 'MT':
        printout += '\\resizebox*{!}{\\textheight} {\n'
    else:
        printout += '\\resizebox{\\columnwidth}{!} {\n'
    printout += '\\begin{tabular}{l' + 'r'*len(bins) + '}\n'
    printout += '\\hline\n'

    header = 'Uncertainty source '
    rows = {}

    assert(len(bins) == len(errors['central']))
    if print_before_unfolding:
        assert(len(bins) == len(central_values['measured']))
    else:
        assert(len(bins) == len(central_values['unfolded']))
    
    errorHists = {}
    errorHists['statistical'] = []
    for source in all_measurements:
        errorHists[source] = []

    for bin_i, variable_bin in enumerate(bins):
        header += '& %s' % (bins_latex[variable_bin])
        if print_before_unfolding:
            central_value = central_values['measured'][bin_i][0]
        else:
            central_value = central_values['unfolded'][bin_i][0]

        for source in all_measurements:
            if ( variable == 'HT' or variable == 'NJets' or variable == 'lepton_pt' or variable == 'abs_lepton_eta'  ) and source in measurement_config.met_systematics and not 'JES' in source and not 'JER' in source:
                continue

            abs_error = errors[source][bin_i]
            relative_error = getRelativeError(central_value, abs_error)

            errorHists[source].append(relative_error)

            text = '%.2f' % (relative_error*100)
            if rows.has_key(source):
                rows[source].append(text)
            elif met_type in source:
                rows[source] = [measurements_latex[source.replace(met_type, '')] + ' (\%)', text]
            else:
                if source in met_systematics_latex.keys():
                    rows[source] = [met_systematics_latex[source] + ' (\%)', text]
                else:
                    rows[source] = [measurements_latex[source] + ' (\%)', text]
    header += ' \\\\'
    printout += header
    printout += '\n\\hline\n'

    for source in sorted(rows.keys()):
        if source == 'central':
            continue
        for item in rows[source]:
            printout += item + ' & '
        printout = printout.rstrip('& ')
        printout += ' \\\\ \n'

    #append the total statistical error to the table
    printout += '\\hline \n'
    total_line = 'Total Stat. (\%)'
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, error = central_values['measured'][bin_i]
        else:
            value, error = central_values['unfolded'][bin_i]
        relativeError = getRelativeError(value, error)
        errorHists['statistical'].append(relativeError)
        total_line += ' & %.2f ' % (relativeError * 100)
    printout += total_line + '\\\\ \n'

    if not print_before_unfolding:
        make_error_plot( errorHists, binEdges )

    #append the total systematic error to the table
    total_line = 'Total Sys. (\%)'
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, error_up, error_down = central_values['measured_with_systematics_only'][bin_i]
        else:
            value, error_up, error_down = central_values['unfolded_with_systematics_only'][bin_i]
        error = max(error_up, error_down)
        relativeError = getRelativeError(value, error)
        total_line += ' & %.2f ' % (relativeError * 100)
    printout += total_line + '\\\\ \n'

    #append the total error to the table
    printout += '\\hline \n'
    total_line = 'Total (\%)'
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, error_up, error_down = central_values['measured_with_systematics'][bin_i]
        else:
            value, error_up, error_down = central_values['unfolded_with_systematics'][bin_i]
        error = max(error_up, error_down)
        relativeError = getRelativeError(value, error)
        total_line += ' & %.2f ' % (relativeError * 100)
    printout += total_line + '\\\\ \n'
    printout += '\\hline \n'
    printout += '\\end{tabular}\n'
    printout += '}\n'
    printout += '\\end{table}\n'
    
    if toFile:
        path = output_folder + '/'  + variable + '/'
        make_folder_if_not_exists(path)
        file_template = path + '/%s_systematics_%dTeV_%s.tex' % (variable, measurement_config.centre_of_mass_energy, channel)

        if print_before_unfolding:
            make_folder_if_not_exists(path + '/before_unfolding/')
            file_template = file_template.replace(path, path + '/before_unfolding/')
        output_file = open(file_template, 'w')
        output_file.write(printout)
        output_file.close()
    else:
        print printout
def print_xsections(xsections, channel, toFile = True, print_before_unfolding = False):
    global output_folder, variable, met_type, b_tag_bin, phase_space
    printout = '%% ' + '=' * 60
    printout += '\n'
    printout += '%% Results for %s variable, %s channel, met type %s, %s b-tag region\n' % (variable, channel, met_type, b_tag_bin)
    if print_before_unfolding:
        printout += '%% BEFORE UNFOLDING\n'
    printout += '%% ' + '=' * 60
    printout += '\n'

    printout += '\\begin{table}[htbp]\n'
    printout += '\\setlength{\\tabcolsep}{2pt}\n'
    printout += '\\centering\n'
    printout += '\\caption{Normalised \\ttbar cross section measurement with respect to %s variable\n' % variables_latex[variable]
    printout += 'at a centre-of-mass energy of %d TeV ' % measurement_config.centre_of_mass_energy
    if channel == 'combined':
        printout += '(combination of electron and muon channels).'
    else:
        printout += '(%s channel).' % channel
    printout += ' The errors shown are combined statistical, fit and unfolding errors ($^\dagger$) and systematic uncertainty ($^\star$).}\n'
    printout += '\\label{tab:%s_xsections_%dTeV_%s}\n' % (variable, measurement_config.centre_of_mass_energy, channel)
    #printout += '\\resizebox{\\columnwidth}{!} {\n'
    printout += '\\begin{tabular}{lrrrr}\n'
    printout += '\\hline\n'
    printout += '$%s$ bin [\\GeV] & \\multicolumn{4}{c}{$\sigma_{meas} \\left(\\times 10^{3}\\right)$}' % variables_latex[variable]
    printout += '\\\\ \n\hline\n'
    scale = 1000
    
    bins = None
    bins_latex = None
    if phase_space == 'VisiblePS':
        bins = variable_bins_visiblePS_ROOT[variable]
        bins_latex = variable_bins_visiblePS_latex[variable]
    elif phase_space == 'FullPS':
        bins = variable_bins_ROOT[variable]
        bins_latex = variable_bins_latex[variable]

    assert(len(bins) == len(xsections['unfolded_with_systematics']))
    
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, stat_error = xsections['measured'][bin_i]
            _, total_error_up, total_error_down = xsections['measured_with_systematics'][bin_i]
        else:
            value, stat_error = xsections['unfolded'][bin_i]
            _, total_error_up, total_error_down = xsections['unfolded_with_systematics'][bin_i]
        # extracting the systematic error from the total in quadrature
        syst_error_up = math.sqrt(total_error_up**2 - stat_error**2)
        syst_error_down = math.sqrt(total_error_down**2 - stat_error**2)
        #relative errors for percentages
        total_relativeError_up = getRelativeError(value, total_error_up)
        total_relativeError_down = getRelativeError(value, total_error_down)
        if total_error_up == total_error_down:
            printout += '%s & ' % bins_latex[variable_bin] + ' $%.2f$ & $ \pm~ %.2f^\\dagger$ & $ \pm~ %.2f^\\star$ & ' % (value * scale, stat_error * scale, syst_error_up * scale) +\
                    '$(%.2f' % (total_relativeError_up * 100) + '\%)$'
        else:
            printout += '%s & ' % bins_latex[variable_bin] + ' $%.2f$ & $ \pm~ %.2f^\\dagger$ & $ ~^{+%.2f}_{-%.2f}^\\star$ & ' % (value * scale, stat_error * scale, syst_error_up * scale, syst_error_down * scale) +\
                    '$(^{+%.2f}_{-%.2f}' % (total_relativeError_up * 100, total_relativeError_down * 100) + '\%)$'
        printout += '\\\\ \n'

    printout += '\\hline \n'
    printout += '\\end{tabular}\n'
    #printout += '}\n' #for resizebox
    printout += '\\end{table}\n'
    
    if toFile:
        path = output_folder + '/' + variable
        make_folder_if_not_exists(path)
        file_template = path + '/%s_normalised_xsection_%dTeV_%s.tex' % (variable, measurement_config.centre_of_mass_energy, channel)

        if print_before_unfolding:
            make_folder_if_not_exists(path + '/before_unfolding/')
            file_template = file_template.replace(path, path + '/before_unfolding/')
        output_file = open(file_template, 'w')
        output_file.write(printout)
        output_file.close()
    else:
        print printout
def print_error_table(central_values, errors, channel, toFile = True, print_before_unfolding = False):
    global output_folder, variable, k_value, met_type, b_tag_bin, all_measurements
    printout = '=' * 60
    printout += '\n'
    printout += 'Errors for %s variable, %s channel, k-value %s, met type %s, %s b-tag region\n' % (variable, channel, k_value, met_type, b_tag_bin)
    if print_before_unfolding:
        printout += 'BEFORE UNFOLDING\n'
    printout += '=' * 60
    printout += '\n\hline\n'
    
    header = 'Systematic'
    scale = 100
    rows = {}
    
    bins = variable_bins_ROOT[variable]
    assert(len(bins) == len(errors['central']))
    if print_before_unfolding:
        assert(len(bins) == len(central_values['measured']))
    else:
        assert(len(bins) == len(central_values['unfolded']))
    
    for bin_i, variable_bin in enumerate(bins):
        header += '& %s' % (variable_bins_latex[variable_bin])
        if print_before_unfolding:
            central_value = central_values['measured'][bin_i][0]
        else:
            central_value = central_values['unfolded'][bin_i][0]
        for source in all_measurements:
            abs_error = errors[source][bin_i]
            relative_error = getRelativeError(central_value, abs_error)
            text = '%.2f' % (relative_error*100)
            if rows.has_key(source):
                rows[source].append(text)
            elif 'PDF' in source:
                continue
            elif met_type in source:
                rows[source] = [met_systematics_latex[source.replace(met_type, '')] + ' (\%)', text]
            else:
                rows[source] = [measurements_latex[source] + ' (\%)', text]

    header += ' \\\\'
    printout += header
    printout += '\n\hline\n'
    
    for source in sorted(rows.keys()):
        if source == 'central':
            continue
        for item in rows[source]:
            printout += item + ' & '
        printout = printout.rstrip('& ')
        printout += '\\\\ \n'

    printout += '\hline \n\n'
    
    if toFile:
        path = output_folder + '/'  + str(measurement_config.centre_of_mass) + 'TeV/'  + variable
        make_folder_if_not_exists(path)
        if print_before_unfolding:
            output_file = open(path + '/error_table_' + channel + '_' + met_type + '_kv' + str(k_value) + '_measured.tex', 'w')
        else:
            output_file = open(path + '/error_table_' + channel + '_' + met_type + '_kv' + str(k_value) + '_unfolded.tex', 'w')
        output_file.write(printout)
        output_file.close()
    else:
        print printout
示例#7
0
def print_xsections_with_uncertainties(xsections, channel, toFile=True):
    global savePath, variable, k_value, met_type, b_tag_bin
    printout = '\n'
    printout += '=' * 60
    printout = '\n'
    printout += 'Results for %s variable, %s channel, k-value %s, met type %s, %s b-tag region\n' % (
        variable, channel, k_value, met_type, b_tag_bin)
    printout += '=' * 60
    printout += '\n'
    #    rows = {}
    printout += '%s bin & $\sigma_{meas}$ \\\\ \n' % variable
    printout += '\hline\n'
    uncertainties = {}
    header = 'Uncertainty'

    bins = variable_bins_ROOT[variable]
    assert (len(bins) == len(xsections['central']))

    for bin_i, variable_bin in enumerate(bins):
        header += '& %s bin %s' % (variable, variable_bin)
        centralresult = xsections['central'][bin_i]
        uncertainty = calculateTotalUncertainty(xsections, bin_i)
        uncertainty_total_plus = uncertainty['Total+'][0]
        uncertainty_total_minus = uncertainty['Total-'][0]
        uncertainty_total_plus, uncertainty_total_minus = symmetriseErrors(
            uncertainty_total_plus, uncertainty_total_minus)
        scale = 100
        central_measurement = centralresult[0]
        fit_error = centralresult[1]

        formatting = (variable_bins_latex[variable_bin],
                      central_measurement * scale, fit_error * scale,
                      uncertainty_total_plus * scale,
                      uncertainty_total_minus * scale)
        text = '%s & $%.2f \pm %.2f (fit)^{+%.2f}_{-%.2f} (sys) \cdot 10^{-2}$\\\\ \n' % formatting
        if doSymmetricErrors:
            relativeError = getRelativeError(
                central_measurement, fit_error + uncertainty_total_plus)
            formatting = (variable_bins_latex[variable_bin],
                          central_measurement * scale, fit_error * scale,
                          uncertainty_total_plus * scale)
            text = '%s & $\\left(%.2f \\pm %.2f \\text{ (fit)} \pm %.2f \\text{ (syst.)}\\right)' % formatting + '(%.2f' % (
                relativeError *
                100) + '\%) \\times 10^{-2}\, \\GeV^{-1}$\\\\ \n'
        printout += text
        for source in uncertainty.keys():
            unc_result = uncertainty[source]
            if not uncertainties.has_key(source):
                if source in metsystematics_sources:
                    uncertainties[
                        source] = metsystematics_sources_latex[source] + ' & '
                else:
                    uncertainties[source] = source + ' & '
            relativeError = getRelativeError(centralresult[0], unc_result[0])
            #            text = ' $(%.2f \pm %.2f) \cdot 10^{-2} $ ' % (unc_result[0]*scale,unc_result[1]*scale) + '(%.2f' % (relativeError * 100) + '\%) &'
            text = '%.2f' % (relativeError * 100) + '\% &'
            #            text = ' $%.2f \pm %.2f $ ' % (unc_result[0]*scale,unc_result[1]*scale) + '(%.2f' % (relativeError * 100) + '\%) &'
            uncertainties[source] += text

    printout += '\\\\ \n'
    for source in sorted(uncertainties.keys()):
        value = uncertainties[source]
        value = value.rstrip('&')
        value += '\\\\ \n'
        printout += value

    make_folder_if_not_exists(savePath + '/' + variable)
    if toFile:
        output_file = open(
            savePath + '/' + variable + '/normalised_xsection_main_result_' +
            channel + '_' + met_type + '_kv' + str(k_value) + '.tex', 'w')
        output_file.write(printout)
        output_file.close()
    else:
        print printout
def print_typical_systematics_table(central_values, errors, channel, toFile = True, print_before_unfolding = False):
    global output_folder, variable, k_values, met_type, b_tag_bin, all_measurements
    bins = variable_bins_ROOT[variable]

    values_for_typical_systematics_table = {}
    
    assert(len(bins) == len(errors['central']))
    if print_before_unfolding:
        assert(len(bins) == len(central_values['measured']))
    else:
        assert(len(bins) == len(central_values['unfolded']))
    
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            central_value = central_values['measured'][bin_i][0]
        else:
            central_value = central_values['unfolded'][bin_i][0]

        for systematic_group in typical_systematics.keys():
            for source in all_measurements:
                if source in typical_systematics[systematic_group]:
                    abs_error = errors[source][bin_i]
                    relative_error = getRelativeError(central_value, abs_error)
                    value = relative_error
                    if values_for_typical_systematics_table.has_key(source):
                        values_for_typical_systematics_table[source].append(value)
                    else:
                        values_for_typical_systematics_table[source] = [typical_systematics_latex[systematic_group] + ' (\%)', value]
    
    rows_for_typical_systematics_table = {}
    
    if variable == "HT":
        del(typical_systematics["typical_systematics_MET"])
    for systematic_group in typical_systematics.keys():
        typical_systematics_row = []
        typical_systematics_row.append(typical_systematics_latex[systematic_group] + ' (\%)')
        for bin_i, variable_bin in enumerate(bins):
            sum = 0.
            
            #if only one systematic in the group, in each bin just use the systematic value (absolute)
            if len(typical_systematics[systematic_group]) == 1:
                sum += abs(values_for_typical_systematics_table[typical_systematics[systematic_group][0]][bin_i+1])
            
            #if two systematics in the group, in each bin use the largest of the two absolute values
            elif len(typical_systematics[systematic_group]) == 2:
                low_syst = abs(values_for_typical_systematics_table[typical_systematics[systematic_group][0]][bin_i+1])
                high_syst = abs(values_for_typical_systematics_table[typical_systematics[systematic_group][1]][bin_i+1])
                sum += max(low_syst, high_syst)
            
            #if more than two systematics in the group, take the maximum of each pair of up/down systematics, and add these maximum values for all systematics in quadrature
            else:
                for systematic in typical_systematics [systematic_group]:
                    index_in_group = typical_systematics[systematic_group].index(systematic)
                    if index_in_group %2 == 1 : #if not 0th, 2nd, 4th etc. element of list, get out of loop
                        continue
                    else:
                        low_syst = abs(values_for_typical_systematics_table[typical_systematics[systematic_group][index_in_group]][bin_i+1])
                        high_syst = abs(values_for_typical_systematics_table[typical_systematics[systematic_group][index_in_group+1]][bin_i+1])
                        largest = max(low_syst, high_syst) #(values_for_typical_systematics_table[systematic][bin_i+1])
                        sum += pow(largest, 2)
                sum = math.sqrt(sum)

            typical_systematics_row.append(sum)
        label = typical_systematics_row.pop(0)
        
        #for each systematic group, take the median of the values across all bins 
        value = median(typical_systematics_row)
        text = '%.2f' % (value*100)
        rows_for_typical_systematics_table[systematic_group] = [label, text]

    printout = '%% ' + '=' * 60
    printout += '\n'
    printout += '%% Typical systematics table for %s channel, k-value %s, met type %s, %s b-tag region\n' % (channel, str(k_values[channel]), met_type, b_tag_bin)
    if print_before_unfolding:
        printout += '%% BEFORE UNFOLDING\n'
    printout += '%% ' + '=' * 60
    printout += '\n'

    printout += '\\begin{table}[htbp]\n'
    printout += '\\centering\n'
    printout += '\\caption{Typical systematic uncertainties (median values) for the normalised \\ttbar cross section measurement \n'
    printout += 'at a centre-of-mass energy of %d TeV ' % measurement_config.centre_of_mass_energy
    if channel == 'combined':
        printout += '(combination of electron and muon channels).}\n'
    else:
        printout += '(%s channel).}\n' % channel
    printout += '\\label{tab:typical_systematics_%dTeV_%s}\n' % (measurement_config.centre_of_mass_energy, channel)
    printout += '\\resizebox{\\columnwidth}{!} {\n'
    printout += '\\begin{tabular}{l' + 'r'*len(variable_bins_ROOT) + '}\n'
    printout += '\\hline\n'
    
    header = 'Uncertainty source '
    header += '& %s' % (variables_latex[variable])

    header += ' '
    printout += header
    printout += '\n\\hline\n'

    for systematic_group in sorted(rows_for_typical_systematics_table.keys()):
        if systematic_group == 'central':
            continue
        for item in rows_for_typical_systematics_table[systematic_group]:
            printout += item + ' & '
        printout = printout.rstrip('& ')
        printout += ' \n'

    printout += '\\hline \n'
    printout += '\\hline \n'
    printout += '\\end{tabular}\n'
    printout += '}\n'
    printout += '\\end{table}\n'

    if toFile:
        path = output_folder + '/'  + str(measurement_config.centre_of_mass_energy) + 'TeV/'
        make_folder_if_not_exists(path)
        file_template = path + '/typical_systematics_%dTeV_%s.tex' % (measurement_config.centre_of_mass_energy, channel)

        if print_before_unfolding:
            make_folder_if_not_exists(path + '/before_unfolding/')
            file_template = file_template.replace(path, path + '/before_unfolding/')
        if os.path.isfile(file_template): 
            with open(file_template, 'r+') as output_file:
                lines = output_file.readlines()
                for line_number, line in enumerate (lines):
                    if line.startswith("Uncertainty source"):
                        lines[line_number] = lines[line_number].strip() + "& " + variables_latex[variable] + "\n"
                    elif variable == "HT" and line.startswith("$E_{T}^{miss}$ uncertainties"):
                        lines[line_number] = lines[line_number].strip() + "& - \n"
                    else:
                        for table_entry in enumerate(typical_systematics_latex):
                            if line.startswith(typical_systematics_latex[table_entry[1]]):
                                lines[line_number] = lines[line_number].strip() + "& "  + rows_for_typical_systematics_table[table_entry[1]][1] + " \n"
                output_file.seek(0)
                for line in lines:
                    output_file.write(line)            
        else:
            output_file = open(file_template, 'w')
            output_file.write(printout)
        output_file.close()
    else:
        print printout
def print_error_table(central_values, errors, channel, toFile = True, print_before_unfolding = False):
    global output_folder, variable, k_values, met_type, b_tag_bin, all_measurements
    bins = variable_bins_ROOT[variable]

    printout = '%% ' + '=' * 60
    printout += '\n'
    printout += '%% Systematics table for %s variable, %s channel, k-value %s, met type %s, %s b-tag region\n' % (variable, channel, str(k_values[channel]), met_type, b_tag_bin)
    if print_before_unfolding:
        printout += '%% BEFORE UNFOLDING\n'
    printout += '%% ' + '=' * 60
    printout += '\n'

    printout += '\\begin{table}[htbp]\n'
    printout += '\\centering\n'
    printout += '\\caption{Systematic uncertainties for the normalised \\ttbar cross section measurement with respect to \\%s variable\n' % variable
    printout += 'at a centre-of-mass energy of %d TeV ' % measurement_config.centre_of_mass_energy
    if channel == 'combined':
        printout += '(combination of electron and muon channels).}\n'
    else:
        printout += '(%s channel).}\n' % channel
    printout += '\\label{tab:%s_systematics_%dTeV_%s}\n' % (variable, measurement_config.centre_of_mass_energy, channel)
    printout += '\\resizebox{\\columnwidth}{!} {\n'
    printout += '\\begin{tabular}{l' + 'r'*len(bins) + '}\n'
    printout += '\\hline\n'
    
    header = 'Uncertainty source '
    rows = {}

    assert(len(bins) == len(errors['central']))
    if print_before_unfolding:
        assert(len(bins) == len(central_values['measured']))
    else:
        assert(len(bins) == len(central_values['unfolded']))
    
    for bin_i, variable_bin in enumerate(bins):
        header += '& %s' % (variable_bins_latex[variable_bin])
        if print_before_unfolding:
            central_value = central_values['measured'][bin_i][0]
        else:
            central_value = central_values['unfolded'][bin_i][0]
        for source in all_measurements:
            abs_error = errors[source][bin_i]
            relative_error = getRelativeError(central_value, abs_error)
            text = '%.2f' % (relative_error*100)
            if rows.has_key(source):
                rows[source].append(text)
            elif met_type in source:
                rows[source] = [met_systematics_latex[source.replace(met_type, '')] + ' (\%)', text]
            else:
                rows[source] = [measurements_latex[source] + ' (\%)', text]


    header += ' \\\\'
    printout += header
    printout += '\n\\hline\n'

    for source in sorted(rows.keys()):
        if source == 'central':
            continue
        for item in rows[source]:
            printout += item + ' & '
        printout = printout.rstrip('& ')
        printout += ' \\\\ \n'

    #append the total error to the table
    printout += '\\hline \n'
    total_line = 'Total (\%)'
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, error_up, error_down = central_values['measured_with_systematics'][bin_i]
        else:
            value, error_up, error_down = central_values['unfolded_with_systematics'][bin_i]
        error = max(error_up, error_down)
        relativeError = getRelativeError(value, error)
        total_line += ' & %.2f ' % (relativeError * 100)
    printout += total_line + '\\\\ \n'
    printout += '\\hline \n'
    printout += '\\end{tabular}\n'
    printout += '}\n'
    printout += '\\end{table}\n'
    
    if toFile:
        path = output_folder + '/'  + str(measurement_config.centre_of_mass_energy) + 'TeV/'  + variable
        make_folder_if_not_exists(path)
        file_template = path + '/%s_systematics_%dTeV_%s.tex' % (variable, measurement_config.centre_of_mass_energy, channel)

        if print_before_unfolding:
            make_folder_if_not_exists(path + '/before_unfolding/')
            file_template = file_template.replace(path, path + '/before_unfolding/')
        output_file = open(file_template, 'w')
        output_file.write(printout)
        output_file.close()
    else:
        print printout
示例#10
0
def print_typical_systematics_table(central_values,
                                    errors,
                                    channel,
                                    toFile=True,
                                    print_before_unfolding=False):
    global output_folder, variable, met_type, b_tag_bin, all_measurements, phase_space, measurement_config
    bins = None
    if phase_space == 'VisiblePS':
        bins = variable_bins_visiblePS_ROOT[variable]
    elif phase_space == 'FullPS':
        bins = variable_bins_ROOT[variable]
    if print_before_unfolding:
        measurement = 'measured'
    else:
        measurement = 'unfolded'

    assert (len(bins) == len(errors['central']))
    assert (len(bins) == len(central_values[measurement]))
    typical_systematics = measurement_config.typical_systematics
    for s in typical_systematics:
        assert (errors.has_key(s))

    group_errors = {}
    for group in measurement_config.typical_systematics_summary:
        group_errors[group] = []

    for bin_i, _ in enumerate(bins):

        central_value = central_values[measurement][bin_i][0]
        uncertainties = {}
        # calculate all relative errors
        for systematic in typical_systematics:
            abs_error = errors[systematic][bin_i]
            relative_error = getRelativeError(central_value, abs_error)
            uncertainties[systematic] = relative_error
        # add errors in a group in quadrature
        for group, u_list in measurement_config.typical_systematics_summary.items(
        ):

            group_error_squared = 0
            for subgroup in u_list:
                # use the biggest of up and down
                subgroup_error = max(uncertainties[subgroup[0]],
                                     uncertainties[subgroup[1]])
                group_error_squared += pow(subgroup_error, 2)
            group_errors[group].append(math.sqrt(group_error_squared))

    summarised_typical_systematics = {}
    summarised_max_systematics = {}
    # calculate the median
    # x 100 to be in %
    for group, u_list in group_errors.items():
        summarised_typical_systematics[group] = median(u_list) * 100
        summarised_max_systematics[group] = max(u_list) * 100

    for summary, errors in {
            'median': summarised_typical_systematics,
            'max': summarised_max_systematics
    }.iteritems():
        printout = '%% ' + '=' * 60
        printout += '\n'
        printout += '%% Typical systematics table for {0} channel, met type {1}, {2} b-tag region\n'.format(
            channel, met_type, b_tag_bin)
        if print_before_unfolding:
            printout += '%% BEFORE UNFOLDING\n'
        printout += '%% ' + '=' * 60
        printout += '\n'
        printout += '\\begin{table}[htbp]\n'
        printout += '\\centering\n'
        printout += '\\caption{Typical systematic uncertainties (median values) for the normalised \\ttbar cross section measurement \n'
        printout += 'at a centre-of-mass energy of {0} TeV '.format(
            measurement_config.centre_of_mass_energy)
        if channel == 'combined' or channel == 'combinedBeforeUnfolding':
            printout += '(combination of electron and muon channels).}\n'
        else:
            printout += '({0} channel).}\n'.format(channel)
        printout += '\\label{{tab:typical_systematics_{0}TeV_{1}}}\n'.format(
            measurement_config.centre_of_mass_energy, channel)
        printout += '\\resizebox{\\columnwidth}{!} {\n'
        printout += '\\begin{tabular}{l' + 'r' * len(bins) + '}\n'
        printout += '\\hline\n'

        header = 'Uncertainty source '
        header += '& {0}'.format(variables_latex[variable])

        header += ' '
        printout += header
        printout += '\n\\hline\n'
        for group, ts in errors.items():
            printout += group + ' (\\%) & {:.2f} \\\\ \n'.format(ts)
        printout += '\\hline \n'
        printout += '\\hline \n'
        printout += '\\end{tabular}\n'
        printout += '}\n'
        printout += '\\end{table}\n'

        if toFile:
            path = output_folder + '/'
            make_folder_if_not_exists(path)
            file_template = path + '/{0}_systematics_{1}TeV_{2}.tex'.format(
                summary, measurement_config.centre_of_mass_energy, channel)

            if print_before_unfolding:
                make_folder_if_not_exists(path + '/before_unfolding/')
                file_template = file_template.replace(
                    path, path + '/before_unfolding/')
            if os.path.isfile(file_template):
                with open(file_template, 'r+') as output_file:
                    lines = output_file.readlines()
                    for line_number, line in enumerate(lines):
                        if line.startswith("Uncertainty source"):
                            lines[line_number] = lines[line_number].strip(
                            ) + "& " + variables_latex[variable] + "\n"
                        elif variable == "HT" and line.startswith(
                                "$E_{T}^{miss}$ uncertainties"):
                            lines[line_number] = lines[line_number].strip(
                            ) + "& - \n"
                        else:
                            for group, ts in errors.items():
                                if line.startswith(group):
                                    new_line = line.replace('\\\\', '')
                                    new_line = new_line.strip()
                                    lines[
                                        line_number] = new_line + '& {:.2f} \\\\ \n'.format(
                                            ts)
                    output_file.seek(0)
                    for line in lines:
                        output_file.write(line)
            else:
                output_file = open(file_template, 'w')
                output_file.write(printout)
            output_file.close()
        else:
            print printout
示例#11
0
def print_error_table(central_values,
                      errors,
                      channel,
                      toFile=True,
                      print_before_unfolding=False):
    global output_folder, variable, met_type, b_tag_bin, all_measurements, phase_space
    bins = None
    bins_latex = None
    binEdges = None
    variable_latex = variables_latex[variable]
    if phase_space == 'VisiblePS':
        bins = variable_bins_visiblePS_ROOT[variable]
        bins_latex = variable_bins_visiblePS_latex[variable]
        binEdges = bin_edges_vis[variable]
    elif phase_space == 'FullPS':
        bins = variable_bins_ROOT[variable]
        bins_latex = variable_bins_latex[variable]
        binEdges = bin_edges[variable]
    printout = '%% ' + '=' * 60
    printout += '\n'
    printout += '%% Systematics table for %s variable, %s channel, met type %s, %s b-tag region\n' % (
        variable, channel, met_type, b_tag_bin)
    if print_before_unfolding:
        printout += '%% BEFORE UNFOLDING\n'
    printout += '%% ' + '=' * 60
    printout += '\n'

    printout += '\\begin{table}[htbp]\n'
    printout += '\\centering\n'
    printout += '\\caption{Systematic uncertainties for the normalised \\ttbar cross section measurement with respect to %s variable\n' % variable_latex
    printout += 'at a centre-of-mass energy of %d TeV ' % measurement_config.centre_of_mass_energy
    if channel == 'combined' or channel == "combinedBeforeUnfolding":
        printout += '(combination of electron and muon channels).}\n'
    else:
        printout += '(%s channel).}\n' % channel
    printout += '\\label{tab:%s_systematics_%dTeV_%s}\n' % (
        variable, measurement_config.centre_of_mass_energy, channel)
    if variable == 'MT':
        printout += '\\resizebox*{!}{\\textheight} {\n'
    else:
        printout += '\\resizebox{\\columnwidth}{!} {\n'
    printout += '\\begin{tabular}{l' + 'r' * len(bins) + '}\n'
    printout += '\\hline\n'

    header = 'Uncertainty source '
    rows = {}

    assert (len(bins) == len(errors['central']))
    if print_before_unfolding:
        assert (len(bins) == len(central_values['measured']))
    else:
        assert (len(bins) == len(central_values['unfolded']))

    errorHists = {}
    errorHists['statistical'] = []
    for source in all_measurements:
        errorHists[source] = []

    for bin_i, variable_bin in enumerate(bins):
        header += '& %s' % (bins_latex[variable_bin])
        if print_before_unfolding:
            central_value = central_values['measured'][bin_i][0]
        else:
            central_value = central_values['unfolded'][bin_i][0]

        for source in all_measurements:
            if (
                    variable == 'HT' or variable == 'NJets'
                    or variable == 'lepton_pt' or variable == 'abs_lepton_eta'
            ) and source in measurement_config.met_systematics and not 'JES' in source and not 'JER' in source:
                continue

            abs_error = errors[source][bin_i]
            relative_error = getRelativeError(central_value, abs_error)

            errorHists[source].append(relative_error)

            text = '%.2f' % (relative_error * 100)
            if rows.has_key(source):
                rows[source].append(text)
            elif met_type in source:
                rows[source] = [
                    measurements_latex[source.replace(met_type, '')] + ' (\%)',
                    text
                ]
            else:
                if source in met_systematics_latex.keys():
                    rows[source] = [
                        met_systematics_latex[source] + ' (\%)', text
                    ]
                else:
                    rows[source] = [measurements_latex[source] + ' (\%)', text]
    header += ' \\\\'
    printout += header
    printout += '\n\\hline\n'

    for source in sorted(rows.keys()):
        if source == 'central':
            continue
        for item in rows[source]:
            printout += item + ' & '
        printout = printout.rstrip('& ')
        printout += ' \\\\ \n'

    #append the total statistical error to the table
    printout += '\\hline \n'
    total_line = 'Total Stat. (\%)'
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, error = central_values['measured'][bin_i]
        else:
            value, error = central_values['unfolded'][bin_i]
        relativeError = getRelativeError(value, error)
        errorHists['statistical'].append(relativeError)
        total_line += ' & %.2f ' % (relativeError * 100)
    printout += total_line + '\\\\ \n'

    if not print_before_unfolding:
        make_error_plot(errorHists, binEdges)

    #append the total systematic error to the table
    total_line = 'Total Sys. (\%)'
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, error_up, error_down = central_values[
                'measured_with_systematics_only'][bin_i]
        else:
            value, error_up, error_down = central_values[
                'unfolded_with_systematics_only'][bin_i]
        error = max(error_up, error_down)
        relativeError = getRelativeError(value, error)
        total_line += ' & %.2f ' % (relativeError * 100)
    printout += total_line + '\\\\ \n'

    #append the total error to the table
    printout += '\\hline \n'
    total_line = 'Total (\%)'
    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, error_up, error_down = central_values[
                'measured_with_systematics'][bin_i]
        else:
            value, error_up, error_down = central_values[
                'unfolded_with_systematics'][bin_i]
        error = max(error_up, error_down)
        relativeError = getRelativeError(value, error)
        total_line += ' & %.2f ' % (relativeError * 100)
    printout += total_line + '\\\\ \n'
    printout += '\\hline \n'
    printout += '\\end{tabular}\n'
    printout += '}\n'
    printout += '\\end{table}\n'

    if toFile:
        path = output_folder + '/' + variable + '/'
        make_folder_if_not_exists(path)
        file_template = path + '/%s_systematics_%dTeV_%s.tex' % (
            variable, measurement_config.centre_of_mass_energy, channel)

        if print_before_unfolding:
            make_folder_if_not_exists(path + '/before_unfolding/')
            file_template = file_template.replace(path,
                                                  path + '/before_unfolding/')
        output_file = open(file_template, 'w')
        output_file.write(printout)
        output_file.close()
    else:
        print printout
示例#12
0
def print_xsections(xsections,
                    channel,
                    toFile=True,
                    print_before_unfolding=False):
    global output_folder, variable, met_type, b_tag_bin, phase_space
    printout = '%% ' + '=' * 60
    printout += '\n'
    printout += '%% Results for %s variable, %s channel, met type %s, %s b-tag region\n' % (
        variable, channel, met_type, b_tag_bin)
    if print_before_unfolding:
        printout += '%% BEFORE UNFOLDING\n'
    printout += '%% ' + '=' * 60
    printout += '\n'

    printout += '\\begin{table}[htbp]\n'
    printout += '\\setlength{\\tabcolsep}{2pt}\n'
    printout += '\\centering\n'
    printout += '\\caption{Normalised \\ttbar cross section measurement with respect to %s variable\n' % variables_latex[
        variable]
    printout += 'at a centre-of-mass energy of %d TeV ' % measurement_config.centre_of_mass_energy
    if channel == 'combined':
        printout += '(combination of electron and muon channels).'
    else:
        printout += '(%s channel).' % channel
    printout += ' The errors shown are combined statistical, fit and unfolding errors ($^\dagger$) and systematic uncertainty ($^\star$).}\n'
    printout += '\\label{tab:%s_xsections_%dTeV_%s}\n' % (
        variable, measurement_config.centre_of_mass_energy, channel)
    #printout += '\\resizebox{\\columnwidth}{!} {\n'
    printout += '\\begin{tabular}{lrrrr}\n'
    printout += '\\hline\n'
    printout += '$%s$ bin [\\GeV] & \\multicolumn{4}{c}{$\sigma_{meas} \\left(\\times 10^{3}\\right)$}' % variables_latex[
        variable]
    printout += '\\\\ \n\hline\n'
    scale = 1000

    bins = None
    bins_latex = None
    if phase_space == 'VisiblePS':
        bins = variable_bins_visiblePS_ROOT[variable]
        bins_latex = variable_bins_visiblePS_latex[variable]
    elif phase_space == 'FullPS':
        bins = variable_bins_ROOT[variable]
        bins_latex = variable_bins_latex[variable]

    assert (len(bins) == len(xsections['unfolded_with_systematics']))

    for bin_i, variable_bin in enumerate(bins):
        if print_before_unfolding:
            value, stat_error = xsections['measured'][bin_i]
            _, total_error_up, total_error_down = xsections[
                'measured_with_systematics'][bin_i]
        else:
            value, stat_error = xsections['unfolded'][bin_i]
            _, total_error_up, total_error_down = xsections[
                'unfolded_with_systematics'][bin_i]
        # extracting the systematic error from the total in quadrature
        syst_error_up = math.sqrt(total_error_up**2 - stat_error**2)
        syst_error_down = math.sqrt(total_error_down**2 - stat_error**2)
        #relative errors for percentages
        total_relativeError_up = getRelativeError(value, total_error_up)
        total_relativeError_down = getRelativeError(value, total_error_down)
        if total_error_up == total_error_down:
            printout += '%s & ' % bins_latex[variable_bin] + ' $%.2f$ & $ \pm~ %.2f^\\dagger$ & $ \pm~ %.2f^\\star$ & ' % (value * scale, stat_error * scale, syst_error_up * scale) +\
                    '$(%.2f' % (total_relativeError_up * 100) + '\%)$'
        else:
            printout += '%s & ' % bins_latex[variable_bin] + ' $%.2f$ & $ \pm~ %.2f^\\dagger$ & $ ~^{+%.2f}_{-%.2f}^\\star$ & ' % (value * scale, stat_error * scale, syst_error_up * scale, syst_error_down * scale) +\
                    '$(^{+%.2f}_{-%.2f}' % (total_relativeError_up * 100, total_relativeError_down * 100) + '\%)$'
        printout += '\\\\ \n'

    printout += '\\hline \n'
    printout += '\\end{tabular}\n'
    #printout += '}\n' #for resizebox
    printout += '\\end{table}\n'

    if toFile:
        path = output_folder + '/' + variable
        make_folder_if_not_exists(path)
        file_template = path + '/%s_normalised_xsection_%dTeV_%s.tex' % (
            variable, measurement_config.centre_of_mass_energy, channel)

        if print_before_unfolding:
            make_folder_if_not_exists(path + '/before_unfolding/')
            file_template = file_template.replace(path,
                                                  path + '/before_unfolding/')
        output_file = open(file_template, 'w')
        output_file.write(printout)
        output_file.close()
    else:
        print printout
def print_xsections(xsections, channel, toFile=True):
    global savePath, variable, k_value, met_type, b_tag_bin
    printout = "\n"
    printout += "=" * 60
    printout = "\n"
    printout += "Results for %s variable, %s channel, k-value %s, met type %s, %s b-tag region\n" % (
        variable,
        channel,
        k_value,
        met_type,
        b_tag_bin,
    )
    printout += "=" * 60
    printout += "\n"
    rows = {}
    header = "Measurement"
    scale = 100

    bins = variable_bins_ROOT[variable]
    assert len(bins) == len(xsections["central"])

    for bin_i, variable_bin in enumerate(bins):
        header += "& $\sigma_{meas}$ %s bin %s~\GeV" % (variable, variable_bin)
        for source in categories:
            value, error = xsections[source][bin_i]
            relativeError = getRelativeError(value, error)
            text = (
                " $(%.2f \pm %.2f) \cdot 10^{-2}$ " % (value * scale, error * scale)
                + "(%.2f" % (relativeError * 100)
                + "\%)"
            )
            if rows.has_key(source):
                rows[source].append(text)
            else:
                rows[source] = [translateOptions[source], text]

    header += "\\\\ \n"
    printout += header
    printout += "\hline\n"
    for item in rows["central"]:
        printout += item + "&"
    printout = printout.rstrip("&")
    printout += "\\\\ \n"

    for source in sorted(rows.keys()):
        if source == "central":
            continue
        for item in rows[source]:
            printout += item + "&"
        printout = printout.rstrip("&")
        printout += "\\\\ \n"
    printout += "\hline \n\n"

    make_folder_if_not_exists(savePath + "/" + variable)
    if toFile:
        output_file = open(
            savePath
            + "/"
            + variable
            + "/normalised_xsection_result_"
            + channel
            + "_"
            + met_type
            + "_kv"
            + str(k_value)
            + ".tex",
            "w",
        )
        output_file.write(printout)
        output_file.close()
    else:
        print printout
def print_xsections_with_uncertainties(xsections, channel, toFile=True):
    global savePath, variable, k_value, met_type, b_tag_bin
    printout = "\n"
    printout += "=" * 60
    printout = "\n"
    printout += "Results for %s variable, %s channel, k-value %s, met type %s, %s b-tag region\n" % (
        variable,
        channel,
        k_value,
        met_type,
        b_tag_bin,
    )
    printout += "=" * 60
    printout += "\n"
    #    rows = {}
    printout += "%s bin & $\sigma_{meas}$ \\\\ \n" % variable
    printout += "\hline\n"
    uncertainties = {}
    header = "Uncertainty"

    bins = variable_bins_ROOT[variable]
    assert len(bins) == len(xsections["central"])

    for bin_i, variable_bin in enumerate(bins):
        header += "& %s bin %s" % (variable, variable_bin)
        centralresult = xsections["central"][bin_i]
        uncertainty = calculateTotalUncertainty(xsections, bin_i)
        uncertainty_total_plus = uncertainty["Total+"][0]
        uncertainty_total_minus = uncertainty["Total-"][0]
        uncertainty_total_plus, uncertainty_total_minus = symmetriseErrors(
            uncertainty_total_plus, uncertainty_total_minus
        )
        scale = 100
        central_measurement = centralresult[0]
        fit_error = centralresult[1]

        formatting = (
            variable_bins_latex[variable_bin],
            central_measurement * scale,
            fit_error * scale,
            uncertainty_total_plus * scale,
            uncertainty_total_minus * scale,
        )
        text = "%s & $%.2f \pm %.2f (fit)^{+%.2f}_{-%.2f} (sys) \cdot 10^{-2}$\\\\ \n" % formatting
        if doSymmetricErrors:
            relativeError = getRelativeError(central_measurement, fit_error + uncertainty_total_plus)
            formatting = (
                variable_bins_latex[variable_bin],
                central_measurement * scale,
                fit_error * scale,
                uncertainty_total_plus * scale,
            )
            text = (
                "%s & $\\left(%.2f \\pm %.2f \\text{ (fit)} \pm %.2f \\text{ (syst.)}\\right)" % formatting
                + "(%.2f" % (relativeError * 100)
                + "\%) \\times 10^{-2}\, \\GeV^{-1}$\\\\ \n"
            )
        printout += text
        for source in uncertainty.keys():
            unc_result = uncertainty[source]
            if not uncertainties.has_key(source):
                if source in metsystematics_sources:
                    uncertainties[source] = metsystematics_sources_latex[source] + " & "
                else:
                    uncertainties[source] = source + " & "
            relativeError = getRelativeError(centralresult[0], unc_result[0])
            #            text = ' $(%.2f \pm %.2f) \cdot 10^{-2} $ ' % (unc_result[0]*scale,unc_result[1]*scale) + '(%.2f' % (relativeError * 100) + '\%) &'
            text = "%.2f" % (relativeError * 100) + "\% &"
            #            text = ' $%.2f \pm %.2f $ ' % (unc_result[0]*scale,unc_result[1]*scale) + '(%.2f' % (relativeError * 100) + '\%) &'
            uncertainties[source] += text

    printout += "\\\\ \n"
    for source in sorted(uncertainties.keys()):
        value = uncertainties[source]
        value = value.rstrip("&")
        value += "\\\\ \n"
        printout += value

    make_folder_if_not_exists(savePath + "/" + variable)
    if toFile:
        output_file = open(
            savePath
            + "/"
            + variable
            + "/normalised_xsection_main_result_"
            + channel
            + "_"
            + met_type
            + "_kv"
            + str(k_value)
            + ".tex",
            "w",
        )
        output_file.write(printout)
        output_file.close()
    else:
        print printout