def cutflowplot(config_list):
	##Import math for s/sqrt(s+b) 
	from math import sqrt

	##Name of output root file
	root_file = "/cutflow_" + channel + ".root"

	##Construct list of weights = [no cut, cut1, cut1+cut2, ..., cut1+...+cutN]
	cut_strings = [parameter_info[param][4] for param in cut_info[0 if weight == "(njetspt30==0)" else 1][channel].keys()]
	cut_values, cut_side = [[entry[index] for entry in cut_info[0 if weight == "(njetspt30==0)" else 1][channel].values()] for index in [0,1]]
	weight_list = ["1"] + ["*".join(cut_strings[index].format(cut = cut, side = side) for index, (cut, side) in enumerate(zip(cut_values[:1 + index2], cut_side[:1 + index2]))) for index2 in range(len(cut_strings))]

	##Using harry.py the magician to get root files, read out for each process the total number of events and save them in hists as list for each weight
	hists = []
	configs = []
	for index, weights in enumerate(weight_list):
		config = configmaster.ConfigMaster(*base_config(x[0], weight_input = weights))
		config.change_config_info("plot_modules", "ExportRoot")
		config.pop(["www", "www_nodate", "legend_markers"])
		config.change_config_info("filename", str(index))
		configs.append(config.return_config())

	higgsplot.HiggsPlotter(list_of_config_dicts=configs, n_plots = len(configs), batch = "rwthcondor")	

	for index in range(len(weight_list)):
		root = ROOT.TFile.Open(flavio_path + "/VisibleMass"  + weight_name + str(index) + ".root")
		hists.append([root.Get(process).Integral() for process in sample_list])
		hists[index].append(hists[index][0]/sqrt(hists[index][0] + sum(hists[index][1:])))

	for filetype in [".root", ".json"]:
		for index in range(len(weight_list)):
			os.remove(flavio_path + "/VisibleMass" + weight_name + str(index) + filetype)

	##Use ROOT to fill hists in a new root file in which each process is a hist with the Number of events for each weight applied
	if not os.path.exists(flavio_path):
		os.mkdir(flavio_path)

	if os.path.isfile(flavio_path + root_file):
		os.remove(flavio_path + root_file)

	for index1, process in enumerate(sample_list + ["soverb"]):	
		output = ROOT.TFile(flavio_path + root_file, "UPDATE")
		root_hist = ROOT.TH1F(process, process, len(hists), 0, len(hists))
		for index2, hist in enumerate(hists):
			root_hist.SetBinContent(index2+1, hist[index1])

		output.Write()
		output.Close()

	##Write config for cutflow plots
	config = configmaster.ConfigMaster(base_config(x[0], no_plot = True)[0])
	config.add_config_info(cutflow_config(x[0])[0], 0)
	config.add_config_info(cutflow_config(x[0])[1], 5)
	config.pop(["directories", "x_expressions", "x_bins"])
	config.change_config_info(["directories", "x_expressions", "x_tick_labels"], [flavio_path, sample_list + ["soverb"], ["no cuts"] + cut_strings])
	config.print_config()

	config_list.append(config.return_config())

	return config_list
def main():
	###Call and get parser arguments
	args = parser()

	###Define global constants
	global_constants(args)

	##Define libary with all analysis modules of flavio
	Analysismodule_libary = {
				Analysismodule.control_plot:		controlplot,
				Analysismodule.effiency_plot:		efficiencyplot,
				Analysismodule.shape_plot:		shapeplot,
				Analysismodule.cut_optimization:	cutoptimization,
				Analysismodule.nminus1_plot:		nminus1plot,
				Analysismodule.cutflow_plot:		cutflowplot,
				Analysismodule.limit_plot:		limitplot
	}

	###Write config using your desired analysis function
	config_list = []
	config_list = Analysismodule_libary[args.analysis](config_list)

	###Call MVP harry.py to get your job done
	higgsplot.HiggsPlotter(list_of_config_dicts=config_list, n_plots = len(config_list), batch = "rwthcondor" if len(config_list) != 1 else None)
	if len(config_list) != 1: subprocess.Popen("cd $CMSSW_BASE/src/websync/; webplot.py {channel} -r -c".format(channel = channel), shell = True)
Exemplo n.º 3
0
def plots(channel, method):
    ##Plot postfit shapes
    config_list = []
    for category in categories:
        for shape in ["_prefit", "_postfit"]:
            config = {
                "files":
                output_dir + channel + "/" + "postfit_" + method + ".root",
                "folders":
                category[1] + shape,
                "x_expressions":
                sorted(backgrounds.keys(), reverse=True) + ["TotalBkg"] +
                data.keys() + ["Z" + channel.upper()],
                "stacks": ["bkg" for bkg in backgrounds.keys()] +
                ["unc", "data", "sig", "ratio1", "ratio2"],
                "markers": ["HIST" for hist in backgrounds.keys()] +
                ["E2", "E", "LINE", "E", "E2"],
                "formats": ["png", "pdf"],
                "nicks":
                sorted(backgrounds.values(), reverse=True) + ["Uncertainty"] +
                data.values() + ["z" + channel],
                "colors":
                sorted(backgrounds.values(), reverse=True) + ["kBlack"] +
                +data.values() + ["z" + channel, "kBlack", "kBlack"],
                "legend": [0.65, 0.5, 0.95, 0.85],
                "y_subplot_lims": [0.5, 1.5],
                "analysis_modules": ["Ratio", "MaskHistograms"],
                "mask_histogram_nicks": ["data", "ratio1"],
                "mask_above_reference_nick":
                "z" + channel,
                "mask_above_reference_value":
                50 if category[0] == 0 else 10,
                "ratio_denominator_nicks": ["Uncertainty", "Uncertainty"],
                "ratio_numerator_nicks": ["data", "Uncertainty"],
                "ratio_result_nicks": ["ratio1", "ratio2"],
                "x_label":
                "BDT score" if method == "BDT" else "m_{vis}",
                "filename":
                method + "_" + category[1] + shape,
                "lumis": [35.87],
                "energies": [13],
                "year":
                "2016",
                "scale_factors":
                ["1"
                 for proc in backgrounds.keys() + data.keys()] + ["1", "10"],
                "title": {
                    "em": "e#mu",
                    "et": "e#tau",
                    "mt": "#mu#tau"
                }[channel],
                "www":
                channel + "/Limits",
                "www_nodate":
                True,
            }
        config_list.append(config)

    higgsplot.HiggsPlotter(list_of_config_dicts=config_list)
Exemplo n.º 4
0
	def prefit_postfit_plots(self, datacards_cbs, datacards_postfit_shapes, plotting_args=None, n_processes=1, *args):
		if plotting_args is None:
			plotting_args = {}

		plot_configs = []
		bkg_plotting_order = ["ZTT", "ZLL", "ZL", "ZJ", "TTJ", "TT", "VV", "WJ", "W", "QCD"]
		for level in ["prefit", "postfit"]:
			for index, (fit_type, datacards_postfit_shapes_dict) in enumerate(datacards_postfit_shapes.iteritems()):
				if (index == 0) or (level == "postfit"):
					for datacard, postfit_shapes in datacards_postfit_shapes_dict.iteritems():
						for category in datacards_cbs[datacard].cp().bin_set():
							bkg_processes = datacards_cbs[datacard].cp().bin([category]).backgrounds().process_set()
							bkg_processes.sort(key=lambda process: bkg_plotting_order.index(process) if process in bkg_plotting_order else len(bkg_plotting_order))

							config = {}
							config["files"] = [postfit_shapes]
							config["folders"] = [category+"_"+level]
							config["x_expressions"] = ["TotalSig"] + bkg_processes + ["TotalBkg", "data_obs"]
							config["nicks"] = ["TotalSig"] + bkg_processes + ["TotalBkg", "data_obs"]
							config["stacks"] = ["sig_bkg"] + (["sig_bkg"]*len(bkg_processes)) + ["bkg_unc", "data"]

							config["labels"] = [label.lower() for label in ["TotalSig"] + bkg_processes + ["TotalBkg", "data_obs"]]
							config["colors"] = [color.lower() for color in ["TotalSig"] + bkg_processes + ["TotalBkg", "data_obs"]]
							config["markers"] = ["LINE"] + (["HIST"]*len(bkg_processes)) + ["E2", "E"]
							config["legend_markers"] = ["L"] + (["F"]*len(bkg_processes)) + ["F", "ELP"]

							config["y_lims"] = [0.0]
							config["y_rel_lims"] = [0.0, 1.4]
							config["legend"] = [0.23, 0.73, 0.9, 0.89]
							config["legend_cols"] = 3

							config["title"] = "channel_"+(category.split("_")[0])

							config["output_dir"] = os.path.join(os.path.dirname(datacard), "plots")
							config["filename"] = level+("_"+fit_type if level == "postfit" else "")+"_"+category

							if not "NormalizeByBinWidth" in config.get("analysis_modules", []):
								config.setdefault("analysis_modules", []).append("NormalizeByBinWidth")
							config["y_label"] = "Entries / bin"

							if plotting_args.get("ratio", False):
								if not "Ratio" in config.get("analysis_modules", []):
									config.setdefault("analysis_modules", []).append("Ratio")
								config.setdefault("ratio_numerator_nicks", []).extend(["noplot_TotalBkg", "noplot_TotalBkg TotalSig", "data_obs"])
								config.setdefault("ratio_denominator_nicks", []).extend(["noplot_TotalBkg"] * 3)
								config.setdefault("ratio_result_nicks", []).extend(["ratio_unc", "ratio_sig", "ratio"])
								config.setdefault("colors", []).extend(["totalbkg", "totalsig", "#000000"])
								config.setdefault("markers", []).extend(["E2", "LINE", "E"])
								config.setdefault("legend_markers", []).extend(["F", "L", "ELP"])
								config.setdefault("labels", []).extend([""] * 3)
								config["legend"] = [0.7, 0.5, 0.95, 0.92]

							plot_configs.append(config)

		# create result plots HarryPlotter
		return higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs, list_of_args_strings=[plotting_args.get("args", "")], n_processes=n_processes)
Exemplo n.º 5
0
    def pull_plots(self,
                   datacards_postfit_shapes,
                   s_fit_only=False,
                   plotting_args=None,
                   n_processes=1):
        if plotting_args is None:
            plotting_args = {}

        plot_configs = []
        for index, (fit_type, datacards_postfit_shapes_dict) in enumerate(
                datacards_postfit_shapes.iteritems()):
            if (index == 0):
                for datacard, postfit_shapes in datacards_postfit_shapes_dict.iteritems(
                ):

                    config = {}
                    config["files"] = [
                        os.path.join(os.path.dirname(datacard), "mlfit.root")
                    ]
                    config["input_modules"] = ["InputRootSimple"]
                    config["root_names"] = [
                        "fit_s", "fit_b", "nuisances_prefit"
                    ]
                    if s_fit_only:
                        config["root_names"] = ["fit_s", "nuisances_prefit"]
                        config["fit_s_only"] = [True]
                    config["analysis_modules"] = ["ComputePullValues"]
                    config["nicks_blacklist"] = ["graph_b"]
                    config["fit_poi"] = plotting_args.get("fit_poi", "r")

                    config["left_pad_margin"] = 0.40
                    config["labels"] = ["prefit", "S+B model"]
                    config["markers"] = ["L2", "P"]
                    config["fill_styles"] = [3001, 0]
                    config["legend"] = [0.75, 0.8]
                    config["legend_markers"] = ["LF", "LP"]
                    config["x_lims"] = [-5.0, 5.0]
                    config["x_label"] = "Pull values"

                    config["output_dir"] = os.path.join(
                        os.path.dirname(datacard), "plots")
                    config["filename"] = "pulls"

                    plot_configs.append(config)

        # create result plots HarryPlotter
        return higgsplot.HiggsPlotter(
            list_of_config_dicts=plot_configs,
            list_of_args_strings=[plotting_args.get("args", "")],
            n_processes=n_processes)
Exemplo n.º 6
0
def get_hists(parameter, base_config, binning, channel):
	##Get shapes for variables
	config = base_config + {
		"directories":	"/net/scratch_cms3b/{}/artus/AllSamples/merged/".format(os.environ["USER"]),
		"x_expressions": parameter,
		"plot_modules": ["ExportRoot"],
		"output_dir": plot_dir + channel,
		"x_bins": binning,
		"file_mode": "UPDATE",
		"filename": parameter,
	}
	config.pop("legend_markers")


	higgsplot.HiggsPlotter(list_of_config_dicts=[config])
	if args.y_quantity is not None:
		y_bins = [x for x in range(int(float(args.y_range[0])*100),int(float(args.y_range[1])*100)+1, int((float(args.y_range[1])-float(args.y_range[0]))*4))]
		config["y_expressions"] = [args.y_quantity]
		config["y_bins"] = [" ".join([str(x/100.0) for x in y_bins])]
		variables.append(args.y_quantity)
	if args.z_quantity is not None:
		z_bins = [x for x in range(int(float(args.z_range[0])*100),int(int(float(args.z_range[1])*100)+1), int((float(args.z_range[1])-float(args.z_range[0]))*4))]
		config["z_expressions"] = [args.z_quantity]
		config["z_bins"] = [" ".join([str(x/100.0) for x in z_bins])]
		variables.append(args.z_quantity)
	current_cuts = []
	current_max = 0
	real_values = []
	for i in range(3):
		if i == 0:
			higgsplot.HiggsPlotter(list_of_config_dicts=[config])
		else:
			b_max_index = current_cuts[0]-1
			b_min = x_bins[b_max_index-3] if b_max_index >=3 else x_bins[0]
			b_max = x_bins[b_max_index+3] if (len(x_bins) - b_max_index) >3 else x_bins[-1]
			b_min = int(b_min*100)
			b_max = int(b_max*100)
			step = (b_max-b_min)/10 if (b_max-b_min)/10 >= 1 else 1
			x_bins = [x_bins[0]]+[x/100.0 for x in range(b_min, b_max, int(step))]+[x_bins[-1]]
			config["x_bins"] = [" ".join([str(x/100.0) for x in x_bins])]
			print config["x_bins"]
			if args.y_quantity is not None:
				b_max_index = current_cuts[1]-1
				b_min = y_bins[b_max_index-3] if b_max_index >=3 else y_bins[0]
				b_max = y_bins[b_max_index+3] if (len(y_bins) - b_max_index) >3 else y_bins[-1]
				b_min = int(b_min*100)
Exemplo n.º 8
0
    )

    configs.extend(
        selection_check_lMu_PPV.return_json_with_changed_x_and_weight(
            x_expressions=["leadingMuon_PhotonsFromFirstPVPtFlow_full"]))

    selection_check_lMu_PPV_peak = selection_check_lMu_PPV.clone(
        name="selection_check_lMu_PPV_peak",
        legend=data_embedded_mirrored_random_legend_upper_right,
        title="#DeltaR in [0.21,0.22]; peak region (#pm 10 GeV)",
        #title = "DeltaR 0.21-0.22 & peak region"
    )

    configs.extend(
        selection_check_lMu_PPV_peak.return_json_with_changed_x_and_weight(
            x_expressions=["leadingMuon_PhotonsFromFirstPVPtFlow_peak"]))

    selection_check_lMu_PPV_sideband = selection_check_lMu_PPV.clone(
        name="selection_check_lMu_PPV_sideband",
        legend=data_embedded_mirrored_random_legend_upper_right,
        title="#DeltaR in [0.21,0.22]; sideband region (#pm 10 GeV)",
        #title = "DeltaR 0.21-0.22 & sideband region"
    )

    configs.extend(
        selection_check_lMu_PPV_sideband.return_json_with_changed_x_and_weight(
            x_expressions=["leadingMuon_PhotonsFromFirstPVPtFlow_sideband"]))

    higgs_plotter = higgsplot.HiggsPlotter(list_of_config_dicts=configs,
                                           list_of_args_strings=[""])
Exemplo n.º 9
0
def main(args):

    signal_names = ["ggh", "qqh"]
    signal = ["ggH125", "qqH125"]

    if args.emb and args.ff:
        bkg_processes_names = ["emb", "zll", "ttl", "vvl", "fakes"]
        bkg_processes = ["EMB", "ZL", "TTL", "VVL", "jetFakes"]
    elif args.emb:
        bkg_processes_names = [
            "emb", "zll", "zj", "ttl", "ttj", "vvl", "vvj", "w", "qcd"
        ]
        bkg_processes = [
            "EMB", "ZL", "ZJ", "TTL", "TTJ", "VVL", "VVJ", "W", "QCD"
        ]
    elif args.ff:
        bkg_processes_names = [
            "ztt", "zll", "ttt", "ttl", "vvt", "vvl", "fakes"
        ]  # enforced by HarryPlotter
        bkg_processes = ["ZTT", "ZL", "TTT", "TTL", "VVT", "VVL", "jetFakes"]
    else:
        bkg_processes_names = [
            "ztt", "zll", "zj", "ttl", "ttt", "ttj", "vvl", "vvt", "vvj", "w",
            "qcd"
        ]
        bkg_processes = [
            "ZTT", "ZL", "ZJ", "TTL", "TTT", "TTJ", "VVL", "VVT", "VVJ", "W",
            "QCD"
        ]
    channels = args.channels
    analysis = args.analysis
    era = args.era
    if args.lumi is None:
        if "2016" in era:
            lumi = 35.9
        elif "2017" in era:
            lumi = 41.5
        elif "2018" in era:
            lumi = 59.7
    else:
        lumi = args.lumi
    output_dir = args.output_dir
    y_log = args.y_log
    mass = args.mass
    variables = args.variables
    categories = [c for c in args.categories
                  ] if args.categories is not None else None
    if "all" in variables:
        variables = [
            "mt_1", "mt_2", "pt_1", "pt_2", "eta_1", "eta_2", "m_vis", "ptvis",
            "npv", "njets", "nbtag", "jpt_1", "jpt_2", "jeta_1", "jeta_2",
            "met", "mjj", "dijetpt", "pZetaMissVis", "m_1", "m_2",
            "decayMode_1", "decayMode_2", "iso_1", "iso_2", "rho", "mt_tot",
            "d0_1", "d0_2", "dZ_1", "dZ_2"
        ]

    if args.www:
        config_template['www'] = ''
    if args.www_dir:
        config_template['www_dir'] = args.www_dir
    if args.www_no_overwrite:
        config_template['www_no_overwrite'] = True
        config_template['no_overwrite'] = True
    if args.no_overwrite:
        config_template['no_overwrite'] = True
    if args.redo_cache:
        config_template['redo_cache'] = True

    if args.filename_prefix != '':
        args.filename_prefix = args.filename_prefix if args.filename_prefix[
            0] == '_' else '_' + args.filename_prefix
    # if args.log_level == 'debug':
    # config['log_level'] =

    configs = []
    for channel in channels:
        if args.categories is None:
            categories = [channel + "_" + v for v in variables]
        shapes = args.shapes if args.shapes is not None else "shapes_{}.root".format(
            channel)
        if "em" in channel:
            bkg_processes = [
                p for p in bkg_processes if p not in ["ZJ", "TTJ", "VVJ"]
            ]
            bkg_processes_names = [
                p for p in bkg_processes_names
                if p not in ["zj", "ttj", "vvj"]
            ]
        if len(variables) == 1:
            variables = [variables[0]] * len(categories)
        for variable, category in zip(variables, categories):
            config = deepcopy(config_template)

            if args.chi2:
                config["analysis_modules"].append("AddHistograms")
                config["analysis_modules"].append("Chi2Test")
                config["add_nicks"] = [" ".join(bkg_processes_names)]
                config["add_result_nicks"] = ["tot_background_noplot"]
                config["chi2test_nicks"] = ["tot_background_noplot data"]
                config["chi2test_compare"] = ["UU CHI2/NDF"]

            config["files"] = [shapes]
            config["lumis"] = [lumi]
            config["year"] = era.strip("Run")
            config["output_dir"] = output_dir + "/" + channel
            config["y_log"] = True if ((variable in logvars)
                                       or y_log) else False
            config["y_rel_lims"] = [5, 500] if (variable
                                                in logvars) else [0.9, 1.5]
            config["markers"] = ["HIST"] * len(
                bkg_processes_names) + ["LINE"] * len(signal_names) + ["P"] + [
                    "E2"
                ] + ["LINE"] * len(signal_names) + ["P"]
            config["legend_markers"] = ["F"] * (
                len(bkg_processes_names)) + ["LINE"] * len(signal_names) + [
                    "ELP"
                ] + ["E2"] + ["L"] * len(signal_names) + ["P"]
            config["labels"] = bkg_processes_names + signal_names + [
                "data"
            ] + [""] * len(signal_names) + config["labels"]
            config["colors"] = bkg_processes_names + signal_names + [
                "data"
            ] + ["#B7B7B7"] + signal_names + ["#000000"]
            config["nicks"] = bkg_processes_names + signal_names + ["data"]
            config["x_expressions"] = [
                "#" + "#".join([
                    channel, category, process, analysis, era, variable, mass
                ]) + "#" for process in bkg_processes + signal
            ] + [
                "#" + "#".join([
                    channel, category, "data_obs", analysis, era, variable,
                    mass
                ]) + "#"
            ]
            if args.emb == False:
                config["filename"] = "_".join([
                    channel, category, analysis, era, variable, mass
                ]) + args.filename_prefix
            else:
                config["filename"] = "_".join([
                    channel, category, analysis, era, variable, mass, "emb"
                ]) + args.filename_prefix
            if args.comparison:
                config["filename"] = "_".join([
                    channel, category, analysis, era, variable, mass,
                    "comparison"
                ]) + args.filename_prefix
            if args.ff:
                config["filename"] = config["filename"] + "_ff"
            if not args.x_label == None:
                config["x_label"] = args.x_label
            else:
                config["x_label"] = "_".join([channel, variable])
            config["title"] = "_".join(["channel", channel])
            config["stacks"] = ["mc"
                                ] * len(bkg_processes_names) + signal_names + [
                                    "data"
                                ] + [x + "Ratio"
                                     for x in signal_names] + config["stacks"]
            config["ratio_denominator_nicks"] = [
                " ".join(bkg_processes_names)
            ] * (2 + len(signal_names))
            config["ratio_numerator_nicks"] = [
                " ".join(bkg_processes_names)
            ] + [" ".join(bkg_processes_names + [signal_names[0]])] + [
                " ".join(bkg_processes_names + [signal_names[1]])
            ] + ["data"]
            config["ratio_result_nicks"] = ["bkg_ratio"] + [
                x + "_ratio" for x in signal_names
            ] + ["data_ratio"]
            if args.comparison:
                config["markers"] = ["HIST"] + ["LINE"] + ["HIST"] * (
                    len(bkg_processes_names) - 1) + ["EX0"] + ["E2", "EX0"
                                                               ] + ["EX0"]
                config["legend_markers"] = ["F"] + ["L"] + ["F"] * (
                    len(bkg_processes_names) - 1) + ["PEX0"] + ["F", "PEX0"
                                                                ] + ["PEX0"]
                config["labels"] = [bkg_processes_names[0]] + [
                    "Z #rightarrow #tau#tau (simulation)"
                ] + bkg_processes_names[1:] + ["data"] + [
                    "Bkg. (embedded) unc.", "Obs./Bkg. (simulation)",
                    "Obs./Bkg. (embedded)"
                ]
                config["colors"] = [bkg_processes_names[0]] + [
                    "#C70039"
                ] + bkg_processes_names[1:] + ["data"] + [
                    "#B7B7B7", " #C70039 ", "#000000"
                ]

                config["subplot_legend"] = [0.195, 0.77, 0.92, 0.93],
                config["subplot_legend_cols"] = 3
                config["subplot_legend_fontsize"] = 0.06
                config["y_subplot_label"] = ""
                config["legend_fontsize"] = 0.044
                bkg_processes_names_noplot = [
                    "ztt_noplot", "ttt_noplot", "vvt_noplot"
                ] + [
                    x + "_noplot"
                    for x in bkg_processes_names if not "emb" in x
                ]
                config[
                    "nicks"] = bkg_processes_names + bkg_processes_names_noplot + [
                        "data"
                    ]
                config["analysis_modules"].append("AddHistograms")
                config["add_nicks"] = [" ".join(bkg_processes_names_noplot)]
                config["add_result_nicks"] = ["ztt"]
                config["add_result_position"] = 1
                config["x_expressions"] = [
                    "#" + "#".join([
                        channel, category, process, analysis, era, variable,
                        mass
                    ]) + "#" for process in bkg_processes
                ] + [
                    "#" + "#".join([
                        channel, category, process, analysis, era, variable,
                        mass
                    ]) + "#" for process in ["ZTT", "TTT", "VVT"] +
                    [x for x in bkg_processes if x != "EMB"]
                ] + [
                    "#" + "#".join([
                        channel, category, "data_obs", analysis, era, variable,
                        mass
                    ]) + "#"
                ]
                config["stacks"] = [
                    "mc"
                ] + ["ztt"] + ["mc"] * (len(bkg_processes_names) - 1) + [
                    "data"
                ] + config_template["stacks"] + ["ratio_data_zttmc"]
                config["ratio_denominator_nicks"] = [
                    " ".join(bkg_processes_names)
                ] + [" ".join(bkg_processes_names_noplot)
                     ] + [" ".join(bkg_processes_names)]
                config["ratio_numerator_nicks"] = [
                    " ".join(bkg_processes_names), "data", "data"
                ]
                config["ratio_result_nicks"] = [
                    "ratio_Bkg", "ratio_Data_to_MC", "ratio_Data"
                ]
            configs.append(config)

    import pprint as pp
    pp.pprint(configs)
    higgsplot.HiggsPlotter(list_of_config_dicts=configs,
                           list_of_args_strings=[args.additional_arguments],
                           n_processes=args.num_processes)
Exemplo n.º 10
0
def main(args):
    bkg_processes_names = ["ztt", "zl", "zj", "wj", "ttt", "ttj",
                           "qcd", "vv", "ewk"]  # enforced by HarryPlotter
    bkg_processes = ["ZTT", "ZL", "ZJ", "W", "TTT", "TTJ",
                     "QCD", "VV", "EWKZ"]  # names in ROOT file
    signal_processes_names = ["htt125", "ggh125",
                              "qqh125"]  # enforced by HarryPlotter
    signal_processes = ["HTT", "qqH", "ggH"]  # name in ROOT file

    if args.emb and args.ff:
        bkg_processes_names = ["emb", "zll", "ttl", "vvl", "fakes"]
        bkg_processes = ["EMB", "ZL", "TTL", "VVL", "jetFakesEMB"]
    elif args.emb:
        bkg_processes_names = ["emb", "zll", "zj", "ttl", "ttj", "vvl", "vvj", "w", "qcd"]
        bkg_processes = ["EMB", "ZL", "ZJ", "TTL", "TTJ", "VVL", "VVJ", "W", "QCD"]
    elif args.ff:
        bkg_processes_names = ["ztt", "zll", "ttt", "ttl", "vvt", "vvl", "fakes"]  # enforced by HarryPlotter
        bkg_processes = ["ZTT", "ZL", "TTT", "TTL", "VVT", "VVL", "jetFakes"]
    else:
        bkg_processes_names = ["ztt", "zll", "zj", "ttl", "ttt", "ttj", "vvl", "vvt", "vvj", "w", "qcd"]
        bkg_processes = ["ZTT", "ZL", "ZJ", "TTL", "TTT", "TTJ", "VVL", "VVT", "VVJ", "W", "QCD"]
    if args.bg_proc:
        bkg_processes = args.bg_proc
        bkg_processes_names = [i.lower() for i in bkg_processes]
        bkg_processes_names = ['fakes' if i.startswith('jetfakes') else i for i in bkg_processes_names]
        bkg_processes_names = ['qcd' if i.startswith('qcd') else i for i in bkg_processes_names]
        bkg_processes_names = ['w' if i.startswith('w') else i for i in bkg_processes_names]
        bkg_processes_names = ["zll" if i == 'zl' else i for i in bkg_processes_names]
        logger.info('bkg_processes:' + ' '.join(bkg_processes))
        logger.info('bkg_processes_names:' + ' '.bkg_processes_names)

    if args.no_signal:
        signal_processes_names = []
        signal_processes = []

    scale_signal = args.scale_signal
    config_template["scales"] = scale_signal
    config_template["scale_nicks"] = signal_processes_names
    if scale_signal != 1:
        postfix = "_" + str(scale_signal)
    else:
        postfix = ""
    signal_processes_labels = [
        label + postfix for label in signal_processes_names
    ]

    if args.www:
        config_template['www'] = ''
    if args.www_dir:
        config_template['www_dir'] = args.www_dir
    if args.www_no_overwrite:
        config_template['www_no_overwrite'] = True
        config_template['no_overwrite'] = True
    if args.no_overwrite:
        config_template['no_overwrite'] = True
    if args.redo_cache:
        config_template['redo_cache'] = True

    if args.filename_prefix != '':
        args.filename_prefix = args.filename_prefix if args.filename_prefix[0] == '_' else '_' + args.filename_prefix
    categories = args.categories
    channel = args.channel
    analysis = args.analysis
    era = args.era

    if "2016" in era:
        config_template['lumi'] = [35.9]
        config_template['year'] = '2016'
    elif "2017" in era:
        config_template['lumi'] = [41.5]
        config_template['year'] = '2017'
    elif "2018" in era:
        config_template['lumi'] = [59.7]
        config_template['year'] = '2018'

    mass = args.mass
    variables = args.variables

    configs = []
    for category in categories:
        for variable in variables:
            config = deepcopy(config_template)
            config["files"] = [args.shapes]
            config["markers"] = ["HIST"] * len(bkg_processes_names) + [
                "LINE"
            ] * len(signal_processes_names) + ["E"] + config["markers"]
            config["legend_markers"] = ["F"] * len(
                bkg_processes_names) + ["L"] * len(signal_processes_names) + [
                    "ELP"
                ] + config["legend_markers"]
            config[
                "labels"] = bkg_processes_names + signal_processes_labels + [
                    "data"
                ] + config["labels"]
            config["colors"] = bkg_processes_names + signal_processes_names + [
                "data"
            ] + config["colors"]
            config["nicks"] = bkg_processes_names + signal_processes_names + [
                "data"
            ]
            config["x_expressions"] = [
                "#" + "#".join([
                    channel, category, process, analysis, era, variable, mass
                ]) + "#" for process in bkg_processes + signal_processes
            ] + [
                "#" + "#".join([
                    channel, category, "data_obs", analysis, era, variable,
                    mass
                ]) + "#"
            ]
            config["filename"] = "_".join(
                [channel, category, analysis, era, variable, mass]) + args.filename_prefix
            if not args.x_label == None:
                config["x_label"] = args.x_label
            else:
                config["x_label"] = "_".join([channel, variable])
            config["title"] = "_".join(["channel", channel])
            config["stacks"] = ["mc"] * len(
                bkg_processes_names) + signal_processes_names + [
                    "data"
                ] + config["stacks"]
            config["ratio_denominator_nicks"] = [
                " ".join(bkg_processes_names)
            ] * 2
            config["ratio_numerator_nicks"] = [
                " ".join(bkg_processes_names), "data"
            ]
            configs.append(config)

    import pprint as pp
    pp.pprint(configs[0])
    higgsplot.HiggsPlotter(
        list_of_config_dicts=configs,
        list_of_args_strings=[""],
        n_processes=args.num_processes)
Exemplo n.º 11
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import Artus.Utility.logger as logger
log = logging.getLogger(__name__)

import sys

import HiggsAnalysis.KITHiggsToTauTau.plotting.higgsplot as higgsplot


if __name__ == "__main__":
	"""This script executes all steps necessary to create a plot."""
	if len(sys.argv) == 1:
		sys.argv.append("-h")
		
	higgsplot.HiggsPlotter()
Exemplo n.º 12
0
def main(args):
    bkg_processes_names = [
        "ztt", "zl", "zj", "wj", "ttt", "ttj", "qcd", "vv", "ewk"
    ]  # enforced by HarryPlotter
    bkg_processes = [
        "ZTT", "ZL", "ZJ", "W", "TTT", "TTJ", "QCD", "VV", "EWKZ"
    ]  # names in ROOT file
    signal_processes_names = ["htt125", "ggh125",
                              "qqh125"]  # enforced by HarryPlotter
    signal_processes = ["HTT", "qqH", "ggH"]  # name in ROOT file

    scale_signal = args.scale_signal
    config_template["scales"] = scale_signal
    config_template["scale_nicks"] = signal_processes_names
    if scale_signal != 1:
        postfix = "_" + str(scale_signal)
    else:
        postfix = ""
    signal_processes_labels = [
        label + postfix for label in signal_processes_names
    ]

    categories = args.categories
    channel = args.channel
    analysis = args.analysis
    era = args.era
    mass = args.mass
    variables = args.variables

    configs = []
    for category in categories:
        for variable in variables:
            config = deepcopy(config_template)
            config["files"] = [args.shapes]
            config["markers"] = ["HIST"] * len(bkg_processes_names) + [
                "LINE"
            ] * len(signal_processes_names) + ["E"] + config["markers"]
            config["legend_markers"] = ["F"] * len(bkg_processes_names) + [
                "L"
            ] * len(signal_processes_names) + ["ELP"
                                               ] + config["legend_markers"]
            config[
                "labels"] = bkg_processes_names + signal_processes_labels + [
                    "data"
                ] + config["labels"]
            config["colors"] = bkg_processes_names + signal_processes_names + [
                "data"
            ] + config["colors"]
            config["nicks"] = bkg_processes_names + signal_processes_names + [
                "data"
            ]
            config["x_expressions"] = [
                "#" + "#".join([
                    channel, category, process, analysis, era, variable, mass
                ]) + "#" for process in bkg_processes + signal_processes
            ] + [
                "#" + "#".join([
                    channel, category, "data_obs", analysis, era, variable,
                    mass
                ]) + "#"
            ]
            config["filename"] = "_".join(
                [channel, category, analysis, era, variable, mass])
            if not args.x_label == None:
                config["x_label"] = args.x_label
            else:
                config["x_label"] = "_".join([channel, variable])
            config["title"] = "_".join(["channel", channel])
            config["stacks"] = ["mc"] * len(
                bkg_processes_names) + signal_processes_names + [
                    "data"
                ] + config["stacks"]
            config["ratio_denominator_nicks"] = [
                " ".join(bkg_processes_names)
            ] * 2
            config["ratio_numerator_nicks"] = [
                " ".join(bkg_processes_names), "data"
            ]
            configs.append(config)

    for key in configs[0]:
        print(key, configs[0][key])
    higgsplot.HiggsPlotter(list_of_config_dicts=configs,
                           list_of_args_strings=[""],
                           n_processes=args.num_processes)
Exemplo n.º 13
0
    # delete existing output files
    tmp_output_files = list(
        set([
            os.path.join(config["output_dir"], config["filename"] + ".root")
            for config in plot_configs[:args.n_plots[0]]
        ]))
    for output_file_iterator in tmp_output_files:
        if os.path.exists(output_file_iterator):
            os.remove(output_file_iterator)
            log.debug("Removed file \"" + output_file_iterator +
                      "\" before it is recreated again.")
    output_files = list(set(output_files))

    # create input histograms with HarryPlotter
    higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs,
                           list_of_args_strings=[args.args],
                           n_processes=args.n_processes,
                           n_plots=args.n_plots[0])
    if args.n_plots[0] != 0:
        tools.parallelize(_call_command,
                          hadd_commands,
                          n_processes=args.n_processes)
    if args.debug_plots:
        debug_plot_configs = []
        for output_file in merged_output_files:
            debug_plot_configs.extend(plotconfigs.PlotConfigs().all_histograms(
                output_file,
                plot_config_template={
                    "markers": ["E"],
                    "colors": ["#FF0000"]
                }))
        higgsplot.HiggsPlotter(list_of_config_dicts=debug_plot_configs,
Exemplo n.º 14
0
def create_input_root_files(datacards, args):
    ''' Configuring Harry plotter according to the samples and creating input root files according to the args.'''
    plot_configs = []
    output_files = []
    merged_output_files = []
    hadd_commands = []

    sample_settings = samples.Samples()
    expression_settings = expressions.ExpressionsDict()
    binnings_settings = binnings.BinningsDict()
    systematics_factory = systematics.SystematicsFactory()

    datacards.configs._mapping_process2sample = {
        "data_obs": "data",
        "EWKZ": "ewkz",
        "QCD": "qcd",
        "TT": "ttj",
        "TTT": "ttt",
        "TTJ": "ttjj",
        "VV": "vv",
        "VVT": "vvt",
        "VVJ": "vvj",
        "W": "wj",
        "ZJ": "zj",
        "ZL": "zl",
        "ZLL": "zll",
        "ZTTPOSPOL": "zttpospol",
        "ZTTNEGPOL": "zttnegpol",
        "ZTT_GEN_DM_ZERO": "ztt_gen_dm_zero",
        "ZTT_GEN_DM_ONE": "ztt_gen_dm_one",
        "ZTT_GEN_DM_TWO": "ztt_gen_dm_two",
        "ZTT_GEN_DM_TEN": "ztt_gen_dm_ten",
        "ZTT_GEN_DM_ELEVEN": "ztt_gen_dm_eleven",
    }

    for index, (channel,
                categories) in enumerate(zip(args.channel, args.categories)):

        for category in categories:
            datacards_per_channel_category = datacardsbase.Datacards(
                cb=datacards.cb.cp().channel([channel]).bin([category]))

            higgs_masses = [
                mass for mass in datacards_per_channel_category.cb.mass_set()
                if mass != "*"
            ]

            output_file = os.path.join(
                args.output_dir,
                "input/{ANALYSIS}_{CHANNEL}_{BIN}_{ERA}.root".format(
                    ANALYSIS="ztt", CHANNEL=channel, BIN=category,
                    ERA="13TeV"))
            output_files.append(output_file)
            tmp_output_files = []

            for shape_systematic, list_of_samples in datacards_per_channel_category.get_samples_per_shape_systematic(
            ).iteritems():
                nominal = (shape_systematic == "nominal")
                list_of_samples = [
                    datacards.configs.process2sample(process)
                    for process in list_of_samples
                ]
                if ("wj"
                        in list_of_samples) and not ("qcd" in list_of_samples):
                    list_of_samples.append("qcd")
                elif ("qcd"
                      in list_of_samples) and not ("wj" in list_of_samples):
                    list_of_samples.append("wj")
                asimov_nicks = []
                if args.use_asimov_dataset:
                    asimov_nicks = [
                        nick.replace("zttpospol", "zttpospol_noplot").replace(
                            "zttnegpol", "zttnegpol_noplot")
                        for nick in list_of_samples
                    ]
                    if "data" in asimov_nicks:
                        asimov_nicks.remove("data")

                for shift_up in ([True] if nominal else [True, False]):
                    systematic = "nominal" if nominal else (
                        shape_systematic + ("Up" if shift_up else "Down"))

                    log.debug(
                        "Create inputs for (samples, systematic) = ([\"{samples}\"], {systematic}), (channel, category) = ({channel}, {category})."
                        .format(samples="\", \"".join(list_of_samples),
                                channel=channel,
                                category=category,
                                systematic=systematic))

                    tmp_quantity = args.quantity
                    tmp_omega_version = args.omega_version
                    if args.fixed_variables == "best_choice":
                        if channel in ["tt"]:
                            if category in [
                                    channel + "_" + cat for cat in [
                                        "combined_rho_oneprong",
                                        "combined_oneprong_oneprong"
                                    ]
                            ]:
                                tmp_quantity = "m_vis"
                                tmp_omega_version = None
                            elif category in [
                                    channel + "_" + cat
                                    for cat in ["combined_a1_rho"]
                            ]:
                                tmp_quantity = None
                                tmp_omega_version = None
                            elif category in [
                                    channel + "_" + cat for cat in
                                ["combined_a1_a1", "combined_a1_oneprong"]
                            ]:
                                tmp_quantity = None
                                tmp_omega_version = "BarSvfitM91"
                            elif category in [
                                    channel + "_" + cat
                                    for cat in ["combined_rho_rho", "rho"]
                            ]:
                                tmp_quantity = None
                                tmp_omega_version = "VisibleSvfit"
                        elif channel in ["mt", "et"]:
                            if category in [
                                    channel + "_" + cat
                                    for cat in ["combined_a1_oneprong"]
                            ]:
                                tmp_quantity = "m_vis"
                                tmp_omega_version = None
                            elif category in [
                                    channel + "_" + cat
                                    for cat in ["combined_rho_oneprong"]
                            ]:
                                tmp_quantity = None
                                tmp_omega_version = None
                            elif category in [
                                    channel + "_" + cat for cat in [
                                        "combined_oneprong_oneprong", "a1",
                                        "oneprong"
                                    ]
                            ]:
                                tmp_quantity = None
                                tmp_omega_version = "BarSvfitM91"
                            elif category in [
                                    channel + "_" + cat for cat in ["rho"]
                            ]:
                                tmp_quantity = None
                                tmp_omega_version = "VisibleSvfit"
                        elif channel in ["em"]:
                            if category in [
                                    channel + "_" + cat
                                    for cat in ["combined_oneprong_oneprong"]
                            ]:
                                tmp_quantity = "m_vis"
                                tmp_omega_version = None
                    elif args.fixed_variables == "best_choice_no_svfit":
                        tmp_quantity = "m_vis"
                        tmp_omega_version = None
                        if channel in ["tt", "mt", "et"]:
                            if category in [
                                    channel + "_" + cat
                                    for cat in ["combined_rho_rho", "rho"]
                            ]:
                                tmp_quantity = None
                                tmp_omega_version = "VisibleSvfit"

                    x_expression = None
                    if tmp_quantity:
                        x_expression = tmp_quantity
                    else:
                        x_expression = "testZttPol13TeV_" + category
                        if tmp_omega_version:
                            x_expression = expression_settings.expressions_dict[
                                x_expression].replace("BarSvfit",
                                                      tmp_omega_version)
                    x_expression = expression_settings.expressions_dict.get(
                        x_expression, x_expression)

                    # prepare plotting configs for retrieving the input histograms
                    config = sample_settings.get_config(
                        samples=[
                            getattr(samples.Samples, sample)
                            for sample in list_of_samples
                        ],
                        channel=channel,
                        category="catZttPol13TeV_" + category,
                        weight=args.weight,
                        lumi=args.lumi * 1000,
                        higgs_masses=higgs_masses,
                        estimationMethod="new",
                        polarisation_bias_correction=True,
                        cut_type="low_mvis_smhtt2016",
                        exclude_cuts=(["m_vis"]
                                      if x_expression == "m_vis" else []),
                        no_ewk_samples=args.no_ewk_samples,
                        no_ewkz_as_dy=True,
                        asimov_nicks=asimov_nicks)

                    systematics_settings = systematics_factory.get(
                        shape_systematic)(config)
                    # TODO: evaluate shift from datacards_per_channel_category.cb

                    config = systematics_settings.get_config(shift=(
                        0.0 if nominal else (1.0 if shift_up else -1.0)))

                    #config["qcd_subtract_shape"] =[args.qcd_subtract_shapes]

                    config["x_expressions"] = [
                        ("0" if (("gen_zttpospol" in nick) or
                                 ("gen_zttnegpol" in nick)) else x_expression)
                        for nick in config["nicks"]
                    ]

                    binnings_key = "binningZttPol13TeV_" + category + "_" + x_expression
                    if not (binnings_key in binnings_settings.binnings_dict):
                        binnings_key = "binningZttPol13TeV_" + category + (
                            ("_" + tmp_quantity) if tmp_quantity else "")
                    if binnings_key in binnings_settings.binnings_dict:
                        config["x_bins"] = [
                            ("1,-1,1" if
                             (("gen_zttpospol" in nick) or
                              ("gen_zttnegpol" in nick)) else binnings_key)
                            for nick in config["nicks"]
                        ]

                    if args.fixed_binning:
                        if args.fixed_variables:
                            if channel == "tt":
                                config["x_bins"] = [
                                    args.fixed_binning.split(",")[0] +
                                    ",-1.0001,1.0001"
                                    for nick in config["nicks"]
                                ]
                            else:
                                config["x_bins"] = [
                                    args.fixed_binning
                                    for nick in config["nicks"]
                                ]
                        else:
                            config["x_bins"] = [
                                args.fixed_binning for nick in config["nicks"]
                            ]

                    config["directories"] = [args.input_dir]

                    histogram_name_template = "${BIN}/${PROCESS}" if nominal else "${BIN}/${PROCESS}_${SYSTEMATIC}"
                    config["labels"] = [
                        histogram_name_template.replace("$", "").format(
                            PROCESS=datacards.configs.sample2process(
                                sample.replace("asimov", "data")),
                            BIN=category,
                            SYSTEMATIC=systematic)
                        for sample in config["labels"]
                    ]

                    tmp_output_file = os.path.join(
                        args.output_dir,
                        "input/{ANALYSIS}_{CHANNEL}_{BIN}_{SYSTEMATIC}_{ERA}.root"
                        .format(ANALYSIS="ztt",
                                CHANNEL=channel,
                                BIN=category,
                                SYSTEMATIC=systematic,
                                ERA="13TeV"))
                    tmp_output_files.append(tmp_output_file)
                    config["output_dir"] = os.path.dirname(tmp_output_file)
                    config["filename"] = os.path.splitext(
                        os.path.basename(tmp_output_file))[0]

                    config["plot_modules"] = ["ExportRoot"]
                    config["file_mode"] = "UPDATE"

                    if "legend_markers" in config:
                        config.pop("legend_markers")

                    plot_configs.append(config)

            hadd_commands.append("hadd -f {DST} {SRC} && rm {SRC}".format(
                DST=output_file, SRC=" ".join(tmp_output_files)))

    tmp_output_files = list(
        set([
            os.path.join(config["output_dir"], config["filename"] + ".root")
            for config in plot_configs[:args.n_plots[0]]
        ]))
    for output_file in tmp_output_files:
        if os.path.exists(output_file):
            os.remove(output_file)
            log.debug("Removed file \"" + output_file +
                      "\" before it is recreated again.")
    output_files = list(set(output_files))

    higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs,
                           list_of_args_strings=[args.args],
                           n_processes=args.n_processes,
                           n_plots=args.n_plots[0],
                           batch=args.batch)

    if args.n_plots[0] != 0:
        tools.parallelize(_call_command,
                          hadd_commands,
                          n_processes=args.n_processes)

    debug_plot_configs = []
    for output_file in (output_files):
        debug_plot_configs.extend(plotconfigs.PlotConfigs().all_histograms(
            output_file,
            plot_config_template={
                "markers": ["E"],
                "colors": ["#FF0000"]
            }))
    if args.www:
        for debug_plot_config in debug_plot_configs:
            debug_plot_config["www"] = debug_plot_config["output_dir"].replace(
                args.output_dir, args.www)
    #higgsplot.HiggsPlotter(list_of_config_dicts=debug_plot_configs, list_of_args_strings=[args.args], n_processes=args.n_processes, n_plots=args.n_plots[0])

    return None
Exemplo n.º 15
0
	exclude_cuts=args.exclude_cuts
	)
	firsts = [exp_dict.replace_expressions(s) for s in args.first_category]
	seconds = [exp_dict.replace_expressions(s) for s in args.second_category]
	config["x_expressions"] = ["1*({first})+2*({second})".format(first=" || ".join(firsts), second=" || ".join(seconds))]
	#config["x_expressions"] = ["m_sv"]

	#config["x_ticks"] = [1,2,3]
	config["x_bins"] = ["3,0.5,3.5"]
	#config["x_tick_labels"] = args.Ticklabels
	config["filename"] = args.filename
	config["output_dir"] = args.output_dir
	config["directories"] = [args.input_dir]
	args.args += " --plot-modules ExportRoot"
	#print config
	higgsplot.HiggsPlotter(list_of_config_dicts=[config],
							list_of_args_strings=[args.args])
	names = []
	ratio_names = []
	for nick in args.Samples:
		for mass in args.higgs_masses:
			add = nick
			if nick in ("ggh", "qqh", "vh"):
				add += "%s"%mass
			names.append(add)
			if nick in args.ratio:
				ratio_names.append(add)

	#sys.exit()
	tfile = ROOT.TFile(os.path.join(args.output_dir, args.filename+".root"), "READ")
	#ztt = tfile.Get("ztt")
	#zll = tfile.Get("zll")
Exemplo n.º 16
0
            # delete existing output files
            output_files = list(
                set([
                    os.path.join(config["output_dir"],
                                 config["filename"] + ".root")
                    for config in input_plot_configs[:args.n_plots[0]]
                ]))
            for output_file in output_files:
                if os.path.exists(output_file):
                    os.remove(output_file)
                    log.debug("Removed file \"" + output_file +
                              "\" before it is recreated again.")

            # create input histograms with HarryPlotter
            higgsplot.HiggsPlotter(list_of_config_dicts=input_plot_configs,
                                   n_processes=args.n_processes,
                                   n_plots=args.n_plots[0])
            tools.parallelize(_call_command,
                              hadd_commands,
                              n_processes=args.n_processes)

    # update CombineHarvester with the yields and shapes
    datacards.extract_shapes(os.path.join(
        args.output_dir, input_root_filename_template.replace("$", "")),
                             bkg_histogram_name_template,
                             sig_histogram_name_template,
                             bkg_syst_histogram_name_template,
                             sig_syst_histogram_name_template,
                             update_systematics=False)

    # create morphing
Exemplo n.º 17
0
def cutoptimization(config_list):
    ##Information to create weight string for harry plotter
    cut_side = ["<" for parameter in x]
    cut_values = range(len(cut_side))
    cut_strings = [parameter_info[parameter][4] for parameter in x]

    for index1 in range(3):
        for index2, parameter in enumerate(x):
            ##Create N-1 weight for Nth parameter if not zero Iteration
            if index1 != 0:
                weights = "*".join([
                    cut_strings[index3].format(side=side, cut=value)
                    for index3, (side,
                                 value) in enumerate(zip(cut_side, cut_values))
                    if index2 != index3
                ])

            ##Create harry plotter config
            config = configmaster.ConfigMaster(
                *base_config(parameter,
                             nick_suffix="noplot",
                             no_plot=True,
                             weight_input="1" if index1 == 0 else weights))
            config.add_config_info(
                sumofhists_config([
                    "zttnoplot zllnoplot ttjnoplot vvnoplot wjnoplot qcdnoplot"
                ], ["bkg_sum"]), 1)
            config.add_config_info(
                efficiency_config(
                    parameter, ["bkg_sum"],
                    lower_cut=True if cut_side[index2] == "<" else False)[1],
                2)
            config.pop(["www", "www_nodate", "cms", "extra_text"])
            config.change_config_info(["filename", "plot_modules"],
                                      ["_" + channel, "ExportRoot"])

            ##Create config in zero iteration to check on which side the cut should be applied
            if (index1 == 0):
                config2 = config.copy()
                config2.replace("select_lower_values", False)
                config2.change_config_info("filename", "_2")

            ##Harry.py the MVP
            higgsplot.HiggsPlotter(
                list_of_config_dicts=[config.return_config()] if index1 != 0
                else [config.return_config(),
                      config2.return_config()],
                n_plots=2 if index1 == 0 else 1)

            ##Readout best cut value
            cut_file = ROOT.TFile.Open(flavio_path + "/" +
                                       config.return_config()["filename"] +
                                       ".root")
            histogram = cut_file.Get("sOverSqrtSB")
            cut_values[index2] = (histogram.GetXaxis().GetBinCenter(
                histogram.GetMaximumBin()))

            for filetype in [".json", ".root"]:
                os.remove(flavio_path + "/" +
                          config.return_config()["filename"] + filetype)

            ##Choose which side to cut on in zero iteration
            if index1 == 0:
                cut_file2 = ROOT.TFile.Open(
                    flavio_path + "/" + config2.return_config()["filename"] +
                    ".root")
                histogram2 = cut_file2.Get("sOverSqrtSB")

                if histogram.GetMaximum() < histogram2.GetMaximum():
                    cut_values[index2] = histogram2.GetXaxis().GetBinCenter(
                        histogram2.GetMaximumBin())
                    cut_side[index2] = ">"

                for filetype in [".json", ".root"]:
                    os.remove(flavio_path + "/" +
                              config2.return_config()["filename"] + filetype)

        ##print progress
        if index1 == 0:
            print "Cut sides which are choosen: {sides}".format(sides=cut_side)
        print "Cut values for iteration {index}: {values}".format(
            index=index1, values=cut_values)

    ##Save data in cut.yaml
    cut_info = yaml.load(
        open(
            os.path.abspath(
                os.path.expandvars(
                    "$CMSSW_BASE/src/HiggsAnalysis/KITHiggsToTauTau/python/lfv/cuts.yaml"
                )), "r"))
    if cut_info == None:
        for category in [0 if weight == "(njetspt30==0)" else 1]:
            cut_info = {category: {channel: {}}}
            for (cut, side, parameter) in zip(cut_values, cut_side, x):
                cut_info[category][channel][parameter] = [cut, side]

    else:
        for category in [0 if weight == "(njetspt30==0)" else 1]:
            try:
                cut_info[category][channel] = {}
            except KeyError:
                cut_info[category] = {channel: {}}
            for (cut, side, parameter) in zip(cut_values, cut_side, x):
                cut_info[category][channel][parameter] = [cut, side]

    yaml.dump(
        cut_info,
        open(
            os.path.abspath(
                os.path.expandvars(
                    "$CMSSW_BASE/src/HiggsAnalysis/KITHiggsToTauTau/python/lfv/cuts.yaml"
                )), "w"),
        default_flow_style=False)

    sys.exit()
Exemplo n.º 18
0
def main(args):
    '''
    ### Use this for inclusive background process labels (probably obsolete)
    if args.emb:
        bkg_processes_names = [
         "emb", "zll","ttl", "vv","w", "qcd"
        ]
        bkg_processes = ["EMB", "ZL", "TTL", "VV", "W", "QCD"]  # names in ROOT file

    else:
        bkg_processes_names = [
            "ztt", "zll", "tt", "vv", "ewk", "w", "qcd"
        ]  # enforced by HarryPlotter
        bkg_processes = ["ZTT", "ZL", "TT", "VV", "EWK", "W", "QCD"]  # names in ROOT file
    '''
    ### Use this for fake-factor shapes
    if args.emb and args.ff:
        bkg_processes_names = ["emb", "zll", "ttl", "vvl", "fakes"]
        bkg_processes = ["EMB", "ZL", "TTL", "VVL",
                         "jetFakes"]  # names in ROOT file
    elif args.emb:
        bkg_processes_names = [
            "emb", "zll", "zj", "ttl", "ttj", "ewkl", "ewkj", "vvl", "vvj",
            "w", "qcd"
        ]
        bkg_processes = [
            "EMB", "ZL", "ZJ", "TTL", "TTJ", "EWKL", "EWKJ", "VVL", "VVJ",
            "WEMB", "QCDEMB"
        ]  # names in ROOT file
    elif args.ff:
        bkg_processes_names = [
            "ztt", "zll", "ttt", "ttl", "vvt", "vvl", "ewk", "fakes"
        ]  # enforced by HarryPlotter
        bkg_processes = [
            "ZTT", "ZL", "TTT", "TTL", "VVT", "VVL", "EWK", "jetFakes"
        ]  # names in ROOT file
    else:
        bkg_processes_names = [
            "ztt", "zll", "zj", "tt", "vv", "ewk", "w", "qcd"
        ]  # enforced by HarryPlotter
        bkg_processes = ["ZTT", "ZL", "ZJ", "TT", "VV", "EWK", "W",
                         "QCD"]  # names in ROOT file

    qcd_scale_factors = {"mt": 1.0, "et": 1.0, "tt": 1.0, "em": 1.0}
    channels = args.channels
    analysis = args.analysis
    era = args.era
    output_dir = args.output_dir
    y_log = args.y_log
    mass = args.mass
    variables = args.variables
    categories = [c for c in args.categories
                  ] if args.categories is not None else None
    if "all" in variables:
        variables = [
            "mt_1", "mt_2", "pt_1", "pt_2", "eta_1", "eta_2", "m_vis", "ptvis",
            "npv", "njets", "nbtag", "jpt_1", "jpt_2", "jeta_1", "jeta_2",
            "met", "mjj", "dijetpt", "pZetaMissVis", "m_1", "m_2",
            "decayMode_1", "decayMode_2", "iso_1", "iso_2", "rho", "mt_tot",
            "d0_1", "d0_2", "dZ_1", "dZ_2"
        ]

    configs = []
    for channel in channels:
        if args.categories is None:
            categories = [channel + "_" + v for v in variables]
        shapes = args.shapes if args.shapes is not None else "shapes_{}.root".format(
            channel)
        if "em" in channel:
            bkg_processes = [
                p for p in bkg_processes
                if p not in ["ZJ", "TTJ", "VVJ", "EWKJ", "WEMB"]
            ]
            bkg_processes_names = [
                p for p in bkg_processes_names
                if p not in ["zj", "ttj", "vvj", "ewkj"]
            ]
            if not "W" in bkg_processes:
                bkg_processes.insert(bkg_processes_names.index("w"), "W")
        elif "tt" in channel and not args.ff:
            bkg_processes = [
                p for p in bkg_processes if p not in ["WEMB", "QCDEMB"]
            ]
            if not "W" in bkg_processes:
                bkg_processes.insert(bkg_processes_names.index("w"), "W")
            if not "QCD" in bkg_processes:
                bkg_processes.insert(bkg_processes_names.index("qcd"), "QCD")
        for variable, category in zip(variables, categories):
            config = deepcopy(config_template)

            if args.chi2:
                config["analysis_modules"].append("AddHistograms")
                config["analysis_modules"].append("Chi2Test")
                config["add_nicks"] = [" ".join(bkg_processes_names)]
                config["add_result_nicks"] = ["tot_background_noplot"]
                config["chi2test_nicks"] = ["tot_background_noplot data"]
                config["chi2test_compare"] = ["UW CHI2/NDF"]

            config["files"] = [shapes]
            config["lumis"] = [args.lumi]
            config["output_dir"] = output_dir + "/" + channel
            config["y_log"] = True if (variable in logvars) else False
            config["y_rel_lims"] = [0.9, 50] if y_log else [0.9, 1.5]
            config["markers"] = ["HIST"] * len(bkg_processes_names) + [
                "P"
            ] + config["markers"]
            config["legend_markers"] = ["F"] * (len(bkg_processes_names)) + [
                "ELP"
            ] + config["legend_markers"]
            config["labels"] = bkg_processes_names + ["data"
                                                      ] + config["labels"]
            config["colors"] = bkg_processes_names + ["data"
                                                      ] + config["colors"]
            config["nicks"] = bkg_processes_names + ["data"]
            config["x_expressions"] = [
                "#" + "#".join([
                    channel, category, process, analysis, "Run2017ReReco31Mar",
                    variable, mass
                ]) + "#" for process in bkg_processes
            ] + [
                "#" + "#".join([
                    channel, category, "data_obs", analysis,
                    "Run2017ReReco31Mar", variable, mass
                ]) + "#"
            ]
            if args.emb == False:
                config["filename"] = "_".join([
                    channel, category, analysis, "Run2017ReReco31Mar",
                    variable, mass
                ])
            else:
                config["filename"] = "_".join([
                    channel, category, analysis, "Run2017ReReco31Mar",
                    variable, mass, "emb"
                ])
            if args.ff:
                config["filename"] = config["filename"] + "_ff"
            if not args.x_label == None:
                config["x_label"] = args.x_label
            else:
                config["x_label"] = "_".join([channel, variable])
            config["title"] = "_".join(["channel", channel])
            config["stacks"] = ["mc"] * len(bkg_processes_names) + [
                "data"
            ] + config["stacks"]
            config["ratio_denominator_nicks"] = [
                " ".join(bkg_processes_names)
            ] * 2
            config["ratio_numerator_nicks"] = [
                " ".join(bkg_processes_names), "data"
            ]
            #config["scale_factors"] = [1.0  if x != "qcd" else qcd_scale_factors[channel] for x in bkg_processes_names] + [1.0]
            configs.append(config)

    higgsplot.HiggsPlotter(list_of_config_dicts=configs,
                           list_of_args_strings=[args.additional_arguments],
                           n_processes=args.num_processes)
Exemplo n.º 19
0
def main(args, heavy_mass, light_mass):
    signal = []
    signal_names = []

    prefit_categories = {1: "Inclusive"}

    # folders = ["htt_{}_{}_2016_prefit".format(args.channels[0],x,era.strip("Run") for x in categories.keys())

    # signal_names=["ggh","qqh"]
    # signal=["ggH125","qqH125"]
    if "em" in args.channels:
        args.ff = False
        prefit_categories.update({4: "ss"})
    signal.append("NMSSM_{}_125_{}".format(heavy_mass, light_mass))
    signal_names.append("nmssm_{}_125_{}".format(heavy_mass, light_mass))

    if args.emb and args.ff:
        bkg_processes_names = ["emb", "zll", "ttl", "vvl", "fakes", "htt"]
        bkg_processes = ["EMB", "ZL", "TTL", "VVL", "jetFakes", "HTT"]

    elif args.emb:
        bkg_processes_names = [
            "emb", "zll", "zj", "ttl", "ttj", "vvl", "vvj", "w", "qcd"
        ]
        bkg_processes = [
            "EMB", "ZL", "ZJ", "TTL", "TTJ", "VVL", "VVJ", "W", "QCD"
        ]
    elif args.ff:
        bkg_processes_names = [
            "ztt", "zll", "ttt", "ttl", "vvt", "vvl", "fakes"
        ]  # enforced by HarryPlotter
        bkg_processes = ["ZTT", "ZL", "TTT", "TTL", "VVT", "VVL", "jetFakes"]
    else:
        bkg_processes_names = [
            "ztt", "zll", "zj", "ttl", "ttt", "ttj", "vvl", "vvt", "vvj", "w",
            "qcd"
        ]
        bkg_processes = [
            "ZTT", "ZL", "ZJ", "TTL", "TTT", "TTJ", "VVL", "VVT", "VVJ", "W",
            "QCD"
        ]

    if "em" in args.channels:
        bkg_processes = [
            p for p in bkg_processes if p not in ["ZJ", "TTJ", "VVJ"]
        ]
        bkg_processes_names = [
            p for p in bkg_processes_names if p not in ["zj", "ttj", "vvj"]
        ]

    bkg_processes_save = [x for x in bkg_processes]
    bkg_processes_names_save = [x for x in bkg_processes_names]

    rfile = ROOT.TFile(args.shapes)

    channels = args.channels
    analysis = args.analysis
    era = args.era
    if args.lumi is None:
        if "2016" in era:
            lumi = 35.9
        elif "2017" in era:
            lumi = 41.5
        elif "2018" in era:
            lumi = 59.7
    else:
        lumi = args.lumi
    output_dir = args.output_dir + "/" + era
    y_log = args.y_log
    x_log = args.x_log

    mass = args.mass
    variables = args.variables

    if args.www:
        config_template['www'] = ''
    if args.www_dir:
        config_template['www_dir'] = args.www_dir
    if args.www_no_overwrite:
        config_template['www_no_overwrite'] = True
        config_template['no_overwrite'] = True
    if args.no_overwrite:
        config_template['no_overwrite'] = True
    if args.redo_cache:
        config_template['redo_cache'] = True

    if args.filename_prefix != '':
        args.filename_prefix = args.filename_prefix if args.filename_prefix[
            0] == '_' else '_' + args.filename_prefix
    # if args.log_level == 'debug':
    # config['log_level'] =

    configs = []
    for channel in channels:
        if args.categories is None:
            categories = [channel + "_" + v for v in variables]
        shapes = args.shapes if args.shapes is not None else "shapes_{}.root".format(
            channel)
        if len(variables) == 1:
            variables = [variables[0]] * len(prefit_categories.keys())
        for variable, category in zip(variables, prefit_categories.keys()):
            config = deepcopy(config_template)
            bkg_processes = [p for p in bkg_processes_save]
            bkg_processes_names = [p for p in bkg_processes_names_save]
            empty_processes = []
            empty_names = []
            fit_mode = "postfit" if "postfit" in shapes else "prefit"
            for process, name in zip(bkg_processes, bkg_processes_names):
                if not rfile.Get("htt_{}_{}_{}_{}/{}".format(
                        args.channels[0], category, era.strip("Run"), fit_mode,
                        process)):
                    print "Process {} not found for category htt_{}_{}_{}_{}. Continuing.".format(
                        process, args.channels[0], category, era.strip("Run"),
                        fit_mode)
                    empty_processes.append(process)
                    empty_names.append(name)
            for process, name in zip(signal, signal_names):
                if not rfile.Get("htt_{}_{}_{}_{}/{}".format(
                        args.channels[0], category, era.strip("Run"), fit_mode,
                        process)):
                    print "Process {} not found for category htt_{}_{}_{}_{}. Continuing.".format(
                        process, args.channels[0], category, era.strip("Run"),
                        fit_mode)
                    empty_processes.append(process)
                    empty_names.append(name)
                    signal = ["TotalSig"]

            bkg_processes = [
                p for p in bkg_processes if not p in empty_processes
            ]
            bkg_processes_names = [
                p for p in bkg_processes_names if not p in empty_names
            ]
            config["files"] = [shapes]
            config["folders"] = "htt_{}_{}_{}_{}".format(
                args.channels[0], category, era.strip("Run"), fit_mode)
            config["lumis"] = [lumi]
            config["year"] = era.strip("Run")
            config[
                "output_dir"] = output_dir + "/" + channel  #+"/"+prefit_categories[category]
            config["y_log"] = True if ((variable in logvars)
                                       or y_log) else False
            config["x_log"] = True if x_log else False
            gof_file = "CMSSW_10_2_16_UL/src/output/gof/{era}_{channel}_{mass}_{batch}_{cat}/gof.json".format(
                era=era.strip("Run"),
                channel=channel,
                mass=args.mass,
                batch=args.batch,
                cat=category)
            if category == 5:
                gof_file = "gof_categories/output/gof/{era}_{channel}_{mass}_{batch}_{cat}/gof.json".format(
                    era=era.strip("Run"),
                    channel=channel,
                    mass=args.mass,
                    batch=args.batch,
                    cat=category)
            p_value = -1
            if os.path.exists(gof_file):
                with open(gof_file, "r") as f:
                    gof_result = json.load(f)
                    temp_mass = args.heavy_mass
                    print args.heavy_mass
                    print gof_file
                    if int(args.batch) in [5, 6] and int(
                            args.heavy_mass) == 2000:
                        temp_mass = 3000
                    try:
                        p_value = gof_result["{}.0".format(temp_mass)]["p"]
                    except:
                        p_value = -1

            # config["subplot_legend"]  = [0.15, 0.7, 0.4, 0.8]
            # config["subplot_legend_fontsize"] = 0.14
            config["y_rel_lims"] = [10, 1000000] if ((variable in logvars)
                                                     or y_log) else [0., 1.5]
            config["markers"] = ["HIST"] * len(bkg_processes_names) + [
                "E2"
            ] + ["LINE"] * len(signal_names) + ["P"] + [
                "E2"
            ] + ["LINE"] * len(signal_names) + ["P"]
            config["legend_markers"] = ["F"] * (len(bkg_processes_names)) + [
                "F"
            ] + ["LX0"] * len(signal_names) + ["ELP"] + [
                "E2"
            ] + ["L"] * len(signal_names) + [""]
            p_label = [""] if (fit_mode == "postfit"
                               or category > 10) else ["p={}".format(p_value)]
            signal_label = [
                '#scale[0.85]{H(%s)#rightarrowh_{SM}(125)h_{S}(%s) (5 pb)}' %
                (heavy_mass, light_mass)
            ]
            if channel == "tt":
                signal_label = [
                    '#scale[0.85]{H(%s)#rightarrowh_{SM}(125)h_{S}(%s) (0.05 pb)}'
                    % (heavy_mass, light_mass)
                ]

            config["labels"] = bkg_processes_names + [
                "Bkg.unc."
            ] + signal_label + ["data"
                                ] + [""] * len(signal_names) + [""] + p_label
            config["nicks"] = bkg_processes_names + signal_names + ["data"]

            config["colors"] = bkg_processes_names + [
                "transgrey"
            ] + ["#CF5E61"] * len(signal_names) + ["data"] + [
                "#B7B7B7"
            ] + ["#CF5E61"] * len(signal_names) + ["#000000"]

            config["x_expressions"] = bkg_processes + signal + ["data_obs"]
            config["scale_factors"] = [1] * len(bkg_processes_names) + [
                0.5
            ] * len(signal_label) + [1] if channel == "tt" else [1] * len(
                bkg_processes_names) + [50] * len(signal_label) + [1]

            if args.emb == False:
                config["filename"] = "_".join([
                    channel,
                    str(category), analysis, era, variable, mass,
                    str(heavy_mass),
                    str(light_mass)
                ])
            else:
                config["filename"] = "_".join([
                    channel,
                    str(category), analysis, era, variable, mass,
                    str(heavy_mass),
                    str(light_mass), "emb"
                ])
            if args.comparison:
                config["filename"] = "_".join([
                    channel,
                    str(category), analysis, era, variable, mass, "comparison"
                ]) + args.filename_prefix
            if args.ff:
                config["filename"] = config["filename"] + "_ff"
            if not args.x_label == None:
                config["x_label"] = args.x_label
            else:
                config["x_label"] = "_".join([channel, variable])
            config["filename"] = config["filename"] + "_" + fit_mode

            title_dict = {}
            title_dict["mt"] = "#mu#tau_{h}"
            title_dict["tt"] = "#tau_{h}#tau_{h}"
            title_dict["et"] = "e#tau_{h}"
            title_dict["em"] = "e#mu"

            config["title"] = title_dict[channel]
            config["stacks"] = ["mc"] * len(
                bkg_processes_names) + signal_names + ["bkg_unc"] + [
                    "data"
                ] + [x + "Ratio" for x in signal_names] + config["stacks"]
            config["ratio_denominator_nicks"] = [
                " ".join(bkg_processes_names)
            ] * (2 + len(signal_names))
            config["ratio_numerator_nicks"] = [" ".join(bkg_processes_names)]
            for i in range(len(signal_names)):
                config["ratio_numerator_nicks"].append(
                    " ".join(bkg_processes_names + [signal_names[i]]))
            config["ratio_numerator_nicks"].append("data")
            config["ratio_result_nicks"] = ["bkg_ratio"] + [
                x + "_ratio" for x in signal_names
            ] + ["data_ratio"]
            config["analysis_modules"].append("AddHistograms")
            config["add_nicks"] = [" ".join(bkg_processes_names)]
            config["add_result_nicks"] = ["background_uncertainty"]

            if args.blind or category > 99:
                for key in config.keys():
                    if isinstance(config[key], list):
                        if isinstance(config[key][0], str):
                            config[key] = [
                                x for x in config[key] if (not "data" in x)
                            ]
                            if "marker" in key:
                                config[key] = [
                                    x for x in config[key] if (x != "P")
                                ]
                        if key in [
                                "ratio_denominator_nicks", "scale_factors",
                                "colors"
                        ]:
                            config[key] = config[key][:-1]
                #config["legend_cols"] = 1
            # import pprint
            # pp = pprint.PrettyPrinter(indent=2)
            # pp.pprint(config)
            configs.append(config)

    higgsplot.HiggsPlotter(list_of_config_dicts=configs,
                           list_of_args_strings=[args.additional_arguments],
                           n_processes=args.num_processes)

    rfile.Close()
                        config["labels"][index] = os.path.join(
                            "%s_%s" %
                            (channel_renamings.get(channel, channel),
                             category_renamings.get(category, category)),
                            label_renamings.get(label, label))

                    config = uncertainty(config, name).get_config(shift)

                    if "PrintInfos" in config.get("analysis_modules", []):
                        config.get("analysis_modules", []).remove("PrintInfos")

                    harry_configs.append(config)
                    harry_args.append("-d %s --formats png pdf %s" %
                                      (args["input_dir"], args["args"]))

    higgs_plotter = higgsplot.HiggsPlotter(list_of_config_dicts=harry_configs,
                                           list_of_args_strings=harry_args,
                                           n_processes=args["n_processes"],
                                           n_plots=args["n_plots"])

    root_outputs = list(
        set([
            output
            for output in tools.flattenList(higgs_plotter.output_filenames)
            if output.endswith(".root")
        ]))
    command = "hadd -f %s %s" % (args["root_output"], " ".join(root_outputs))
    log.info(command)
    logger.subprocessCall(shlex.split(command))
    log.info("Merged ROOT output is saved to \"%s\"." % args["root_output"])
            plot_configs[-1]["files"] = [args["input"]]
            plot_configs[-1]["folders"] = [directory]
            plot_configs[-1]["x_expressions"] = background + signal + data
            plot_configs[-1]["nicks"] = background + (
                ["sig"] * len(signal)) + (["data"] * len(data))
            plot_configs[-1]["stacks"] = (["bkg"] * len(background)) + (
                ["sig"] * (1 if len(signal) > 0 else 0)) + ["data"]
            plot_configs[-1]["markers"] = (["hist"] * len(background)) + (
                ["line"] * (1 if len(signal) > 0 else 0)) + ["E"]
            plot_configs[-1]["ratio"] = args["ratio"]
            plot_configs[-1]["colors"] = [
                colors.get(x, "#000000")
                for index, x in enumerate(plot_configs[-1]["x_expressions"])
            ]
            plot_configs[-1]["labels"] = background + (
                ["Signal (%d)" % mass] *
                (1 if len(signal) > 0 else 0)) + ["Data"]
            plot_configs[-1]["legend"] = [0.75, 0.6]
            plot_configs[-1]["x_label"] = ""
            plot_configs[-1]["y_label"] = "Events"
            plot_configs[-1]["output_dir"] = os.path.join(
                "plots", "prefit",
                os.path.splitext(os.path.basename(args["input"]))[0])
            plot_configs[-1]["filename"] = directory + "_mH" + str(mass)

    higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs,
                           list_of_args_strings=[args["args"]],
                           n_processes=args["n_processes"],
                           n_plots=args["n_plots"])
	def prefit_postfit_plots(self, datacards_cbs, datacards_postfit_shapes, plotting_args=None, n_processes=1, signal_stacked_on_bkg=False, *args, **kwargs):
		if plotting_args is None:
			plotting_args = {}	

		base_path = reduce(lambda datacard1, datacard2: tools.longest_common_substring(datacard1, datacard2), datacards_cbs.keys())

		plot_configs = []
		bkg_plotting_order = ["ZTTPOSPOL", "ZTTNEGPOL", "ZTT", "ZLL", "ZL", "ZJ", "EWKZ", "TTTAUTAU", "TTT", "TTJJ", "TTJ", "TT", "VVT", "VVJ", "VV", "WJ", "W", "hww_gg125", "hww_qq125", "EWK", "QCD"]
		for level in ["prefit", "postfit"]:
			for index, (fit_type, datacards_postfit_shapes_dict) in enumerate(datacards_postfit_shapes.iteritems()):
				if (index == 0) or (level == "postfit"):
					for datacard, postfit_shapes in datacards_postfit_shapes_dict.iteritems():
						for category in datacards_cbs[datacard].cp().bin_set():
							stacked_processes = []
							if signal_stacked_on_bkg:
								stacked_processes.extend(datacards_cbs[datacard].cp().bin([category]).signals().process_set())
							stacked_processes.extend(datacards_cbs[datacard].cp().bin([category]).backgrounds().process_set())
							stacked_processes.sort(key=lambda process: bkg_plotting_order.index(process) if process in bkg_plotting_order else len(bkg_plotting_order))
							#stacked_processes = [process for process in bkg_plotting_order if datacards_cbs[datacard].cp().bin([category]).backgrounds().process([process]).GetRate() > 0.0]

							config = {}

							processes_to_plot = list(stacked_processes)
							# merge backgrounds from dictionary if provided
							if plotting_args.get("merge_backgrounds", False):
								if not "SumOfHistograms" in config.get("analysis_modules", []):
									config.setdefault("analysis_modules", []).append("SumOfHistograms")

								merge_backgrounds = plotting_args.get("merge_backgrounds", {})
								for new_background, backgrounds_to_merge in merge_backgrounds.iteritems():
									if new_background not in stacked_processes:
										backgrounds_to_remove = ""
										for background in backgrounds_to_merge:
											if background in stacked_processes:
												stacked_processes = [p + ("_noplot" if p == background else "") for p in stacked_processes]
												backgrounds_to_remove += background + "_noplot "
										config.setdefault("sum_nicks", []).append(backgrounds_to_remove)
										config.setdefault("sum_result_nicks", []).append(new_background)

								processes_to_plot = [p for p in stacked_processes if not "noplot" in p]

								for new_background in merge_backgrounds:
									if new_background not in stacked_processes:
										processes_to_plot.append(new_background)

								processes_to_plot.sort(key=lambda process: bkg_plotting_order.index(process) if process in bkg_plotting_order else len(bkg_plotting_order))

							config["files"] = [postfit_shapes]
							config["folders"] = [category+"_"+level]
							config["x_expressions"] = [p.strip("_noplot") for p in stacked_processes] + ["TotalSig"] + ["data_obs", "TotalBkg"]
							config["nicks"] = stacked_processes + ["TotalSig" + ("_noplot" if signal_stacked_on_bkg else "")] + ["data_obs", "TotalBkg" + ("_noplot" if signal_stacked_on_bkg else "")]
							config["stacks"] = (["stack"]*len(processes_to_plot)) + (["data"] if signal_stacked_on_bkg else ["sig", "data", "bkg_unc"])

							config["labels"] = [label.lower() for label in processes_to_plot + (["data_obs"] if signal_stacked_on_bkg else ["TotalSig", "data_obs", "TotalBkg"])]
							config["colors"] = [color.lower() for color in processes_to_plot + (["data_obs"] if signal_stacked_on_bkg else ["TotalSig", "data_obs", "TotalBkg"])]
							config["markers"] = (["HIST"]*len(processes_to_plot)) + (["E"] if signal_stacked_on_bkg else ["LINE", "E", "E2"])
							config["legend_markers"] = (["F"]*len(processes_to_plot)) + (["ELP"] if signal_stacked_on_bkg else ["L", "ELP", "F"])

							config["x_label"] = category.split("_")[0]+"_"+plotting_args.get("x_expressions", None)
							config["title"] = "channel_"+category.split("_")[0]
							config["energies"] = [13.0]
							config["lumis"] = [float("%.1f" % plotting_args.get("lumi", 1.0))]
							if plotting_args.get("era", False):
								config["year"] = plotting_args.get("era")
							config["legend"] = [0.7, 0.6, 0.9, 0.88]
							config["y_lims"] = [0.0]

							config["output_dir"] = os.path.join(os.path.dirname(datacard), "plots")
							config["filename"] = level+("_"+fit_type if level == "postfit" else "")+"_"+category
							if plotting_args.get("www", False):
								config["www"] = os.path.join(config["output_dir"].replace(base_path, plotting_args["www"]+"/"))

							if plotting_args.get("normalize", False):
								config.setdefault("analysis_modules", []).append("NormalizeByBinWidth")

							if plotting_args.get("ratio", False):
								if signal_stacked_on_bkg:
									if not "SumOfHistograms" in config.get("analysis_modules", []):
										config.setdefault("analysis_modules", []).append("SumOfHistograms")
									config.setdefault("sum_nicks", []).append("TotalBkg_noplot TotalSig_noplot")
									config.setdefault("sum_result_nicks", []).append("TotalBkg")

								if not "Ratio" in config.get("analysis_modules", []):
									config.setdefault("analysis_modules", []).append("Ratio")
								# add signal/bkg ratio first
								if plotting_args.get("add_soverb_ratio", False):
									if not "SumOfHistograms" in config.get("analysis_modules", []):
										config.setdefault("analysis_modules", []).append("SumOfHistograms")
									config.setdefault("sum_nicks", []).append("TotalBkg TotalSig")
									config.setdefault("sum_result_nicks", []).append("TotalSignalPlusBackground_noplot")
									config.setdefault("ratio_numerator_nicks", []).append("TotalSignalPlusBackground_noplot")
									config.setdefault("ratio_denominator_nicks", []).append("TotalBkg")
									config.setdefault("ratio_result_nicks", []).append("ratio_soverb")
								# now add data/bkg ratio
								config.setdefault("ratio_numerator_nicks", []).extend(["TotalBkg", "data_obs"])
								config.setdefault("ratio_denominator_nicks", []).extend(["TotalBkg"] * 2)
								config.setdefault("ratio_result_nicks", []).extend(["ratio_unc", "ratio"])
								config["ratio_denominator_no_errors"] = True

								if plotting_args.get("add_soverb_ratio", False):
									config.setdefault("colors", []).append("kRed")
									config.setdefault("markers", []).append("LINE")
									config.setdefault("legend_markers", []).append("L")
									config.setdefault("labels", []).append("")
									config.setdefault("stacks", []).append("ratio_soverb")
								config.setdefault("colors", []).extend(["totalbkg", "#000000"])
								config.setdefault("markers", []).extend(["E2", "E"])
								config.setdefault("legend_markers", []).extend(["F", "ELP"])
								config.setdefault("labels", []).extend([""] * 2)
								config.setdefault("stacks", []).extend(["unc", "ratio"])
								config["legend"] = [0.7, 0.4, 0.95, 0.83]
								config["subplot_grid"] = "True"
								config["y_subplot_lims"] = [0.5, 1.5]
								config["y_subplot_label"] = "Obs./Exp."

							# update ordering if backgrounds were merged
							if plotting_args.get("merge_backgrounds", False):
								config["nicks_whitelist"] = processes_to_plot + ["TotalSig" + ("_noplot" if signal_stacked_on_bkg else "")] + ["data_obs", "TotalBkg" + ("_noplot" if signal_stacked_on_bkg else "")]
								if plotting_args.get("ratio", False):
									if plotting_args.get("add_soverb_ratio", False):
										config["nicks_whitelist"].append("ratio_soverb")
									config["nicks_whitelist"].extend(["ratio_unc", "ratio"])

							plot_configs.append(config)

		if plotting_args.get("return_configs", False):
			return plot_configs
		else:
			# create result plots HarryPlotter
			return higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs, list_of_args_strings=[plotting_args.get("args", "")], n_processes=n_processes)
Exemplo n.º 23
0
                    plot_configs.append(config)

        hadd_commands.append("hadd -f {DST} {SRC}".format(
            DST=output_file, SRC=" ".join(tmp_output_files)))

    # 	# Delete existing output files
    # output_files = list(set([os.path.join(config["output_dir"], config["filename"]+".root") for config in plot_configs[:args.n_plots[0]]]))
    # for output_file in output_files:
    # 		if os.path.exists(output_file):
    # 			os.remove(output_file)
    # 			log.debug("Removed file \""+output_file+"\" before it is recreated again.")

    # Create input histograms with HarryPlotter
    if "inputs" in args.steps:
        higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs,
                               n_processes=args.n_processes,
                               n_plots=args.n_plots[0])
        if args.n_plots[0] != 0:
            tools.parallelize(_call_command,
                              hadd_commands,
                              n_processes=args.n_processes)

    # Update CombineHarvester with the yields and shapes
    datacards.extract_shapes(os.path.join(
        args.output_dir, input_root_filename_template.replace("$", "")),
                             bkg_histogram_name_template,
                             sig_histogram_name_template,
                             bkg_syst_histogram_name_template,
                             sig_syst_histogram_name_template,
                             update_systematics=True)
Exemplo n.º 24
0
        "_alphas_only_": plot_configs_alphas_only,
        "_scale_only_": plot_configs_scale_only
    }
    for category in args.categories:
        log.debug(category)
        for (key, value) in configs_dict.items():
            merge(value[category])
            if category != 'inclusive' and 'inclusive' in args.categories:
                value[category][0] = sample_settings.merge_configs(
                    value[category][0], value['inclusive'][0])
            log.debug(args.quantities[0] + key + str(category))
            value[category][0]["filename"] = args.samples[0] + args.quantities[
                0] + key + str(category)

            fout = open("merged" + key + str(category) + ".json", "w")
            fout.write(
                pprint.pformat(value[category][0]).replace("u'D",
                                                           "'D").replace(
                                                               "'", '"'))

    if log.isEnabledFor(logging.DEBUG):
        print "Addititonal args for the configuration files:", [args.args]
    # This is not working BECAUSE of the single quotes.
    #pprint.pprint([configs_dict.values()[0][0]])
    higgsplot.HiggsPlotter(
        list_of_config_dicts=[configs_dict["_all_"][args.categories[0]][0]],
        list_of_args_strings=[args.args],
        n_processes=args.n_processes,
        n_plots=args.n_plots)
    #higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs[args.categories[0]][0], list_of_args_strings=[args.args], n_processes=args.n_processes, n_plots=args.n_plots)
Exemplo n.º 25
0
def harry_do_your_job(config):
    higgsplot.HiggsPlotter(list_of_config_dicts=[config])
    output_files = list(set(output_files))

    # create input histograms with HarryPlotter
    if not args.batch:
        pool = Pool(cpu_count())

        for config in plot_configs:
            pool.apply_async(harry_do_your_job, args=(config, ))

        pool.close()
        pool.join()

    else:
        # create input histograms with HarryPlotter
        higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs,
                               n_processes=1,
                               n_plots=len(plot_configs),
                               batch="rwthcondor")

    tools.parallelize(_call_command, hadd_commands, n_processes=1)

    datacards.extract_shapes(os.path.join(
        args.output_dir, input_root_filename_template.replace("$", "")),
                             bkg_histogram_name_template,
                             sig_histogram_name_template,
                             bkg_syst_histogram_name_template,
                             sig_syst_histogram_name_template,
                             update_systematics=True)
    """
	datacards.add_bin_by_bin_uncertainties(
				processes=datacards.cb.cp().backgrounds().process_set(),
				add_threshold=0.05, merge_threshold=0.8, fix_norm=False
    w_gen = [wll_gen, whl_gen, whh_gen]
    w_reco = [wll_reco, whl_reco, whh_reco]

    plot_configs = []

    #comparing gen and reco
    for i in xrange(3):
        config = jsonTools.JsonDict(
            os.path.expandvars(
                "$CMSSW_BASE/src/HiggsAnalysis/KITHiggsToTauTau/data/plots/configs/reco_CP/recogenPhiStarCP.json"
            ))
        config["files"] = files[i]
        for j in xrange(3):
            config["filename"] = "recogenPhiStarCP_" + samples[
                i] + "_" + channels[j]
            config["directories"] = args.input
            config["folders"] = [folders[j]]
            config["texts"] = texts[j]
            config["weights"] = [w_gen[j] + "&&" + w_reco[j]]
            plot_configs.append(config.copy())

    if log.isEnabledFor(logging.DEBUG):
        import pprint
        pprint.pprint(plot_configs)

    higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs,
                           list_of_args_strings=[args.args],
                           n_processes=args.n_processes,
                           n_plots=args.n_plots)
					config["fill_styles"] = [3001, 0]
					config["legend"] = [0.75, 0.8]
					config["legend_markers"] = ["LF", "LP"]
					config["x_lims"] = [-5.0, 5.0]
					config["x_label"] = "Pull values"

					config["output_dir"] = os.path.join(os.path.dirname(datacard), "plots")
					config["filename"] = "pulls"
					if plotting_args.get("www", False):
						config["www"] = os.path.join(config["output_dir"].replace(base_path, plotting_args["www"]+"/"))

					plot_configs.append(config)

		# create result plots HarryPlotter
<<<<<<< HEAD
		return higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs, list_of_args_strings=[plotting_args.get("args", "")], n_processes=n_processes)		
	
	def nuisance_impacts(self, datacards_cbs, datacards_workspaces, n_processes=1, *args, **kwargs):
=======
		return higgsplot.HiggsPlotter(list_of_config_dicts=plot_configs, list_of_args_strings=[plotting_args.get("args", "")], n_processes=n_processes)

	def nuisance_impacts(self, datacards_cbs, datacards_workspaces, n_processes=1, *args):

>>>>>>> origin/master
		tmp_args = " ".join(args)
		for key, value in kwargs.items():
			higgs_mass = value if "higgs_mass" in key else "0"	
		
		commandsInitialFit = []
		commandsInitialFit.extend([[
				"combineTool.py -M Impacts -d {WORKSPACE} -m {MASS} --robustFit 1 --minimizerTolerance 0.1 --minimizerStrategy 0 --minimizerAlgoForMinos Minuit2,migrad --doInitialFit --allPars {ARGS}".format(