コード例 #1
0
def save_weights(fp, sample_names, stitching_dict):
    for sample_name in sample_names:
        sample_dir = fp.mkdir(sample_name)
        hist_dir = sample_dir.mkdir(SPLITVAR)
        hist_dir.cd()
        for bin_count_name in stitching_dict:
            bin_array = array.array('f', list(range(max(bin_keys) + 2)))
            hist = ROOT.TH1D(bin_count_name, bin_count_name,
                             len(bin_array) - 1, bin_array)
            hist.SetDirectory(hist_dir)
            hist.SetTitle(bin_count_name)
            hist.SetXTitle(SPLITVAR)
            for bin_idx in bin_keys:
                hist.SetBinContent(bin_idx + 1,
                                   stitching_dict[bin_count_name][bin_idx])
                hist.GetXaxis().SetBinLabel(
                    bin_idx + 1,
                    "%d <= %s < %d" % (bin_idx, SPLITVAR, bin_idx + 1))
            hist.Write()
コード例 #2
0
 def create_histogram(key, title):
     if is2D:
         histograms[key] = ROOT.TH2D(key, title,
                                     len(bins_x) - 1, bins_x,
                                     len(bins_y) - 1, bins_y)
         for bin_idx in range(len(bins_x) - 1):
             histograms[key].GetXaxis().SetBinLabel(
                 bin_idx + 1, '%d <= %s < %d' %
                 (bins_x[bin_idx], x_var, bins_x[bin_idx + 1]))
         for bin_idx in range(len(bins_y) - 1):
             histograms[key].GetYaxis().SetBinLabel(
                 bin_idx + 1, '%d <= %s < %d' %
                 (bins_y[bin_idx], y_var, bins_y[bin_idx + 1]))
         histograms[key].SetXTitle(x_var)
         histograms[key].SetYTitle(y_var)
     else:
         histograms[key] = ROOT.TH1D(key, title, len(bins_x) - 1, bins_x)
         for bin_idx in range(len(bins_x) - 1):
             histograms[key].GetXaxis().SetBinLabel(
                 bin_idx + 1, '%d <= %s < %d' %
                 (bins_x[bin_idx], x_var, bins_x[bin_idx + 1]))
         histograms[key].SetXTitle(x_var)
コード例 #3
0
def comp_weights_1(f, samples, samples_to_stitch, split_var, apply_sf=True):
    inclusive_samples = samples_to_stitch['inclusive']['samples']
    inclusive_binning = samples_to_stitch['inclusive'][split_var]

    split_dict = samples_to_stitch['exclusive'][split_var]
    split_binning = [sample['value'] for sample in split_dict]
    complete_binning = list(
        sorted(
            list(
                map(
                    float,
                    set(inclusive_binning) | set(
                        list(itertools.chain.from_iterable(split_binning)))))))

    inclusive_xs = -1
    for sample_key, sample_entry in samples.items():
        if sample_key == 'sum_events': continue
        if sample_entry['process_name_specific'] == inclusive_samples[0]:
            inclusive_xs = sample_entry['xsection']
    assert (inclusive_xs > 0)

    # sum the inclusive nof events
    inclusive_nof_events = {}
    for sample_key, sample_entry in samples.items():
        if sample_key == 'sum_events': continue
        if sample_entry['process_name_specific'] in inclusive_samples:
            if not inclusive_nof_events:
                inclusive_nof_events = copy_nof_events(sample_entry)
            else:
                nof_events_keys = set(nof_key
                                      for nof_key in sample_entry['nof_events']
                                      if is_valid_event_type(nof_key))
                assert (nof_events_keys == set(inclusive_nof_events))
                for nof_key, nof_arr in sample_entry['nof_events'].items():
                    if not is_valid_event_type(nof_key):
                        continue
                    assert (len(nof_arr) == len(inclusive_nof_events[nof_key]))
                    for idx, nof in enumerate(nof_arr):
                        assert (nof > 0)
                        inclusive_nof_events[nof_key][idx] += nof

    # sum the binned nof events
    for binned_sample in split_dict:
        nof_events = {}
        xs = -1
        for sample_key, sample_entry in samples.items():
            if sample_key == 'sum_events': continue
            if sample_entry['process_name_specific'] in binned_sample[
                    'samples']:
                if not nof_events:
                    nof_events = copy_nof_events(sample_entry)
                    inclusive_nof_events_type = set(
                        event_type
                        for event_type in inclusive_nof_events.keys()
                        if is_valid_event_type(event_type))
                    nof_events_type = set(event_type
                                          for event_type in nof_events.keys()
                                          if is_valid_event_type(event_type))
                    assert (inclusive_nof_events_type == nof_events_type)
                else:
                    nof_events_keys = set(
                        nof_key for nof_key in sample_entry['nof_events']
                        if is_valid_event_type(nof_key))
                    assert (nof_events_keys == set(nof_events.keys()))
                    for nof_key, nof_arr in sample_entry['nof_events'].items():
                        if not is_valid_event_type(nof_key):
                            continue
                        assert (len(nof_arr) == len(nof_events[nof_key]))
                        for idx, nof in enumerate(nof_arr):
                            assert (nof > 0)
                            nof_events[nof_key][idx] += nof
                if xs < 0:
                    xs = sample_entry['xsection']
        assert (xs > 0)
        binned_sample['xsection'] = xs
        binned_sample['nof_events'] = nof_events

        lumis = {}
        for nof_key, nof_arr in binned_sample['nof_events'].items():
            lumis[nof_key] = list(
                map(lambda nof: nof / binned_sample['xsection'], nof_arr))
        binned_sample['lumis'] = lumis

    # compute integrated luminosities for the inclusive sample
    inclusive_lumis = {}
    for nof_key, nof_arr in inclusive_nof_events.items():
        inclusive_lumis[nof_key] = list(
            map(lambda nof: nof / inclusive_xs, nof_arr))

    # decide on the bin indices
    idxs_split_sample = []
    for binned_sample in split_dict:
        binned_idx = complete_binning.index(binned_sample['value'][0]) + 1
        binned_sample['idx'] = binned_idx
        idxs_split_sample.append(binned_idx)

    for inclusive_sample in inclusive_samples:
        if inclusive_sample not in [
                key.GetName() for key in f.GetListOfKeys()
        ]:
            histogram_dir_root = f.mkdir(inclusive_sample)
        else:
            histogram_dir_root = f.Get(inclusive_sample)
        if split_var not in [
                key.GetName() for key in histogram_dir_root.GetListOfKeys()
        ]:
            histogram_dir = histogram_dir_root.mkdir(split_var)
        else:
            histogram_dir = histogram_dir_root.Get(split_var)
        histogram_dir.cd()

        for nof_key, lumi_arr in inclusive_lumis.items():
            if not is_valid_event_type(nof_key):
                continue
            for idx, lumi_incl in enumerate(lumi_arr):
                histogram_name = '%s_%d' % (nof_key, idx)
                binning = array.array('f', complete_binning)

                histogram = ROOT.TH1D(histogram_name, histogram_name,
                                      len(binning) - 1, binning)
                histogram.SetDirectory(histogram_dir)
                histogram.SetXTitle(split_var)

                for split_idx in range(1, len(binning)):
                    if split_idx in idxs_split_sample:
                        for binned_sample in split_dict:
                            if split_idx == binned_sample['idx']:
                                lumi_split = binned_sample['lumis'][nof_key][
                                    idx]
                                if binning[split_idx] > inclusive_binning[-1] or \
                                   binning[split_idx] < inclusive_binning[0]:
                                    lumi_incl_calc = 0.
                                else:
                                    lumi_incl_calc = lumi_incl
                                weight = lumi_incl_calc / (lumi_incl_calc +
                                                           lumi_split)
                                histogram.SetBinContent(split_idx, weight)
                    else:
                        histogram.SetBinContent(split_idx, 1.)
                    histogram.GetXaxis().SetBinLabel(
                        split_idx, '%.0f <= %s < %.0f' %
                        (complete_binning[split_idx - 1], split_var,
                         complete_binning[split_idx]))

                histogram.Write()

    for binned_sample in split_dict:
        for sample_name in binned_sample['samples']:
            if sample_name not in [key.GetName() for key in f.GetListOfKeys()]:
                histogram_dir_root = f.mkdir(sample_name)
            else:
                histogram_dir_root = f.Get(sample_name)
            if split_var not in [
                    key.GetName()
                    for key in histogram_dir_root.GetListOfKeys()
            ]:
                histogram_dir = histogram_dir_root.mkdir(split_var)
            else:
                histogram_dir = histogram_dir_root.Get(split_var)
            histogram_dir.cd()

            for nof_key, lumi_arr in inclusive_lumis.items():
                if not is_valid_event_type(nof_key):
                    continue
                for idx, lumi_incl in enumerate(lumi_arr):

                    histogram_name = '%s_%d' % (nof_key, idx)
                    binning = array.array('f', complete_binning)

                    histogram = ROOT.TH1D(histogram_name, histogram_name,
                                          len(binning) - 1, binning)
                    histogram.SetDirectory(histogram_dir)
                    histogram.SetXTitle(split_var)

                    for split_idx in range(1, len(binning)):
                        if split_idx == binned_sample['idx']:
                            lumi_split = binned_sample['lumis'][nof_key][idx]
                            if binning[split_idx] > inclusive_binning[-1] or \
                               binning[split_idx] < inclusive_binning[0]:
                                weight = 1.
                            else:
                                if apply_sf:
                                    weight = lumi_split / (lumi_incl +
                                                           lumi_split)
                                else:
                                    weight = lumi_incl / (lumi_incl +
                                                          lumi_split)
                            assert (weight >= 0.)
                            histogram.SetBinContent(split_idx, weight)
                        else:
                            histogram.SetBinContent(split_idx, 0.)
                        histogram.GetXaxis().SetBinLabel(
                            split_idx, '%.0f <= %s < %.0f' %
                            (complete_binning[split_idx - 1], split_var,
                             complete_binning[split_idx]))

                    histogram.Write()
コード例 #4
0
ファイル: getRefGenWeight.py プロジェクト: huiling110/tth-htt
    "The most frequent LHE weight is: {:.6e}".format(most_frequent_weight))
with open(output_file, 'w') as output_file_ptr:
    output_file_ptr.write('{:.6e}'.format(most_frequent_weight))
logging.info("Wrote output file: {}".format(output_file))

if plot_files:
    has_neg_weights = any(weight_freq[0] < 0
                          for weight_freq in weights_by_frequency)
    binning_max = MAX_CUTOFF * most_frequent_weight
    binning_min = -MAX_CUTOFF * most_frequent_weight
    if not has_neg_weights:
        binning_min = max(0., binning_min)
    binning = array.array(
        'f', list(np.linspace(binning_min, binning_max, PLOT_BINS + 1)))

    histogram = ROOT.TH1D(GENWEIGHT_NAME, GENWEIGHT_NAME, PLOT_BINS, binning)
    for weight_freq in weights_by_frequency:
        histogram.Fill(weight_freq[0], weight_freq[1])
    histogram.GetXaxis().SetRange(0, histogram.GetNbinsX() + 1)

    for plot_file in plot_files:
        title = os.path.splitext(os.path.basename(plot_file))[0]
        histogram.SetTitle(title)

        canvas = ROOT.TCanvas()
        canvas.SetCanvasSize(1000, 800)
        canvas.SetLogy(True)
        histogram.Draw('hist')
        canvas.SaveAs(plot_file)
        canvas.Close()
        del canvas
コード例 #5
0
            for sample_idx, sample_name in enumerate(results.keys()):
                logging.info("Creating plots for sample {} ({}/{})".format(
                    sample_name, sample_idx + 1, nof_samples))
                plot_output(pdf)

out_fptr = ROOT.TFile.Open(output, 'recreate')
for sample_key in results:
    logging.info("Saving histograms for process {}".format(sample_key))
    sample_dir = out_fptr.mkdir(sample_key)
    sample_dir.cd()
    assert ('central' in results[sample_key])
    for sys_key in results[sample_key]:
        if central_only and sys_key != 'central':
            continue
        if not sys_key or sys_key.startswith('envelope'):
            continue
        ratios = results[sample_key][sys_key]['ratio']['count']
        ratio_errs = results[sample_key][sys_key]['ratio']['err']
        nbins = len(ratios)
        xbins = array.array('f', range(nbins))
        histogram = ROOT.TH1D(sys_key, sys_key, len(xbins) - 1, xbins)
        histogram.SetDirectory(sample_dir)
        histogram.SetXTitle('# preselected jets')
        histogram.SetTitle('{} ({})'.format(sample_key, sys_key))
        for bin_idx in range(nbins):
            histogram.SetBinContent(bin_idx + 1, ratios[bin_idx])
            histogram.SetBinError(bin_idx + 1, ratio_errs[bin_idx])
        histogram.Write()
        del histogram
out_fptr.Close()