Example #1
0
    def calculate_invariant_m():
        lepTopCandP4 = None
        # Get the z-component of the lepton from the W mass constraint
        solution, nuz1, nuz2 = solve_nu(vlep=theLepton, vnu=nuCandP4)
        # If there is at least one real solution, pick it up
        if solution:
            nuCandP4.SetPz(nuz1)
        else:
            nuCandP4.SetPz(nuz1.real)

        lepTopCandP4 = nuCandP4 + theLepton + bJetCandP4

        ttbarCand = hadTopCandP4 + lepTopCandP4
        mttbar = ttbarCand.M()
        return mttbar
Example #2
0
def plot_mttbar(argv):
    parser = OptionParser()

    parser.add_option('--file_in',
                      type='string',
                      action='store',
                      dest='file_in',
                      help='Input file')

    parser.add_option('--file_out',
                      type='string',
                      action='store',
                      dest='file_out',
                      help='Output file')

    #parser.add_option('--isData', action='store_true',
    #                  dest='isData',
    #                  default = False,
    #                  help='Is this Data?')

    (options, args) = parser.parse_args(argv)
    argv = []

    print '===== Command line options ====='
    print options
    print '================================'

    import ROOT

    from leptonic_nu_z_component import solve_nu_tmass, solve_nu

    fout = ROOT.TFile(options.file_out, "RECREATE")

    h_muonpt = ROOT.TH1F("h_muonpt", ";leading p_{T}^{#mu} (GeV);Events", 50,
                         0, 1000)
    h_muonphi = ROOT.TH1F("h_muonphi", ";leading #phi_{#mu};Events", 20, 0,
                          3.15)
    h_muoneta = ROOT.TH1F("h_muoneta", ";leading #eta_{#mu};Events", 20, -2.5,
                          2.5)
    h_mttbar = ROOT.TH1F("h_mttbar", ";m_{t#bar{t}} (GeV);Events", 100, 0,
                         5000)
    h_mtopHad = ROOT.TH1F("h_mtopHad", ";m_{jet} (GeV);Events", 100, 0, 400)
    h_mtopHadGroomed = ROOT.TH1F("h_mtopHadGroomed",
                                 ";Groomed m_{jet} (GeV);Events", 100, 0, 400)

    fin = ROOT.TFile.Open(options.file_in)

    trees = [fin.Get("TreeSemiLept")]

    for itree, t in enumerate(trees):

        #if options.isData :
        SemiLeptTrig = ROOT.vector('int')()
        SemiLeptWeight = array.array('f', [0.])
        PUWeight = array.array('f', [0.])
        GenWeight = array.array('f', [0.])
        FatJetPt = array.array('f', [-1.])
        FatJetEta = array.array('f', [-1.])
        FatJetPhi = array.array('f', [-1.])
        FatJetRap = array.array('f', [-1.])
        FatJetEnergy = array.array('f', [-1.])
        FatJetBDisc = array.array('f', [-1.])
        FatJetMass = array.array('f', [-1.])
        FatJetMassSoftDrop = array.array('f', [-1.])
        FatJetTau32 = array.array('f', [-1.])
        FatJetTau21 = array.array('f', [-1.])
        FatJetSDBDiscW = array.array('f', [-1.])
        FatJetSDBDiscB = array.array('f', [-1.])
        FatJetSDsubjetWpt = array.array('f', [-1.])
        FatJetSDsubjetWmass = array.array('f', [-1.])
        FatJetSDsubjetBpt = array.array('f', [-1.])
        FatJetSDsubjetBmass = array.array('f', [-1.])
        FatJetJECUpSys = array.array('f', [-1.])
        FatJetJECDnSys = array.array('f', [-1.])
        FatJetJERUpSys = array.array('f', [-1.])
        FatJetJERDnSys = array.array('f', [-1.])
        LeptonType = array.array('i', [-1])
        LeptonPt = array.array('f', [-1.])
        LeptonEta = array.array('f', [-1.])
        LeptonPhi = array.array('f', [-1.])
        LeptonEnergy = array.array('f', [-1.])
        LeptonIso = array.array('f', [-1.])
        LeptonPtRel = array.array('f', [-1.])
        LeptonDRMin = array.array('f', [-1.])
        SemiLepMETpt = array.array('f', [-1.])
        SemiLepMETphi = array.array('f', [-1.])
        SemiLepNvtx = array.array('f', [-1.])
        FatJetDeltaPhiLep = array.array('f', [-1.])
        NearestAK4JetBDisc = array.array('f', [-1.])
        NearestAK4JetPt = array.array('f', [-1.])
        NearestAK4JetEta = array.array('f', [-1.])
        NearestAK4JetPhi = array.array('f', [-1.])
        NearestAK4JetMass = array.array('f', [-1.])
        NearestAK4JetJECUpSys = array.array('f', [-1.])
        NearestAK4JetJECDnSys = array.array('f', [-1.])
        NearestAK4JetJERUpSys = array.array('f', [-1.])
        NearestAK4JetJERDnSys = array.array('f', [-1.])
        SemiLeptRunNum = array.array('f', [-1.])
        SemiLeptLumiNum = array.array('f', [-1.])
        SemiLeptEventNum = array.array('f', [-1.])

        #if options.isData :
        t.SetBranchAddress('SemiLeptTrig', SemiLeptTrig)
        t.SetBranchAddress(
            'SemiLeptWeight', SemiLeptWeight
        )  #Combined weight of all scale factors (lepton, PU, generator) relevant for the smeileptonic event selection
        t.SetBranchAddress('PUWeight', PUWeight)
        t.SetBranchAddress('GenWeight', GenWeight)
        t.SetBranchAddress('FatJetPt', FatJetPt)
        t.SetBranchAddress('FatJetEta', FatJetEta)
        t.SetBranchAddress('FatJetPhi', FatJetPhi)
        t.SetBranchAddress('FatJetRap', FatJetRap)
        t.SetBranchAddress('FatJetEnergy', FatJetEnergy)
        t.SetBranchAddress('FatJetBDisc', FatJetBDisc)
        t.SetBranchAddress('FatJetMass', FatJetMass)
        t.SetBranchAddress('FatJetMassSoftDrop', FatJetMassSoftDrop)
        t.SetBranchAddress('FatJetTau32', FatJetTau32)
        t.SetBranchAddress('FatJetTau21', FatJetTau21)
        t.SetBranchAddress('FatJetSDBDiscW', FatJetSDBDiscW)
        t.SetBranchAddress('FatJetSDBDiscB', FatJetSDBDiscB)
        t.SetBranchAddress('FatJetSDsubjetWpt', FatJetSDsubjetWpt)
        t.SetBranchAddress('FatJetSDsubjetWmass', FatJetSDsubjetWmass)
        t.SetBranchAddress('FatJetSDsubjetBpt', FatJetSDsubjetBpt)
        t.SetBranchAddress('FatJetSDsubjetBmass', FatJetSDsubjetBmass)
        t.SetBranchAddress('FatJetJECUpSys', FatJetJECUpSys)
        t.SetBranchAddress('FatJetJECDnSys', FatJetJECDnSys)
        t.SetBranchAddress('FatJetJERUpSys', FatJetJERUpSys)
        t.SetBranchAddress('FatJetJERDnSys', FatJetJERDnSys)
        t.SetBranchAddress('LeptonType', LeptonType)
        t.SetBranchAddress('LeptonPt', LeptonPt)
        t.SetBranchAddress('LeptonEta', LeptonEta)
        t.SetBranchAddress('LeptonPhi', LeptonPhi)
        t.SetBranchAddress('LeptonEnergy', LeptonEnergy)
        t.SetBranchAddress('LeptonIso', LeptonIso)
        t.SetBranchAddress('LeptonPtRel', LeptonPtRel)
        t.SetBranchAddress('LeptonDRMin', LeptonDRMin)
        t.SetBranchAddress('SemiLepMETpt', SemiLepMETpt)
        t.SetBranchAddress('SemiLepMETphi', SemiLepMETphi)
        t.SetBranchAddress('SemiLepNvtx', SemiLepNvtx)
        t.SetBranchAddress('FatJetDeltaPhiLep', FatJetDeltaPhiLep)
        t.SetBranchAddress('NearestAK4JetBDisc', NearestAK4JetBDisc)
        t.SetBranchAddress('NearestAK4JetPt', NearestAK4JetPt)
        t.SetBranchAddress('NearestAK4JetEta', NearestAK4JetEta)
        t.SetBranchAddress('NearestAK4JetPhi', NearestAK4JetPhi)
        t.SetBranchAddress('NearestAK4JetMass', NearestAK4JetMass)
        t.SetBranchAddress('NearestAK4JetJECUpSys', NearestAK4JetJECUpSys)
        t.SetBranchAddress('NearestAK4JetJECDnSys', NearestAK4JetJECDnSys)
        t.SetBranchAddress('NearestAK4JetJERUpSys', NearestAK4JetJERUpSys)
        t.SetBranchAddress('NearestAK4JetJERDnSys', NearestAK4JetJERDnSys)
        t.SetBranchAddress('SemiLeptRunNum', SemiLeptRunNum)
        t.SetBranchAddress('SemiLeptLumiNum', SemiLeptLumiNum)
        t.SetBranchAddress('SemiLeptEventNum', SemiLeptEventNum)

        t.SetBranchStatus('*', 0)
        t.SetBranchStatus('SemiLeptWeight', 1)
        t.SetBranchStatus('PUWeight', 1)
        t.SetBranchStatus('GenWeight', 1)
        t.SetBranchStatus('FatJetPt', 1)
        t.SetBranchStatus('FatJetEta', 1)
        t.SetBranchStatus('FatJetPhi', 1)
        t.SetBranchStatus('FatJetMass', 1)
        t.SetBranchStatus('FatJetMassSoftDrop', 1)
        t.SetBranchStatus('FatJetTau32', 1)
        t.SetBranchStatus('SemiLeptTrig', 1)
        t.SetBranchStatus('NearestAK4JetBDisc', 1)
        t.SetBranchStatus('NearestAK4JetPt', 1)
        t.SetBranchStatus('NearestAK4JetEta', 1)
        t.SetBranchStatus('NearestAK4JetPhi', 1)
        t.SetBranchStatus('NearestAK4JetMass', 1)
        t.SetBranchStatus('SemiLepMETpt', 1)
        t.SetBranchStatus('SemiLepMETphi', 1)
        t.SetBranchStatus('LeptonType', 1)
        t.SetBranchStatus('LeptonPt', 1)
        t.SetBranchStatus('LeptonEta', 1)
        t.SetBranchStatus('LeptonPhi', 1)
        t.SetBranchStatus('LeptonEnergy', 1)
        t.SetBranchStatus('LeptonIso', 1)
        t.SetBranchStatus('LeptonPtRel', 1)
        t.SetBranchStatus('LeptonDRMin', 1)

        entries = t.GetEntriesFast()
        print 'Processing tree ' + str(itree)

        eventsToRun = entries
        for jentry in xrange(eventsToRun):
            if jentry % 100000 == 0:
                print 'processing ' + str(jentry)
            # get the next tree in the chain and verify
            ientry = t.GetEntry(jentry)
            if ientry < 0:
                break

            # Muons only
            if LeptonType[0] != 13:
                continue

            # Muon triggers
            if SemiLeptTrig[0] != 1:
                continue

            # Fill basic muon quantities
            h_muonpt.Fill(LeptonPt[0], SemiLeptWeight[0])
            h_muoneta.Fill(LeptonEta[0], SemiLeptWeight[0])
            h_muonphi.Fill(LeptonPhi[0], SemiLeptWeight[0])

            hadTopCandP4 = ROOT.TLorentzVector()
            hadTopCandP4.SetPtEtaPhiM(FatJetPt[0], FatJetEta[0], FatJetPhi[0],
                                      FatJetMass[0])
            bJetCandP4 = ROOT.TLorentzVector()
            bJetCandP4.SetPtEtaPhiM(NearestAK4JetPt[0], NearestAK4JetEta[0],
                                    NearestAK4JetPhi[0], NearestAK4JetMass[0])
            nuCandP4 = ROOT.TLorentzVector()
            nuCandP4.SetPtEtaPhiM(SemiLepMETpt[0], 0, SemiLepMETphi[0],
                                  SemiLepMETpt[0])
            theLepton = ROOT.TLorentzVector()
            theLepton.SetPtEtaPhiE(LeptonPt[0], LeptonEta[0], LeptonPhi[0],
                                   LeptonEnergy[0])  # Assume massless

            tau32 = FatJetTau32[0]
            mass_sd = FatJetMassSoftDrop[0]
            bdisc = NearestAK4JetBDisc[0]

            passKin = hadTopCandP4.Perp() > 400.
            passTopTag = tau32 < 0.6 and mass_sd > 110. and mass_sd < 250.
            pass2DCut = LeptonPtRel[0] > 55. or LeptonDRMin[0] > 0.4
            passBtag = bdisc > 0.7

            if not passKin or not pass2DCut or not passBtag or not passTopTag:
                continue

            ##  ____  __.__                              __  .__         __________
            ## |    |/ _|__| ____   ____   _____ _____ _/  |_|__| ____   \______   \ ____   ____  ____
            ## |      < |  |/    \_/ __ \ /     \\__  \\   __\  |/ ___\   |       _// __ \_/ ___\/  _ \
            ## |    |  \|  |   |  \  ___/|  Y Y  \/ __ \|  | |  \  \___   |    |   \  ___/\  \__(  <_> )
            ## |____|__ \__|___|  /\___  >__|_|  (____  /__| |__|\___  >  |____|_  /\___  >\___  >____/
            ##         \/       \/     \/      \/     \/             \/          \/     \/     \/

            # Now we do our kinematic calculation based on the categories of the
            # number of top and bottom tags
            mttbar = -1.0

            lepTopCandP4 = None
            # Get the z-component of the lepton from the W mass constraint
            solution, nuz1, nuz2 = solve_nu(vlep=theLepton, vnu=nuCandP4)
            # If there is at least one real solution, pick it up
            if solution:
                nuCandP4.SetPz(nuz1)
            else:
                nuCandP4.SetPz(nuz1.real)

            lepTopCandP4 = nuCandP4 + theLepton + bJetCandP4

            ttbarCand = hadTopCandP4 + lepTopCandP4
            mttbar = ttbarCand.M()

            h_mttbar.Fill(mttbar, SemiLeptWeight[0])
            h_mtopHadGroomed.Fill(mass_sd, SemiLeptWeight[0])
            h_mtopHad.Fill(hadTopCandP4.M(), SemiLeptWeight[0])

    fout.cd()
    fout.Write()
    fout.Close()
Example #3
0
            lepTopCandP4 = None
            # Check if the nearest jet to the lepton is b-tagged
            if theLepJetBDisc < options.bDiscMin:
                if options.verbose:
                    print 'closest jet to lepton is not b-tagged'
            else:

                if options.verbose:
                    print 'Event is fully tagged.'
                # Get the z-component of the lepton from the W mass constraint

                bJetCandP4 = theLepJet

                nuCandP4 = ROOT.TLorentzVector(metPx, metPy, 0.0, metPt)

                solution, nuz1, nuz2 = solve_nu(vlep=theLepton, vnu=nuCandP4)
                # If there is at least one real solution, pick it up
                if solution:
                    if options.verbose:
                        print '--- Have a solution --- '
                    nuCandP4.SetPz(nuz1)
                else:
                    if options.verbose:
                        print '--- No solution for neutrino z ---'
                    nuCandP4.SetPz(nuz1.real)

                lepTopCandP4 = nuCandP4 + theLepton + bJetCandP4

                ttbarCand = hadTopCandP4 + lepTopCandP4
                h_mttbar.Fill(ttbarCand.M())
Example #4
0
            lepTopCandP4 = None
            # Check if the nearest jet to the lepton is b-tagged
            if theLepJetBDisc < options.bDiscMin :
                if options.verbose : 
                    print 'closest jet to lepton is not b-tagged'
            else  :

                if options.verbose :
                    print 'Event is fully tagged.'
                # Get the z-component of the lepton from the W mass constraint
                
                bJetCandP4 = theLepJet
                
                nuCandP4 = ROOT.TLorentzVector(metPx, metPy ,0.0, metPt)

                solution, nuz1, nuz2 = solve_nu( vlep=theLepton, vnu=nuCandP4 )
                # If there is at least one real solution, pick it up
                if solution :
                    if options.verbose : 
                        print '--- Have a solution --- '
                    nuCandP4.SetPz(nuz1)
                else :
                    if options.verbose : 
                        print '--- No solution for neutrino z ---'
                    nuCandP4.SetPz(nuz1.real)

                lepTopCandP4 = nuCandP4 + theLepton + bJetCandP4

                ttbarCand = hadTopCandP4 + lepTopCandP4
                h_mttbar.Fill( ttbarCand.M() )
        
Example #5
0
def plot_mttbar(argv):
    parser = OptionParser()

    parser.add_option('--file_in',
                      type='string',
                      action='store',
                      dest='file_in',
                      help='Input file')

    parser.add_option('--file_out',
                      type='string',
                      action='store',
                      dest='file_out',
                      help='Output file')

    parser.add_option(
        '--jer',
        type='string',
        action='store',
        dest='jer',
        default=None,
        help=
        'Choice of up and down for Jet Energy Resolution (only set jer or jec')

    parser.add_option(
        '--jec',
        type='string',
        action='store',
        dest='jec',
        default=None,
        help=
        'Choice of up and down for Jet Energy Correction (only set jer or jec')

    parser.add_option('--lepton',
                      type='string',
                      action='store',
                      dest='lepton',
                      default='mu',
                      help='Choice of lepton (mu or ele)')

    parser.add_option('--isData',
                      action='store_true',
                      dest='isData',
                      default=False,
                      help='Is this Data?')

    (options, args) = parser.parse_args(argv)
    argv = []

    #write to temp file
    fh = open("num.txt", "a")

    #print '===== Command line options ====='
    #print options
    #print '================================'

    import ROOT

    from leptonic_nu_z_component import solve_nu_tmass, solve_nu  #load Z_momentum with these functions

    histogramSuffix = ''
    #Adding name change to histograms based on --jec options and --jer options
    if options.lepton == 'ele':
        histogramSuffix = '_el'
    if options.lepton == 'mu':
        histogramSuffix = '_mu'

    if options.jec is not None and options.jer is not None:
        print 'You are trying to do two systematics at once! Please fix your input options'

    if options.jec == 'up':
        histogramSuffix += '_jec_Up'

    if options.jec == 'down':
        histogramSuffix += '_jec_Down'

    if options.jer == 'up':
        histogramSuffix += '_jer_Up'

    if options.jer == 'down':
        histogramSuffix += '_jer_Down'

    fout = ROOT.TFile(options.file_out, "RECREATE")

    #fpileup = ROOT.TFile.Open('purw.root', 'read')
    #h_pileupWeight = fpileup.Get('pileup')

    h_cuts = ROOT.TH1F("Cut_flow", "", 4, 0, 4)

    h_mttbar = ROOT.TH1F("h_mttbar" + histogramSuffix,
                         ";m_{t#bar{t}} (GeV);Number", 100, 0,
                         5000)  #invariant ttbar mass
    h_mtopHad = ROOT.TH1F("h_mtopHad" + histogramSuffix,
                          ";m_{jet} (GeV);Number", 100, 0, 400)
    h_mtopHadGroomed = ROOT.TH1F("h_mtopHadGroomed" + histogramSuffix,
                                 ";Groomed m_{jet} (GeV);Number", 100, 0, 400)

    #ele plots
    h_lepPt = ROOT.TH1F("h_lepPt" + histogramSuffix, "; lep_{pt}(GeV);Number",
                        100, 0, 1200)
    h_lepEta = ROOT.TH1F("h_lepEta" + histogramSuffix, ";lep_{#eta};Number",
                         100, -2.5, 2.5)
    h_lepPhi = ROOT.TH1F("h_lepPhi" + histogramSuffix, ";lep_{#phi};Number",
                         100, -3.5, 3.5)

    #AK8

    h_AK8Pt = ROOT.TH1F("h_AK8Pt" + histogramSuffix, ";AK8_{pt} (GeV);Number",
                        100, 400, 2500)
    h_AK8Eta = ROOT.TH1F("h_AK8Eta" + histogramSuffix, ";AK8_{#eta} ;Number",
                         100, -2.5, 2.5)
    h_AK8Phi = ROOT.TH1F("h_AK8Phi" + histogramSuffix, ";AK8_{#phi} ;Number",
                         100, -3.5, 3.5)
    h_AK8Tau32 = ROOT.TH1F("h_AK8Tau32" + histogramSuffix,
                           ";AK8_{#tau_{32}};Number", 50, 0.0, 1.0)
    h_AK8Tau21 = ROOT.TH1F("h_AK8Tau21" + histogramSuffix,
                           ";AK8_{#tau_{21}};Number", 50, 0.0, 1.0)

    #AK8
    h_AK4Pt = ROOT.TH1F("h_AK4Pt" + histogramSuffix,
                        ";ak4jet_{pT} (GeV);Number", 100, 0, 1500)
    h_AK4Eta = ROOT.TH1F("h_AK4Eta" + histogramSuffix, ";ak4jet_{#eta};Number",
                         100, -2.5, 2.5)
    h_AK4Phi = ROOT.TH1F("h_AK4Phi" + histogramSuffix, ";ak4jet_{#phi};Number",
                         100, -3.5, 3.5)
    h_AK4M = ROOT.TH1F("h_AK4M" + histogramSuffix, ";ak4jet_{mass};Number",
                       100, 0, 400)
    h_AK4Bdisc = ROOT.TH1F("h_AK4Bdisc" + histogramSuffix,
                           ";ak4jet_{bdisc};Number", 100, 0, 1.0)

    h_drAK4AK8 = ROOT.TH1F("h_drAK4AK8" + histogramSuffix,
                           ";#DeltaR_{AK4, AK8} ;Number", 100, 0, 5)
    #    h_drLepAK8    = ROOT.TH1F("h_drLepAK8",";{#delta r}_{lep, AK8} ;Number", 100, 0, 1500)
    h_drLepAK4 = ROOT.TH1F("h_drLepAK4" + histogramSuffix,
                           ";#DeltaR_{lep, AK4} ;Number", 100, 0, 5)
    h_dPhiLepAK8 = ROOT.TH1F("h_dPhiLepAK8" + histogramSuffix,
                             ";#Delta#phi_{l,AK8};Number", 100, 0.0, 1.0)

    fin = ROOT.TFile.Open(options.file_in)

    trees = [fin.Get("TreeSemiLept")]

    tot_entries, count = 0, 0
    cut1, cut2, cut3, cut4 = 0, 0, 0, 0

    for itree, t in enumerate(trees):

        #if options.isData :
        SemiLeptTrig = ROOT.vector('int')()
        SemiLeptWeight = array.array('f', [0.])
        PUWeight = array.array('f', [0.])
        GenWeight = array.array('f', [0.])
        FatJetPt = array.array('f', [-1.])
        FatJetEta = array.array('f', [-1.])
        FatJetPhi = array.array('f', [-1.])
        FatJetRap = array.array('f', [-1.])
        FatJetEnergy = array.array('f', [-1.])
        FatJetBDisc = array.array('f', [-1.])
        FatJetMass = array.array('f', [-1.])
        FatJetMassSoftDrop = array.array('f', [-1.])
        FatJetTau32 = array.array('f', [-1.])
        FatJetTau21 = array.array('f', [-1.])
        FatJetSDBDiscW = array.array('f', [-1.])
        FatJetSDBDiscB = array.array('f', [-1.])
        FatJetSDsubjetWpt = array.array('f', [-1.])
        FatJetSDsubjetWmass = array.array('f', [-1.])
        FatJetSDsubjetBpt = array.array('f', [-1.])
        FatJetSDsubjetBmass = array.array('f', [-1.])
        FatJetJECUpSys = array.array('f', [-1.])
        FatJetJECDnSys = array.array('f', [-1.])
        FatJetJERUpSys = array.array('f', [-1.])
        FatJetJERDnSys = array.array('f', [-1.])
        LeptonType = array.array('i', [-1])
        LeptonPt = array.array('f', [-1.])
        LeptonEta = array.array('f', [-1.])
        LeptonPhi = array.array('f', [-1.])
        LeptonEnergy = array.array('f', [-1.])
        LeptonIso = array.array('f', [-1.])
        LeptonPtRel = array.array('f', [-1.])
        LeptonDRMin = array.array('f', [-1.])
        SemiLepMETpt = array.array('f', [-1.])
        SemiLepMETphi = array.array('f', [-1.])
        SemiLepNvtx = array.array('f', [-1.])
        FatJetDeltaPhiLep = array.array('f', [-1.])
        NearestAK4JetBDisc = array.array('f', [-1.])
        NearestAK4JetPt = array.array('f', [-1.])
        NearestAK4JetEta = array.array('f', [-1.])
        NearestAK4JetPhi = array.array('f', [-1.])
        NearestAK4JetMass = array.array('f', [-1.])
        NearestAK4JetJECUpSys = array.array('f', [-1.])
        NearestAK4JetJECDnSys = array.array('f', [-1.])
        NearestAK4JetJERUpSys = array.array('f', [-1.])
        NearestAK4JetJERDnSys = array.array('f', [-1.])
        SemiLeptRunNum = array.array('f', [-1.])
        SemiLeptLumiNum = array.array('f', [-1.])
        SemiLeptEventNum = array.array('f', [-1.])

        #if options.isData :
        t.SetBranchAddress('SemiLeptTrig', SemiLeptTrig)
        t.SetBranchAddress(
            'SemiLeptWeight', SemiLeptWeight
        )  #Combined weight of all scale factors (lepton, PU, generator) relevant for the smeileptonic event selection
        t.SetBranchAddress('PUWeight', PUWeight)
        t.SetBranchAddress('GenWeight', GenWeight)
        t.SetBranchAddress('FatJetPt', FatJetPt)
        t.SetBranchAddress('FatJetEta', FatJetEta)
        t.SetBranchAddress('FatJetPhi', FatJetPhi)
        t.SetBranchAddress('FatJetRap', FatJetRap)
        t.SetBranchAddress('FatJetEnergy', FatJetEnergy)
        t.SetBranchAddress('FatJetBDisc', FatJetBDisc)
        t.SetBranchAddress('FatJetMass', FatJetMass)
        t.SetBranchAddress('FatJetMassSoftDrop', FatJetMassSoftDrop)
        t.SetBranchAddress('FatJetTau32', FatJetTau32)
        t.SetBranchAddress('FatJetTau21', FatJetTau21)
        t.SetBranchAddress('FatJetSDBDiscW', FatJetSDBDiscW)
        t.SetBranchAddress('FatJetSDBDiscB', FatJetSDBDiscB)
        t.SetBranchAddress('FatJetSDsubjetWpt', FatJetSDsubjetWpt)
        t.SetBranchAddress('FatJetSDsubjetWmass', FatJetSDsubjetWmass)
        t.SetBranchAddress('FatJetSDsubjetBpt', FatJetSDsubjetBpt)
        t.SetBranchAddress('FatJetSDsubjetBmass', FatJetSDsubjetBmass)
        t.SetBranchAddress('FatJetJECUpSys', FatJetJECUpSys)
        t.SetBranchAddress('FatJetJECDnSys', FatJetJECDnSys)
        t.SetBranchAddress('FatJetJERUpSys', FatJetJERUpSys)
        t.SetBranchAddress('FatJetJERDnSys', FatJetJERDnSys)
        t.SetBranchAddress('LeptonType', LeptonType)
        t.SetBranchAddress('LeptonPt', LeptonPt)
        t.SetBranchAddress('LeptonEta', LeptonEta)
        t.SetBranchAddress('LeptonPhi', LeptonPhi)
        t.SetBranchAddress('LeptonEnergy', LeptonEnergy)
        t.SetBranchAddress('LeptonIso', LeptonIso)
        t.SetBranchAddress('LeptonPtRel', LeptonPtRel)
        t.SetBranchAddress('LeptonDRMin', LeptonDRMin)
        t.SetBranchAddress('SemiLepMETpt', SemiLepMETpt)
        t.SetBranchAddress('SemiLepMETphi', SemiLepMETphi)
        t.SetBranchAddress('SemiLepNvtx', SemiLepNvtx)
        t.SetBranchAddress('FatJetDeltaPhiLep', FatJetDeltaPhiLep)
        t.SetBranchAddress('NearestAK4JetBDisc', NearestAK4JetBDisc)
        t.SetBranchAddress('NearestAK4JetPt', NearestAK4JetPt)
        t.SetBranchAddress('NearestAK4JetEta', NearestAK4JetEta)
        t.SetBranchAddress('NearestAK4JetPhi', NearestAK4JetPhi)
        t.SetBranchAddress('NearestAK4JetMass', NearestAK4JetMass)
        t.SetBranchAddress('NearestAK4JetJECUpSys', NearestAK4JetJECUpSys)
        t.SetBranchAddress('NearestAK4JetJECDnSys', NearestAK4JetJECDnSys)
        t.SetBranchAddress('NearestAK4JetJERUpSys', NearestAK4JetJERUpSys)
        t.SetBranchAddress('NearestAK4JetJERDnSys', NearestAK4JetJERDnSys)
        t.SetBranchAddress('SemiLeptRunNum', SemiLeptRunNum)
        t.SetBranchAddress('SemiLeptLumiNum', SemiLeptLumiNum)
        t.SetBranchAddress('SemiLeptEventNum', SemiLeptEventNum)

        t.SetBranchStatus('*', 0)
        t.SetBranchStatus('SemiLeptWeight', 1)
        t.SetBranchStatus('PUWeight', 1)
        t.SetBranchStatus('GenWeight', 1)
        t.SetBranchStatus('FatJetPt', 1)
        t.SetBranchStatus('FatJetEta', 1)
        t.SetBranchStatus('FatJetPhi', 1)
        t.SetBranchStatus('FatJetMass', 1)
        t.SetBranchStatus('FatJetMassSoftDrop', 1)
        t.SetBranchStatus('FatJetTau32', 1)
        t.SetBranchStatus('FatJetTau21', 1)
        t.SetBranchStatus('SemiLeptTrig', 1)
        t.SetBranchStatus('NearestAK4JetBDisc', 1)
        t.SetBranchStatus('NearestAK4JetPt', 1)
        t.SetBranchStatus('NearestAK4JetEta', 1)
        t.SetBranchStatus('NearestAK4JetPhi', 1)
        t.SetBranchStatus('NearestAK4JetMass', 1)
        t.SetBranchStatus('SemiLepMETpt', 1)
        t.SetBranchStatus('SemiLepMETphi', 1)
        t.SetBranchStatus('LeptonType', 1)
        t.SetBranchStatus('LeptonPt', 1)
        t.SetBranchStatus('LeptonEta', 1)
        t.SetBranchStatus('LeptonPhi', 1)
        t.SetBranchStatus('LeptonEnergy', 1)
        t.SetBranchStatus('LeptonIso', 1)
        t.SetBranchStatus('LeptonPtRel', 1)
        t.SetBranchStatus('LeptonDRMin', 1)
        t.SetBranchStatus('FatJetJECUpSys', 1)
        t.SetBranchStatus('FatJetJECDnSys', 1)
        t.SetBranchStatus('FatJetJERUpSys', 1)
        t.SetBranchStatus('FatJetJERDnSys', 1)
        t.SetBranchStatus('NearestAK4JetJECUpSys', 1)
        t.SetBranchStatus('NearestAK4JetJECDnSys', 1)
        t.SetBranchStatus('NearestAK4JetJERUpSys', 1)
        t.SetBranchStatus('NearestAK4JetJERDnSys', 1)

        entries = t.GetEntriesFast()
        tot_entries += entries

        #print 'Processing tree ' + str(itree)

        eventsToRun = entries
        for jentry in xrange(eventsToRun):
            #if jentry % 100000 == 0 :
            #    print 'processing ' + str(jentry)
            # get the next tree in the chain and verify
            ientry = t.GetEntry(jentry)
            if ientry < 0:
                break

#Triggers that are available:
#   0: "HLT_Mu50"
#   1: "HLT_Ele50_CaloIdVT_GsfTrkIdT_PFJet165"
#   2: "HLT_Ele115_CaloIdVT_GsfTrkIdT"
#   3: "HLT_PFHT800"

            if options.lepton == 'mu':

                if LeptonType[0] != 13:
                    continue

                # Muon triggers only for now (use HLT_Mu50 with index 0)

                if SemiLeptTrig[0] == 0:
                    continue

            if options.lepton == 'ele':

                if LeptonType[0] != 11:
                    continue

                # Muon triggers only for now (use HLT_Ele50_CaloIdVT_GsfTrkIdT_PFJet165 with index 1 and HLT_Ele115_CaloIdVT_GsfTrkIdT with index 2)
                if SemiLeptTrig[1] == 0 and SemiLeptTrig[2] == 0:
                    continue

            # Hadronic top
            hadTopCandP4 = ROOT.TLorentzVector()
            hadTopCandP4.SetPtEtaPhiM(
                FatJetPt[0], FatJetEta[0], FatJetPhi[0],
                FatJetMass[0])  #set up with lead Ak8 jet in event
            bJetCandP4 = ROOT.TLorentzVector()
            bJetCandP4.SetPtEtaPhiM(NearestAK4JetPt[0], NearestAK4JetEta[0],
                                    NearestAK4JetPhi[0], NearestAK4JetMass[0])
            # MET
            nuCandP4 = ROOT.TLorentzVector()
            nuCandP4.SetPtEtaPhiM(SemiLepMETpt[0], 0, SemiLepMETphi[0],
                                  SemiLepMETpt[0])
            # Leptoon
            theLepton = ROOT.TLorentzVector()
            theLepton.SetPtEtaPhiE(LeptonPt[0], LeptonEta[0], LeptonPhi[0],
                                   LeptonEnergy[0])  # Assume massless

            tau32 = FatJetTau32[0]
            mass_sd = FatJetMassSoftDrop[0]
            bdisc = NearestAK4JetBDisc[0]

            #h_pileupWeight.GetBinContent(SemiLepNvtx+1)
            #Weights
            weight = 1
            if options.jec == 'up':
                weight = 1 * NearestAK4JetJECUpSys[0] * FatJetJECUpSys[0]
            if options.jec == 'down':
                weight = 1 * NearestAK4JetJECDnSys[0] * FatJetJECDnSys[0]
            if options.jer == 'up':
                weight = 1 * NearestAK4JetJERUpSys[0] * FatJetJECUpSys[0]
            if options.jer == 'down':
                weight = 1 * NearestAK4JetJERDnSys[0] * FatJetJECDnSys[0]

            #print weight

            #preselection histos
            #h_AK4BdiscPreSel.Fill( NearestAK4JetBDisc[0], weight )
            #h_AK8Tau32PreSel.Fill(FatJetTau32[0], weight )
            #h_AK8Tau21PreSel.Fill(FatJetTau21[0], weight )

            passKin = hadTopCandP4.Perp() > 100.
            passTopTag = tau32 < 0.8 and mass_sd > 110. and mass_sd < 250.
            pass2DCut = LeptonPtRel[0] > 20. or LeptonDRMin[0] > 0.4
            passBtag = bdisc > 0.7

            # Applying and counting cuts
            if not passKin:
                continue
            else:
                cut1 += 1
            if not pass2DCut:
                continue
            else:
                cut2 += 1
            if not passBtag:
                continue
            else:
                cut3 += 1
            if not passTopTag:
                continue
            else:
                cut4 += 1

            ##  ____  __.__                              __  .__         __________
            ## |    |/ _|__| ____   ____   _____ _____ _/  |_|__| ____   \______   \ ____   ____  ____
            ## |      < |  |/    \_/ __ \ /     \\__  \\   __\  |/ ___\   |       _// __ \_/ ___\/  _ \
            ## |    |  \|  |   |  \  ___/|  Y Y  \/ __ \|  | |  \  \___   |    |   \  ___/\  \__(  <_> )
            ## |____|__ \__|___|  /\___  >__|_|  (____  /__| |__|\___  >  |____|_  /\___  >\___  >____/
            ##         \/       \/     \/      \/     \/             \/          \/     \/     \/

            # Now we do our kinematic calculation based on the categories of the
            # number of top and bottom tags
            mttbar = -1.0

            lepTopCandP4 = None
            # Get the z-component of the lepton from the W mass constraint
            solution, nuz1, nuz2 = solve_nu(vlep=theLepton, vnu=nuCandP4)
            # If there is at least one real solution, pick it up
            if solution:
                nuCandP4.SetPz(nuz1)
            else:
                nuCandP4.SetPz(nuz1.real)

            lepTopCandP4 = nuCandP4 + theLepton + bJetCandP4

            ttbarCand = hadTopCandP4 + lepTopCandP4
            mttbar = ttbarCand.M()

            # Filling plots
            count += 1
            h_mttbar.Fill(mttbar, weight)
            h_mtopHadGroomed.Fill(mass_sd, weight)
            h_mtopHad.Fill(hadTopCandP4.M(), weight)

            #fill lepton histos
            h_lepPt.Fill(LeptonPt[0], weight)
            h_lepEta.Fill(LeptonEta[0], weight)
            h_lepPhi.Fill(LeptonPhi[0], weight)

            #fill Jet histos
            h_AK8Pt.Fill(FatJetPt[0], weight)
            h_AK8Eta.Fill(FatJetEta[0], weight)
            h_AK8Phi.Fill(FatJetPhi[0], weight)

            h_AK4Pt.Fill(NearestAK4JetPt[0], weight)
            h_AK4Eta.Fill(NearestAK4JetEta[0], weight)
            h_AK4Phi.Fill(NearestAK4JetPhi[0], weight)
            h_AK4M.Fill(NearestAK4JetMass[0], weight)
            h_AK4Bdisc.Fill(NearestAK4JetBDisc[0], weight)

            #dr's
            h_drAK4AK8.Fill(bJetCandP4.DeltaR(bJetCandP4), weight)
            h_drLepAK4.Fill(theLepton.DeltaR(hadTopCandP4), weight)
            h_AK8Tau32.Fill(FatJetTau32[0], weight)
            h_AK8Tau21.Fill(FatJetTau21[0], weight)

            h_dPhiLepAK8.Fill(FatJetDeltaPhiLep[0], weight)

    # Fill cut-flow
    h_cuts.SetBinContent(1, cut1)
    h_cuts.SetBinContent(2, cut2)
    h_cuts.SetBinContent(3, cut3)
    h_cuts.SetBinContent(4, cut4)
    h_cuts.GetXaxis().SetBinLabel(1, "passKin")
    h_cuts.GetXaxis().SetBinLabel(2, "pass2DCut")
    h_cuts.GetXaxis().SetBinLabel(3, "passBtag")
    h_cuts.GetXaxis().SetBinLabel(4, "passTopTag")


    print options.file_out, " : ", count, "/", tot_entries, ", Percentage:", round(float(count)/(float(tot_entries+1))*100,3), "%", \
     "Cut_flow: [", cut1, cut2, cut3, cut4, "]"

    fh.write(options.file_in)
    fh.write("  " + str(count))
    fh.write('\n')
    fh.close

    fout.cd()
    fout.Write()
    fout.Close()
Example #6
0
def plot_mttbar(argv) : 
    parser = OptionParser()

    parser.add_option('--file_in', type='string', action='store',
                      dest='file_in',
                      help='Input file')

    parser.add_option('--file_out', type='string', action='store',
                      dest='file_out',
                      help='Output file')
    
    #parser.add_option('--isData', action='store_true',
    #                  dest='isData',
    #                  default = False,
    #                  help='Is this Data?')
        
    (options, args) = parser.parse_args(argv)
    argv = []

    print '===== Command line options ====='
    print options
    print '================================'

    import ROOT

    from leptonic_nu_z_component import solve_nu_tmass, solve_nu

    fout= ROOT.TFile(options.file_out, "RECREATE")
    h_mttbar = ROOT.TH1F("h_mttbar", ";m_{t#bar{t}} (GeV);Number", 100, 0, 5000)
    h_mtopHad = ROOT.TH1F("h_mtopHad", ";m_{jet} (GeV);Number", 100, 0, 400)
    h_mtopHadGroomed = ROOT.TH1F("h_mtopHadGroomed", ";Groomed m_{jet} (GeV);Number", 100, 0, 400)
    fin = ROOT.TFile.Open(options.file_in)


    trees = [ fin.Get("TreeSemiLept") ]


    
    for itree,t in enumerate(trees) :

        #if options.isData : 
        SemiLeptTrig        =  ROOT.vector('int')()
        SemiLeptWeight      = array.array('f', [0.] )
        PUWeight            = array.array('f', [0.] )
        GenWeight           = array.array('f', [0.] )
        FatJetPt            = array.array('f', [-1.])
        FatJetEta           = array.array('f', [-1.])
        FatJetPhi           = array.array('f', [-1.])
        FatJetRap           = array.array('f', [-1.])
        FatJetEnergy        = array.array('f', [-1.])
        FatJetBDisc         = array.array('f', [-1.])
        FatJetMass          = array.array('f', [-1.])
        FatJetMassSoftDrop  = array.array('f', [-1.])
        FatJetTau32         = array.array('f', [-1.])
        FatJetTau21         = array.array('f', [-1.]) 
        FatJetSDBDiscW      = array.array('f', [-1.])
        FatJetSDBDiscB      = array.array('f', [-1.])
        FatJetSDsubjetWpt   = array.array('f', [-1.])
        FatJetSDsubjetWmass = array.array('f', [-1.])
        FatJetSDsubjetBpt   = array.array('f', [-1.])
        FatJetSDsubjetBmass = array.array('f', [-1.])
        FatJetJECUpSys      = array.array('f', [-1.])
        FatJetJECDnSys      = array.array('f', [-1.])
        FatJetJERUpSys      = array.array('f', [-1.])
        FatJetJERDnSys      = array.array('f', [-1.])
        LeptonType          = array.array('i', [-1])
        LeptonPt            = array.array('f', [-1.])
        LeptonEta           = array.array('f', [-1.])
        LeptonPhi           = array.array('f', [-1.])
        LeptonEnergy        = array.array('f', [-1.])
        LeptonIso           = array.array('f', [-1.])
        LeptonPtRel         = array.array('f', [-1.])
        LeptonDRMin         = array.array('f', [-1.])
        SemiLepMETpt        = array.array('f', [-1.])
        SemiLepMETphi       = array.array('f', [-1.])
        SemiLepNvtx         = array.array('f', [-1.])
        FatJetDeltaPhiLep      = array.array('f', [-1.]) 
        NearestAK4JetBDisc            = array.array('f', [-1.])
        NearestAK4JetPt     = array.array('f', [-1.])
        NearestAK4JetEta    = array.array('f', [-1.])
        NearestAK4JetPhi    = array.array('f', [-1.])
        NearestAK4JetMass   = array.array('f', [-1.])
        NearestAK4JetJECUpSys = array.array('f', [-1.])
        NearestAK4JetJECDnSys = array.array('f', [-1.])
        NearestAK4JetJERUpSys = array.array('f', [-1.])
        NearestAK4JetJERDnSys = array.array('f', [-1.])
        SemiLeptRunNum        = array.array('f', [-1.])   
        SemiLeptLumiNum     = array.array('f', [-1.])   
        SemiLeptEventNum      = array.array('f', [-1.])   


        #if options.isData : 
        t.SetBranchAddress('SemiLeptTrig'        , SemiLeptTrig )
        t.SetBranchAddress('SemiLeptWeight'      , SemiLeptWeight      ) #Combined weight of all scale factors (lepton, PU, generator) relevant for the smeileptonic event selection
        t.SetBranchAddress('PUWeight'            , PUWeight            )
        t.SetBranchAddress('GenWeight'           , GenWeight               )
        t.SetBranchAddress('FatJetPt'            , FatJetPt            )
        t.SetBranchAddress('FatJetEta'           , FatJetEta           )
        t.SetBranchAddress('FatJetPhi'           , FatJetPhi           )
        t.SetBranchAddress('FatJetRap'           , FatJetRap           )
        t.SetBranchAddress('FatJetEnergy'        , FatJetEnergy        )
        t.SetBranchAddress('FatJetBDisc'         , FatJetBDisc         )
        t.SetBranchAddress('FatJetMass'          , FatJetMass           )
        t.SetBranchAddress('FatJetMassSoftDrop'  , FatJetMassSoftDrop  )
        t.SetBranchAddress('FatJetTau32'         , FatJetTau32         )
        t.SetBranchAddress('FatJetTau21'         , FatJetTau21         )
        t.SetBranchAddress('FatJetSDBDiscW'      , FatJetSDBDiscW      )
        t.SetBranchAddress('FatJetSDBDiscB'      , FatJetSDBDiscB              )
        t.SetBranchAddress('FatJetSDsubjetWpt'   , FatJetSDsubjetWpt   )
        t.SetBranchAddress('FatJetSDsubjetWmass' , FatJetSDsubjetWmass )
        t.SetBranchAddress('FatJetSDsubjetBpt'   , FatJetSDsubjetBpt   )
        t.SetBranchAddress('FatJetSDsubjetBmass' , FatJetSDsubjetBmass )
        t.SetBranchAddress('FatJetJECUpSys'      , FatJetJECUpSys      )
        t.SetBranchAddress('FatJetJECDnSys'      , FatJetJECDnSys      )
        t.SetBranchAddress('FatJetJERUpSys'      , FatJetJERUpSys      )
        t.SetBranchAddress('FatJetJERDnSys'      , FatJetJERDnSys      )
        t.SetBranchAddress('LeptonType'          , LeptonType          )
        t.SetBranchAddress('LeptonPt'            , LeptonPt            )
        t.SetBranchAddress('LeptonEta'           , LeptonEta           )
        t.SetBranchAddress('LeptonPhi'           , LeptonPhi           )
        t.SetBranchAddress('LeptonEnergy'        , LeptonEnergy        )
        t.SetBranchAddress('LeptonIso'           , LeptonIso           )
        t.SetBranchAddress('LeptonPtRel'         , LeptonPtRel         )
        t.SetBranchAddress('LeptonDRMin'         , LeptonDRMin         )
        t.SetBranchAddress('SemiLepMETpt'        , SemiLepMETpt        )
        t.SetBranchAddress('SemiLepMETphi'       , SemiLepMETphi       )
        t.SetBranchAddress('SemiLepNvtx'         , SemiLepNvtx         )
        t.SetBranchAddress('FatJetDeltaPhiLep'      , FatJetDeltaPhiLep      )
        t.SetBranchAddress('NearestAK4JetBDisc'            ,NearestAK4JetBDisc             )
        t.SetBranchAddress('NearestAK4JetPt'     ,NearestAK4JetPt      )
        t.SetBranchAddress('NearestAK4JetEta'    ,NearestAK4JetEta     )
        t.SetBranchAddress('NearestAK4JetPhi'    ,NearestAK4JetPhi     )
        t.SetBranchAddress('NearestAK4JetMass'   ,NearestAK4JetMass    )
        t.SetBranchAddress('NearestAK4JetJECUpSys'      , NearestAK4JetJECUpSys)
        t.SetBranchAddress('NearestAK4JetJECDnSys'      , NearestAK4JetJECDnSys)
        t.SetBranchAddress('NearestAK4JetJERUpSys'      , NearestAK4JetJERUpSys)
        t.SetBranchAddress('NearestAK4JetJERDnSys'      , NearestAK4JetJERDnSys)
        t.SetBranchAddress('SemiLeptRunNum'         ,  SemiLeptRunNum       )
        t.SetBranchAddress('SemiLeptLumiNum'      ,  SemiLeptLumiNum    )
        t.SetBranchAddress('SemiLeptEventNum'       ,  SemiLeptEventNum     )


        t.SetBranchStatus ('*', 0)
        t.SetBranchStatus ('SemiLeptWeight', 1)
        t.SetBranchStatus ('PUWeight', 1)
        t.SetBranchStatus ('GenWeight', 1)
        t.SetBranchStatus ('FatJetPt', 1)
        t.SetBranchStatus ('FatJetEta', 1)
        t.SetBranchStatus ('FatJetPhi', 1)
        t.SetBranchStatus ('FatJetMass', 1)
        t.SetBranchStatus ('FatJetMassSoftDrop', 1)
        t.SetBranchStatus ('FatJetTau32', 1)
        t.SetBranchStatus ('SemiLeptTrig', 1)
        t.SetBranchStatus ('NearestAK4JetBDisc', 1)
        t.SetBranchStatus ('NearestAK4JetPt'   ,1 )
        t.SetBranchStatus ('NearestAK4JetEta'  ,1 )
        t.SetBranchStatus ('NearestAK4JetPhi'  ,1 )
        t.SetBranchStatus ('NearestAK4JetMass' ,1 )
        t.SetBranchStatus ('SemiLepMETpt' , 1 )
        t.SetBranchStatus ('SemiLepMETphi' , 1 )
        t.SetBranchStatus ('LeptonType'          , 1 )
        t.SetBranchStatus ('LeptonPt'            , 1)
        t.SetBranchStatus ('LeptonEta'           , 1)
        t.SetBranchStatus ('LeptonPhi'           , 1)
        t.SetBranchStatus ('LeptonEnergy'        , 1)
        t.SetBranchStatus ('LeptonIso'           , 1)
        t.SetBranchStatus ('LeptonPtRel'         , 1)
        t.SetBranchStatus ('LeptonDRMin'         , 1)


        entries = t.GetEntriesFast()
        print 'Processing tree ' + str(itree)

        eventsToRun = entries
        for jentry in xrange( eventsToRun ):
            if jentry % 100000 == 0 :
                print 'processing ' + str(jentry)
            # get the next tree in the chain and verify
            ientry = t.GetEntry( jentry )
            if ientry < 0:
                break

            # Muons only for now
            if LeptonType[0] != 13 :
                continue

            # Muon triggers only for now (use HLT_Mu45_eta2p1 with index 1)
            if SemiLeptTrig[1] != 1  :
                continue


            hadTopCandP4 = ROOT.TLorentzVector()
            hadTopCandP4.SetPtEtaPhiM( FatJetPt[0], FatJetEta[0], FatJetPhi[0], FatJetMass[0])
            bJetCandP4 = ROOT.TLorentzVector()
            bJetCandP4.SetPtEtaPhiM( NearestAK4JetPt[0], NearestAK4JetEta[0], NearestAK4JetPhi[0], NearestAK4JetMass[0])
            nuCandP4 = ROOT.TLorentzVector( )
            nuCandP4.SetPtEtaPhiM( SemiLepMETpt[0], 0, SemiLepMETphi[0], SemiLepMETpt[0] )
            theLepton = ROOT.TLorentzVector()
            theLepton.SetPtEtaPhiE( LeptonPt[0], LeptonEta[0], LeptonPhi[0], LeptonEnergy[0] ) # Assume massless
            
            
            tau32 = FatJetTau32[0]
            mass_sd = FatJetMassSoftDrop[0]
            bdisc = NearestAK4JetBDisc[0]

            passKin = hadTopCandP4.Perp() > 400.
            passTopTag = tau32 < 0.6 and mass_sd > 110. and mass_sd < 250.
            pass2DCut = LeptonPtRel[0] > 55. or LeptonDRMin[0] > 0.4
            passBtag = bdisc > 0.7

            if not passKin or not pass2DCut or not passBtag or not passTopTag :
                continue


            ##  ____  __.__                              __  .__         __________                     
            ## |    |/ _|__| ____   ____   _____ _____ _/  |_|__| ____   \______   \ ____   ____  ____  
            ## |      < |  |/    \_/ __ \ /     \\__  \\   __\  |/ ___\   |       _// __ \_/ ___\/  _ \ 
            ## |    |  \|  |   |  \  ___/|  Y Y  \/ __ \|  | |  \  \___   |    |   \  ___/\  \__(  <_> )
            ## |____|__ \__|___|  /\___  >__|_|  (____  /__| |__|\___  >  |____|_  /\___  >\___  >____/ 
            ##         \/       \/     \/      \/     \/             \/          \/     \/     \/       

            # Now we do our kinematic calculation based on the categories of the
            # number of top and bottom tags
            mttbar = -1.0


            lepTopCandP4 = None
            # Get the z-component of the lepton from the W mass constraint
            solution, nuz1, nuz2 = solve_nu( vlep=theLepton, vnu=nuCandP4 )
            # If there is at least one real solution, pick it up
            if solution :
                nuCandP4.SetPz( nuz1 )
            else :
                nuCandP4.SetPz( nuz1.real )

            lepTopCandP4 = nuCandP4 + theLepton + bJetCandP4

            ttbarCand = hadTopCandP4 + lepTopCandP4
            mttbar = ttbarCand.M()

            h_mttbar.Fill( mttbar, SemiLeptWeight[0] )
            h_mtopHadGroomed.Fill( mass_sd, SemiLeptWeight[0] )
            h_mtopHad.Fill( hadTopCandP4.M(), SemiLeptWeight[0] )
            

    fout.cd()
    fout.Write()
    fout.Close()
Example #7
0
def plot_mttbar(argv):
    parser = OptionParser()

    parser.add_option('--file_in',
                      type='string',
                      action='store',
                      dest='file_in',
                      help='Input file')

    parser.add_option('--file_out',
                      type='string',
                      action='store',
                      dest='file_out',
                      help='Output file')

    parser.add_option('--leptonType',
                      type='string',
                      action='store',
                      dest='lepton_type',
                      default="electron",
                      help='Lepton Type')

    parser.add_option('--topTag',
                      action='store_true',
                      dest='topTag',
                      help='Require top tag. Defaults to true')

    parser.add_option('--bTag',
                      action='store_true',
                      dest='bTag',
                      help='Require b tag. Defaults to true')

    # Not necessary because LeptonType is a reco quantity
    #parser.add_option('--isData', action='store_true',
    #                  dest='isData',
    #                  default = False,
    #                  help='Is this Data?')

    (options, args) = parser.parse_args(argv)
    print "Top tag", options.topTag
    print "B tag", options.bTag
    argv = []

    print '===== Command line options ====='
    print options
    print '================================'

    import ROOT

    from leptonic_nu_z_component import solve_nu_tmass, solve_nu

    fout = ROOT.TFile(options.file_out, "RECREATE")

    h_mttbar = ROOT.TH1F("h_mttbar", ";m_{t#bar{t}} (GeV);Number", 50, 0, 5000)

    h_mttbar_lwu = ROOT.TH1F("h_mttbar_lwu", ";m_{t#bar{t}} (GeV);Number", 50,
                             0, 4000)
    h_mttbar_lwd = ROOT.TH1F("h_mttbar_lwd", ";m_{t#bar{t}} (GeV);Number", 50,
                             0, 4000)

    h_mttbar_jec_up = ROOT.TH1F("h_mttbar_jec_up",
                                ";m_{t#bar{t}} (GeV);Number", 50, 0, 4000)
    h_mttbar_jec_down = ROOT.TH1F("h_mttbar_jec_down",
                                  ";m_{t#bar{t}} (GeV);Number", 50, 0, 4000)
    h_mttbar_jer_up = ROOT.TH1F("h_mttbar_jer_up",
                                ";m_{t#bar{t}} (GeV);Number", 50, 0, 4000)
    h_mttbar_jer_down = ROOT.TH1F("h_mttbar_jer_down",
                                  ";m_{t#bar{t}} (GeV);Number", 50, 0, 4000)

    h_mtopHad = ROOT.TH1F("h_mtopHad", ";m_{jet} (GeV);Number", 50, 100, 350)
    h_mtopHadGroomed = ROOT.TH1F("h_mtopHadGroomed",
                                 ";Groomed m_{jet} (GeV);Number", 50, 100, 250)

    h_lepton_pt = ROOT.TH1F("h_lepton_pt", ";p_{t} (GeV);Number", 50, 45, 500)
    h_MET = ROOT.TH1F("h_MET", ";E_T Missing (GeV);Number", 50, 0, 500)
    h_lepton_eta = ROOT.TH1F("h_lepton_eta", "eta;Number", 50, -3, 3)
    h_LepTopCandP4_pt = ROOT.TH1F("h_LepTopCanP4_pt",
                                  ";p_T Leptonic Top (GeV);Number", 50, 0,
                                  1500)
    h_LepTopCandP4_m = ROOT.TH1F("h_LepTopCanP4_m", ";m_t (GeV);Number", 50, 0,
                                 1500)
    h_Compare = ROOT.TH2D("h_Compare", "FatJet p_T (GeV);Lepton DR Min", 50, 0,
                          2000, 50, 0, 6)  # Figure out syntax later...

    h_FatJetPt = ROOT.TH1F("h_FatJetPt", ";pt_{jet} (GeV) ; Number", 50, 400,
                           1000)
    h_FatJetEta = ROOT.TH1F("h_FatJetEta", ";#Eta ; Number", 50, -3, 3)
    h_NearestAK4JetPt = ROOT.TH1F("h_NearestAK4JetPt",
                                  ";pt_{jet} (GeV) ; Number", 50, 25, 500)
    h_NearestAK4JetEta = ROOT.TH1F("h_NearestAK4JetEta", "; #Eta ; Number", 50,
                                   -3, 3)
    h_NearestAK4JetBDisc = ROOT.TH1F("h_NearestAK4JetBDisc", ";BDisc ; Number",
                                     50, 0.1, 1)

    h_FatJetPt.Sumw2()
    h_FatJetEta.Sumw2()
    h_NearestAK4JetPt.Sumw2()
    h_NearestAK4JetEta.Sumw2()
    h_NearestAK4JetBDisc.Sumw2()

    h_mttbar.Sumw2()

    h_mttbar_lwu.Sumw2()
    h_mttbar_lwd.Sumw2()

    h_mttbar_jec_up.Sumw2()
    h_mttbar_jec_down.Sumw2()
    h_mttbar_jer_up.Sumw2()
    h_mttbar_jer_down.Sumw2()

    h_mtopHad.Sumw2()
    h_mtopHadGroomed.Sumw2()

    h_lepton_pt.Sumw2()
    h_MET.Sumw2()
    h_lepton_eta.Sumw2()
    h_LepTopCandP4_pt.Sumw2()
    h_LepTopCandP4_m.Sumw2()
    h_Compare.Sumw2()

    h_passElHT = ROOT.TH1F("h_passElHT", ";pt_{el} (Gev) ; Number", NBinsEl,
                           array.array('f', edgesEl))
    h_passHTelBin = ROOT.TH1F("h_passHTelBin", ";pt_{el} (Gev) ; Number",
                              NBinsEl, array.array('f', edgesEl))
    h_passHTmuBin = ROOT.TH1F("h_passHTmuBin", ";pt_{el} (Gev) ; Number",
                              NBinsMu, array.array('f', edgesMu))
    h_passMuHT = ROOT.TH1F("h_passMuHT", ";pt_{mu} (Gev) ; Number", NBinsMu,
                           array.array('f', edgesMu))
    h_passElHT.Sumw2()
    h_passMuHT.Sumw2()
    h_passHTmuBin.Sumw2()
    h_passHTelBin.Sumw2()

    h_effEl = ROOT.TH1F("h_effEl", ";pt_{el} (Gev) ; Number", NBinsEl,
                        array.array('f', edgesEl))
    h_effMu = ROOT.TH1F("h_effMu", ";pt_{mu} (Gev) ; Number", NBinsMu,
                        array.array('f', edgesMu))

    fin = ROOT.TFile.Open(options.file_in)

    trees = [fin.Get("TreeSemiLept")]

    isData = False
    if "SingleElectron" in options.file_in or "SingleMuon" in options.file_in:
        isData = True
        print "Is Data"

    for itree, t in enumerate(trees):

        #if options.isData :
        SemiLeptTrig = ROOT.vector('int')()
        LeptonIDWeight = array.array('f', [0.])
        LeptonIDWeightUnc = array.array('f', [0.])
        EleRecoWeight = array.array('f', [0.])
        EleRecoWeightUnc = array.array('f', [0.])
        SemiLeptWeight = array.array('f', [0.])
        PUWeight = array.array('f', [0.])
        GenWeight = array.array('f', [0.])
        FatJetPt = array.array('f', [-1.])
        FatJetEta = array.array('f', [-1.])
        FatJetPhi = array.array('f', [-1.])
        FatJetRap = array.array('f', [-1.])
        FatJetEnergy = array.array('f', [-1.])
        FatJetBDisc = array.array('f', [-1.])
        FatJetMass = array.array('f', [-1.])
        FatJetMassSoftDrop = array.array('f', [-1.])
        FatJetTau32 = array.array('f', [-1.])
        FatJetTau21 = array.array('f', [-1.])
        FatJetSDBDiscW = array.array('f', [-1.])
        FatJetSDBDiscB = array.array('f', [-1.])
        FatJetSDsubjetWpt = array.array('f', [-1.])
        FatJetSDsubjetWmass = array.array('f', [-1.])
        FatJetSDsubjetBpt = array.array('f', [-1.])
        FatJetSDsubjetBmass = array.array('f', [-1.])
        FatJetJECUpSys = array.array('f', [-1.])
        FatJetJECDnSys = array.array('f', [-1.])
        FatJetJERUpSys = array.array('f', [-1.])
        FatJetJERDnSys = array.array('f', [-1.])
        LeptonType = array.array('i', [-1])
        LeptonPt = array.array('f', [-1.])
        LeptonEta = array.array('f', [-1.])
        LeptonPhi = array.array('f', [-1.])
        LeptonEnergy = array.array('f', [-1.])
        LeptonIso = array.array('f', [-1.])
        LeptonPtRel = array.array('f', [-1.])
        LeptonDRMin = array.array('f', [-1.])
        SemiLepMETpt = array.array('f', [-1.])
        SemiLepMETphi = array.array('f', [-1.])
        SemiLepNvtx = array.array('f', [-1.])
        FatJetDeltaPhiLep = array.array('f', [-1.])
        NearestAK4JetBDisc = array.array('f', [-1.])
        NearestAK4JetPt = array.array('f', [-1.])
        NearestAK4JetEta = array.array('f', [-1.])
        NearestAK4JetPhi = array.array('f', [-1.])
        NearestAK4JetMass = array.array('f', [-1.])
        NearestAK4JetJECUpSys = array.array('f', [-1.])
        NearestAK4JetJECDnSys = array.array('f', [-1.])
        NearestAK4JetJERUpSys = array.array('f', [-1.])
        NearestAK4JetJERDnSys = array.array('f', [-1.])
        SemiLeptRunNum = array.array('f', [-1.])
        SemiLeptLumiNum = array.array('f', [-1.])
        SemiLeptEventNum = array.array('f', [-1.])

        #if options.isData :
        t.SetBranchAddress('SemiLeptTrig', SemiLeptTrig)
        t.SetBranchAddress(
            'SemiLeptWeight', SemiLeptWeight
        )  #Combined weight of all scale factors (lepton, PU, generator) relevant for the smeileptonic event selection
        t.SetBranchAddress('LeptonIDWeight', LeptonIDWeight)
        t.SetBranchAddress('LeptonIDWeightUnc', LeptonIDWeightUnc)
        t.SetBranchAddress('EleRecoWeight', EleRecoWeight)
        t.SetBranchAddress('EleRecoWeightUnc', EleRecoWeightUnc)
        t.SetBranchAddress('PUWeight', PUWeight)
        t.SetBranchAddress('GenWeight', GenWeight)
        t.SetBranchAddress('FatJetPt', FatJetPt)
        t.SetBranchAddress('FatJetEta', FatJetEta)
        t.SetBranchAddress('FatJetPhi', FatJetPhi)
        t.SetBranchAddress('FatJetRap', FatJetRap)
        t.SetBranchAddress('FatJetEnergy', FatJetEnergy)
        t.SetBranchAddress('FatJetBDisc', FatJetBDisc)
        t.SetBranchAddress('FatJetMass', FatJetMass)
        t.SetBranchAddress('FatJetMassSoftDrop', FatJetMassSoftDrop)
        t.SetBranchAddress('FatJetTau32', FatJetTau32)
        t.SetBranchAddress('FatJetTau21', FatJetTau21)
        t.SetBranchAddress('FatJetSDBDiscW', FatJetSDBDiscW)
        t.SetBranchAddress('FatJetSDBDiscB', FatJetSDBDiscB)
        t.SetBranchAddress('FatJetSDsubjetWpt', FatJetSDsubjetWpt)
        t.SetBranchAddress('FatJetSDsubjetWmass', FatJetSDsubjetWmass)
        t.SetBranchAddress('FatJetSDsubjetBpt', FatJetSDsubjetBpt)
        t.SetBranchAddress('FatJetSDsubjetBmass', FatJetSDsubjetBmass)
        t.SetBranchAddress('FatJetJECUpSys', FatJetJECUpSys)
        t.SetBranchAddress('FatJetJECDnSys', FatJetJECDnSys)
        t.SetBranchAddress('FatJetJERUpSys', FatJetJERUpSys)
        t.SetBranchAddress('FatJetJERDnSys', FatJetJERDnSys)
        t.SetBranchAddress('LeptonType', LeptonType)
        t.SetBranchAddress('LeptonPt', LeptonPt)
        t.SetBranchAddress('LeptonEta', LeptonEta)
        t.SetBranchAddress('LeptonPhi', LeptonPhi)
        t.SetBranchAddress('LeptonEnergy', LeptonEnergy)
        t.SetBranchAddress('LeptonIso', LeptonIso)
        t.SetBranchAddress('LeptonPtRel', LeptonPtRel)
        t.SetBranchAddress('LeptonDRMin', LeptonDRMin)
        t.SetBranchAddress('SemiLepMETpt', SemiLepMETpt)
        t.SetBranchAddress('SemiLepMETphi', SemiLepMETphi)
        t.SetBranchAddress('SemiLepNvtx', SemiLepNvtx)
        t.SetBranchAddress('FatJetDeltaPhiLep', FatJetDeltaPhiLep)
        t.SetBranchAddress('NearestAK4JetBDisc', NearestAK4JetBDisc)
        t.SetBranchAddress('NearestAK4JetPt', NearestAK4JetPt)
        t.SetBranchAddress('NearestAK4JetEta', NearestAK4JetEta)
        t.SetBranchAddress('NearestAK4JetPhi', NearestAK4JetPhi)
        t.SetBranchAddress('NearestAK4JetMass', NearestAK4JetMass)
        t.SetBranchAddress('NearestAK4JetJECUpSys', NearestAK4JetJECUpSys)
        t.SetBranchAddress('NearestAK4JetJECDnSys', NearestAK4JetJECDnSys)
        t.SetBranchAddress('NearestAK4JetJERUpSys', NearestAK4JetJERUpSys)
        t.SetBranchAddress('NearestAK4JetJERDnSys', NearestAK4JetJERDnSys)
        t.SetBranchAddress('SemiLeptRunNum', SemiLeptRunNum)
        t.SetBranchAddress('SemiLeptLumiNum', SemiLeptLumiNum)
        t.SetBranchAddress('SemiLeptEventNum', SemiLeptEventNum)

        t.SetBranchStatus('*', 0)
        t.SetBranchStatus('FatJetSDBDiscB', 1)
        t.SetBranchStatus('LeptonIDWeight', 1)
        t.SetBranchStatus('LeptonIDWeightUnc', 1)
        t.SetBranchStatus('EleRecoWeight', 1)
        t.SetBranchStatus('EleRecoWeightUnc', 1)
        t.SetBranchStatus('FatJetSDBDiscW', 1)
        t.SetBranchStatus('SemiLeptWeight', 1)
        t.SetBranchStatus('PUWeight', 1)
        t.SetBranchStatus('GenWeight', 1)
        t.SetBranchStatus('FatJetPt', 1)
        t.SetBranchStatus('FatJetEta', 1)
        t.SetBranchStatus('FatJetPhi', 1)
        t.SetBranchStatus('FatJetMass', 1)
        t.SetBranchStatus('FatJetMassSoftDrop', 1)
        t.SetBranchStatus('FatJetTau32', 1)
        t.SetBranchStatus('SemiLeptTrig', 1)
        t.SetBranchStatus('NearestAK4JetBDisc', 1)
        t.SetBranchStatus('NearestAK4JetPt', 1)
        t.SetBranchStatus('NearestAK4JetEta', 1)
        t.SetBranchStatus('NearestAK4JetPhi', 1)
        t.SetBranchStatus('NearestAK4JetMass', 1)
        t.SetBranchStatus('SemiLepMETpt', 1)
        t.SetBranchStatus('SemiLepMETphi', 1)
        t.SetBranchStatus('LeptonType', 1)
        t.SetBranchStatus('LeptonPt', 1)
        t.SetBranchStatus('LeptonEta', 1)
        t.SetBranchStatus('LeptonPhi', 1)
        t.SetBranchStatus('LeptonEnergy', 1)
        t.SetBranchStatus('LeptonIso', 1)
        t.SetBranchStatus('LeptonPtRel', 1)
        t.SetBranchStatus('LeptonDRMin', 1)
        t.SetBranchStatus('FatJetJECUpSys', 1)
        t.SetBranchStatus('FatJetJECDnSys', 1)
        t.SetBranchStatus('FatJetJERUpSys', 1)
        t.SetBranchStatus('FatJetJERDnSys', 1)
        t.SetBranchStatus('NearestAK4JetJECUpSys', 1)
        t.SetBranchStatus('NearestAK4JetJECDnSys', 1)
        t.SetBranchStatus('NearestAK4JetJERUpSys', 1)
        t.SetBranchStatus('NearestAK4JetJERDnSys', 1)

        entries = t.GetEntriesFast()
        print 'Processing tree ' + str(itree)
        #disc_l = [0.1522, 0.4941, 0.8001]

        eventsToRun = entries

        #eventsToRun = 100000
        count = 0
        for jentry in xrange(eventsToRun):
            triggerEfficiency = 1.
            if jentry % 100000 == 0:
                print 'processing ' + str(jentry)
                # get the next tree in the chain and verify
            ientry = t.GetEntry(jentry)
            if ientry < 0:
                break

            # Triggering: Data -- Fills histograms to calculate trigger efficiencies for electrons and muons against HT. MC -- just prints the efficiency for now.
            # Muon triggers only for now
            # 0   "HLT_Mu50",
            # 1   "HLT_Ele50_CaloIdVT_GsfTrkIdT_PFJet165",
            # 2   "HLT_Ele115_CaloIdVT_GsfTrkIdT",
            # 3   "HLT_PFHT1050"
            if isData:
                if not (SemiLeptTrig[0] or SemiLeptTrig[1] or SemiLeptTrig[2]
                        or SemiLeptTrig[3]):
                    continue
                # Check if HTPass for both electron and muon channels
                if SemiLeptTrig[3]:
                    h_passHTelBin.Fill(LeptonPt[0])
                    h_passHTmuBin.Fill(LeptonPt[0])
                # Electrons
                if (options.lepton_type == "electron" and LeptonType[0] == 11):
                    if not (SemiLeptTrig[1] == 1 or SemiLeptTrig[2] == 1):
                        continue
                    elif (SemiLeptTrig[3]):
                        h_passElHT.Fill(LeptonPt[0])
                # Muons
                elif (options.lepton_type == "muon" and LeptonType[0] == 13):
                    if not SemiLeptTrig[0]:
                        continue
                    elif SemiLeptTrig[3]:
                        h_passMuHT.Fill(LeptonPt[0])
                else:
                    continue
            else:
                if (options.lepton_type == "electron" and LeptonType[0] == 11):
                    triggerEfficiency = getTriggerEfficiency(
                        LeptonType, options.lepton_type, LeptonPt[0])
                elif (options.lepton_type == "muon" and LeptonType[0] == 13):
                    triggerEfficiency = getTriggerEfficiency(
                        LeptonType, options.lepton_type, LeptonPt[0])
                else:
                    continue

            jec_up = FatJetJECUpSys[0]
            jec_down = FatJetJECDnSys[0]
            jer_up = FatJetJERUpSys[0]
            jer_down = FatJetJERDnSys[0]

            hadTopCandP4 = ROOT.TLorentzVector()

            hadTopCandP4.SetPtEtaPhiM(FatJetPt[0], FatJetEta[0], FatJetPhi[0],
                                      FatJetMass[0])
            bJetCandP4 = ROOT.TLorentzVector()
            bJetCandP4.SetPtEtaPhiM(NearestAK4JetPt[0], NearestAK4JetEta[0],
                                    NearestAK4JetPhi[0], NearestAK4JetMass[0])
            nuCandP4 = ROOT.TLorentzVector()
            nuCandP4.SetPtEtaPhiM(SemiLepMETpt[0], 0, SemiLepMETphi[0], 0)
            theLepton = ROOT.TLorentzVector()
            theLepton.SetPtEtaPhiE(LeptonPt[0], LeptonEta[0], LeptonPhi[0],
                                   LeptonEnergy[0])  # Assume massless

            # ------------ Jet Energy Corrections+Resolution ----------------#
            hadTopCandP4_jec_up = hadTopCandP4 * jec_up
            hadTopCandP4_jec_down = hadTopCandP4 * jec_down
            hadTopCandP4_jer_up = hadTopCandP4 * jer_up
            hadTopCandP4_jer_down = hadTopCandP4 * jer_down

            bJetCandP4_jec_up = bJetCandP4 * jec_up
            bJetCandP4_jec_down = bJetCandP4 * jec_down
            bJetCandP4_jer_up = bJetCandP4 * jer_up
            bJetCandP4_jer_down = bJetCandP4 * jer_down

            theLeptonWeight = LeptonIDWeight[0]
            theLepton_WeightUp = theLeptonWeight + LeptonIDWeightUnc[0]
            theLepton_WeightDown = theLeptonWeight - LeptonIDWeightUnc[0]
            if LeptonType[0] != 13:  # If not a muon...
                theLeptonWeight = LeptonIDWeight[0] * EleRecoWeight[0]
                theLepton_WeightUp = theLeptonWeight + math.sqrt(
                    LeptonIDWeightUnc[0]**2 + EleRecoWeightUnc[0]**2)
                theLepton_WeightDown = theLeptonWeight - math.sqrt(
                    LeptonIDWeightUnc[0]**2 + EleRecoWeightUnc[0]**2)
            # -------------------------------------------------------------- #

            tau32 = FatJetTau32[0]
            mass_sd = FatJetMassSoftDrop[0]
            bdisc = NearestAK4JetBDisc[0]

            bsjet_disc = FatJetSDBDiscB[0]
            wsjet_disc = FatJetSDBDiscW[0]

            # CUTS
            passLepPt = LeptonPt[0] > 55
            passLepEta = (LeptonType[0] == 11 and abs(LeptonEta[0]) < 2.5) or (
                LeptonType[0] == 13 and abs(LeptonEta[0] < 2.4))
            passMET = SemiLepMETpt > 50
            passKin = hadTopCandP4.Perp() > 400.
            passTopTag = passKin and tau32 < 0.7 and mass_sd > 110. and mass_sd < 230 and (
                wsjet_disc > .1522 or bsjet_disc > .1522)
            passBtag = bdisc > .1522

            if not passLepPt or not passLepEta or not passMET:
                continue
            if options.bTag and not passBtag:
                continue
            if not options.bTag and passBtag:
                continue
            if options.topTag and not passTopTag:
                continue
            if not options.topTag and passTopTag:
                continue

            count += 1

            ##  ____  __.__                              __  .__         __________
            ## |    |/ _|__| ____   ____   _____ _____ _/  |_|__| ____   \______   \ ____   ____  ____
            ## |      < |  |/    \_/ __ \ /     \\__  \\   __\  |/ ___\   |       _// __ \_/ ___\/  _ \
            ## |    |  \|  |   |  \  ___/|  Y Y  \/ __ \|  | |  \  \___   |    |   \  ___/\  \__(  <_> )
            ## |____|__ \__|___|  /\___  >__|_|  (____  /__| |__|\___  >  |____|_  /\___  >\___  >____/
            ##         \/       \/     \/      \/     \/             \/          \/     \/     \/

            # Now we do our kinematic calculation based on the categories of the
            # number of top and bottom tags
            mttbar = -1.0

            lepTopCandP4 = None
            # Get the z-component of the lepton from the W mass constraint
            solution, nuz1, nuz2 = solve_nu(vlep=theLepton, vnu=nuCandP4)
            # If there is at least one real solution, pick it up
            if solution:
                nuCandP4.SetPz(nuz1)
            else:
                nuCandP4.SetPz(nuz1.real)

            lepTopCandP4 = nuCandP4 + theLepton + bJetCandP4
            ttbarCand = hadTopCandP4 + lepTopCandP4
            mttbar = ttbarCand.M()

            # ----------- Jet Energy Corrections+Resolution ------------ #
            lepTopCandP4_jec_up = nuCandP4 + theLepton + bJetCandP4_jec_up
            ttbarCand_jec_up = hadTopCandP4_jec_up + lepTopCandP4_jec_up

            lepTopCandP4_jec_down = nuCandP4 + theLepton + bJetCandP4_jec_down
            ttbarCand_jec_down = hadTopCandP4_jec_down + lepTopCandP4_jec_down

            lepTopCandP4_jer_up = nuCandP4 + theLepton + bJetCandP4_jer_up
            ttbarCand_jer_up = hadTopCandP4_jer_up + lepTopCandP4_jer_up

            lepTopCandP4_jer_down = nuCandP4 + theLepton + bJetCandP4_jer_down
            ttbarCand_jer_down = hadTopCandP4_jer_down + lepTopCandP4_jer_down

            mttbar_jec_up = ttbarCand_jec_up.M()
            mttbar_jec_down = ttbarCand_jec_down.M()
            mttbar_jer_up = ttbarCand_jer_up.M()
            mttbar_jer_down = ttbarCand_jec_down.M()
            # ---------------------------------------------------------- #

            SemiLeptWeight[0] *= triggerEfficiency * theLeptonWeight

            h_mttbar.Fill(mttbar, SemiLeptWeight[0])

            h_mttbar_jec_up.Fill(mttbar_jec_up, SemiLeptWeight[0])
            h_mttbar_jec_down.Fill(mttbar_jec_down, SemiLeptWeight[0])
            h_mttbar_jer_up.Fill(mttbar_jer_up, SemiLeptWeight[0])
            h_mttbar_jer_down.Fill(mttbar_jer_down, SemiLeptWeight[0])

            h_mtopHadGroomed.Fill(mass_sd, SemiLeptWeight[0])
            h_mtopHad.Fill(hadTopCandP4.M(), SemiLeptWeight[0])

            h_lepton_pt.Fill(theLepton.Pt(), SemiLeptWeight[0])
            h_lepton_eta.Fill(theLepton.Eta(), SemiLeptWeight[0])
            h_MET.Fill(SemiLepMETpt[0], SemiLeptWeight[0])
            h_LepTopCandP4_pt.Fill(lepTopCandP4.Pt(), SemiLeptWeight[0])
            h_LepTopCandP4_m.Fill(lepTopCandP4.M(), SemiLeptWeight[0])
            h_Compare.Fill(FatJetPt[0], LeptonDRMin[0], SemiLeptWeight[0])

            h_FatJetPt.Fill(FatJetPt[0], SemiLeptWeight[0])
            h_FatJetEta.Fill(FatJetEta[0], SemiLeptWeight[0])
            h_NearestAK4JetPt.Fill(NearestAK4JetPt[0], SemiLeptWeight[0])
            h_NearestAK4JetEta.Fill(NearestAK4JetEta[0], SemiLeptWeight[0])
            h_NearestAK4JetBDisc.Fill(NearestAK4JetBDisc[0], SemiLeptWeight[0])

            # -------- Avoid Division By Zero ---------- #

            if theLeptonWeight != 0:
                LWU = theLepton_WeightUp / theLeptonWeight
                LWD = theLepton_WeightDown / theLeptonWeight
            else:
                continue

            h_mttbar_lwu.Fill(mttbar, SemiLeptWeight[0] * LWU)
            h_mttbar_lwd.Fill(mttbar, SemiLeptWeight[0] * LWD)

        print(count)

        # Original hists have fine binning, change the bins below
        # Eff = PassHt&&PassEL(Mu)/PassHt
        h_effEl.Divide(h_passElHT, h_passHTelBin, 1, 1, "B")
        h_effMu.Divide(h_passMuHT, h_passHTmuBin, 1, 1, "B")

        # Loop over histogram bins and finds appropriate efficiency to apply
        #applyTriggerEfficiency(h_mttbar, )

    fout.cd()
    fout.Write()
    fout.Close()