def unfold_results( results, category, channel, k_value, h_truth, h_measured, h_response, h_fakes, method ): global variable, path_to_JSON, options h_data = value_error_tuplelist_to_hist( results, bin_edges[variable] ) unfolding = Unfolding( h_truth, h_measured, h_response, h_fakes, method = method, k_value = k_value ) # turning off the unfolding errors for systematic samples if not category == 'central': unfoldCfg.Hreco = 0 else: unfoldCfg.Hreco = options.Hreco h_unfolded_data = unfolding.unfold( h_data ) if options.write_unfolding_objects: # export the D and SV distributions SVD_path = path_to_JSON + '/unfolding_objects/' + channel + '/kv_' + str( k_value ) + '/' make_folder_if_not_exists( SVD_path ) if method == 'TSVDUnfold': SVDdist = File( SVD_path + method + '_SVDdistributions_' + category + '.root', 'recreate' ) directory = SVDdist.mkdir( 'SVDdist' ) directory.cd() unfolding.unfoldObject.GetD().Write() unfolding.unfoldObject.GetSV().Write() # unfolding.unfoldObject.GetUnfoldCovMatrix(data_covariance_matrix(h_data), unfoldCfg.SVD_n_toy).Write() SVDdist.Close() else: SVDdist = File( SVD_path + method + '_SVDdistributions_Hreco' + str( unfoldCfg.Hreco ) + '_' + category + '.root', 'recreate' ) directory = SVDdist.mkdir( 'SVDdist' ) directory.cd() unfolding.unfoldObject.Impl().GetD().Write() unfolding.unfoldObject.Impl().GetSV().Write() h_truth.Write() h_measured.Write() h_response.Write() # unfolding.unfoldObject.Impl().GetUnfoldCovMatrix(data_covariance_matrix(h_data), unfoldCfg.SVD_n_toy).Write() SVDdist.Close() # export the whole unfolding object if it doesn't exist if method == 'TSVDUnfold': unfolding_object_file_name = SVD_path + method + '_unfoldingObject_' + category + '.root' else: unfolding_object_file_name = SVD_path + method + '_unfoldingObject_Hreco' + str( unfoldCfg.Hreco ) + '_' + category + '.root' if not os.path.isfile( unfolding_object_file_name ): unfoldingObjectFile = File( unfolding_object_file_name, 'recreate' ) directory = unfoldingObjectFile.mkdir( 'unfoldingObject' ) directory.cd() if method == 'TSVDUnfold': unfolding.unfoldObject.Write() else: unfolding.unfoldObject.Impl().Write() unfoldingObjectFile.Close() del unfolding return hist_to_value_error_tuplelist( h_unfolded_data )
def convert_unfolding_histograms(file_name, histograms_to_load=[ 'truth', 'fake', 'measured', 'response', 'response_withoutFakes', 'response_without_fakes', 'EventCounter', ]): file_start = Timer() print 'Converting', file_name histograms = {} with File(file_name) as f: for path, _, objects in f.walk(): # keep only unfolding and EventFilter if path.startswith('unfolding_') or path == 'EventFilter': histograms[path] = {} for hist_name in objects: if hist_name in histograms_to_load: hist = f.Get(path + '/' + hist_name).Clone() hist.SetDirectory(0) histograms[path][hist_name] = hist new_histograms = {} # rebin for path, hists in histograms.iteritems(): new_histograms[path] = {} variable = '' if not path == 'EventFilter': variable = path.split('_')[1] for name, hist in hists.iteritems(): if name == 'EventCounter': new_histograms[path][name] = hist.Clone() else: new_hist = hist.rebinned(bin_edges_vis[variable]) if 'TH2' in new_hist.class_name(): new_hist = new_hist.rebinned(bin_edges_vis[variable], axis=1) new_histograms[path][name] = new_hist # save_to_file output = File(file_name.replace('.root', '_asymmetric.root'), 'recreate') for path, hists in new_histograms.iteritems(): directory = output.mkdir(path) directory.cd() for name, hist in hists.iteritems(): if name == 'response_withoutFakes': # fix this name hist.Write('response_without_fakes') else: hist.Write(name) output.close() secs = file_start.elapsed_time() print 'File %s converted in %d seconds' % (file_name, secs)
def convert_unfolding_histograms(file_name, histograms_to_load=['truth', 'fake', 'measured', 'response', 'response_withoutFakes', 'response_without_fakes', 'EventCounter', ]): file_start = Timer() print 'Converting', file_name histograms = {} with File(file_name) as f: for path, _, objects in f.walk(): # keep only unfolding and EventFilter if path.startswith('unfolding_') or path == 'EventFilter': histograms[path] = {} for hist_name in objects: if hist_name in histograms_to_load: hist = f.Get(path + '/' + hist_name).Clone() hist.SetDirectory(0) histograms[path][hist_name] = hist new_histograms = {} # rebin for path, hists in histograms.iteritems(): new_histograms[path] = {} variable = '' if not path == 'EventFilter': variable = path.split('_')[1] for name, hist in hists.iteritems(): if name == 'EventCounter': new_histograms[path][name] = hist.Clone() else: new_hist = hist.rebinned(bin_edges_vis[variable]) if 'TH2' in new_hist.class_name(): new_hist = new_hist.rebinned(bin_edges_vis[variable], axis=1) new_histograms[path][name] = new_hist # save_to_file output = File(file_name.replace('.root', '_asymmetric.root'), 'recreate') for path, hists in new_histograms.iteritems(): directory = output.mkdir(path) directory.cd() for name, hist in hists.iteritems(): if name == 'response_withoutFakes': # fix this name hist.Write('response_without_fakes') else: hist.Write(name) output.close() secs = file_start.elapsed_time() print 'File %s converted in %d seconds' % (file_name, secs)
def main(options, args): config = XSectionConfig(options.CoM) variables = ['MET', 'HT', 'ST', 'WPT'] channels = ['electron', 'muon', 'combined'] m_file = 'normalised_xsection_patType1CorrectedPFMet.txt' m_with_errors_file = 'normalised_xsection_patType1CorrectedPFMet_with_errors.txt' path_template = args[0] output_file = 'measurement_{0}TeV.root'.format(options.CoM) f = File(output_file, 'recreate') for channel in channels: d = f.mkdir(channel) d.cd() for variable in variables: dv = d.mkdir(variable) dv.cd() if channel == 'combined': path = path_template.format(variable=variable, channel=channel, centre_of_mass_energy=options.CoM) else: kv = channel + \ '/kv{0}/'.format(config.k_values[channel][variable]) path = path_template.format(variable=variable, channel=kv, centre_of_mass_energy=options.CoM) m = read_data_from_JSON(path + '/' + m_file) m_with_errors = read_data_from_JSON( path + '/' + m_with_errors_file) for name, result in m.items(): h = make_histogram(result, bin_edges_full[variable]) h.SetName(name) h.write() for name, result in m_with_errors.items(): if not 'TTJet' in name: continue h = make_histogram(result, bin_edges_full[variable]) h.SetName(name + '_with_syst') h.write() dv.write() d.cd() d.write() f.write() f.close()
def main(options, args): config = XSectionConfig(options.CoM) variables = ['MET', 'HT', 'ST', 'WPT'] channels = ['electron', 'muon', 'combined'] m_file = 'normalised_xsection_patType1CorrectedPFMet.txt' m_with_errors_file = 'normalised_xsection_patType1CorrectedPFMet_with_errors.txt' path_template = args[0] output_file = 'measurement_{0}TeV.root'.format(options.CoM) f = File(output_file, 'recreate') for channel in channels: d = f.mkdir(channel) d.cd() for variable in variables: dv = d.mkdir(variable) dv.cd() if channel == 'combined': path = path_template.format(variable=variable, channel=channel, centre_of_mass_energy=options.CoM) else: kv = channel + \ '/kv{0}/'.format(config.k_values[channel][variable]) path = path_template.format(variable=variable, channel=kv, centre_of_mass_energy=options.CoM) m = read_data_from_JSON(path + '/' + m_file) m_with_errors = read_data_from_JSON(path + '/' + m_with_errors_file) for name, result in m.items(): h = make_histogram(result, bin_edges[variable]) h.SetName(name) h.write() for name, result in m_with_errors.items(): if not 'TTJet' in name: continue h = make_histogram(result, bin_edges[variable]) h.SetName(name + '_with_syst') h.write() dv.write() d.cd() d.write() f.write() f.close()
dest="n_toy_mc", default=100, help="number of toy MC to create") parser.add_option("-i", "--input", dest="input_file", default='/storage/TopQuarkGroup/unfolding/unfolding_merged_sub1.root', help="input file for templates") parser.add_option("-o", "--output", dest="output_file", default='../data/unfolding_toy_mc.root', help="output file for toy MC") (options, args) = parser.parse_args() # define output file output = File(options.output_file, 'recreate') for channel in ['electron', 'muon']: # get histograms h_truth, h_measured, h_fakes, h_response_AsymBins, h_reco_truth, h_truth_selected = read_and_scale_histograms(channel) directory = output.mkdir(channel) directory.cd() mkdir = directory.mkdir cd = directory.cd # generate toy MC for i in range(1, options.n_toy_mc + 1): mkdir('toy_%d' % i) cd('toy_%d' % i) # should the numbering be transferred to the histograms? if i % 100 == 0: print 'Done %d toy MC' % i new_histograms = get_new_set_of_histograms(h_truth, h_measured, h_fakes, h_response_AsymBins, h_reco_truth, h_truth_selected) for hist in new_histograms: hist.Write() output.Write() output.Close()
for lumiRange in runLumis[run]: for lumi in range(lumiRange[0], lumiRange[1] + 1): fullRunLumi.append([int(run), lumi]) return fullRunLumi inputFile = File( '/hdfs/TopQuarkGroup/run2/ntuples/v22/SingleMuonFullDCS/SingleMuon.root') treeName = 'nTupleTree/tree' tree = inputFile.Get(treeName) jsonForFiltering = '/users/ec6821/lumiScripts/lcr2/lcr2/good_list.txt' json = readJson(jsonForFiltering) newFile = File('SingleMuonFiltered.root', 'RECREATE') newFile.mkdir('nTupleTree') newFile.cd('nTupleTree') newTree = tree.CloneTree(0) print 'Number of events in tree : ', tree.GetEntries() for event in tree: run = event.__getattr__('Event.Run') lumi = event.__getattr__('Event.LumiSection') if [run, lumi] in json: newTree.Fill() else: pass newFile.Close()
''' Created on 11 Mar 2015 @author: kreczko ''' from rootpy.io import File from rootpy.plotting import Hist from rootpy import asrootpy rootpy_hist = Hist(100, 0, 100, type='F') rootpy_hist.SetName('hist') test_file = File('test.root', 'RECREATE') test_file.mkdir("test") test_file.cd('test') rootpy_hist.Write() test_file.Write() test_file.Close() read_file = File('test.root') folder = read_file.Get('test') hist = folder.hist print hist.TYPE read_file.Close() hist = None read_file = File('test.root') hist = read_file.Get('test/hist') hist1 = hist.empty_clone(type='D')
def main(): set_root_defaults() # prevent directory ownership of ROOT histograms (python does the garbage collection) TH1F.AddDirectory( False ) parser = OptionParser() parser.add_option( "-n", "--n_toy_mc", dest = "n_toy_mc", default = 300, help = "number of toy MC to create", type = int ) parser.add_option( "-o", "--output", dest = "output_folder", default = 'data/toy_mc/', help = "output folder for toy MC" ) parser.add_option( "-v", "--variable", dest = "variable", default = 'MET', help = "set the variable to analyse (MET, HT, ST, MT, WPT)" ) parser.add_option( "-m", "--metType", dest = "metType", default = 'type1', help = "set MET type for analysis of MET, ST or MT" ) parser.add_option( "-c", "--centre-of-mass-energy", dest = "CoM", default = 8, help = "set the centre of mass energy for analysis. Default = 8 [TeV]", type = int ) parser.add_option( '-V', '--verbose', dest = "verbose", action = "store_true", help = "Print the event number, reco and gen variable value" ) ( options, _ ) = parser.parse_args() measurement_config = XSectionConfig( options.CoM ) centre_of_mass = options.CoM ttbar_xsection = measurement_config.ttbar_xsection variable = options.variable met_type = measurement_config.translate_options[options.metType] n_toy_mc = options.n_toy_mc make_folder_if_not_exists( options.output_folder ) # get histograms input_file_hists = File( measurement_config.unfolding_madgraph ) # define output file out_file_template = '%s/toy_mc_%s_N_%d_%dTeV.root' out_file_name = out_file_template % (options.output_folder, variable, n_toy_mc, centre_of_mass) output = File( out_file_name, 'recreate' ) for channel in ['electron', 'muon']: # first get the weights 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 ) # create directories directory = output.mkdir( channel ) mkdir = directory.mkdir cd = directory.cd cd() # generate toy MC for i in range( 1, n_toy_mc + 1 ): mkdir( 'toy_%d' % i ) cd( 'toy_%d' % i ) # 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() output.Write() output.Close()
''' Created on 11 Mar 2015 @author: kreczko ''' from rootpy.io import File from rootpy.plotting import Hist from rootpy import asrootpy rootpy_hist = Hist( 100, 0, 100, type = 'F' ) rootpy_hist.SetName('hist') test_file = File('test.root', 'RECREATE') test_file.mkdir("test") test_file.cd('test') rootpy_hist.Write() test_file.Write() test_file.Close() read_file = File('test.root') folder = read_file.Get('test') hist = folder.hist print hist.TYPE read_file.Close() hist = None read_file = File('test.root') hist = read_file.Get('test/hist')