def main(filenames, outfilename=None): #filenames = sys.argv[1:] outfile = None if outfilename is None: outfilename = filenames[0].split('/')[-1].split( '.root')[0] + "_OUTROOT.root" outfile = ROOT.TFile(outfilename, 'recreate') outtree = ROOT.TTree('T', 'TTree object to hold data.') print("Will open files:") for f in filenames: print(f) # Define our data we want to write out. maxn = 128 ntop = array('i', [0]) outtree.Branch('ntop', ntop, 'ntop/I') topmass = array('f', maxn * [0.]) outtree.Branch('topmass', topmass, 'topmass[ntop]/F') wmass = array('f', maxn * [0.]) outtree.Branch('wmass', wmass, 'wmass[ntop]/F') wangle = array('f', maxn * [0.]) outtree.Branch('wangle', wangle, 'wangle[ntop]/F') wdR = array('f', maxn * [0.]) outtree.Branch('wdR', wdR, 'wdR[ntop]/F') wH = array('f', maxn * [0.]) outtree.Branch('wH', wH, 'wH[ntop]/F') METpt = array('f', [0.]) outtree.Branch('METpt', METpt, 'METpt/F') nmuon = array('i', [0]) outtree.Branch('nmuon', nmuon, 'nmuon/I') leadmupt = array('f', [0.]) outtree.Branch('leadmupt', leadmupt, 'leadmupt/F') leadmueta = array('f', [0.]) outtree.Branch('leadmueta', leadmueta, 'leadmueta/F') subleadmupt = array('f', [0.]) outtree.Branch('subleadmupt', subleadmupt, 'subleadmupt/F') subleadmueta = array('f', [0.]) outtree.Branch('subleadmueta', subleadmueta, 'subleadmueta/F') trig_HLT_IsoMu24_accept = array('i', [0]) trig_HLT_IsoTkMu24_accept = array('i', [0]) trig_HLT_IsoMu22_eta2p1_accept = array('i', [0]) trig_HLT_IsoTkMu22_eta2p1_accept = array('i', [0]) outtree.Branch("trig_HLT_IsoMu24_accept", trig_HLT_IsoMu24_accept, 'trig_HLT_IsoMu24_accept/I') outtree.Branch("trig_HLT_IsoTkMu24_accept", trig_HLT_IsoTkMu24_accept, 'trig_HLT_IsoTkMu24_accept/I') outtree.Branch("trig_HLT_IsoMu22_eta2p1_accept", trig_HLT_IsoMu24_accept, 'trig_HLT_IsoMu24_accept/I') outtree.Branch("trig_HLT_IsoTkMu22_eta2p1_accept", trig_HLT_IsoTkMu22_eta2p1_accept, 'trig_HLT_IsoTkMu22_eta2p1_accept/I') njet = array('i', [0]) outtree.Branch('njet', njet, 'njet/I') jetcsv = array('f', maxn * [0.]) outtree.Branch('jetcsv', jetcsv, 'jetcsv[njet]/F') #nbjet = array( 'i', [ 0 ] ) #outtree.Branch( 'nbjet', nbjet, 'nbjet/I' ) genbjetdR = array('f', maxn * [0.]) outtree.Branch('genbjetdR', genbjetdR, 'genbjetdR[njet]/F') genbjetdpt = array('f', maxn * [0.]) outtree.Branch('genbjetdpt', genbjetdpt, 'genbjetdpt[njet]/F') nbjetmatch = array('i', [0]) outtree.Branch('nbjetmatch', nbjetmatch, 'nbjetmatch/I') bjetmatchcsv = array('f', maxn * [0.]) outtree.Branch('bjetmatchcsv', bjetmatchcsv, 'bjetmatchcsv[nbjetmatch]/F') nbjetnotmatch = array('i', [0]) outtree.Branch('nbjetnotmatch', nbjetnotmatch, 'nbjetnotmatch/I') bjetnotmatchcsv = array('f', maxn * [0.]) outtree.Branch('bjetnotmatchcsv', bjetnotmatchcsv, 'bjetnotmatchcsv[nbjetnotmatch]/F') ''' data = {} data["topmass"] = [] data["wmass"] = [] data["csvs"] = [] data["angles"] = [] data["dRs"] = [] data["METpt"] = [] data["njets"] = [] data["nbjets"] = [] data["mumass"] = [] data["leadmupt"] = [] data["subleadmupt"] = [] data["leadmueta"] = [] data["subleadmueta"] = [] data["elecmass"] = [] data["leadelecpt"] = [] data["subleadelecpt"] = [] data["leadeleceta"] = [] data["subleadeleceta"] = [] data["leadjetpt"] = [] data["subleadjetpt"] = [] data["leadjeteta"] = [] data["subleadjeteta"] = [] data["trig_HLT_IsoMu24_accept"] = [] data["trig_HLT_IsoTkMu24_accept"] = [] data["trig_HLT_IsoMu22_eta2p1_accept"] = [] data["trig_HLT_IsoTkMu22_eta2p1_accept"] = [] ''' # Loop over the files. for filename in filenames: print("Opening file %s" % (filename)) f = ROOT.TFile.Open(filename) #f.ls() tree = f.Get("IIHEAnalysis") #tree.Print() #tree.Print("*jet*") #exit() nentries = tree.GetEntries() print("Will run over %d entries" % (nentries)) for i in range(nentries): if i % 1000 == 0: output = "Event: %d out of %d" % (i, nentries) print(output) tree.GetEntry(i) gen_b = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] #''' gen_particles = tbt.get_gen_particles(tree) #print("----------") ib = 0 for gen in gen_particles: #if np.abs(gen['pdgId'])==24 and gen['ndau']==2: if np.abs(gen['pdgId']) == 5 and np.abs(gen['motherpdg']) == 6: #print(gen) p4 = gen['p4'] b_pt, b_eta, b_phi = tbt.xyzTOetaphi(p4[1], p4[2], p4[3]) gen_b[ib] = b_pt, b_eta, b_phi ib += 1 # Assume we only have 2 b-quarks coming from the tops per event #''' #njet = tree.jet_n pt = tree.jet_pt px = tree.jet_px py = tree.jet_py pz = tree.jet_pz eta = tree.jet_eta phi = tree.jet_phi e = tree.jet_energy csv = tree.jet_CSVv2 metpt = tree.MET_Pt mue = tree.mu_gt_p mupx = tree.mu_gt_px mupy = tree.mu_gt_py mupz = tree.mu_gt_pz mupt = tree.mu_gt_pt mueta = tree.mu_gt_eta muphi = tree.mu_gt_phi trig_HLT_IsoMu24_accept[0] = tree.trig_HLT_IsoMu24_accept trig_HLT_IsoTkMu24_accept[0] = tree.trig_HLT_IsoTkMu24_accept trig_HLT_IsoMu22_eta2p1_accept[ 0] = tree.trig_HLT_IsoMu22_eta2p1_accept trig_HLT_IsoTkMu22_eta2p1_accept[ 0] = tree.trig_HLT_IsoTkMu22_eta2p1_accept # Doing this because the jet_n value seems to be bigger. njet[0] = len(csv) jet = [] bjet = [] muon = [] #print(njet,len(csv),len(px)) # Try to match bjets #print("Looking -------------------------------------------------------") nj = 0 nbj = 0 nbjetmatch[0] = 0 nbjetnotmatch[0] = 0 for n in range(njet[0]): mindR = 1e6 for gb in gen_b: etaph0 = [eta[n], phi[n]] etaph1 = [gb[1], gb[2]] gendR = tbt.deltaR(etaph0, etaph1) dpt = math.fabs(pt[n] - gb[0]) # To store in TTree if gendR < mindR: genbjetdR[n] = gendR genbjetdpt[n] = dpt mindR = gendR #''' if dpt < 100 and gendR < 0.3: #print("FOUND MATCH! ",csv[n]) #print(gb) #print(pt[n],eta[n],phi[n]) #print(gendR) bjetmatchcsv[nbjetmatch[0]] = jetcsv[n] nbjetmatch[0] += 1 else: bjetnotmatchcsv[nbjetnotmatch[0]] = jetcsv[n] nbjetnotmatch[0] += 1 #''' nj = 0 for n in range(njet[0]): if pt[n] > 30: #data["csvs"].append(csv[n]) if csv[n] > 0.87 or csv[n] < -9: bjet.append( [e[n], px[n], py[n], pz[n], eta[n], phi[n]]) jetcsv[nj] = csv[n] else: jet.append([e[n], px[n], py[n], pz[n], eta[n], phi[n]]) jetcsv[nj] = csv[n] nj += 1 #print("+++++++++++++++++++++++++++") #''' #print("+++++++++++++++++++++++++++") if len(mue) > 0: leadmupt[0] = mupt[0] leadmueta[0] = mueta[0] if len(mue) > 1: subleadmupt[0] = mupt[1] subleadmueta[0] = mueta[1] ''' for n in range(len(mue)): print(mupt[n]) #muon.append([mue[n],mupx[n],mupy[n],mupz[n],mueta[n],muphi[n]]) #data["mumass"].append(mue[n]*mue[n] - (mupy[n]*mupy[n] + mupx[n]*mupx[n] + mupz[n]*mupz[n])) if n == 0: leadmupt[0] = mupt[n] leadmueta[0] = mueta[n] if n == 1: subleadmupt[0] = mupt[n] subleadmueta[0] = mueta[n] ''' #print("+++++++++++++++++++++++++++") #''' ntop[0] = 0 for b in bjet: for j in range(0, len(jet) - 1): for k in range(j + 1, len(jet)): #print(b,jet[j],jet[k]) #print(ntop) if ntop[0] < maxn: m = tbt.invmass([b[0:4], jet[j][0:4], jet[k][0:4]]) topmass[ntop[0]] = m wm = tbt.invmass([jet[j][0:4], jet[k][0:4]]) wmass[ntop[0]] = wm wangle[ntop[0]] = tbt.angle_between_vectors( jet[j][1:4], jet[k][1:4]) wdR[ntop[0]] = tbt.deltaR(jet[j][4:], jet[k][4:]) wH[ntop[0]] = tbt.scalarH([jet[j], jet[k]]) ntop[0] += 1 METpt[0] = metpt #data['njets'].append(njet) #data['nbjets'].append(len(bjet)) outtree.Fill() ################################################################################ #if outfile is None: #outfile = filenames[0].split('/')[-1].split('.root')[0] + "_OUTROOT.root" #tbt.write_pickle_file(data,outfile) outfile.Write() outfile.Close()
bjet = [] #print(njet,len(csv),len(px)) for n in range(njet): if pt[n] > 30: csvs.append(csv[n]) if csv[n] > 0.87: bjet.append([e[n], px[n], py[n], pz[n], eta[n], phi[n]]) else: jet.append([e[n], px[n], py[n], pz[n], eta[n], phi[n]]) for b in bjet: for j in range(0, len(jet) - 1): for k in range(j + 1, len(jet)): #print(b,jet[j],jet[k]) m = tbt.invmass([b[0:4], jet[j][0:4], jet[k][0:4]]) topmass.append(m) wm = tbt.invmass([jet[j][0:4], jet[k][0:4]]) wmass.append(wm) angles.append( tbt.angle_between_vectors(jet[j][1:4], jet[k][1:4])) dRs.append(tbt.deltaR(jet[j][4:], jet[k][4:])) ################################################################################ topmass = np.array(topmass) wmass = np.array(wmass) csvs = np.array(csvs) angles = np.array(angles) dRs = np.array(dRs)
def main(filenames, outfilename=None): # Loop over the files. vals = [[], [], [], [], [], []] plotvals = OrderedDict() plotvals["pt"] = [[], [], []] plotvals["eta"] = [[], [], []] plotvals["csv"] = [[], [], []] plotvals["NHF"] = [[], [], []] plotvals["NEMF"] = [[], [], []] plotvals["CHF"] = [[], [], []] plotvals["MUF"] = [[], [], []] plotvals["CEMF"] = [[], [], []] plotvals["NC"] = [[], [], []] plotvals["NNP"] = [[], [], []] plotvals["CHM"] = [[], [], []] wmass = [] wdR = [] topmass = [] topdR_bnb = [] topdR_nbnb = [] top01 = [] top02 = [] top12 = [] for filename in filenames: print("Opening file %s" % (filename)) f = ROOT.TFile.Open(filename) tree = f.Get("T") #tree.Print() nentries = tree.GetEntries() print("Will run over %d entries" % (nentries)) for i in range(nentries): if i % 10000 == 0: output = "Event: %d out of %d" % (i, nentries) print(output) tree.GetEntry(i) alljets = tbt.get_good_jets(tree, ptcut=30) bjets, nonbjets = tbt.get_top_candidate_jets(alljets, csvcut=0.87) #print("-------------") #if len(bjets)>0 and len(nonbjets)>0: #print(bjets) #print(nonbjets) if bjets is None or nonbjets is None: continue if len(nonbjets) < 4: continue #''' #print("=======================") for bjet in bjets: for j in range(0, len(nonbjets) - 1): for k in range(j + 1, len(nonbjets)): nbjet0 = nonbjets[j] nbjet1 = nonbjets[k] #print(bjet) #print(nonbjets) mass = tbt.invmass([nbjet0, nbjet1]) dR = tbt.deltaR(nbjet0[5:], nbjet1[5:]) if mass > 60 and mass < 105: wmass.append(mass) wdR.append(dR) mass = tbt.invmass([nbjet0, nbjet1, bjet]) topmass.append(mass) dR = tbt.deltaR(nbjet0[5:], bjet[5:]) topdR_bnb.append(dR) dR = tbt.deltaR(nbjet1[5:], bjet[5:]) topdR_bnb.append(dR) mass = tbt.invmass([nbjet0, bjet]) top01.append(mass**2) mass = tbt.invmass([nbjet1, bjet]) top02.append(mass**2) mass = tbt.invmass([nbjet0, nbjet1]) top12.append(mass**2) top01 = np.array(top01) top02 = np.array(top02) top12 = np.array(top12) wmass = np.array(wmass) wdR = np.array(wdR) topmass = np.array(topmass) topdR_bnb = np.array(topdR_bnb) dal_cuts = tbt.dalitz_boundaries(top02, top12) print(len(dal_cuts), len(dal_cuts[dal_cuts])) print(len(topmass), len(wmass)) alpha = 0.1 plt.figure() plt.subplot(3, 2, 1) #plt.hist(wmass,bins=100,range=(20,140)) h = plt.hist(wmass, bins=400, range=(0, 400)) plt.plot([80.3, 80.3], [0, 1.1 * max(h[0])], 'k--') plt.subplot(3, 2, 2) h = plt.hist(topmass, bins=100, range=(0, 400)) plt.plot([173, 173], [0, 1.1 * max(h[0])], 'k--') plt.subplot(3, 2, 3) plt.hist(wdR, bins=100, range=(-1, 7)) plt.subplot(3, 2, 4) plt.hist(topdR_bnb, bins=100, range=(-1, 7)) plt.subplot(3, 2, 5) plt.plot(wmass, wdR, '.', markersize=1.0, alpha=alpha) plt.xlim(20, 140) plt.ylim(-1, 7) plt.figure() plt.subplot(1, 3, 1) plt.plot(top01, top02, '.', alpha=alpha, markersize=0.5) plt.xlim(0, 30000) plt.ylim(0, 30000) plt.subplot(1, 3, 2) plt.plot(top01, top12, '.', alpha=alpha, markersize=0.5) plt.xlim(0, 30000) plt.ylim(0, 30000) plt.subplot(1, 3, 3) plt.plot(top02, top12, '.', alpha=alpha, markersize=0.5) plt.xlim(0, 30000) plt.ylim(0, 30000) ############################################################################ plt.figure() plt.subplot(1, 3, 1) plt.plot(top01[dal_cuts], top02[dal_cuts], '.', alpha=alpha, markersize=0.5) plt.xlim(0, 30000) plt.ylim(0, 30000) plt.subplot(1, 3, 2) plt.plot(top01[dal_cuts], top12[dal_cuts], '.', alpha=alpha, markersize=0.5) plt.xlim(0, 30000) plt.ylim(0, 30000) plt.subplot(1, 3, 3) plt.plot(top02[dal_cuts], top12[dal_cuts], '.', alpha=alpha, markersize=0.5) plt.xlim(0, 30000) plt.ylim(0, 30000) plt.figure() plt.subplot(3, 2, 1) h = plt.hist(wmass[dal_cuts], bins=100, range=(20, 140)) plt.plot([80.3, 80.3], [0, 1.1 * max(h[0])], 'k--') plt.subplot(3, 2, 2) h = plt.hist(topmass[dal_cuts], bins=100, range=(0, 400)) plt.plot([173, 173], [0, 1.1 * max(h[0])], 'k--') plt.subplot(3, 2, 3) plt.hist(wdR[dal_cuts], bins=100, range=(-1, 7)) #plt.subplot(3,2,4) #plt.hist(topdR_bnb[dal_cuts],bins=100,range=(-1,7)) plt.subplot(3, 2, 5) plt.plot(wmass[dal_cuts], wdR[dal_cuts], '.', markersize=1.0, alpha=alpha) plt.xlim(20, 140) plt.ylim(-1, 7) plt.show()
def main(filenames, outfile=None): #filenames = sys.argv[1:] print("Will open files:") for f in filenames: print(f) # Define our data we want to write out. data = {} data["topmass"] = [] data["wmass"] = [] data["csvs"] = [] data["angles"] = [] data["dRs"] = [] data["METpt"] = [] data["njets"] = [] data["nbjets"] = [] data["mumass"] = [] data["leadmupt"] = [] data["subleadmupt"] = [] data["leadmueta"] = [] data["subleadmueta"] = [] data["elecmass"] = [] data["leadelecpt"] = [] data["subleadelecpt"] = [] data["leadeleceta"] = [] data["subleadeleceta"] = [] data["leadjetpt"] = [] data["subleadjetpt"] = [] data["leadjeteta"] = [] data["subleadjeteta"] = [] data["trig_HLT_IsoMu24_accept"] = [] data["trig_HLT_IsoTkMu24_accept"] = [] data["trig_HLT_IsoMu22_eta2p1_accept"] = [] data["trig_HLT_IsoTkMu22_eta2p1_accept"] = [] # Loop over the files. for filename in filenames: print("Opening file %s" % (filename)) f = ROOT.TFile.Open(filename) #f.ls() tree = f.Get("IIHEAnalysis") #tree.Print() #tree.Print("*jet*") #exit() nentries = tree.GetEntries() print("Will run over %d entries" % (nentries)) for i in range(nentries): if i % 1000 == 0: output = "Event: %d out of %d" % (i, nentries) print(output) tree.GetEntry(i) njet = tree.jet_n pt = tree.jet_pt px = tree.jet_px py = tree.jet_py pz = tree.jet_pz eta = tree.jet_eta phi = tree.jet_phi e = tree.jet_energy csv = tree.jet_CSVv2 metpt = tree.MET_Pt mue = tree.mu_gt_p mupx = tree.mu_gt_px mupy = tree.mu_gt_py mupz = tree.mu_gt_pz mupt = tree.mu_gt_pt mueta = tree.mu_gt_eta muphi = tree.mu_gt_phi data["trig_HLT_IsoMu24_accept"].append( tree.trig_HLT_IsoMu24_accept) data["trig_HLT_IsoTkMu24_accept"].append( tree.trig_HLT_IsoTkMu24_accept) data["trig_HLT_IsoMu22_eta2p1_accept"].append( tree.trig_HLT_IsoMu22_eta2p1_accept) data["trig_HLT_IsoTkMu22_eta2p1_accept"].append( tree.trig_HLT_IsoTkMu22_eta2p1_accept) # Doing this because the jet_n value seems to be bigger. njet = len(csv) jet = [] bjet = [] muon = [] #print(njet,len(csv),len(px)) for n in range(njet): if pt[n] > 30: data["csvs"].append(csv[n]) if csv[n] > 0.87: bjet.append( [e[n], px[n], py[n], pz[n], eta[n], phi[n]]) else: jet.append([e[n], px[n], py[n], pz[n], eta[n], phi[n]]) #print("+++++++++++++++++++++++++++") for n in range(len(mue)): #print(mupt[n]) muon.append( [mue[n], mupx[n], mupy[n], mupz[n], mueta[n], muphi[n]]) data["mumass"].append(mue[n] * mue[n] - (mupy[n] * mupy[n] + mupx[n] * mupx[n] + mupz[n] * mupz[n])) if n == 0: data["leadmupt"].append(mupt[n]) data["leadmueta"].append(mueta[n]) if n == 1: data["subleadmupt"].append(mupt[n]) data["subleadmueta"].append(mueta[n]) #print("+++++++++++++++++++++++++++") for b in bjet: for j in range(0, len(jet) - 1): for k in range(j + 1, len(jet)): #print(b,jet[j],jet[k]) m = tbt.invmass([b[0:4], jet[j][0:4], jet[k][0:4]]) data["topmass"].append(m) wm = tbt.invmass([jet[j][0:4], jet[k][0:4]]) data["wmass"].append(wm) data["angles"].append( tbt.angle_between_vectors(jet[j][1:4], jet[k][1:4])) data["dRs"].append(tbt.deltaR(jet[j][4:], jet[k][4:])) # There is only 1 MET, but we associate with every W/top candidate. data['METpt'].append(metpt) data['njets'].append(njet) data['nbjets'].append(len(bjet)) ################################################################################ if outfile is None: outfile = filenames[0].split('/')[-1].split('.root')[0] + "_PICKLE.pkl" tbt.write_pickle_file(data, outfile)
def main(): filenames = sys.argv[1:] print("Will open files:") for f in filenames: print(f) # mu_pfIsoDbCorrected04 < 0.12 # mu pt > 25 # mu |eta| < 2.1 # # Define our data we want to write out. data = {} data["topmass"] = [] data["wmass"] = [] data["csvs"] = [] data["angles"] = [] data["dRs"] = [] data["METpt"] = [] data["muiso04"] = [] data["muisPF"] = [] data["muist"] = [] data["muism"] = [] data["muisl"] = [] data["mupt"] = [] data["mueta"] = [] # Loop over the files. for filename in filenames: print("Opening file ", filename) f = ROOT.TFile.Open(filename) #f.ls() tree = f.Get("IIHEAnalysis") #tree.Print() #tree.Print("*jet*") #exit() nentries = tree.GetEntries() print("Will run over %d entries" % (nentries)) for i in range(nentries): if i % 1000 == 0: output = "Event: %d out of %d" % (i, nentries) print(output) tree.GetEntry(i) njet = tree.jet_n pt = tree.jet_pt px = tree.jet_px py = tree.jet_py pz = tree.jet_pz eta = tree.jet_eta phi = tree.jet_phi e = tree.jet_energy csv = tree.jet_CSVv2 metpt = tree.MET_Pt muisPF = tree.mu_isPFMuon muistight = tree.mu_isTightMuon muismedium = tree.mu_isMediumMuon muisloose = tree.mu_isLooseMuon mupt = tree.mu_gt_pt muiso04 = tree.mu_pfIsoDbCorrected04 # Muons nmuons = len(mupt) for n in range(nmuons): if muisPF[n]: data["mupt"].append(mupt[n]) data["muiso04"].append(muiso04[n]) data["muisPF"].append(int(bool(muisPF[n]))) data["muist"].append(int(int(bool(muistight[n])))) data["muism"].append(int(int(bool(muismedium[n])))) data["muisl"].append(int(int(bool(muisloose[n])))) # Doing this because the jet_n value seems to be bigger. njet = len(csv) jet = [] bjet = [] #print(njet,len(csv),len(px)) for n in range(njet): if pt[n] > 30: data["csvs"].append(csv[n]) if csv[n] > 0.87: bjet.append( [e[n], px[n], py[n], pz[n], eta[n], phi[n]]) else: jet.append([e[n], px[n], py[n], pz[n], eta[n], phi[n]]) for b in bjet: for j in range(0, len(jet) - 1): for k in range(j + 1, len(jet)): #print(b,jet[j],jet[k]) m = tbt.invmass([b[0:4], jet[j][0:4], jet[k][0:4]]) data["topmass"].append(m) wm = tbt.invmass([jet[j][0:4], jet[k][0:4]]) data["wmass"].append(wm) data["angles"].append( tbt.angle_between_vectors(jet[j][1:4], jet[k][1:4])) data["dRs"].append(tbt.deltaR(jet[j][4:], jet[k][4:])) # There is only 1 MET, but we associate with every W/top candidate. data['METpt'].append(metpt) ################################################################################ outfilename = filenames[0].split('/')[-1].split('.root')[0] + "_PICKLE.pkl" tbt.write_pickle_file(data, outfilename)