示例#1
0
    def init(self):
        self.jetGetter = JetGetter("PFAK4CHS")
        self.dr = ROOT.Math.VectorUtil.DeltaR
        self.normFactor = self.getNormalizationFactor()

        self.var = {}
        self.var["recoEta"] = array('d', [0])
        self.var["recoPt"] = array('d', [0])
        #self.var["hltEta"] = array('d', [0])
        self.var["hltPt"] = array('d', [0])
        #self.var["l1Eta"] = array('d', [0])
        self.var["l1Pt"] = array('d', [0])
        #self.var["s1l1Eta"] = array('d', [0])
        self.var["s1l1Pt"] = array('d', [0])
        self.var["PUNumInteractions"] = array('d', [0])
        self.var["puTrueNumInteractions"] = array('d', [0])
        self.var["weight"] = array('d', [0])

        # for the PU file run
        # utils/GetFlatPUDist.py
        puFile = edm.FileInPath(
            "CommonFSQFramework.Core/test/mnTrgAnalyzer/PUhists.root"
        ).fullPath()

        self.newlumiWeighters = {}
        #'''
        self.newlumiWeighters["flat010toflat010"] = edm.LumiReWeighting(
            puFile, puFile, "Flat0to10/pileup", "Flat0to10/pileup")
        self.newlumiWeighters["flat010toPU1"] = edm.LumiReWeighting(
            puFile, puFile, "Flat0to10/pileup", "PU1/pileup")
        #self.newlumiWeighters["flat010toPU2"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU2/pileup")
        #self.newlumiWeighters["flat010toPU3"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU3/pileup")
        #self.newlumiWeighters["flat010toPU4"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU4/pileup")
        self.newlumiWeighters["flat010toPU5"] = edm.LumiReWeighting(
            puFile, puFile, "Flat0to10/pileup", "PU5/pileup")
        self.newlumiWeighters["flat010toPU10"] = edm.LumiReWeighting(
            puFile, puFile, "Flat0to10/pileup", "PU10/pileup")
        #'''
        '''
        self.newlumiWeighters["flat2050toPU20"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU20/pileup")
        self.newlumiWeighters["flat2050toPU25"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU25/pileup")
        self.newlumiWeighters["flat2050toPU30"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU30/pileup")
        #'''
        '''
        self.newlumiWeighters["PU20to15"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU15/pileup")
        self.newlumiWeighters["PU20to18"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU18/pileup")
        self.newlumiWeighters["PU20to19"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU19/pileup")
        self.newlumiWeighters["PU20to21"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU21/pileup")
        self.newlumiWeighters["PU20to22"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU22/pileup")
        self.newlumiWeighters["PU20to20"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU20/pileup")
        self.newlumiWeighters["PU20to25"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU25/pileup")
        #'''
        for l in self.newlumiWeighters.keys():
            self.var[l] = array('d', [0])

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

        for v in self.var:
            self.tree.Branch(v, self.var[v], v + "/D")
示例#2
0
    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 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)
    def init(self):
        self.jetGetter = JetGetter("PFAK4CHS")
        self.dr = ROOT.Math.VectorUtil.DeltaR
        self.normFactor = self.getNormalizationFactor()

        self.var = {}
        self.var["recoEta"] = array('d', [0])
        self.var["recoPt"] = array('d', [0])
        #self.var["hltEta"] = array('d', [0])
        self.var["hltPt"] = array('d', [0])
        #self.var["l1Eta"] = array('d', [0])
        self.var["l1Pt"] = array('d', [0])
        #self.var["s1l1Eta"] = array('d', [0])
        self.var["s1l1Pt"] = array('d', [0])
        self.var["PUNumInteractions"] = array('d', [0])
        self.var["puTrueNumInteractions"] = array('d', [0])
        self.var["weight"] = array('d', [0])


        # for the PU file run
        # utils/GetFlatPUDist.py
        puFile = edm.FileInPath("CommonFSQFramework.Core/test/mnTrgAnalyzer/PUhists.root").fullPath()

        self.newlumiWeighters = {}
        #'''
        self.newlumiWeighters["flat010toflat010"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "Flat0to10/pileup")
        self.newlumiWeighters["flat010toPU1"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU1/pileup")
        #self.newlumiWeighters["flat010toPU2"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU2/pileup")
        #self.newlumiWeighters["flat010toPU3"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU3/pileup")
        #self.newlumiWeighters["flat010toPU4"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU4/pileup")
        self.newlumiWeighters["flat010toPU5"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU5/pileup")
        self.newlumiWeighters["flat010toPU10"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU10/pileup")
        #'''
        '''
        self.newlumiWeighters["flat2050toPU20"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU20/pileup")
        self.newlumiWeighters["flat2050toPU25"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU25/pileup")
        self.newlumiWeighters["flat2050toPU30"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU30/pileup")
        #'''

        '''
        self.newlumiWeighters["PU20to15"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU15/pileup")
        self.newlumiWeighters["PU20to18"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU18/pileup")
        self.newlumiWeighters["PU20to19"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU19/pileup")
        self.newlumiWeighters["PU20to21"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU21/pileup")
        self.newlumiWeighters["PU20to22"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU22/pileup")
        self.newlumiWeighters["PU20to20"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU20/pileup")
        self.newlumiWeighters["PU20to25"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU25/pileup")
        #'''
        for l in self.newlumiWeighters.keys():
            self.var[l] =  array('d', [0])

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

        for v in self.var:
            self.tree.Branch(v, self.var[v], v+"/D")
示例#5
0
class HLTEfficiencyTreeProducer(
        CommonFSQFramework.Core.ExampleProofReader.ExampleProofReader):
    def init(self):
        self.jetGetter = JetGetter("PFAK4CHS")
        self.dr = ROOT.Math.VectorUtil.DeltaR
        self.normFactor = self.getNormalizationFactor()

        self.var = {}
        self.var["recoEta"] = array('d', [0])
        self.var["recoPt"] = array('d', [0])
        #self.var["hltEta"] = array('d', [0])
        self.var["hltPt"] = array('d', [0])
        #self.var["l1Eta"] = array('d', [0])
        self.var["l1Pt"] = array('d', [0])
        #self.var["s1l1Eta"] = array('d', [0])
        self.var["s1l1Pt"] = array('d', [0])
        self.var["PUNumInteractions"] = array('d', [0])
        self.var["puTrueNumInteractions"] = array('d', [0])
        self.var["weight"] = array('d', [0])

        # for the PU file run
        # utils/GetFlatPUDist.py
        puFile = edm.FileInPath(
            "CommonFSQFramework.Core/test/mnTrgAnalyzer/PUhists.root"
        ).fullPath()

        self.newlumiWeighters = {}
        #'''
        self.newlumiWeighters["flat010toflat010"] = edm.LumiReWeighting(
            puFile, puFile, "Flat0to10/pileup", "Flat0to10/pileup")
        self.newlumiWeighters["flat010toPU1"] = edm.LumiReWeighting(
            puFile, puFile, "Flat0to10/pileup", "PU1/pileup")
        #self.newlumiWeighters["flat010toPU2"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU2/pileup")
        #self.newlumiWeighters["flat010toPU3"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU3/pileup")
        #self.newlumiWeighters["flat010toPU4"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU4/pileup")
        self.newlumiWeighters["flat010toPU5"] = edm.LumiReWeighting(
            puFile, puFile, "Flat0to10/pileup", "PU5/pileup")
        self.newlumiWeighters["flat010toPU10"] = edm.LumiReWeighting(
            puFile, puFile, "Flat0to10/pileup", "PU10/pileup")
        #'''
        '''
        self.newlumiWeighters["flat2050toPU20"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU20/pileup")
        self.newlumiWeighters["flat2050toPU25"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU25/pileup")
        self.newlumiWeighters["flat2050toPU30"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU30/pileup")
        #'''
        '''
        self.newlumiWeighters["PU20to15"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU15/pileup")
        self.newlumiWeighters["PU20to18"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU18/pileup")
        self.newlumiWeighters["PU20to19"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU19/pileup")
        self.newlumiWeighters["PU20to21"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU21/pileup")
        self.newlumiWeighters["PU20to22"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU22/pileup")
        self.newlumiWeighters["PU20to20"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU20/pileup")
        self.newlumiWeighters["PU20to25"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU25/pileup")
        #'''
        for l in self.newlumiWeighters.keys():
            self.var[l] = array('d', [0])

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

        for v in self.var:
            self.tree.Branch(v, self.var[v], v + "/D")

    def match(self, jetReco, jets):
        bestMatch = None
        for jet in jets:
            dr = self.dr(jetReco.p4(), jet)
            if dr < 0.3:
                if bestMatch == None or bestMatch.pt() < jet.pt():
                    bestMatch = jet

        return bestMatch

    def genWeight(self):
        return self.fChain.genWeight * self.normFactor

    def analyze(self):
        self.jetGetter.newEvent(self.fChain)
        self.var["PUNumInteractions"][0] = self.fChain.PUNumInteractions
        self.var["puTrueNumInteractions"][
            0] = self.fChain.puTrueNumInteractions

        pu = self.fChain.PUNumInteractions
        genW = self.genWeight()
        self.var["weight"][0] = genW
        for l in self.newlumiWeighters:
            w = self.newlumiWeighters[l].weight(pu)
            #print pu, l, w, genW
            self.var[l][0] = w * genW

        todo = {}
        todo["l1"] = self.fChain.l1Jets
        todo["s1l1"] = self.fChain.stage1L1Jets
        todo["hlt"] = self.fChain.hltAK4PFJetsCorrected
        for jet in self.jetGetter.get("_central"):
            if jet.pt() < 30: continue
            self.var["recoPt"][0] = jet.pt()
            self.var["recoEta"][0] = jet.eta()
            for t in todo.keys(
            ):  # reset subset of variables (rest is filled each time in loop)
                self.var[t + "Pt"][0] = 0

            for t in todo:
                ptMatch = 0
                match = self.match(jet, todo[t])
                if match != None:
                    self.var[t + "Pt"][0] = match.pt()
            self.tree.Fill()

        # self.fChain.hltAK4PFJetsCorrected

    def finalize(self):
        print "Finalize HLTBala:"
    def init(self):
        #self.pr = cProfile.Profile()

        print "Params:", self.etaMax, self.ptMin

        if not self.isData:
            #self.hltMCWeighter = HLTMCWeighter("HLT_Jet15U")
            self.HLTMCWeighterJ15Raw = HLTMCWeighter("HLT_Jet15U_raw")
            self.HLTMCWeighterJ15L1Raw = HLTMCWeighter("HLT_Jet15U_L1Seeding_raw")
            #self.HLTMCWeighterDJ15FBRaw = HLTMCWeighter("HLT_DoubleJet15U_ForwardBackward_raw")
            #self.HLTMCWeighterDJ15L1FBRaw = HLTMCWeighter("HLT_DoubleJet15U_ForwardBackward_L1Seeding_raw")

        self.normFactor = self.getNormalizationFactor()
        self.dphi = ROOT.Math.VectorUtil.DeltaPhi

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

        self.var = {}
        self.histos = {}
        self.histos["evcnt"] =  ROOT.TH1F("evcnt_central_jet15", "evcnt_central_jet15",  1, -0.5, 0.5)

        self.todoShifts = ["_central"]

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

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


        trg = "_jet15"
        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["veto1"+t] = array('d', [0])
            self.var["veto2"+t] = array('d', [0])

            histoPostFix = t+trg
            self.histos["ptProbe"+t] = ROOT.TH1F("ptProbe"+histoPostFix, "ptProbe"+histoPostFix, 100, 0, 100)
            self.histos["ptTag"+t] = ROOT.TH1F("ptTag"+histoPostFix, "ptTag"+histoPostFix, 100, 0, 100)
            self.histos["etaProbe"+t] = ROOT.TH1F("etaProbe"+histoPostFix, "etaProbe"+histoPostFix, 35, 1.3, 4.8)
            self.histos["etaTag"+t] = ROOT.TH1F("etaTag"+histoPostFix, "etaTag"+histoPostFix, 15, 0, 1.5)
            self.histos["nvtx"+t] = ROOT.TH1F("nvtx"+histoPostFix, "nvtx"+histoPostFix, 10, -0.5, 9.5)
 

        for t in self.histos:
            #self.histos[t][1] = ROOT.TH1F(name, name, nbins, self.histos[t][2], self.histos[t][3])
            self.histos[t].Sumw2()
            #self.GetOutputList().Add(self.histos[t])
            self.addToOutput(self.histos[t])

        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")



        if self.HLT2015TempWorkaround:
            #self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="recoPFAK4ChsCorrectedMyRhop4")
            #self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="recoPFAK4ChsCorrectedp4")
            self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="hltAK4PFJetsCorrectedp4")
            #self.jetGetter = JetGetter("PFAK4CHS")
            #self.jetGetter = JetGetter("PFAK5CHS")
            #self.jetGetter = JetGetter("PF")
            self.jetGetter.disableGenJet()
            self.jetGetter.disableJetId()
            #self.jetGetter = GenJetProxy()
        else:
            self.jetGetter = JetGetter("PFAK5")
            #self.jetGetter = JetGetter("PFlegacy")
            self.jetGetter.disableGenJet()

        #self.jetGetter = BetterJetGetter("PFAK5") 
        #self.jetGetter = BetterJetGetter("Calo") 

        '''
        if self.isData:
            self.jetGetter = JetGetter("PFAK5") 
        else:
            self.jetGetter = JetGetter("PFlegacy") 
        '''

        self.varE = {}
        sys.stdout.flush()
class BalanceTreeProducer(CommonFSQFramework.Core.ExampleProofReader.ExampleProofReader):
    def init(self):
        #self.pr = cProfile.Profile()

        print "Params:", self.etaMax, self.ptMin

        if not self.isData:
            #self.hltMCWeighter = HLTMCWeighter("HLT_Jet15U")
            self.HLTMCWeighterJ15Raw = HLTMCWeighter("HLT_Jet15U_raw")
            self.HLTMCWeighterJ15L1Raw = HLTMCWeighter("HLT_Jet15U_L1Seeding_raw")
            #self.HLTMCWeighterDJ15FBRaw = HLTMCWeighter("HLT_DoubleJet15U_ForwardBackward_raw")
            #self.HLTMCWeighterDJ15L1FBRaw = HLTMCWeighter("HLT_DoubleJet15U_ForwardBackward_L1Seeding_raw")

        self.normFactor = self.getNormalizationFactor()
        self.dphi = ROOT.Math.VectorUtil.DeltaPhi

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

        self.var = {}
        self.histos = {}
        self.histos["evcnt"] =  ROOT.TH1F("evcnt_central_jet15", "evcnt_central_jet15",  1, -0.5, 0.5)

        self.todoShifts = ["_central"]

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

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


        trg = "_jet15"
        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["veto1"+t] = array('d', [0])
            self.var["veto2"+t] = array('d', [0])

            histoPostFix = t+trg
            self.histos["ptProbe"+t] = ROOT.TH1F("ptProbe"+histoPostFix, "ptProbe"+histoPostFix, 100, 0, 100)
            self.histos["ptTag"+t] = ROOT.TH1F("ptTag"+histoPostFix, "ptTag"+histoPostFix, 100, 0, 100)
            self.histos["etaProbe"+t] = ROOT.TH1F("etaProbe"+histoPostFix, "etaProbe"+histoPostFix, 35, 1.3, 4.8)
            self.histos["etaTag"+t] = ROOT.TH1F("etaTag"+histoPostFix, "etaTag"+histoPostFix, 15, 0, 1.5)
            self.histos["nvtx"+t] = ROOT.TH1F("nvtx"+histoPostFix, "nvtx"+histoPostFix, 10, -0.5, 9.5)
 

        for t in self.histos:
            #self.histos[t][1] = ROOT.TH1F(name, name, nbins, self.histos[t][2], self.histos[t][3])
            self.histos[t].Sumw2()
            #self.GetOutputList().Add(self.histos[t])
            self.addToOutput(self.histos[t])

        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")



        if self.HLT2015TempWorkaround:
            #self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="recoPFAK4ChsCorrectedMyRhop4")
            #self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="recoPFAK4ChsCorrectedp4")
            self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="hltAK4PFJetsCorrectedp4")
            #self.jetGetter = JetGetter("PFAK4CHS")
            #self.jetGetter = JetGetter("PFAK5CHS")
            #self.jetGetter = JetGetter("PF")
            self.jetGetter.disableGenJet()
            self.jetGetter.disableJetId()
            #self.jetGetter = GenJetProxy()
        else:
            self.jetGetter = JetGetter("PFAK5")
            #self.jetGetter = JetGetter("PFlegacy")
            self.jetGetter.disableGenJet()

        #self.jetGetter = BetterJetGetter("PFAK5") 
        #self.jetGetter = BetterJetGetter("Calo") 

        '''
        if self.isData:
            self.jetGetter = JetGetter("PFAK5") 
        else:
            self.jetGetter = JetGetter("PFlegacy") 
        '''

        self.varE = {}
        sys.stdout.flush()

    def addExternalVar(self, names):
        for name in names:
            self.varE[name] =  0.
            self.var[name] = array('d', [0])
            self.tree.Branch(name, self.var[name], name+"/D")

    def setExternals(self):
        for v in self.varE:
            self.var[v][0] = self.varE[v]
    def fill(self):
            self.tree.Fill()
    def resetExternals(self):
        for v in self.varE: 
            self.varE[v] = 0

    def fillGenWeight(self):
        weight = self.genWeight()
        self.var["weight"][0] = weight


    def setExternalVar(self, name, val):
        self.varE[name] = val

    def genWeight(self):
        #print "ASDFASD", self.fChain.genWeight
        return self.fChain.genWeight*self.normFactor
    
    def weight(self):
        if self.weightCache != None:
            return self.weightCache
        if not self.isData:
            weight = self.genWeight()
            if not self.HLT2015TempWorkaround:
                truePU = self.fChain.puTrueNumInteractions
                puWeight =  self.lumiWeighters["_jet15_central"].weight(truePU)
                weight *= puWeight
                w1 = self.HLTMCWeighterJ15L1Raw.getWeight()
                w2 = self.HLTMCWeighterJ15Raw.getWeight()
                triggerEff = w1*w2
                weight *= triggerEff
        else:
            weight = 1

        self.weightCache = weight
        return weight



    '''
    # stuff for code profiling
    def analyze(self):
        self.pr.enable()
        self.analyzeTT()
        self.pr.disable()
    def analyzeTT(self):
        '''
    def analyze(self):
        self.weightCache = None
        self.histos["evcnt"].Fill(0)
        # '''
        if not self.HLT2015TempWorkaround:
            if self.fChain.ngoodVTX == 0: return
            if self.fChain.HBHENoiseFilterResult == 0: return


        if self.isData:
            if self.fChain.jet15 < 0.5:
                return 1
            
        for v in self.var:
            self.var[v][0] = 0

        # reset is done after fill
        self.setExternals()

        self.jetGetter.newEvent(self.fChain)
        if not self.isData:
            self.HLTMCWeighterJ15L1Raw.newEvent(self.fChain)
            self.HLTMCWeighterJ15Raw.newEvent(self.fChain)


        # xxxwei

            

        fill = False
        for shift in self.todoShifts:
            tagJet = None
            probeJet = None
            probePT = None
            tagPT = None


            for jet in self.jetGetter.get(shift):
                pt = jet.pt()

                if pt < self.ptMin: continue
                eta = abs(jet.eta())
                if eta > self.etaMax: continue

                if not self.HLT2015TempWorkaround:
                    if not jet.jetid(): continue
                if eta < 1.4:
                    tagJet = jet
                    tagPT = pt
                else:
                    probeJet = jet
                    probePT = pt

            if tagJet != None and probeJet != None:
                dphi = abs(self.dphi(tagJet.p4(), probeJet.p4()))
                if dphi < 2.7: continue
                
                # check veto:
                ptAve = (probePT+tagPT)/2
                if ptAve < 25: continue


                veto2 = -1
                #for jet in self.jetGetter.get(shift):
                for jet in self.jetGetter.get(shift):
                    if jet == tagJet or probeJet == jet: continue
                    eta = abs(jet.eta())
                    if eta > self.etaMax: continue
                    vetoCand = jet.pt()/ptAve
                    if veto2 < vetoCand:
                        veto2 = vetoCand

                tagEta = abs(tagJet.eta())
                probeEta = abs(probeJet.eta())

                self.var["tagPt"+shift][0] = tagPT 
                self.var["tagEta"+shift][0] = tagEta
                self.var["probePt"+shift][0] = probePT
                self.var["probeEta"+shift][0] = probeEta
                self.var["ptAve"+shift][0] = ptAve
                self.var["balance"+shift][0] = (probePT-tagPT)/ptAve
                #self.var["veto1"+shift][0] = veto1
                self.var["veto2"+shift][0] = veto2
                fill = True


                #print "Hist fill", weight
                weight = self.weight()
                self.histos["ptProbe"+shift].Fill(probePT, weight)
                self.histos["ptTag"+shift].Fill(tagPT, weight)
                self.histos["etaProbe"+shift].Fill(probeEta, weight)
                self.histos["etaTag"+shift].Fill(tagEta, weight)
                #self.histos["nvtx"+shift].Fill(self.fChain.ngoodVTX, weight)

   
        # at least one variation ok.
        if fill:
            self.var["weight"][0] = self.weight()
            self.fill()

        self.resetExternals()

        return 1

    def finalize(self):
        print "Finalize:"
        if hasattr(self, "pr"):
            dname = "/nfs/dust/cms/user/fruboest/2014.11.MN2010/CMSSW_4_2_8_lowpupatch1/src/CommonFSQFramework.Core/test/MNxsectionAna/"
            profName = dname + "stats"
            self.pr.dump_stats(profName)
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)
示例#9
0
    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)
示例#10
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)
示例#11
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:"
    def init(self):
        #self.pr = cProfile.Profile()

        print "Params:", self.etaMax, self.ptMin

        if not self.isData:
            #self.hltMCWeighter = HLTMCWeighter("HLT_Jet15U")
            self.HLTMCWeighterJ15Raw = HLTMCWeighter("HLT_Jet15U_raw")
            self.HLTMCWeighterJ15L1Raw = HLTMCWeighter(
                "HLT_Jet15U_L1Seeding_raw")
            #self.HLTMCWeighterDJ15FBRaw = HLTMCWeighter("HLT_DoubleJet15U_ForwardBackward_raw")
            #self.HLTMCWeighterDJ15L1FBRaw = HLTMCWeighter("HLT_DoubleJet15U_ForwardBackward_L1Seeding_raw")

        self.normFactor = self.getNormalizationFactor()
        self.dphi = ROOT.Math.VectorUtil.DeltaPhi

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

        self.var = {}
        self.histos = {}
        self.histos["evcnt"] = ROOT.TH1F("evcnt_central_jet15",
                                         "evcnt_central_jet15", 1, -0.5, 0.5)

        self.todoShifts = ["_central"]

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

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

        trg = "_jet15"
        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["veto1"+t] = array('d', [0])
            self.var["veto2" + t] = array('d', [0])

            histoPostFix = t + trg
            self.histos["ptProbe" + t] = ROOT.TH1F("ptProbe" + histoPostFix,
                                                   "ptProbe" + histoPostFix,
                                                   100, 0, 100)
            self.histos["ptTag" + t] = ROOT.TH1F("ptTag" + histoPostFix,
                                                 "ptTag" + histoPostFix, 100,
                                                 0, 100)
            self.histos["etaProbe" + t] = ROOT.TH1F("etaProbe" + histoPostFix,
                                                    "etaProbe" + histoPostFix,
                                                    35, 1.3, 4.8)
            self.histos["etaTag" + t] = ROOT.TH1F("etaTag" + histoPostFix,
                                                  "etaTag" + histoPostFix, 15,
                                                  0, 1.5)
            self.histos["nvtx" + t] = ROOT.TH1F("nvtx" + histoPostFix,
                                                "nvtx" + histoPostFix, 10,
                                                -0.5, 9.5)

        for t in self.histos:
            #self.histos[t][1] = ROOT.TH1F(name, name, nbins, self.histos[t][2], self.histos[t][3])
            self.histos[t].Sumw2()
            #self.GetOutputList().Add(self.histos[t])
            self.addToOutput(self.histos[t])

        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")

        if self.HLT2015TempWorkaround:
            #self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="recoPFAK4ChsCorrectedMyRhop4")
            #self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="recoPFAK4ChsCorrectedp4")
            self.jetGetter = JetGetter(
                "PFAK4CHS", jetColOverride="hltAK4PFJetsCorrectedp4")
            #self.jetGetter = JetGetter("PFAK4CHS")
            #self.jetGetter = JetGetter("PFAK5CHS")
            #self.jetGetter = JetGetter("PF")
            self.jetGetter.disableGenJet()
            self.jetGetter.disableJetId()
            #self.jetGetter = GenJetProxy()
        else:
            self.jetGetter = JetGetter("PFAK5")
            #self.jetGetter = JetGetter("PFlegacy")
            self.jetGetter.disableGenJet()

        #self.jetGetter = BetterJetGetter("PFAK5")
        #self.jetGetter = BetterJetGetter("Calo")
        '''
        if self.isData:
            self.jetGetter = JetGetter("PFAK5") 
        else:
            self.jetGetter = JetGetter("PFlegacy") 
        '''

        self.varE = {}
        sys.stdout.flush()
class BalanceTreeProducer(
        CommonFSQFramework.Core.ExampleProofReader.ExampleProofReader):
    def init(self):
        #self.pr = cProfile.Profile()

        print "Params:", self.etaMax, self.ptMin

        if not self.isData:
            #self.hltMCWeighter = HLTMCWeighter("HLT_Jet15U")
            self.HLTMCWeighterJ15Raw = HLTMCWeighter("HLT_Jet15U_raw")
            self.HLTMCWeighterJ15L1Raw = HLTMCWeighter(
                "HLT_Jet15U_L1Seeding_raw")
            #self.HLTMCWeighterDJ15FBRaw = HLTMCWeighter("HLT_DoubleJet15U_ForwardBackward_raw")
            #self.HLTMCWeighterDJ15L1FBRaw = HLTMCWeighter("HLT_DoubleJet15U_ForwardBackward_L1Seeding_raw")

        self.normFactor = self.getNormalizationFactor()
        self.dphi = ROOT.Math.VectorUtil.DeltaPhi

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

        self.var = {}
        self.histos = {}
        self.histos["evcnt"] = ROOT.TH1F("evcnt_central_jet15",
                                         "evcnt_central_jet15", 1, -0.5, 0.5)

        self.todoShifts = ["_central"]

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

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

        trg = "_jet15"
        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["veto1"+t] = array('d', [0])
            self.var["veto2" + t] = array('d', [0])

            histoPostFix = t + trg
            self.histos["ptProbe" + t] = ROOT.TH1F("ptProbe" + histoPostFix,
                                                   "ptProbe" + histoPostFix,
                                                   100, 0, 100)
            self.histos["ptTag" + t] = ROOT.TH1F("ptTag" + histoPostFix,
                                                 "ptTag" + histoPostFix, 100,
                                                 0, 100)
            self.histos["etaProbe" + t] = ROOT.TH1F("etaProbe" + histoPostFix,
                                                    "etaProbe" + histoPostFix,
                                                    35, 1.3, 4.8)
            self.histos["etaTag" + t] = ROOT.TH1F("etaTag" + histoPostFix,
                                                  "etaTag" + histoPostFix, 15,
                                                  0, 1.5)
            self.histos["nvtx" + t] = ROOT.TH1F("nvtx" + histoPostFix,
                                                "nvtx" + histoPostFix, 10,
                                                -0.5, 9.5)

        for t in self.histos:
            #self.histos[t][1] = ROOT.TH1F(name, name, nbins, self.histos[t][2], self.histos[t][3])
            self.histos[t].Sumw2()
            #self.GetOutputList().Add(self.histos[t])
            self.addToOutput(self.histos[t])

        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")

        if self.HLT2015TempWorkaround:
            #self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="recoPFAK4ChsCorrectedMyRhop4")
            #self.jetGetter = JetGetter("PFAK4CHS", jetColOverride="recoPFAK4ChsCorrectedp4")
            self.jetGetter = JetGetter(
                "PFAK4CHS", jetColOverride="hltAK4PFJetsCorrectedp4")
            #self.jetGetter = JetGetter("PFAK4CHS")
            #self.jetGetter = JetGetter("PFAK5CHS")
            #self.jetGetter = JetGetter("PF")
            self.jetGetter.disableGenJet()
            self.jetGetter.disableJetId()
            #self.jetGetter = GenJetProxy()
        else:
            self.jetGetter = JetGetter("PFAK5")
            #self.jetGetter = JetGetter("PFlegacy")
            self.jetGetter.disableGenJet()

        #self.jetGetter = BetterJetGetter("PFAK5")
        #self.jetGetter = BetterJetGetter("Calo")
        '''
        if self.isData:
            self.jetGetter = JetGetter("PFAK5") 
        else:
            self.jetGetter = JetGetter("PFlegacy") 
        '''

        self.varE = {}
        sys.stdout.flush()

    def addExternalVar(self, names):
        for name in names:
            self.varE[name] = 0.
            self.var[name] = array('d', [0])
            self.tree.Branch(name, self.var[name], name + "/D")

    def setExternals(self):
        for v in self.varE:
            self.var[v][0] = self.varE[v]

    def fill(self):
        self.tree.Fill()

    def resetExternals(self):
        for v in self.varE:
            self.varE[v] = 0

    def fillGenWeight(self):
        weight = self.genWeight()
        self.var["weight"][0] = weight

    def setExternalVar(self, name, val):
        self.varE[name] = val

    def genWeight(self):
        #print "ASDFASD", self.fChain.genWeight
        return self.fChain.genWeight * self.normFactor

    def weight(self):
        if self.weightCache != None:
            return self.weightCache
        if not self.isData:
            weight = self.genWeight()
            if not self.HLT2015TempWorkaround:
                truePU = self.fChain.puTrueNumInteractions
                puWeight = self.lumiWeighters["_jet15_central"].weight(truePU)
                weight *= puWeight
                w1 = self.HLTMCWeighterJ15L1Raw.getWeight()
                w2 = self.HLTMCWeighterJ15Raw.getWeight()
                triggerEff = w1 * w2
                weight *= triggerEff
        else:
            weight = 1

        self.weightCache = weight
        return weight

    '''
    # stuff for code profiling
    def analyze(self):
        self.pr.enable()
        self.analyzeTT()
        self.pr.disable()
    def analyzeTT(self):
        '''

    def analyze(self):
        self.weightCache = None
        self.histos["evcnt"].Fill(0)
        # '''
        if not self.HLT2015TempWorkaround:
            if self.fChain.ngoodVTX == 0: return
            if self.fChain.HBHENoiseFilterResult == 0: return

        if self.isData:
            if self.fChain.jet15 < 0.5:
                return 1

        for v in self.var:
            self.var[v][0] = 0

        # reset is done after fill
        self.setExternals()

        self.jetGetter.newEvent(self.fChain)
        if not self.isData:
            self.HLTMCWeighterJ15L1Raw.newEvent(self.fChain)
            self.HLTMCWeighterJ15Raw.newEvent(self.fChain)

        # xxxwei

        fill = False
        for shift in self.todoShifts:
            tagJet = None
            probeJet = None
            probePT = None
            tagPT = None

            for jet in self.jetGetter.get(shift):
                pt = jet.pt()

                if pt < self.ptMin: continue
                eta = abs(jet.eta())
                if eta > self.etaMax: continue

                if not self.HLT2015TempWorkaround:
                    if not jet.jetid(): continue
                if eta < 1.4:
                    tagJet = jet
                    tagPT = pt
                else:
                    probeJet = jet
                    probePT = pt

            if tagJet != None and probeJet != None:
                dphi = abs(self.dphi(tagJet.p4(), probeJet.p4()))
                if dphi < 2.7: continue

                # check veto:
                ptAve = (probePT + tagPT) / 2
                if ptAve < 25: continue

                veto2 = -1
                #for jet in self.jetGetter.get(shift):
                for jet in self.jetGetter.get(shift):
                    if jet == tagJet or probeJet == jet: continue
                    eta = abs(jet.eta())
                    if eta > self.etaMax: continue
                    vetoCand = jet.pt() / ptAve
                    if veto2 < vetoCand:
                        veto2 = vetoCand

                tagEta = abs(tagJet.eta())
                probeEta = abs(probeJet.eta())

                self.var["tagPt" + shift][0] = tagPT
                self.var["tagEta" + shift][0] = tagEta
                self.var["probePt" + shift][0] = probePT
                self.var["probeEta" + shift][0] = probeEta
                self.var["ptAve" + shift][0] = ptAve
                self.var["balance" + shift][0] = (probePT - tagPT) / ptAve
                #self.var["veto1"+shift][0] = veto1
                self.var["veto2" + shift][0] = veto2
                fill = True

                #print "Hist fill", weight
                weight = self.weight()
                self.histos["ptProbe" + shift].Fill(probePT, weight)
                self.histos["ptTag" + shift].Fill(tagPT, weight)
                self.histos["etaProbe" + shift].Fill(probeEta, weight)
                self.histos["etaTag" + shift].Fill(tagEta, weight)
                #self.histos["nvtx"+shift].Fill(self.fChain.ngoodVTX, weight)

        # at least one variation ok.
        if fill:
            self.var["weight"][0] = self.weight()
            self.fill()

        self.resetExternals()

        return 1

    def finalize(self):
        print "Finalize:"
        if hasattr(self, "pr"):
            dname = "/nfs/dust/cms/user/fruboest/2014.11.MN2010/CMSSW_4_2_8_lowpupatch1/src/CommonFSQFramework.Core/test/MNxsectionAna/"
            profName = dname + "stats"
            self.pr.dump_stats(profName)
class HLTEfficiencyTreeProducer(CommonFSQFramework.Core.ExampleProofReader.ExampleProofReader):
    def init(self):
        self.jetGetter = JetGetter("PFAK4CHS")
        self.dr = ROOT.Math.VectorUtil.DeltaR
        self.normFactor = self.getNormalizationFactor()

        self.var = {}
        self.var["recoEta"] = array('d', [0])
        self.var["recoPt"] = array('d', [0])
        #self.var["hltEta"] = array('d', [0])
        self.var["hltPt"] = array('d', [0])
        #self.var["l1Eta"] = array('d', [0])
        self.var["l1Pt"] = array('d', [0])
        #self.var["s1l1Eta"] = array('d', [0])
        self.var["s1l1Pt"] = array('d', [0])
        self.var["PUNumInteractions"] = array('d', [0])
        self.var["puTrueNumInteractions"] = array('d', [0])
        self.var["weight"] = array('d', [0])


        # for the PU file run
        # utils/GetFlatPUDist.py
        puFile = edm.FileInPath("CommonFSQFramework.Core/test/mnTrgAnalyzer/PUhists.root").fullPath()

        self.newlumiWeighters = {}
        #'''
        self.newlumiWeighters["flat010toflat010"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "Flat0to10/pileup")
        self.newlumiWeighters["flat010toPU1"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU1/pileup")
        #self.newlumiWeighters["flat010toPU2"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU2/pileup")
        #self.newlumiWeighters["flat010toPU3"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU3/pileup")
        #self.newlumiWeighters["flat010toPU4"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU4/pileup")
        self.newlumiWeighters["flat010toPU5"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU5/pileup")
        self.newlumiWeighters["flat010toPU10"] = edm.LumiReWeighting(puFile, puFile, "Flat0to10/pileup", "PU10/pileup")
        #'''
        '''
        self.newlumiWeighters["flat2050toPU20"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU20/pileup")
        self.newlumiWeighters["flat2050toPU25"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU25/pileup")
        self.newlumiWeighters["flat2050toPU30"] = edm.LumiReWeighting(puFile, puFile, "Flat20to50/pileup", "PU30/pileup")
        #'''

        '''
        self.newlumiWeighters["PU20to15"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU15/pileup")
        self.newlumiWeighters["PU20to18"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU18/pileup")
        self.newlumiWeighters["PU20to19"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU19/pileup")
        self.newlumiWeighters["PU20to21"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU21/pileup")
        self.newlumiWeighters["PU20to22"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU22/pileup")
        self.newlumiWeighters["PU20to20"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU20/pileup")
        self.newlumiWeighters["PU20to25"] = edm.LumiReWeighting(puFile, puFile, "PU20/pileup", "PU25/pileup")
        #'''
        for l in self.newlumiWeighters.keys():
            self.var[l] =  array('d', [0])

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

        for v in self.var:
            self.tree.Branch(v, self.var[v], v+"/D")


    def match(self, jetReco, jets):
        bestMatch = None
        for jet in jets:
            dr = self.dr(jetReco.p4(), jet)
            if dr < 0.3:
                if bestMatch == None or bestMatch.pt() < jet.pt():
                    bestMatch = jet

        return bestMatch

    def genWeight(self):
        return self.fChain.genWeight*self.normFactor

    def analyze(self):
        self.jetGetter.newEvent(self.fChain)
        self.var["PUNumInteractions"][0] = self.fChain.PUNumInteractions
        self.var["puTrueNumInteractions"][0] = self.fChain.puTrueNumInteractions

        pu = self.fChain.PUNumInteractions
        genW = self.genWeight()
        self.var["weight"][0]=genW
        for l in self.newlumiWeighters:
            w = self.newlumiWeighters[l].weight(pu)
            #print pu, l, w, genW
            self.var[l][0] = w*genW


        todo = {}
        todo["l1"] = self.fChain.l1Jets
        todo["s1l1"] = self.fChain.stage1L1Jets
        todo["hlt"] = self.fChain.hltAK4PFJetsCorrected
        for jet in self.jetGetter.get("_central"):
            if jet.pt() < 30: continue
            self.var["recoPt"][0] = jet.pt()
            self.var["recoEta"][0] = jet.eta()
            for t in todo.keys(): # reset subset of variables (rest is filled each time in loop)
                self.var[t+"Pt"][0] = 0

            for t in todo:
                ptMatch = 0
                match = self.match(jet, todo[t])
                if match != None:
                    self.var[t+"Pt"][0] = match.pt()
            self.tree.Fill()


        # self.fChain.hltAK4PFJetsCorrected




    def finalize(self):
        print "Finalize HLTBala:"