Beispiel #1
0
    def draw_syst(self, save_dir, results_dir, root_dir=None):
        """Draw all fits one-by-one

        Args:
            save_dir: directory where to save plots
            results_dir: where to find intermediate results of the multi trial
            root_dir: TDirectory where to save summary plots
        """

        gStyle.SetOptStat(0)
        gStyle.SetOptStat(0000)
        gStyle.SetPalette(1)
        gStyle.SetCanvasColor(0)
        gStyle.SetFrameFillColor(0)

        bins1_ranges = self.pars_factory.bins1_edges_low.copy()
        bins1_ranges.append(self.pars_factory.bins1_edges_up[-1])

        for (ibin1, ibin2), fit in self.syst_fits.items():
            if not fit:
                self.logger.warning(
                    "No systematic fit for bins (%i, %i). Skip...", ibin1,
                    ibin2)
                continue
            bin_id_match = self.pars_factory.bin_matching[ibin1]

            # Some variables set for drawing
            title = f"{self.pars_factory.bins1_edges_low[ibin1]:.1f} < #it{{p}}_{{T}} < " \
                    f"{self.pars_factory.bins1_edges_up[ibin1]:.1f}" \
                    f"(prob > {self.pars_factory.prob_cut_fin[bin_id_match]:.2f})"

            suffix_write = self.pars_factory.make_suffix(ibin1, ibin2)

            fit.results_path = os.path.join(
                results_dir, f"multi_trial_bin1_{ibin1}_bin2_{ibin2}.root")

            # Central fits
            canvas = TCanvas("fit_canvas", suffix_write, 1400, 800)
            fit.draw(canvas, title=title)

            if self.pars_factory.apply_weights is False:
                canvas.SaveAs(
                    make_file_path(save_dir, "multi_trial", "eps", None,
                                   suffix_write))
            else:
                canvas.SaveAs(
                    make_file_path(save_dir, "multi_trial_weights", "eps",
                                   None, suffix_write))

            if root_dir:
                root_dir.cd()
                canvas.Write(f"multi_trial_{suffix_write}")

            canvas.Close()
Beispiel #2
0
    def draw_fits(self, save_dir, root_dir=None):
        """
        Draw all fits one-by-one
        Args:
            save_dir: directory where to save plots
            root_dir: TDirectory where to save summary plots
        """
        gStyle.SetOptStat(0)
        gStyle.SetOptStat(0000)
        gStyle.SetPalette(1)
        gStyle.SetCanvasColor(0)
        gStyle.SetFrameFillColor(0)

        bins2 = self.get_bins2()
        bins1_ranges = self.pars_factory.bins1_edges_low.copy()
        bins1_ranges.append(self.pars_factory.bins1_edges_up[-1])
        n_bins1 = len(bins1_ranges) - 1

        # Summarize in mult histograms in pT bins
        yieldshistos = {ibin2: TH1F("hyields%d" % (ibin2), "", \
                n_bins1, array("d", bins1_ranges)) for ibin2 in bins2}
        means_histos = {ibin2:TH1F("hmeanss%d" % (ibin2), "", \
                n_bins1, array("d", bins1_ranges)) for ibin2 in bins2}
        sigmas_histos = {ibin2: TH1F("hsigmas%d" % (ibin2), "", \
                n_bins1, array("d", bins1_ranges)) for ibin2 in bins2}
        have_summary_pt_bins = []
        means_init_mc_histos = TH1F("hmeans_init_mc", "", n_bins1,
                                    array("d", bins1_ranges))
        sigmas_init_mc_histos = TH1F("hsigmas_init_mc", "", n_bins1,
                                     array("d", bins1_ranges))
        means_init_data_histos = TH1F("hmeans_init_data", "", n_bins1,
                                      array("d", bins1_ranges))
        sigmas_init_data_histos = TH1F("hsigmas_init_data", "", n_bins1,
                                       array("d", bins1_ranges))

        nx = 4
        ny = 2
        canvy = 533
        if n_bins1 > 12:
            nx = 5
            ny = 4
            canvy = 1200
        elif n_bins1 > 8:
            nx = 4
            ny = 3
            canvy = 800

        canvas_init_mc = TCanvas("canvas_init_mc", "MC", 1000, canvy)
        canvas_init_data = TCanvas("canvas_init_data", "Data", 1000, canvy)
        canvas_data = {ibin2: TCanvas("canvas_data%d" % (ibin2), "Data", 1000, canvy) \
                       for ibin2 in bins2}
        canvas_init_mc.Divide(nx, ny)
        canvas_init_data.Divide(nx, ny)

        for c in canvas_data.values():
            c.Divide(nx, ny)

        # Need to cache some object for which the canvas is only written after the loop...
        for (ibin1, ibin2), fit in self.central_fits.items():
            bin_id_match = self.pars_factory.bin_matching[ibin1]

            # Some variables set for drawing
            title = f"{self.pars_factory.bins1_edges_low[ibin1]:.1f} < #it{{p}}_{{T}} < " \
                    f"{self.pars_factory.bins1_edges_up[ibin1]:.1f}" \
                    f"(prob > {self.pars_factory.prob_cut_fin[bin_id_match]:.2f})"

            x_axis_label = "#it{M}_{inv} (GeV/#it{c}^{2})"
            n_sigma_signal = self.pars_factory.n_sigma_signal

            suffix_write = self.pars_factory.make_suffix(ibin1, ibin2)

            kernel = fit.kernel
            histo = fit.histo

            # Central fits
            y_axis_label = \
                    f"Entries/({histo.GetBinWidth(1) * 1000:.0f} MeV/#it{{c}}^{{2}})"
            canvas = TCanvas("fit_canvas", suffix_write, 700, 700)
            fit.draw(canvas,
                     sigma_signal=n_sigma_signal,
                     x_axis_label=x_axis_label,
                     y_axis_label=y_axis_label,
                     title=title)
            if self.pars_factory.apply_weights is False:
                canvas.SaveAs(
                    make_file_path(save_dir, "fittedplot", "eps", None,
                                   suffix_write))
            else:
                canvas.SaveAs(
                    make_file_path(save_dir, "fittedplotweights", "eps", None,
                                   suffix_write))
            canvas.Close()
            fit.draw(canvas_data[ibin2].cd(ibin1 + 1),
                     sigma_signal=n_sigma_signal,
                     x_axis_label=x_axis_label,
                     y_axis_label=y_axis_label,
                     title=title)

            if fit.success:
                # Only fill these summary plots in case of success
                yieldshistos[ibin2].SetBinContent(ibin1 + 1,
                                                  kernel.GetRawYield())
                yieldshistos[ibin2].SetBinError(ibin1 + 1,
                                                kernel.GetRawYieldError())

                means_histos[ibin2].SetBinContent(ibin1 + 1, kernel.GetMean())
                means_histos[ibin2].SetBinError(ibin1 + 1,
                                                kernel.GetMeanUncertainty())

                sigmas_histos[ibin2].SetBinContent(ibin1 + 1,
                                                   kernel.GetSigma())
                sigmas_histos[ibin2].SetBinError(ibin1 + 1,
                                                 kernel.GetSigmaUncertainty())

                # Residual plot
                c_res = TCanvas('cRes', 'The Fit Canvas', 800, 800)
                c_res.cd()
                h_pulls = histo.Clone(f"{histo.GetName()}_pull")
                h_residual_trend = histo.Clone(
                    f"{histo.GetName()}_residual_trend")
                h_pulls_trend = histo.Clone(f"{histo.GetName()}_pulls_trend")
                _ = kernel.GetOverBackgroundResidualsAndPulls( \
                        h_pulls, h_residual_trend, h_pulls_trend, \
                        self.pars_factory.fit_range_low[ibin1], \
                        self.pars_factory.fit_range_up[ibin1])
                h_residual_trend.Draw()
                c_res.SaveAs(
                    make_file_path(save_dir, "residual", "eps", None,
                                   suffix_write))
                c_res.Close()

            # Summary plots to be done only once per pT bin
            if ibin1 in have_summary_pt_bins:
                continue

            have_summary_pt_bins.append(ibin1)

            # Pre-fit MC
            suffix_write = self.pars_factory.make_suffix(
                ibin1, self.pars_factory.bins2_int_bin)

            pre_fit_mc = self.pre_fits_mc[ibin1]
            kernel = pre_fit_mc.kernel
            histo = pre_fit_mc.histo
            y_axis_label = \
                    f"Entries/({histo.GetBinWidth(1) * 1000:.0f} MeV/#it{{c}}^{{2}})"
            canvas = TCanvas("fit_canvas_mc_init", suffix_write, 700, 700)
            pre_fit_mc.draw(canvas,
                            x_axis_label=x_axis_label,
                            y_axis_label=y_axis_label,
                            title=title)

            canvas.SaveAs(
                make_file_path(save_dir, "fittedplot_integrated_mc", "eps",
                               None, suffix_write))
            canvas.Close()
            pre_fit_mc.draw(canvas_init_mc.cd(ibin1 + 1),
                            x_axis_label=x_axis_label,
                            y_axis_label=y_axis_label,
                            title=title)

            if pre_fit_mc.success:
                # Only fill these summary plots in case of success
                means_init_mc_histos.SetBinContent(ibin1 + 1,
                                                   kernel.GetParameter(1))
                means_init_mc_histos.SetBinError(ibin1 + 1,
                                                 kernel.GetParError(1))
                sigmas_init_mc_histos.SetBinContent(ibin1 + 1,
                                                    kernel.GetParameter(2))
                sigmas_init_mc_histos.SetBinError(ibin1 + 1,
                                                  kernel.GetParError(2))

            pre_fit_data = self.pre_fits_data[ibin1]
            kernel = pre_fit_data.kernel
            histo = pre_fit_data.histo

            # Pre-fit data
            y_axis_label = \
                    f"Entries/({histo.GetBinWidth(1) * 1000:.0f} MeV/#it{{c}}^{{2}})"
            canvas = TCanvas("fit_canvas_data_init", suffix_write, 700, 700)
            pre_fit_data.draw(canvas,
                              sigma_signal=n_sigma_signal,
                              x_axis_label=x_axis_label,
                              y_axis_label=y_axis_label,
                              title=title)
            canvas.SaveAs(
                make_file_path(save_dir, "fittedplot_integrated", "eps", None,
                               suffix_write))
            canvas.Close()
            pre_fit_data.draw(canvas_init_data.cd(ibin1 + 1),
                              sigma_signal=n_sigma_signal,
                              x_axis_label=x_axis_label,
                              y_axis_label=y_axis_label,
                              title=title)

            if pre_fit_data.success:
                # Only fill these summary plots in case of success
                means_init_data_histos.SetBinContent(ibin1 + 1,
                                                     kernel.GetMean())
                means_init_data_histos.SetBinError(ibin1 + 1,
                                                   kernel.GetMeanUncertainty())
                sigmas_init_data_histos.SetBinContent(ibin1 + 1,
                                                      kernel.GetSigma())
                sigmas_init_data_histos.SetBinError(
                    ibin1 + 1, kernel.GetSigmaUncertainty())

        canvas_init_mc.SaveAs(make_file_path(save_dir, "canvas_InitMC", "eps"))
        canvas_init_mc.Close()
        canvas_init_data.SaveAs(
            make_file_path(save_dir, "canvas_InitData", "eps"))
        canvas_init_data.Close()
        for ibin2 in bins2:
            suffix2 = f"ibin2_{ibin2}"
            canvas_data[ibin2].SaveAs(
                make_file_path(save_dir, "canvas_FinalData", "eps", None,
                               suffix2))
            if root_dir:
                root_dir.cd()
                yieldshistos[ibin2].Write()
                means_histos[ibin2].Write()
                sigmas_histos[ibin2].Write()
            #canvas_data[ibin2].Close()

        latex_bin2_var = self.ana_config["latexbin2var"]
        latex_hadron_name = self.ana_config["latexnamehadron"]
        # Plot some summary historgrams
        leg_strings = [f"{self.pars_factory.bins2_edges_low[ibin2]} #leq {latex_bin2_var} < " \
                       f"{self.pars_factory.bins2_edges_up[ibin2]}" for ibin2 in bins2]
        save_name = make_file_path(save_dir, "Yields", "eps", None,
                                   [self.case, self.ana_type])
        # Yields summary plot
        plot_histograms(
            [yieldshistos[ibin2] for ibin2 in bins2], True, True, leg_strings,
            "uncorrected yields", "#it{p}_{T} (GeV/#it{c})",
            f"Uncorrected yields {latex_hadron_name} {self.ana_type}",
            "mult. / int.", save_name)
        save_name = make_file_path(save_dir, "Means", "eps", None,
                                   [self.case, self.ana_type])
        # Means summary plot
        plot_histograms([means_histos[ibin2] for ibin2 in bins2], False, True,
                        leg_strings, "Means", "#it{p}_{T} (GeV/#it{c})",
                        "#mu_{fit} " + f"{latex_hadron_name} {self.ana_type}",
                        "mult. / int.", save_name)
        save_name = make_file_path(save_dir, "Sigmas", "eps", None,
                                   [self.case, self.ana_type])
        #Sigmas summary plot
        plot_histograms([sigmas_histos[ibin2]
                         for ibin2 in bins2], False, True, leg_strings,
                        "Sigmas", "#it{p}_{T} (GeV/#it{c})", "#sigma_{fit} " +
                        f"{latex_hadron_name} {self.ana_type}", "mult. / int.",
                        save_name)

        # Plot the initialized means and sigma for MC and data
        save_name = make_file_path(save_dir, "Means_mult_int", "eps", None,
                                   [self.case, self.ana_type])
        plot_histograms([means_init_mc_histos, means_init_data_histos], False,
                        False, ["MC", "data"], "Means of int. mult.",
                        "#it{p}_{T} (GeV/#it{c})",
                        "#mu_{fit} " + f"{latex_hadron_name} {self.ana_type}",
                        "", save_name)

        save_name = make_file_path(save_dir, "Sigmas_mult_int", "eps", None,
                                   [self.case, self.ana_type])
        plot_histograms([sigmas_init_mc_histos, sigmas_init_data_histos],
                        False, False, ["MC", "data"], "Sigmas of int. mult.",
                        "#it{p}_{T} (GeV/#it{c})", "#sigma_{fit} " +
                        f"{latex_hadron_name} {self.ana_type}", "", save_name)