Esempio n. 1
0
    def beginLoop(self):
        super(JetIDAnalyzer, self).beginLoop()
        self.file = TFile('/'.join([self.looperName, 'testJets.root']),
                          'recreate')

        if self.cfg_ana.applyPFLooseId:
            from ROOT import PFJetIDSelectionFunctor
            self.isPFLooseFunc = PFJetIDSelectionFunctor(
                0, PFJetIDSelectionFunctor.LOOSE)
            ## Workaround: for some reason PyROOT does not bind PFJetIDSelectionFunctor(Jet) nor PFJetIDSelectionFunctor.getBitsTemplates
            from ROOT import pat
            self.isPFLooseFunc.bits = pat.strbitset()
            for i in "CHF", "NHF", "CEF", "NEF", "NCH", "nConstituents":
                self.isPFLooseFunc.bits.push_back(i)
            ## /Workaround
            self.isPFLoose = lambda x: self.isPFLooseFunc(
                x, self.isPFLooseFunc.bits)
        else:
            self.isPFLoose = lambda x: True

        # general histograms
        self.jetHistos = JetHistograms('Jets')
        self.cleanJetHistos = JetHistograms('CleanJets')
        self.matchedCleanJetHistos = JetHistograms('MatchedCleanJets')
        self.unmatchedCleanJetHistos = JetHistograms('UnmatchedCleanJets')

        ## Pileup JetID
        if self.cfg_ana.jetIdMva:
            self.puidalgo = PileupJetIdNtupleAlgo(*self.cfg_ana.jetIdMva)
            self.runMva = True
        else:
            self.puidalgo = PileupJetIdNtupleAlgo()
            self.runMva = False

        self.vtxBins = (0, 10, 15, 20, 30)  ## (0,2,4,6,10,15,20,30,35)
        self.vtxBinLabels = mkBinLabels(self.vtxBins)
        self.ptBins = (20, 30, 50)  ## (20,30,40,50,100)
        self.etaBins = (0, 1.4, 2.5, 3.0)
        self.puEtaLables = ["_barrel", "_endtk", "_endNOtk", "_fwd"]
        reweight_f = TF1("f", "pol2(0)+expo(3)")
        reweight_f.SetParameters(0.1955298, -0.003830591, 1.944794e-05,
                                 4.649755, -0.1722024)
        self.reweight = ("pt", reweight_f)

        ## PileUp histograms
        if self.cfg_ana.doJetIdHisto:
            # histograms for pileup jet identification variables

            self.gluCleanHistosId = PileupJetHistograms(
                "GluonMatchedCleanHistosId",
                self.vtxBins,
                self.ptBins,
                self.etaBins,
                etalabels=self.puEtaLables,
                reweight=self.reweight)
            self.quarkCleanHistosId = PileupJetHistograms(
                "QuarkMatchedCleanHistosId",
                self.vtxBins,
                self.ptBins,
                self.etaBins,
                etalabels=self.puEtaLables,
                reweight=self.reweight)
            self.reweiMatchedCleanHistosId = PileupJetHistograms(
                "ReweiMatchedCleanHistosId",
                self.vtxBins,
                self.ptBins,
                self.etaBins,
                etalabels=self.puEtaLables,
                reweight=self.reweight)
            self.unmatchedCleanHistosId = PileupJetHistograms(
                "UnmatchedCleanHistosId",
                self.vtxBins,
                self.ptBins,
                self.etaBins,
                etalabels=self.puEtaLables)

        ### self.jetKin = []
        ### for vtx in self.vtxBinLabels:
        ###     self.jetKin.append([])
        ###     for eta in self.puEtaLables:
        ###         self.jetKin[-1].append( JetHistograms('Jets%s%s' % (vtx,eta) ) )

        if self.cfg_ana.dumpTree:
            self.tree = TTree("tree", "tree")
            self.tree.SetDirectory(self.file)

            self.puidalgo.bookBranches(self.tree)

            gROOT.ProcessLine(
                """struct MyTreeVars{ Int_t nvtx; Bool_t isMatched; Int_t jetFlavour; Float_t partonPt; Float_t genPt; Float_t genDr; }"""
            )
            from ROOT import MyTreeVars
            self.treevars = MyTreeVars()

            for i, j in ("nvtx", "I"), ("isMatched",
                                        "O"), ("jetFlavour",
                                               "I"), ("partonPt",
                                                      "F"), ("genPt",
                                                             "F"), ("genDr",
                                                                    "F"):
                self.tree.Branch(i, AddressOf(self.treevars, i),
                                 "%s/%s" % (i, j))

        self.h_nvtx = TH1F("h_nvtx", "", 50, 0, 50)
        self.h_genjetspt = TH1F("h_genjetspt", "", 500, 0, 500)
Esempio n. 2
0
def main(options, args):

    from ROOT import TFile, TChain, gROOT, TF1, TH1, TGraph
    from CMGTools.RootTools.statistics.Histograms import Histograms
    from CMGTools.RootTools.utils.PileupJetHistograms import PileupJetHistograms, mkBinLabels, findBin
    from CMGTools.RootTools.analyzers.SimpleJetAnalyzer import ResolutionJetHistograms, FractionJetHistograms, JetHistograms

    ## customize the histograms filled by PileupJetHistograms
    if options.customPrototypes:
        PileupJetHistograms.prototypes = {
            "d0":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             "cm", 100, 0, 2),
            "dZ":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             "cm", 100, 0, 10),
            "beta":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             "", 200, 0, 1.),
            "betaStar":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             "", 200, 0, 1.),
            "nParticles":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             50, 0, 50),
            "dRMean":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             50, 0, 0.5),
            "dR2Mean":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             50, 0, 0.25),
            "etaW":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             50, 0, 0.5),
            "phiW":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             50, 0, 0.5),
            "majW":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             50, 0, 0.5),
            "minW":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             50, 0, 0.5),
            "jetW":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             50, 0, 0.5),
            "jetPt":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             "GeV/c", 300, 0, 150),
            "jetEta": (
                "%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
                101,
                -5.05,
                5.05,
            ),
            "nvtx":
            ("%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
             50, .5, 50.5),
        }
        if options.mvas != "":
            for mva in options.mvas.split(","):
                PileupJetHistograms.prototypes[mva] = (
                    "%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
                    400, -1., 1.)
        if options.customPrototypes:
            PileupJetHistograms.prototypes["mva"] = (
                "%(name)s %(hname)s%(jetbin)s;%(hname)s %(unitx)s;Jets %(unity)s",
                400, -1., 1.)

    ## cuts and binning
    genDrCut = 0.3
    genPtCut = 0.
    genDrAntiCut = 0.2

    vtxBins = (1, 10, 20)
    ptBins = (0, 10, 20, 25, 30, 40, 50)
    if options.inclusive:
        vtxBins = (0, 100)
        ptBins = (0, 20)
    vtxBinLabels = mkBinLabels(vtxBins)

    etaBins = (0, 2.5, 2.75, 3.0)
    puEtaLables = ["_TK", "_HEin", "_HEout", "_HF"]

    ### etaBins   = (0,2.5,3.0)
    ### puEtaLables = ["_central","_endNOtk","_fwd"]

    reweight_f = TF1("f", "pol2(0)+expo(3)")
    reweight_f.SetParameters(0.1955298, -0.003830591, 1.944794e-05, 4.649755,
                             -0.1722024)
    reweight = ("jetPt", reweight_f)

    ## book histograms
    fout = TFile.Open(options.outfile, "recreate")
    fout.cd()

    if options.tmva:
        sig = PileupJetHistograms("signal",
                                  vtxBins,
                                  ptBins,
                                  etaBins,
                                  etalabels=puEtaLables,
                                  reweight=reweight)
        bkg = PileupJetHistograms("background",
                                  vtxBins,
                                  ptBins,
                                  etaBins,
                                  etalabels=puEtaLables,
                                  reweight=reweight)
    else:
        matchedCleanJetHistos = JetHistograms('MatchedCleanJets')
        unmatchedCleanJetHistos = JetHistograms('UnmatchedCleanJets')

        gluCleanHistosId = PileupJetHistograms("GluonMatchedCleanHistosId",
                                               vtxBins,
                                               ptBins,
                                               etaBins,
                                               etalabels=puEtaLables,
                                               reweight=reweight)
        quarkCleanHistosId = PileupJetHistograms("QuarkMatchedCleanHistosId",
                                                 vtxBins,
                                                 ptBins,
                                                 etaBins,
                                                 etalabels=puEtaLables,
                                                 reweight=reweight)
        reweiMatchedCleanHistosId = PileupJetHistograms(
            "ReweiMatchedCleanHistosId",
            vtxBins,
            ptBins,
            etaBins,
            etalabels=puEtaLables,
            reweight=reweight)
        unmatchedCleanHistosId = PileupJetHistograms("UnmatchedCleanHistosId",
                                                     vtxBins,
                                                     ptBins,
                                                     etaBins,
                                                     etalabels=puEtaLables)

    ## create TChain and MakeClass
    if "," in options.indir:
        files = []
        for d in options.indir.split(","):
            files += getListOfFiles(d, options.filePattern)
    else:
        files = getListOfFiles(options.indir, options.filePattern)
    print files
    chain = mkChain(files, options.treename)
    if options.tmva:
        tr = mkReader(chain, True, "tmva_reader")
    else:
        tr = mkReader(chain, options.redoReader)

    ## Loop over ntuples
    nentries = chain.GetEntries()
    print "Reading %d entries" % nentries
    for i in xrange(nentries):
        if i % 1000 == 0:
            print >> sys.stderr, "Entry %d (%1.0f%%)\r" % (i, float(i) /
                                                           nentries * 100.),
        tr.GetEntry(i)

        ## TMVA training/test tree
        if options.tmva:
            if tr.classID == 0:
                ## tr.weight = tr.sigwei
                tr.weight = 1.
                sig.fillRootTuple(tr)
            else:
                tr.weight = 1.
                ## tr.weight = tr.bkgwei
                bkg.fillRootTuple(tr)

        ## Original ntuples
        else:
            if not tr.jetLooseID or tr.jetPt < 20.:
                continue

            if options.forceFlavour != 0:
                tr.jetFlavour = options.forceFlavour

            ## tr.weight = reweight[1](getattr(tr,reweight[0]))
            tr.weight = 1.

            if tr.isMatched and tr.jetGenDr < 0.3 and (
                    tr.jetGenPt > 10 or tr.jetGenPt / tr.jetPt > 0.7):
                reweiMatchedCleanHistosId.fillRootTuple(tr)
                matchedCleanJetHistos.fillRootTuple(tr)
                if abs(tr.jetFlavour) == 21:
                    gluCleanHistosId.fillRootTuple(tr)
                elif abs(tr.jetFlavour) <= 3 and abs(tr.jetFlavour) != 0:
                    quarkCleanHistosId.fillRootTuple(tr)

            elif not tr.isMatched or (tr.jetGenDr > 0.3
                                      and tr.jetGenPt / tr.jetPt < 0.7
                                      and tr.jetGenPt < 10):
                unmatchedCleanJetHistos.fillRootTuple(tr)
                unmatchedCleanHistosId.fillRootTuple(tr)

    ## Write out histograms
    for name, obj in locals().iteritems():
        if isinstance(obj, Histograms):
            obj.Write(fout)
        elif isinstance(obj, TH1) or isinstance(obj, TGraph):
            fout.cd()
            obj.Write()

    fout.Close()
Esempio n. 3
0
    def beginLoop (self) :
        super (SimpleJetAnalyzer,self).beginLoop ()
        self.file = TFile ('/'.join ([self.looperName, 'testJets.root']),
                           'recreate')
        if self.cfg_ana.applyPFLooseId:
            from ROOT import PFJetIDSelectionFunctor 
            self.isPFLooseFunc = PFJetIDSelectionFunctor(0,PFJetIDSelectionFunctor.LOOSE)
            ## Workaround: for some reason PyROOT does not bind nor PFJetIDSelectionFunctor(Jet)PFJetIDSelectionFunctor.getBitsTemplates 
            from ROOT import pat        
            self.isPFLooseFunc.bits = pat.strbitset()
            for i in "CHF","NHF","CEF","NEF","NCH","nConstituents": self.isPFLooseFunc.bits.push_back(i) 
            ## /Workaround
            self.isPFLoose = lambda x : self.isPFLooseFunc(x,self.isPFLooseFunc.bits)
        else:
            self.isPFLoose = lambda x : True

        # general histograms
        self.jetHistos = JetHistograms ('Jets')
        self.cleanJetHistos = JetHistograms ('CleanJets')
        self.matchedCleanJetHistos = JetHistograms ('MatchedCleanJets')
        self.matchedCleanJetHistos_barrel = JetHistograms ('MatchedCleanJets_barrel')
        self.matchedCleanJetHistos_endtk = JetHistograms ('MatchedCleanJets_endtk')
        self.matchedCleanJetHistos_endNOtk = JetHistograms ('MatchedCleanJets_endNOtk')
        self.matchedCleanJetHistos_fwd = JetHistograms ('MatchedCleanJets_fwd')
        self.LPtmatchedCleanJetHistos = JetHistograms ('LPtMatchedCleanJets') 
        self.HPtmatchedCleanJetHistos = JetHistograms ('HPtMatchedCleanJets')
        self.unmatchedCleanJetHistos = JetHistograms ('UnmatchedCleanJets')
        self.LPtUnmatchedCleanJetHistos = JetHistograms ('LPtUnmatchedCleanJets') 
        self.HPtUnmatchedCleanJetHistos = JetHistograms ('HPtUnmatchedCleanJets')

        # histograms of the components fraction
        self.matchedCleanJetHistosComponents = FractionJetHistograms ('MatchedCleanJetsCompontents')
        self.unmatchedCleanJetHistosComponents = FractionJetHistograms ('UnmatchedCleanJetsCompontents')

        # histograms for the resolution of matched jets
        self.matchedCleanJetHistosResolution = ResolutionJetHistograms ('MatchedCleanJetsResolution', 50, 1)
        self.matchedCleanJetHistosResolution_barrel = ResolutionJetHistograms ('MatchedCleanJetsResolution_barrel', 50, 1)
        self.matchedCleanJetHistosResolution_endtk = ResolutionJetHistograms ('MatchedCleanJetsResolution_endtk', 50, 1)
        self.matchedCleanJetHistosResolution_endNOtk = ResolutionJetHistograms ('MatchedCleanJetsResolution_endNOtk', 50, 1)
        self.matchedCleanJetHistosResolution_fwd = ResolutionJetHistograms ('MatchedCleanJetsResolution_fwd', 50, 1)

        print 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
        self.doJetIdHisto = False
        if self.cfg_ana.doJetIdHisto:
            print 'doing jet ID'
            # histograms for pileup jet identification variables
            self.vtxBins   = (0,5,10,15,20,30) ## (0,2,4,6,10,15,20,30,35)
            self.ptBins    = (20,30,50) ## (20,30,40,50,100)
            self.etaBins   = (0,1.4,2.5,3.0)
            self.puEtaLables = ["_barrel","_endtk","_endNOtk","_fwd"]
            reweight_f = TF1("f","pol2(0)+expo(3)")
            reweight_f.SetParameters(0.1955298,-0.003830591,1.944794e-05,4.649755,-0.1722024)
            self.reweight = ("pt", reweight_f)
            self.doJetIdHisto = True
            self.gluCleanHistosId = PileupJetHistograms("GluonMatchedCleanHistosId",self.vtxBins,self.ptBins,self.etaBins,etalabels=self.puEtaLables,reweight=self.reweight,
                                                        jetIdMva=self.cfg_ana.jetIdMva)
            self.quarkCleanHistosId = PileupJetHistograms("QuarkMatchedCleanHistosId",self.vtxBins,self.ptBins,self.etaBins,etalabels=self.puEtaLables,reweight=self.reweight,
                                                          jetIdMva=self.cfg_ana.jetIdMva)
            self.reweiMatchedCleanHistosId = PileupJetHistograms("ReweiMatchedCleanHistosId",self.vtxBins,self.ptBins,self.etaBins,etalabels=self.puEtaLables,reweight=self.reweight,
                                                                 jetIdMva=self.cfg_ana.jetIdMva)
            self.unmatchedCleanHistosId = PileupJetHistograms("UnmatchedCleanHistosId",self.vtxBins,self.ptBins,self.etaBins,etalabels=self.puEtaLables,
                                                              jetIdMva=self.cfg_ana.jetIdMva)
            
        self.h_nvtx = TH1F ("h_nvtx", "" ,50, 0, 50)
        self.h_genjetspt = TH1F ("h_genjetspt", "" ,500, 0, 500)
        self.h_secondClosestVsPtratio = TH2F ("h_secondClosestVsPtratio", "" ,100, 0, 2, 100, 0, 6)
        self.h_avedistanceVSNvtx = TH2F ("h_avedistanceVSNvtx", "" ,50, 0, 50, 100, 0, 6)
        self.h_PTRatioVSgenEta = TH2F ("h_PTRatioVSgenEta", "" ,150, -5, 5, 100, 0, 2)
        self.h_PTRatioVSgenPt = TH2F ("h_PTRatioVSgenPt", "" ,200, 0, 100, 100, 0, 2)
        self.h_matchDR = TH1F ("h_matchDR", "" ,60, 0, 0.30)
        self.h_relPtVSmatchDR = TH2F ("h_relPtVSmatchDR", "" ,60, 0, 0.30, 100, 0, 2)
        self.h_relPtVSchFrac = TH2F ("h_relPtVSchFrac", "" ,100, 0, 1, 100, 0, 2)