示例#1
0
    def _save_templates(self):
        """
        Save loaded channels in output ROOT file
        
        Output file will be updated. All channels will be saved or only those,
        that are specified with --savechannels option.

        The histogram naming convention is as follows:

            [analysis]_[plot]__[channel][__[systematics]]

        where
        
            analysis    analysis channel: mu, el
            plot        plot name, e.g. mttbar
            channel     channel name: ttbar, zjets, wjets, singletop, etc.
            systematics source of the systematic error, e.g. jes__plus

        """

        # Make sure required plot is loaded
        channels = self.loader.plots.get("/mttbar_after_htlep")
        if not channels:
            raise RuntimeError("mttbar_after_htlep is not loaded")

        # format string has different format with(-out) systematics
        format_string = str(self.theta_prefix) + "_mttbar__{channel}"
        if self.suffix:
            format_string += self.suffix

        with topen(self.output_filename, "update"):
            # save only those channels that are supported or specified by user
            for channel_type, channel in channels.items():
                if channel_type not in self.channel_names or (
                    self.save_channels and channel_type not in self.save_channels
                ):

                    continue

                # All Zprimes are originally normalized to 5pb. Scale to 1pb
                if channel_type.startswith("zprime"):
                    # channel.hist.Scale(1. / 5)
                    channel.hist.Scale(2.0)

                name = format_string.format(channel=self.channel_names[channel_type])

                hist = channel.hist.Clone(name)
                hist.SetTitle(channel.hist.GetTitle())

                hist.Write(name)
示例#2
0
    def _plot(self):
        canvases = []

        '''
        loop over plots and draw them:

            1. all background channels stacked (MC channels + QCD)
            2. background error band (MC + QCD)
            3. data with errors
            4. ratio of data over background
        '''

        # loop over plots
        for plot_name, channels in self.loader.plots.items():
            background = None
            if "mc" in channels:
                background = channels["mc"].hist.Clone()

            if "qcd" in channels:
                if not background:
                    background = channels["qcd"].hist.Clone()
                else:
                    background.Add(channels["qcd"].hist)

            if not background:
                print("background is not avilable for", plot_name,
                      file=sys.stderr)

                continue

            background = calculate_efficiency(background)
            obj = self._create_obj("canvas_bg")
            obj.hist.append(background)
            background.Draw('hist 9')
            obj.legend.AddEntry(background, "background", "l")
            obj.legend.Draw('9')
            canvases.append(obj)

            bg_sqrt = background.Clone()
            sqrt_hist(bg_sqrt)
            obj = self._create_obj("canvas_sqrt_bg")
            obj.hist.append(bg_sqrt)
            bg_sqrt.Draw('hist 9')
            obj.legend.AddEntry(bg_sqrt, "sqrt(background)", "l")
            obj.legend.Draw('9')

            canvases.append(obj)

            # process Z'
            obj = None
            for channel_type, template in channels.items():
                if not channel_type.startswith("zp"):
                    continue

                hist = calculate_efficiency(template.hist)
                hist.SetFillStyle(0)

                obj = self._create_obj("canvas_chi2_{0}".format(channel_type))

                '''
                if '1000' in channel_type:
                    hist.Print('all')
                    background.Print('all')
                '''

                ratio = signal_over_background(hist, background,
                                               title="#frac{s}{b}")
                ratio.SetMaximum(5)
                ratio.SetMinimum(0.5)
                obj.hist.append(ratio)
                obj.legend.AddEntry(hist, channel_type, "l")

                ratio.Draw('hist 9')
                obj.legend.Draw('9')

                obj.canvas.cd(2)
                ratio = signal_over_background(hist, bg_sqrt,
                                               title="#frac{s}{#sqrt{b}}")
                obj.hist.append(ratio)

                ratio.SetMaximum(1.5)
                ratio.SetMinimum(0.5)
                ratio.Draw('hist 9')
                obj.legend.Draw('9')

                canvases.append(obj)

        return canvases
示例#3
0
    def _plot(self):
        canvases = []

        '''
        loop over plots and draw them:

            1. all background channels stacked (MC channels + QCD)
            2. background error band (MC + QCD)
            3. data with errors
            4. ratio of data over background
        '''

        # loop over plots
        for plot_name, channels in self.loader.plots.items():
            # process Z'
            obj = None
            for channel_type, template in channels.items():
                if not channel_type.startswith("zp"):
                    continue

                hist = calculate_efficiency(template.hist)
                hist.SetFillStyle(0)
                hist.SetMaximum(1.2)
                hist.SetMinimum(0)
                hist.GetXaxis().SetRangeUser(0, 20)

                if not obj:
                    obj = self._create_obj("canvas_signal_chi2")
                    hist.Draw('hist 9')
                else:
                    hist.Draw("hist 9 same")

                obj.legend.AddEntry(hist, channel_type, "l")

                obj.hist.append(hist)

            if obj:
                obj.legend.Draw('9')

                for label in obj.labels:
                    label.draw()

                canvases.append(obj)

            # take care of background
            obj = None
            for channel_type, template in channels.items():
                if (channel_type.startswith("zp") or
                    channel_type not in set(channels.keys()) -
                                        set(["data", "mc"])):
                    continue

                hist = calculate_efficiency(template.hist)
                hist.SetFillStyle(0)
                hist.SetMaximum(1.2)
                hist.SetMinimum(0)
                hist.GetXaxis().SetRangeUser(0, 20)

                if not obj:
                    obj = self._create_obj("canvas_background_chi2")
                    hist.Draw('hist 9')
                else:
                    hist.Draw("hist 9 same")

                obj.legend.AddEntry(hist, channel_type, "l")

                obj.hist.append(hist)

            if obj:
                obj.legend.Draw('9')

                for label in obj.labels:
                    label.draw()

                canvases.append(obj)

        return canvases
示例#4
0
    def _plot(self):
        canvases = []

        htop_1jet = self.loader.plots["/htop_1jet/mass"]
        htop_2jet = self.loader.plots["/htop_2jet/mass"]
        htop_3jet = self.loader.plots["/htop_3jet/mass"]

        # process Z'
        for channel_type, template in htop_1jet.items():
            if (not channel_type.startswith("zp") and
                not channel_type.startswith("rsg")):
                continue

            print(channel_type)

            h1 = template.hist
            h2 = htop_2jet[channel_type].hist
            h3 = htop_3jet[channel_type].hist

            for hist in h1, h2, h3:
                hist.SetFillStyle(0)
                hist.SetLineStyle(1)
                hist.Rebin(5)
                hist.GetYaxis().SetTitle("event yield / {0:.1f} GeV/c^".format(
                                         hist.GetBinWidth(1)) +
                                         "{2}")
                hist.GetXaxis().SetTitle("M_{htop} [GeV/c^{2}]")
                hist.SetNdivisions(5, "x")
                hist.SetNdivisions(3, "y")

            h1.SetLineColor(ROOT.kBlack)
            h2.SetLineColor(ROOT.kRed + 1)
            h3.SetLineColor(ROOT.kGreen + 1)

            canvas = ROOT.TCanvas()
            canvas.SetWindowSize(640, 640)
            canvas.SetName(channel_type)
            #canvas.Divide(2, 2)

            for pad in range(1, 2):
                pad = canvas.cd(pad)
                pad.SetRightMargin(5)
                pad.SetBottomMargin(0.15)

            for key, h in enumerate([h1, h2, h3], 1):
                print("{0} jets: {1:>4.1f}".format(key, h.Integral()))

            hists = [h.Clone() for h in (h1, h2, h3) if h.Integral()]
            
            is_drawn = False
            for h in hists:
                h.Scale(1 / h.Integral())
                if is_drawn:
                    h.Draw("hist 9 same")
                else:
                    h.Draw("hist 9")
                    is_drawn = True

            hists[0].SetMaximum(0.2)
            canvas.hists = hists

            legend = ROOT.TLegend(.65, .7, .85, .85)
            legend.SetHeader(channel_type)
            legend.SetMargin(0.12);
            legend.SetTextSize(0.03);
            legend.SetFillColor(10);
            legend.SetBorderSize(0);

            legend.AddEntry(h1, "htop 1 jets", "l")
            legend.AddEntry(h2, "htop 2 jets", "l")
            legend.AddEntry(h3, "htop 3+jets", "l")
            legend.Draw("9")

            canvas.legend = legend
            canvas.Update()
            
            canvases.append(canvas)

            print()

        return canvases