def main(): set_root_defaults() options, _ = parse_arguments() variable = 'ST' config_7TeV = XSectionConfig(7) config_8TeV = XSectionConfig(8) path_to_JSON_7TeV = options.path + '/7TeV/' + variable + '/' path_to_JSON_8TeV = options.path + '/8TeV/' + variable + '/' # we need the generators # and the central samples + errors results_7TeV, _ = read_xsection_measurement_results( path_to_JSON_7TeV, variable, bin_edges_full, category='central', channel='combined', k_values={'combined': config_7TeV.k_values_combined}) results_8TeV, _ = read_xsection_measurement_results( path_to_JSON_8TeV, variable, bin_edges_full, category='central', channel='combined', k_values={'combined': config_8TeV.k_values_combined}) plot_results(results_7TeV, results_8TeV, variable)
def read_tuple_from_file(filename): ''' Reading the output of 01 to a dataframe Reads a pandas output file of the form: A | A_Unc | B | B_Unc (v) | (e) | (v) | (e) Returns a pandas dataframe of the form: A | B (v,e) | (v,e) ''' from dps.config.xsection import XSectionConfig config = XSectionConfig(13) # First read the dataframe df = file_to_df(filename) l = df.columns.tolist() # Now to retupleise the columns for sample in l: if '_Unc' in sample: continue vals = df[sample] errs = df[sample + '_Unc'] df[sample] = tupleise_cols(vals, errs) del df[sample + '_Unc'] return df
def main(): parser = OptionParser(__doc__) parser.add_option( "-c", "--centre-of-mass-energy", dest="CoM", default=13, type=int, help="set the centre of mass energy for analysis. Default = 13 [TeV]") parser.add_option('-d', '--debug', dest="debug", action="store_true", help="Print the debug information") (options, _) = parser.parse_args() centre_of_mass_energy = options.CoM # set global variables debug = options.debug if debug: log.setLevel(log.DEBUG) measurement_config = XSectionConfig(centre_of_mass_energy) categories = ['QCD_shape'] categories.extend(measurement_config.categories_and_prefixes.keys()) categories.extend(measurement_config.rate_changing_systematics_names) categories.extend([ measurement_config.vjets_theory_systematic_prefix + scale for scale in ['scaleup', 'scaledown'] ]) for variable in measurement_config.variables: for category in categories: for channel in ['electron', 'muon']: if channel == 'electron' and (category == 'Muon_down' or category == 'Muon_up'): continue elif channel == 'muon' and (category == 'Electron_down' or category == 'Electron_up'): continue # create_measurement( # centre_of_mass_energy, category, variable, channel, # phase_space='FullPS', norm_method='background_subtraction') # and the visible phase space create_measurement(centre_of_mass_energy, category, variable, channel, phase_space='VisiblePS', norm_method='background_subtraction')
def __init__(self, output_folder, n_toy, n_input_mc, centre_of_mass, start_at=0, split=1): Job.__init__(self) self.output_folder = output_folder self.n_toy = n_toy self.centre_of_mass = centre_of_mass self.start_at = start_at self.n_input_mc = n_input_mc self.config = XSectionConfig(centre_of_mass) self.part_of_split = split
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(): set_root_defaults() # prevent directory ownership of ROOT histograms (python does the garbage # collection) 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("-s", dest="sample", default='powhegPythia', help='set underlying sample for creating the toy MC. Possible options : madgraph, powhegPythia, amcatnlo. Default is madgraph') parser.add_option("-c", "--centre-of-mass-energy", dest="CoM", default=13, help="set the centre of mass energy for analysis. Default = 13 [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) # baseDir = '/storage/ec6821/DailyPythonScripts/new/DailyPythonScripts/unfolding/13TeV/' # input_files = [ # baseDir + 'unfolding_TTJets_13TeV_asymmetric_50pc_tp_55pc.root', # baseDir + 'unfolding_TTJets_13TeV_asymmetric_95pc_tp_100pc.root', # baseDir + 'unfolding_TTJets_13TeV_asymmetric_55pc_tp_60pc.root', # baseDir + 'unfolding_TTJets_13TeV_asymmetric_60pc_tp_65pc.root', # baseDir + 'unfolding_TTJets_13TeV_asymmetric_65pc_tp_70pc.root', # baseDir + 'unfolding_TTJets_13TeV_asymmetric_70pc_tp_75pc.root', # baseDir + 'unfolding_TTJets_13TeV_asymmetric_80pc_tp_85pc.root', # baseDir + 'unfolding_TTJets_13TeV_asymmetric_75pc_tp_80pc.root', # ] input_files = [ measurement_config.unfolding_central_secondHalf ] create_toy_mc(input_files=input_files, sample=options.sample, output_folder=options.output_folder, # variable=variable, n_toy=options.n_toy_mc, centre_of_mass=options.CoM, config=measurement_config )
def main(): config = XSectionConfig(13) # method = 'RooUnfoldSvd' method = 'RooUnfoldBayes' file_for_data = File(config.unfolding_powheg_herwig, 'read') file_for_unfolding = File(config.unfolding_madgraphMLM, 'read') for channel in ['electron', 'muon', 'combined']: for variable in config.variables: tau_value = get_tau_value(config, channel, variable) h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( inputfile=file_for_unfolding, variable=variable, channel=channel, met_type=config.met_type, centre_of_mass=config.centre_of_mass_energy, ttbar_xsection=config.ttbar_xsection, luminosity=config.luminosity, load_fakes=False, visiblePS=False, ) h_data_model, h_data, _, _ = get_unfold_histogram_tuple( inputfile=file_for_data, variable=variable, channel=channel, met_type=config.met_type, centre_of_mass=config.centre_of_mass_energy, ttbar_xsection=config.ttbar_xsection, luminosity=config.luminosity, load_fakes=False, visiblePS=False, ) unfolding = Unfolding(h_truth, h_measured, h_response, h_fakes, method=method, k_value=-1, tau=tau_value) unfolded_data = unfolding.unfold(h_data) plot_bias(h_truth, h_data_model, unfolded_data, variable, channel, config.centre_of_mass_energy, method)
def __init__( self, input_values ): self.centre_of_mass_energy = input_values['centre-of-mass energy'] self.measurement_config = XSectionConfig( self.centre_of_mass_energy ) self.channel = input_values['channel'] self.variable = input_values['variable'] self.ndf = len(reco_bin_edges_vis[self.variable])-1 self.phaseSpace = input_values['phaseSpace'] self.output_folder = input_values['output_folder'] self.output_format = input_values['output_format'] self.truth = input_values['truth'] self.gen_vs_reco = input_values['gen_vs_reco'] self.measured = input_values['measured'] self.data = input_values['data'] self.taus_to_test = [] self.outpath = 'tables/taufinding/' self.__set_unfolding_histograms__()
def __init__(self, input_values): self.centre_of_mass_energy = input_values['centre-of-mass energy'] self.measurement_config = XSectionConfig(self.centre_of_mass_energy) self.channel = input_values['channel'] self.variable = input_values['variable'] self.phaseSpace = input_values['phaseSpace'] self.output_folder = input_values['output_folder'] self.output_format = input_values['output_format'] self.truth = input_values['truth'] self.gen_vs_reco = input_values['gen_vs_reco'] self.measured = input_values['measured'] self.data = input_values['data'] # optional if 'n_tau_scan_points' in input_values: self.n_tau_scan_points = input_values['n_tau_scan_points'] if 'n_toy' in input_values: self.n_toy = input_values['n_toy'] self.__set_unfolding_histograms__()
def create_measurement(options, norm_method): ''' Create the config file ''' # Create dictionary to write to config file measurement = {} xsec_config = XSectionConfig(options['com']) # Generate basic normalisation config info measurement["com"] = options['com'] measurement["channel"] = options['channel'] measurement["variable"] = options['variable'] measurement["name"] = options['category'] measurement["data_driven_qcd"] = options['data_driven_qcd'] if 'QCD_signal_MC' in options['category']: measurement["data_driven_qcd"] = False # Add specific samples to config measurement["samples"] = get_samples(options, xsec_config) return measurement
def __init__(self, input_file, method, channels, n_toy_data, sample, centre_of_mass, response, variables, output_folder, do_best_tau, tau_values): ''' Constructor ''' Job.__init__(self) self.input_file = input_file self.method = method self.n_toy_data = n_toy_data self.sample = sample self.centre_of_mass = centre_of_mass self.response = response self.output_folder = output_folder self.all_channels = channels self.all_variables = variables self.do_best_tau = do_best_tau self.all_tau_values = tau_values self.cross_section_config = XSectionConfig(self.centre_of_mass) print (self.all_tau_values)
def main(): ''' Step 1: Get the 2D histogram for every sample (channel and/or centre of mass energy) Step 2: Change the size of the first bin until it fulfils the minimal criteria Step 3: Check if it is true for all other histograms. If not back to step 2 Step 4: Repeat step 2 & 3 until no mo bins can be created ''' parser = ArgumentParser() parser.add_argument('-v', dest="visiblePhaseSpace", action="store_true", help="Consider visible phase space or not") parser.add_argument('-c', dest="combined", action="store_true", help="Combine channels") parser.add_argument('-C', dest="com", default=13, type=int, help="Centre of mass") parser.add_argument('-V', "--variable", dest="variable_to_run", default=None, help="Variable to run") parser.add_argument('-b', dest="from_previous_binning", action="store_true", help="Find parameters from current binning scheme") parser.add_argument('-p', dest="plotting", action="store_true", help="Plot purity, stability and resolution") args = parser.parse_args() measurement_config = XSectionConfig(13) # Initialise binning parameters bin_choices = {} # Min Purity and Stability p_min = 0.6 s_min = 0.6 # 0.5 for MET # Min events in bin for appropriate stat unc # error = 1/sqrt(N) [ unc=5% : (1/0.05)^2 = 400] n_min = 500 n_min_lepton = 500 variables = measurement_config.variables for variable in variables: if args.variable_to_run and variable not in args.variable_to_run: continue global var var = variable print('--- Doing variable', variable) variableToUse = variable if 'Rap' in variable: variableToUse = 'abs_%s' % variable histogram_information = get_histograms(measurement_config, variableToUse, args) # Calculate binning criteria from previous binning scheme if args.from_previous_binning: for hist_info in histogram_information: p, s = calculate_purity_stability(hist_info, bin_edges_vis[variable]) r = calculate_resolutions(variable, bin_edges=bin_edges_vis[variable], channel=hist_info['channel'], res_to_plot=args.plotting) bin_criteria = {'p_i': p, 's_i': s, 'res': r} if args.plotting: plotting_purity_stability(var, hist_info['channel'], bin_criteria, bin_edges_vis[var]) plotting_response(hist_info, var, hist_info['channel'], bin_edges_vis[var]) # f_out = 'unfolding/13TeV/binning_combined_{}.txt'.format(variable) # df_bin = dict_to_df(bin_criteria) # df_to_file( f_out, df_bin ) continue # Claculate the best binning if variable == 'HT': best_binning, histogram_information = get_best_binning( histogram_information, p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable], plot_resolution=args.plotting, x_min=120.) elif variable == 'ST': best_binning, histogram_information = get_best_binning( histogram_information, p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable], plot_resolution=args.plotting, x_min=146.) elif variable == 'MET': best_binning, histogram_information = get_best_binning( histogram_information, 0.5, 0.5, n_min, minimum_bin_width[variable], nice_bin_width[variable], plot_resolution=args.plotting) elif variable == 'NJets': best_binning, histogram_information = get_best_binning( histogram_information, p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable], plot_resolution=args.plotting, x_min=3.5) elif variable == 'lepton_pt': best_binning, histogram_information = get_best_binning( histogram_information, p_min, s_min, n_min_lepton, minimum_bin_width[variable], nice_bin_width[variable], plot_resolution=args.plotting, x_min=26.) elif variable == 'abs_lepton_eta': best_binning, histogram_information = get_best_binning( histogram_information, p_min, s_min, n_min_lepton, minimum_bin_width[variable], nice_bin_width[variable], plot_resolution=args.plotting) elif variable == 'NJets': best_binning, histogram_information = get_best_binning( histogram_information, p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable], plot_resolution=args.plotting, is_NJet=True) else: best_binning, histogram_information = get_best_binning( histogram_information, p_min, s_min, n_min, minimum_bin_width[variable], nice_bin_width[variable], plot_resolution=args.plotting) # Symmetric binning for lepton_eta if 'Rap' in variable: for b in list(best_binning): if b != 0.0: best_binning.append(-1.0 * b) best_binning.sort() # Make last bin smaller if huge # Won't change final results if len(best_binning) >= 4: lastBinWidth = best_binning[-1] - best_binning[-2] penultimateBinWidth = best_binning[-2] - best_binning[-3] if lastBinWidth / penultimateBinWidth > 5: newLastBinWidth = penultimateBinWidth * 5 best_binning[-1] = best_binning[-2] + newLastBinWidth # Smooth bin edges if variable == 'abs_lepton_eta': best_binning = [round(i, 2) for i in best_binning] elif variable != 'NJets': best_binning = [round(i) for i in best_binning] bin_choices[variable] = best_binning # Print the best binning to screen and JSON print('The best binning for', variable, 'is:') print('bin edges =', best_binning) print('N_bins =', len(best_binning) - 1) print('The corresponding purities and stabilities are:') for info in histogram_information: outputInfo = {} outputInfo['p_i'] = info['p_i'] outputInfo['s_i'] = info['s_i'] outputInfo['N'] = info['N'] outputInfo['res'] = info['res'] output_file = 'unfolding/13TeV/binningInfo_%s_%s_FullPS.txt' % ( variable, info['channel']) if args.visiblePhaseSpace: output_file = 'unfolding/13TeV/binningInfo_%s_%s_VisiblePS.txt' % ( variable, info['channel']) if args.plotting: plotting_purity_stability(variable, info['channel'], outputInfo, bin_choices[variable]) plotting_response(histogram_information, variable, info['channel'], bin_choices[variable]) df_out = dict_to_df(outputInfo) df_to_file(output_file, df_out) print('-' * 120) # # # # # # # # # # # # # # # # # Plots? # # # # # # # # # # # # # # # # # Final print of all binnings to screen print('=' * 120) print('For config/variable_binning.py') print('=' * 120) for variable in bin_choices: print('\'' + variable + '\' : ' + str(bin_choices[variable]) + ',')
from dps.config.summations_7TeV import sample_summations from dps.config.xsection import XSectionConfig from dps.utils.file_utilities import make_folder_if_not_exists from dps.utils.file_utilities import merge_ROOT_files import os import subprocess import time new_files = [] config_7TeV = XSectionConfig(7) #Can not output to DICE, and since we are now working with input files on /hdfs, need to find somewhere to output the merged files #this script creates #So, create folders in working directory with structure: AN-XX-XXX_Xth_draft/XTeV/<central/BJet_up/Light_Jet_up/etc. for all categories> #NOTE: YOU WILL THEN HAVE TO MOVE THESE MERGED FILES MANUALLY TO THE APPROPRIATE LOCATION IN /hdfs/TopQuarkGroup/results/histogramfiles/... # first get current working directory current_working_directory = os.getcwd() path_to_AN_folder = config_7TeV.path_to_files # change path from /hdfs to current working directory path_to_AN_folder = path_to_AN_folder.replace( "/hdfs/TopQuarkGroup/results/histogramfiles", current_working_directory) #loop through all categories (e.g. central, BJet_up, LightJet_up, etc....) and make folder for category in config_7TeV.categories_and_prefixes.keys(): make_folder_if_not_exists(path_to_AN_folder + "/" + category) # merge generator systematics histogram files for sample, input_samples in sample_summations.iteritems(): if not sample in [
def main(): config = XSectionConfig(13) method = 'TUnfold' file_for_response = File(config.unfolding_central_secondHalf, 'read') file_for_powhegPythia = File(config.unfolding_central_firstHalf, 'read') file_for_ptReweight_up = File(config.unfolding_ptreweight_up_firstHalf, 'read') file_for_ptReweight_down = File(config.unfolding_ptreweight_down_firstHalf, 'read') file_for_amcatnlo_pythia8 = File(config.unfolding_amcatnlo_pythia8, 'read') file_for_powhegHerwig = File(config.unfolding_powheg_herwig, 'read') file_for_etaReweight_up = File(config.unfolding_etareweight_up, 'read') file_for_etaReweight_down = File(config.unfolding_etareweight_down, 'read') samples_and_files_to_compare = { 'Central' : file_for_powhegPythia, 'Nominal' : file_for_response, 'PtReweighted Up' : file_for_ptReweight_up, 'PtReweighted Down' : file_for_ptReweight_down, # 'amcatnlo_pythia8' : file_for_amcatnlo_pythia8, # 'powhegHerwig' : file_for_powhegHerwig, # 'EtaReweighted Up' : file_for_etaReweight_up, # 'EtaReweighted Down' : file_for_etaReweight_down, } for channel in config.analysis_types.keys(): if channel is 'combined':continue print 'Channel :',channel for variable in config.variables: # for variable in ['ST']: print 'Variable :',variable # Always unfold with the same response matrix and tau value tau_value = get_tau_value(config, channel, variable) # tau_value = 0.00000001 _, _, h_response, _ = get_unfold_histogram_tuple( inputfile=file_for_response, variable=variable, channel=channel, met_type=config.met_type, centre_of_mass=config.centre_of_mass_energy, ttbar_xsection=config.ttbar_xsection, luminosity=config.luminosity, load_fakes=False, visiblePS=True, ) integralOfResponse = asrootpy(h_response.ProjectionY()).integral(0,-1) # Dictionary to hold results unfolded_and_truth_for_sample = {} unfolded_and_truth_xsection_for_sample = {} for sample, input_file_for_unfolding in samples_and_files_to_compare.iteritems(): _, _, h_response_to_unfold, _ = get_unfold_histogram_tuple( inputfile=input_file_for_unfolding, variable=variable, channel=channel, met_type=config.met_type, centre_of_mass=config.centre_of_mass_energy, ttbar_xsection=config.ttbar_xsection, luminosity=config.luminosity, load_fakes=False, visiblePS=True, ) measured = asrootpy(h_response_to_unfold.ProjectionX('px',1)) truth = asrootpy(h_response_to_unfold.ProjectionY()) scale = integralOfResponse / truth.integral(0,-1) measured.Scale( scale ) truth.Scale( scale ) # Unfold, and set 'data' to 'measured' unfolding = Unfolding( measured, truth, measured, h_response, None, method=method, tau=tau_value) unfolded_data = unfolding.unfold() # unfolded_and_truth_for_sample[sample] = { # 'truth' : truth_xsection, # 'unfolded' : unfolded_xsection, # 'bias' : bias # } bias = calculate_bias( truth, unfolded_data ) unfolded_and_truth_for_sample[sample] = { 'truth' : truth, 'unfolded' : unfolded_data, 'bias' : bias } unfolded_xsection = calculate_xsection( unfolded_data, variable ) truth_xsection = calculate_xsection( truth, variable ) bias_xsection = calculate_bias( truth_xsection, unfolded_xsection ) unfolded_and_truth_xsection_for_sample[sample] = { 'truth' : truth_xsection, 'unfolded' : unfolded_xsection, 'bias' : bias_xsection } plot_closure(unfolded_and_truth_for_sample, variable, channel, config.centre_of_mass_energy, method, 'number_of_unfolded_events') plot_closure(unfolded_and_truth_xsection_for_sample, variable, channel, config.centre_of_mass_energy, method, 'normalised_xsection') plot_bias(unfolded_and_truth_for_sample, variable, channel, config.centre_of_mass_energy, method, 'number_of_unfolded_events') plot_bias(unfolded_and_truth_xsection_for_sample, variable, channel, config.centre_of_mass_energy, method, 'normalised_xsection', plot_systematics=True)
action = "store_true", help = "Preliminary plot or not" ) args = parser.parse_args() return args if __name__ == '__main__': set_root_defaults() args = parse_arguments() if args.debug: log.setLevel(log.DEBUG) output_formats = ['pdf'] measurement_config = XSectionConfig( args.CoM ) # caching of variables for shorter access method = args.unfolding_method variable = args.variable show_ratio = args.show_ratio show_generator_ratio = args.show_generator_ratio visiblePS = args.visiblePS output_folder = args.output_folder plot_scale_uncertainties= args.plot_scale_uncertainties is_preliminary = not args.is_final if not output_folder.endswith( '/' ): output_folder += '/' phase_space = 'FullPS'
dest="listJobs", action='store_true', default=False, help="Just list the jobs to run and the total number of jobs") (options, _) = parser.parse_args() if options.jobNumber < 0: print "Choose a job number >= 0" sys.exit() if options.com != 7 and options.com != 8: print "Centre of mass must be 7 or 8 TeV" print "You've chosen", options.com sys.exit() config = XSectionConfig(options.com) sample_summations = None if options.com == 7: sample_summations = sample_summations_7TeV elif options.com == 8: sample_summations = sample_summations_8TeV #Can not output to DICE, and since we are now working with input files on /hdfs, need to find somewhere to output the merged files #this script creates # first get current working directory current_working_directory = os.getcwd() path_to_AN_folder = config.path_to_files # Where you want files to end up on hdfs # Because you can't merge directly to hdfs at the moment, # have to merge somewhere else then cp/mv/rsync file to hdfs
args = parser.parse_args() path = args.path com = args.CoM method = args.unfolding_method variable = args.variable output_folder = args.output_folder ps_vis = args.visiblePS phase_space = 'FullPS' bin_edges = bin_edges_full[variable] if ps_vis: phase_space = 'VisiblePS' bin_edges = bin_edges_vis[variable] measurement_config = XSectionConfig(com) # for keys in measurement_config.rate_changing_systematics_values.keys(): # print keys # print measurement_config.rate_changing_systematics_values[keys].scale unc_type = [ 'normalised', 'absolute', ] # for channel in ['electron', 'muon', 'combined']: for channel in ['combined']: for utype in unc_type: input_file = '{basepath}/{com}TeV/{var}/{ps}/central/xsection_{type}_{channel}_{method}_summary_relative.txt'.format( basepath=path, com=com,
def compare_QCD_control_regions_to_MC(): config = XSectionConfig(13) ctrl_e1 = 'TTbar_plus_X_analysis/EPlusJets/QCDConversions/FitVariables' ctrl_e2 = 'TTbar_plus_X_analysis/EPlusJets/QCD non iso e+jets/FitVariables' mc_e = 'TTbar_plus_X_analysis/EPlusJets/Ref selection/FitVariables' data_file_e = config.data_file_electron_trees ttbar_file = config.ttbar_category_templates_trees['central'] vjets_file = config.VJets_category_templates_trees['central'] singleTop_file = config.SingleTop_category_templates_trees['central'] qcd_file_e = config.electron_QCD_MC_tree_file ctrl_mu1 = 'TTbar_plus_X_analysis/MuPlusJets/QCD iso > 0.3/FitVariables' ctrl_mu2 = 'TTbar_plus_X_analysis/MuPlusJets/QCD 0.12 < iso <= 0.3/FitVariables' mc_mu = 'TTbar_plus_X_analysis/MuPlusJets/Ref selection/FitVariables' data_file_mu = config.data_file_muon_trees qcd_file_mu = config.muon_QCD_MC_tree_file weight_branches_electron = [ "EventWeight", "PUWeight", "BJetWeight", "ElectronEfficiencyCorrection" ] weight_branches_mu = [ "EventWeight", "PUWeight", "BJetWeight", "MuonEfficiencyCorrection" ] variables = ['MET', 'HT', 'ST', 'NJets', 'lepton_pt', 'abs_lepton_eta', 'WPT'] # variables = ['abs_lepton_eta'] for variable in variables: branch = variable selection = '{0} >= 0'.format(branch) if variable == 'abs_lepton_eta': branch = 'abs(lepton_eta)' selection = 'lepton_eta >= -3' for channel in ['electron', 'muon']: data_file = data_file_e qcd_file = qcd_file_e ctrl1 = ctrl_e1 ctrl2 = ctrl_e2 mc = mc_e weight_branches = weight_branches_electron if channel == 'muon': data_file = data_file_mu qcd_file = qcd_file_mu ctrl1 = ctrl_mu1 ctrl2 = ctrl_mu2 mc = mc_mu weight_branches = weight_branches_mu inputs = { 'branch': branch, 'weight_branches': weight_branches, 'tree': ctrl1, 'bin_edges': bin_edges_vis[variable], 'selection': selection, } hs_ctrl1 = { 'data': get_histogram_from_tree(input_file=data_file, **inputs), 'TTJet': get_histogram_from_tree(input_file=ttbar_file, **inputs), 'VJets': get_histogram_from_tree(input_file=vjets_file, **inputs), 'SingleTop': get_histogram_from_tree(input_file=singleTop_file, **inputs), 'QCD': get_histogram_from_tree(input_file=qcd_file, **inputs), } inputs['tree'] = ctrl2 hs_ctrl2 = { 'data': get_histogram_from_tree(input_file=data_file, **inputs), 'TTJet': get_histogram_from_tree(input_file=ttbar_file, **inputs), 'VJets': get_histogram_from_tree(input_file=vjets_file, **inputs), 'SingleTop': get_histogram_from_tree(input_file=singleTop_file, **inputs), 'QCD': get_histogram_from_tree(input_file=qcd_file, **inputs), } inputs['tree'] = mc h_qcd = get_histogram_from_tree(input_file=qcd_file, **inputs) h_ctrl1 = clean_control_region( hs_ctrl1, data_label='data', subtract=['TTJet', 'VJets', 'SingleTop'], fix_to_zero=True) h_ctrl2 = clean_control_region( hs_ctrl2, data_label='data', subtract=['TTJet', 'VJets', 'SingleTop'], fix_to_zero=True) n_qcd_ctrl1 = hs_ctrl1['QCD'].integral() n_qcd_ctrl2 = hs_ctrl2['QCD'].integral() n_data1 = h_ctrl1.integral() n_data2 = h_ctrl2.integral() n_qcd_sg = h_qcd.integral() ratio_ctrl1 = n_data1 / n_qcd_ctrl1 ratio_ctrl2 = n_data2 / n_qcd_ctrl2 qcd_estimate_ctrl1 = n_qcd_sg * ratio_ctrl1 qcd_estimate_ctrl2 = n_qcd_sg * ratio_ctrl2 h_ctrl1.Scale(qcd_estimate_ctrl1 / n_data1) h_ctrl2.Scale(qcd_estimate_ctrl2 / n_data2) properties = Histogram_properties() properties.name = 'compare_qcd_control_regions_to_mc_{0}_{1}_channel'.format( variable, channel) properties.title = 'Comparison of QCD control regions ({0} channel)'.format( channel) properties.path = 'plots' properties.has_ratio = False properties.xerr = True properties.x_limits = ( bin_edges_vis[variable][0], bin_edges_vis[variable][-1]) properties.x_axis_title = variables_latex[variable] properties.y_axis_title = 'number of QCD events' histograms = {'control region 1': h_ctrl1, 'control region 2': h_ctrl2, 'MC prediction': h_qcd} diff = absolute(h_ctrl1 - h_ctrl2) lower = h_ctrl1 - diff upper = h_ctrl1 + diff err_e = ErrorBand('uncertainty', lower, upper) plot_e = Plot(histograms, properties) plot_e.draw_method = 'errorbar' plot_e.add_error_band(err_e) compare_histograms(plot_e)
histogram_properties.y_limits = [0.1, y_limits[-1]*100 ] histogram_properties.legend_location = ( 0.9, 0.9 ) histogram_properties.name += '_logY' make_data_mc_comparison_plot( histograms_to_draw, histogram_lables, histogram_colors, histogram_properties, save_folder = 'plots/control_plots_with_systematic/logY/', show_ratio = True, normalise = False, systematics_for_ratio = uncertaintyBand, systematics_for_plot = uncertaintyBand, ) config = XSectionConfig(13) for variable in config.variables: print variable histogramsForEachChannel = {} uncertaintiesForEachChannel = {} binEdges = control_plots_bins_for01[variable] bin_low = binEdges[0] bin_high = binEdges[-1] nBins = control_plot_nbins[variable] binWidth = ( bin_high - bin_low ) / nBins reco_bin_edges = [ bin_low + binWidth * i for i in range(0, nBins + 1) ]
from dps.config.summations_8TeV import sample_summations from dps.config.xsection import XSectionConfig from dps.utils.file_utilities import make_folder_if_not_exists from dps.utils.file_utilities import merge_ROOT_files import os import subprocess import time new_files = [] config_8TeV = XSectionConfig(8) #Can not output to DICE, and since we are now working with input files on /hdfs, need to find somewhere to output the merged files #this script creates #So, create folders in working directory with structure: AN-XX-XXX_Xth_draft/XTeV/<central/BJet_up/Light_Jet_up/etc. for all categories> #NOTE: YOU WILL THEN HAVE TO MOVE THESE MERGED FILES MANUALLY TO THE APPROPRIATE LOCATION IN /hdfs/TopQuarkGroup/results/histogramfiles/... # first get current working directory current_working_directory = os.getcwd() path_to_AN_folder = config_8TeV.path_to_files # change path from /hdfs to current working directory path_to_AN_folder = path_to_AN_folder.replace( "/hdfs/TopQuarkGroup/results/histogramfiles", current_working_directory) #loop through all categories (e.g. central, BJet_up, LightJet_up, etc....) and make folder for category in config_8TeV.categories_and_prefixes.keys(): make_folder_if_not_exists(path_to_AN_folder + "/" + category) # merge generator systematics histogram files for sample, input_samples in sample_summations.iteritems(): if not sample in [
def main(): config = XSectionConfig(13) file_for_powhegPythia = File(config.unfolding_central_firstHalf, 'read') file_for_ptReweight_up = File(config.unfolding_ptreweight_up_firstHalf, 'read') file_for_ptReweight_down = File(config.unfolding_ptreweight_down_firstHalf, 'read') file_for_amcatnlo_pythia8 = File(config.unfolding_amcatnlo_pythia8, 'read') file_for_powhegHerwig = File(config.unfolding_powheg_herwig, 'read') file_for_etaReweight_up = File(config.unfolding_etareweight_up, 'read') file_for_etaReweight_down = File(config.unfolding_etareweight_down, 'read') file_for_data_template = 'data/normalisation/background_subtraction/13TeV/{variable}/VisiblePS/central/normalisation_{channel}.txt' for channel in config.analysis_types.keys(): if channel is 'combined': continue for variable in config.variables: print variable # for variable in ['HT']: # Get the central powheg pythia distributions _, _, response_central, fakes_central = get_unfold_histogram_tuple( inputfile=file_for_powhegPythia, variable=variable, channel=channel, centre_of_mass=13, load_fakes=True, visiblePS=True) measured_central = asrootpy(response_central.ProjectionX('px', 1)) truth_central = asrootpy(response_central.ProjectionY()) # Get the reweighted powheg pythia distributions _, _, response_pt_reweighted_up, _ = get_unfold_histogram_tuple( inputfile=file_for_ptReweight_up, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True) measured_pt_reweighted_up = asrootpy( response_pt_reweighted_up.ProjectionX('px', 1)) truth_pt_reweighted_up = asrootpy( response_pt_reweighted_up.ProjectionY()) _, _, response_pt_reweighted_down, _ = get_unfold_histogram_tuple( inputfile=file_for_ptReweight_down, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True) measured_pt_reweighted_down = asrootpy( response_pt_reweighted_down.ProjectionX('px', 1)) truth_pt_reweighted_down = asrootpy( response_pt_reweighted_down.ProjectionY()) # _, _, response_eta_reweighted_up, _ = get_unfold_histogram_tuple( # inputfile=file_for_etaReweight_up, # variable=variable, # channel=channel, # centre_of_mass=13, # load_fakes=False, # visiblePS=True # ) # measured_eta_reweighted_up = asrootpy(response_eta_reweighted_up.ProjectionX('px',1)) # truth_eta_reweighted_up = asrootpy(response_eta_reweighted_up.ProjectionY()) # _, _, response_eta_reweighted_down, _ = get_unfold_histogram_tuple( # inputfile=file_for_etaReweight_down, # variable=variable, # channel=channel, # centre_of_mass=13, # load_fakes=False, # visiblePS=True # ) # measured_eta_reweighted_down = asrootpy(response_eta_reweighted_down.ProjectionX('px',1)) # truth_eta_reweighted_down = asrootpy(response_eta_reweighted_down.ProjectionY()) # Get the distributions for other MC models _, _, response_amcatnlo_pythia8, _ = get_unfold_histogram_tuple( inputfile=file_for_amcatnlo_pythia8, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True) measured_amcatnlo_pythia8 = asrootpy( response_amcatnlo_pythia8.ProjectionX('px', 1)) truth_amcatnlo_pythia8 = asrootpy( response_amcatnlo_pythia8.ProjectionY()) _, _, response_powhegHerwig, _ = get_unfold_histogram_tuple( inputfile=file_for_powhegHerwig, variable=variable, channel=channel, centre_of_mass=13, load_fakes=False, visiblePS=True) measured_powhegHerwig = asrootpy( response_powhegHerwig.ProjectionX('px', 1)) truth_powhegHerwig = asrootpy(response_powhegHerwig.ProjectionY()) # Get the data input (data after background subtraction, and fake removal) file_for_data = file_for_data_template.format(variable=variable, channel=channel) data = read_tuple_from_file(file_for_data)['TTJet'] data = value_error_tuplelist_to_hist(data, reco_bin_edges_vis[variable]) data = removeFakes(measured_central, fakes_central, data) # Plot all three hp = Histogram_properties() hp.name = 'Reweighting_check_{channel}_{variable}_at_{com}TeV'.format( channel=channel, variable=variable, com='13', ) v_latex = latex_labels.variables_latex[variable] unit = '' if variable in ['HT', 'ST', 'MET', 'WPT', 'lepton_pt']: unit = ' [GeV]' hp.x_axis_title = v_latex + unit hp.x_limits = [ reco_bin_edges_vis[variable][0], reco_bin_edges_vis[variable][-1] ] hp.ratio_y_limits = [0.1, 1.9] hp.ratio_y_title = 'Reweighted / Central' hp.y_axis_title = 'Number of events' hp.title = 'Reweighting check for {variable}'.format( variable=v_latex) measured_central.Rebin(2) measured_pt_reweighted_up.Rebin(2) measured_pt_reweighted_down.Rebin(2) # measured_eta_reweighted_up.Rebin(2) # measured_eta_reweighted_down.Rebin(2) measured_amcatnlo_pythia8.Rebin(2) measured_powhegHerwig.Rebin(2) data.Rebin(2) measured_central.Scale(1 / measured_central.Integral()) measured_pt_reweighted_up.Scale( 1 / measured_pt_reweighted_up.Integral()) measured_pt_reweighted_down.Scale( 1 / measured_pt_reweighted_down.Integral()) measured_amcatnlo_pythia8.Scale( 1 / measured_amcatnlo_pythia8.Integral()) measured_powhegHerwig.Scale(1 / measured_powhegHerwig.Integral()) # measured_eta_reweighted_up.Scale( 1 / measured_eta_reweighted_up.Integral() ) # measured_eta_reweighted_down.Scale( 1/ measured_eta_reweighted_down.Integral() ) data.Scale(1 / data.Integral()) print list(measured_central.y()) print list(measured_amcatnlo_pythia8.y()) print list(measured_powhegHerwig.y()) print list(data.y()) compare_measurements( # models = {'Central' : measured_central, 'PtReweighted Up' : measured_pt_reweighted_up, 'PtReweighted Down' : measured_pt_reweighted_down, 'EtaReweighted Up' : measured_eta_reweighted_up, 'EtaReweighted Down' : measured_eta_reweighted_down}, models=OrderedDict([ ('Central', measured_central), ('PtReweighted Up', measured_pt_reweighted_up), ('PtReweighted Down', measured_pt_reweighted_down), ('amc@nlo', measured_amcatnlo_pythia8), ('powhegHerwig', measured_powhegHerwig) ]), measurements={'Data': data}, show_measurement_errors=True, histogram_properties=hp, save_folder='plots/unfolding/reweighting_check', save_as=['pdf'], line_styles_for_models=[ 'solid', 'solid', 'solid', 'dashed', 'dashed' ], show_ratio_for_pairs=OrderedDict([ ('PtUpVsCentral', [measured_pt_reweighted_up, measured_central]), ('PtDownVsCentral', [measured_pt_reweighted_down, measured_central]), ('amcatnloVsCentral', [measured_amcatnlo_pythia8, measured_central]), ('powhegHerwigVsCentral', [measured_powhegHerwig, measured_central]), ('DataVsCentral', [data, measured_central]) ]), )
def main(): config = XSectionConfig(13) method = 'TUnfold' # A few different files for testing different inputs file_for_unfolding = File(config.unfolding_central, 'read') powheg_herwig_file = File(config.unfolding_powheg_herwig, 'read') for channel in ['combined', 'muon', 'electron']: # for variable in config.variables: for variable in config.variables: # for variable in ['MET']: print variable # tau_value = get_tau_value(config, channel, variable) # tau_value = 0.000228338590921 tau_value = 0.000 h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( inputfile=file_for_unfolding, variable=variable, channel=channel, centre_of_mass=config.centre_of_mass_energy, ttbar_xsection=config.ttbar_xsection, luminosity=config.luminosity, load_fakes=False, visiblePS=True, ) # measured = asrootpy(h_response.ProjectionX('px',1)) # print 'Measured from response :',list(measured.y()) # truth = asrootpy(h_response.ProjectionY()) # print 'Truth from response :',list(truth.y()) h_truth_ph, h_measured_ph, h_response_ph, h_fakes_ph = get_unfold_histogram_tuple( inputfile=powheg_herwig_file, variable=variable, channel=channel, met_type=config.met_type, centre_of_mass=config.centre_of_mass_energy, ttbar_xsection=config.ttbar_xsection, luminosity=config.luminosity, load_fakes=False, visiblePS=True, ) measured = asrootpy(h_response_ph.ProjectionX('px',1)) # print 'Measured from response :',list(measured.y()) measured.SetBinContent(0,0) truth = asrootpy(h_response_ph.ProjectionY()) # print 'Truth from response :',list(truth.y()) # print 'Truth underflow :',truth.GetBinContent(0),truth.GetBinContent(truth.GetNbinsX()+1) # Unfold unfolding = Unfolding( measured, truth, measured, h_response, None, method=method, k_value=-1, tau=tau_value) # unfolded_data = unfolding.closureTest() # print 'Measured :',list( h_measured.y() ) # h_measured, _ = removeFakes( h_measured, None, h_response) # for binx in range(0,h_truth.GetNbinsX()+2): # for biny in range(0,h_truth.GetNbinsX()+2): # print binx, biny,h_response.GetBinContent(binx,biny) # print bin,h_truth.GetBinContent(bin) # print 'Tau :',tau_value unfolded_results = unfolding.unfold() # print 'Unfolded :',list( unfolded_results.y() ) # print unfolding.unfoldObject.GetTau() # print 'Unfolded :',list( unfolded_results.y() ) refolded_results = unfolding.refold() refolded_results.rebin(2) measured.rebin(2) print 'Refolded :',list( refolded_results.y() ) print 'Measured :',list( measured.y() ) # for i in range(1,refolded_results.GetNbinsX()): # print i,measured.GetBinContent(i),measured.GetBinError(i),abs( measured.GetBinContent(i) - refolded_results.GetBinContent(i) ) pValue = measured.Chi2Test(refolded_results) print pValue,1-pValue
help= "Specify the path where the BAT output files you want to move to hdfs are located.\ Format should be e.g /storage/<username>/<folder>/<cmssw_folder>/src/") (options, _) = parser.parse_args() if not options.pathToBATOutputFiles: print "No path to files to be moved. Exiting." sys.exit() if options.centreOfMassEnergy != 7 and options.centreOfMassEnergy != 8: print "Centre of mass energy must be 7 or 8 TeV" print "You've chosen", options.centreOfMassEnergy sys.exit() #set up the config according to the centre of mass energy config = XSectionConfig(options.centreOfMassEnergy) #Get the luminosity for the centre of mass energy luminosity = config.luminosities[options.centreOfMassEnergy] #Get current working directory current_working_directory = os.getcwd() #Get folder to move files to path_to_AN_folder = config.path_to_files #move log files separately first, since there is no "logs" category in categories_and_prefixes make_folder_if_not_exists(path_to_AN_folder + '/logs/') command = 'mv ' + options.pathToBATOutputFiles + '/*' + str( options.centreOfMassEnergy) + 'TeV*.log ' + path_to_AN_folder + '/logs/' if options.doNothing:
def main(): ''' Main function for this script ''' set_root_defaults(msg_ignore_level=3001) parser = OptionParser() parser.add_option("-o", "--output", dest="output_folder", default='data/pull_data/', help="output folder for pull data files") parser.add_option("--tau", type='float', dest="tau_value", default=-1., help="tau-value for SVD unfolding") parser.add_option("-m", "--method", type='string', dest="method", default='TUnfold', help="unfolding method") parser.add_option( "-f", "--file", type='string', dest="file", default='data/toy_mc/toy_mc_powhegPythia_N_300_13TeV.root', help="file with toy MC") parser.add_option( "-v", "--variable", dest="variable", default='MET', help= "set the variable to analyse (defined in config/variable_binning.py)") parser.add_option("--com", "--centre-of-mass-energy", dest="CoM", default=13, help='''set the centre of mass energy for analysis. Default = 8 [TeV]''', type=int) (options, _) = parser.parse_args() centre_of_mass = options.CoM measurement_config = XSectionConfig(centre_of_mass) make_folder_if_not_exists(options.output_folder) use_n_toy = int(options.file.split('_')[5]) print(use_n_toy) method = options.method variable = options.variable sample = str(options.file.split('_')[3]) tau_value = options.tau_value for channel in measurement_config.analysis_types.keys(): if channel is 'combined': continue create_unfolding_pull_data( options.file, method, channel, centre_of_mass, variable, sample, measurement_config.unfolding_central_firstHalf, # measurement_config.unfolding_central, use_n_toy, options.output_folder, tau_value)
hist_properties.y_limits = (1e-20, 1e20) path = output_base + '/' + COMEnergy + 'TeV/' + channel + '/' + variable + '/' + fit_variable + '/' if options.test: path = output_base + '/test/' measurements = {} for test, histogram in histograms[sample].iteritems(): measurements[test.replace('_', ' ')] = histogram compare_measurements( {}, measurements, show_measurement_errors=False, histogram_properties=hist_properties, save_folder=path, save_as=['pdf']) def is_test_only(COMEnergy, channel, variable, fit_variable): never = COMEnergy == '8' gonna = channel == 'electron' give = variable == 'MET' you = fit_variable == 'absolute_eta' up = True return never and gonna and give and you and up if __name__ == '__main__': config = XSectionConfig(8) # assume same setup for all options = None main()
import json from dps.config.xsection import XSectionConfig from dps.utils.file_utilities import make_folder_if_not_exists com = 13 config = XSectionConfig(com) # make_folder_if_not_exists('config/unfolding/FullPS/') make_folder_if_not_exists('config/unfolding/VisiblePS/') for channel in config.analysis_types.keys(): # for variable in config.variables: # histogramTemplate = "%s_%s" % ( variable, channel ) # outputJson = { # "output_folder": "plots/unfolding/bestRegularisation/FullPS", # "output_format": ["png", "pdf"], # "centre-of-mass energy" : com, # "channel": "%s" % channel, # "variable": "%s" % variable, # "phaseSpace" : "FullPS", # "truth" : { # "file" : "%s" % config.unfolding_central, # # "histogram": "%s/truth" % ( histogramTemplate ), # }, # "gen_vs_reco" : { # "file" : "%s" % config.unfolding_central, # # "histogram": "%s/response_without_fakes" % ( histogramTemplate ), # }, # "measured" : { # "file" : "%s" % config.unfolding_central,
def main(argv=None): '''Command line options.''' program_name = os.path.basename(sys.argv[0]) program_version = "v{0}".format(__version__) program_build_date = "{0}".format(__updated__) program_version_string = '%%prog %s (%s)' % (program_version, program_build_date) # program_usage = '''usage: spam two eggs''' # optional - will be # autogenerated by optparse program_longdesc = '''''' # optional - give further explanation about what the program does program_license = "Copyright 2015 user_name (organization_name) \ Licensed under the Apache License 2.0\nhttp://www.apache.org/licenses/LICENSE-2.0" if argv is None: argv = sys.argv[1:] try: # setup option parser parser = OptionParser(version=program_version_string, epilog=program_longdesc, description=program_license) parser.add_option("-n", "--n_toy_mc", dest="n_toy_mc", help="number of toy MC to create", type=int) parser.add_option("-i", "--n_input_mc", dest="n_input_mc", help="number of toy MC use as input", type=int) parser.add_option("-o", "--output", dest="output_folder", help="output folder for toy MC") parser.add_option( "-c", "--centre-of-mass-energy", dest="CoM", help="Centre of mass energy. Default = %default[TeV]", type=int) parser.add_option('-v', '--verbose', dest="verbose", action="store_true", help="set verbosity level [default: %default]") # set defaults parser.set_defaults(CoM=13, variable="MET", output_folder='data/toy_mc/', n_toy_mc=300, n_input_mc=50000) # process options (opts, _) = parser.parse_args(argv) if opts.verbose > 0: print("verbosity level = %d" % opts.verbose) # MAIN BODY # config = XSectionConfig(opts.CoM) generate_toy(opts.n_toy_mc, opts.n_input_mc, config, opts.output_folder) except Exception, e: indent = len(program_name) * " " sys.stderr.write(program_name + ": " + repr(e) + "\n") sys.stderr.write(indent + " for help use --help" + "\n") return 2
def run(self, args, variables): from dps.config.xsection import XSectionConfig from dps.utils.systematic import append_PDF_uncertainties from dps.utils.systematic import print_dictionary,\ get_symmetrised_systematic_uncertainty, generate_covariance_matrices,\ get_measurement_with_total_systematic_uncertainty,\ write_normalised_xsection_measurement, get_normalised_cross_sections from dps.utils.file_utilities import make_folder_if_not_exists from dps.config.variable_binning import bin_edges_vis self.__prepare(args, variables) self.__text = "Running calculate systematics" centre_of_mass_energy = self.__variables['centre_of_mass_energy'] self.__config = XSectionConfig(centre_of_mass_energy) translate_options = self.__config.translate_options met_specific_systematics = self.__config.met_specific_systematics met_type = translate_options[self.__variables['metType']] variables_no_met = self.__config.variables_no_met symmetrise_errors = self.__variables['symmetrise_errors'] variable = self.__variables['variable'] topMasses = self.__config.topMasses topMassUncertainty = self.__config.topMassUncertainty method = 'TUnfold' phase_space = 'VisiblePS' if not self.__variables['visiblePS']: phase_space = 'FullPS' path_to_JSON = '{path}/{com}TeV/{variable}/{phase_space}'.format( path=self.__variables['json_path'], com=centre_of_mass_energy, variable=self.__variables['variable'], phase_space=phase_space, ) number_of_bins = len(bin_edges_vis[variable]) - 1 # List of options to pass to systematic functions opts = { 'met_specific_systematics': met_specific_systematics, 'met_type': met_type, 'variables_no_met': variables_no_met, 'symmetrise_errors': symmetrise_errors, 'path_to_JSON': path_to_JSON, 'method': method, 'variable': variable, 'number_of_bins': number_of_bins, 'topMasses': topMasses, 'topMassUncertainty': topMassUncertainty } # Get list of all systematics all_systematics = self.__config.list_of_systematics # Add in the PDF weights all_systematics = append_PDF_uncertainties(all_systematics) list_of_systematics = {} # Do you want to use different groups of systematics? list_of_systematics['all'] = all_systematics # Print the systematics if required if os.environ.get("DEBUG", False): print_dictionary("List of the systematics in use", list_of_systematics) for channel in [ 'electron', 'muon', 'combined', 'combinedBeforeUnfolding' ]: LOG.info("Channel in use is {0} : ".format(channel)) # Output folder of covariance matrices covariance_matrix_output_path = 'plots/covariance_matrices/{phase_space}/{channel}/{variable}/' covariance_matrix_output_path = covariance_matrix_output_path.format( variable=variable, channel=channel, phase_space=phase_space, ) make_folder_if_not_exists(covariance_matrix_output_path) # Add channel specific options to list of options opts['channel'] = channel opts[ 'covariance_matrix_output_path'] = covariance_matrix_output_path # Retreive the normalised cross sections, for all groups in # list_of_systematics. systematic_normalised_uncertainty, unfolded_systematic_normalised_uncertainty = get_normalised_cross_sections( opts, list_of_systematics) # Get and symmetrise the uncertainties x_sec_with_symmetrised_systematics = get_symmetrised_systematic_uncertainty( systematic_normalised_uncertainty, opts) unfolded_x_sec_with_symmetrised_systematics = get_symmetrised_systematic_uncertainty( unfolded_systematic_normalised_uncertainty, opts) # Create covariance matrices generate_covariance_matrices(opts, x_sec_with_symmetrised_systematics) generate_covariance_matrices( opts, unfolded_x_sec_with_symmetrised_systematics) # Combine all systematic uncertainties for each of the groups of # systematics full_measurement = get_measurement_with_total_systematic_uncertainty( opts, x_sec_with_symmetrised_systematics) full_unfolded_measurement = get_measurement_with_total_systematic_uncertainty( opts, unfolded_x_sec_with_symmetrised_systematics) # Write central +- error to JSON. Group of systematics in question # is included in outputfile name. for keys in list_of_systematics.keys(): write_normalised_xsection_measurement( opts, full_measurement[keys], full_unfolded_measurement[keys], summary=keys) return True
"--normalise_to_fit", dest="normalise_to_fit", action="store_true", help="normalise the MC to fit results") parser.add_option( "-e", "--centre-of-mass-energy", dest="CoM", default=8, type=int, help="set the centre of mass energy for analysis. Default = 8 [TeV]") # parser.add_option("-i", "--use_inputs", dest="use_inputs", action="store_true", # help="use fit inputs instead of fit results") (options, args) = parser.parse_args() measurement_config = XSectionConfig(options.CoM) # caching of variables for shorter access translate_options = measurement_config.translate_options lumi = measurement_config.new_luminosity CoM = measurement_config.centre_of_mass_energy electron_histogram_title = 'CMS Preliminary, $\mathcal{L}$ = %.1f fb$^{-1}$ at $\sqrt{s}$ = %d TeV \n e+jets, $\geq$4 jets' % ( lumi / 1000.0, CoM) muon_histogram_title = 'CMS Preliminary, $\mathcal{L}$ = %.1f fb$^{-1}$ at $\sqrt{s}$ = %d TeV \n $\mu$+jets, $\geq$4 jets' % ( lumi / 1000.0, CoM) path_to_JSON = options.path + '/' + str(CoM) + 'TeV/' output_folder = options.output_folder + '/%dTeV/' % CoM make_folder_if_not_exists(output_folder) normalise_to_fit = options.normalise_to_fit category = options.category met_type = translate_options[options.metType]
def main(): args = parse_arguments() measurement_config = XSectionConfig(args.centreOfMassEnergy) # Input file name file_name = 'crap.root' if int(args.centreOfMassEnergy) == 13: file_name = getFileName('13TeV', args.sample, measurement_config) else: print "Error: Unrecognised centre of mass energy." pdfWeight = args.pdfWeight CT14Weight = args.CT14Weight MMHT14Weight = args.MMHT14Weight muFmuRWeight = args.muFmuRWeight alphaSWeight = args.alphaSWeight semiLepBrWeight = args.semiLepBrWeight fragWeight = args.fragWeight # Output file name outputFileName = 'crap.root' outputFileDir = 'unfolding/%sTeV/' % args.centreOfMassEnergy make_folder_if_not_exists(outputFileDir) energySuffix = '%sTeV' % (args.centreOfMassEnergy) if args.applyTopEtaReweighting != 0: if args.applyTopEtaReweighting == 1: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_withTopEtaReweighting_up.root' % energySuffix elif args.applyTopEtaReweighting == -1: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_withTopEtaReweighting_down.root' % energySuffix elif args.applyTopPtReweighting: if args.applyTopPtReweighting == 1: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_withTopPtReweighting_up.root' % energySuffix elif args.applyTopPtReweighting == -1: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_withTopPtReweighting_down.root' % energySuffix elif muFmuRWeight == 1: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_1muR2muF.root' % ( energySuffix) elif muFmuRWeight == 2: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_1muR05muF.root' % ( energySuffix) elif muFmuRWeight == 3: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_2muR1muF.root' % ( energySuffix) elif muFmuRWeight == 4: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_2muR2muF.root' % ( energySuffix) elif muFmuRWeight == 6: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_05muR1muF.root' % ( energySuffix) elif muFmuRWeight == 8: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_05muR05muF.root' % ( energySuffix) elif alphaSWeight == 0: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_alphaS_down.root' % ( energySuffix) elif alphaSWeight == 1: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_alphaS_up.root' % ( energySuffix) elif semiLepBrWeight == -1: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_semiLepBr_down.root' % ( energySuffix) elif semiLepBrWeight == 1: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_semiLepBr_up.root' % ( energySuffix) elif fragWeight == 1: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_frag_down.root' % ( energySuffix) elif fragWeight == 2: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_frag_central.root' % ( energySuffix) elif fragWeight == 3: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_frag_up.root' % ( energySuffix) elif fragWeight == 4: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_frag_peterson.root' % ( energySuffix) elif pdfWeight >= 0 and pdfWeight <= 99: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_pdfWeight_%i.root' % ( energySuffix, pdfWeight) elif CT14Weight >= 0 and CT14Weight <= 54: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_CT14Weight_%i.root' % ( energySuffix, CT14Weight) elif MMHT14Weight >= 0 and MMHT14Weight <= 55: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric_MMHT14Weight_%i.root' % ( energySuffix, MMHT14Weight) elif 'central' not in args.sample: outputFileName = outputFileDir + '/unfolding_TTJets_%s_%s_asymmetric.root' % ( energySuffix, args.sample) elif args.fineBinned: outputFileName = outputFileDir + '/unfolding_TTJets_%s.root' % ( energySuffix) else: outputFileName = outputFileDir + '/unfolding_TTJets_%s_asymmetric.root' % energySuffix if '70pc' in args.sample or '30pc' in args.sample: outputFileName.replace('asymmetric', 'asymmetric_' + args.sample.split('_')[1]) if 'firstHalf' in args.sample: outputFileName = outputFileName.replace('asymmetric', 'asymmetric_firstHalf') elif 'secondHalf' in args.sample: outputFileName = outputFileName.replace('asymmetric', 'asymmetric_secondHalf') if args.newPS: outputFileName = outputFileName.replace('asymmetric', 'asymmetric_newPS') # Get the tree/chain treeName = "TTbar_plus_X_analysis/Unfolding/Unfolding" print file_name file_name = getUnmergedDirectory(file_name) print file_name tree = ROOT.TChain(treeName) filenames = glob.glob(file_name) for f in filenames: tree.Add(f) with root_open(outputFileName, 'recreate') as out: nEntries = tree.GetEntries() print 'Number of entries:', nEntries # For variables where you want bins to be symmetric about 0, use abs(variable) (but also make plots for signed variable) allVariablesBins = bin_edges_vis.copy() for variable in bin_edges_vis: if 'Rap' in variable: allVariablesBins['abs_%s' % variable] = [0, bin_edges_vis[variable][-1]] recoVariableNames = {} genVariable_particle_names = {} genVariable_parton_names = {} histograms = {} residuals = {} residual_options = {} outputDirs = {} outputDirsRes = {} for variable in allVariablesBins: if args.debug and variable != 'HT': continue if args.sample in measurement_config.met_specific_systematics \ and variable in measurement_config.variables_no_met: continue outputDirs[variable] = {} outputDirsRes[variable] = {} histograms[variable] = {} residuals[variable] = {} residual_options[variable] = {} # # Variable names # recoVariableName = branchNames[variable] sysIndex = None if variable in ['MET', 'ST', 'WPT']: if args.sample == "jerup": recoVariableName += '_METUncertainties' sysIndex = 0 elif args.sample == "jerdown": recoVariableName += '_METUncertainties' sysIndex = 1 elif args.sample == "jesup": recoVariableName += '_METUncertainties' sysIndex = 2 elif args.sample == "jesdown": recoVariableName += '_METUncertainties' sysIndex = 3 # Dont need this? elif args.sample in measurement_config.met_systematics: recoVariableName += '_METUncertainties' sysIndex = measurement_config.met_systematics[args.sample] genVariable_particle_name = None genVariable_parton_name = None if variable in genBranchNames_particle: genVariable_particle_name = genBranchNames_particle[variable] if args.newPS and variable in ['HT', 'ST', 'NJets']: genVariable_particle_name += '_20GeVLastJet' if variable in genBranchNames_parton: genVariable_parton_name = genBranchNames_parton[variable] recoVariableNames[variable] = recoVariableName genVariable_particle_names[variable] = genVariable_particle_name genVariable_parton_names[variable] = genVariable_parton_name reco_bin_edges_vis_to_use = reco_bin_edges_vis[variable] for channel in channels: # Make dir in output file outputDirName = variable + '_' + channel.outputDirName outputDir = out.mkdir(outputDirName) outputDirs[variable][channel.channelName] = outputDir if args.fineBinned: outputDirResName = outputDirName + '/residuals/' outputDirRes = out.mkdir(outputDirResName) outputDirsRes[variable][channel.channelName] = outputDirRes # # Book histograms # # 1D histograms histograms[variable][channel.channelName] = {} h = histograms[variable][channel.channelName] h['truth'] = Hist(allVariablesBins[variable], name='truth', type='D') h['truthVis'] = Hist(allVariablesBins[variable], name='truthVis', type='D') h['truthVis_noWeight'] = Hist(allVariablesBins[variable], name='truthVis_noWeight', type='D') h['truth_parton'] = Hist(allVariablesBins[variable], name='truth_parton', type='D') h['measured'] = Hist(reco_bin_edges_vis_to_use, name='measured', type='D') h['measuredVis'] = Hist(reco_bin_edges_vis_to_use, name='measuredVis', type='D') h['measured_without_fakes'] = Hist( reco_bin_edges_vis_to_use, name='measured_without_fakes', type='D') h['measuredVis_without_fakes'] = Hist( reco_bin_edges_vis_to_use, name='measuredVis_without_fakes', type='D') h['fake'] = Hist(reco_bin_edges_vis_to_use, name='fake', type='D') h['fakeVis'] = Hist(reco_bin_edges_vis_to_use, name='fakeVis', type='D') # 2D histograms h['response'] = Hist2D(reco_bin_edges_vis_to_use, allVariablesBins[variable], name='response', type='D') h['response_without_fakes'] = Hist2D( reco_bin_edges_vis_to_use, allVariablesBins[variable], name='response_without_fakes', type='D') h['responseVis_without_fakes'] = Hist2D( reco_bin_edges_vis_to_use, allVariablesBins[variable], name='responseVis_without_fakes', type='D') h['response_parton'] = Hist2D(reco_bin_edges_vis_to_use, allVariablesBins[variable], name='response_parton', type='D') h['response_without_fakes_parton'] = Hist2D( reco_bin_edges_vis_to_use, allVariablesBins[variable], name='response_without_fakes_parton', type='D') if args.fineBinned: minVar = trunc(allVariablesBins[variable][0]) maxVar = trunc( max( tree.GetMaximum( genVariable_particle_names[variable]), tree.GetMaximum(reco_bin_edges_vis_to_use)) * 1.2) nBins = int(maxVar - minVar) if variable is 'lepton_eta' or variable is 'bjets_eta': maxVar = 2.4 minVar = -2.4 nBins = 1000 # nBins = 960 so that small bin width is usable in 00. [0.0025] elif 'abs' in variable and 'eta' in variable: maxVar = 2.4 minVar = 0. nBins = 960 elif 'Rap' in variable: maxVar = 2.4 minVar = -2.4 nBins = 1000 elif 'NJets' in variable: maxVar = 20.5 minVar = 3.5 nBins = 17 h['truth'] = Hist(nBins, minVar, maxVar, name='truth') h['truthVis'] = Hist(nBins, minVar, maxVar, name='truthVis') h['truthVis_noWeight'] = Hist(nBins, minVar, maxVar, name='truthVis_noWeight') h['truth_parton'] = Hist(nBins, minVar, maxVar, name='truth_parton') h['measured'] = Hist(nBins, minVar, maxVar, name='measured') h['measuredVis'] = Hist(nBins, minVar, maxVar, name='measuredVis') h['measured_without_fakes'] = Hist( nBins, minVar, maxVar, name='measured_without_fakes') h['measuredVis_without_fakes'] = Hist( nBins, minVar, maxVar, name='measuredVis_without_fakes') h['fake'] = Hist(nBins, minVar, maxVar, name='fake') h['fakeVis'] = Hist(nBins, minVar, maxVar, name='fakeVis') h['response'] = Hist2D(nBins, minVar, maxVar, nBins, minVar, maxVar, name='response') h['response_without_fakes'] = Hist2D( nBins, minVar, maxVar, nBins, minVar, maxVar, name='response_without_fakes') h['responseVis_without_fakes'] = Hist2D( nBins, minVar, maxVar, nBins, minVar, maxVar, name='responseVis_without_fakes') h['response_parton'] = Hist2D(nBins, minVar, maxVar, nBins, minVar, maxVar, name='response_parton') h['response_without_fakes_parton'] = Hist2D( nBins, minVar, maxVar, nBins, minVar, maxVar, name='response_without_fakes_parton') residuals[variable][channel.channelName] = {} r = residuals[variable][channel.channelName] for i in range(1, nBins + 1): r[i] = Hist(100, 0, maxVar * 0.1, name='Residuals_Bin_' + str(i)) residual_options[variable][channel.channelName] = {} o = residual_options[variable][channel.channelName] o['min'] = minVar o['max'] = maxVar o['nbins'] = nBins o['step'] = (maxVar - minVar) / nBins o['bin_edges'] = np.arange(minVar, maxVar, (maxVar - minVar) / nBins) # Some interesting histograms h['puOffline'] = Hist(20, 0, 2, name='puWeights_offline') h['eventWeightHist'] = Hist(100, -2, 2, name='eventWeightHist') h['genWeightHist'] = Hist(1000, 0, 1, name='genWeightHist') h['offlineWeightHist'] = Hist(100, -2, 2, name='offlineWeightHist') h['phaseSpaceInfoHist'] = Hist(10, 0, 1, name='phaseSpaceInfoHist') print("Initialisation of Histograms Complete") # Counters for studying phase space nPassGenSelection = 0 nVis = {c.channelName: 0 for c in channels} nVisNotOffline = {c.channelName: 0 for c in channels} nOffline = {c.channelName: 0 for c in channels} nOfflineNotVis = {c.channelName: 0 for c in channels} nFull = {c.channelName: 0 for c in channels} nOfflineSL = {c.channelName: 0 for c in channels} n = 0 maxEvents = -1 halfOfEvents = 0 if '70pc' in args.sample or '30pc' in args.sample: print 'Only processing fraction of total events for sample :', args.sample totalEvents = tree.GetEntries() if '70pc' in args.sample: maxEvents = int(totalEvents * 0.7) elif '30pc' in args.sample: maxEvents = int(totalEvents * 0.3) print 'Will process ', maxEvents, 'out of', totalEvents, 'events' if 'firstHalf' in args.sample or 'secondHalf' in args.sample: totalEvents = tree.GetEntries() halfOfEvents = int(totalEvents / 2) # Event Loop # for event, weight in zip(tree,weightTree): for event in tree: branch = event.__getattr__ n += 1 if not n % 100000: print 'Processing event %.0f Progress : %.2g %%' % ( n, float(n) / nEntries * 100) # if n > 100000: break if maxEvents > 0 and n > maxEvents: break if 'firstHalf' in args.sample and n >= halfOfEvents: break elif 'secondHalf' in args.sample and n < halfOfEvents: continue # # # # # # Weights and selection # # # # Pileup weight # Don't apply if calculating systematic pileupWeight = event.PUWeight # print event.PUWeight,event.PUWeight_up,event.PUWeight_down if args.sample == "pileupUp": pileupWeight = event.PUWeight_up elif args.sample == "pileupDown": pileupWeight = event.PUWeight_down # Generator level weight genWeight = event.EventWeight * measurement_config.luminosity_scale # B Jet Weight # bjetWeight = event.BJetWeight bjetWeight = event.BJetAlternativeWeight if 'fsr' in args.sample: # bjetWeight = event.BJetAlternativeWeight * event.BJetEfficiencyCorrectionWeight bjetWeight = event.BJetWeight * event.BJetEfficiencyCorrectionWeight if args.sample == "bjetup": bjetWeight = event.BJetUpWeight elif args.sample == "bjetdown": bjetWeight = event.BJetDownWeight elif args.sample == "lightjetup": bjetWeight = event.LightJetUpWeight elif args.sample == "lightjetdown": bjetWeight = event.LightJetDownWeight # Top pt systematic weight topPtSystematicWeight = 1 if args.sample == 'topPtSystematic': topPtSystematicWeight = calculateTopPtSystematicWeight( branch('lepTopPt_parton'), branch('hadTopPt_parton')) # Offline level weights offlineWeight = 1 offlineWeight *= pileupWeight offlineWeight *= bjetWeight offlineWeight_forLeptonEta = offlineWeight genWeight *= topPtSystematicWeight # Generator weight # Scale up/down, pdf if pdfWeight >= 0: genWeight *= branch('pdfWeight_%i' % pdfWeight) pass if MMHT14Weight >= 0: genWeight *= branch('MMHT14Weight_%i' % MMHT14Weight) pass if CT14Weight >= 0: genWeight *= branch('CT14Weight_%i' % CT14Weight) pass if muFmuRWeight >= 0: genWeight *= branch('muFmuRWeight_%i' % muFmuRWeight) pass if alphaSWeight == 0 or alphaSWeight == 1: genWeight *= branch('alphaSWeight_%i' % alphaSWeight) pass if semiLepBrWeight == -1: genWeight *= branch('semilepbrDown') elif semiLepBrWeight == 1: genWeight *= branch('semilepbrUp') pass if fragWeight == 1: genWeight *= branch('downFrag') elif fragWeight == 2: genWeight *= branch('centralFrag') elif fragWeight == 3: genWeight *= branch('upFrag') elif fragWeight == 4: genWeight *= branch('petersonFrag') pass if args.applyTopPtReweighting != 0: ptWeight = calculateTopPtWeight(branch('lepTopPt_parton'), branch('hadTopPt_parton'), args.applyTopPtReweighting) genWeight *= ptWeight if args.applyTopEtaReweighting != 0: etaWeight = calculateTopEtaWeight(branch('lepTopRap_parton'), branch('hadTopRap_parton'), args.applyTopEtaReweighting) genWeight *= etaWeight for channel in channels: # Generator level selection genSelection = '' genSelectionVis = '' if channel.channelName is 'muPlusJets': genSelection = event.isSemiLeptonicMuon == 1 genSelectionVis = event.passesGenEventSelection == 1 and event.pseudoLepton_pdgId == 13 if args.newPS: genSelectionVis = event.passesGenEventSelection_20GeVLastJet == 1 and event.pseudoLepton_pdgId == 13 elif channel.channelName is 'ePlusJets': genSelection = event.isSemiLeptonicElectron == 1 genSelectionVis = event.passesGenEventSelection == 1 and event.pseudoLepton_pdgId == 11 if args.newPS: genSelectionVis = event.passesGenEventSelection_20GeVLastJet == 1 and event.pseudoLepton_pdgId == 11 # Lepton weight # Channel specific leptonWeight = 1 leptonWeight_forLeptonEta = 1 if channel.channelName is 'muPlusJets': leptonWeight = event.MuonEfficiencyCorrection leptonWeight_forLeptonEta = event.MuonEfficiencyCorrection_etaBins if args.sample == 'muonup': leptonWeight = event.MuonEfficiencyCorrectionUp leptonWeight_forLeptonEta = event.MuonEfficiencyCorrectionUp_etaBins elif args.sample == 'muondown': leptonWeight = event.MuonEfficiencyCorrectionDown leptonWeight_forLeptonEta = event.MuonEfficiencyCorrectionDown_etaBins elif channel.channelName is 'ePlusJets': leptonWeight = event.ElectronEfficiencyCorrection leptonWeight_forLeptonEta = event.ElectronEfficiencyCorrection_etaBins if args.sample == 'electronup': leptonWeight = event.ElectronEfficiencyCorrectionUp leptonWeight_forLeptonEta = event.ElectronEfficiencyCorrectionUp_etaBins elif args.sample == 'electrondown': leptonWeight = event.ElectronEfficiencyCorrectionDown leptonWeight_forLeptonEta = event.ElectronEfficiencyCorrectionDown_etaBins offlineWeight_withLeptonWeight = offlineWeight * leptonWeight offlineWeight_withLeptonWeight_forLeptonEta = offlineWeight_forLeptonEta * leptonWeight_forLeptonEta # Offline level selection offlineSelection = 0 if channel.channelName is 'muPlusJets': offlineSelection = int(event.passSelection) == 1 elif channel.channelName is 'ePlusJets': offlineSelection = int(event.passSelection) == 2 # Fake selection fakeSelection = offlineSelection and not genSelection fakeSelectionVis = offlineSelection and not genSelectionVis # Phase space info if genSelection: nFull[channel.channelName] += genWeight if offlineSelection: nOfflineSL[channel.channelName] += genWeight if genSelectionVis: nPassGenSelection += 1 nVis[channel.channelName] += genWeight if not offlineSelection: nVisNotOffline[channel.channelName] += genWeight if offlineSelection: nOffline[channel.channelName] += offlineWeight * genWeight if not genSelectionVis: nOfflineNotVis[ channel.channelName] += offlineWeight * genWeight for variable in allVariablesBins: if args.debug and variable != 'HT': continue if args.sample in measurement_config.met_specific_systematics and \ variable in measurement_config.variables_no_met: continue offlineWeight_toUse = offlineWeight_withLeptonWeight if 'abs_lepton_eta' in variable: offlineWeight_toUse = offlineWeight_withLeptonWeight_forLeptonEta # # # # # # Variable to plot # # # recoVariable = branch(recoVariableNames[variable]) if variable in ['MET', 'ST', 'WPT'] and \ sysIndex != None and ( offlineSelection or fakeSelection or fakeSelectionVis ) : recoVariable = recoVariable[sysIndex] if 'abs' in variable: recoVariable = abs(recoVariable) # With TUnfold, reco variable never goes in the overflow (or underflow) if not args.fineBinned: recoVariable = min( recoVariable, allVariablesBins[variable][-1] - 0.000001) genVariable_particle = branch( genVariable_particle_names[variable]) if 'abs' in variable: genVariable_particle = abs(genVariable_particle) # # # # Fill histograms # # histogramsToFill = histograms[variable][ channel.channelName] if not args.donothing: if genSelection: histogramsToFill['truth'].Fill( genVariable_particle, genWeight) if genSelectionVis: filledTruth = True histogramsToFill['truthVis'].Fill( genVariable_particle, genWeight) histogramsToFill['truthVis_noWeight'].Fill( genVariable_particle, 1) if offlineSelection: histogramsToFill['measured'].Fill( recoVariable, offlineWeight_toUse * genWeight) histogramsToFill['measuredVis'].Fill( recoVariable, offlineWeight_toUse * genWeight) if genSelectionVis: histogramsToFill[ 'measuredVis_without_fakes'].Fill( recoVariable, offlineWeight_toUse * genWeight) if genSelection: histogramsToFill[ 'measured_without_fakes'].Fill( recoVariable, offlineWeight_toUse * genWeight) histogramsToFill['response'].Fill( recoVariable, genVariable_particle, offlineWeight_toUse * genWeight) if offlineSelection and genSelection: histogramsToFill['response_without_fakes'].Fill( recoVariable, genVariable_particle, offlineWeight_toUse * genWeight) histogramsToFill['response_without_fakes'].Fill( allVariablesBins[variable][0] - 1, genVariable_particle, (1 - offlineWeight_toUse) * genWeight) elif genSelection: histogramsToFill['response_without_fakes'].Fill( allVariablesBins[variable][0] - 1, genVariable_particle, genWeight) if offlineSelection and genSelectionVis: histogramsToFill['responseVis_without_fakes'].Fill( recoVariable, genVariable_particle, offlineWeight_toUse * genWeight) histogramsToFill['responseVis_without_fakes'].Fill( allVariablesBins[variable][0] - 1, genVariable_particle, (1 - offlineWeight_toUse) * genWeight) filledResponse = True elif genSelectionVis: histogramsToFill['responseVis_without_fakes'].Fill( allVariablesBins[variable][0] - 1, genVariable_particle, genWeight) filledResponse = True if fakeSelection: histogramsToFill['fake'].Fill( recoVariable, offlineWeight_toUse * genWeight) if fakeSelectionVis: histogramsToFill['fakeVis'].Fill( recoVariable, offlineWeight_toUse * genWeight) if args.extraHists: if genSelectionVis: histogramsToFill['eventWeightHist'].Fill( event.EventWeight) histogramsToFill['genWeightHist'].Fill( genWeight) histogramsToFill['offlineWeightHist'].Fill( offlineWeight_toUse) if args.fineBinned: # Bin reco var is in options = residual_options[variable][ channel.channelName] if offlineSelection and genSelection: # i will be fine bin number the reco var resides in for i, edge in enumerate(options['bin_edges']): if recoVariable > edge: continue else: break residual = abs(recoVariable - genVariable_particle) if not residual > options['max'] * 0.1: residuals[variable][ channel.channelName][i].Fill( residual, offlineWeight_toUse * genWeight) # # Output histgorams to file # for variable in allVariablesBins: if args.debug and variable != 'HT': continue if args.sample in measurement_config.met_systematics and variable not in [ 'MET', 'ST', 'WPT' ]: continue for channel in channels: if nOffline[channel.channelName] != 0: # Fill phase space info h = histograms[variable][ channel.channelName]['phaseSpaceInfoHist'] h.SetBinContent( 1, nVisNotOffline[channel.channelName] / nVis[channel.channelName]) # h.GetXaxis().SetBinLabel(1, "nVisNotOffline/nVis") h.SetBinContent( 2, nOfflineNotVis[channel.channelName] / nOffline[channel.channelName]) # h.GetXaxis().SetBinLabel(2, "nOfflineNotVis/nOffline") h.SetBinContent( 3, nVis[channel.channelName] / nFull[channel.channelName]) # h.GetXaxis().SetBinLabel(3, "nVis/nFull") # Selection efficiency for SL ttbar h.SetBinContent( 4, nOfflineSL[channel.channelName] / nFull[channel.channelName]) # h.GetXaxis().SetBinLabel(4, "nOfflineSL/nFull") # Fraction of offline that are SL h.SetBinContent( 5, nOfflineSL[channel.channelName] / nOffline[channel.channelName]) # h.GetXaxis().SetBinLabel(5, "nOfflineSL/nOffline") outputDirs[variable][channel.channelName].cd() for h in histograms[variable][channel.channelName]: histograms[variable][channel.channelName][h].Write() if args.fineBinned: outputDirsRes[variable][channel.channelName].cd() for r in residuals[variable][channel.channelName]: residuals[variable][channel.channelName][r].Write() print 'N events passing gen selection : ', nPassGenSelection with root_open(outputFileName, 'update') as out: # Done all channels, now combine the two channels, and output to the same file for path, dirs, objects in out.walk(): if 'electron' in path: if 'coarse' in path: continue outputDir = out.mkdir(path.replace('electron', 'combined')) outputDir.cd() for h in objects: h_e = out.Get(path + '/' + h) h_mu = out.Get(path.replace('electron', 'muon') + '/' + h) h_comb = (h_e + h_mu).Clone(h) h_comb.Write() pass pass pass