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)
Example #3
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)
def create_unfolding_pull_data(input_file_name, method, channel,
                               centre_of_mass, variable,
                               sample, 
                               responseFile,
                               n_toy_data,
                               output_folder, 
                               tau_value,
                                run_matrix=None):
    '''
        Sets up all variables for check_multiple_data_multiple_unfolding
    '''
    set_root_defaults(msg_ignore_level=3001)
    timer = Timer()
    input_file = File(input_file_name, 'read')
    folder_template = '{path}/{centre_of_mass}TeV/{variable}/{sample}/'

    msg_template = 'Producing unfolding pull data for {variable},'
    msg_template += ' tau-value {value}'
    inputs = {
        'path': output_folder,
        'centre_of_mass': centre_of_mass,
        'variable': variable,
        'sample': sample,
        'value': round(tau_value,4),
    }

    h_response = get_response_histogram(responseFile, variable, channel)
    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))
    print ('Response here :',h_response)
    output_file_name = check_multiple_data_multiple_unfolding(
                                input_file, method, channel, variable, 
                                h_response,
                                n_toy_data,
                                output_folder, 
                                tau_value,
                            )
    print('Runtime', timer.elapsed_time())

    return output_file_name
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())
    # set the number of toy MC for error calculation
    k_value = options.k_value
    use_N_toy = options.n_input_mc
    offset_toy_mc = options.offset_toy_mc
    offset_toy_data = options.offset_toy_data
    method = options.method
    variable = options.variable

    # define bins
    bins = array( 'd', bin_edges[variable] )
    nbins = len( bins ) - 1
    
    output_folder = options.output_folder + '/' + str(centre_of_mass) + 'TeV/' + variable + '/%d_input_toy_mc/k_value_%d/' % ( use_N_toy, k_value )
    make_folder_if_not_exists( output_folder )

    print 'Producing unfolding pull data for %s variable, k-value %s. \nOutput folder: %s' % ( variable, k_value, output_folder )

    input_file = File( options.file, 'read' )
    
    timer = Timer()
    if options.channel == 'electron':
        check_multiple_data_multiple_unfolding( input_file, method, 'electron' )
    elif options.channel == 'muon':
        check_multiple_data_multiple_unfolding( input_file, method, 'muon' )
    else:
        check_multiple_data_multiple_unfolding( input_file, method, 'combined' )

    end1, end2 = clock(), time()
    
    print 'Runtime', timer.elapsed_time()