def plot_fit_results( histograms, category, channel ): global variable, b_tag_bin, output_folder from tools.plotting import Histogram_properties, make_data_mc_comparison_plot fit_variables = histograms.keys() for variable_bin in variable_bins_ROOT[variable]: path = output_folder + str( measurement_config.centre_of_mass_energy ) + 'TeV/' + variable + '/' + category + '/fit_results/' make_folder_if_not_exists( path ) for fit_variable in fit_variables: plotname = channel + '_' + fit_variable + '_bin_' + variable_bin # check if template plots exist already for output_format in output_formats: if os.path.isfile( plotname + '.' + output_format ): continue # plot with matplotlib h_data = histograms[fit_variable][variable_bin]['data'] h_signal = histograms[fit_variable][variable_bin]['signal'] h_background = histograms[fit_variable][variable_bin]['background'] histogram_properties = Histogram_properties() histogram_properties.name = plotname histogram_properties.x_axis_title = fit_variables_latex[fit_variable] histogram_properties.y_axis_title = 'Events/(%s)' % get_unit_string(fit_variable) label, _ = get_cms_labels( channel ) histogram_properties.title = label histogram_properties.x_limits = measurement_config.fit_boundaries[fit_variable] make_data_mc_comparison_plot( [h_data, h_background, h_signal], ['data', 'background', 'signal'], ['black', 'green', 'red'], histogram_properties, save_folder = path, save_as = output_formats )
def plot_fit_results(histograms, category, channel): global variable, b_tag_bin, output_folder from tools.plotting import Histogram_properties, make_data_mc_comparison_plot for variable_bin in variable_bins_ROOT[variable]: path = output_folder + str(measurement_config.centre_of_mass) + 'TeV/' + variable + '/' + category + '/fit_results/' make_folder_if_not_exists(path) plotname = channel + '_bin_' + variable_bin # check if template plots exist already for output_format in output_formats: if os.path.isfile(plotname + '.' + output_format): continue # plot with matplotlib h_data = histograms[variable_bin]['data'] h_signal = histograms[variable_bin]['signal'] h_background = histograms[variable_bin]['background'] histogram_properties = Histogram_properties() histogram_properties.name = plotname histogram_properties.x_axis_title = channel + ' $\left|\eta\\right|$' histogram_properties.y_axis_title = 'events/0.2' histogram_properties.title = get_cms_labels(channel) make_data_mc_comparison_plot([h_data, h_background, h_signal], ['data', 'background', 'signal'], ['black', 'green', 'red'], histogram_properties, save_folder = path, save_as = output_formats)
def plot( self, plot_options = {} ): ''' Plots the stored histograms based on the plot options ''' file_name = self.name if plot_options.has_key('output_file'): file_name = plot_options['output_file'] output_format = plot_options['output_format'] output_folder = plot_options['output_folder'] plot_type = plot_options['plot_type'] histogram_properties = Histogram_properties(plot_options) histogram_properties.name = file_name if plot_options.has_key('rebin') and plot_options['rebin'] > 1: for hist in self.histograms: hist.Rebin(plot_options['rebin']) colours = ['green', 'yellow', 'magenta', 'red', 'black'] if plot_options.has_key('colours'): colours = plot_options['colours'] if plot_type == 'shape_comparison': make_shape_comparison_plot( shapes = self.histograms, names = self.labels, colours = colours, histogram_properties = histogram_properties, fill_area = True, make_ratio = True, alpha = 0.5, save_folder = output_folder, save_as = output_format ) elif plot_type == 'data_mc_comparison': make_data_mc_comparison_plot( histograms = self.histograms, histogram_lables = self.labels, histogram_colors = colours, histogram_properties = histogram_properties, data_index = 0, save_folder = output_folder, save_as = output_format, normalise = False, show_ratio = True, show_stat_errors_on_mc = True, draw_vertical_line = 0 ) else: print 'Plot type "%s" not known, exiting.' % plot_type sys.exit()
def plot_fit_results( histograms, category, channel ): global variable, b_tag_bin, output_folder, phase_space from tools.plotting import Histogram_properties, make_data_mc_comparison_plot fit_variables = histograms.keys() variableBins = None if phase_space == 'VisiblePS': variableBins = variable_bins_visiblePS_ROOT elif phase_space == 'FullPS': variableBins = variable_bins_ROOT for variable_bin in variableBins[variable]: path = output_folder + str( measurement_config.centre_of_mass_energy ) + 'TeV/' + variable + '/' + category + '/fit_results/' make_folder_if_not_exists( path ) for fit_variable in fit_variables: plotname = channel + '_' + fit_variable + '_bin_' + variable_bin # check if template plots exist already for output_format in output_formats: if os.path.isfile( plotname + '.' + output_format ): continue # plot with matplotlib h_data = histograms[fit_variable][variable_bin]['data'] h_signal = histograms[fit_variable][variable_bin]['signal'] h_background = histograms[fit_variable][variable_bin]['background'] histogram_properties = Histogram_properties() histogram_properties.name = plotname histogram_properties.x_axis_title = fit_variables_latex[fit_variable] histogram_properties.y_axis_title = 'Events/(%s)' % get_unit_string(fit_variable) label, _ = get_cms_labels( channel ) histogram_properties.title = label histogram_properties.x_limits = measurement_config.fit_boundaries[fit_variable] make_data_mc_comparison_plot( [h_data, h_background, h_signal], ['data', 'background', 'signal'], ['black', 'green', 'red'], histogram_properties, save_folder = path, save_as = output_formats )
histogram_lables = [ 'data', 'QCD', 'V+Jets', 'Single-Top', 'TTJet' ] histogram_colors = [ 'black', 'yellow', 'green', 'magenta', 'red' ] histogram_properties = Histogram_properties() histogram_properties.name = 'Closure_' + 'simple' + '_' + fitVariable + '_' + variable + '_' + str( whichBin) histogram_properties.x_axis_title = fit_variable_properties[ fitVariable]['x-title'] histogram_properties.y_axis_title = fit_variable_properties[ fitVariable]['y-title'] make_data_mc_comparison_plot( histogramsToDraw, histogram_lables, histogram_colors, histogram_properties, save_folder='data/closure_test/' + test + '/absolute_eta_M3_angle_bl/8TeV/', show_ratio=False, save_as=['pdf'], ) pass pass pass pass
def plot(self, plot_options={}): ''' Plots the stored histograms based on the plot options ''' # defaults file_name = self.name alpha = 0.5 fill_area = True if plot_options.has_key('output_file'): file_name = plot_options['output_file'] output_format = plot_options['output_format'] output_folder = plot_options['output_folder'] plot_type = plot_options['plot_type'] if plot_options.has_key('fill_area'): fill_area = plot_options['fill_area'] if plot_options.has_key('alpha'): alpha = plot_options['alpha'] histogram_properties = Histogram_properties(plot_options) histogram_properties.name = file_name if plot_options.has_key('rebin') and plot_options['rebin'] > 1: rebin = plot_options['rebin'] is_list = isinstance(rebin, types.ListType) for i, hist in enumerate(self.histograms): if is_list: self.histograms[i] = conditional_rebin(hist, rebin) else: hist.rebin(rebin) colours = ['green', 'yellow', 'magenta', 'red', 'black'] if plot_options.has_key('colours'): colours = plot_options['colours'] if plot_type == 'shape_comparison': make_shape_comparison_plot( shapes=self.histograms, names=self.labels, colours=colours, histogram_properties=histogram_properties, fill_area=fill_area, make_ratio=True, alpha=alpha, save_folder=output_folder, save_as=output_format) elif plot_type == 'data_mc_comparison': make_data_mc_comparison_plot( histograms=self.histograms, histogram_lables=self.labels, histogram_colors=colours, histogram_properties=histogram_properties, data_index=0, save_folder=output_folder, save_as=output_format, normalise=False, show_ratio=True, show_stat_errors_on_mc=True, draw_vertical_line=0) else: print 'Plot type "%s" not known, exiting.' % plot_type sys.exit()
def make_ttbarReco_plot( channel, x_axis_title, y_axis_title, signal_region_tree, control_region_tree, branchName, name_prefix, x_limits, nBins, use_qcd_data_region = False, y_limits = [], y_max_scale = 1.2, rebin = 1, legend_location = ( 0.98, 0.78 ), cms_logo_location = 'right', log_y = False, legend_color = False, ratio_y_limits = [0.3, 1.7], normalise = False, ): global output_folder, measurement_config, category, normalise_to_fit global preliminary, norm_variable, sum_bins, b_tag_bin, histogram_files # Input files, normalisations, tree/region names qcd_data_region = '' title = title_template % ( measurement_config.new_luminosity / 1000., measurement_config.centre_of_mass_energy ) normalisation = None if channel == 'electron': histogram_files['data'] = measurement_config.data_file_electron_trees histogram_files['QCD'] = measurement_config.electron_QCD_MC_category_templates_trees[category] if normalise_to_fit: normalisation = normalisations_electron[norm_variable] if use_qcd_data_region: qcd_data_region = 'QCDConversions' if channel == 'muon': histogram_files['data'] = measurement_config.data_file_muon_trees histogram_files['QCD'] = measurement_config.muon_QCD_MC_category_templates_trees[category] if normalise_to_fit: normalisation = normalisations_muon[norm_variable] if use_qcd_data_region: qcd_data_region = 'QCD non iso mu+jets ge3j' histograms = get_histograms_from_trees( trees = [signal_region_tree, control_region_tree], branch = branchName, weightBranch = '1', files = histogram_files, nBins = nBins, xMin = x_limits[0], xMax = x_limits[-1] ) selection = 'SolutionCategory == 0' histogramsNoSolution = get_histograms_from_trees( trees = [signal_region_tree], branch = branchName, weightBranch = '1', selection = selection, files = histogram_files, nBins = nBins, xMin = x_limits[0], xMax = x_limits[-1] ) selection = 'SolutionCategory == 1' histogramsCorrect = get_histograms_from_trees( trees = [signal_region_tree], branch = branchName, weightBranch = '1', selection = selection, files = histogram_files, nBins = nBins, xMin = x_limits[0], xMax = x_limits[-1] ) selection = 'SolutionCategory == 2' histogramsNotSL = get_histograms_from_trees( trees = [signal_region_tree], branch = branchName, weightBranch = '1', selection = selection, files = histogram_files, nBins = nBins, xMin = x_limits[0], xMax = x_limits[-1] ) selection = 'SolutionCategory == 3' histogramsNotReco = get_histograms_from_trees( trees = [signal_region_tree], branch = branchName, weightBranch = '1', selection = selection, files = histogram_files, nBins = nBins, xMin = x_limits[0], xMax = x_limits[-1] ) selection = 'SolutionCategory > 3' histogramsWrong = get_histograms_from_trees( trees = [signal_region_tree], branch = branchName, weightBranch = '1', selection = selection, files = histogram_files, nBins = nBins, xMin = x_limits[0], xMax = x_limits[-1] ) # Split histograms up into signal/control (?) signal_region_hists = {} inclusive_control_region_hists = {} for sample in histograms.keys(): signal_region_hists[sample] = histograms[sample][signal_region_tree] if use_qcd_data_region: inclusive_control_region_hists[sample] = histograms[sample][control_region_tree] prepare_histograms( histograms, rebin = 1, scale_factor = measurement_config.luminosity_scale ) prepare_histograms( histogramsNoSolution, rebin = 1, scale_factor = measurement_config.luminosity_scale ) prepare_histograms( histogramsCorrect, rebin = 1, scale_factor = measurement_config.luminosity_scale ) prepare_histograms( histogramsNotSL, rebin = 1, scale_factor = measurement_config.luminosity_scale ) prepare_histograms( histogramsNotReco, rebin = 1, scale_factor = measurement_config.luminosity_scale ) prepare_histograms( histogramsWrong, rebin = 1, scale_factor = measurement_config.luminosity_scale ) qcd_from_data = signal_region_hists['QCD'] # Which histograms to draw, and properties histograms_to_draw = [signal_region_hists['data'], qcd_from_data, signal_region_hists['V+Jets'], signal_region_hists['SingleTop'], histogramsNoSolution['TTJet'][signal_region_tree], histogramsNotSL['TTJet'][signal_region_tree], histogramsNotReco['TTJet'][signal_region_tree], histogramsWrong['TTJet'][signal_region_tree], histogramsCorrect['TTJet'][signal_region_tree] ] histogram_lables = ['data', 'QCD', 'V+Jets', 'Single-Top', samples_latex['TTJet'] + ' - no solution', samples_latex['TTJet'] + ' - not SL', samples_latex['TTJet'] + ' - not reconstructible', samples_latex['TTJet'] + ' - wrong reco', samples_latex['TTJet'] + ' - correct', ] histogram_colors = ['black', 'yellow', 'green', 'magenta', 'black', 'burlywood', 'chartreuse', 'blue', 'red' ] histogram_properties = Histogram_properties() histogram_properties.name = name_prefix + b_tag_bin if category != 'central': histogram_properties.name += '_' + category histogram_properties.title = title histogram_properties.x_axis_title = x_axis_title histogram_properties.y_axis_title = y_axis_title histogram_properties.x_limits = x_limits histogram_properties.y_limits = y_limits histogram_properties.y_max_scale = y_max_scale histogram_properties.xerr = None # workaround for rootpy issue #638 histogram_properties.emptybins = True if b_tag_bin: histogram_properties.additional_text = channel_latex[channel] + ', ' + b_tag_bins_latex[b_tag_bin] else: histogram_properties.additional_text = channel_latex[channel] histogram_properties.legend_location = legend_location histogram_properties.cms_logo_location = cms_logo_location histogram_properties.preliminary = preliminary histogram_properties.set_log_y = log_y histogram_properties.legend_color = legend_color if ratio_y_limits: histogram_properties.ratio_y_limits = ratio_y_limits if normalise_to_fit: histogram_properties.mc_error = get_normalisation_error( normalisation ) histogram_properties.mc_errors_label = 'fit uncertainty' else: histogram_properties.mc_error = mc_uncertainty histogram_properties.mc_errors_label = 'MC unc.' # Actually draw histograms make_data_mc_comparison_plot( histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder = output_folder, show_ratio = False, normalise = normalise, ) histogram_properties.name += '_with_ratio' loc = histogram_properties.legend_location # adjust legend location as it is relative to canvas! histogram_properties.legend_location = ( loc[0], loc[1] + 0.05 ) make_data_mc_comparison_plot( histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder = output_folder, show_ratio = True, normalise = normalise, )
eventsPerBin = (xMax - xMin) / nBins if fit_variables_units_latex[var] != '': histogram_properties.x_axis_title = '%s [%s]' % ( fit_variables_latex[var], fit_variables_units_latex[var]) histogram_properties.y_axis_title = 'Events/(%.2g %s)' % ( eventsPerBin, fit_variables_units_latex[var]) else: histogram_properties.x_axis_title = '%s' % ( fit_variables_latex[var]) histogram_properties.y_axis_title = 'Events/(%.2g)' % eventsPerBin histogram_properties.name += '_with_ratio' make_data_mc_comparison_plot(histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder=output_folder, show_ratio=False) # Variables for diff xsec for var in ['MET', 'HT', 'ST', 'WPT', 'MT']: print var controlTree = 'TTbar_plus_X_analysis/%s/%s/FitVariables' % ( channel, control_region) bins = bin_edges[var] xMin = bins[0] xMax = bins[-1] nBins = 40 histograms = get_histograms_from_trees(
qcd_predicted_mc = histograms['QCD'][control_region] histograms_to_draw = [histograms['data'][control_region], qcd_predicted_mc, histograms['V+Jets'][control_region], histograms['SingleTop'][control_region], histograms['TTJet'][control_region]] histogram_lables = ['data', 'QCD', 'V+Jets', 'Single-Top', samples_latex['TTJet']] histogram_colors = ['black', 'yellow', 'green', 'magenta', 'red'] histogram_properties = Histogram_properties() histogram_properties.name = 'EPlusJets_BJets_invmass_' + b_tag_bin histogram_properties.title = e_title + ', ' + b_tag_bins_latex[b_tag_bin] histogram_properties.x_axis_title = '$M_{\mathrm{b}\\bar{\mathrm{b}}}$' histogram_properties.y_axis_title = 'Normalised events/(20 GeV)' histogram_properties.x_limits = [0, 800] histogram_properties.mc_error = 0.15 make_data_mc_comparison_plot(histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder = output_folder, show_ratio = False) histogram_properties.name += '_with_ratio' make_data_mc_comparison_plot(histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder = output_folder, show_ratio = True) #bjet invariant mass b_tag_bin = '3btags' control_region = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/bjet_invariant_mass_' + b_tag_bin histograms = get_histograms_from_files([control_region], histogram_files) prepare_histograms(histograms, rebin=10, scale_factor = measurement_config.luminosity_scale) qcd_predicted_mc = histograms['QCD'][control_region] histograms_to_draw = [histograms['data'][control_region], qcd_predicted_mc, histograms['V+Jets'][control_region],
def plot_fit_variable(histograms, fit_variable, variable, bin_range, fit_variable_distribution, qcd_fit_variable_distribution, title, save_path, channel='electron'): global fit_variable_properties, b_tag_bin, save_as, b_tag_bin_ctl histograms_ = deepcopy(histograms) mc_uncertainty = 0.10 prepare_histograms(histograms_, rebin=fit_variable_properties[fit_variable]['rebin'], scale_factor=measurement_config.luminosity_scale) ###################################### # plot the control regions as they are ###################################### 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.x_limits = [ fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max'] ] histogram_properties.y_max_scale = 2 histogram_lables = [ 'data', 'QCD', 'V+Jets', 'Single-Top', samples_latex['TTJet'] ] histogram_colors = ['black', 'yellow', 'green', 'magenta', 'red'] # qcd_from_data = histograms_['data'][qcd_fit_variable_distribution].Clone() # clean against other processes 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']) histograms_to_draw = [ histograms_['data'][qcd_fit_variable_distribution], histograms_['QCD'][qcd_fit_variable_distribution], histograms_['V+Jets'][qcd_fit_variable_distribution], histograms_['SingleTop'][qcd_fit_variable_distribution], histograms_['TTJet'][qcd_fit_variable_distribution] ] histogram_properties.title = title histogram_properties.additional_text = channel_latex[ channel] + ', ' + b_tag_bins_latex[b_tag_bin_ctl] histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_%s_QCDConversions' % b_tag_bin_ctl make_data_mc_comparison_plot( histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder=save_path + '/qcd/', show_ratio=False, save_as=save_as, ) ###################################### # plot QCD against data control region with TTJet, SingleTop and V+Jets removed ###################################### histograms_to_draw = [ qcd_from_data, histograms_['QCD'][qcd_fit_variable_distribution], ] histogram_properties.y_max_scale = 1.5 histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_%s_QCDConversions_subtracted' % b_tag_bin_ctl make_data_mc_comparison_plot( histograms_to_draw, histogram_lables=['data', 'QCD'], histogram_colors=['black', 'yellow'], histogram_properties=histogram_properties, save_folder=save_path + '/qcd/', show_ratio=False, save_as=save_as, ) ###################################### # plot signal region ###################################### # scale QCD to predicted n_qcd_predicted_mc = histograms_['QCD'][ fit_variable_distribution].Integral() n_qcd_fit_variable_distribution = qcd_from_data.Integral() if not n_qcd_fit_variable_distribution == 0: qcd_from_data.Scale(1.0 / n_qcd_fit_variable_distribution * n_qcd_predicted_mc) histograms_to_draw = [ histograms_['data'][fit_variable_distribution], qcd_from_data, histograms_['V+Jets'][fit_variable_distribution], histograms_['SingleTop'][fit_variable_distribution], histograms_['TTJet'][fit_variable_distribution] ] histogram_properties.additional_text = channel_latex[ channel] + ', ' + b_tag_bins_latex[b_tag_bin] histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_' + b_tag_bin make_data_mc_comparison_plot( histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder=save_path, show_ratio=False, save_as=save_as, ) ###################################### # plot templates ###################################### histogram_properties.mc_error = mc_uncertainty histogram_properties.mc_errors_label = '$\mathrm{t}\\bar{\mathrm{t}}$ uncertainty' histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_' + b_tag_bin + '_templates' histogram_properties.y_max_scale = 2 # change histogram order for better visibility histograms_to_draw = [ histograms_['TTJet'][fit_variable_distribution] + histograms_['SingleTop'][fit_variable_distribution], histograms_['TTJet'][fit_variable_distribution], histograms_['SingleTop'][fit_variable_distribution], histograms_['V+Jets'][fit_variable_distribution], qcd_from_data ] histogram_lables = [ 'QCD', 'V+Jets', 'Single-Top', samples_latex['TTJet'], samples_latex['TTJet'] + ' + ' + 'Single-Top' ] histogram_lables.reverse() # change QCD color to orange for better visibility histogram_colors = ['orange', 'green', 'magenta', 'red', 'black'] histogram_colors.reverse() # plot template make_shape_comparison_plot( shapes=histograms_to_draw, names=histogram_lables, colours=histogram_colors, histogram_properties=histogram_properties, fill_area=False, alpha=1, save_folder=save_path, save_as=save_as, )
h2.legendstyle = 'F' stack = HistStack() stack.Add(h1) stack.Add(h2) # plot with matplotlib plot_with_plotting_script = True if plot_with_plotting_script: properties = Histogram_properties() properties.name = 'matplotlib_hist' properties.x_axis_title = 'Mass' properties.y_axis_title = 'Events' make_data_mc_comparison_plot([h3, h1, h2], ['data', 'background', 'signal'], ['black', 'green', 'red'], properties) properties.name += '_with_ratio' make_data_mc_comparison_plot([h3, h1, h2], ['data', 'background', 'signal'], ['black', 'green', 'red'], properties, show_ratio=True) properties.name = 'matplotlib_hist_comparison' properties.y_limits = [0, 0.4] make_control_region_comparison(h1, h2, 'background', 'signal', properties) else: fig = plt.figure(figsize=(14, 10), dpi=300) #, facecolor='white')
b_tag_bin = '0btag' control_region = 'topReconstruction/backgroundShape/mttbar_3jets_conversions_withMETAndAsymJets_' + b_tag_bin histograms = get_histograms_from_files([control_region], histogram_files) prepare_histograms(histograms, rebin=50) histograms_to_draw = [ histograms['data'][control_region], histograms['QCD'][control_region], histograms['ZJets'][control_region], histograms['WJets'][control_region], histograms['SingleTop'][control_region], histograms['TTJet'][control_region] ] histogram_lables = [ 'data', 'QCD', samples_latex['ZJets'], samples_latex['WJets'], 'Single-Top', samples_latex['TTJet'] ] histogram_colors = ['black', 'yellow', 'blue', 'green', 'magenta', 'red'] histogram_properties = Histogram_properties() histogram_properties.name = 'Mttbar' histogram_properties.title = 'CMS Preliminary, $\mathcal{L}$ = 5.1 fb$^{-1}$ at $\sqrt{s}$ = 7 TeV \n e+jets, $\geq$4 jets, ' + b_tag_bins_latex[ b_tag_bin] histogram_properties.x_axis_title = '$m_{\mathrm{t}\\bar{\mathrm{t}}}$ [GeV]' histogram_properties.y_axis_title = 'Events/(50 GeV)' histogram_properties.x_limits = [300, 1800] histogram_properties.mc_error = 0.15 histogram_properties.mc_errors_label = '$\mathrm{t}\\bar{\mathrm{t}}$ uncertainty' make_data_mc_comparison_plot(histograms_to_draw, histogram_lables, histogram_colors, histogram_properties)
h2.linewidth = 0 h2.legendstyle = 'F' stack = HistStack() stack.Add(h1) stack.Add(h2) # plot with matplotlib plot_with_plotting_script = True if plot_with_plotting_script: properties = Histogram_properties() properties.name = 'matplotlib_hist' properties.x_axis_title = 'Mass' properties.y_axis_title = 'Events' make_data_mc_comparison_plot( [h3, h1, h2], ['data', 'background', 'signal'], ['black', 'green', 'red'], properties ) properties.name += '_with_ratio' make_data_mc_comparison_plot( [h3, h1, h2], ['data', 'background', 'signal'], ['black', 'green', 'red'], properties, show_ratio = True ) properties.name = 'matplotlib_hist_comparison' properties.y_limits = [0, 0.4] make_control_region_comparison( h1, h2, 'background', 'signal', properties ) else: fig = plt.figure(figsize=(14, 10), dpi=300)#, facecolor='white') axes = plt.axes() axes.xaxis.set_minor_locator(AutoMinorLocator()) axes.yaxis.set_minor_locator(AutoMinorLocator()) # axes.yaxis.set_major_locator(MultipleLocator(20)) axes.tick_params(which='major', labelsize=15, length=8)
elif template == 'data': histograms[template].Scale( initial_values_[template][whichBin][0]) pass histogramsToDraw = [ histograms['data'], histograms['QCD'], histograms['V+Jets'], histograms['SingleTop'], histograms['TTJet'] ] histogram_lables = ['data', 'QCD', 'V+Jets', 'Single-Top', 'TTJet'] histogram_colors = ['black', 'yellow', 'green', 'magenta', 'red'] histogram_properties = Histogram_properties() histogram_properties.name = 'Closure_'+'simple'+'_'+fitVariable+'_'+variable+'_'+str(whichBin) histogram_properties.x_axis_title = fit_variable_properties[fitVariable]['x-title'] histogram_properties.y_axis_title = fit_variable_properties[fitVariable]['y-title'] make_data_mc_comparison_plot( histogramsToDraw, histogram_lables, histogram_colors, histogram_properties, save_folder = 'data/closure_test/'+test+'/absolute_eta_M3_angle_bl/8TeV/', show_ratio = False, save_as = ['pdf'], ) pass pass pass pass
'TTJet': path_to_files + 'TTJet_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon), 'data' : path_to_files + '%s_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (data, str(lumi), pfmuon), 'WJets': path_to_files + 'WJetsToLNu_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon), 'ZJets': path_to_files + 'DYJetsToLL_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon), 'QCD': path_to_files + 'QCD_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon), 'SingleTop': path_to_files + 'SingleTop_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon), } b_tag_bin = '0btag' control_region = 'topReconstruction/backgroundShape/mttbar_3jets_conversions_withMETAndAsymJets_' + b_tag_bin histograms = get_histograms_from_files([control_region], histogram_files) prepare_histograms(histograms, rebin=50) histograms_to_draw = [histograms['data'][control_region], histograms['QCD'][control_region], histograms['ZJets'][control_region], histograms['WJets'][control_region], histograms['SingleTop'][control_region], histograms['TTJet'][control_region]] histogram_lables = ['data', 'QCD', samples_latex['ZJets'], samples_latex['WJets'], 'Single-Top', samples_latex['TTJet']] histogram_colors = ['black', 'yellow', 'blue', 'green', 'magenta', 'red'] histogram_properties = Histogram_properties() histogram_properties.name = 'Mttbar' histogram_properties.title = 'CMS Preliminary, $\mathcal{L}$ = 5.1 fb$^{-1}$ at $\sqrt{s}$ = 7 TeV \n e+jets, $\geq$4 jets, ' + b_tag_bins_latex[b_tag_bin] histogram_properties.x_axis_title = '$m_{\mathrm{t}\\bar{\mathrm{t}}}$ [GeV]' histogram_properties.y_axis_title = 'Events/(50 GeV)' histogram_properties.x_limits=[300,1800] histogram_properties.mc_error = 0.15 histogram_properties.mc_errors_label = '$\mathrm{t}\\bar{\mathrm{t}}$ uncertainty' make_data_mc_comparison_plot(histograms_to_draw, histogram_lables, histogram_colors, histogram_properties)
def plot_fit_variable( histograms, fit_variable, variable, bin_range, fit_variable_distribution, qcd_fit_variable_distribution, title, save_path ): global fit_variable_properties, b_tag_bin, save_as, b_tag_bin_ctl mc_uncertainty = 0.10 prepare_histograms( histograms, rebin = fit_variable_properties[fit_variable]['rebin'], scale_factor = measurement_config.luminosity_scale ) 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.x_limits = [fit_variable_properties[fit_variable]['min'], fit_variable_properties[fit_variable]['max']] histogram_lables = ['data', 'QCD', 'V+Jets', 'Single-Top', samples_latex['TTJet']] histogram_colors = ['black', 'yellow', 'green', 'magenta', 'red'] # qcd_from_data = histograms['data'][qcd_fit_variable_distribution].Clone() # clean against other processes 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'] ) histograms_to_draw = [histograms['data'][qcd_fit_variable_distribution], histograms['QCD'][qcd_fit_variable_distribution], histograms['V+Jets'][qcd_fit_variable_distribution], histograms['SingleTop'][qcd_fit_variable_distribution], histograms['TTJet'][qcd_fit_variable_distribution]] histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin_ctl] histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_%s_QCDConversions' % b_tag_bin_ctl make_data_mc_comparison_plot( histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder = save_path + '/qcd/', show_ratio = False, save_as = save_as, ) histograms_to_draw = [qcd_from_data, histograms['QCD'][qcd_fit_variable_distribution], ] histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_%s_QCDConversions_subtracted' % b_tag_bin_ctl make_data_mc_comparison_plot( histograms_to_draw, histogram_lables = ['data', 'QCD'], histogram_colors = ['black', 'yellow'], histogram_properties = histogram_properties, save_folder = save_path + '/qcd/', show_ratio = False, save_as = save_as, ) # scale QCD to predicted n_qcd_predicted_mc = histograms['QCD'][fit_variable_distribution].Integral() n_qcd_fit_variable_distribution = qcd_from_data.Integral() if not n_qcd_fit_variable_distribution == 0: qcd_from_data.Scale( 1.0 / n_qcd_fit_variable_distribution * n_qcd_predicted_mc ) histograms_to_draw = [histograms['data'][fit_variable_distribution], qcd_from_data, histograms['V+Jets'][fit_variable_distribution], histograms['SingleTop'][fit_variable_distribution], histograms['TTJet'][fit_variable_distribution]] histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin] histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_' + b_tag_bin make_data_mc_comparison_plot( histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder = save_path, show_ratio = False, save_as = save_as, ) histogram_properties.mc_error = mc_uncertainty histogram_properties.mc_errors_label = '$\mathrm{t}\\bar{\mathrm{t}}$ uncertainty' histogram_properties.name = variable + '_' + bin_range + '_' + fit_variable + '_' + b_tag_bin + '_templates' # change histogram order for better visibility histograms_to_draw = [histograms['TTJet'][fit_variable_distribution] + histograms['SingleTop'][fit_variable_distribution], histograms['TTJet'][fit_variable_distribution], histograms['SingleTop'][fit_variable_distribution], histograms['V+Jets'][fit_variable_distribution], qcd_from_data] histogram_lables = ['QCD', 'V+Jets', 'Single-Top', samples_latex['TTJet'], samples_latex['TTJet'] + ' + ' + 'Single-Top'] histogram_lables.reverse() # change QCD color to orange for better visibility histogram_colors = ['orange', 'green', 'magenta', 'red', 'black'] histogram_colors.reverse() make_shape_comparison_plot( shapes = histograms_to_draw, names = histogram_lables, colours = histogram_colors, histogram_properties = histogram_properties, fill_area = False, alpha = 1, save_folder = save_path, save_as = save_as, )
def make_ttbarReco_plot( channel, x_axis_title, y_axis_title, signal_region_tree, control_region_tree, branchName, name_prefix, x_limits, nBins, use_qcd_data_region=False, y_limits=[], y_max_scale=1.2, rebin=1, legend_location=(0.98, 0.78), cms_logo_location='right', log_y=False, legend_color=False, ratio_y_limits=[0.3, 1.7], normalise=False, ): global output_folder, measurement_config, category, normalise_to_fit global preliminary, norm_variable, sum_bins, b_tag_bin, histogram_files # Input files, normalisations, tree/region names qcd_data_region = '' title = title_template % (measurement_config.new_luminosity / 1000., measurement_config.centre_of_mass_energy) normalisation = None if channel == 'electron': histogram_files['data'] = measurement_config.data_file_electron_trees histogram_files[ 'QCD'] = measurement_config.electron_QCD_MC_category_templates_trees[ category] if normalise_to_fit: normalisation = normalisations_electron[norm_variable] if use_qcd_data_region: qcd_data_region = 'QCDConversions' if channel == 'muon': histogram_files['data'] = measurement_config.data_file_muon_trees histogram_files[ 'QCD'] = measurement_config.muon_QCD_MC_category_templates_trees[ category] if normalise_to_fit: normalisation = normalisations_muon[norm_variable] if use_qcd_data_region: qcd_data_region = 'QCD non iso mu+jets ge3j' histograms = get_histograms_from_trees( trees=[signal_region_tree, control_region_tree], branch=branchName, weightBranch='1', files=histogram_files, nBins=nBins, xMin=x_limits[0], xMax=x_limits[-1]) selection = 'SolutionCategory == 0' histogramsNoSolution = get_histograms_from_trees( trees=[signal_region_tree], branch=branchName, weightBranch='1', selection=selection, files=histogram_files, nBins=nBins, xMin=x_limits[0], xMax=x_limits[-1]) selection = 'SolutionCategory == 1' histogramsCorrect = get_histograms_from_trees(trees=[signal_region_tree], branch=branchName, weightBranch='1', selection=selection, files=histogram_files, nBins=nBins, xMin=x_limits[0], xMax=x_limits[-1]) selection = 'SolutionCategory == 2' histogramsNotSL = get_histograms_from_trees(trees=[signal_region_tree], branch=branchName, weightBranch='1', selection=selection, files=histogram_files, nBins=nBins, xMin=x_limits[0], xMax=x_limits[-1]) selection = 'SolutionCategory == 3' histogramsNotReco = get_histograms_from_trees(trees=[signal_region_tree], branch=branchName, weightBranch='1', selection=selection, files=histogram_files, nBins=nBins, xMin=x_limits[0], xMax=x_limits[-1]) selection = 'SolutionCategory > 3' histogramsWrong = get_histograms_from_trees(trees=[signal_region_tree], branch=branchName, weightBranch='1', selection=selection, files=histogram_files, nBins=nBins, xMin=x_limits[0], xMax=x_limits[-1]) # Split histograms up into signal/control (?) signal_region_hists = {} inclusive_control_region_hists = {} for sample in histograms.keys(): signal_region_hists[sample] = histograms[sample][signal_region_tree] if use_qcd_data_region: inclusive_control_region_hists[sample] = histograms[sample][ control_region_tree] prepare_histograms(histograms, rebin=1, scale_factor=measurement_config.luminosity_scale) prepare_histograms(histogramsNoSolution, rebin=1, scale_factor=measurement_config.luminosity_scale) prepare_histograms(histogramsCorrect, rebin=1, scale_factor=measurement_config.luminosity_scale) prepare_histograms(histogramsNotSL, rebin=1, scale_factor=measurement_config.luminosity_scale) prepare_histograms(histogramsNotReco, rebin=1, scale_factor=measurement_config.luminosity_scale) prepare_histograms(histogramsWrong, rebin=1, scale_factor=measurement_config.luminosity_scale) qcd_from_data = signal_region_hists['QCD'] # Which histograms to draw, and properties histograms_to_draw = [ signal_region_hists['data'], qcd_from_data, signal_region_hists['V+Jets'], signal_region_hists['SingleTop'], histogramsNoSolution['TTJet'][signal_region_tree], histogramsNotSL['TTJet'][signal_region_tree], histogramsNotReco['TTJet'][signal_region_tree], histogramsWrong['TTJet'][signal_region_tree], histogramsCorrect['TTJet'][signal_region_tree] ] histogram_lables = [ 'data', 'QCD', 'V+Jets', 'Single-Top', samples_latex['TTJet'] + ' - no solution', samples_latex['TTJet'] + ' - not SL', samples_latex['TTJet'] + ' - not reconstructible', samples_latex['TTJet'] + ' - wrong reco', samples_latex['TTJet'] + ' - correct', ] histogram_colors = [ 'black', 'yellow', 'green', 'magenta', 'black', 'burlywood', 'chartreuse', 'blue', 'red' ] histogram_properties = Histogram_properties() histogram_properties.name = name_prefix + b_tag_bin if category != 'central': histogram_properties.name += '_' + category histogram_properties.title = title histogram_properties.x_axis_title = x_axis_title histogram_properties.y_axis_title = y_axis_title histogram_properties.x_limits = x_limits histogram_properties.y_limits = y_limits histogram_properties.y_max_scale = y_max_scale histogram_properties.xerr = None # workaround for rootpy issue #638 histogram_properties.emptybins = True if b_tag_bin: histogram_properties.additional_text = channel_latex[ channel] + ', ' + b_tag_bins_latex[b_tag_bin] else: histogram_properties.additional_text = channel_latex[channel] histogram_properties.legend_location = legend_location histogram_properties.cms_logo_location = cms_logo_location histogram_properties.preliminary = preliminary histogram_properties.set_log_y = log_y histogram_properties.legend_color = legend_color if ratio_y_limits: histogram_properties.ratio_y_limits = ratio_y_limits if normalise_to_fit: histogram_properties.mc_error = get_normalisation_error(normalisation) histogram_properties.mc_errors_label = 'fit uncertainty' else: histogram_properties.mc_error = mc_uncertainty histogram_properties.mc_errors_label = 'MC unc.' # Actually draw histograms make_data_mc_comparison_plot( histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder=output_folder, show_ratio=False, normalise=normalise, ) histogram_properties.name += '_with_ratio' loc = histogram_properties.legend_location # adjust legend location as it is relative to canvas! histogram_properties.legend_location = (loc[0], loc[1] + 0.05) make_data_mc_comparison_plot( histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder=output_folder, show_ratio=True, normalise=normalise, )