Ejemplo n.º 1
0
 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_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,
                                )
Ejemplo n.º 3
0
    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 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,
    )