def main():
    config = XSectionConfig(13)
#     method = 'RooUnfoldSvd'
    method = 'RooUnfoldBayes'
    file_for_unfolding = File(config.unfolding_central, 'read')
    for channel in ['electron', 'muon', 'combined']:
        for variable in bin_edges.keys():
            tau_value = get_tau_value(config, channel, variable)
            h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple(
                inputfile=file_for_unfolding,
                variable=variable,
                channel=channel,
                met_type=config.met_type,
                centre_of_mass=config.centre_of_mass_energy,
                ttbar_xsection=config.ttbar_xsection,
                luminosity=config.luminosity,
                load_fakes=False,
                visiblePS=False,
            )
            unfolding = Unfolding(
                h_truth, h_measured, h_response, h_fakes,
                method=method, k_value=-1, tau=tau_value)

            unfolded_data = unfolding.closureTest()
            plot_closure(h_truth, unfolded_data, variable, channel,
                         config.centre_of_mass_energy, method)
def main():
    config = XSectionConfig(13)
    #     method = 'RooUnfoldSvd'
    method = 'RooUnfoldBayes'
    file_for_unfolding = File(config.unfolding_central, 'read')
    for channel in ['electron', 'muon', 'combined']:
        for variable in bin_edges.keys():
            tau_value = get_tau_value(config, channel, variable)
            h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple(
                inputfile=file_for_unfolding,
                variable=variable,
                channel=channel,
                met_type=config.met_type,
                centre_of_mass=config.centre_of_mass_energy,
                ttbar_xsection=config.ttbar_xsection,
                luminosity=config.luminosity,
                load_fakes=False,
                visiblePS=False,
            )
            unfolding = Unfolding(h_truth,
                                  h_measured,
                                  h_response,
                                  h_fakes,
                                  method=method,
                                  k_value=-1,
                                  tau=tau_value)

            unfolded_data = unfolding.closureTest()
            plot_closure(h_truth, unfolded_data, variable, channel,
                         config.centre_of_mass_energy, method)
    def setUp(self):
        # load histograms
        self.input_file = File("data/unfolding_merged_asymmetric.root")
        self.k_value = 3
        self.unfold_method = "RooUnfoldSvd"
        self.met_type = "patType1CorrectedPFMet"
        self.variables = ["MET", "WPT", "MT", "ST", "HT"]
        self.channels = ["electron", "muon", "combined"]
        self.dict = {}
        for channel in self.channels:
            self.dict[channel] = {}
            for variable in self.variables:
                self.dict[variable] = {}
                h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple(
                    inputfile=self.input_file, variable=variable, channel=channel, met_type=self.met_type
                )

                unfolding_object = Unfolding(
                    h_truth, h_measured, h_response, k_value=self.k_value, method=self.unfold_method
                )

                self.dict[channel][variable] = {
                    "h_truth": h_truth,
                    "h_measured": h_measured,
                    "h_response": h_response,
                    "unfolding_object": unfolding_object,
                }
    def setUp( self ):
        # load histograms
        self.input_file = File('test/data/unfolding_merged_asymmetric.root')
        self.k_value = 3
        self.unfold_method = 'RooUnfoldSvd'
        self.met_type = 'patType1CorrectedPFMet'
        self.variables = ['MET', 'WPT', 'MT' , 'ST', 'HT']
        self.channels = ['electron', 'muon', 'combined']
        self.dict = {}
        for channel in self.channels:
            self.dict[channel] = {}
            for variable in self.variables:
                self.dict[variable] = {}
                h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple(
                                                            inputfile = self.input_file,
                                                            variable = variable,
                                                            channel = channel,
                                                            met_type = self.met_type)

                unfolding_object = Unfolding( h_truth,
                                       h_measured,
                                       h_response,
                                       k_value = self.k_value,
                                       method = self.unfold_method
                                       )

                self.dict[channel][variable] = {'h_truth' : h_truth,
                                                'h_measured' : h_measured,
                                                'h_response' : h_response,
                                                'unfolding_object' : unfolding_object
                                                }
Example #5
0
def main():
    config = XSectionConfig(13)
    #     method = 'RooUnfoldSvd'
    method = "RooUnfoldBayes"
    file_for_data = File(config.unfolding_powheg_herwig, "read")
    file_for_unfolding = File(config.unfolding_madgraphMLM, "read")
    for channel in ["electron", "muon", "combined"]:
        for variable in config.variables:
            tau_value = get_tau_value(config, channel, variable)
            h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple(
                inputfile=file_for_unfolding,
                variable=variable,
                channel=channel,
                met_type=config.met_type,
                centre_of_mass=config.centre_of_mass_energy,
                ttbar_xsection=config.ttbar_xsection,
                luminosity=config.luminosity,
                load_fakes=False,
                visiblePS=False,
            )
            h_data_model, h_data, _, _ = get_unfold_histogram_tuple(
                inputfile=file_for_data,
                variable=variable,
                channel=channel,
                met_type=config.met_type,
                centre_of_mass=config.centre_of_mass_energy,
                ttbar_xsection=config.ttbar_xsection,
                luminosity=config.luminosity,
                load_fakes=False,
                visiblePS=False,
            )

            unfolding = Unfolding(h_truth, h_measured, h_response, h_fakes, method=method, k_value=-1, tau=tau_value)

            unfolded_data = unfolding.unfold(h_data)
            plot_bias(h_truth, h_data_model, unfolded_data, variable, channel, config.centre_of_mass_energy, method)
Example #6
0
    def __set_unfolding_histograms__( self ):
        # at the moment only one file is supported for the unfolding input
        files = set( [self.truth['file'],
                     self.gen_vs_reco['file'],
                     self.measured['file']]
                    )
        if len( files ) > 1:
            print "Currently not supported to have different files for truth, gen_vs_reco and measured"
            sys.exit()
            
        input_file = files.pop()

        visiblePS = False
        if self.phaseSpace == 'VisiblePS':
            visiblePS = True

        t, m, r, f = get_unfold_histogram_tuple( File(input_file),
                                              self.variable,
                                              self.channel,
                                              centre_of_mass = self.centre_of_mass_energy,
                                              ttbar_xsection=self.measurement_config.ttbar_xsection,
                                              luminosity=self.measurement_config.luminosity,
                                              load_fakes = True,
                                              visiblePS = visiblePS
                                            )
        self.h_truth = asrootpy ( t )
        self.h_response = asrootpy ( r )
        self.h_measured = asrootpy ( m )
        self.h_fakes = asrootpy ( f )
        
        data_file = self.data['file']
        if data_file.endswith('.root'):
            self.h_data = get_histogram_from_file(self.data['histogram'], self.data['file'])
        elif data_file.endswith('.json') or data_file.endswith('.txt'):
            data_key = self.data['histogram']
            # assume configured bin edges
            edges = []
            edges = reco_bin_edges_vis[self.variable]

            json_input = read_data_from_JSON(data_file)

            if data_key == "": # JSON file == histogram
                self.h_data = value_error_tuplelist_to_hist(json_input, edges)
            else:
                self.h_data = value_error_tuplelist_to_hist(json_input[data_key], edges)
        else:
            print 'Unkown file extension', data_file.split('.')[-1]
def create_toy_mc(input_file, sample, output_folder, n_toy, centre_of_mass, ttbar_xsection):
    from tools.file_utilities import make_folder_if_not_exists
    from tools.toy_mc import generate_toy_MC_from_distribution, generate_toy_MC_from_2Ddistribution
    from tools.Unfolding import get_unfold_histogram_tuple
    make_folder_if_not_exists(output_folder)
    input_file_hists = File(input_file)
    output_file_name = get_output_file_name(output_folder, sample, n_toy, centre_of_mass)
    variable_bins = bin_edges_vis.copy()
    with root_open(output_file_name, 'recreate') as f_out:
        for channel in ['combined']:
            for variable in variable_bins:
                output_dir = f_out.mkdir(channel + '/' + variable, recurse=True)
                cd = output_dir.cd
                mkdir = output_dir.mkdir
                h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple(input_file_hists,
                                                                        variable,
                                                                        channel,
                                                                        centre_of_mass = centre_of_mass,
                                                                        ttbar_xsection = ttbar_xsection,
                                                                        visiblePS = True,
                                                                        load_fakes=False)

                cd()

                mkdir('Original')
                cd ('Original')
                h_truth.Write('truth')
                h_measured.Write('measured')
                h_response.Write('response')

                for i in range(1, n_toy+1):
                    toy_id = 'toy_{0}'.format(i)
                    mkdir(toy_id)
                    cd(toy_id)
                    # create histograms
                    # add tuples (truth, measured, response) of histograms
                    truth = generate_toy_MC_from_distribution(h_truth)
                    measured = generate_toy_MC_from_distribution(h_measured)
                    response = generate_toy_MC_from_2Ddistribution(h_response)

                    truth.SetName('truth')
                    measured.SetName('measured')
                    response.SetName('response')

                    truth.Write()
                    measured.Write()
                    response.Write()
    def __set_unfolding_histograms__(self):
        # at the moment only one file is supported for the unfolding input
        files = set([
            self.truth['file'], self.gen_vs_reco['file'], self.measured['file']
        ])
        if len(files) > 1:
            print "Currently not supported to have different files for truth, gen_vs_reco and measured"
            sys.exit()

        input_file = files.pop()
        t, m, r, _ = get_unfold_histogram_tuple(
            File(input_file),
            self.variable,
            self.channel,
            centre_of_mass=self.centre_of_mass_energy,
            ttbar_xsection=self.measurement_config.ttbar_xsection,
            luminosity=self.measurement_config.luminosity,
        )
        self.h_truth = t
        self.h_response = r
        self.h_measured = m

        data_file = self.data['file']
        if data_file.endswith('.root'):
            self.h_data = get_histogram_from_file(self.data['histogram'],
                                                  self.data['file'])
        elif data_file.endswith('.json') or data_file.endswith('.txt'):
            data_key = self.data['histogram']
            # assume configured bin edges
            edges = []
            if self.phaseSpace == 'FullPS':
                edges = bin_edges[self.variable]
            elif self.phaseSpace == 'VisiblePS':
                edges = bin_edges_vis[self.variable]
            json_input = read_data_from_JSON(data_file)
            if data_key == "":  # JSON file == histogram
                self.h_data = value_error_tuplelist_to_hist(json_input, edges)
            else:
                self.h_data = value_error_tuplelist_to_hist(
                    json_input[data_key], edges)
        else:
            print 'Unkown file extension', data_file.split('.')[-1]
    def setUp( self ):
        # load histograms
        self.input_file = File('tests/data/unfolding_merged_asymmetric.root')
        self.k_value = 3
        self.unfold_method = 'RooUnfoldSvd'
        self.met_type = 'patType1CorrectedPFMet'
        self.variables = ['MET', 'WPT', 'MT' , 'ST', 'HT']
        self.channels = ['electron', 'muon', 'combined']
        self.dict = {}
        for channel in self.channels:
            self.dict[channel] = {}
            for variable in self.variables:
                self.dict[variable] = {}
                h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple(
                                                            inputfile = self.input_file,
                                                            variable = variable,
                                                            channel = channel,
                                                            met_type = self.met_type)

                unfolding_object = Unfolding( h_truth,
                                       h_measured,
                                       h_response,
                                       k_value = self.k_value,
                                       method = self.unfold_method
                                       )
                
                tau_unfolding_object = Unfolding( h_truth,
                                                  h_measured,
                                                  h_response,
                                                  tau=100,
                                                  k_value= -1,
                                                  method='RooUnfoldSvd')

                self.dict[channel][variable] = {'h_truth' : h_truth,
                                                'h_measured' : h_measured,
                                                'h_response' : h_response,
                                                'unfolding_object' : unfolding_object,
                                                'tau_unfolding_object': tau_unfolding_object,
                                                }
    for channel in ['electron', 'muon']:
        taus_from_global_correlaton[channel] = {}
        taus_from_L_shape[channel] = {}

        k_values = {}
        k_values_crosscheck = {}

        for variable in variables:
            if variable is 'MT': continue
            print 'Doing variable', variable, 'in', channel, 'channel'
        
            h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( 
                                inputfile = input_file,
                                variable = variable,
                                channel = channel,
                                met_type = met_type,
                                centre_of_mass = centre_of_mass,
                                ttbar_xsection = ttbar_xsection,
                                luminosity = luminosity,
                                load_fakes = load_fakes)
            # print 'h_fakes = ', h_fakes
            
            h_data = None
            if test == 'data':
                h_data = get_fit_results_histogram( data_path = path_to_JSON,
                               centre_of_mass = centre_of_mass,
                               channel = channel,
                               variable = variable,
                               met_type = met_type,
                               bin_edges = bin_edges_full[variable] )
            elif test == 'bias':
def main():
    config = XSectionConfig(13)
    method = 'TUnfold'

    file_for_response = File(config.unfolding_central, 'read')
    file_for_powhegPythia  = File(config.unfolding_central, 'read')
    file_for_madgraph  = File(config.unfolding_madgraphMLM, 'read')
    file_for_amcatnlo  = File(config.unfolding_amcatnlo, 'read')
    file_for_ptReweight_up  = File(config.unfolding_ptreweight_up, 'read')
    file_for_ptReweight_down  = File(config.unfolding_ptreweight_down, 'read')

    samples_and_files_to_compare = {
    'Central' : file_for_powhegPythia,
    'Reweighted Up' : file_for_ptReweight_up,
    'Reweighted Down' : file_for_ptReweight_down,

    # 'Madgraph' : file_for_madgraph,
    # 'amc@NLO' : file_for_amcatnlo
    }

    for channel in ['combined']:
        for variable in config.variables:
        # for variable in ['ST']:


            print 'Variable :',variable

            # Always unfold with the same response matrix and tau value
            tau_value = get_tau_value(config, channel, variable) 
            _, _, h_response, _ = get_unfold_histogram_tuple(
                inputfile=file_for_response,
                variable=variable,
                channel=channel,
                met_type=config.met_type,
                centre_of_mass=config.centre_of_mass_energy,
                ttbar_xsection=config.ttbar_xsection,
                luminosity=config.luminosity,
                load_fakes=False,
                visiblePS=True,
            )

            integralOfResponse = asrootpy(h_response.ProjectionY()).integral(0,-1)

            # Dictionary to hold results
            unfolded_and_truth_for_sample = {}

            for sample, input_file_for_unfolding in samples_and_files_to_compare.iteritems():

                _, _, h_response_to_unfold, _ = get_unfold_histogram_tuple(
                    inputfile=input_file_for_unfolding,
                    variable=variable,
                    channel=channel,
                    met_type=config.met_type,
                    centre_of_mass=config.centre_of_mass_energy,
                    ttbar_xsection=config.ttbar_xsection,
                    luminosity=config.luminosity,
                    load_fakes=False,
                    visiblePS=True,
                )

                measured = asrootpy(h_response_to_unfold.ProjectionX('px',1))
                truth = asrootpy(h_response_to_unfold.ProjectionY())

                scale = integralOfResponse / truth.integral(0,-1)
                measured.Scale( scale )
                truth.Scale( scale )
                # Unfold, and set 'data' to 'measured' 
                unfolding = Unfolding( measured,
                    truth, measured, h_response, None,
                    method=method, k_value=-1, tau=tau_value)
                
                unfolded_data = unfolding.unfold()

                unfolded_xsection = calculate_xsection( unfolded_data, variable )
                truth_xsection = calculate_xsection( truth, variable )

                bias = calculate_bias( truth, unfolded_data )
                unfolded_and_truth_for_sample[sample] = {
                                                            'truth' : truth_xsection,
                                                            'unfolded' : unfolded_xsection,
                                                            'bias' : bias
                }

            plot_closure(unfolded_and_truth_for_sample, variable, channel,
                         config.centre_of_mass_energy, method)

            plot_bias(unfolded_and_truth_for_sample, variable, channel,
                         config.centre_of_mass_energy, method)
def get_unfolded_normalisation( TTJet_fit_results, category, channel, k_value ):
    global variable, met_type, path_to_JSON, file_for_unfolding, file_for_powheg_pythia, file_for_powheg_herwig, file_for_mcatnlo, file_for_ptreweight, files_for_pdfs
    global centre_of_mass, luminosity, ttbar_xsection, load_fakes, method
    global file_for_matchingdown, file_for_matchingup, file_for_scaledown, file_for_scaleup
    global ttbar_generator_systematics, ttbar_theory_systematics, pdf_uncertainties
    global use_ptreweight

    files_for_systematics = {
                             ttbar_theory_systematic_prefix + 'matchingdown':file_for_matchingdown,
                             ttbar_theory_systematic_prefix + 'matchingup':file_for_matchingup,
                             ttbar_theory_systematic_prefix + 'scaledown':file_for_scaledown,
                             ttbar_theory_systematic_prefix + 'scaleup':file_for_scaleup,
                             ttbar_theory_systematic_prefix + 'powheg_pythia':file_for_powheg_pythia,
                             ttbar_theory_systematic_prefix + 'powheg_herwig':file_for_powheg_herwig,
                             ttbar_theory_systematic_prefix + 'ptreweight':file_for_ptreweight,
                             }
    
    h_truth, h_measured, h_response, h_fakes = None, None, None, None
    if category in ttbar_generator_systematics or category in ttbar_theory_systematics:
        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 = load_fakes
                                                                              )
    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 = load_fakes
                                                                              )
    elif use_ptreweight:
        h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( inputfile = file_for_ptreweight,
                                                                              variable = variable,
                                                                              channel = channel,
                                                                              met_type = met_type,
                                                                              centre_of_mass = centre_of_mass,
                                                                              ttbar_xsection = ttbar_xsection,
                                                                              luminosity = luminosity,
                                                                              load_fakes = load_fakes
                                                                              )
    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 = load_fakes
                                                                              )

    h_truth_POWHEG_PYTHIA, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_powheg_pythia,
                                                variable = variable,
                                                channel = channel,
                                                met_type = met_type,
                                                centre_of_mass = centre_of_mass,
                                                ttbar_xsection = ttbar_xsection,
                                                luminosity = luminosity,
                                                load_fakes = load_fakes
                                                )
    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 = load_fakes
                                                )
    h_truth_MCATNLO, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_mcatnlo,
                                                variable = variable,
                                                channel = channel,
                                                met_type = met_type,
                                                centre_of_mass = centre_of_mass,
                                                ttbar_xsection = ttbar_xsection,
                                                luminosity = luminosity,
                                                load_fakes = load_fakes
                                                )
    h_truth_matchingdown, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_matchingdown,
                                                variable = variable,
                                                channel = channel,
                                                met_type = met_type,
                                                centre_of_mass = centre_of_mass,
                                                ttbar_xsection = ttbar_xsection,
                                                luminosity = luminosity,
                                                load_fakes = load_fakes
                                                )
    h_truth_matchingup, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_matchingup,
                                                variable = variable,
                                                channel = channel,
                                                met_type = met_type,
                                                centre_of_mass = centre_of_mass,
                                                ttbar_xsection = ttbar_xsection,
                                                luminosity = luminosity,
                                                load_fakes = load_fakes
                                                )
    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 = load_fakes
                                                )
    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 = load_fakes
                                                )
    h_truth_ptreweight, _, _, _ = get_unfold_histogram_tuple(
                                                inputfile = file_for_ptreweight,
                                                variable = variable,
                                                channel = channel,
                                                met_type = met_type,
                                                centre_of_mass = centre_of_mass,
                                                ttbar_xsection = ttbar_xsection,
                                                luminosity = luminosity,
                                                load_fakes = load_fakes
                                                )

    MADGRAPH_results = hist_to_value_error_tuplelist( h_truth )
    MADGRAPH_ptreweight_results = hist_to_value_error_tuplelist( h_truth_ptreweight )
    POWHEG_PYTHIA_results = hist_to_value_error_tuplelist( h_truth_POWHEG_PYTHIA )
    POWHEG_HERWIG_results = hist_to_value_error_tuplelist( h_truth_POWHEG_HERWIG )
    MCATNLO_results = hist_to_value_error_tuplelist( h_truth_MCATNLO )
    
    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 )

    TTJet_fit_results_unfolded = unfold_results( TTJet_fit_results,
                                                category,
                                                channel,
                                                k_value,
                                                h_truth,
                                                h_measured,
                                                h_response,
                                                h_fakes,
                                                method
                                                )
        
    normalisation_unfolded = {
                              'TTJet_measured' : TTJet_fit_results,
                              'TTJet_unfolded' : TTJet_fit_results_unfolded,
                              'MADGRAPH': MADGRAPH_results,
                              'MADGRAPH_ptreweight': MADGRAPH_ptreweight_results,
                              # other generators
                              'POWHEG_PYTHIA': POWHEG_PYTHIA_results,
                              'POWHEG_HERWIG': POWHEG_HERWIG_results,
                              'MCATNLO': MCATNLO_results,
                              # systematics
                              'matchingdown': matchingdown_results,
                              'matchingup': matchingup_results,
                              'scaledown': scaledown_results,
                              'scaleup': scaleup_results
                              }
    
    return normalisation_unfolded
    for channel in ['electron', 'muon']:
        taus_from_global_correlaton[channel] = {}
        taus_from_L_shape[channel] = {}

        k_values = {}
        k_values_crosscheck = {}

        for variable in variables:
            if variable is 'MT': continue
            print 'Doing variable', variable, 'in', channel, 'channel'

            h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple(
                inputfile=input_file,
                variable=variable,
                channel=channel,
                met_type=met_type,
                centre_of_mass=centre_of_mass,
                ttbar_xsection=ttbar_xsection,
                luminosity=luminosity,
                load_fakes=load_fakes)
            # print 'h_fakes = ', h_fakes

            h_data = None
            if test == 'data':
                h_data = get_fit_results_histogram(
                    data_path=path_to_JSON,
                    centre_of_mass=centre_of_mass,
                    channel=channel,
                    variable=variable,
                    met_type=met_type,
                    bin_edges=bin_edges[variable])
Example #14
0
#     taus_from_L_shape = {}
    for variable in variables:
#         taus_from_global_correlaton[channel] = {}
#         taus_from_L_shape[channel] = {}
        for channel in ['electron', 'muon']:
            if channel == 'electron':
                used_k = measurement_config.k_values_electron[variable]
            if channel == 'muon':
                used_k = measurement_config.k_values_muon[variable]
                
            print 'Doing variable"', variable, '" in', channel, 'channel'
        
            h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple( 
                                inputfile = input_file,
                                variable = variable,
                                channel = channel,
                                met_type = met_type,
                                centre_of_mass = centre_of_mass,
                                ttbar_xsection = ttbar_xsection,
                                luminosity = luminosity )
            
            h_data = None
            if use_data:
                h_data = get_data_histogram( channel, variable, met_type )
            else:
                h_data = deepcopy( h_measured )
                
            
            tau, rho, tau_values, rho_values, tau_from_k = get_tau_from_global_correlation( h_truth, h_measured, h_response, h_data )
            draw_global_correlation( tau_values, rho_values, tau, rho, channel, variable, tau_from_k )
            
            #tau, l_curve, x, y = get_tau_from_L_shape( h_truth, h_measured, h_response, h_data )
def main():
    set_root_defaults()
    # prevent directory ownership of ROOT histograms (python does the garbage collection)
    TH1F.AddDirectory( False )
    parser = OptionParser()
    parser.add_option( "-n", "--n_toy_mc",
                      dest = "n_toy_mc", default = 300,
                      help = "number of toy MC to create", type = int )
    parser.add_option( "-o", "--output",
                      dest = "output_folder", default = 'data/toy_mc/',
                      help = "output folder for toy MC" )
    parser.add_option( "-v", "--variable", dest = "variable", default = 'MET',
                      help = "set the variable to analyse (MET, HT, ST, MT, WPT)" )
    parser.add_option( "-m", "--metType", dest = "metType", default = 'type1',
                      help = "set MET type for analysis of MET, ST or MT" )
    parser.add_option( "-c", "--centre-of-mass-energy", dest = "CoM", default = 8,
                      help = "set the centre of mass energy for analysis. Default = 8 [TeV]", type = int )
    parser.add_option( '-V', '--verbose', dest = "verbose", action = "store_true",
                      help = "Print the event number, reco and gen variable value" )

    ( options, _ ) = parser.parse_args()
    measurement_config = XSectionConfig( options.CoM )

    centre_of_mass = options.CoM
    ttbar_xsection = measurement_config.ttbar_xsection
    variable = options.variable
    met_type = measurement_config.translate_options[options.metType]
    n_toy_mc = options.n_toy_mc
    make_folder_if_not_exists( options.output_folder )
    
    # get histograms
    input_file_hists = File( measurement_config.unfolding_madgraph )
    # define output file
    out_file_template = '%s/toy_mc_%s_N_%d_%dTeV.root'
    out_file_name = out_file_template % (options.output_folder, variable, n_toy_mc, centre_of_mass)
    output = File( out_file_name, 'recreate' )
    
    for channel in ['electron', 'muon']:
        # first get the weights
        h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple( input_file_hists,
                                                                                       variable,
                                                                                       channel,
                                                                                       met_type,
                                                                                       centre_of_mass,
                                                                                       ttbar_xsection,
                                                                                       load_fakes = False )
        # create directories
        directory = output.mkdir( channel )
        mkdir = directory.mkdir
        cd = directory.cd
        cd()
        # generate toy MC
        for i in range( 1, n_toy_mc + 1 ):
            mkdir( 'toy_%d' % i )
            cd( 'toy_%d' % i )
            # create histograms
            # add tuples (truth, measured, response) of histograms
            truth = generate_toy_MC_from_distribution(h_truth)
            measured = generate_toy_MC_from_distribution(h_measured)
            response = generate_toy_MC_from_2Ddistribution(h_response)
            
            truth.SetName('truth')
            measured.SetName('measured')
            response.SetName('response')
            
            truth.Write()
            measured.Write()
            response.Write()
    output.Write()
    output.Close()
Example #16
0
def get_unfolded_normalisation(TTJet_fit_results, category, channel, tau_value,
                               visiblePS):
    global variable, met_type, path_to_JSON, file_for_unfolding, file_for_powheg_pythia, file_for_amcatnlo_herwig, file_for_ptreweight, files_for_pdfs
    global centre_of_mass, luminosity, ttbar_xsection, load_fakes, method
    global file_for_powhegPythia8, file_for_madgraphMLM, file_for_amcatnlo
    # 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 + 'matchingdown'    :  file_for_matchingdown,
        # ttbar_theory_systematic_prefix + 'matchingup'      :  file_for_matchingup,
        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,

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

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

        # 'BJet_up'        :  file_for_bjetdown,
        # 'BJet_down'        :  file_for_bjetup,
        ttbar_theory_systematic_prefix + 'hadronisation':
        file_for_amcatnlo_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,

        # 'PileUpSystematic' : file_for_PUSystematic,
    }

    h_truth, h_measured, h_response, h_fakes = None, None, None, None
    # Systematics where you change the response matrix
    if category in ttbar_generator_systematics or 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=load_fakes,
            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=load_fakes,
            visiblePS=visiblePS,
        )
    # Systematics where you 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=load_fakes,
            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_matchingdown, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_matchingdown,
        #                                             variable = variable,
        #                                             channel = channel,
        #                                             met_type = met_type,
        #                                             centre_of_mass = centre_of_mass,
        #                                             ttbar_xsection = ttbar_xsection,
        #                                             luminosity = luminosity,
        #                                             load_fakes = load_fakes
        #                                             )
        # h_truth_matchingup, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_matchingup,
        #                                             variable = variable,
        #                                             channel = channel,
        #                                             met_type = met_type,
        #                                             centre_of_mass = centre_of_mass,
        #                                             ttbar_xsection = ttbar_xsection,
        #                                             luminosity = luminosity,
        #                                             load_fakes = load_fakes
        #                                             )
        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=load_fakes,
            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=load_fakes,
            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=load_fakes,
            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=load_fakes,
            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=load_fakes,
            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=load_fakes,
            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=load_fakes,
            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=load_fakes,
            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)
        AMCATNLO_results = hist_to_value_error_tuplelist(h_truth_amcatnlo)
        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'] = AMCATNLO_results
        normalisation_unfolded['madgraphMLM'] = madgraphMLM_results
        normalisation_unfolded['amcatnlo_HERWIG'] = 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
def main():

    config = XSectionConfig(13)
    method = 'TUnfold'

    # A few different files for testing different inputs
    file_for_unfolding = File(config.unfolding_central, 'read')
    madgraph_file = File(config.unfolding_madgraphMLM, 'read')

    for channel in ['combined']:

        # for variable in config.variables:
        for variable in ['HT']:
        
            print variable

            # tau_value = get_tau_value(config, channel, variable)
            # tau_value = 0.000228338590921
            tau_value = 0.0

            # h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple(
            #     inputfile=file_for_unfolding,
            #     variable=variable,
            #     channel=channel,
            #     met_type=config.met_type,
            #     centre_of_mass=config.centre_of_mass_energy,
            #     ttbar_xsection=config.ttbar_xsection,
            #     luminosity=config.luminosity,
            #     load_fakes=False,
            #     visiblePS=True,
            # )

            # measured = asrootpy(h_response.ProjectionX('px',1))
            # print 'Measured from response :',list(measured.y())
            # truth = asrootpy(h_response.ProjectionY())
            # print 'Truth from response :',list(truth.y())

            h_truth_mad, h_measured_mad, h_response_mad, h_fakes_mad = get_unfold_histogram_tuple(
                inputfile=madgraph_file,
                variable=variable,
                channel=channel,
                met_type=config.met_type,
                centre_of_mass=config.centre_of_mass_energy,
                ttbar_xsection=config.ttbar_xsection,
                luminosity=config.luminosity,
                load_fakes=False,
                visiblePS=True,
            )

            measured = asrootpy(h_response_mad.ProjectionX('px',1))
            print 'Measured from response :',list(measured.y())
            truth = asrootpy(h_response_mad.ProjectionY())
            print 'Truth from response :',list(truth.y())

            # Unfold
            unfolding = Unfolding( measured,
                truth, measured, h_response_mad, None,
                method=method, k_value=-1, tau=tau_value)

            # unfolded_data = unfolding.closureTest()

            # print 'Measured :',list( h_measured.y() )
            # h_measured, _ = removeFakes( h_measured, None, h_response)

            # for binx in range(0,h_truth.GetNbinsX()+2):
            #     for biny in range(0,h_truth.GetNbinsX()+2):
            #         print binx, biny,h_response.GetBinContent(binx,biny)
                # print bin,h_truth.GetBinContent(bin)
            print 'Tau :',tau_value
            unfolded_results = unfolding.unfold()
            print 'Unfolded :',list( unfolded_results.y() )
            print unfolding.unfoldObject.GetTau()
def get_unfolded_normalisation( TTJet_fit_results, category, channel, tau_value, visiblePS ):
    global variable, met_type, path_to_JSON, file_for_unfolding, file_for_powheg_pythia, file_for_amcatnlo_herwig, file_for_ptreweight, files_for_pdfs
    global centre_of_mass, luminosity, ttbar_xsection, load_fakes, method
    global file_for_powhegPythia8, file_for_madgraphMLM, file_for_amcatnlo
    # 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 + 'matchingdown'    :  file_for_matchingdown,
                             # ttbar_theory_systematic_prefix + 'matchingup'      :  file_for_matchingup,
                             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,

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

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

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

                             ttbar_theory_systematic_prefix + 'hadronisation'   :  file_for_amcatnlo_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,

                             # 'PileUpSystematic' : file_for_PUSystematic,
                             }

    h_truth, h_measured, h_response, h_fakes = None, None, None, None
    # Systematics where you change the response matrix
    if category in ttbar_generator_systematics or 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 = load_fakes,
                                                                              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 = load_fakes,
                                                                              visiblePS = visiblePS,
                                                                              )
    # Systematics where you 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 = load_fakes,
                                                                              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_matchingdown, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_matchingdown,
        #                                             variable = variable,
        #                                             channel = channel,
        #                                             met_type = met_type,
        #                                             centre_of_mass = centre_of_mass,
        #                                             ttbar_xsection = ttbar_xsection,
        #                                             luminosity = luminosity,
        #                                             load_fakes = load_fakes
        #                                             )
        # h_truth_matchingup, _, _, _ = get_unfold_histogram_tuple( inputfile = file_for_matchingup,
        #                                             variable = variable,
        #                                             channel = channel,
        #                                             met_type = met_type,
        #                                             centre_of_mass = centre_of_mass,
        #                                             ttbar_xsection = ttbar_xsection,
        #                                             luminosity = luminosity,
        #                                             load_fakes = load_fakes
        #                                             )
        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 = load_fakes,
                                                    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 = load_fakes,
                                                    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 = load_fakes,
                                                    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 = load_fakes,
                                                    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 = load_fakes,
                                                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 = load_fakes,
                                                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 = load_fakes,
                                                    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 = load_fakes,
                                                    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 )
        AMCATNLO_results = hist_to_value_error_tuplelist( h_truth_amcatnlo )
        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'] =  AMCATNLO_results
        normalisation_unfolded['madgraphMLM'] = madgraphMLM_results
        normalisation_unfolded['amcatnlo_HERWIG'] =  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
def main():

	config = XSectionConfig(13)

	file_for_powhegPythia  = File(config.unfolding_central, 'read')
	file_for_ptReweight_up  = File(config.unfolding_ptreweight_up, 'read')
	file_for_ptReweight_down  = File(config.unfolding_ptreweight_down, 'read')
	file_for_data_template = 'data/normalisation/background_subtraction/13TeV/{variable}/VisiblePS/central/normalisation_combined_patType1CorrectedPFMet.txt'



	for channel in ['combined']:
		for variable in config.variables:
			print variable
		# for variable in ['HT']:
			# Get the central powheg pythia distributions
			_, _, response_central, fakes_central = get_unfold_histogram_tuple(
				inputfile=file_for_powhegPythia,
				variable=variable,
				channel=channel,
				centre_of_mass=13,
				load_fakes=True,
				visiblePS=True
			)

			measured_central = asrootpy(response_central.ProjectionX('px',1))
			truth_central = asrootpy(response_central.ProjectionY())


			# Get the reweighted powheg pythia distributions
			_, _, response_reweighted_up, _ = get_unfold_histogram_tuple(
				inputfile=file_for_ptReweight_up,
				variable=variable,
				channel=channel,
				centre_of_mass=13,
				load_fakes=False,
				visiblePS=True
			)

			measured_reweighted_up = asrootpy(response_reweighted_up.ProjectionX('px',1))
			truth_reweighted_up = asrootpy(response_reweighted_up.ProjectionY())

			_, _, response_reweighted_down, _ = get_unfold_histogram_tuple(
				inputfile=file_for_ptReweight_down,
				variable=variable,
				channel=channel,
				centre_of_mass=13,
				load_fakes=False,
				visiblePS=True
			)

			measured_reweighted_down = asrootpy(response_reweighted_down.ProjectionX('px',1))
			truth_reweighted_down = asrootpy(response_reweighted_down.ProjectionY())

			# Get the data input (data after background subtraction, and fake removal)
			file_for_data = file_for_data_template.format( variable = variable )
			data = read_data_from_JSON(file_for_data)['TTJet']
			data = value_error_tuplelist_to_hist( data, reco_bin_edges_vis[variable] )
			data = removeFakes( measured_central, fakes_central, data )

			# Plot all three

			hp = Histogram_properties()
			hp.name = 'Reweighting_check_{channel}_{variable}_at_{com}TeV'.format(
						channel=channel,
						variable=variable,
						com='13',
			)

			v_latex = latex_labels.variables_latex[variable]
			unit = ''
			if variable in ['HT', 'ST', 'MET', 'WPT', 'lepton_pt']:
			    unit = ' [GeV]'
			hp.x_axis_title = v_latex + unit
			hp.y_axis_title = 'Number of events'
			hp.title = 'Reweighting check for {variable}'.format(variable=v_latex)

			measured_central.Rebin(2)
			measured_reweighted_up.Rebin(2)
			measured_reweighted_down.Rebin(2)
			data.Rebin(2)

			measured_central.Scale( 1 / measured_central.Integral() )
			measured_reweighted_up.Scale( 1 / measured_reweighted_up.Integral() )
			measured_reweighted_down.Scale( 1 / measured_reweighted_down.Integral() )

			data.Scale( 1 / data.Integral() )

			compare_measurements(
					models = {'Central' : measured_central, 'Reweighted Up' : measured_reweighted_up, 'Reweighted Down' : measured_reweighted_down},
					measurements = {'Data' : data},
					show_measurement_errors=True,
					histogram_properties=hp,
					save_folder='plots/unfolding/reweighting_check',
					save_as=['pdf']
					)