def validateMomenta(self, jet, tolerance=.01): """ Checks if the jetmomentum is the sum of the constituent momenta. """ v = TLorentzVector(0, 0, 0, 0) for comp in jet.constituents.values(): for ptc in comp: v += ptc.p4() ret = True if abs(jet.pt() - v.Pt()) > tolerance: print 'pt unequal: ', jet.pt() - v.Pt() ret = False if abs(jet.theta() - v.Theta()) > tolerance: print 'theta unequal: ', jet.theta() - v.Theta() ret = False if abs(jet.eta() - v.Eta()) > tolerance: print 'eta unequal', jet.eta() - v.Eta() ret = False if abs(jet.phi() - v.Phi()) > tolerance: print 'phi unequal: ', jet.phi() - v.Phi() ret = False if abs(jet.m() - v.M()) > tolerance: print 'mass unequal: ', jet.m() - v.M() ret = False return ret
def jet_processing(jet): # Find the jet (eta, phi) center=jet.sum(axis=0) v_jet=TLorentzVector(center[1], center[2], center[3], center[0]) # Centering parameters phi=v_jet.Phi() bv = v_jet.BoostVector() bv.SetPerp(0) for n in np.arange(len(jet)): if np.sum(jet[n,:]) != 0: v = TLorentzVector(jet[n,1], jet[n,2], jet[n,3], jet[n,0]) v.RotateZ(-phi) v.Boost(-bv) jet[n,:] = v[3], v[0], v[1], v[2] #(E,Px,Py,Pz) # Rotating parameters weighted_phi=0 weighted_eta=0 for n in np.arange(len(jet)): if np.sum(jet[n,:]) != 0: v = TLorentzVector(jet[n,1], jet[n,2], jet[n,3], jet[n,0]) r = np.sqrt(v.Phi()**2 + v.Eta()**2) if r != 0: #in case there is only one component weighted_phi += v.Phi() * v.E()/r weighted_eta += v.Eta() * v.E()/r #alpha = np.arctan2(weighted_phi, weighted_eta) #approximately align at eta alpha = np.arctan2(weighted_eta, weighted_phi) #approximately align at phi for n in np.arange(len(jet)): if np.sum(jet[n,:]) != 0: v = TLorentzVector(jet[n,1], jet[n,2], jet[n,3], jet[n,0]) #v.rotate_x(alpha) #approximately align at eta v.RotateX(-alpha) #approximately align at phi jet[n,:] = v[3], v[0], v[1], v[2] #(E,Px,Py,Pz) return jet
class particle: def __init__(self, tree=None, i=None, original=None): if tree: #Build particle instance from TTree self.PID = tree.Particle[i].PID self.mom = tree.Particle[i].Mother1 self.daughters = [] self.m = tree.Particle[i].M self.e = tree.Particle[i].E self.p = TLorentzVector() self.p.SetPxPyPzE(tree.Particle[i].Px, tree.Particle[i].Py, tree.Particle[i].Pz, tree.Particle[i].E) self.pt = self.p.Pt() self.eta = self.p.Eta() self.phi = self.p.Phi() self.SF = 1 self.smear = None self.res = None elif original: #Make a resolution smeared version of the original self.PID = copy(original.PID) self.mom = copy(original.mom) self.daughters = copy(original.daughters) self.res = jetEnergyResolution(original.p.E()) self.smearE = -1 while self.smearE < 0: self.smearE = np.random.normal(0.985, self.res) # self.offsetM = -1 # while self.offsetM+original.p.M() < 5: self.offsetM = np.random.normal(5, 5) # self.smearM = -1 # while self.smearM < 0.5: self.smearM = np.random.normal(1, 1) self.p = TLorentzVector() self.p.SetPtEtaPhiM(original.p.Pt() * self.smearE, original.p.Eta(), original.p.Phi(), 0) # self.p.SetPtEtaPhiM( original.p.Pt() * self.smearE, # original.p.Eta(), # original.p.Phi(), # (original.p.M()+self.offsetM) * self.smearM) self.pt = self.p.Pt() self.eta = self.p.Eta() self.phi = self.p.Phi() self.m = self.p.M() self.e = self.p.E() self.SF = 1 def getDump(self): out = "PID " + str(self.PID).rjust(3) + " | mom " + str( self.mom).rjust(3) + " | mass " + str(self.m).ljust( 12) + " | pt " + str(self.pt).ljust(20) + " | eta " + str( self.eta).ljust(20) + " | phi " + str(self.phi).ljust(20) if self.res: out += " | res " + str(self.res).ljust(20) + " | smear " + str( self.smear).ljust(20) return out def dump(self): print(self.getDump())
def GetEtaPhi(px, py, pz, en): l = TLorentzVector() l.SetPxPyPzE(px,py,pz,en) #print( "sizeof4Floats: %d, sizeofTL: %d" % ( 4*sys.getsizeof(e), sys.getsizeof(l) ) ) eta = l.Eta() phi = l.Phi() return eta, phi
def corrected_p4(self, mu, run): p4 = TLorentzVector(mu.px(), mu.py(), mu.pz(), mu.energy()) if self.isMC: self.corr.applyPtCorrection(p4, mu.charge()) self.corr.applyPtSmearing(p4, mu.charge(), self.isSync) else: corr = self.corrD if run >= 203773 else self.corrABC corr.applyPtCorrection(p4, mu.charge()) ## convert to the proper C++ class (but preserve the mass!) return ROOT.reco.Muon.PolarLorentzVector(p4.Pt(), p4.Eta(), p4.Phi(), mu.mass())
def mcjetmatch(lmc, jet_px, jet_py, jet_pz, jet_en, drmax): #lmc = TLorentzVector() #lmc.SetPxPyPzE( amc_px, amc_py, amc_pz, amc_en ) aeta = lmc.Eta() aphi = lmc.Phi() n = len(jet_px) for ijet in range(0, n): ljet = TLorentzVector() ljet.SetPxPyPzE( jet_px[ijet], jet_py[ijet], jet_pz[ijet], jet_en[ijet] ) if DeltaR( aeta, aphi, ljet.Eta(), ljet.Phi() ) < drmax: return True return False
def D0VarsMC(collections): L1charge=collections[0] L1Pt=collections[1] L1Eta=collections[2] L1Phi=collections[3] L2charge=collections[4] L2Pt=collections[5] L2Eta=collections[6] L2Phi=collections[7] Kcharge=collections[8] KPt=collections[9] KEta=collections[10] KPhi=collections[11] trk_opp_l_kpi_mass=-99 trk_opp_l_mumu_mass=-99 if (L1charge<-1 or L2charge<-1 or Kcharge<-1): return [trk_opp_l_kpi_mass,trk_opp_l_mumu_mass] vK=TLorentzVector() vL=TLorentzVector() if L1charge != Kcharge: vL.SetPtEtaPhiM(L1Pt,L1Eta,L1Phi,0.493) else: vL.SetPtEtaPhiM(L2Pt,L2Eta,L2Phi,0.493) vK.SetPtEtaPhiM(KPt,KEta,KPhi,0.139) # getattr(Bcand,"fit_k_pt"), getattr(Bcand,"fit_k_eta"), getattr(Bcand,"fit_k_phi"),0.139) trk_opp_l_kpi_mass= (vL+vK).M() vL.SetPtEtaPhiM(vL.Pt(),vL.Eta(),vL.Phi(),0.139) vK.SetPtEtaPhiM(vK.Pt(),vK.Eta(),vK.Phi(),0.493) if trk_opp_l_kpi_mass>(vL+vK).M(): trk_opp_l_kpi_mass=(vL+vK).M() vK.SetPtEtaPhiM(vK.Pt(),vK.Eta(),vK.Phi(),0.105) vL.SetPtEtaPhiM(vL.Pt(),vL.Eta(),vL.Phi(),0.105) trk_opp_l_mumu_mass=(vL+vK).M() return [trk_opp_l_kpi_mass,trk_opp_l_mumu_mass]
def print_vec(self, vec): print "theta vec:", TMath.Pi() - vec.Theta() print return print "pxyz:", vec.Px(), vec.Py(), vec.Pz() print "en, phi:", vec.E(), vec.Phi() print v3 = vec.Vect() print "vxyz:", v3.x(), v3.y(), v3.z() print "theta v3: ", TMath.Pi() - v3.Theta() print theta_add = 1e-5 v3.SetTheta(v3.Theta() - theta_add) print "vxyz:", v3.x(), v3.y(), v3.z() print "theta v3: ", TMath.Pi() - v3.Theta() print vec2 = TLorentzVector(vec) vec3 = TLorentzVector(vec) vec.SetVect(v3) print "theta vec:", TMath.Pi() - vec.Theta() print "pxyz:", vec.Px(), vec.Py(), vec.Pz() print "en, phi:", vec.E(), vec.Phi() print vec2.SetTheta(vec2.Theta() - theta_add) print "theta vec:", TMath.Pi() - vec2.Theta() print "pxyz:", vec2.Px(), vec2.Py(), vec2.Pz() print "en, phi:", vec2.E(), vec2.Phi() print print "Delta theta, en, phi", vec3.Theta() - vec.Theta(), vec3.E( ) - vec.E(), vec3.Phi() - vec.Phi() print "Delta theta, en, phi", vec3.Theta() - vec2.Theta(), vec3.E( ) - vec2.E(), vec3.Phi() - vec2.Phi()
def fixMiss(self): self.fillUnknowns() miss = TLorentzVector(0., 0., 0., 1.) for i in range(5): miss -= TLorentzVector(self.x[i]*self.beta[i]*sin(self.theta[i])*cos(self.phi[i]), \ self.x[i]*self.beta[i]*sin(self.theta[i])*sin(self.phi[i]), \ self.x[i]*self.beta[i]*cos(self.theta[i]), \ self.x[i]) self.alpha[5] = miss.E() self.alpha[6] = miss.P() / miss.E() self.alpha[7] = miss.Theta() self.alpha[8] = miss.Phi() self.fillUnknowns() self.fillConstraints(False)
def D0Vars(collections): Bcands=collections[0] trk_opp_l_kpi_mass=[] trk_opp_l_mumu_mass=[] for Bcand in Bcands: vK=TLorentzVector() vL=TLorentzVector() kcharge=getattr(Bcand,"k_charge") if getattr(Bcand,"l1_charge") == getattr(Bcand,"l2_charge"): if getattr(Bcand,"l1_charge") == getattr(Bcand,"k_charge"): trk_opp_l_kpi_mass.append(-1) trk_opp_l_mumu_mass.append(-1) continue; else: vL.SetPtEtaPhiM( getattr(Bcand,"fit_l1_pt"), getattr(Bcand,"fit_l1_eta"), getattr(Bcand,"fit_l1_phi"),0.493) else: if getattr(Bcand,"l1_charge") != kcharge: vL.SetPtEtaPhiM( getattr(Bcand,"fit_l1_pt"), getattr(Bcand,"fit_l1_eta"), getattr(Bcand,"fit_l1_phi"),0.493) else: vL.SetPtEtaPhiM( getattr(Bcand,"fit_l2_pt"), getattr(Bcand,"fit_l2_eta"), getattr(Bcand,"fit_l2_phi"),0.493) vK.SetPtEtaPhiM( getattr(Bcand,"fit_k_pt"), getattr(Bcand,"fit_k_eta"), getattr(Bcand,"fit_k_phi"),0.139) k_l_mass_hypoth1=(vL+vK).M() vL.SetPtEtaPhiM( vL.Pt(),vL.Eta(),vL.Phi(),0.139) vK.SetPtEtaPhiM( vK.Pt(),vK.Eta(),vK.Phi(),0.493) if k_l_mass_hypoth1> (vL+vK).M(): trk_opp_l_kpi_mass.append((vL+vK).M()) else: trk_opp_l_kpi_mass.append(k_l_mass_hypoth1) vL.SetPtEtaPhiM( vL.Pt(),vL.Eta(),vL.Phi(),0.105) vK.SetPtEtaPhiM( vK.Pt(),vK.Eta(),vK.Phi(),0.105) trk_opp_l_mumu_mass.append((vL+vK).M()) return [trk_opp_l_kpi_mass,trk_opp_l_mumu_mass]
def shiftMET(): """Test function to see how one can apply a shift to the MET.""" print ">>> create TLVs..." shift = 1.05 # 5% jet1 = TLorentzVector() jet2 = TLorentzVector() met = TLorentzVector() jet1.SetPtEtaPhiM(40.0, 1.5, 0.0, 4.0) jet2.SetPtEtaPhiM(30.0, -2.5, 1.5, 10.0) met.SetPtEtaPhiM(45.0, 0.0, -2.3, 0.0) printTLV(jet1, jet2, met, names=["tlv1", "tlv2", "met"], header=True) # SHIFT JET ENERGY jet1_shift = TLorentzVector() jet1_shift.SetPtEtaPhiM(shift * jet1.Pt(), jet1.Eta(), jet1.Phi(), shift * jet1.M()) jet2_shift = TLorentzVector() jet2_shift.SetPtEtaPhiM(shift * jet2.Pt(), jet2.Eta(), jet2.Phi(), shift * jet2.M()) #jet1_shift2 = shift*jet1 #jet2_shift2 = shift*jet2 printTLV(jet1_shift, jet2_shift, names=["jet1_shift", "jet2_shift"]) # SHIFT MET dtlv = TLorentzVector() dtlv += (jet1 - jet1_shift) dtlv += (jet2 - jet2_shift) printTLV(dtlv, names=["dtlv"]) met_shift1 = met - dtlv met_shift2 = TLorentzVector() met_shift2.SetPtEtaPhiM(met_shift1.Pt(), 0, met_shift1.Phi(), 0) printTLV(met_shift1, met_shift2, names=["met_shift1", "met_shift2"])
def boost(df, args): #args must have 4 elements xyzt or pxpypzE px, py, pz = getXYZ(df, args) E = DfToNp(df[[args[3]]].dropna(axis=0)) l = TLorentzVector() with np.nditer([px, py, pz, E, None, None, None, None]) as it: for pi, pj, pk, el, pm, en, phi, cost in it: l.SetPxPyPzE(pi, pj, pk, el) bi = pi / el bj = pj / el bk = pk / el l.Boost(-bi, -bj, -bk) pm[...] = l.Px() en[...] = l.E() phi[...] = l.Phi() cost[...] = l.CosTheta() return (it.operands[4], it.operands[5], it.operands[6], it.operands[7])
def Transform(Px, Py, Pz, E): N = Px.shape[0] Pt = np.zeros(N) Eta = np.zeros(N) Phi = np.zeros(N) M = np.zeros(N) LV = TLorentzVector() print('\tStarting Transformation') for i in range(0, N): sys.stdout.write('\r') sys.stdout.write('\tCurrent process : %0.2f%%' % (i % N / N * 100)) sys.stdout.flush() LV.SetPx(Px[i]) LV.SetPy(Py[i]) LV.SetPz(Pz[i]) LV.SetE(E[i]) Pt[i] = LV.Pt() Eta[i] = LV.Eta() Phi[i] = LV.Phi() M[i] = LV.M() print() return Pt, Eta, Phi, M
def Return_Top(jet, id_csv, metPhi): masses = [-1000, -1000, -1000, -1000, -1000, -1000] b_tmp = TLorentzVector() b_tmp.SetPtEtaPhiE(jet[id_csv][0], jet[id_csv][1], jet[id_csv][2], jet[id_csv][3]) jet_nob = list(jet) jet_nob.pop(id_csv) j1dR_tmp = TLorentzVector() j1dR_tmp.SetPtEtaPhiE(0, 0, 0, 0) j2dR_tmp = TLorentzVector() j2dR_tmp.SetPtEtaPhiE(0, 0, 0, 0) a = list(combinations(jet_nob, 2)) min_DeltaR = 1000 for x in a: if x[0][0] < -5 or x[1][0] < -5: continue if x[0][1] < -5 or x[1][1] < -5: continue if (math.sqrt( math.pow((x[0][0] - x[1][0]), 2) + math.pow((x[0][1] - x[1][1]), 2)) < min_DeltaR): j1dR_tmp.SetPtEtaPhiE(x[0][0], x[0][1], x[0][2], x[0][3]) j2dR_tmp.SetPtEtaPhiE(x[1][0], x[1][1], x[1][2], x[1][3]) topdR_tmp = TLorentzVector() topdR_tmp = j1dR_tmp + j2dR_tmp + b_tmp WdR_tmp = TLorentzVector() WdR_tmp = j1dR_tmp + j2dR_tmp masses[0] = topdR_tmp.M() masses[1] = WdR_tmp.M() masses[2] = math.fabs(DeltaPhi(WdR_tmp.Phi(), b_tmp.Phi())) masses[3] = math.fabs(DeltaPhi(topdR_tmp.Phi(), b_tmp.Phi())) masses[4] = math.fabs(DeltaPhi(WdR_tmp.Phi(), metPhi)) masses[5] = math.fabs(DeltaPhi(topdR_tmp.Phi(), metPhi)) return masses
def PruneGenr8File(fname_in, fname_out, e_gamma_curr, theta_curr, verbose=False): # print "directory: " + os.getcwd() # print "input genr8 file: " + fname_in # print "output file: " + fname_out counter = 1 line1_out = "" #Run and event info line2_out = "" #Gamma ID info line3_out = "" #Gamma P4 info line4_out = "" #Proton ID info line5_out = "" #Proton P4 info line6_out = "" #Pi+ ID info line7_out = "" #Pi+ P4 info line8_out = "" #Pi- ID info line9_out = "" #Pi- P4 info rand = TRandom3(int(e_gamma_curr * 1000.)) p4_proton = TLorentzVector() for line in open(fname_in, 'r'): if (counter % 9 == 1): #Event info value_in_line = 1 run = "" event = "" for value in line.split(): if (value_in_line == 1): run = value if (value_in_line == 2): event = value if (verbose and value_in_line == 1): print "Run " + value if (verbose and value_in_line == 2): print "Event " + value if (verbose and value_in_line == 3): print "NParticles " + value value_in_line += 1 line1_out = run + " " + event + " " + "4\n" if (counter % 9 == 2): #Pi0 info value_in_line = 1 for value in line.split(): if (verbose and value_in_line == 1): print "Particle number in event: " + value if (verbose and value_in_line == 2): print "Particle enum value " + value if (verbose and value_in_line == 3): print "Particle mass " + value value_in_line += 1 if (counter % 9 == 3): #Pi0 p4 value_in_line = 1 p4_string = line.split() p4_gam1 = TLorentzVector(float(p4_string[1]), float(p4_string[2]), float(p4_string[3]), float(p4_string[4])) for value in line.split(): if (verbose and value_in_line == 1): print "Charge: " + value if (verbose and value_in_line == 2): print "Px " + value if (verbose and value_in_line == 3): print "Py " + value if (verbose and value_in_line == 4): print "Pz " + value if (verbose and value_in_line == 5): print "E " + value value_in_line += 1 if (counter % 9 == 4): #Pi+ info value_in_line = 1 line2_str = line for value in line.split(): if (verbose and value_in_line == 1): print "Particle number in event: " + value if (verbose and value_in_line == 2): print "Particle enum value " + value if (verbose and value_in_line == 3): print "Particle mass " + value value_in_line += 1 line6_out = "3 8 0.13957\n" if (counter % 9 == 5): #Pi+ p4 value_in_line = 1 p4_string = line.split() p4_pipl = TLorentzVector(float(p4_string[1]), float(p4_string[2]), float(p4_string[3]), float(p4_string[4])) for value in line.split(): if (verbose and value_in_line == 1): print "Charge: " + value if (verbose and value_in_line == 2): print "Px " + value if (verbose and value_in_line == 3): print "Py " + value if (verbose and value_in_line == 4): print "Pz " + value if (verbose and value_in_line == 5): print "E " + value value_in_line += 1 line7_out = " 1 " + str(p4_pipl.Px()) + " " + str( p4_pipl.Py()) + " " + str(p4_pipl.Pz()) + " " + str( p4_pipl.E()) + "\n" if (counter % 9 == 6): #Pi- info value_in_line = 1 for value in line.split(): if (verbose and value_in_line == 1): print "Particle number in event: " + value if (verbose and value_in_line == 2): print "Particle enum value " + value if (verbose and value_in_line == 3): print "Particle mass " + value value_in_line += 1 line8_out = "4 9 0.13957\n" if (counter % 9 == 7): #Pi- p4 value_in_line = 1 p4_string = line.split() p4_pim = TLorentzVector(float(p4_string[1]), float(p4_string[2]), float(p4_string[3]), float(p4_string[4])) for value in line.split(): if (verbose and value_in_line == 1): print "Charge: " + value if (verbose and value_in_line == 2): print "Px " + value if (verbose and value_in_line == 3): print "Py " + value if (verbose and value_in_line == 4): print "Pz " + value if (verbose and value_in_line == 5): print "E " + value value_in_line += 1 line9_out = " -1 " + str(p4_pim.Px()) + " " + str( p4_pim.Py()) + " " + str(p4_pim.Pz()) + " " + str( p4_pim.E()) + "\n" if (counter % 9 == 8): #proton info value_in_line = 1 for value in line.split(): if (verbose and value_in_line == 1): print "Particle number in event: " + value if (verbose and value_in_line == 2): print "Particle enum value " + value if (verbose and value_in_line == 3): print "Particle mass " + value value_in_line += 1 line4_out = "2 14 0.938272\n" if (counter % 9 == 0): #proton p4 value_in_line = 1 p4_string = line.split() p4_proton = TLorentzVector(float(p4_string[1]), float(p4_string[2]), float(p4_string[3]), float(p4_string[4])) p4_proton_boost_px = p4_proton.Px() p4_proton_boost_py = p4_proton.Py() p4_proton_boost_pz = p4_proton.Pz() p4_proton_boost_E = sqrt( abs(p4_proton_boost_px**2 + p4_proton_boost_py**2 + p4_proton_boost_pz**2 - 0.938272**2)) p4_proton_boost = TLorentzVector(p4_proton_boost_px, p4_proton_boost_py, p4_proton_boost_pz, p4_proton_boost_E) for value in line.split(): if (verbose and value_in_line == 1): print "Charge: " + value if (verbose and value_in_line == 2): print "Px " + value if (verbose and value_in_line == 3): print "Py " + value if (verbose and value_in_line == 4): print "Pz " + value if (verbose and value_in_line == 5): print "E " + value value_in_line += 1 line5_out = " 1 " + str(p4_proton.Px()) + " " + str( p4_proton.Py()) + " " + str(p4_proton.Pz()) + " " + str( p4_proton.E()) + "\n" my_phi = p4_proton_boost.Phi() + 3.14159 #Opposite proton my_theta = theta_curr * (3.14159 / 180.) gamma_px = str(e_gamma_curr * TMath.Sin(my_theta) * TMath.Cos(my_phi)) gamma_py = str(e_gamma_curr * TMath.Sin(my_theta) * TMath.Sin(my_phi)) gamma_pz = str(e_gamma_curr * TMath.Cos(my_theta)) line2_out = "1 1 0\n" line3_out = " 0 " + gamma_px + " " + gamma_py + " " + gamma_pz + " " + str( e_gamma_curr) + "\n" p4_gam_fcal = TLorentzVector(float(gamma_px), float(gamma_py), float(gamma_pz), e_gamma_curr) # print "Beam photon: " + str( (p4_gam_fcal+p4_proton_boost).E()) with open(fname_out, "a") as myfile: myfile.write(line1_out) myfile.write(line2_out) myfile.write(line3_out) myfile.write(line4_out) myfile.write(line5_out) myfile.write(line6_out) myfile.write(line7_out) myfile.write(line8_out) myfile.write(line9_out) counter += 1 # if(counter>200): break return
#from b bar HbbP4 = p3 + p4 #props #m_H1 props[1].append(HggP4.M()) #m_H2 props[2].append(HbbP4.M()) #b_pT props[3].append(p3.Pt()) #b_eta props[4].append(p3.Eta()) #b_phi props[5].append(p3.Phi()) #bbar_pT props[6].append(p4.Pt()) #bbar_eta props[7].append(p4.Eta()) #bbar_phi props[8].append(p4.Phi()) #a_pT props[9].append(p1.Pt()) #a_eta props[10].append(p1.Eta()) #a_phi props[11].append(p1.Phi())
for index in range(0, tau_features_test.shape[1], 3): lorentz = TLorentzVector() lorentz.SetPtEtaPhiM((tau_features_test[event][index]), (tau_features_test[event][index + 1]), (tau_features_test[event][index + 2]), (0.139)) tau_lorentz_no_neutrino += lorentz print("I fired") #firedCount += 1 tofill['tau_pt_no_neutrino'] = tau_lorentz_no_neutrino.Pt() print("tau_lorentz_neutrino.Px()", tau_lorentz_no_neutrino.Px()) print("tau_lorentz_no_neutrino.Py()", tau_lorentz_no_neutrino.Py()) print("tau_lorentz_no_neutrino.Pz()", tau_lorentz_no_neutrino.Pz()) print("tau_lorentz_no_neutrino.E()", tau_lorentz_no_neutrino.E()) tofill['tau_eta_no_neutrino'] = tau_lorentz_no_neutrino.Eta() tofill['tau_phi_no_neutrino'] = tau_lorentz_no_neutrino.Phi() tofill['tau_mass_no_neutrino'] = tau_lorentz_no_neutrino.M() print("tau_mass_no_neutrino", tau_lorentz_no_neutrino.M()) print("tau_eta_no_neutrino", tau_lorentz_no_neutrino.Eta()) tau_lorentz = TLorentzVector() tau_lorentz.SetPxPyPzE( tau_lorentz_no_neutrino.Px(), tau_lorentz_no_neutrino.Py(), tau_lorentz_no_neutrino.Pz(), tau_lorentz_no_neutrino.E(), ) print("tau_lorentz.Px()", tau_lorentz.Px()) print("tau_lorentz.Py()", tau_lorentz.Py()) print("tau_lorentz_Pz()", tau_lorentz.Pz()) for index in range(0, pred.shape[1], 3):
def ClosestTrkVars(collections): results = [] mll_e1_trk = [] mll_e2_trk = [] minxy1_b_trk = [] minxy2_b_trk = [] minxy3_b_trk = [] meanxy_b_trk = [] tracks = collections[0] Bcands = collections[1] Electrons = collections[2] for Bcand in Bcands: e1_vec = TLorentzVector() e2_vec = TLorentzVector() trk1_vec = TLorentzVector() trk2_vec = TLorentzVector() k_idx = getattr(Bcand, "kIdx") recoE1 = Electrons[getattr(Bcand, "l1Idx")] recoE2 = Electrons[getattr(Bcand, "l2Idx")] recoK = tracks[k_idx] e1_vec.SetPtEtaPhiM(getattr(recoE1, "pt"), getattr(recoE1, "eta"), getattr(recoE1, "phi"), 0.0005) e2_vec.SetPtEtaPhiM(getattr(recoE2, "pt"), getattr(recoE2, "eta"), getattr(recoE2, "phi"), 0.0005) k_eta = getattr(recoK, "eta") k_phi = getattr(recoK, "phi") b_eta = getattr(Bcand, "fit_eta") b_phi = getattr(Bcand, "fit_phi") vx_e1 = getattr(Bcand, "l1Vx") vy_e1 = getattr(Bcand, "l1Vy") vx_e2 = getattr(Bcand, "l2Vx") vy_e2 = getattr(Bcand, "l2Vy") vx_b = getattr(Bcand, "vtx_x") vy_b = getattr(Bcand, "vtx_y") min_distance_trk_e1 = 100 min_distance_trk_e2 = 100 imin_distance_trk_e1 = -1 imin_distance_trk_e2 = -1 distances_b_trk = [] mean_b_trk = 0 imean_b_trk = 0. for itrk, track in enumerate(tracks): if k_idx == itrk: continue vx_trk = getattr(track, "vx") vy_trk = getattr(track, "vy") vz_trk = getattr(track, "vz") trk_eta = getattr(track, "eta") trk_phi = getattr(track, "phi") if deltaR(e1_vec.Eta(),e1_vec.Phi(),trk_eta,trk_phi)<0.03 or \ deltaR(e2_vec.Eta(),e2_vec.Phi(),trk_eta,trk_phi)<0.03 or \ deltaR(k_eta,k_phi,trk_eta,trk_phi)<0.03: continue if min_distance_trk_e1 > sqrt((vx_e1 - vx_trk)**(2) + (vy_e1 - vy_trk)**(2)): min_distance_trk_e1 = sqrt((vx_e1 - vx_trk)**(2) + (vy_e1 - vy_trk)**(2)) imin_distance_trk_e1 = itrk if min_distance_trk_e2 > sqrt((vx_e2 - vx_trk)**(2) + (vy_e2 - vy_trk)**(2)): min_distance_trk_e2 = sqrt((vx_e2 - vx_trk)**(2) + (vy_e2 - vy_trk)**(2)) imin_distance_trk_e2 = itrk distances_b_trk.append( sqrt((vx_b - vx_trk)**(2) + (vy_b - vy_trk)**(2))) if deltaR(b_eta, b_phi, trk_eta, trk_phi) < 0.4: mean_b_trk += sqrt((vx_b - vx_trk)**(2) + (vy_b - vy_trk)**(2)) imean_b_trk += 1.0 distances_b_trk = sorted(distances_b_trk) trk1_vec.SetPtEtaPhiM(getattr(tracks[imin_distance_trk_e1], "pt"), getattr(tracks[imin_distance_trk_e1], "eta"), getattr(tracks[imin_distance_trk_e1], "phi"), 0.139) trk2_vec.SetPtEtaPhiM(getattr(tracks[imin_distance_trk_e2], "pt"), getattr(tracks[imin_distance_trk_e2], "eta"), getattr(tracks[imin_distance_trk_e2], "phi"), 0.139) mll_e1_trk.append((trk1_vec + e1_vec).M()) mll_e2_trk.append((trk2_vec + e2_vec).M()) if len(distances_b_trk) > 0: minxy1_b_trk.append(distances_b_trk[0]) else: minxy1_b_trk.append(10) if len(distances_b_trk) > 1: minxy2_b_trk.append(distances_b_trk[1]) else: minxy2_b_trk.append(10) if len(distances_b_trk) > 2: minxy3_b_trk.append(distances_b_trk[2]) else: minxy3_b_trk.append(10) if imean_b_trk > 0: meanxy_b_trk.append(mean_b_trk / imean_b_trk) else: meanxy_b_trk.append(10) results=[ mll_e1_trk, mll_e2_trk, minxy1_b_trk, minxy2_b_trk, minxy3_b_trk,\ meanxy_b_trk ] return results
def ClosestTrkVarsMC(collections): mll_e1_trk = -99 mll_e2_trk = -99 minxy1_b_trk = -99 minxy2_b_trk = -99 minxy3_b_trk = -99 meanxy_b_trk = -99 results = [ mll_e1_trk, mll_e2_trk, minxy1_b_trk, minxy2_b_trk, minxy3_b_trk, meanxy_b_trk ] tracks = collections[0] Bcands = collections[1] Bidx = collections[2] reco_electrons = collections[3] idx_e1 = collections[4] idx_e2 = collections[5] if Bidx < 0: return results recoB = Bcands[Bidx] idx_k = getattr(recoB, "kIdx") recoE1 = reco_electrons[idx_e1] recoE2 = reco_electrons[idx_e2] recoK = tracks[idx_k] e1_vec = TLorentzVector() e2_vec = TLorentzVector() e1_vec.SetPtEtaPhiM(getattr(recoE1, "pt"), getattr(recoE1, "eta"), getattr(recoE1, "phi"), 0.0005) e2_vec.SetPtEtaPhiM(getattr(recoE2, "pt"), getattr(recoE2, "eta"), getattr(recoE2, "phi"), 0.0005) k_eta = getattr(recoK, "eta") k_phi = getattr(recoK, "phi") b_eta = getattr(recoB, "fit_eta") b_phi = getattr(recoB, "fit_phi") vx_b = getattr(recoB, "vtx_x") vy_b = getattr(recoB, "vtx_y") vx_e1 = getattr(recoE1, "vx") vy_e1 = getattr(recoE1, "vy") vx_e2 = getattr(recoE2, "vx") vy_e2 = getattr(recoE2, "vy") min_dist_e1 = 100 min_dist_e2 = 100 itrk_e1 = -1 itrk_e2 = -1 distance_b_trk = [] mean_b_trk = 0. imean_b_trk = 0. for itrk, track in enumerate(tracks): if itrk == idx_k: continue vx_trk = getattr(track, "vx") vy_trk = getattr(track, "vy") trk_eta = getattr(track, "eta") trk_phi = getattr(track, "phi") if deltaR(e1_vec.Eta(),e1_vec.Phi(),trk_eta,trk_phi)<0.03 or \ deltaR(e2_vec.Eta(),e2_vec.Phi(),trk_eta,trk_phi)<0.03 or \ deltaR(k_eta,k_phi,trk_eta,trk_phi)<0.03: continue if min_dist_e1 > sqrt((vx_e1 - vx_trk)**(2) + (vy_e1 - vy_trk)**(2)): min_dist_e1 = sqrt((vx_e1 - vx_trk)**(2) + (vy_e1 - vy_trk)**(2)) itrk_e1 = itrk if min_dist_e2 > sqrt((vx_e2 - vx_trk)**(2) + (vy_e2 - vy_trk)**(2)): min_dist_e2 = sqrt((vx_e2 - vx_trk)**(2) + (vy_e2 - vy_trk)**(2)) itrk_e2 = itrk distance_b_trk.append(sqrt((vx_b - vx_trk)**(2) + (vy_b - vy_trk)**(2))) if deltaR(b_eta, b_phi, trk_eta, trk_phi) < 0.4: mean_b_trk += sqrt((vx_b - vx_trk)**(2) + (vy_b - vy_trk)**(2)) imean_b_trk += 1.0 distance_b_trk = sorted(distance_b_trk) minxy_e1_trk = min_dist_e1 minxy_e2_trk = min_dist_e2 trk1_vec = TLorentzVector() trk2_vec = TLorentzVector() trk1_vec.SetPtEtaPhiM(getattr(tracks[itrk_e1], "pt"), getattr(tracks[itrk_e1], "eta"), getattr(tracks[itrk_e1], "phi"), 0.139) trk2_vec.SetPtEtaPhiM(getattr(tracks[itrk_e2], "pt"), getattr(tracks[itrk_e2], "eta"), getattr(tracks[itrk_e2], "phi"), 0.139) mll_e1_trk = (trk1_vec + e1_vec).M() mll_e2_trk = (trk2_vec + e2_vec).M() if len(distance_b_trk) > 0: minxy1_b_trk = distance_b_trk[0] else: minxy1_b_trk = 10 if len(distance_b_trk) > 1: minxy2_b_trk = distance_b_trk[1] else: minxy2_b_trk = 10 if len(distance_b_trk) > 2: minxy3_b_trk = distance_b_trk[2] else: minxy3_b_trk = 10 if imean_b_trk > 0: meanxy_b_trk = mean_b_trk / imean_b_trk else: meanxy_b_trk = 10 results=[ mll_e1_trk, mll_e2_trk, minxy1_b_trk, minxy2_b_trk, minxy3_b_trk,\ meanxy_b_trk] return results
def analyze(self, event): eventWeightLumi, lheWeight, stitchWeight, puWeight, qcdnloWeight, qcdnnloWeight, ewknloWeight, topWeight = 1., 1., 1., 1., 1., 1., 1., 1. triggerWeight, leptonWeight = 1., 1. isSingleMuonTrigger, isSingleMuonPhotonTrigger, isSingleMuonNoFiltersPhotonTrigger, isDoubleMuonTrigger, isDoubleMuonPhotonTrigger, isJPsiTrigger, isDisplacedTrigger = False, False, False, False, False, False, False nCleanElectron, nCleanMuon, nCleanTau, nCleanPhoton, nCleanJet, nCleanBTagJet, HT30 = 0, 0, 0, 0, 0, 0, 0 cosThetaStar, cosTheta1, phi1 = -2., -2., -4. genCosThetaStar, genCosTheta1, genPhi1 = -2., -2., -4. for t in self.SingleMuonTriggers: if hasattr(event, t) and getattr(event, t): isSingleMuonTrigger = True for t in self.SingleMuonPhotonTriggers: if hasattr(event, t) and getattr(event, t): isSingleMuonPhotonTrigger = True for t in self.SingleMuonNoFiltersPhotonTriggers: if hasattr(event, t) and getattr(event, t): isSingleMuonNoFiltersPhotonTrigger = True for t in self.DoubleMuonTriggers: if hasattr(event, t) and getattr(event, t): isDoubleMuonTrigger = True for t in self.DoubleMuonPhotonTriggers: if hasattr(event, t) and getattr(event, t): isDoubleMuonPhotonTrigger = True for t in self.JPsiTriggers: if hasattr(event, t) and getattr(event, t): isJPsiTrigger = True for t in self.DisplacedTriggers: if hasattr(event, t) and getattr(event, t): isDisplacedTrigger = True lheWeight = 1. if self.isMC: # Event weight if not self.isLO and hasattr(event, "LHEWeight_originalXWGTUP"): lheWeight = event.LHEWeight_originalXWGTUP # PU weight puWeight = self.puTool.getWeight(event.Pileup_nTrueInt) self.hists["Nevents"].Fill(0, lheWeight) self.hists["Acceptance"].Fill(0, lheWeight) self.hists["Cutflow"].Fill(0, lheWeight) # Gen studies if self.isMC and self.isSignal: genHIdx, genJPsiIdx, genMuon1Idx, genMuon2Idx, genPhotonIdx = -1, -1, -1, -1, -1 # print "-"*40 for i in range(event.nGenPart): # print i, "\t", event.GenPart_pdgId[i], "\t", event.GenPart_status[i], "\t", event.GenPart_statusFlags[i], "\t", event.GenPart_pt[i] if event.GenPart_pdgId[i] == 25 or event.GenPart_pdgId[i] == 23: genHIdx = i if event.GenPart_pdgId[i] == 443 and event.GenPart_genPartIdxMother[i] >= 0 and event.GenPart_pdgId[event.GenPart_genPartIdxMother[i]] == 25: genJPsiIdx = i if event.GenPart_pdgId[i] == 22 and event.GenPart_status[i] in [1, 23] and event.GenPart_genPartIdxMother[i] >= 0 and event.GenPart_pdgId[event.GenPart_genPartIdxMother[i]] == 25: genPhotonIdx = i #and (genPhotonIdx < 0 or event.GenPart_pt[i] > event.GenPart_pt[genPhotonIdx]) if event.GenPart_pdgId[i] == -13 and event.GenPart_status[i] == 1 and event.GenPart_genPartIdxMother[i] >= 0 and event.GenPart_pdgId[event.GenPart_genPartIdxMother[i]] != 22 and (genMuon1Idx < 0 or event.GenPart_pt[i] > event.GenPart_pt[genMuon1Idx]): genMuon1Idx = i if event.GenPart_pdgId[i] == +13 and event.GenPart_status[i] == 1 and event.GenPart_genPartIdxMother[i] >= 0 and event.GenPart_pdgId[event.GenPart_genPartIdxMother[i]] != 22 and (genMuon2Idx < 0 or event.GenPart_pt[i] > event.GenPart_pt[genMuon2Idx]): genMuon2Idx = i if genHIdx >= 0 and genJPsiIdx >= 0 and genPhotonIdx >= 0 and genMuon1Idx >= 0 and genMuon2Idx >= 0: genH, genJPsi, genMuon1, genMuon2, genPhoton = TLorentzVector(), TLorentzVector(), TLorentzVector(), TLorentzVector(), TLorentzVector() if genHIdx >= 0: genH.SetPtEtaPhiM(event.GenPart_pt[genHIdx], event.GenPart_eta[genHIdx], event.GenPart_phi[genHIdx], event.GenPart_mass[genHIdx]) if genJPsiIdx >= 0: genJPsi.SetPtEtaPhiM(event.GenPart_pt[genJPsiIdx], event.GenPart_eta[genJPsiIdx], event.GenPart_phi[genJPsiIdx], event.GenPart_mass[genJPsiIdx]) if genPhotonIdx >= 0: genPhoton.SetPtEtaPhiM(event.GenPart_pt[genPhotonIdx], event.GenPart_eta[genPhotonIdx], event.GenPart_phi[genPhotonIdx], event.GenPart_mass[genPhotonIdx]) if genMuon1Idx >= 0: genMuon1.SetPtEtaPhiM(event.GenPart_pt[genMuon1Idx], event.GenPart_eta[genMuon1Idx], event.GenPart_phi[genMuon1Idx], event.GenPart_mass[genMuon1Idx]) if genMuon2Idx >= 0: genMuon2.SetPtEtaPhiM(event.GenPart_pt[genMuon2Idx], event.GenPart_eta[genMuon2Idx], event.GenPart_phi[genMuon2Idx], event.GenPart_mass[genMuon2Idx]) # Recalculate candidate 4-vectors for consistent calculation of angular variables genJPsi = genMuon1 + genMuon2 genH = genJPsi + genPhoton genCosThetaStar, genCosTheta1, genPhi1 = self.returnCosThetaStar(genH, genJPsi), self.returnCosTheta1(genJPsi, genMuon1, genMuon2, genPhoton), self.returnPhi1(genH, genMuon1, genMuon2) self.hists["genH_pt"].Fill(genH.Pt(), lheWeight) self.hists["genH_eta"].Fill(genH.Eta(), lheWeight) self.hists["genH_phi"].Fill(genH.Phi(), lheWeight) self.hists["genH_mass"].Fill(genH.M(), lheWeight) self.hists["genJPsi_pt"].Fill(genJPsi.Pt(), lheWeight) self.hists["genJPsi_eta"].Fill(genJPsi.Eta(), lheWeight) self.hists["genJPsi_phi"].Fill(genJPsi.Phi(), lheWeight) self.hists["genJPsi_mass"].Fill(genJPsi.M(), lheWeight) self.hists["genPhoton_pt"].Fill(genPhoton.Pt(), lheWeight) self.hists["genPhoton_eta"].Fill(genPhoton.Eta(), lheWeight) self.hists["genMuon1_pt"].Fill(max(genMuon1.Pt(), genMuon2.Pt()), lheWeight) self.hists["genMuon1_eta"].Fill(genMuon1.Eta(), lheWeight) self.hists["genMuon2_pt"].Fill(min(genMuon1.Pt(), genMuon2.Pt()), lheWeight) self.hists["genMuon2_eta"].Fill(genMuon2.Eta(), lheWeight) self.hists["genCosThetaStar"].Fill(genCosThetaStar, lheWeight) self.hists["genCosTheta1"].Fill(genCosTheta1, lheWeight) self.hists["genPhi1"].Fill(genPhi1, lheWeight) self.hists["genCosThetaStarZtoMM"].Fill(self.returnCosThetaStar(genH, genMuon1), lheWeight) # Reweight topWeight = 3./4. * (1. + genCosTheta1**2) # Transverse polarization (H, Z) if 'ZToJPsiG' in self.sampleName: stitchWeight = 3./2. * (1. - genCosTheta1**2) # Longitudinal polarization (Z) # Acceptance if abs(genPhoton.Eta()) < 2.5: self.hists["Acceptance"].Fill(1, lheWeight) if abs(genMuon1.Eta()) < 2.4: self.hists["Acceptance"].Fill(2, lheWeight) if abs(genMuon2.Eta()) < 2.4: self.hists["Acceptance"].Fill(3, lheWeight) self.hists["Cutflow"].Fill(1, lheWeight) if genPhoton.Pt() > 15. and genMuon1.Pt() > 5. and genMuon2.Pt() > 5.: self.hists["Acceptance"].Fill(4, lheWeight) # Muons m1, m2 = -1, -1 for i in range(event.nMuon): if event.Muon_pt[i] > self.thMuons[0 if m1 < 0 else 1] and abs(event.Muon_eta[i]) < 2.4 and event.Muon_looseId[i]: if m1 < 0: m1 = i if m2 < 0 and m1 >= 0 and event.Muon_charge[m1] != event.Muon_charge[i]: m2 = i if m1 < 0 or m2 < 0: if self.verbose >= 2: print "- No OS loose muons in acceptance" return False self.hists["Cutflow"].Fill(2, lheWeight) muon1, muon2 = TLorentzVector(), TLorentzVector() muon1.SetPtEtaPhiM(event.Muon_pt[m1], event.Muon_eta[m1], event.Muon_phi[m1], event.Muon_mass[m1]) muon2.SetPtEtaPhiM(event.Muon_pt[m2], event.Muon_eta[m2], event.Muon_phi[m2], event.Muon_mass[m2]) muonP = muon1 if event.Muon_charge[m1] > event.Muon_charge[m2] else muon2 muonM = muon1 if event.Muon_charge[m1] < event.Muon_charge[m2] else muon2 muon1v2, muon2v2 = TVector2(muon1.Px(), muon1.Py()), TVector2(muon2.Px(), muon2.Py()) jpsi = muon1 + muon2 if jpsi.M() < 2. or jpsi.M() > 12.: if self.verbose >= 2: print "- Dimuon invariant mass < 2 or > 12 GeV" return False self.hists["Cutflow"].Fill(3, lheWeight) # Photons p0 = -1 for i in range(event.nPhoton): if event.Photon_pt[i] > self.thPhoton[0] and abs(event.Photon_eta[i]) < 2.5 and event.Photon_pfRelIso03_all[i] < 0.25: # and event.Photon_mvaID_WP80[i]: if p0 < 0: p0 = i if p0 < 0: if self.verbose >= 2: print "- No isolated photons in acceptance" return False self.hists["Cutflow"].Fill(4, lheWeight) photon = TLorentzVector() photon.SetPtEtaPhiM(event.Photon_pt[p0], event.Photon_eta[p0], event.Photon_phi[p0], event.Photon_mass[p0]) photonv2 = TVector2(photon.Px(), photon.Py()) met, metPlusPhoton = TVector2(), TVector2() met.SetMagPhi(event.MET_pt, event.MET_phi) metPlusPhoton.Set(met.Px() + photon.Px(), met.Py() + photon.Py()) h = jpsi + photon jpsi_pt = jpsi.Pt() jpsi_eta = jpsi.Eta() jpsi_phi = jpsi.Phi() jpsi_mass = jpsi.M() jpsi_dEta = abs(muon1.Eta() - muon2.Eta()) jpsi_dPhi = abs(muon1.DeltaPhi(muon2)) jpsi_dR = muon1.DeltaR(muon2) h_pt = h.Pt() h_eta = h.Eta() h_phi = h.Phi() h_mass = h.M() h_dEta = abs(jpsi.Eta() - photon.Eta()) h_dPhi = abs(jpsi.DeltaPhi(photon)) h_dR = jpsi.DeltaR(photon) Muon1TrkIso, Muon2TrkIso = event.Muon_tkRelIso[m1], event.Muon_tkRelIso[m2] if jpsi_dR < 0.3: Muon1TrkIso = max(0., (Muon1TrkIso * event.Muon_pt[m1] * event.Muon_tunepRelPt[m1] - event.Muon_pt[m2] * event.Muon_tunepRelPt[m2])) / (event.Muon_pt[m1] * event.Muon_tunepRelPt[m1]) Muon2TrkIso = max(0., (Muon2TrkIso * event.Muon_pt[m2] * event.Muon_tunepRelPt[m2] - event.Muon_pt[m1] * event.Muon_tunepRelPt[m1])) / (event.Muon_pt[m2] * event.Muon_tunepRelPt[m2]) minMuonTrkIso = min(Muon1TrkIso, Muon2TrkIso) maxMuonTrkIso = max(Muon1TrkIso, Muon2TrkIso) minMuonIso = min(event.Muon_pfRelIso03_all[m1], event.Muon_pfRelIso03_all[m2]) maxMuonIso = max(event.Muon_pfRelIso03_all[m1], event.Muon_pfRelIso03_all[m2]) minMuonMetDPhi = min(abs(muon1v2.DeltaPhi(met)), abs(muon2v2.DeltaPhi(met))) maxMuonMetDPhi = max(abs(muon1v2.DeltaPhi(met)), abs(muon2v2.DeltaPhi(met))) photonMetDPhi = abs(photonv2.DeltaPhi(met)) metPlusPhotonDPhi = abs(met.DeltaPhi(metPlusPhoton)) cosThetaStar = self.returnCosThetaStar(h, jpsi) cosTheta1 = self.returnCosTheta1(jpsi, muonP, muonM, photon) phi1 = self.returnPhi1(h, muonP, muonM) # Weights # if self.isMC: # triggerWeight = self.muSFs.getTriggerSF(event.Muon_pt[m1], event.Muon_eta[m1]) # IdSF1 = self.muSFs.getIdSF(event.Muon_pt[0], event.Muon_eta[0], 2) # IsoSF1 = self.muSFs.getIsoSF(event.Muon_pt[0], event.Muon_eta[0]) # IdSF2 = self.muSFs.getIdSF(event.Muon_pt[1], event.Muon_eta[1], 2) # IsoSF2 = self.muSFs.getIsoSF(event.Muon_pt[1], event.Muon_eta[1]) # IdIsoSF3 = self.elSFs.getIdIsoSF(event.Electron_pt[0], event.Electron_eta[0]) # leptonWeight = IdSF1 * IsoSF1 * IdSF2 * IsoSF2 * IdIsoSF3 passedMETFilters = True filters = ["Flag_goodVertices", "Flag_globalSuperTightHalo2016Filter", "Flag_BadPFMuonFilter", "Flag_EcalDeadCellTriggerPrimitiveFilter", "Flag_HBHENoiseFilter", "Flag_HBHENoiseIsoFilter", "Flag_ecalBadCalibFilter", "Flag_ecalBadCalibFilterV2"] if not self.isMC: filters += ["Flag_eeBadScFilter"] for f in filters: if hasattr(event, f) and getattr(event, f) == False: passedMETFilters = False # try: ## if event.Flag_goodVertices: print "Flag_goodVertices" ## if event.Flag_globalSuperTightHalo2016Filter: print "Flag_globalSuperTightHalo2016Filter" ## if event.Flag_BadPFMuonFilter: print "Flag_BadPFMuonFilter" ## if event.Flag_EcalDeadCellTriggerPrimitiveFilter: print "Flag_EcalDeadCellTriggerPrimitiveFilter" ## if event.Flag_HBHENoiseFilter: print "Flag_HBHENoiseFilter" ## if event.Flag_HBHENoiseIsoFilter: print "Flag_HBHENoiseIsoFilter" ### if (self.isMC or event.Flag_eeBadScFilter): print "Flag_eeBadScFilter" ## if event.Flag_ecalBadCalibFilter: print "Flag_ecalBadCalibFilterV2" # if event.Flag_goodVertices and event.Flag_globalSuperTightHalo2016Filter and event.Flag_BadPFMuonFilter and event.Flag_EcalDeadCellTriggerPrimitiveFilter and event.Flag_HBHENoiseFilter and event.Flag_HBHENoiseIsoFilter: # and event.Flag_ecalBadCalibFilter: #and (self.isMC or event.Flag_eeBadScFilter): FIXME # passedMETFilters = True ## if not self.isMC: ## if not event.Flag_eeBadScFilter: ## passedMETFilters = False # except: # passedMETFilters = False ### Event variables ### # Muons for i in range(event.nMuon): if i != m1 and i != m2 and event.Muon_pt[i] > 10. and abs(event.Muon_eta[i]) < 2.4 and event.Muon_looseId[i] and event.Muon_pfRelIso03_all[i] < 0.15: nCleanMuon += 1 # Electrons for i in range(event.nElectron): if event.Electron_pt[i] > 10. and abs(event.Electron_eta[i]) < 2.5 and event.Electron_cutBased[i] >= 2: nCleanElectron += 1 # Taus for i in range(event.nTau): if event.Tau_pt[i] > 20. and abs(event.Tau_eta[i]) < 2.5 and event.Tau_idDeepTau2017v2p1VSe[i] >= 16 and event.Tau_idDeepTau2017v2p1VSmu[i] >= 8 and event.Tau_idDeepTau2017v2p1VSjet[i] >= 16 and event.Tau_rawIsodR03[i] < 0.15: nCleanTau += 1 # Photons for i in range(event.nPhoton): if i != p0 and event.Photon_pt[i] > 15. and abs(event.Photon_eta[i]) < 2.5 and event.Photon_pfRelIso03_all[i] < 0.15 and event.Photon_mvaID_WP90[i]: nCleanPhoton += 1 # Jets and Event variables for i in range(event.nJet): if event.Jet_jetId[i] >= 6 and abs(event.Jet_eta[i]) < 2.5: HT30 += event.Jet_pt[i] nCleanJet += 1 if event.Jet_btagDeepB[i] >= self.btagMedium: nCleanBTagJet += 1 if self.isMC: eventWeightLumi = self.lumiWeight * lheWeight * puWeight * topWeight * qcdnloWeight * qcdnnloWeight * ewknloWeight * triggerWeight * leptonWeight self.out.fillBranch("isMC", self.isMC) self.out.fillBranch("is2016", (self.year == 2016)) self.out.fillBranch("is2017", (self.year == 2017)) self.out.fillBranch("is2018", (self.year == 2018)) self.out.fillBranch("isSingleMuonTrigger", isSingleMuonTrigger) self.out.fillBranch("isSingleMuonPhotonTrigger", isSingleMuonPhotonTrigger) self.out.fillBranch("isSingleMuonNoFiltersPhotonTrigger", isSingleMuonNoFiltersPhotonTrigger) self.out.fillBranch("isDoubleMuonTrigger", isDoubleMuonTrigger) self.out.fillBranch("isDoubleMuonPhotonTrigger", isDoubleMuonPhotonTrigger) self.out.fillBranch("isJPsiTrigger", isJPsiTrigger) self.out.fillBranch("passedMETFilters", passedMETFilters) self.out.fillBranch("nCleanElectron", nCleanElectron) self.out.fillBranch("nCleanMuon", nCleanMuon) self.out.fillBranch("nCleanTau", nCleanTau) self.out.fillBranch("nCleanPhoton", nCleanPhoton) self.out.fillBranch("nCleanJet", nCleanJet) self.out.fillBranch("nCleanBTagJet", nCleanBTagJet) self.out.fillBranch("HT30", HT30) self.out.fillBranch("iPhoton", p0) self.out.fillBranch("iMuon1", m1) self.out.fillBranch("iMuon2", m2) # self.out.fillBranch("Muon1_pt", event.Muon_pt[m1]) self.out.fillBranch("Muon1_eta", event.Muon_eta[m1]) self.out.fillBranch("Muon2_pt", event.Muon_pt[m2]) self.out.fillBranch("Muon2_eta", event.Muon_eta[m2]) self.out.fillBranch("Muon1_pfRelIso03_all", event.Muon_pfRelIso03_all[m1]) self.out.fillBranch("Muon2_pfRelIso03_all", event.Muon_pfRelIso03_all[m2]) self.out.fillBranch("Muon1_mediumId", event.Muon_mediumId[m1]) self.out.fillBranch("Muon2_mediumId", event.Muon_mediumId[m2]) self.out.fillBranch("Muon1_ip3d", event.Muon_ip3d[m1]) self.out.fillBranch("Muon2_ip3d", event.Muon_ip3d[m2]) self.out.fillBranch("minMuonIso", minMuonIso) self.out.fillBranch("maxMuonIso", maxMuonIso) self.out.fillBranch("minMuonTrkIso", minMuonTrkIso) self.out.fillBranch("maxMuonTrkIso", maxMuonTrkIso) self.out.fillBranch("Muon12_diffdxy", abs(event.Muon_dxy[m1]-event.Muon_dxy[m2])) self.out.fillBranch("Muon12_diffdz", abs(event.Muon_dz[m1]-event.Muon_dz[m2])) self.out.fillBranch("Muon12_signdxy", abs(event.Muon_dxy[m1]-event.Muon_dxy[m2]) / math.sqrt(event.Muon_dxyErr[m1]**2 + event.Muon_dxyErr[m2]**2)) self.out.fillBranch("Muon12_signdz", abs(event.Muon_dz[m1]-event.Muon_dz[m2]) / math.sqrt(event.Muon_dzErr[m1]**2 + event.Muon_dzErr[m2]**2)) self.out.fillBranch("Photon1_pt", event.Photon_pt[p0]) self.out.fillBranch("Photon1_eta", event.Photon_eta[p0]) self.out.fillBranch("Photon1_mvaID_WP80", event.Photon_mvaID_WP80[p0]) self.out.fillBranch("Photon1_pfRelIso03_all", event.Photon_pfRelIso03_all[p0]) # self.out.fillBranch("JPsi_pt", jpsi_pt) self.out.fillBranch("JPsi_eta", jpsi_eta) self.out.fillBranch("JPsi_phi", jpsi_phi) self.out.fillBranch("JPsi_mass", jpsi_mass) self.out.fillBranch("JPsi_dEta", jpsi_dEta) self.out.fillBranch("JPsi_dPhi", jpsi_dPhi) self.out.fillBranch("JPsi_dR", jpsi_dR) self.out.fillBranch("H_pt", h_pt) self.out.fillBranch("H_eta", h_eta) self.out.fillBranch("H_phi", h_phi) self.out.fillBranch("H_mass", h_mass) self.out.fillBranch("H_dEta", h_dEta) self.out.fillBranch("H_dPhi", h_dPhi) self.out.fillBranch("H_dR", h_dR) self.out.fillBranch("minMuonMetDPhi", minMuonMetDPhi) self.out.fillBranch("maxMuonMetDPhi", maxMuonMetDPhi) self.out.fillBranch("photonMetDPhi", photonMetDPhi) self.out.fillBranch("metPlusPhotonDPhi", metPlusPhotonDPhi) self.out.fillBranch("cosThetaStar", cosThetaStar) self.out.fillBranch("cosTheta1", cosTheta1) self.out.fillBranch("phi1", phi1) self.out.fillBranch("genCosThetaStar", genCosThetaStar) self.out.fillBranch("genCosTheta1", genCosTheta1) self.out.fillBranch("genPhi1", genPhi1) self.out.fillBranch("lumiWeight", self.lumiWeight) self.out.fillBranch("lheWeight", lheWeight) self.out.fillBranch("stitchWeight", stitchWeight) self.out.fillBranch("puWeight", puWeight) self.out.fillBranch("topWeight", topWeight) self.out.fillBranch("qcdnloWeight", qcdnloWeight) self.out.fillBranch("qcdnnloWeight", qcdnnloWeight) self.out.fillBranch("ewknloWeight", ewknloWeight) self.out.fillBranch("triggerWeight", triggerWeight) self.out.fillBranch("leptonWeight", leptonWeight) self.out.fillBranch("eventWeightLumi", eventWeightLumi) if self.verbose >= 2: print "+ Tree filled" return True
Lam = f.Events.recoVertexCompositePtrCandidates_slimmedLambdaVertices__RECO.product( ) for k in Lam: k = k.p4() TLk.SetPtEtaPhiE(k.pt(), k.eta(), k.phi(), k.e()) if TLk.DeltaR(TLs4) < 0.5: print "Match L", k.pt() / s4.pt(), k.mass(), isS nKSL += 1 if nKSL > 0: print "Total KS/Lambda: ", nKSL if len(mK) == 0: continue jtl = TLorentzVector() Jets = f.Events.patJets_slimmedJets__RECO.product() for j in Jets: jtl.SetPtEtaPhiM(j.pt(), j.eta(), j.phi(), j.mass()) if any(jtl.DeltaR(m) < 0.5 for m in mK): print "J", jtl.Pt(), jtl.Eta(), jtl.Phi(), "Q", TLs4.Pt( ), TLs4.Eta(), TLs4.Phi() else: continue # print dir(j) l = None for m in mK: print " KS", m.Pt(), m.Eta(), m.Phi(), m.M() ptrs = [] ntrs = [] ptl = TLorentzVector() for p in j.getJetConstituents(): # print " C", p, p.charge(), p.pt(), p.eta(), p.phi() # # print " C", p.pdgId(), p.charge(), p.isIsolatedChargedHadron(), p.hasTrackDetails(), p.mass() ptl.SetPtEtaPhiM(p.pt(), p.eta(), p.phi(), p.mass())
tmp_unrotated_Py = tmp_unrotated_PxPyPz_vec[1] tmp_unrotated_Pz = tmp_unrotated_PxPyPz_vec[2] Global_4vec = ROOT.TLorentzVector() Global_4vec.SetPxPyPzE(tmp_unrotated_Px, tmp_unrotated_Py, tmp_unrotated_Pz, tmp_E) return Global_4vec ##### test ##### v = TLorentzVector() v.SetPxPyPzE(-3.6740152498, -2.79192430698, 21.6557548444, 22.1777103583) print "Px,Py,Pz,E,M:", v.Px(), v.Py(), v.Pz(), v.E(), v.M() print "tau_orig_theta, tau_orig_phi:", v.Theta(), v.Phi() tau_orig_theta_test = v.Theta() tau_orig_phi_test = v.Phi() toPrint = rotateToVisTauMomPointsInEtaEqualsZero(tau_orig_theta_test, tau_orig_phi_test, v) # print toPrint newPx = toPrint.Px() newPy = toPrint.Py() newPz = toPrint.Pz() newE = toPrint.E() newM = toPrint.M() newTheta = toPrint.Theta() newPhi = toPrint.Phi()
lep1.SetPtEtaPhiM(lep_pt[ev][index1], lep_eta[ev][index1], lep_phi[ev][index1], lep_mass[ev][index1]) lep2.SetPtEtaPhiM(lep_pt[ev][index2], lep_eta[ev][index2], lep_phi[ev][index2], lep_mass[ev][index2]) lep3.SetPtEtaPhiM(lep_pt[ev][index3], lep_eta[ev][index3], lep_phi[ev][index3], lep_mass[ev][index3]) threeleps = lep1 + lep2 + lep3 Met = TLorentzVector() Met.SetPtEtaPhiM(MET[ev], 0, MET_phi[ev], 0) ot.idL1[0], ot.idL2[0], ot.idL3[0] = lep_id[ev][index1], lep_id[ev][ index2], lep_id[ev][index3] ot.pTL1[0], ot.pTL2[0], ot.pTL3[0] = lep1.Pt(), lep2.Pt(), lep3.Pt() ot.etaL1[0], ot.etaL2[0], ot.etaL3[0] = lep1.Eta(), lep2.Eta(), lep3.Eta() ot.phiL1[0], ot.phiL2[0], ot.phiL3[0] = lep1.Phi(), lep2.Phi(), lep3.Phi() ot.IsoL1[0], ot.IsoL2[0], ot.IsoL3[0] = lep_iso[ev][index1], lep_iso[ev][ index2], lep_iso[ev][index3] ot.ip3dL1[0], ot.ip3dL2[0], ot.ip3dL3[0] = lep_ip[ev][index1], lep_ip[ev][ index2], lep_ip[ev][index3] ot.sip3dL1[0], ot.sip3dL2[0], ot.sip3dL3[0] = lep_sip[ev][index1], lep_sip[ ev][index2], lep_sip[ev][index3] ot.massL1[0], ot.massL2[0], ot.massL3[0] = lep1.M(), lep2.M(), lep3.M() ot.tightIdL1[0], ot.tightIdL2[0], ot.tightIdL3[0] = lep_tight[ev][ index1], lep_tight[ev][index2], lep_tight[ev][index3] ot.medIdL1[0], ot.medIdL2[0], ot.medIdL3[0] = lep_med[ev][index1], lep_med[ ev][index2], lep_med[ev][index3] ot.dR12[0] = deltaR(lep1.Eta(), lep1.Phi(), lep2.Eta(), lep2.Phi()) ot.dR13[0] = deltaR(lep1.Eta(), lep1.Phi(), lep3.Eta(), lep3.Phi()) ot.dR23[0] = deltaR(lep2.Eta(), lep2.Phi(), lep3.Eta(), lep3.Phi()) ot.met[0], ot.met_phi[0] = MET[ev], MET_phi[ev]
def FillHisto(sample, donAOD=False, DEBUG=True): gStyle.SetOptFit(0) gStyle.SetOptStat(0) gROOT.SetBatch(1) #Clean Jet Var hpt_1j = TH1F("pt_1j", ";Pt 1st clean gen jet [GeV]; Events", 20, 0., 200) hpt_2j = TH1F("pt_2j", ";Pt 2st clean gen jet [GeV]; Events", 20, 0., 200) heta_1j = TH1F("eta_1j", ";Eta 1st clean gen jet;Events", 20, -5., 5.) heta_2j = TH1F("eta_2j", ";Eta 2nd clean gen jet;Events", 20, -5., 5.) hmjj = TH1F("mjj", ";Gen_mjj [GeV]; Events ", 10, 500, 3000) hdetajj = TH1F("detajj", ";Gen_detajj; Events ", 10, 3.5, 10.5) #Lepton VAr hpt_1l = TH1F("pt_1l", ";Pt 1st lepton gen [Gev]; Events", 20, 0., 200) hpt_2l = TH1F("pt_2l", ";Pt 2nd lepton gen [GeV]; Events", 20, 0., 200) heta_1l = TH1F("eta_1l", ";Eta 1st lepton gen; Events", 10, -2.5, 2.5) heta_2l = TH1F("eta_2l", ";Eta 2nd lepton gen; Events", 10, -2.5, 2.5) hmll = TH1F("mll", "; Gen_mll[GeV]; Events; ", 10, 0, 400) hlist = [ hmjj, hdetajj, hpt_1j, hpt_2j, heta_1j, heta_2j, hpt_1l, hpt_2l, heta_1l, heta_2l, hmll ] for h in hlist: h.Sumw2() Events = TChain("Events") Runs = TChain("Runs") if donAOD: print "Opening the File: ", sample['nAODpath'] for ifile in sample['nAODpath']: Events.Add(ifile) Runs.Add(ifile) Events.SetBranchStatus("*", 0) Events.SetBranchStatus("GenDressedLepton_pt", 1) Events.SetBranchStatus("GenDressedLepton_eta", 1) Events.SetBranchStatus("GenDressedLepton_phi", 1) Events.SetBranchStatus("GenDressedLepton_mass", 1) Events.SetBranchStatus("nGenDressedLepton", 1) else: Events.SetBranchStatus("*", 0) print "Opening the File: ", sample['path'] Events.Add(sample['path']) Runs.Add(sample['path']) Events.SetBranchStatus("nLeptonGen", 1) Events.SetBranchStatus("LeptonGen_pt", 1) Events.SetBranchStatus("LeptonGen_eta", 1) Events.SetBranchStatus("LeptonGen_phi", 1) Events.SetBranchStatus("LeptonGen_mass", 1) Events.SetBranchStatus("nGenJet", 1) Events.SetBranchStatus("GenJet_pt", 1) Events.SetBranchStatus("GenJet_eta", 1) Events.SetBranchStatus("GenJet_phi", 1) Events.SetBranchStatus("GenJet_mass", 1) Events.SetBranchStatus("LHEPart_pt", 1) Events.SetBranchStatus("LHEPart_eta", 1) Events.SetBranchStatus("LHEPart_phi", 1) Events.SetBranchStatus("GenPart_pdgId", 1) Events.SetBranchStatus("genWeight", 1) #Events.SetBranchStatus("XSWeight",1) Runs.SetBranchStatus("*", 0) Runs.SetBranchStatus("genEventSumw_", 1) #Get the correct weights, xsec and lumi sumGenW = 0 for ir in range(Runs.GetEntries()): Runs.GetEntry(ir) print "#", ir, "genEventSumw", Runs.genEventSumw_ sumGenW += Runs.genEventSumw_ print "So sumGenW is ", sumGenW xsec = sample['xsec'] lumi = sample['lumi'] #Xsec for 2016 if "2016" in sample['year']: xsec = sample['xsec_2016'] print "Year: ", sample['year'], "Lumi ", lumi, "using this xsec: ", xsec print "Filling the Histo ... " print ">> Starting the Loop on Events " #Starting the loop on Events to fill the histo for ev in range(Events.GetEntries()): jets_clean = [] #the list containing the 2 jets clean with pT > 30 GeV if DEBUG: if ev % 1 == 0: if ev > 50: break print "\n\n----------------------------------------------------\n" \ "Processing event " , ev , " out of " ,Events.GetEntries() ,"\n" \ "----------------------------------------------------\n" sys.stdout.flush() else: if ev % 10000 == 0: print "Processing event ", ev, " out of ", Events.GetEntries() sys.stdout.flush() nCleanJet = 0 Events.GetEntry(ev) if donAOD: nLep = Events.nGenDressedLepton else: nLep = Events.nLeptonGen if nLep < 2: #requiring at least 2 leptons if DEBUG: print nLep, "Skipping:: Not enough Leptons, continue ..." continue if donAOD: pt_leading = Events.GenDressedLepton_pt[0] pt_subleading = Events.GenDressedLepton_pt[1] else: pt_leading = Events.LeptonGen_pt[0] pt_subleading = Events.LeptonGen_pt[1] #Asking for two leptons: over the treshold if (pt_leading < 20 or pt_subleading < 10): if DEBUG: print pt_leading, pt_subleading, "Skipping:: Lepton Not Over the pt Threshold, continue ..." continue #Cleaning the Jets (pT > 30, no Leptons with pt>10 in dR < 0.4) if Events.nGenJet == 0: if DEBUG: print " Skipping No Jets: ", Events.nGenJet continue if DEBUG: print ">>>> Starting the Loop on Jets --> # of Jets: ", Events.nGenJet for iJ in range(Events.nGenJet): jet = TLorentzVector() jpt = Events.GenJet_pt[iJ] jeta = Events.GenJet_eta[iJ] jphi = Events.GenJet_phi[iJ] jmass = Events.GenJet_mass[iJ] CleanJet = True if DEBUG: print " iJets", iJ + 1, "# of Jets: ", Events.nGenJet, " # of CleanJet", nCleanJet print " Jet Pt ", jpt if jpt < 30: CleanJet = False if DEBUG: print " Pt < 30, So Not a CleanJet ", CleanJet, " Exit the Jet Loop" break if DEBUG: print ">>>>>> Starting the Loop on Leptons --> # of Leptons: ", nLep for iLep in range(nLep): if donAOD: lpt = Events.GenDressedLepton_pt[iLep] leta = Events.GenDressedLepton_eta[iLep] lphi = Events.GenDressedLepton_phi[iLep] else: lpt = Events.LeptonGen_pt[iLep] leta = Events.LeptonGen_eta[iLep] lphi = Events.LeptonGen_phi[iLep] if DEBUG: print " iLepton ", iLep + 1, " # of Leptons: ", nLep print " Lepton Pt ", lpt print " deltaR ", DeltaR(jeta, jphi, leta, lphi) if lpt < 10: if DEBUG: print " Pt < 10, So CleanJet is ", CleanJet, "--> Quit the Lepton Loop ..." break if DeltaR(jeta, jphi, leta, lphi) < 0.4: CleanJet = False if DEBUG: print " DeltaR < 0.4, So CleanJet is ", CleanJet, "--> Quit the Lepton Loop ..." break if DEBUG: print "<<<<<< Ended Loop on Leptons " if CleanJet: nCleanJet += 1 jet.SetPtEtaPhiM(jpt, jeta, jphi, jmass) jets_clean.append(jet) if DEBUG: print ">>>>>> !!! So We Have a CleanJet ", CleanJet, "And # of CleanJet is ", nCleanJet print " jets clean: Pt , Eta , Phi , Mass \n" \ " ",jets_clean[nCleanJet-1].Pt(),jets_clean[nCleanJet-1].Eta(), jets_clean[nCleanJet-1].Phi(), jets_clean[nCleanJet-1].M() if nCleanJet == 2: if DEBUG: print ">>>>>> Found at least ", nCleanJet, "CleanJets with pT > 30 GeV --> Quit the Jet loop ..." break if DEBUG: print "<<<< Ended Loop on Jets " #At least 2 CleanJet if nCleanJet < 2: if DEBUG: print " --- We dont have enough CleanJet ... continue ... ---" continue if DEBUG: print " Found ", nCleanJet, " CleanJet with pT > 30 GeV" noTop_noHiggs = 0 # excluding the top and the Higgs for ipart in range(0, len(Events.GenPart_pdgId)): noTop_noHiggs += (abs(Events.GenPart_pdgId[ipart]) == 6 or abs(Events.GenPart_pdgId[ipart]) == 25) if noTop_noHiggs > 0: if DEBUG: print "find an Higgs: ", noTop_noHiggs #VBS Phase Space Cut mjj = (jets_clean[0] + jets_clean[1]).M() detajj = abs(jets_clean[0].Eta() - jets_clean[1].Eta()) #print " ",jets_clean[0].Pt(),jets_clean[0].Eta(), jets_clean[0].Phi(), jets_clean[0].M() #print " ",jets_clean[1].Pt(),jets_clean[1].Eta(), jets_clean[1].Phi(), jets_clean[1].M() if DEBUG: print "Invariant mass of the jets: ", mjj, "and gap in eta : ", detajj if mjj <= 500 or detajj <= 3.5: if DEBUG: "Not in the VBS phase space" continue #Save the var of the 2 first lepton lepton1 = TLorentzVector() lepton2 = TLorentzVector() if donAOD: leppt = getattr(Events, "GenDressedLepton_pt") lepeta = getattr(Events, "GenDressedLepton_eta") lepphi = getattr(Events, "GenDressedLepton_phi") lepmass = getattr(Events, "GenDressedLepton_mass") else: leppt = getattr(Events, "LeptonGen_pt") lepeta = getattr(Events, "LeptonGen_eta") lepphi = getattr(Events, "LeptonGen_phi") lepmass = getattr(Events, "LeptonGen_mass") lepton1.SetPtEtaPhiM(leppt[0], lepeta[0], lepphi[0], lepmass[0]) lepton2.SetPtEtaPhiM(leppt[1], lepeta[1], lepphi[1], lepmass[1]) mll = (lepton1 + lepton2).M() if DEBUG: print " ,lepton Pt(), Eta(), Phi(), M()" print " ", lepton1.Pt(), lepton1.Eta(), lepton1.Phi( ), lepton1.M() print " ", lepton2.Pt(), lepton2.Eta(), lepton2.Phi( ), lepton2.M() print " and mlll is: ", mll #To Cut at LHE level W Mass to fix 2016 bug part0 = TLorentzVector() part1 = TLorentzVector() part2 = TLorentzVector() part3 = TLorentzVector() pt = getattr(Events, "LHEPart_pt") eta = getattr(Events, "LHEPart_eta") phi = getattr(Events, "LHEPart_phi") part0.SetPtEtaPhiM(pt[0], eta[0], phi[0], 0) part1.SetPtEtaPhiM(pt[1], eta[1], phi[1], 0) part2.SetPtEtaPhiM(pt[2], eta[2], phi[2], 0) part3.SetPtEtaPhiM(pt[3], eta[3], phi[3], 0) LHE_mW1 = (part0 + part1).M() LHE_mW2 = (part2 + part3).M() xsec = sample['xsec'] lumi = sample['lumi'] massRange = True #Remove the bug from 2016 if "2016" in sample['year']: xsec = sample['xsec_2016'] if DEBUG: print "LHE_mW1, LHEmW2" print LHE_mW1, LHE_mW2 if (LHE_mW1 < 63 or LHE_mW2 < 63 or LHE_mW1 > 100 or LHE_mW2 > 100): if DEBUG: print "So massRange should be false" #print "LHE_mW1, LHEmW2" #print LHE_mW1, LHE_mW2 #print "So massRange should be false" massRange = False if DEBUG: print "Mass Range is ", massRange if DEBUG: print "Year: ", sample[ 'year'], "Lumi ", lumi, "using this xsec: ", xsec print "So sumGenW is ", sumGenW print "So genWeight is ", Events.genWeight print "Mass Range is ", massRange weight = (1000 * xsec * lumi * Events.genWeight) / sumGenW if massRange: #always True for 2017-2018 if DEBUG: print "!!! The Event passed the Selections !!!" print "!!! So I Can Finally Fill the Histo !!! " print "mjj ---> ", mjj print "detajj ---> ", detajj print "pt 1 j ---> ", jets_clean[0].Pt() print "pt 2 j ---> ", jets_clean[1].Pt() print "eta 1 j ---> ", jets_clean[0].Eta() print "eta 2 j ---> ", jets_clean[1].Eta() print "mll ---> ", mll print "pt 1 lep ---> ", lepton1.Pt() print "pt 2 lep ---> ", lepton2.Pt() print "eta 1 lep ---> ", lepton1.Eta() print "eta 2 lep ---> ", lepton2.Eta() hmjj.Fill(mjj, weight) hdetajj.Fill(detajj, weight) hpt_1j.Fill(jets_clean[0].Pt(), weight) hpt_2j.Fill(jets_clean[1].Pt(), weight) heta_1j.Fill(jets_clean[0].Eta(), weight) heta_2j.Fill(jets_clean[1].Eta(), weight) hpt_1l.Fill(lepton1.Pt(), weight) hpt_2l.Fill(lepton2.Pt(), weight) heta_1l.Fill(lepton1.Eta(), weight) heta_2l.Fill(lepton2.Eta(), weight) hmll.Fill(mll, weight) print "<< Ended Loop on Events " print "Filled Histos " if DEBUG: "Now returning the histo: " return hlist
eng_triplet[i][12] ]) # mult tvec1 = TLorentzVector() tvec2 = TLorentzVector() tvec3 = TLorentzVector() tvec1.SetPtEtaPhiM(eng_triplet[0][0], eng_triplet[0][1], eng_triplet[0][2], eng_triplet[0][3]) tvec2.SetPtEtaPhiM(eng_triplet[1][0], eng_triplet[1][1], eng_triplet[1][2], eng_triplet[1][3]) tvec3.SetPtEtaPhiM(eng_triplet[2][0], eng_triplet[2][1], eng_triplet[2][2], eng_triplet[2][3]) W = tvec2 + tvec3 top = tvec1 + tvec2 + tvec3 temp = abs(tvec1.Phi() - tvec2.Phi()) temp = temp if temp <= math.pi else temp - 2 * math.pi b_wj1_deltaR = np.sqrt((tvec1.Eta() - tvec2.Eta())**2 + temp**2) b_wj1_mass = (tvec1 + tvec2).M() temp = abs(tvec1.Phi() - tvec3.Phi()) temp = temp if temp <= math.pi else temp - 2 * math.pi b_wj2_deltaR = np.sqrt((tvec1.Eta() - tvec3.Eta())**2 + temp**2) b_wj2_mass = (tvec1 + tvec3).M() temp = abs(tvec2.Phi() - tvec3.Phi()) temp = temp if temp <= math.pi else temp - 2 * math.pi w_deltaR = np.sqrt((tvec2.Eta() - tvec3.Eta())**2 + temp**2)
def analyze(self, event): Wmass = 80.4 # Fixed W mass OriginTmass = 172.5 # Fixed T mass ## initialize ## SMTKinVal = [0, 0, 0, 0] FCNCTKinVal = [0, 0, 0, 0] consted = 0 # For NPL Selecton NPLflag = 1 if 'NPL' in self.mode: if (event._tree.b_out_nGoodLepton != 2 and event._tree.b_out_nVetoLepton > 0): NPLflag = 0 else: NPLflag = 1 else: if (event._tree.b_out_nGoodLepton != 3): NPLflag = 0 else: NPLflag = 1 # Check basic event selection if event._tree.b_out_GoodLeptonCode != 111 or\ not( 2 <= event._tree.b_out_nGoodJet <= 3 ) or\ event._tree.b_out_nBjet < 1 or\ NPLflag == 0: self.out.fillBranch("KinTop_status", 0) self.out.fillBranch("KinTopWb_pt", 0) self.out.fillBranch("KinTopWb_eta", 0) self.out.fillBranch("KinTopWb_phi", 0) self.out.fillBranch("KinTopWb_mass", 0) self.out.fillBranch("KinTopZq_pt", 0) self.out.fillBranch("KinTopZq_eta", 0) self.out.fillBranch("KinTopZq_phi", 0) self.out.fillBranch("KinTopZq_mass", 0) return True else: # Variable construct : Lepton vars = [pt, eta, phi, mass] Wlepvar = TLorentzVector() Zlep1var = TLorentzVector() Zlep2var = TLorentzVector() Wlepvar.SetPtEtaPhiM(event._tree.b_out_Lepton1_pt, event._tree.b_out_Lepton1_eta, event._tree.b_out_Lepton1_phi, event._tree.b_out_Lepton1_mass) Zlep1var.SetPtEtaPhiM(event._tree.b_out_Lepton2_pt, event._tree.b_out_Lepton2_eta, event._tree.b_out_Lepton2_phi, event._tree.b_out_Lepton2_mass) Zlep2var.SetPtEtaPhiM(event._tree.b_out_Lepton3_pt, event._tree.b_out_Lepton3_eta, event._tree.b_out_Lepton3_phi, event._tree.b_out_Lepton3_mass) # Variable coonstruct : Jet vars = [pt, eta, phi, mass, CSVv2] #if event._tree._b_out_nGoodJet < 2: continue # nJet >= 2 for tZq reconstruction bvar = TLorentzVector() qvar = TLorentzVector() bvar.SetPtEtaPhiM(event._tree.b_out_GoodJet_pt[0], event._tree.b_out_GoodJet_eta[0], event._tree.b_out_GoodJet_phi[0], event._tree.b_out_GoodJet_mass[0]) qvar.SetPtEtaPhiM(event._tree.b_out_GoodJet_pt[1], event._tree.b_out_GoodJet_eta[1], event._tree.b_out_GoodJet_phi[1], event._tree.b_out_GoodJet_mass[1]) #bjetCSV, qjetCSV = event._tree.b_out_GoodJet_CSVv2[0], event._tree.b_out_GoodJet_CSVv2[1] bjetDeepFlavB, qjetDeepFlavB = event._tree.b_out_GoodJet_DeepFlavB[ 0], event._tree.b_out_GoodJet_DeepFlavB[1] # Variable condtruct : Neutrino vars = [MET, phi] metvar = TLorentzVector() metvar.SetPtEtaPhiM(event._tree.b_out_MET_pt, 0, event._tree.b_out_MET_phi, 0) ## b jet assign by CSVv2 discriminator #if ( bjetCSV < qjetCSV ): # bvar, qvar = qvar, bvar # bjetCSV, qjetCSV = qjetCSV, bjetCSV # b jet assign by CSVv2 discriminator if (bjetDeepFlavB < qjetDeepFlavB): bvar, qvar = qvar, bvar bjetDeepFlavB, qjetDeepFlavB = qjetDeepFlavB, bjetDeepFlavB # pxyzE calculation & construction : [px, py, pz, E] Zlep1 = self.getKinVar(Zlep1var.Pt(), Zlep1var.Eta(), Zlep1var.Phi(), 0.) Zlep2 = self.getKinVar(Zlep2var.Pt(), Zlep2var.Eta(), Zlep2var.Phi(), 0.) Wlep = self.getKinVar(Wlepvar.Pt(), Wlepvar.Eta(), Wlepvar.Phi(), 0.) ## Assume lepton mass = 0 bjet = self.getKinVar(bvar.Pt(), bvar.Eta(), bvar.Phi(), bvar.M()) qjet = self.getKinVar(qvar.Pt(), qvar.Eta(), qvar.Phi(), qvar.M()) # MET pxy calculation & construction : [px, py] metpx = metvar.Pt() * (math.cos(metvar.Phi())) metpy = metvar.Pt() * (math.sin(metvar.Phi())) met = [metpx, metpy] # Calculate pz_neu and E_neu metpz = self.getSol(Wlep[0], Wlep[1], Wlep[2], Wlep[3], 0., met[0], met[1], Wmass) # Assume lepton mass = 0 posneuE = math.sqrt((metpx * metpx) + (metpy * metpy) + (metpz[0] * metpz[0])) negneuE = math.sqrt((metpx * metpx) + (metpy * metpy) + (metpz[1] * metpz[1])) consted = metpz[2] # Reconstructed flag # SM Top mass construction SMpxs = [bjet[0], Wlep[0], met[0]] SMpys = [bjet[1], Wlep[1], met[1]] posSMpzs = [bjet[2], Wlep[2], metpz[0]] negSMpzs = [bjet[2], Wlep[2], metpz[1]] posSMEs = [bjet[3], Wlep[3], posneuE] negSMEs = [bjet[3], Wlep[3], negneuE] posTMass = self.getTPEPM(SMpxs, SMpys, posSMpzs, posSMEs)[3] negTMass = self.getTPEPM(SMpxs, SMpys, negSMpzs, negSMEs)[3] ## Top mass variance comparison and reconsruct SM Top KinVals if (math.fabs(posTMass - OriginTmass) < math.fabs(negTMass - OriginTmass)): SMTMass = posTMass SMTKinVal = self.getTPEPM(SMpxs, SMpys, posSMpzs, posSMEs) else: SMTMass = negTMass SMTKinVal = self.getTPEPM(SMpxs, SMpys, negSMpzs, negSMEs) # FCNC Top reconstruction FCNCpxs = [qjet[0], Zlep1[0], Zlep2[0]] FCNCpys = [qjet[1], Zlep1[1], Zlep2[1]] FCNCpzs = [qjet[2], Zlep1[2], Zlep2[2]] FCNCEs = [qjet[3], Zlep1[3], Zlep2[3]] FCNCTKinVal = self.getTPEPM(FCNCpxs, FCNCpys, FCNCpzs, FCNCEs) self.out.fillBranch("KinTop_status", consted) self.out.fillBranch("KinTopWb_pt", SMTKinVal[0]) self.out.fillBranch("KinTopWb_eta", SMTKinVal[1]) self.out.fillBranch("KinTopWb_phi", SMTKinVal[2]) self.out.fillBranch("KinTopWb_mass", SMTKinVal[3]) self.out.fillBranch("KinTopZq_pt", FCNCTKinVal[0]) self.out.fillBranch("KinTopZq_eta", FCNCTKinVal[1]) self.out.fillBranch("KinTopZq_phi", FCNCTKinVal[2]) self.out.fillBranch("KinTopZq_mass", FCNCTKinVal[3]) ## for debugging #print " Coefficients A/B/aterm/bterm/cterm : ", metpz[3], metpz[4], metpz[5], metpz[6], metpz[7] #print " Sum pxyzE for top solution : ", SMTKinVal[4], SMTKinVal[5], SMTKinVal[6], SMTKinVal[7] #print "Z lepton 1 : ", Zlep1 #print "Z lepton 2 : ", Zlep2 #print "W lepton 1 : ", Wlep #print "b jet : ", bjet #print "q jet : ", qjet #print "neu px, py : ", met #print " Neu Z pos/neg solution : ", metpz[0], metpz[1] #print "neu pos/neg energy : ", posneuE, negneuE return True
for event in events: # if count>10:break tau_mad = TLorentzVector(0,0,0,0) neu_mad = TLorentzVector(0,0,0,0) count = count+1 event.getByLabel(labelPruned, handlePruned) pruned = handlePruned.product() event.getByLabel(geninfoLabel,geninfo) info_pruned = geninfo.product() weight_mad = info_pruned.weight()/abs(info_pruned.weight()) if count%100==0: print "count = ", count, 'weight: ', weight_mad for i in range(pruned.hepeup().NUP): if abs(pruned.hepeup().IDUP[i])==15: tau_mad.SetPxPyPzE(pruned.hepeup().PUP[i][0],pruned.hepeup().PUP[i][1],pruned.hepeup().PUP[i][2],pruned.hepeup().PUP[i][3]) lphi_mad.Fill(tau_mad.Phi(),weight_mad) lpt_mad.Fill(tau_mad.Pt(),weight_mad) leta_mad.Fill(tau_mad.Eta(),weight_mad) if abs(pruned.hepeup().IDUP[i])==16: neu_mad.SetPxPyPzE(pruned.hepeup().PUP[i][0],pruned.hepeup().PUP[i][1],pruned.hepeup().PUP[i][2],pruned.hepeup().PUP[i][3]) neuphi_mad.Fill(neu_mad.Phi(),weight_mad) neupt_mad.Fill(neu_mad.Pt(),weight_mad) neueta_mad.Fill(neu_mad.Eta(),weight_mad) lphi_mad.Scale(1./lphi_mad.Integral()) lpt_mad.Scale(1./lpt_mad.Integral()) leta_mad.Scale(1./leta_mad.Integral()) neuphi_mad.Scale(1./neuphi_mad.Integral()) neupt_mad.Scale(1./neupt_mad.Integral()) neueta_mad.Scale(1./neueta_mad.Integral()) lphi_mad.SetMaximum(1.4*lphi_mad.GetMaximum()) lpt_mad.SetMaximum(1.4*lpt_mad.GetMaximum())
decay_lenght = TVector3(sim.fDecayVtxX, sim.fDecayVtxY, sim.fDecayVtxZ) dl = decay_lenght.Mag() if hyp.Gamma() == 0 or hyp.Beta() == 0: continue ct = dl / (hyp.Gamma() * hyp.Beta()) if hyp.Pt() < 1. or hyp.Pt() > 10.: continue hist_ctsim.Fill(ct) hist_ptsim.Fill(hyp.Pt()) hist_psim.Fill(hyp.P()) hist_etasim.Fill(hyp.Eta()) hist_phisim.Fill(hyp.Phi()) # rec - sim diff # if sim.fRecoIndex >= 0: # r = ev.RHypertriton[sim.fRecoIndex] # hyp_rec = TLorentzVector() # deu_rec = TLorentzVector() # p_rec = TLorentzVector() # pi_rec = TLorentzVector() # deu_rec.SetXYZM(r.fPxDeu, r.fPyDeu, r.fPzDeu, AliPID.ParticleMass(AliPID.kDeuteron)) # p_rec.SetXYZM(r.fPxP, r.fPyP, r.fPzP, AliPID.ParticleMass(AliPID.kProton)) # pi_rec.SetXYZM(r.fPxPi, r.fPyPi, r.fPzPi, AliPID.ParticleMass(AliPID.kPion)) # hyp_rec = deu_rec + p_rec + pi_rec
if bbar: px4 = float(bbar[0].split()[6]) py4 = float(bbar[0].split()[7]) pz4 = float(bbar[0].split()[8]) e4 = float(bbar[0].split()[9]) p4 = TLorentzVector(px4, py4, pz4, e4) HggP4 = p1 + p2 HbbP4 = p3 + p4 m_H1.append(HggP4.M()) m_H2.append(HbbP4.M()) b_pT.append(p3.Pt()) b_eta.append(p3.Eta()) b_phi.append(p3.Phi()) bbar_pT.append(p4.Pt()) bbar_eta.append(p4.Eta()) bbar_phi.append(p4.Phi()) a_pT.append(p1.Pt()) a_eta.append(p1.Eta()) a_phi.append(p1.Phi()) for i in m_H1: h_m_H1.Fill(i) hists1.append(h_m_H1) for i in m_H2: h_m_H2.Fill(i)
fig1.savefig('antitau_phi.png') plt.clf() for event in range(pred.shape[0]): tau_lorentz_no_neutrino = TLorentzVector() for index in range(0, tau_features_test.shape[1], 3): lorentz = TLorentzVector() lorentz.SetPtEtaPhiM(tau_features_test[event][index], tau_features_test[event][index + 1], tau_features_test[event][index + 2], 0.139) tau_lorentz_no_neutrino += lorentz tofill['tau_pt_no_neutrino'] = tau_lorentz_no_neutrino.Pt() tofill['tau_eta_no_neutrino'] = tau_lorentz_no_neutrino.Eta() tofill['tau_phi_no_neutrino'] = tau_lorentz_no_neutrino.Phi() tofill['tau_mass_no_neutrino'] = tau_lorentz_no_neutrino.M() tau_lorentz = TLorentzVector() tau_lorentz.SetPtEtaPhiM( tau_lorentz_no_neutrino.Pt(), tau_lorentz_no_neutrino.Eta(), tau_lorentz_no_neutrino.Phi(), tau_lorentz_no_neutrino.M(), ) for index in range(0, pred.shape[1], 3): lorentz = TLorentzVector() lorentz.SetPtEtaPhiM(pred[event][index], pred[event][index + 1], pred[event][index + 2], 0) tau_lorentz += lorentz