def plot_bias(h_unfold_model, h_data_model, unfolded_data, variable, channel, come, method): hp = Histogram_properties() hp.name = '{channel}_bias_test_for_{variable}_at_{come}TeV'.format( channel=channel, variable=variable, come=come, ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit hp.y_axis_title = 'Events' hp.title = 'Closure tests for {variable}'.format(variable=v_latex) output_folder = 'plots/unfolding/bias_test/{0}/'.format(method) compare_measurements(models={'MC truth': h_data_model, 'unfold model': h_unfold_model}, measurements={'unfolded reco': unfolded_data}, show_measurement_errors=True, histogram_properties=hp, save_folder=output_folder, save_as=['png', 'pdf'])
def plot_bias(h_unfold_model, h_data_model, unfolded_data, variable, channel, come, method): hp = Histogram_properties() hp.name = '{channel}_bias_test_for_{variable}_at_{come}TeV'.format( channel=channel, variable=variable, come=come, ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit hp.y_axis_title = 'Events' hp.title = 'Closure tests for {variable}'.format(variable=v_latex) output_folder = 'plots/unfolding/bias_test/{0}/'.format(method) compare_measurements(models={ 'MC truth': h_data_model, 'unfold model': h_unfold_model }, measurements={'unfolded reco': unfolded_data}, show_measurement_errors=True, histogram_properties=hp, save_folder=output_folder, save_as=['png', 'pdf'])
def plot_bias_in_all_bins(biases, mean_bias, centre_of_mass, channel, variable, tau_value, output_folder, output_formats, bin_edges): h_bias = Hist(bin_edges, type='D') n_bins = h_bias.nbins() assert len(biases) == n_bins for i, bias in enumerate(biases): h_bias.SetBinContent(i + 1, bias) histogram_properties = Histogram_properties() name_mpt = 'bias_{0}_{1}_{2}TeV' histogram_properties.name = name_mpt.format(variable, channel, centre_of_mass) histogram_properties.y_axis_title = 'Bias' histogram_properties.x_axis_title = latex_labels.variables_latex[variable] title = 'pull distribution mean \& sigma for {0}'.format(tau_value) histogram_properties.title = title histogram_properties.y_limits = [0, 10] histogram_properties.xerr = True compare_measurements( models={'Mean bias': make_line_hist(bin_edges, mean_bias)}, measurements={'Bias': h_bias}, show_measurement_errors=True, histogram_properties=histogram_properties, save_folder=output_folder, save_as=output_formats)
def plot_bias_in_all_bins(biases, mean_bias, centre_of_mass, channel, variable, tau_value, output_folder, output_formats, bin_edges): h_bias = Hist(bin_edges, type='D') n_bins = h_bias.nbins() assert len(biases) == n_bins for i, bias in enumerate(biases): h_bias.SetBinContent(i+1, bias) histogram_properties = Histogram_properties() name_mpt = 'bias_{0}_{1}_{2}TeV' histogram_properties.name = name_mpt.format( variable, channel, centre_of_mass ) histogram_properties.y_axis_title = 'Bias' histogram_properties.x_axis_title = latex_labels.variables_latex[variable] title = 'pull distribution mean \& sigma for {0}'.format(tau_value) histogram_properties.title = title histogram_properties.y_limits = [0, 10] histogram_properties.xerr = True compare_measurements( models={ 'Mean bias':make_line_hist(bin_edges, mean_bias) }, measurements={ 'Bias': h_bias }, show_measurement_errors=True, histogram_properties=histogram_properties, save_folder=output_folder, save_as=output_formats)
def plot_closure(unfolded_and_truths, variable, channel, come, method): hp = Histogram_properties() hp.name = '{channel}_closure_test_for_{variable}_at_{come}TeV'.format( channel=channel, variable=variable, come=come, ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT', 'lepton_pt']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit # plt.ylabel( r, CMS.y_axis_title ) hp.y_axis_title = r'$\frac{1}{\sigma} \frac{d\sigma}{d' + v_latex + '}$' + unit hp.title = 'Closure tests for {variable}'.format(variable=v_latex) output_folder = 'plots/unfolding/closure_test/{0}/'.format(method) models = OrderedDict() measurements = OrderedDict() for sample in unfolded_and_truths: models[sample + ' truth'] = unfolded_and_truths[sample]['truth'] measurements[sample + ' unfolded'] = unfolded_and_truths[sample]['unfolded'] compare_measurements( models = models, measurements = measurements, show_measurement_errors=True, histogram_properties=hp, save_folder=output_folder, save_as=['pdf'], match_models_to_measurements = True)
def plot_bias(unfolded_and_truths, variable, channel, come, method): hp = Histogram_properties() hp.name = 'Bias_{channel}_{variable}_at_{come}TeV'.format( channel=channel, variable=variable, come=come, ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT', 'lepton_pt']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit # plt.ylabel( r, CMS.y_axis_title ) hp.y_axis_title = 'Unfolded / Truth' hp.y_limits = [0.92, 1.08] hp.title = 'Bias for {variable}'.format(variable=v_latex) output_folder = 'plots/unfolding/bias_test/' measurements = { 'Central' : unfolded_and_truths['Central']['bias'] } models = {} for sample in unfolded_and_truths: if sample == 'Central' : continue models[sample] = unfolded_and_truths[sample]['bias'] compare_measurements( models = models, measurements = measurements, show_measurement_errors=True, histogram_properties=hp, save_folder=output_folder, save_as=['pdf'], match_models_to_measurements = True)
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 compare_vjets_templates( variable = 'MET', met_type = 'patType1CorrectedPFMet', title = 'Untitled', channel = 'electron' ): ''' Compares the V+jets templates in different bins of the current variable''' global fit_variable_properties, b_tag_bin, save_as variable_bins = variable_bins_ROOT[variable] histogram_template = get_histogram_template( variable ) for fit_variable in electron_fit_variables: all_hists = {} inclusive_hist = None save_path = 'plots/%dTeV/fit_variables/%s/%s/' % ( measurement_config.centre_of_mass_energy, variable, fit_variable ) make_folder_if_not_exists( save_path + '/vjets/' ) max_bins = len( variable_bins ) for bin_range in variable_bins[0:max_bins]: params = {'met_type': met_type, 'bin_range':bin_range, 'fit_variable':fit_variable, 'b_tag_bin':b_tag_bin, 'variable':variable} fit_variable_distribution = histogram_template % params # format: histograms['data'][qcd_fit_variable_distribution] histograms = get_histograms_from_files( [fit_variable_distribution], histogram_files ) prepare_histograms( histograms, rebin = fit_variable_properties[fit_variable]['rebin'], scale_factor = measurement_config.luminosity_scale ) all_hists[bin_range] = histograms['V+Jets'][fit_variable_distribution] # create the inclusive distributions inclusive_hist = deepcopy( all_hists[variable_bins[0]] ) for bin_range in variable_bins[1:max_bins]: inclusive_hist += all_hists[bin_range] for bin_range in variable_bins[0:max_bins]: if not all_hists[bin_range].Integral() == 0: all_hists[bin_range].Scale( 1 / all_hists[bin_range].Integral() ) # normalise all histograms inclusive_hist.Scale( 1 / inclusive_hist.Integral() ) # now compare inclusive to all bins histogram_properties = Histogram_properties() histogram_properties.x_axis_title = fit_variable_properties[fit_variable]['x-title'] histogram_properties.y_axis_title = fit_variable_properties[fit_variable]['y-title'] histogram_properties.y_axis_title = histogram_properties.y_axis_title.replace( 'Events', 'a.u.' ) histogram_properties.x_limits = [fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max']] histogram_properties.title = title histogram_properties.additional_text = channel_latex[channel] + ', ' + b_tag_bins_latex[b_tag_bin] histogram_properties.name = variable + '_' + fit_variable + '_' + b_tag_bin + '_VJets_template_comparison' histogram_properties.y_max_scale = 1.5 measurements = {bin_range + ' GeV': histogram for bin_range, histogram in all_hists.iteritems()} measurements = OrderedDict( sorted( measurements.items() ) ) fit_var = fit_variable.replace( 'electron_', '' ) fit_var = fit_var.replace( 'muon_', '' ) graphs = spread_x( measurements.values(), fit_variable_bin_edges[fit_var] ) for key, graph in zip( sorted( measurements.keys() ), graphs ): measurements[key] = graph compare_measurements( models = {'inclusive' : inclusive_hist}, measurements = measurements, show_measurement_errors = True, histogram_properties = histogram_properties, save_folder = save_path + '/vjets/', save_as = save_as )
def compare( central_mc, expected_result = None, measured_result = None, results = {}, variable = 'MET', channel = 'electron', bin_edges = [] ): global input_file, plot_location, ttbar_xsection, luminosity, centre_of_mass, method, test, log_plots channel_label = '' if channel == 'electron': channel_label = 'e+jets, $\geq$4 jets' elif channel == 'muon': channel_label = '$\mu$+jets, $\geq$4 jets' else: channel_label = '$e, \mu$ + jets combined, $\geq$4 jets' if test == 'data': title_template = 'CMS Preliminary, $\mathcal{L} = %.1f$ fb$^{-1}$ at $\sqrt{s}$ = %d TeV \n %s' title = title_template % ( luminosity / 1000., centre_of_mass, channel_label ) else: title_template = 'CMS Simulation at $\sqrt{s}$ = %d TeV \n %s' title = title_template % ( centre_of_mass, channel_label ) models = {latex_labels.measurements_latex['MADGRAPH'] : central_mc} if expected_result and test == 'data': models.update({'fitted data' : expected_result}) # scale central MC to lumi nEvents = input_file.EventFilter.EventCounter.GetBinContent( 1 ) # number of processed events lumiweight = ttbar_xsection * luminosity / nEvents central_mc.Scale( lumiweight ) elif expected_result: models.update({'expected' : expected_result}) if measured_result and test != 'data': models.update({'measured' : measured_result}) measurements = collections.OrderedDict() for key, value in results['k_value_results'].iteritems(): measurements['k = ' + str( key )] = value # get some spread in x graphs = spread_x( measurements.values(), bin_edges ) for key, graph in zip( measurements.keys(), graphs ): measurements[key] = graph histogram_properties = Histogram_properties() histogram_properties.name = channel + '_' + variable + '_' + method + '_' + test histogram_properties.title = title + ', ' + latex_labels.b_tag_bins_latex['2orMoreBtags'] histogram_properties.x_axis_title = '$' + latex_labels.variables_latex[variable] + '$' histogram_properties.y_axis_title = r'Events' # histogram_properties.y_limits = [0, 0.03] histogram_properties.x_limits = [bin_edges[0], bin_edges[-1]] if log_plots: histogram_properties.set_log_y = True histogram_properties.name += '_log' compare_measurements( models, measurements, show_measurement_errors = True, histogram_properties = histogram_properties, save_folder = plot_location, save_as = ['pdf'] )
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 plot_bias(unfolded_and_truths, variable, channel, come, method, prefix, plot_systematics=False): hp = Histogram_properties() hp.name = 'Bias_{prefix}_{channel}_{variable}_at_{come}TeV'.format( prefix=prefix, channel=channel, variable=variable, come=come, ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT', 'lepton_pt']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit # plt.ylabel( r, CMS.y_axis_title ) hp.y_axis_title = 'Unfolded / Truth' hp.y_limits = [0.7, 1.5] hp.title = 'Bias for {variable}'.format(variable=v_latex) hp.legend_location = (0.98, 0.95) output_folder = 'plots/unfolding/bias_test/' measurements = {} # measurements = { 'Central' : unfolded_and_truths['Central']['bias'] } # for bin in range(0, unfolded_and_truths['Central']['bias'].GetNbinsX() + 1 ): # unfolded_and_truths['Central']['bias'].SetBinError(bin,0) models = OrderedDict() lineStyles = [] for sample in unfolded_and_truths: if sample == 'Central' or sample == 'Nominal': lineStyles.append('dashed') else: lineStyles.append('dotted') models[sample] = unfolded_and_truths[sample]['bias'] if plot_systematics: models['systematicsup'], models['systematicsdown'] = get_systematics(variable,channel,come,method) lineStyles.append('solid') lineStyles.append('solid') compare_measurements( models = models, measurements = measurements, show_measurement_errors=True, histogram_properties=hp, save_folder=output_folder, save_as=['pdf'], line_styles_for_models = lineStyles, match_models_to_measurements = True )
def plot_fit_results(fit_results, centre_of_mass, channel, variable, k_value, tau_value, output_folder, output_formats, bin_edges): h_mean = Hist(bin_edges, type='D') h_sigma = Hist(bin_edges, type='D') n_bins = h_mean.nbins() assert len(fit_results) == n_bins mean_abs_pull = 0 for i, fr in enumerate(fit_results): mean_abs_pull += abs(fr.mean) h_mean.SetBinContent(i + 1, fr.mean) h_mean.SetBinError(i + 1, fr.meanError) h_sigma.SetBinContent(i + 1, fr.sigma) h_sigma.SetBinError(i + 1, fr.sigmaError) mean_abs_pull /= n_bins histogram_properties = Histogram_properties() name_mpt = 'pull_distribution_mean_and_sigma_{0}_{1}_{2}TeV' histogram_properties.name = name_mpt.format( variable, channel, centre_of_mass ) histogram_properties.y_axis_title = r'$\mu_{\text{pull}}$ ($\sigma_{\text{pull}}$)' histogram_properties.x_axis_title = latex_labels.variables_latex[variable] histogram_properties.legend_location = (0.98, 0.48) value = get_value_title(k_value, tau_value) title = 'pull distribution mean \& sigma for {0}'.format(tau_value) histogram_properties.title = title histogram_properties.y_limits = [-2, 2] histogram_properties.xerr = True compare_measurements( models={ # 'mean $|\mu|$':make_line_hist(bin_edges,mean_abs_pull), 'ideal $\mu$': make_line_hist(bin_edges, 0), 'ideal $\sigma$': make_line_hist(bin_edges, 1), }, measurements={ r'$\mu_{\text{pull}}$': h_mean, r'$\sigma_{\text{pull}}$': h_sigma }, show_measurement_errors=True, histogram_properties=histogram_properties, save_folder=output_folder, save_as=output_formats)
def plot_bias(unfolded_and_truths, variable, channel, come, method): hp = Histogram_properties() hp.name = 'Bias_{channel}_{variable}_at_{come}TeV'.format( channel=channel, variable=variable, come=come, ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT', 'lepton_pt']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit # plt.ylabel( r, CMS.y_axis_title ) hp.y_axis_title = 'Unfolded / Truth' hp.y_limits = [0.85, 1.15] hp.title = 'Bias for {variable}'.format(variable=v_latex) output_folder = 'plots/unfolding/bias_test/' measurements = { 'Central' : unfolded_and_truths['Central']['bias'] } for bin in range(0, unfolded_and_truths['Central']['bias'].GetNbinsX() + 1 ): unfolded_and_truths['Central']['bias'].SetBinError(bin,0) # central_truth = unfolded_and_truths['Central']['truth'] # for label, hists in unfolded_and_truths.iteritems(): # truth = hists['truth'] # print label # for i,j in zip( list(truth.y()), list(central_truth.y())) : # print abs(1-i/j)*100 models = {} for sample in unfolded_and_truths: if sample == 'Central' : continue models[sample] = unfolded_and_truths[sample]['bias'] compare_measurements( models = models, measurements = measurements, show_measurement_errors=True, histogram_properties=hp, save_folder=output_folder, save_as=['pdf'], match_models_to_measurements = True)
def plot_fit_results(fit_results, centre_of_mass, channel, variable, k_value, tau_value, output_folder, output_formats, bin_edges): h_mean = Hist(bin_edges, type='D') h_sigma = Hist(bin_edges, type='D') n_bins = h_mean.nbins() assert len(fit_results) == n_bins mean_abs_pull = 0 for i, fr in enumerate(fit_results): mean_abs_pull += abs(fr.mean) h_mean.SetBinContent(i + 1, fr.mean) h_mean.SetBinError(i + 1, fr.meanError) h_sigma.SetBinContent(i + 1, fr.sigma) h_sigma.SetBinError(i + 1, fr.sigmaError) mean_abs_pull /= n_bins histogram_properties = Histogram_properties() name_mpt = 'pull_distribution_mean_and_sigma_{0}_{1}_{2}TeV' histogram_properties.name = name_mpt.format(variable, channel, centre_of_mass) histogram_properties.y_axis_title = r'$\mu_{\text{pull}}$ ($\sigma_{\text{pull}}$)' histogram_properties.x_axis_title = latex_labels.variables_latex[variable] histogram_properties.legend_location = (0.98, 0.48) value = get_value_title(k_value, tau_value) title = 'pull distribution mean \& sigma for {0}'.format(tau_value) histogram_properties.title = title histogram_properties.y_limits = [-2, 2] histogram_properties.xerr = True compare_measurements( models={ # 'mean $|\mu|$':make_line_hist(bin_edges,mean_abs_pull), 'ideal $\mu$': make_line_hist(bin_edges, 0), 'ideal $\sigma$': make_line_hist(bin_edges, 1), }, measurements={ r'$\mu_{\text{pull}}$': h_mean, r'$\sigma_{\text{pull}}$': h_sigma }, show_measurement_errors=True, histogram_properties=histogram_properties, save_folder=output_folder, save_as=output_formats)
def compare_vjets_btag_regions( variable = 'MET', met_type = 'patType1CorrectedPFMet', title = 'Untitled', channel = 'electron' ): ''' Compares the V+Jets template in different b-tag bins''' global fit_variable_properties, b_tag_bin, save_as, b_tag_bin_ctl b_tag_bin_ctl = '0orMoreBtag' variable_bins = variable_bins_ROOT[variable] histogram_template = get_histogram_template( variable ) for fit_variable in electron_fit_variables: if '_bl' in fit_variable: b_tag_bin_ctl = '1orMoreBtag' else: b_tag_bin_ctl = '0orMoreBtag' save_path = 'plots/%dTeV/fit_variables/%s/%s/' % ( measurement_config.centre_of_mass_energy, variable, fit_variable ) make_folder_if_not_exists( save_path + '/vjets/' ) histogram_properties = Histogram_properties() histogram_properties.x_axis_title = fit_variable_properties[fit_variable]['x-title'] histogram_properties.y_axis_title = fit_variable_properties[fit_variable]['y-title'] histogram_properties.y_axis_title = histogram_properties.y_axis_title.replace( 'Events', 'a.u.' ) histogram_properties.x_limits = [fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max']] histogram_properties.title = title histogram_properties.additional_text = channel_latex[channel] + ', ' + b_tag_bins_latex[b_tag_bin_ctl] histogram_properties.y_max_scale = 1.5 for bin_range in variable_bins: params = {'met_type': met_type, 'bin_range':bin_range, 'fit_variable':fit_variable, 'b_tag_bin':b_tag_bin, 'variable':variable} fit_variable_distribution = histogram_template % params fit_variable_distribution_ctl = fit_variable_distribution.replace( b_tag_bin, b_tag_bin_ctl ) # format: histograms['data'][qcd_fit_variable_distribution] histograms = get_histograms_from_files( [fit_variable_distribution, fit_variable_distribution_ctl], {'V+Jets' : histogram_files['V+Jets']} ) prepare_histograms( histograms, rebin = fit_variable_properties[fit_variable]['rebin'], scale_factor = measurement_config.luminosity_scale ) histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_' + b_tag_bin_ctl + '_VJets_template_comparison' histograms['V+Jets'][fit_variable_distribution].Scale( 1 / histograms['V+Jets'][fit_variable_distribution].Integral() ) histograms['V+Jets'][fit_variable_distribution_ctl].Scale( 1 / histograms['V+Jets'][fit_variable_distribution_ctl].Integral() ) compare_measurements( models = {'no b-tag' : histograms['V+Jets'][fit_variable_distribution_ctl]}, measurements = {'$>=$ 2 b-tags': histograms['V+Jets'][fit_variable_distribution]}, show_measurement_errors = True, histogram_properties = histogram_properties, save_folder = save_path + '/vjets/', save_as = save_as )
def plot_closure(unfolded_and_truths, variable, channel, come, method, quantity): hp = Histogram_properties() hp.name = '{quantity}_{channel}_closure_test_for_{variable}_at_{come}TeV'.format( quantity=quantity, channel=channel, variable=variable, come=come, ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT', 'lepton_pt']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit if quantity == 'number_of_unfolded_events': hp.y_axis_title = 'Number of unfolded events' elif quantity == 'normalised_xsection': hp.y_axis_title = 'Normalised Cross Section' hp.title = 'Closure tests for {variable}'.format(variable=v_latex) output_folder = 'plots/unfolding/closure_test/' models = OrderedDict() measurements = OrderedDict() for sample in unfolded_and_truths: models[sample + ' truth'] = unfolded_and_truths[sample]['truth'] measurements[sample + ' unfolded'] = unfolded_and_truths[sample]['unfolded'] compare_measurements( models = models, measurements = measurements, show_measurement_errors=True, histogram_properties=hp, save_folder=output_folder, save_as=['pdf'], match_models_to_measurements = True )
def compare_vjets_templates(variable='MET', met_type='patType1CorrectedPFMet', title='Untitled', channel='electron'): ''' Compares the V+jets templates in different bins of the current variable''' global fit_variable_properties, b_tag_bin, save_as variable_bins = variable_bins_ROOT[variable] histogram_template = get_histogram_template(variable) for fit_variable in electron_fit_variables: all_hists = {} inclusive_hist = None save_path = 'plots/%dTeV/fit_variables/%s/%s/' % ( measurement_config.centre_of_mass_energy, variable, fit_variable) make_folder_if_not_exists(save_path + '/vjets/') max_bins = len(variable_bins) for bin_range in variable_bins[0:max_bins]: params = { 'met_type': met_type, 'bin_range': bin_range, 'fit_variable': fit_variable, 'b_tag_bin': b_tag_bin, 'variable': variable } fit_variable_distribution = histogram_template % params # format: histograms['data'][qcd_fit_variable_distribution] histograms = get_histograms_from_files([fit_variable_distribution], histogram_files) prepare_histograms( histograms, rebin=fit_variable_properties[fit_variable]['rebin'], scale_factor=measurement_config.luminosity_scale) all_hists[bin_range] = histograms['V+Jets'][ fit_variable_distribution] # create the inclusive distributions inclusive_hist = deepcopy(all_hists[variable_bins[0]]) for bin_range in variable_bins[1:max_bins]: inclusive_hist += all_hists[bin_range] for bin_range in variable_bins[0:max_bins]: if not all_hists[bin_range].Integral() == 0: all_hists[bin_range].Scale(1 / all_hists[bin_range].Integral()) # normalise all histograms inclusive_hist.Scale(1 / inclusive_hist.Integral()) # now compare inclusive to all bins histogram_properties = Histogram_properties() histogram_properties.x_axis_title = fit_variable_properties[ fit_variable]['x-title'] histogram_properties.y_axis_title = fit_variable_properties[ fit_variable]['y-title'] histogram_properties.y_axis_title = histogram_properties.y_axis_title.replace( 'Events', 'a.u.') histogram_properties.x_limits = [ fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max'] ] histogram_properties.title = title histogram_properties.additional_text = channel_latex[ channel] + ', ' + b_tag_bins_latex[b_tag_bin] histogram_properties.name = variable + '_' + fit_variable + '_' + b_tag_bin + '_VJets_template_comparison' histogram_properties.y_max_scale = 1.5 measurements = { bin_range + ' GeV': histogram for bin_range, histogram in all_hists.iteritems() } measurements = OrderedDict(sorted(measurements.items())) fit_var = fit_variable.replace('electron_', '') fit_var = fit_var.replace('muon_', '') graphs = spread_x(measurements.values(), fit_variable_bin_edges[fit_var]) for key, graph in zip(sorted(measurements.keys()), graphs): measurements[key] = graph compare_measurements(models={'inclusive': inclusive_hist}, measurements=measurements, show_measurement_errors=True, histogram_properties=histogram_properties, save_folder=save_path + '/vjets/', save_as=save_as)
def main(): config = XSectionConfig(13) file_for_powhegPythia = File(config.unfolding_central_firstHalf, 'read') file_for_ptReweight_up = File(config.unfolding_ptreweight_up_firstHalf, 'read') file_for_ptReweight_down = File(config.unfolding_ptreweight_down_firstHalf, 'read') file_for_amcatnlo_pythia8 = File(config.unfolding_amcatnlo_pythia8, 'read') file_for_powhegHerwig = File(config.unfolding_powheg_herwig, 'read') file_for_etaReweight_up = File(config.unfolding_etareweight_up, 'read') file_for_etaReweight_down = File(config.unfolding_etareweight_down, 'read') file_for_data_template = 'data/normalisation/background_subtraction/13TeV/{variable}/VisiblePS/central/normalisation_{channel}.txt' for channel in config.analysis_types.keys(): if channel is 'combined': continue for variable in config.variables: print variable # for variable in ['HT']: # Get the central powheg pythia distributions _, _, response_central, fakes_central = get_unfold_histogram_tuple( inputfile=file_for_powhegPythia, variable=variable, channel=channel, centre_of_mass=13, load_fakes=True, visiblePS=True) measured_central = asrootpy(response_central.ProjectionX('px', 1)) truth_central = asrootpy(response_central.ProjectionY()) # Get the reweighted powheg pythia distributions _, _, response_pt_reweighted_up, _ = get_unfold_histogram_tuple( inputfile=file_for_ptReweight_up, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True) measured_pt_reweighted_up = asrootpy( response_pt_reweighted_up.ProjectionX('px', 1)) truth_pt_reweighted_up = asrootpy( response_pt_reweighted_up.ProjectionY()) _, _, response_pt_reweighted_down, _ = get_unfold_histogram_tuple( inputfile=file_for_ptReweight_down, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True) measured_pt_reweighted_down = asrootpy( response_pt_reweighted_down.ProjectionX('px', 1)) truth_pt_reweighted_down = asrootpy( response_pt_reweighted_down.ProjectionY()) # _, _, response_eta_reweighted_up, _ = get_unfold_histogram_tuple( # inputfile=file_for_etaReweight_up, # variable=variable, # channel=channel, # centre_of_mass=13, # load_fakes=False, # visiblePS=True # ) # measured_eta_reweighted_up = asrootpy(response_eta_reweighted_up.ProjectionX('px',1)) # truth_eta_reweighted_up = asrootpy(response_eta_reweighted_up.ProjectionY()) # _, _, response_eta_reweighted_down, _ = get_unfold_histogram_tuple( # inputfile=file_for_etaReweight_down, # variable=variable, # channel=channel, # centre_of_mass=13, # load_fakes=False, # visiblePS=True # ) # measured_eta_reweighted_down = asrootpy(response_eta_reweighted_down.ProjectionX('px',1)) # truth_eta_reweighted_down = asrootpy(response_eta_reweighted_down.ProjectionY()) # Get the distributions for other MC models _, _, response_amcatnlo_pythia8, _ = get_unfold_histogram_tuple( inputfile=file_for_amcatnlo_pythia8, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True) measured_amcatnlo_pythia8 = asrootpy( response_amcatnlo_pythia8.ProjectionX('px', 1)) truth_amcatnlo_pythia8 = asrootpy( response_amcatnlo_pythia8.ProjectionY()) _, _, response_powhegHerwig, _ = get_unfold_histogram_tuple( inputfile=file_for_powhegHerwig, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True) measured_powhegHerwig = asrootpy( response_powhegHerwig.ProjectionX('px', 1)) truth_powhegHerwig = asrootpy(response_powhegHerwig.ProjectionY()) # Get the data input (data after background subtraction, and fake removal) file_for_data = file_for_data_template.format(variable=variable, channel=channel) data = read_tuple_from_file(file_for_data)['TTJet'] data = value_error_tuplelist_to_hist(data, reco_bin_edges_vis[variable]) data = removeFakes(measured_central, fakes_central, data) # Plot all three hp = Histogram_properties() hp.name = 'Reweighting_check_{channel}_{variable}_at_{com}TeV'.format( channel=channel, variable=variable, com='13', ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT', 'lepton_pt']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit hp.x_limits = [ reco_bin_edges_vis[variable][0], reco_bin_edges_vis[variable][-1] ] hp.ratio_y_limits = [0.1, 1.9] hp.ratio_y_title = 'Reweighted / Central' hp.y_axis_title = 'Number of events' hp.title = 'Reweighting check for {variable}'.format( variable=v_latex) measured_central.Rebin(2) measured_pt_reweighted_up.Rebin(2) measured_pt_reweighted_down.Rebin(2) # measured_eta_reweighted_up.Rebin(2) # measured_eta_reweighted_down.Rebin(2) measured_amcatnlo_pythia8.Rebin(2) measured_powhegHerwig.Rebin(2) data.Rebin(2) measured_central.Scale(1 / measured_central.Integral()) measured_pt_reweighted_up.Scale( 1 / measured_pt_reweighted_up.Integral()) measured_pt_reweighted_down.Scale( 1 / measured_pt_reweighted_down.Integral()) measured_amcatnlo_pythia8.Scale( 1 / measured_amcatnlo_pythia8.Integral()) measured_powhegHerwig.Scale(1 / measured_powhegHerwig.Integral()) # measured_eta_reweighted_up.Scale( 1 / measured_eta_reweighted_up.Integral() ) # measured_eta_reweighted_down.Scale( 1/ measured_eta_reweighted_down.Integral() ) data.Scale(1 / data.Integral()) print list(measured_central.y()) print list(measured_amcatnlo_pythia8.y()) print list(measured_powhegHerwig.y()) print list(data.y()) compare_measurements( # models = {'Central' : measured_central, 'PtReweighted Up' : measured_pt_reweighted_up, 'PtReweighted Down' : measured_pt_reweighted_down, 'EtaReweighted Up' : measured_eta_reweighted_up, 'EtaReweighted Down' : measured_eta_reweighted_down}, models=OrderedDict([ ('Central', measured_central), ('PtReweighted Up', measured_pt_reweighted_up), ('PtReweighted Down', measured_pt_reweighted_down), ('amc@nlo', measured_amcatnlo_pythia8), ('powhegHerwig', measured_powhegHerwig) ]), measurements={'Data': data}, show_measurement_errors=True, histogram_properties=hp, save_folder='plots/unfolding/reweighting_check', save_as=['pdf'], line_styles_for_models=[ 'solid', 'solid', 'solid', 'dashed', 'dashed' ], show_ratio_for_pairs=OrderedDict([ ('PtUpVsCentral', [measured_pt_reweighted_up, measured_central]), ('PtDownVsCentral', [measured_pt_reweighted_down, measured_central]), ('amcatnloVsCentral', [measured_amcatnlo_pythia8, measured_central]), ('powhegHerwigVsCentral', [measured_powhegHerwig, measured_central]), ('DataVsCentral', [data, measured_central]) ]), )
def main(): config = XSectionConfig(13) file_for_powhegPythia = File(config.unfolding_central_firstHalf, 'read') file_for_ptReweight_up = File(config.unfolding_ptreweight_up_firstHalf, 'read') file_for_ptReweight_down = File(config.unfolding_ptreweight_down_firstHalf, 'read') file_for_amcatnlo_pythia8 = File(config.unfolding_amcatnlo_pythia8, 'read') file_for_powhegHerwig = File(config.unfolding_powheg_herwig, 'read') file_for_etaReweight_up = File(config.unfolding_etareweight_up, 'read') file_for_etaReweight_down = File(config.unfolding_etareweight_down, 'read') file_for_data_template = 'data/normalisation/background_subtraction/13TeV/{variable}/VisiblePS/central/normalisation_{channel}.txt' for channel in config.analysis_types.keys(): if channel is 'combined':continue for variable in config.variables: print variable # for variable in ['HT']: # Get the central powheg pythia distributions _, _, response_central, fakes_central = get_unfold_histogram_tuple( inputfile=file_for_powhegPythia, variable=variable, channel=channel, centre_of_mass=13, load_fakes=True, visiblePS=True ) measured_central = asrootpy(response_central.ProjectionX('px',1)) truth_central = asrootpy(response_central.ProjectionY()) # Get the reweighted powheg pythia distributions _, _, response_pt_reweighted_up, _ = get_unfold_histogram_tuple( inputfile=file_for_ptReweight_up, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True ) measured_pt_reweighted_up = asrootpy(response_pt_reweighted_up.ProjectionX('px',1)) truth_pt_reweighted_up = asrootpy(response_pt_reweighted_up.ProjectionY()) _, _, response_pt_reweighted_down, _ = get_unfold_histogram_tuple( inputfile=file_for_ptReweight_down, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True ) measured_pt_reweighted_down = asrootpy(response_pt_reweighted_down.ProjectionX('px',1)) truth_pt_reweighted_down = asrootpy(response_pt_reweighted_down.ProjectionY()) # _, _, response_eta_reweighted_up, _ = get_unfold_histogram_tuple( # inputfile=file_for_etaReweight_up, # variable=variable, # channel=channel, # centre_of_mass=13, # load_fakes=False, # visiblePS=True # ) # measured_eta_reweighted_up = asrootpy(response_eta_reweighted_up.ProjectionX('px',1)) # truth_eta_reweighted_up = asrootpy(response_eta_reweighted_up.ProjectionY()) # _, _, response_eta_reweighted_down, _ = get_unfold_histogram_tuple( # inputfile=file_for_etaReweight_down, # variable=variable, # channel=channel, # centre_of_mass=13, # load_fakes=False, # visiblePS=True # ) # measured_eta_reweighted_down = asrootpy(response_eta_reweighted_down.ProjectionX('px',1)) # truth_eta_reweighted_down = asrootpy(response_eta_reweighted_down.ProjectionY()) # Get the distributions for other MC models _, _, response_amcatnlo_pythia8, _ = get_unfold_histogram_tuple( inputfile=file_for_amcatnlo_pythia8, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True ) measured_amcatnlo_pythia8 = asrootpy(response_amcatnlo_pythia8.ProjectionX('px',1)) truth_amcatnlo_pythia8 = asrootpy(response_amcatnlo_pythia8.ProjectionY()) _, _, response_powhegHerwig, _ = get_unfold_histogram_tuple( inputfile=file_for_powhegHerwig, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True ) measured_powhegHerwig = asrootpy(response_powhegHerwig.ProjectionX('px',1)) truth_powhegHerwig = asrootpy(response_powhegHerwig.ProjectionY()) # Get the data input (data after background subtraction, and fake removal) file_for_data = file_for_data_template.format( variable = variable, channel = channel ) data = read_tuple_from_file(file_for_data)['TTJet'] data = value_error_tuplelist_to_hist( data, reco_bin_edges_vis[variable] ) data = removeFakes( measured_central, fakes_central, data ) # Plot all three hp = Histogram_properties() hp.name = 'Reweighting_check_{channel}_{variable}_at_{com}TeV'.format( channel=channel, variable=variable, com='13', ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT', 'lepton_pt']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit hp.x_limits = [ reco_bin_edges_vis[variable][0], reco_bin_edges_vis[variable][-1]] hp.ratio_y_limits = [0.1,1.9] hp.ratio_y_title = 'Reweighted / Central' hp.y_axis_title = 'Number of events' hp.title = 'Reweighting check for {variable}'.format(variable=v_latex) measured_central.Rebin(2) measured_pt_reweighted_up.Rebin(2) measured_pt_reweighted_down.Rebin(2) # measured_eta_reweighted_up.Rebin(2) # measured_eta_reweighted_down.Rebin(2) measured_amcatnlo_pythia8.Rebin(2) measured_powhegHerwig.Rebin(2) data.Rebin(2) measured_central.Scale( 1 / measured_central.Integral() ) measured_pt_reweighted_up.Scale( 1 / measured_pt_reweighted_up.Integral() ) measured_pt_reweighted_down.Scale( 1 / measured_pt_reweighted_down.Integral() ) measured_amcatnlo_pythia8.Scale( 1 / measured_amcatnlo_pythia8.Integral() ) measured_powhegHerwig.Scale( 1 / measured_powhegHerwig.Integral() ) # measured_eta_reweighted_up.Scale( 1 / measured_eta_reweighted_up.Integral() ) # measured_eta_reweighted_down.Scale( 1/ measured_eta_reweighted_down.Integral() ) data.Scale( 1 / data.Integral() ) print list(measured_central.y()) print list(measured_amcatnlo_pythia8.y()) print list(measured_powhegHerwig.y()) print list(data.y()) compare_measurements( # models = {'Central' : measured_central, 'PtReweighted Up' : measured_pt_reweighted_up, 'PtReweighted Down' : measured_pt_reweighted_down, 'EtaReweighted Up' : measured_eta_reweighted_up, 'EtaReweighted Down' : measured_eta_reweighted_down}, models = OrderedDict([('Central' , measured_central), ('PtReweighted Up' , measured_pt_reweighted_up), ('PtReweighted Down' , measured_pt_reweighted_down), ('amc@nlo' , measured_amcatnlo_pythia8), ('powhegHerwig' , measured_powhegHerwig) ] ), measurements = {'Data' : data}, show_measurement_errors=True, histogram_properties=hp, save_folder='plots/unfolding/reweighting_check', save_as=['pdf'], line_styles_for_models = ['solid','solid','solid','dashed','dashed'], show_ratio_for_pairs = OrderedDict( [ ('PtUpVsCentral' , [ measured_pt_reweighted_up, measured_central ] ), ('PtDownVsCentral' , [ measured_pt_reweighted_down, measured_central ] ), ('amcatnloVsCentral' , [ measured_amcatnlo_pythia8, measured_central ] ), ('powhegHerwigVsCentral' , [ measured_powhegHerwig, measured_central ] ), ('DataVsCentral' , [data, measured_central] ) ]), )
def debug_last_bin(): ''' For debugging why the last bin in the problematic variables deviates a lot in _one_ of the channels only. ''' file_template = '/hdfs/TopQuarkGroup/run2/dpsData/' file_template += 'data/normalisation/background_subtraction/13TeV/' file_template += '{variable}/VisiblePS/central/' file_template += 'normalised_xsection_{channel}_RooUnfoldSvd{suffix}.txt' problematic_variables = ['HT', 'MET', 'NJets', 'lepton_pt'] for variable in problematic_variables: results = {} Result = namedtuple( 'Result', ['before_unfolding', 'after_unfolding', 'model']) for channel in ['electron', 'muon', 'combined']: input_file_data = file_template.format( variable=variable, channel=channel, suffix='_with_errors', ) input_file_model = file_template.format( variable=variable, channel=channel, suffix='', ) data = read_data_from_JSON(input_file_data) data_model = read_data_from_JSON(input_file_model) before_unfolding = data['TTJet_measured_withoutFakes'] after_unfolding = data['TTJet_unfolded'] model = data_model['powhegPythia8'] # only use the last bin h_before_unfolding = value_errors_tuplelist_to_graph( [before_unfolding[-1]], bin_edges_vis[variable][-2:]) h_after_unfolding = value_errors_tuplelist_to_graph( [after_unfolding[-1]], bin_edges_vis[variable][-2:]) h_model = value_error_tuplelist_to_hist( [model[-1]], bin_edges_vis[variable][-2:]) r = Result(before_unfolding, after_unfolding, model) h = Result(h_before_unfolding, h_after_unfolding, h_model) results[channel] = (r, h) models = {'POWHEG+PYTHIA': results['combined'][1].model} h_unfolded = [results[channel][1].after_unfolding for channel in [ 'electron', 'muon', 'combined']] tmp_hists = spread_x(h_unfolded, bin_edges_vis[variable][-2:]) measurements = {} for channel, hist in zip(['electron', 'muon', 'combined'], tmp_hists): value = results[channel][0].after_unfolding[-1][0] error = results[channel][0].after_unfolding[-1][1] label = '{c_label} ({value:1.2g} $\pm$ {error:1.2g})'.format( c_label=channel, value=value, error=error, ) measurements[label] = hist properties = Histogram_properties() properties.name = 'normalised_xsection_compare_channels_{0}_{1}_last_bin'.format( variable, channel) properties.title = 'Comparison of channels' properties.path = 'plots' properties.has_ratio = True properties.xerr = False properties.x_limits = ( bin_edges_vis[variable][-2], bin_edges_vis[variable][-1]) properties.x_axis_title = variables_latex[variable] properties.y_axis_title = r'$\frac{1}{\sigma} \frac{d\sigma}{d' + \ variables_latex[variable] + '}$' properties.legend_location = (0.95, 0.40) if variable == 'NJets': properties.legend_location = (0.97, 0.80) properties.formats = ['png'] compare_measurements(models=models, measurements=measurements, show_measurement_errors=True, histogram_properties=properties, save_folder='plots/', save_as=properties.formats)
def main(): config = XSectionConfig(13) file_for_powhegPythia = File(config.unfolding_central, "read") file_for_ptReweight_up = File(config.unfolding_ptreweight_up, "read") file_for_ptReweight_down = File(config.unfolding_ptreweight_down, "read") file_for_etaReweight_up = File(config.unfolding_etareweight_up, "read") file_for_etaReweight_down = File(config.unfolding_etareweight_down, "read") file_for_data_template = "data/normalisation/background_subtraction/13TeV/{variable}/VisiblePS/central/normalisation_combined_patType1CorrectedPFMet.txt" for channel in ["combined"]: for variable in config.variables: print variable # for variable in ['HT']: # Get the central powheg pythia distributions _, _, response_central, fakes_central = get_unfold_histogram_tuple( inputfile=file_for_powhegPythia, variable=variable, channel=channel, centre_of_mass=13, load_fakes=True, visiblePS=True, ) measured_central = asrootpy(response_central.ProjectionX("px", 1)) truth_central = asrootpy(response_central.ProjectionY()) # Get the reweighted powheg pythia distributions _, _, response_pt_reweighted_up, _ = get_unfold_histogram_tuple( inputfile=file_for_ptReweight_up, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True, ) measured_pt_reweighted_up = asrootpy(response_pt_reweighted_up.ProjectionX("px", 1)) truth_pt_reweighted_up = asrootpy(response_pt_reweighted_up.ProjectionY()) _, _, response_pt_reweighted_down, _ = get_unfold_histogram_tuple( inputfile=file_for_ptReweight_down, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True, ) measured_pt_reweighted_down = asrootpy(response_pt_reweighted_down.ProjectionX("px", 1)) truth_pt_reweighted_down = asrootpy(response_pt_reweighted_down.ProjectionY()) _, _, response_eta_reweighted_up, _ = get_unfold_histogram_tuple( inputfile=file_for_etaReweight_up, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True, ) measured_eta_reweighted_up = asrootpy(response_eta_reweighted_up.ProjectionX("px", 1)) truth_eta_reweighted_up = asrootpy(response_eta_reweighted_up.ProjectionY()) _, _, response_eta_reweighted_down, _ = get_unfold_histogram_tuple( inputfile=file_for_etaReweight_down, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True, ) measured_eta_reweighted_down = asrootpy(response_eta_reweighted_down.ProjectionX("px", 1)) truth_eta_reweighted_down = asrootpy(response_eta_reweighted_down.ProjectionY()) # Get the data input (data after background subtraction, and fake removal) file_for_data = file_for_data_template.format(variable=variable) data = read_data_from_JSON(file_for_data)["TTJet"] data = value_error_tuplelist_to_hist(data, reco_bin_edges_vis[variable]) data = removeFakes(measured_central, fakes_central, data) # Plot all three hp = Histogram_properties() hp.name = "Reweighting_check_{channel}_{variable}_at_{com}TeV".format( channel=channel, variable=variable, com="13" ) v_latex = latex_labels.variables_latex[variable] unit = "" if variable in ["HT", "ST", "MET", "WPT", "lepton_pt"]: unit = " [GeV]" hp.x_axis_title = v_latex + unit hp.y_axis_title = "Number of events" hp.title = "Reweighting check for {variable}".format(variable=v_latex) measured_central.Rebin(2) measured_pt_reweighted_up.Rebin(2) measured_pt_reweighted_down.Rebin(2) measured_eta_reweighted_up.Rebin(2) measured_eta_reweighted_down.Rebin(2) data.Rebin(2) measured_central.Scale(1 / measured_central.Integral()) measured_pt_reweighted_up.Scale(1 / measured_pt_reweighted_up.Integral()) measured_pt_reweighted_down.Scale(1 / measured_pt_reweighted_down.Integral()) measured_eta_reweighted_up.Scale(1 / measured_eta_reweighted_up.Integral()) measured_eta_reweighted_down.Scale(1 / measured_eta_reweighted_down.Integral()) data.Scale(1 / data.Integral()) compare_measurements( models={ "Central": measured_central, "PtReweighted Up": measured_pt_reweighted_up, "PtReweighted Down": measured_pt_reweighted_down, "EtaReweighted Up": measured_eta_reweighted_up, "EtaReweighted Down": measured_eta_reweighted_down, }, measurements={"Data": data}, show_measurement_errors=True, histogram_properties=hp, save_folder="plots/unfolding/reweighting_check", save_as=["pdf"], )
def compare_qcd_control_regions( variable = 'MET', met_type = 'patType1CorrectedPFMet', title = 'Untitled', channel = 'electron' ): ''' Compares the templates from the control regions in different bins of the current variable''' global fit_variable_properties, b_tag_bin, save_as, b_tag_bin_ctl variable_bins = variable_bins_ROOT[variable] histogram_template = get_histogram_template( variable ) for fit_variable in electron_fit_variables: all_hists = {} inclusive_hist = None if '_bl' in fit_variable: b_tag_bin_ctl = '1orMoreBtag' else: b_tag_bin_ctl = '0orMoreBtag' save_path = 'plots/%dTeV/fit_variables/%s/%s/' % ( measurement_config.centre_of_mass_energy, variable, fit_variable ) make_folder_if_not_exists( save_path + '/qcd/' ) max_bins = 3 for bin_range in variable_bins[0:max_bins]: params = {'met_type': met_type, 'bin_range':bin_range, 'fit_variable':fit_variable, 'b_tag_bin':b_tag_bin, 'variable':variable} fit_variable_distribution = histogram_template % params qcd_fit_variable_distribution = fit_variable_distribution.replace( 'Ref selection', 'QCDConversions' ) qcd_fit_variable_distribution = qcd_fit_variable_distribution.replace( b_tag_bin, b_tag_bin_ctl ) # format: histograms['data'][qcd_fit_variable_distribution] histograms = get_histograms_from_files( [qcd_fit_variable_distribution], histogram_files ) prepare_histograms( histograms, rebin = fit_variable_properties[fit_variable]['rebin'], scale_factor = measurement_config.luminosity_scale ) histograms_for_cleaning = {'data':histograms['data'][qcd_fit_variable_distribution], 'V+Jets':histograms['V+Jets'][qcd_fit_variable_distribution], 'SingleTop':histograms['SingleTop'][qcd_fit_variable_distribution], 'TTJet':histograms['TTJet'][qcd_fit_variable_distribution]} qcd_from_data = clean_control_region( histograms_for_cleaning, subtract = ['TTJet', 'V+Jets', 'SingleTop'] ) # clean all_hists[bin_range] = qcd_from_data # create the inclusive distributions inclusive_hist = deepcopy( all_hists[variable_bins[0]] ) for bin_range in variable_bins[1:max_bins]: inclusive_hist += all_hists[bin_range] for bin_range in variable_bins[0:max_bins]: if not all_hists[bin_range].Integral() == 0: all_hists[bin_range].Scale( 1 / all_hists[bin_range].Integral() ) # normalise all histograms inclusive_hist.Scale( 1 / inclusive_hist.Integral() ) # now compare inclusive to all bins histogram_properties = Histogram_properties() histogram_properties.x_axis_title = fit_variable_properties[fit_variable]['x-title'] histogram_properties.y_axis_title = fit_variable_properties[fit_variable]['y-title'] histogram_properties.y_axis_title = histogram_properties.y_axis_title.replace( 'Events', 'a.u.' ) histogram_properties.x_limits = [fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max']] # histogram_properties.y_limits = [0, 0.5] histogram_properties.title = title histogram_properties.additional_text = channel_latex[channel] + ', ' + b_tag_bins_latex[b_tag_bin_ctl] histogram_properties.name = variable + '_' + fit_variable + '_' + b_tag_bin_ctl + '_QCD_template_comparison' histogram_properties.y_max_scale = 1.5 measurements = {bin_range + ' GeV': histogram for bin_range, histogram in all_hists.iteritems()} measurements = OrderedDict( sorted( measurements.items() ) ) compare_measurements( models = {'inclusive' : inclusive_hist}, measurements = measurements, show_measurement_errors = True, histogram_properties = histogram_properties, save_folder = save_path + '/qcd/', save_as = save_as )
def compare_vjets_btag_regions(variable='MET', met_type='patType1CorrectedPFMet', title='Untitled', channel='electron'): ''' Compares the V+Jets template in different b-tag bins''' global fit_variable_properties, b_tag_bin, save_as, b_tag_bin_ctl b_tag_bin_ctl = '0orMoreBtag' variable_bins = variable_bins_ROOT[variable] histogram_template = get_histogram_template(variable) for fit_variable in electron_fit_variables: if '_bl' in fit_variable: b_tag_bin_ctl = '1orMoreBtag' else: b_tag_bin_ctl = '0orMoreBtag' save_path = 'plots/%dTeV/fit_variables/%s/%s/' % ( measurement_config.centre_of_mass_energy, variable, fit_variable) make_folder_if_not_exists(save_path + '/vjets/') histogram_properties = Histogram_properties() histogram_properties.x_axis_title = fit_variable_properties[ fit_variable]['x-title'] histogram_properties.y_axis_title = fit_variable_properties[ fit_variable]['y-title'] histogram_properties.y_axis_title = histogram_properties.y_axis_title.replace( 'Events', 'a.u.') histogram_properties.x_limits = [ fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max'] ] histogram_properties.title = title histogram_properties.additional_text = channel_latex[ channel] + ', ' + b_tag_bins_latex[b_tag_bin_ctl] histogram_properties.y_max_scale = 1.5 for bin_range in variable_bins: params = { 'met_type': met_type, 'bin_range': bin_range, 'fit_variable': fit_variable, 'b_tag_bin': b_tag_bin, 'variable': variable } fit_variable_distribution = histogram_template % params fit_variable_distribution_ctl = fit_variable_distribution.replace( b_tag_bin, b_tag_bin_ctl) # format: histograms['data'][qcd_fit_variable_distribution] histograms = get_histograms_from_files( [fit_variable_distribution, fit_variable_distribution_ctl], {'V+Jets': histogram_files['V+Jets']}) prepare_histograms( histograms, rebin=fit_variable_properties[fit_variable]['rebin'], scale_factor=measurement_config.luminosity_scale) histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_' + b_tag_bin_ctl + '_VJets_template_comparison' histograms['V+Jets'][fit_variable_distribution].Scale( 1 / histograms['V+Jets'][fit_variable_distribution].Integral()) histograms['V+Jets'][fit_variable_distribution_ctl].Scale( 1 / histograms['V+Jets'][fit_variable_distribution_ctl].Integral()) compare_measurements( models={ 'no b-tag': histograms['V+Jets'][fit_variable_distribution_ctl] }, measurements={ '$>=$ 2 b-tags': histograms['V+Jets'][fit_variable_distribution] }, show_measurement_errors=True, histogram_properties=histogram_properties, save_folder=save_path + '/vjets/', save_as=save_as)
def compare_qcd_control_regions(variable='MET', met_type='patType1CorrectedPFMet', title='Untitled', channel='electron'): ''' Compares the templates from the control regions in different bins of the current variable''' global fit_variable_properties, b_tag_bin, save_as, b_tag_bin_ctl variable_bins = variable_bins_ROOT[variable] histogram_template = get_histogram_template(variable) for fit_variable in electron_fit_variables: all_hists = {} inclusive_hist = None if '_bl' in fit_variable: b_tag_bin_ctl = '1orMoreBtag' else: b_tag_bin_ctl = '0orMoreBtag' save_path = 'plots/%dTeV/fit_variables/%s/%s/' % ( measurement_config.centre_of_mass_energy, variable, fit_variable) make_folder_if_not_exists(save_path + '/qcd/') max_bins = 3 for bin_range in variable_bins[0:max_bins]: params = { 'met_type': met_type, 'bin_range': bin_range, 'fit_variable': fit_variable, 'b_tag_bin': b_tag_bin, 'variable': variable } fit_variable_distribution = histogram_template % params qcd_fit_variable_distribution = fit_variable_distribution.replace( 'Ref selection', 'QCDConversions') qcd_fit_variable_distribution = qcd_fit_variable_distribution.replace( b_tag_bin, b_tag_bin_ctl) # format: histograms['data'][qcd_fit_variable_distribution] histograms = get_histograms_from_files( [qcd_fit_variable_distribution], histogram_files) prepare_histograms( histograms, rebin=fit_variable_properties[fit_variable]['rebin'], scale_factor=measurement_config.luminosity_scale) histograms_for_cleaning = { 'data': histograms['data'][qcd_fit_variable_distribution], 'V+Jets': histograms['V+Jets'][qcd_fit_variable_distribution], 'SingleTop': histograms['SingleTop'][qcd_fit_variable_distribution], 'TTJet': histograms['TTJet'][qcd_fit_variable_distribution] } qcd_from_data = clean_control_region( histograms_for_cleaning, subtract=['TTJet', 'V+Jets', 'SingleTop']) # clean all_hists[bin_range] = qcd_from_data # create the inclusive distributions inclusive_hist = deepcopy(all_hists[variable_bins[0]]) for bin_range in variable_bins[1:max_bins]: inclusive_hist += all_hists[bin_range] for bin_range in variable_bins[0:max_bins]: if not all_hists[bin_range].Integral() == 0: all_hists[bin_range].Scale(1 / all_hists[bin_range].Integral()) # normalise all histograms inclusive_hist.Scale(1 / inclusive_hist.Integral()) # now compare inclusive to all bins histogram_properties = Histogram_properties() histogram_properties.x_axis_title = fit_variable_properties[ fit_variable]['x-title'] histogram_properties.y_axis_title = fit_variable_properties[ fit_variable]['y-title'] histogram_properties.y_axis_title = histogram_properties.y_axis_title.replace( 'Events', 'a.u.') histogram_properties.x_limits = [ fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max'] ] # histogram_properties.y_limits = [0, 0.5] histogram_properties.title = title histogram_properties.additional_text = channel_latex[ channel] + ', ' + b_tag_bins_latex[b_tag_bin_ctl] histogram_properties.name = variable + '_' + fit_variable + '_' + b_tag_bin_ctl + '_QCD_template_comparison' histogram_properties.y_max_scale = 1.5 measurements = { bin_range + ' GeV': histogram for bin_range, histogram in all_hists.iteritems() } measurements = OrderedDict(sorted(measurements.items())) compare_measurements(models={'inclusive': inclusive_hist}, measurements=measurements, show_measurement_errors=True, histogram_properties=histogram_properties, save_folder=save_path + '/qcd/', save_as=save_as)