示例#1
0
    def plot(self, norm_matrix=True, privateWork=False):

        # norm confusion matrix if activated
        if norm_matrix:
            new_matrix = np.empty((self.n_classes, self.n_classes),
                                  dtype=np.float64)
            for yit in range(self.n_classes):
                evt_sum = float(sum(self.confusion_matrix[yit, :]))
                for xit in range(self.n_classes):
                    new_matrix[yit, xit] = self.confusion_matrix[yit, xit] / (
                        evt_sum + 1e-9)

            self.confusion_matrix = new_matrix

        # initialize Histogram
        cm = setup.setup2DHistogram(matrix=self.confusion_matrix.T,
                                    ncls=self.n_classes,
                                    xtitle="predicted class",
                                    ytitle="true class",
                                    binlabel=self.event_classes)

        canvas = setup.draw2DHistOnCanvas(cm,
                                          "confusion matrix",
                                          self.event_category,
                                          self.ROCScore,
                                          privateWork=privateWork)
        setup.saveCanvas(canvas, self.plotdir + "/confusionMatrix.pdf")
示例#2
0
    def plot(self, ratio=False):
        for i, v in enumerate(self.variables):
            weights = self.sample.lumi_weights

            # define original distribution as background hist
            bkgHist = setup.setupHistogram(values=self.sample.input_values[:,
                                                                           i],
                                           weights=weights,
                                           nbins=self.nbins,
                                           bin_range=self.bin_range,
                                           color=ROOT.kRed,
                                           xtitle=str(v) + " input",
                                           ytitle=setup.GetyTitle(),
                                           filled=True)

            sigHist = setup.setupHistogram(
                values=self.sample.prediction_vector[:, i],
                weights=weights,
                nbins=self.nbins,
                bin_range=self.bin_range,
                color=ROOT.kBlack,
                xtitle=str(v) + " reco",
                ytitle=setup.GetyTitle(),
                filled=False)

            sigHist.SetLineWidth(3)
            plotOptions = {
                "ratio": ratio,
                "ratioTitle": "#frac{reco}{original}",
                "logscale": self.logscale
            }

            canvas = setup.drawHistsOnCanvas(sigHist,
                                             bkgHist,
                                             plotOptions,
                                             canvasName=str(v))

            # setup legend
            legend = setup.getLegend()

            legend.AddEntry(bkgHist, self.sample.label + " orig.", "F")
            legend.AddEntry(sigHist, self.sample.label + " reco.", "L")

            # draw legend
            legend.Draw("same")

            # add lumi and category to plot
            setup.printLumi(canvas, ratio=plotOptions["ratio"])
            setup.printCategoryLabel(canvas,
                                     self.event_category,
                                     ratio=plotOptions["ratio"])

            out_path = self.plotdir + "/reconstruction_" + str(v) + ".pdf"
            setup.saveCanvas(canvas, out_path)

        workdir = os.path.dirname(self.plotdir[:-1])
        cmd = "pdfunite " + str(self.plotdir) + "/reconstruction_*.pdf " + str(
            workdir) + "/variableReconstructions.pdf"
        print(cmd)
        os.system(cmd)
    def histVariables2D(self, vX, vY, plot_name, sample, cat):

        # get number of bins and binrange from config file
        binsX = binning.getNbins(vX)
        binsY = binning.getNbins(vY)
        rangeX = binning.getBinrange(vX)
        rangeY = binning.getBinrange(vY)

        # check if bin_range was found
        if not rangeX:
            maxValue = max(self.samples[sample].cut_data[cat][vX].values)
            minValue = min(self.samples[sample].cut_data[cat][vX].values)
            config_string = "variables[\""+vX+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue)
            with open("new_variable_configs.txt", "a") as f:
                f.write(config_string)
            rangeX = [minValue, maxValue]

        if not rangeY:
            maxValue = max(self.samples[sample].cut_data[cat][vY].values)
            minValue = min(self.samples[sample].cut_data[cat][vY].values)
            config_string = "variables[\""+vY+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue)
            with open("new_variable_configs.txt", "a") as f:
                f.write(config_string)
            rangeY = [minValue, maxValue]


        # fill hist
        weights = self.samples[sample].cut_data[cat]["weight"].values
        valuesX = self.samples[sample].cut_data[cat][vX].values
        valuesY = self.samples[sample].cut_data[cat][vY].values

        hist = setup.setupHistogram2D(
            valuesX     = valuesX,
            valuesY     = valuesY,
            weights     = weights,
            binsX       = binsX,
            binsY       = binsY,
            rangeX      = rangeX,
            rangeY      = rangeY,
            titleX      = vX,
            titleY      = vY)

        canvas = setup.drawHistOnCanvas2D(
            hist        = hist,
            canvasName  = vX+"_vs_"+vY,
            catLabel    = JTcut.getJTlabel(cat),
            sampleName  = sample)

        # add lumi and category to plot
        setup.printLumi(canvas, lumi = self.options["lumiScale"], twoDim = True)

        # save canvas
        setup.saveCanvas(canvas, plot_name)
示例#4
0
    def histVariable(self, variable, plot_name, cat):
        # get number of bins and binrange from config filea
        bins = binning.getNbins(variable)
        bin_range = binning.getBinrange(variable)

        # check if bin_range was found
        if not bin_range:
            maxValue = -999
            minValue = 999
            for key in self.samples:
                maxValue = max(
                    maxValue,
                    max(self.samples[key].cut_data[cat][variable].values))
                minValue = min(
                    minValue,
                    min(self.samples[key].cut_data[cat][variable].values))
            config_string = "variables[\"" + variable + "\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(
                minValue, maxValue)
            with open("new_variable_configs.txt", "a") as f:
                f.write(config_string)
            bin_range = [minValue, maxValue]

        bkgHists = []
        bkgLabels = []
        weightIntegral = 0

        # loop over bachgrounds and fill hists
        for sampleName in self.ordered_stack:
            sample = self.samples[sampleName]

            # get weights
            weights = sample.cut_data[cat]["weight"].values
            weightIntegral += sum(weights)

            # setup histogram
            hist = setup.setupHistogram(
                values=sample.cut_data[cat][variable].values,
                weights=weights,
                nbins=bins,
                bin_range=bin_range,
                color=sample.plotColor,
                xtitle=cat + "_" + sample.sampleName + "_" + variable,
                ytitle=setup.GetyTitle(),
                filled=True)

            bkgHists.append(hist)
            bkgLabels.append(sample.sampleName)

        sigHists = []
        sigLabels = []
        sigScales = []

        # if not background was added, the weight integral is equal to 0
        if weightIntegral == 0:
            self.options["scaleSignal"] = 1

        # loop over signals and fill hists
        for key in self.samples:
            sample = self.samples[key]
            if not sample.isSignal: continue

            # get weights
            weights = sample.cut_data[cat]["weight"].values

            # determine scale factor
            if self.options["scaleSignal"] == -1:
                scaleFactor = weightIntegral / (sum(weights) + 1e-9)
            else:
                scaleFactor = float(self.options["scaleSignal"])

            # setup histogram
            hist = setup.setupHistogram(
                values=sample.cut_data[cat][variable].values,
                weights=weights,
                nbins=bins,
                bin_range=bin_range,
                color=sample.plotColor,
                xtitle=cat + "_" + sample.sampleName + "_" + variable,
                ytitle=setup.GetyTitle(),
                filled=False)

            hist.Scale(scaleFactor)

            sigHists.append(hist)
            sigLabels.append(sample.sampleName)
            sigScales.append(scaleFactor)

        # init canvas
        canvas = setup.drawHistsOnCanvas(sigHists,
                                         bkgHists,
                                         self.options,
                                         canvasName=cat + "_" + variable)

        # setup legend
        legend = setup.getLegend()
        # add signal entries
        for iSig in range(len(sigHists)):
            legend.AddEntry(
                sigHists[iSig],
                sigLabels[iSig] + " x {:4.0f}".format(sigScales[iSig]), "L")
        # add background entries
        for iBkg in range(len(bkgHists)):
            legend.AddEntry(bkgHists[iBkg], bkgLabels[iBkg], "F")

        # draw loegend
        legend.Draw("same")

        # add lumi and category to plot
        setup.printLumi(canvas,
                        lumi=self.options["lumiScale"],
                        ratio=self.options["ratio"])
        setup.printCategoryLabel(canvas,
                                 JTcut.getJTlabel(cat),
                                 ratio=self.options["ratio"])

        # save canvas
        setup.saveCanvas(canvas, plot_name)
示例#5
0
    def histVariable(self, variable, plot_name, cat):
        histInfo = {}

        if variable in self.variableconfig.index:
            # get variable info from config file
            bins = int(self.variableconfig.loc[variable, 'numberofbins'])
            minValue = float(self.variableconfig.loc[variable, 'minvalue'])
            maxValue = float(self.variableconfig.loc[variable, 'maxvalue'])
            displayname = self.variableconfig.loc[variable, 'displayname']
            logoption = self.variableconfig.loc[variable, 'logoption']
        else:
            bins = 50
            maxValue = max([
                max(self.samples[sample].cut_data[cat][variable].values)
                for sample in self.samples
            ])
            minValue = min([
                min(self.samples[sample].cut_data[cat][variable].values)
                for sample in self.samples
            ])
            displayname = variable
            logoption = "-"

            config_string = "{},{},{},{},{},{}\n".format(
                variable, minValue, maxValue, bins, logoption, displayname)
            with open("new_variable_configs.csv", "a") as f:
                f.write(config_string)

        bin_range = [minValue, maxValue]
        if logoption == "x" or logoption == "X":
            logoption = True
        else:
            logoption = False

        histInfo["nbins"] = bins
        histInfo["range"] = bin_range

        bkgHists = []
        bkgLabels = []
        weightIntegral = 0

        # loop over backgrounds and fill hists
        for sampleName in self.ordered_stack:
            sample = self.samples[sampleName]

            # get weights
            weights = sample.cut_data[cat]["weight"].values
            # get values
            values = sample.cut_data[cat][variable].values

            #weights = [weights[i] for i in range(len(weights)) if not np.isnan(values[i])]
            #values =  [values[i]  for i in range(len(values))  if not np.isnan(values[i])]

            weightIntegral += sum(weights)
            # setup histogram
            hist = setup.setupHistogram(
                values=values,
                weights=weights,
                nbins=bins,
                bin_range=bin_range,
                color=sample.plotColor,
                xtitle=cat + "_" + sample.sampleName + "_" + variable,
                ytitle=setup.GetyTitle(self.options["privateWork"]),
                filled=sample.filled)
            bkgHists.append(hist)
            bkgLabels.append(sample.sampleName)

        sigHists = []
        sigLabels = []
        sigScales = []

        # if not background was added, the weight integral is equal to 0
        if weightIntegral == 0:
            self.options["scaleSignal"] = 0
        histInfo["bkgYield"] = weightIntegral

        # scale stack to one if lumiScale is set to zero
        if self.options["lumiScale"] == 0:
            for hist in bkgHists:
                hist.Scale(1. / weightIntegral)
            weightIntegral = 1.

        # loop over signals and fill hists
        for key in self.samples:
            sample = self.samples[key]
            if not sample.isSignal: continue

            # get weights
            weights = sample.cut_data[cat]["weight"].values
            # determine scale factor
            if self.options["scaleSignal"] == -1:
                scaleFactor = weightIntegral / (sum(weights) + 1e-9)
            elif self.options["scaleSignal"] == 0:
                scaleFactor = (1. / (sum(weights) + 1e-9))
            else:
                scaleFactor = float(self.options["scaleSignal"])

            # setup histogram
            hist = setup.setupHistogram(
                values=sample.cut_data[cat][variable].values,
                weights=weights,
                nbins=bins,
                bin_range=bin_range,
                color=sample.plotColor,
                xtitle=cat + "_" + sample.sampleName + "_" + variable,
                ytitle=setup.GetyTitle(),
                filled=sample.filled)

            hist.Scale(scaleFactor)

            sigHists.append(hist)
            sigLabels.append(sample.sampleName)
            sigScales.append(scaleFactor)

        # init canvas
        canvas = setup.drawHistsOnCanvas(sigHists,
                                         bkgHists,
                                         self.options,
                                         canvasName=variable,
                                         displayname=displayname,
                                         logoption=logoption)

        # setup legend
        legend = setup.getLegend()
        # add signal entriesa
        for iSig in range(len(sigHists)):
            labelstring = sigLabels[iSig]
            if not self.options["lumiScale"] == 0.:
                labelstring = sigLabels[iSig] + " x {:4.0f}".format(
                    sigScales[iSig])

            # add KS score to label if activated
            if self.options["KSscore"]:
                KSscore = setup.calculateKSscore(bkgHists[0], sigHists[iSig])
                labelstring = "#splitline{" + labelstring + "}{KSscore = %.3f}" % (
                    KSscore)
                histInfo["KSScore"] = KSscore

            legend.AddEntry(sigHists[iSig], labelstring, "L")

        # add background entries
        for iBkg in range(len(bkgHists)):
            legend.AddEntry(bkgHists[iBkg], bkgLabels[iBkg], "F")

        # draw loegend
        legend.Draw("same")

        # add lumi and category to plot
        setup.printLumi(canvas,
                        lumi=self.options["lumiScale"],
                        ratio=self.options["ratio"])
        setup.printCategoryLabel(canvas,
                                 JTcut.getJTlabel(cat),
                                 ratio=self.options["ratio"])
        if self.options["privateWork"]:
            setup.printPrivateWork(canvas, ratio=self.options["ratio"])

        # save canvas
        setup.saveCanvas(canvas, plot_name)

        return histInfo
    def histVariable2D(self, name, vX, vY, plot_name, cat):
        # get number of bins and binrange from config file
        binsX = binning.getNbins(vX)
        binsY = binning.getNbins(vY)
        rangeX = binning.getBinrange(vX)
        rangeY = binning.getBinrange(vY)

        # check if bin_range was found
        if not rangeX:
            maxValue = max([max(self.samples[sample].cut_data[cat][vX].values) for sample in self.samples])
            minValue = min([min(self.samples[sample].cut_data[cat][vX].values) for sample in self.samples])
            config_string = "variables[\""+vX+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue)
            with open("new_variable_configs.txt", "a") as f:
                f.write(config_string)
            rangeX = [minValue, maxValue]

        if not rangeY:
            maxValue = max([max(self.samples[sample].cut_data[cat][vY].values) for sample in self.samples])
            minValue = min([min(self.samples[sample].cut_data[cat][vY].values) for sample in self.samples])
            config_string = "variables[\""+vY+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue)
            with open("new_variable_configs.txt", "a") as f:
                f.write(config_string)
            rangeY = [minValue, maxValue]

        # init value lists
        weights = np.array([])
        valuesX = np.array([])
        valuesY = np.array([])
        for sample in self.samples:
            weights = np.append(weights, self.samples[sample].cut_data[cat]["weight"].values)
            valuesX = np.append(valuesX, self.samples[sample].cut_data[cat][vX].values)
            valuesY = np.append(valuesY, self.samples[sample].cut_data[cat][vY].values)

        hist = setup.setupHistogram2D(
            valuesX     = valuesX,
            valuesY     = valuesY,
            weights     = weights,
            binsX       = binsX,
            binsY       = binsY,
            rangeX      = rangeX,
            rangeY      = rangeY,
            titleX      = vX,
            titleY      = vY)

        canvas = setup.drawHistOnCanvas2D(
            hist        = hist,
            canvasName  = name,
            catLabel    = JTcut.getJTlabel(cat),
            sampleName  = name)

        # add lumi
        setup.printLumi(canvas, lumi = self.options["lumiScale"], twoDim = True)
        if self.options["privateWork"]: 
            setup.printPrivateWork(canvas, ratio = self.options["ratio"], twoDim = True)

        if self.options["getCorr"]:
            correlation = hist.GetCorrelationFactor()
            setup.printCorrelation(canvas, correlation)

        # save canvas
        setup.saveCanvas(canvas, plot_name)

        # plot distributions in 1D if activated
        if self.options["plot1D"]:
             # get averages
            bins = int((binsX + binsY)/2.)
            bin_range = [min(rangeX[0],rangeY[0]), max(rangeX[1],rangeY[1])]

            hX = setup.setupHistogram(
                    values      = valuesX,
                    weights     = weights,
                    nbins       = bins,
                    bin_range   = bin_range,
                    color       = ROOT.kBlack,
                    xtitle      = vX+"1D",
                    ytitle      = setup.GetyTitle(self.options["lumiScale"]),
                    filled      = False)

            if self.options["lumiScale"] == 0.:
                hXInt = hX.Integral()
                hX.Scale(1./hXInt)

            hY = setup.setupHistogram(
                    values      = valuesY,
                    weights     = weights,
                    nbins       = bins,
                    bin_range   = bin_range,
                    color       = ROOT.kRed,
                    xtitle      = vY+"1D",
                    ytitle      = setup.GetyTitle(self.options["lumiScale"]),
                    filled      = False)

            if self.options["lumiScale"] == 0.:
                hYInt = hY.Integral()
                hY.Scale(1./hYInt)

            # init canvas
            canvas = setup.drawHistsOnCanvas(
                hX, hY, self.options,
                canvasName = name)

            # setup legend
            legend = setup.getLegend()
            legend.AddEntry( hX, self.options["xName"], "L")

            labelY = self.options["yName"]
            # add KS score to label if activated
            if self.options["KSscore"]:
                KSscore = setup.calculateKSscore(hX, hY)
                labelY="#splitline{"+labelY+"}{KSscore = %.3f}"%(KSscore)
            legend.AddEntry( hY, labelY, "L")

            # draw loegend
            legend.Draw("same")

            # add lumi and category to plot
            setup.printLumi(canvas, lumi = self.options["lumiScale"], ratio = self.options["ratio"])
            setup.printCategoryLabel(canvas, JTcut.getJTlabel(cat), ratio = self.options["ratio"])
            if self.options["privateWork"]: 
                setup.printPrivateWork(canvas, ratio = self.options["ratio"])

            # save canvas
            setup.saveCanvas(canvas, plot_name.replace(".pdf","_1D.pdf"))
    def histVariable1D(self, sample, name, vX, vY, plot_name, cat):
        # get number of bins and binrange from config file
        binsX = binning.getNbins(vX)
        binsY = binning.getNbins(vY)
        rangeX = binning.getBinrange(vX)
        rangeY = binning.getBinrange(vY)

        # check if bin_range was found
        if not rangeX:
            maxValue = max(sample.cut_data[cat][vX].values)
            minValue = min(sample.cut_data[cat][vX].values)
            config_string = "variables[\""+vX+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue)
            with open("new_variable_configs.txt", "a") as f:
                f.write(config_string)
            rangeX = [minValue, maxValue]

        if not rangeY:
            maxValue = max(sample.cut_data[cat][vY].values)
            minValue = min(sample.cut_data[cat][vY].values)
            config_string = "variables[\""+vY+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue)
            with open("new_variable_configs.txt", "a") as f:
                f.write(config_string)
            rangeY = [minValue, maxValue]

        # get averages
        bins = int((binsX + binsY)/2.)
        bin_range = [min(rangeX[0],rangeY[0]), max(rangeX[1],rangeY[1])]

        weights = sample.cut_data[cat]["weight"].values
        valuesX = sample.cut_data[cat][vX].values
        valuesY = sample.cut_data[cat][vY].values

        hX = setup.setupHistogram(
                values      = valuesX,
                weights     = weights,
                nbins       = bins,
                bin_range   = bin_range,
                color       = ROOT.kBlack,
                xtitle      = cat+"_"+sample.sampleName+"_"+vX,
                ytitle      = setup.GetyTitle(self.options["lumiScale"]),
                filled      = False)

        hY = setup.setupHistogram(
                values      = valuesY,
                weights     = weights,
                nbins       = bins,
                bin_range   = bin_range,
                color       = ROOT.kRed,
                xtitle      = cat+"_"+sample.sampleName+"_"+vY,
                ytitle      = setup.GetyTitle(self.options["lumiScale"]),
                filled      = False)
        
        # init canvas
        canvas = setup.drawHistsOnCanvas(
            hX, hY, self.options,
            canvasName = "[{}] {}".format(sample.sampleName, name))

        # setup legend
        legend = setup.getLegend()
        legend.AddEntry( hX, self.options["xName"], "L")

        labelY = self.options["yName"]
        # add KS score to label if activated
        if self.options["KSscore"]:
            KSscore = setup.calculateKSscore(hX, hY)
            labelY="#splitline{"+labelY+"}{KSscore = %.3f}"%(KSscore)
        legend.AddEntry( hY, labelY, "L")

        # draw loegend
        legend.Draw("same")

        # add lumi and category to plot
        setup.printLumi(canvas, lumi = self.options["lumiScale"], ratio = self.options["ratio"])
        setup.printCategoryLabel(canvas, JTcut.getJTlabel(cat), ratio = self.options["ratio"])
        if self.options["privateWork"]:
            setup.printPrivateWork(canvas, ratio = self.options["ratio"])

        # save canvas
        setup.saveCanvas(canvas, plot_name)
示例#8
0
    def plot_mean(self, ratio=False):

        bkgHists = []
        bkgLabels = []
        weightIntegral = 0

        for sample in self.other_samples:
            weights = sample.lumi_weights
            values = sample.lossVector

            hist = setup.setupHistogram(values=values,
                                        weights=weights,
                                        nbins=self.nbins,
                                        bin_range=self.bin_range,
                                        color=setup.GetPlotColor(sample.label),
                                        xtitle=str(sample.label) + " loss",
                                        ytitle=setup.GetyTitle(),
                                        filled=True)

            # save hist
            bkgHists.append(hist)
            bkgLabels.append(sample.label)
            weightIntegral += sum(weights)

        # get sig histogram (trained sample)
        sig_weights = self.train_sample.lumi_weights

        sigHist = setup.setupHistogram(
            values=self.train_sample.lossVector,
            weights=sig_weights,
            nbins=self.nbins,
            bin_range=self.bin_range,
            color=setup.GetPlotColor(self.train_sample.label),
            xtitle=str(self.train_sample.label) + " loss",
            ytitle=setup.GetyTitle(),
            filled=False)
        sigHist.SetLineWidth(3)
        scaleFactor = weightIntegral / (sum(sig_weights) + 1e-9)
        sigHist.Scale(scaleFactor)

        plotOptions = {
            "ratio": ratio,
            "ratioTitle": "#frac{scaled Signal}{Background}",
            "logscale": self.logscale
        }

        canvas = setup.drawHistsOnCanvas(sigHist,
                                         bkgHists,
                                         plotOptions,
                                         canvasName=self.loss_function)

        # setup legend
        legend = setup.getLegend()

        for i, h in enumerate(bkgHists):
            legend.AddEntry(h, bkgLabels[i], "F")
        legend.AddEntry(
            sigHist,
            self.train_sample.label + " x {:4.0f}".format(scaleFactor), "L")

        # draw legend
        legend.Draw("same")

        # add lumi and category to plot
        setup.printLumi(canvas, ratio=plotOptions["ratio"])
        setup.printCategoryLabel(canvas,
                                 self.event_category,
                                 ratio=plotOptions["ratio"])

        workdir = os.path.dirname(self.plotdir[:-1])
        out_path = workdir + "/lossValues.pdf"
        setup.saveCanvas(canvas, out_path)
示例#9
0
    def plot(self, ratio=False):
        # generate one plot per output node
        for i, node_cls in enumerate(self.event_classes):
            print("\nPLOTTING OUTPUT NODE '" + str(node_cls)) + "'"
            nodeIndex = self.data.class_translation[node_cls]

            # get output values of this node
            out_values = self.prediction_vector[:, i]

            if self.printROCScore:
                # calculate ROC value for specific node
                nodeROC = roc_auc_score(self.signalFlag, out_values)

            # fill lists according to class
            bkgHists = []
            bkgLabels = []
            weightIntegral = 0

            sig_values = []
            sig_labels = []
            sig_weights = []

            # if non-train data plotting is enabled, add the histograms here
            if self.plot_nonTrainData:
                for sample in self.data.non_train_samples:
                    values = sample.prediction_vector[:, i]
                    filtered_values = [ values[k] for k in range(len(values)) \
                        if sample.predicted_classes[k] == nodeIndex]
                    filtered_weights = [ sample.lumi_weights[k] for k in range(len(values)) \
                        if sample.predicted_classes[k] == nodeIndex]
                    print("{} events in discriminator: {}\t(Integral: {})".
                          format(sample.label, len(filtered_values),
                                 sum(filtered_weights)))

                    if sample.signalSample:
                        sig_values.append(filtered_values)
                        sig_labels.append(sample.label)
                        sig_weights.append(filtered_weights)
                    else:
                        histogram = setup.setupHistogram(
                            values=filtered_values,
                            weights=filtered_weights,
                            nbins=self.nbins,
                            bin_range=self.bin_range,
                            color=setup.GetPlotColor(sample.label),
                            xtitle=str(sample.label) + " at " + str(node_cls) +
                            " node",
                            ytitle=setup.GetyTitle(),
                            filled=True)

                        bkgHists.append(histogram)
                        bkgLabels.append(sample.label)

            # loop over all classes to fill hists according to truth level class
            for j, truth_cls in enumerate(self.event_classes):
                classIndex = self.data.class_translation[truth_cls]

                # filter values per event class
                filtered_values = [ out_values[k] for k in range(len(out_values)) \
                    if self.data.get_test_labels(as_categorical = False)[k] == classIndex \
                    and self.predicted_classes[k] == nodeIndex]

                filtered_weights = [ self.data.get_lumi_weights()[k] for k in range(len(out_values)) \
                    if self.data.get_test_labels(as_categorical = False)[k] == classIndex \
                    and self.predicted_classes[k] == nodeIndex]

                print("{} events in discriminator: {}\t(Integral: {})".format(
                    truth_cls, len(filtered_values), sum(filtered_weights)))

                if j == self.signalIndex:
                    # signal histogram
                    sig_values.append(filtered_values)
                    sig_labels.append(str(truth_cls))
                    sig_weights.append(filtered_weights)
                else:
                    # background histograms
                    weightIntegral += sum(filtered_weights)

                    histogram = setup.setupHistogram(
                        values=filtered_values,
                        weights=filtered_weights,
                        nbins=self.nbins,
                        bin_range=self.bin_range,
                        color=setup.GetPlotColor(truth_cls),
                        xtitle=str(truth_cls) + " at " + str(node_cls) +
                        " node",
                        ytitle=setup.GetyTitle(),
                        filled=True)

                    bkgHists.append(histogram)
                    bkgLabels.append(truth_cls)

            sigHists = []
            scaleFactors = []
            for iSig in range(len(sig_labels)):
                # setup signal histogram
                sigHist = setup.setupHistogram(
                    values=sig_values[iSig],
                    weights=sig_weights[iSig],
                    nbins=self.nbins,
                    bin_range=self.bin_range,
                    color=setup.GetPlotColor(sig_labels[iSig]),
                    xtitle=str(sig_labels[iSig]) + " at " + str(node_cls) +
                    " node",
                    ytitle=setup.GetyTitle(),
                    filled=False)

                # set signal histogram linewidth
                sigHist.SetLineWidth(3)

                # set scalefactor
                scaleFactor = weightIntegral / (sum(sig_weights[iSig]) + 1e-9)
                sigHist.Scale(scaleFactor)
                sigHists.append(sigHist)
                scaleFactors.append(scaleFactor)

                figure_of_merit = get_FOM(sum(sig_weights[iSig]),
                                          weightIntegral)
                print("figure of merit for {}: {}".format(
                    sig_labels[iSig], figure_of_merit))

            plotOptions = {
                "ratio": ratio,
                "ratioTitle": "#frac{scaled Signal}{Background}",
                "logscale": self.logscale
            }
            canvas = setup.drawHistsOnCanvas(sigHists,
                                             bkgHists,
                                             plotOptions,
                                             canvasName=node_cls +
                                             " final discriminator")

            # setup legend
            legend = setup.getLegend()

            # add signal entry
            for i, h in enumerate(sigHists):
                legend.AddEntry(
                    h, sig_labels[i] + " x {:4.0f}".format(scaleFactors[i]),
                    "L")

            # add background entries
            for i, h in enumerate(bkgHists):
                legend.AddEntry(h, bkgLabels[i], "F")

            # draw legend
            legend.Draw("same")

            # add ROC score if activated
            if self.printROCScore:
                setup.printROCScore(canvas, nodeROC, plotOptions["ratio"])

            # add lumi and category to plot
            setup.printLumi(canvas, ratio=plotOptions["ratio"])
            setup.printCategoryLabel(canvas,
                                     self.event_category,
                                     ratio=plotOptions["ratio"])

            out_path = self.plotdir + "/finaldiscr_{}.pdf".format(node_cls)
            setup.saveCanvas(canvas, out_path)

        # add the histograms together
        workdir = os.path.dirname(self.plotdir[:-1])
        cmd = "pdfunite " + str(self.plotdir) + "/finaldiscr_*.pdf " + str(
            workdir) + "/discriminators.pdf"
        print(cmd)
        os.system(cmd)
示例#10
0
    def plot(self, ratio=False):
        # generate one plot per output node
        for i, node_cls in enumerate(self.event_classes):
            # get output values of this node
            out_values = self.prediction_vector[:, i]

            if self.printROCScore:
                # calculate ROC value for specific node
                nodeROC = roc_auc_score(self.signalFlag, out_values)

            # fill lists according to class
            bkgHists = []
            bkgLabels = []
            weightIntegral = 0

            # loop over all classes to fill hists according to truth level class
            for j, truth_cls in enumerate(self.event_classes):
                classIndex = self.data.class_translation[truth_cls]

                # filter values per event class
                filtered_values = [ out_values[k] for k in range(len(out_values)) \
                    if self.data.get_test_labels(as_categorical = False)[k] == classIndex \
                    and self.eventInCut[k]]

                filtered_weights = [ self.data.get_lumi_weights()[k] for k in range(len(out_values)) \
                    if self.data.get_test_labels(as_categorical = False)[k] == classIndex \
                    and self.eventInCut[k]]

                if j == self.signalIndex:
                    # signal histogram
                    sig_values = filtered_values
                    sig_label = str(truth_cls)
                    sig_weights = filtered_weights
                else:
                    # background histograms
                    weightIntegral += sum(filtered_weights)

                    histogram = setup.setupHistogram(
                        values=filtered_values,
                        weights=filtered_weights,
                        nbins=self.nbins,
                        bin_range=self.bin_range,
                        color=setup.GetPlotColor(truth_cls),
                        xtitle=str(truth_cls) + " at " + str(node_cls) +
                        " node",
                        ytitle=setup.GetyTitle(),
                        filled=True)

                    bkgHists.append(histogram)
                    bkgLabels.append(truth_cls)
            # setup signal histogram
            sigHist = setup.setupHistogram(values=sig_values,
                                           weights=sig_weights,
                                           nbins=self.nbins,
                                           bin_range=self.bin_range,
                                           color=setup.GetPlotColor(sig_label),
                                           xtitle=str(sig_label) + " at " +
                                           str(node_cls) + " node",
                                           ytitle=setup.GetyTitle(),
                                           filled=False)
            # set signal histogram linewidth
            sigHist.SetLineWidth(3)

            # set scalefactor
            scaleFactor = weightIntegral / (sum(sig_weights) + 1e-9)
            sigHist.Scale(scaleFactor)

            plotOptions = {
                "ratio": ratio,
                "ratioTitle": "#frac{scaled Signal}{Background}",
                "logscale": self.logscale
            }
            canvas = setup.drawHistsOnCanvas(sigHist,
                                             bkgHists,
                                             plotOptions,
                                             canvasName=node_cls + " node")

            # setup legend
            legend = setup.getLegend()

            # add signal entry
            legend.AddEntry(sigHist,
                            sig_label + " x {:4.0f}".format(scaleFactor), "L")

            # add background entries
            for i, h in enumerate(bkgHists):
                legend.AddEntry(h, bkgLabels[i], "F")

            ## scale signal Histogram
            #sigHist.Scale( scaleFactor )

            # draw legend
            legend.Draw("same")

            # add ROC score if activated
            if self.printROCScore:
                setup.printROCScore(canvas, nodeROC, plotOptions["ratio"])

            # add lumi and category to plot
            setup.printLumi(canvas, ratio=plotOptions["ratio"])
            setup.printCategoryLabel(canvas,
                                     self.event_category,
                                     ratio=plotOptions["ratio"])

            out_path = self.plotdir + "/outputNode_{}.pdf".format(node_cls)
            setup.saveCanvas(canvas, out_path)

        # add the histograms together
        workdir = os.path.dirname(self.plotdir[:-1])
        cmd = "pdfunite " + str(self.plotdir) + "/outputNode_*.pdf " + str(
            workdir) + "/outputNodes.pdf"
        print(cmd)
        os.system(cmd)