コード例 #1
0
 def _draw(self):
     passed = self.sim_dir.Get(self.passed_name)
     total = self.sim_dir.Get(self.total_name)
     if not TEfficiency.CheckConsistency(passed, total):
         return None
     eff = TEfficiency(passed, total)
     self.hist = eff.CreateHistogram()
コード例 #2
0
def buildAccXRecEffiHist(self):
    """Build two step efficiency histograms for later fitting/plotting"""
    binKey = self.process.cfg['binKey']
    fin = self.process.filemanager.open("buildAccXRecEffiHist", modulePath + "/data/accXrecEffHists_{0}_{1}.root".format(str(self.process.cfg['args'].Year), q2bins[binKey]['label']), "UPDATE")
    # Build acceptance, reco efficiency, and accXrec
    forceRebuild = False

    h2_accXrec = fin.Get("h2_accXrec_{0}".format(binKey))
    if h2_accXrec == None or forceRebuild:
        h2_acc = fin.Get("h2_acc_{0}".format(binKey))
        h2_rec = fin.Get("h2_rec_{0}".format(binKey))

        # Fill histograms
        setupEfficiencyBuildProcedure['acc'].update({
            'ifiles'    : self.process.cfg['genonly']['PhiMuMu'],
            'baseString': re.sub("Q2", "genQ2", q2bins[binKey]['cutString']) + " && genKpPt>0 && genKmPt>0",
            'cutString' : "({0}) && ({1})".format(re.sub("Q2", "genQ2", q2bins[binKey]['cutString']), genSel)})
        setupEfficiencyBuildProcedure['rec'].update({
            'ifiles'    : self.process.cfg['sigMC'],
            'dfiles'    : self.process.cfg['genOff']['PhiMuMu'],
            'baseString': "({0}) && ({1})".format(re.sub("Q2", "genQ2", q2bins[binKey]['cutString']), genSel),
            'cutString' : "({0}) && ({1}) && ({2})".format(cuts_antiResVeto if binKey in ['jpsi', 'psi2s'] else self.process.cfg['cuts'][-1], q2bins[binKey]['cutString'], 1 if ExtraCuts==None else ExtraCuts)})

        LBins=accXEffThetaLBins #if not binKey=="belowJpsiA" else ThetaLBins
        KBins=accXEffThetaKBins #if not binKey=="belowJpsiA" else ThetaKBins
        for h2, label in (h2_acc, 'acc'), (h2_rec, 'rec'):
            if h2 == None or forceRebuild:
                treein = TChain("tree")
                for f in setupEfficiencyBuildProcedure[label]['ifiles']:
                    treein.Add(f)

                treeDen = TChain()
                if label=="rec":
                    for f in setupEfficiencyBuildProcedure[label]['dfiles']:
                        treeDen.Add(f)
                UseDataFrame=True
                if UseDataFrame:
                    if setupEfficiencyBuildProcedure[label]['weight'] is None and label=="acc":
                        df_tot = ROOT.RDataFrame(treein).Define('weight', "1").Filter(setupEfficiencyBuildProcedure[label]['baseString'])
                    elif label=="acc":
                        df_tot = ROOT.RDataFrame(treein).Define('weight', *setupEfficiencyBuildProcedure[label]['weight']).Filter(setupEfficiencyBuildProcedure[label]['baseString'])
                    elif setupEfficiencyBuildProcedure[label]['weight'] is None and label=="rec":
                        df_tot = ROOT.RDataFrame(treeDen).Define('weight', "1").Filter(setupEfficiencyBuildProcedure[label]['baseString'])
                    elif label=="rec":
                        df_tot = ROOT.RDataFrame(treeDen).Define('weight', *setupEfficiencyBuildProcedure[label]['weight']).Filter(setupEfficiencyBuildProcedure[label]['baseString'])
                    df_acc = ROOT.RDataFrame(treein).Define('weight', "1").Filter(setupEfficiencyBuildProcedure[label]['cutString'])

                    fillXY = setupEfficiencyBuildProcedure[label]['fillXY'].split(':')
                    if label=="rec": fillXYDen = setupEfficiencyBuildProcedure[label]['fillXYDen'].split(':')
                    h2_total_config = ("h2_{0}_{1}_total".format(label, binKey), "", len(LBins) - 1, LBins, len(KBins) - 1, KBins)
                    h2_passed_config  = ("h2_{0}_{1}_passed".format(label, binKey), "", len(LBins) - 1, LBins, len(KBins) - 1, KBins)
                    h2_fine_total_config = ("h2_{0}_fine_{1}_total".format(label, binKey), "", 20, -1, 1, 20, -1, 1)
                    h2_fine_passed_config = ("h2_{0}_fine_{1}_passed".format(label, binKey), "", 20, -1, 1, 20, -1, 1)

                    h2ptr_total = df_tot.Histo2D(h2_total_config, fillXY[1], fillXY[0], "weight") if label=="acc" else df_tot.Histo2D(h2_total_config, fillXYDen[1], fillXYDen[0], "weight")
                    h2ptr_passed = df_acc.Histo2D(h2_passed_config, fillXY[1], fillXY[0], "weight")
                    h2ptr_fine_total = df_tot.Histo2D(h2_fine_total_config, fillXY[1], fillXY[0], "weight") if label=="acc" else df_tot.Histo2D(h2_fine_total_config, fillXYDen[1], fillXYDen[0], "weight")
                    h2ptr_fine_passed = df_acc.Histo2D(h2_fine_passed_config, fillXY[1], fillXY[0], "weight")

                    h2_total = h2ptr_total.GetValue()
                    h2_passed = h2ptr_passed.GetValue()
                    h2_fine_total = h2ptr_fine_total.GetValue()
                    h2_fine_passed = h2ptr_fine_passed.GetValue()
                    print("{2}: {0}/{1}".format(df_acc.Count().GetValue(), df_tot.Count().GetValue(), label))
                else:
                    if label=='acc':
                        treein.Draw(">>totEvtList", setupEfficiencyBuildProcedure['acc']['baseString'])
                        totEvtList = ROOT.gDirectory.Get("totEvtList")
                        treein.Draw(">>accEvtList", setupEfficiencyBuildProcedure['acc']['cutString'])
                        accEvtList = ROOT.gDirectory.Get("accEvtList")
                    if label=='rec':
                        treeDen.Draw(">>totEvtList", setupEfficiencyBuildProcedure['rec']['baseString'])
                        totEvtList = ROOT.gDirectory.Get("totEvtList")
                        treein.Draw(">>accEvtList", setupEfficiencyBuildProcedure['rec']['cutString'])
                        accEvtList = ROOT.gDirectory.Get("accEvtList")
                    h2_total = TH2D("h2_{0}_{1}_total".format(label,binKey), "", len(LBins)-1, LBins, len(KBins)-1, KBins)
                    h2_passed = h2_total.Clone("h2_{0}_{1}_passed".format(label, binKey))
                    h2_fine_total = TH2D("h2_{0}_fine_{1}_total".format(label, binKey), "", 20, -1, 1, 20, -1, 1)
                    h2_fine_passed = h2_fine_total.Clone("h2_{0}_fine_{1}_passed".format(label, binKey))
                    
                    treeDen.SetEventList(totEvtList) if label=='rec' else treein.SetEventList(totEvtList)
                    for hist in h2_total, h2_fine_total:
                        if label=='rec':
                            treeDen.Draw("{0}>>{1}".format(setupEfficiencyBuildProcedure['rec']['fillXYDen'], hist.GetName()), "", "goff")
                        else:
                            treein.Draw("{0}>>{1}".format(setupEfficiencyBuildProcedure['acc']['fillXY'], hist.GetName()), "", "goff")
                    treein.SetEventList(accEvtList)
                    for hist in h2_passed, h2_fine_passed:
                        treein.Draw("{0}>>{1}".format(setupEfficiencyBuildProcedure[label]['fillXY'], hist.GetName()), "", "goff")
                    print("{2}: {0}/{1}".format(accEvtList.GetN(), totEvtList.GetN(), label))
                print("Base String: ", setupEfficiencyBuildProcedure[label]['baseString'])
                print("Cut String:  ", setupEfficiencyBuildProcedure[label]['cutString'])
                h2_eff = TEfficiency(h2_passed, h2_total)
                h2_eff_fine = TEfficiency(h2_fine_passed, h2_fine_total)

                fin.cd()
                for proj, var in [("ProjectionX", CosThetaL), ("ProjectionY", CosThetaK)]:
                    proj_fine_total = getattr(h2_fine_total, proj)("{0}_{1}".format(h2_fine_total.GetName(), proj), 0, -1, "e")
                    proj_fine_passed = getattr(h2_fine_passed, proj)("{0}_{1}".format(h2_fine_passed.GetName(), proj), 0, -1, "e")
                    h_eff = TEfficiency(proj_fine_passed, proj_fine_total)
                    h_eff.Write("h_{0}_fine_{1}_{2}".format(label, binKey, proj), ROOT.TObject.kOverwrite)

                h2_eff.Write("h2_{0}_{1}".format(label, binKey), ROOT.TObject.kOverwrite)
                h2_eff_fine.Write("h2_{0}_fine_{1}".format(label, binKey), ROOT.TObject.kOverwrite)
                h2_eff.CreateHistogram().Write("hist2_{0}_{1}".format(label, binKey), ROOT.TObject.kOverwrite)
                h2_eff_fine.CreateHistogram().Write("hist2_{0}_fine_{1}".format(label, binKey), ROOT.TObject.kOverwrite)
                if UseDataFrame: del df_acc, df_tot

        # Merge acc and rec to accXrec
        fin.cd()
        for proj in ["ProjectionX", "ProjectionY"]:
            h_acc_fine = fin.Get("h_acc_fine_{0}_{1}".format(binKey, proj))
            h_rec_fine = fin.Get("h_rec_fine_{0}_{1}".format(binKey, proj))
            hist_acc_fine = h_acc_fine.GetPassedHistogram().Clone("hist_acc_fine_{0}_{1}".format(binKey, proj))
            hist_rec_fine = h_rec_fine.GetPassedHistogram().Clone("hist_rec_fine_{0}_{1}".format(binKey, proj))
            h_accXrec_fine = h_acc_fine.GetPassedHistogram().Clone("h_accXrec_fine_{0}_{1}".format(binKey, proj))
            hist_acc_fine.Reset("ICESM")
            hist_rec_fine.Reset("ICESM")
            h_accXrec_fine.Reset("ICESM")
            for b in range(1, h_accXrec_fine.GetNbinsX() + 1):
                hist_acc_fine.SetBinContent(b, h_acc_fine.GetEfficiency(b))
                hist_acc_fine.SetBinError(b, max(h_acc_fine.GetEfficiencyErrorLow(b), h_acc_fine.GetEfficiencyErrorUp(b)))
                hist_rec_fine.SetBinContent(b, h_rec_fine.GetEfficiency(b))
                hist_rec_fine.SetBinError(b, max(h_rec_fine.GetEfficiencyErrorLow(b), h_rec_fine.GetEfficiencyErrorUp(b)))
                if h_rec_fine.GetTotalHistogram().GetBinContent(b) == 0 or h_rec_fine.GetPassedHistogram().GetBinContent(b) == 0:
                    h_accXrec_fine.SetBinContent(b, 0)
                    h_accXrec_fine.SetBinError(b, 1)
                    print(">> Empty reco eff bin #", b)
                else:
                    h_accXrec_fine.SetBinContent(b, h_acc_fine.GetEfficiency(b) * h_rec_fine.GetEfficiency(b))
                    h_accXrec_fine.SetBinError(b, h_accXrec_fine.GetBinContent(b) * math.sqrt(1 / h_acc_fine.GetTotalHistogram().GetBinContent(b) + 1 / h_acc_fine.GetPassedHistogram().GetBinContent(b) + 1 / h_rec_fine.GetTotalHistogram().GetBinContent(b) + 1 / h_rec_fine.GetPassedHistogram().GetBinContent(b)))
            hist_acc_fine.Write("hist_acc_{0}_{1}".format(binKey, proj), ROOT.TObject.kOverwrite)
            hist_rec_fine.Write("hist_rec_{0}_{1}".format(binKey, proj), ROOT.TObject.kOverwrite)
            h_accXrec_fine.Write("h_accXrec_{0}_{1}".format(binKey, proj), ROOT.TObject.kOverwrite)

        h2_acc = fin.Get("h2_acc_{0}".format(binKey))
        h2_rec = fin.Get("h2_rec_{0}".format(binKey))
        h2_accXrec = h2_acc.GetPassedHistogram().Clone("h2_accXrec_{0}".format(binKey))
        h2_accXrec.Reset("ICESM")
        for iL, iK in itertools.product(range(1, len(LBins)), range(1, len(KBins))):
            if h2_rec.GetTotalHistogram().GetBinContent(iL, iK) == 0 or h2_rec.GetPassedHistogram().GetBinContent(iL, iK) == 0 or h2_acc.GetTotalHistogram().GetBinContent(iL, iK) == 0 or h2_acc.GetPassedHistogram().GetBinContent(iL, iK) == 0:
                h2_accXrec.SetBinContent(iL, iK, 0)
                h2_accXrec.SetBinError(iL, iK, 1)
                print(">> Empty recoORacc eff bin #", iL, iK)
            else:
                iLK = h2_acc.GetGlobalBin(iL, iK)
                h2_accXrec.SetBinContent(iL, iK, h2_acc.GetEfficiency(iLK) * h2_rec.GetEfficiency(iLK))
                h2_accXrec.SetBinError(iL, iK, h2_accXrec.GetBinContent(iL, iK) * math.sqrt(1 / h2_acc.GetTotalHistogram().GetBinContent(iLK) + 1 / h2_acc.GetPassedHistogram().GetBinContent(iLK) + 1 / h2_rec.GetTotalHistogram().GetBinContent(iLK) + 1 / h2_rec.GetPassedHistogram().GetBinContent(iLK)))
        h2_accXrec.SetXTitle("cos#theta_{l}")
        h2_accXrec.SetYTitle("cos#theta_{K}")
        h2_accXrec.SetZTitle("Overall efficiency")

        h2_accXrec.Write("h2_accXrec_{0}".format(binKey), ROOT.TObject.kOverwrite)
        self.logger.logINFO("Overall efficiency is built.")

    # Register the chosen one to sourcemanager
    #  h2_accXrec = fin.Get("h2_accXrec_{0}".format(self.process.cfg['binKey']))
    Year=str(self.process.cfg['args'].Year)
    self.cfg['source'][self.name + '.h2_accXrec.{0}'.format(Year)] = h2_accXrec
    self.cfg['source'][self.name + '.accXrec.{0}'.format(Year)] = RooDataHist("accXrec", "", RooArgList(CosThetaL, CosThetaK), ROOT.RooFit.Import(h2_accXrec))
    self.cfg['source'][self.name + '.h_accXrec_fine_ProjectionX.{0}'.format(Year)] = fin.Get("h_accXrec_{0}_ProjectionX".format(self.process.cfg['binKey']))
    self.cfg['source'][self.name + '.h_accXrec_fine_ProjectionY.{0}'.format(Year)] = fin.Get("h_accXrec_{0}_ProjectionY".format(self.process.cfg['binKey']))
   
    hist2_acc_fine = fin.Get("hist2_acc_fine_{0}".format(binKey))
    self.cfg['source'][self.name + '.hist2_acc_fine.{}'.format(Year)] = hist2_acc_fine
    self.cfg['source'][self.name + '.acc_fine.{0}'.format(Year)] = RooDataHist("acc_fine", "", RooArgList(CosThetaL, CosThetaK), ROOT.RooFit.Import(hist2_acc_fine))
    self.cfg['source'][self.name + '.h_acc_fine_ProjectionX.{0}'.format(Year)] = fin.Get("hist_acc_{0}_ProjectionX".format(self.process.cfg['binKey']))
    self.cfg['source'][self.name + '.h_acc_fine_ProjectionY.{0}'.format(Year)] = fin.Get("hist_acc_{0}_ProjectionY".format(self.process.cfg['binKey']))

    hist2_rec_fine = fin.Get("hist2_rec_fine_{0}".format(binKey))
    self.cfg['source'][self.name + '.hist2_rec_fine.{}'.format(Year)] = hist2_rec_fine
    self.cfg['source'][self.name + '.rec_fine.{0}'.format(Year)] = RooDataHist("rec_fine", "", RooArgList(CosThetaL, CosThetaK), ROOT.RooFit.Import(hist2_rec_fine))
    self.cfg['source'][self.name + '.h_rec_fine_ProjectionX.{0}'.format(Year)] = fin.Get("hist_rec_{0}_ProjectionX".format(self.process.cfg['binKey']))
    self.cfg['source'][self.name + '.h_rec_fine_ProjectionY.{0}'.format(Year)] = fin.Get("hist_rec_{0}_ProjectionY".format(self.process.cfg['binKey']))

    hist2_acc = fin.Get("hist2_acc_{0}".format(binKey))
    self.cfg['source'][self.name + '.hist2_acc.{}'.format(Year)] = hist2_acc
    self.cfg['source'][self.name + '.acc.{0}'.format(Year)] = RooDataHist("acc", "", RooArgList(CosThetaL, CosThetaK), ROOT.RooFit.Import(hist2_acc))
    hist2_rec = fin.Get("hist2_rec_{0}".format(binKey))
    self.cfg['source'][self.name + '.hist2_rec.{}'.format(Year)] = hist2_rec
    self.cfg['source'][self.name + '.rec.{0}'.format(Year)] = RooDataHist("rec", "", RooArgList(CosThetaL, CosThetaK), ROOT.RooFit.Import(hist2_rec))