def create_new_trees(input_file, suffix = ''): tree1_name = 'TTbar_plus_X_analysis/MuPlusJets/QCD non iso mu+jets/FitVariables' + suffix tree2_name = 'TTbar_plus_X_analysis/MuPlusJets/QCD non iso mu+jets/Muon/Muons' + suffix s_cr1 = 'relIso_04_deltaBeta <= 0.3 && relIso_04_deltaBeta > 0.12' s_cr2 = 'relIso_04_deltaBeta > 0.3' cr1 = 'TTbar_plus_X_analysis/MuPlusJets/QCD 0.12 < iso <= 0.3' cr2 = 'TTbar_plus_X_analysis/MuPlusJets/QCD iso > 0.3' with root_open(input_file) as file: t1 = file.Get(tree1_name) t2 = file.Get(tree2_name) t1.AddFriend(t2) # h1 = t1.Draw('MET', 'relIso_04_deltaBeta > 0.3') # h2 = t1.Draw( # 'MET', 'relIso_04_deltaBeta <= 0.3 && relIso_04_deltaBeta > 0.12') # h3 = t1.Draw('MET', 'relIso_04_deltaBeta > 0.12') # h4 = t2.Draw('relIso_04_deltaBeta', 'relIso_04_deltaBeta > 0.12') # print h1.integral() # print h2.integral() # print h3.integral(), h1.integral() + h2.integral() output = File('test.root', 'recreate') output.mkdir(cr1, recurse=True) output.mkdir(cr2, recurse=True) output.cd(cr2) new_tree1 = t1.CopyTree(s_cr2) new_tree1.Write() output.cd(cr1) new_tree2 = t1.CopyTree(s_cr1) new_tree2.Write() output.close() new_tree1 = None new_tree2 = None f_out = File(input_file, 'update') root_mkdir(f_out, cr1) root_mkdir(f_out, cr2) with root_open('test.root') as f_in: f_out.cd(cr1) new_tree1 = f_in.Get(cr1 + '/FitVariables' + suffix).CloneTree() f_out.cd(cr2) new_tree2 = f_in.Get(cr2 + '/FitVariables' + suffix).CloneTree()
def create_new_trees(input_file, suffix=''): tree1_name = 'TTbar_plus_X_analysis/MuPlusJets/QCD non iso mu+jets/FitVariables' + suffix tree2_name = 'TTbar_plus_X_analysis/MuPlusJets/QCD non iso mu+jets/Muon/Muons' + suffix s_cr1 = 'relIso_04_deltaBeta <= 0.3 && relIso_04_deltaBeta > 0.12' s_cr2 = 'relIso_04_deltaBeta > 0.3' cr1 = 'TTbar_plus_X_analysis/MuPlusJets/QCD 0.12 < iso <= 0.3' cr2 = 'TTbar_plus_X_analysis/MuPlusJets/QCD iso > 0.3' with root_open(input_file) as file: t1 = file.Get(tree1_name) t2 = file.Get(tree2_name) t1.AddFriend(t2) # h1 = t1.Draw('MET', 'relIso_04_deltaBeta > 0.3') # h2 = t1.Draw( # 'MET', 'relIso_04_deltaBeta <= 0.3 && relIso_04_deltaBeta > 0.12') # h3 = t1.Draw('MET', 'relIso_04_deltaBeta > 0.12') # h4 = t2.Draw('relIso_04_deltaBeta', 'relIso_04_deltaBeta > 0.12') # print h1.integral() # print h2.integral() # print h3.integral(), h1.integral() + h2.integral() output = File('test.root', 'recreate') output.mkdir(cr1, recurse=True) output.mkdir(cr2, recurse=True) output.cd(cr2) new_tree1 = t1.CopyTree(s_cr2) new_tree1.Write() output.cd(cr1) new_tree2 = t1.CopyTree(s_cr1) new_tree2.Write() output.close() new_tree1 = None new_tree2 = None f_out = File(input_file, 'update') root_mkdir(f_out, cr1) root_mkdir(f_out, cr2) with root_open('test.root') as f_in: f_out.cd(cr1) new_tree1 = f_in.Get(cr1 + '/FitVariables' + suffix).CloneTree() f_out.cd(cr2) new_tree2 = f_in.Get(cr2 + '/FitVariables' + suffix).CloneTree()
def create_toy_mc(input_files, sample, output_folder, n_toy, centre_of_mass, config): from dps.utils.file_utilities import make_folder_if_not_exists from dps.utils.toy_mc import generate_toy_MC_from_distribution, generate_toy_MC_from_2Ddistribution from dps.utils.Unfolding import get_unfold_histogram_tuple make_folder_if_not_exists(output_folder) output_file_name = get_output_file_name(output_folder, sample, n_toy, centre_of_mass) variable_bins = bin_edges_vis.copy() with root_open(output_file_name, 'recreate') as f_out: input_file_index = 0 for input_file in input_files: input_file_hists = File(input_file) for channel in config.analysis_types.keys(): if channel is 'combined':continue for variable in variable_bins: output_dir = f_out.mkdir(str(input_file_index) + '/' + channel + '/' + variable, recurse=True) cd = output_dir.cd mkdir = output_dir.mkdir h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple(input_file_hists, variable, channel, centre_of_mass = centre_of_mass, visiblePS = True, load_fakes=False) cd() mkdir('Original') cd ('Original') h_truth.Write('truth') h_measured.Write('measured') h_response.Write('response') for i in range(1, n_toy+1): toy_id = 'toy_{0}'.format(i) mkdir(toy_id) cd(toy_id) # create histograms # add tuples (truth, measured, response) of histograms truth = generate_toy_MC_from_distribution(h_truth) measured = generate_toy_MC_from_distribution(h_measured) response = generate_toy_MC_from_2Ddistribution(h_response) truth.SetName('truth') measured.SetName('measured') response.SetName('response') truth.Write() measured.Write() response.Write() input_file_index += 1
def create_toy_mc(input_file, output_folder, n_toy, centre_of_mass, ttbar_xsection, met_type, start_at=1): from tools.file_utilities import make_folder_if_not_exists from tools.toy_mc import generate_toy_MC_from_distribution, generate_toy_MC_from_2Ddistribution from tools.Unfolding import get_unfold_histogram_tuple make_folder_if_not_exists(output_folder) input_file_hists = File(input_file) output_file_name = get_output_file_name( output_folder, start_at, n_toy, centre_of_mass) variable_bins = bin_edges.copy() with root_open(output_file_name, 'recreate') as f_out: for channel in ['electron', 'muon']: for variable in variable_bins: output_dir = f_out.mkdir(channel + '/' + variable, recurse=True) cd = output_dir.cd mkdir = output_dir.mkdir h_truth, h_measured, h_response, _ = get_unfold_histogram_tuple(input_file_hists, variable, channel, met_type, centre_of_mass, ttbar_xsection, load_fakes=False) cd() for i in range(start_at, start_at + n_toy): toy_id = 'toy_{0}'.format(i) mkdir(toy_id) cd(toy_id) # create histograms # add tuples (truth, measured, response) of histograms truth = generate_toy_MC_from_distribution(h_truth) measured = generate_toy_MC_from_distribution(h_measured) response = generate_toy_MC_from_2Ddistribution(h_response) truth.SetName('truth') measured.SetName('measured') response.SetName('response') truth.Write() measured.Write() response.Write()
def generate_toy(n_toy, n_input_mc, config, output_folder, start_at=0, split=1): from progressbar import Percentage, Bar, ProgressBar, ETA set_root_defaults() genWeight = '( EventWeight * {0})'.format(config.luminosity_scale) file_name = config.ttbar_category_templates_trees['central'] make_folder_if_not_exists(output_folder) outfile = get_output_file_name(output_folder, n_toy, start_at, n_input_mc, config.centre_of_mass_energy) variable_bins = bin_edges.copy() widgets = ['Progress: ', Percentage(), ' ', Bar(), ' ', ETA()] with root_open(file_name, 'read') as f_in, root_open(outfile, 'recreate') as f_out: tree = f_in.Get("TTbar_plus_X_analysis/Unfolding/Unfolding") n_events = tree.GetEntries() print("Number of entries in tree : ", n_events) for channel in ['electron', 'muon']: print('Channel :', channel) gen_selection, gen_selection_vis = '', '' if channel is 'muon': gen_selection = '( isSemiLeptonicMuon == 1 )' gen_selection_vis = '( isSemiLeptonicMuon == 1 && passesGenEventSelection )' else: gen_selection = '( isSemiLeptonicElectron == 1 )' gen_selection_vis = '( isSemiLeptonicElectron == 1 && passesGenEventSelection )' selection = '( {0} ) * ( {1} )'.format(genWeight, gen_selection) selection_vis = '( {0} ) * ( {1} )'.format(genWeight, gen_selection_vis) weighted_entries = get_weighted_entries(tree, selection) weighted_entries_vis = get_weighted_entries(tree, selection_vis) pbar = ProgressBar(widgets=widgets, maxval=n_input_mc).start() toy_mc_sets = [] for variable in ['MET', 'HT', 'ST', 'WPT']: # variable_bins: toy_mc = ToySet(f_out, variable, channel, n_toy) toy_mc_sets.append(toy_mc) count = 0 for event in tree: # generate 300 weights for each event mc_weights = get_mc_weight(weighted_entries, n_toy) mc_weights_vis = get_mc_weight(weighted_entries_vis, n_toy) if count >= n_input_mc: break count += 1 if count < start_at: continue # weight = event.EventWeight * config.luminosity_scale # # rescale to N input events # weight *= n_events / n_input_mc / split weight = 1 for toy_mc in toy_mc_sets: toy_mc.fill(event, weight, mc_weights, mc_weights_vis) if count % 1000 == 1: pbar.update(count) print('Processed {0} events'.format(count)) pbar.finish() for toy_mc in toy_mc_sets: toy_mc.write() print('Toy MC was saved to file:', outfile)
''' Created on 21 Aug 2015 @author: phxlk ''' from rootpy.plotting.hist import Hist from rootpy.io.file import root_open from rootpy.interactive.rootwait import wait if __name__ == '__main__': input_file = '/hdfs/TopQuarkGroup/run2/atOutput/13TeV/50ns/data_electron_tree.root' tree_path = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/FitVariables' h = Hist(100, 0, 2) with root_open(input_file) as f: tree = f.Get(tree_path) tree.Draw('PUWeight', 'PUWeight > 0', hist=h) h.Draw() wait()
def generate_toy(n_toy, n_input_mc, config, output_folder, start_at=0, split=1): from progressbar import Percentage, Bar, ProgressBar, ETA set_root_defaults() genWeight = '( EventWeight * {0})'.format(config.luminosity_scale) file_name = config.ttbar_category_templates_trees['central'] make_folder_if_not_exists(output_folder) outfile = get_output_file_name( output_folder, n_toy, start_at, n_input_mc, config.centre_of_mass_energy) variable_bins = bin_edges.copy() widgets = ['Progress: ', Percentage(), ' ', Bar(), ' ', ETA()] with root_open(file_name, 'read') as f_in, root_open(outfile, 'recreate') as f_out: tree = f_in.Get("TTbar_plus_X_analysis/Unfolding/Unfolding") n_events = tree.GetEntries() print("Number of entries in tree : ", n_events) for channel in ['electron', 'muon']: print('Channel :', channel) gen_selection, gen_selection_vis = '', '' if channel is 'muon': gen_selection = '( isSemiLeptonicMuon == 1 )' gen_selection_vis = '( isSemiLeptonicMuon == 1 && passesGenEventSelection )' else: gen_selection = '( isSemiLeptonicElectron == 1 )' gen_selection_vis = '( isSemiLeptonicElectron == 1 && passesGenEventSelection )' selection = '( {0} ) * ( {1} )'.format(genWeight, gen_selection) selection_vis = '( {0} ) * ( {1} )'.format(genWeight, gen_selection_vis) weighted_entries = get_weighted_entries(tree, selection) weighted_entries_vis = get_weighted_entries(tree, selection_vis) pbar = ProgressBar(widgets=widgets, maxval=n_input_mc).start() toy_mc_sets = [] for variable in ['MET', 'HT', 'ST', 'WPT']: # variable_bins: toy_mc = ToySet(f_out, variable, channel, n_toy) toy_mc_sets.append(toy_mc) count = 0 for event in tree: # generate 300 weights for each event mc_weights = get_mc_weight(weighted_entries, n_toy) mc_weights_vis = get_mc_weight(weighted_entries_vis, n_toy) if count >= n_input_mc: break count += 1 if count < start_at: continue # weight = event.EventWeight * config.luminosity_scale # # rescale to N input events # weight *= n_events / n_input_mc / split weight = 1 for toy_mc in toy_mc_sets: toy_mc.fill(event, weight, mc_weights, mc_weights_vis) if count % 1000 == 1: pbar.update(count) print('Processed {0} events'.format(count)) pbar.finish() for toy_mc in toy_mc_sets: toy_mc.write() print('Toy MC was saved to file:', outfile)
''' Created on 21 Aug 2015 @author: phxlk ''' from rootpy.plotting.hist import Hist from rootpy.io.file import root_open from rootpy.interactive.rootwait import wait if __name__ == '__main__': input_file = '/hdfs/TopQuarkGroup/run2/atOutput/13TeV/50ns/data_electron_tree.root' tree_path = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/FitVariables' h = Hist(100, 0, 2) with root_open(input_file) as f: tree = f.Get(tree_path) tree.Draw('PUWeight', 'PUWeight > 0', hist = h) h.Draw() wait()