def test_tau_closure(self):
     for channel in self.channels:
         for variable in self.variables:
             data = self.dict[channel][variable]["h_measured"]
             truth = hist_to_value_error_tuplelist(self.dict[channel][variable]["h_truth"])
             unfolded_result = hist_to_value_error_tuplelist(
                 self.dict[channel][variable]["tau_unfolding_object"].unfold(data)
             )
             # the difference between the truth and unfolded result should
             # be within the unfolding error
             for (value, error), (true_value, _) in zip(unfolded_result, truth):
                 self.assertAlmostEquals(value, true_value, delta=error)
 def test_tau_closure(self):
     for channel in self.channels:
         for variable in self.variables:
             data = self.dict[channel][variable]['h_measured']
             truth = hist_to_value_error_tuplelist(
                 self.dict[channel][variable]['h_truth'])
             unfolded_result = hist_to_value_error_tuplelist(
                 self.dict[channel][variable]
                 ['tau_unfolding_object'].unfold(data))
             # the difference between the truth and unfolded result should
             # be within the unfolding error
             for (value, error), (true_value,
                                  _) in zip(unfolded_result, truth):
                 self.assertAlmostEquals(value, true_value, delta=error)
    def __init__(self,
                 config,
                 measurement,
                 method=BACKGROUND_SUBTRACTION,
                 phase_space='FullPS'):
        self.config = config
        self.variable = measurement.variable
        self.category = measurement.name
        self.channel = measurement.channel
        self.method = method
        self.phase_space = phase_space
        self.measurement = measurement
        self.measurement.read()

        self.met_type = measurement.met_type
        self.fit_variables = ['M3']

        self.normalisation = {}
        self.initial_normalisation = {}
        self.templates = {}

        self.have_normalisation = False

        for sample, hist in self.measurement.histograms.items():
            h = deepcopy(hist)
            h_norm = h.integral()
            if h_norm > 0:
                h.Scale(1 / h.integral())
            self.templates[sample] = hist_to_value_error_tuplelist(h)
        self.auxiliary_info = {}
        self.auxiliary_info['norms'] = measurement.aux_info_norms
    def calculate_normalisation(self):
        '''
            1. get file names
            2. get histograms from files
            3. ???
            4. calculate normalisation
        '''
        if self.have_normalisation:
            return
        histograms = self.measurement.histograms

        for sample, hist in histograms.items():
            # TODO: this should be a list of bin-contents
            hist = fix_overflow(hist)
            histograms[sample] = hist
            self.initial_normalisation[sample] = hist_to_value_error_tuplelist(
                hist)
            self.normalisation[sample] = self.initial_normalisation[sample]

        self.background_subtraction(histograms)

        # next, let's round all numbers (they are event numbers after all
        for sample, values in self.normalisation.items():
            new_values = [(round(v, 1), round(e, 1)) for v, e in values]
            self.normalisation[sample] = new_values

        self.have_normalisation = True
    def calculate_normalisation(self):
        '''
            1. get file names
            2. get histograms from files
            3. ???
            4. calculate normalisation based on self.method
        '''
        if self.have_normalisation:
            return
        histograms = self.measurement.histograms

        for sample, hist in histograms.items():
            # TODO: this should be a list of bin-contents
            hist = fix_overflow(hist)
            histograms[sample] = hist
            self.initial_normalisation[
                sample] = hist_to_value_error_tuplelist(hist)
            if self.method == self.BACKGROUND_SUBTRACTION and sample != 'TTJet':
                self.normalisation[sample] = self.initial_normalisation[sample]

        if self.method == self.BACKGROUND_SUBTRACTION:
            self.background_subtraction(histograms)
        if self.method == self.SIMULTANEOUS_FIT:
            self.simultaneous_fit(histograms)

        # next, let's round all numbers (they are event numbers after all
        for sample, values in self.normalisation.items():
            new_values = [(round(v, 1), round(e, 1)) for v, e in values]
            self.normalisation[sample] = new_values

        self.have_normalisation = True
    def calculate_normalisation(self):
        '''
            1. get file names
            2. get histograms from files
            3. ???
            4. calculate normalisation
        '''
        if self.have_normalisation:
            return
        histograms = self.measurement.histograms

        for sample, hist in histograms.items():
            # TODO: this should be a list of bin-contents
            hist = fix_overflow(hist)
            histograms[sample] = hist
            self.initial_normalisation[sample] = hist_to_value_error_tuplelist(hist)
            self.normalisation[sample] = self.initial_normalisation[sample]

        self.background_subtraction(histograms)

        # next, let's round all numbers (they are event numbers after all
        for sample, values in self.normalisation.items():
            new_values = [(round(v, 1), round(e, 1)) for v, e in values]
            self.normalisation[sample] = new_values

        self.have_normalisation = True
def unfold_results( results, category, channel, tau_value, h_truth, h_measured, h_response, h_fakes, method, visiblePS ):
    global variable, path_to_DF, args

    edges = reco_bin_edges_full[variable]
    if visiblePS:
        edges = reco_bin_edges_vis[variable]

    h_data = value_error_tuplelist_to_hist( results, edges )

    # Rebin original TTJet_Measured in terms of final binning (h_data is later replaced with h_data_no_fakes)
    h_data_rebinned = h_data.rebinned(2)

    # Remove fakes before unfolding
    h_data_no_fakes = removeFakes( h_measured, h_fakes, h_data )

    # unfold
    unfolding = Unfolding( h_data_no_fakes, h_truth, h_measured, h_response, h_fakes, method = method, tau = tau_value )

    # turning off the unfolding errors for systematic samples
    if not category == 'central':
        unfoldCfg.error_treatment = 0
    else:
        unfoldCfg.error_treatment = args.error_treatment

    h_unfolded_data = unfolding.unfold()
    h_data_no_fakes = h_data_no_fakes.rebinned(2)
    covariance_matrix = None
    if category == 'central':
        # Return the covariance matrices (They have been normailsed)
        covariance_matrix, correlation_matrix = unfolding.get_covariance_matrix()

        # Write covariance matrices
        covariance_output_tempalte = '{path_to_DF}/central/covarianceMatrices/{cat}_{label}_{channel}.txt'
        # Unfolded number of events
        table_outfile=covariance_output_tempalte.format( path_to_DF=path_to_DF, channel = channel, label='Covariance', cat='Stat_unfoldedNormalisation' )
        create_covariance_matrix( covariance_matrix, table_outfile)
        table_outfile=covariance_output_tempalte.format( path_to_DF=path_to_DF, channel = channel, label='Correlation', cat='Stat_unfoldedNormalisation' )
        create_covariance_matrix( correlation_matrix, table_outfile )
        # # Normalised cross section
        # table_outfile=covariance_output_tempalte.format( path_to_DF=path_to_DF, channel = channel, label='Covariance', cat='Stat_normalisedXsection' )
        # create_covariance_matrix( norm_covariance_matrix, table_outfile)
        # table_outfile=covariance_output_tempalte.format( path_to_DF=path_to_DF, channel = channel, label='Correlation', cat='Stat_normalisedXsection' )
        # create_covariance_matrix( norm_correlation_matrix, table_outfile )

    del unfolding
    return hist_to_value_error_tuplelist( h_data_rebinned ), hist_to_value_error_tuplelist( h_unfolded_data ), hist_to_value_error_tuplelist( h_data_no_fakes ), covariance_matrix
Exemple #8
0
def unfold_results(results, category, channel, h_truth, h_measured, h_response,
                   method):
    global variable, path_to_JSON
    h_data = value_error_tuplelist_to_hist(results, bin_edges[variable])
    unfolding = Unfolding(h_truth, h_measured, h_response, method=method)

    #turning off the unfolding errors for systematic samples
    if category != 'central':
        unfoldCfg.Hreco = 0

    h_unfolded_data = unfolding.unfold(h_data)

    #export the D and SV distributions
    SVD_path = path_to_JSON + '/' + variable + '/unfolding_objects/' + channel + '/kv_' + str(
        unfoldCfg.SVD_k_value) + '/'
    make_folder_if_not_exists(SVD_path)
    if method == 'TSVDUnfold':
        SVDdist = TFile(
            SVD_path + method + '_SVDdistributions_' + category + '.root',
            'recreate')
        directory = SVDdist.mkdir('SVDdist')
        directory.cd()
        unfolding.unfoldObject.GetD().Write()
        unfolding.unfoldObject.GetSV().Write()
        #    unfolding.unfoldObject.GetUnfoldCovMatrix(data_covariance_matrix(h_data), unfoldCfg.SVD_n_toy).Write()
        SVDdist.Close()
    else:
        SVDdist = TFile(
            SVD_path + method + '_SVDdistributions_Hreco' +
            str(unfoldCfg.Hreco) + '_' + category + '.root', 'recreate')
        directory = SVDdist.mkdir('SVDdist')
        directory.cd()
        unfolding.unfoldObject.Impl().GetD().Write()
        unfolding.unfoldObject.Impl().GetSV().Write()
        h_truth.Write()
        h_measured.Write()
        h_response.Write()
        #    unfolding.unfoldObject.Impl().GetUnfoldCovMatrix(data_covariance_matrix(h_data), unfoldCfg.SVD_n_toy).Write()
        SVDdist.Close()

    #export the whole unfolding object if it doesn't exist
    if method == 'TSVDUnfold':
        unfolding_object_file_name = SVD_path + method + '_unfoldingObject_' + category + '.root'
    else:
        unfolding_object_file_name = SVD_path + method + '_unfoldingObject_Hreco' + str(
            unfoldCfg.Hreco) + '_' + category + '.root'
    if not os.path.isfile(unfolding_object_file_name):
        unfoldingObjectFile = TFile(unfolding_object_file_name, 'recreate')
        directory = unfoldingObjectFile.mkdir('unfoldingObject')
        directory.cd()
        if method == 'TSVDUnfold':
            unfolding.unfoldObject.Write()
        else:
            unfolding.unfoldObject.Impl().Write()
        unfoldingObjectFile.Close()

    del unfolding
    return hist_to_value_error_tuplelist(h_unfolded_data)
def main():
    args, input_values_sets, json_input_files = parse_options()

    results = {}
    clear_old_df('tables/taufinding/')

    for input_values, json_file in zip( input_values_sets, json_input_files ):
        # print '\nProcessing', json_file
        # Initialise the TauFinding class
        regularisation_settings = TauFinding( input_values )
        # Set additional elemtents
        regularisation_settings.taus_to_test = get_tau_list(args.n_ticks_in_log)

        variable = regularisation_settings.variable
        channel = regularisation_settings.channel
        com = regularisation_settings.centre_of_mass_energy
        if 'muon' not in channel : continue
        print 'Variable = {0}, channel = {1}, sqrt(s) = {2}'.format(variable, channel, com)

        if args.run_measured_as_data:
            regularisation_settings.taus_to_test = [0]
            regularisation_settings.h_data = regularisation_settings.h_measured
            df_chi2 = get_chi2s_of_tau_range(regularisation_settings, args)

            if args.perform_varied_measured_unfolding_test:
                h_data = hist_to_value_error_tuplelist(regularisation_settings.h_data)
                h_data_varied = [(return_rnd_Poisson(val),return_rnd_Poisson(err)) for val, err in h_data ]
                h_data_varied = value_error_tuplelist_to_hist(h_data_varied, reco_bin_edges_vis[variable])
                regularisation_settings.h_data = h_data_varied
                df_chi2_smeared = get_chi2s_of_tau_range(regularisation_settings, args, unfold_test=True)
                print df_chi2_smeared
            # No point in trying to find best tau if it is given as 0...
            sys.exit()
        
        # Find the corresponding Chi2 and write to file
        df_chi2 = get_chi2s_of_tau_range(regularisation_settings, args)
        print df_chi2

    # Have the dataframes now - albeit read to a file
    # Read in each one corresponding to their channel
    # Find the best tau and print to screen
    for channel in ['electron', 'muon', 'combined']:
        chi2_cut = 0.005
        path = regularisation_settings.outpath+'tbl_'+channel+'_tauscan.txt'
        df_chi2 = get_df_from_file(path)
        if df_chi2 is None: continue
        print '\n', "1 - P(Chi2|NDF)", '\n', df_chi2, '\n'

        # cutoff to be changed to 0.001 when able to
        best_taus = interpolate_tau(chi2_cut, df_chi2)
        chi2_to_plots(df_chi2, regularisation_settings, chi2_cut, channel)
        print_results_to_screen(best_taus, channel)
    return
def unfold_results(results, category, channel, h_truth, h_measured, h_response, method):
    global variable, path_to_JSON
    h_data = value_error_tuplelist_to_hist(results, bin_edges[variable])
    unfolding = Unfolding(h_truth, h_measured, h_response, method=method)
    
    #turning off the unfolding errors for systematic samples
    if category != 'central':
        unfoldCfg.Hreco = 0
    
    h_unfolded_data = unfolding.unfold(h_data)
    
    #export the D and SV distributions
    SVD_path = path_to_JSON + '/' + variable + '/unfolding_objects/' + channel + '/kv_' + str(unfoldCfg.SVD_k_value) + '/'
    make_folder_if_not_exists(SVD_path)
    if method == 'TSVDUnfold':
        SVDdist = TFile(SVD_path + method + '_SVDdistributions_' + category + '.root', 'recreate')
        directory = SVDdist.mkdir('SVDdist')
        directory.cd()
        unfolding.unfoldObject.GetD().Write()
        unfolding.unfoldObject.GetSV().Write()
        #    unfolding.unfoldObject.GetUnfoldCovMatrix(data_covariance_matrix(h_data), unfoldCfg.SVD_n_toy).Write()
        SVDdist.Close()
    else:
        SVDdist = TFile(SVD_path + method + '_SVDdistributions_Hreco' + str(unfoldCfg.Hreco) + '_' + category + '.root', 'recreate')
        directory = SVDdist.mkdir('SVDdist')
        directory.cd()
        unfolding.unfoldObject.Impl().GetD().Write()
        unfolding.unfoldObject.Impl().GetSV().Write()
        h_truth.Write()
        h_measured.Write()
        h_response.Write()
        #    unfolding.unfoldObject.Impl().GetUnfoldCovMatrix(data_covariance_matrix(h_data), unfoldCfg.SVD_n_toy).Write()
        SVDdist.Close()

    #export the whole unfolding object if it doesn't exist
    if method == 'TSVDUnfold':
        unfolding_object_file_name = SVD_path + method + '_unfoldingObject_' + category + '.root'
    else:
        unfolding_object_file_name = SVD_path + method + '_unfoldingObject_Hreco' + str(unfoldCfg.Hreco) + '_' + category + '.root'
    if not os.path.isfile(unfolding_object_file_name):
        unfoldingObjectFile = TFile(unfolding_object_file_name, 'recreate')
        directory = unfoldingObjectFile.mkdir('unfoldingObject')
        directory.cd()
        if method == 'TSVDUnfold':
            unfolding.unfoldObject.Write()
        else:
            unfolding.unfoldObject.Impl().Write()
        unfoldingObjectFile.Close()
    
    del unfolding
    return hist_to_value_error_tuplelist(h_unfolded_data)
def unfold_results( results, category, channel, tau_value, h_truth, h_measured, h_response, h_fakes, method, visiblePS ):
    global variable, path_to_JSON, options
    edges = reco_bin_edges_full[variable]
    if visiblePS:
        edges = reco_bin_edges_vis[variable]
    h_data = value_error_tuplelist_to_hist( results, edges )

    # Remove fakes before unfolding
    h_data = removeFakes( h_measured, h_fakes, h_data )

    unfolding = Unfolding( h_data, h_truth, h_measured, h_response, h_fakes, method = method, tau = tau_value )

    # turning off the unfolding errors for systematic samples
    if not category == 'central':
        unfoldCfg.error_treatment = 0
    else:
        unfoldCfg.error_treatment = options.error_treatment

    h_unfolded_data = unfolding.unfold()
    print "h_response bin edges : ", h_response
    print "h_unfolded_data bin edges : ", h_unfolded_data

    del unfolding
    return hist_to_value_error_tuplelist( h_unfolded_data ), hist_to_value_error_tuplelist( h_data )
Exemple #12
0
def get_unfolded_normalisation(TTJet_fit_results, category, channel):
    global variable, met_type, path_to_JSON
    h_truth, h_measured, h_response = get_unfold_histogram_tuple(
        file_for_unfolding, variable, channel, met_type)
    MADGRAPH_results = hist_to_value_error_tuplelist(h_truth)
    POWHEG_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_powheg, variable, channel,
                                   met_type)[0])
    MCATNLO_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_mcatnlo, variable, channel,
                                   met_type)[0])

    matchingdown_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_matchingdown, variable, channel,
                                   met_type)[0])
    matchingup_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_matchingup, variable, channel,
                                   met_type)[0])
    scaledown_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_scaledown, variable, channel,
                                   met_type)[0])
    scaleup_results = hist_to_value_error_tuplelist(
        get_unfold_histogram_tuple(file_for_scaleup, variable, channel,
                                   met_type)[0])

    TTJet_fit_results_unfolded = unfold_results(
        TTJet_fit_results, category, channel, h_truth, h_measured, h_response,
        'RooUnfoldSvd'
        #                                                'TSVDUnfold'
    )

    normalisation_unfolded = {
        'TTJet_measured': TTJet_fit_results,
        'TTJet_unfolded': TTJet_fit_results_unfolded,
        'MADGRAPH': MADGRAPH_results,
        #other generators
        'POWHEG': POWHEG_results,
        'MCATNLO': MCATNLO_results,
        #systematics
        'matchingdown': matchingdown_results,
        'matchingup': matchingup_results,
        'scaledown': scaledown_results,
        'scaleup': scaleup_results
    }
    write_data_to_JSON(
        normalisation_unfolded, path_to_JSON + '/' + variable +
        '/xsection_measurement_results' + '/kv' + str(unfoldCfg.SVD_k_value) +
        '/' + category + '/normalisation_' + channel + '_' + met_type + '.txt')

    return normalisation_unfolded
 def __background_subtraction(self, histograms):
     '''
     Subtracts the backgrounds from data to give amount of ttbar in data.
     Also adds all backgrounds to normalisation output
     '''
     ttjet_hist = clean_control_region(
         histograms,
         subtract=['QCD', 'V+Jets', 'SingleTop']
     )
     self.normalisation['TTJet']     = hist_to_value_error_tuplelist(ttjet_hist)
     self.normalisation['data']      = hist_to_value_error_tuplelist(histograms['data'])
     self.normalisation['TTJet_MC']   = hist_to_value_error_tuplelist(histograms['TTBar'])
     self.normalisation['SingleTop'] = hist_to_value_error_tuplelist(histograms['SingleTop'])
     self.normalisation['V+Jets']    = hist_to_value_error_tuplelist(histograms['V+Jets'])
     self.normalisation['QCD']       = hist_to_value_error_tuplelist(histograms['QCD'])
     return
def get_unfolded_normalisation(TTJet_fit_results, category, channel):
    global variable, met_type, path_to_JSON
    h_truth, h_measured, h_response = get_unfold_histogram_tuple(file_for_unfolding, variable, channel, met_type)
    MADGRAPH_results = hist_to_value_error_tuplelist(h_truth)
    POWHEG_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_powheg, variable, channel, met_type)[0])
    MCATNLO_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_mcatnlo, variable, channel, met_type)[0])
    
    matchingdown_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_matchingdown, variable, channel, met_type)[0])
    matchingup_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_matchingup, variable, channel, met_type)[0])
    scaledown_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_scaledown, variable, channel, met_type)[0])
    scaleup_results = hist_to_value_error_tuplelist(get_unfold_histogram_tuple(file_for_scaleup, variable, channel, met_type)[0])
    
    TTJet_fit_results_unfolded = unfold_results(TTJet_fit_results,
                                                category,
                                                channel,
                                                h_truth,
                                                h_measured,
                                                h_response,
                                                'RooUnfoldSvd'
#                                                'TSVDUnfold'
                                                )
        
    normalisation_unfolded = {
                              'TTJet_measured' : TTJet_fit_results,
                              'TTJet_unfolded' : TTJet_fit_results_unfolded,
                              'MADGRAPH': MADGRAPH_results,
                              #other generators
                              'POWHEG': POWHEG_results,
                              'MCATNLO': MCATNLO_results,
                              #systematics
                              'matchingdown': matchingdown_results,
                              'matchingup': matchingup_results,
                              'scaledown': scaledown_results,
                              'scaleup': scaleup_results
                              }
    write_data_to_JSON(normalisation_unfolded, path_to_JSON + '/' + variable + '/xsection_measurement_results' + '/kv' + str(unfoldCfg.SVD_k_value) + '/' + category + '/normalisation_' + channel + '_' + met_type + '.txt')
    
    return normalisation_unfolded
 def __background_subtraction(self, histograms):
     '''
     Subtracts the backgrounds from data to give amount of ttbar in data.
     Also adds all backgrounds to normalisation output
     '''
     ttjet_hist = clean_control_region(
         histograms, subtract=['QCD', 'V+Jets', 'SingleTop'])
     self.normalisation['TTJet'] = hist_to_value_error_tuplelist(ttjet_hist)
     self.normalisation['data'] = hist_to_value_error_tuplelist(
         histograms['data'])
     self.normalisation['TTJet_MC'] = hist_to_value_error_tuplelist(
         histograms['TTBar'])
     self.normalisation['SingleTop'] = hist_to_value_error_tuplelist(
         histograms['SingleTop'])
     self.normalisation['V+Jets'] = hist_to_value_error_tuplelist(
         histograms['V+Jets'])
     self.normalisation['QCD'] = hist_to_value_error_tuplelist(
         histograms['QCD'])
     return
def get_unfolded_normalisation( TTJet_normalisation_results, category, channel, tau_value, visiblePS ):
    global com, luminosity, ttbar_xsection, method, variable, path_to_DF
    global unfolding_files

    files_for_systematics = {

        'TTJets_massdown'        	 :  unfolding_files['file_for_massdown'],
        'TTJets_massup'          	 :  unfolding_files['file_for_massup'],
       
        'TTJets_factorisationdown'	 :  unfolding_files['file_for_factorisationdown'],
        'TTJets_factorisationup'   	 :  unfolding_files['file_for_factorisationup'],
        'TTJets_renormalisationdown' :  unfolding_files['file_for_renormalisationdown'],
        'TTJets_renormalisationup'   :  unfolding_files['file_for_renormalisationup'],
        'TTJets_combineddown'     	 :  unfolding_files['file_for_combineddown'],
        'TTJets_combinedup'          :  unfolding_files['file_for_combinedup'],
        'TTJets_alphaSdown'			 :  unfolding_files['file_for_alphaSdown'],
        'TTJets_alphaSup'   	     :  unfolding_files['file_for_alphaSup'],

        'TTJets_matchingdown'        :  unfolding_files['file_for_matchingdown'],
        'TTJets_matchingup'          :  unfolding_files['file_for_matchingup'],

        'TTJets_isrdown'             :  unfolding_files['file_for_isrdown'],
        'TTJets_isrup'               :  unfolding_files['file_for_isrup'],
        # 'TTJets_fsrdown'             :  unfolding_files['file_for_fsrdown'],
        'TTJets_fsrup'               :  unfolding_files['file_for_fsrup'],
        'TTJets_uedown'              :  unfolding_files['file_for_uedown'],
        'TTJets_ueup'                :  unfolding_files['file_for_ueup'],

        'TTJets_topPt'               :  unfolding_files['file_for_topPtSystematic'],

        'JES_down'                   :  unfolding_files['file_for_jesdown'],
        'JES_up'                     :  unfolding_files['file_for_jesup'],

        'JER_down'                   :  unfolding_files['file_for_jerdown'],
        'JER_up'                     :  unfolding_files['file_for_jerup'],

        'BJet_up'                    :  unfolding_files['file_for_bjetup'],
        'BJet_down'                  :  unfolding_files['file_for_bjetdown'],

        'LightJet_up'                :  unfolding_files['file_for_lightjetup'],
        'LightJet_down'              :  unfolding_files['file_for_lightjetdown'],

        'TTJets_hadronisation'       :  unfolding_files['file_for_powheg_herwig'],

        'ElectronEnUp'               :  unfolding_files['file_for_ElectronEnUp'],
        'ElectronEnDown'             :  unfolding_files['file_for_ElectronEnDown'],
        'MuonEnUp'                   :  unfolding_files['file_for_MuonEnUp'],
        'MuonEnDown'                 :  unfolding_files['file_for_MuonEnDown'],
        'TauEnUp'                    :  unfolding_files['file_for_TauEnUp'],
        'TauEnDown'                  :  unfolding_files['file_for_TauEnDown'],
        'UnclusteredEnUp'            :  unfolding_files['file_for_UnclusteredEnUp'],
        'UnclusteredEnDown'          :  unfolding_files['file_for_UnclusteredEnDown'],

        'Muon_up'                    :  unfolding_files['file_for_LeptonUp'],
        'Muon_down'                  :  unfolding_files['file_for_LeptonDown'],
        'Electron_up'                :  unfolding_files['file_for_LeptonUp'],
        'Electron_down'              :  unfolding_files['file_for_LeptonDown'],

        'PileUp_up'                  :  unfolding_files['file_for_PUUp'],
        'PileUp_down'                :  unfolding_files['file_for_PUDown'],

        'Top_Pt_reweight'            :  unfolding_files['file_for_ptreweight'],

    }

    h_truth, h_measured, h_response, h_fakes = None, None, None, None

    # Uncertainties by changing the response matrix
    if category in files_for_systematics :
        print 'Doing category',category,'by changing response matrix'
        h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( 
            inputfile = files_for_systematics[category],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )
    # PDF Uncertainties
    elif category in pdf_uncertainties:
        print 'Doing category',category,'by changing response matrix'
        h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['files_for_pdfs'][category],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )
    # Central and systematics where you just change input MC
    else:
        h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_unfolding'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )

    # Unfold current normalisation measurements  
    TTJet_normalisation_results, TTJet_normalisation_results_unfolded, TTJet_normalisation_results_withoutFakes, covariance_matrix = unfold_results( 
        TTJet_normalisation_results,
        category,
        channel,
        tau_value,
        h_truth,
        h_measured,
        h_response,
        h_fakes,
        method,
        visiblePS,
    )

    # Store TTJet yields after background subtraction, after background subtraction without fakes and after Unfolding
    normalisation_unfolded = {
        'TTJet_measured'                : TTJet_normalisation_results,
        'TTJet_measured_withoutFakes'   : TTJet_normalisation_results_withoutFakes,
        'TTJet_unfolded'                : TTJet_normalisation_results_unfolded,
    }

    # Return truth of different generators for comparison to data in 04
    if category == 'central':
        h_truth_massdown, _, _, _ = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_massdown'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )
        h_truth_massup, _, _, _ = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_massup'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )
        # h_truth_fsrdown, _, _, _ = get_unfold_histogram_tuple( 
        #     inputfile = unfolding_files['file_for_fsrdown'],
        #     variable = variable,
        #     channel = channel,
        #     centre_of_mass = com,
        #     ttbar_xsection = ttbar_xsection,
        #     luminosity = luminosity,
        #     load_fakes = True,
        #     visiblePS = visiblePS,
        # )
        h_truth_fsrup, _, _, _ = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_fsrup'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )
        h_truth_isrdown, _, _, _ = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_isrdown'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )
        h_truth_isrup, _, _, _ = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_isrup'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )
        h_truth_uedown, _, _, _ = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_uedown'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )
        h_truth_ueup, _, _, _ = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_ueup'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )

        h_truth_powhegPythia8, _, _, _ = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_powhegPythia8'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )
        # h_truth_amcatnlo, _, _, _ = get_unfold_histogram_tuple( 
        #     inputfile = unfolding_files['file_for_amcatnlo'],
        #     variable = variable,
        #     channel = channel,
        #     centre_of_mass = com,
        #     ttbar_xsection = ttbar_xsection,
        #     luminosity = luminosity,
        #     load_fakes = True,
        #     visiblePS = visiblePS,
        # )
        # h_truth_madgraphMLM, _, _, _ = get_unfold_histogram_tuple( 
        #     inputfile = unfolding_files['file_for_madgraphMLM'],
        #     variable = variable,
        #     channel = channel,
        #     centre_of_mass = com,
        #     ttbar_xsection = ttbar_xsection,
        #     luminosity = luminosity,
        #     load_fakes = True,
        #     visiblePS = visiblePS,
        # )
        h_truth_powheg_herwig, _, _, _ = get_unfold_histogram_tuple( 
            inputfile = unfolding_files['file_for_powheg_herwig'],
            variable = variable,
            channel = channel,
            centre_of_mass = com,
            ttbar_xsection = ttbar_xsection,
            luminosity = luminosity,
            load_fakes = True,
            visiblePS = visiblePS,
        )

    
        normalisation_unfolded['powhegPythia8'] = hist_to_value_error_tuplelist( h_truth_powhegPythia8 )
        # normalisation_unfolded['amcatnlo']      = hist_to_value_error_tuplelist( h_truth_madgraphMLM )
        # normalisation_unfolded['madgraphMLM']   = hist_to_value_error_tuplelist( h_truth_amcatnlo )
        normalisation_unfolded['powhegHerwig']  = hist_to_value_error_tuplelist( h_truth_powheg_herwig )

        normalisation_unfolded['massdown']      = hist_to_value_error_tuplelist( h_truth_massdown )
        normalisation_unfolded['massup']        = hist_to_value_error_tuplelist( h_truth_massup )
        normalisation_unfolded['isrdown']       = hist_to_value_error_tuplelist( h_truth_isrdown )
        normalisation_unfolded['isrup']         = hist_to_value_error_tuplelist( h_truth_isrup )
        # normalisation_unfolded['fsrdown']       = hist_to_value_error_tuplelist( h_truth_fsrdown )
        normalisation_unfolded['fsrup']         = hist_to_value_error_tuplelist( h_truth_fsrup )
        normalisation_unfolded['uedown']        = hist_to_value_error_tuplelist( h_truth_uedown )
        normalisation_unfolded['ueup']          = hist_to_value_error_tuplelist( h_truth_ueup )

    # Write all normalisations in unfolded binning scheme to dataframes
    file_template = '{path_to_DF}/{category}/unfolded_normalisation_{channel}_{method}.txt'
    write_02(normalisation_unfolded, file_template, path_to_DF, category, channel, method)
    return normalisation_unfolded, covariance_matrix
def check_multiple_data_multiple_unfolding(
        input_file, method, channel, variable,
        responseMatrix,
        n_toy_data, output_folder,
        tau_value=-1
        ):
    '''
        Loops through a n_toy_data of pseudo data, 
        unfolds the pseudo data and compares it to the MC truth
    '''
    # same unfolding input, different data
    get_folder = input_file.Get
    pulls = []
    add_pull = pulls.append
    histograms = []
    add_histograms = histograms.append

    print('Reading toy MC')
    start1 = time()
    data_range = range(0, n_toy_data)
    for nth_toy_data in range(0, n_toy_data + 1):  # read all of them (easier)
        if nth_toy_data in data_range:
            tpl = '{channel}/{variable}/toy_{nth}'
            folder_mc = tpl.format(channel=channel, variable=variable,
                                   nth=nth_toy_data+1)
            folder_mc = get_folder(folder_mc)
            add_histograms(get_measured_histogram(folder_mc))
        else:
            add_histograms(0)
    print('Done reading toy MC in', time() - start1, 's')


    # Get truth and measured histograms
    h_truth = get_truth_histogram( get_folder('{channel}/{variable}/Original'.format(channel=channel, variable=variable) ) )
    h_measured = get_measured_histogram( get_folder('{channel}/{variable}/Original'.format(channel=channel, variable=variable) ) )

    # Set response matrix
    h_response = responseMatrix


    # Make sure the pseudo data to be unfolded has the same integral as the response matrix
    measured_from_response = asrootpy( h_response.ProjectionX('px',1) )
    truth_from_response = asrootpy( h_response.ProjectionY() )
    truthScale = truth_from_response.Integral() / h_truth.Integral()
    h_truth.Scale( truthScale )
    h_measured.Scale( truthScale )

    for nth_toy_data in data_range:
        if nth_toy_data % 100 == 0 :
            print(
                'Doing data no', nth_toy_data)
        h_data = histograms[nth_toy_data]

        h_data.Scale( truthScale )

        unfolding_obj = Unfolding(
            h_data,
            h_truth, h_data, h_response, method=method, k_value=-1,
            tau=tau_value)
        unfold, get_pull = unfolding_obj.unfold, unfolding_obj.pull
        reset = unfolding_obj.Reset


        unfold()
        pull = get_pull()
        diff = unfolding_obj.unfolded_data - h_truth
        diff_tuple = hist_to_value_error_tuplelist(diff)

        truth_tuple = hist_to_value_error_tuplelist(unfolding_obj.truth)

        bias = []
        sumBias2 = 0
        for d, t in zip(diff_tuple, truth_tuple):
            b = d[0] / t[0]
            bias.append(b)

        unfolded = unfolding_obj.unfolded_data
        unfolded_tuple = hist_to_value_error_tuplelist(unfolded)

        all_data = {'unfolded': unfolded_tuple,
                    'difference': diff_tuple,
                    'truth': truth_tuple,
                    'bias':bias,
                    'pull': pull,
                    'nth_toy_data': nth_toy_data
                    }

        add_pull(all_data)
        reset()

    output_file_name = save_pulls(pulls, method,
                                channel, tau_value, output_folder)

    return output_file_name
 def background_subtraction(self, histograms):
     ttjet_hist = clean_control_region(
         histograms, subtract=['QCD', 'V+Jets', 'SingleTop'])
     self.normalisation['TTJet'] = hist_to_value_error_tuplelist(ttjet_hist)
def check_multiple_data_multiple_unfolding(input_file,
                                           method,
                                           channel,
                                           variable,
                                           responseMatrix,
                                           n_toy_data,
                                           output_folder,
                                           tau_value=-1):
    '''
        Loops through a n_toy_data of pseudo data, 
        unfolds the pseudo data and compares it to the MC truth
    '''
    # same unfolding input, different data
    get_folder = input_file.Get
    pulls = []
    add_pull = pulls.append
    histograms = []
    add_histograms = histograms.append
    truth_histograms = []
    dirs = None
    for path, dir, objects in input_file.walk(maxdepth=0):
        dirs = dir

    for dir in dirs:
        print('Reading toy MC')
        start1 = time()
        data_range = range(0, n_toy_data)
        for nth_toy_data in range(0,
                                  n_toy_data + 1):  # read all of them (easier)
            if nth_toy_data in data_range:
                tpl = '{dir}/{channel}/{variable}/toy_{nth}'
                folder_mc = tpl.format(dir=dir,
                                       channel=channel,
                                       variable=variable,
                                       nth=nth_toy_data + 1)
                folder_mc = get_folder(folder_mc)
                add_histograms(get_measured_histogram(folder_mc))
                truth_histograms.append(get_truth_histogram(folder_mc))
            else:
                add_histograms(0)
        print('Done reading toy MC in', time() - start1, 's')

        # Get truth and measured histograms
        h_truth = get_truth_histogram(
            get_folder('{dir}/{channel}/{variable}/Original'.format(
                dir=dir, channel=channel, variable=variable)))
        h_measured = get_measured_histogram(
            get_folder('{dir}/{channel}/{variable}/Original'.format(
                dir=dir, channel=channel, variable=variable)))

        # Set response matrix
        h_response = generate_toy_MC_from_2Ddistribution(responseMatrix)

        # Make sure the response matrix has the same normalisatio as the pseudo data to be unfolded
        truthScale = h_truth.integral(overflow=True) / h_response.integral(
            overflow=True)
        h_response.Scale(truthScale)
        # measured_from_response = asrootpy( h_response.ProjectionX('px',1) )
        # truth_from_response = asrootpy( h_response.ProjectionY() )

        for nth_toy_data in data_range:
            if nth_toy_data % 100 == 0:
                print('Doing data no', nth_toy_data)
            h_data = histograms[nth_toy_data]
            # h_truth = truth_histograms[nth_toy_data]

            unfolding_obj = Unfolding(h_data,
                                      h_truth,
                                      h_data,
                                      h_response,
                                      method=method,
                                      tau=tau_value)
            unfold, get_pull = unfolding_obj.unfold, unfolding_obj.pull
            reset = unfolding_obj.Reset

            unfold()
            # print ('Measured :',list(h_data.y()))
            # print ('Unfolded :',list( unfolding_obj.unfolded_data.y() ))
            pull = get_pull()
            # print ('Pull :',pull)
            diff = unfolding_obj.unfolded_data - h_truth
            # print ('Diff :',list(diff.y()))
            diff_tuple = hist_to_value_error_tuplelist(diff)

            truth_tuple = hist_to_value_error_tuplelist(unfolding_obj.truth)

            bias = []
            sumBias2 = 0
            for d, t in zip(diff_tuple, truth_tuple):
                b = d[0] / t[0]
                bias.append(b)

            unfolded = unfolding_obj.unfolded_data
            unfolded_tuple = hist_to_value_error_tuplelist(unfolded)

            all_data = {
                'unfolded': unfolded_tuple,
                'difference': diff_tuple,
                'truth': truth_tuple,
                'bias': bias,
                'pull': pull,
                'nth_toy_data': nth_toy_data
            }

            add_pull(all_data)
            reset()

    output_file_name = save_pulls(pulls, method, channel, tau_value,
                                  output_folder)

    return output_file_name
 def background_subtraction(self, histograms):
     ttjet_hist = clean_control_region(histograms,
                                       subtract=['QCD', 'V+Jets', 'SingleTop'])
     self.normalisation[
         'TTJet'] = hist_to_value_error_tuplelist(ttjet_hist)
def get_unfolded_normalisation( TTJet_fit_results, category, channel, tau_value, visiblePS ):
    global centre_of_mass, luminosity, ttbar_xsection, method
    global variable, met_type, path_to_JSON, file_for_unfolding, file_for_powheg_pythia, file_for_powheg_herwig, file_for_ptreweight, files_for_pdfs
    global file_for_powhegPythia8, file_for_madgraphMLM, file_for_amcatnlo, file_for_amcatnlo_herwig
    # global file_for_matchingdown, file_for_matchingup
    global file_for_scaledown, file_for_scaleup
    global file_for_massdown, file_for_massup
    global ttbar_generator_systematics, ttbar_theory_systematics, pdf_uncertainties
    global use_ptreweight

    files_for_systematics = {
                             ttbar_theory_systematic_prefix + 'scaledown'       	:  file_for_scaledown,
                             ttbar_theory_systematic_prefix + 'scaleup'         	:  file_for_scaleup,
                             ttbar_theory_systematic_prefix + 'massdown'        	:  file_for_massdown,
                             ttbar_theory_systematic_prefix + 'massup'          	:  file_for_massup,
                             
                             ttbar_theory_systematic_prefix + 'factorisationdown'	:  file_for_factorisationdown,
                             ttbar_theory_systematic_prefix + 'factorisationup'   	:  file_for_factorisationup,
                             ttbar_theory_systematic_prefix + 'renormalisationdown'	:  file_for_renormalisationdown,
                             ttbar_theory_systematic_prefix + 'renormalisationup'  	:  file_for_renormalisationup,
                             ttbar_theory_systematic_prefix + 'combineddown'     	:  file_for_combineddown,
                             ttbar_theory_systematic_prefix + 'combinedup'          :  file_for_combinedup,
                             ttbar_theory_systematic_prefix + 'alphaSdown'			:  file_for_alphaSdown,
                             ttbar_theory_systematic_prefix + 'alphaSup'   			:  file_for_alphaSup,

                             'JES_down'        :  file_for_jesdown,
                             'JES_up'        :  file_for_jesup,

                             'JER_down'        :  file_for_jerdown,
                             'JER_up'        :  file_for_jerup,

                             'BJet_up'        :  file_for_bjetup,
                             'BJet_down'        :  file_for_bjetdown,

                             'LightJet_up'        :  file_for_lightjetup,
                             'LightJet_down'        :  file_for_lightjetdown,

                             ttbar_theory_systematic_prefix + 'hadronisation'   :  file_for_powheg_herwig,
                             ttbar_theory_systematic_prefix + 'NLOgenerator'   :  file_for_amcatnlo,

                             'ElectronEnUp' : file_for_ElectronEnUp,
                             'ElectronEnDown' : file_for_ElectronEnDown,
                             'MuonEnUp' : file_for_MuonEnUp,
                             'MuonEnDown' : file_for_MuonEnDown,
                             'TauEnUp' : file_for_TauEnUp,
                             'TauEnDown' : file_for_TauEnDown,
                             'UnclusteredEnUp' : file_for_UnclusteredEnUp,
                             'UnclusteredEnDown' : file_for_UnclusteredEnDown,

                             'Muon_up' : file_for_LeptonUp,
                             'Muon_down' : file_for_LeptonDown,
                             'Electron_up' : file_for_LeptonUp,
                             'Electron_down' : file_for_LeptonDown,

                             'PileUp_up' : file_for_PUUp,
                             'PileUp_down' : file_for_PUDown,
                             }

    h_truth, h_measured, h_response, h_fakes = None, None, None, None
    # Systematics where you change the response matrix
    if category in files_for_systematics :
        print 'Doing category',category,'by changing response matrix'
        h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( inputfile = files_for_systematics[category],
                                                                              variable = variable,
                                                                              channel = channel,
                                                                              met_type = met_type,
                                                                              centre_of_mass = centre_of_mass,
                                                                              ttbar_xsection = ttbar_xsection,
                                                                              luminosity = luminosity,
                                                                              load_fakes = True,
                                                                              visiblePS = visiblePS,
                                                                              )
    elif category in pdf_uncertainties:
        h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( inputfile = files_for_pdfs[category],
                                                                              variable = variable,
                                                                              channel = channel,
                                                                              met_type = met_type,
                                                                              centre_of_mass = centre_of_mass,
                                                                              ttbar_xsection = ttbar_xsection,
                                                                              luminosity = luminosity,
                                                                              load_fakes = True,
                                                                              visiblePS = visiblePS,
                                                                              )
    # Central and systematics where you just change input MC
    else:
        h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( inputfile = file_for_unfolding,
                                                                              variable = variable,
                                                                              channel = channel,
                                                                              met_type = met_type,
                                                                              centre_of_mass = centre_of_mass,
                                                                              ttbar_xsection = ttbar_xsection,
                                                                              luminosity = luminosity,
                                                                              load_fakes = True,
                                                                              visiblePS = visiblePS,
                                                                              )

#     central_results = hist_to_value_error_tuplelist( h_truth )
    TTJet_fit_results_unfolded, TTJet_fit_results_withoutFakes = unfold_results( TTJet_fit_results,
                                                category,
                                                channel,
                                                tau_value,
                                                h_truth,
                                                h_measured,
                                                h_response,
                                                h_fakes,
                                                method,
                                                visiblePS,
                                                )
    normalisation_unfolded = {
                      'TTJet_measured' : TTJet_fit_results,
                      'TTJet_measured_withoutFakes' : TTJet_fit_results_withoutFakes,
                      'TTJet_unfolded' : TTJet_fit_results_unfolded
                      }

    #
    # THESE ARE FOR GETTING THE HISTOGRAMS FOR COMPARING WITH UNFOLDED DATA
    #

    if category == 'central':
        h_truth_scaledown, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_scaledown,
                                                    variable = variable,
                                                    channel = channel,
                                                    met_type = met_type,
                                                    centre_of_mass = centre_of_mass,
                                                    ttbar_xsection = ttbar_xsection,
                                                    luminosity = luminosity,
                                                    load_fakes = True,
                                                    visiblePS = visiblePS,
                                                    )
        h_truth_scaleup, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_scaleup,
                                                    variable = variable,
                                                    channel = channel,
                                                    met_type = met_type,
                                                    centre_of_mass = centre_of_mass,
                                                    ttbar_xsection = ttbar_xsection,
                                                    luminosity = luminosity,
                                                    load_fakes = True,
                                                    visiblePS = visiblePS,
                                                    )

        h_truth_massdown, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_massdown,
                                                    variable = variable,
                                                    channel = channel,
                                                    met_type = met_type,
                                                    centre_of_mass = centre_of_mass,
                                                    ttbar_xsection = ttbar_xsection,
                                                    luminosity = luminosity,
                                                    load_fakes = True,
                                                    visiblePS = visiblePS,
                                                    )
        h_truth_massup, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_massup,
                                                    variable = variable,
                                                    channel = channel,
                                                    met_type = met_type,
                                                    centre_of_mass = centre_of_mass,
                                                    ttbar_xsection = ttbar_xsection,
                                                    luminosity = luminosity,
                                                    load_fakes = True,
                                                    visiblePS = visiblePS,
                                                    )

        h_truth_powhegPythia8, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_powhegPythia8,
                                                variable = variable,
                                                channel = channel,
                                                met_type = met_type,
                                                centre_of_mass = centre_of_mass,
                                                ttbar_xsection = ttbar_xsection,
                                                luminosity = luminosity,
                                                load_fakes = True,
                                                visiblePS = visiblePS,
                                                )

        h_truth_amcatnlo, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_amcatnlo,
                                                variable = variable,
                                                channel = channel,
                                                met_type = met_type,
                                                centre_of_mass = centre_of_mass,
                                                ttbar_xsection = ttbar_xsection,
                                                luminosity = luminosity,
                                                load_fakes = True,
                                                visiblePS = visiblePS,
                                                )

        h_truth_madgraphMLM, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_madgraphMLM,
                                                    variable = variable,
                                                    channel = channel,
                                                    met_type = met_type,
                                                    centre_of_mass = centre_of_mass,
                                                    ttbar_xsection = ttbar_xsection,
                                                    luminosity = luminosity,
                                                    load_fakes = True,
                                                    visiblePS = visiblePS,
                                                    )

        h_truth_powheg_herwig, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_powheg_herwig,
                                                    variable = variable,
                                                    channel = channel,
                                                    met_type = met_type,
                                                    centre_of_mass = centre_of_mass,
                                                    ttbar_xsection = ttbar_xsection,
                                                    luminosity = luminosity,
                                                    load_fakes = True,
                                                    visiblePS = visiblePS,
                                                    )

        # h_truth_amcatnlo_herwig, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_amcatnlo_herwig,
        #                                             variable = variable,
        #                                             channel = channel,
        #                                             met_type = met_type,
        #                                             centre_of_mass = centre_of_mass,
        #                                             ttbar_xsection = ttbar_xsection,
        #                                             luminosity = luminosity,
        #                                             load_fakes = True,
        #                                             visiblePS = visiblePS,
        #                                             )
    
        # MADGRAPH_ptreweight_results = hist_to_value_error_tuplelist( h_truth_ptreweight )
        # POWHEG_PYTHIA_results = hist_to_value_error_tuplelist( h_truth_POWHEG_PYTHIA )
        # MCATNLO_results = None
        powhegPythia8_results = hist_to_value_error_tuplelist( h_truth_powhegPythia8 )
        madgraphMLM_results = hist_to_value_error_tuplelist( h_truth_madgraphMLM )
        amcatnloPythia8_results = hist_to_value_error_tuplelist( h_truth_amcatnlo )
        powheg_herwig_results = hist_to_value_error_tuplelist( h_truth_powheg_herwig )
        # amcatnlo_herwig_results = hist_to_value_error_tuplelist( h_truth_amcatnlo_herwig )

        # matchingdown_results = hist_to_value_error_tuplelist( h_truth_matchingdown )
        # matchingup_results = hist_to_value_error_tuplelist( h_truth_matchingup )
        scaledown_results = hist_to_value_error_tuplelist( h_truth_scaledown )
        scaleup_results = hist_to_value_error_tuplelist( h_truth_scaleup )
        massdown_results = hist_to_value_error_tuplelist( h_truth_massdown )
        massup_results = hist_to_value_error_tuplelist( h_truth_massup )

        normalisation_unfolded['powhegPythia8'] =  powhegPythia8_results
        normalisation_unfolded['amcatnlo'] =  amcatnloPythia8_results
        normalisation_unfolded['madgraphMLM'] = madgraphMLM_results
        normalisation_unfolded['powhegHerwig'] =  powheg_herwig_results
        # normalisation_unfolded['amcatnloHerwig'] =  amcatnlo_herwig_results

        normalisation_unfolded['scaledown'] =  scaledown_results
        normalisation_unfolded['scaleup'] =  scaleup_results
        normalisation_unfolded['massdown'] =  massdown_results
        normalisation_unfolded['massup'] =  massup_results


    return normalisation_unfolded
    '/storage/TopQuarkGroup/mc/8TeV/NoSkimUnfolding/v10/TTJets_scaledown_TuneZ2star_8TeV-madgraph-tauola/unfolding_v10_Summer12_DR53X-PU_S10_START53_V7A-v1_NoSkim/TTJets-scaledown_nTuple_53X_mc_merged_001.root'
)

test_file = root_open('test_unfolded.root')

test1 = test_file.Get(channel + '_MET__TTJet__TTJetsMatching__plus')
test2 = test_file.Get(channel + '_MET__TTJet__TTJetsMatching__minus')
test3 = test_file.Get(channel + '_MET__TTJet__TTJetsScale__plus')
test4 = test_file.Get(channel + '_MET__TTJet__TTJetsScale__minus')
test1.Sumw2()
test2.Sumw2()
test3.Sumw2()
test4.Sumw2()

folder = 'unfolding_MET_analyser_' + channel + '_channel_patMETsPFlow'
ref1 = hist_to_value_error_tuplelist(
    unfolding_file1.Get(folder + '/truth_AsymBins'))
ref2 = hist_to_value_error_tuplelist(
    unfolding_file2.Get(folder + '/truth_AsymBins'))
ref3 = hist_to_value_error_tuplelist(
    unfolding_file3.Get(folder + '/truth_AsymBins'))
ref4 = hist_to_value_error_tuplelist(
    unfolding_file4.Get(folder + '/truth_AsymBins'))
ref1 = value_error_tuplelist_to_hist(ref1, bin_edges['MET'])
ref2 = value_error_tuplelist_to_hist(ref2, bin_edges['MET'])
ref3 = value_error_tuplelist_to_hist(ref3, bin_edges['MET'])
ref4 = value_error_tuplelist_to_hist(ref4, bin_edges['MET'])

normalise([test1, test2, test3, test4, ref1, ref2, ref3, ref4])

draw_pair(test1, ref1, 'matching_up')
draw_pair(test2, ref2, 'matching_down')
def calculate_xsection( nEventsHistogram, variable ):
    resultsAsTuple = hist_to_value_error_tuplelist( nEventsHistogram )
    normalised_xsection, _, _, _ = calculate_normalised_xsection( resultsAsTuple, bin_widths_visiblePS[variable], False )

    return value_error_tuplelist_to_hist(normalised_xsection, bin_edges_vis[variable])
Exemple #24
0
def main():
    args, input_values_sets, json_input_files = parse_options()

    results = {}
    clear_old_df('tables/taufinding/')

    for input_values, json_file in zip( input_values_sets, json_input_files ):
        if 'combined' in json_file: continue

        # Initialise the TauFinding class
        regularisation_settings = TauFinding( input_values )
        variable = regularisation_settings.variable
        channel = regularisation_settings.channel
        com = regularisation_settings.centre_of_mass_energy

        # Specific channel or variable
        if args.ch:
            if args.ch not in channel: continue
        if args.var:
            if args.var not in variable: continue

        print 'Running for:'
        print 'Variable = {0}, channel = {1}, sqrt(s) = {2}'.format(variable, channel, com)

        # Set additional elements
        regularisation_settings.taus_to_test = get_tau_values(args.n_tau_in_log)
        isTauCalculator = True

        # Specific unfolding tests go here
        if args.specific_tau is not None:
            regularisation_settings.taus_to_test = [args.specific_tau]
            df_chi2_specific_tau = get_chi2(regularisation_settings, args)
            isTauCalculator = False

        if args.run_measured_as_data:
            regularisation_settings.taus_to_test = [0]
            regularisation_settings.h_data = regularisation_settings.h_measured
            df_chi2_measured = get_chi2(regularisation_settings, args)
            isTauCalculator = False

        if args.run_smeared_measured_as_data:
            regularisation_settings.taus_to_test = [0]
            regularisation_settings.h_data = regularisation_settings.h_measured
            h_data = hist_to_value_error_tuplelist(regularisation_settings.h_data)
            h_data_varied = [(return_rnd_Poisson(val),return_rnd_Poisson(err)) for val, err in h_data ]
            h_data_varied = value_error_tuplelist_to_hist(h_data_varied, reco_bin_edges_vis[variable])
            regularisation_settings.h_data = h_data_varied
            df_chi2_smeared = get_chi2(regularisation_settings, args, smearing_test=True)
            isTauCalculator = False
        
        # Dont need to calculate chi2 for given tau tests
        if not isTauCalculator: continue

        # Find Chi2 for each tau and write to file
        df_chi2 = get_chi2(regularisation_settings, args)

    # Dont need to calculate tau for given tests
    if not isTauCalculator: sys.exit()

    # Have the dataframes now - albeit read to a file
    # Read in each one corresponding to their channel
    # Find the best tau and print to screen
    for channel in ['electron', 'muon', 'combined']:
        chi2_cut = 0.005
        path = regularisation_settings.outpath+'tbl_'+channel+'_tauscan.txt'
        df_chi2 = get_df_from_file(path)
        if df_chi2 is None: continue
        print '\n', "1 - P(Chi2|NDF)", '\n', df_chi2, '\n'

        # cutoff to be changed to 0.001 when able to
        best_taus = interpolate_tau(chi2_cut, df_chi2)
        chi2_to_plots(args, df_chi2, regularisation_settings, chi2_cut, channel)
        print_results_to_screen(best_taus, channel)
    return
def calculate_xsection( nEventsHistogram, variable ):
    resultsAsTuple = hist_to_value_error_tuplelist( nEventsHistogram )
    normalised_xsection = calculate_normalised_xsection( resultsAsTuple, bin_widths_visiblePS[variable], False )
    return value_error_tuplelist_to_hist(normalised_xsection, bin_edges_vis[variable])
unfolding_file3 = root_open('/storage/TopQuarkGroup/mc/8TeV/NoSkimUnfolding/v10/TTJets_scaleup_TuneZ2star_8TeV-madgraph-tauola/unfolding_v10_Summer12_DR53X-PU_S10_START53_V7A-v1_NoSkim/TTJets-scaleup_nTuple_53X_mc_merged_001.root')
unfolding_file4 = root_open('/storage/TopQuarkGroup/mc/8TeV/NoSkimUnfolding/v10/TTJets_scaledown_TuneZ2star_8TeV-madgraph-tauola/unfolding_v10_Summer12_DR53X-PU_S10_START53_V7A-v1_NoSkim/TTJets-scaledown_nTuple_53X_mc_merged_001.root')

test_file = root_open('test_unfolded.root')

test1 = test_file.Get(channel + '_MET__TTJet__TTJetsMatching__plus')
test2 = test_file.Get(channel + '_MET__TTJet__TTJetsMatching__minus')
test3 = test_file.Get(channel + '_MET__TTJet__TTJetsScale__plus')
test4 = test_file.Get(channel + '_MET__TTJet__TTJetsScale__minus')
test1.Sumw2()
test2.Sumw2()
test3.Sumw2()
test4.Sumw2()

folder = 'unfolding_MET_analyser_' + channel + '_channel_patMETsPFlow'
ref1 = hist_to_value_error_tuplelist(unfolding_file1.Get(folder + '/truth_AsymBins'))
ref2 = hist_to_value_error_tuplelist(unfolding_file2.Get(folder + '/truth_AsymBins'))
ref3 = hist_to_value_error_tuplelist(unfolding_file3.Get(folder + '/truth_AsymBins'))
ref4 = hist_to_value_error_tuplelist(unfolding_file4.Get(folder + '/truth_AsymBins'))
ref1 = value_error_tuplelist_to_hist(ref1, bin_edges['MET'])
ref2 = value_error_tuplelist_to_hist(ref2, bin_edges['MET'])
ref3 = value_error_tuplelist_to_hist(ref3, bin_edges['MET'])
ref4 = value_error_tuplelist_to_hist(ref4, bin_edges['MET'])

normalise([test1, test2, test3, test4, ref1, ref2, ref3, ref4])

draw_pair(test1, ref1, 'matching_up')
draw_pair(test2, ref2, 'matching_down')
draw_pair(test3, ref3, 'scale_up')
draw_pair(test4, ref4, 'scale_down')