def record_aviv_truth_jets(is_signal, input_list, events_to_read, event_data_dump): tree_name = 'Nominal' branches = [ 'eventWeight', ('truth_particles', ['tpartpdgID', 'tpartstatus', 'tpartpT', 'tparteta', 'tpartphi', 'tpartm'] ), ('truth_jets', ['truthjpT', 'truthjeta', 'truthjphi', 'truthjm'] ) ] for event in event_iterator(input_list, tree_name, branches, events_to_read): # Loop over truth jets and convert them into generic acorn_jet objects truth_particles = [ tp.copy() for tp in event['truth_particles'] ] recorded_jets = [] for tj in event['truth_jets']: v = TLorentzVector.from_ptetaphim(tj['truthjpT'], tj['truthjeta'], tj['truthjphi'], tj['truthjm']) pdgid = match_aviv_reco_jet(v, truth_particles) # Create jet object storing the essential aspects of the ntuple truth jet, # faking some of the data normally associated with reco jets new_jet = acorn_jet(v, pdgid, False, True, True, -1, None, []) recorded_jets.append(new_jet) # Categorize event, and then either discard the event or perform tagging on it for category in event_data_dump.values(): category.add_event(recorded_jets, is_signal, event['eventWeight'])
def record_cmilkeV1_truth_jets(is_signal, input_list, events_to_read, event_data_dump): tree_name = 'ntuple' branches = [ 'EventWeight', ('truth_jets', ['TruthJetPt', 'TruthJetEta', 'TruthJetPhi', 'TruthJetM', 'TruthJetID'] ) ] for event in event_iterator(input_list, tree_name, branches, events_to_read): # Loop over truth jets and convert them into generic acorn_jet objects recorded_jets = [] for truth_jet in event['truth_jets']: v = TLorentzVector.from_ptetaphim( truth_jet['TruthJetPt'], truth_jet['TruthJetEta'], truth_jet['TruthJetPhi'], truth_jet['TruthJetM'] ) pdgid = truth_jet['TruthJetID'] # Create jet object storing the essential aspects of the ntuple truth jet, # faking some of the data normally associated with reco jets new_jet = acorn_jet(v, pdgid, False, True, True, -1) recorded_jets.append(new_jet) # Categorize event, and then either discard the event or perform tagging on it for category in event_data_dump.values(): category.add_event(recorded_jets, is_signal, event['EventWeight'])
def record_aviv_reco_jets(is_signal, input_list, events_to_read, event_data_dump): tree_name = 'Nominal' branches = [ 'eventWeight', ('truth_particles', ['tpartpdgID', 'tpartstatus', 'tpartpT', 'tparteta', 'tpartphi', 'tpartm'] ), ('reco_jets', ['tj0pT', 'j0_isPU', 'j0_QGTagger', 'j0_JVT', 'j0_fJVT_Tight', 'j0pT', 'j0eta', 'j0phi', 'j0m'] ) ] for event in event_iterator(input_list, tree_name, branches, events_to_read): # Loop over reco jets and convert them into generic acorn_jet objects #truth_particles = [ tp.copy() for tp in event['truth_particles'] ] particle_list = [] for truth_particle in event['truth_particles']: if truth_particle['tpartpdgID'] == autils.PDGID['higgs']: continue v = TLorentzVector.from_ptetaphim(truth_particle['tpartpT'], truth_particle['tparteta'], truth_particle['tpartphi'], truth_particle['tpartm']) particle_list.append( (v, truth_particle['tpartpdgID'], truth_particle['tpartstatus']) ) recorded_jets = [] for rj in event['reco_jets']: v = TLorentzVector.from_ptetaphim(rj['j0pT'], rj['j0eta'], rj['j0phi'], rj['j0m']) pdgid = match_jet(v, particle_list) #is_pileup = rj['j0_isPU'] is_pileup = rj['tj0pT'] < 0 new_jet = acorn_jet(v, pdgid, is_pileup, rj['j0_JVT'], rj['j0_fJVT_Tight'], rj['j0_QGTagger'], None, []) recorded_jets.append(new_jet) # Categorize event, and then either discard the event or perform tagging on it for category in event_data_dump.values(): category.add_event(recorded_jets, is_signal, event['eventWeight'])
def record_cmilke_reco_jets(is_signal, input_list, events_to_read, event_data_dump): tree_name = 'ntuple' branches = [ 'EventWeight', ('reco_jets', [ 'JetPt_calib', 'JetEta_calib', 'JetPhi_calib', 'JetM_calib', 'JetFlavor', 'JetScatterType', 'JetIsTightPhoton', 'JetJVT', 'JetfJVT_tight', 'JetPullMagnitude', 'JetPullAngle' #('reco_tracks', ['JetTrkPt', 'JetTrkEta', 'JetTrkPhi', 'JetTrkM']) ]) ] for event in event_iterator(input_list, tree_name, branches, events_to_read): # Loop over reco jets and convert them into generic acorn_jet objects recorded_jets = [] for reco_jet in event['reco_jets']: pdgid = reco_jet['JetFlavor'] is_pileup = reco_jet['JetScatterType'] != 2 # Create jet object storing the essential aspects of the ntuple reco jet, new_jet = acorn_jet(pdgid, is_pileup, reco_jet['JetJVT'], reco_jet['JetfJVT_tight'], -1, reco_jet['JetPt_calib'], reco_jet['JetEta_calib'], reco_jet['JetPhi_calib'], reco_jet['JetM_calib'], reco_jet['JetPullMagnitude'], reco_jet['JetPullAngle'] ) recorded_jets.append(new_jet) # Categorize event, and then either discard the event or perform tagging on it for category in event_data_dump.values(): category.add_event(recorded_jets, is_signal, event['EventWeight'])
def validate(): ntuple_type = sys.argv[1] input_type = 'sig' branch_list = _branch_options[ntuple_type] tree_name = _tree_options[ntuple_type] # Load data input_list = _input_type_options[ntuple_type][input_type] print('\n\nLoading '+ntuple_type+'...') _loaders[ntuple_type]( event_iterator(input_list, tree_name, branch_list, _Nevents) )
def record_events(input_type, categories): input_list = _input_type_options[input_type] for event in event_iterator(input_list, 'Nominal', _branch_list, 10000): for rj in event['j0']: is_pu = rj['tj0pT'] < 0 is_vbf = rj['j0truthid'] in autils.PDG['quarks'] passes_jvt = rj['j0_JVT'] and rj['j0_fJVT_Tight'] for key, value in categories[input_type].items(): if key[0] and rj['j0_isTightPhoton']: continue if key[1] and not is_pu: continue if key[2] and not is_vbf: continue if key[3] and not passes_jvt: continue value[2][0].append(rj['j0pT']) value[2][1].append(rj['j0eta'])
def count_all_jets(input_type): tcounter = [] rcounter = [] input_list = _input_type_options[input_type] truth_particles = [ tp.copy() for tp in event['tpart'] ] for event in event_iterator(input_list, 'Nominal', _branch_list, 10000): if event_fails_photon_cut(truth_particles): continue num_tjets, num_tquarks = count_tjets_with_truthj(event,truth_particles) num_rjets, num_rquarks = count_rjets(event,truth_particles) if num_rjets >= 2: #if num_rjets >= 2 and num_rquarks >= 2: tcounter.append(num_tjets) rcounter.append(num_rjets) return tcounter, rcounter
def validate(): ntuple_type = sys.argv[1] branch_list = _branch_options[ntuple_type] tree_name = _tree_options[ntuple_type] # Load data print('Loading...') branch_keys = unnest_list(branch_list, {}) #+ _extra_keys input_keys = ['sig', 'bgd'] #input_keys = ['sig'] validation_data = { b_key: {i_key: [] for i_key in input_keys} for b_key in branch_keys } for input_type in input_keys: input_list = _input_type_options[ntuple_type][input_type] event_generator = event_iterator(input_list, tree_name, branch_list, _Nevents) recursive_load(None, event_generator, validation_data, input_type) # Plot data print('Plotting...') output = PdfPages('plots_validation_' + ntuple_type + '.pdf') for plot_name, inputs in validation_data.items(): plot_range = None if plot_name not in _xlimits else _xlimits[plot_name] plot_values = {'x': [], 'label': []} for input_type, data in inputs.items(): plot_values['x'].append(data) plot_values['label'].append(input_type + ' - ' + str(len(data))) fig, ax = plt.subplots() counts, bins, hist = plt.hist(**plot_values, histtype='step', linewidth=2, bins=_hist_bins, range=plot_range) ax.legend() plt.grid() plt.title(ntuple_type + ': Distribution of ' + plot_name + ' Over ' + str(_Nevents) + ' Events') if plot_range != None: plt.xlim(*_xlimits[plot_name]) output.savefig() plt.close() output.close()
def test(): meaningless_number = 0 for event in event_iterator(input_list, 'Nominal', _branch_list, _Nevents): meaningless_number += event['eventWeight'] for truth_jet in event['truth_particles']: meaningless_number += truth_jet['tpartpdgID'] meaningless_number += truth_jet['tpartstatus'] meaningless_number += truth_jet['tpartpT'] meaningless_number += truth_jet['tparteta'] for reco_jet in event['reco_jets']: meaningless_number += reco_jet['j0truthid'] meaningless_number += reco_jet['j0_isTightPhoton'] meaningless_number += reco_jet['j0pT'] meaningless_number += reco_jet['j0eta'] print(meaningless_number)
def test(input_type): input_list = _input_type_options[input_type] raw_reco = [] for ntuple_file in input_list: print('\nnutple file: ' + ntuple_file) tree = uproot.rootio.open(ntuple_file)['Nominal'] tree_iterator = tree.iterate(branches=branch_list, entrysteps=10000) for basket_number, basket in enumerate(tree_iterator): for i in range(Nevents): tps = [] for j in range(len(basket[b'tpartpT'][i])): Tpt = basket[b'tpartpT'][i][j] Teta = basket[b'tparteta'][i][j] Tphi = basket[b'tpartphi'][i][j] Tm = basket[b'tpartm'][i][j] TpdgID = basket[b'tpartpdgID'][i][j] Tstatus = basket[b'tpartstatus'][i][j] tps.append((Tpt, Teta, Tphi, Tm, TpdgID, Tstatus)) rjs = [] for j in range(len(basket[b'j0pT'][i])): RJpt = basket[b'j0pT'][i][j] RJeta = basket[b'j0eta'][i][j] RJphi = basket[b'j0phi'][i][j] RJm = basket[b'j0m'][i][j] RJpdgID = basket[b'j0truthid'][i][j] rjs.append((RJpt, RJeta, RJphi, RJm, RJpdgID)) raw_reco.append(rjs) break break fancy_reco = [] for event in event_iterator(input_list, 'Nominal', _branch_list, Nevents): rjs = [(rj['j0pT'], rj['j0eta'], rj['j0phi'], rj['j0m'], rj['j0truthid']) for rj in event['reco_jets']] fancy_reco.append(rjs) for raw, fancy in zip(raw_reco, fancy_reco): print(raw) print(fancy) print()
def test(input_type): input_list = _input_type_options[input_type] final = [] for ntuple_file in input_list: print('\nnutple file: ' + ntuple_file) tree = uproot.rootio.open(ntuple_file)['Nominal'] tree_iterator = tree.iterate(branches=branch_list, entrysteps=10000) for basket_number, basket in enumerate(tree_iterator): for i in range(Nevents): tps = [] for j in range( len(basket[b'tpartpT'][i]) ): Tpt = basket[b'tpartpT'][i][j] Teta = basket[b'tparteta'][i][j] Tphi = basket[b'tpartphi'][i][j] Tm = basket[b'tpartm'][i][j] TpdgID = basket[b'tpartpdgID'][i][j] Tstatus = basket[b'tpartstatus'][i][j] tps.append( (Tpt, Teta, Tphi, Tm, TpdgID, Tstatus) ) vs = [] tmp = [] for j in range( len(basket[b'j0pT'][i]) ): if not ( basket[b'j0pT'][i][j] > 30 and abs(basket[b'j0eta'][i][j]) < 4 ): continue if not ( basket[b'j0_JVT'][i][j] and basket[b'j0_fJVT_Tight'][i][j]): continue v = TLorentzVector.from_ptetaphim(basket[b'j0pT'][i][j], basket[b'j0eta'][i][j], basket[b'j0phi'][i][j], basket[b'j0m'][i][j]) ID = get_pdgID_raw(v,tps) tmp.append(ID) #ID = rj['j0truthid'] if ID != 22: #if not basket[b'j0_isTightPhoton'][i][j]: vs.append( ID in range(1,7) ) if len(vs) == 3 and vs.count(1) == 2: final.append( vs[0] and vs[1] ) break break print() print( final.count(1) / len(final) ) final = [] for event in event_iterator(input_list, 'Nominal', _branch_list, Nevents): #tp = list(event['truth_particles']) tps = [ tp.copy() for tp in event['truth_particles'] ] #for tp in event['truth_particles']: pass vs = [] tmp = [] for rj in event['reco_jets']: if not (rj['j0pT'] > 30 and abs(rj['j0eta']) < 4 ): continue if not (rj['j0_JVT'] and rj['j0_fJVT_Tight']): continue v = TLorentzVector.from_ptetaphim(rj['j0pT'], rj['j0eta'], rj['j0phi'], rj['j0m']) ID = get_pdgID(v,tps) #print() tmp.append(ID) if ID != 22: #if not rj['j0_isTightPhoton']: vs.append( ID in range(1,7) ) if len(vs) == 3 and vs.count(1) == 2: final.append( vs[0] and vs[1] ) print( final.count(1) / len(final) )
def draw(input_type): correct_pt = 0 correct_mjj = 0 contains_leading_quark_pt = 0 contains_leading_quark_mjj = 0 tagged_pt = 0 tagged_mjj = 0 events_with_3_jets = 0 pt_based_jets = 0 mjj_based_jets = 0 ntuple_type = 'aviv' #sys.argv[1] branch_list = _branch_options[ntuple_type] tree_name = _tree_options[ntuple_type] input_list = _input_type_options[ntuple_type][input_type] for event in event_iterator(input_list, tree_name, branch_list, _Nevents): particle_list = [] for truth_particle in event['truth_particles']: if truth_particle['tpartpdgID'] == autils.PDGID['higgs']: continue v = TLorentzVector.from_ptetaphim(truth_particle['tpartpT'], truth_particle['tparteta'], truth_particle['tpartphi'], truth_particle['tpartm']) particle_list.append((v, truth_particle['tpartpdgID'], truth_particle['tpartstatus'])) #if truth_particle['tpartpdgID'] == 22 and truth_particle['tpartstatus'] not in (1,23): print(truth_particle['tpartstatus']) jet_list = [] #for jet in event['truth_jets']: num_dual_matched = 0 for jet in event['reco_jets']: if not (jet['j0_JVT'] and jet['j0_fJVT_Tight']): continue #v = TLorentzVector.from_ptetaphim(jet['truthjpT'], jet['truthjeta'], jet['truthjphi'], jet['truthjm']) v = TLorentzVector.from_ptetaphim(jet['j0pT'], jet['j0eta'], jet['j0phi'], jet['j0m']) if v.pt < 30 or abs(v.eta) > 4: continue pdgid, dual_matched = match_jet(v, particle_list) if pdgid == autils.PDGID['photon']: continue jet_list.append((v, pdgid in autils.PDGID['quarks'])) if not passes_cuts(input_type, jet_list): continue quark_jets = [jet for jet in jet_list if jet[1]] #print(jet_list) events_with_3_jets += 1 # Leading Pt pt_chosen_jets = jet_list[:2] if input_type == 'sig': correct_jets, has_leading_pt = check_if_signature_jets( pt_chosen_jets, quark_jets[0]) correct_pt += int(correct_jets) contains_leading_quark_pt += int(has_leading_pt) if (pt_chosen_jets[0][0] + pt_chosen_jets[1][0]).mass > 500: pt_based_jets += 1 #tagged_pt += int( (pt_chosen_jets[0][0] + pt_chosen_jets[1][0]).mass > 252 ) tagged_pt += int( (pt_chosen_jets[0][0] + pt_chosen_jets[1][0]).mass > 700) # Maximized Mjj mass_pairs = sorted([((i[0] + j[0]).mass, [i, j]) for i, j in combinations(jet_list, 2)], reverse=True, key=lambda t: t[0]) mjj_chosen_jets = mass_pairs[0][1] if input_type == 'sig': correct_jets, has_leading_pt = check_if_signature_jets( mjj_chosen_jets, quark_jets[0]) correct_mjj += int(correct_jets) contains_leading_quark_mjj += int(has_leading_pt) if (mjj_chosen_jets[0][0] + mjj_chosen_jets[1][0]).mass > 500: mjj_based_jets += 1 #tagged_mjj += int( (mjj_chosen_jets[0][0] + mjj_chosen_jets[1][0]).mass > 310 ) tagged_mjj += int( (mjj_chosen_jets[0][0] + mjj_chosen_jets[1][0]).mass > 700) num_jets = events_with_3_jets print() print(num_jets) if input_type == 'sig': print('{}, {}, {:.02}, {:.02} | {:.02}, {:.02}'.format( correct_pt, correct_mjj, correct_pt / num_jets, correct_mjj / num_jets, contains_leading_quark_pt / num_jets, contains_leading_quark_mjj / num_jets)) print('{}, {} | {}, {} | {:.02}, {:.02}'.format( tagged_pt, pt_based_jets, tagged_mjj, mjj_based_jets, tagged_pt / pt_based_jets, tagged_mjj / mjj_based_jets))