def read_fit_templates_and_results_as_histograms( category, channel ):
    global path_to_JSON, variable, met_type, phase_space
    templates = read_data_from_JSON( path_to_JSON + '/fit_results/' + category + '/templates_' + channel + '_' + met_type + '.txt' )

    data_values = read_data_from_JSON( path_to_JSON + '/fit_results/' + category + '/initial_values_' + channel + '_' + met_type + '.txt' )['data']
    fit_results = read_data_from_JSON( path_to_JSON + '/fit_results/' + category + '/fit_results_' + channel + '_' + met_type + '.txt' )
    fit_variables = templates.keys()
    template_histograms = {fit_variable: {} for fit_variable in fit_variables}
    fit_results_histograms = {fit_variable: {} for fit_variable in fit_variables}

    variableBins = None
    if phase_space == 'VisiblePS':
        variableBins = variable_bins_visiblePS_ROOT
    elif phase_space == 'FullPS':
        variableBins = variable_bins_ROOT

    for bin_i, variable_bin in enumerate( variableBins[variable] ):
        for fit_variable in fit_variables:
            h_template_data = value_tuplelist_to_hist( templates[fit_variable]['data'][bin_i], fit_variable_bin_edges[fit_variable] )
            h_template_ttjet =  value_tuplelist_to_hist( templates[fit_variable]['TTJet'][bin_i], fit_variable_bin_edges[fit_variable] )
            h_template_singletop =  value_tuplelist_to_hist( templates[fit_variable]['SingleTop'][bin_i], fit_variable_bin_edges[fit_variable] )
            h_template_VJets = value_tuplelist_to_hist( templates[fit_variable]['V+Jets'][bin_i], fit_variable_bin_edges[fit_variable] )
            h_template_QCD = value_tuplelist_to_hist( templates[fit_variable]['QCD'][bin_i], fit_variable_bin_edges[fit_variable] )
            template_histograms[fit_variable][variable_bin] = {
                                        'TTJet' : h_template_ttjet,
                                        'SingleTop' : h_template_singletop,
                                        'V+Jets':h_template_VJets,
                                        'QCD':h_template_QCD
                                        }
            h_data = h_template_data.Clone()
            h_ttjet = h_template_ttjet.Clone()
            h_singletop = h_template_singletop.Clone()
            h_VJets = h_template_VJets.Clone()
            h_QCD = h_template_QCD.Clone()

            data_normalisation = data_values[bin_i][0]
            n_ttjet = fit_results['TTJet'][bin_i][0]
            n_singletop = fit_results['SingleTop'][bin_i][0]
            VJets_normalisation = fit_results['V+Jets'][bin_i][0]
            QCD_normalisation = fit_results['QCD'][bin_i][0]

            h_data.Scale( data_normalisation )
            h_ttjet.Scale( n_ttjet )
            h_singletop.Scale( n_singletop )
            h_VJets.Scale( VJets_normalisation )
            h_QCD.Scale( QCD_normalisation )
            h_background = h_VJets + h_QCD + h_singletop

            for bin_i_data in range( len( h_data ) ):
                h_data.SetBinError( bin_i_data + 1, sqrt( h_data.GetBinContent( bin_i_data + 1 ) ) )

            fit_results_histograms[fit_variable][variable_bin] = {
                                                    'data' : h_data,
                                                    'signal' : h_ttjet,
                                                    'background' : h_background
                                                    }

    return template_histograms, fit_results_histograms
def read_fit_templates_and_results_as_histograms(category, channel):
    global path_to_JSON, variable, met_type
    templates = read_data_from_JSON(path_to_JSON + '/' + variable +
                                    '/fit_results/' + category +
                                    '/templates_' + channel + '_' + met_type +
                                    '.txt')
    data_values = read_data_from_JSON(path_to_JSON + '/' + variable +
                                      '/fit_results/' + category +
                                      '/initial_values_' + channel + '_' +
                                      met_type + '.txt')['data']
    fit_results = read_data_from_JSON(path_to_JSON + '/' + variable +
                                      '/fit_results/' + category +
                                      '/fit_results_' + channel + '_' +
                                      met_type + '.txt')
    template_histograms = {}
    fit_results_histograms = {}
    for bin_i, variable_bin in enumerate(variable_bins_ROOT[variable]):
        h_template_data = value_tuplelist_to_hist(templates['data'][bin_i],
                                                  eta_bin_edges)
        h_template_signal = value_tuplelist_to_hist(templates['signal'][bin_i],
                                                    eta_bin_edges)
        h_template_VJets = value_tuplelist_to_hist(templates['V+Jets'][bin_i],
                                                   eta_bin_edges)
        h_template_QCD = value_tuplelist_to_hist(templates['QCD'][bin_i],
                                                 eta_bin_edges)
        template_histograms[variable_bin] = {
            'signal': h_template_signal,
            'V+Jets': h_template_VJets,
            'QCD': h_template_QCD
        }
        h_data = h_template_data.Clone()
        h_signal = h_template_signal.Clone()
        h_VJets = h_template_VJets.Clone()
        h_QCD = h_template_QCD.Clone()

        data_normalisation = data_values[bin_i]
        signal_normalisation = fit_results['signal'][bin_i][0]
        VJets_normalisation = fit_results['V+Jets'][bin_i][0]
        QCD_normalisation = fit_results['QCD'][bin_i][0]

        h_data.Scale(data_normalisation)
        h_signal.Scale(signal_normalisation)
        h_VJets.Scale(VJets_normalisation)
        h_QCD.Scale(QCD_normalisation)
        h_background = h_VJets + h_QCD

        for bin_i in range(len(h_data)):
            h_data.SetBinError(bin_i + 1,
                               sqrt(h_data.GetBinContent(bin_i + 1)))

        fit_results_histograms[variable_bin] = {
            'data': h_data,
            'signal': h_signal,
            'background': h_background
        }

    return template_histograms, fit_results_histograms
Exemple #3
0
def makePurityStabilityPlots(input_file, histogram, bin_edges, channel, variable, isVisiblePhaseSpace):
    global output_folder, output_formats
 
    hist = get_histogram_from_file( histogram, input_file )
    print "bin edges contents   : ", bin_edges
    new_hist = rebin_2d( hist, bin_edges, bin_edges ).Clone()

    # get_bin_content = hist.ProjectionX().GetBinContent
    purities = calculate_purities( new_hist.Clone() )
    stabilities = calculate_stabilities( new_hist.Clone() )
    # n_events = [int( get_bin_content( i ) ) for i in range( 1, len( bin_edges ) )]
    print "purities contents    : ", purities
    print "stabilities contents : ", stabilities

    hist_stability = value_tuplelist_to_hist(stabilities, bin_edges)
    hist_purity = value_tuplelist_to_hist(purities, bin_edges)

    hist_purity.color = 'red'
    hist_stability.color = 'blue'

    hist_stability.linewidth = 4
    hist_purity.linewidth = 4

    x_limits = [bin_edges[0], bin_edges[-1]]
    y_limits = [0,1]
    plt.figure( figsize = ( 20, 16 ), dpi = 200, facecolor = 'white' )

    ax0 = plt.axes()
    ax0.minorticks_on()
#     ax0.grid( True, 'major', linewidth = 2 )
#     ax0.grid( True, 'minor' )
    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )
    ax0.xaxis.labelpad = 12
    ax0.yaxis.labelpad = 12
    rplt.hist( hist_stability , stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = 'Stability' )
    rplt.hist( hist_purity, stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = 'Purity' )

    ax0.set_xlim( x_limits )
    ax0.set_ylim( y_limits )

    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )

    x_title = '$' + variables_latex[variable] + '$ [GeV]'
    plt.xlabel( x_title, CMS.x_axis_title )

    leg = plt.legend(loc=4,prop={'size':40})

    plt.tight_layout()

    plt.savefig('test.pdf')
    save_as_name = 'purityStability_'+channel + '_' + variable + '_' + str(options.CoM) + 'TeV'
    for output_format in output_formats:
        plt.savefig( output_folder + save_as_name + '.' + output_format )
def compare_combine_before_after_unfolding_uncertainties():
    file_template = 'data/normalisation/background_subtraction/13TeV/'
    file_template += '{variable}/VisiblePS/central/'
    file_template += 'unfolded_normalisation_{channel}_RooUnfoldSvd.txt'

    variables = ['MET', 'HT', 'ST', 'NJets',
                 'lepton_pt', 'abs_lepton_eta', 'WPT']
#     variables = ['ST']
    for variable in variables:
        beforeUnfolding = file_template.format(
            variable=variable, channel='combinedBeforeUnfolding')
        afterUnfolding = file_template.format(
            variable=variable, channel='combined')
        data = read_data_from_JSON(beforeUnfolding)
        before_unfolding = data['TTJet_measured']
        beforeUnfolding_data = data['TTJet_unfolded']
        afterUnfolding_data = read_data_from_JSON(afterUnfolding)['TTJet_unfolded']

        before_unfolding = [e / v * 100 for v, e in before_unfolding]
        beforeUnfolding_data = [e / v * 100 for v, e in beforeUnfolding_data]
        afterUnfolding_data = [e / v * 100 for v, e in afterUnfolding_data]

        h_beforeUnfolding = value_tuplelist_to_hist(
            beforeUnfolding_data, bin_edges_vis[variable])
        h_afterUnfolding = value_tuplelist_to_hist(
            afterUnfolding_data, bin_edges_vis[variable])
        h_before_unfolding = value_tuplelist_to_hist(
            before_unfolding, bin_edges_vis[variable])

        properties = Histogram_properties()
        properties.name = 'compare_combine_before_after_unfolding_uncertainties_{0}'.format(
            variable)
        properties.title = 'Comparison of unfolding uncertainties'
        properties.path = 'plots'
        properties.has_ratio = False
        properties.xerr = True
        properties.x_limits = (
            bin_edges_vis[variable][0], bin_edges_vis[variable][-1])
        properties.x_axis_title = variables_latex[variable]
        properties.y_axis_title = 'relative uncertainty (\\%)'
        properties.legend_location = (0.98, 0.95)

        histograms = {'Combine before unfolding': h_beforeUnfolding, 'Combine after unfolding': h_afterUnfolding,
                      # 'before unfolding': h_before_unfolding
                      }
        plot = Plot(histograms, properties)
        plot.draw_method = 'errorbar'
        compare_histograms(plot)
def compare_unfolding_uncertainties():
    file_template = '/hdfs/TopQuarkGroup/run2/dpsData/'
    file_template += 'data/normalisation/background_subtraction/13TeV/'
    file_template += '{variable}/VisiblePS/central/'
    file_template += 'unfolded_normalisation_combined_RooUnfold{method}.txt'

    variables = ['MET', 'HT', 'ST', 'NJets',
                 'lepton_pt', 'abs_lepton_eta', 'WPT']
#     variables = ['ST']
    for variable in variables:
        svd = file_template.format(
            variable=variable, method='Svd')
        bayes = file_template.format(
            variable=variable, method='Bayes')
        data = read_data_from_JSON(svd)
        before_unfolding = data['TTJet_measured_withoutFakes']
        svd_data = data['TTJet_unfolded']
        bayes_data = read_data_from_JSON(bayes)['TTJet_unfolded']

        before_unfolding = [e / v * 100 for v, e in before_unfolding]
        svd_data = [e / v * 100 for v, e in svd_data]
        bayes_data = [e / v * 100 for v, e in bayes_data]

        h_svd = value_tuplelist_to_hist(
            svd_data, bin_edges_vis[variable])
        h_bayes = value_tuplelist_to_hist(
            bayes_data, bin_edges_vis[variable])
        h_before_unfolding = value_tuplelist_to_hist(
            before_unfolding, bin_edges_vis[variable])

        properties = Histogram_properties()
        properties.name = 'compare_unfolding_uncertainties_{0}'.format(
            variable)
        properties.title = 'Comparison of unfolding uncertainties'
        properties.path = 'plots'
        properties.has_ratio = False
        properties.xerr = True
        properties.x_limits = (
            bin_edges_vis[variable][0], bin_edges_vis[variable][-1])
        properties.x_axis_title = variables_latex[variable]
        properties.y_axis_title = 'relative uncertainty (\\%)'
        properties.legend_location = (0.98, 0.95)

        histograms = {'SVD': h_svd, 'Bayes': h_bayes,
                      'before unfolding': h_before_unfolding}
        plot = Plot(histograms, properties)
        plot.draw_method = 'errorbar'
        compare_histograms(plot)
def run_test(test_data):
    ''' Used the test_data to fit the number of events for each process
    '''
    global config
    data_scale = 1.2
    fit_data_collection = FitDataCollection()
    for fit_variable, fit_input in test_data.iteritems():
        # create the histograms
        mc_histograms = {}
        for sample, h_input in fit_input.iteritems():
            mc_histograms[sample] = value_tuplelist_to_hist(
                h_input['distribution'], fit_variable_bin_edges[fit_variable])
        real_data = sum(mc_histograms[sample]
                        for sample in mc_histograms.keys())
        # scale data so that the fit does not start in the minimum
        real_data.Scale(data_scale)
        fit_data = FitData(real_data,
                           mc_histograms,
                           fit_boundaries=config.fit_boundaries[fit_variable])
        fit_data_collection.add(fit_data, fit_variable)
    # do fit
    fitter = Minuit(fit_data_collection)
    fitter.fit()
    fit_results = fitter.results
    # calculate chi2 for each sample
    chi2_results = {}
    for sample in fit_results.keys():
        true_normalisation = fit_input[sample]['normalisation'] * data_scale
        #         fit_result, fit_error = fit_results[sample]
        #         chi2 = pow( true_normalisation - fit_result, 2 ) / pow( fit_error, 2 )
        fit_result, _ = fit_results[sample]
        chi2 = pow(true_normalisation - fit_result, 2)
        chi2_results[sample] = chi2

    return chi2_results
def run_test ( test_data ):
    ''' Used the test_data to fit the number of events for each process
    '''
    global config
    data_scale = 1.2
    fit_data_collection = FitDataCollection()
    for fit_variable, fit_input in test_data.iteritems():
        # create the histograms
        mc_histograms = {}
        for sample, h_input in fit_input.iteritems():
            mc_histograms[sample] = value_tuplelist_to_hist( h_input['distribution'],
                                                             fit_variable_bin_edges[fit_variable] )
        real_data = sum( mc_histograms[sample] for sample in mc_histograms.keys() )
        # scale data so that the fit does not start in the minimum
        real_data.Scale( data_scale )
        fit_data = FitData( real_data, mc_histograms, fit_boundaries = config.fit_boundaries[fit_variable] )
        fit_data_collection.add( fit_data, fit_variable )
    # do fit
    fitter = Minuit( fit_data_collection )
    fitter.fit()
    fit_results = fitter.results
    # calculate chi2 for each sample
    chi2_results = {}
    for sample in fit_results.keys():
        true_normalisation = fit_input[sample]['normalisation'] * data_scale
#         fit_result, fit_error = fit_results[sample]
#         chi2 = pow( true_normalisation - fit_result, 2 ) / pow( fit_error, 2 )
        fit_result, _ = fit_results[sample]
        chi2 = pow( true_normalisation - fit_result, 2 )
        chi2_results[sample] = chi2
    
    return chi2_results
def plot_results(results):
    '''
    Takes results fo the form:
        {centre-of-mass-energy: {
            channel : {
                variable : {
                    fit_variable : {
                        test : { sample : []},
                        }
                    }
                }
            }
        }
    '''
    global options
    output_base = 'plots/fit_checks/chi2'
    for COMEnergy in results.keys():
        tmp_result_1 = results[COMEnergy]
        for channel in tmp_result_1.keys():
            tmp_result_2 = tmp_result_1[channel]
            for variable in tmp_result_2.keys():
                tmp_result_3 = tmp_result_2[variable]
                for fit_variable in tmp_result_3.keys():
                    tmp_result_4 = tmp_result_3[fit_variable]
                    # histograms should be {sample: {test : histogram}}
                    histograms = {}
                    for test, chi2 in tmp_result_4.iteritems():
                        for sample in chi2.keys():
                            if not histograms.has_key(sample):
                                histograms[sample] = {}
                            # reverse order of test and sample
                            histograms[sample][test] = value_tuplelist_to_hist(
                                chi2[sample], bin_edges_vis[variable])
                    for sample in histograms.keys():
                        hist_properties = Histogram_properties()
                        hist_properties.name = sample.replace('+',
                                                              '') + '_chi2'
                        hist_properties.title = '$\\chi^2$ distribution for fit output (' + sample + ')'
                        hist_properties.x_axis_title = '$' + latex_labels.variables_latex[
                            variable] + '$ [TeV]'
                        hist_properties.y_axis_title = '$\chi^2 = \\left({N_{fit}} - N_{{exp}}\\right)^2$'
                        hist_properties.set_log_y = True
                        hist_properties.y_limits = (1e-20, 1e20)
                        path = output_base + '/' + COMEnergy + 'TeV/' + channel + '/' + variable + '/' + fit_variable + '/'
                        if options.test:
                            path = output_base + '/test/'

                        measurements = {}
                        for test, histogram in histograms[sample].iteritems():
                            measurements[test.replace('_', ' ')] = histogram
                        compare_measurements(
                            {},
                            measurements,
                            show_measurement_errors=False,
                            histogram_properties=hist_properties,
                            save_folder=path,
                            save_as=['pdf'])
def read_fit_templates_and_results_as_histograms(category, channel):
    global path_to_JSON, variable, met_type
    templates = read_data_from_JSON(path_to_JSON + '/' + variable + '/fit_results/' + category + '/templates_' + channel + '_' + met_type + '.txt')
    data_values = read_data_from_JSON(path_to_JSON + '/' + variable + '/fit_results/' + category + '/initial_values_' + channel + '_' + met_type + '.txt')['data']
    fit_results = read_data_from_JSON(path_to_JSON + '/' + variable + '/fit_results/' + category + '/fit_results_' + channel + '_' + met_type + '.txt')
    template_histograms = {}
    fit_results_histograms = {}
    for bin_i, variable_bin in enumerate(variable_bins_ROOT[variable]):
        h_template_data = value_tuplelist_to_hist(templates['data'][bin_i], eta_bin_edges)
        h_template_signal = value_tuplelist_to_hist(templates['signal'][bin_i], eta_bin_edges)
        h_template_VJets = value_tuplelist_to_hist(templates['V+Jets'][bin_i], eta_bin_edges)
        h_template_QCD = value_tuplelist_to_hist(templates['QCD'][bin_i], eta_bin_edges)
        template_histograms[variable_bin] = {
                                    'signal':h_template_signal,
                                    'V+Jets':h_template_VJets,
                                    'QCD':h_template_QCD
                                    }
        h_data = h_template_data.Clone()
        h_signal = h_template_signal.Clone()
        h_VJets = h_template_VJets.Clone()
        h_QCD = h_template_QCD.Clone()
        
        data_normalisation = data_values[bin_i]
        signal_normalisation = fit_results['signal'][bin_i][0]
        VJets_normalisation = fit_results['V+Jets'][bin_i][0]
        QCD_normalisation = fit_results['QCD'][bin_i][0]
        
        h_data.Scale(data_normalisation)
        h_signal.Scale(signal_normalisation)
        h_VJets.Scale(VJets_normalisation)
        h_QCD.Scale(QCD_normalisation)
        h_background = h_VJets + h_QCD
        
        for bin_i in range(len(h_data)):
            h_data.SetBinError(bin_i+1, sqrt(h_data.GetBinContent(bin_i+1)))
        
        fit_results_histograms[variable_bin] = {
                                                'data':h_data,
                                                'signal':h_signal,
                                                'background':h_background
                                                }
        
    return template_histograms, fit_results_histograms
Exemple #10
0
def plotting_purity_stability(variable, channel, binning_criteria, bin_edges):
    '''
    Purity, stability and resolution plots.
    '''
    p = binning_criteria['p_i']
    s = binning_criteria['s_i']

    hist_stability = value_tuplelist_to_hist(s, bin_edges)
    hist_purity = value_tuplelist_to_hist(p, bin_edges)

    hist_purity.color = 'red'
    hist_stability.color = 'blue'

    hist_stability.linewidth = 4
    hist_purity.linewidth = 4

    fig = plt.figure(figsize=(20, 16), dpi=200, facecolor='white')
    axes = plt.axes()
    axes.minorticks_on()
    axes.set_xlim([bin_edges[0], bin_edges[-1]])
    axes.set_ylim([0, 1])

    axes.xaxis.labelpad = 12
    axes.yaxis.labelpad = 12

    rplt.hist(hist_stability, stacked=False, axes=axes, label='Stability')
    rplt.hist(hist_purity, stacked=False, axes=axes, label='Purity')

    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)

    x_title = '$' + variables_latex[variable] + '$'
    if variable in ['HT', 'ST', 'MET', 'lepton_pt', 'WPT']: x_title += '[GeV]'
    plt.xlabel(x_title, CMS.x_axis_title)

    leg = plt.legend(loc=4, prop={'size': 40})

    plt.tight_layout()

    plot_filepath = 'plots/binning/purity_stability/'
    make_folder_if_not_exists(plot_filepath)
    plot_filename = channel + '_' + variable + '_purityStability.pdf'
    fig.savefig(plot_filepath + plot_filename, bbox_inches='tight')
def plotting_purity_stability(variable, channel, binning_criteria, bin_edges ):
    '''
    Purity, stability and resolution plots.
    '''
    p = binning_criteria['p_i']
    s = binning_criteria['s_i']

    hist_stability = value_tuplelist_to_hist(s, bin_edges)
    hist_purity = value_tuplelist_to_hist(p, bin_edges)

    hist_purity.color = 'red'
    hist_stability.color = 'blue'

    hist_stability.linewidth = 4
    hist_purity.linewidth = 4

    fig = plt.figure( figsize = ( 20, 16 ), dpi = 200, facecolor = 'white' )
    axes = plt.axes()
    axes.minorticks_on()
    axes.set_xlim( [bin_edges[0], bin_edges[-1]] )
    axes.set_ylim( [0,1] )

    axes.xaxis.labelpad = 12
    axes.yaxis.labelpad = 12

    rplt.hist( hist_stability , stacked=False, axes = axes, label = 'Stability' )
    rplt.hist( hist_purity, stacked=False, axes = axes, label = 'Purity' )

    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )

    x_title = '$' + variables_latex[variable] + '$'
    if variable in ['HT', 'ST', 'MET', 'lepton_pt', 'WPT']: x_title += '[GeV]'
    plt.xlabel( x_title, CMS.x_axis_title )

    leg = plt.legend(loc=4,prop={'size':40})

    plt.tight_layout()

    plot_filepath = 'plots/binning/purity_stability/'
    make_folder_if_not_exists(plot_filepath)
    plot_filename = channel + '_' + variable+'_purityStability.pdf'
    fig.savefig(plot_filepath+plot_filename, bbox_inches='tight')
def plot_results ( results ):
    '''
    Takes results fo the form:
        {centre-of-mass-energy: {
            channel : {
                variable : {
                    fit_variable : {
                        test : { sample : []},
                        }
                    }
                }
            }
        }
    '''
    global options
    output_base = 'plots/fit_checks/chi2'
    for COMEnergy in results.keys():
        tmp_result_1 = results[COMEnergy]
        for channel in tmp_result_1.keys():
            tmp_result_2 = tmp_result_1[channel]
            for variable in tmp_result_2.keys():
                tmp_result_3 = tmp_result_2[variable]
                for fit_variable in tmp_result_3.keys():
                    tmp_result_4 = tmp_result_3[fit_variable]
                    # histograms should be {sample: {test : histogram}}
                    histograms = {}
                    for test, chi2 in tmp_result_4.iteritems():
                        for sample in chi2.keys():
                            if not histograms.has_key(sample):
                                histograms[sample] = {}
                            # reverse order of test and sample
                            histograms[sample][test] = value_tuplelist_to_hist(chi2[sample], bin_edges_vis[variable])
                    for sample in histograms.keys():
                        hist_properties = Histogram_properties()
                        hist_properties.name = sample.replace('+', '') + '_chi2'
                        hist_properties.title = '$\\chi^2$ distribution for fit output (' + sample + ')'
                        hist_properties.x_axis_title = '$' + latex_labels.variables_latex[variable] + '$ [TeV]'
                        hist_properties.y_axis_title = '$\chi^2 = \\left({N_{fit}} - N_{{exp}}\\right)^2$'
                        hist_properties.set_log_y = True
                        hist_properties.y_limits = (1e-20, 1e20)
                        path = output_base + '/' + COMEnergy + 'TeV/' + channel + '/' + variable + '/' + fit_variable + '/'
                        if options.test:
                            path = output_base + '/test/'
                        
                        measurements = {}
                        for test, histogram in histograms[sample].iteritems():
                            measurements[test.replace('_',' ')] = histogram
                        compare_measurements({}, 
                                             measurements, 
                                             show_measurement_errors = False, 
                                             histogram_properties = hist_properties, 
                                             save_folder = path, 
                                             save_as = ['pdf'])
Exemple #13
0
def scaleFSRHist(histogram_files):
    '''
	Scale the fsr histograms from 2 to sqrt(2)
	'''
    scale = sqrt(2) / 2
    up_scale, down_scale = [], []

    central = hist_to_value_list(histogram_files['TTJet'])
    fsrUp = hist_to_value_list(histogram_files['TTJet_fsrup'])
    fsrDown = hist_to_value_list(histogram_files['TTJet_fsrdown'])
    edges = hist_to_binEdges_list(histogram_files['TTJet'])

    for v_up, v_down, c in zip(fsrUp, fsrDown, central):
        diff_up = (v_up - c) * scale
        diff_down = (v_down - c) * scale

        up_scale.append(c + diff_up)
        down_scale.append(c + diff_down)

    fsrUpScale = value_tuplelist_to_hist(up_scale, edges)
    fsrDownScale = value_tuplelist_to_hist(down_scale, edges)

    return fsrUpScale, fsrDownScale
def scaleFSRHist(histogram_files):
	'''
	Scale the fsr histograms from 2 to sqrt(2)
	'''
	scale = sqrt(2) / 2
	up_scale, down_scale = [], []

	central = hist_to_value_list(histogram_files['TTJet'])
	fsrUp = hist_to_value_list(histogram_files['TTJet_fsrup'])
	fsrDown = hist_to_value_list(histogram_files['TTJet_fsrdown'])
	edges = hist_to_binEdges_list(histogram_files['TTJet'])

	for v_up, v_down, c in zip( fsrUp, fsrDown, central ):
		diff_up = ( v_up - c ) * scale
		diff_down = ( v_down - c ) * scale

		up_scale.append(c + diff_up)
		down_scale.append(c + diff_down)

	fsrUpScale = value_tuplelist_to_hist(up_scale, edges)
	fsrDownScale = value_tuplelist_to_hist(down_scale, edges)

	return fsrUpScale, fsrDownScale
Exemple #15
0
def read_xsection_measurement_results( category, channel, unc_type, scale_uncertanties=False ):
    '''
    Reading the unfolded xsection results from DFs into graphs
    '''
    global path_to_DF, variable, phase_space, method

    file_template = '{path}/{category}/xsection_{name}_{channel}_{method}{suffix}.txt'

    filename = file_template.format(
        path = path_to_DF,
        category = category,
        name = unc_type,
        channel = channel,
        method = method,
        suffix = '',
    )

    xsec_04_log.debug('Reading file {0}'.format(filename))

    edges = bin_edges_full[variable]
    if phase_space == 'VisiblePS':
        edges = bin_edges_vis[variable]

    # Collect the cross section measured/unfolded results from dataframes
    normalised_xsection_unfolded    = read_tuple_from_file( filename )

    # Create TTJets_Scale
    d_scale_syst = {}
    partonShower_uncertainties = measurement_config.list_of_systematics['TTJets_scale']
    for psUnc in partonShower_uncertainties:
        normalised_xsection_unfolded[psUnc] = [value for value, error in normalised_xsection_unfolded[psUnc]]
        d_scale_syst[psUnc] = normalised_xsection_unfolded[psUnc]

    normalised_xsection_unfolded['TTJets_scaledown'], normalised_xsection_unfolded['TTJets_scaleup'] = get_scale_envelope(
        d_scale_syst, 
        normalised_xsection_unfolded['TTJets_powhegPythia8'],
    )

    # Need to strip errors from central before passing to scaleFSR()
    central = [c[0] for c in normalised_xsection_unfolded['TTJets_powhegPythia8']]
    # Scale FSR
    if scale_uncertanties:
        normalised_xsection_unfolded['TTJets_fsrdown'] = scaleFSR(
            normalised_xsection_unfolded['TTJets_fsrdown'],
            central,
        )
        normalised_xsection_unfolded['TTJets_fsrup'] = scaleFSR(
            normalised_xsection_unfolded['TTJets_fsrup'], 
            central,
        )

    # h_normalised_xsection           = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_measured'], edges )
    h_normalised_xsection_unfolded  = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_unfolded'], edges )

    histograms_normalised_xsection_different_generators = {
        # 'measured':h_normalised_xsection,
        'unfolded':h_normalised_xsection_unfolded,
    }
    histograms_normalised_xsection_different_systematics = {
        'unfolded':h_normalised_xsection_unfolded,
    }

    if category == 'central':
        # Add in distributions for the different MC to be shown
        h_normalised_xsection_powhegPythia8     = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_powhegPythia8'], edges )
        h_normalised_xsection_amcatnlo          = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_amcatnloPythia8'], edges )
        h_normalised_xsection_madgraphMLM       = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_madgraphMLM'], edges )
        h_normalised_xsection_powhegHerwigpp    = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_powhegHerwig'], edges )
        # SCALE BREAKDOWN
        h_normalised_xsection_fsrup             = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_fsrup'], edges )
        h_normalised_xsection_fsrdown           = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_fsrdown'], edges )
        h_normalised_xsection_isrdown           = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_isrdown'], edges )
        h_normalised_xsection_isrup             = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_isrup'], edges )
        h_normalised_xsection_factorisationup   = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_factorisationup'], edges )
        h_normalised_xsection_factorisationdown = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_factorisationdown'], edges )
        h_normalised_xsection_renormalisationup = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_renormalisationup'], edges )
        h_normalised_xsection_renormalisationdown   = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_renormalisationdown'], edges )
        h_normalised_xsection_combinedup        = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_combinedup'], edges )
        h_normalised_xsection_combineddown      = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_combineddown'], edges )
        # PARTON SHOWER
        h_normalised_xsection_scaleup           = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_scaleup'], edges )
        h_normalised_xsection_scaledown         = value_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_scaledown'], edges )
        h_normalised_xsection_massup            = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_massup'], edges )
        h_normalised_xsection_massdown          = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_massdown'], edges )
        h_normalised_xsection_ueup              = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_ueup'], edges )
        h_normalised_xsection_uedown            = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_uedown'], edges )
        h_normalised_xsection_hdampup           = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_hdampup'], edges )
        h_normalised_xsection_hdampdown         = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_hdampdown'], edges )
        h_normalised_xsection_erdOn             = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_erdOn'], edges )
        h_normalised_xsection_QCDbased_erdOn    = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_QCDbased_erdOn'], edges )
        # h_normalised_xsection_GluonMove         = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_GluonMove'], edges )
        h_normalised_xsection_semiLepBrup       = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_semiLepBrup'], edges )
        h_normalised_xsection_semiLepBrdown     = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_semiLepBrdown'], edges )
        h_normalised_xsection_fragup            = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_fragup'], edges )
        h_normalised_xsection_fragdown          = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_fragdown'], edges )
        h_normalised_xsection_petersonFrag      = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_petersonFrag'], edges )
        # OTHER
        # h_normalised_xsection_alphaSup          = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_alphaSup'], edges )
        # h_normalised_xsection_alphaSdown        = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_alphaSdown'], edges )
        h_normalised_xsection_topPt             = value_error_tuplelist_to_hist( normalised_xsection_unfolded['TTJets_topPt'], edges )

        # And update
        histograms_normalised_xsection_different_generators.update( 
            {
                'TTJets_powhegPythia8'   : h_normalised_xsection_powhegPythia8,
                'TTJets_amcatnloPythia8' : h_normalised_xsection_amcatnlo,
                'TTJets_madgraphMLM'     : h_normalised_xsection_madgraphMLM,
                'TTJets_powhegHerwig'    : h_normalised_xsection_powhegHerwigpp,
            }
        )

        if scale_uncertanties:
            histograms_normalised_xsection_different_systematics.update( 
                {
                    'TTJets_powhegPythia8'  : h_normalised_xsection_powhegPythia8,
                    'TTJets_fsrup'          : h_normalised_xsection_fsrup,
                    'TTJets_fsrdown'        : h_normalised_xsection_fsrdown,
                    'TTJets_isrdown'        : h_normalised_xsection_isrdown,
                    'TTJets_isrup'          : h_normalised_xsection_isrup,
                    'TTJets_factorisationup'    : h_normalised_xsection_factorisationup,
                    'TTJets_factorisationdown'  : h_normalised_xsection_factorisationdown,
                    'TTJets_renormalisationup'  : h_normalised_xsection_renormalisationup,
                    'TTJets_renormalisationdown'    : h_normalised_xsection_renormalisationdown,
                    'TTJets_combinedup'     : h_normalised_xsection_combinedup,
                    'TTJets_combineddown'   : h_normalised_xsection_combineddown,
                }
            )
        else:
            histograms_normalised_xsection_different_systematics.update( 
                {
                    'TTJets_powhegPythia8'  : h_normalised_xsection_powhegPythia8,
                    'TTJets_scaleup'        : h_normalised_xsection_scaleup,
                    'TTJets_scaledown'      : h_normalised_xsection_scaledown,
                    # 'TTJets_massup'         : h_normalised_xsection_massup,
                    # 'TTJets_massdown'       : h_normalised_xsection_massdown,
                    # 'TTJets_ueup'           : h_normalised_xsection_ueup,
                    # 'TTJets_uedown'         : h_normalised_xsection_uedown,
                    'TTJets_hdampup'        : h_normalised_xsection_hdampup,
                    'TTJets_hdampdown'      : h_normalised_xsection_hdampdown,
                    # 'TTJets_erdOn'          : h_normalised_xsection_erdOn,
                    # 'TTJets_QCDbased_erdOn' : h_normalised_xsection_QCDbased_erdOn,
                    # 'TTJets_GluonMove'      : h_normalised_xsection_GluonMove,

                    # 'TTJets_semiLepBrup'    : h_normalised_xsection_semiLepBrup,
                    # 'TTJets_semiLepBrdown'  : h_normalised_xsection_semiLepBrdown,
                    # 'TTJets_fragup'         : h_normalised_xsection_fragup,
                    # 'TTJets_fragdown'       : h_normalised_xsection_fragdown,
                    # 'TTJets_petersonFrag'   : h_normalised_xsection_petersonFrag,

                    'TTJets_topPt'          : h_normalised_xsection_topPt,
                }
            )

        filename = file_template.format(
            path = path_to_DF,
            category = category,
            name = unc_type,
            channel = channel,
            method = method,
            suffix = '_summary_absolute',
        )

        # Now for the systematic uncertainties
        normalised_xsection_unfolded_with_errors = file_to_df( filename )
        normalised_xsection_unfolded_with_errors['TTJets_unfolded'] = tupleise_cols(
            normalised_xsection_unfolded_with_errors['central'], 
            normalised_xsection_unfolded_with_errors['systematic'],
        )

        xsec_04_log.debug('Reading file {0}'.format(filename))

        # Transform unfolded data into graph form
        h_normalised_xsection_unfolded_with_errors_unfolded = value_errors_tuplelist_to_graph(
            normalised_xsection_unfolded_with_errors['TTJets_unfolded'],
            edges, 
            is_symmetric_errors=True
        )

        # Add to list of histograms
        histograms_normalised_xsection_different_generators['unfolded_with_systematics'] = h_normalised_xsection_unfolded_with_errors_unfolded
        histograms_normalised_xsection_different_systematics['unfolded_with_systematics'] = h_normalised_xsection_unfolded_with_errors_unfolded

    return histograms_normalised_xsection_different_generators, histograms_normalised_xsection_different_systematics
def main():
	########## 			SETUP 			##########
	t = time.time()
	gStyle.SetOptStat("")

	#################################################################################################################################
	# READ THE OPTIONS
	#################################################################################################################################
	parser = ArgumentParser()
	parser.add_argument("-t", "--test", dest="test", action = "store_true", 
		help="Run over a few events only")
	parser.add_argument("-p", "--plots", dest="make_plots", action = "store_true", 
    	help="Print out files to .png")
	parser.add_argument("-o", "--only_plots", dest="only_plots", action = "store_true", 
    	help="Print out files to .png")
	parser.add_argument("-d", "--debug", dest="debug", action = "store_true", 
    	help="Run debugger")
	args = parser.parse_args()
	if args.test : print "RUNNING OVER TEST SAMPLE"
	debug = args.debug

	#################################################################################################################################
	# ADD THE GENERATOR INPUT FILES
	#################################################################################################################################
	samples = [
		"PowhegPythia8",
		"PowhegHerwigpp",
		"aMCatNLOPythia8",
		"Madgraph",
		"PowhegPythia8_fsrup", 
		"PowhegPythia8_fsrdown", 
		"PowhegPythia8_isrup", 
		"PowhegPythia8_isrdown", 
		# "PowhegPythia8_ueup", 
		# "PowhegPythia8_uedown", 
		# "PowhegPythia8_mtop1755", 
		# "PowhegPythia8_mtop1695", 
		# "PowhegPythia8_hdampup", 
		# "PowhegPythia8_hdampdown", 
		# "PowhegPythia8_erdOn", 
		# "PowhegPythia8_QCDbased_erdOn", 
		# "PowhegPythia8_GluonMove", 
	]
	if debug:
		print "Constructed Input Files and Read Arguments"
		t = get_time(t)

	#################################################################################################################################
	# INITIALISE THE BINNING AND WEIGHTS
	#################################################################################################################################
	pt_binning = [30, 50, 70, 100, 140, 200, 300, 600, 1000]
	# pt_binning = [30, 50, 70, 100, 140, 200]
	barrel_endcap_split = 1.3

	if debug:
		print "Initialised Pt and Eta binning"
		t = get_time(t)

	#################################################################################################################################
	# INITIALISE THE INPUT AND OUTPUT PATHS
	#################################################################################################################################
	file_path = 'dps/experimental/DougsJESEff/JESEfficiency.root'

	output_file = root_open(file_path, "recreate")
	make_folder_if_not_exists('plots/JetEfficiency/Summary/')


	#################################################################################################################################
	# CALCULATE THE JET PT / PSEUDOJET PT GAUSSIANS
	#################################################################################################################################
	r = {}
	for sample in samples:
		make_folder_if_not_exists('plots/JetEfficiency/'+sample)

		r[sample] = {}
		r[sample]['BarrelLightJet'] = {}
		r[sample]['BarrelBJet'] 	= {}
		r[sample]['EndcapLightJet'] = {}
		r[sample]['EndcapBJet'] 	= {}
		for i in range (1, len(pt_binning)):
			r[sample]['BarrelLightJet'][i] 	= Hist(50, 0, 2, name='Residuals_Bin_BarrelLightJet_'+sample+'_'+str(i))
			r[sample]['BarrelBJet'][i] 		= Hist(50, 0, 2, name='Residuals_Bin_BarrelBJet_'+sample+'_'+str(i))
			r[sample]['EndcapLightJet'][i] 	= Hist(50, 0, 2, name='Residuals_Bin_EndcapLightJet_'+sample+'_'+str(i))
			r[sample]['EndcapBJet'][i] 		= Hist(50, 0, 2, name='Residuals_Bin_EndcapBJet_'+sample+'_'+str(i))

		file_name = getFileName(sample, measurement_config)
		print "Calculating JES Efficiency For Generator : ",sample
		treeName = "TTbar_plus_X_analysis/JESAnalyser/JESAnalyser"
		file_name = getUnmergedDirectory(file_name)

		tree = ROOT.TChain(treeName);
		filenames = glob.glob( file_name )
		for f in filenames:
			tree.Add(f)

		nEntries = tree.GetEntries()
		print 'Number of entries:',nEntries
		n=0
		for event in tree:
			branch = event.__getattr__
			n+=1
			if not n%1000000: print 'Processing event %.0f Progress : %.2g %%' % ( n, float(n)/nEntries*100 )
			# if n > 1000: break

			jetPts = branch('jetPt')
			isBJets = branch('isB')
			isBarrels = branch('isBarrel')
			recoGenRatios = branch('recoGenRatio')

			for jpt, isBJet, isBarrel, recoGenRatio in zip(jetPts, isBJets, isBarrels, recoGenRatios):
				if jpt <= 30: continue
				# Find Jet Pt Bin to Fill
				for i, edge in enumerate(pt_binning):
					if jpt > edge: 
						continue
					else: 
						break

				# DEBUG PURPOSES
				# print "- "*30
				# print "Jet Pt : {}".format(jpt)
				# print "is a BJet : {}".format(isBJet)
				# print "is in Barrel : {}".format(isBarrel)
				# print "recoGen ratio : {}".format(recoGenRatio)
				# print "filled in Bin {}".format(i)
				# raw_input()

				if isBarrel:
					if isBJet:
						r[sample]['BarrelBJet'][i].Fill(recoGenRatio)
					else:
						r[sample]['BarrelLightJet'][i].Fill(recoGenRatio)
				else:
					if isBJet:
						r[sample]['EndcapBJet'][i].Fill(recoGenRatio)
					else:
						r[sample]['EndcapLightJet'][i].Fill(recoGenRatio)

	#################################################################################################################################
	# FIT THE GAUSSIANS AND CREATE AVERAGE JET PT / PSEUDOJET PT HISTOGRAMS
	#################################################################################################################################
	h_mean = {}
	for sample in samples:
		h_mean[sample] = {}
		for quadrant in ['BarrelLightJet', 'BarrelBJet', 'EndcapLightJet', 'EndcapBJet']:
			gaussian_fit = []
			for i, hist in r[sample][quadrant].iteritems():
				try:
					f = hist.Fit("gaus", "SMQ","", 0, 2)
					mean = f.Parameter(1)
				except:
					# Jet pt 20-30 not available
					mean = -1
					print "Cannot fit histogram"
				gaussian_fit.append(mean)

				# Plot individual pt bins
				canvas = TCanvas("c","c", 0, 0, 800, 600)
				hist.SetTitle(" ; pt; jet pT / pseudo jet pT")
				hist.Draw()
				canvas.Update()
				canvas.SaveAs('plots/JetEfficiency/'+sample+"/"+quadrant+"_"+str(i)+"_JetEfficiency.png")

			# Combine <pt ratio> into one hist
			h_mean[sample][quadrant] = value_tuplelist_to_hist(gaussian_fit, pt_binning)
			canvas = TCanvas("c","c", 0, 0, 800, 600)
			h_mean[sample][quadrant].SetTitle("JES efficiency ; pt; <jet over genjet>")
			h_mean[sample][quadrant].Draw()
			canvas.Update()
			canvas.SaveAs('plots/JetEfficiency/'+sample+"/"+quadrant+"_JetAveEfficiency.png")

	#################################################################################################################################
	# FIND THE CORRECTION, RATIO OF <Central Pt> / <Variation Pt>
	#################################################################################################################################
	h_jes = {}
	for sample in samples:
		h_jes[sample] = {}
		for quadrant in ['BarrelLightJet', 'BarrelBJet', 'EndcapLightJet', 'EndcapBJet']:

			h_jes[sample][quadrant] = h_mean['PowhegPythia8'][quadrant].Clone()
			h_jes[sample][quadrant].Divide(h_jes[sample][quadrant],h_mean[sample][quadrant],1,1,"B")

			canvas = TCanvas("c","c", 0, 0, 800, 600)
			h_jes[sample][quadrant].SetTitle("JES efficiency ; pt; <jet over genjet> over <jet over genjet>")
			h_jes[sample][quadrant].Draw()
			canvas.Update()
			canvas.SaveAs('plots/JetEfficiency/Summary/'+sample+"_"+quadrant+"_JetCorrection.png")



	colours = [
		'red', 'blue', 'green', 'chartreuse', 'indigo', 
		'magenta', 'darkmagenta', 'hotpink', 'cyan', 'darkred', 
		'darkgoldenrod', 'mediumvioletred', 'mediumspringgreen', 
		'gold', 'darkgoldenrod', 'slategray', 'dodgerblue', 
		'cadetblue', 'darkblue', 'seagreen', 'deeppink', 'deepskyblue' 
	]
	
	for quadrant in ['BarrelLightJet', 'BarrelBJet', 'EndcapLightJet', 'EndcapBJet']:
		i=0
		fig_eff = plt.figure( figsize = ( 20, 16 ), dpi = 400, facecolor = 'white' )
		ax_eff = fig_eff.add_subplot(1, 1, 1)
		ax_eff.minorticks_on()
		ax_eff.xaxis.labelpad = 12
		ax_eff.yaxis.labelpad = 12
		ax_eff.set_ylim( 0, 1 )
		plt.tick_params( **CMS.axis_label_major )
		plt.tick_params( **CMS.axis_label_minor )

		y_limits = [0.95,1.05]
		y_title = '<jet over genjet> / <jet over genjet>'
		x_title = 'Jet Pt [GeV]'
		plt.xlabel( x_title, CMS.x_axis_title )
		plt.ylabel( y_title, CMS.y_axis_title)

		template = '%.1f fb$^{-1}$ (%d TeV)'
		label = template % ( measurement_config.new_luminosity/1000., measurement_config.centre_of_mass_energy)
		plt.title( label,loc='right', **CMS.title )

		for sample, hists in h_jes.iteritems():
			hist = hists[quadrant]
			hist.linewidth = 4
			hist.color = 'black'
			l = sample
			if sample != 'PowhegPythia8':
				hist.linestyle = 'dashed'
				hist.linewidth = 2
				if 'aMCatNLOPythia8' in sample:
					hist.color = 'blue'
					hist.linestyle = 'solid'
				if 'PowhegHerwigpp' in sample:
					hist.color = 'red'
					hist.linestyle = 'solid'
				if 'Madgraph' in sample:
					hist.color = 'green'
					hist.linestyle = 'solid'
				if 'fsr' in sample:
					hist.color = 'hotpink'
					l = l.replace('down', '')
					if 'up' in sample: 	l = ''
				if 'isr' in sample:
					hist.color = 'cyan'
					l = l.replace('down', '')
					if 'up' in sample: 	l = ''

			rplt.hist( hist, stacked=False, label = l )
			i+=1

		# plot legend
		leg = plt.legend(loc='best',prop={'size':25},ncol=2)
		leg.draw_frame(False)	
		ax_eff.set_ylim( y_limits )

		# plt.text(0.05, 0.97, 
		#     r"{}".format(quadrant), 
		#     transform=ax_eff.transAxes, 
		#     fontsize=42,
		#     verticalalignment='top',
		#     horizontalalignment='left'
		# )

		plt.tight_layout()

		fig_eff.savefig('plots/JetEfficiency/Summary/'+quadrant+"_JetCorrections.pdf")
def main():
    ########## 			SETUP 			##########
    t = time.time()
    gStyle.SetOptStat("")

    #################################################################################################################################
    # READ THE OPTIONS
    #################################################################################################################################
    parser = ArgumentParser()
    parser.add_argument("-t",
                        "--test",
                        dest="test",
                        action="store_true",
                        help="Run over a few events only")
    parser.add_argument("-p",
                        "--plots",
                        dest="make_plots",
                        action="store_true",
                        help="Print out files to .png")
    parser.add_argument("-o",
                        "--only_plots",
                        dest="only_plots",
                        action="store_true",
                        help="Print out files to .png")
    parser.add_argument("-d",
                        "--debug",
                        dest="debug",
                        action="store_true",
                        help="Run debugger")
    args = parser.parse_args()
    if args.test: print "RUNNING OVER TEST SAMPLE"
    debug = args.debug

    #################################################################################################################################
    # ADD THE GENERATOR INPUT FILES
    #################################################################################################################################
    samples = [
        "PowhegPythia8",
        "PowhegHerwigpp",
        "aMCatNLOPythia8",
        "Madgraph",
        "PowhegPythia8_fsrup",
        "PowhegPythia8_fsrdown",
        "PowhegPythia8_isrup",
        "PowhegPythia8_isrdown",
        # "PowhegPythia8_ueup",
        # "PowhegPythia8_uedown",
        # "PowhegPythia8_mtop1755",
        # "PowhegPythia8_mtop1695",
        # "PowhegPythia8_hdampup",
        # "PowhegPythia8_hdampdown",
        # "PowhegPythia8_erdOn",
        # "PowhegPythia8_QCDbased_erdOn",
        # "PowhegPythia8_GluonMove",
    ]
    if debug:
        print "Constructed Input Files and Read Arguments"
        t = get_time(t)

    #################################################################################################################################
    # INITIALISE THE BINNING AND WEIGHTS
    #################################################################################################################################
    pt_binning = [30, 50, 70, 100, 140, 200, 300, 600, 1000]
    # pt_binning = [30, 50, 70, 100, 140, 200]
    barrel_endcap_split = 1.3

    if debug:
        print "Initialised Pt and Eta binning"
        t = get_time(t)

    #################################################################################################################################
    # INITIALISE THE INPUT AND OUTPUT PATHS
    #################################################################################################################################
    file_path = 'dps/experimental/DougsJESEff/JESEfficiency.root'

    output_file = root_open(file_path, "recreate")
    make_folder_if_not_exists('plots/JetEfficiency/Summary/')

    #################################################################################################################################
    # CALCULATE THE JET PT / PSEUDOJET PT GAUSSIANS
    #################################################################################################################################
    r = {}
    for sample in samples:
        make_folder_if_not_exists('plots/JetEfficiency/' + sample)

        r[sample] = {}
        r[sample]['BarrelLightJet'] = {}
        r[sample]['BarrelBJet'] = {}
        r[sample]['EndcapLightJet'] = {}
        r[sample]['EndcapBJet'] = {}
        for i in range(1, len(pt_binning)):
            r[sample]['BarrelLightJet'][i] = Hist(
                50,
                0,
                2,
                name='Residuals_Bin_BarrelLightJet_' + sample + '_' + str(i))
            r[sample]['BarrelBJet'][i] = Hist(
                50,
                0,
                2,
                name='Residuals_Bin_BarrelBJet_' + sample + '_' + str(i))
            r[sample]['EndcapLightJet'][i] = Hist(
                50,
                0,
                2,
                name='Residuals_Bin_EndcapLightJet_' + sample + '_' + str(i))
            r[sample]['EndcapBJet'][i] = Hist(
                50,
                0,
                2,
                name='Residuals_Bin_EndcapBJet_' + sample + '_' + str(i))

        file_name = getFileName(sample, measurement_config)
        print "Calculating JES Efficiency For Generator : ", sample
        treeName = "TTbar_plus_X_analysis/JESAnalyser/JESAnalyser"
        file_name = getUnmergedDirectory(file_name)

        tree = ROOT.TChain(treeName)
        filenames = glob.glob(file_name)
        for f in filenames:
            tree.Add(f)

        nEntries = tree.GetEntries()
        print 'Number of entries:', nEntries
        n = 0
        for event in tree:
            branch = event.__getattr__
            n += 1
            if not n % 1000000:
                print 'Processing event %.0f Progress : %.2g %%' % (
                    n, float(n) / nEntries * 100)
            # if n > 1000: break

            jetPts = branch('jetPt')
            isBJets = branch('isB')
            isBarrels = branch('isBarrel')
            recoGenRatios = branch('recoGenRatio')

            for jpt, isBJet, isBarrel, recoGenRatio in zip(
                    jetPts, isBJets, isBarrels, recoGenRatios):
                if jpt <= 30: continue
                # Find Jet Pt Bin to Fill
                for i, edge in enumerate(pt_binning):
                    if jpt > edge:
                        continue
                    else:
                        break

                # DEBUG PURPOSES
                # print "- "*30
                # print "Jet Pt : {}".format(jpt)
                # print "is a BJet : {}".format(isBJet)
                # print "is in Barrel : {}".format(isBarrel)
                # print "recoGen ratio : {}".format(recoGenRatio)
                # print "filled in Bin {}".format(i)
                # raw_input()

                if isBarrel:
                    if isBJet:
                        r[sample]['BarrelBJet'][i].Fill(recoGenRatio)
                    else:
                        r[sample]['BarrelLightJet'][i].Fill(recoGenRatio)
                else:
                    if isBJet:
                        r[sample]['EndcapBJet'][i].Fill(recoGenRatio)
                    else:
                        r[sample]['EndcapLightJet'][i].Fill(recoGenRatio)

    #################################################################################################################################
    # FIT THE GAUSSIANS AND CREATE AVERAGE JET PT / PSEUDOJET PT HISTOGRAMS
    #################################################################################################################################
    h_mean = {}
    for sample in samples:
        h_mean[sample] = {}
        for quadrant in [
                'BarrelLightJet', 'BarrelBJet', 'EndcapLightJet', 'EndcapBJet'
        ]:
            gaussian_fit = []
            for i, hist in r[sample][quadrant].iteritems():
                try:
                    f = hist.Fit("gaus", "SMQ", "", 0, 2)
                    mean = f.Parameter(1)
                except:
                    # Jet pt 20-30 not available
                    mean = -1
                    print "Cannot fit histogram"
                gaussian_fit.append(mean)

                # Plot individual pt bins
                canvas = TCanvas("c", "c", 0, 0, 800, 600)
                hist.SetTitle(" ; pt; jet pT / pseudo jet pT")
                hist.Draw()
                canvas.Update()
                canvas.SaveAs('plots/JetEfficiency/' + sample + "/" +
                              quadrant + "_" + str(i) + "_JetEfficiency.png")

            # Combine <pt ratio> into one hist
            h_mean[sample][quadrant] = value_tuplelist_to_hist(
                gaussian_fit, pt_binning)
            canvas = TCanvas("c", "c", 0, 0, 800, 600)
            h_mean[sample][quadrant].SetTitle(
                "JES efficiency ; pt; <jet over genjet>")
            h_mean[sample][quadrant].Draw()
            canvas.Update()
            canvas.SaveAs('plots/JetEfficiency/' + sample + "/" + quadrant +
                          "_JetAveEfficiency.png")

    #################################################################################################################################
    # FIND THE CORRECTION, RATIO OF <Central Pt> / <Variation Pt>
    #################################################################################################################################
    h_jes = {}
    for sample in samples:
        h_jes[sample] = {}
        for quadrant in [
                'BarrelLightJet', 'BarrelBJet', 'EndcapLightJet', 'EndcapBJet'
        ]:

            h_jes[sample][quadrant] = h_mean['PowhegPythia8'][quadrant].Clone()
            h_jes[sample][quadrant].Divide(h_jes[sample][quadrant],
                                           h_mean[sample][quadrant], 1, 1, "B")

            canvas = TCanvas("c", "c", 0, 0, 800, 600)
            h_jes[sample][quadrant].SetTitle(
                "JES efficiency ; pt; <jet over genjet> over <jet over genjet>"
            )
            h_jes[sample][quadrant].Draw()
            canvas.Update()
            canvas.SaveAs('plots/JetEfficiency/Summary/' + sample + "_" +
                          quadrant + "_JetCorrection.png")

    colours = [
        'red', 'blue', 'green', 'chartreuse', 'indigo', 'magenta',
        'darkmagenta', 'hotpink', 'cyan', 'darkred', 'darkgoldenrod',
        'mediumvioletred', 'mediumspringgreen', 'gold', 'darkgoldenrod',
        'slategray', 'dodgerblue', 'cadetblue', 'darkblue', 'seagreen',
        'deeppink', 'deepskyblue'
    ]

    for quadrant in [
            'BarrelLightJet', 'BarrelBJet', 'EndcapLightJet', 'EndcapBJet'
    ]:
        i = 0
        fig_eff = plt.figure(figsize=(20, 16), dpi=400, facecolor='white')
        ax_eff = fig_eff.add_subplot(1, 1, 1)
        ax_eff.minorticks_on()
        ax_eff.xaxis.labelpad = 12
        ax_eff.yaxis.labelpad = 12
        ax_eff.set_ylim(0, 1)
        plt.tick_params(**CMS.axis_label_major)
        plt.tick_params(**CMS.axis_label_minor)

        y_limits = [0.95, 1.05]
        y_title = '<jet over genjet> / <jet over genjet>'
        x_title = 'Jet Pt [GeV]'
        plt.xlabel(x_title, CMS.x_axis_title)
        plt.ylabel(y_title, CMS.y_axis_title)

        template = '%.1f fb$^{-1}$ (%d TeV)'
        label = template % (measurement_config.new_luminosity / 1000.,
                            measurement_config.centre_of_mass_energy)
        plt.title(label, loc='right', **CMS.title)

        for sample, hists in h_jes.iteritems():
            hist = hists[quadrant]
            hist.linewidth = 4
            hist.color = 'black'
            l = sample
            if sample != 'PowhegPythia8':
                hist.linestyle = 'dashed'
                hist.linewidth = 2
                if 'aMCatNLOPythia8' in sample:
                    hist.color = 'blue'
                    hist.linestyle = 'solid'
                if 'PowhegHerwigpp' in sample:
                    hist.color = 'red'
                    hist.linestyle = 'solid'
                if 'Madgraph' in sample:
                    hist.color = 'green'
                    hist.linestyle = 'solid'
                if 'fsr' in sample:
                    hist.color = 'hotpink'
                    l = l.replace('down', '')
                    if 'up' in sample: l = ''
                if 'isr' in sample:
                    hist.color = 'cyan'
                    l = l.replace('down', '')
                    if 'up' in sample: l = ''

            rplt.hist(hist, stacked=False, label=l)
            i += 1

        # plot legend
        leg = plt.legend(loc='best', prop={'size': 25}, ncol=2)
        leg.draw_frame(False)
        ax_eff.set_ylim(y_limits)

        # plt.text(0.05, 0.97,
        #     r"{}".format(quadrant),
        #     transform=ax_eff.transAxes,
        #     fontsize=42,
        #     verticalalignment='top',
        #     horizontalalignment='left'
        # )

        plt.tight_layout()

        fig_eff.savefig('plots/JetEfficiency/Summary/' + quadrant +
                        "_JetCorrections.pdf")