Esempio n. 1
0
 def __init__(self, genHist, recHist, hist2d, name):
     self.genHist = genHist.Clone()
     self.recHist = recHist.Clone()
     self.hist2d = hist2d.Clone()
     self.response = RooUnfoldResponse(recHist, genHist, hist2d,
                                       "response matrix", "response matrix")
     self.response.UseOverflow()
     self.name = name
     hx = self.hist2d.ProjectionX()
     hy = self.hist2d.ProjectionY()
     vaild = 0.0001
     test1 = 0
     test2 = 0
     for i in range(hx.GetNbinsX() + 2):
         if recHist.GetBinContent(i) != 0:
             val = abs(hx.GetBinContent(i) / recHist.GetBinContent(i) - 1)
         else:
             val = 0
         if val > test1:
             test1 = val
     for i in range(hx.GetNbinsX() + 2):
         if genHist.GetBinContent(i) != 0:
             val = abs(hy.GetBinContent(i) / genHist.GetBinContent(i) - 1)
         else:
             val = 0
         if val > test2:
             test2 = val
     if test1 < vaild and test2 < vaild:
         print "response matrix is correct"
     else:
         print "response matrix is wrong"
         print test1
         print test2
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 4
0
def UnfoldFF(rawFF, detResponse, tag):
    padFF.Clear()
    padFF.Divide(2)
    # Detector response
    ResponseNorm(detResponse)
    padFF.cd(1)
    detResponse.Draw('COLZ')
    response = RooUnfoldResponse(rawFF, rawFF, detResponse)
    bayes = RooUnfoldBayes(response, rawFF)
    # Unfolding - Bayesian
    ana_util.COLOR = SelectColor(0)
    ana_util.MARKER = SelectMarker(0)
    padFF.cd(2)
    # Legend
    lgd = TLegend(0.12, 0.6, 0.3, 0.85)
    lgd.SetName('lgd' + tag)
    lgd.SetBorderSize(0)
    lgd.SetFillColor(0)
    # Z measured
    rawFF.SetBins(10, 0, 1.0)
    DrawFF(rawFF, 'hRaw_' + tag)
    lgd.AddEntry(rawFF, 'Raw')
    for nIter in range(4, 5):
        bayes.SetIterations(nIter)
        hist = DrawFF(bayes.Hreco(0), 'hBayes' + repr(nIter) + '_' + tag)
        for ix in range(1, hist.GetNbinsX()):
            hist.SetBinError(ix, rawFF.GetBinError(ix))
        lgd.AddEntry(hist, 'Bayes (N=%d)' % nIter)
    lgd.Draw('same')
    padFF.Print(printFile, 'Title:' + tag)
    padFF.Write('c' + tag)
Esempio n. 5
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
Esempio n. 6
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
Esempio n. 7
0
    def _create_response(reco_norm_data, true_norm_data, data, reco_binning,
                         true_binning):
        """Create the response object from the signal data."""
        logging.debug('Creating response object.')

        reco_hist = roounfold._histogram(events=reco_norm_data,
                                         binning=reco_binning,
                                         weights=reco_norm_data['pisa_weight'],
                                         errors=True,
                                         name='reco_signal',
                                         tex=r'\rm{reco_signal}')
        true_hist = roounfold._histogram(events=true_norm_data,
                                         binning=true_binning,
                                         weights=true_norm_data['pisa_weight'],
                                         errors=True,
                                         name='true_signal',
                                         tex=r'\rm{true_signal}')
        r_flat = roounfold._flatten_to_1d(reco_hist)
        t_flat = roounfold._flatten_to_1d(true_hist)

        smear_matrix = roounfold._histogram(events=data,
                                            binning=reco_binning +
                                            true_binning,
                                            weights=data['pisa_weight'],
                                            errors=True,
                                            name='smearing_matrix',
                                            tex=r'\rm{smearing_matrix}')
        smear_flat = roounfold._flatten_to_2d(smear_matrix)

        r_th1d = convert_to_th1d(r_flat, errors=True)
        t_th1d = convert_to_th1d(t_flat, errors=True)
        smear_th2d = convert_to_th2d(smear_flat, errors=True)

        response = RooUnfoldResponse(r_th1d, t_th1d, smear_th2d)
        del r_th1d
        del smear_th2d
        return response, t_th1d
Esempio n. 8
0
    def process_response(self):
        list_df_mc_reco = []
        list_df_mc_gen = []
        for iptskim, _ in enumerate(self.lpt_anbinmin):
            df_mc_reco = pickle.load(openfile(self.lpt_recodecmerged[iptskim], "rb"))
            if "pt_jet" not in df_mc_reco.columns:
                print("Jet variables not found in the dataframe. Skipping process_response.")
                return
            if self.s_evtsel is not None:
                df_mc_reco = df_mc_reco.query(self.s_evtsel)
            if self.s_trigger is not None:
                df_mc_reco = df_mc_reco.query(self.s_trigger)
            df_mc_reco = selectdfrunlist(df_mc_reco, \
                  self.run_param[self.runlistrigger[self.triggerbit]], "run_number")
            if self.doml is True:
                df_mc_reco = df_mc_reco.query(self.l_selml[iptskim])
            else:
                print("Doing std analysis")
            list_df_mc_reco.append(df_mc_reco)
            df_mc_gen = pickle.load(openfile(self.lpt_gendecmerged[iptskim], "rb"))
            df_mc_gen = selectdfrunlist(df_mc_gen, \
                    self.run_param[self.runlistrigger[self.triggerbit]], "run_number")
            df_mc_gen = df_mc_gen.query(self.s_presel_gen_eff)
            list_df_mc_gen.append(df_mc_gen)
        df_rec = pd.concat(list_df_mc_reco)
        df_gen = pd.concat(list_df_mc_gen)
        his_njets = TH1F("his_njets_gen", "Number of MC jets", 1, 0, 1)
        his_njets.SetBinContent(1, len(df_gen.index)) # total number of generated & selected jets for normalisation
        df_rec = df_rec[df_rec.ismcfd == 1] # reconstructed & selected non-prompt jets
        df_gen = df_gen[df_gen.ismcfd == 1] # generated & selected non-prompt jets
        out_file = TFile.Open(self.n_fileeff, "update")

        # Bin arrays
        # pt_cand
        n_bins_ptc = len(self.lpt_finbinmin)
        bins_ptc_temp = self.lpt_finbinmin.copy()
        bins_ptc_temp.append(self.lpt_finbinmax[n_bins_ptc - 1])
        bins_ptc = array.array('d', bins_ptc_temp)
        # pt_jet
        n_bins_ptjet = len(self.lvar2_binmin)
        bins_ptjet_temp = self.lvar2_binmin.copy()
        bins_ptjet_temp.append(self.lvar2_binmax[n_bins_ptjet - 1])
        bins_ptjet = array.array('d', bins_ptjet_temp)
        # z
        bins_z_temp = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1]
        n_bins_z = len(bins_z_temp) - 1
        bins_z = array.array('d', bins_z_temp)

        # Detector response matrix of pt_jet of non-prompt jets
        df_resp_jet_fd = df_rec.loc[:, ["pt_gen_jet", "pt_jet"]]
        his_resp_jet_fd = TH2F("his_resp_jet_fd", \
            "Response matrix of #it{p}_{T}^{jet, ch} of non-prompt jets;#it{p}_{T}^{jet, ch, gen.} (GeV/#it{c});#it{p}_{T}^{jet, ch, rec.} (GeV/#it{c})", \
            100, 0, 100, 100, 0, 100)
        fill_hist(his_resp_jet_fd, df_resp_jet_fd)

        # Simulated pt_cand vs. pt_jet of non-prompt jets
        df_ptc_ptjet_fd = df_gen.loc[:, ["pt_cand", "pt_jet"]]
        his_ptc_ptjet_fd = TH2F("his_ptc_ptjet_fd", \
            "Simulated #it{p}_{T}^{cand.} vs. #it{p}_{T}^{jet} of non-prompt jets;#it{p}_{T}^{cand., gen.} (GeV/#it{c});#it{p}_{T}^{jet, ch, gen.} (GeV/#it{c})", \
            n_bins_ptc, bins_ptc, 100, 0, 100)
        fill_hist(his_ptc_ptjet_fd, df_ptc_ptjet_fd)

        # z_gen of reconstructed feed-down jets (for response)
        arr_z_gen_resp = z_gen_calc(df_rec.pt_gen_jet, df_rec.phi_gen_jet, df_rec.eta_gen_jet,
                                    df_rec.pt_gen_cand, df_rec.delta_phi_gen_jet, df_rec.delta_eta_gen_jet)
        # z_rec of reconstructed feed-down jets (for response)
        arr_z_rec_resp = z_calc(df_rec.pt_jet, df_rec.phi_jet, df_rec.eta_jet,
                                df_rec.pt_cand, df_rec.phi_cand, df_rec.eta_cand)
        # z_gen of simulated feed-down jets
        arr_z_gen_sim = z_calc(df_gen.pt_jet, df_gen.phi_jet, df_gen.eta_jet,
                               df_gen.pt_cand, df_gen.phi_cand, df_gen.eta_cand)
        df_rec["z_gen"] = arr_z_gen_resp
        df_rec["z"] = arr_z_rec_resp
        df_gen["z"] = arr_z_gen_sim

        # Simulated pt_cand vs. pt_jet vs z of non-prompt jets
        df_ptc_ptjet_z_fd = df_gen.loc[:, ["pt_cand", "pt_jet", "z"]]
        his_ptc_ptjet_z_fd = TH3F("his_ptc_ptjet_z_fd", \
            "Simulated #it{p}_{T}^{cand.} vs. #it{p}_{T}^{jet} vs. #it{z} of non-prompt jets;"
            "#it{p}_{T}^{cand., gen.} (GeV/#it{c});"
            "#it{p}_{T}^{jet, ch, gen.} (GeV/#it{c});"
            "#it{z}", \
            n_bins_ptc, bins_ptc, n_bins_ptjet, bins_ptjet, n_bins_z, bins_z)
        fill_hist(his_ptc_ptjet_z_fd, df_ptc_ptjet_z_fd)

        # Create response matrix for feed-down smearing
        # x axis = z, y axis = pt_jet
        his_resp_rec = TH2F("his_resp_rec", "his_resp_rec", n_bins_z, bins_z, n_bins_ptjet, bins_ptjet)
        his_resp_gen = TH2F("his_resp_gen", "his_resp_gen", n_bins_z, bins_z, n_bins_ptjet, bins_ptjet)
        resp_z = RooUnfoldResponse(his_resp_rec, his_resp_gen)
        for row in df_rec.itertuples():
            resp_z.Fill(row.z, row.pt_jet, row.z_gen, row.pt_gen_jet)

        out_file.cd()
        his_resp_jet_fd.Write()
        his_ptc_ptjet_fd.Write()
        his_ptc_ptjet_z_fd.Write()
        his_njets.Write()
        resp_z.Write("resp_z")
        out_file.Close()
Esempio n. 9
0
 def _makeUnfoldResponse( self ):
     if self.fakes:
         return RooUnfoldResponse ( self.measured, self.truth, self.fakes, self.response )
     else:
         return RooUnfoldResponse ( self.measured, self.truth, self.response )
Esempio n. 10
0
def main():

    gROOT.SetBatch()

    #range for |t|
    ptmin = 0.
    ptmax = 0.109  #   0.109  0.01 for interference range

    #default binning
    ptbin = 0.004  # 0.004  0.0005 for interference range

    #long bins at high |t|
    ptmid = 0.06  # 0.08, value > ptmax will switch it off   0.06
    ptlon = 0.01  # 0.01

    #short bins at low |t|
    ptlow = 0.01
    ptshort = 0.0005

    #mass interval
    mmin = 2.8
    mmax = 3.2

    #dy = 2. # rapidity interval, for integrated sigma
    dy = 1.

    ngg = 131  # number of gamma-gamma from mass fit

    lumi = 13871.907  # lumi in inv. ub

    #correction to luminosity for ana/triggered events
    ratio_ana = 3420950. / 3694000

    #scale the lumi for |z| around nominal bunch crossing
    ratio_zdc_vtx = 0.502

    Reta = 0.503  # pseudorapidity preselection
    #Reta = 1.

    trg_eff = 0.67  # bemc trigger efficiency

    ratio_tof = 1.433  # tof correction to efficiency

    bbceff = 0.97  # BBC veto inefficiency

    zdc_acc = 0.49  # ZDC acceptance to XnXn 0.7
    #zdc_acc = 1.

    br = 0.05971  # dielectrons branching ratio

    #data
    basedir = "../../../star-upc-data/ana/muDst/muDst_run1/sel5"
    infile = "ana_muDst_run1_all_sel5z.root"

    #MC
    basedir_sl = "../../../star-upc-data/ana/starsim/slight14e/sel5"
    #infile_sl = "ana_slight14e1x2_s6_sel5z.root"
    infile_sl = "ana_slight14e1x3_s6_sel5z.root"
    #
    basedir_sart = "../../../star-upc-data/ana/starsim/sartre14a/sel5"
    infile_sart = "ana_sartre14a1_sel5z_s6_v2.root"
    #
    basedir_bgen = "../../../star-upc-data/ana/starsim/bgen14a/sel5"
    infile_bgen = "ana_bgen14a1_v0_sel5z_s6.root"
    #infile_bgen = "ana_bgen14a2_sel5z_s6.root"
    #
    basedir_gg = "../../../star-upc-data/ana/starsim/slight14e/sel5"
    infile_gg = "ana_slight14e2x1_sel5_nzvtx.root"

    #model predictions
    gSlight = load_starlight(dy)
    gSartre = load_sartre()
    gFlat = loat_flat_pt2()
    gMS = load_ms()
    gCCK = load_cck()

    #open the inputs
    inp = TFile.Open(basedir + "/" + infile)
    tree = inp.Get("jRecTree")
    #
    inp_gg = TFile.Open(basedir_gg + "/" + infile_gg)
    tree_gg = inp_gg.Get("jRecTree")
    #
    inp_sl = TFile.Open(basedir_sl + "/" + infile_sl)
    tree_sl_gen = inp_sl.Get("jGenTree")
    #
    inp_sart = TFile.Open(basedir_sart + "/" + infile_sart)
    tree_sart_gen = inp_sart.Get("jGenTree")
    #
    inp_bgen = TFile.Open(basedir_bgen + "/" + infile_bgen)
    tree_bgen_gen = inp_bgen.Get("jGenTree")

    #evaluate binning
    #print "bins:", ut.get_nbins(ptbin, ptmin, ptmax)

    bins = ut.get_bins_vec_2pt(ptbin, ptlon, ptmin, ptmax, ptmid)
    #bins = ut.get_bins_vec_3pt(ptshort, ptbin, ptlon, ptmin, ptmax, ptlow, ptmid)
    #print "bins2:", bins.size()-1

    #load the data
    strsel = "jRecM>{0:.3f} && jRecM<{1:.3f}".format(mmin, mmax)

    hPt = ut.prepare_TH1D_vec("hPt", bins)
    tree.Draw("jRecPt*jRecPt >> hPt", strsel)

    #distribution for bin centers
    hPtCen = hPt.Clone("hPtCen")

    #gamma-gamma component
    hPtGG = ut.prepare_TH1D_vec("hPtGG", bins)
    tree_gg.Draw("jRecPt*jRecPt >> hPtGG", strsel)

    #normalize the gamma-gamma component
    ut.norm_to_num(hPtGG, ngg, rt.kGreen)

    #incoherent functional shape
    func_incoh_pt2 = TF1("func_incoh", "[0]*exp(-[1]*x)", 0., 10.)
    func_incoh_pt2.SetParameters(873.04, 3.28)

    #fill incoherent histogram from functional shape
    hPtIncoh = ut.prepare_TH1D_vec("hPtIncoh", bins)
    ut.fill_h1_tf(hPtIncoh, func_incoh_pt2, rt.kRed)

    #print "Entries before gamma-gamma and incoherent subtraction:", hPt.GetEntries()

    #subtract gamma-gamma and incoherent components
    hPt.Sumw2()
    hPt.Add(hPtGG, -1)
    #print "Gamma-gamma entries:", hPtGG.Integral()
    #print "Entries after gamma-gamma subtraction:", hPt.Integral()
    #print "Incoherent entries:", hPtIncoh.Integral()
    hPt.Add(hPtIncoh, -1)

    #print "Entries after all subtraction:", hPt.Integral()

    #scale the luminosity
    lumi_scaled = lumi * ratio_ana * ratio_zdc_vtx
    #print "lumi_scaled:", lumi_scaled

    #denominator for deconvoluted distribution, conversion ub to mb
    den = Reta * br * zdc_acc * trg_eff * bbceff * ratio_tof * lumi_scaled * 1000. * dy

    #deconvolution
    deconv_min = bins[0]
    deconv_max = bins[bins.size() - 1]
    deconv_nbin = bins.size() - 1
    gROOT.LoadMacro("fill_response_matrix.C")

    #Starlight response
    #resp_sl = RooUnfoldResponse(deconv_nbin, deconv_min, deconv_max, deconv_nbin, deconv_min, deconv_max)
    resp_sl = RooUnfoldResponse(hPt, hPt)
    rt.fill_response_matrix(tree_sl_gen, resp_sl)
    #
    unfold_sl = RooUnfoldBayes(resp_sl, hPt, 15)
    #unfold_sl = RooUnfoldSvd(resp_sl, hPt, 15)
    hPtSl = unfold_sl.Hreco()
    #ut.set_H1D(hPtSl)
    #apply the denominator and bin width
    ut.norm_to_den_w(hPtSl, den)

    #Sartre response
    #resp_sart = RooUnfoldResponse(deconv_nbin, deconv_min, deconv_max, deconv_nbin, deconv_min, deconv_max)
    #resp_sart = RooUnfoldResponse(hPt, hPt)
    #rt.fill_response_matrix(tree_sart_gen, resp_sart)
    #
    #unfold_sart = RooUnfoldBayes(resp_sart, hPt, 10)
    #hPtSart = unfold_sart.Hreco()
    #ut.set_H1D(hPtSart)
    #hPtSart.SetMarkerStyle(21)

    #Flat pT^2 response
    #resp_bgen = RooUnfoldResponse(deconv_nbin, deconv_min, deconv_max, deconv_nbin, deconv_min, deconv_max)
    resp_bgen = RooUnfoldResponse(hPt, hPt)
    rt.fill_response_matrix(tree_bgen_gen, resp_bgen)
    #
    unfold_bgen = RooUnfoldBayes(resp_bgen, hPt, 14)
    hPtFlat = unfold_bgen.Hreco()
    #ut.set_H1D(hPtFlat)
    #apply the denominator and bin width
    ut.norm_to_den_w(hPtFlat, den)
    #hPtFlat.SetMarkerStyle(22)
    #hPtFlat.SetMarkerSize(1.3)

    #systematical errors
    err_zdc_acc = 0.1
    err_bemc_eff = 0.03
    #sys_err = rt.TMath.Sqrt(err_zdc_acc*err_zdc_acc + err_bemc_eff*err_bemc_eff)
    sys_err = err_zdc_acc * err_zdc_acc + err_bemc_eff * err_bemc_eff
    #print "Total sys err:", sys_err
    hSys = ut.prepare_TH1D_vec("hSys", bins)
    hSys.SetOption("E2")
    hSys.SetFillColor(rt.kOrange + 1)
    hSys.SetLineColor(rt.kOrange)
    for ibin in xrange(1, hPtFlat.GetNbinsX() + 1):
        hSys.SetBinContent(ibin, hPtFlat.GetBinContent(ibin))
        sig_sl = hPtSl.GetBinContent(ibin)
        sig_fl = hPtFlat.GetBinContent(ibin)
        err_deconv = TMath.Abs(sig_fl - sig_sl) / sig_fl
        #print "err_deconv", err_deconv
        #sys_err += err_deconv*err_deconv
        sys_err_sq = sys_err + err_deconv * err_deconv
        sys_err_bin = TMath.Sqrt(sys_err_sq)
        stat_err = hPtFlat.GetBinError(ibin) / hPtFlat.GetBinContent(ibin)
        tot_err = TMath.Sqrt(stat_err * stat_err + sys_err_sq)
        #hSys.SetBinError(ibin, hPtFlat.GetBinContent(ibin)*err_deconv)
        hSys.SetBinError(ibin, hPtFlat.GetBinContent(ibin) * sys_err_bin)
        #hPtFlat.SetBinError(ibin, hPtFlat.GetBinContent(ibin)*tot_err)

    #draw the results
    gStyle.SetPadTickX(1)
    gStyle.SetFrameLineWidth(2)

    #frame for models plot only
    frame = ut.prepare_TH1D("frame", ptbin, ptmin, ptmax)

    can = ut.box_canvas()
    #ut.set_margin_lbtr(gPad, 0.1, 0.09, 0.03, 0.03)
    ut.set_margin_lbtr(gPad, 0.1, 0.09, 0.055, 0.01)

    ytit = "d#it{#sigma}/d#it{t}d#it{y} (mb/(GeV/c)^{2})"
    xtit = "|#kern[0.3]{#it{t}}| ((GeV/c)^{2})"

    ut.put_yx_tit(frame, ytit, xtit, 1.4, 1.2)
    frame.SetMaximum(11)
    #frame.SetMinimum(1.e-6)
    #frame.SetMinimum(2e-4)
    frame.SetMinimum(1e-5)  # 3e-5
    frame.Draw()

    #hSys.Draw("e2same")

    #bin center points from data
    #gSig = apply_centers(hPtFlat, hPtCen)
    gSig = fixed_centers(hPtFlat)
    ut.set_graph(gSig)

    #hPtSl.Draw("e1same")
    #hPtSart.Draw("e1same")
    #hPtFlat.Draw("e1same")

    #put model predictions
    #gSartre.Draw("lsame")
    #gFlat.Draw("lsame")
    gMS.Draw("lsame")
    gCCK.Draw("lsame")
    gSlight.Draw("lsame")

    gSig.Draw("P")

    frame.Draw("same")

    gPad.SetLogy()

    cleg = ut.prepare_leg(0.1, 0.96, 0.14, 0.01, 0.035)
    cleg.AddEntry(
        None,
        "Au+Au #rightarrow J/#psi + Au+Au + XnXn, #sqrt{#it{s}_{#it{NN}}} = 200 GeV",
        "")
    cleg.Draw("same")

    leg = ut.prepare_leg(0.45, 0.82, 0.18, 0.1, 0.035)
    leg.AddEntry(None, "#bf{|#kern[0.3]{#it{y}}| < 1}", "")
    hx = ut.prepare_TH1D("hx", 1, 0, 1)
    leg.AddEntry(hx, "STAR")
    hx.Draw("same")
    leg.Draw("same")

    #legend for models
    mleg = ut.prepare_leg(0.68, 0.76, 0.3, 0.16, 0.035)
    #mleg = ut.prepare_leg(0.68, 0.8, 0.3, 0.12, 0.035)
    mleg.AddEntry(gSlight, "STARLIGHT", "l")
    mleg.AddEntry(gMS, "MS", "l")
    mleg.AddEntry(gCCK, "CCK-hs", "l")
    #mleg.AddEntry(gSartre, "Sartre", "l")
    #mleg.AddEntry(gFlat, "Flat #it{p}_{T}^{2}", "l")
    mleg.Draw("same")

    #legend for deconvolution method
    dleg = ut.prepare_leg(0.3, 0.75, 0.2, 0.18, 0.035)
    #dleg = ut.prepare_leg(0.3, 0.83, 0.2, 0.1, 0.035)
    dleg.AddEntry(None, "Unfolding with:", "")
    dleg.AddEntry(hPtSl, "Starlight", "p")
    #dleg.AddEntry(hPtSart, "Sartre", "p")
    dleg.AddEntry(hPtFlat, "Flat #it{p}_{T}^{2}", "p")
    #dleg.Draw("same")

    ut.invert_col(rt.gPad)
    can.SaveAs("01fig.pdf")

    #to prevent 'pure virtual method called'
    gPad.Close()

    #save the cross section to output file
    out = TFile("sigma.root", "recreate")
    gSig.Write("sigma")
    out.Close()

    #beep when finished
    gSystem.Exec("mplayer ../computerbeep_1.mp3 > /dev/null 2>&1")
from ROOT import RooUnfoldResponse
from ROOT import RooUnfoldBayes

Nbins = len(E_resp_array)
Emin = E_resp_array[0]
Emax = E_resp_array[-1]

matrix_unfolded1 = np.zeros((Nbins, Nbins))

hTrue = TH1D("true", "Test Truth", Nbins, Emin, Emax)
hMeas = TH1D("meas", "Test Measured", Nbins, Emin, Emax)

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 ====================================="
Esempio n. 12
0
reMBBn = f1.Get("reMBBn")
reMBEn = f1.Get("reMBEn")
genBB = f1.Get("genBB")
genBE = f1.Get("genBE")
recBB = f1.Get("recBB")
recBE = f1.Get("recBE")
f2 = ROOT.TFile.Open("unfoldingSample.root")
hmuBE = f2.Get("hist_f26f74ed27824519bcad66a6f539b3e5")
hmuBB = f2.Get("hist_1405a58d67164a8bb28859df616bad3c")
f3 = ROOT.TFile.Open("unfoldingMC.root")
hmuBE_mc = f3.Get("hist_bab6670dc2a946a7af6bdfbc761f87c5")
hmuBB_mc = f3.Get("hist_35313753f6e34206bd2294885ac1096f")
#f1.Close()
#f2.Close()
f = ROOT.TFile("unfoldSample.root", "RECREATE")
responseBB = RooUnfoldResponse(recBB, genBB, reMBB, "reBB", "reBB")
#responseBB.UseOverflow()
print recBB.GetNbinsX()
print reMBB.GetNbinsX()
#responseBB=RooUnfoldResponse(reMBB)
print "Print values of RooUnfold response matrix for BB category"
responseBB.Print()
print "Print values of response matrix for calculated by hand for BB category"
reMBBList = []
for i in range(1, reMBB.GetNbinsX() + 1):
    column = []
    for j in range(1, reMBB.GetNbinsX() + 1):
        column.append(reMBBn.GetBinContent(i, j))
    print column
invertBB = RooUnfoldInvert(responseBB, hmuBB, "UnfoldedBB", "UnfoldedBB")
inverthmuBB = invertBB.Hreco()
Esempio n. 13
0
    def process_response(self):
        """
        First of all, we load all the mc gen and reco files that are skimmed
        in bins of HF candidate ptand we apply the standard selection to all
        of them. After this, we merged them all to create a single file of gen
        and reco monte carlo sample with all the HF candidate pt. In particular
        gen jets are selected according to run trigger, runlist, and gen jet
        zbin_recoand pseudorapidity. Reco candidates according to evt selection, eta
        jets, trigger and ml probability of the HF hadron
        """
        zbin_reco = []
        nzbin_reco = self.p_nbinshape_reco
        zbin_reco = self.varshaperanges_reco
        zbinarray_reco = array.array('d', zbin_reco)

        zbin_gen = []
        nzbin_gen = self.p_nbinshape_gen
        zbin_gen = self.varshaperanges_gen
        zbinarray_gen = array.array('d', zbin_gen)

        jetptbin_reco = []
        njetptbin_reco = self.p_nbin2_reco
        jetptbin_reco = self.var2ranges_reco
        jetptbinarray_reco = array.array('d', jetptbin_reco)

        jetptbin_gen = []
        njetptbin_gen = self.p_nbin2_gen
        jetptbin_gen = self.var2ranges_gen
        jetptbinarray_gen = array.array('d', jetptbin_gen)

        candptbin = []
        candptbin = self.lpt_finbinmin.copy()
        candptbin.append(self.lpt_finbinmax[-1])
        candptbinarray = array.array('d', candptbin)

        out_file = TFile.Open(self.n_fileeff, "update")
        list_df_mc_reco = []
        list_df_mc_gen = []

        for iptskim, _ in enumerate(self.lpt_anbinmin):

            df_mc_gen = pickle.load(openfile(self.lpt_gendecmerged[iptskim], "rb"))
            df_mc_gen = selectdfrunlist(df_mc_gen, \
                    self.run_param[self.runlistrigger[self.triggerbit]], "run_number")
            df_mc_gen = df_mc_gen.query(self.s_jetsel_gen)
            list_df_mc_gen.append(df_mc_gen)

            df_mc_reco = pickle.load(openfile(self.lpt_recodecmerged[iptskim], "rb"))
            if self.s_evtsel is not None:
                df_mc_reco = df_mc_reco.query(self.s_evtsel)
            if self.s_jetsel_reco is not None:
                df_mc_reco = df_mc_reco.query(self.s_jetsel_reco)
            if self.s_trigger is not None:
                df_mc_reco = df_mc_reco.query(self.s_trigger)
            if self.doml is True:
                df_mc_reco = df_mc_reco.query(self.l_selml[iptskim])
            list_df_mc_reco.append(df_mc_reco)

        # Here we can merge the dataframes corresponding to different HF pt in a
        # single one. In addition we are here selecting only non prompt HF

        df_gen = pd.concat(list_df_mc_gen)
        df_mc_reco = pd.concat(list_df_mc_reco)

        # add the z columns
        df_gen["z"] = z_calc(df_gen.pt_jet, df_gen.phi_jet, df_gen.eta_jet,
                             df_gen.pt_cand, df_gen.phi_cand, df_gen.eta_cand)

        df_mc_reco["z"] = z_calc(df_mc_reco.pt_jet, df_mc_reco.phi_jet, df_mc_reco.eta_jet,
                                 df_mc_reco.pt_cand, df_mc_reco.phi_cand, df_mc_reco.eta_cand)

        df_mc_reco["z_gen"] = z_gen_calc(df_mc_reco.pt_gen_jet, df_mc_reco.phi_gen_jet,
                                         df_mc_reco.eta_gen_jet, df_mc_reco.pt_gen_cand,
                                         df_mc_reco.delta_phi_gen_jet, df_mc_reco.delta_eta_gen_jet)

        df_gen_nonprompt = df_gen[df_gen.ismcfd == 1]
        df_gen_prompt = df_gen[df_gen.ismcprompt == 1]
        df_mc_reco_merged_nonprompt = df_mc_reco[df_mc_reco.ismcfd == 1]
        df_mc_reco_merged_prompt = df_mc_reco[df_mc_reco.ismcprompt == 1]

        # The following plots are 3d plots all at generated level of z,
        # pt_jet and pt_cand. This was used in the first version of the feeddown
        # subtraction, currently is obsolete

        hzvsjetpt_gen_unmatched = TH2F("hzvsjetpt_gen_unmatched", "hzvsjetpt_gen_unmatched", \
            nzbin_gen, zbinarray_gen, njetptbin_gen, jetptbinarray_gen)
        df_zvsjetpt_gen_unmatched = df_gen_prompt.loc[:, [self.v_varshape_binning, "pt_jet"]]
        fill_hist(hzvsjetpt_gen_unmatched, df_zvsjetpt_gen_unmatched)
        hzvsjetpt_gen_unmatched.Write()
        titlehist = "hzvsjetptvscandpt_gen_nonprompt"
        hzvsjetptvscandpt_gen_nonprompt = makefill3dhist(df_gen_nonprompt, titlehist, \
            zbinarray_gen, jetptbinarray_gen, candptbinarray, self.v_varshape_binning, "pt_jet", "pt_cand")
        hzvsjetptvscandpt_gen_nonprompt.Write()

        # hz_gen_nocuts is the distribution of generated z values in b in
        # bins of gen_jet pt before the reco z and jetpt selection. hz_gen_cuts
        # also includes cut on z reco and jet pt reco. These are used for overall
        # efficiency correction to estimate the fraction of candidates that are
        # in the reco range but outside the gen range and viceversa

        for ibin2 in range(self.p_nbin2_gen):
            suffix = "%s_%.2f_%.2f" % \
                (self.v_var2_binning, self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            hz_gen_nocuts = TH1F("hz_gen_nocuts_nonprompt" + suffix, \
                "hz_gen_nocuts_nonprompt" + suffix, nzbin_gen, zbinarray_gen)
            hz_gen_nocuts.Sumw2()
            hz_gen_cuts = TH1F("hz_gen_cuts_nonprompt" + suffix,
                               "hz_gen_cuts_nonprompt" + suffix, nzbin_gen, zbinarray_gen)
            hz_gen_cuts.Sumw2()

            df_tmp = seldf_singlevar(df_mc_reco_merged_nonprompt, "pt_gen_jet", \
                                     self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            df_tmp = seldf_singlevar(df_tmp, self.v_varshape_binning_gen, \
                                     self.lvarshape_binmin_gen[0], self.lvarshape_binmax_gen[-1])
            fill_hist(hz_gen_nocuts, df_tmp[self.v_varshape_binning_gen])
            df_tmp = seldf_singlevar(df_tmp, "pt_jet",
                                     self.lvar2_binmin_reco[0], self.lvar2_binmax_reco[-1])
            df_tmp = seldf_singlevar(df_tmp, self.v_varshape_binning,
                                     self.lvarshape_binmin_reco[0], self.lvarshape_binmax_reco[-1])
            fill_hist(hz_gen_cuts, df_tmp[self.v_varshape_binning_gen])
            hz_gen_cuts.Write()
            hz_gen_nocuts.Write()

            # Addendum for unfolding
            hz_gen_nocuts_pr = TH1F("hz_gen_nocuts" + suffix, \
                "hz_gen_nocuts" + suffix, nzbin_gen, zbinarray_gen)
            hz_gen_nocuts_pr.Sumw2()
            hz_gen_cuts_pr = TH1F("hz_gen_cuts" + suffix,
                                  "hz_gen_cuts" + suffix, nzbin_gen, zbinarray_gen)
            hz_gen_cuts_pr.Sumw2()
            df_tmp_pr = seldf_singlevar(df_mc_reco_merged_prompt, "pt_gen_jet", \
                                     self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            df_tmp_pr = seldf_singlevar(df_tmp_pr, self.v_varshape_binning_gen, \
                                     self.lvarshape_binmin_gen[0], self.lvarshape_binmax_gen[-1])
            fill_hist(hz_gen_nocuts_pr, df_tmp_pr[self.v_varshape_binning_gen])
            df_tmp_pr = seldf_singlevar(df_tmp_pr, "pt_jet",
                                        self.lvar2_binmin_reco[0], self.lvar2_binmax_reco[-1])
            df_tmp_pr = seldf_singlevar(df_tmp_pr, self.v_varshape_binning,
                                        self.lvarshape_binmin_reco[0], self.lvarshape_binmax_reco[-1])
            fill_hist(hz_gen_cuts_pr, df_tmp_pr[self.v_varshape_binning_gen])
            hz_gen_cuts_pr.Write()
            hz_gen_nocuts_pr.Write()
            # End addendum for unfolding


        df_tmp_selgen, df_tmp_selreco, df_tmp_selrecogen = \
                self.create_df_closure(df_mc_reco_merged_nonprompt)

        df_tmp_selgen_pr, df_tmp_selreco_pr, df_tmp_selrecogen_pr = \
                self.create_df_closure(df_mc_reco_merged_prompt)

        # histograms for response of feeddown
        hzvsjetpt_reco_nocuts = \
            build2dhisto("hzvsjetpt_reco_nocuts_nonprompt", zbinarray_reco, jetptbinarray_reco)
        hzvsjetpt_reco_cuts = \
            build2dhisto("hzvsjetpt_reco_cuts_nonprompt", zbinarray_reco, jetptbinarray_reco)
        hzvsjetpt_gen_nocuts = \
            build2dhisto("hzvsjetpt_gen_nocuts_nonprompt", zbinarray_gen, jetptbinarray_gen)
        hzvsjetpt_gen_cuts = \
            build2dhisto("hzvsjetpt_gen_cuts_nonprompt", zbinarray_gen, jetptbinarray_gen)

        hzvsjetpt_reco = hzvsjetpt_reco_nocuts.Clone("hzvsjetpt_reco_nonprompt")
        hzvsjetpt_gen = hzvsjetpt_gen_nocuts.Clone("hzvsjetpt_genv")
        response_matrix = RooUnfoldResponse(hzvsjetpt_reco, hzvsjetpt_gen)

        fill2dhist(df_tmp_selreco, hzvsjetpt_reco_nocuts, self.v_varshape_binning, "pt_jet")
        fill2dhist(df_tmp_selgen, hzvsjetpt_gen_nocuts, self.v_varshape_binning_gen, "pt_gen_jet")
        fill2dhist(df_tmp_selrecogen, hzvsjetpt_reco_cuts, self.v_varshape_binning, "pt_jet")
        fill2dhist(df_tmp_selrecogen, hzvsjetpt_gen_cuts, self.v_varshape_binning_gen, "pt_gen_jet")

        hzvsjetpt_reco_nocuts.Write()
        hzvsjetpt_gen_nocuts.Write()
        hzvsjetpt_reco_cuts.Write()
        hzvsjetpt_gen_cuts.Write()

        # histograms for unfolding
        hzvsjetpt_reco_nocuts_pr = \
            build2dhisto("hzvsjetpt_reco_nocuts", zbinarray_reco, jetptbinarray_reco)
        hzvsjetpt_reco_cuts_pr = \
            build2dhisto("hzvsjetpt_reco_cuts", zbinarray_reco, jetptbinarray_reco)
        hzvsjetpt_gen_nocuts_pr = \
            build2dhisto("hzvsjetpt_gen_nocuts", zbinarray_gen, jetptbinarray_gen)
        hzvsjetpt_gen_cuts_pr = \
            build2dhisto("hzvsjetpt_gen_cuts", zbinarray_gen, jetptbinarray_gen)

        fill2dhist(df_tmp_selreco_pr, hzvsjetpt_reco_nocuts_pr, self.v_varshape_binning, "pt_jet")
        fill2dhist(df_tmp_selgen_pr, hzvsjetpt_gen_nocuts_pr, self.v_varshape_binning_gen, "pt_gen_jet")
        fill2dhist(df_tmp_selrecogen_pr, hzvsjetpt_reco_cuts_pr, self.v_varshape_binning, "pt_jet")
        fill2dhist(df_tmp_selrecogen_pr, hzvsjetpt_gen_cuts_pr, self.v_varshape_binning_gen, "pt_gen_jet")
        hzvsjetpt_reco_nocuts_pr.Write()
        hzvsjetpt_gen_nocuts_pr.Write()
        hzvsjetpt_reco_cuts_pr.Write()
        hzvsjetpt_gen_cuts_pr.Write()

        hzvsjetpt_reco_closure_pr = \
            build2dhisto("hzvsjetpt_reco_closure", zbinarray_reco, jetptbinarray_reco)
        hzvsjetpt_gen_closure_pr = \
            build2dhisto("hzvsjetpt_gen_closure", zbinarray_reco, jetptbinarray_reco)
        hzvsjetpt_reco_pr = \
            build2dhisto("hzvsjetpt_reco", zbinarray_reco, jetptbinarray_reco)
        hzvsjetpt_gen_pr = \
            build2dhisto("hzvsjetpt_gen", zbinarray_gen, jetptbinarray_gen)
        response_matrix_pr = RooUnfoldResponse(hzvsjetpt_reco_pr, hzvsjetpt_gen_pr)
        response_matrix_closure_pr = RooUnfoldResponse(hzvsjetpt_reco_pr, hzvsjetpt_gen_pr)

        fill2dhist(df_tmp_selreco_pr, hzvsjetpt_reco_pr, self.v_varshape_binning, "pt_jet")
        fill2dhist(df_tmp_selgen_pr, hzvsjetpt_gen_pr, self.v_varshape_binning_gen, "pt_gen_jet")
        hzvsjetpt_reco_pr.Write()
        hzvsjetpt_gen_pr.Write()

        hjetpt_gen_nocuts_pr = TH1F("hjetpt_gen_nocuts", \
            "hjetpt_gen_nocuts", njetptbin_gen, jetptbinarray_gen)
        hjetpt_gen_cuts_pr = TH1F("hjetpt_gen_cuts", \
            "hjetpt_gen_cuts", njetptbin_gen, jetptbinarray_gen)
        hjetpt_gen_nocuts_closure = TH1F("hjetpt_gen_nocuts_closure", \
            "hjetpt_gen_nocuts_closure", njetptbin_gen, jetptbinarray_gen)
        hjetpt_gen_cuts_closure = TH1F("hjetpt_gen_cuts_closure", \
            "hjetpt_gen_cuts_closure", njetptbin_gen, jetptbinarray_gen)
        hjetpt_gen_nocuts_pr.Sumw2()
        hjetpt_gen_cuts_pr.Sumw2()
        hjetpt_gen_nocuts_closure.Sumw2()
        hjetpt_gen_nocuts_closure.Sumw2()

        fill_hist(hjetpt_gen_nocuts_pr, df_tmp_selgen_pr["pt_gen_jet"])
        fill_hist(hjetpt_gen_cuts_pr, df_tmp_selrecogen_pr["pt_gen_jet"])
        hjetpt_gen_nocuts_pr.Write()
        hjetpt_gen_cuts_pr.Write()
        # end of histograms for unfolding

        hjetpt_genvsreco_full = \
            TH2F("hjetpt_genvsreco_full_nonprompt", "hjetpt_genvsreco_full_nonprompt", \
            njetptbin_gen * 100, self.lvar2_binmin_gen[0], self.lvar2_binmax_gen[-1], \
            njetptbin_reco * 100, self.lvar2_binmin_reco[0], self.lvar2_binmax_reco[-1])

        hz_genvsreco_full = \
            TH2F("hz_genvsreco_full_nonprompt", "hz_genvsreco_full_nonprompt", \
                 nzbin_gen * 100, self.lvarshape_binmin_gen[0], self.lvarshape_binmax_gen[-1],
                 nzbin_reco * 100, self.lvarshape_binmin_reco[0], self.lvarshape_binmax_reco[-1])

        fill2dhist(df_tmp_selrecogen, hjetpt_genvsreco_full, "pt_gen_jet", "pt_jet")
        hjetpt_genvsreco_full.Scale(1.0 / hjetpt_genvsreco_full.Integral(1, -1, 1, -1))
        hjetpt_genvsreco_full.Write()
        fill2dhist(df_tmp_selrecogen, hz_genvsreco_full, self.v_varshape_binning_gen, self.v_varshape_binning)
        hz_genvsreco_full.Scale(1.0 / hz_genvsreco_full.Integral(1, -1, 1, -1))
        hz_genvsreco_full.Write()
        for row in df_tmp_selrecogen.itertuples():
            response_matrix.Fill(getattr(row, self.v_varshape_binning), row.pt_jet, getattr(row, self.v_varshape_binning_gen), row.pt_gen_jet)
        response_matrix.Write("response_matrix_nonprompt")

        # histograms for unfolding
        hjetpt_genvsreco_full_pr = \
            TH2F("hjetpt_genvsreco_full", "hjetpt_genvsreco_full", \
            njetptbin_gen * 100, self.lvar2_binmin_gen[0], self.lvar2_binmax_gen[-1], \
            njetptbin_reco * 100, self.lvar2_binmin_reco[0], self.lvar2_binmax_reco[-1])

        hz_genvsreco_full_pr = \
            TH2F("hz_genvsreco_full", "hz_genvsreco_full", \
                 nzbin_gen * 100, self.lvarshape_binmin_gen[0], self.lvarshape_binmax_gen[-1],
                 nzbin_reco * 100, self.lvarshape_binmin_reco[0], self.lvarshape_binmax_reco[-1])
        fill2dhist(df_tmp_selrecogen_pr, hjetpt_genvsreco_full_pr, "pt_gen_jet", "pt_jet")
        hjetpt_genvsreco_full_pr.Scale(1.0 / hjetpt_genvsreco_full_pr.Integral(1, -1, 1, -1))
        hjetpt_genvsreco_full_pr.Write()
        fill2dhist(df_tmp_selrecogen_pr, hz_genvsreco_full_pr, self.v_varshape_binning_gen, self.v_varshape_binning)
        hz_genvsreco_full_pr.Scale(1.0 / hz_genvsreco_full_pr.Integral(1, -1, 1, -1))
        hz_genvsreco_full_pr.Write()


        hzvsjetpt_prior_weights = build2dhisto("hzvsjetpt_prior_weights", \
            zbinarray_gen, jetptbinarray_gen)
        fill2dhist(df_tmp_selrecogen_pr, hzvsjetpt_prior_weights, self.v_varshape_binning_gen, "pt_gen_jet")
        # end of histograms for unfolding

        for ibin2 in range(self.p_nbin2_reco):
            df_tmp_selrecogen_jetbin = seldf_singlevar(df_tmp_selrecogen, "pt_jet", \
                self.lvar2_binmin_reco[ibin2], self.lvar2_binmax_reco[ibin2])
            suffix = "%s_%.2f_%.2f" % (self.v_var2_binning, \
                self.lvar2_binmin_reco[ibin2], self.lvar2_binmax_reco[ibin2])
            hz_genvsreco = TH2F("hz_genvsreco_nonprompt" + suffix, "hz_genvsreco_nonprompt" + suffix, \
                nzbin_gen * 100, self.lvarshape_binmin_gen[0], self.lvarshape_binmax_gen[-1], \
                nzbin_reco*100, self.lvarshape_binmin_reco[0], self.lvarshape_binmax_reco[-1])
            fill2dhist(df_tmp_selrecogen_jetbin, hz_genvsreco, self.v_varshape_binning_gen, self.v_varshape_binning)
            norm = hz_genvsreco.Integral(1, -1, 1, -1)
            if norm > 0:
                hz_genvsreco.Scale(1.0/norm)
            hz_genvsreco.Write()

            df_tmp_selrecogen_pr_jetbin = seldf_singlevar(df_tmp_selrecogen_pr, "pt_jet", \
                self.lvar2_binmin_reco[ibin2], self.lvar2_binmax_reco[ibin2])
            suffix = "%s_%.2f_%.2f" % (self.v_var2_binning, \
                self.lvar2_binmin_reco[ibin2], self.lvar2_binmax_reco[ibin2])
            hz_genvsreco_pr = TH2F("hz_genvsreco" + suffix, "hz_genvsreco" + suffix, \
                nzbin_gen * 100, self.lvarshape_binmin_gen[0], self.lvarshape_binmax_gen[-1], \
                nzbin_reco*100, self.lvarshape_binmin_reco[0], self.lvarshape_binmax_reco[-1])
            fill2dhist(df_tmp_selrecogen_pr_jetbin, hz_genvsreco_pr, self.v_varshape_binning_gen, self.v_varshape_binning)
            norm_pr = hz_genvsreco_pr.Integral(1, -1, 1, -1)
            if norm_pr > 0:
                hz_genvsreco_pr.Scale(1.0/norm_pr)
            hz_genvsreco_pr.Write()

        for ibinshape in range(len(self.lvarshape_binmin_reco)):
            df_tmp_selrecogen_zbin = seldf_singlevar(df_tmp_selrecogen, self.v_varshape_binning, \
                self.lvarshape_binmin_reco[ibinshape], self.lvarshape_binmax_reco[ibinshape])
            suffix = "%s_%.2f_%.2f" % \
                (self.v_varshape_binning, self.lvarshape_binmin_reco[ibinshape], self.lvarshape_binmax_reco[ibinshape])
            hjetpt_genvsreco = TH2F("hjetpt_genvsreco_nonprompt" + suffix, \
                "hjetpt_genvsreco_nonprompt" + suffix, njetptbin_gen * 100, self.lvar2_binmin_gen[0], \
                self.lvar2_binmax_gen[-1], njetptbin_reco * 100, self.lvar2_binmin_reco[0], \
                self.lvar2_binmax_reco[-1])
            fill2dhist(df_tmp_selrecogen_zbin, hjetpt_genvsreco, "pt_gen_jet", "pt_jet")
            norm = hjetpt_genvsreco.Integral(1, -1, 1, -1)
            if norm > 0:
                hjetpt_genvsreco.Scale(1.0/norm)
            hjetpt_genvsreco.Write()

            df_tmp_selrecogen_pr_zbin = seldf_singlevar(df_tmp_selrecogen_pr, self.v_varshape_binning, \
                self.lvarshape_binmin_reco[ibinshape], self.lvarshape_binmax_reco[ibinshape])
            suffix = "%s_%.2f_%.2f" % \
                (self.v_varshape_binning, self.lvarshape_binmin_reco[ibinshape], self.lvarshape_binmax_reco[ibinshape])
            hjetpt_genvsreco_pr = TH2F("hjetpt_genvsreco" + suffix, \
                "hjetpt_genvsreco" + suffix, njetptbin_gen * 100, self.lvar2_binmin_gen[0], \
                self.lvar2_binmax_gen[-1], njetptbin_reco * 100, self.lvar2_binmin_reco[0], \
                self.lvar2_binmax_reco[-1])
            fill2dhist(df_tmp_selrecogen_pr_zbin, hjetpt_genvsreco_pr, "pt_gen_jet", "pt_jet")
            norm_pr = hjetpt_genvsreco_pr.Integral(1, -1, 1, -1)
            if norm_pr > 0:
                hjetpt_genvsreco_pr.Scale(1.0/norm_pr)
            hjetpt_genvsreco_pr.Write()

        for ibinshape in range(len(self.lvarshape_binmin_gen)):
            dtmp_nonprompt_zgen = seldf_singlevar(df_mc_reco_merged_nonprompt, \
                self.v_varshape_binning_gen, self.lvarshape_binmin_gen[ibinshape], self.lvarshape_binmax_gen[ibinshape])
            suffix = "%s_%.2f_%.2f" % \
                     (self.v_varshape_binning, self.lvarshape_binmin_gen[ibinshape], self.lvarshape_binmax_gen[ibinshape])
            hz_fracdiff = TH1F("hz_fracdiff_nonprompt" + suffix,
                               "hz_fracdiff_nonprompt" + suffix, 100, -2, 2)
            fill_hist(hz_fracdiff, (dtmp_nonprompt_zgen[self.v_varshape_binning] - \
                    dtmp_nonprompt_zgen[self.v_varshape_binning_gen])/dtmp_nonprompt_zgen[self.v_varshape_binning_gen])
            norm = hz_fracdiff.Integral(1, -1)
            if norm:
                hz_fracdiff.Scale(1.0 / norm)
            hz_fracdiff.Write()

            dtmp_prompt_zgen = seldf_singlevar(df_mc_reco_merged_prompt, \
                self.v_varshape_binning_gen, self.lvarshape_binmin_gen[ibinshape], self.lvarshape_binmax_gen[ibinshape])
            suffix = "%s_%.2f_%.2f" % \
                     (self.v_varshape_binning, self.lvarshape_binmin_gen[ibinshape], self.lvarshape_binmax_gen[ibinshape])
            hz_fracdiff_pr = TH1F("hz_fracdiff_prompt" + suffix,
                                  "hz_fracdiff_prompt" + suffix, 100, -2, 2)
            fill_hist(hz_fracdiff_pr, (dtmp_prompt_zgen[self.v_varshape_binning] - \
                    dtmp_prompt_zgen[self.v_varshape_binning_gen])/dtmp_prompt_zgen[self.v_varshape_binning_gen])
            norm_pr = hz_fracdiff_pr.Integral(1, -1)
            if norm_pr:
                hz_fracdiff_pr.Scale(1.0 / norm_pr)
            hz_fracdiff_pr.Write()

        for ibin2 in range(self.p_nbin2_gen):
            dtmp_nonprompt_jetptgen = seldf_singlevar(df_mc_reco_merged_nonprompt, \
                "pt_gen_jet", self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            suffix = "%s_%.2f_%.2f" % (self.v_var2_binning,
                                       self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            hjetpt_fracdiff = TH1F("hjetpt_fracdiff_nonprompt" + suffix,
                                   "hjetpt_fracdiff_nonprompt" + suffix, 100, -2, 2)
            fill_hist(hjetpt_fracdiff, (dtmp_nonprompt_jetptgen["pt_jet"] - \
                dtmp_nonprompt_jetptgen["pt_gen_jet"])/dtmp_nonprompt_jetptgen["pt_gen_jet"])
            norm = hjetpt_fracdiff.Integral(1, -1)
            if norm:
                hjetpt_fracdiff.Scale(1.0 / norm)
            hjetpt_fracdiff.Write()

            dtmp_prompt_jetptgen = seldf_singlevar(df_mc_reco_merged_prompt, \
                "pt_gen_jet", self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            suffix = "%s_%.2f_%.2f" % (self.v_var2_binning,
                                       self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            hjetpt_fracdiff_pr = TH1F("hjetpt_fracdiff_prompt" + suffix,
                                      "hjetpt_fracdiff_prompt" + suffix, 100, -2, 2)
            fill_hist(hjetpt_fracdiff_pr, (dtmp_prompt_jetptgen["pt_jet"] - \
                dtmp_prompt_jetptgen["pt_gen_jet"])/dtmp_prompt_jetptgen["pt_gen_jet"])
            norm_pr = hjetpt_fracdiff_pr.Integral(1, -1)
            if norm_pr:
                hjetpt_fracdiff_pr.Scale(1.0 / norm_pr)
            hjetpt_fracdiff_pr.Write()

        df_mc_reco_merged_prompt_train, df_mc_reco_merged_prompt_test = \
                train_test_split(df_mc_reco_merged_prompt, test_size=self.closure_frac)
        df_tmp_selgen_pr_test, df_tmp_selreco_pr_test, df_tmp_selrecogen_pr_test = \
                self.create_df_closure(df_mc_reco_merged_prompt_test)
        _, _, df_tmp_selrecogen_pr_train = \
                self.create_df_closure(df_mc_reco_merged_prompt_train)

        fill2dhist(df_tmp_selreco_pr_test, hzvsjetpt_reco_closure_pr, self.v_varshape_binning, "pt_jet")
        fill2dhist(df_tmp_selgen_pr_test, hzvsjetpt_gen_closure_pr, self.v_varshape_binning_gen, "pt_gen_jet")
        hzvsjetpt_reco_closure_pr.Write("input_closure_reco")
        hzvsjetpt_gen_closure_pr.Write("input_closure_gen")


        for ibin2 in range(self.p_nbin2_gen):
            suffix = "%s_%.2f_%.2f" % \
                (self.v_var2_binning, self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            hz_gen_nocuts_closure = TH1F("hz_gen_nocuts_closure" + suffix,
                                         "hz_gen_nocuts_closure" + suffix,
                                         nzbin_gen, zbinarray_gen)
            hz_gen_nocuts_closure.Sumw2()
            hz_gen_cuts_closure = TH1F("hz_gen_cuts_closure" + suffix,
                                       "hz_gen_cuts_closure" + suffix,
                                       nzbin_gen, zbinarray_gen)
            hz_gen_cuts_closure.Sumw2()
            df_tmp_selgen_pr_test_bin = seldf_singlevar(df_tmp_selgen_pr_test, \
                "pt_gen_jet", self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            df_tmp_selrecogen_pr_test_bin = seldf_singlevar(df_tmp_selrecogen_pr_test, \
                "pt_gen_jet", self.lvar2_binmin_gen[ibin2], self.lvar2_binmax_gen[ibin2])
            fill_hist(hz_gen_nocuts_closure, df_tmp_selgen_pr_test_bin[self.v_varshape_binning_gen])
            fill_hist(hz_gen_cuts_closure, df_tmp_selrecogen_pr_test_bin[self.v_varshape_binning_gen])
            hz_gen_cuts_closure.Write()
            hz_gen_nocuts_closure.Write()

        fill_hist(hjetpt_gen_nocuts_closure, df_tmp_selgen_pr_test["pt_gen_jet"])
        fill_hist(hjetpt_gen_cuts_closure, df_tmp_selrecogen_pr_test["pt_gen_jet"])
        hjetpt_gen_nocuts_closure.Write()
        hjetpt_gen_cuts_closure.Write()

        hzvsjetpt_reco_nocuts_closure = TH2F("hzvsjetpt_reco_nocuts_closure",
                                             "hzvsjetpt_reco_nocuts_closure",
                                             nzbin_reco, zbinarray_reco,
                                             njetptbin_reco, jetptbinarray_reco)
        hzvsjetpt_reco_nocuts_closure.Sumw2()
        hzvsjetpt_reco_cuts_closure = TH2F("hzvsjetpt_reco_cuts_closure",
                                           "hzvsjetpt_reco_cuts_closure",
                                           nzbin_reco, zbinarray_reco,
                                           njetptbin_reco, jetptbinarray_reco)
        hzvsjetpt_reco_cuts_closure.Sumw2()

        fill2dhist(df_tmp_selreco_pr_test, hzvsjetpt_reco_nocuts_closure, self.v_varshape_binning, "pt_jet")
        fill2dhist(df_tmp_selrecogen_pr_test, hzvsjetpt_reco_cuts_closure, self.v_varshape_binning, "pt_jet")
        hzvsjetpt_reco_nocuts_closure.Write()
        hzvsjetpt_reco_cuts_closure.Write()

        for row in df_tmp_selrecogen_pr.itertuples():
            response_matrix_weight = 1.0
            if self.doprior is True:
                binx = hzvsjetpt_prior_weights.GetXaxis().FindBin(getattr(row, self.v_varshape_binning_gen))
                biny = hzvsjetpt_prior_weights.GetYaxis().FindBin(row.pt_gen_jet)
                weight = hzvsjetpt_prior_weights.GetBinContent(binx, biny)

                if weight > 0.0:
                    response_matrix_weight = 1.0/weight
            response_matrix_pr.Fill(getattr(row, self.v_varshape_binning), row.pt_jet,\
                getattr(row, self.v_varshape_binning_gen), row.pt_gen_jet, response_matrix_weight)
        for row in df_tmp_selrecogen_pr_train.itertuples():
            response_matrix_weight = 1.0
            if self.doprior is True:
                binx = hzvsjetpt_prior_weights.GetXaxis().FindBin(getattr(row, self.v_varshape_binning_gen))
                biny = hzvsjetpt_prior_weights.GetYaxis().FindBin(row.pt_gen_jet)
                weight = hzvsjetpt_prior_weights.GetBinContent(binx, biny)

                if weight > 0.0:
                    response_matrix_weight = 1.0/weight
            response_matrix_closure_pr.Fill(getattr(row, self.v_varshape_binning), row.pt_jet,\
                getattr(row, self.v_varshape_binning_gen), row.pt_gen_jet, response_matrix_weight)
        response_matrix_pr.Write("response_matrix")
        response_matrix_closure_pr.Write("response_matrix_closure")

        out_file.Close()
Esempio n. 14
0
def MyRooUnfold(matrix_name=args.h_matrix, h_reco_getG0_name=args.h_data, h_ptcl_getG0_name = args.h_particle,h_reco_get_bkg_name = args.h_background,outputname=args.h_data+"_unfolded",nrebin = args.nrebin):

    rfile_data = TFile(args.rfile_data, 'read')
    rfile_particle = TFile(args.rfile_particle, 'read')
    rfile_matrix = TFile(args.rfile_matrix, 'read')
    rfile_background = TFile(args.rfile_background, 'read')

    myfbu = fbu.PyFBU()
    myfbu.verbose = True 
    #GET DATA
    h_reco_get = rfile_data.Get(h_reco_getG0_name)
    h_reco_get.Rebin(nrebin)
    #GET PARTICLE
    h_ptcl_get = rfile_particle.Get(h_ptcl_getG0_name)
    h_ptcl_get.Rebin(nrebin)
    #GET MATRIX
    h_response_unf = rfile_matrix.Get(matrix_name)
    h_response_unf.ClearUnderflowAndOverflow()
    h_response_unf.GetXaxis().SetRange(1, h_response_unf.GetXaxis().GetNbins() )
    h_response_unf.GetYaxis().SetRange(1, h_response_unf.GetYaxis().GetNbins() )
    h_response_unf.Rebin2D(nrebin,nrebin)
    h_response_unf.SetName("Migration_Matrix_simulation")

    ########### ACCEPTANCY
    h_acc = h_response_unf.ProjectionX("reco_recoandparticleX") # Reco M
    h_acc.Divide(h_reco_get)
    ########### AKCEPTANCE saved in h_acc #############
    ########### EFFICIENCY
    h_eff = h_response_unf.ProjectionY("reco_recoandparticleY") # Ptcl M
    h_eff.Divide(h_ptcl_get)
    
    h_reco_get_input = rfile_data.Get(h_reco_getG0_name)
    h_reco_get_bkg = rfile_background.Get(h_reco_get_bkg_name)
    h_reco_get_bkg.Rebin(nrebin)

    h_reco_get_input_clone=h_reco_get_input.Clone("")

    h_reco_get_input_clone.Add(h_reco_get_bkg,-1)
    h_reco_get_input_clone.Multiply(h_acc)
    
   
    h_reco_or = rfile_data.Get(h_reco_getG0_name)
    h_ptcl_or = rfile_particle.Get(h_ptcl_getG0_name)
    h_ptcl_or.SetMaximum(h_ptcl_or.GetMaximum()*1.5)
    
    ### ROOUNFOLD METHOD ###
    
    m_RooUnfold = RooUnfoldBayes()
    m_RooUnfold.SetRegParm( 4 )
    m_RooUnfold.SetNToys( 10000 )
    m_RooUnfold.SetVerbose( 0 )
    m_RooUnfold.SetSmoothing( 0 )
  
    response = RooUnfoldResponse(None, None, h_response_unf, "response", "methods")
    
    m_RooUnfold.SetResponse( response )
    m_RooUnfold.SetMeasured( h_reco_get_input_clone )
    
    ### SVD METHOD ###
    
    m_RooUnfold_svd = RooUnfoldSvd (response, h_reco_get_input_clone, int(round(h_reco_get_input_clone.GetNbinsX()/2.0,0))) #8
    svd_par = int(round(h_reco_get_input_clone.GetNbinsX()/2.0,0))
    m_RooUnfold_T = RooUnfoldTUnfold (response, h_reco_get_input_clone)         #  OR
    m_RooUnfold_Ids= RooUnfoldIds (response, h_reco_get_input_clone,int(round(h_reco_get_input_clone.GetNbinsX()/12.0,0))) ## TO DO, SET PARAMETERS TO THE BINNING
    Ids_par = int(round(h_reco_get_input_clone.GetNbinsX()/12.0,0))
    
    ### FBU METHOD ###
    
    h_response_unf_fbu = TransposeMatrix(h_response_unf)
    h_response_unf_fbu_norm = NormalizeResponse(h_response_unf_fbu)
    h_response_unf_fbu_norm.SetName("Migration_Matrix_simulation_transpose")
    histograms.append(h_response_unf_fbu_norm)
    myfbu.response = MakeListResponse(h_response_unf_fbu_norm)
    myfbu.data = MakeListFromHisto(h_reco_get_input_clone) 
    myfbu.lower = []
    myfbu.upper = []
    
    h_det_div_ptcl=h_reco_get_input_clone.Clone("")
    h_det_div_ptcl.Divide(h_ptcl_or)
    h_det_div_ptcl.Divide(h_eff)
    h_det_div_ptcl.SetName("det_div_ptcl")
    histograms.append(h_det_div_ptcl)

    for l in range(len(myfbu.data)):
        if ( args.SplitFromBinLow != 0) and ( l+1 <= args.SplitFromBinLow ):
            myfbu.lower.append(h_reco_get_input_clone.GetBinContent(l+1)*(2-args.ParameterSplitFromBinLow)*h_det_div_ptcl.GetBinContent(l+1))
            myfbu.upper.append(h_reco_get_input_clone.GetBinContent(l+1)*args.ParameterSplitFromBinLow*h_det_div_ptcl.GetBinContent(l+1))
        elif ( args.SplitFromBinHigh != 0 ) and ( l+1 >= args.SplitFromBinHigh ):
            myfbu.lower.append(h_reco_get_input_clone.GetBinContent(l+1)*(2-args.ParameterSplitFromBinHigh)*h_det_div_ptcl.GetBinContent(l+1))
            myfbu.upper.append(h_reco_get_input_clone.GetBinContent(l+1)*args.ParameterSplitFromBinHigh*h_det_div_ptcl.GetBinContent(l+1))
        else:
            myfbu.lower.append(h_reco_get_input_clone.GetBinContent(l+1)*(2-args.par)*h_det_div_ptcl.GetBinContent(l+1))
            myfbu.upper.append(h_reco_get_input_clone.GetBinContent(l+1)*args.par*h_det_div_ptcl.GetBinContent(l+1))
    #myfbu.regularization = Regularization('Tikhonov',parameters=[{'refcurv':0.1,'alpha':0.2}]) works for old FBU package and python2.7 and old pymc
    myfbu.run()
    trace = myfbu.trace
    traceName = 'Posterior_1_iteration'
    posteriors_diag = MakeTH1Ds(trace, traceName)
    h_reco_unfolded, h_reco_unfolded_Mean = MakeUnfoldedHisto(h_reco_or, posteriors_diag)
    PlotPosteriors(posteriors_diag,outputname+'_iteration_1')
    # EFFICIENCY AND ACCEPTANCY CORRECTIONS
    h_reco_unfolded.Divide(h_eff)
    h_reco_unfolded_Mean.Divide(h_eff)

    h_reco_unfolded_roof = m_RooUnfold.Hreco()
    h_reco_unfolded_roof.Divide(h_eff)

    h_reco_unfolded_svd = m_RooUnfold_svd.Hreco()
    h_reco_unfolded_svd.Divide(h_eff)

    h_reco_unfolded_T = m_RooUnfold_T.Hreco()
    h_reco_unfolded_T.Divide(h_eff)

    h_reco_unfolded_Ids = m_RooUnfold_Ids.Hreco()
    h_reco_unfolded_Ids.Divide(h_eff)

    PlotRatio(h_reco_unfolded_Mean, h_ptcl_or, h_reco_unfolded_roof, h_reco_unfolded_svd, h_reco_unfolded_T,h_reco_unfolded_Ids, svd_par, Ids_par, outputname+'_iteration_1')        

    Repeat = True
    j = 2
    while Repeat:
        print("Runnig iteration number: ",j)
        myfbu.lower = []
        myfbu.upper = []
        for l in range(len(myfbu.data)):
            posteriors_diag[l].Fit("gaus")
            fit = posteriors_diag[l].GetFunction("gaus") 
            p1 = fit.GetParameter(1)
            p2 = fit.GetParameter(2)
            myfbu.lower.append(p1-4*p2)
            myfbu.upper.append(p1+4*p2)
        myfbu.run()
        trace = myfbu.trace
        traceName = 'Posterior_'+str(j)+'_iteration'
        posteriors_diag = MakeTH1Ds(trace, traceName)
        h_reco_unfolded, h_reco_unfolded_Mean = MakeUnfoldedHisto(h_reco_or, posteriors_diag)
        Repeat = PlotPosteriors(posteriors_diag,outputname+'_iteration_'+str(j))
        # EFFICIENCY AND ACCEPTANCY CORRECTIONS
        h_reco_unfolded.Divide(h_eff)
        h_reco_unfolded_Mean.Divide(h_eff)
        h_reco_unfolded_roof = m_RooUnfold.Hreco()
        h_reco_unfolded_roof.Divide(h_eff)
        h_reco_unfolded_svd = m_RooUnfold_svd.Hreco()
        h_reco_unfolded_svd.Divide(h_eff)
        h_reco_unfolded_T = m_RooUnfold_T.Hreco()
        h_reco_unfolded_T.Divide(h_eff)
        h_reco_unfolded_Ids = m_RooUnfold_Ids.Hreco()
        h_reco_unfolded_Ids.Divide(h_eff)
        PlotRatio(h_reco_unfolded_Mean, h_ptcl_or, h_reco_unfolded_roof, h_reco_unfolded_svd, h_reco_unfolded_T,h_reco_unfolded_Ids, svd_par, Ids_par, outputname+'_iteration_'+str(j))
        if j == args.maxiterations:
            break
        j = j+1

    h_reco_unfolded.SetName("result_fbu_fit")
    histograms.append(h_reco_unfolded)
    
    h_reco_unfolded_Mean.SetName("result_fbu_Mean")
    histograms.append(h_reco_unfolded_Mean)
    
    h_reco_unfolded_roof.SetName("result_roof")
    histograms.append(h_reco_unfolded_roof)
    
    h_reco_unfolded_svd.SetName("result_svd")
    histograms.append(h_reco_unfolded_svd)
    
    h_reco_unfolded_T.SetName("result_T")
    histograms.append(h_reco_unfolded_T)
    
    h_reco_unfolded_Ids.SetName("result_Ids")
    histograms.append(h_reco_unfolded_Ids)

    h_eff.SetName("efficiency")
    histograms.append(h_eff)
    h_acc.SetName("acceptancy")
    histograms.append(h_acc)
    
    h_reco_or.SetName("reco")
    histograms.append(h_reco_or)
    h_ptcl_or.SetName("ptcl_simulation")
    histograms.append(h_ptcl_or)

    h_ratio = h_reco_unfolded.Clone("")
    h_ratio.Divide(h_ptcl_or)
    h_ratio.SetName("ratio_fbu_fit")
    histograms.append(h_ratio)
    
    h_ratio = h_reco_unfolded_Mean.Clone("")
    h_ratio.Divide(h_ptcl_or)
    h_ratio.SetName("ratio_fbu_Mean")
    histograms.append(h_ratio)

    h_ratio = h_reco_unfolded_roof.Clone("")
    h_ratio.Divide(h_ptcl_or)
    h_ratio.SetName("ratio_roof")
    histograms.append(h_ratio)
    
    h_ratio = h_reco_unfolded_svd.Clone("")
    h_ratio.Divide(h_ptcl_or)
    h_ratio.SetName("ratio_svd")
    histograms.append(h_ratio)

    m_RooUnfold_svd.PrintTable (cout, h_ptcl_or)
    m_RooUnfold.PrintTable (cout, h_ptcl_or)
  
    # CORRECTIONS TO GET CROSS SECTION

    #DivideBinWidth(h_reco_unfolded_Mean)
    #DivideBinWidth(h_reco_unfolded_roof)
    #DivideBinWidth(h_reco_unfolded_svd)
    #DivideBinWidth(h_reco_unfolded_T)
    #DivideBinWidth(h_reco_unfolded_Ids)
    #DivideBinWidth(h_ptcl_or)
    #Lumi = 36.1e3
    #for j in range(1,h_reco_unfolded_Mean.GetXaxis().GetNbins()+1):
    #    h_reco_unfolded_Mean.SetBinContent(j,h_reco_unfolded_Mean.GetBinContent(j)/(Lumi))
    #    h_reco_unfolded_Mean.SetBinError(j,h_reco_unfolded_Mean.GetBinError(j)/(Lumi))
    #    h_reco_unfolded_roof.SetBinContent(j,h_reco_unfolded_roof.GetBinContent(j)/(Lumi))
    #    h_reco_unfolded_roof.SetBinError(j,h_reco_unfolded_roof.GetBinError(j)/(Lumi))
    #    h_reco_unfolded_svd.SetBinContent(j,h_reco_unfolded_svd.GetBinContent(j)/(Lumi))
    #    h_reco_unfolded_svd.SetBinError(j,h_reco_unfolded_svd.GetBinError(j)/(Lumi))
    #    h_reco_unfolded_T.SetBinContent(j,h_reco_unfolded_T.GetBinContent(j)/(Lumi))
    #    h_reco_unfolded_T.SetBinError(j,h_reco_unfolded_T.GetBinError(j)/(Lumi))
    #    h_reco_unfolded_Ids.SetBinContent(j,h_reco_unfolded_Ids.GetBinContent(j)/(Lumi))
    #    h_reco_unfolded_Ids.SetBinError(j,h_reco_unfolded_Ids.GetBinError(j)/(Lumi))
    #    h_ptcl_or.SetBinContent(j,h_ptcl_or.GetBinContent(j)/(Lumi))
    #    h_ptcl_or.SetBinError(j,h_ptcl_or.GetBinError(j)/(Lumi))
    #h_reco_unfolded_Mean_clone=h_reco_unfolded_Mean.Clone("FBU_cross_section")
    #h_reco_unfolded_roof_clone=h_reco_unfolded_roof.Clone("DAgostini_cross_section")
    #h_reco_unfolded_svd_clone=h_reco_unfolded_svd.Clone("SVD_cross_section")
    #h_reco_unfolded_T_clone=h_reco_unfolded_T.Clone("TUnfold_cross_section")
    #h_reco_unfolded_Ids_clone=h_reco_unfolded_Ids.Clone("Ids_cross_section")
    #h_ptcl_or_clone=h_ptcl_or.Clone("Truth_cross_section")
    #
    #print("CONTROL*******************************************************************: ",h_reco_unfolded_Mean_clone.GetXaxis().GetNbins(),h_reco_unfolded_roof_clone.GetXaxis().GetNbins(),h_reco_unfolded_svd_clone.GetXaxis().GetNbins(),h_reco_unfolded_T_clone.GetXaxis().GetNbins(),h_reco_unfolded_Ids_clone.GetXaxis().GetNbins(),h_ptcl_or_clone.GetXaxis().GetNbins())
    #histograms.append(h_reco_unfolded_Mean_clone)
    #histograms.append(h_reco_unfolded_roof_clone)
    #histograms.append(h_reco_unfolded_svd_clone)
    #histograms.append(h_reco_unfolded_T_clone)
    #histograms.append(h_reco_unfolded_Ids_clone)
    #histograms.append(h_ptcl_or_clone)
    SaveHistograms(outputname)
Esempio n. 15
0
    def createToyTraining(self, rootFile, numberEvents):
        self._f = root_open(rootFile, "read")
        self._hJetPtIn = self._f.Get(
            "AliJJetJtTask/AliJJetJtHistManager/JetPt/JetPtNFin{:02d}".format(
                2))
        self._hZIn = self._f.Get(
            "AliJJetJtTask/AliJJetJtHistManager/Z/ZNFin{:02d}".format(2))
        LimL = 0.1
        LimH = 500
        logBW = (TMath.Log(LimH) - TMath.Log(LimL)) / self._NBINS
        self._LogBinsX = [
            LimL * math.exp(ij * logBW) for ij in range(0, self._NBINS + 1)
        ]

        self._hJetPtMeas = Hist(self._LogBinsX)
        self._hJetPtTrue = Hist(self._LogBinsX)

        self._myRandom = TRandom3(self._randomSeed)
        if self._fEff is None:
            self._fEff = TF1("fEff", "1-0.5*exp(-x)")
        if self._jetBinBorders is None:
            self._jetBinBorders = [5, 10, 20, 30, 40, 60, 80, 100, 150, 500]

        self._hJetPtMeasCoarse = Hist(self._jetBinBorders)
        self._hJetPtTrueCoarse = Hist(self._jetBinBorders)

        low = 0.01
        high = 10
        BinW = (TMath.Log(high) - TMath.Log(low)) / self._NBINSJt
        self._LogBinsJt = [
            low * math.exp(i * BinW) for i in range(self._NBINSJt + 1)
        ]
        self._jetBinBorders = self._jetBinBorders
        self._jetPtBins = [
            (a, b)
            for a, b in zip(self._jetBinBorders, self._jetBinBorders[1:])
        ]

        self._hJtTrue2D = Hist2D(self._LogBinsJt, self._jetBinBorders)
        self._hJtMeas2D = Hist2D(self._LogBinsJt, self._jetBinBorders)
        self._hJtFake2D = Hist2D(self._LogBinsJt, self._jetBinBorders)

        # Histogram to store jT with respect to the leading hadron
        self._hJtTestMeas2D = Hist2D(
            self._LogBinsJt, self._jetBinBorders)  # Needs a better name
        self._hJtTestTrue2D = Hist2D(
            self._LogBinsJt, self._jetBinBorders)  # Needs a better name

        self._responseJetPt = RooUnfoldResponse(self._hJetPtMeas,
                                                self._hJetPtTrue)
        self._responseJetPtCoarse = RooUnfoldResponse(self._hJetPtMeasCoarse,
                                                      self._hJetPtTrueCoarse)
        self._hresponseJetPt = Hist2D(self._jetBinBorders, self._jetBinBorders)
        self._hresponseJetPtCoarse = Hist2D(self._jetBinBorders,
                                            self._jetBinBorders)
        # 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
        self._2Dresponses = [
            Hist3D(self._LogBinsJt, self._jetBinBorders, self._LogBinsJt)
            for i in self._jetPtBins
        ]
        self._misses2D = Hist2D(self._LogBinsJt, self._jetBinBorders)
        self._fakes2D = Hist2D(self._LogBinsJt, self._jetBinBorders)

        self._numberJetsMeasBin = [0 for i in self._jetBinBorders]
        self._numberJetsTrueBin = [0 for i in self._jetBinBorders]
        self._numberJetsTestMeasBin = [0 for i in self._jetBinBorders]
        self._numberJetsTestTrueBin = [0 for i in self._jetBinBorders]
        self._numberFakesBin = [0 for i in self._jetBinBorders]
        ieout = numberEvents / 10
        if ieout > 20000:
            ieout = 20000
        start_time = datetime.now()
        print("Processing MC 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 = self._hJetPtIn.GetRandom()
            remainder = jetPt
            if jetPt < 5:
                continue
            nt = 0
            nt_meas = 0
            while remainder > 0:
                trackPt = self._hZIn.GetRandom() * jetPt
                if trackPt < remainder:
                    track = TVector3()
                    remainder = remainder - trackPt
                else:
                    trackPt = remainder
                    remainder = -1
                if trackPt > 0.15:
                    track.SetPtEtaPhi(
                        trackPt,
                        self._myRandom.Gaus(0, 0.1),
                        self._myRandom.Gaus(math.pi, 0.2),
                    )
                    tracksTrue.append(track)
                    jetTrue += track
                    if self._fEff.Eval(trackPt) > self._myRandom.Uniform(0, 1):
                        tracksMeas[nt] = 1
                        jetMeas += track
                        nt_meas += 1
                    else:
                        tracksMeas[nt] = 0
                    nt += 1
            fakes = []
            for it in range(self._fakeRate * 100):
                if self._myRandom.Uniform(0, 1) > 0.99:
                    fake = TVector3()
                    fake.SetPtEtaPhi(
                        self._myRandom.Uniform(0.15, 1),
                        self._myRandom.Gaus(0, 0.1),
                        self._myRandom.Gaus(math.pi, 0.2),
                    )
                    fakes.append(fake)
                    jetMeas += fake

            self._responseJetPt.Fill(jetMeas.Pt(), jetTrue.Pt())
            self._responseJetPtCoarse.Fill(jetMeas.Pt(), jetTrue.Pt())
            self._hresponseJetPt.Fill(jetMeas.Pt(), jetTrue.Pt())
            self._hresponseJetPtCoarse.Fill(jetMeas.Pt(), jetTrue.Pt())
            ij_meas = GetBin(self._jetBinBorders, jetMeas.Pt())
            ij_true = GetBin(self._jetBinBorders, jetTrue.Pt())
            if nt < 5 or nt_meas < 5:
                continue
            if ij_meas >= 0:
                self._numberJetsMeasBin[ij_meas] += 1
            if ij_true >= 0:
                self._numberJetsTrueBin[ij_true] += 1
            for track, it in zip(tracksTrue, range(100)):
                zTrue = (track * jetTrue.Unit()) / jetTrue.Mag()
                jtTrue = (track - scaleJet(jetTrue, zTrue)).Mag()
                if ij_meas >= 0:
                    if tracksMeas[it] == 1:
                        zMeas = (track * jetMeas.Unit()) / jetMeas.Mag()
                        jtMeas = (track - scaleJet(jetMeas, zMeas)).Mag()
                        self._2Dresponses[ij_true].Fill(
                            jtMeas, jetMeas.Pt(), jtTrue)
                    else:
                        self._misses2D.Fill(jtTrue, jetTrue.Pt())
            if ij_meas >= 0:
                for fake in fakes:
                    zFake = (fake * jetMeas.Unit()) / jetMeas.Mag()
                    jtFake = (fake - scaleJet(jetMeas, zFake)).Mag()
                    if self._weight:
                        self._hJtFake2D.Fill(jtFake, jetMeas.Pt(),
                                             1.0 / jtFake)
                    else:
                        self._hJtFake2D.Fill(jtFake, jetMeas.Pt())
                    if self._fillFakes:
                        self._fakes2D.Fill(jtFake, jetMeas.Pt())
        print("Event {} [{:.2f}%] Time Elapsed: {}".format(
            numberEvents, 100.0, fmtDelta(time_elapsed)))
        self._numberJetsMeasTrain = sum(self._numberJetsMeasBin)
Esempio n. 16
0
class JtUnfolder(object):
    def __init__(self, name, **kwargs):
        self._name = name
        print("Create Unfolder {}".format(name))
        self._Njets = kwargs.get("Njets", 0)
        self._fEff = None
        self._jetBinBorders = kwargs.get("jetBinBorders", None)
        if self._jetBinBorders is not None:
            self._jetPtBins = [
                (a, b)
                for a, b in zip(self._jetBinBorders, self._jetBinBorders[1:])
            ]
        self._fakeRate = kwargs.get("fakeRate", 1)
        self._randomSeed = kwargs.get("randomSeed", 123)
        self._weight = kwargs.get("weight", True)
        self._fillFakes = kwargs.get("fillFakes", False)
        self._NBINSJt = kwargs.get("NBINSJt", 64)
        self._NBINS = kwargs.get("NBINS", 64)
        self._responseJetPt = None
        self._responseJetPtCoarse = None
        self._IsData = kwargs.get("Data", False)
        self._hJtTrue2D = None
        self._Niter = kwargs.get("Iterations", 4)
        self._NFin = kwargs.get("NFin", 0)
        self._hBgJt = None
        self._hBgJtNormalized = None
        self._numberBackgroundBin = None

    def fill_jt_histogram(self, histo, jt, pt):
        if self._weight:
            histo.Fill(jt, pt, 1.0 / jt)
        else:
            histo.Fill(jt, pt)

    def setTrackMatch(self, hists):
        self._matching = hists

    def setJtBins(self, bins):
        self._LogBinsJt = bins

    def setPtBins(self, bins):
        self._LogBinsX = bins

    def setNbinsJt(self, nbins):
        self._NBINSJt = nbins

    def setNbinsPt(self, nbins):
        self._NBINS = nbins

    def setRandomSeed(self, seed):
        self._randomSeed = seed

    def setJtTestMeas2D(self, hJtMeas):
        self._hJtTestMeas2D = hJtMeas

    def setJtTestTrue2D(self, hJtTrue):
        self._hJtTestTrue2D = hJtTrue

    def setJtMeas2D(self, hJtMeas):
        self._hJtMeas2D = hJtMeas

    def setJtTrue2D(self, hJtTrue):
        self._hJtTrue2D = hJtTrue

    def setJetPtMeas(self, hPtMeas):
        self._hJetPtMeas = hPtMeas

    def setJetPtTrue(self, hPtTrue):
        self._hJetPtTrue = hPtTrue

    def setJetPtMeasCoarse(self, hPtMeas):
        self._hJetPtMeasCoarse = hPtMeas

    def setJetPtTrueCoarse(self, hPtTrue):
        self._hJetPtTrueCoarse = hPtTrue

    def setFakeRate(self, rate):
        self._fakeRate = rate

    def setWeight(self, weight):
        self._weight = weight

    def setMisses2D(self, misses):
        self._misses2D = misses

    def setFakes2D(self, fakes):
        self._hJtFake2D = fakes

    def setJetPtResponseHisto(self, hresponse):
        self._hresponseJetPt = hresponse

    def setJtBackground(self, background):
        self._hBgJt = background
        if self._numberBackgroundBin is not None:
            self._hBgJtNormalized = normalize(self._hBgJt,
                                              self._numberBackgroundBin)
            print("Jt background normalized")

    def setJtBackgroundNumbers(self, n):
        self._numberBackgroundBin = n
        if self._hBgJt is not None:
            self._hBgJtNormalized = normalize(self._hBgJt,
                                              self._numberBackgroundBin)
            print("Jt background normalized")

    def setJetPtResponseCoarseHisto(self, hresponse):
        self._hresponseJetPtCoarse = hresponse

    def setJetPtResponse(self, response):
        self._responseJetPt = response

    def setJetPtResponseCoarse(self, response):
        self._responseJetPtCoarse = response

    def setNumberJetsMeas(self, n):
        self._numberJetsMeasBin = n

    def setNumberJetsTestMeas(self, n):
        self._numberJetsTestMeasBin = n

    def setNumberJetsTrue(self, n):
        self._numberJetsTrueBin = n

    def setNumberJetsTestTrue(self, n):
        self._numberJetsTestTrueBin = n

    def setNumberJetsMeasTrain(self, n):
        self._numberJetsMeasTrain = n

    def set2Dresponse(self, responses):
        self._2Dresponses = responses

    def drawTrackMatch(self, name, option):
        drawing.drawMatchHisto(self._matching, self._jetPtBins, name, option)

    def createToyTraining(self, rootFile, numberEvents):
        self._f = root_open(rootFile, "read")
        self._hJetPtIn = self._f.Get(
            "AliJJetJtTask/AliJJetJtHistManager/JetPt/JetPtNFin{:02d}".format(
                2))
        self._hZIn = self._f.Get(
            "AliJJetJtTask/AliJJetJtHistManager/Z/ZNFin{:02d}".format(2))
        LimL = 0.1
        LimH = 500
        logBW = (TMath.Log(LimH) - TMath.Log(LimL)) / self._NBINS
        self._LogBinsX = [
            LimL * math.exp(ij * logBW) for ij in range(0, self._NBINS + 1)
        ]

        self._hJetPtMeas = Hist(self._LogBinsX)
        self._hJetPtTrue = Hist(self._LogBinsX)

        self._myRandom = TRandom3(self._randomSeed)
        if self._fEff is None:
            self._fEff = TF1("fEff", "1-0.5*exp(-x)")
        if self._jetBinBorders is None:
            self._jetBinBorders = [5, 10, 20, 30, 40, 60, 80, 100, 150, 500]

        self._hJetPtMeasCoarse = Hist(self._jetBinBorders)
        self._hJetPtTrueCoarse = Hist(self._jetBinBorders)

        low = 0.01
        high = 10
        BinW = (TMath.Log(high) - TMath.Log(low)) / self._NBINSJt
        self._LogBinsJt = [
            low * math.exp(i * BinW) for i in range(self._NBINSJt + 1)
        ]
        self._jetBinBorders = self._jetBinBorders
        self._jetPtBins = [
            (a, b)
            for a, b in zip(self._jetBinBorders, self._jetBinBorders[1:])
        ]

        self._hJtTrue2D = Hist2D(self._LogBinsJt, self._jetBinBorders)
        self._hJtMeas2D = Hist2D(self._LogBinsJt, self._jetBinBorders)
        self._hJtFake2D = Hist2D(self._LogBinsJt, self._jetBinBorders)

        # Histogram to store jT with respect to the leading hadron
        self._hJtTestMeas2D = Hist2D(
            self._LogBinsJt, self._jetBinBorders)  # Needs a better name
        self._hJtTestTrue2D = Hist2D(
            self._LogBinsJt, self._jetBinBorders)  # Needs a better name

        self._responseJetPt = RooUnfoldResponse(self._hJetPtMeas,
                                                self._hJetPtTrue)
        self._responseJetPtCoarse = RooUnfoldResponse(self._hJetPtMeasCoarse,
                                                      self._hJetPtTrueCoarse)
        self._hresponseJetPt = Hist2D(self._jetBinBorders, self._jetBinBorders)
        self._hresponseJetPtCoarse = Hist2D(self._jetBinBorders,
                                            self._jetBinBorders)
        # 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
        self._2Dresponses = [
            Hist3D(self._LogBinsJt, self._jetBinBorders, self._LogBinsJt)
            for i in self._jetPtBins
        ]
        self._misses2D = Hist2D(self._LogBinsJt, self._jetBinBorders)
        self._fakes2D = Hist2D(self._LogBinsJt, self._jetBinBorders)

        self._numberJetsMeasBin = [0 for i in self._jetBinBorders]
        self._numberJetsTrueBin = [0 for i in self._jetBinBorders]
        self._numberJetsTestMeasBin = [0 for i in self._jetBinBorders]
        self._numberJetsTestTrueBin = [0 for i in self._jetBinBorders]
        self._numberFakesBin = [0 for i in self._jetBinBorders]
        ieout = numberEvents / 10
        if ieout > 20000:
            ieout = 20000
        start_time = datetime.now()
        print("Processing MC 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 = self._hJetPtIn.GetRandom()
            remainder = jetPt
            if jetPt < 5:
                continue
            nt = 0
            nt_meas = 0
            while remainder > 0:
                trackPt = self._hZIn.GetRandom() * jetPt
                if trackPt < remainder:
                    track = TVector3()
                    remainder = remainder - trackPt
                else:
                    trackPt = remainder
                    remainder = -1
                if trackPt > 0.15:
                    track.SetPtEtaPhi(
                        trackPt,
                        self._myRandom.Gaus(0, 0.1),
                        self._myRandom.Gaus(math.pi, 0.2),
                    )
                    tracksTrue.append(track)
                    jetTrue += track
                    if self._fEff.Eval(trackPt) > self._myRandom.Uniform(0, 1):
                        tracksMeas[nt] = 1
                        jetMeas += track
                        nt_meas += 1
                    else:
                        tracksMeas[nt] = 0
                    nt += 1
            fakes = []
            for it in range(self._fakeRate * 100):
                if self._myRandom.Uniform(0, 1) > 0.99:
                    fake = TVector3()
                    fake.SetPtEtaPhi(
                        self._myRandom.Uniform(0.15, 1),
                        self._myRandom.Gaus(0, 0.1),
                        self._myRandom.Gaus(math.pi, 0.2),
                    )
                    fakes.append(fake)
                    jetMeas += fake

            self._responseJetPt.Fill(jetMeas.Pt(), jetTrue.Pt())
            self._responseJetPtCoarse.Fill(jetMeas.Pt(), jetTrue.Pt())
            self._hresponseJetPt.Fill(jetMeas.Pt(), jetTrue.Pt())
            self._hresponseJetPtCoarse.Fill(jetMeas.Pt(), jetTrue.Pt())
            ij_meas = GetBin(self._jetBinBorders, jetMeas.Pt())
            ij_true = GetBin(self._jetBinBorders, jetTrue.Pt())
            if nt < 5 or nt_meas < 5:
                continue
            if ij_meas >= 0:
                self._numberJetsMeasBin[ij_meas] += 1
            if ij_true >= 0:
                self._numberJetsTrueBin[ij_true] += 1
            for track, it in zip(tracksTrue, range(100)):
                zTrue = (track * jetTrue.Unit()) / jetTrue.Mag()
                jtTrue = (track - scaleJet(jetTrue, zTrue)).Mag()
                if ij_meas >= 0:
                    if tracksMeas[it] == 1:
                        zMeas = (track * jetMeas.Unit()) / jetMeas.Mag()
                        jtMeas = (track - scaleJet(jetMeas, zMeas)).Mag()
                        self._2Dresponses[ij_true].Fill(
                            jtMeas, jetMeas.Pt(), jtTrue)
                    else:
                        self._misses2D.Fill(jtTrue, jetTrue.Pt())
            if ij_meas >= 0:
                for fake in fakes:
                    zFake = (fake * jetMeas.Unit()) / jetMeas.Mag()
                    jtFake = (fake - scaleJet(jetMeas, zFake)).Mag()
                    if self._weight:
                        self._hJtFake2D.Fill(jtFake, jetMeas.Pt(),
                                             1.0 / jtFake)
                    else:
                        self._hJtFake2D.Fill(jtFake, jetMeas.Pt())
                    if self._fillFakes:
                        self._fakes2D.Fill(jtFake, jetMeas.Pt())
        print("Event {} [{:.2f}%] Time Elapsed: {}".format(
            numberEvents, 100.0, fmtDelta(time_elapsed)))
        self._numberJetsMeasTrain = sum(self._numberJetsMeasBin)

    def createToyData(self, rootFile, numberEvents):
        self._f = root_open(rootFile, "read")
        self._hJetPtIn = self._f.Get(
            "AliJJetJtTask/AliJJetJtHistManager/JetPt/JetPtNFin{:02d}".format(
                2))
        self._hZIn = self._f.Get(
            "AliJJetJtTask/AliJJetJtHistManager/Z/ZNFin{:02d}".format(2))
        if self._fEff is None:
            self._fEff = TF1("fEff", "1-0.5*exp(-x)")
        if self._jetBinBorders is None:
            self._jetBinBorders = [5, 10, 20, 30, 40, 60, 80, 100, 150, 500]
        self._hMultiTrue = Hist(50, 0, 50)
        self._hMultiMeas = Hist(50, 0, 50)
        self._hZMeas = Hist(50, 0, 1)
        self._hZTrue = Hist(50, 0, 1)
        self._hZFake = Hist(50, 0, 1)
        self._numberJetsMeasBin = [0 for i in self._jetBinBorders]
        self._numberJetsTrueBin = [0 for i in self._jetBinBorders]
        print("Create testing data")
        start_time = datetime.now()
        numberEvents = numberEvents
        ieout = numberEvents / 10
        jtLeadingMeas = 0
        if ieout > 20000:
            ieout = 20000
        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 = self._hJetPtIn.GetRandom()
            remainder = jetPt
            if jetPt < 5:
                continue
            nt = 0
            nt_meas = 0
            leading = None
            leadingPt = 0
            while remainder > 0:
                trackPt = self._hZIn.GetRandom() * jetPt
                if trackPt < remainder:
                    track = TVector3()
                    remainder = remainder - trackPt
                else:
                    trackPt = remainder
                    remainder = -1
                if trackPt > 0.15:
                    track.SetPtEtaPhi(
                        trackPt,
                        self._myRandom.Gaus(0, 0.1),
                        self._myRandom.Gaus(math.pi, 0.2),
                    )
                    if track.Pt() > leadingPt:
                        leading = track
                        leadingPt = leading.Pt()
                    tracksTrue.append(track)
                    jetTrue += track
                    if self._fEff.Eval(trackPt) > self._myRandom.Uniform(0, 1):
                        tracksMeas[nt] = 1
                        jetMeas += track
                        nt_meas += 1
                    else:
                        tracksMeas[nt] = 0
                    nt += 1
            fakes = []
            if leadingPt > 0.25 * jetPt:
                doLeading = True
            else:
                doLeading = False
            for it in range(self._fakeRate * 100):
                if self._myRandom.Uniform(0, 1) > 0.99:
                    fake = TVector3()
                    fake.SetPtEtaPhi(
                        self._myRandom.Uniform(0.15, 1),
                        self._myRandom.Gaus(0, 0.1),
                        self._myRandom.Gaus(math.pi, 0.2),
                    )
                    fakes.append(fake)
                    jetMeas += fake
            self._hJetPtMeas.Fill(jetMeas.Pt())
            self._hJetPtTrue.Fill(jetTrue.Pt())
            self._hMultiTrue.Fill(nt)
            self._hMultiMeas.Fill(nt_meas)
            ij_meas = GetBin(self._jetBinBorders, jetMeas.Pt())
            ij_true = GetBin(self._jetBinBorders, jetTrue.Pt())
            if nt < 5 or nt_meas < 5:
                continue
            if ij_meas >= 0:
                self._numberJetsMeasBin[ij_meas] += 1
                self._hJetPtMeasCoarse.Fill(jetMeas.Pt())
                if doLeading:
                    self._numberJetsTestMeasBin[ij_meas] += 1
            if ij_true >= 0:
                self._numberJetsTrueBin[ij_true] += 1
                self._hJetPtTrueCoarse.Fill(jetTrue.Pt())
                if doLeading:
                    self._numberJetsTestTrueBin[ij_true] += 1
            for track, it in zip(tracksTrue, range(100)):
                zTrue = (track * jetTrue.Unit()) / jetTrue.Mag()
                jtTrue = (track - scaleJet(jetTrue, zTrue)).Mag()
                self._hZTrue.Fill(zTrue)
                if track.Pt() < 0.95 * leadingPt and doLeading:
                    zLeadingTrue = (track * leading.Unit()) / leading.Mag()
                    jtLeadingTrue = (track -
                                     scaleJet(leading, zLeadingTrue)).Mag()
                if ij_true >= 0:
                    self.fill_jt_histogram(self._hJtTrue2D, jtTrue,
                                           jetTrue.Pt())
                    if (track.Pt() < 0.95 * leading.Pt()) and doLeading:
                        self.fill_jt_histogram(self._hJtTestTrue2D,
                                               jtLeadingTrue, jetTrue.Pt())
                if ij_meas >= 0 and tracksMeas[it] == 1:
                    zMeas = (track * jetMeas.Unit()) / jetMeas.Mag()
                    jtMeas = (track - scaleJet(jetMeas, zMeas)).Mag()
                    if track.Pt() < 0.95 * leading.Pt():
                        zLeadingMeas = (track * leading.Unit()) / leading.Mag()
                        jtLeadingMeas = (
                            track - scaleJet(leading, zLeadingMeas)).Mag()
                    self._hZMeas.Fill(zMeas)

                    self.fill_jt_histogram(self._hJtMeas2D, jtMeas,
                                           jetMeas.Pt())
                    if (track.Pt() < 0.95 * leadingPt and doLeading
                            and jtLeadingMeas > 0):
                        self.fill_jt_histogram(self._hJtTestMeas2D,
                                               jtLeadingMeas, jetMeas.Pt())

            if ij_meas < 0:
                continue

            for fake in fakes:
                zFake = (fake * jetMeas.Unit()) / jetMeas.Mag()
                jtFake = (fake - scaleJet(jetMeas, zFake)).Mag()
                zLeadingFake = (fake * leading.Unit()) / leading.Mag()
                jtLeadingFake = (fake - scaleJet(leading, zLeadingFake)).Mag()
                self._hZMeas.Fill(zFake)
                self._hZFake.Fill(zFake)
                self.fill_jt_histogram(self._hJtMeas2D, jtFake, jetMeas.Pt())
                self.fill_jt_histogram(self._hJtTestMeas2D, jtLeadingFake,
                                       leadingPt)
                # self.fill_jt_histogram(self._hJtFake2D, jtFake,jetMeas.Pt())

        time_elapsed = datetime.now() - start_time
        print("Event {} [{:.2f}%] Time Elapsed: {}".format(
            numberEvents, 100.0, fmtDelta(time_elapsed)))

    def unfold(self):
        if self._fillFakes:
            fakes = self._hJtFake2D
        else:
            fakes = None
        if self._hJtTrue2D:
            self._response2D = make2Dresponse(
                self._2Dresponses,
                self._jetPtBins,
                self._hJtMeas2D,
                self._hJtTrue2D,
                misses=self._misses2D,
                fakes=fakes,
            )
        else:
            self._response2D = make2Dresponse(
                self._2Dresponses,
                self._jetPtBins,
                self._hJtMeas2D,
                self._hJtMeas2D,
                misses=self._misses2D,
                fakes=fakes,
            )
        if not self._fillFakes:
            print("Scale hJtFake2D by {}".format(1.0 *
                                                 sum(self._numberJetsMeasBin) /
                                                 self._numberJetsMeasTrain))
            self._hJtFake2D.Scale(1.0 * sum(self._numberJetsMeasBin) /
                                  self._numberJetsMeasTrain)
            self._hJtMeas2D.Add(self._hJtFake2D, -1)

        self._hJtFakeProjBin = [
            makeHist(
                self._hJtFake2D.ProjectionX("histFake{}".format(i), i, i),
                bins=self._LogBinsJt,
            ) for i in range(1, len(self._jetBinBorders))
        ]

        for h, nj in zip(self._hJtFakeProjBin, self._numberJetsMeasBin):
            if nj > 0:
                h.Scale(1.0 / nj, "width")
        if self._responseJetPtCoarse is None:
            print(self._responseJetPtCoarse)
            self._responseJetPtCoarse = createResponse(
                self._hJetPtMeasCoarse, self._hresponseJetPtCoarse)
        if self._responseJetPt is None:
            self._responseJetPt = createResponse(self._hJetPtMeas,
                                                 self._hresponseJetPt)
        self._hJetPtRecoCoarse = unfoldJetPt(self._hJetPtMeasCoarse,
                                             self._responseJetPtCoarse,
                                             self._jetBinBorders)
        self._hJetPtReco = unfoldJetPt(self._hJetPtMeas, self._responseJetPt,
                                       self._LogBinsX)

        self._numberJetsMeasFromHist = [
            self._hJetPtMeasCoarse.GetBinContent(i)
            for i in range(1,
                           self._hJetPtMeasCoarse.GetNbinsX() + 1)
        ]
        if not self._IsData:
            self._numberJetsTrueFromHist = [
                self._hJetPtTrueCoarse.GetBinContent(i)
                for i in range(1,
                               self._hJetPtTrueCoarse.GetNbinsX() + 1)
            ]
        self._numberJetsFromReco = [
            self._hJetPtRecoCoarse.GetBinContent(i)
            for i in range(1, self._hJetPtRecoCoarse.GetNbinsX())
        ]
        self.printJetNumbers()

        unfold2D = RooUnfoldBayes(self._response2D, self._hJtMeas2D,
                                  self._Niter)
        self._hJtReco2D = make2DHist(unfold2D.Hreco(),
                                     xbins=self._LogBinsJt,
                                     ybins=self._jetBinBorders)
        self._hJtMeasBin = [
            makeHist(
                self._hJtMeas2D.ProjectionX("histMeas{}".format(i), i, i),
                bins=self._LogBinsJt,
            ) for i in range(1, len(self._jetBinBorders))
        ]
        if not self._IsData:
            self._hJtTestMeasBin = [
                makeHist(
                    self._hJtTestMeas2D.ProjectionX("histTestMeas{}".format(i),
                                                    i, i),
                    bins=self._LogBinsJt,
                ) for i in range(1, len(self._jetBinBorders))
            ]
            self._hJtTrueBin = [
                makeHist(
                    self._hJtTrue2D.ProjectionX("histMeas{}".format(i), i, i),
                    bins=self._LogBinsJt,
                ) for i in range(1, len(self._jetBinBorders))
            ]
            self._hJtTestTrueBin = [
                makeHist(
                    self._hJtTestTrue2D.ProjectionX("histMeas{}".format(i), i,
                                                    i),
                    bins=self._LogBinsJt,
                ) for i in range(1, len(self._jetBinBorders))
            ]
        self._hJtRecoBin = [
            makeHist(
                self._hJtReco2D.ProjectionX("histMeas{}".format(i), i, i),
                bins=self._LogBinsJt,
            ) for i in range(1, len(self._jetBinBorders))
        ]
        for h, n, in zip(
                self._hJtMeasBin,
                self._numberJetsMeasFromHist,
        ):
            if n > 0:
                h.Scale(1.0 / n, "width")
        if not self._IsData:
            for h, h2, h3, n, n2, n3 in zip(
                    self._hJtTrueBin,
                    self._hJtTestTrueBin,
                    self._hJtTestMeasBin,
                    self._numberJetsTrueFromHist,
                    self._numberJetsTestTrueBin,
                    self._numberJetsTestMeasBin,
            ):
                if n > 0:
                    h.Scale(1.0 / n, "width")
                if n2 > 0:
                    h2.Scale(1.0 / n2, "width")
                if n3 > 0:
                    h3.Scale(1.0 / n3, "width")
        for h, n in zip(self._hJtRecoBin, self._numberJetsFromReco):
            if n > 0:
                h.Scale(1.0 / n, "width")

    def plotJetPt(self):
        if self._IsData:
            drawing.drawJetPt(
                self._hJetPtMeas,
                None,
                self._hJetPtReco,
                filename="JetPtUnfoldedData.pdf",
            )
            drawing.drawJetPt(
                self._hJetPtMeasCoarse,
                None,
                self._hJetPtRecoCoarse,
                filename="JetPtCoarseUnfoldedData.pdf",
            )
        else:
            drawing.drawJetPt(
                self._hJetPtMeas,
                self._hJetPtTrue,
                self._hJetPtReco,
                filename="JetPtUnfolded.pdf",
            )
            drawing.drawJetPt(
                self._hJetPtMeasCoarse,
                self._hJetPtTrueCoarse,
                self._hJetPtRecoCoarse,
                filename="JetPtCoarseUnfolded.pdf",
            )

    def plotJt(self, filename, **kwargs):
        nRebin = kwargs.get("Rebin", 1)
        histlist = [self._hJtMeasBin, self._hJtRecoBin, self._hJtFakeProjBin]
        if not self._IsData:
            histlist.append(self._hJtTrueBin)
        if self._hBgJtNormalized is not None:
            histlist.append(self._hBgJtNormalized)
            bg = self._hBgJtNormalized
        else:
            bg = None
        if nRebin > 0:
            for hists in histlist:
                for h in hists:
                    h.Rebin(nRebin)
                    h.Scale(1.0 / nRebin)
        if self._IsData:
            drawing.draw8gridcomparison(
                self._hJtMeasBin,
                None,
                self._jetPtBins,
                xlog=True,
                ylog=True,
                name="{}.pdf".format(filename),
                unf2d=self._hJtRecoBin,
                start=4,
                stride=1,
                backgroundJt=bg,
            )
            drawing.draw8gridcomparison(
                self._hJtMeasBin,
                None,
                self._jetPtBins,
                xlog=True,
                ylog=True,
                name="{}_Extra.pdf".format(filename),
                unf2d=self._hJtRecoBin,
                start=0,
                stride=1,
                backgroundJt=bg,
            )
        else:
            drawing.draw8gridcomparison(
                self._hJtMeasBin,
                self._hJtTrueBin,
                self._jetPtBins,
                xlog=True,
                ylog=True,
                name="{}.pdf".format(filename),
                unf2d=self._hJtRecoBin,
                fake=self._hJtFakeProjBin,
                start=4,
                stride=1,
            )
            drawing.draw8gridcomparison(
                self._hJtMeasBin,
                self._hJtTrueBin,
                self._jetPtBins,
                xlog=True,
                ylog=True,
                name="{}_Extra.pdf".format(filename),
                unf2d=self._hJtRecoBin,
                fake=self._hJtFakeProjBin,
                start=0,
                stride=1,
            )

    def plotLeadingJtComparison(self, filename, **kwargs):
        nRebin = kwargs.get("Rebin", 1)
        histlist = [self._hJtTestTrueBin, self._hJtTrueBin, self._hJtMeasBin]
        if nRebin > 1:
            for hists in histlist:
                for h in hists:
                    h.Rebin(nRebin)
                    h.Scale(1.0 / nRebin)

        drawing.draw8gridcomparison(
            self._hJtMeasBin,
            self._hJtTrueBin,
            self._jetPtBins,
            xlog=True,
            ylog=True,
            name="{}.pdf".format(filename),
            leadingJt=self._hJtTestTrueBin,
            start=2,
            stride=1,
        )
        drawing.draw8gridcomparison(
            self._hJtMeasBin,
            self._hJtTrueBin,
            self._jetPtBins,
            xlog=True,
            ylog=True,
            name="{}_Extra.pdf".format(filename),
            leadingJt=self._hJtTestTrueBin,
            start=0,
            stride=1,
        )

    def plotResponse(self):
        fig, axs = plt.subplots(2, 1, figsize=(10, 10))
        axs = axs.reshape(2)
        for ax, h in zip(axs, (self._responseJetPt, self._response2D)):
            hResponse = h.Hresponse()
            xbins = [
                hResponse.GetXaxis().GetBinLowEdge(iBin)
                for iBin in range(1,
                                  hResponse.GetNbinsX() + 2)
            ]
            ybins = [
                hResponse.GetYaxis().GetBinLowEdge(iBin)
                for iBin in range(1,
                                  hResponse.GetNbinsY() + 2)
            ]
            drawing.drawResponse(
                ax, make2DHist(hResponse, xbins=xbins, ybins=ybins))
        plt.show()  # Draw figure on screen

        fig, ax = plt.subplots(1, 1, figsize=(10, 10))
        hResponse = self._responseJetPt.Hresponse()
        xbins = [
            hResponse.GetXaxis().GetBinLowEdge(iBin)
            for iBin in range(1,
                              hResponse.GetNbinsX() + 2)
        ]
        ybins = [
            hResponse.GetYaxis().GetBinLowEdge(iBin)
            for iBin in range(1,
                              hResponse.GetNbinsY() + 2)
        ]
        drawing.drawPtResponse(ax,
                               make2DHist(hResponse, xbins=xbins, ybins=ybins))
        plt.savefig("JetPtResponse", format="pdf")  # Save figure
        plt.show()

    def printJetNumbers(self):
        print("Measured jets by bin")
        print(self._numberJetsMeasBin)
        print(self._numberJetsMeasFromHist)
        if not self._IsData:
            print("True jets by bin")
            print(self._numberJetsTrueBin)
            print(self._numberJetsTrueFromHist)
        print("Unfolded jet numbers by bin:")
        print(self._numberJetsFromReco)
        print("Jet bin centers:")
        print([
            self._hJetPtRecoCoarse.GetBinCenter(i)
            for i in range(1, self._hJetPtRecoCoarse.GetNbinsX())
        ])

    def write_files(self, filename, **kwargs):
        """
        Write output histograms to file

        Args:
        filename: Name of output file
        """
        print("{}: write results to file {} and folder".format(
            self._name, filename))
        base_folder = "{}/BayesSubUnfolding/".format(self._name)
        print(base_folder)

        open_as = "append" if kwargs.get("append", False) else "recreate"

        with root_open(filename, open_as) as output_file:
            TDir = output_file.mkdir("{}{}".format(base_folder,
                                                   "JetConeJtWeightBin"),
                                     title="JetConeJtWeightBin",
                                     recurse=True)
            TDir.cd()
            # output_file.cd(TDir)
            for i, (jt, pt) in enumerate(zip(self._hJtMeasBin,
                                             self._jetPtBins)):
                jt.name = "JetConeJtWeightBinNFin{0[NFin]:02d}JetPt{0[pT]:02d}".format(
                    {
                        "NFin": self._NFin,
                        "pT": i
                    })
                jt.title = "Finder:Full_Jets_R04_00 p_{{T,jet}} : {} - {}".format(
                    pt[0], pt[1])
                jt.Write()

            if self._hBgJtNormalized is not None:
                TDir = output_file.mkdir("{}{}".format(base_folder,
                                                       "BgJtWeightBin"),
                                         title="BgJtWeightBin",
                                         recurse=True)
                TDir.cd()
                for i, (jt, pt) in enumerate(
                        zip(self._hBgJtNormalized, self._jetPtBins)):
                    jt.name = "BgJtWeightBinNFin{0[NFin]:02d}JetPt{0[pT]:02d}".format(
                        {
                            "NFin": self._NFin,
                            "pT": i
                        })
                    jt.Write()
Esempio n. 17
0
from ROOT import RooUnfoldTUnfold
import sys

unfold = "unfold" in sys.argv

f = ROOT.TFile("~/Documents/stpol/trees/noSkim/T_t.root")
tree = f.Get("treesCands").Get("eventTree")

N = tree.GetEntries()
firstHalf = N / 2

hMeas_test = ROOT.TH1D("hMeas_test", "measured", 20, -5, 5)
hTrue_test = ROOT.TH1D("hTrue_test", "true", 20, -5, 5)

if unfold:
    response = RooUnfoldResponse(20, -5, 5)
    for i in range(N / 2):
        tree.GetEntry(i)
        xm = tree._recoTop_0_Eta
        xt = tree.trueTop_genParticleSelector_0_Eta
        if xm == xm and xt == xt:
            response.Fill(xm, xt)
        elif xm == xm and xt != xt:
            response.Fake(xm)
        elif xt == xt and xm != xm:
            response.Miss(xt)

for i in range(N / 2, N):
    tree.GetEntry(i)
    xm = tree._recoTop_0_Eta
    xt = tree.trueTop_genParticleSelector_0_Eta
Esempio n. 18
0
def smear(xt):
    xeff = 0.3 + (1.0 - 0.3) / 20 * (xt + 10.0)
    #  efficiency
    x = gRandom.Rndm()
    if x > xeff: return None
    xsmear = gRandom.Gaus(-2.5, 0.2)
    #  bias and smear
    return xt + xsmear


# ==============================================================================
#  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):
Esempio n. 19
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
Esempio n. 20
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, hTrueEE, hTrueMM, hMeasMM, hMeasEE

    #c2 = TCanvas("c2")
    print "==================================== TRAIN ===================================="
    # Create response matrix object
    inputdirZZ = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/UNFOLDacc/HADD/UnfoldInput/"  #ZZ
    # inputdirWZ = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/UNFOLDWZ/HADD/UnfoldInput/" #WZ
    # inputdirNoPrep = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/ZZoutput/" #data, NRB
    inputdirDY = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/DYbkgd/"  #gamma
    #inputGEN = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/Unfolding/PT.root"

    inputdir = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/EWKSET/HADD/UnfoldInput/"  #ZZ
    inputdirWZ = inputdir  #WZ
    inputdirNoPrep = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/EWKSET/"  #data, NRB
    #inputdirNoPrep2 = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/" #data, NRB
    inputdir2 = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/NOSET/HADD/UnfoldInput/"
    # inputdirWZ = inputdir

    fw = '/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/Unfolding/MCFM_withAcceptance/ZZlept_tota_MSTW200_90__90__test.root'
    fwo = '/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/Unfolding/MCFM_withoutAcceptance/ZZlept_tota_MSTW200_90__90__test.root'

    #inputdirOTH = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/NOSET/HADD/UnfoldInput/"
    #inputdirWZOTH = inputdirOTH

    #inputdirEWK = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/EWKSET/HADD/UnfoldInput/" #ZZ

    PAIR = ['zptG', 'zpt']
    outputnameGR = PAIR[0] + '_vs_' + PAIR[1] + '.png'
    outputnameG = PAIR[0] + '.png'
    outputnameR = PAIR[1] + '.png'
    cutData = "(zpt>45)*(preco>0)"  #"preco"

    cutGMM = "((L1GId*L2GId)==-13*13)"
    cutRMM = "((l1id*l2id)==-13*13)"
    cutGEE = "((L1GId*L2GId)==-11*11)"
    cutREE = "((l1id*l2id)==-11*11)"
    cutQQ = "(abs(l1id*l2id) == 11*13)"

    hsWW = makehistos("DataFused.root", inputdirNoPrep)
    hsDY = makehistosDY('gamma_out_8_MoreBins_ll.root', inputdirDY)
    print "MEASURED DATA " + "=" * 30
    hMeasMM = Make1DPlot(inputdirNoPrep + "DataFused.root", "finalTree",
                         PAIR[1], "Data.png", cutRMM + "*" + cutData)
    hMeasEE = Make1DPlot(inputdirNoPrep + "DataFused.root", "finalTree",
                         PAIR[1], "Data.png", cutREE + "*" + cutData)
    #hMeasQQ = Make1DPlot(inputdirNoPrep+"DataFused.root","finalTree",PAIR[1],"Data.png",cutQQ + "*" + cutData)
    print "MEASURED DATA " + "=" * 30

    print hMeasMM.Integral(), hMeasEE.Integral(), "Measured"

    print "NRB and DY"
    print hsWW[0].Integral(), hsWW[1].Integral(), "WW"
    print hsDY[0].Integral(), hsDY[1].Integral(), "DY"

    #MCFM rescaling and plotting
    #
    hMCFM = GetMCFMHisto(fwo, True)
    hMCFM.Scale(143.2 / hMCFM.Integral())
    hMCFM = REBIN(hMCFM)

    print hMCFM.Integral()
    hMCFM.Print("range")

    #sys.exit("donesies")

    MWO = GetMCFMHisto(fwo, True)
    MW = GetMCFMHisto(fw, False)
    MWO.Divide(MW)

    MWO.Print("range")

    MWO = REBIN(MWO)

    MWO.Print("range")

    # MWO = REBIN(GetMCFMHisto(fwo,True))
    # MW = REBIN(GetMCFMHisto(fw, False))

    # MWO.Divide(MW)

    # MWO.Print("range")

    #sys.exit("donesies")

    systematic = [
        '_jer', '_jes', '_umet', '_les', '_pu', '_btag', '_qcd', '_pdf'
    ]  #'_jer','_jes','_umet','_les','_pu','_btag','_sherpa','_qcd','_pdf']
    # systematic = ['_jer','_jes','_qcd','_pdf']
    UD = ['up', 'down']
    SYST_UD = ['']
    for syst in systematic:
        for ud in UD:
            SYST_UD.append(syst + ud)

    MCGenHistos = []
    MCRecoHistos = []
    hTrues = []
    hDiffs = []
    hCloses = []
    hunfolds = []
    ########
    MCGenHistosMM = []
    MCRecoHistosMM = []
    hTruesMM = []
    hDiffsMM = []
    hClosesMM = []
    hunfoldsMM = []
    ########
    MCGenHistosEE = []
    MCRecoHistosEE = []
    hTruesEE = []
    hDiffsEE = []
    hClosesEE = []
    hunfoldsEE = []
    ######
    MCGenHistosMM2 = []
    MCGenHistosEE2 = []
    MCGenHistos2 = []

    for syst_ud in SYST_UD:
        print "=O" * 40
        print syst_ud
        print "O=" * 40
        SYS = "(sys" + syst_ud + ">0)"
        cutR = "Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(preco>0)*" + SYS
        # cutG = "Eweight*BR*(B2/B3)*(pgen>0)*"+SYS #Accstring
        # [5718.8469039797783, 1737.4891278286839, 1737.4891278286839] fb
        #SSS = 2.1299863918*(3.0)
        #SSS = 2.1299863918*(3.0/2.0)
        #SSS = (3.0/2.0)
        #SSS = (3.0)
        #cutG = str(SSS)+"*Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(pgen>0)*"+SYS #Accstring
        cutG = "Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(pgen>0)*" + SYS  #Accstring
        # cutG = str(SSS)+"*Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(1/Acc2)*(pgen>0)*"+SYS #Accstring
        #cutG = str(SSS)+"*Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(pgen>0)*"+SYS #Accstring
        cutGR = "Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(pgen>0)*(preco>0)*" + SYS  #+Accstring
        cutWZ = "Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(preco>0)*" + SYS

        cutGEN = "XS*LUM*(1/NGE)*(B2/B3)*(g_pt>45.0)"
        cutee = "(ee>0.0)"
        cutmm = "(mumu>0.0)"

        # MCRecoHistoMM = REBIN(Make1DPlot(inputdir+"ZZ.root","tmvatree",PAIR[1],outputnameR,cutR+"*"+cutRMM))
        # MCRecoHistoEE = REBIN(Make1DPlot(inputdir+"ZZ.root","tmvatree",PAIR[1],outputnameR,cutR+"*"+cutREE))
        # MCGenHistoMM = REBIN(Make1DPlot(inputdir+"ZZ.root","tmvatree",PAIR[0],outputnameG,cutG+"*"+cutGMM))
        # MCGenHistoEE = REBIN(Make1DPlot(inputdir+"ZZ.root","tmvatree",PAIR[0],outputnameG,cutG+"*"+cutGEE))
        MCRecoHistoMM = Make1DPlot(inputdir + "ZZ.root", "tmvatree", PAIR[1],
                                   outputnameR, cutR + "*" + cutRMM)
        MCRecoHistoEE = Make1DPlot(inputdir + "ZZ.root", "tmvatree", PAIR[1],
                                   outputnameR, cutR + "*" + cutREE)
        MCGenHistoMM = Make1DPlotGEN(inputdir + "ZZ.root", "tmvatree", PAIR[0],
                                     outputnameG, cutG + "*" + cutGMM)
        MCGenHistoEE = Make1DPlotGEN(inputdir + "ZZ.root", "tmvatree", PAIR[0],
                                     outputnameG, cutG + "*" + cutGEE)
        # MCGenHistoMM = Make1DPlotGEN(inputGEN,"GPT","g_pt",outputnameG,cutGEN+"*"+cutmm)
        # MCGenHistoEE = Make1DPlotGEN(inputGEN,"GPT","g_pt",outputnameG,cutGEN+"*"+cutee)
        # MCGenHistoMM.Print("range")
        # sys.exit("done")
        GenVsReco2DHistoMM = Make2DPlot(inputdir + "ZZ.root", "tmvatree", PAIR,
                                        'MM' + outputnameGR,
                                        cutGR + "*" + cutGMM + "*" + cutRMM,
                                        syst_ud)
        GenVsReco2DHistoEE = Make2DPlot(inputdir + "ZZ.root", "tmvatree", PAIR,
                                        'EE' + outputnameGR,
                                        cutGR + "*" + cutGEE + "*" + cutREE,
                                        syst_ud)
        # sys.exit("done")
        #
        MCGenHistoMM2 = Make1DPlotGEN(inputdir2 + "ZZ.root", "tmvatree",
                                      PAIR[0], outputnameG,
                                      cutG + "*" + cutGMM)
        MCGenHistoEE2 = Make1DPlotGEN(inputdir2 + "ZZ.root", "tmvatree",
                                      PAIR[0], outputnameG,
                                      cutG + "*" + cutGEE)

        #MCGenHistoMM2 = Make1DPlotMCFM(1)
        #MCGenHistoEE2 = Make1DPlotMCFM(1)

        hWZMM = Make1DPlot(inputdirWZ + "WZ.root", "tmvatree", PAIR[1],
                           "WZ.png", cutWZ + "*" + cutRMM)  #MC
        hWZEE = Make1DPlot(inputdirWZ + "WZ.root", "tmvatree", PAIR[1],
                           "WZ.png", cutWZ + "*" + cutREE)  #MC

        print hWZMM.Integral(), hWZEE.Integral(), "WZ MM EE"
        print MCRecoHistoMM.Integral(), MCRecoHistoEE.Integral(), "ZZ MM EE"

        BKGDSMM = [REBIN(hsWW[0]), REBIN(hsDY[0]), hWZMM]
        BKGDSEE = [REBIN(hsWW[1]), REBIN(hsDY[1]), hWZEE]

        for bk in (BKGDSMM + BKGDSEE):
            print bk.Integral(), "subtract"

        print hMeasMM.Integral()
        print hMeasEE.Integral()

        hDiffMM = SubtractMany1DPlots(hMeasMM, BKGDSMM)
        hDiffEE = SubtractMany1DPlots(hMeasEE, BKGDSEE)

        # hDiffMM = hMeasMM
        # hDiffEE = hMeasEE

        print hDiffMM.Integral(), quadadd(BinError(hDiffMM))
        print hDiffEE.Integral(), quadadd(BinError(hDiffEE))
        print "Difference between Data and Background^"

        print hMeasMM.Integral(), quadadd(BinError(hMeasMM))
        print hMeasEE.Integral(), quadadd(BinError(hMeasEE))
        print "DATA yield^"

        print REBIN(hsWW[0]).Integral(), quadadd(BinError(REBIN(hsWW[0])))
        print REBIN(hsWW[1]).Integral(), quadadd(BinError(REBIN(hsWW[1])))
        print "NRB yield^"

        print REBIN(hsDY[0]).Integral(), quadadd(BinError(REBIN(hsDY[0])))
        print REBIN(hsDY[1]).Integral(), quadadd(BinError(REBIN(hsDY[1])))
        print "DY yield^"

        print hWZMM.Integral(), quadadd(BinError(hWZMM))
        print hWZEE.Integral(), quadadd(BinError(hWZEE))
        print "WZ yield^"

        print MCRecoHistoMM.Integral(), quadadd(BinError(MCRecoHistoMM))
        print MCRecoHistoEE.Integral(), quadadd(BinError(MCRecoHistoEE))
        print "ZZ yield^"

        #sys.exit("donesies")

        responseMM = RooUnfoldResponse(MCRecoHistoMM, MCGenHistoMM,
                                       GenVsReco2DHistoMM)
        responseEE = RooUnfoldResponse(MCRecoHistoEE, MCGenHistoEE,
                                       GenVsReco2DHistoEE)
        # MCRecoHistoMM.Print("range")
        # MCGenHistoMM.Print("range")
        # GenVsReco2DHistoMM.Print("range")
        #sys.exit("DONE")
        #sys.exit("done")

        # print "==================================== TEST ====================================="
        # #hTrue= TH1D( "true", "Test Truth", 20, -40.0, 40.0 )
        # #hMeas= TH1D( "meas", "Test Measured", 40, -10.0, 10.0 )
        # #hTrue= TH1D( "true", "Test Truth", 10, 40.0, 760.0 )
        # # hMeas= TH1D( "meas", "Test Measured", 10, 0.0, 800.0 )

        print "==================================== UNFOLD ==================================="
        print "Unfolding method:", optunf
        if "Bayes" in optunf:
            # Bayes unfoldung with 4 iterations
            #unfoldMM= RooUnfoldBayes( responseMM, hDiffMM, 4)
            closureMM = RooUnfoldBayes(responseMM, MCRecoHistoMM, 4)

            #unfoldEE= RooUnfoldBayes( responseEE, hDiffEE, 4)
            closureEE = RooUnfoldBayes(responseEE, MCRecoHistoEE, 4)

            unfoldsysMM = RooUnfoldBayes(responseMM, hDiffMM, 4)
            unfoldsysEE = RooUnfoldBayes(responseEE, hDiffEE, 4)

            # if syst_ud == "":
            #   unfoldsysMM.SetNToys(100)
            #   unfoldsysMM.IncludeSystematics()
            #   unfoldsysEE.SetNToys(100)
            #   unfoldsysEE.IncludeSystematics()

            print "Bayes " * 20
            #closure= RooUnfoldBayes( response, MCRecoHisto , 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)

        #hTrueMM= unfoldMM.Hreco()
        hCloseMM = closureMM.Hreco()
        #hTrueEE= unfoldEE.Hreco()
        hCloseEE = closureEE.Hreco()

        if syst_ud == "":
            #unfoldsysMM.SetNToys(500)
            # unfoldsysMM.IncludeSystematics(0) # 0 DATA ONLY
            unfoldsysMM.IncludeSystematics(1)  # 1 EVERYTHING
            # unfoldsysMM.IncludeSystematics(2) # 2 MC RESPONSE
            #unfoldsysEE.SetNToys(500)
            unfoldsysEE.IncludeSystematics(1)
            # hunfoldMM = unfoldsysMM.Hreco(2) # 2 DIRECT ERROR PROAGATION, ONLY WITH IncludeSystematics(0) -- data only. Can't directly propagate MC response unc.
            hunfoldMM = unfoldsysMM.Hreco(2)
            hunfoldEE = unfoldsysEE.Hreco(
                2
            )  # 3 Toy MC error evaluation. Would apply to both data-MC uncertainty, and MC response uncertainty.
            print "STUFF" * 40
            hunfoldMM.Print("range")

            print "UNFOLDED NUMBERS :--:" * 10
            print hunfoldMM.Integral(), quadadd(BinError(hunfoldMM))
            print hunfoldEE.Integral(), quadadd(BinError(hunfoldEE))
            print "UNFOLDED NUMBERS :--:" * 10

            #############################
            #IMPORTANT!!! Includesys&Hreco: 0&2, 1&2 (some errors), N&2 (bayesian)*, 1&3, 2&3
            #############################
        else:
            hunfoldMM = unfoldsysMM.Hreco()
            hunfoldEE = unfoldsysEE.Hreco()

        print "      ---- TEST @@@@@@@@@@@@@@@@@@@@@@@@@ "
        hunfoldEE.Print("range")
        hunfoldMM.Print("range")

        # print hTrueMM.Integral(), hTrueEE.Integral(), "True"
        print MCGenHistoMM.Integral(), MCGenHistoEE.Integral(), "Gen"
        print hunfoldMM.Integral(), hunfoldEE.Integral(), "unfold"
        #makeComparison(MCGenHistoMM,MCRecoHistoMM,hTrueMM,hDiffMM,hCloseMM,"MM"+syst_ud)
        #makeComparison(MCGenHistoEE,MCRecoHistoEE,hTrueEE,hDiffEE,hCloseEE,"EE"+syst_ud)

        #RESCALE TO MCFM with kinematic cut ratios
        hunfoldEE.Multiply(MWO)
        hunfoldMM.Multiply(MWO)
        MCGenHistoEE.Multiply(MWO)
        MCGenHistoMM.Multiply(MWO)
        MCGenHistoEE2.Multiply(MWO)
        MCGenHistoMM2.Multiply(MWO)
        SSS = 3.0  #3.0 for single channel, 3.0/2.0 for both
        hunfoldMM.Scale(SSS)
        hunfoldEE.Scale(SSS)
        MCGenHistoMM.Scale(SSS)
        MCGenHistoEE.Scale(SSS)
        MCGenHistoMM2.Scale(SSS)
        MCGenHistoEE2.Scale(SSS)

        print MCGenHistoMM.Integral(), MCGenHistoEE.Integral(), "Gen"
        print hunfoldMM.Integral(), hunfoldEE.Integral(), "unfold"

        #sys.exit("donesies")

        #For mumu channel
        hDiffsEE.append([hDiffEE, syst_ud])
        hunfoldsEE.append([hunfoldEE, syst_ud])
        if syst_ud == "":
            hClosesEE.append([hCloseEE, syst_ud])
            MCGenHistosEE.append([MCGenHistoEE, syst_ud])
            MCRecoHistosEE.append([MCRecoHistoEE, syst_ud])
            #MCGenHistosEE2.append([hMCFM,syst_ud])

        #For mumu channel
        hDiffsMM.append([hDiffMM, syst_ud])
        hunfoldsMM.append([hunfoldMM, syst_ud])
        if syst_ud == "":
            hClosesMM.append([hCloseMM, syst_ud])
            MCGenHistosMM.append([MCGenHistoMM, syst_ud])
            MCRecoHistosMM.append([MCRecoHistoMM, syst_ud])
            MCGenHistosMM2.append([hMCFM, syst_ud])

        #For combined channel
        hDiffs.append([ADDER(hDiffMM, hDiffEE, 1), syst_ud])
        hunfolds.append([ADDER(hunfoldMM, hunfoldEE, 1), syst_ud])
        if syst_ud == "":
            hCloses.append([ADDER(hCloseMM, hCloseEE, 1), syst_ud])
            MCGenHistos.append([ADDER(MCGenHistoMM, MCGenHistoEE, 1), syst_ud])
            MCRecoHistos.append(
                [ADDER(MCRecoHistoMM, MCRecoHistoEE, 1), syst_ud])
            MCGenHistos2.append(
                [ADDER(MCGenHistoMM2, MCGenHistoEE2, 1), syst_ud])

        print "=|" * 20
        print syst_ud * 20
        print "unfold yield:"
        print(hunfoldMM.Integral() + hunfoldEE.Integral()) / 19.7
        print(MCGenHistoMM.Integral() + MCGenHistoEE.Integral()) / 19.7, "Gen"

        hunfoldMM.Print("range")
        hunfoldEE.Print("range")
        print ">GEN<" * 40
        MCGenHistoMM.Print("range")
        MCGenHistoEE.Print("range")

        print "=|" * 20

        # print "=|"*20
        # print "unfold yield:"
        # print (hunfoldMM.Integral()+hunfoldEE.Integral())/19.7
        # print "=|"*20

        # hDiffs.append([hDiffEE,syst_ud])
        # hunfolds.append([hunfoldEE,syst_ud])
        # if syst_ud == "":
        #   hCloses.append([hCloseEE,syst_ud])
        #   MCGenHistos.append([MCGenHistoEE,syst_ud])
        #   MCRecoHistos.append([MCRecoHistoEE,syst_ud])

    #QQQ = makeComparison(LUMscale(MCGenHistos),LUMscale(MCRecoHistos),LUMscale(hunfolds),LUMscale(hDiffs),LUMscale(MCGenHistos2),"comb")
    #QQQ= makeComparison(LUMscale(MCGenHistosEE),LUMscale(MCRecoHistosEE),LUMscale(hunfoldsEE),LUMscale(hDiffsEE),MCGenHistosMM2,"ee")
    #QQQ = makeComparison(LUMscale(MCGenHistosMM),LUMscale(MCRecoHistosMM),LUMscale(hunfoldsMM),LUMscale(hDiffsMM),MCGenHistosMM2,"mm")

    #QQQ = makeComparison(LUMscale(MCGenHistos),LUMscale(MCRecoHistos),LUMscale(hunfolds),LUMscale(hDiffs),MCGenHistosMM2,"comb")
    QQQ = makeComparison(LUMscale(MCGenHistosEE), LUMscale(MCRecoHistosEE),
                         LUMscale(hunfoldsEE), LUMscale(hDiffsEE),
                         MCGenHistosMM2, "ee")
    #QQQ = makeComparison(LUMscale(MCGenHistosMM),LUMscale(MCRecoHistosMM),LUMscale(hunfoldsMM),LUMscale(hDiffsMM),MCGenHistosMM2,"mm")
    Neemm = QQQ[0]
    #Nee = makeComparison(LUMscale(MCGenHistosEE),LUMscale(MCRecoHistosEE),LUMscale(hunfoldsEE),LUMscale(hDiffsEE),LUMscale(hClosesEE),"ee")
    #Nmm = makeComparison(LUMscale(MCGenHistosMM),LUMscale(MCRecoHistosMM),LUMscale(hunfoldsMM),LUMscale(hDiffsMM),LUMscale(hClosesMM),"mm")
    # #makeComparison(MCGenHistos,MCRecoHistos,hunfolds,hDiffs,hCloses)
    # print len(MCRecoHistos)
    # print len(hunfolds)

    # print "Bin Content Check"
    # print "#"*30
    # print "#"*10, "DIMUON CHANNEL", "#"*10
    # print "MCReco", MCRecoHistosMM[0][0].Integral(), BinContent(MCRecoHistosMM[0][0])
    # print "MCGen", MCGenHistosMM[0][0].Integral(), BinContent(MCGenHistosMM[0][0])
    # print "True", hunfoldsMM[0][0].Integral(), BinContent(hunfoldsMM[0][0])
    # print "Diff", hDiffsMM[0][0].Integral(), BinContent(hDiffsMM[0][0])

    # print "#"*30
    # print "#"*10, "DIELECTRON CHANNEL", "#"*10
    # print "MCReco", MCRecoHistosEE[0][0].Integral(), BinContent(MCRecoHistosEE[0][0])
    # print "MCGen", MCGenHistosEE[0][0].Integral(), BinContent(MCGenHistosEE[0][0])
    # print "True", hunfoldsEE[0][0].Integral(), BinContent(hunfoldsEE[0][0])
    # print "Diff", hDiffsEE[0][0].Integral(), BinContent(hDiffsEE[0][0])

    # print "#"*30
    # print "#"*10, "COMBINED CHANNEL", "#"*10
    # print "MCReco", MCRecoHistos[0][0].Integral(), BinContent(MCRecoHistos[0][0])
    # print "MCGen", MCGenHistos[0][0].Integral(), BinContent(MCGenHistos[0][0])
    # print "True", hunfolds[0][0].Integral(), BinContent(hunfolds[0][0])
    # print "Diff", hDiffs[0][0].Integral(), BinContent(hDiffs[0][0])
    # print "#"*30
    # print "#"*30
    # print "#"*30

    # print hunfoldsMM[0][0].Integral()*(3.0)*2.13, "ZZ->2l2nu cross section, muon"
    # print hunfoldsEE[0][0].Integral()*(3.0)*2.13, "ZZ->2l2nu cross section, electron"
    # print hunfolds[0][0].Integral()*(3.0/2.0)*2.13, "ZZ->2l2nu cross section, combined"

    # print "#"*30

    # print Nmm
    # print Nee
    print Neemm

    print QQQ[1]
    #print Nmm
    #print Nee

    # print "@"*30
    # print "@"*30
    # print "@"*30

    # S=2.1299863918
    # dS=0.0451194907919

    # print numpy.multiply(Nmm,S*3)
    # print numpy.multiply(Nee,S*3)
    # print numpy.multiply(Neemm,S*3.0/2.0)

    # print "#"*30
    # print "#"*30
    # print "#"*30
    # print numpy.multiply(RescaleToPreZpt45(Nmm,S,dS),3.0), "muons"
    # print numpy.multiply(RescaleToPreZpt45(Nee,S,dS),3.0), "electrons"
    # print numpy.multiply(RescaleToPreZpt45(Neemm,S,dS),3.0/2.0), "combined"

    # Just unfolding
    # [ 317.58667967  124.62000891  124.62000891] muons
    # [ 204.44743361   93.817413     93.817413  ] electrons
    # [ 261.01705568   78.68129176   78.68129176] combined

    #all else
    # Bin Content Check
    # ##############################
    # ########## DIMUON CHANNEL ##########
    # MCReco 5.48821856594 [1.6566265821456909, 1.5307177305221558, 2.0848486423492432, 0.21176119148731232, 0.0042644194327294827]
    # MCGen 50.3628177345 [28.911533355712891, 8.1944065093994141, 11.555961608886719, 1.5890809297561646, 0.11183533072471619]
    # True 49.7008933779 [27.718360900878906, 7.7494301795959473, 13.04161262512207, 1.2007522583007812, -0.0092625860124826431]
    # Diff 5.54152165353 [1.6073417663574219, 1.4832497835159302, 2.371938943862915, 0.16530285775661469, -0.086311697959899902]
    # ##############################
    # ########## DIELECTRON CHANNEL ##########
    # MCReco 3.674643751 [1.0442177057266235, 0.9972614049911499, 1.4791457653045654, 0.15098364651203156, 0.0030352284666150808]
    # MCGen 33.3869778588 [18.415399551391602, 5.4404187202453613, 8.2116708755493164, 1.2221240997314453, 0.097364611923694611]
    # True 31.9951079488 [22.219881057739258, 3.5517585277557373, 5.7815923690795898, 0.44187599420547485, 0.0]
    # Diff 3.02480854467 [1.2913563251495361, 0.70207256078720093, 1.0841209888458252, 0.059399198740720749, -0.11214052885770798]
    # ##############################
    # ########## COMBINED CHANNEL ##########
    # MCReco 9.16539874254 [2.7004456520080566, 2.5281918048858643, 3.5664489269256592, 0.36300751566886902, 0.0073048430494964123]
    # MCGen 83.7497940361 [47.326930999755859, 13.634824752807617, 19.767633438110352, 2.8112049102783203, 0.2091999351978302]
    # True 81.6960010286 [49.938240051269531, 11.301189422607422, 18.823205947875977, 1.6426281929016113, -0.0092625860124826431]
    # Diff 8.56633037329 [2.8986983299255371, 2.1853222846984863, 3.4560599327087402, 0.22470206022262573, -0.19845223426818848]
    # ##############################
    # ##############################
    # ##############################
    # 317.588708685 ZZ->2l2nu cross section, muon
    # 204.448739793 ZZ->2l2nu cross section, electron
    # 261.018723287 ZZ->2l2nu cross section, combined
    # ##############################
    # [49.700893377885222, 24.356035601246898, 24.954409212519852]
    # [31.99510794878006, 17.444453802993618, 19.251802135458732]
    # [81.696001028642058, 34.234254955780528, 37.056285190785381]
    # ##############################
    # ##############################
    # ##############################
    # [ 317.58667967  155.77940538  159.59950658] muons       93.47233023637472982875, 99.70885557387058841952 quaddifference to just unfolding
    # [ 204.44743361  111.55344624  123.09443832] electrons   60.35283246052335146028, 79.69023631100434516015
    # [ 261.01705568  109.51740717  118.52311228] combined    76.17950380658385143785, 88.63962134122213690629

    # os.system(str(Npm)+' > output.txt')
    # SystematicErrors(MCRecoHistos,MCGenHistos,hTrues,hDiffs,hCloses)
    #
    #testtest

    return
Esempio n. 21
0
        binError = h_data_toUse.GetBinError(bin)
        newContent = gauss(binContent, sqrt(binContent))
        if newContent < 0: newContent = 0
        h_data_toUse.SetBinContent(bin, newContent)
        h_data_toUse.SetBinError(bin, sqrt(newContent))

    h_measured_toUse = asrootpy(h_measured)

    # Remove fakes before unfolding
    h_fakes = h_measured_toUse - h_response.ProjectionX()
    nonFakeRatio = 1 - h_fakes / h_measured_toUse
    h_measured_toUse *= nonFakeRatio / nonFakeRatio
    h_data_toUse *= nonFakeRatio / nonFakeRatio

    # Unfold with SVD
    response = RooUnfoldResponse(h_measured_toUse, h_truth, h_response)
    svdUnfolding = RooUnfoldSvd(response, h_data_toUse, 0.7)
    svdUnfolding.SetVerbose(0)
    h_unfolded_data_svd = svdUnfolding.Hreco(3)

    # Unfold with Bayes
    response_bayes = RooUnfoldResponse(h_measured_toUse, h_truth, h_response)
    bayesUnfolding = RooUnfoldBayes(response_bayes, h_data_toUse, 4)
    h_unfolded_data_bayes = bayesUnfolding.Hreco(3)

    # Store result of first bin
    h_svdFirstBin.Fill(h_unfolded_data_svd.GetBinContent(1))
    h_bayesFirstBin.Fill(h_unfolded_data_bayes.GetBinContent(1))

# Draw histograms
h_svdFirstBin.Draw()
Esempio n. 22
0
class unfoldHandle(object):
    def __init__(self, genHist, recHist, hist2d, name):
        self.genHist = genHist.Clone()
        self.recHist = recHist.Clone()
        self.hist2d = hist2d.Clone()
        self.response = RooUnfoldResponse(recHist, genHist, hist2d,
                                          "response matrix", "response matrix")
        self.response.UseOverflow()
        self.name = name
        hx = self.hist2d.ProjectionX()
        hy = self.hist2d.ProjectionY()
        vaild = 0.0001
        test1 = 0
        test2 = 0
        for i in range(hx.GetNbinsX() + 2):
            if recHist.GetBinContent(i) != 0:
                val = abs(hx.GetBinContent(i) / recHist.GetBinContent(i) - 1)
            else:
                val = 0
            if val > test1:
                test1 = val
        for i in range(hx.GetNbinsX() + 2):
            if genHist.GetBinContent(i) != 0:
                val = abs(hy.GetBinContent(i) / genHist.GetBinContent(i) - 1)
            else:
                val = 0
            if val > test2:
                test2 = val
        if test1 < vaild and test2 < vaild:
            print "response matrix is correct"
        else:
            print "response matrix is wrong"
            print test1
            print test2

    def getConditionN(self):
        m = self.response.Mresponse()
        SVD = ROOT.TDecompSVD(m)
        return SVD.Condition()

    def doUnfold(self, data, back=0, regdiff=0, iteration=0):
        self.data = data.Clone()
        if back != 0:
            self.data.Sumw2()
            back.Sumw2()
            self.data.Add(back, -1.)
            self.back = back.Clone()
            for i in range(self.data.GetNbinsX() + 2):
                if self.data.GetBinContent(i) < 0:
                    self.data.SetBinContent(i, 0)
        if regdiff != 0:
            Unfold = RooUnfoldBayes(self.response, self.data, 1, False,
                                    self.name + "_reg", self.name + "_reg")
            h2 = self.data.Clone()
            for i in range(9999):
                h1 = Unfold.Hreco().Clone()
                vaild = 0
                val = 0
                for j in range(1, h2.GetNbinsX() + 1):
                    if h2.GetBinContent(j) > 0:
                        val = h1.GetBinContent(j) / h2.GetBinContent(j)
                        val = abs(val - 1)
                        if vaild < val:
                            vaild = val
                print vaild
                if vaild < regdiff:
                    print "%d times iteration" % i
                    self.UnfoldedData = h1
                    return h1
                else:
                    h2 = h1.Clone()
                    Unfold.Reset()
                    Unfold = RooUnfoldBayes(self.response, self.data, i + 2,
                                            False, self.name + "_reg",
                                            self.name + "_reg")
        elif iteration != 0:
            s = "iter_%d" % iteration
            Unfold = RooUnfoldBayes(self.response, self.data, iteration, False,
                                    self.name + s, self.name + s)
            h1 = Unfold.Hreco().Clone()
            return h1
        else:
            Unfold = RooUnfoldInvert(self.response, self.data,
                                     self.name + "_invert",
                                     self.name + "_invert")
            Unfold.SetMeasured(self.data)
            self.Unfold = Unfold
            h1 = Unfold.Hreco().Clone()
            self.UnfoldedData = h1
            return h1
Esempio n. 23
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()