Esempio n. 1
0
 def test_acoplanarity(self):
     '''test acoplanarity w/r to Patricks implementation
     '''
     ptc1 = Particle(11, -1, TLorentzVector(1, 2, 3, 7))
     ptc2 = Particle(-11, 1, TLorentzVector(2, 3, 4, 10))
     reso = Resonance(ptc1, ptc2, 23)
     self.assertEqual(reso.cross(), acop_patrick(reso.leg1(), reso.leg2()))
Esempio n. 2
0
 def test_deltaR2(self):
     """Test that the deltaR2 method properly uses either eta or
     theta depending on the collider configuration
     """
     Collider.BEAMS = 'pp'
     tlv1 = TLorentzVector()
     tlv1.SetPtEtaPhiM(10, 1.1, 0, 0)
     tlv2 = TLorentzVector()
     tlv2.SetPtEtaPhiM(10, 1.2, 0, 0)
     ptc1 = Particle(1, 1, tlv1)        
     ptc2 = Particle(1, 1, tlv2)
     dR = math.sqrt( deltaR2(ptc1, ptc2))
     self.assertAlmostEqual(dR, 0.1)
     
     Collider.BEAMS = 'ee'
     tlv1 = TLorentzVector()
     tlv1.SetPtEtaPhiM(10, 1.1, 0, 0)
     tlv1.SetTheta(1.1)
     tlv2 = TLorentzVector()
     tlv2.SetPtEtaPhiM(10, 1.2, 0, 0)
     tlv2.SetTheta(1.2)
     ptc1 = Particle(1, 1, tlv1)        
     ptc2 = Particle(1, 1, tlv2)
     dR = math.sqrt( deltaR2(ptc1, ptc2))
     self.assertAlmostEqual(dR, 0.1)
Esempio n. 3
0
 def test_printout(self):
     '''Test that the particle printout is adapted to the collider
     beams.'''
     ptc = TlvParticle(1, 1, TLorentzVector())        
     Collider.BEAMS = 'pp'
     self.assertIn('pt', ptc.__repr__())
     Collider.BEAMS = 'ee'
     self.assertIn('theta', ptc.__repr__())
     Collider.BEAMS = 'pp'
Esempio n. 4
0
 def test_printout(self):
     '''Test that the particle printout is adapted to the collider
     beams.'''
     ptc = TlvParticle(1, 1, TLorentzVector())
     Collider.BEAMS = 'pp'
     self.assertIn('pt', ptc.__repr__())
     Collider.BEAMS = 'ee'
     self.assertIn('theta', ptc.__repr__())
     Collider.BEAMS = 'pp'
Esempio n. 5
0
 def test_resonance(self):
     ptc1 = Particle(11, -1, TLorentzVector(1, 0, 0, 1))
     ptc2 = Particle(-11, 1, TLorentzVector(2, 0, 0, 2))
     reso = Resonance(ptc1, ptc2, 23)
     self.assertEqual(reso._pid, 23)
     self.assertEqual(reso.e(), 3)
     self.assertEqual(reso.leg1(), ptc1)
     self.assertEqual(reso.leg2(), ptc2)
     self.assertEqual(reso.q(), 0)
     self.assertEqual(reso.p4(), TLorentzVector(3, 0, 0, 3))
Esempio n. 6
0
    def process(self, event):
        #sqrts = self.cfg_ana.sqrts
        sqrts = 240.
        #sqrts = random.gauss(240.,240.*0.0012)
        #sqrts = random.gauss(240.,10.)
        jets = getattr(event, self.cfg_ana.input_jets)
        misenergy = getattr(event, self.cfg_ana.misenergy)
        #        solving the equation

        vis_p4 = TLorentzVector(0, 0, 0, 0)
        for jet in jets:
            vis_p4 += jet.p4()
        vis = Recoil(0, 0, vis_p4, 1)
        #         m=m_Z constrain
        a = vis.e() * sqrts / (vis.m()**2)
        b = a**2 - (sqrts**2 - 91.**2) / (vis.m()**2)

        #      sf stands for scaling factor

        if b < 0:
            sf = 1
            setattr(event, self.cfg_ana.det, 1)
        else:
            #sf2 corresponds to the solution where the missing energy is negative therefore sf is the scaling factor that makes sense from a physics pov.
            sf = a - math.sqrt(b)
            sf2 = a + math.sqrt(b)
            setattr(event, self.cfg_ana.det, 2)

######test
#visscaled1=TLorentzVector(0,0,0,0)
#visscaled2=TLorentzVector(0,0,0,0)
#for jet in jets:
#    visscaled1+=jet.p4()*sf
#    visscaled2+=jet.p4()*sf2
#cms=TLorentzVector(0,0,0,240)
#v1=cms-visscaled1
#v2=cms-visscaled2
#print v1.E(),v2.E()

        setattr(event, self.cfg_ana.scalingfac, sf)

        setattr(event, self.cfg_ana.scalingfac2, sf2)

        scale_factors = [sf] * 2
        output = []
        for jet, factor in zip(jets, scale_factors):
            # the jets should not be deepcopied
            # as they are heavy objects containing
            # in particular a list of consistuent particles
            scaled_jet = copy.copy(jet)
            scaled_jet._tlv = copy.deepcopy(jet._tlv)
            scaled_jet._tlv *= factor
            output.append(scaled_jet)

        setattr(event, self.cfg_ana.output_jets, output)
Esempio n. 7
0
def particle(pdgid, thetamin, thetamax, ptmin, ptmax, flat_pt=False):
    '''Create and return a particle in a given phase space
    
    @param pdgid: the pdg ID code
    @param thetamin: min theta
    @param thetamax: max theta
    @param ptmin: min pt
    @param ptmax: max pt
    @param flat_pt: False by default, indicating that the pt of the
       particle should be chosen from a uniform distribution between
       ptmin and ptmax. if set to true,
       the energy of the particle is drawn from a uniform distribution
       between ptmin and ptmax (then considered as Emin and Emax).
    '''
    mass, charge = particle_data[pdgid]
    theta = random.uniform(thetamin, thetamax)
    phi = random.uniform(-math.pi, math.pi)
    energy = random.uniform(ptmin, ptmax)
    costheta = math.cos(math.pi / 2. - theta)
    sintheta = math.sin(math.pi / 2. - theta)
    tantheta = sintheta / costheta
    cosphi = math.cos(phi)
    sinphi = math.sin(phi)
    if flat_pt:
        pt = energy
        momentum = pt / sintheta
        energy = math.sqrt(momentum**2 + mass**2)
    else:
        energy = max([energy, mass])
        momentum = math.sqrt(energy**2 - mass**2)
    tlv = TLorentzVector(momentum * sintheta * cosphi,
                         momentum * sintheta * sinphi, momentum * costheta,
                         energy)
    return Particle(pdgid, charge, tlv)
Esempio n. 8
0
 def process(self, event):
     initial = TLorentzVector()
     initial.SetXYZM(0, 0, 0, self.cfg_ana.sqrts)
     particles = getattr(event, self.cfg_ana.particles)
     visible_p4 = TLorentzVector()
     for ptc in particles:
         if ptc.status() > 1:  #PF cand status=0 in CMS
             raise ValueError('are you sure? status=' + str(ptc.status()))
         visible_p4 += ptc.p4()
     recoil_p4 = initial - visible_p4
     recoil = Particle(0, 0, recoil_p4)
     visible = Particle(0, 0, visible_p4)
     setattr(event, '_'.join(['recoil', self.cfg_ana.instance_label]),
             recoil)
     setattr(event,
             '_'.join(['recoil_visible',
                       self.cfg_ana.instance_label]), visible)
Esempio n. 9
0
    def test_iso1(self):
        p4 = TLorentzVector()
        p4.SetPtEtaPhiM(10, 0, 0, 0.105)
        lepton = Particle(13, 1, p4)

        p4 = TLorentzVector()
        p4.SetPtEtaPhiM(1, 0, 0, 0.105)
        ptc = Particle(211, 1, p4)

        # test iso calc
        computer = IsolationComputer([EtaPhiCircle(0.4)])
        iso = computer.compute(lepton, [ptc,ptc])
        self.assertEqual(iso.sumpt, 2*ptc.pt())
        self.assertEqual(iso.sume, 2*ptc.e())
        self.assertEqual(iso.num, 2)

        # test IsolationInfo addition
        iso2 = copy.copy(iso)
        iso2 += iso
        self.assertEqual(iso2.sumpt, 4*ptc.pt())
        self.assertEqual(iso2.sume, 4*ptc.e())
        self.assertEqual(iso2.num, 4)
       
        # test veto
        computer = IsolationComputer([EtaPhiCircle(0.4)], [EtaPhiCircle(0.1)])
        iso = computer.compute(lepton, [ptc])
        self.assertEqual(iso.sumpt, 0.)
Esempio n. 10
0
 def setUp(self):
     """maps the space with particles"""
     self.ptcs = {}
     for eta in range(-30, 30, 2):
         eta /= 10.
         for phi in range(-30, 30, 2):
             phi /= 10.
             tlv = TLorentzVector()
             tlv.SetPtEtaPhiM(10, eta, phi, 0)
             self.ptcs[(eta, phi)] = Particle(1, 0, tlv)
Esempio n. 11
0
    def corrMET2(self, jet1, jet2, met):

        metp4 = ROOT.TLorentzVector()
        px = met.p4().Px()
        py = met.p4().Py()

        if (jet1.p4().Pt() < jet2.p4().Pt()):
            pz = jet1.p4().Pz() / 2.
            e = math.sqrt(px**2 + py**2 + pz**2)
            metp4.SetPxPyPzE(px, py, pz, e)
            jetcorr1 = Particle(15, 0, jet1.p4() + metp4, 1)
            jetcorr2 = Particle(15, 0, jet2.p4(), 1)
        else:
            pz = jet2.p4().Pz() / 2.
            e = math.sqrt(px**2 + py**2 + pz**2)
            metp4.SetPxPyPzE(px, py, pz, e)
            jetcorr1 = Particle(15, 0, jet1.p4(), 1)
            jetcorr2 = Particle(15, 0, jet2.p4() + metp4, 1)
        return jetcorr1, jetcorr2
Esempio n. 12
0
    def corrMET(self, jet1, jet2, met):
        dphi1 = abs(jet1.p4().DeltaPhi(met.p4()))
        dphi2 = abs(jet2.p4().DeltaPhi(met.p4()))

        metp4 = ROOT.TLorentzVector()
        px = met.p4().Px()
        py = met.p4().Py()

        if (dphi1 < dphi2):
            pz = jet1.p4().Pz() / 2.
            e = math.sqrt(px**2 + py**2 + pz**2)
            metp4.SetPxPyPzE(px, py, pz, e)
            jetcorr1 = Particle(15, 0, jet1.p4() + metp4, 1)
            jetcorr2 = Particle(15, 0, jet2.p4(), 1)
        else:
            pz = jet2.p4().Pz() / 2.
            e = math.sqrt(px**2 + py**2 + pz**2)
            metp4.SetPxPyPzE(px, py, pz, e)
            jetcorr1 = Particle(15, 0, jet1.p4(), 1)
            jetcorr2 = Particle(15, 0, jet2.p4() + metp4, 1)
        return jetcorr1, jetcorr2
Esempio n. 13
0
 def process(self, event):
     ptcs = getattr(event, self.cfg_ana.particles)
     sump3 = TVector3()
     charge = 0
     sume = 0
     for ptc in ptcs:
         sump3 += ptc.p3()
         charge += ptc.q()
     p4 = TLorentzVector()
     p4.SetVectM(-sump3, 0)
     missing = Particle(0, charge, p4)
     setattr(event, self.instance_label, missing)
Esempio n. 14
0
 def test_many(self):
     nzeds = 100
     masses = np.linspace(80, 100, nzeds)
     boosts = np.linspace(1, 50, nzeds)
     thetas = np.linspace(1, math.pi, nzeds)
     for mass, boost, theta in zip(masses, boosts, thetas):
         energy = mass / 2.
         ptc1 = Particle(11, -1, TLorentzVector(0, energy, 0, energy))
         ptc2 = Particle(11, 1, TLorentzVector(0, -energy, 0, energy))
         resonance = Resonance(ptc1, ptc2, 23)
         p3_lab = TVector3()
         p3_lab.SetMagThetaPhi(boost, theta, 0.1)
         p4_lab = TLorentzVector()
         p4_lab.SetVectM(p3_lab, mass)
         bp4 = copy.deepcopy(resonance.p4())
         boost_vector = p4_lab.BoostVector()
         bp4.Boost(boost_vector)
         places = 8
         self.assertAlmostEqual(bp4.Vect().Mag(), boost, places)
         self.assertAlmostEqual(bp4.M(), mass, places)
         resonance.boost(boost_vector)
         self.assertAlmostEqual(bp4.E(), resonance.e(), places)
Esempio n. 15
0
 def process(self, event):
     '''Process event.
     
     The event must contain:
      - self.cfg_ana.particles: the input collection of particles.
     '''
     p4 = TLorentzVector()
     charge = 0
     pdgid = 0
     ptcs = getattr(event, self.cfg_ana.particles)
     for ptc in ptcs:
         p4 += ptc.p4()
         charge += ptc.q()
     sumptc = Particle(pdgid, charge, p4)
     setattr(event, self.cfg_ana.output, sumptc)
Esempio n. 16
0
 def process(self, event):
     p4 = TLorentzVector()
     charge = 0
     pdgid = 0
     ptcs = getattr(event, self.cfg_ana.particles)
     jet = Jet(p4)
     constituents = JetConstituents()
     for ptc in ptcs:
         p4 += ptc.p4()
         charge += ptc.q()
         constituents.append(ptc)
     sumptc = Particle(pdgid, charge, p4)
     jet = Jet(p4)
     jet.constituents = constituents
     jet.constituents.sort()
     setattr(event, self.cfg_ana.output, jet)
 def process(self, event):
     pivot = getattr(event, self.cfg_ana.pivot)[0]
     ptcs = getattr(event, self.cfg_ana.particles)
     dR = self.cfg_ana.dR
     cone_ptcs = inConeCollection(pivot, ptcs, dR, 0.)
     for ptc in cone_ptcs:
         setattr(ptc, 'dR', deltaR(pivot, ptc))
     tlv = TLorentzVector(pivot.p4())
     tlv.SetPhi(pivot.p4().Phi() + math.pi)
     tlv_theta = math.pi - pivot.p4().Theta()
     if tlv_theta >= math.pi:
         tlv_theta -= math.pi
     tlv.SetTheta(tlv_theta)
     control_pivot = Particle(pivot.pdgid(), pivot.charge(), tlv)
     control_cone_ptcs = inConeCollection(control_pivot, ptcs, dR, 0.)
     setattr(event, self.cfg_ana.output, cone_ptcs)
     setattr(event, self.cfg_ana.control_output, control_cone_ptcs)
Esempio n. 18
0
    def process(self, event):
        particles = getattr(event, self.cfg_ana.particles)
        leptons = getattr(event, self.cfg_ana.leptons)

        dressed = []
        for lepton in leptons:
            sump4 = TLorentzVector()

            for particle in particles:
                if self.cfg_ana.area.is_inside(lepton.eta(), lepton.phi(),
                                               particle.eta(), particle.phi()):
                    sump4 += particle.p4()

            sump4 += lepton.p4()
            dressed.append(Particle(lepton.pdgid(), lepton.q(), sump4))

        setattr(event, self.cfg_ana.output, dressed)
Esempio n. 19
0
 def process(self, event):
     '''Process event.
     
     The event must contain:
      - self.cfg_ana.particles: the input collection of particles.
     '''
     p4 = TLorentzVector()
     charge = 0
     pdgid = 0
     ptcs = getattr(event, self.cfg_ana.particles)
     jet = Jet(p4)
     constituents = JetConstituents()
     for ptc in ptcs:
         p4 += ptc.p4()
         charge += ptc.q()
         constituents.append(ptc)
     sumptc = Particle(pdgid, charge, p4)
     jet = Jet(p4)
     jet.constituents = constituents
     jet.constituents.sort()
     setattr(event, self.cfg_ana.output, jet)
Esempio n. 20
0
def particle(pdgid, thetamin, thetamax, ptmin, ptmax, flat_pt=False):
    mass, charge = particle_data[pdgid]
    theta = random.uniform(thetamin, thetamax)
    phi = random.uniform(-math.pi, math.pi)
    energy = random.uniform(ptmin, ptmax)
    costheta = math.cos(math.pi / 2. - theta)
    sintheta = math.sin(math.pi / 2. - theta)
    tantheta = sintheta / costheta
    cosphi = math.cos(phi)
    sinphi = math.sin(phi)
    if flat_pt:
        pt = energy
        momentum = pt / sintheta
        energy = math.sqrt(momentum**2 + mass**2)
    else:
        energy = max([energy, mass])
        momentum = math.sqrt(energy**2 - mass**2)
    tlv = TLorentzVector(momentum * sintheta * cosphi,
                         momentum * sintheta * sinphi, momentum * costheta,
                         energy)
    return Particle(pdgid, charge, tlv)
Esempio n. 21
0
    def process(self, event):
        self.tree.reset()
        jets_trk08 = getattr(event, self.cfg_ana.jets_trk08_20)
        jets_pf04 = getattr(event, self.cfg_ana.jets_pf04)
        jets_pf04_pdg = event.jets_pf04_pdg
        jets_pf08 = getattr(event, self.cfg_ana.jets_pf08_30)

        electrons = getattr(event, self.cfg_ana.electrons)
        muons = getattr(event, self.cfg_ana.muons)

        Ht = 0
        SSee = 0
        SSem = 0
        SSmm = 0
        SSeee = 0
        SSeem = 0
        SSemm = 0
        SSmmm = 0
        SS4l = 0

        # pt cut on lepton collections in analysis.py

        #####################################################################################
        ##### selection for the SSML channel
        #####################################################################################

        # flag if this is a same-sign lepton or a multilepton events

        nlep = len(electrons) + len(muons)
        if (nlep >= 2):

            if (len(electrons) > 2):
                SSeee = 1
            if (len(electrons) == 2 and len(muons) > 0):
                SSeem = 1
            if (len(electrons) == 2 and len(muons) == 0):
                if (electrons[0].pdgid() * electrons[1].pdgid() > 0):
                    SSee = 1
            if (len(electrons) == 1 and len(muons) == 1):
                if (electrons[0].pdgid() * muons[0].pdgid() > 0):
                    SSem = 1
            if (len(muons) == 2 and len(electrons) == 0):
                if (muons[0].pdgid() * muons[1].pdgid() > 0):
                    SSmm = 1
            if (len(electrons) == 1 and len(muons) > 1):
                SSemm = 1
            if (len(muons) > 2 and SSeee == 0 and SSeem == 0 and SSemm == 0):
                SSmmm = 1

#	  sum = SSeee+SSeem+SSemm+SSmmm+SSee+SSem+SSmm
#	  if (sum>1):
#	     print  'SSeee:',SSeee,' SSeem:',SSeem,' SSemm:',SSemm,' SSmmm:',SSmmm, ' SSee:',SSee,' SSem:', SSem, ' SSmm:',SSmm

            if (nlep >= 4):
                SS4l = 1

            for el in electrons:
                Ht += el.pt()

            for mu in muons:
                Ht += mu.pt()

        # number of jets and b-jets (pt cut on jet collection in analysis.py)

            njets = len(jets_pf04)
            nbjets = 0
            for jet in jets_pf04:
                Ht += jet.pt()
                if (jet.tags['bf'] > 0):
                    nbjets += 1

        # minimum number of jets and b-jets
            if (njets >= 5 and nbjets >= 2):

                self.tree.fill('numberOfLeptons', nlep)

                self.tree.fill('SSee', SSee)
                self.tree.fill('SSem', SSem)
                self.tree.fill('SSmm', SSmm)
                self.tree.fill('SSeee', SSeee)
                self.tree.fill('SSeem', SSeem)
                self.tree.fill('SSemm', SSemm)
                self.tree.fill('SSmmm', SSmmm)
                self.tree.fill('SS4l', SS4l)

                if (len(electrons) >= 2):
                    fillParticle(self.tree, 'Electron2', electrons[1])
                if (len(electrons) >= 3):
                    fillParticle(self.tree, 'Electron3', electrons[2])

                if (len(muons) >= 1):
                    fillParticle(self.tree, 'Muon1', muons[0])
                if (len(muons) >= 2):
                    fillParticle(self.tree, 'Muon2', muons[1])
                if (len(muons) >= 3):
                    fillParticle(self.tree, 'Muon3', muons[2])

                for jet in jets_pf04:
                    self.tree.fill('Jet_pt', jet.pt())

                for el in electrons:
                    self.tree.fill('Lep_pt', el.pt())

                for mu in muons:
                    self.tree.fill('Lep_pt', mu.pt())

                self.tree.fill('Ht', Ht)

                self.tree.fill('numberOfJets', njets)
                self.tree.fill('numberOfBJets', nbjets)

                self.tree.fill('weight', event.weight)
                self.tree.fill('missingET', event.met.pt())
                self.tree.fill('numberOfElectrons', len(electrons))
                self.tree.fill('numberOfMuons', len(muons))

                Jet_dR = []
                for i_jet in range(5):
                    the_Jet_dR = 999
                    j = ROOT.TLorentzVector()
                    j.SetPtEtaPhiE(jets_pf04[i_jet].pt(),
                                   jets_pf04[i_jet].eta(),
                                   jets_pf04[i_jet].phi(),
                                   jets_pf04[i_jet].e())
                    for i_e in range(len(electrons)):
                        e = ROOT.TLorentzVector()
                        e.SetPtEtaPhiE(electrons[i_e].pt(),
                                       electrons[i_e].eta(),
                                       electrons[i_e].phi(),
                                       electrons[i_e].e())
                        if j.DeltaR(e) < the_Jet_dR: the_Jet_dR = j.DeltaR(e)
                    for i_m in range(len(muons)):
                        m = ROOT.TLorentzVector()
                        m.SetPtEtaPhiE(muons[i_m].pt(), muons[i_m].eta(),
                                       muons[i_m].phi(), muons[i_m].e())
                        if j.DeltaR(m) < the_Jet_dR: the_Jet_dR = j.DeltaR(m)
                    Jet_dR.append(the_Jet_dR)
                self.tree.fill('Jet1_dR_lep', Jet_dR[0])
                self.tree.fill('Jet2_dR_lep', Jet_dR[1])
                self.tree.fill('Jet3_dR_lep', Jet_dR[2])
                self.tree.fill('Jet4_dR_lep', Jet_dR[3])
                self.tree.fill('Jet5_dR_lep', Jet_dR[4])

                pdg1 = 0
                pdg2 = 0
                pdg3 = 0
                pdg4 = 0
                pdg5 = 0
                if (jets_pf04[0].tags['bf'] > 0):
                    pdg1 = 5
                if (jets_pf04[1].tags['bf'] > 0):
                    pdg2 = 5
                if (jets_pf04[2].tags['bf'] > 0):
                    pdg3 = 5
                if (jets_pf04[3].tags['bf'] > 0):
                    pdg4 = 5
                if (jets_pf04[4].tags['bf'] > 0):
                    pdg5 = 5

                myjet1 = Particle(pdg1, 0, jets_pf04[0].p4(), 1)
                myjet2 = Particle(pdg2, 0, jets_pf04[1].p4(), 1)
                myjet3 = Particle(pdg3, 0, jets_pf04[2].p4(), 1)
                myjet4 = Particle(pdg4, 0, jets_pf04[3].p4(), 1)
                myjet5 = Particle(pdg5, 0, jets_pf04[4].p4(), 1)

                fillParticle(self.tree, 'Jet1', myjet1)
                fillParticle(self.tree, 'Jet2', myjet2)
                fillParticle(self.tree, 'Jet3', myjet3)
                fillParticle(self.tree, 'Jet4', myjet4)
                fillParticle(self.tree, 'Jet5', myjet5)

                # TRF / truth b-tagging -> need at least 2 jets_pf04
                use_DELPHES = False
                weight_0tagex = 0.
                weight_1tagex = 0.
                weight_2tagex = 0.
                weight_3tagex = 0.
                weight_4tagex = 0.
                jet = []
                ipdg = 0
                for i in range(len(jets_pf04)):
                    if use_DELPHES == True:
                        ipdg = jets_pf04[i].tags['flav']
                        if ipdg != 4 and ipdg != 5: ipdg = 0
                    else:
                        ipdg = jets_pf04_pdg[i].flavour
                    jet.append([jets_pf04[i], ipdg])
                weight_0tagex = getNbTagEx(0, jet, 4)
                weight_1tagex = getNbTagEx(1, jet, 4)
                weight_2tagex = getNbTagEx(2, jet, 4)
                weight_3tagex = getNbTagEx(3, jet, 4)
                weight_4tagex = getNbTagEx(4, jet, 4)
                self.tree.fill('weight_0tagex', weight_0tagex)
                self.tree.fill('weight_1tagex', weight_1tagex)
                self.tree.fill('weight_2tagex', weight_2tagex)
                self.tree.fill('weight_3tagex', weight_3tagex)
                self.tree.fill('weight_4tagex', weight_4tagex)

        #####################################################################################
        ##### selection for the lepton+jets channel
        #####################################################################################

        if (len(jets_trk08) >= 4 and len(jets_pf08) >= 4):
            if jets_trk08[0].pt() > 200.:
                #print len(jets_trk08), len(jets_pf08), len(electrons), len(muons)

                j1 = ROOT.TLorentzVector()
                j2 = ROOT.TLorentzVector()
                j3 = ROOT.TLorentzVector()
                j4 = ROOT.TLorentzVector()
                j1.SetPtEtaPhiE(jets_trk08[0].pt(), jets_trk08[0].eta(),
                                jets_trk08[0].phi(), jets_trk08[0].e())
                j2.SetPtEtaPhiE(jets_trk08[1].pt(), jets_trk08[1].eta(),
                                jets_trk08[1].phi(), jets_trk08[1].e())
                j3.SetPtEtaPhiE(jets_trk08[2].pt(), jets_trk08[2].eta(),
                                jets_trk08[2].phi(), jets_trk08[2].e())
                j4.SetPtEtaPhiE(jets_trk08[3].pt(), jets_trk08[3].eta(),
                                jets_trk08[3].phi(), jets_trk08[3].e())

                # compute the closest lepton/jet dR
                Jet_dR = []
                for i_jet in range(4):
                    the_Jet_dR = 999
                    j = ROOT.TLorentzVector()
                    j.SetPtEtaPhiE(jets_trk08[i_jet].pt(),
                                   jets_trk08[i_jet].eta(),
                                   jets_trk08[i_jet].phi(),
                                   jets_trk08[i_jet].e())
                    for i_e in range(len(electrons)):
                        e = ROOT.TLorentzVector()
                        e.SetPtEtaPhiE(electrons[i_e].pt(),
                                       electrons[i_e].eta(),
                                       electrons[i_e].phi(),
                                       electrons[i_e].e())
                        if j.DeltaR(e) < the_Jet_dR: the_Jet_dR = j.DeltaR(e)
                    for i_m in range(len(muons)):
                        m = ROOT.TLorentzVector()
                        m.SetPtEtaPhiE(muons[i_m].pt(), muons[i_m].eta(),
                                       muons[i_m].phi(), muons[i_m].e())
                        if j.DeltaR(m) < the_Jet_dR: the_Jet_dR = j.DeltaR(m)
                    Jet_dR.append(the_Jet_dR)
                self.tree.fill('Jet1_trk08_dR_lep', Jet_dR[0])
                self.tree.fill('Jet2_trk08_dR_lep', Jet_dR[1])
                self.tree.fill('Jet3_trk08_dR_lep', Jet_dR[2])
                self.tree.fill('Jet4_trk08_dR_lep', Jet_dR[3])

                self.tree.fill('weight', event.weight)
                self.tree.fill('missingET', event.met.pt())
                self.tree.fill('numberOfElectrons', len(electrons))
                self.tree.fill('numberOfMuons', len(muons))

                self.tree.fill('rapiditySeparation_trk08_j1j2',
                               abs(jets_trk08[0].eta() - jets_trk08[1].eta()))
                self.tree.fill('rapiditySeparation_trk08_j1j3',
                               abs(jets_trk08[0].eta() - jets_trk08[2].eta()))
                self.tree.fill('rapiditySeparation_trk08_j1j4',
                               abs(jets_trk08[0].eta() - jets_trk08[3].eta()))
                self.tree.fill('transverseMomentumAsymmetry_trk08_j1j2',
                               (jets_trk08[0].pt() - jets_trk08[1].pt()) /
                               (jets_trk08[0].pt() + jets_trk08[1].pt()))
                self.tree.fill('transverseMomentumAsymmetry_trk08_j1j3',
                               (jets_trk08[0].pt() - jets_trk08[2].pt()) /
                               (jets_trk08[0].pt() + jets_trk08[2].pt()))
                self.tree.fill('transverseMomentumAsymmetry_trk08_j1j4',
                               (jets_trk08[0].pt() - jets_trk08[3].pt()) /
                               (jets_trk08[0].pt() + jets_trk08[3].pt()))

                self.tree.fill('Jet1_trk08_tau1', jets_trk08[0].tau1)
                self.tree.fill('Jet1_trk08_tau2', jets_trk08[0].tau2)
                self.tree.fill('Jet1_trk08_tau3', jets_trk08[0].tau3)
                self.tree.fill('Jet2_trk08_tau1', jets_trk08[1].tau1)
                self.tree.fill('Jet2_trk08_tau2', jets_trk08[1].tau2)
                self.tree.fill('Jet2_trk08_tau3', jets_trk08[1].tau3)
                self.tree.fill('Jet3_trk08_tau1', jets_trk08[2].tau1)
                self.tree.fill('Jet3_trk08_tau2', jets_trk08[2].tau2)
                self.tree.fill('Jet3_trk08_tau3', jets_trk08[2].tau3)
                self.tree.fill('Jet4_trk08_tau1', jets_trk08[3].tau1)
                self.tree.fill('Jet4_trk08_tau2', jets_trk08[3].tau2)
                self.tree.fill('Jet4_trk08_tau3', jets_trk08[3].tau3)

                Jet1_trk08_tau31 = -999.0
                Jet1_trk08_tau21 = -999.0
                Jet1_trk08_tau32 = -999.0
                Jet2_trk08_tau31 = -999.0
                Jet2_trk08_tau21 = -999.0
                Jet2_trk08_tau32 = -999.0
                Jet3_trk08_tau31 = -999.0
                Jet3_trk08_tau21 = -999.0
                Jet3_trk08_tau32 = -999.0
                Jet4_trk08_tau31 = -999.0
                Jet4_trk08_tau21 = -999.0
                Jet4_trk08_tau32 = -999.0

                if (jets_trk08[0].tau1 != 0.0):
                    Jet1_trk08_tau31 = jets_trk08[0].tau3 / jets_trk08[0].tau1
                    Jet1_trk08_tau21 = jets_trk08[0].tau2 / jets_trk08[0].tau1
                if (jets_trk08[0].tau2 != 0.0):
                    Jet1_trk08_tau32 = jets_trk08[0].tau3 / jets_trk08[0].tau2

                if (jets_trk08[1].tau1 != 0.0):
                    Jet2_trk08_tau31 = jets_trk08[1].tau3 / jets_trk08[1].tau1
                    Jet2_trk08_tau21 = jets_trk08[1].tau2 / jets_trk08[1].tau1
                if (jets_trk08[1].tau2 != 0.0):
                    Jet2_trk08_tau32 = jets_trk08[1].tau3 / jets_trk08[1].tau2

                if (jets_trk08[2].tau1 != 0.0):
                    Jet3_trk08_tau31 = jets_trk08[2].tau3 / jets_trk08[2].tau1
                    Jet3_trk08_tau21 = jets_trk08[2].tau2 / jets_trk08[2].tau1
                if (jets_trk08[2].tau2 != 0.0):
                    Jet3_trk08_tau32 = jets_trk08[2].tau3 / jets_trk08[2].tau2

                if (jets_trk08[3].tau1 != 0.0):
                    Jet4_trk08_tau31 = jets_trk08[3].tau3 / jets_trk08[3].tau1
                    Jet4_trk08_tau21 = jets_trk08[3].tau2 / jets_trk08[3].tau1
                if (jets_trk08[3].tau2 != 0.0):
                    Jet4_trk08_tau32 = jets_trk08[3].tau3 / jets_trk08[3].tau2

                self.tree.fill('Jet1_trk08_tau31', Jet1_trk08_tau31)
                self.tree.fill('Jet1_trk08_tau21', Jet1_trk08_tau21)
                self.tree.fill('Jet1_trk08_tau32', Jet1_trk08_tau32)
                self.tree.fill('Jet2_trk08_tau31', Jet2_trk08_tau31)
                self.tree.fill('Jet2_trk08_tau21', Jet2_trk08_tau21)
                self.tree.fill('Jet2_trk08_tau32', Jet2_trk08_tau32)
                self.tree.fill('Jet3_trk08_tau31', Jet3_trk08_tau31)
                self.tree.fill('Jet3_trk08_tau21', Jet3_trk08_tau21)
                self.tree.fill('Jet3_trk08_tau32', Jet3_trk08_tau32)
                self.tree.fill('Jet4_trk08_tau31', Jet4_trk08_tau31)
                self.tree.fill('Jet4_trk08_tau21', Jet4_trk08_tau21)
                self.tree.fill('Jet4_trk08_tau32', Jet4_trk08_tau32)

                # here is btag, need matching in DR
                Jet1_trk08_dR_pf04 = 999
                Jet2_trk08_dR_pf04 = 999
                Jet3_trk08_dR_pf04 = 999
                Jet4_trk08_dR_pf04 = 999
                for j in jets_pf04:
                    pf04 = ROOT.TLorentzVector()
                    pf04.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                    if j.tags['bf'] > 0:
                        if pf04.DeltaR(j1) < Jet1_trk08_dR_pf04:
                            Jet1_trk08_dR_pf04 = pf04.DeltaR(j1)
                        if pf04.DeltaR(j2) < Jet2_trk08_dR_pf04:
                            Jet2_trk08_dR_pf04 = pf04.DeltaR(j2)
                        if pf04.DeltaR(j3) < Jet3_trk08_dR_pf04:
                            Jet3_trk08_dR_pf04 = pf04.DeltaR(j3)
                        if pf04.DeltaR(j4) < Jet4_trk08_dR_pf04:
                            Jet4_trk08_dR_pf04 = pf04.DeltaR(j4)

                pdg1 = 0
                pdg2 = 0
                pdg3 = 0
                pdg4 = 0
                if Jet1_trk08_dR_pf04 < 0.3:
                    pdg1 = 5
                if Jet2_trk08_dR_pf04 < 0.3:
                    pdg2 = 5
                if Jet3_trk08_dR_pf04 < 0.3:
                    pdg3 = 5
                if Jet4_trk08_dR_pf04 < 0.3:
                    pdg4 = 5

                # TRF / truth b-tagging -> need at least 2 jets_pf04
                use_DELPHES = False
                weight_0tagex = 0.
                weight_1tagex = 0.
                weight_2tagex = 0.
                weight_3tagex = 0.
                weight_4tagex = 0.
                jet = []
                ipdg = 0
                for i in range(len(jets_pf04)):
                    if use_DELPHES == True:
                        ipdg = jets_pf04[i].tags['flav']
                        if ipdg != 4 and ipdg != 5: ipdg = 0
                    else:
                        ipdg = jets_pf04_pdg[i].flavour
                    jet.append([jets_pf04[i], ipdg])
                weight_0tagex = getNbTagEx(0, jet, 4)
                weight_1tagex = getNbTagEx(1, jet, 4)
                weight_2tagex = getNbTagEx(2, jet, 4)
                weight_3tagex = getNbTagEx(3, jet, 4)
                weight_4tagex = getNbTagEx(4, jet, 4)
                self.tree.fill('weight_0tagex', weight_0tagex)
                self.tree.fill('weight_1tagex', weight_1tagex)
                self.tree.fill('weight_2tagex', weight_2tagex)
                self.tree.fill('weight_3tagex', weight_3tagex)
                self.tree.fill('weight_4tagex', weight_4tagex)

                ######################
                # trkjet08 mass info #
                ######################

                #MATCHING PF08 and trk08 for CORRECTION
                Jet1_trk08_dR_pf08 = 999
                Jet2_trk08_dR_pf08 = 999
                Jet3_trk08_dR_pf08 = 999
                Jet4_trk08_dR_pf08 = 999
                Jet1_pf08 = None
                Jet2_pf08 = None
                Jet3_pf08 = None
                Jet4_pf08 = None
                for j in jets_pf08:
                    pf08 = ROOT.TLorentzVector()
                    pf08.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                    if pf08.DeltaR(j1) < Jet1_trk08_dR_pf08:
                        Jet1_trk08_dR_pf08 = pf08.DeltaR(j1)
                        Jet1_pf08 = j
                    if pf08.DeltaR(j2) < Jet2_trk08_dR_pf08:
                        Jet2_trk08_dR_pf08 = pf08.DeltaR(j2)
                        Jet2_pf08 = j
                    if pf08.DeltaR(j3) < Jet3_trk08_dR_pf08:
                        Jet3_trk08_dR_pf08 = pf08.DeltaR(j3)
                        Jet3_pf08 = j
                    if pf08.DeltaR(j4) < Jet4_trk08_dR_pf08:
                        Jet4_trk08_dR_pf08 = pf08.DeltaR(j4)
                        Jet4_pf08 = j

                #print 'jet1 dr ',Jet1_trk08_dR_pf08,'  pf08   ',Jet1_pf08,'  trk08  ',jets_trk08[0]
                #print 'jet2 dr ',Jet2_trk08_dR_pf08,'  pf08   ',Jet2_pf08,'  trk08  ',jets_trk08[1]

                corr1 = Jet1_pf08.p4().Pt() / j1.Pt()
                corr2 = Jet2_pf08.p4().Pt() / j2.Pt()
                corr3 = Jet3_pf08.p4().Pt() / j3.Pt()
                corr4 = Jet4_pf08.p4().Pt() / j4.Pt()

                #print 'corr 1  ',corr1,'   corr2  ',corr2
                #NORMAL TRK08 SD corrected jet
                p4sd1 = ROOT.TLorentzVector()
                p4sd2 = ROOT.TLorentzVector()
                p4sd3 = ROOT.TLorentzVector()
                p4sd4 = ROOT.TLorentzVector()
                p4sd1.SetPtEtaPhiM(
                    jets_trk08[0].subjetsSoftDrop[0].p4().Pt() * corr1,
                    jets_trk08[0].eta(), jets_trk08[0].phi(),
                    jets_trk08[0].subjetsSoftDrop[0].p4().M() * corr1)

                p4sd2.SetPtEtaPhiM(
                    jets_trk08[1].subjetsSoftDrop[0].p4().Pt() * corr2,
                    jets_trk08[1].eta(), jets_trk08[1].phi(),
                    jets_trk08[1].subjetsSoftDrop[0].p4().M() * corr2)

                p4sd3.SetPtEtaPhiM(
                    jets_trk08[2].subjetsSoftDrop[0].p4().Pt() * corr3,
                    jets_trk08[2].eta(), jets_trk08[2].phi(),
                    jets_trk08[2].subjetsSoftDrop[0].p4().M() * corr3)

                p4sd4.SetPtEtaPhiM(
                    jets_trk08[3].subjetsSoftDrop[0].p4().Pt() * corr4,
                    jets_trk08[3].eta(), jets_trk08[3].phi(),
                    jets_trk08[3].subjetsSoftDrop[0].p4().M() * corr4)

                sdjet1_corr = Particle(pdg1, 0, p4sd1, 1)
                sdjet2_corr = Particle(pdg2, 0, p4sd2, 1)
                sdjet3_corr = Particle(pdg3, 0, p4sd3, 1)
                sdjet4_corr = Particle(pdg4, 0, p4sd4, 1)
                fillParticle(self.tree, 'Jet1_trk08_SD_Corr', sdjet1_corr)
                fillParticle(self.tree, 'Jet2_trk08_SD_Corr', sdjet2_corr)
                fillParticle(self.tree, 'Jet3_trk08_SD_Corr', sdjet3_corr)
                fillParticle(self.tree, 'Jet4_trk08_SD_Corr', sdjet4_corr)

                #NORMAL TRK08 SD jet
                #sdjet1 = Particle(pdg1, 0, jets_trk08[0].subjetsSoftDrop[0].p4(), 1)
                #sdjet2 = Particle(pdg2, 0, jets_trk08[1].subjetsSoftDrop[0].p4(), 1)
                #fillParticle(self.tree, 'Jet1_trk08_SD', sdjet1)
                #fillParticle(self.tree, 'Jet2_trk08_SD', sdjet2)

                #CORRECTED TRK08 jet
                p4jet1_corr = ROOT.TLorentzVector()
                p4jet2_corr = ROOT.TLorentzVector()
                p4jet3_corr = ROOT.TLorentzVector()
                p4jet4_corr = ROOT.TLorentzVector()
                p4jet1_corr.SetPtEtaPhiM(jets_trk08[0].pt() * corr1,
                                         jets_trk08[0].eta(),
                                         jets_trk08[0].phi(),
                                         jets_trk08[0].m() * corr1)
                p4jet2_corr.SetPtEtaPhiM(jets_trk08[1].pt() * corr2,
                                         jets_trk08[1].eta(),
                                         jets_trk08[1].phi(),
                                         jets_trk08[1].m() * corr2)
                p4jet3_corr.SetPtEtaPhiM(jets_trk08[2].pt() * corr3,
                                         jets_trk08[2].eta(),
                                         jets_trk08[2].phi(),
                                         jets_trk08[2].m() * corr3)
                p4jet4_corr.SetPtEtaPhiM(jets_trk08[3].pt() * corr4,
                                         jets_trk08[3].eta(),
                                         jets_trk08[3].phi(),
                                         jets_trk08[3].m() * corr4)

                jet1_corr = Particle(pdg1, 0, p4jet1_corr, 1)
                jet2_corr = Particle(pdg2, 0, p4jet2_corr, 1)
                jet3_corr = Particle(pdg3, 0, p4jet3_corr, 1)
                jet4_corr = Particle(pdg4, 0, p4jet4_corr, 1)
                #fillParticle(self.tree, 'Jet1_trk08_Corr', jet1_corr)
                #fillParticle(self.tree, 'Jet2_trk08_Corr', jet2_corr)

                # associate MET to one jet or another based on softdrop
                sdjetmet1, sdjetmet2, sdjetmet3, sdjetmet4 = self.corrMET(
                    jets_trk08[0].subjetsSoftDrop[0], pdg1,
                    jets_trk08[1].subjetsSoftDrop[0], pdg2,
                    jets_trk08[2].subjetsSoftDrop[0], pdg3,
                    jets_trk08[3].subjetsSoftDrop[0], pdg4, event.met)
                fillParticle(self.tree, 'Jet1_trk08_SD_MetCorr', sdjetmet1)
                fillParticle(self.tree, 'Jet2_trk08_SD_MetCorr', sdjetmet2)
                fillParticle(self.tree, 'Jet3_trk08_SD_MetCorr', sdjetmet3)
                fillParticle(self.tree, 'Jet4_trk08_SD_MetCorr', sdjetmet4)

                sdjetmet1, sdjetmet2, sdjetmet3, sdjetmet4 = self.corrMET(
                    sdjet1_corr, pdg1, sdjet2_corr, pdg2, sdjet3_corr, pdg3,
                    sdjet4_corr, pdg4, event.met)
                fillParticle(self.tree, 'Jet1_trk08_SD_Corr_MetCorr',
                             sdjetmet1)
                fillParticle(self.tree, 'Jet2_trk08_SD_Corr_MetCorr',
                             sdjetmet2)
                fillParticle(self.tree, 'Jet3_trk08_SD_Corr_MetCorr',
                             sdjetmet3)
                fillParticle(self.tree, 'Jet4_trk08_SD_Corr_MetCorr',
                             sdjetmet4)

                # form masses
                self.tree.fill(
                    'Mj1j2j3j4_trk08',
                    self.fillMass(jets_trk08[0], jets_trk08[1], jets_trk08[2],
                                  jets_trk08[3]))
                self.tree.fill(
                    'Mj1j2j3j4_trk08_Corr',
                    self.fillMass(jet1_corr, jet2_corr, jet3_corr, jet4_corr))
                jetmet1, jetmet2, jetmet3, jetmet4 = self.corrMET(
                    jets_trk08[0], pdg1, jets_trk08[1], pdg2, jets_trk08[2],
                    pdg3, jets_trk08[3], pdg4, event.met)
                self.tree.fill(
                    'Mj1j2j3j4_trk08_MetCorr',
                    self.fillMass(jetmet1, jetmet2, jetmet3, jetmet4))
                #fillParticle(self.tree, 'Jet1_trk08_MetCorr', jetmet1)

                jetmet1, jetmet2, jetmet3, jetmet4 = self.corrMET(
                    jet1_corr, pdg1, jet2_corr, pdg2, jet3_corr, pdg3,
                    jet4_corr, pdg4, event.met)
                self.tree.fill(
                    'Mj1j2j3j4_trk08_Corr_MetCorr',
                    self.fillMass(jetmet1, jetmet2, jetmet3, jetmet4))
                fillParticle(self.tree, 'Jet1_trk08_Corr_MetCorr', jetmet1)
                fillParticle(self.tree, 'Jet2_trk08_Corr_MetCorr', jetmet2)
                fillParticle(self.tree, 'Jet3_trk08_Corr_MetCorr', jetmet3)
                fillParticle(self.tree, 'Jet4_trk08_Corr_MetCorr', jetmet4)

                self.tree.fill(
                    'Mj1j2j3j4_pf08',
                    self.fillMass(jets_pf08[0], jets_pf08[1], jets_pf08[2],
                                  jets_pf08[3]))
                jetmet1, jetmet2, jetmet3, jetmet4 = self.corrMET(
                    jets_pf08[0], pdg1, jets_pf08[1], pdg2, jets_pf08[2], pdg3,
                    jets_pf08[3], pdg4, event.met)
                self.tree.fill(
                    'Mj1j2j3j4_pf08_MetCorr',
                    self.fillMass(jetmet1, jetmet2, jetmet3, jetmet4))

                if (len(electrons) >= 1):
                    fillParticle(self.tree, 'Electron1', electrons[0])
                if (len(electrons) >= 2):
                    fillParticle(self.tree, 'Electron2', electrons[1])
                if (len(electrons) >= 3):
                    fillParticle(self.tree, 'Electron3', electrons[2])

                if (len(muons) >= 1):
                    fillParticle(self.tree, 'Muon1', muons[0])
                if (len(muons) >= 2):
                    fillParticle(self.tree, 'Muon2', muons[1])
                if (len(muons) >= 3):
                    fillParticle(self.tree, 'Muon3', muons[2])

        self.tree.tree.Fill()
Esempio n. 22
0
    def corrMET(self, jet1, pdg1, jet2, pdg2, jet3, pdg3, jet4, pdg4, met):
        dphi1 = abs(jet1.p4().DeltaPhi(met.p4()))
        dphi2 = abs(jet2.p4().DeltaPhi(met.p4()))
        dphi3 = abs(jet3.p4().DeltaPhi(met.p4()))
        dphi4 = abs(jet4.p4().DeltaPhi(met.p4()))

        metp4 = ROOT.TLorentzVector()
        px = met.p4().Px()
        py = met.p4().Py()

        dphi_min = min(dphi1, dphi2, dphi3, dphi4)
        if (dphi_min == dphi1):
            pz = jet1.p4().Pz() / 2.
            e = math.sqrt(px**2 + py**2 + pz**2)
            metp4.SetPxPyPzE(px, py, pz, e)
            jetcorr1 = Particle(pdg1, 0, jet1.p4() + metp4, 1)
            jetcorr2 = Particle(pdg2, 0, jet2.p4(), 1)
            jetcorr3 = Particle(pdg3, 0, jet3.p4(), 1)
            jetcorr4 = Particle(pdg4, 0, jet4.p4(), 1)
        elif (dphi_min == dphi2):
            pz = jet2.p4().Pz() / 2.
            e = math.sqrt(px**2 + py**2 + pz**2)
            metp4.SetPxPyPzE(px, py, pz, e)
            jetcorr1 = Particle(pdg1, 0, jet1.p4(), 1)
            jetcorr2 = Particle(pdg2, 0, jet2.p4() + metp4, 1)
            jetcorr3 = Particle(pdg3, 0, jet3.p4(), 1)
            jetcorr4 = Particle(pdg4, 0, jet4.p4(), 1)
        elif (dphi_min == dphi3):
            pz = jet3.p4().Pz() / 2.
            e = math.sqrt(px**2 + py**2 + pz**2)
            metp4.SetPxPyPzE(px, py, pz, e)
            jetcorr1 = Particle(pdg1, 0, jet1.p4(), 1)
            jetcorr2 = Particle(pdg2, 0, jet2.p4(), 1)
            jetcorr3 = Particle(pdg3, 0, jet3.p4() + metp4, 1)
            jetcorr4 = Particle(pdg4, 0, jet4.p4(), 1)
        else:
            pz = jet4.p4().Pz() / 2.
            e = math.sqrt(px**2 + py**2 + pz**2)
            metp4.SetPxPyPzE(px, py, pz, e)
            jetcorr1 = Particle(pdg1, 0, jet1.p4(), 1)
            jetcorr2 = Particle(pdg2, 0, jet2.p4(), 1)
            jetcorr3 = Particle(pdg3, 0, jet3.p4(), 1)
            jetcorr4 = Particle(pdg4, 0, jet4.p4() + metp4, 1)
        return jetcorr1, jetcorr2, jetcorr3, jetcorr4
Esempio n. 23
0
    def process(self, event):
        self.tree.reset()
        jets_trk08 = getattr(event, self.cfg_ana.jets_trk08_20)
        jets_pf04 = getattr(event, self.cfg_ana.jets_pf04_20)
        jets_pf04_pdg = event.jets_pf04_20_pdg
        jets_pf08 = getattr(event, self.cfg_ana.jets_pf08_30)

        electrons = getattr(event, self.cfg_ana.electrons)
        muons = getattr(event, self.cfg_ana.muons)

        if (len(jets_trk08) >= 4 and len(jets_pf08) >= 4):
            if jets_trk08[0].pt() > 200.:
                #print len(jets_trk08), len(jets_pf08), len(electrons), len(muons)

                j1 = ROOT.TLorentzVector()
                j2 = ROOT.TLorentzVector()
                j3 = ROOT.TLorentzVector()
                j4 = ROOT.TLorentzVector()
                j1.SetPtEtaPhiE(jets_trk08[0].pt(), jets_trk08[0].eta(),
                                jets_trk08[0].phi(), jets_trk08[0].e())
                j2.SetPtEtaPhiE(jets_trk08[1].pt(), jets_trk08[1].eta(),
                                jets_trk08[1].phi(), jets_trk08[1].e())
                j3.SetPtEtaPhiE(jets_trk08[2].pt(), jets_trk08[2].eta(),
                                jets_trk08[2].phi(), jets_trk08[2].e())
                j4.SetPtEtaPhiE(jets_trk08[3].pt(), jets_trk08[3].eta(),
                                jets_trk08[3].phi(), jets_trk08[3].e())

                # compute the closest lepton/jet dR
                Jet_dR = []
                for i_jet in range(4):
                    the_Jet_dR = 999
                    j = ROOT.TLorentzVector()
                    j.SetPtEtaPhiE(jets_trk08[i_jet].pt(),
                                   jets_trk08[i_jet].eta(),
                                   jets_trk08[i_jet].phi(),
                                   jets_trk08[i_jet].e())
                    for i_e in range(len(electrons)):
                        e = ROOT.TLorentzVector()
                        e.SetPtEtaPhiE(electrons[i_e].pt(),
                                       electrons[i_e].eta(),
                                       electrons[i_e].phi(),
                                       electrons[i_e].e())
                        if j.DeltaR(e) < the_Jet_dR: the_Jet_dR = j.DeltaR(e)
                    for i_m in range(len(muons)):
                        m = ROOT.TLorentzVector()
                        m.SetPtEtaPhiE(muons[i_m].pt(), muons[i_m].eta(),
                                       muons[i_m].phi(), muons[i_m].e())
                        if j.DeltaR(m) < the_Jet_dR: the_Jet_dR = j.DeltaR(m)
                    Jet_dR.append(the_Jet_dR)
                self.tree.fill('Jet1_trk08_dR_lep', Jet_dR[0])
                self.tree.fill('Jet2_trk08_dR_lep', Jet_dR[1])
                self.tree.fill('Jet3_trk08_dR_lep', Jet_dR[2])
                self.tree.fill('Jet4_trk08_dR_lep', Jet_dR[3])

                self.tree.fill('weight', event.weight)
                self.tree.fill('missingET', event.met.pt())
                self.tree.fill('numberOfElectrons', len(electrons))
                self.tree.fill('numberOfMuons', len(muons))

                self.tree.fill('rapiditySeparation_trk08_j1j2',
                               abs(jets_trk08[0].eta() - jets_trk08[1].eta()))
                self.tree.fill('rapiditySeparation_trk08_j1j3',
                               abs(jets_trk08[0].eta() - jets_trk08[2].eta()))
                self.tree.fill('rapiditySeparation_trk08_j1j4',
                               abs(jets_trk08[0].eta() - jets_trk08[3].eta()))
                self.tree.fill('transverseMomentumAsymmetry_trk08_j1j2',
                               (jets_trk08[0].pt() - jets_trk08[1].pt()) /
                               (jets_trk08[0].pt() + jets_trk08[1].pt()))
                self.tree.fill('transverseMomentumAsymmetry_trk08_j1j3',
                               (jets_trk08[0].pt() - jets_trk08[2].pt()) /
                               (jets_trk08[0].pt() + jets_trk08[2].pt()))
                self.tree.fill('transverseMomentumAsymmetry_trk08_j1j4',
                               (jets_trk08[0].pt() - jets_trk08[3].pt()) /
                               (jets_trk08[0].pt() + jets_trk08[3].pt()))

                self.tree.fill('Jet1_trk08_tau1', jets_trk08[0].tau1)
                self.tree.fill('Jet1_trk08_tau2', jets_trk08[0].tau2)
                self.tree.fill('Jet1_trk08_tau3', jets_trk08[0].tau3)
                self.tree.fill('Jet2_trk08_tau1', jets_trk08[1].tau1)
                self.tree.fill('Jet2_trk08_tau2', jets_trk08[1].tau2)
                self.tree.fill('Jet2_trk08_tau3', jets_trk08[1].tau3)
                self.tree.fill('Jet3_trk08_tau1', jets_trk08[2].tau1)
                self.tree.fill('Jet3_trk08_tau2', jets_trk08[2].tau2)
                self.tree.fill('Jet3_trk08_tau3', jets_trk08[2].tau3)
                self.tree.fill('Jet4_trk08_tau1', jets_trk08[3].tau1)
                self.tree.fill('Jet4_trk08_tau2', jets_trk08[3].tau2)
                self.tree.fill('Jet4_trk08_tau3', jets_trk08[3].tau3)

                Jet1_trk08_tau31 = -999.0
                Jet1_trk08_tau21 = -999.0
                Jet1_trk08_tau32 = -999.0
                Jet2_trk08_tau31 = -999.0
                Jet2_trk08_tau21 = -999.0
                Jet2_trk08_tau32 = -999.0
                Jet3_trk08_tau31 = -999.0
                Jet3_trk08_tau21 = -999.0
                Jet3_trk08_tau32 = -999.0
                Jet4_trk08_tau31 = -999.0
                Jet4_trk08_tau21 = -999.0
                Jet4_trk08_tau32 = -999.0

                if (jets_trk08[0].tau1 != 0.0):
                    Jet1_trk08_tau31 = jets_trk08[0].tau3 / jets_trk08[0].tau1
                    Jet1_trk08_tau21 = jets_trk08[0].tau2 / jets_trk08[0].tau1
                if (jets_trk08[0].tau2 != 0.0):
                    Jet1_trk08_tau32 = jets_trk08[0].tau3 / jets_trk08[0].tau2

                if (jets_trk08[1].tau1 != 0.0):
                    Jet2_trk08_tau31 = jets_trk08[1].tau3 / jets_trk08[1].tau1
                    Jet2_trk08_tau21 = jets_trk08[1].tau2 / jets_trk08[1].tau1
                if (jets_trk08[1].tau2 != 0.0):
                    Jet2_trk08_tau32 = jets_trk08[1].tau3 / jets_trk08[1].tau2

                if (jets_trk08[2].tau1 != 0.0):
                    Jet3_trk08_tau31 = jets_trk08[2].tau3 / jets_trk08[2].tau1
                    Jet3_trk08_tau21 = jets_trk08[2].tau2 / jets_trk08[2].tau1
                if (jets_trk08[2].tau2 != 0.0):
                    Jet3_trk08_tau32 = jets_trk08[2].tau3 / jets_trk08[2].tau2

                if (jets_trk08[3].tau1 != 0.0):
                    Jet4_trk08_tau31 = jets_trk08[3].tau3 / jets_trk08[3].tau1
                    Jet4_trk08_tau21 = jets_trk08[3].tau2 / jets_trk08[3].tau1
                if (jets_trk08[3].tau2 != 0.0):
                    Jet4_trk08_tau32 = jets_trk08[3].tau3 / jets_trk08[3].tau2

                self.tree.fill('Jet1_trk08_tau31', Jet1_trk08_tau31)
                self.tree.fill('Jet1_trk08_tau21', Jet1_trk08_tau21)
                self.tree.fill('Jet1_trk08_tau32', Jet1_trk08_tau32)
                self.tree.fill('Jet2_trk08_tau31', Jet2_trk08_tau31)
                self.tree.fill('Jet2_trk08_tau21', Jet2_trk08_tau21)
                self.tree.fill('Jet2_trk08_tau32', Jet2_trk08_tau32)
                self.tree.fill('Jet3_trk08_tau31', Jet3_trk08_tau31)
                self.tree.fill('Jet3_trk08_tau21', Jet3_trk08_tau21)
                self.tree.fill('Jet3_trk08_tau32', Jet3_trk08_tau32)
                self.tree.fill('Jet4_trk08_tau31', Jet4_trk08_tau31)
                self.tree.fill('Jet4_trk08_tau21', Jet4_trk08_tau21)
                self.tree.fill('Jet4_trk08_tau32', Jet4_trk08_tau32)

                # here is btag, need matching in DR
                Jet1_trk08_dR_pf04 = 999
                Jet2_trk08_dR_pf04 = 999
                Jet3_trk08_dR_pf04 = 999
                Jet4_trk08_dR_pf04 = 999
                for j in jets_pf04:
                    pf04 = ROOT.TLorentzVector()
                    pf04.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                    if j.tags['bf'] > 0:
                        if pf04.DeltaR(j1) < Jet1_trk08_dR_pf04:
                            Jet1_trk08_dR_pf04 = pf04.DeltaR(j1)
                        if pf04.DeltaR(j2) < Jet2_trk08_dR_pf04:
                            Jet2_trk08_dR_pf04 = pf04.DeltaR(j2)
                        if pf04.DeltaR(j3) < Jet3_trk08_dR_pf04:
                            Jet3_trk08_dR_pf04 = pf04.DeltaR(j3)
                        if pf04.DeltaR(j4) < Jet4_trk08_dR_pf04:
                            Jet4_trk08_dR_pf04 = pf04.DeltaR(j4)

                pdg1 = 0
                pdg2 = 0
                pdg3 = 0
                pdg4 = 0
                if Jet1_trk08_dR_pf04 < 0.3:
                    pdg1 = 5
                if Jet2_trk08_dR_pf04 < 0.3:
                    pdg2 = 5
                if Jet3_trk08_dR_pf04 < 0.3:
                    pdg3 = 5
                if Jet4_trk08_dR_pf04 < 0.3:
                    pdg4 = 5

                # TRF / truth b-tagging -> need at least 2 jets_pf04
                use_DELPHES = False
                weight_0tagex = 0.
                weight_1tagex = 0.
                weight_2tagex = 0.
                weight_3tagex = 0.
                weight_4tagex = 0.
                jet = []
                ipdg = 0
                for i in range(len(jets_pf04)):
                    if use_DELPHES == True:
                        ipdg = jets_pf04[i].tags['flav']
                        if ipdg != 4 and ipdg != 5: ipdg = 0
                    else:
                        ipdg = jets_pf04_pdg[i].flavour
                    jet.append([jets_pf04[i], ipdg])
                weight_0tagex = getNbTagEx(0, jet, 4)
                weight_1tagex = getNbTagEx(1, jet, 4)
                weight_2tagex = getNbTagEx(2, jet, 4)
                weight_3tagex = getNbTagEx(3, jet, 4)
                weight_4tagex = getNbTagEx(4, jet, 4)
                self.tree.fill('weight_0tagex', weight_0tagex)
                self.tree.fill('weight_1tagex', weight_1tagex)
                self.tree.fill('weight_2tagex', weight_2tagex)
                self.tree.fill('weight_3tagex', weight_3tagex)
                self.tree.fill('weight_4tagex', weight_4tagex)

                ######################
                # trkjet08 mass info #
                ######################

                #MATCHING PF08 and trk08 for CORRECTION
                Jet1_trk08_dR_pf08 = 999
                Jet2_trk08_dR_pf08 = 999
                Jet3_trk08_dR_pf08 = 999
                Jet4_trk08_dR_pf08 = 999
                Jet1_pf08 = None
                Jet2_pf08 = None
                Jet3_pf08 = None
                Jet4_pf08 = None
                for j in jets_pf08:
                    pf08 = ROOT.TLorentzVector()
                    pf08.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                    if pf08.DeltaR(j1) < Jet1_trk08_dR_pf08:
                        Jet1_trk08_dR_pf08 = pf08.DeltaR(j1)
                        Jet1_pf08 = j
                    if pf08.DeltaR(j2) < Jet2_trk08_dR_pf08:
                        Jet2_trk08_dR_pf08 = pf08.DeltaR(j2)
                        Jet2_pf08 = j
                    if pf08.DeltaR(j3) < Jet3_trk08_dR_pf08:
                        Jet3_trk08_dR_pf08 = pf08.DeltaR(j3)
                        Jet3_pf08 = j
                    if pf08.DeltaR(j4) < Jet4_trk08_dR_pf08:
                        Jet4_trk08_dR_pf08 = pf08.DeltaR(j4)
                        Jet4_pf08 = j

                #print 'jet1 dr ',Jet1_trk08_dR_pf08,'  pf08   ',Jet1_pf08,'  trk08  ',jets_trk08[0]
                #print 'jet2 dr ',Jet2_trk08_dR_pf08,'  pf08   ',Jet2_pf08,'  trk08  ',jets_trk08[1]

                corr1 = Jet1_pf08.p4().Pt() / j1.Pt()
                corr2 = Jet2_pf08.p4().Pt() / j2.Pt()
                corr3 = Jet3_pf08.p4().Pt() / j3.Pt()
                corr4 = Jet4_pf08.p4().Pt() / j4.Pt()

                #print 'corr 1  ',corr1,'   corr2  ',corr2
                #NORMAL TRK08 SD corrected jet
                p4sd1 = ROOT.TLorentzVector()
                p4sd2 = ROOT.TLorentzVector()
                p4sd3 = ROOT.TLorentzVector()
                p4sd4 = ROOT.TLorentzVector()
                p4sd1.SetPtEtaPhiM(
                    jets_trk08[0].subjetsSoftDrop[0].p4().Pt() * corr1,
                    jets_trk08[0].eta(), jets_trk08[0].phi(),
                    jets_trk08[0].subjetsSoftDrop[0].p4().M() * corr1)

                p4sd2.SetPtEtaPhiM(
                    jets_trk08[1].subjetsSoftDrop[0].p4().Pt() * corr2,
                    jets_trk08[1].eta(), jets_trk08[1].phi(),
                    jets_trk08[1].subjetsSoftDrop[0].p4().M() * corr2)

                p4sd3.SetPtEtaPhiM(
                    jets_trk08[2].subjetsSoftDrop[0].p4().Pt() * corr3,
                    jets_trk08[2].eta(), jets_trk08[2].phi(),
                    jets_trk08[2].subjetsSoftDrop[0].p4().M() * corr3)

                p4sd4.SetPtEtaPhiM(
                    jets_trk08[3].subjetsSoftDrop[0].p4().Pt() * corr4,
                    jets_trk08[3].eta(), jets_trk08[3].phi(),
                    jets_trk08[3].subjetsSoftDrop[0].p4().M() * corr4)

                sdjet1_corr = Particle(pdg1, 0, p4sd1, 1)
                sdjet2_corr = Particle(pdg2, 0, p4sd2, 1)
                sdjet3_corr = Particle(pdg3, 0, p4sd3, 1)
                sdjet4_corr = Particle(pdg4, 0, p4sd4, 1)
                fillParticle(self.tree, 'Jet1_trk08_SD_Corr', sdjet1_corr)
                fillParticle(self.tree, 'Jet2_trk08_SD_Corr', sdjet2_corr)
                fillParticle(self.tree, 'Jet3_trk08_SD_Corr', sdjet3_corr)
                fillParticle(self.tree, 'Jet4_trk08_SD_Corr', sdjet4_corr)

                #NORMAL TRK08 SD jet
                #sdjet1 = Particle(pdg1, 0, jets_trk08[0].subjetsSoftDrop[0].p4(), 1)
                #sdjet2 = Particle(pdg2, 0, jets_trk08[1].subjetsSoftDrop[0].p4(), 1)
                #fillParticle(self.tree, 'Jet1_trk08_SD', sdjet1)
                #fillParticle(self.tree, 'Jet2_trk08_SD', sdjet2)

                #CORRECTED TRK08 jet
                p4jet1_corr = ROOT.TLorentzVector()
                p4jet2_corr = ROOT.TLorentzVector()
                p4jet3_corr = ROOT.TLorentzVector()
                p4jet4_corr = ROOT.TLorentzVector()
                p4jet1_corr.SetPtEtaPhiM(jets_trk08[0].pt() * corr1,
                                         jets_trk08[0].eta(),
                                         jets_trk08[0].phi(),
                                         jets_trk08[0].m() * corr1)
                p4jet2_corr.SetPtEtaPhiM(jets_trk08[1].pt() * corr2,
                                         jets_trk08[1].eta(),
                                         jets_trk08[1].phi(),
                                         jets_trk08[1].m() * corr2)
                p4jet3_corr.SetPtEtaPhiM(jets_trk08[2].pt() * corr3,
                                         jets_trk08[2].eta(),
                                         jets_trk08[2].phi(),
                                         jets_trk08[2].m() * corr3)
                p4jet4_corr.SetPtEtaPhiM(jets_trk08[3].pt() * corr4,
                                         jets_trk08[3].eta(),
                                         jets_trk08[3].phi(),
                                         jets_trk08[3].m() * corr4)

                jet1_corr = Particle(pdg1, 0, p4jet1_corr, 1)
                jet2_corr = Particle(pdg2, 0, p4jet2_corr, 1)
                jet3_corr = Particle(pdg3, 0, p4jet3_corr, 1)
                jet4_corr = Particle(pdg4, 0, p4jet4_corr, 1)
                #fillParticle(self.tree, 'Jet1_trk08_Corr', jet1_corr)
                #fillParticle(self.tree, 'Jet2_trk08_Corr', jet2_corr)

                # associate MET to one jet or another based on softdrop
                sdjetmet1, sdjetmet2, sdjetmet3, sdjetmet4 = self.corrMET(
                    jets_trk08[0].subjetsSoftDrop[0], pdg1,
                    jets_trk08[1].subjetsSoftDrop[0], pdg2,
                    jets_trk08[2].subjetsSoftDrop[0], pdg3,
                    jets_trk08[3].subjetsSoftDrop[0], pdg4, event.met)
                fillParticle(self.tree, 'Jet1_trk08_SD_MetCorr', sdjetmet1)
                fillParticle(self.tree, 'Jet2_trk08_SD_MetCorr', sdjetmet2)
                fillParticle(self.tree, 'Jet3_trk08_SD_MetCorr', sdjetmet3)
                fillParticle(self.tree, 'Jet4_trk08_SD_MetCorr', sdjetmet4)

                sdjetmet1, sdjetmet2, sdjetmet3, sdjetmet4 = self.corrMET(
                    sdjet1_corr, pdg1, sdjet2_corr, pdg2, sdjet3_corr, pdg3,
                    sdjet4_corr, pdg4, event.met)
                fillParticle(self.tree, 'Jet1_trk08_SD_Corr_MetCorr',
                             sdjetmet1)
                fillParticle(self.tree, 'Jet2_trk08_SD_Corr_MetCorr',
                             sdjetmet2)
                fillParticle(self.tree, 'Jet3_trk08_SD_Corr_MetCorr',
                             sdjetmet3)
                fillParticle(self.tree, 'Jet4_trk08_SD_Corr_MetCorr',
                             sdjetmet4)

                # form masses
                self.tree.fill(
                    'Mj1j2j3j4_trk08',
                    self.fillMass(jets_trk08[0], jets_trk08[1], jets_trk08[2],
                                  jets_trk08[3]))
                self.tree.fill(
                    'Mj1j2j3j4_trk08_Corr',
                    self.fillMass(jet1_corr, jet2_corr, jet3_corr, jet4_corr))
                jetmet1, jetmet2, jetmet3, jetmet4 = self.corrMET(
                    jets_trk08[0], pdg1, jets_trk08[1], pdg2, jets_trk08[2],
                    pdg3, jets_trk08[3], pdg4, event.met)
                self.tree.fill(
                    'Mj1j2j3j4_trk08_MetCorr',
                    self.fillMass(jetmet1, jetmet2, jetmet3, jetmet4))
                #fillParticle(self.tree, 'Jet1_trk08_MetCorr', jetmet1)

                jetmet1, jetmet2, jetmet3, jetmet4 = self.corrMET(
                    jet1_corr, pdg1, jet2_corr, pdg2, jet3_corr, pdg3,
                    jet4_corr, pdg4, event.met)
                self.tree.fill(
                    'Mj1j2j3j4_trk08_Corr_MetCorr',
                    self.fillMass(jetmet1, jetmet2, jetmet3, jetmet4))
                fillParticle(self.tree, 'Jet1_trk08_Corr_MetCorr', jetmet1)
                fillParticle(self.tree, 'Jet2_trk08_Corr_MetCorr', jetmet2)
                fillParticle(self.tree, 'Jet3_trk08_Corr_MetCorr', jetmet3)
                fillParticle(self.tree, 'Jet4_trk08_Corr_MetCorr', jetmet4)

                self.tree.fill(
                    'Mj1j2j3j4_pf08',
                    self.fillMass(jets_pf08[0], jets_pf08[1], jets_pf08[2],
                                  jets_pf08[3]))
                jetmet1, jetmet2, jetmet3, jetmet4 = self.corrMET(
                    jets_pf08[0], pdg1, jets_pf08[1], pdg2, jets_pf08[2], pdg3,
                    jets_pf08[3], pdg4, event.met)
                self.tree.fill(
                    'Mj1j2j3j4_pf08_MetCorr',
                    self.fillMass(jetmet1, jetmet2, jetmet3, jetmet4))

                if (len(electrons) >= 1):
                    fillParticle(self.tree, 'Electron1', electrons[0])
                if (len(electrons) >= 2):
                    fillParticle(self.tree, 'Electron2', electrons[1])
                if (len(electrons) >= 3):
                    fillParticle(self.tree, 'Electron3', electrons[2])

                if (len(muons) >= 1):
                    fillParticle(self.tree, 'Muon1', muons[0])
                if (len(muons) >= 2):
                    fillParticle(self.tree, 'Muon2', muons[1])
                if (len(muons) >= 3):
                    fillParticle(self.tree, 'Muon3', muons[2])

                self.tree.tree.Fill()
Esempio n. 24
0
    def process(self, event):
        self.tree.reset()

        jets_trk02 = getattr(event, self.cfg_ana.jets_trk02_1000)
        jets_pf02 = getattr(event, self.cfg_ana.jets_pf02_1500)
        jets_pf04 = getattr(event, self.cfg_ana.jets_pf04_1000)
        jets_pf08 = getattr(event, self.cfg_ana.jets_pf08_1500)

        jets_pf04_1500 = getattr(event, self.cfg_ana.jets_pf04_1500)
        jets_trk04 = getattr(event, self.cfg_ana.jets_trk04_1000)
        jets_trk08 = getattr(event, self.cfg_ana.jets_trk08_1000)

        electrons = getattr(event, self.cfg_ana.electrons)
        muons = getattr(event, self.cfg_ana.muons)

        if (len(jets_trk02) >= 2 and len(jets_pf02) >= 2):

            self.tree.fill('weight', event.weight)
            self.tree.fill('rapiditySeparation_trk02',
                           abs(jets_trk02[0].eta() - jets_trk02[1].eta()))
            self.tree.fill('transverseMomentumAsymmetry_trk02',
                           (jets_trk02[0].pt() - jets_trk02[1].pt()) /
                           (jets_trk02[0].pt() + jets_trk02[1].pt()))

            self.tree.fill('Jet1_trk02_tau1', jets_trk02[0].tau1)
            self.tree.fill('Jet1_trk02_tau2', jets_trk02[0].tau2)
            self.tree.fill('Jet1_trk02_tau3', jets_trk02[0].tau3)
            self.tree.fill('Jet2_trk02_tau1', jets_trk02[1].tau1)
            self.tree.fill('Jet2_trk02_tau2', jets_trk02[1].tau2)
            self.tree.fill('Jet2_trk02_tau3', jets_trk02[1].tau3)

            self.tree.fill('numberOfElectrons', len(electrons))
            self.tree.fill('numberOfMuons', len(muons))

            if (len(muons) >= 1): fillParticle(self.tree, 'Muon1', muons[0])
            if (len(muons) >= 2): fillParticle(self.tree, 'Muon2', muons[1])

            Jet1_trk02_tau31 = -9.0
            Jet1_trk02_tau21 = -9.0
            Jet1_trk02_tau32 = -9.0
            Jet2_trk02_tau31 = -9.0
            Jet2_trk02_tau21 = -9.0
            Jet2_trk02_tau32 = -9.0

            if (jets_trk02[0].tau1 != 0.0):
                Jet1_trk02_tau31 = jets_trk02[0].tau3 / jets_trk02[0].tau1
                Jet1_trk02_tau21 = jets_trk02[0].tau2 / jets_trk02[0].tau1
            if (jets_trk02[0].tau2 != 0.0):
                Jet1_trk02_tau32 = jets_trk02[0].tau3 / jets_trk02[0].tau2

            if (jets_trk02[1].tau1 != 0.0):
                Jet2_trk02_tau31 = jets_trk02[1].tau3 / jets_trk02[1].tau1
                Jet2_trk02_tau21 = jets_trk02[1].tau2 / jets_trk02[1].tau1
            if (jets_trk02[1].tau2 != 0.0):
                Jet2_trk02_tau32 = jets_trk02[1].tau3 / jets_trk02[1].tau2

            self.tree.fill('Jet1_trk02_tau31', Jet1_trk02_tau31)
            self.tree.fill('Jet1_trk02_tau21', Jet1_trk02_tau21)
            self.tree.fill('Jet1_trk02_tau32', Jet1_trk02_tau32)
            self.tree.fill('Jet2_trk02_tau31', Jet2_trk02_tau31)
            self.tree.fill('Jet2_trk02_tau21', Jet2_trk02_tau21)
            self.tree.fill('Jet2_trk02_tau32', Jet2_trk02_tau32)

            j1 = ROOT.TLorentzVector()
            j2 = ROOT.TLorentzVector()
            j1.SetPtEtaPhiE(jets_trk02[0].pt(), jets_trk02[0].eta(),
                            jets_trk02[0].phi(), jets_trk02[0].e())
            j2.SetPtEtaPhiE(jets_trk02[1].pt(), jets_trk02[1].eta(),
                            jets_trk02[1].phi(), jets_trk02[1].e())

            #MATCHING PF02 and trk02 for CORRECTION
            Jet1_trk02_dR_pf02 = 999
            Jet2_trk02_dR_pf02 = 999
            Jet1_pf02 = None
            Jet2_pf02 = None
            for j in jets_pf02:
                pf02 = ROOT.TLorentzVector()
                pf02.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf02.DeltaR(j1) < Jet1_trk02_dR_pf02:
                    Jet1_trk02_dR_pf02 = pf02.DeltaR(j1)
                    Jet1_pf02 = j
                if pf02.DeltaR(j2) < Jet2_trk02_dR_pf02:
                    Jet2_trk02_dR_pf02 = pf02.DeltaR(j2)
                    Jet2_pf02 = j
        #print 'jet1 dr ',Jet1_trk02_dR_pf02,'  pf02   ',Jet1_pf02,'  trk02  ',jets_trk02[0]
        #print 'jet2 dr ',Jet2_trk02_dR_pf02,'  pf02   ',Jet2_pf02,'  trk02  ',jets_trk02[1]

            corr1 = Jet1_pf02.p4().Pt() / j1.Pt()
            corr2 = Jet2_pf02.p4().Pt() / j2.Pt()

            #print 'corr 1  ',corr1,'   corr2  ',corr2
            #NORMAL TRK02 SD corrected jet
            p4sd1 = ROOT.TLorentzVector()
            p4sd2 = ROOT.TLorentzVector()
            p4sd1.SetPtEtaPhiM(
                jets_trk02[0].subjetsSoftDrop[0].p4().Pt() * corr1,
                jets_trk02[0].eta(), jets_trk02[0].phi(),
                jets_trk02[0].subjetsSoftDrop[0].p4().M() * corr1)

            p4sd2.SetPtEtaPhiM(
                jets_trk02[1].subjetsSoftDrop[0].p4().Pt() * corr2,
                jets_trk02[1].eta(), jets_trk02[1].phi(),
                jets_trk02[1].subjetsSoftDrop[0].p4().M() * corr2)

            pdg1 = 0
            pdg2 = 0
            sdjet1_corr = Particle(pdg1, 0, p4sd1, 1)
            sdjet2_corr = Particle(pdg2, 0, p4sd2, 1)
            fillParticle(self.tree, 'Jet1_trk02_SD_Corr', sdjet1_corr)
            fillParticle(self.tree, 'Jet2_trk02_SD_Corr', sdjet2_corr)

            ######################
            # trkjet04 mass info #
            ######################
            #MATCHING PF04 and trk04 for CORRECTION
            Jet1_trk04_dR_pf04 = 999
            Jet2_trk04_dR_pf04 = 999
            Jet1_pf04 = None
            Jet2_pf04 = None
            for j in jets_pf04_1500:
                pf04 = ROOT.TLorentzVector()
                pf04.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf04.DeltaR(j1) < Jet1_trk04_dR_pf04:
                    Jet1_trk04_dR_pf04 = pf04.DeltaR(j1)
                    Jet1_pf04 = j
                if pf04.DeltaR(j2) < Jet2_trk04_dR_pf04:
                    Jet2_trk04_dR_pf04 = pf04.DeltaR(j2)
                    Jet2_pf04 = j
            corr1_04 = Jet1_pf04.p4().Pt() / j1.Pt()
            corr2_04 = Jet2_pf04.p4().Pt() / j2.Pt()

            #NORMAL TRK04 SD corrected jet
            p4sd1_04 = ROOT.TLorentzVector()
            p4sd2_04 = ROOT.TLorentzVector()
            Jet1_trk04_SD_Corr_m = -1000.
            Jet2_trk04_SD_Corr_m = -1000.

            if len(jets_trk04) >= 1:
                p4sd1_04.SetPtEtaPhiM(
                    jets_trk04[0].subjetsSoftDrop[0].p4().Pt() * corr1_04,
                    jets_trk04[0].eta(), jets_trk04[0].phi(),
                    jets_trk04[0].subjetsSoftDrop[0].p4().M() * corr1_04)
                pdg1 = 0
                sdjet1_corr_04 = Particle(pdg1, 0, p4sd1_04, 1)
                Jet1_trk04_SD_Corr_m = sdjet1_corr_04.p4().M()

            if len(jets_trk04) >= 2:
                p4sd2_04.SetPtEtaPhiM(
                    jets_trk04[1].subjetsSoftDrop[0].p4().Pt() * corr2_04,
                    jets_trk04[1].eta(), jets_trk04[1].phi(),
                    jets_trk04[1].subjetsSoftDrop[0].p4().M() * corr2_04)
                pdg2 = 0
                sdjet2_corr_04 = Particle(pdg2, 0, p4sd2_04, 1)
                Jet2_trk04_SD_Corr_m = sdjet2_corr_04.p4().M()

            self.tree.fill('Jet1_trk04_SD_Corr_m', Jet1_trk04_SD_Corr_m)
            self.tree.fill('Jet2_trk04_SD_Corr_m', Jet2_trk04_SD_Corr_m)

            ######################
            # trkjet08 mass info #
            ######################
            #MATCHING PF08 and trk08 for CORRECTION
            Jet1_trk08_dR_pf08 = 999
            Jet2_trk08_dR_pf08 = 999
            Jet1_pf08 = None
            Jet2_pf08 = None
            for j in jets_pf08:
                pf08 = ROOT.TLorentzVector()
                pf08.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf08.DeltaR(j1) < Jet1_trk08_dR_pf08:
                    Jet1_trk08_dR_pf08 = pf08.DeltaR(j1)
                    Jet1_pf08 = j
                if pf08.DeltaR(j2) < Jet2_trk08_dR_pf08:
                    Jet2_trk08_dR_pf08 = pf08.DeltaR(j2)
                    Jet2_pf08 = j
            corr1_08 = Jet1_pf08.p4().Pt() / j1.Pt()
            corr2_08 = Jet2_pf08.p4().Pt() / j2.Pt()

            #NORMAL TRK08 SD corrected jet
            p4sd1_08 = ROOT.TLorentzVector()
            p4sd2_08 = ROOT.TLorentzVector()
            Jet1_trk08_SD_Corr_m = -1000.
            Jet2_trk08_SD_Corr_m = -1000.

            if len(jets_trk08) >= 1:
                p4sd1_08.SetPtEtaPhiM(
                    jets_trk08[0].subjetsSoftDrop[0].p4().Pt() * corr1_08,
                    jets_trk08[0].eta(), jets_trk08[0].phi(),
                    jets_trk08[0].subjetsSoftDrop[0].p4().M() * corr1_08)
                pdg1 = 0
                sdjet1_corr_08 = Particle(pdg1, 0, p4sd1_08, 1)
                Jet1_trk08_SD_Corr_m = sdjet1_corr_08.p4().M()

            if len(jets_trk08) >= 2:
                p4sd2_08.SetPtEtaPhiM(
                    jets_trk08[1].subjetsSoftDrop[0].p4().Pt() * corr2_08,
                    jets_trk08[1].eta(), jets_trk08[1].phi(),
                    jets_trk08[1].subjetsSoftDrop[0].p4().M() * corr2_08)
                pdg2 = 0
                sdjet2_corr_08 = Particle(pdg2, 0, p4sd2_08, 1)
                Jet2_trk08_SD_Corr_m = sdjet2_corr_08.p4().M()

            self.tree.fill('Jet1_trk08_SD_Corr_m', Jet1_trk08_SD_Corr_m)
            self.tree.fill('Jet2_trk08_SD_Corr_m', Jet2_trk08_SD_Corr_m)

            #CORRECTED TRK02 jet
            p4jet1_corr = ROOT.TLorentzVector()
            p4jet2_corr = ROOT.TLorentzVector()
            p4jet1_corr.SetPtEtaPhiM(jets_trk02[0].pt() * corr1,
                                     jets_trk02[0].eta(), jets_trk02[0].phi(),
                                     jets_trk02[0].m() * corr1)
            p4jet2_corr.SetPtEtaPhiM(jets_trk02[1].pt() * corr2,
                                     jets_trk02[1].eta(), jets_trk02[1].phi(),
                                     jets_trk02[1].m() * corr2)

            jet1_corr = Particle(pdg1, 0, p4jet1_corr, 1)
            jet2_corr = Particle(pdg2, 0, p4jet2_corr, 1)

            if (len(jets_trk02) > 1):
                self.tree.fill('Mj1j2_trk02',
                               self.fillMass(jets_trk02[0], jets_trk02[1]))
                self.tree.fill('Mj1j2_trk02_Corr',
                               self.fillMass(jet1_corr, jet2_corr))

            if (len(jets_pf02) > 1):
                self.tree.fill('Mj1j2_pf02',
                               self.fillMass(jets_pf02[0], jets_pf02[1]))

            if (len(jets_pf04) > 1):
                self.tree.fill('Mj1j2_pf04',
                               self.fillMass(jets_pf04[0], jets_pf04[1]))

            if (len(jets_pf08) > 1):
                self.tree.fill('Mj1j2_pf08',
                               self.fillMass(jets_pf08[0], jets_pf08[1]))

            #Flow n,5
            #############################################################################
            #REQUIRES THE FOLLOWING IN heppy/analyzers/fcc/Reader.py AFTER LINE 151:

            #	particle_relations = defaultdict(list)
            #       for tjet in store.get(self.cfg_ana.fatjets):
            #		for i in range(tjet.particles_size()):
            #     			particle_relations[Jet(tjet)].append(Particle(tjet.particles(i)))
            #	for fatjet, particles in particle_relations.items():
            # 		fatjets[fatjet].jetConstituents = particles

            #############################################################################

            #R = 0.8 # init
            R = 0.05

            flow_Jet1 = [0] * 5
            flow_Jet2 = [0] * 5
            if len(jets_pf08) < 2: return
            jet1_ungroomed = ROOT.TLorentzVector()
            jet2_ungroomed = ROOT.TLorentzVector()
            jet1_ungroomed.SetPtEtaPhiE(jets_pf08[0].pt(), jets_pf08[0].eta(),
                                        jets_pf08[0].phi(), jets_pf08[0].e())
            jet2_ungroomed.SetPtEtaPhiE(jets_pf08[1].pt(), jets_pf08[1].eta(),
                                        jets_pf08[1].phi(), jets_pf08[1].e())

            constituent_vector = ROOT.TLorentzVector()
            for n in range(1, 5 + 1):
                for constituent in jets_pf08[0].jetConstituents[1:]:
                    constituent_vector.SetPtEtaPhiE(constituent.pt(),
                                                    constituent.eta(),
                                                    constituent.phi(),
                                                    constituent.e())
                    dR = jet1_ungroomed.DeltaR(constituent_vector)
                    if ((dR >= (n - 1) / 5 * R) and (dR < n / 5 * R)):
                        flow_Jet1[n - 1] += abs(constituent.pt()) / abs(
                            jets_pf08[0].pt())
                for constituent in jets_pf08[1].jetConstituents[1:]:
                    constituent_vector.SetPtEtaPhiE(constituent.pt(),
                                                    constituent.eta(),
                                                    constituent.phi(),
                                                    constituent.e())
                    dR = jet2_ungroomed.DeltaR(constituent_vector)
                    if ((dR >= (n - 1) / 5 * R) and (dR < n / 5 * R)):
                        flow_Jet2[n - 1] += abs(constituent.pt()) / abs(
                            jets_pf08[1].pt())

            self.tree.fill('Jet1_Flow15', flow_Jet1[0])
            self.tree.fill('Jet2_Flow15', flow_Jet2[0])
            self.tree.fill('Jet1_Flow25', flow_Jet1[1])
            self.tree.fill('Jet2_Flow25', flow_Jet2[1])
            self.tree.fill('Jet1_Flow35', flow_Jet1[2])
            self.tree.fill('Jet2_Flow35', flow_Jet2[2])
            self.tree.fill('Jet1_Flow45', flow_Jet1[3])
            self.tree.fill('Jet2_Flow45', flow_Jet2[3])
            self.tree.fill('Jet1_Flow55', flow_Jet1[4])
            self.tree.fill('Jet2_Flow55', flow_Jet2[4])

            ###################################
            #TMVA Stuff Starts!
            ###################################

            self.bdt_Jet_trk02_tau1[0] = jets_trk02[0].tau1
            self.bdt_Jet_trk02_tau2[0] = jets_trk02[0].tau2
            self.bdt_Jet_trk02_tau3[0] = jets_trk02[0].tau3
            self.bdt_Jet_trk02_tau21[0] = Jet1_trk02_tau21
            self.bdt_Jet_trk02_tau31[0] = Jet1_trk02_tau31
            self.bdt_Jet_trk02_tau32[0] = Jet1_trk02_tau32
            self.bdt_Jet_trk02_SD_Corr_m[0] = sdjet1_corr.p4().M()
            if len(jets_trk04) >= 1:
                self.bdt_Jet_trk04_SD_Corr_m[0] = sdjet1_corr_04.p4().M()
            else:
                self.bdt_Jet_trk04_SD_Corr_m[0] = -1000.
            if len(jets_trk08) >= 1:
                self.bdt_Jet_trk08_SD_Corr_m[0] = sdjet1_corr_08.p4().M()
            else:
                self.bdt_Jet_trk08_SD_Corr_m[0] = -1000.
            self.bdt_Jet_Flow15[0] = flow_Jet1[0]
            self.bdt_Jet_Flow25[0] = flow_Jet1[1]
            self.bdt_Jet_Flow35[0] = flow_Jet1[2]
            self.bdt_Jet_Flow45[0] = flow_Jet1[3]
            self.bdt_Jet_Flow55[0] = flow_Jet1[4]
            mva_value = self.reader.EvaluateMVA("BDT")
            self.tree.fill('Jet1_Whad_vs_QCD_tagger', mva_value)
            #
            self.bdt_Jet_trk02_tau1[0] = jets_trk02[1].tau1
            self.bdt_Jet_trk02_tau2[0] = jets_trk02[1].tau2
            self.bdt_Jet_trk02_tau3[0] = jets_trk02[1].tau3
            self.bdt_Jet_trk02_tau21[0] = Jet2_trk02_tau21
            self.bdt_Jet_trk02_tau31[0] = Jet2_trk02_tau31
            self.bdt_Jet_trk02_tau32[0] = Jet2_trk02_tau32
            self.bdt_Jet_trk02_SD_Corr_m[0] = sdjet2_corr.p4().M()
            if len(jets_trk04) >= 2:
                self.bdt_Jet_trk04_SD_Corr_m[0] = sdjet2_corr_04.p4().M()
            else:
                self.bdt_Jet_trk04_SD_Corr_m[0] = -1000.
            if len(jets_trk08) >= 2:
                self.bdt_Jet_trk08_SD_Corr_m[0] = sdjet2_corr_08.p4().M()
            else:
                self.bdt_Jet_trk08_SD_Corr_m[0] = -1000.
            self.bdt_Jet_Flow15[0] = flow_Jet2[0]
            self.bdt_Jet_Flow25[0] = flow_Jet2[1]
            self.bdt_Jet_Flow35[0] = flow_Jet2[2]
            self.bdt_Jet_Flow45[0] = flow_Jet2[3]
            self.bdt_Jet_Flow55[0] = flow_Jet2[4]
            mva_value = self.reader.EvaluateMVA("BDT")
            self.tree.fill('Jet2_Whad_vs_QCD_tagger', mva_value)

            self.tree.tree.Fill()
Esempio n. 25
0
    def process(self, event):
        self.tree.reset()
        jets_trk02 = getattr(event, self.cfg_ana.jets_trk02_1000)
        jets_pf02 = getattr(event, self.cfg_ana.jets_pf02_1500)
        jets_pf04 = getattr(event, self.cfg_ana.jets_pf04_1000)
        jets_pf04_pdg = event.jets_pf04_1000_pdg
        jets_pf08 = getattr(event, self.cfg_ana.jets_pf08_1500)
        #gen_particles = event.all_particles

        jets_pf04_1500 = getattr(event, self.cfg_ana.jets_pf04_1500)
        jets_trk04 = getattr(event, self.cfg_ana.jets_trk04_1000)
        jets_trk08 = getattr(event, self.cfg_ana.jets_trk08_1000)
        
        electrons = getattr(event, self.cfg_ana.electrons)
        muons = getattr(event, self.cfg_ana.muons)

        #self.tree2.reset()
        ##
        #label = -1
        #part1 = -10000
        #part2 = -10000
        #index = 0
        #part_index = -1
        #count = 0
        #for j in gen_particles :
        #  # 1st part
        #  if (j.status()==22 or j.status()==23) and count==0:
        #    part1 = j.pdgid()
        #    count += 1
        #    part_index = index
        #  # 2nd part
        #  if (j.status()==22 or j.status()==23) and count==1 and j.q()+gen_particles[part_index].q()==0:
        #    part2 = j.pdgid()
        #    count += 1
        #  index += 1
        #if abs(part1)==6 and abs(part2)==6 : label = 6
        #if abs(part1)==5 and abs(part2)==5 : label = 5
        #if abs(part1)==4 and abs(part2)==4 : label = 4
        #if abs(part1)==3 and abs(part2)==3 : label = 0
        #if abs(part1)==2 and abs(part2)==2 : label = 0
        #if abs(part1)==1 and abs(part2)==1 : label = 0
        ## missed cases
        #if label == -1 :
        #  count = 0
        #  for j in gen_particles :
        #    if count<10 and abs(j.pdgid())==6 :
        #      label = 6
        #    count += 1
        #if label==-1 : print "issue label==-1"
        ##
        #self.tree2.fill('label' , label )
        #self.tree2.tree.Fill()

        Jet1_trk02_dR_lep = 999
        Jet2_trk02_dR_lep  = 999
        if ( len(jets_trk02)>=2 and  len(jets_pf02)>=2):

            #self.tree.fill('label' , label )

            j1 = ROOT.TLorentzVector(); j2 = ROOT.TLorentzVector()
            j1.SetPtEtaPhiE(jets_trk02[0].pt(), jets_trk02[0].eta(), jets_trk02[0].phi(), jets_trk02[0].e())
            j2.SetPtEtaPhiE(jets_trk02[1].pt(), jets_trk02[1].eta(), jets_trk02[1].phi(), jets_trk02[1].e())
            if ( len(electrons)!=0 and len(muons)==0 ):
                e = ROOT.TLorentzVector()
                e.SetPtEtaPhiE(electrons[0].pt(), electrons[0].eta(), electrons[0].phi(), electrons[0].e())
                Jet1_dR = j1.DeltaR(e)
                Jet2_dR = j2.DeltaR(e)
            if ( len(electrons)==0 and len(muons)!=0 ):
                m = ROOT.TLorentzVector()
                m.SetPtEtaPhiE(muons[0].pt(), muons[0].eta(), muons[0].phi(), muons[0].e())
                Jet1_dR = j1.DeltaR(m)
                Jet2_dR = j2.DeltaR(m)
            if ( len(electrons)!=0 and len(muons)!=0 ):
                isElectron = False; isMuon = False
                if ( electrons[0].pt() > muons[0].pt() ): isElectron = True
                else: isMuon = True
                l = ROOT.TLorentzVector()
                if isElectron: l.SetPtEtaPhiE(electrons[0].pt(), electrons[0].eta(), electrons[0].phi(), electrons[0].e())
                if isMuon: l.SetPtEtaPhiE(muons[0].pt(), muons[0].eta(), muons[0].phi(), muons[0].e())
                Jet1_trk02_dR_lep  = j1.DeltaR(l)
                Jet2_trk02_dR_lep  = j2.DeltaR(l)
            
            self.tree.fill('Jet1_trk02_dR_lep' , Jet1_trk02_dR_lep )
            self.tree.fill('Jet2_trk02_dR_lep' , Jet2_trk02_dR_lep )
#had_hadsemilep_lep_decays

            self.tree.fill('weight' , event.weight )
            self.tree.fill('missingET', event.met.pt())
            self.tree.fill('numberOfElectrons', len(electrons))
            self.tree.fill('numberOfMuons', len(muons))

            self.tree.fill('rapiditySeparation_trk02', abs(jets_trk02[0].eta() - jets_trk02[1].eta()))
            self.tree.fill('transverseMomentumAsymmetry_trk02', (jets_trk02[0].pt() - jets_trk02[1].pt())/(jets_trk02[0].pt() + jets_trk02[1].pt()))

            self.tree.fill('Jet1_trk02_tau1' , jets_trk02[0].tau1 )
            self.tree.fill('Jet1_trk02_tau2' , jets_trk02[0].tau2 )
            self.tree.fill('Jet1_trk02_tau3' , jets_trk02[0].tau3 )
            self.tree.fill('Jet2_trk02_tau1' , jets_trk02[1].tau1 )
            self.tree.fill('Jet2_trk02_tau2' , jets_trk02[1].tau2 )
            self.tree.fill('Jet2_trk02_tau3' , jets_trk02[1].tau3 )

            Jet1_trk02_tau31 = -999.0
            Jet1_trk02_tau21 = -999.0
            Jet1_trk02_tau32 = -999.0
            Jet2_trk02_tau31 = -999.0
            Jet2_trk02_tau21 = -999.0
            Jet2_trk02_tau32 = -999.0

            if (jets_trk02[0].tau1 != 0.0):
                Jet1_trk02_tau31 = jets_trk02[0].tau3/jets_trk02[0].tau1
                Jet1_trk02_tau21 = jets_trk02[0].tau2/jets_trk02[0].tau1 
            if (jets_trk02[0].tau2 != 0.0):
                Jet1_trk02_tau32 = jets_trk02[0].tau3/jets_trk02[0].tau2

            if (jets_trk02[1].tau1 != 0.0):
                Jet2_trk02_tau31 = jets_trk02[1].tau3/jets_trk02[1].tau1
                Jet2_trk02_tau21 = jets_trk02[1].tau2/jets_trk02[1].tau1
            if (jets_trk02[1].tau2 != 0.0):
                Jet2_trk02_tau32 = jets_trk02[1].tau3/jets_trk02[1].tau2

            self.tree.fill('Jet1_trk02_tau31', Jet1_trk02_tau31)
            self.tree.fill('Jet1_trk02_tau21', Jet1_trk02_tau21)
            self.tree.fill('Jet1_trk02_tau32', Jet1_trk02_tau32)
            self.tree.fill('Jet2_trk02_tau31', Jet2_trk02_tau31)
            self.tree.fill('Jet2_trk02_tau21', Jet2_trk02_tau21)
            self.tree.fill('Jet2_trk02_tau32', Jet2_trk02_tau32)

	    # here is btag, need matching in DR
            Jet1_trk02_dR_pf04 = 999
            Jet2_trk02_dR_pf04 = 999
	    for j in jets_pf04:
                pf04= ROOT.TLorentzVector()
                pf04.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if j.tags['bf'] > 0:
                    if pf04.DeltaR(j1)<Jet1_trk02_dR_pf04:
                        Jet1_trk02_dR_pf04=pf04.DeltaR(j1)
                    if pf04.DeltaR(j2)<Jet2_trk02_dR_pf04:
                        Jet2_trk02_dR_pf04=pf04.DeltaR(j2)
            #print 'dr j1  ',Jet1_trk02_dR_pf04
            #print 'dr j2  ',Jet2_trk02_dR_pf04
            
            pdg1 = 0
            pdg2 = 0
            if Jet1_trk02_dR_pf04 < 0.3:
                pdg1 = 5
            if Jet2_trk02_dR_pf04 < 0.3:
                pdg2 = 5

            # TRF / truth b-tagging -> need at least 2 jets_pf04
            use_DELPHES=False
            weight_1tagex=0.
            weight_2tagex=0.
            jet=[]
            ipdg=0
            for i in range(len(jets_pf04)):
              if use_DELPHES==True:
                ipdg = jets_pf04[i].tags['flav']
                if ipdg!=4 and ipdg!=5 : ipdg=0
              else:
                ipdg = jets_pf04_pdg[i].flavour
              jet.append([jets_pf04[i],ipdg])
            if (len(jet)>0): weight_1tagex=getNbTagEx(1,jet,2)
            if (len(jet)>1): weight_2tagex=getNbTagEx(2,jet,2)
            weight_1tagin=weight_1tagex+weight_2tagex
            self.tree.fill('weight_1tagex', weight_1tagex)
            self.tree.fill('weight_2tagex', weight_2tagex)
            self.tree.fill('weight_1tagin', weight_1tagin)

            #MATCHING PF02 and trk02 for CORRECTION
            Jet1_trk02_dR_pf02 = 999
            Jet2_trk02_dR_pf02 = 999
            Jet1_pf02 = None
            Jet2_pf02 = None
            for j in jets_pf02:
                pf02= ROOT.TLorentzVector()
                pf02.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf02.DeltaR(j1)<Jet1_trk02_dR_pf02:
                    Jet1_trk02_dR_pf02=pf02.DeltaR(j1)
                    Jet1_pf02=j
                if pf02.DeltaR(j2)<Jet2_trk02_dR_pf02:
                    Jet2_trk02_dR_pf02=pf02.DeltaR(j2)
                    Jet2_pf02=j
            #print 'jet1 dr ',Jet1_trk02_dR_pf02,'  pf02   ',Jet1_pf02,'  trk02  ',jets_trk02[0]
            #print 'jet2 dr ',Jet2_trk02_dR_pf02,'  pf02   ',Jet2_pf02,'  trk02  ',jets_trk02[1]

	    corr1 = Jet1_pf02.p4().Pt()/j1.Pt()
	    corr2 = Jet2_pf02.p4().Pt()/j2.Pt()

            #print 'corr 1  ',corr1,'   corr2  ',corr2
            #NORMAL TRK02 SD corrected jet
	    p4sd1 = ROOT.TLorentzVector(); p4sd2 = ROOT.TLorentzVector()
	    p4sd1.SetPtEtaPhiM(jets_trk02[0].subjetsSoftDrop[0].p4().Pt()*corr1, 
	    			jets_trk02[0].eta(), 
	        		jets_trk02[0].phi(), 
	        		jets_trk02[0].subjetsSoftDrop[0].p4().M()*corr1)
	    
	    p4sd2.SetPtEtaPhiM(jets_trk02[1].subjetsSoftDrop[0].p4().Pt()*corr2, 
	    			jets_trk02[1].eta(), 
	        		jets_trk02[1].phi(), 
	        		jets_trk02[1].subjetsSoftDrop[0].p4().M()*corr2)
	    
            sdjet1_corr = Particle(pdg1, 0, p4sd1, 1)
            sdjet2_corr = Particle(pdg2, 0, p4sd2, 1)
            fillParticle(self.tree, 'Jet1_trk02_SD_Corr', sdjet1_corr)
            fillParticle(self.tree, 'Jet2_trk02_SD_Corr', sdjet2_corr)

            #NORMAL TRK02 SD jet
	    #sdjet1 = Particle(pdg1, 0, jets_trk02[0].subjetsSoftDrop[0].p4(), 1)
            #sdjet2 = Particle(pdg2, 0, jets_trk02[1].subjetsSoftDrop[0].p4(), 1)
            #fillParticle(self.tree, 'Jet1_trk02_SD', sdjet1)
            #fillParticle(self.tree, 'Jet2_trk02_SD', sdjet2)

            #CORRECTED TRK02 jet
	    p4jet1_corr = ROOT.TLorentzVector(); p4jet2_corr = ROOT.TLorentzVector()
            p4jet1_corr.SetPtEtaPhiM(jets_trk02[0].pt()*corr1, jets_trk02[0].eta(), jets_trk02[0].phi(), jets_trk02[0].m()*corr1)
	    p4jet2_corr.SetPtEtaPhiM(jets_trk02[1].pt()*corr2, jets_trk02[1].eta(), jets_trk02[1].phi(), jets_trk02[1].m()*corr2)

            jet1_corr = Particle(pdg1, 0, p4jet1_corr, 1)
            jet2_corr = Particle(pdg2, 0, p4jet2_corr, 1)
            #fillParticle(self.tree, 'Jet1_trk02_Corr', jet1_corr)
            #fillParticle(self.tree, 'Jet2_trk02_Corr', jet2_corr)

 
            # associate MET to one jet or another based on softdrop
            sdjetmet1, sdjetmet2 = self.corrMET(jets_trk02[0].subjetsSoftDrop[0], pdg1, jets_trk02[1].subjetsSoftDrop[0], pdg2, event.met)
            fillParticle(self.tree, 'Jet1_trk02_SD_MetCorr', sdjetmet1)
            fillParticle(self.tree, 'Jet2_trk02_SD_MetCorr', sdjetmet2)

            sdjetmet1, sdjetmet2 = self.corrMET(sdjet1_corr, pdg1, sdjet2_corr, pdg2, event.met)
            fillParticle(self.tree, 'Jet1_trk02_SD_Corr_MetCorr', sdjetmet1)
            fillParticle(self.tree, 'Jet2_trk02_SD_Corr_MetCorr', sdjetmet2)

            ######################
            # trkjet04 mass info #
            ######################
            #MATCHING PF04 and trk04 for CORRECTION
            Jet1_trk04_dR_pf04 = 999
            Jet2_trk04_dR_pf04 = 999
            Jet1_pf04 = None
            Jet2_pf04 = None
            for j in jets_pf04_1500:
                pf04= ROOT.TLorentzVector()
                pf04.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf04.DeltaR(j1)<Jet1_trk04_dR_pf04:
                    Jet1_trk04_dR_pf04=pf04.DeltaR(j1)
                    Jet1_pf04=j
                if pf04.DeltaR(j2)<Jet2_trk04_dR_pf04:
                    Jet2_trk04_dR_pf04=pf04.DeltaR(j2)
                    Jet2_pf04=j
            corr1_04 = Jet1_pf04.p4().Pt()/j1.Pt()
            corr2_04 = Jet2_pf04.p4().Pt()/j2.Pt()

            #NORMAL TRK04 SD corrected jet
            p4sd1_04 = ROOT.TLorentzVector(); p4sd2_04 = ROOT.TLorentzVector()
            Jet1_trk04_SD_Corr_m = -1000.   ; Jet2_trk04_SD_Corr_m = -1000.

            if len(jets_trk04)>=1 :
                p4sd1_04.SetPtEtaPhiM(jets_trk04[0].subjetsSoftDrop[0].p4().Pt()*corr1_04,
                                      jets_trk04[0].eta(),
                                      jets_trk04[0].phi(),
                                      jets_trk04[0].subjetsSoftDrop[0].p4().M()*corr1_04)
                pdg1 = 0
                sdjet1_corr_04 = Particle(pdg1, 0, p4sd1_04, 1)
                Jet1_trk04_SD_Corr_m = sdjet1_corr_04.p4().M()

            if len(jets_trk04)>=2 :
                p4sd2_04.SetPtEtaPhiM(jets_trk04[1].subjetsSoftDrop[0].p4().Pt()*corr2_04,
                                      jets_trk04[1].eta(),
                                      jets_trk04[1].phi(),
                                      jets_trk04[1].subjetsSoftDrop[0].p4().M()*corr2_04)
                pdg2 = 0
                sdjet2_corr_04 = Particle(pdg2, 0, p4sd2_04, 1)
                Jet2_trk04_SD_Corr_m = sdjet2_corr_04.p4().M()

            self.tree.fill('Jet1_trk04_SD_Corr_m', Jet1_trk04_SD_Corr_m)
            self.tree.fill('Jet2_trk04_SD_Corr_m', Jet2_trk04_SD_Corr_m)

            ######################
            # trkjet08 mass info #
            ######################
            #MATCHING PF08 and trk08 for CORRECTION
            Jet1_trk08_dR_pf08 = 999
            Jet2_trk08_dR_pf08 = 999
            Jet1_pf08 = None
            Jet2_pf08 = None
            for j in jets_pf08:
                pf08= ROOT.TLorentzVector()
                pf08.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf08.DeltaR(j1)<Jet1_trk08_dR_pf08:
                    Jet1_trk08_dR_pf08=pf08.DeltaR(j1)
                    Jet1_pf08=j
                if pf08.DeltaR(j2)<Jet2_trk08_dR_pf08:
                    Jet2_trk08_dR_pf08=pf08.DeltaR(j2)
                    Jet2_pf08=j
            corr1_08 = Jet1_pf08.p4().Pt()/j1.Pt()
            corr2_08 = Jet2_pf08.p4().Pt()/j2.Pt()

            #NORMAL TRK08 SD corrected jet
            p4sd1_08 = ROOT.TLorentzVector(); p4sd2_08 = ROOT.TLorentzVector()
            Jet1_trk08_SD_Corr_m = -1000.   ; Jet2_trk08_SD_Corr_m = -1000.

            if len(jets_trk08)>=1 :
                p4sd1_08.SetPtEtaPhiM(jets_trk08[0].subjetsSoftDrop[0].p4().Pt()*corr1_08,
                                      jets_trk08[0].eta(),
                                      jets_trk08[0].phi(),
                                      jets_trk08[0].subjetsSoftDrop[0].p4().M()*corr1_08)
                pdg1 = 0
                sdjet1_corr_08 = Particle(pdg1, 0, p4sd1_08, 1)
                Jet1_trk08_SD_Corr_m = sdjet1_corr_08.p4().M()

            if len(jets_trk08)>=2 :
                p4sd2_08.SetPtEtaPhiM(jets_trk08[1].subjetsSoftDrop[0].p4().Pt()*corr2_08,
                                      jets_trk08[1].eta(),
                                      jets_trk08[1].phi(),
                                      jets_trk08[1].subjetsSoftDrop[0].p4().M()*corr2_08)
                pdg2 = 0
                sdjet2_corr_08 = Particle(pdg2, 0, p4sd2_08, 1)
                Jet2_trk08_SD_Corr_m = sdjet2_corr_08.p4().M()

            self.tree.fill('Jet1_trk08_SD_Corr_m', Jet1_trk08_SD_Corr_m)
            self.tree.fill('Jet2_trk08_SD_Corr_m', Jet2_trk08_SD_Corr_m)

            if (len(jets_trk02)>1): 
                self.tree.fill( 'Mj1j2_trk02',self.fillMass(jets_trk02[0],jets_trk02[1]))
                self.tree.fill( 'Mj1j2_trk02_Corr',self.fillMass(jet1_corr,jet2_corr))
                jetmet1, jetmet2 = self.corrMET(jets_trk02[0], pdg1, jets_trk02[1], pdg2, event.met)
                self.tree.fill( 'Mj1j2_trk02_MetCorr',self.fillMass(jetmet1,jetmet2))
                #fillParticle(self.tree, 'Jet1_trk02_MetCorr', jetmet1)
                #fillParticle(self.tree, 'Jet2_trk02_MetCorr', jetmet2)

                jetmet1, jetmet2 = self.corrMET(jet1_corr, pdg1, jet2_corr, pdg2, event.met)
                self.tree.fill( 'Mj1j2_trk02_Corr_MetCorr',self.fillMass(jetmet1,jetmet2))
                fillParticle(self.tree, 'Jet1_trk02_Corr_MetCorr', jetmet1)
                fillParticle(self.tree, 'Jet2_trk02_Corr_MetCorr', jetmet2)

            if (len(jets_pf02)>1):  
                self.tree.fill( 'Mj1j2_pf02', self.fillMass(jets_pf02[0],jets_pf02[1]))
                jetmet1, jetmet2 = self.corrMET(jets_pf02[0], pdg1, jets_pf02[1], pdg2, event.met)
                self.tree.fill( 'Mj1j2_pf02_MetCorr', self.fillMass(jetmet1,jetmet2))

            if (len(jets_pf04)>1):  
                self.tree.fill( 'Mj1j2_pf04', self.fillMass(jets_pf04[0],jets_pf04[1]))
                jetmet1, jetmet2 = self.corrMET(jets_pf04[0], pdg1, jets_pf04[1], pdg2, event.met)
                self.tree.fill( 'Mj1j2_pf04_MetCorr', self.fillMass(jetmet1,jetmet2))

            if (len(jets_pf08)>1):  
                self.tree.fill( 'Mj1j2_pf08', self.fillMass(jets_pf08[0],jets_pf08[1]))
                jetmet1, jetmet2 = self.corrMET(jets_pf08[0], pdg1, jets_pf08[1], pdg2, event.met)
                self.tree.fill( 'Mj1j2_pf08_MetCorr', self.fillMass(jetmet1,jetmet2))





            if ( len(electrons) >=1 ): fillParticle(self.tree, 'Electron1', electrons[0])
            if ( len(electrons) >=2 ): fillParticle(self.tree, 'Electron2', electrons[1])

            if ( len(muons) >=1 ): fillParticle(self.tree, 'Muon1', muons[0])
            if ( len(muons) >=2 ): fillParticle(self.tree, 'Muon2', muons[1])

            ###################################
            #TMVA Stuff Starts!
            ###################################

            self.bdt_Jet_trk02_tau1[0] = jets_trk02[0].tau1
            self.bdt_Jet_trk02_tau2[0] = jets_trk02[0].tau2
            self.bdt_Jet_trk02_tau3[0] = jets_trk02[0].tau3
            self.bdt_Jet_trk02_tau21[0] = Jet1_trk02_tau21
            self.bdt_Jet_trk02_tau31[0] = Jet1_trk02_tau31
            self.bdt_Jet_trk02_tau32[0] = Jet1_trk02_tau32
            self.bdt_Jet_trk02_SD_Corr_m[0]  = sdjet1_corr.p4().M()
            if len(jets_trk04)>=1 : self.bdt_Jet_trk04_SD_Corr_m[0] = sdjet1_corr_04.p4().M()
            else                  : self.bdt_Jet_trk04_SD_Corr_m[0] = -1000.
            if len(jets_trk08)>=1 : self.bdt_Jet_trk08_SD_Corr_m[0] = sdjet1_corr_08.p4().M()
            else                  : self.bdt_Jet_trk08_SD_Corr_m[0] = -1000.
            mva_value = self.reader.EvaluateMVA("BDT")
            self.tree.fill( 'Jet1_thad_vs_QCD_tagger', mva_value)
            #
            self.bdt_Jet_trk02_tau1[0] = jets_trk02[1].tau1
            self.bdt_Jet_trk02_tau2[0] = jets_trk02[1].tau2
            self.bdt_Jet_trk02_tau3[0] = jets_trk02[1].tau3
            self.bdt_Jet_trk02_tau21[0] = Jet2_trk02_tau21
            self.bdt_Jet_trk02_tau31[0] = Jet2_trk02_tau31
            self.bdt_Jet_trk02_tau32[0] = Jet2_trk02_tau32
            self.bdt_Jet_trk02_SD_Corr_m[0]  = sdjet2_corr.p4().M()
            if len(jets_trk04)>=2 : self.bdt_Jet_trk04_SD_Corr_m[0] = sdjet2_corr_04.p4().M()
            else                  : self.bdt_Jet_trk04_SD_Corr_m[0] = -1000.
            if len(jets_trk08)>=2 : self.bdt_Jet_trk08_SD_Corr_m[0] = sdjet2_corr_08.p4().M()
            else                  : self.bdt_Jet_trk08_SD_Corr_m[0] = -1000.
            mva_value = self.reader.EvaluateMVA("BDT")
            self.tree.fill( 'Jet2_thad_vs_QCD_tagger', mva_value)

            self.tree.tree.Fill()
Esempio n. 26
0
    def process(self, event):
        self.tree.reset()
        jets_trk02 = getattr(event, self.cfg_ana.jets_trk02_1000)
        jets_pf02 = getattr(event, self.cfg_ana.jets_pf02_1500)
        jets_pf04 = getattr(event, self.cfg_ana.jets_pf04_1000)
        jets_pf04_pdg = event.jets_pf04_1000_pdg
        jets_pf08 = getattr(event, self.cfg_ana.jets_pf08_1500)
        #gen_particles = event.all_particles

        jets_pf04_1500 = getattr(event, self.cfg_ana.jets_pf04_1500)
        jets_trk04 = getattr(event, self.cfg_ana.jets_trk04_1000)
        jets_trk08 = getattr(event, self.cfg_ana.jets_trk08_1000)

        electrons = getattr(event, self.cfg_ana.electrons)
        muons = getattr(event, self.cfg_ana.muons)

        Jet1_trk02_dR_lep = 999
        Jet2_trk02_dR_lep = 999
        Jet1_trk02_dR_Jet2_trk02 = 999
        if (len(jets_trk02) >= 2 and len(jets_pf02) >= 2):
            j1 = ROOT.TLorentzVector()
            j2 = ROOT.TLorentzVector()
            j1.SetPtEtaPhiE(jets_trk02[0].pt(), jets_trk02[0].eta(),
                            jets_trk02[0].phi(), jets_trk02[0].e())
            j2.SetPtEtaPhiE(jets_trk02[1].pt(), jets_trk02[1].eta(),
                            jets_trk02[1].phi(), jets_trk02[1].e())
            if (len(electrons) != 0 and len(muons) == 0):
                e = ROOT.TLorentzVector()
                e.SetPtEtaPhiE(electrons[0].pt(), electrons[0].eta(),
                               electrons[0].phi(), electrons[0].e())
                Jet1_dR = j1.DeltaR(e)
                Jet2_dR = j2.DeltaR(e)
            if (len(electrons) == 0 and len(muons) != 0):
                m = ROOT.TLorentzVector()
                m.SetPtEtaPhiE(muons[0].pt(), muons[0].eta(), muons[0].phi(),
                               muons[0].e())
                Jet1_dR = j1.DeltaR(m)
                Jet2_dR = j2.DeltaR(m)
            if (len(electrons) != 0 and len(muons) != 0):
                isElectron = False
                isMuon = False
                if (electrons[0].pt() > muons[0].pt()): isElectron = True
                else: isMuon = True
                l = ROOT.TLorentzVector()
                if isElectron:
                    l.SetPtEtaPhiE(electrons[0].pt(), electrons[0].eta(),
                                   electrons[0].phi(), electrons[0].e())
                if isMuon:
                    l.SetPtEtaPhiE(muons[0].pt(), muons[0].eta(),
                                   muons[0].phi(), muons[0].e())
                Jet1_trk02_dR_lep = j1.DeltaR(l)
                Jet2_trk02_dR_lep = j2.DeltaR(l)

            self.tree.fill('Jet1_trk02_dR_lep', Jet1_trk02_dR_lep)
            self.tree.fill('Jet2_trk02_dR_lep', Jet2_trk02_dR_lep)

            Jet1_trk02_dR_Jet2_trk02 = j1.DeltaR(j2)
            self.tree.fill('Jet1_trk02_dR_Jet2_trk02',
                           Jet1_trk02_dR_Jet2_trk02)

            # jets and leptons
            Jet1_lepclose = False
            Jet2_lepclose = False
            dR_close = 0.7
            for i in xrange(len(electrons)):
                l = ROOT.TLorentzVector()
                l.SetPtEtaPhiE(electrons[i].pt(), electrons[i].eta(),
                               electrons[i].phi(), electrons[i].e())
                # do not use leptons with pTl/pTj<0.15 (our definiton of a semi-lep decay)
                if j1.DeltaR(l) < dR_close and l.Pt() / j1.Pt() > 0.15:
                    Jet1_lepclose = True
                if j2.DeltaR(l) < dR_close and l.Pt() / j2.Pt() > 0.15:
                    Jet2_lepclose = True
            for i in xrange(len(muons)):
                l = ROOT.TLorentzVector()
                l.SetPtEtaPhiE(muons[i].pt(), muons[i].eta(), muons[i].phi(),
                               muons[i].e())
                # do not use leptons with pTl/pTj<0.15 (our definiton of a semi-lep decay)
                if j1.DeltaR(l) < dR_close and l.Pt() / j1.Pt() > 0.15:
                    Jet1_lepclose = True
                if j2.DeltaR(l) < dR_close and l.Pt() / j2.Pt() > 0.15:
                    Jet2_lepclose = True
            #
            lepton_iso = False
            dR_match = 0.2
            for i in xrange(len(electrons)):
                l = ROOT.TLorentzVector()
                l.SetPtEtaPhiE(electrons[i].pt(), electrons[i].eta(),
                               electrons[i].phi(), electrons[i].e())
                if lepton_iso == False:
                    n_match = 0
                    for j in xrange(len(jets_trk02)):
                        j1 = ROOT.TLorentzVector()
                        j1.SetPtEtaPhiE(jets_trk02[j].pt(),
                                        jets_trk02[j].eta(),
                                        jets_trk02[j].phi(), jets_trk02[j].e())
                        if j1.DeltaR(l) < dR_match: n_match += 1
                    if n_match == 0: lepton_iso = True
            for i in xrange(len(muons)):
                l = ROOT.TLorentzVector()
                l.SetPtEtaPhiE(muons[i].pt(), muons[i].eta(), muons[i].phi(),
                               muons[i].e())
                if lepton_iso == False:
                    n_match = 0
                    for j in xrange(len(jets_trk02)):
                        j1 = ROOT.TLorentzVector()
                        j1.SetPtEtaPhiE(jets_trk02[j].pt(),
                                        jets_trk02[j].eta(),
                                        jets_trk02[j].phi(), jets_trk02[j].e())
                        if j1.DeltaR(l) < dR_match: n_match += 1
                    if n_match == 0: lepton_iso = True
            #
            self.tree.fill('Jet1_lepclose', Jet1_lepclose)
            self.tree.fill('Jet2_lepclose', Jet2_lepclose)
            self.tree.fill('lepton_iso', lepton_iso)

            self.tree.fill('weight', event.weight)
            self.tree.fill('missingET', event.met.pt())
            self.tree.fill('numberOfElectrons', len(electrons))
            self.tree.fill('numberOfMuons', len(muons))

            self.tree.fill('rapiditySeparation_trk02',
                           abs(jets_trk02[0].eta() - jets_trk02[1].eta()))
            self.tree.fill('transverseMomentumAsymmetry_trk02',
                           (jets_trk02[0].pt() - jets_trk02[1].pt()) /
                           (jets_trk02[0].pt() + jets_trk02[1].pt()))

            self.tree.fill('Jet1_trk02_tau1', jets_trk02[0].tau1)
            self.tree.fill('Jet1_trk02_tau2', jets_trk02[0].tau2)
            self.tree.fill('Jet1_trk02_tau3', jets_trk02[0].tau3)
            self.tree.fill('Jet2_trk02_tau1', jets_trk02[1].tau1)
            self.tree.fill('Jet2_trk02_tau2', jets_trk02[1].tau2)
            self.tree.fill('Jet2_trk02_tau3', jets_trk02[1].tau3)

            Jet1_trk02_tau31 = -999.0
            Jet1_trk02_tau21 = -999.0
            Jet1_trk02_tau32 = -999.0
            Jet2_trk02_tau31 = -999.0
            Jet2_trk02_tau21 = -999.0
            Jet2_trk02_tau32 = -999.0

            if (jets_trk02[0].tau1 != 0.0):
                Jet1_trk02_tau31 = jets_trk02[0].tau3 / jets_trk02[0].tau1
                Jet1_trk02_tau21 = jets_trk02[0].tau2 / jets_trk02[0].tau1
            if (jets_trk02[0].tau2 != 0.0):
                Jet1_trk02_tau32 = jets_trk02[0].tau3 / jets_trk02[0].tau2

            if (jets_trk02[1].tau1 != 0.0):
                Jet2_trk02_tau31 = jets_trk02[1].tau3 / jets_trk02[1].tau1
                Jet2_trk02_tau21 = jets_trk02[1].tau2 / jets_trk02[1].tau1
            if (jets_trk02[1].tau2 != 0.0):
                Jet2_trk02_tau32 = jets_trk02[1].tau3 / jets_trk02[1].tau2

            self.tree.fill('Jet1_trk02_tau31', Jet1_trk02_tau31)
            self.tree.fill('Jet1_trk02_tau21', Jet1_trk02_tau21)
            self.tree.fill('Jet1_trk02_tau32', Jet1_trk02_tau32)
            self.tree.fill('Jet2_trk02_tau31', Jet2_trk02_tau31)
            self.tree.fill('Jet2_trk02_tau21', Jet2_trk02_tau21)
            self.tree.fill('Jet2_trk02_tau32', Jet2_trk02_tau32)

            # here is btag, need matching in DR
            Jet1_trk02_dR_pf04 = 999
            Jet2_trk02_dR_pf04 = 999
            for j in jets_pf04:
                pf04 = ROOT.TLorentzVector()
                pf04.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if j.tags['bf'] > 0:
                    if pf04.DeltaR(j1) < Jet1_trk02_dR_pf04:
                        Jet1_trk02_dR_pf04 = pf04.DeltaR(j1)
                    if pf04.DeltaR(j2) < Jet2_trk02_dR_pf04:
                        Jet2_trk02_dR_pf04 = pf04.DeltaR(j2)
            #print 'dr j1  ',Jet1_trk02_dR_pf04
            #print 'dr j2  ',Jet2_trk02_dR_pf04

            pdg1 = 0
            pdg2 = 0
            if Jet1_trk02_dR_pf04 < 0.3:
                pdg1 = 5
            if Jet2_trk02_dR_pf04 < 0.3:
                pdg2 = 5

            # TRF / truth b-tagging -> need at least 2 jets_pf04
            use_DELPHES = False
            jet = []
            ipdg = 0
            for i in range(len(jets_pf04)):
                if use_DELPHES == True:
                    ipdg = jets_pf04[i].tags['flav']
                    if ipdg != 4 and ipdg != 5: ipdg = 0
                else:
                    ipdg = jets_pf04_pdg[i].flavour
                jet.append([jets_pf04[i], ipdg])
            weight_1tagex = getNbTagEx(1, jet, 2)
            weight_1tagexJet1 = getNbTagEx(1, jet, 2, 0)
            weight_1tagexJet2 = getNbTagEx(1, jet, 2, 1)
            weight_2tagex = getNbTagEx(2, jet, 2)
            self.tree.fill('weight_1tagex', weight_1tagex)
            self.tree.fill('weight_2tagex', weight_2tagex)
            self.tree.fill('weight_1tagexJet1', weight_1tagexJet1)
            self.tree.fill('weight_1tagexJet2', weight_1tagexJet2)

            #MATCHING PF02 and trk02 for CORRECTION
            Jet1_trk02_dR_pf02 = 999
            Jet2_trk02_dR_pf02 = 999
            Jet1_pf02 = None
            Jet2_pf02 = None
            for j in jets_pf02:
                pf02 = ROOT.TLorentzVector()
                pf02.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf02.DeltaR(j1) < Jet1_trk02_dR_pf02:
                    Jet1_trk02_dR_pf02 = pf02.DeltaR(j1)
                    Jet1_pf02 = j
                if pf02.DeltaR(j2) < Jet2_trk02_dR_pf02:
                    Jet2_trk02_dR_pf02 = pf02.DeltaR(j2)
                    Jet2_pf02 = j
            #print 'jet1 dr ',Jet1_trk02_dR_pf02,'  pf02   ',Jet1_pf02,'  trk02  ',jets_trk02[0]
            #print 'jet2 dr ',Jet2_trk02_dR_pf02,'  pf02   ',Jet2_pf02,'  trk02  ',jets_trk02[1]

            corr1 = Jet1_pf02.p4().Pt() / j1.Pt()
            corr2 = Jet2_pf02.p4().Pt() / j2.Pt()

            #print 'corr 1  ',corr1,'   corr2  ',corr2
            #NORMAL TRK02 SD corrected jet
            p4sd1 = ROOT.TLorentzVector()
            p4sd2 = ROOT.TLorentzVector()
            p4sd1.SetPtEtaPhiM(
                jets_trk02[0].subjetsSoftDrop[0].p4().Pt() * corr1,
                jets_trk02[0].eta(), jets_trk02[0].phi(),
                jets_trk02[0].subjetsSoftDrop[0].p4().M() * corr1)

            p4sd2.SetPtEtaPhiM(
                jets_trk02[1].subjetsSoftDrop[0].p4().Pt() * corr2,
                jets_trk02[1].eta(), jets_trk02[1].phi(),
                jets_trk02[1].subjetsSoftDrop[0].p4().M() * corr2)

            sdjet1_corr = Particle(pdg1, 0, p4sd1, 1)
            sdjet2_corr = Particle(pdg2, 0, p4sd2, 1)
            fillParticle(self.tree, 'Jet1_trk02_SD_Corr', sdjet1_corr)
            fillParticle(self.tree, 'Jet2_trk02_SD_Corr', sdjet2_corr)

            #NORMAL TRK02 SD jet
            #sdjet1 = Particle(pdg1, 0, jets_trk02[0].subjetsSoftDrop[0].p4(), 1)
            #sdjet2 = Particle(pdg2, 0, jets_trk02[1].subjetsSoftDrop[0].p4(), 1)
            #fillParticle(self.tree, 'Jet1_trk02_SD', sdjet1)
            #fillParticle(self.tree, 'Jet2_trk02_SD', sdjet2)

            #CORRECTED TRK02 jet
            p4jet1_corr = ROOT.TLorentzVector()
            p4jet2_corr = ROOT.TLorentzVector()
            p4jet1_corr.SetPtEtaPhiM(jets_trk02[0].pt() * corr1,
                                     jets_trk02[0].eta(), jets_trk02[0].phi(),
                                     jets_trk02[0].m() * corr1)
            p4jet2_corr.SetPtEtaPhiM(jets_trk02[1].pt() * corr2,
                                     jets_trk02[1].eta(), jets_trk02[1].phi(),
                                     jets_trk02[1].m() * corr2)

            jet1_corr = Particle(pdg1, 0, p4jet1_corr, 1)
            jet2_corr = Particle(pdg2, 0, p4jet2_corr, 1)
            #fillParticle(self.tree, 'Jet1_trk02_Corr', jet1_corr)
            #fillParticle(self.tree, 'Jet2_trk02_Corr', jet2_corr)

            # associate MET to one jet or another based on softdrop
            sdjetmet1, sdjetmet2 = self.corrMET(
                jets_trk02[0].subjetsSoftDrop[0], pdg1,
                jets_trk02[1].subjetsSoftDrop[0], pdg2, event.met)
            fillParticle(self.tree, 'Jet1_trk02_SD_MetCorr', sdjetmet1)
            fillParticle(self.tree, 'Jet2_trk02_SD_MetCorr', sdjetmet2)

            sdjetmet1, sdjetmet2 = self.corrMET(sdjet1_corr, pdg1, sdjet2_corr,
                                                pdg2, event.met)
            fillParticle(self.tree, 'Jet1_trk02_SD_Corr_MetCorr', sdjetmet1)
            fillParticle(self.tree, 'Jet2_trk02_SD_Corr_MetCorr', sdjetmet2)

            ######################
            # trkjet04 mass info #
            ######################
            #MATCHING PF04 and trk04 for CORRECTION
            Jet1_trk04_dR_pf04 = 999
            Jet2_trk04_dR_pf04 = 999
            Jet1_pf04 = None
            Jet2_pf04 = None
            for j in jets_pf04_1500:
                pf04 = ROOT.TLorentzVector()
                pf04.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf04.DeltaR(j1) < Jet1_trk04_dR_pf04:
                    Jet1_trk04_dR_pf04 = pf04.DeltaR(j1)
                    Jet1_pf04 = j
                if pf04.DeltaR(j2) < Jet2_trk04_dR_pf04:
                    Jet2_trk04_dR_pf04 = pf04.DeltaR(j2)
                    Jet2_pf04 = j
            corr1_04 = Jet1_pf04.p4().Pt() / j1.Pt()
            corr2_04 = Jet2_pf04.p4().Pt() / j2.Pt()

            #NORMAL TRK04 SD corrected jet
            p4sd1_04 = ROOT.TLorentzVector()
            p4sd2_04 = ROOT.TLorentzVector()
            Jet1_trk04_SD_Corr_m = -1000.
            Jet2_trk04_SD_Corr_m = -1000.

            if len(jets_trk04) >= 1:
                p4sd1_04.SetPtEtaPhiM(
                    jets_trk04[0].subjetsSoftDrop[0].p4().Pt() * corr1_04,
                    jets_trk04[0].eta(), jets_trk04[0].phi(),
                    jets_trk04[0].subjetsSoftDrop[0].p4().M() * corr1_04)
                pdg1 = 0
                sdjet1_corr_04 = Particle(pdg1, 0, p4sd1_04, 1)
                Jet1_trk04_SD_Corr_m = sdjet1_corr_04.p4().M()

            if len(jets_trk04) >= 2:
                p4sd2_04.SetPtEtaPhiM(
                    jets_trk04[1].subjetsSoftDrop[0].p4().Pt() * corr2_04,
                    jets_trk04[1].eta(), jets_trk04[1].phi(),
                    jets_trk04[1].subjetsSoftDrop[0].p4().M() * corr2_04)
                pdg2 = 0
                sdjet2_corr_04 = Particle(pdg2, 0, p4sd2_04, 1)
                Jet2_trk04_SD_Corr_m = sdjet2_corr_04.p4().M()

            self.tree.fill('Jet1_trk04_SD_Corr_m', Jet1_trk04_SD_Corr_m)
            self.tree.fill('Jet2_trk04_SD_Corr_m', Jet2_trk04_SD_Corr_m)

            ######################
            # trkjet08 mass info #
            ######################
            #MATCHING PF08 and trk08 for CORRECTION
            Jet1_trk08_dR_pf08 = 999
            Jet2_trk08_dR_pf08 = 999
            Jet1_pf08 = None
            Jet2_pf08 = None
            for j in jets_pf08:
                pf08 = ROOT.TLorentzVector()
                pf08.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf08.DeltaR(j1) < Jet1_trk08_dR_pf08:
                    Jet1_trk08_dR_pf08 = pf08.DeltaR(j1)
                    Jet1_pf08 = j
                if pf08.DeltaR(j2) < Jet2_trk08_dR_pf08:
                    Jet2_trk08_dR_pf08 = pf08.DeltaR(j2)
                    Jet2_pf08 = j
            corr1_08 = Jet1_pf08.p4().Pt() / j1.Pt()
            corr2_08 = Jet2_pf08.p4().Pt() / j2.Pt()

            #NORMAL TRK08 SD corrected jet
            p4sd1_08 = ROOT.TLorentzVector()
            p4sd2_08 = ROOT.TLorentzVector()
            Jet1_trk08_SD_Corr_m = -1000.
            Jet2_trk08_SD_Corr_m = -1000.

            if len(jets_trk08) >= 1:
                p4sd1_08.SetPtEtaPhiM(
                    jets_trk08[0].subjetsSoftDrop[0].p4().Pt() * corr1_08,
                    jets_trk08[0].eta(), jets_trk08[0].phi(),
                    jets_trk08[0].subjetsSoftDrop[0].p4().M() * corr1_08)
                pdg1 = 0
                sdjet1_corr_08 = Particle(pdg1, 0, p4sd1_08, 1)
                Jet1_trk08_SD_Corr_m = sdjet1_corr_08.p4().M()

            if len(jets_trk08) >= 2:
                p4sd2_08.SetPtEtaPhiM(
                    jets_trk08[1].subjetsSoftDrop[0].p4().Pt() * corr2_08,
                    jets_trk08[1].eta(), jets_trk08[1].phi(),
                    jets_trk08[1].subjetsSoftDrop[0].p4().M() * corr2_08)
                pdg2 = 0
                sdjet2_corr_08 = Particle(pdg2, 0, p4sd2_08, 1)
                Jet2_trk08_SD_Corr_m = sdjet2_corr_08.p4().M()

            self.tree.fill('Jet1_trk08_SD_Corr_m', Jet1_trk08_SD_Corr_m)
            self.tree.fill('Jet2_trk08_SD_Corr_m', Jet2_trk08_SD_Corr_m)

            if (len(jets_trk02) > 1):
                self.tree.fill('Mj1j2_trk02',
                               self.fillMass(jets_trk02[0], jets_trk02[1]))
                self.tree.fill('Mj1j2_trk02_Corr',
                               self.fillMass(jet1_corr, jet2_corr))
                jetmet1, jetmet2 = self.corrMET(jets_trk02[0], pdg1,
                                                jets_trk02[1], pdg2, event.met)
                self.tree.fill('Mj1j2_trk02_MetCorr',
                               self.fillMass(jetmet1, jetmet2))
                #fillParticle(self.tree, 'Jet1_trk02_MetCorr', jetmet1)
                #fillParticle(self.tree, 'Jet2_trk02_MetCorr', jetmet2)

                jetmet1, jetmet2 = self.corrMET(jet1_corr, pdg1, jet2_corr,
                                                pdg2, event.met)
                self.tree.fill('Mj1j2_trk02_Corr_MetCorr',
                               self.fillMass(jetmet1, jetmet2))
                fillParticle(self.tree, 'Jet1_trk02_Corr_MetCorr', jetmet1)
                fillParticle(self.tree, 'Jet2_trk02_Corr_MetCorr', jetmet2)

            if (len(jets_pf02) > 1):
                self.tree.fill('Mj1j2_pf02',
                               self.fillMass(jets_pf02[0], jets_pf02[1]))
                jetmet1, jetmet2 = self.corrMET(jets_pf02[0], pdg1,
                                                jets_pf02[1], pdg2, event.met)
                self.tree.fill('Mj1j2_pf02_MetCorr',
                               self.fillMass(jetmet1, jetmet2))

            if (len(jets_pf04) > 1):
                self.tree.fill('Mj1j2_pf04',
                               self.fillMass(jets_pf04[0], jets_pf04[1]))
                jetmet1, jetmet2 = self.corrMET(jets_pf04[0], pdg1,
                                                jets_pf04[1], pdg2, event.met)
                self.tree.fill('Mj1j2_pf04_MetCorr',
                               self.fillMass(jetmet1, jetmet2))

            if (len(jets_pf08) > 1):
                self.tree.fill('Mj1j2_pf08',
                               self.fillMass(jets_pf08[0], jets_pf08[1]))
                jetmet1, jetmet2 = self.corrMET(jets_pf08[0], pdg1,
                                                jets_pf08[1], pdg2, event.met)
                self.tree.fill('Mj1j2_pf08_MetCorr',
                               self.fillMass(jetmet1, jetmet2))

            if (len(electrons) >= 1):
                fillParticle(self.tree, 'Electron1', electrons[0])
            if (len(electrons) >= 2):
                fillParticle(self.tree, 'Electron2', electrons[1])

            if (len(muons) >= 1): fillParticle(self.tree, 'Muon1', muons[0])
            if (len(muons) >= 2): fillParticle(self.tree, 'Muon2', muons[1])

            ###################################
            #TMVA Stuff Starts!
            ###################################

            self.bdt_Jet_trk02_tau1[0] = jets_trk02[0].tau1
            self.bdt_Jet_trk02_tau2[0] = jets_trk02[0].tau2
            self.bdt_Jet_trk02_tau3[0] = jets_trk02[0].tau3
            self.bdt_Jet_trk02_tau21[0] = Jet1_trk02_tau21
            self.bdt_Jet_trk02_tau31[0] = Jet1_trk02_tau31
            self.bdt_Jet_trk02_tau32[0] = Jet1_trk02_tau32
            self.bdt_Jet_trk02_SD_Corr_m[0] = sdjet1_corr.p4().M()
            if len(jets_trk04) >= 1:
                self.bdt_Jet_trk04_SD_Corr_m[0] = sdjet1_corr_04.p4().M()
            else:
                self.bdt_Jet_trk04_SD_Corr_m[0] = -1000.
            if len(jets_trk08) >= 1:
                self.bdt_Jet_trk08_SD_Corr_m[0] = sdjet1_corr_08.p4().M()
            else:
                self.bdt_Jet_trk08_SD_Corr_m[0] = -1000.
            mva_value = self.reader.EvaluateMVA("BDT")
            self.tree.fill('Jet1_thad_vs_QCD_tagger', mva_value)
            #
            self.bdt_Jet_trk02_tau1[0] = jets_trk02[1].tau1
            self.bdt_Jet_trk02_tau2[0] = jets_trk02[1].tau2
            self.bdt_Jet_trk02_tau3[0] = jets_trk02[1].tau3
            self.bdt_Jet_trk02_tau21[0] = Jet2_trk02_tau21
            self.bdt_Jet_trk02_tau31[0] = Jet2_trk02_tau31
            self.bdt_Jet_trk02_tau32[0] = Jet2_trk02_tau32
            self.bdt_Jet_trk02_SD_Corr_m[0] = sdjet2_corr.p4().M()
            if len(jets_trk04) >= 2:
                self.bdt_Jet_trk04_SD_Corr_m[0] = sdjet2_corr_04.p4().M()
            else:
                self.bdt_Jet_trk04_SD_Corr_m[0] = -1000.
            if len(jets_trk08) >= 2:
                self.bdt_Jet_trk08_SD_Corr_m[0] = sdjet2_corr_08.p4().M()
            else:
                self.bdt_Jet_trk08_SD_Corr_m[0] = -1000.
            mva_value = self.reader.EvaluateMVA("BDT")
            self.tree.fill('Jet2_thad_vs_QCD_tagger', mva_value)

            self.tree.tree.Fill()
Esempio n. 27
0
    def process(self, event):
        self.tree.reset()
        jets_trk02 = getattr(event, self.cfg_ana.jets_trk02_1000)
        jets_pf02 = getattr(event, self.cfg_ana.jets_pf02_1500)
        jets_pf04 = getattr(event, self.cfg_ana.jets_pf04_1000)
        jets_pf08 = getattr(event, self.cfg_ana.jets_pf08_1500)

        electrons = getattr(event, self.cfg_ana.electrons)
        muons = getattr(event, self.cfg_ana.muons)

        Jet1_trk02_dR_lep = 999
        Jet2_trk02_dR_lep = 999
        if (len(jets_trk02) >= 2 and len(jets_pf02) >= 2):

            j1 = ROOT.TLorentzVector()
            j2 = ROOT.TLorentzVector()
            j1.SetPtEtaPhiE(jets_trk02[0].pt(), jets_trk02[0].eta(),
                            jets_trk02[0].phi(), jets_trk02[0].e())
            j2.SetPtEtaPhiE(jets_trk02[1].pt(), jets_trk02[1].eta(),
                            jets_trk02[1].phi(), jets_trk02[1].e())
            if (len(electrons) != 0 and len(muons) == 0):
                e = ROOT.TLorentzVector()
                e.SetPtEtaPhiE(electrons[0].pt(), electrons[0].eta(),
                               electrons[0].phi(), electrons[0].e())
                Jet1_dR = j1.DeltaR(e)
                Jet2_dR = j2.DeltaR(e)
            if (len(electrons) == 0 and len(muons) != 0):
                m = ROOT.TLorentzVector()
                m.SetPtEtaPhiE(muons[0].pt(), muons[0].eta(), muons[0].phi(),
                               muons[0].e())
                Jet1_dR = j1.DeltaR(m)
                Jet2_dR = j2.DeltaR(m)
            if (len(electrons) != 0 and len(muons) != 0):
                isElectron = False
                isMuon = False
                if (electrons[0].pt() > muons[0].pt()): isElectron = True
                else: isMuon = True
                l = ROOT.TLorentzVector()
                if isElectron:
                    l.SetPtEtaPhiE(electrons[0].pt(), electrons[0].eta(),
                                   electrons[0].phi(), electrons[0].e())
                if isMuon:
                    l.SetPtEtaPhiE(muons[0].pt(), muons[0].eta(),
                                   muons[0].phi(), muons[0].e())
                Jet1_trk02_dR_lep = j1.DeltaR(l)
                Jet2_trk02_dR_lep = j2.DeltaR(l)

            self.tree.fill('Jet1_trk02_dR_lep', Jet1_trk02_dR_lep)
            self.tree.fill('Jet2_trk02_dR_lep', Jet2_trk02_dR_lep)

            self.tree.fill('weight', event.weight)
            self.tree.fill('missingET', event.met.pt())
            self.tree.fill('numberOfElectrons', len(electrons))
            self.tree.fill('numberOfMuons', len(muons))

            self.tree.fill('rapiditySeparation_trk02',
                           abs(jets_trk02[0].eta() - jets_trk02[1].eta()))
            self.tree.fill('transverseMomentumAsymmetry_trk02',
                           (jets_trk02[0].pt() - jets_trk02[1].pt()) /
                           (jets_trk02[0].pt() + jets_trk02[1].pt()))

            self.tree.fill('Jet1_trk02_tau1', jets_trk02[0].tau1)
            self.tree.fill('Jet1_trk02_tau2', jets_trk02[0].tau2)
            self.tree.fill('Jet1_trk02_tau3', jets_trk02[0].tau3)
            self.tree.fill('Jet2_trk02_tau1', jets_trk02[1].tau1)
            self.tree.fill('Jet2_trk02_tau2', jets_trk02[1].tau2)
            self.tree.fill('Jet2_trk02_tau3', jets_trk02[1].tau3)

            Jet1_trk02_tau31 = -999.0
            Jet1_trk02_tau21 = -999.0
            Jet1_trk02_tau32 = -999.0
            Jet2_trk02_tau31 = -999.0
            Jet2_trk02_tau21 = -999.0
            Jet2_trk02_tau32 = -999.0

            if (jets_trk02[0].tau1 != 0.0):
                Jet1_trk02_tau31 = jets_trk02[0].tau3 / jets_trk02[0].tau1
                Jet1_trk02_tau21 = jets_trk02[0].tau2 / jets_trk02[0].tau1
            if (jets_trk02[0].tau2 != 0.0):
                Jet1_trk02_tau32 = jets_trk02[0].tau3 / jets_trk02[0].tau2

            if (jets_trk02[1].tau1 != 0.0):
                Jet2_trk02_tau31 = jets_trk02[1].tau3 / jets_trk02[1].tau1
                Jet2_trk02_tau21 = jets_trk02[1].tau2 / jets_trk02[1].tau1
            if (jets_trk02[1].tau2 != 0.0):
                Jet2_trk02_tau32 = jets_trk02[1].tau3 / jets_trk02[1].tau2

            self.tree.fill('Jet1_trk02_tau31', Jet1_trk02_tau31)
            self.tree.fill('Jet1_trk02_tau21', Jet1_trk02_tau21)
            self.tree.fill('Jet1_trk02_tau32', Jet1_trk02_tau32)
            self.tree.fill('Jet2_trk02_tau31', Jet2_trk02_tau31)
            self.tree.fill('Jet2_trk02_tau21', Jet2_trk02_tau21)
            self.tree.fill('Jet2_trk02_tau32', Jet2_trk02_tau32)

            # here is btag, need matching in DR
            Jet1_trk02_dR_pf04 = 999
            Jet2_trk02_dR_pf04 = 999
            for j in jets_pf04:
                pf04 = ROOT.TLorentzVector()
                pf04.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if j.tags['bf'] > 0:
                    if pf04.DeltaR(j1) < Jet1_trk02_dR_pf04:
                        Jet1_trk02_dR_pf04 = pf04.DeltaR(j1)
                    if pf04.DeltaR(j2) < Jet2_trk02_dR_pf04:
                        Jet2_trk02_dR_pf04 = pf04.DeltaR(j2)
            #print 'dr j1  ',Jet1_trk02_dR_pf04
            #print 'dr j2  ',Jet2_trk02_dR_pf04

            pdg1 = 0
            pdg2 = 0
            if Jet1_trk02_dR_pf04 < 0.3:
                pdg1 = 5
            if Jet2_trk02_dR_pf04 < 0.3:
                pdg2 = 5

            #MATCHING PF02 and trk02 for CORRECTION
            Jet1_trk02_dR_pf02 = 999
            Jet2_trk02_dR_pf02 = 999
            Jet1_pf02 = None
            Jet2_pf02 = None
            for j in jets_pf02:
                pf02 = ROOT.TLorentzVector()
                pf02.SetPtEtaPhiE(j.pt(), j.eta(), j.phi(), j.e())
                if pf02.DeltaR(j1) < Jet1_trk02_dR_pf02:
                    Jet1_trk02_dR_pf02 = pf02.DeltaR(j1)
                    Jet1_pf02 = j
                if pf02.DeltaR(j2) < Jet2_trk02_dR_pf02:
                    Jet2_trk02_dR_pf02 = pf02.DeltaR(j2)
                    Jet2_pf02 = j
            #print 'jet1 dr ',Jet1_trk02_dR_pf02,'  pf02   ',Jet1_pf02,'  trk02  ',jets_trk02[0]
            #print 'jet2 dr ',Jet2_trk02_dR_pf02,'  pf02   ',Jet2_pf02,'  trk02  ',jets_trk02[1]

            corr1 = Jet1_pf02.p4().Pt() / j1.Pt()
            corr2 = Jet2_pf02.p4().Pt() / j2.Pt()

            #print 'corr 1  ',corr1,'   corr2  ',corr2
            #NORMAL TRK02 SD corrected jet
            p4sd1 = ROOT.TLorentzVector()
            p4sd2 = ROOT.TLorentzVector()
            p4sd1.SetPtEtaPhiM(
                jets_trk02[0].subjetsSoftDrop[0].p4().Pt() * corr1,
                jets_trk02[0].eta(), jets_trk02[0].phi(),
                jets_trk02[0].subjetsSoftDrop[0].p4().M() * corr1)

            p4sd2.SetPtEtaPhiM(
                jets_trk02[1].subjetsSoftDrop[0].p4().Pt() * corr2,
                jets_trk02[1].eta(), jets_trk02[1].phi(),
                jets_trk02[1].subjetsSoftDrop[0].p4().M() * corr2)

            sdjet1_corr = Particle(pdg1, 0, p4sd1, 1)
            sdjet2_corr = Particle(pdg2, 0, p4sd2, 1)
            fillParticle(self.tree, 'Jet1_trk02_SD_Corr', sdjet1_corr)
            fillParticle(self.tree, 'Jet2_trk02_SD_Corr', sdjet2_corr)

            #NORMAL TRK02 SD jet
            #sdjet1 = Particle(pdg1, 0, jets_trk02[0].subjetsSoftDrop[0].p4(), 1)
            #sdjet2 = Particle(pdg2, 0, jets_trk02[1].subjetsSoftDrop[0].p4(), 1)
            #fillParticle(self.tree, 'Jet1_trk02_SD', sdjet1)
            #fillParticle(self.tree, 'Jet2_trk02_SD', sdjet2)

            #CORRECTED TRK02 jet
            p4jet1_corr = ROOT.TLorentzVector()
            p4jet2_corr = ROOT.TLorentzVector()
            p4jet1_corr.SetPtEtaPhiM(jets_trk02[0].pt() * corr1,
                                     jets_trk02[0].eta(), jets_trk02[0].phi(),
                                     jets_trk02[0].m() * corr1)
            p4jet2_corr.SetPtEtaPhiM(jets_trk02[1].pt() * corr2,
                                     jets_trk02[1].eta(), jets_trk02[1].phi(),
                                     jets_trk02[1].m() * corr2)

            jet1_corr = Particle(pdg1, 0, p4jet1_corr, 1)
            jet2_corr = Particle(pdg2, 0, p4jet2_corr, 1)
            #fillParticle(self.tree, 'Jet1_trk02_Corr', jet1_corr)
            #fillParticle(self.tree, 'Jet2_trk02_Corr', jet2_corr)

            # associate MET to one jet or another based on softdrop
            sdjetmet1, sdjetmet2 = self.corrMET(
                jets_trk02[0].subjetsSoftDrop[0], pdg1,
                jets_trk02[1].subjetsSoftDrop[0], pdg2, event.met)
            fillParticle(self.tree, 'Jet1_trk02_SD_MetCorr', sdjetmet1)
            fillParticle(self.tree, 'Jet2_trk02_SD_MetCorr', sdjetmet2)

            sdjetmet1, sdjetmet2 = self.corrMET(sdjet1_corr, pdg1, sdjet2_corr,
                                                pdg2, event.met)
            fillParticle(self.tree, 'Jet1_trk02_SD_Corr_MetCorr', sdjetmet1)
            fillParticle(self.tree, 'Jet2_trk02_SD_Corr_MetCorr', sdjetmet2)

            if (len(jets_trk02) > 1):
                self.tree.fill('Mj1j2_trk02',
                               self.fillMass(jets_trk02[0], jets_trk02[1]))
                self.tree.fill('Mj1j2_trk02_Corr',
                               self.fillMass(jet1_corr, jet2_corr))
                jetmet1, jetmet2 = self.corrMET(jets_trk02[0], pdg1,
                                                jets_trk02[1], pdg2, event.met)
                self.tree.fill('Mj1j2_trk02_MetCorr',
                               self.fillMass(jetmet1, jetmet2))
                #fillParticle(self.tree, 'Jet1_trk02_MetCorr', jetmet1)
                #fillParticle(self.tree, 'Jet2_trk02_MetCorr', jetmet2)

                jetmet1, jetmet2 = self.corrMET(jet1_corr, pdg1, jet2_corr,
                                                pdg2, event.met)
                self.tree.fill('Mj1j2_trk02_Corr_MetCorr',
                               self.fillMass(jetmet1, jetmet2))
                fillParticle(self.tree, 'Jet1_trk02_Corr_MetCorr', jetmet1)
                fillParticle(self.tree, 'Jet2_trk02_Corr_MetCorr', jetmet2)

            if (len(jets_pf02) > 1):
                self.tree.fill('Mj1j2_pf02',
                               self.fillMass(jets_pf02[0], jets_pf02[1]))
                jetmet1, jetmet2 = self.corrMET(jets_pf02[0], pdg1,
                                                jets_pf02[1], pdg2, event.met)
                self.tree.fill('Mj1j2_pf02_MetCorr',
                               self.fillMass(jetmet1, jetmet2))

            if (len(jets_pf04) > 1):
                self.tree.fill('Mj1j2_pf04',
                               self.fillMass(jets_pf04[0], jets_pf04[1]))
                jetmet1, jetmet2 = self.corrMET(jets_pf04[0], pdg1,
                                                jets_pf04[1], pdg2, event.met)
                self.tree.fill('Mj1j2_pf04_MetCorr',
                               self.fillMass(jetmet1, jetmet2))

            if (len(jets_pf08) > 1):
                self.tree.fill('Mj1j2_pf08',
                               self.fillMass(jets_pf08[0], jets_pf08[1]))
                jetmet1, jetmet2 = self.corrMET(jets_pf08[0], pdg1,
                                                jets_pf08[1], pdg2, event.met)
                self.tree.fill('Mj1j2_pf08_MetCorr',
                               self.fillMass(jetmet1, jetmet2))

            if (len(electrons) >= 1):
                fillParticle(self.tree, 'Electron1', electrons[0])
            if (len(electrons) >= 2):
                fillParticle(self.tree, 'Electron2', electrons[1])

            if (len(muons) >= 1): fillParticle(self.tree, 'Muon1', muons[0])
            if (len(muons) >= 2): fillParticle(self.tree, 'Muon2', muons[1])

            ###################################
            #TMVA Stuff Starts!
            ###################################

            ###################################
            #qcd Background >>>>>>>>>>>>>>>>>
            ###################################

            #            varlist = [
            #                                "Jet1_tau32",
            #                                "Jet2_tau32",
            #                                "Jet1_tau21",
            #                                "Jet2_tau21",
            #                                "softDroppedJet1_m",
            #                                "softDroppedJet2_m",
            #                                "Jet1_trimmedProngMaxPtRatio",
            #                                "Jet1_trimmedProngMinPtRatio",
            #                                "Jet2_trimmedProngMaxPtRatio",
            #                                "Jet2_trimmedProngMinPtRatio",
            #            ]
            #
            #            inputs = ROOT.vector('string')()
            #            for v in varlist:
            #                inputs.push_back(v)
            #
            #            mva = ROOT.ReadQCD(inputs)
            #            values = ROOT.vector('double')()
            #
            #            values.push_back(Jet1_tau32)
            #            values.push_back(Jet2_tau32)
            #            values.push_back(Jet1_tau21)
            #            values.push_back(Jet2_tau21)
            #            values.push_back(jets[0].subjetsSoftDrop[0].m())
            #            values.push_back(jets[1].subjetsSoftDrop[0].m())
            #            values.push_back(Jet1_trimmedProngMaxPtRatio)
            #            values.push_back(Jet1_trimmedProngMinPtRatio)
            #            values.push_back(Jet2_trimmedProngMaxPtRatio)
            #            values.push_back(Jet2_trimmedProngMinPtRatio)
            #
            #            mva_value=mva.GetMvaValue(values)
            #            self.tree.fill('BDTvariable_qcd', mva_value)

            ###################################
            #TMVA Stuff Ends!
            ###################################

            self.tree.tree.Fill()
Esempio n. 28
0
    def process(self, event):
        leptons = getattr(event, self.cfg_ana.leptons)
        jets = getattr(event, self.cfg_ana.jets)
        sqrts = self.cfg_ana.sqrts
        w_mass = self.cfg_ana.w_mass
        top_mass = self.cfg_ana.top_mass

        jets_p4 = []
        btags = []
        for jet in jets:
            jets_p4.append(jet.p4())
            btags.append(jet.logbtag)

        if len(leptons) == 0:
            event.success = -1
        else:
            myTopConstrainer = topConstrainer(jets_p4, btags, leptons[0].p4(), sqrts, w_mass, top_mass)
            succeded, chi2 = myTopConstrainer.rescale(1E-4)

            if succeded:
                pRec = myTopConstrainer.p_ini
                tophadRec = Particle(top_pdgid, top_charge, (pRec[0]+pRec[1]+pRec[2]) )
                whadRec = Particle(w_pdgid, w_charge, (pRec[0]+pRec[1]) )
                toplepRec = Particle(top_pdgid, top_charge, (pRec[3]+pRec[4]+pRec[5]) )
                wlepRec = Particle(w_pdgid, w_charge, (pRec[4]+pRec[5]) )
                missingMassRec = Particle( 0, 0, pRec[5] )

                pFit = myTopConstrainer.p_out
                tophadFit = Particle(top_pdgid, top_charge, (pFit[0]+pFit[1]+pFit[2]) )
                whadFit = Particle(w_pdgid, w_charge, (pFit[0]+pFit[1]) )
                toplepFit = Particle(top_pdgid, top_charge, (pFit[3]+pFit[4]+pFit[5]) )
                wlepFit = Particle(w_pdgid, w_charge, (pFit[4]+pFit[5]) )
                missingMassFit = Particle( 0, 0, pFit[5] )

                chi2_tophadRec = ( (tophadRec.m() - self.cfg_ana.tophadRec_m) / self.cfg_ana.tophadRec_w )**2
                chi2_whadRec = ( (whadRec.m() - self.cfg_ana.whadRec_m) / self.cfg_ana.whadRec_w )**2
                chi2_toplepRec = ( (toplepRec.m() - self.cfg_ana.toplepRec_m) / self.cfg_ana.toplepRec_w )**2
                chi2_wlepRec = ( (wlepRec.m() - self.cfg_ana.wlepRec_m) / self.cfg_ana.wlepRec_w )**2
                chi2_top_constrainer = chi2_tophadRec + chi2_whadRec + chi2_toplepRec + chi2_wlepRec

                setattr(event, 'success', 1)
                setattr(event, 'chi2_algorithm', chi2)

                setattr(event, 'tophadRec', tophadRec)
                setattr(event, 'whadRec', whadRec)
                setattr(event, 'toplepRec', toplepRec)
                setattr(event, 'wlepRec', wlepRec)
                setattr(event, 'missingMassRec', missingMassRec)
                setattr(event, 'tophadFit', tophadFit)
                setattr(event, 'whadFit', whadFit)
                setattr(event, 'toplepFit', toplepFit)
                setattr(event, 'wlepFit', wlepFit)
                setattr(event, 'missingMassFit', missingMassFit)

                setattr(event, 'chi2_tophadRec', chi2_tophadRec)
                setattr(event, 'chi2_whadRec', chi2_whadRec)
                setattr(event, 'chi2_toplepRec', chi2_toplepRec)
                setattr(event, 'chi2_wlepRec', chi2_wlepRec)

                setattr(event, 'chi2_top_constrainer', chi2_top_constrainer)

            else:
                event.success = 0
Esempio n. 29
0
    def process(self, event):
        self.tree.reset()
        htatas = getattr(event, self.cfg_ana.htatas)
        hbbs = getattr(event, self.cfg_ana.hbbs)
        met = event.met

        htatas.sort(key=lambda x: abs(x.m() - 125.))
        hbbs.sort(key=lambda x: abs(x.m() - 125.))

        bs = event.selected_bs
        taus = event.selected_taus
        lights = event.selected_lights
        leptons = event.selected_leptons

        #print '-------------------'

        #print len(htatas), len(hbbs)
        #print len(taus), len(bs)

        #for tau in taus:
        #    print tau.charge

        #print ROOT.return2()
        #f = Foo()
        #f.bar() #and you will see "Hello" on the screen
        '''
        mVisA = 10.; # mass of visible object on side A.  Must be >=0.
        pxA = 20.; # x momentum of visible object on side A.
        pyA = 30.; # y momentum of visible object on side A.

        mVisB = 10.; # mass of visible object on side B.  Must be >=0.
        pxB = -20.; # x momentum of visible object on side B.
        pyB = -30.; # y momentum of visible object on side B.

        pxMiss = -5.; # x component of missing transverse momentum.
        pyMiss = -5.; # y component of missing transverse momentum.

        chiA = 4.; # hypothesised mass of invisible on side A.  Must be >=0.
        chiB = 7.; # hypothesised mass of invisible on side B.  Must be >=0.

        desiredPrecisionOnMt2 = 0.; # Must be >=0.  If 0 alg aims for machine precision.  if >0, MT2 computed to supplied absolute precision.
        useDeciSectionsInitially=True

        asymm_mt2 = asymm_mt2_lester_bisect()
        print '-----------------------------------------'
        MT2 =  asymm_mt2.get_mT2(
           mVisA, pxA, pyA,
           mVisB, pxB, pyB,
           pxMiss, pyMiss,
           chiA, chiB,
           desiredPrecisionOnMt2,
           useDeciSectionsInitially)
        
        print 'MT2', MT2
        '''

        # fully hadronic selection
        if len(taus) > 1 and len(bs) > 1 and len(leptons) == 0:

            self.tree.fill('weight', event.weight)
            #print  event.weight
            fillParticle(self.tree, 'ta1', taus[0])
            fillParticle(self.tree, 'ta2', taus[1])
            fillParticle(self.tree, 'b1', bs[0])
            fillParticle(self.tree, 'b2', bs[1])
            '''
            def mTsq(bT, cT, mB, mC):
                eB = math.sqrt(mB*mB+ bT*bT)
                eC = math.sqrt(mC*mC+ cT*cT)
                return mB*mB+mC*mC+2*(eB*eC - bT*cT)

            def smT2(bt1, bt2):
                return max(math.sqrt(bt1),math.sqrt(bt2))

            mTsq1 = mTsq(taus[0].p4().Vect().XYvector(), bs[0].p4().Vect().XYvector(), taus[0].p4().M(), bs[0].p4().M())
            mTsq2 = mTsq(taus[1].p4().Vect().XYvector(), bs[1].p4().Vect().XYvector(), taus[1].p4().M(), bs[1].p4().M())

            smTsq = smT2(mTsq1, mTsq2)
            #print mTsq1, mTsq2, smTsq
            '''

            mVisA = bs[0].p4().M()
            # mass of visible object on side A.  Must be >=0.
            pxA = bs[0].p4().Px()
            # x momentum of visible object on side A.
            pyA = bs[0].p4().Py()
            # y momentum of visible object on side A.

            mVisB = bs[1].p4().M()
            # mass of visible object on side A.  Must be >=0.
            pxB = bs[1].p4().Px()
            # x momentum of visible object on side A.
            pyB = bs[1].p4().Py()
            # y momentum of visible object on side A.

            pxMiss = taus[0].p4().Px() + taus[1].p4().Px() + met.p4().Px(
            )  # x component of missing transverse momentum.
            pyMiss = taus[0].p4().Py() + taus[1].p4().Py() + met.p4().Py(
            )  # x component of missing transverse momentum.

            chiA = taus[0].p4().M()
            # hypothesised mass of invisible on side A.  Must be >=0.
            chiB = taus[1].p4().M()
            # hypothesised mass of invisible on side B.  Must be >=0.

            desiredPrecisionOnMt2 = 0.
            # Must be >=0.  If 0 alg aims for machine precision.  if >0, MT2 computed to supplied absolute precision.
            useDeciSectionsInitially = True

            asymm_mt2 = asymm_mt2_lester_bisect()

            MT2 = asymm_mt2.get_mT2(mVisA, pxA, pyA, mVisB, pxB, pyB, pxMiss,
                                    pyMiss, chiA, chiB, desiredPrecisionOnMt2,
                                    useDeciSectionsInitially)

            #print 'MT2', MT2

            self.tree.fill('mT2', MT2)

            if len(lights) > 0:
                fillParticle(self.tree, 'j1', lights[0])
                if len(lights) > 1:
                    fillParticle(self.tree, 'j2', lights[1])

            def computeMT(taup4, metp4):
                scalar_prod = taup4.Px() * metp4.Px() + taup4.Py() * metp4.Py()
                return math.sqrt(2 * (taup4.Pt() * metp4.Pt() - scalar_prod))

            mt1 = computeMT(taus[0].p4(), met.p4())
            mt2 = computeMT(taus[1].p4(), met.p4())

            self.tree.fill('ta1_mt', mt1)
            self.tree.fill('ta2_mt', mt2)

            st = taus[0].p4().Pt() + taus[1].p4().Pt() + bs[0].p4().Pt(
            ) + bs[0].p4().Pt() + met.p4().Pt()
            self.tree.fill('sT', st)

            fillMet(self.tree, 'met', met)
            fillParticle(self.tree, 'htata', htatas[0])
            fillParticle(self.tree, 'hbb', hbbs[0])

            htata_metcorr_p4 = taus[0].p4() + taus[1].p4() + met.p4()
            htata_metcorr = Particle(25, 0, htata_metcorr_p4, 1)

            fillParticle(self.tree, 'htata_metcorr', htata_metcorr)

            hh = Resonance(htatas[0], hbbs[0], 25)
            hh_metcorr = Resonance(htata_metcorr, hbbs[0], 25)

            fillParticle(self.tree, 'hh', hh)
            fillParticle(self.tree, 'hh_metcorr', hh_metcorr)

            self.tree.fill('nbjets', len(event.selected_bs))
            self.tree.fill('nljets', len(event.selected_lights))
            self.tree.fill(
                'njets',
                len(event.selected_lights) + len(event.selected_bs) +
                len(event.selected_taus))
            self.tree.fill('nlep', len(event.selected_leptons))
            self.tree.fill('ntajets', len(event.selected_taus))

            drbb = deltaR(bs[0], bs[1])
            drtata = deltaR(taus[0], taus[1])

            self.tree.fill('drtata', drtata)
            self.tree.fill('drbb', drbb)

            # here fill all variables for BDT
            self.bdt_ta1_pt[0] = taus[0].p4().Pt()
            self.bdt_ta1_eta[0] = taus[0].p4().Eta()
            self.bdt_ta1_phi[0] = taus[0].p4().Phi()
            self.bdt_ta2_pt[0] = taus[1].p4().Pt()
            self.bdt_ta2_eta[0] = taus[1].p4().Eta()
            self.bdt_ta2_phi[0] = taus[1].p4().Phi()
            self.bdt_b1_pt[0] = bs[0].p4().Pt()
            self.bdt_b1_eta[0] = bs[0].p4().Eta()
            self.bdt_b1_phi[0] = bs[0].p4().Phi()
            self.bdt_b2_pt[0] = bs[1].p4().Pt()
            self.bdt_b2_eta[0] = bs[1].p4().Eta()
            self.bdt_b2_phi[0] = bs[1].p4().Phi()
            self.bdt_met_pt[0] = met.p4().Pt()
            self.bdt_met_phi[0] = met.p4().Phi()
            self.bdt_met_px[0] = met.p4().Px()
            self.bdt_met_py[0] = met.p4().Py()
            self.bdt_htata_pt[0] = htatas[0].p4().Pt()
            self.bdt_htata_eta[0] = htatas[0].p4().Eta()
            self.bdt_htata_phi[0] = htatas[0].p4().Phi()
            self.bdt_htata_m[0] = htatas[0].p4().M()
            self.bdt_hbb_pt[0] = hbbs[0].p4().Pt()
            self.bdt_hbb_eta[0] = hbbs[0].p4().Eta()
            self.bdt_hbb_phi[0] = hbbs[0].p4().Phi()
            self.bdt_hbb_m[0] = hbbs[0].p4().M()
            self.bdt_hh_pt[0] = hh.p4().Pt()
            self.bdt_hh_eta[0] = hh.p4().Eta()
            self.bdt_hh_phi[0] = hh.p4().Phi()
            self.bdt_hh_m[0] = hh.p4().M()
            self.bdt_ta1_mt[0] = mt1
            self.bdt_ta2_mt[0] = mt2
            self.bdt_mT2[0] = MT2
            self.bdt_sT[0] = st
            self.bdt_njets[0] = len(event.selected_lights) + len(
                event.selected_bs) + len(event.selected_taus)
            self.bdt_nbjets[0] = len(event.selected_bs)
            self.bdt_ntajets[0] = len(event.selected_taus)
            #self.bdt_nlep     [0] =  len(event.selected_leptons)

            #print MT2, ",", s ,",", len(event.selected_lights) + len(event.selected_bs) + len(event.selected_taus), ",", len(event.selected_bs) ,",", len(event.selected_taus) ,",", len(event.selected_leptons)t

            mva_value = self.reader.EvaluateMVA("BDT")
            #print mva_value
            self.tree.fill('tmva_bdt', mva_value)
            self.tree.tree.Fill()