def create_unfolding_pull_data(input_file_name,
                               method,
                               channel,
                               centre_of_mass,
                               variable,
                               n_toy_mc,
                               n_toy_data,
                               output_folder,
                               offset_toy_mc,
                               offset_toy_data,
                               k_value,
                               tau_value=-1,
                               run_matrix=None):
    '''
        Sets up all variables for check_multiple_data_multiple_unfolding
    '''
    timer = Timer()
    input_file = File(input_file_name, 'read')
    folder_template = '{path}/{centre_of_mass}TeV/{variable}/'
    folder_template += '{n_toy_mc}_input_toy_mc/{n_toy_data}_input_toy_data/'
    folder_template += '{vtype}_value_{value}/'

    msg_template = 'Producing unfolding pull data for {variable},'
    msg_template += ' {vtype}-value {value}'
    inputs = {
        'path': output_folder,
        'centre_of_mass': centre_of_mass,
        'variable': variable,
        'n_toy_mc': n_toy_mc,
        'n_toy_data': n_toy_data,
        'vtype': 'k',
        'value': k_value,
    }
    if tau_value >= 0:
        inputs['vtype'] = 'tau'
        inputs['value'] = round(tau_value, 1)

    output_folder = folder_template.format(**inputs)
    make_folder_if_not_exists(output_folder)
    print(msg_template.format(**inputs))
    print('Output folder: {0}'.format(output_folder))

    check_multiple_data_multiple_unfolding(
        input_file,
        method,
        channel,
        variable,
        n_toy_mc,
        n_toy_data,
        output_folder,
        offset_toy_mc,
        offset_toy_data,
        k_value,
        tau_value,
        run_matrix,
    )
    print('Runtime', timer.elapsed_time())
Example #2
0
def convert_unfolding_histograms(file_name,
                                 histograms_to_load=[
                                     'truth',
                                     'fake',
                                     'measured',
                                     'response',
                                     'response_withoutFakes',
                                     'response_without_fakes',
                                     'EventCounter',
                                 ]):

    file_start = Timer()
    print 'Converting', file_name
    histograms = {}
    with File(file_name) as f:
        for path, _, objects in f.walk():
            # keep only unfolding and EventFilter
            if path.startswith('unfolding_') or path == 'EventFilter':
                histograms[path] = {}
                for hist_name in objects:
                    if hist_name in histograms_to_load:
                        hist = f.Get(path + '/' + hist_name).Clone()
                        hist.SetDirectory(0)
                        histograms[path][hist_name] = hist
    new_histograms = {}
    # rebin
    for path, hists in histograms.iteritems():
        new_histograms[path] = {}
        variable = ''
        if not path == 'EventFilter':
            variable = path.split('_')[1]
        for name, hist in hists.iteritems():
            if name == 'EventCounter':
                new_histograms[path][name] = hist.Clone()
            else:
                new_hist = hist.rebinned(bin_edges[variable])
                if 'TH2' in new_hist.class_name():
                    new_hist = new_hist.rebinned(bin_edges[variable], axis=1)
                new_histograms[path][name] = new_hist

    # save_to_file
    output = File(file_name.replace('.root', '_asymmetric.root'), 'recreate')
    for path, hists in new_histograms.iteritems():
        directory = output.mkdir(path)
        directory.cd()
        for name, hist in hists.iteritems():
            if name == 'response_withoutFakes':  # fix this name
                hist.Write('response_without_fakes')
            else:
                hist.Write(name)
    output.close()
    secs = file_start.elapsed_time()
    print 'File %s converted in %d seconds' % (file_name, secs)