Example #1
0
    def _save2d(self, outdir, systematics=None):
        bkg_sum = self._get_sum(self._plotconfig['backgrounds'])
        signals = zip((cfg.keys()[0] for cfg in self._plotconfig['signals']), self._get_signals())

        for label, hist in signals + [('background', bkg_sum)]:
            self._add_binlabels(hist)
            canvas = r.TCanvas(self.__name + label, self.__name, 600, 600)

            canvas.SetTopMargin(.18)
            canvas.SetLeftMargin(0.13)
            canvas.SetRightMargin(0.14)

            stylish.setup_upper_axis(hist, scale=False, is2d=True)
            stylish.draw_channel_info(canvas, plot_ratio=False)
            hist.Draw("COLZ")

            subdir = os.path.dirname(os.path.join(outdir, self.__name))
            if not os.path.exists(subdir) and subdir != '':
                os.makedirs(subdir)

            for fmt in "pdf tex".split():
                canvas.SaveAs(os.path.join(outdir, "{0}_{1}.{2}".format(self.__name, label, fmt)))
                canvas.SetLogz()
                canvas.SaveAs(os.path.join(outdir, "{0}_{1}_log.{2}".format(self.__name, label, fmt)))
                canvas.SetLogz(False)
Example #2
0
    def _save2d(self, config, outdir):
        bkg_sum = self._get_background_sum(config)
        signals = zip((cfg.keys()[0] for cfg in config['signals']), self._get_signals(config))

        for label, hist in signals + [('background', bkg_sum)]:
            canvas = r.TCanvas(self.__name + label, self.__name, 600, 600)

            canvas.SetTopMargin(.18)
            canvas.SetLeftMargin(0.13)
            canvas.SetRightMargin(0.14)

            stylish.setup_upper_axis(hist, scale=False, is2d=True)
            hist.Draw("COLZ")

            subdir = os.path.dirname(os.path.join(outdir, self.__name))
            if not os.path.exists(subdir) and subdir != '':
                os.makedirs(subdir)
            canvas.SaveAs(os.path.join(outdir, "{0}_{1}.pdf".format(self.__name, label)))
Example #3
0
    def _save2d(self, config, outdir):
        bkg_sum = self._get_background_sum(config)
        signals = zip((cfg.keys()[0] for cfg in config['signals']),
                      self._get_signals(config))

        for label, hist in signals + [('background', bkg_sum)]:
            canvas = r.TCanvas(self.__name + label, self.__name, 600, 600)

            canvas.SetTopMargin(.18)
            canvas.SetLeftMargin(0.13)
            canvas.SetRightMargin(0.14)

            stylish.setup_upper_axis(hist, scale=False, is2d=True)
            hist.Draw("COLZ")

            subdir = os.path.dirname(os.path.join(outdir, self.__name))
            if not os.path.exists(subdir) and subdir != '':
                os.makedirs(subdir)
            canvas.SaveAs(
                os.path.join(outdir, "{0}_{1}.pdf".format(self.__name, label)))
Example #4
0
    def _save1d(self, config, outdir):
        min_y = 0.002
        max_y = min_y
        scale = 1.15
        factor = config.get("scale factor", "auto")

        canvas = r.TCanvas(self.__name, self.__name, 600, 700)
        canvas.Divide(1, 2)

        stylish.setup_upper_pad(canvas.GetPad(1))
        stylish.setup_lower_pad(canvas.GetPad(2))

        args = list(self.__args)
        args[0] += "_base"
        base_histo = self.__class(*args)
        stylish.setup_upper_axis(base_histo)

        canvas.cd(1)

        bkg_sum = self._get_background_sum(config)
        bkg_stack = self._get_backgrounds(config)
        bkg_stack.Draw()

        signals = self._get_signals(config)
        collisions = self._get_data(config)

        if factor == "auto":
            factor = self._get_scale_factor(bkg_sum, signals)

        if config.get("legend", True):
            scale = 1.175 + 0.05 * (math.ceil(
                len(config['backgrounds'] + config.get('data', [])) / 3. + 1) +
                                    math.ceil(len(config['signals']) / 3.))

        max_y = scale * max([bkg_stack.GetMaximum()] +
                            [factor * h.GetMaximum() for h in signals])

        if max_y == 0:
            return

        base_histo.GetYaxis().SetRangeUser(min_y, max_y)
        base_histo.Draw("hist")

        bkg_stack.SetMinimum(min_y)
        bkg_stack.SetMaximum(max_y)
        bkg_stack.Draw("hist same")

        for sig in signals:
            sig.Scale(factor)
            sig.DrawCopy("hist same")

        for data in collisions:
            data.DrawCopy("E1 P same")

        bkg_sum.Draw("E2 same")

        base_histo.Draw("axis same")

        # need to keep legend in memory, otherwise legend boxes are not
        # drawn (thank you, ROOT)
        legend = None
        if config.get("legend", True):
            legend = self._add_legend(config, factor)

        canvas.cd(2)

        lower = base_histo.Clone()
        stylish.setup_lower_axis(lower)

        lower.Draw("axis")

        err = self._draw_ratio(config)

        lower.Draw("axis same")

        line = r.TLine()
        line.SetLineColor(1)
        line.SetLineWidth(1)
        line.DrawLineNDC(
            r.gPad.GetLeftMargin(),
            r.gPad.GetBottomMargin() + (1 / stylish.ratio_plot_max) *
            (1 - r.gPad.GetBottomMargin() - r.gPad.GetTopMargin()),
            1 - r.gPad.GetRightMargin(),
            r.gPad.GetBottomMargin() + (1 / stylish.ratio_plot_max) *
            (1 - r.gPad.GetBottomMargin() - r.gPad.GetTopMargin()))

        subdir = os.path.dirname(os.path.join(outdir, self.__name))
        if not os.path.exists(subdir) and subdir != '':
            os.makedirs(subdir)
        canvas.SaveAs(os.path.join(outdir, self.__name + ".pdf"))
Example #5
0
    def _save1d(self, outdir, systematics=None):
        min_y = 0.002
        max_y = min_y
        scale = 1.05
        factor = self._plotconfig.get("scale factor", "auto")
        split = self._plotconfig.get("show ratio", True)

        height = 700
        if not split:
            height = int(height * stylish.single)
        canvas = r.TCanvas(self.__name, self.__name, 600, height)

        if split:
            canvas.Divide(1, 2)
            stylish.setup_upper_pad(canvas.GetPad(1))
            stylish.setup_lower_pad(canvas.GetPad(2))
        else:
            canvas.Divide(1, 1)
            stylish.setup_pad(canvas.GetPad(1))

        args = list(self.__args)
        args[0] += "_base"
        base_histo = self.__class(*args)
        self._add_binlabels(base_histo)

        if self.__labels:
            if isinstance(self.__labels[0], list) or isinstance(self.__labels[0], tuple):
                for n, label in enumerate(self.__labels[0], 1):
                    base_histo.GetXaxis().SetBinLabel(n, label)
                for n, label in enumerate(self.__labels[1], 1):
                    base_histo.GetYaxis().SetBinLabel(n, label)
            else:
                for n, label in enumerate(self.__labels, 1):
                    base_histo.GetXaxis().SetBinLabel(n, label)

        stylish.setup_upper_axis(base_histo, split=split)
        stylish.draw_channel_info(canvas.GetPad(1), plot_ratio=split)

        canvas.cd(1)

        bkg_sum = self._get_sum(self._plotconfig["backgrounds"])
        err_abs, err_rel = self._get_errors(self._plotconfig["backgrounds"], systematics)
        bkg_stack = self._get_backgrounds()
        bkg_stack.Draw()

        signals = self._get_signals(systematics)
        collisions = self._get_data()

        if factor == "auto":
            factor = self._get_scale_factor(bkg_sum, signals)
        elif factor == "norm":
            factor = 1.
            self._normalize_to_unity(signals)
            base_histo.GetYaxis().SetTitle("")

        if self._plotconfig.get("legend", True):
            scale += 0.05 * (math.ceil(len(self._plotconfig['backgrounds'] + self._plotconfig.get('data', [])) / 4. + 1)
                             + len(self._plotconfig['signals']))

        max_y = scale * self._get_maximum(err_abs, factor, signals, collisions)

        if max_y == 0:
            logging.warning("empty plot: {0}".format(self.__name))
            return

        base_histo.GetYaxis().SetRangeUser(min_y, max_y)
        base_histo.Draw("hist")

        err_abs.SetFillColorAlpha(r.kGray, .75)
        # err_abs.SetFillStyle(3003)
        err_abs.Draw("2 same")

        bkg_stack.SetMinimum(min_y)
        bkg_stack.SetMaximum(max_y)
        bkg_stack.Draw("hist same")

        for sig in signals:
            if isinstance(sig, r.TH1):
                sig.Scale(factor)
                sig.DrawCopy("hist same")
            else:
                sig.Apply(r.TF2("f", "y*{}".format(factor)))
                sig.Draw("2 same")

        if not self.__blind:
            for data in collisions:
                data.DrawCopy("E1 P same")

        base_histo.Draw("axis same")

        # need to keep legend in memory, otherwise legend boxes are not
        # drawn (thank you, ROOT)
        legend = None
        if self._plotconfig.get("legend", True):
            legend = self._add_legend(factor)

        if split:
            canvas.cd(2)
            err, rel_err = self._draw_ratio(base_histo, err_rel)

        subdir = os.path.dirname(os.path.join(outdir, self.__name))
        if not os.path.exists(subdir) and subdir != '':
            os.makedirs(subdir)

        canvas.SaveAs(os.path.join(outdir, self.__name + ".pdf"))
        canvas.SaveAs(os.path.join(outdir, self.__name + ".tex"))
        canvas.GetPad(1).SetLogy()
        base_histo.GetYaxis().SetRangeUser(min_y, max_y * 20)
        canvas.SaveAs(os.path.join(outdir, self.__name + "_log.pdf"))
        canvas.SaveAs(os.path.join(outdir, self.__name + "_log.tex"))

        if legend:
            del legend
Example #6
0
    def _save1d(self, config, outdir):
        min_y = 0.002
        max_y = min_y
        scale = 1.15
        factor = config.get("scale factor", "auto")

        canvas = r.TCanvas(self.__name, self.__name, 600, 700)
        canvas.Divide(1, 2)

        stylish.setup_upper_pad(canvas.GetPad(1))
        stylish.setup_lower_pad(canvas.GetPad(2))

        args = list(self.__args)
        args[0] += "_base"
        base_histo = self.__class(*args)
        stylish.setup_upper_axis(base_histo)

        canvas.cd(1)

        bkg_sum = self._get_background_sum(config)
        bkg_stack = self._get_backgrounds(config)
        bkg_stack.Draw()

        signals = self._get_signals(config)
        collisions = self._get_data(config)

        if factor == "auto":
            factor = self._get_scale_factor(bkg_sum, signals)

        if config.get("legend", True):
            scale = 1.175 + 0.05 * (
                    math.ceil(len(config['backgrounds'] + config.get('data', [])) / 3. + 1)
                    + math.ceil(len(config['signals']) / 3.))

        max_y = scale * max([bkg_stack.GetMaximum()] + [factor * h.GetMaximum() for h in signals])

        if max_y == 0:
            return

        base_histo.GetYaxis().SetRangeUser(min_y, max_y)
        base_histo.Draw("hist")

        bkg_stack.SetMinimum(min_y)
        bkg_stack.SetMaximum(max_y)
        bkg_stack.Draw("hist same")

        for sig in signals:
            sig.Scale(factor)
            sig.DrawCopy("hist same")

        for data in collisions:
            data.DrawCopy("E1 P same")

        bkg_sum.Draw("E2 same")

        base_histo.Draw("axis same")

        # need to keep legend in memory, otherwise legend boxes are not
        # drawn (thank you, ROOT)
        legend = None
        if config.get("legend", True):
            legend = self._add_legend(config, factor)

        canvas.cd(2)

        lower = base_histo.Clone()
        stylish.setup_lower_axis(lower)

        lower.Draw("axis")

        err = self._draw_ratio(config)

        lower.Draw("axis same")

        line = r.TLine()
        line.SetLineColor(1)
        line.SetLineWidth(1)
        line.DrawLineNDC(
                r.gPad.GetLeftMargin(),
                r.gPad.GetBottomMargin() +
                    (1 / stylish.ratio_plot_max) *
                    (1 - r.gPad.GetBottomMargin() - r.gPad.GetTopMargin()),
                1 - r.gPad.GetRightMargin(),
                r.gPad.GetBottomMargin() +
                    (1 / stylish.ratio_plot_max) *
                    (1 - r.gPad.GetBottomMargin() - r.gPad.GetTopMargin()))

        subdir = os.path.dirname(os.path.join(outdir, self.__name))
        if not os.path.exists(subdir) and subdir != '':
            os.makedirs(subdir)
        canvas.SaveAs(os.path.join(outdir, self.__name + ".pdf"))