def __init__(self,
                 opts,
                 config,
                 dirname,
                 luminosity,
                 observation,
                 datasetGroups,
                 verbose=False):
        self._validateDatacard(config)
        self._config = config
        self._verbose = verbose
        self._opts = opts
        self._dirname = dirname
        self._luminosity = luminosity
        self._observation = observation
        self._datasetGroups = datasetGroups

        # Define label options
        myStyle = tdrstyle.TDRStyle()
        myStyle.setOptStat(False)
        plots._legendLabels["MCStatError"] = "Bkg. stat."
        plots._legendLabels["MCStatSystError"] = "Bkg. stat.#oplussyst."
        plots._legendLabels["BackgroundStatError"] = "Bkg. stat. unc"
        plots._legendLabels[
            "BackgroundStatSystError"] = "Bkg. stat.#oplussyst. unc."

        # Make control plots
        self.Verbose(
            ShellStyles.HighlightStyle() + "Generating control plots" +
            ShellStyles.NormalStyle(), True)

        # Definitions
        massPoints = []
        massPoints.extend(self._config.MassPoints)
        if self._config.OptionDoWithoutSignal:
            massPoints.append(-1)  # for plotting with no signal
        nMasses = len(massPoints)
        nPlots = len(self._config.ControlPlots)
        counter = 0

        # For-loop: All mass points
        for m in massPoints:

            # Initialize flow plot
            selectionFlow = SelectionFlowPlotMaker(self._opts, self._config, m)

            # For-loop: All control plots
            for i in range(0, nPlots):
                counter += 1

                # Skip if control plot does not exist
                if observation.getControlPlotByIndex(i) == None:
                    continue

                # Get the control plot
                myCtrlPlot = self._config.ControlPlots[i]

                # The case m < 0 is for plotting hitograms without any signal
                if m > 0:
                    # saveName = "%s/DataDrivenCtrlPlot_M%d_%02d_%s" % (self._dirname, m, i, myCtrlPlot.title)
                    saveName = "%s/DataDrivenCtrlPlot_M%d_%s" % (
                        self._dirname, m, myCtrlPlot.title)
                    msg = "Control Plot %d/%d (m=%s GeV)" % (counter, nMasses *
                                                             nPlots, str(m))
                else:
                    # saveName = "%s/DataDrivenCtrlPlot_%02d_%s" % (self._dirname, i, myCtrlPlot.title)
                    saveName = "%s/DataDrivenCtrlPlot_%s" % (self._dirname,
                                                             myCtrlPlot.title)
                    msg = "Control Plot %d/%d (no signal)" % (counter,
                                                              nMasses * nPlots)

                # Inform the user of progress
                self.PrintFlushed(
                    ShellStyles.AltStyle() + msg + ShellStyles.NormalStyle(),
                    counter == 1)
                if counter == len(massPoints) * nPlots:
                    print

                # Initialize histograms
                hData = None
                hSignal = None
                hFakeB = None
                hQCDMC = None
                myStackList = []

                # For-loop: All dataset columns (to find histograms)
                for c in self._datasetGroups:
                    self.Verbose(
                        "Dataset is %s for plot %s" %
                        (myCtrlPlot.title, c.getLabel()), False)

                    # Skip plot?
                    bDoPlot = (m < 0 or c.isActiveForMass(
                        m, self._config)) and not c.typeIsEmptyColumn(
                        ) and not c.getControlPlotByIndex(i) == None
                    if not bDoPlot:
                        continue

                    # Clone histo
                    h = c.getControlPlotByIndex(i)["shape"].Clone()

                    if c.typeIsSignal():
                        self.Verbose(
                            "Scaling histogram labelled \"%s\" with BR=%.2f" %
                            (c.getLabel(), self._config.OptionBr), False)
                        h.Scale(self._config.OptionBr)

                        if hSignal == None:
                            hSignal = h.Clone()
                        else:
                            hSignal.Add(h)
                    elif c.typeIsFakeB():
                        if hFakeB == None:
                            hFakeB = h.Clone()
                        else:
                            hFakeB.Add(h)
                    elif c.typeIsQCDMC():
                        if hQCD == None:
                            hQCDMC = h.Clone()
                        else:
                            hQCDMC.Add(h)
                    elif c.typeIsEWKMC() or c.typeIsGenuineB():
                        myHisto = histograms.Histo(h, c._datasetMgrColumn)
                        myHisto.setIsDataMC(isData=False, isMC=True)
                        myStackList.append(myHisto)

                # FIXME: what's this exactly?
                if len(
                        myStackList
                ) < 1 or self._config.OptionFakeBMeasurementSource != "DataDriven":
                    continue

                # Stack all the histograms
                if hFakeB != None:
                    myHisto = histograms.Histo(hFakeB,
                                               "FakeB",
                                               legendLabel=_legendLabelFakeB)
                    myHisto.setIsDataMC(isData=False, isMC=True)
                    myStackList.insert(0, myHisto)
                elif hQCDMC != None:
                    myHisto = histograms.Histo(hQCDMC,
                                               "QCDMC",
                                               legendLabel=_legendLabelQCDMC)
                    myHisto.setIsDataMC(isData=False, isMC=True)
                    myStackList.insert(0, myHisto)

                hData = observation.getControlPlotByIndex(i)["shape"].Clone()
                hDataUnblinded = hData.Clone()

                # Apply blinding & Get blinding string
                myBlindingString = self._applyBlinding(myCtrlPlot, myStackList,
                                                       hData, hSignal)

                # Data
                myDataHisto = histograms.Histo(hData, "Data")
                myDataHisto.setIsDataMC(isData=True, isMC=False)
                myStackList.insert(0, myDataHisto)

                # Add signal
                if m > 0:
                    #mySignalLabel = "HplusTB_M%d" % m
                    mySignalLabel = "ChargedHiggs_HplusTB_HplusToTB_M_%d" % (m)
                    myHisto = histograms.Histo(hSignal, mySignalLabel)
                    myHisto.setIsDataMC(isData=False, isMC=True)
                    myStackList.insert(1, myHisto)

                # Add data to selection flow plot
                selectionFlow.addColumn(myCtrlPlot.flowPlotCaption,
                                        hDataUnblinded, myStackList[1:])

                # Make plot
                myStackPlot = None
                myParams = myCtrlPlot.details.copy()
                myStackPlot = plots.DataMCPlot2(myStackList)
                myStackPlot.setLuminosity(self._luminosity)
                myStackPlot.setEnergy("%d" % self._config.OptionSqrtS)
                myStackPlot.setDefaultStyles()

                # Tweak paramaters
                if not "unit" in myParams.keys():
                    myParams["unit"] = ""

                if myParams["unit"] != "":
                    myParams["xlabel"] = "%s (%s)" % (myParams["xlabel"],
                                                      myParams["unit"])

                # Apply various settings to my parameters
                self._setBlingingString(myBlindingString, myParams)
                self._setYlabelWidthSuffix(hData, myParams)
                self._setLegendPosition(myParams)
                self._setRatioLegendPosition(myParams)

                # Remove non-dientified keywords
                del myParams["unit"]

                # Ratio axis
                if not "opts2" in myParams.keys():
                    myParams["opts2"] = {"ymin": 0.3, "ymax": 1.7}

                # Make sure BR is indicated if anyting else but BR=1.0
                if m > 0 and self._config.OptionBr != 1.0:
                    myStackPlot.histoMgr.setHistoLegendLabelMany({
                        #mySignalLabel: "H^{+} m_{H^{+}}=%d GeV (x %s)" % (m, self._config.OptionBr)
                        mySignalLabel:
                        "m_{H^{+}}=%d GeV (x %s)" % (m, self._config.OptionBr)
                    })

                # Do plotting
                drawPlot(myStackPlot, saveName, **myParams)

            # Do selection flow plot
            selectionFlow.makePlot(self._dirname, m,
                                   len(self._config.ControlPlots),
                                   self._luminosity)

        return
Beispiel #2
0
import HiggsAnalysis.NtupleAnalysis.tools.statisticalFunctions as statisticalFunctions
import HiggsAnalysis.NtupleAnalysis.tools.plots as plots
import HiggsAnalysis.NtupleAnalysis.tools.histograms as histograms
import HiggsAnalysis.NtupleAnalysis.tools.styles as styles
import HiggsAnalysis.NtupleAnalysis.tools.ShellStyles as ShellStyles
import HiggsAnalysis.NtupleAnalysis.tools.aux as aux

#================================================================================================
# Shell Type
#================================================================================================
sh_e = ShellStyles.ErrorStyle()
sh_s = ShellStyles.SuccessStyle()
sh_h = ShellStyles.HighlightStyle()
sh_a = ShellStyles.HighlightAltStyle()
sh_l = ShellStyles.AltStyle()
sh_t = ShellStyles.NoteStyle()
sh_n = ShellStyles.NormalStyle()
sh_w = ShellStyles.WarningStyle()


#================================================================================================
# Function definitions
#================================================================================================
def cleanGraph(graph, massPoint):
    '''
    Remove mass points lower than 100
    
    \param graph   TGraph to operate
    \param minX    Minimum value of mass hypotheses to keep