Example #1
0
 def getDphiJ1MET(self):
     dphi_j1met = -99.
     met_phi = self.met_phi
     if self.n_jet > 0:
         j1phi = self.jet_phi[0]
         dphi_j1met = TVector2.Phi_mpi_pi(j1phi - met_phi)
     return dphi_j1met
Example #2
0
def RotateEvent(lep, jets, met_phi, phi):
    """Takes in LorentzVector lep, jets and rotates each along the Z axis by
    an angle phi
    @==========================================================
    @ Parameters
    lep: LorentzVector containing lepton information
    jets: Array of LorentzVectors containing jet information
    phi: Angle between 0 and 2 pi
    @==========================================================
    @ Return
    A rotated LorentzVector
    """
    # Missing Azimuthal Energy
    met_phi = TVector2.Phi_mpi_pi(met_phi + phi)

    # Lepton
    lep_new = TLorentzVector(lep)
    lep_new.RotateZ(phi)

    # Jets
    jets_new = []
    for j in jets:
        jets_new += [TLorentzVector(j)]
        j_new = jets_new[-1]
        j_new.btag = j.btag
        j_new.RotateZ(phi)

    return lep_new, jets_new, met_phi
Example #3
0
 def isMatched(self, eta, phi, genParticles, gpIdx=[], minDR=0.5):
     belong = -1
     for iGen in gpIdx:
         geta, gphi = genParticles.eta[iGen], genParticles.phi[iGen]
         deta = geta - eta
         dphi = TVector2.Phi_mpi_pi(gphi - phi)
         dR = TMath.Sqrt(deta**2 + dphi**2)
         if dR < minDR:
             belong = iGen
             break
     return belong
Example #4
0
 def getDphiL1MET(self, l1phi):
     dphi_l1met = -99.
     met_phi = self.met_phi
     if l1phi:
         dphi_l1met = TVector2.Phi_mpi_pi(l1phi - met_phi)
     return dphi_l1met
Example #5
0
def Calculate_DeltaPhi(phi_1, phi_2):
    dphi = TVector2.Phi_mpi_pi(phi_1 - phi_2)
    return fabs(dphi)
Example #6
0
    def process(self, event):
        self.tree.reset()

        # just the ll pair added into a particle for convenience
        higgses = event.higgses

        leptons = event.selected_leptons
        leptons.sort(key=lambda x: x.pt(), reverse=True)

        jets = event.jets_nolepton
        jets.sort(key=lambda x: x.pt(), reverse=True)

        met = event.met
        '''
        for jet in jets:
           print jet
        '''

        sign = lambda x: x and (1, -1)[x < 0]
        same_sign_pair = []
        # this gives orders pairs (so twice as many as we need)
        for i in itertools.permutations(leptons, 2):
            #print 'new lep pair', i[0], i[1]
            if sign(i[0].pdgid()) == sign(i[1].pdgid()):
                same_sign_pair.append(i)

        if len(same_sign_pair) > 0 and len(jets) > 1:

            self.tree.fill('weight', 1.)
            self.tree.fill('njets', len(jets))
            self.tree.fill('nleptons', len(leptons))

            l1 = leptons[0]
            l2 = leptons[1]
            j1 = jets[0]
            j2 = jets[1]

            fillParticle(self.tree, 'lpt1', l1)
            fillParticle(self.tree, 'lpt2', l2)

            fillParticle(self.tree, 'jpt1', j1)
            fillParticle(self.tree, 'jpt2', j2)

            leptons.sort(key=lambda x: abs(x.eta()))
            jets.sort(key=lambda x: abs(x.eta()))

            l1 = leptons[0]
            l2 = leptons[1]
            j1 = jets[0]
            j2 = jets[1]

            fillParticle(self.tree, 'leta1', l1)
            fillParticle(self.tree, 'leta2', l2)

            fillParticle(self.tree, 'jeta1', j1)
            fillParticle(self.tree, 'jeta2', j2)

            if abs(l1.pdgid()) == abs(l2.pdgid()):
                self.tree.fill('is_sf', 1.0)
                self.tree.fill('is_of', 0.0)
            else:
                self.tree.fill('is_sf', 0.0)
                self.tree.fill('is_of', 1.0)

            higgs = higgses[0]
            mll = higgs.m()

            dphi_ll = TVector2.Phi_mpi_pi(l1.phi() - l2.phi())
            dphi_jj = TVector2.Phi_mpi_pi(j1.phi() - j2.phi())
            dphi_llmet = TVector2.Phi_mpi_pi(higgs.phi() - event.met.phi())

            mt = math.sqrt(2 * higgs.pt() * event.met.pt() *
                           (1 - math.cos(dphi_llmet)))

            mll = higgs.m()
            ptll = higgs.pt()
            met = event.met.pt()
            hpx = higgs.pt() * math.cos(higgs.phi())
            hpy = higgs.pt() * math.sin(higgs.phi())
            metx = event.met.pt() * math.cos(event.met.phi())
            mety = event.met.pt() * math.sin(event.met.phi())
            met_d_ptll = metx * hpx + mety * hpy
            mr = math.sqrt((mll**2 - met_d_ptll + math.sqrt(
                (mll**2 + ptll**2) * (mll**2 + met**2))))

            self.tree.fill('mt', mt)
            self.tree.fill('mr', mr)

            mjj = math.sqrt((j1.e() + j2.e())**2 -
                            (j1.p4().Px() + j2.p4().Px())**2 -
                            (j1.p4().Py() + j2.p4().Py())**2 -
                            (j1.p4().Pz() + j2.p4().Pz())**2)
            ptjj = math.sqrt((j1.p4().Px() + j2.p4().Px())**2 +
                             (j1.p4().Py() + j2.p4().Py())**2)

            self.tree.fill('mll', mll)
            self.tree.fill('mjj', mjj)

            self.tree.fill('ptll', higgs.pt())
            self.tree.fill('ptjj', ptjj)

            deta_ll = abs(l1.eta() - l2.eta())
            deta_jj = abs(j1.eta() - j2.eta())

            self.tree.fill('detall', deta_ll)
            self.tree.fill('detajj', deta_jj)

            #mll2 = math.sqrt( (l1.e()+l2.e())**2 - (l1.p4().Px()+l2.p4().Px())**2  - (l1.p4().Py()+l2.p4().Py())**2  - (l1.p4().Pz()+l2.p4().Pz())**2 )

            self.tree.fill('dphill', abs(dphi_ll))
            self.tree.fill('dphijj', abs(dphi_jj))
            self.tree.fill('dphillmet', abs(dphi_llmet))

            fillMet(self.tree, 'met', event.met)

            self.tree.tree.Fill()
Example #7
0
    def process(self, event):
        self.tree.reset()
        higgses = event.higgses

        if len(higgses) > 0:
            self.tree.fill('weight', event.weight)
            print len(higgses)

            # Reco Higgs
            higgs = higgses[0]
            l1 = higgs.legs[0]
            l2 = higgs.legs[1]

            fillParticle(self.tree, 'll', higgs)
            fillMet(self.tree, 'met', event.met)

            fillLepton(self.tree, 'l1', l1)
            fillLepton(self.tree, 'l2', l2)

            if abs(l1.pdgid()) == abs(l2.pdgid()):
                self.tree.fill('is_sf', 1.0)
                self.tree.fill('is_of', 0.0)
            else:
                self.tree.fill('is_sf', 0.0)
                self.tree.fill('is_of', 1.0)

            hpx = higgs.pt() * math.cos(higgs.phi())
            hpy = higgs.pt() * math.sin(higgs.phi())
            metx = event.met.pt() * math.cos(event.met.phi())
            mety = event.met.pt() * math.sin(event.met.phi())

            pth = math.sqrt((hpx + metx)**2 + (hpy + mety)**2)

            dphi_ll = TVector2.Phi_mpi_pi(l1.phi() - l2.phi())
            dphi_llmet = TVector2.Phi_mpi_pi(higgs.phi() - event.met.phi())

            mt = math.sqrt(2 * higgs.pt() * event.met.pt() *
                           (1 - math.cos(dphi_llmet)))

            mll = higgs.m()
            ptll = higgs.pt()
            met = event.met.pt()

            met_d_ptll = metx * hpx + mety * hpy
            mr = math.sqrt((mll**2 - met_d_ptll + math.sqrt(
                (mll**2 + ptll**2) * (mll**2 + met**2))))

            pl1 = ROOT.TLorentzVector()
            pl2 = ROOT.TLorentzVector()
            pMet = ROOT.TVector3()

            pl1.SetPtEtaPhiM(l1.pt(), l1.eta(), l1.phi(), l1.m())
            pl2.SetPtEtaPhiM(l2.pt(), l2.eta(), l2.phi(), l2.m())
            pMet.SetPtEtaPhi(event.met.pt(), 0.0, event.met.phi())

            def calcMR(L1, L2):
                E = L1.P() + L2.P()
                Pz = L1.Pz() + L2.Pz()
                MR = math.sqrt(E * E - Pz * Pz)
                return MR

            def calcMRNEW(L1, L2, M):
                vI = M + L1.Vect() + L2.Vect()
                vI.SetZ(0.0)
                PpQ = calcMR(L1, L2)
                vptx = (L1 + L2).Px()
                vpty = (L1 + L2).Py()
                vpt = ROOT.TVector3()
                vpt.SetXYZ(vptx, vpty, 0.0)
                MR2 = 0.5 * (PpQ * PpQ - vpt.Dot(vI) + PpQ *
                             sqrt(PpQ * PpQ + vI.Dot(vI) - 2. * vI.Dot(vpt)))
                return MR2

            mr = 2 * math.sqrt(calcMRNEW(pl1, pl2, pMet))

            self.tree.fill('mt', mt)
            self.tree.fill('mr', mr)
            self.tree.fill('higgs_pt', pth)
            self.tree.fill('dphi_ll', abs(dphi_ll))
            self.tree.fill('dphi_llmet', abs(dphi_llmet))
            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))

            self.tree.tree.Fill()
Example #8
0
def shoot_in_cell(eta, phi, eta_min, eta_max, phi_min, phi_max, z, shoot_type, small_cell_side,\
                      large_cell_side, limit_first_zone, type, vertex_number, edge_number):

    if type not in ['Triangles', 'Hexagons']:
        raise Exception(
            'shoot_cell_center() not implemented for geometry type ' + type)
    if eta < eta_min or eta > eta_max or\
       TVector2.Phi_mpi_pi(phi-phi_min) < 0 or TVector2.Phi_mpi_pi(phi-phi_max) > 0:
        raise Exception(
            "Error: trying to shoot particle outside geometry window")

    # Find center
    theta0 = 2. * m.atan(m.exp(-eta_max))
    theta = 2. * m.atan(m.exp(-eta))
    r0 = z * m.tan(theta0)
    r = z * m.tan(theta)
    x0 = r0 * m.cos(phi_min)
    y0 = r0 * m.sin(phi_min)
    x = r * m.cos(phi)
    y = r * m.sin(phi)

    if x * x + y * y <= limit_first_zone * limit_first_zone:
        cell_side = small_cell_side
    else:
        cell_side = large_cell_side

    if type is 'Triangles':
        dxdi = cell_side / 2.
        dxdj = cell_side / 2.
        dydj = cell_side * m.sqrt(3.) / 2.
    else:
        dxdi = cell_side * m.sqrt(3.)
        dxdj = cell_side * m.sqrt(3.) / 2.
        dydj = cell_side * 3. / 2.

    j = round((y - y0) / dydj)
    i = round((x - x0 - j * dxdj) / dxdi)
    x_center = x0 + i * dxdi + j * dxdj
    y_center = y0 + j * dydj

    if type is 'Triangles' and int(i) % 2:
        y_center += cell_side * m.sqrt(3) / 6.

    if shoot_type == "cell_center":
        r_center = m.sqrt(x_center**2 + y_center**2)
        theta_center = m.atan(r_center / z)
        eta_shoot = -m.log(m.tan(theta_center / 2.))
        phi_cell = m.copysign(m.acos(x_center / r_center), y_center)

    elif shoot_type == "cell_vertex":
        # Get position of vertices
        vertex = vertices(type, int(i), cell_side)
        if vertex_number >= len(vertex):
            raise Exception('Vertex {0} doesn\'t exist for type {1}'.format(
                vertex_number, type))
        x_vertex, y_vertex = vertex[vertex_number]
        x_vertex += x_center
        y_vertex += y_center
        r_vertex = m.sqrt(x_vertex**2 + y_vertex**2)
        theta_vertex = m.atan(r_vertex / z)
        eta_shoot = -m.log(m.tan(theta_vertex / 2.))
        phi_cell = m.copysign(m.acos(x_vertex / r_vertex), y_vertex)

    elif shoot_type == "cell_edge":
        edges = edge_centers(type, int(i), cell_side)
        if edge_number >= len(edges):
            raise Exception('Edge {0} doesn\'t exist for type {1}'.format(
                edge_number, type))
        x_edge, y_edge = edges[edge_number]
        x_edge += x_center
        y_edge += y_center
        r_edge = m.sqrt(x_edge**2 + y_edge**2)
        theta_edge = m.atan(r_edge / z)
        eta_shoot = -m.log(m.tan(theta_edge / 2.))
        phi_cell = m.copysign(m.acos(x_edge / r_edge), y_edge)

    else:
        raise Exception(
            'The shoot type should be cell_center, cell_edge or cell_vertex')

    return eta_shoot, phi_cell
Example #9
0
    def fill(self, event):
        #print 'this is the DSID: %s' % self.DSID
        weight=float(1.0)
        totalWeight=float(1.0)
        intLumi = 36300.0 #Same as used by Lorenzo Rossini
        #intLumi = 3230.0 #data16PeriodC (in pb^-1)
        #intLumi = 2582.0 #date16PeriodK (in pb^-1)
        #intLumi = 2222.0 #(in pb^-1)
        #intLumi = 2147.0 #(in pb^-1)
        #intLumi = 1948.44 #(in pb^-1)
        #intLumi = 5812.0 #(in pb^-1)
        #intLumi = 1.0

        #Initialize TLorentz vectors
        lep1Vec = ROOT.TLorentzVector()
        lep2Vec = ROOT.TLorentzVector()
        jet1Vec = ROOT.TLorentzVector()
        jet2Vec = ROOT.TLorentzVector()

        #Get variables from tree for filling histogram
        mu = event.mu
        nVtx = event.nVtx
        datasetNum = event.DatasetNumber
        MET=event.met_Et
        MET_Phi = event.met_Phi
        ht30 = event.Ht30
        if event.jetPt.size() > 0: jet1Vec.SetPtEtaPhiM(event.jetPt[0], event.jetEta[0], event.jetPhi[0], event.jetM[0])
        if event.jetPt.size() > 1: jet2Vec.SetPtEtaPhiM(event.jetPt[1], event.jetEta[1], event.jetPhi[1], event.jetM[1])
        dphi_j1met = TVector2.Phi_mpi_pi(jet1Vec.Phi() - MET_Phi) 
        dphi_j2met = TVector2.Phi_mpi_pi(jet2Vec.Phi() - MET_Phi) 
        nJet25 = event.nJet25
        mt = event.mt
        nLep_base = event.nLep_base
        nLep_signal = event.nLep_signal

        obs = observable()
        lep1Vec, lep1Charge, lep1Flavor = obs.getLep1TLVChargeFlavor(event)
        lep2Vec, lep2Charge, lep2Flavor = obs.getLep2TLVChargeFlavor(event)

        #Calculate dilepton variables
        lepPairVec = lep1Vec + lep2Vec
        mll = lepPairVec.M()
        ptll = lepPairVec.Pt()
        qlql = lep1Charge*lep2Charge
        dphi_l1met = TVector2.Phi_mpi_pi(lep1Vec.Phi() - MET_Phi) 
        dphi_l2met = TVector2.Phi_mpi_pi(lep2Vec.Phi() - MET_Phi) 
        dphi_l1l2  = lep2Vec.DeltaPhi(lep1Vec)
        mt_l1met = TMath.Sqrt(2*lep1Vec.Pt()*MET*(1-TMath.Cos(dphi_l1met)))
        mt_l2met = TMath.Sqrt(2*lep2Vec.Pt()*MET*(1-TMath.Cos(dphi_l2met)))
        mtautau = -999.
        dR_l1l2 = -999.
        if nLep_signal >= 2:
            dR_l1l2  = lep2Vec.DeltaR(lep1Vec)
            mtautau = obs.calcMtautau(event)

        genWeight = event.genWeight

        #Calculating weight
        if self.isdata:
            totalWeight = 1.0
        else:
            totalWeight = float(event.SherpaVjetsNjetsWeight*event.ttbarNNLOWeight*event.pileupWeight*event.eventWeight*event.leptonWeight*event.jvtWeight*event.bTagWeight*genWeight) #TODO: NO TRIGGER WEIGHT!!

        self.hists["mu"].Fill(float(mu), totalWeight)
        self.hists["MET"].Fill(float(MET), totalWeight)
        self.hists["Lep1Pt"].Fill(float(lep1Vec.Pt()), totalWeight)
        self.hists["Lep2Pt"].Fill(float(lep2Vec.Pt()), totalWeight)
        self.hists["Lep1Eta"].Fill(float(lep1Vec.Eta()), totalWeight)
        if lep1Flavor == 1: self.hists["ElPt"].Fill(int(lep1Vec.Pt()), totalWeight) 
        elif lep1Flavor == 2: self.hists["MuPt"].Fill(int(lep1Vec.Pt()), totalWeight) 
        if lep2Flavor == 1: self.hists["ElPt"].Fill(int(lep2Vec.Pt()), totalWeight) 
        elif lep2Flavor == 2: self.hists["MuPt"].Fill(int(lep2Vec.Pt()), totalWeight) 
        self.hists["Lep2Eta"].Fill(float(lep2Vec.Eta()), totalWeight)
        self.hists["Jet1Pt"].Fill(float(jet1Vec.Pt()), totalWeight)
        self.hists["Jet2Pt"].Fill(float(jet2Vec.Pt()), totalWeight)
        self.hists["nLepSignal"].Fill(float(nLep_signal), totalWeight)
        self.hists["nJet25"].Fill(float(nJet25), totalWeight)
        self.hists["nVtx"].Fill(float(nVtx), totalWeight)
        self.hists["mll"].Fill(float(mll), totalWeight)
        self.hists["mll2"].Fill(float(mll), totalWeight)
        self.hists["ptll"].Fill(float(ptll), totalWeight)
        self.hists["dphiJ1met"].Fill(float((-1)*math.fabs(dphi_j1met)), totalWeight)
        self.hists["dphiJ2met"].Fill(float((-1)*math.fabs(dphi_j2met)), totalWeight)
        self.hists["dphiL1met"].Fill(float(math.fabs(dphi_l1met)), totalWeight)
        self.hists["dphiL2met"].Fill(float(math.fabs(dphi_l2met)), totalWeight)
        self.hists["dphiL1L2"].Fill(float( math.fabs(dphi_l1l2)), totalWeight)
        self.hists["dRL1L2"].Fill(float(dR_l1l2), totalWeight)
        self.hists["lepCharge"].Fill(int(lep1Charge), totalWeight)
        self.hists["lepCharge"].Fill(int(lep2Charge), totalWeight)
        self.hists["qlql"].Fill(int(qlql), totalWeight)
        self.hists["lepFlavor"].Fill(int(lep1Flavor), totalWeight)
        self.hists["lepFlavor"].Fill(int(lep2Flavor), totalWeight)
        if (ht30): self.hists["METoverHt"].Fill(float(MET/ht30), totalWeight)
        self.hists["Ht30"].Fill(float(ht30), totalWeight)
        self.hists["Mt"].Fill(float(mt), totalWeight)
        self.hists["MtL1met"].Fill(float(mt_l1met), totalWeight)
        self.hists["MtL2met"].Fill(float(mt_l2met), totalWeight)
        self.hists["MTauTau"].Fill(float(mtautau), totalWeight)