Esempio n. 1
0
def test_draw():

    with root_open(FILE_PATHS[0]) as f:
        tree = f.tree

        tree.draw('a_x')
        tree.draw('a_x:a_y')
        tree.draw('a_x:TMath::Exp(a_y)')
        tree.draw('a_x:a_y:a_z')
        tree.draw('a_x:a_y:a_z:b_x')
        tree.draw('a_x:a_y:a_z:b_x:b_y', options='para')

        h1 = Hist(10, -1, 2, name='h1')
        h2 = Hist2D(10, -1, 2, 10, -1, 2)
        h3 = Hist3D(10, -1, 2, 10, -1, 2, 10, -1, 2)

        # dimensionality does not match
        assert_raises(TypeError, tree.draw, 'a_x:a_y', hist=h1)

        # name does not match
        assert_raises(ValueError, tree.draw, 'a_x>>+something', hist=h1)

        # hist is not a TH1
        assert_raises(TypeError, tree.draw, 'a_x:a_y', hist=ROOT.TGraph())

        # name does match and is fine (just redundant)
        tree.draw('a_x>>h1', hist=h1)
        assert_equal(h1.Integral() > 0, True)
        h1.Reset()
        tree.draw('a_x>>+h1', hist=h1)
        assert_equal(h1.Integral() > 0, True)
        h1.Reset()

        # both binning and hist are specified
        assert_raises(ValueError, tree.draw, 'a_x>>+h1(10, 0, 1)', hist=h1)

        tree.draw('a_x', hist=h1)
        assert_equal(h1.Integral() > 0, True)
        tree.draw('a_x:a_y', hist=h2)
        assert_equal(h2.Integral() > 0, True)
        tree.draw('a_x:a_y:a_z', hist=h3)
        assert_equal(h3.Integral() > 0, True)

        h3.Reset()
        tree.draw('a_x>0:a_y/2:a_z*2', hist=h3)
        assert_equal(h3.Integral() > 0, True)

        # create a histogram
        hist = tree.draw('a_x:a_y:a_z', create_hist=True)
        assert_equal(hist.Integral() > 0, True)

        hist = tree.draw('a_x:a_y:a_z>>new_hist_1')
        assert_equal(hist.Integral() > 0, True)
        assert_equal(hist.name, 'new_hist_1')

        # create_hist=True is redundant here
        hist = tree.draw('a_x:a_y:a_z>>new_hist_2', create_hist=True)
        assert_equal(hist.Integral() > 0, True)
        assert_equal(hist.name, 'new_hist_2')
Esempio n. 2
0
def check_events(analysis, sample, category, region):
    clf = analysis.get_clf(category, mass=125, load=True)
    scores, weights = sample.scores(
        clf, category, region,
        systematics=False)['NOMINAL']
    rec = sample.merged_records(
        category, region)
    sample_events = sample.events(category, region)[1].value
    hist = Hist(5, scores.min() - 1, scores.max() + 1)
    fill_hist(hist, scores, weights)
    clf_events = hist.integral()

    # test events consistency
    assert_equal(weights.shape[0], rec['weight'].shape[0])
    assert_array_equal(weights, rec['weight'])
    assert_almost_equal(clf_events, weights.sum(), 1)
    assert_almost_equal(sample_events, rec['weight'].sum(), 1)
    assert_almost_equal(sample_events, clf_events, 1)

    # test draw_array
    hist = Hist(1, -1000, 1000)
    sample.draw_array({'tau1_charge': hist}, category, region)
    assert_almost_equal(hist.integral(), sample_events, 1)

    # test scaling
    orig_scale = sample.scale
    sample.scale *= 2.
    scores, weights = sample.scores(
        clf, category, region,
        systematics=False)['NOMINAL']
    hist.Reset()
    fill_hist(hist, scores, weights)
    scale_clf_events = hist.integral()
    assert_almost_equal(scale_clf_events, weights.sum(), 1)
    assert_almost_equal(scale_clf_events, 2. * clf_events, 1)
    scale_sample_events = sample.events(category, region)[1].value
    assert_almost_equal(scale_sample_events, 2. * sample_events, 1)
    sample.scale = orig_scale
Esempio n. 3
0
array = np.random.randn(1E6)


def time_repeat(cmd, repeat=5, number=1):
    best_time = min(
        timeit.repeat(cmd,
                      repeat=repeat,
                      number=number,
                      setup="from __main__ import h, array, np")) / number
    print "%d loops, best of %d: %fs per loop" % (number, repeat, best_time)


print "Using Python's map()..."
cProfile.run('map(h.Fill, array)')

h.Reset()

print "time without profiler overhead:"
time_repeat('map(h.Fill, array)')

h.Reset()

print
print '=' * 40
print

print "Using NumPy's histogram..."
cProfile.run('np.histogram(array)')

h.Reset()
def unfolding_toy_diagnostics(indir, variable):

    plotter = BasePlotter(defaults={
        'clone': False,
        'name_canvas': True,
        'show_title': True,
        'save': {
            'png': True,
            'pdf': False
        }
    }, )
    styles = {
        'dots': {
            'linestyle': 0,
            'markerstyle': 21,
            'markercolor': 1
        },
        'compare': {
            'linesstyle': [1, 0],
            'markerstyle': [0, 21],
            'markercolor': [2, 1],
            'linecolor': [2, 1],
            'drawstyle': ['hist', 'pe'],
            'legendstyle': ['l', 'p']
        }
    }

    xaxislabel = set_pretty_label(variable)

    true_distribution = None

    curdir = os.getcwd()
    os.chdir(indir)
    toydirs = get_immediate_subdirectories(".")

    methods = []
    pulls_lists = {}
    pull_means_lists = {}
    pull_mean_errors_lists = {}
    pull_sums_lists = {}
    pull_sigmas_lists = {}
    pull_sigma_errors_lists = {}
    deltas_lists = {}
    delta_means_lists = {}
    delta_mean_errors_lists = {}
    delta_sigmas_lists = {}
    delta_sigma_errors_lists = {}
    ratio_sums_lists = {}
    nneg_bins_lists = {}
    unfoldeds_lists = {}
    unfolded_sigmas_lists = {}
    taus_lists = {}

    histos_created = False
    lists_created = False
    idir = 0
    true_distro = None
    #loop over toys
    for directory in toydirs:
        if not directory.startswith('toy_'): continue
        os.chdir(directory)
        log.debug('Inspecting toy %s' % directory)
        idir = idir + 1
        i = 0
        if not os.path.isfile("result_unfolding.root"):
            raise ValueError('root file not found in %s' % os.getcwd())
        with io.root_open("result_unfolding.root") as inputfile:
            log.debug('Iteration %s over the file' % i)
            i = i + 1
            if not methods:
                keys = [i.name for i in inputfile.keys()]
                for key in keys:
                    if hasattr(getattr(inputfile, key), "hdata_unfolded"):
                        methods.append(key)

            unfolded_hists = [
                inputfile.get('%s/hdata_unfolded' % i) for i in methods
            ]
            unfolded_wps_hists = [
                inputfile.get('%s/hdata_unfolded_ps_corrected' % i)
                for i in methods
            ]
            for unf, unfps, method in zip(unfolded_hists, unfolded_wps_hists,
                                          methods):
                unf.name = method
                unfps.name = method
            if true_distro is None:
                true_distribution = inputfile.true_distribution
                ROOT.TH1.AddDirectory(False)
                true_distro = true_distribution.Clone()
            taus = prettyjson.loads(inputfile.best_taus.GetTitle())
            if len(taus_lists) == 0:
                taus_lists = dict((i, []) for i in taus)
            for i, t in taus.iteritems():
                taus_lists[i].append(t)

            for histo in unfolded_hists:
                #create pull/delta containers during first iteration
                name = histo.name
                nbins = histo.nbins()
                log.debug("name = %s, n bins = %s" % (name, nbins))
                if not lists_created:
                    for ibin in range(1, nbins + 1):
                        outname = "pull_" + name + "_bin" + str(ibin)
                        pulls_lists[outname] = []
                        outname = "delta_" + name + "_bin" + str(ibin)
                        deltas_lists[outname] = []
                        outname = "unfolded_" + name + "_bin" + str(ibin)
                        unfoldeds_lists[outname] = []
                        unfolded_sigmas_lists[outname] = []
                    outname = "pull_" + name
                    pull_means_lists[outname] = {}
                    pull_mean_errors_lists[outname] = {}
                    pull_sigmas_lists[outname] = {}
                    pull_sigma_errors_lists[outname] = {}

                    outname = "delta_" + name
                    delta_means_lists[outname] = {}
                    delta_mean_errors_lists[outname] = {}
                    delta_sigmas_lists[outname] = {}
                    delta_sigma_errors_lists[outname] = {}

                for ibin in range(1, nbins + 1):
                    outname = "pull_" + name + "_bin" + str(ibin)
                    unfolded_bin_content = histo.GetBinContent(ibin)
                    unfolded_bin_error = histo.GetBinError(ibin)
                    true_bin_content = true_distro.GetBinContent(ibin)
                    true_bin_error = true_distro.GetBinError(ibin)
                    total_bin_error = math.sqrt(unfolded_bin_error**2)  #???
                    if (total_bin_error != 0):
                        pull = (unfolded_bin_content -
                                true_bin_content) / total_bin_error
                    else:
                        pull = 9999
                    log.debug(
                        'unfolded bin content %s +/- %s, true bin content %s, pull %s'
                        % (unfolded_bin_content, unfolded_bin_error,
                           true_bin_content, pull))
                    pulls_lists[outname].append(pull)
                    outname = "delta_" + name + "_bin" + str(ibin)
                    delta = unfolded_bin_content - true_bin_content
                    log.debug(
                        'unfolded bin content %s +/- %s, true bin content %s, delta %s'
                        % (unfolded_bin_content, unfolded_bin_error,
                           true_bin_content, delta))
                    deltas_lists[outname].append(delta)
                    outname = "unfolded_" + name + "_bin" + str(ibin)
                    unfoldeds_lists[outname].append(unfolded_bin_content)
                    unfolded_sigmas_lists[outname].append(unfolded_bin_error)

            nneg_bins_hists = [
                i for i in inputfile.keys()
                if i.GetName().startswith("nneg_bins")
            ]
            nneg_bins_hists = [asrootpy(i.ReadObj()) for i in nneg_bins_hists]
            for histo in nneg_bins_hists:
                #create pull/delta containers during first iteration
                name = histo.name
                nbins = histo.nbins()
                log.debug("name = %s, n bins = %s" % (name, nbins))
                if not lists_created:
                    outname = name
                    nneg_bins_lists[outname] = []
                outname = name
                nneg_bins_lists[outname].append(histo.GetBinContent(1))

            pull_sums_hists = [
                i for i in inputfile.keys()
                if i.GetName().startswith("sum_of_pulls")
            ]
            pull_sums_hists = [asrootpy(i.ReadObj()) for i in pull_sums_hists]
            for histo in pull_sums_hists:
                #create pull/delta containers during first iteration
                name = histo.name
                nbins = histo.nbins()
                log.debug("name = %s, n bins = %s" % (name, nbins))
                if not lists_created:
                    outname = name
                    pull_sums_lists[outname] = []
                outname = name
                pull_sums_lists[outname].append(histo.GetBinContent(1))

            ratio_sums_hists = [
                i for i in inputfile.keys()
                if i.GetName().startswith("sum_of_ratios")
            ]
            ratio_sums_hists = [
                asrootpy(i.ReadObj()) for i in ratio_sums_hists
            ]
            for histo in ratio_sums_hists:
                #create ratio/delta containers during first iteration
                name = histo.name
                nbins = histo.nbins()
                log.debug("name = %s, n bins = %s" % (name, nbins))
                if not lists_created:
                    outname = name
                    ratio_sums_lists[outname] = []
                outname = name
                ratio_sums_lists[outname].append(histo.GetBinContent(1))

            #after the first iteration on the file all the lists are created
            lists_created = True

        os.chdir("..")

    #create histograms
    #histo containers
    taus = {}
    for name, vals in taus_lists.iteritems():
        ROOT.TH1.AddDirectory(False)  #repeat, you never know
        val_min = min(vals)
        val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min
        val_max = max(vals)
        val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max
        if val_min == val_max:
            if tau_nbins % 2:  #if odd
                val_min, val_max = val_min - 0.01, val_min + 0.01
            else:
                brange = 0.02
                bwidth = brange / tau_nbins
                val_min, val_max = val_min - 0.01 + bwidth / 2., val_min + 0.01 + bwidth / 2.
        title = '#tau choice - %s ;#tau;N_{toys}' % (name)
        histo = Hist(tau_nbins, val_min, val_max, name=name, title=title)
        for val in vals:
            histo.Fill(val)
        taus[name] = histo

    pulls = {}
    for name, vals in pulls_lists.iteritems():
        ROOT.TH1.AddDirectory(False)  #repeat, you never know
        val_min = min(vals)
        val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min
        val_max = max(vals)
        val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max
        abs_max = max(abs(val_min), abs(val_max))
        if 'L_curve' in name:
            method = 'L_curve'
            binno = name.split('_')[-1]
        else:
            _, method, binno = tuple(name.split('_'))
        title = 'Pulls - %s - %s ;Pull;N_{toys}' % (binno, method)
        histo = Hist(pull_nbins, -abs_max, abs_max, name=name, title=title)
        for val in vals:
            histo.Fill(val)
        pulls[name] = histo

    deltas = {}
    for name, vals in deltas_lists.iteritems():
        ROOT.TH1.AddDirectory(False)  #repeat, you never know
        val_min = min(vals)
        val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min
        val_max = max(vals)
        val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max
        if 'L_curve' in name:
            method = 'L_curve'
            binno = name.split('_')[-1]
        else:
            _, method, binno = tuple(name.split('_'))
        title = 'Deltas - %s - %s ;Delta;N_{toys}' % (binno, method)
        histo = Hist(delta_nbins, val_min, val_max, name=name, title=title)
        for val in vals:
            histo.Fill(val)
        deltas[name] = histo

    unfoldeds = {}
    for name, vals in unfoldeds_lists.iteritems():
        ROOT.TH1.AddDirectory(False)  #repeat, you never know
        val_min = min(vals)
        val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min
        val_max = max(vals)
        val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max
        if 'L_curve' in name:
            method = 'L_curve'
            binno = name.split('_')[-1]
        else:
            _, method, binno = tuple(name.split('_'))
        title = 'Unfoldeds - %s - %s ;Unfolded;N_{toys}' % (binno, method)
        histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title)
        for val in vals:
            histo.Fill(val)
        unfoldeds[name] = histo

    nneg_bins = {}
    for name, vals, in nneg_bins_lists.iteritems():
        ROOT.TH1.AddDirectory(False)  #repeat, you never know
        val_min = min(vals)
        val_min = 0 if val_min > 0 else val_min - 1
        val_max = max(vals)
        val_max = 0 if val_max < 0 else val_max + 1
        if 'L_curve' in name:
            method = 'L_curve'
        else:
            set_trace()
            _, method, _ = tuple(name.split('_'))
        title = 'N of negative bins - %s ;N. neg bins;N_{toys}' % method
        histo = Hist(int(val_max - val_min + 1),
                     val_min,
                     val_max,
                     name=name,
                     title=title)
        for val in vals:
            histo.Fill(val)
        nneg_bins[name] = histo

    pull_sums = {}
    for name, vals in pull_sums_lists.iteritems():
        ROOT.TH1.AddDirectory(False)  #repeat, you never know
        val_min = min(vals)
        val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min
        val_max = max(vals)
        val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max
        if 'L_curve' in name:
            method = 'L_curve'
        else:
            set_trace()
            _, _, _, _, _, method = tuple(name.split('_'))
        title = 'Pull sums - %s ;#Sigma(pull)/N_{bins};N_{toys}' % method
        histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title)
        for val in vals:
            histo.Fill(val)
        pull_sums[name] = histo

    ratio_sums = {}
    for name, vals in ratio_sums_lists.iteritems():
        ROOT.TH1.AddDirectory(False)  #repeat, you never know
        val_min = min(vals)
        val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min
        val_max = max(vals)
        val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max
        if 'L_curve' in name:
            method = 'L_curve'
            binno = name.split('_')[-1]
        else:
            set_trace()
            _, _, _, _, _, method = tuple(name.split('_'))
        title = 'Ratio sums - %s;#Sigma(ratio)/N_{bins};N_{toys}' % method
        histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title)
        for val in vals:
            histo.Fill(val)
        ratio_sums[name] = histo

    unfolded_sigmas = {}
    for name, vals in unfolded_sigmas_lists.iteritems():
        ROOT.TH1.AddDirectory(False)  #repeat, you never know
        val_min = min(vals)
        val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min
        val_max = max(vals)
        val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max
        if 'L_curve' in name:
            method = 'L_curve'
            binno = name.split('_')[-1]
        else:
            _, method, binno = tuple(name.split('_'))
        title = 'Unfolded uncertainties - %s - %s ;Uncertainty;N_{toys}' % (
            binno, method)
        histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title)
        for val in vals:
            histo.Fill(val)
        unfolded_sigmas[name] = histo

    for name, histo in pulls.iteritems():
        log.debug("name is %s and object type is %s" % (name, type(histo)))
        histo.Fit("gaus", 'Q')
        if not histo.GetFunction("gaus"):
            log.warning("Function not found for histogram %s" % name)
            continue
        mean = histo.GetFunction("gaus").GetParameter(1)
        meanError = histo.GetFunction("gaus").GetParError(1)
        sigma = histo.GetFunction("gaus").GetParameter(2)
        sigmaError = histo.GetFunction("gaus").GetParError(2)

        general_name, idx = tuple(name.split('_bin'))
        idx = int(idx)

        pull_means_lists[general_name][idx] = mean
        pull_mean_errors_lists[general_name][idx] = meanError
        pull_sigmas_lists[general_name][idx] = sigma
        pull_sigma_errors_lists[general_name][idx] = sigmaError

    for name, histo in deltas.iteritems():
        log.debug("name is %s and object type is %s" % (name, type(histo)))
        histo.Fit("gaus", 'Q')
        if not histo.GetFunction("gaus"):
            log.warning("Function not found for histogram %s" % name)
            continue
        mean = histo.GetFunction("gaus").GetParameter(1)
        meanError = histo.GetFunction("gaus").GetParError(1)
        sigma = histo.GetFunction("gaus").GetParameter(2)
        sigmaError = histo.GetFunction("gaus").GetParError(2)

        general_name, idx = tuple(name.split('_bin'))
        idx = int(idx)

        delta_means_lists[general_name][idx] = mean
        delta_mean_errors_lists[general_name][idx] = meanError
        delta_sigmas_lists[general_name][idx] = sigma
        delta_sigma_errors_lists[general_name][idx] = sigmaError

    outfile = rootpy.io.File("unfolding_diagnostics.root", "RECREATE")
    outfile.cd()

    pull_means = {}
    pull_sigmas = {}
    pull_means_summary = {}
    pull_sigmas_summary = {}
    delta_means = {}
    delta_sigmas = {}
    delta_means_summary = {}
    delta_sigmas_summary = {}

    for outname, pmeans in pull_means_lists.iteritems():
        outname_mean = outname + "_mean"
        outtitle = "Pull means - " + outname + ";Pull mean; N_{toys}"
        pull_mean_min = min(pmeans.values())
        pull_mean_max = max(pmeans.values())
        pull_mean_newmin = pull_mean_min - (pull_mean_max -
                                            pull_mean_min) * 0.5
        pull_mean_newmax = pull_mean_max + (pull_mean_max -
                                            pull_mean_min) * 0.5
        pull_means[outname] = plotting.Hist(pull_mean_nbins,
                                            pull_mean_newmin,
                                            pull_mean_newmax,
                                            name=outname_mean,
                                            title=outtitle)

        outname_mean_summary = outname + "_mean_summary"
        outtitle_mean_summary = "Pull mean summary - " + outname
        histocloned = true_distro.Clone(outname_mean_summary)
        histocloned.Reset()
        histocloned.xaxis.title = xaxislabel
        histocloned.yaxis.title = 'Pull mean'
        histocloned.title = outtitle_mean_summary
        pull_means_summary[outname] = histocloned

        for idx, pmean in pmeans.iteritems():
            pull_means[outname].Fill(pmean)
            histocloned[idx].value = pmean
            histocloned[idx].error = pull_mean_errors_lists[outname][idx]
        histocloned.yaxis.SetRangeUser(min(pmeans.values()),
                                       max(pmeans.values()))

    for outname, psigmas in pull_sigmas_lists.iteritems():
        outname_sigma = outname + "_sigma"
        outtitle_sigma = "Pull #sigma's - " + outname + ";Pull #sigma; N_{toys}"
        pull_sigma_min = min(psigmas.values())
        pull_sigma_max = max(psigmas.values())
        pull_sigma_newmin = pull_sigma_min - (pull_sigma_max -
                                              pull_sigma_min) * 0.5
        pull_sigma_newmax = pull_sigma_max + (pull_sigma_max -
                                              pull_sigma_min) * 0.5
        pull_sigmas[outname] = plotting.Hist(pull_sigma_nbins,
                                             pull_sigma_newmin,
                                             pull_sigma_newmax,
                                             name=outname_sigma,
                                             title=outtitle_sigma)

        outname_sigma_summary = outname + "_sigma_summary"
        outtitle_sigma_summary = "Pull #sigma summary - " + outname
        histocloned = true_distro.Clone(outname_sigma_summary)
        histocloned.Reset()
        histocloned.xaxis.title = xaxislabel
        histocloned.yaxis.title = 'Pull #sigma'
        histocloned.title = outtitle_sigma_summary
        pull_sigmas_summary[outname] = histocloned

        for idx, psigma in psigmas.iteritems():
            pull_sigmas[outname].Fill(psigma)
            histocloned[idx].value = psigma
            histocloned[idx].error = pull_sigma_errors_lists[outname][idx]
        histocloned.yaxis.SetRangeUser(min(psigmas.values()),
                                       max(psigmas.values()))

    for outname, dmeans in delta_means_lists.iteritems():
        outname_mean = outname + "_mean"
        outtitle = "Delta means - " + outname + ";Delta mean; N_{toys}"
        delta_mean_min = min(dmeans.values())
        delta_mean_max = max(dmeans.values())
        delta_mean_newmin = delta_mean_min - (delta_mean_max -
                                              delta_mean_min) * 0.5
        delta_mean_newmax = delta_mean_max + (delta_mean_max -
                                              delta_mean_min) * 0.5
        delta_means[outname] = plotting.Hist(delta_mean_nbins,
                                             delta_mean_newmin,
                                             delta_mean_newmax,
                                             name=outname_mean,
                                             title=outtitle)

        outname_mean_summary = outname + "_mean_summary"
        outtitle_mean_summary = "Delta mean summary - " + outname
        histocloned = true_distro.Clone(outname_mean_summary)
        histocloned.Reset()
        histocloned.xaxis.title = xaxislabel
        histocloned.yaxis.title = 'Delta mean'
        histocloned.title = outtitle_mean_summary
        delta_means_summary[outname] = histocloned

        for idx, dmean in dmeans.iteritems():
            delta_means[outname].Fill(dmean)
            histocloned[idx].value = dmean
            histocloned[idx].error = delta_mean_errors_lists[outname][idx]
        histocloned.yaxis.SetRangeUser(min(dmeans.values()),
                                       max(dmeans.values()))

    for outname, dsigmas in delta_sigmas_lists.iteritems():
        outname_sigma = outname + "_sigma"
        outtitle_sigma = "Delta #sigma's - " + outname + ";Delta #sigma; N_{toys}"
        delta_sigma_min = min(dsigmas.values())
        delta_sigma_max = max(dsigmas.values())
        delta_sigma_newmin = delta_sigma_min - (delta_sigma_max -
                                                delta_sigma_min) * 0.5
        delta_sigma_newmax = delta_sigma_max + (delta_sigma_max -
                                                delta_sigma_min) * 0.5
        delta_sigmas[outname] = plotting.Hist(delta_sigma_nbins,
                                              delta_sigma_newmin,
                                              delta_sigma_newmax,
                                              name=outname_sigma,
                                              title=outtitle_sigma)

        outname_sigma_summary = outname + "_sigma_summary"
        outtitle_sigma_summary = "Delta #sigma summary - " + outname
        histocloned = true_distro.Clone(outname_sigma_summary)
        histocloned.Reset()
        histocloned.xaxis.title = xaxislabel
        histocloned.yaxis.title = 'Delta #sigma'
        histocloned.title = outtitle_sigma_summary
        delta_sigmas_summary[outname] = histocloned

        for idx, dsigma in dsigmas.iteritems():
            delta_sigmas[outname].Fill(dsigma)
            histocloned[idx].value = dsigma
            histocloned[idx].error = delta_sigma_errors_lists[outname][idx]
        histocloned.yaxis.SetRangeUser(min(dsigmas.values()),
                                       max(dsigmas.values()))

    unfolded_summary = {}
    unfolded_average = {}
    unfolded_envelope = {}
    for name, histo in unfoldeds.iteritems():
        log.debug("name is %s and object type is %s" % (name, type(histo)))
        histo.Fit("gaus", 'Q')
        if not histo.GetFunction("gaus"):
            log.warning("Function not found for histogram %s" % name)
            continue
        mean = histo.GetFunction("gaus").GetParameter(1)
        meanError = histo.GetFunction("gaus").GetParError(1)
        sigma = histo.GetFunction("gaus").GetParameter(2)
        sigmaError = histo.GetFunction("gaus").GetParError(2)

        general_name, idx = tuple(name.split('_bin'))
        idx = int(idx)

        if general_name not in unfolded_summary:
            histo = true_distro.Clone("%s_unfolded_summary" % general_name)
            outtitle_unfolded_summary = "Unfolded summary - " + general_name
            histo.Reset()
            histo.xaxis.title = xaxislabel
            histo.yaxis.title = 'N_{events}'
            histo.title = outtitle_unfolded_summary
            unfolded_summary[general_name] = histo

            unfolded_envelope[general_name] = histo.Clone(
                "%s_unfolded_envelope" % general_name)
            unfolded_average[general_name] = histo.Clone(
                "%s_unfolded_average" % general_name)

        unfolded_summary[general_name][idx].value = mean
        unfolded_summary[general_name][idx].error = meanError

        unfolded_envelope[general_name][idx].value = mean
        unfolded_envelope[general_name][idx].error = sigma

        unfolded_average[general_name][idx].value = mean
        unfolded_average[general_name][idx].error = \
           unfolded_sigmas['%s_bin%i' % (general_name, idx)].GetMean()

    plotter.set_subdir('taus')
    for name, histo in taus.iteritems():
        #canvas = plotter.create_and_write_canvas_single(0, 21, 1, False, False, histo, write=False)
        plotter.canvas.cd()
        histo = plotter.plot(histo, **styles['dots'])
        histo.SetStats(True)

        info = plotter.make_text_box(
            'mode #tau = %.5f' % histo[histo.GetMaximumBin()].x.center,
            position=(plotter.pad.GetLeftMargin(), plotter.pad.GetTopMargin(),
                      0.3, 0.025))
        info.Draw()

        plotter.save()
        histo.Write()
        plotter.canvas.Write()

    plotter.set_subdir('pulls')
    for name, histo in pulls.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()
    for name, histo in pull_means.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.Write()
        plotter.save()
    for name, histo in pull_sigmas.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.Write()
        plotter.save()

    plotter.set_subdir('pull_summaries')
    for name, histo in pull_means_summary.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        #histo.SetStats(True)
        line = ROOT.TLine(histo.GetBinLowEdge(1), 0,
                          histo.GetBinLowEdge(histo.GetNbinsX() + 1), 0)
        line.Draw("same")
        plotter.save()
        histo.Write()
        plotter.canvas.Write()
    for name, histo in pull_sigmas_summary.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        #histo.SetStats(True)
        line = ROOT.TLine(histo.GetBinLowEdge(1), 1,
                          histo.GetBinLowEdge(histo.GetNbinsX() + 1), 1)
        line.Draw("same")
        plotter.save()
        histo.Write()
        plotter.canvas.Write()

    plotter.set_subdir('deltas')
    for name, histo in deltas.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()
    for name, histo in delta_means.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.Write()
        plotter.save()
    for name, histo in delta_sigmas.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.Write()
        plotter.save()

    plotter.set_subdir('delta_summaries')
    for name, histo in delta_means_summary.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        #histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()
    for name, histo in delta_sigmas_summary.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        #histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()

    plotter.set_subdir('unfolding_unc')
    for name, histo in unfolded_sigmas.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()

    plotter.set_subdir('unfolded')
    for name, histo in unfoldeds.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()

    plotter.set_subdir('unfolded_summaries')
    for name, histo in unfolded_summary.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()

    for name, histo in unfolded_summary.iteritems():
        leg = LegendDefinition("Unfolding comparison",
                               'NE',
                               labels=['Truth', 'Unfolded'])
        plotter.overlay_and_compare([true_distro],
                                    histo,
                                    legend_def=leg,
                                    **styles['compare'])
        plotter.canvas.name = 'Pull_' + name
        plotter.save()
        plotter.canvas.Write()
        plotter.overlay_and_compare([true_distro],
                                    histo,
                                    legend_def=leg,
                                    method='ratio',
                                    **styles['compare'])
        plotter.canvas.name = 'Ratio_' + name
        plotter.save()
        plotter.canvas.Write()

    plotter.set_subdir('unfolded_average')
    for name, histo in unfolded_average.iteritems():
        leg = LegendDefinition("Unfolding comparison",
                               'NE',
                               labels=['Truth', 'Unfolded'])
        #set_trace()
        plotter.overlay_and_compare([true_distro],
                                    histo,
                                    legend_def=leg,
                                    **styles['compare'])
        plotter.canvas.name = 'Pull_' + name
        plotter.save()
        plotter.canvas.Write()
        plotter.overlay_and_compare([true_distro],
                                    histo,
                                    legend_def=leg,
                                    method='ratio',
                                    **styles['compare'])
        plotter.canvas.name = 'Ratio_' + name
        plotter.save()
        plotter.canvas.Write()

    plotter.set_subdir('unfolded_envelope')
    for name, histo in unfolded_envelope.iteritems():
        leg = LegendDefinition("Unfolding comparison",
                               'NE',
                               labels=['Truth', 'Unfolded'])
        plotter.overlay_and_compare([true_distro],
                                    histo,
                                    legend_def=leg,
                                    **styles['compare'])
        plotter.canvas.name = 'Pull_' + name
        plotter.save()
        plotter.canvas.Write()
        plotter.overlay_and_compare([true_distro],
                                    histo,
                                    legend_def=leg,
                                    method='ratio',
                                    **styles['compare'])
        plotter.canvas.name = 'Ratio_' + name
        plotter.save()
        plotter.canvas.Write()

    plotter.set_subdir('figures_of_merit')
    for name, histo in nneg_bins.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()
    for name, histo in pull_sums.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()
    for name, histo in ratio_sums.iteritems():
        histo = plotter.plot(histo, **styles['dots'])
        histo.SetStats(True)
        plotter.save()
        histo.Write()
        plotter.canvas.Write()

    outfile.close()
    os.chdir(curdir)
Esempio n. 5
0
def main():
    if len(sys.argv) < 3:
        print("Usage: ToyMC [numberEvents] [randomSeed]")
        return
    numberEvents = int(sys.argv[1])
    seed = int(sys.argv[2])
    print(
        "==================================== TRAIN ===================================="
    )

    f = root_open(
        "legotrain_350_20161117-2106_LHCb4_fix_CF_pPb_MC_ptHardMerged.root", "read"
    )
    hJetPt = f.Get("AliJJetJtTask/AliJJetJtHistManager/JetPt/JetPtNFin{:02d}".format(2))
    hZ = f.Get("AliJJetJtTask/AliJJetJtHistManager/Z/ZNFin{:02d}".format(2))

    FillFakes = False
    dummy_variable = True
    weight = True

    NBINS = 50
    LimL = 0.1
    LimH = 500
    logBW = (TMath.Log(LimH) - TMath.Log(LimL)) / NBINS
    LogBinsX = [LimL * math.exp(ij * logBW) for ij in range(0, NBINS + 1)]

    hJetPtMeas = Hist(LogBinsX)
    hJetPtTrue = Hist(LogBinsX)

    myRandom = TRandom3(seed)
    fEff = TF1("fEff", "1-0.5*exp(-x)")
    jetBinBorders = [5, 10, 20, 30, 40, 60, 80, 100, 150, 500]
    hJetPtMeasCoarse = Hist(jetBinBorders)
    hJetPtTrueCoarse = Hist(jetBinBorders)

    NBINSJt = 64
    low = 0.01
    high = 10
    BinW = (TMath.Log(high) - TMath.Log(low)) / NBINSJt
    LogBinsJt = [low * math.exp(i * BinW) for i in range(NBINSJt + 1)]
    hJtTrue = Hist(LogBinsJt)
    hJtMeas = Hist(LogBinsJt)
    hJtFake = Hist(LogBinsJt)
    LogBinsPt = jetBinBorders
    jetPtBins = [(a, b) for a, b in zip(jetBinBorders, jetBinBorders[1:])]

    hJtTrue2D = Hist2D(LogBinsJt, LogBinsPt)
    hJtMeas2D = Hist2D(LogBinsJt, LogBinsPt)
    hJtFake2D = Hist2D(LogBinsJt, LogBinsPt)
    hJtMeasBin = [Hist(LogBinsJt) for i in jetBinBorders]
    hJtTrueBin = [Hist(LogBinsJt) for i in jetBinBorders]

    response = RooUnfoldResponse(hJtMeas, hJtTrue)
    response2D = RooUnfoldResponse(hJtMeas2D, hJtTrue2D)
    responseBin = [RooUnfoldResponse(hJtMeas, hJtTrue) for i in jetBinBorders]
    responseJetPt = RooUnfoldResponse(hJetPtMeas, hJetPtTrue)
    responseJetPtCoarse = RooUnfoldResponse(hJetPtMeasCoarse, hJetPtTrueCoarse)

    # Histogram index is jet pT index, Bin 0 is 5-10 GeV
    # Histogram X axis is observed jT, Bin 0 is underflow
    # Histogram Y axis is observed jet Pt, Bin 0 is underflow
    # Histogram Z axis is True jT, Bin 0 is underflow
    responses = [Hist3D(LogBinsJt, LogBinsPt, LogBinsJt) for i in jetPtBins]
    misses = Hist2D(LogBinsJt, LogBinsPt)
    fakes2D = Hist2D(LogBinsJt, LogBinsPt)
    outFile = TFile("tuple.root", "recreate")
    responseTuple = TNtuple(
        "responseTuple", "responseTuple", "jtObs:ptObs:jtTrue:ptTrue"
    )

    hMultiTrue = Hist(50, 0, 50)
    hMultiMeas = Hist(50, 0, 50)
    hZMeas = Hist(50, 0, 1)
    hZTrue = Hist(50, 0, 1)
    hZFake = Hist(50, 0, 1)
    responseMatrix = Hist2D(LogBinsJt, LogBinsJt)
    numberJets = 0
    numberFakes = 0
    numberJetsMeasBin = [0 for i in jetBinBorders]
    numberJetsTrueBin = [0 for i in jetBinBorders]
    numberFakesBin = [0 for i in jetBinBorders]
    ieout = numberEvents / 10
    if ieout > 10000:
        ieout = 10000
    fakeRate = 1
    start_time = datetime.now()
    print("Processing Training Events")
    for ievt in range(numberEvents):
        tracksTrue = []
        tracksMeas = [0 for x in range(100)]
        if ievt % ieout == 0 and ievt > 0:
            time_elapsed = datetime.now() - start_time
            time_left = timedelta(
                seconds=time_elapsed.total_seconds()
                * 1.0
                * (numberEvents - ievt)
                / ievt
            )
            print(
                "Event {} [{:.2f}%] Time Elapsed: {} ETA: {}".format(
                    ievt,
                    100.0 * ievt / numberEvents,
                    fmtDelta(time_elapsed),
                    fmtDelta(time_left),
                )
            )
        jetTrue = TVector3(0, 0, 0)
        jetMeas = TVector3(0, 0, 0)
        jetPt = hJetPt.GetRandom()
        remainder = jetPt
        if jetPt < 5:
            continue
        nt = 0
        nt_meas = 0
        while remainder > 0:
            trackPt = hZ.GetRandom() * jetPt
            if trackPt < remainder:
                track = TVector3()
                remainder = remainder - trackPt
            else:
                trackPt = remainder
                remainder = -1
            if trackPt > 0.15:
                track.SetPtEtaPhi(
                    trackPt, myRandom.Gaus(0, 0.1), myRandom.Gaus(math.pi, 0.2)
                )
                tracksTrue.append(track)
                jetTrue += track
                if fEff.Eval(trackPt) > myRandom.Uniform(0, 1):
                    tracksMeas[nt] = 1
                    jetMeas += track
                    nt_meas += 1
                else:
                    tracksMeas[nt] = 0
                nt += 1
        fakes = []
        for it in range(fakeRate * 100):
            if myRandom.Uniform(0, 1) > 0.99:
                fake = TVector3()
                fake.SetPtEtaPhi(
                    myRandom.Uniform(0.15, 1),
                    myRandom.Gaus(0, 0.1),
                    myRandom.Gaus(math.pi, 0.2),
                )
                fakes.append(fake)
                jetMeas += fake

        hJetPtMeas.Fill(jetMeas.Pt())
        hJetPtTrue.Fill(jetTrue.Pt())
        responseJetPt.Fill(jetMeas.Pt(), jetTrue.Pt())
        responseJetPtCoarse.Fill(jetMeas.Pt(), jetTrue.Pt())
        hMultiTrue.Fill(nt)
        hMultiMeas.Fill(nt_meas)
        ij_meas = GetBin(jetBinBorders, jetMeas.Pt())
        ij_true = GetBin(jetBinBorders, jetTrue.Pt())
        if nt < 5 or nt_meas < 5:
            continue
        numberJets += 1
        if ij_meas >= 0:
            numberJetsMeasBin[ij_meas] += 1
            hJetPtMeasCoarse.Fill(jetMeas.Pt())
        if ij_true >= 0:
            numberJetsTrueBin[ij_true] += 1
            hJetPtTrueCoarse.Fill(jetTrue.Pt())
        for track, it in zip(tracksTrue, range(100)):
            zTrue = (track * jetTrue.Unit()) / jetTrue.Mag()
            jtTrue = (track - scaleJet(jetTrue, zTrue)).Mag()
            hZTrue.Fill(zTrue)
            if ij_true >= 0:
                if weight:
                    hJtTrue.Fill(jtTrue, 1.0 / jtTrue)
                    hJtTrueBin[ij_true].Fill(jtTrue, 1.0 / jtTrue)
                    hJtTrue2D.Fill(jtTrue, jetTrue.Pt(), 1.0 / jtTrue)
                else:
                    hJtTrue.Fill(jtTrue)
                    hJtTrueBin[ij_true].Fill(jtTrue)
                    hJtTrue2D.Fill(jtTrue, jetTrue.Pt())
            if ij_meas >= 0:
                if tracksMeas[it] == 1:
                    zMeas = (track * jetMeas.Unit()) / jetMeas.Mag()
                    jtMeas = (track - scaleJet(jetMeas, zMeas)).Mag()
                    hZMeas.Fill(zMeas)
                    if weight:
                        hJtMeasBin[ij_meas].Fill(jtMeas, 1.0 / jtMeas)
                        hJtMeas.Fill(jtMeas, 1.0 / jtMeas)
                        hJtMeas2D.Fill(jtMeas, jetMeas.Pt(), 1.0 / jtMeas)
                    else:
                        hJtMeas.Fill(jtMeas)
                        hJtMeasBin[ij_meas].Fill(jtMeas)
                        hJtMeas2D.Fill(jtMeas, jetMeas.Pt())
                    response.Fill(jtMeas, jtTrue)
                    responseBin[ij_true].Fill(jtMeas, jtTrue)
                    response2D.Fill(jtMeas, jetMeas.Pt(), jtTrue, jetTrue.Pt())
                    responseMatrix.Fill(jtMeas, jtTrue)
                    responses[ij_true].Fill(jtMeas, jetMeas.Pt(), jtTrue)
                    responseTuple.Fill(jtMeas, jetMeas.Pt(), jtTrue, jetTrue.Pt())
                else:
                    response.Miss(jtTrue)
                    responseBin[ij_true].Miss(jtTrue)
                    response2D.Miss(jtTrue, jetTrue.Pt())
                    misses.Fill(jtTrue, jetTrue.Pt())
                    responseTuple.Fill(-1, -1, jtTrue, jetTrue.Pt())
        if ij_meas >= 0:
            for fake in fakes:
                zFake = (fake * jetMeas.Unit()) / jetMeas.Mag()
                jtFake = (fake - scaleJet(jetMeas, zFake)).Mag()
                hZMeas.Fill(zFake)
                hZFake.Fill(zFake)
                if weight:
                    hJtMeas.Fill(jtFake, 1.0 / jtFake)
                    hJtMeasBin[ij_meas].Fill(jtFake, 1.0 / jtFake)
                    hJtMeas2D.Fill(jtFake, jetMeas.Pt(), 1.0 / jtFake)
                    hJtFake2D.Fill(jtFake, jetMeas.Pt(), 1.0 / jtFake)
                    hJtFake.Fill(jtFake, 1.0 / jtFake)
                else:
                    hJtMeas.Fill(jtFake)
                    hJtMeasBin[ij_meas].Fill(jtFake)
                    hJtMeas2D.Fill(jtFake, jetMeas.Pt())
                    hJtFake2D.Fill(jtFake, jetMeas.Pt())
                    hJtFake.Fill(jtFake)
                if FillFakes:
                    response.Fake(jtFake)
                    responseBin[ij_true].Fake(jtFake)
                    response2D.Fake(jtFake, jetMeas.Pt())
                    fakes2D.Fill(jtFake, jetMeas.Pt())
                    responseTuple.Fill(jtFake, jetMeas.Pt(), -1, -1)
                    numberFakes += 1
                    numberFakesBin[ij_true] += 1

    response2Dtest = make2Dresponse(
        responses, jetPtBins, hJtMeas2D, hJtTrue2D, misses=misses, fakes=fakes2D
    )

    if dummy_variable:
        hJetPtMeas.Reset()
        hJetPtTrue.Reset()
        hMultiTrue.Reset()
        hMultiMeas.Reset()
        hJetPtMeasCoarse.Reset()
        hJetPtTrueCoarse.Reset()
        hZTrue.Reset()
        hZMeas.Reset()
        hJtTrue.Reset()
        hJtTrue2D.Reset()
        hJtMeas.Reset()
        hJtMeas2D.Reset()
        hJtFake.Reset()
        hJtFake2D.Reset()
        for h, h2 in zip(hJtTrueBin, hJtMeasBin):
            h.Reset()
            h2.Reset()
        numberJetsMeasBin = [0 for i in jetBinBorders]
        numberJetsTrueBin = [0 for i in jetBinBorders]
        numberJets = 0
        print("Create testing data")
        start_time = datetime.now()
        numberEvents = numberEvents / 2
        for ievt in range(numberEvents):
            tracksTrue = []
            tracksMeas = [0 for x in range(100)]
            if ievt % ieout == 0 and ievt > 0:
                time_elapsed = datetime.now() - start_time
                time_left = timedelta(
                    seconds=time_elapsed.total_seconds()
                    * 1.0
                    * (numberEvents - ievt)
                    / ievt
                )
                print(
                    "Event {} [{:.2f}%] Time Elapsed: {} ETA: {}".format(
                        ievt,
                        100.0 * ievt / numberEvents,
                        fmtDelta(time_elapsed),
                        fmtDelta(time_left),
                    )
                )
            jetTrue = TVector3(0, 0, 0)
            jetMeas = TVector3(0, 0, 0)
            jetPt = hJetPt.GetRandom()
            remainder = jetPt
            if jetPt < 5:
                continue
            nt = 0
            nt_meas = 0
            while remainder > 0:
                trackPt = hZ.GetRandom() * jetPt
                if trackPt < remainder:
                    track = TVector3()
                    remainder = remainder - trackPt
                else:
                    trackPt = remainder
                    remainder = -1
                if trackPt > 0.15:
                    track.SetPtEtaPhi(
                        trackPt, myRandom.Gaus(0, 0.1), myRandom.Gaus(math.pi, 0.2)
                    )
                    tracksTrue.append(track)
                    jetTrue += track
                    if fEff.Eval(trackPt) > myRandom.Uniform(0, 1):
                        tracksMeas[nt] = 1
                        jetMeas += track
                        nt_meas += 1
                    else:
                        tracksMeas[nt] = 0
                    nt += 1
            fakes = []
            for it in range(fakeRate * 100):
                if myRandom.Uniform(0, 1) > 0.99:
                    fake = TVector3()
                    fake.SetPtEtaPhi(
                        myRandom.Uniform(0.15, 1),
                        myRandom.Gaus(0, 0.1),
                        myRandom.Gaus(math.pi, 0.2),
                    )
                    fakes.append(fake)
                    jetMeas += fake
            hJetPtMeas.Fill(jetMeas.Pt())
            hJetPtTrue.Fill(jetTrue.Pt())
            hMultiTrue.Fill(nt)
            hMultiMeas.Fill(nt_meas)
            ij_meas = GetBin(jetBinBorders, jetMeas.Pt())
            ij_true = GetBin(jetBinBorders, jetTrue.Pt())
            if nt < 5 or nt_meas < 5:
                continue
            numberJets += 1
            if ij_meas >= 0:
                numberJetsMeasBin[ij_meas] += 1
                hJetPtMeasCoarse.Fill(jetMeas.Pt())
            if ij_true >= 0:
                numberJetsTrueBin[ij_true] += 1
                hJetPtTrueCoarse.Fill(jetTrue.Pt())
            for track, it in zip(tracksTrue, range(100)):
                zTrue = (track * jetTrue.Unit()) / jetTrue.Mag()
                jtTrue = (track - scaleJet(jetTrue, zTrue)).Mag()
                hZTrue.Fill(zTrue)
                if ij_true >= 0:
                    if weight:
                        hJtTrue.Fill(jtTrue, 1.0 / jtTrue)
                        hJtTrueBin[ij_true].Fill(jtTrue, 1.0 / jtTrue)
                        hJtTrue2D.Fill(jtTrue, jetTrue.Pt(), 1.0 / jtTrue)
                    else:
                        hJtTrue.Fill(jtTrue)
                        hJtTrueBin[ij_true].Fill(jtTrue)
                        hJtTrue2D.Fill(jtTrue, jetTrue.Pt())
                if ij_meas >= 0:
                    if tracksMeas[it] == 1:
                        zMeas = (track * jetMeas.Unit()) / jetMeas.Mag()
                        jtMeas = (track - scaleJet(jetMeas, zMeas)).Mag()
                        hZMeas.Fill(zMeas)
                        if weight:
                            hJtMeasBin[ij_meas].Fill(jtMeas, 1.0 / jtMeas)
                            hJtMeas.Fill(jtMeas, 1.0 / jtMeas)
                            hJtMeas2D.Fill(jtMeas, jetMeas.Pt(), 1.0 / jtMeas)
                        else:
                            hJtMeas.Fill(jtMeas)
                            hJtMeasBin[ij_meas].Fill(jtMeas)
                            hJtMeas2D.Fill(jtMeas, jetMeas.Pt())
            if ij_meas >= 0:
                for fake in fakes:
                    zFake = (fake * jetMeas.Unit()) / jetMeas.Mag()
                    jtFake = (fake - scaleJet(jetMeas, zFake)).Mag()
                    hZMeas.Fill(zFake)
                    hZFake.Fill(zFake)
                    if weight:
                        hJtMeas.Fill(jtFake, 1.0 / jtFake)
                        hJtMeasBin[ij_meas].Fill(jtFake, 1.0 / jtFake)
                        hJtMeas2D.Fill(jtFake, jetMeas.Pt(), 1.0 / jtFake)
                        hJtFake2D.Fill(jtFake, jetMeas.Pt(), 1.0 / jtFake)
                        hJtFake.Fill(jtFake, 1.0 / jtFake)
                    else:
                        hJtMeas.Fill(jtFake)
                        hJtMeasBin[ij_meas].Fill(jtFake)
                        hJtMeas2D.Fill(jtFake, jetMeas.Pt())
                        hJtFake2D.Fill(jtFake, jetMeas.Pt())
                        hJtFake.Fill(jtFake)

    time_elapsed = datetime.now() - start_time
    print(
        "Event {} [{:.2f}%] Time Elapsed: {}".format(
            numberEvents, 100.0, fmtDelta(time_elapsed)
        )
    )
    if not FillFakes:
        hJtMeas.Add(hJtFake, -1)
        hJtMeas2D.Add(hJtFake2D, -1)
    responseTuple.Print()
    outFile.Write()
    #  printTuple(responseTuple)

    hJtMeasProjBin = [
        makeHist(hJtMeas2D.ProjectionX("histMeas{}".format(i), i, i), bins=LogBinsJt)
        for i in range(1, len(jetBinBorders))
    ]
    hJtMeasProj = makeHist(hJtMeas2D.ProjectionX("histMeas"), bins=LogBinsJt)
    hJtTrueProjBin = [
        makeHist(hJtTrue2D.ProjectionX("histTrue{}".format(i), i, i), bins=LogBinsJt)
        for i in range(1, len(jetBinBorders))
    ]
    hJtTrueProj = makeHist(hJtTrue2D.ProjectionX("histTrue"), bins=LogBinsJt)
    hJtFakeProjBin = [
        makeHist(hJtFake2D.ProjectionX("histFake{}".format(i), i, i), bins=LogBinsJt)
        for i in range(1, len(jetBinBorders))
    ]

    if not FillFakes:
        for h, h2 in zip(hJtMeasBin, hJtFakeProjBin):
            h.Add(h2, -1)

    for h in (
        hJtMeasProj,
        hJtTrueProj,
        hJtMeas,
        hJtTrue,
        hJtFake,
        hZFake,
        hZMeas,
        hZTrue,
    ):
        h.Scale(1.0 / numberJets, "width")
    for meas, true, n_meas, n_true in zip(
        hJtMeasBin, hJtTrueBin, numberJetsMeasBin, numberJetsTrueBin
    ):
        if n_meas > 0:
            meas.Scale(1.0 / n_meas, "width")
        if n_true > 0:
            true.Scale(1.0 / n_true, "width")

    numberJetsMeasFromHist = [
        hJetPtMeasCoarse.GetBinContent(i)
        for i in range(1, hJetPtMeasCoarse.GetNbinsX() + 1)
    ]
    numberJetsTrueFromHist = [
        hJetPtTrueCoarse.GetBinContent(i)
        for i in range(1, hJetPtTrueCoarse.GetNbinsX() + 1)
    ]
    print("Total number of jets: {}".format(numberJets))
    print("Total number of fakes: {}".format(numberFakes))
    print("Measured jets by bin")
    print(numberJetsMeasBin)
    print(numberJetsMeasFromHist)
    print("True jets by bin")
    print(numberJetsTrueBin)
    print(numberJetsTrueFromHist)
    hRecoJetPtCoarse = unfoldJetPt(hJetPtMeasCoarse, responseJetPtCoarse, jetBinBorders)
    numberJetsFromReco = [
        hRecoJetPtCoarse.GetBinContent(i)
        for i in range(1, hRecoJetPtCoarse.GetNbinsX())
    ]
    print("Unfolded jet numbers by bin:")
    print(numberJetsFromReco)

    print("Fakes by bin")
    print(numberFakesBin)

    print(
        "==================================== UNFOLD ==================================="
    )
    unfold = RooUnfoldBayes(response, hJtMeas, 4)  #  OR
    unfoldSVD = RooUnfoldSvd(response, hJtMeas, 20)  #  OR
    unfold2D = RooUnfoldBayes(response2D, hJtMeas2D, 4)
    for u in (unfold, unfoldSVD, unfold2D):
        u.SetVerbose(0)
    # response2Dtest = makeResponseFromTuple(responseTuple,hJtMeas2D,hJtTrue2D)

    unfold2Dtest = RooUnfoldBayes(response2Dtest, hJtMeas2D, 4)

    unfoldBin = [
        RooUnfoldBayes(responseBin[i], hJtMeasBin[i]) for i in range(len(jetBinBorders))
    ]
    for u in unfoldBin:
        u.SetVerbose(0)
    hRecoBayes = makeHist(unfold.Hreco(), bins=LogBinsJt)
    hRecoSVD = makeHist(unfoldSVD.Hreco(), bins=LogBinsJt)
    hRecoBin = [
        makeHist(unfoldBin[i].Hreco(), bins=LogBinsJt)
        for i in range(len(jetBinBorders))
    ]
    hReco2D = make2DHist(unfold2D.Hreco(), xbins=LogBinsJt, ybins=LogBinsPt)
    hReco2Dtest = make2DHist(unfold2Dtest.Hreco(), xbins=LogBinsJt, ybins=LogBinsPt)
    hRecoJetPt = unfoldJetPt(hJetPtMeas, responseJetPt, LogBinsX)

    hReco2DProjBin = [
        makeHist(hReco2D.ProjectionX("histReco{}".format(i), i, i), bins=LogBinsJt)
        for i in range(1, len(jetBinBorders))
    ]
    hReco2DTestProjBin = [
        makeHist(
            hReco2Dtest.ProjectionX("histRecoTest{}".format(i), i, i), bins=LogBinsJt
        )
        for i in range(1, len(jetBinBorders))
    ]

    hReco2DProj = makeHist(hReco2D.ProjectionX("histReco"), bins=LogBinsJt)
    hReco2DProj.Scale(1.0 / numberJets, "width")
    for h, h2, n in zip(hReco2DProjBin, hReco2DTestProjBin, numberJetsFromReco):
        if n > 0:
            h.Scale(1.0 / n, "width")
            h2.Scale(1.0 / n, "width")
    # unfold.PrintTable (cout, hJtTrue)
    for h, h2, nj in zip(hJtMeasProjBin, hJtFakeProjBin, numberJetsMeasBin):
        if nj > 0:
            h.Scale(1.0 / nj, "width")
            h2.Scale(1.0 / nj, "width")
        # else:
        #    print("nj is 0 for {}".format(h.GetName()))
    for h, nj in zip(hJtTrueProjBin, numberJetsTrueBin):
        if nj > 0:
            h.Scale(1.0 / nj, "width")

    # draw8grid(hJtMeasBin[1:],hJtTrueBin[1:],jetPtBins[1:],xlog = True,ylog = True,name="newfile.pdf",proj = hJtMeasProjBin[2:], unf2d = hReco2DProjBin[2:], unf=hRecoBin[1:])
    if numberEvents > 1000:
        if numberEvents > 1000000:
            filename = "ToyMC_{}M_events.pdf".format(numberEvents / 1000000)
        else:
            filename = "ToyMC_{}k_events.pdf".format(numberEvents / 1000)
    else:
        filename = "ToyMC_{}_events.pdf".format(numberEvents)
    draw8gridcomparison(
        hJtMeasBin,
        hJtTrueBin,
        jetPtBins,
        xlog=True,
        ylog=True,
        name=filename,
        proj=None,
        unf2d=hReco2DProjBin,
        unf2dtest=hReco2DTestProjBin,
        unf=hRecoBin,
        fake=hJtFakeProjBin,
        start=1,
        stride=1,
    )
    drawQA(
        hJtMeas,
        hJtTrue,
        hJtFake,
        hRecoBayes,
        hRecoSVD,
        hReco2DProj,
        hZ,
        hZTrue,
        hZMeas,
        hZFake,
        hMultiMeas,
        hMultiTrue,
        hJetPt,
        hJetPtTrue,
        hJetPtMeas,
        hRecoJetPt,
        responseMatrix,
    )
    outFile.Close()