Exemple #1
0
def make2Dresponse(responses, jetPt, meas, true, misses=None, fakes=None):
    print("++++++++ Create Response from 3D histograms ++++++++")
    response2D = RooUnfoldResponse(meas, true)
    for r, pT, i in zip(responses, jetPt, range(len(responses))):
        nx = r.GetNbinsX()
        ny = r.GetNbinsY()
        nz = r.GetNbinsZ()
        pttrue = (pT[0] + pT[1]) / 2.0
        print("{:.0f}%".format(100.0 * i / len(jetPt)))
        for ix in range(0, nx):
            for iy in range(0, ny):
                for iz in range(1, nz):
                    ib = r.GetBin(ix, iy, iz)
                    c = r.GetBinContent(ib)
                    jttrue = r.GetZaxis().GetBinCenter(iz)
                    # if iy == 0 and ix == 0:
                    # response2D.Miss(jttrue,pttrue,c)
                    if ix > 0 and iy > 0:
                        jtobs = r.GetXaxis().GetBinCenter(ix)
                        jtobs_meas = meas.GetXaxis().GetBinCenter(ix)
                        ptobs_meas = meas.GetYaxis().GetBinCenter(iy)
                        if TMath.Abs(jtobs - jtobs_meas) > 0.01:
                            print("jtobs: {}, jtobs_meas: {}".format(
                                jtobs, jtobs_meas))
                            raise ValueError(
                                "Incorrect binning in make2Dresponse")
                        ptobs = r.GetYaxis().GetBinCenter(iy)
                        if TMath.Abs(ptobs - ptobs_meas) > 0.01:
                            print("ptobs: {}, ptobs_meas: {}".format(
                                ptobs, ptobs_meas))
                            raise ValueError(
                                "Incorrect binning in make2Dresponse")
                        jttrue = r.GetZaxis().GetBinCenter(iz)
                        response2D.Fill(jtobs, ptobs, jttrue, pttrue, c)
    print("{:.0f}%".format(100))

    if misses != None:
        nx = misses.GetNbinsX()
        ny = misses.GetNbinsY()
        for ix in range(1, nx):
            for iy in range(1, ny):
                ib = misses.GetBin(ix, iy)
                c = misses.GetBinContent(ib)
                jttrue = misses.GetXaxis().GetBinCenter(ix)
                pttrue = misses.GetYaxis().GetBinCenter(iy)
                # print("jtTrue: {}, ptTrue: {}, Misses: {}".format(jttrue,pttrue,c))
                response2D.Miss(jttrue, pttrue, c)
    if fakes != None:
        nx = fakes.GetNbinsX()
        ny = fakes.GetNbinsY()
        for ix in range(1, nx):
            for iy in range(1, ny):
                ib = fakes.GetBin(ix, iy)
                c = fakes.GetBinContent(ib)
                jtobs = fakes.GetXaxis().GetBinCenter(ix)
                ptobs = fakes.GetYaxis().GetBinCenter(iy)
                # print("jtObs: {}, ptObs: {}, Fakes: {}".format(jtobs,ptobs,c))
                response2D.Fake(jtobs, ptobs, c)
    return response2D
Exemple #2
0
def createResponse(hMeas, hResponse):
    response = RooUnfoldResponse(hMeas, hMeas)
    for iby in range(1, hResponse.GetNbinsY() + 1):
        jt = hResponse.GetYaxis().GetBinCenter(iby)
        ib = hResponse.GetBin(iby, 0)
        N = hResponse.GetBinContent(ib)
        response.Miss(jt, N)
        for ibx in range(1, hResponse.GetNbinsX() + 1):
            jtobs = hResponse.GetXaxis().GetBinCenter(ibx)
            ib = hResponse.GetBin(ibx, iby)
            N = hResponse.GetBinContent(ib)
            response.Fill(jtobs, jt, N)
    return response
Exemple #3
0
 def train(self, measurement, loufl=False):
     print "============================ TRAIN ============================="
     txt = "Smear mu, s.d.: " + str(measurement.getMean())
     txt += ", " + str(measurement.getSigma()) + ", eff.: "
     txt += str(measurement.getLeff())
     txt += ", o/u-flow: " + str(loufl) + ", function: " + self.opttfun
     print txt
     # Create response matrix object:
     response = RooUnfoldResponse(
         self.bininfo["mbins"], self.bininfo["mlo"], self.bininfo["mhi"],
         self.bininfo["tbins"] * self.bininfo["nrebin"],
         self.bininfo["tlo"], self.bininfo["thi"])
     response.UseOverflow(loufl)
     for i in xrange(100000):
         xt, x = measurement.generate(self.trainfun)
         if x != None:
             response.Fill(x, xt)
         else:
             response.Miss(xt)
     return response
Exemple #4
0
def makeResponseFromTuple(Ntuple, meas, true):
    print("Start makeResponseFromTuple")
    start = time.time()
    response2D = RooUnfoldResponse(meas, true)
    for entry in Ntuple:
        jtObs = entry.jtObs
        ptObs = entry.ptObs
        jtTrue = entry.jtTrue
        ptTrue = entry.ptTrue
        if ptObs > 0 and ptTrue > 0:
            response2D.Fill(jtObs, ptObs, jtTrue, ptTrue)
        elif ptObs < 0:
            response2D.Miss(jtTrue, ptTrue)
        elif ptTrue < 0:
            response2D.Fake(jtObs, ptObs)
        else:
            print("ERROR")

    end = time.time()
    print("Finished in {}s".format(end - start))
    return response2D
print(
    "==================================== TRAIN ===================================="
)
response = RooUnfoldResponse(hMeas, hTrue)

for i in range(Nbins):  # x_true
    Ei = E_resp_array[i]  # x_true
    for j in range(Nbins):  # x_measured
        Ej = E_resp_array[j]  # x_measured
        mc = R_2D[i, j]
        # response.Fill (x_measured, x_true)
        response.Fill(Ej, Ei, mc)
    # account for eff < 1
    eff_ = R_2D[i, :].sum()
    pmisses = 1 - eff_  # probability of misses
    response.Miss(Ei, pmisses)

print(
    "==================================== TEST ====================================="
)

# # "True" Eg in keV, counts
# Eg_choose = np.array([[4000,2000]])

# Eg_choose = np.array([[4000,2000],
#                       [2000,1000],
#                       [1500,1000],
#                       [3000,500],
#                       ])

# Eg_min = 1e3
Exemple #6
0
# ==============================================================================
#  Example Unfolding
# ==============================================================================

print "==================================== TRAIN ===================================="
response = RooUnfoldResponse(40, -10.0, 10.0)

#  Train with a Breit-Wigner, mean 0.3 and width 2.5.
for i in xrange(100000):
    xt = gRandom.BreitWigner(0.3, 2.5)
    x = smear(xt)
    if x != None:
        response.Fill(x, xt)
    else:
        response.Miss(xt)

print "==================================== TEST ====================================="
hTrue = TH1D("true", "Test Truth", 40, -10.0, 10.0)
hMeas = TH1D("meas", "Test Measured", 40, -10.0, 10.0)
#  Test with a Gaussian, mean 0 and width 2.
for i in xrange(10000):
    xt = gRandom.Gaus(0.0, 2.0)
    x = smear(xt)
    hTrue.Fill(xt)
    if x != None: hMeas.Fill(x)

print "==================================== UNFOLD ==================================="
unfold = RooUnfoldBayes(response, hMeas, 4)
#  OR
# unfold= RooUnfoldSvd     (response, hMeas, 20);     #  OR
Exemple #7
0
def main(optunf="Bayes"):

    optunfs = ["Bayes", "SVD", "TUnfold", "Invert", "Reverse"]
    if not optunf in optunfs:
        txt = "Unfolding option " + optunf + " not recognised"
        raise ValueError(txt)

    global hReco, hMeas, hTrue

    print "==================================== TRAIN ===================================="
    # Create response matrix object for 40 measured and 20
    # unfolded bins:
    response = RooUnfoldResponse(40, -10.0, 10.0, 20, -10.0, 10.0)

    #  Train with a Breit-Wigner, mean 0.3 and width 2.5.
    for i in xrange(100000):
        # xt= gRandom.BreitWigner( 0.3, 2.5 )
        xt = gRandom.Gaus(0.0, 5.0)
        x = smear(xt)
        if x != None:
            response.Fill(x, xt)
        else:
            response.Miss(xt)

    print "==================================== TEST ====================================="
    hTrue = TH1D("true", "Test Truth", 20, -10.0, 10.0)
    hMeas = TH1D("meas", "Test Measured", 40, -10.0, 10.0)
    #  Test with a Gaussian, mean 0 and width 2.
    for i in xrange(10000):
        # xt= gRandom.Gaus( 0.0, 2.0 )
        xt = gRandom.BreitWigner(0.3, 2.5)
        x = smear(xt)
        hTrue.Fill(xt)
        if x != None:
            hMeas.Fill(x)

    print "==================================== UNFOLD ==================================="
    print "Unfolding method:", optunf
    if "Bayes" in optunf:
        # Bayes unfoldung with 4 iterations
        # unfold= RooUnfoldBayes( response, hMeas, 4 )
        unfold = RooUnfoldBayes(response, hMeas, 10, False, True)
    elif "SVD" in optunf:
        # SVD unfoding with free regularisation
        # unfold= RooUnfoldSvd( response, hMeas, 20 )
        unfold = RooUnfoldSvd(response, hMeas)
    elif "TUnfold" in optunf:
        # TUnfold with fixed regularisation tau=0.002
        # unfold= RooUnfoldTUnfold( response, hMeas )
        unfold = RooUnfoldTUnfold(response, hMeas, 0.002)
    elif "Invert" in optunf:
        unfold = RooUnfoldInvert(response, hMeas)
    elif "Reverse" in optunf:
        unfold = RooUnfoldBayes(response, hMeas, 1)

    hReco = unfold.Hreco()
    # unfold.PrintTable( cout, hTrue )
    unfold.PrintTable(cout, hTrue, 2)

    hReco.Draw()
    hMeas.Draw("SAME")
    hTrue.SetLineColor(8)
    hTrue.Draw("SAME")

    return
Exemple #8
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()