Beispiel #1
0
def test_get_max_y_graph():
    g = value_errors_tuplelist_to_graph(data_g, bin_edges)
    max_y = get_best_max_y([g])
    assert max_y == 3 + 1
Beispiel #2
0
def test_get_max_y_hist():
    h = value_error_tuplelist_to_hist(data_h, bin_edges)
    max_y = get_best_max_y([h])
    assert max_y == 3 + 1
def make_template_plots( histograms, category, channel ):
    global variable, output_folder
    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_templates/'
        make_folder_if_not_exists( path )
        for fit_variable in fit_variables:
            plotname = path + channel + '_' + fit_variable + '_template_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_ttjet = histograms[fit_variable][variable_bin]['TTJet']
            h_single_top = histograms[fit_variable][variable_bin]['SingleTop']
            h_VJets = histograms[fit_variable][variable_bin]['V+Jets']
            h_QCD = histograms[fit_variable][variable_bin]['QCD']
            
            h_ttjet.linecolor = 'red'
            h_single_top.linecolor = 'magenta'
            h_VJets.linecolor = 'green'
            h_QCD.linecolor = 'gray'
            h_VJets.linestyle = 'dashed'
            h_QCD.linestyle = 'dotted'  # currently not working
            # bug report: http://trac.sagemath.org/sage_trac/ticket/13834
            
            h_ttjet.linewidth = 5
            h_single_top.linewidth = 5
            h_VJets.linewidth = 5
            h_QCD.linewidth = 5
        
            plt.figure( figsize = ( 16, 16 ), dpi = 200, facecolor = 'white' )
            axes = plt.axes()
            axes.minorticks_on()
            
            plt.xlabel( fit_variables_latex[fit_variable], CMS.x_axis_title )
            plt.ylabel( 'normalised to unit area/(%s)' % get_unit_string(fit_variable), CMS.y_axis_title )
            plt.tick_params( **CMS.axis_label_major )
            plt.tick_params( **CMS.axis_label_minor )
    
            rplt.hist( h_ttjet, axes = axes, label = 'signal' )
            rplt.hist( h_single_top, axes = axes, label = 'Single Top' )
            
            if ( h_VJets.Integral() != 0 ):
                rplt.hist( h_VJets, axes = axes, label = 'V+Jets' )
            else:
                print "WARNING: in %s bin %s, %s category, %s channel, V+Jets template is empty: not plotting." % ( variable, variable_bin, category, channel )
            if ( h_QCD.Integral() != 0 ):
                rplt.hist( h_QCD, axes = axes, label = 'QCD' )
            else:
                print "WARNING: in %s bin %s, %s category, %s channel, QCD template is empty: not plotting." % ( variable, variable_bin, category, channel )
            y_max = get_best_max_y([h_ttjet, h_single_top, h_VJets, h_QCD])
            axes.set_ylim( [0, y_max * 1.1] )
            axes.set_xlim( measurement_config.fit_boundaries[fit_variable] )
            
            plt.legend( numpoints = 1, loc = 'upper right', prop = CMS.legend_properties )
            label, channel_label = get_cms_labels( channel )
            plt.title( label, CMS.title )
            # CMS text
            # note: fontweight/weight does not change anything as we use Latex text!!!
            plt.text(0.95, 0.95, r"\textbf{CMS}", transform=axes.transAxes, fontsize=42,
                     verticalalignment='top',horizontalalignment='right')
            # channel text
            axes.text(0.95, 0.90, r"\emph{%s}" %channel_label, transform=axes.transAxes, fontsize=40,
                      verticalalignment='top',horizontalalignment='right')

            plt.tight_layout()
        
            for output_format in output_formats:
                plt.savefig( plotname + '.' + output_format )
            
            plt.close()
            gc.collect()
def make_template_plots( histograms, category, channel ):
    global variable, output_folder, phase_space
    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_templates/'
        make_folder_if_not_exists( path )
        for fit_variable in fit_variables:
            plotname = path + channel + '_' + fit_variable + '_template_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_ttjet = histograms[fit_variable][variable_bin]['TTJet']
            h_single_top = histograms[fit_variable][variable_bin]['SingleTop']
            h_VJets = histograms[fit_variable][variable_bin]['V+Jets']
            h_QCD = histograms[fit_variable][variable_bin]['QCD']

            h_ttjet.linecolor = 'red'
            h_single_top.linecolor = 'magenta'
            h_VJets.linecolor = 'green'
            h_QCD.linecolor = 'gray'
            h_VJets.linestyle = 'dashed'
            h_QCD.linestyle = 'dotted'  # currently not working
            # bug report: http://trac.sagemath.org/sage_trac/ticket/13834

            h_ttjet.linewidth = 5
            h_single_top.linewidth = 5
            h_VJets.linewidth = 5
            h_QCD.linewidth = 5

            plt.figure( figsize = ( 16, 16 ), dpi = 200, facecolor = 'white' )
            axes = plt.axes()
            if not variable in ['NJets']:
                axes.minorticks_on()

            plt.xlabel( fit_variables_latex[fit_variable], CMS.x_axis_title )
            plt.ylabel( 'normalised to unit area/(%s)' % get_unit_string(fit_variable), CMS.y_axis_title )
            plt.tick_params( **CMS.axis_label_major )
            if not variable in ['NJets']:
                plt.tick_params( **CMS.axis_label_minor )

            rplt.hist( h_ttjet, axes = axes, label = 'signal' )
            rplt.hist( h_single_top, axes = axes, label = 'Single Top' )

            if ( h_VJets.Integral() != 0 ):
                rplt.hist( h_VJets, axes = axes, label = 'V+Jets' )
            else:
                print("WARNING: in %s bin %s, %s category, %s channel, V+Jets template is empty: not plotting." % ( variable, variable_bin, category, channel ))
            if ( h_QCD.Integral() != 0 ):
                rplt.hist( h_QCD, axes = axes, label = 'QCD' )
            else:
                print("WARNING: in %s bin %s, %s category, %s channel, QCD template is empty: not plotting." % ( variable, variable_bin, category, channel ))
            y_max = get_best_max_y([h_ttjet, h_single_top, h_VJets, h_QCD])
            axes.set_ylim( [0, y_max * 1.1] )
            axes.set_xlim( measurement_config.fit_boundaries[fit_variable] )

            plt.legend( numpoints = 1, loc = 'upper right', prop = CMS.legend_properties )
            label, channel_label = get_cms_labels( channel )
            plt.title( label, CMS.title )
            # CMS text
            # note: fontweight/weight does not change anything as we use Latex text!!!
            plt.text(0.95, 0.95, r"\textbf{CMS}", transform=axes.transAxes, fontsize=42,
                     verticalalignment='top',horizontalalignment='right')
            # channel text
            axes.text(0.95, 0.95, r"\emph{%s}" %channel_label, transform=axes.transAxes, fontsize=40,
                      verticalalignment='top',horizontalalignment='right')

            plt.tight_layout()

            for output_format in output_formats:
                plt.savefig( plotname + '.' + output_format )

            plt.close()
            gc.collect()
def test_get_max_y_graph():
    g = value_errors_tuplelist_to_graph(data_g, bin_edges)
    max_y = get_best_max_y([g])
    assert max_y == 3 + 1
def test_get_max_y_hist():
    h = value_error_tuplelist_to_hist(data_h, bin_edges)
    max_y = get_best_max_y([h])
    assert max_y == 3 + 1