def get_fit_inputs(template, variable, channel):

    inputs = {}
    for var_bin in variable_bins_ROOT[variable]:
        print var_bin
        histogram = template % var_bin
        histograms = get_histograms_from_files([histogram], histogram_files)
        for sample in [channel, 'TTJet', 'V+Jets', 'SingleTop']:
            n_bins = histograms[sample][histogram].GetNbinsX()
            error = Double(0)
            integral = histograms[sample][histogram].IntegralAndError(
                1, n_bins, error)
            if inputs.has_key(sample):
                inputs[sample].append((integral, error))
            else:
                inputs[sample] = [(integral, error)]

    inputs['QCD'] = []
    for data, ttjet, vjets, singletop in zip(inputs[channel], inputs['TTJet'],
                                             inputs['V+Jets'],
                                             inputs['SingleTop']):
        qcd = ufloat(data) - ufloat(ttjet) - ufloat(vjets) - ufloat(singletop)
        inputs['QCD'].append((qcd.nominal_value, qcd.std_dev))
    print inputs
    return inputs
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():
    global measurement_config, histogram_files
    global electron_fit_variables, muon_fit_variables, fit_variable_properties
    global b_tag_bin, category, histogram_files, variables
    global b_tag_bin_ctl
    
    title_template = '$%.1f$ fb$^{-1}$(%d TeV)'
    e_title = title_template % ( measurement_config.new_luminosity / 1000., measurement_config.centre_of_mass_energy )
    met_type = 'patType1CorrectedPFMet'
    for variable in variables:
        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 )
            make_folder_if_not_exists( save_path + 'qcd/' )
            make_folder_if_not_exists( save_path + 'vjets/' )
            inclusive_histograms = {}
            inclusive_fit_distribution = ''
            inclusive_qcd_distribution = ''
            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
                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 )
                histograms = get_histograms_from_files( [fit_variable_distribution, qcd_fit_variable_distribution], histogram_files )
                plot_fit_variable( histograms, fit_variable, variable, bin_range, fit_variable_distribution, qcd_fit_variable_distribution, e_title, save_path )
                # sum histograms for inclusive plots
                for sample, hist in histograms.iteritems():
                    inclusive_fit_distribution = fit_variable_distribution.replace( bin_range, "inclusive" )
                    inclusive_qcd_distribution = qcd_fit_variable_distribution.replace( bin_range, "inclusive" )
                    if not inclusive_histograms.has_key( sample ):
                        inclusive_histograms[sample] = {}
                        inclusive_histograms[sample][inclusive_fit_distribution] = hist[fit_variable_distribution].clone()
                        inclusive_histograms[sample][inclusive_qcd_distribution] = hist[qcd_fit_variable_distribution].clone() 
                    else:
                        inclusive_histograms[sample][inclusive_fit_distribution] += hist[fit_variable_distribution]   
                        inclusive_histograms[sample][inclusive_qcd_distribution] += hist[qcd_fit_variable_distribution]
                        
            plot_fit_variable( inclusive_histograms, fit_variable, variable,
                               'inclusive', inclusive_fit_distribution,
                               inclusive_qcd_distribution, e_title, save_path )
            
        compare_qcd_control_regions( variable, met_type, e_title )
        compare_vjets_btag_regions( variable, met_type, e_title )
        compare_vjets_templates( variable, met_type, e_title )
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 get_fit_inputs(template, variable, channel):
    
    inputs = {}
    for var_bin in variable_bins_ROOT[variable]:
        print var_bin
        histogram = template % var_bin
        histograms = get_histograms_from_files([histogram], histogram_files)
        for sample in [channel, 'TTJet', 'V+Jets', 'SingleTop']:
            n_bins = histograms[sample][histogram].GetNbinsX()
            error = Double(0)
            integral = histograms[sample][histogram].IntegralAndError(1, n_bins, error)
            if inputs.has_key(sample):
                inputs[sample].append((integral, error))
            else:
                inputs[sample] = [(integral, error)]
    
    inputs['QCD'] = []
    for data,ttjet, vjets, singletop in zip(inputs[channel], inputs['TTJet'], inputs['V+Jets'], inputs['SingleTop']):
        qcd = ufloat(data) - ufloat(ttjet) - ufloat(vjets) - ufloat(singletop)
        inputs['QCD'].append((qcd.nominal_value, qcd.std_dev))
    print inputs
    return inputs
Ejemplo n.º 6
0
        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),
    }

    control_region = 'QCDStudy/PFIsolation_controlRegion_0btag'

    histograms = get_histograms_from_files([control_region], histogram_files)
    prepare_histograms(histograms, rebin=rebin)

    nonQCDMC = histograms['TTJet'][control_region] + histograms['WJets'][
        control_region] + histograms['ZJets'][control_region] + histograms[
            'SingleTop'][control_region]
    make_control_region_data_mc_comparision(histograms,
                                            control_region,
                                            'PFIsolation_0btag',
                                            x_label='relative isolation',
                                            x_min=0,
                                            x_max=1.6,
                                            y_label='Events/0.1')

    make_control_region_comparison(histograms['data'][control_region],
                                   histograms['QCD'][control_region],
    histogram_files = {
                       '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': '/storage/TopQuarkGroup/results/histogramfiles/AN-11-265_V2/QCD_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(1959.75), ''),
            'SingleTop': path_to_files + 'SingleTop_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (str(lumi), pfmuon),
                       }

    control_region_1 = 'topReconstruction/backgroundShape/mttbar_3jets_conversions_withMETAndAsymJets_0btag'
    control_region_2 = 'topReconstruction/backgroundShape/mttbar_3jets_antiIsolated_withMETAndAsymJets_0btag'
    control_region_3 = 'topReconstruction/backgroundShape/mttbar_conversions_withMETAndAsymJets_0btag'
    control_region_4 = 'topReconstruction/backgroundShape/mttbar_antiIsolated_withMETAndAsymJets_0btag'
    
    histograms_to_read = [control_region_1, control_region_2, control_region_3, control_region_4]
    histograms = get_histograms_from_files(histograms_to_read, histogram_files)
    prepare_histograms(histograms, rebin=rebin)
    for _,histogram in histograms['QCD'].iteritems():
        histogram.Scale(5028./1959.75)
    
    make_control_region_data_mc_comparision(histograms, control_region_1, 'mttbar_3jets_conversions_withMETAndAsymJets_0btag')
    make_control_region_data_mc_comparision(histograms, control_region_2, 'mttbar_3jets_antiIsolated_withMETAndAsymJets_0btag')
    make_control_region_data_mc_comparision(histograms, control_region_3, 'mttbar_conversions_withMETAndAsymJets_0btag')
    make_control_region_data_mc_comparision(histograms, control_region_4, 'mttbar_antiIsolated_withMETAndAsymJets_0btag')

    make_control_region_comparison(histograms['data'][control_region_1],
                                   histograms['data'][control_region_2],
                                   'conversions',
                                   'non-isolated electrons',
                                   'mttbar_3jets_withMETAndAsymJets_0btag')
    make_control_region_comparison(histograms['data'][control_region_3],
#             'MET':get_fitted_normalisation('MET', 'muon'),
#             'HT':get_fitted_normalisation('HT', 'muon'),
#             'ST':get_fitted_normalisation('ST', 'muon'),
#             'MT':get_fitted_normalisation('MT', 'muon'),
#             'WPT':get_fitted_normalisation('WPT', 'muon')
#             }
    title_template = 'CMS Preliminary, $\mathcal{L} = %.1f$ fb$^{-1}$  at $\sqrt{s}$ = %d TeV \n %s'
    e_title = title_template % (measurement_config.new_luminosity/ 1000., 
                                measurement_config.centre_of_mass_energy, 
                                'e+jets, $\geq$4 jets')
    #bjet_invariant_mass
    #bjet invariant mass
    b_tag_bin = '4orMoreBtags'
    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=20, 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],
                          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)'
def do_shape_check(channel,
                   control_region_1,
                   control_region_2,
                   variable,
                   normalisation,
                   title,
                   x_title,
                   y_title,
                   x_limits,
                   y_limits,
                   name_region_1='conversions',
                   name_region_2='non-isolated electrons',
                   name_region_3='fit results',
                   rebin=1):
    global b_tag_bin
    # QCD shape comparison
    if channel == 'electron':
        histograms = get_histograms_from_files(
            [control_region_1, control_region_2], histogram_files)

        region_1 = histograms[channel][control_region_1].Clone(
        ) - histograms['TTJet'][control_region_1].Clone(
        ) - histograms['V+Jets'][control_region_1].Clone(
        ) - histograms['SingleTop'][control_region_1].Clone()
        region_2 = histograms[channel][control_region_2].Clone(
        ) - histograms['TTJet'][control_region_2].Clone(
        ) - histograms['V+Jets'][control_region_2].Clone(
        ) - histograms['SingleTop'][control_region_2].Clone()

        region_1.Rebin(rebin)
        region_2.Rebin(rebin)

        histogram_properties = Histogram_properties()
        histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_' + b_tag_bin
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[0]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = 'upper right'
        make_control_region_comparison(
            region_1,
            region_2,
            name_region_1=name_region_1,
            name_region_2=name_region_2,
            histogram_properties=histogram_properties,
            save_folder=output_folder)

        # QCD shape comparison to fit results
        histograms = get_histograms_from_files([control_region_1],
                                               histogram_files)

        region_1_tmp = histograms[channel][control_region_1].Clone(
        ) - histograms['TTJet'][control_region_1].Clone(
        ) - histograms['V+Jets'][control_region_1].Clone(
        ) - histograms['SingleTop'][control_region_1].Clone()
        region_1 = rebin_asymmetric(region_1_tmp, bin_edges_vis[variable])

        fit_results_QCD = normalisation[variable]['QCD']
        region_2 = value_error_tuplelist_to_hist(fit_results_QCD,
                                                 bin_edges_vis[variable])

        histogram_properties = Histogram_properties()
        histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_fits_with_conversions_' + b_tag_bin
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[1]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = 'upper right'
        make_control_region_comparison(
            region_1,
            region_2,
            name_region_1=name_region_1,
            name_region_2=name_region_3,
            histogram_properties=histogram_properties,
            save_folder=output_folder)

    histograms = get_histograms_from_files([control_region_2], histogram_files)

    region_1_tmp = histograms[channel][control_region_2].Clone(
    ) - histograms['TTJet'][control_region_2].Clone(
    ) - histograms['V+Jets'][control_region_2].Clone(
    ) - histograms['SingleTop'][control_region_2].Clone()
    region_1 = rebin_asymmetric(region_1_tmp, bin_edges_vis[variable])

    fit_results_QCD = normalisation[variable]['QCD']
    region_2 = value_error_tuplelist_to_hist(fit_results_QCD,
                                             bin_edges_vis[variable])

    histogram_properties = Histogram_properties()
    histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_fits_with_noniso_' + b_tag_bin
    histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
    histogram_properties.x_axis_title = x_title
    histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
    histogram_properties.x_limits = x_limits
    histogram_properties.y_limits = y_limits[1]
    histogram_properties.mc_error = 0.0
    histogram_properties.legend_location = 'upper right'
    make_control_region_comparison(region_1,
                                   region_2,
                                   name_region_1=name_region_2,
                                   name_region_2=name_region_3,
                                   histogram_properties=histogram_properties,
                                   save_folder=output_folder)
def main():
    global measurement_config, histogram_files
    global electron_fit_variables, muon_fit_variables, fit_variable_properties
    global b_tag_bin, category, histogram_files, variables
    global b_tag_bin_ctl

    title_template = '$%.1f$ fb$^{-1}$(%d TeV)'
    e_title = title_template % (measurement_config.new_luminosity / 1000.,
                                measurement_config.centre_of_mass_energy)
    met_type = 'patType1CorrectedPFMet'
    for variable in variables:
        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)
            make_folder_if_not_exists(save_path + 'qcd/')
            make_folder_if_not_exists(save_path + 'vjets/')
            inclusive_histograms = {}
            inclusive_fit_distribution = ''
            inclusive_qcd_distribution = ''
            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
                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)
                histograms = get_histograms_from_files(
                    [fit_variable_distribution, qcd_fit_variable_distribution],
                    histogram_files)
                plot_fit_variable(histograms, fit_variable, variable,
                                  bin_range, fit_variable_distribution,
                                  qcd_fit_variable_distribution, e_title,
                                  save_path)
                # sum histograms for inclusive plots
                for sample, hist in histograms.iteritems():
                    inclusive_fit_distribution = fit_variable_distribution.replace(
                        bin_range, "inclusive")
                    inclusive_qcd_distribution = qcd_fit_variable_distribution.replace(
                        bin_range, "inclusive")
                    if not inclusive_histograms.has_key(sample):
                        inclusive_histograms[sample] = {}
                        inclusive_histograms[sample][
                            inclusive_fit_distribution] = hist[
                                fit_variable_distribution].clone()
                        inclusive_histograms[sample][
                            inclusive_qcd_distribution] = hist[
                                qcd_fit_variable_distribution].clone()
                    else:
                        inclusive_histograms[sample][
                            inclusive_fit_distribution] += hist[
                                fit_variable_distribution]
                        inclusive_histograms[sample][
                            inclusive_qcd_distribution] += hist[
                                qcd_fit_variable_distribution]

            plot_fit_variable(inclusive_histograms, fit_variable, variable,
                              'inclusive', inclusive_fit_distribution,
                              inclusive_qcd_distribution, e_title, save_path)

        compare_qcd_control_regions(variable, met_type, e_title)
        compare_vjets_btag_regions(variable, met_type, e_title)
        compare_vjets_templates(variable, met_type, e_title)
        '/storage/TopQuarkGroup/results/histogramfiles/AN-11-265_V2/QCD_%spb_PFElectron_%sPF2PATJets_PFMET.root'
        % (str(1959.75), ''),
        'SingleTop':
        path_to_files + 'SingleTop_%spb_PFElectron_%sPF2PATJets_PFMET.root' %
        (str(lumi), pfmuon),
    }

    control_region_1 = 'topReconstruction/backgroundShape/mttbar_3jets_conversions_withMETAndAsymJets_0btag'
    control_region_2 = 'topReconstruction/backgroundShape/mttbar_3jets_antiIsolated_withMETAndAsymJets_0btag'
    control_region_3 = 'topReconstruction/backgroundShape/mttbar_conversions_withMETAndAsymJets_0btag'
    control_region_4 = 'topReconstruction/backgroundShape/mttbar_antiIsolated_withMETAndAsymJets_0btag'

    histograms_to_read = [
        control_region_1, control_region_2, control_region_3, control_region_4
    ]
    histograms = get_histograms_from_files(histograms_to_read, histogram_files)
    prepare_histograms(histograms, rebin=rebin)
    for _, histogram in histograms['QCD'].iteritems():
        histogram.Scale(5028. / 1959.75)

    make_control_region_data_mc_comparision(
        histograms, control_region_1,
        'mttbar_3jets_conversions_withMETAndAsymJets_0btag')
    make_control_region_data_mc_comparision(
        histograms, control_region_2,
        'mttbar_3jets_antiIsolated_withMETAndAsymJets_0btag')
    make_control_region_data_mc_comparision(
        histograms, control_region_3,
        'mttbar_conversions_withMETAndAsymJets_0btag')
    make_control_region_data_mc_comparision(
        histograms, control_region_4,
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)
        path_to_files +
        'DYJetsToLL_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' %
        (str(lumi), pfmuon, suffix),
        'QCD':
        path_to_files + 'QCD_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' %
        (str(lumi), pfmuon, suffix),
        'SingleTop':
        path_to_files + 'SingleTop_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' %
        (str(lumi), pfmuon, suffix)
    }

    electron_selection = 'EventCount/TTbarEplusJetsRefSelection'
    muon_selection = 'EventCount/TTbarMuPlusJetsRefSelection'

    cuts = cuts_electrons
    histograms = get_histograms_from_files([electron_selection],
                                           histogram_files)
    print '=' * 50
    printCutFlow(histograms, electron_selection, luminosity_scale)

    data = 'SingleMu'
    histogram_files[
        'data'] = path_to_files + '%s_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (
            data, str(lumi), pfmuon)
    histogram_files[
        'QCD'] = path_to_files + 'QCD_Muon_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (
            str(lumi), pfmuon, suffix)
    histograms = get_histograms_from_files([muon_selection], histogram_files)

    cuts = cuts_muons
    print '=' * 50
    printCutFlow(histograms, muon_selection, luminosity_scale)
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_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 do_shape_check(channel, control_region_1, control_region_2, variable, normalisation, title, x_title, y_title, x_limits, y_limits,
                   name_region_1='conversions' , name_region_2='non-isolated electrons', name_region_3='fit results', rebin=1):
    global b_tag_bin
    # QCD shape comparison
    if channel == 'electron':
        histograms = get_histograms_from_files([control_region_1, control_region_2], histogram_files)
        
        region_1 = histograms[channel][control_region_1].Clone() - histograms['TTJet'][control_region_1].Clone() - histograms['V+Jets'][control_region_1].Clone() - histograms['SingleTop'][control_region_1].Clone()
        region_2 = histograms[channel][control_region_2].Clone() - histograms['TTJet'][control_region_2].Clone() - histograms['V+Jets'][control_region_2].Clone() - histograms['SingleTop'][control_region_2].Clone()
        
        region_1.Rebin(rebin)
        region_2.Rebin(rebin)
        
        histogram_properties = Histogram_properties()
        histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_' + b_tag_bin
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[0]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = 'upper right'
        make_control_region_comparison(region_1, region_2,
                                       name_region_1=name_region_1, name_region_2=name_region_2,
                                       histogram_properties=histogram_properties, save_folder=output_folder)
        
        # QCD shape comparison to fit results
        histograms = get_histograms_from_files([control_region_1], histogram_files)
        
        region_1_tmp = histograms[channel][control_region_1].Clone() - histograms['TTJet'][control_region_1].Clone() - histograms['V+Jets'][control_region_1].Clone() - histograms['SingleTop'][control_region_1].Clone()
        region_1 = rebin_asymmetric(region_1_tmp, bin_edges_vis[variable])
        
        fit_results_QCD = normalisation[variable]['QCD']
        region_2 = value_error_tuplelist_to_hist(fit_results_QCD, bin_edges_vis[variable])
        
        histogram_properties = Histogram_properties()
        histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_fits_with_conversions_' + b_tag_bin
        histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
        histogram_properties.x_axis_title = x_title
        histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
        histogram_properties.x_limits = x_limits
        histogram_properties.y_limits = y_limits[1]
        histogram_properties.mc_error = 0.0
        histogram_properties.legend_location = 'upper right'
        make_control_region_comparison(region_1, region_2,
                                       name_region_1=name_region_1, name_region_2=name_region_3,
                                       histogram_properties=histogram_properties, save_folder=output_folder)
    
    histograms = get_histograms_from_files([control_region_2], histogram_files)
    
    region_1_tmp = histograms[channel][control_region_2].Clone() - histograms['TTJet'][control_region_2].Clone() - histograms['V+Jets'][control_region_2].Clone() - histograms['SingleTop'][control_region_2].Clone()
    region_1 = rebin_asymmetric(region_1_tmp, bin_edges_vis[variable])    
    
    fit_results_QCD = normalisation[variable]['QCD']
    region_2 = value_error_tuplelist_to_hist(fit_results_QCD, bin_edges_vis[variable])
    
    histogram_properties = Histogram_properties()
    histogram_properties.name = 'QCD_control_region_comparison_' + channel + '_' + variable + '_fits_with_noniso_' + b_tag_bin
    histogram_properties.title = title + ', ' + b_tag_bins_latex[b_tag_bin]
    histogram_properties.x_axis_title = x_title
    histogram_properties.y_axis_title = 'arbitrary units/(0.1)'
    histogram_properties.x_limits = x_limits
    histogram_properties.y_limits = y_limits[1]
    histogram_properties.mc_error = 0.0
    histogram_properties.legend_location = 'upper right'
    make_control_region_comparison(region_1, region_2,
                                   name_region_1=name_region_2, name_region_2=name_region_3,
                                   histogram_properties=histogram_properties, save_folder=output_folder)
    data = 'SingleElectron'
    pfmuon = 'PFMuon_'
    histogram_files = {
            'data' : path_to_files + '%s_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (data, str(lumi), pfmuon),
            'TTJet': path_to_files + 'TTJet_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix),
            'WJets': path_to_files + 'WJets_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix),
            'ZJets': path_to_files + 'DYJetsToLL_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix),
            'QCD': path_to_files + 'QCD_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix),
            'SingleTop': path_to_files + 'SingleTop_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix)
    }

    electron_selection = 'EventCount/TTbarEplusJetsRefSelection'
    muon_selection = 'EventCount/TTbarMuPlusJetsRefSelection'

    cuts = cuts_electrons
    histograms = get_histograms_from_files([electron_selection], histogram_files)
    print '='*50
    printCutFlow(histograms, electron_selection, luminosity_scale)

    data = 'SingleMu'
    histogram_files['data'] = path_to_files + '%s_%spb_PFElectron_%sPF2PATJets_PFMET.root' % (data, str(lumi), pfmuon)
    histogram_files['QCD'] = path_to_files + 'QCD_Muon_%spb_PFElectron_%sPF2PATJets_PFMET%s.root' % (str(lumi), pfmuon, suffix)
    histograms = get_histograms_from_files([muon_selection], histogram_files)

    cuts = cuts_muons
    print '='*50
    printCutFlow(histograms, muon_selection, luminosity_scale)