Esempio n. 1
0
class CMS_FWD_11_002(
        CommonFSQFramework.Core.ExampleProofReader.ExampleProofReader):
    def init(self):
        print "XXX init - CMS_FWD_11_002", self.datasetName, self.isData

        self.todoShifts = ["_central"]
        if not self.isData and self.doPtShiftsJEC:
            self.todoShifts.append("_ptUp")
            self.todoShifts.append("_ptDown")

        if not self.isData and self.doPtShiftsJER:
            self.todoShifts.append("_jerUp")
            self.todoShifts.append("_jerDown")

        self.hist = {}
        todoTrg = ["_jet15"]

        pedroPtBins = array('d', [35, 45, 57, 72, 90, 120, 150, 200])
        for shift in self.todoShifts:
            for trg in todoTrg:
                t = shift + trg
                self.hist["etaFwd" + t] = ROOT.TH1F("etaFwd" + t, "etaFwd" + t,
                                                    100, -5, 5)
                self.hist["etaCen" + t] = ROOT.TH1F("etaCen" + t, "etaCen" + t,
                                                    100, -5, 5)
                self.hist["vtx" + t] = ROOT.TH1F("vtx" + t, "vtx" + t, 10,
                                                 -0.5, 9.5)
                self.hist["ptFwd" + t] = ROOT.TH1F("ptFwd" + t, "ptFwd" + t,
                                                   len(pedroPtBins) - 1,
                                                   pedroPtBins)
                self.hist["ptCen" + t] = ROOT.TH1F("ptCen" + t, "ptCen" + t,
                                                   len(pedroPtBins) - 1,
                                                   pedroPtBins)
                self.hist["xs" + t] = ROOT.TH1F("xs" + t, "xs" + t, 1, 0, 1)

        # follow the histogram naming convention even if it makes no sense for gen - needed for drawPlots.py
        self.hist["genFwd"] = ROOT.TH1F("genJetFwd_central_jet15",
                                        "genJetFwd_central_jet15",
                                        len(pedroPtBins) - 1, pedroPtBins)
        self.hist["genCen"] = ROOT.TH1F("genJetCen_central_jet15",
                                        "genJetCen_central_jet15",
                                        len(pedroPtBins) - 1, pedroPtBins)
        self.hist["xsGen"] = ROOT.TH1F("genTot_central_jet15",
                                       "genTot_central_jet15", 1, 0, 1)

        for h in self.hist:
            self.hist[h].Sumw2()
            self.GetOutputList().Add(self.hist[h])

        puFiles = {}
        # CommonFSQFramework.Core/test/MNxsectionAna/
        jet15FileV2 = edm.FileInPath(
            "CommonFSQFramework/Core/test/MNxsectionAna/data/PUJet15V2.root"
        ).fullPath()  # MC gen distribution
        puFiles["dj15_1"] = edm.FileInPath(
            "CommonFSQFramework/Core/test/MNxsectionAna/data/pu_dj15_1_0.root"
        ).fullPath()
        puFiles["dj15_1_05"] = edm.FileInPath(
            "CommonFSQFramework/Core/test/MNxsectionAna/data/pu_dj15_1_05.root"
        ).fullPath()
        puFiles["dj15_0_95"] = edm.FileInPath(
            "CommonFSQFramework/Core/test/MNxsectionAna/data/pu_dj15_0_95.root"
        ).fullPath()
        puFiles["j15_1"] = edm.FileInPath(
            "CommonFSQFramework/Core/test/MNxsectionAna/data/pu_j15_1_0.root"
        ).fullPath()
        puFiles["j15_1_05"] = edm.FileInPath(
            "CommonFSQFramework/Core/test/MNxsectionAna/data/pu_j15_1_05.root"
        ).fullPath()
        puFiles["j15_0_95"] = edm.FileInPath(
            "CommonFSQFramework/Core/test/MNxsectionAna/data/pu_j15_0_95.root"
        ).fullPath()

        self.lumiWeighters = {}
        self.lumiWeighters["_jet15_central"] = edm.LumiReWeighting(
            jet15FileV2, puFiles["j15_1"], "MC", "pileup")
        self.lumiWeighters["_jet15_puUp"] = edm.LumiReWeighting(
            jet15FileV2, puFiles["j15_1_05"], "MC", "pileup")
        self.lumiWeighters["_jet15_puDown"] = edm.LumiReWeighting(
            jet15FileV2, puFiles["j15_0_95"], "MC", "pileup")

        self.lumiWeighters["_dj15fb_central"] = edm.LumiReWeighting(
            jet15FileV2, puFiles["dj15_1"], "MC", "pileup")
        self.lumiWeighters["_dj15fb_puUp"] = edm.LumiReWeighting(
            jet15FileV2, puFiles["dj15_1_05"], "MC", "pileup")
        self.lumiWeighters["_dj15fb_puDown"] = edm.LumiReWeighting(
            jet15FileV2, puFiles["dj15_0_95"], "MC", "pileup")

        self.jetGetter = JetGetter("PF")
        if hasattr(self, "jetUncFile"):
            self.jetGetter.setJecUncertainty(self.jetUncFile)

        #self.jetGetter.hackJER()

    def analyze(self):
        # generator level plots
        if not self.isData:
            fwdPt = None
            cenPt = None
            genJets = self.fChain.genJets
            for i in xrange(genJets.size()):
                j = genJets.at(i)
                pt = j.pt()
                if pt < 35.: continue
                eta = abs(j.eta())
                if eta > 4.7: continue
                if fwdPt == None and eta > 3.2 and eta < 4.7:
                    fwdPt = pt
                if cenPt == None and eta < 2.8:
                    cenPt = pt
            if cenPt != None and fwdPt != None:
                weight = self.fChain.genWeight
                binN = self.hist["genFwd"].FindBin(pt)
                binWidthFactor = self.hist["genFwd"].GetBinWidth(binN)
                self.hist["genFwd"].Fill(fwdPt, weight / (3. * binWidthFactor))

                binN = self.hist["genCen"].FindBin(pt)
                binWidthFactor = self.hist["genCen"].GetBinWidth(binN)
                self.hist["genCen"].Fill(cenPt,
                                         weight / (5.6 * binWidthFactor))

                self.hist["xsGen"].Fill(0.5, weight)

        if self.fChain.ngoodVTX == 0: return
        #jetID = getattr(self.fChain, self.jetID)

        self.jetGetter.newEvent(self.fChain)
        for shift in self.todoShifts:
            triggerToUse = "_jet15"
            histoName = shift + triggerToUse

            if self.isData:
                if self.fChain.jet15 < 0.5:
                    continue

            # find best fwd-central pair
            fwdPt = None
            cenPt = None
            fwdEta = None
            cenEta = None
            for jet in self.jetGetter.get(shift):
                eta = abs(jet.eta())
                if eta > 4.7: continue
                pt = jet.pt()
                if pt < 35.: continue
                #if jetID.at(i) < 0.5: continue
                #print "XA", pt, eta
                if fwdPt == None and eta > 3.2 and eta < 4.7:
                    fwdPt = pt
                    fwdEta = jet.eta()
                if cenPt == None and eta < 2.8:
                    cenPt = pt
                    cenEta = jet.eta()

            if cenPt != None and fwdPt != None:
                if not self.isData:
                    weightBase = self.fChain.genWeight  # keep inside shift iter
                    truePU = self.fChain.puTrueNumInteractions
                    puWeight = self.lumiWeighters[triggerToUse +
                                                  "_central"].weight(truePU)
                    weight = puWeight * weightBase
                else:
                    weight = 1.

                self.hist["xs" + histoName].Fill(0.5, weight)

                etaFactor = 3.
                binN = self.hist["ptFwd" + histoName].FindBin(fwdPt)
                binWidthFactor = self.hist["ptFwd" +
                                           histoName].GetBinWidth(binN)
                factor = etaFactor * binWidthFactor
                self.hist["ptFwd" + histoName].Fill(fwdPt, weight / factor)
                self.hist["etaFwd" + histoName].Fill(fwdEta, weight / factor)

                etaFactor = 5.6
                binN = self.hist["ptCen" + histoName].FindBin(cenPt)
                binWidthFactor = self.hist["ptCen" +
                                           histoName].GetBinWidth(binN)
                factor = etaFactor * binWidthFactor
                self.hist["ptCen" + histoName].Fill(cenPt, weight / factor)
                self.hist["etaCen" + histoName].Fill(cenEta, weight / factor)

                self.hist["vtx" + histoName].Fill(self.fChain.ngoodVTX, weight)
        return 1

    def finalize(self):
        print "Finalize:"
        normFactor = self.getNormalizationFactor()
        print "  applying norm", normFactor
        for h in self.hist:
            self.hist[h].Scale(normFactor)
class CMS_FWD_11_002(CommonFSQFramework.Core.ExampleProofReader.ExampleProofReader):
    def init( self):
        print "XXX init - CMS_FWD_11_002", self.datasetName, self.isData

        self.todoShifts = ["_central"]
        if not self.isData and self.doPtShiftsJEC:
            self.todoShifts.append("_ptUp")
            self.todoShifts.append("_ptDown")

        if not self.isData and self.doPtShiftsJER:
            self.todoShifts.append("_jerUp")
            self.todoShifts.append("_jerDown")

        self.hist = {}
        todoTrg = ["_jet15"]

        pedroPtBins = array('d', [35, 45, 57, 72, 90, 120, 150, 200])
        for shift in self.todoShifts:
            for trg in todoTrg:
                t = shift+trg
                self.hist["etaFwd"+t] =  ROOT.TH1F("etaFwd"+t,   "etaFwd"+t,  100, -5, 5)
                self.hist["etaCen"+t] =  ROOT.TH1F("etaCen"+t,   "etaCen"+t,  100, -5, 5)
                self.hist["vtx"+t] =  ROOT.TH1F("vtx"+t,   "vtx"+t,  10, -0.5, 9.5)
                self.hist["ptFwd"+t] = ROOT.TH1F("ptFwd"+t, "ptFwd"+t, len(pedroPtBins)-1, pedroPtBins)
                self.hist["ptCen"+t] =  ROOT.TH1F("ptCen"+t,   "ptCen"+t,   len(pedroPtBins)-1, pedroPtBins)
                self.hist["xs"+t] = ROOT.TH1F("xs"+t, "xs"+t, 1, 0, 1)
                

        # follow the histogram naming convention even if it makes no sense for gen - needed for drawPlots.py
        self.hist["genFwd"] = ROOT.TH1F("genJetFwd_central_jet15", "genJetFwd_central_jet15", len(pedroPtBins)-1, pedroPtBins)
        self.hist["genCen"] = ROOT.TH1F("genJetCen_central_jet15", "genJetCen_central_jet15", len(pedroPtBins)-1, pedroPtBins)
        self.hist["xsGen"] = ROOT.TH1F("genTot_central_jet15", "genTot_central_jet15", 1, 0, 1)

        for h in self.hist:
            self.hist[h].Sumw2()
            self.GetOutputList().Add(self.hist[h])

        puFiles = {}
        # CommonFSQFramework.Core/test/MNxsectionAna/
        jet15FileV2 = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/PUJet15V2.root").fullPath()   # MC gen distribution
        puFiles["dj15_1"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_dj15_1_0.root").fullPath()
        puFiles["dj15_1_05"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_dj15_1_05.root").fullPath()
        puFiles["dj15_0_95"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_dj15_0_95.root").fullPath()
        puFiles["j15_1"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_j15_1_0.root").fullPath()
        puFiles["j15_1_05"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_j15_1_05.root").fullPath()
        puFiles["j15_0_95"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_j15_0_95.root").fullPath()

        self.lumiWeighters = {}
        self.lumiWeighters["_jet15_central"] = edm.LumiReWeighting(jet15FileV2, puFiles["j15_1"], "MC", "pileup")
        self.lumiWeighters["_jet15_puUp"] = edm.LumiReWeighting(jet15FileV2, puFiles["j15_1_05"], "MC", "pileup")
        self.lumiWeighters["_jet15_puDown"] = edm.LumiReWeighting(jet15FileV2, puFiles["j15_0_95"], "MC", "pileup")

        self.lumiWeighters["_dj15fb_central"] = edm.LumiReWeighting(jet15FileV2, puFiles["dj15_1"], "MC", "pileup")
        self.lumiWeighters["_dj15fb_puUp"] = edm.LumiReWeighting(jet15FileV2, puFiles["dj15_1_05"], "MC", "pileup")
        self.lumiWeighters["_dj15fb_puDown"] = edm.LumiReWeighting(jet15FileV2, puFiles["dj15_0_95"], "MC", "pileup")

        self.jetGetter = JetGetter("PF")
        if hasattr(self, "jetUncFile"):
            self.jetGetter.setJecUncertainty(self.jetUncFile)

        #self.jetGetter.hackJER()


    def analyze(self):
        # generator level plots
        if not self.isData:
            fwdPt = None
            cenPt = None
            genJets = self.fChain.genJets
            for i in xrange(genJets.size()):
                j = genJets.at(i)
                pt = j.pt()
                if pt < 35. : continue
                eta = abs(j.eta())
                if eta > 4.7: continue
                if fwdPt == None and eta > 3.2 and eta < 4.7:
                    fwdPt = pt
                if cenPt == None and eta < 2.8:
                    cenPt = pt
            if cenPt != None and fwdPt != None:
                weight = self.fChain.genWeight
                binN =  self.hist["genFwd"].FindBin(pt)
                binWidthFactor = self.hist["genFwd"].GetBinWidth(binN)
                self.hist["genFwd"].Fill(fwdPt, weight/(3.*binWidthFactor))

                binN =  self.hist["genCen"].FindBin(pt)
                binWidthFactor = self.hist["genCen"].GetBinWidth(binN)
                self.hist["genCen"].Fill(cenPt, weight/(5.6*binWidthFactor))

                self.hist["xsGen"].Fill(0.5, weight)



        if self.fChain.ngoodVTX == 0: return
        #jetID = getattr(self.fChain, self.jetID)

        self.jetGetter.newEvent(self.fChain)
        for shift in self.todoShifts:
            triggerToUse = "_jet15"
            histoName = shift +triggerToUse

            if self.isData:
                if self.fChain.jet15 < 0.5:
                    continue

            # find best fwd-central pair
            fwdPt = None
            cenPt = None
            fwdEta = None
            cenEta = None
            for jet in self.jetGetter.get(shift):
                eta = abs(jet.eta())
                if eta > 4.7: continue
                pt = jet.pt()
                if pt < 35. : continue
                #if jetID.at(i) < 0.5: continue
                #print "XA", pt, eta
                if fwdPt == None and eta > 3.2 and eta < 4.7:
                    fwdPt = pt
                    fwdEta = jet.eta()
                if cenPt == None and eta < 2.8:
                    cenPt = pt
                    cenEta = jet.eta()

            if cenPt != None and fwdPt != None:
                if not self.isData:
                    weightBase = self.fChain.genWeight # keep inside shift iter
                    truePU = self.fChain.puTrueNumInteractions
                    puWeight =  self.lumiWeighters[triggerToUse+"_central"].weight(truePU)
                    weight = puWeight*weightBase
                else:
                    weight = 1.

                self.hist["xs"+histoName].Fill(0.5, weight)

                etaFactor = 3.
                binN =  self.hist["ptFwd"+histoName].FindBin(fwdPt)
                binWidthFactor = self.hist["ptFwd"+histoName].GetBinWidth(binN)
                factor = etaFactor*binWidthFactor
                self.hist["ptFwd"+histoName].Fill(fwdPt, weight/factor)
                self.hist["etaFwd"+histoName].Fill(fwdEta , weight/factor)

                etaFactor = 5.6
                binN =  self.hist["ptCen"+histoName].FindBin(cenPt)
                binWidthFactor = self.hist["ptCen"+histoName].GetBinWidth(binN)
                factor = etaFactor*binWidthFactor
                self.hist["ptCen"+histoName].Fill(cenPt, weight/factor)
                self.hist["etaCen"+histoName].Fill(cenEta, weight/factor)

                self.hist["vtx"+histoName].Fill(self.fChain.ngoodVTX, weight)
        return 1

    def finalize(self):
        print "Finalize:"
        normFactor = self.getNormalizationFactor()
        print "  applying norm", normFactor
        for h in self.hist:
            self.hist[h].Scale(normFactor)
Esempio n. 3
0
class RecoGenRatio(CommonFSQFramework.Core.ExampleProofReader.ExampleProofReader):
    def init( self):

        self.tree = ROOT.TTree("data", "data")
        self.GetOutputList().Add(self.tree)

        self.var = {}
        self.todoShifts = ["_central"]

        if not self.isData and self.doPtShiftsJEC:
            self.todoShifts.append("_ptUp")
            self.todoShifts.append("_ptDown")

        if not self.isData and self.doPtShiftsJER:
            self.todoShifts.append("_jerUp")
            self.todoShifts.append("_jerDown")

        # this is ugly
        #  by mimicking the tree structure of the balance method
        #  we will be able to use same plot/fit util
        for t in self.todoShifts:
            self.var["tagPt"+t] = array('d', [0])
            self.var["tagEta"+t] = array('d', [0])
            self.var["probePt"+t] = array('d', [0])
            self.var["probeEta"+t] = array('d', [0])
            self.var["ptAve"+t] = array('d', [0])
            self.var["balance"+t] = array('d', [0])

            #self.var["jetEta"+t] = array('d', [0])
            #self.var["jetR"+t] = array('d', [0])

        self.var["weight"] = array('d', [0])
        
        for v in self.var:
            self.tree.Branch(v, self.var[v], v+"/D")
        
        jet15FileV2 = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/PUJet15V2.root").fullPath()   # MC gen distribution
        puFiles = {}
        puFiles["dj15_1"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_dj15_1_0.root").fullPath()
        puFiles["dj15_1_05"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_dj15_1_05.root").fullPath()
        puFiles["dj15_0_95"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_dj15_0_95.root").fullPath()
        puFiles["j15_1"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_j15_1_0.root").fullPath()
        puFiles["j15_1_05"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_j15_1_05.root").fullPath()
        puFiles["j15_0_95"] = edm.FileInPath("CommonFSQFramework/Core/test/MNxsectionAna/data/pu_j15_0_95.root").fullPath()

        self.lumiWeighters = {}
        self.lumiWeighters["_jet15_central"] = edm.LumiReWeighting(jet15FileV2, puFiles["j15_1"], "MC", "pileup")
        self.lumiWeighters["_jet15_puUp"] = edm.LumiReWeighting(jet15FileV2, puFiles["j15_1_05"], "MC", "pileup")
        self.lumiWeighters["_jet15_puDown"] = edm.LumiReWeighting(jet15FileV2, puFiles["j15_0_95"], "MC", "pileup")

        self.lumiWeighters["_dj15fb_central"] = edm.LumiReWeighting(jet15FileV2, puFiles["dj15_1"], "MC", "pileup")
        self.lumiWeighters["_dj15fb_puUp"] = edm.LumiReWeighting(jet15FileV2, puFiles["dj15_1_05"], "MC", "pileup")
        self.lumiWeighters["_dj15fb_puDown"] = edm.LumiReWeighting(jet15FileV2, puFiles["dj15_0_95"], "MC", "pileup")


        self.jetGetter = JetGetter("PF")
        if hasattr(self, "jetUncFile"):
            self.jetGetter.setJecUncertainty(self.jetUncFile)

        sys.stdout.flush()

    def analyze(self):
        if self.isData:
                return 1
            
        for v in self.var:
            self.var[v][0] = 0
    


        self.jetGetter.newEvent(self.fChain)
        for shift in self.todoShifts:
            weight = 1. 
            if not self.isData:
                weight *= self.fChain.genWeight # keep inside shift iter
                truePU = self.fChain.puTrueNumInteractions
                puWeight =  self.lumiWeighters["_jet15_central"].weight(truePU)
                weight *= puWeight

            self.var["weight"][0] = weight
            for jet in self.jetGetter.get(shift):
                pt = jet.pt()
                if pt < 35: continue
                eta = jet.eta()
                if abs(eta) > self.etaMax: continue
                if abs(eta) < self.etaMin: continue
                if not jet.looseId(): continue

                genPt = jet.genP4().pt()
                if genPt < 0.01:
                    continue
                r = (genPt-pt)/genPt

                self.var["tagPt"+shift][0]  = 99
                self.var["tagEta"+shift][0] = 0.
                self.var["probePt"+shift][0] = pt
                self.var["probeEta"+shift][0] = eta
                self.var["ptAve"+shift][0] = 99
                self.var["balance"+shift][0] = r
                self.tree.Fill()



        return 1

    def finalize(self):
        print "Finalize:"