Esempio n. 1
0
def DrawStackHistograms(histograms, outname, blind=True, verbose=False):

    #hists_sorted = sorted(histograms, key=lambda h: h.Integral())

    hs = THStack("h_stack", "")
    h_obs = None

    l = TLegend(0.77, 0.60, 0.90, 0.87)

    for hist in histograms:

        hname = hist.GetName()

        if "data_obs" in hname:
            if not blind:
                h_obs = hist
                SetHistogramStyle(h_obs, "Observed")
                l.AddEntry(h_obs, "Observed", "p")
        else:
            SetHistogramStyle(hist)
            label = GetLabelName(hist)
            hs.Add(hist)
            l.AddEntry(hist, label, "f")

    h_est = hs.GetStack().Last().Clone()

    if verbose:
        est_err = Double(0.)
        nbins_est = h_est.GetNbinsX()
        est_yields = h_est.IntegralAndError(1, nbins_est, est_err)
        print "predicted yields :", est_yields, "+/-", est_err
        if not blind:
            print "observed yields :", h_obs.Integral()

    # draw

    # SetCMSStyle()   TODO

    canvas = TCanvas()
    gPad.SetLogy()

    #ymax_hs = hs.GetMaximum()
    #ymax = ymax_hs if h_obs is None else max(ymax_hs, h_obs.GetMaximum())
    ymax = 10000
    ymin = 0.1

    hs.SetMaximum(ymax)
    hs.SetMinimum(ymin)
    hs.Draw("HIST")
    l.Draw("same")
    if not blind:
        h_obs.Draw("same")

    canvas.SaveAs(outname)
def PlotDataMC(dataFiles_, mcFiles_, signalFiles_, dataDirec_, mcDirec_,
               signalDirec_, drawPads_, Lumi_, SigScale_, ol_, log_, Tags_,
               VarBatch_, CutsType_, verbose_, noQCD_):
    print "Plotting Data / MC"
    gROOT.ProcessLine(
        "gErrorIgnoreLevel = kError"
    )  # kPrint, kInfo, kWarning, kError, kBreak, kSysError, kFatal
    gStyle.SetOptStat(0)
    gStyle.SetErrorX(0.0001)
    HHWWggTags = []

    ##-- Get Tags
    for t in Tags_:
        HHWWggTags.append(t)
    cuts, cutNames = GetCuts(CutsType_)

    ##-- if var batch is loose, need separate titles for variables since it will be sum of vars * bools
    if (VarBatch_ == "Loose"):
        finalStateVars, varNames = GetVars(
            VarBatch_)  # get vars from var batch
        if (verbose_):
            print "finalStateVars = ", finalStateVars
            print "varNames = ", varNames
    else:
        finalStateVars = GetVars(VarBatch_)  # get vars from var batch
    if (verbose_):
        #print"cuts:",cuts
        print "cutNames:", cutNames
        print "vars:", finalStateVars

    ##-- For each data file (can just be one)
    for dF_ in dataFiles_:
        cutBatchTag_pairs = []
        dataNevents_list = []
        MC_names = []
        MC_Nevents_lists = []
        MC_Nevents_noweight_lists = []

        ##-- For each category (Ex: HHWWggTag_0, HHWWggTag_1, HHWWggTag_2, combined)
        for itag, HHWWggTag in enumerate(HHWWggTags):
            if (verbose_):
                print "tag:", HHWWggTag
            dPath = "%s/%s" % (dataDirec_, dF_)
            dFile = TFile.Open(dPath)
            if (HHWWggTag == "combined"):
                ch = TChain('tagsDumper/trees/Data_13TeV_HHWWggTag_0')
                ch.Add("%s/tagsDumper/trees/Data_13TeV_HHWWggTag_0" % (dPath))
                ch.Add("%s/tagsDumper/trees/Data_13TeV_HHWWggTag_1" % (dPath))
                ch.Add("%s/tagsDumper/trees/Data_13TeV_HHWWggTag_2" % (dPath))
            else:
                ch = TChain('tagsDumper/trees/Data_13TeV_%s' % (HHWWggTag))
                ch.Add(dPath)
            BLIND_CUT = "(CMS_hgg_mass < 115 || CMS_hgg_mass > 135)"
            MC_WEIGHT = "1*weight"
            ZERO_CUT = "ZERO_CUT"
            MC_CUT = "%s*(%s)*(%s)" % (MC_WEIGHT, BLIND_CUT, ZERO_CUT)
            DATA_CUT = "%s*(%s)" % (BLIND_CUT, ZERO_CUT)
            SIGNAL_CUT = "%s*(%s)" % (MC_WEIGHT, ZERO_CUT
                                      )  # no blind cut on signal

            ##-- For each cut
            for ic, cut in enumerate(cuts):
                #if(verbose_): print"Plotting with selection:",cut
                cutName = cutNames[ic]
                cutBatchTag = "%s_%s" % (cutName, HHWWggTag)
                cutBatchTag_pairs.append(cutBatchTag)
                dataNevents = -999
                these_MC_Nevents = []
                these_MC_Nevents_noweights = []
                outputFolder = "%s/%s" % (ol_, cutName)
                if (not os.path.exists(outputFolder)):
                    os.system('mkdir %s' % (outputFolder))
                    os.system('cp %s/index.php %s' % (ol_, outputFolder))
                MC_CUT += "*(%s)" % (cut)
                DATA_CUT += "*(%s)" % (cut)
                SIGNAL_CUT += "*(%s)" % (cut)

                ##-- For each variable
                for iv, v in enumerate(finalStateVars):
                    if (VarBatch_ == "Loose"): varTitle = varNames[iv]
                    else: varTitle = GetVarTitle(v)

                    if (verbose_): print "Plotting variable:", varTitle

                    MC_CUT = MC_CUT.replace(
                        "ZERO_CUT", "(%s != 0) && (%s != -999)" % (v, v))
                    DATA_CUT = DATA_CUT.replace(
                        "ZERO_CUT", "(%s != 0) && (%s != -999)" % (v, v))
                    SIGNAL_CUT = SIGNAL_CUT.replace(
                        "ZERO_CUT", "(%s != 0) && (%s != -999)" % (v, v))
                    MC_CUT_NOWEIGHT = MC_WEIGHT.replace(MC_WEIGHT, "(1)")

                    if (varTitle == "weight"):
                        MC_CUT = MC_CUT.replace(
                            MC_WEIGHT, "(1)"
                        )  # if you want to plot the "weight" variable, you should not scale it by weight!

                    ##-- Can add printing of cuts to debug
                    # if(verbose_):
                    # print"MC_CUT:",MC_CUT
                    # print"DATA_CUT:",DATA_CUT
                    ##--

                    legend = TLegend(0.55, 0.65, 0.89, 0.89)
                    legend.SetTextSize(0.025)
                    legend.SetBorderSize(0)
                    legend.SetFillStyle(0)
                    xbins, xmin, xmax = GetBins(varTitle)

                    ##-- Fill histogram with data
                    Data_h_tmp = TH1F('Data_h_tmp', varTitle, xbins, xmin,
                                      xmax)
                    Data_h_tmp.SetTitle("%s" % (varTitle))
                    Data_h_tmp.SetMarkerStyle(8)
                    exec('ch.Draw("%s >> Data_h_tmp","%s")' % (v, DATA_CUT))

                    ##-- Only save number of events for first variable. Should be same for all because same cut is used
                    if (iv == 0):
                        dataNevents = Data_h_tmp.GetEntries()
                        dataNevents_list.append(dataNevents)
                    # print"Blinded Data numEvents:",Data_h_tmp.GetEntries()
                    DataHist = Data_h_tmp.Clone("DataHist")
                    DataHist.SetDirectory(0)
                    legend.AddEntry(DataHist, "Data", "P")

                    ##-- Get histograms with MC Backgrounds
                    bkgStack = THStack("bkgStack", "bkgStack")
                    histos = []
                    histCategories = []
                    for i, mcF_ in enumerate(mcFiles_):
                        mcPath = "%s/%s" % (mcDirec_, mcF_)
                        mcFile = TFile.Open(mcPath)
                        treeName = GetMCTreeName(mcF_)
                        MC_Category = GetMCCategory(mcF_)
                        if (verbose_):
                            # print"Background File:",mcPath
                            print "Background:", MC_Category

                        ##-- If noQCD set to true, skip QCD for Tag_0 and Tag_1
                        if (MC_Category == "QCD") and (
                                noQCD_) and (HHWWggTag == "HHWWggTag_0"
                                             or HHWWggTag == "HHWWggTag_1"):
                            print "Skipping QCD"
                            these_MC_Nevents_noweights.append(
                                0)  # Set yields to 0 for table
                            these_MC_Nevents.append(
                                0)  # Set yields to 0 for table
                            if (itag == 0 and ic == 0 and iv == 0):
                                MCname = GetMCName(mcF_)
                                MC_names.append(
                                    MCname
                                )  # Skipping QCD, but still save name for yields because tag_2 may not be 0
                            continue

                        ##-- Define TChain based on categories
                        if (HHWWggTag == "combined"):
                            mc_ch = TChain(
                                'tagsDumper/trees/%s_13TeV_HHWWggTag_0' %
                                (treeName))
                            mc_ch.Add(
                                "%s/tagsDumper/trees/%s_13TeV_HHWWggTag_0" %
                                (mcPath, treeName))
                            mc_ch.Add(
                                "%s/tagsDumper/trees/%s_13TeV_HHWWggTag_1" %
                                (mcPath, treeName))
                            mc_ch.Add(
                                "%s/tagsDumper/trees/%s_13TeV_HHWWggTag_2" %
                                (mcPath, treeName))
                        else:
                            mc_ch = TChain('tagsDumper/trees/%s_13TeV_%s' %
                                           (treeName, HHWWggTag))
                            mc_ch.Add(mcPath)
                        xbins, xmin, xmax = GetBins(varTitle)
                        exec(
                            "MC_h_tmp_%s = TH1F('MC_h_tmp_%s',varTitle,xbins,xmin,xmax)"
                            % (i, i))
                        exec(
                            "MC_h_tmp_noweight_%s = TH1F('MC_h_tmp_noweight_%s',varTitle,xbins,xmin,xmax)"
                            % (i, i))
                        thisHist = eval("MC_h_tmp_%s" % (i))
                        mcColor = GetMCColor(MC_Category)

                        ##-- If GJet or QCD sample, need to remove prompt-prompt events
                        if (MC_Category == "GJet" or MC_Category == "QCD"):
                            if (verbose_): print "Remove prompt-prompt"
                            removePromptPromptCut = "(!((Leading_Photon_genMatchType == 1) && (Subleading_Photon_genMatchType == 1)))"  # selection: remove events where both photons are prompt
                            original_MC_CUT = "%s" % (MC_CUT)
                            this_MC_CUT = "%s*(%s)" % (original_MC_CUT,
                                                       removePromptPromptCut)
                            this_MC_CUT_NOWEIGHT = this_MC_CUT.replace(
                                MC_WEIGHT, "(1)")

                        eval("MC_h_tmp_%s.SetFillColor(eval(mcColor))" % (i))
                        eval("MC_h_tmp_%s.SetLineColor(eval(mcColor))" % (i))
                        if (MC_Category == "GJet" or MC_Category == "QCD"):
                            exec('mc_ch.Draw("%s >> MC_h_tmp_%s","%s")' %
                                 (v, i, this_MC_CUT))
                        else:
                            exec('mc_ch.Draw("%s >> MC_h_tmp_%s","%s")' %
                                 (v, i, MC_CUT))

                        eval("MC_h_tmp_%s.Scale(float(Lumi_))" % (i))

                        ##-- MC reweighting for HHWWgg backgrounds (turned off for now)
                        # reWeightVals = ReWeightMC(mcF_)
                        # doReWeight, reWeightScale = reWeightVals[0], reWeightVals[1]
                        # print"doReWeight,reWeightScale:",doReWeight, reWeightScale
                        # if(doReWeight):
                        #     if(verbose_):
                        #         print"ReWeighting MC"
                        #         print"With scale: ",reWeightScale
                        #     eval("MC_h_tmp_%s.Scale(float(reWeightScale))"%(i))
                        ##

                        ##-- Only save for 1st variable. Should be same for all variables
                        if (iv == 0):
                            if (MC_Category == "GJet" or MC_Category == "QCD"):
                                exec(
                                    'mc_ch.Draw("%s >> MC_h_tmp_noweight_%s","%s")'
                                    % (v, i, this_MC_CUT_NOWEIGHT))
                            else:
                                exec(
                                    'mc_ch.Draw("%s >> MC_h_tmp_noweight_%s","%s")'
                                    % (v, i, MC_CUT_NOWEIGHT))
                            these_MC_Nevents_noweights.append(
                                eval("MC_h_tmp_noweight_%s.Integral()" % (i)))
                            these_MC_Nevents.append(
                                eval("MC_h_tmp_%s.Integral()" % (i)))

                            ##-- Only need to get MC names once
                            if (itag == 0 and ic == 0 and iv == 0):
                                MCname = GetMCName(mcF_)
                                MC_names.append(
                                    MCname)  # get shorter MC name here

                        newHist = thisHist.Clone("newHist")
                        ##-- Set title based on treeName
                        newHist.SetTitle(MC_Category)
                        newHist.GetXaxis().SetTitle(mcF_)
                        newHist.SetDirectory(0)
                        histos.append(newHist)
                        histCategories.append(MC_Category)

                    sig_histos = []
                    sig_histCategories = []

                    ##-- Add Signal to plots
                    for i, sigF_ in enumerate(signalFiles_):
                        sigPath = "%s/%s" % (signalDirec_, sigF_)
                        sigFile = TFile.Open(sigPath)
                        treeName = GetMCTreeName(sigF_)
                        MC_Category = GetMCCategory(sigF_)
                        if (verbose_):
                            # print"Signal File:",sigPath
                            print "Signal:", MC_Category
                        if (HHWWggTag == "combined"):
                            mc_ch = TChain(
                                'tagsDumper/trees/%s_13TeV_HHWWggTag_0' %
                                (treeName))
                            mc_ch.Add(
                                "%s/tagsDumper/trees/%s_13TeV_HHWWggTag_0" %
                                (sigPath, treeName))
                            mc_ch.Add(
                                "%s/tagsDumper/trees/%s_13TeV_HHWWggTag_1" %
                                (sigPath, treeName))
                            mc_ch.Add(
                                "%s/tagsDumper/trees/%s_13TeV_HHWWggTag_2" %
                                (sigPath, treeName))
                        else:
                            mc_ch = TChain('tagsDumper/trees/%s_13TeV_%s' %
                                           (treeName, HHWWggTag))
                            mc_ch.Add(sigPath)
                        xbins, xmin, xmax = GetBins(varTitle)
                        exec(
                            "MC_h_tmp_%s = TH1F('MC_h_tmp_%s',v,xbins,xmin,xmax)"
                            % (i, i))
                        thisHist = eval("MC_h_tmp_%s" % (i))
                        mcColor = GetMCColor(MC_Category)
                        ##-- Style options for signal to distinguish from Data, Background
                        # eval("MC_h_tmp_%s.SetFillColor(eval(mcColor))"%(i))
                        # eval("MC_h_tmp_%s.SetFillStyle(3004)"%(i))
                        ##--
                        eval("MC_h_tmp_%s.SetFillColorAlpha(eval(mcColor),0.1)"
                             % (i))
                        eval("MC_h_tmp_%s.SetLineColor(eval(mcColor))" % (i))
                        exec('mc_ch.Draw("%s >> MC_h_tmp_%s","%s")' %
                             (v, i, SIGNAL_CUT))
                        eval("MC_h_tmp_%s.Scale(float(Lumi_))" %
                             (i))  # should scale to luminosity by default
                        SigXS_Scale = GetXScale(
                            "HHWWgg_v2-6"
                        )  # how to scale the XS which is by default in flashgg 1fb
                        if (verbose_): print "SigXS_Scale: ", SigXS_Scale
                        eval("MC_h_tmp_%s.Scale(float(SigXS_Scale))" %
                             (i))  # should scale to luminosity by default
                        newHist = thisHist.Clone("newHist")

                        ##-- Set title based on treeName
                        newHist.SetTitle(MC_Category)
                        newHist.GetXaxis().SetTitle(sigF_)
                        newHist.SetLineStyle(1)
                        newHist.SetLineWidth(5)

                        newHist.SetDirectory(0)
                        sig_histos.append(newHist)
                        sig_histCategories.append(MC_Category)

                    MC_AddedtoLegend = {
                        "QCD": 0,
                        "SMhgg": 0,
                        "GJet": 0,
                        "DiPhoJets": 0,
                        "DiPhoJetsBox": 0,
                        # "W1JetsToLNu" : 0,
                        # "W2JetsToLNu" : 0,
                        # "W3JetsToLNu" : 0,
                        # "W4JetsToLNu" : 0,
                        "WJets": 0,
                        "WW": 0,
                        "tt": 0,
                        "DY": 0,
                        "WGGJets": 0,
                        "WGJJ": 0,
                        "ttW": 0
                    }

                    Signals_AddedtoLegend = {"HHWWgg_SM": 0}

                    ##-- Order histograms by MC category
                    orderedHistos = OrderHistos(histos, histCategories)
                    sig_orderedHistos = OrderHistos(sig_histos,
                                                    sig_histCategories)

                    ##-- Add backgrounds to background stack
                    for h in orderedHistos:
                        bkgStack.Add(h, 'hist')
                        bkgName = h.GetTitle()
                        added = MC_AddedtoLegend[bkgName]
                        if (added): continue
                        else:
                            legend.AddEntry(h, bkgName, "F")
                            MC_AddedtoLegend[bkgName] = 1

                    ##-- By default draw background save background contributions. Later delete if not wanted
                    outName = "%s/BackgroundsTest_%s.png" % (outputFolder,
                                                             HHWWggTag)
                    bkgOutName = "%s/BackgroundsPADS_%s_%s.png" % (
                        outputFolder, varTitle, HHWWggTag)
                    SimpleDrawHisto(bkgStack, "PADS", bkgOutName, varTitle)
                    bkgOutName = bkgOutName.replace(".png", ".pdf")
                    SimpleDrawHisto(bkgStack, "PADS", bkgOutName, varTitle)

                    ##-- Add text box with selection type
                    offset = 0
                    selText = TLatex(0.129, 0.85, cutName)
                    selText.SetNDC(1)
                    selText.SetTextSize(0.04)
                    CatText = TLatex(0.129, 0.8, HHWWggTag)
                    CatText.SetNDC(1)
                    CatText.SetTextSize(0.04)
                    stackSum = bkgStack.GetStack().Last(
                    )  #->Draw(); # for computing ratio
                    stackSum.Sumw2()
                    stackSum.SetLineColor(kBlack)
                    stackSum.SetLineStyle(
                        7)  # to distinguish from data uncertainty
                    DataHist.SetLineColor(kBlack)
                    DataHist.Sumw2()
                    xTitle = GetXaxisTitle(varTitle)
                    DataHist.GetXaxis().SetTitle(xTitle)
                    if (log_):
                        DataHist.SetMinimum(0.01)
                        stackSum.SetMinimum(0.01)
                        bkgStack.SetMinimum(0.01)

                    ##-- Define ratio plot for computing Data / MC ratio
                    rp = TRatioPlot(DataHist, stackSum)
                    rp.SetH1DrawOpt("P")
                    rp.SetH2DrawOpt("hist")
                    # rp.SetGraphDrawOpt("PE2")
                    dMax = DataHist.GetMaximum()
                    bMax = stackSum.GetMaximum()
                    maxHeight = max(dMax, bMax)

                    ##-- Create the entire picture: Combine Data, MC, Data / MC ratio and signal in one plot

                    for fileType in ["pdf"]:
                        gStyle.SetErrorX(0.0001)
                        # varTitle = GetVarTitle(v)
                        outName = "%s/DataMC_%s_%s.%s" % (
                            outputFolder, varTitle, HHWWggTag, fileType)
                        if (log_):
                            outName = "%s/DataMC_%s_%s_log.%s" % (
                                outputFolder, varTitle, HHWWggTag, fileType)
                        else:
                            outName = "%s/DataMC_%s_%s_nonLog.%s" % (
                                outputFolder, varTitle, HHWWggTag, fileType)
                        DataMCRatio_c = TCanvas("DataMCRatio_c",
                                                "DataMCRatio_c", 600, 800)
                        rp.Draw("nogrid")
                        rp.GetLowYaxis().SetNdivisions(5)
                        DataMCRatio_c.Update()

                        ratioGraph = rp.GetCalculationOutputGraph()
                        ratioGraph.SetMarkerStyle(8)
                        ratioGraph.SetMarkerSize(0.5)

                        # rp.SetGraphDrawOpt("EP")
                        # rp.SetGraphDrawOpt("EPZ2")
                        # rp.GetLowerRefYaxis().SetTitle("Data / MC")

                        rp.GetUpperRefYaxis().SetTitle("Entries")
                        rp.GetLowerRefYaxis().SetTitle("Data / MC")
                        rp.GetLowerPad().Update()
                        if (log_):
                            rp.GetUpperRefYaxis().SetRangeUser(
                                0.1, maxHeight * 100.)
                        else:
                            rp.GetUpperRefYaxis().SetRangeUser(
                                0, maxHeight * 1.3)

                        UpperPad = rp.GetUpperPad()
                        UpperPad.cd()
                        bkgStack.Draw("same")
                        stackSum.Draw("sameE")
                        DataHist.Draw("samePE")
                        for sig_hist in sig_histos:
                            sigMax = sig_hist.GetMaximum()
                            if sigMax == 0: sigMax = 1

                            ##-- No user input signal scale
                            if (SigScale_ == -999):
                                sigScale = (float(maxHeight) / 10.) / float(
                                    sigMax
                                )  # in order to scale signal to 10th of max of plot
                                sig_hist.Scale(sigScale)

                            ##-- User input signal scale
                            else:
                                if (verbose_):
                                    print "user sig scale:", SigScale_
                                sigScale = SigScale_
                                sig_hist.Scale(sigScale)

                            for sig_h in sig_orderedHistos:
                                # sigName = "%s X %d"%(sig_h.GetTitle(),sigScale)
                                sigName = sig_h.GetTitle()
                                added = Signals_AddedtoLegend[sigName]
                                if (added): continue
                                else:
                                    legend.AddEntry(
                                        sig_h, "%s * %.5g" %
                                        (sig_h.GetTitle(), sigScale), "FL")
                                    Signals_AddedtoLegend[sigName]
                            sig_hist.Draw("samehist")
                        legend.Draw("same")
                        selText.Draw("same")
                        CatText.Draw("same")
                        rp.GetLowerRefGraph().SetMinimum(0.5)
                        rp.GetLowerRefGraph().SetMaximum(1.5)
                        Npoints = rp.GetLowerRefGraph().GetN()
                        for ip in range(0, Npoints):
                            rp.GetLowerRefGraph().SetPointEXhigh(ip, 0)
                            rp.GetLowerRefGraph().SetPointEXlow(ip, 0)
                        if (log_):
                            UpperPad.SetLogy()
                            UpperPad.Update()
                        rp.GetLowerPad().cd()
                        lowerPad = rp.GetLowerPad()
                        rp.GetLowerRefYaxis().SetTitle("Data / MC")
                        lineAtOne = TLine(lowerPad.GetUxmin(), 1,
                                          lowerPad.GetUxmax(), 1)
                        lineAtOne.SetLineStyle(3)
                        lineAtOne.Draw("same")
                        rp.GetLowerPad().Update()
                        DataMCRatio_c.Update()
                        DataMCRatio_c.SaveAs(outName)
                        outName = outName.replace(".pdf", ".png")
                        DataMCRatio_c.SaveAs(outName)
                    if (not drawPads_):
                        bkgOutName = "%s/BackgroundsPADS_%s_%s.png" % (
                            outputFolder, varTitle, HHWWggTag)
                        os.system('rm %s' % (bkgOutName))
                        bkgOutName = bkgOutName.replace(".png", ".pdf")
                        os.system('rm %s' % (bkgOutName))
                    MC_CUT = MC_CUT.replace(
                        "(%s != 0) && (%s != -999)" % (v, v), "ZERO_CUT")
                    DATA_CUT = DATA_CUT.replace(
                        "(%s != 0) && (%s != -999)" % (v, v), "ZERO_CUT")

                MC_Nevents_lists.append(these_MC_Nevents)
                MC_Nevents_noweight_lists.append(these_MC_Nevents_noweights)

        ##-- Produce table with number of events for each MC, total MC, and data
        CreateYieldsTables(cutBatchTag_pairs, dataNevents_list, MC_names,
                           MC_Nevents_lists, MC_Nevents_noweight_lists, ol_)
Esempio n. 3
0
def SaveHisto1D(HIST,
                suffix,
                output,
                snorm=1,
                ratio=0,
                poisson=True,
                logy=False,
                isVal=False):

    # only data and mc
    # SUFFIX = 2016_ss_lep1_pt

    bkgsum = 'BkgSum_%s' % (suffix)
    HIST[bkgsum] = HIST['DATA_%s' % (suffix)].Clone("BkgSum") if 'DATA_%s' % (
        suffix) in HIST else HIST['MC_%s' % (suffix)].Clone("BkgSum")
    HIST[bkgsum].Reset("MICES")
    HIST[bkgsum].SetFillStyle(3003)
    HIST[bkgsum].SetFillColor(1)
    HIST[bkgsum].SetMarkerStyle(0)

    HIST[bkgsum].Add(HIST['MC_%s' % (suffix)])

    HIST['DATA_%s' % (suffix)].SetMarkerStyle(20)
    HIST['DATA_%s' % (suffix)].SetMarkerSize(1.25)
    HIST['DATA_%s' % (suffix)].SetFillColor(418)
    HIST['DATA_%s' % (suffix)].SetFillStyle(1001)
    HIST['DATA_%s' % (suffix)].SetLineColor(1)
    HIST['DATA_%s' % (suffix)].SetLineStyle(1)
    HIST['DATA_%s' % (suffix)].SetLineWidth(2)

    HIST['MC_%s' % (suffix)].SetFillColor(418)
    HIST['MC_%s' % (suffix)].SetFillStyle(1001)
    HIST['MC_%s' % (suffix)].SetLineColor(418)
    HIST['MC_%s' % (suffix)].SetLineStyle(1)
    HIST['MC_%s' % (suffix)].SetLineWidth(2)

    for i, s in enumerate(HIST):
        addOverflow(HIST[s], False)  # Add overflow

    #Stack
    bkg = THStack(
        'bkg', ";" + HIST[bkgsum].GetXaxis().GetTitle() + ";" +
        HIST[bkgsum].GetYaxis().GetTitle())

    bkg.Add(HIST['MC_%s' % (suffix)])  # ADD ALL BKG

    #Legend
    n = len(HIST)
    leg = TLegend(0.7, 0.9 - 0.05 * n, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetTextSize(0.03)
    leg.AddEntry(HIST['DATA_%s' % (suffix)],
                 'Data [%.1f]' % (HIST['DATA_%s' % (suffix)].Integral()), "pl")
    leg.AddEntry(HIST['MC_%s' % (suffix)],
                 'DY [%.1f]' % (HIST['MC_%s' % (suffix)].Integral()), "f")

    #if isFake: leg.AddEntry(HIST['FAKE_%s' %(suffix)], 'Fake [%.1f]' %(HIST['FAKE_%s' %(suffix)].Integral()), "f")

    leg.AddEntry(HIST[bkgsum], 'BkgSum [%.1f]' % (HIST[bkgsum].Integral()),
                 "f")
    c1 = TCanvas("c1",
                 HIST.values()[-1].GetXaxis().GetTitle(), 800,
                 800 if ratio else 600)

    #Ratio pad
    if ratio:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), ratio)
        setBotPad(c1.GetPad(2), ratio)

    c1.cd(1)
    c1.GetPad(bool(ratio)).SetTopMargin(0.06)
    c1.GetPad(bool(ratio)).SetRightMargin(0.05)
    c1.GetPad(bool(ratio)).SetTicks(1, 1)
    if logy:
        c1.GetPad(bool(ratio)).SetLogy()

    #Draw
    bkg.Draw("HIST")  # stack
    HIST[bkgsum].Draw("SAME, E2")  # sum of bkg
    HIST['DATA_%s' % (suffix)].Draw("SAME, PE")  # data

    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1)  #1.075

    bkg.SetMaximum((6.0 if logy else 1.5) * max(
        bkg.GetMaximum(),
        HIST['DATA_%s' %
             (suffix)].GetBinContent(HIST['DATA_%s' %
                                          (suffix)].GetMaximumBin()) +
        HIST['DATA_%s' %
             (suffix)].GetBinError(HIST['DATA_%s' %
                                        (suffix)].GetMaximumBin())))
    bkg.SetMinimum(
        max(
            min(HIST[bkgsum].GetBinContent(HIST[bkgsum].GetMinimumBin(
            )), HIST['DATA_%s' %
                     (suffix)].GetMinimum()), 5.e-1) if logy else 0.)

    #bkg.SetMinimum(1.0)

    leg.Draw()

    setHistStyle(bkg, 1.2 if ratio else 1.1)
    setHistStyle(HIST[bkgsum], 1.2 if ratio else 1.1)

    ##########################
    if ratio:
        c1.cd(2)
        err = HIST[bkgsum].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if HIST[bkgsum].GetBinContent(i) > 0:
                err.SetBinError(
                    i, HIST[bkgsum].GetBinError(i) /
                    HIST[bkgsum].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        errLine.SetLineColor(1)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")

        if 'DATA_%s' % (suffix) in HIST:
            res = HIST['DATA_%s' % (suffix)].Clone("Residues")
            for i in range(0, res.GetNbinsX() + 1):
                if HIST[bkgsum].GetBinContent(i) > 0:
                    res.SetBinContent(
                        i,
                        res.GetBinContent(i) / HIST[bkgsum].GetBinContent(i))
                    res.SetBinError(
                        i,
                        res.GetBinError(i) / HIST[bkgsum].GetBinContent(i))
            setBotStyle(res)
            res.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(HIST['DATA_%s' % (suffix)], HIST[bkgsum])
                drawKolmogorov(HIST['DATA_%s' % (suffix)], HIST[bkgsum])
                #drawRelativeYield(HIST['DATA_%s' %(suffix)], HIST[bkgsum])
        else:
            res = None
    c1.cd(1)
    if '2016' in output:
        drawCMS("35.87", "Object Study")
    elif '2017' in output:
        drawCMS("41.53", "Object Study")
    elif '2018' in output:
        drawCMS("59.74", "Object Study")

    if 'os' in suffix:
        drawRegion('Opposite Sign')
    elif 'ss' in suffix:
        drawRegion('Same Sign')

    c1.Update()

    c1.Print('%s/hstack_%s.png' % (output, suffix))
Esempio n. 4
0
        tex3.SetTextFont(42)
        tex3.SetLineWidth(2)

        cc = TCanvas(var, var, 800, 750)
        #        cc.SetLogy()
        pad1 = TPad("p1", "p1", 0, 0.25, 1, 1, 0, 0)
        pad1.SetMargin(0.15, 0.03, 0, 0.01)
        pad2 = TPad("p2", "p2", 0, 0, 1, 0.25, 0, 0)
        pad2.SetMargin(0.15, 0.03, 0.3, 0.01)
        pad2.SetGrid()
        pad1.Draw()
        pad2.Draw()

        pad1.cd()
        pad1.SetLogy()
        hs.SetMinimum(0.1)
        hs.Draw("HIST")
        #        hs.GetXaxis().SetTitle(TITLE[var])
        hs.GetYaxis().SetTitle("Events")
        hs.GetYaxis().SetTitleOffset(0.8)
        hs.GetYaxis().SetTitleSize(0.05)
        data_hist.Draw("same" "LPE")
        sig_hist.Draw("same" "LPE")
        leg.Draw()
        tex1.Draw()
        tex2.Draw()
        tex3.Draw()

        ROOT.gStyle.SetOptStat(0)
        ROOT.gStyle.SetOptTitle(0)
        pad2.cd()
Esempio n. 5
0
def calcAndDrawSignificance(SignalHist,
                            BkgrHist,
                            xtitle,
                            legendNames,
                            DataName,
                            destination,
                            year,
                            ylog=False,
                            customLabels=None,
                            extraText=None,
                            scaleSignalToBkgr=False,
                            DivideByLine=None):
    GeneralSettings()

    #Make sure the code works for single backgrounds
    if not isinstance(BkgrHist, (list, )):
        BkgrHist = [BkgrHist]
    if not isinstance(SignalHist, (list, )):
        SignalHist = [SignalHist]

    BkgrHist, legendNames = orderHist(BkgrHist, legendNames, True)

    #Add all backgrounds
    totBkgr = BkgrHist[0].Clone("TotBkgr")
    for h in BkgrHist[1:]:
        totBkgr.Add(h)

    #Normalize signal to background if needed
    if scaleSignalToBkgr:
        for h in SignalHist:
            h.scale(totBkgr.GetSumOfWeights() / h.GetSumOfWeights)

    #Define a canvas
    Canv = TCanvas("Canv" + destination, "Canv" + destination, 1500, 1000)

    #Set Histogram Styles
    for h, n in zip(BkgrHist, legendNames):
        h.SetFillColor(TColor.GetColor(GetStackColorTauPOGbyName(n)))
        h.SetLineColor(TColor.GetColor(GetStackColorTauPOGbyName(n)))

    for i, h in enumerate(SignalHist):
        h.SetMarkerColor(TColor.GetColor(GetLineColor(i)))
        h.SetLineColor(TColor.GetColor(GetLineColor(i)))
        h.SetMarkerStyle(GetMarker(i))

    list_of_significance_hists = []
    for i, sh in enumerate(SignalHist):
        #Calculate significance
        total = totBkgr.Clone("Total")
        total.Add(sh)
        sqrt_total = total.Clone('Sqrt_Total')
        for xbin in xrange(
                1,
                total.GetSize() - 1
        ):  #GetSize returns nbins + 2 (for overflow and underflow bin)
            sqrt_x = np.sqrt(total.GetBinContent(xbin))
            sqrt_total.SetBinContent(xbin, sqrt_x)
            sqrt_total.SetBinError(xbin,
                                   0.5 * total.GetBinError(xbin) / sqrt_x)

        significance = sh.Clone('Signal' + str(i))
        significance.Divide(sqrt_total)
        list_of_significance_hists.append(significance)

    #First pad
    plotpad = TPad("plotpad", "plotpad", 0, .3, 1, 0.98)
    plotpad.SetBottomMargin(0.025)
    plotpad.Draw()
    plotpad.cd()

    #Create Stack (Change with most logical ordering)
    hs = THStack("hs", "hs")
    for h in BkgrHist:
        hs.Add(h)
    hs.Draw(
        "EHist"
    )  #Draw before using GetHistogram, see https://root-forum.cern.ch/t/thstack-gethistogram-null-pointer-error/12892/4
    title = " ; ; Events"
    hs.SetTitle(title)
    #    hs.GetHistogram().GetXaxis().SetTickLength(0)
    hs.GetHistogram().GetXaxis().SetLabelOffset(9999999)
    #hs.GetHistogram().SetMaximum(1)
    #Set range
    overallMin = GetOverallMinimum(BkgrHist + SignalHist, True)
    overallMax = GetOverallMaximum([totBkgr] + SignalHist)

    if ylog:
        if overallMin == 0.:
            overallMin = 0.1
        ymin = 0.3 * overallMin
        ymax = 30 * overallMax
        plotpad.SetLogy()
    else:
        ymin = 0.7 * overallMin
        ymax = 1.3 * overallMax
    hs.SetMinimum(ymin)
    hs.SetMaximum(ymax)

    for h in SignalHist:
        h.Draw("EPSame")
    #Create Legend
    legend = TLegend(0.7, .7, .9, .9)
    for h, n in zip(BkgrHist, legendNames):
        legend.AddEntry(h, n)
    for h, n in zip(SignalHist, DataName):
        legend.AddEntry(h, n)
    legend.SetFillStyle(0)
    legend.SetBorderSize(0)

    legend.Draw()

    #Draw lines if needed
    if DivideByLine is not None:
        tdrStyle_Left_Margin = 0.16
        tdrStyle_Right_Margin = 0.02
        plot_size_hor = 1 - tdrStyle_Left_Margin - tdrStyle_Right_Margin
        #Option one, user provides the number of divisions and we divide equally
        if isinstance(DivideByLine[0], int):
            x_pos = np.linspace(totBkgr.GetXaxis().GetXmin(),
                                totBkgr.GetXaxis().GetXmax(),
                                DivideByLine[0] + 1)
        #Option two, user provides the specific boundaries
        if isinstance(DivideByLine[0], (list, )):
            x_pos = DivideByLine[0]

        #Draw the lines
        lines = []
        for i, x in enumerate(x_pos[1:-1]):
            lines.append(TLine(x, ymin, x, ymax))
            lines[i].SetLineColor(ROOT.kRed)
            lines[i].SetLineStyle(10)

        for line in lines:
            line.Draw('same')

        #Add extra text
        for i, name in enumerate(DivideByLine[1]):
            x = ((x_pos[i + 1] + x_pos[i]) /
                 (2 * (x_pos[-1] - x_pos[0]) /
                  plot_size_hor)) + tdrStyle_Left_Margin
            extraText.append(extraTextFormat(name, x, 0.1, None, 22))

    #Draw extra text if needed
    if extraText is not None:
        DrawExtraText(plotpad, extraText)

    #Return to canvas
    Canv.cd()

    #Second pad
    ratiopad = TPad("ratiopad", "ratiopad", 0, 0.05, 1, .3)
    ratiopad.SetTopMargin(0.05)
    ratiopad.SetBottomMargin(0.25)
    ratiopad.Draw()
    ratiopad.cd()

    #print list_of_significance_hists[0].GetMaximum(), list_of_significance_hists[1].GetMaximum(), list_of_significance_hists[2].GetMaximum()
    overallMin = GetOverallMinimum(list_of_significance_hists)
    overallMax = GetOverallMaximum(list_of_significance_hists)

    significance = list_of_significance_hists[0]

    #Prepare lines before changing maximum
    lines_bottom = []
    for i, sig in enumerate(list_of_significance_hists):
        lines_bottom.append(
            TLine(sig.GetXaxis().GetXmin(), sig.GetMaximum(),
                  sig.GetXaxis().GetXmax(), sig.GetMaximum()))
        lines_bottom[i].SetLineColor(TColor.GetColor(GetLineColor(i)))
        lines_bottom[i].SetLineStyle(3)

    #Set Style for bottom plot
    significance.SetTitle(";" + xtitle + "; S/#sqrt{S+B}")
    significance.GetXaxis().SetTitleSize(.12)
    significance.GetYaxis().SetTitleSize(.12)
    significance.GetYaxis().SetTitleOffset(.6)
    significance.GetXaxis().SetLabelSize(.12)
    significance.GetYaxis().SetLabelSize(.12)
    significance.SetMinimum(0.)
    significance.SetMaximum(1.3 * overallMax)
    significance.Draw("EP")

    for sig in list_of_significance_hists[1:]:
        sig.Draw('EPSame')

    for line in lines_bottom:
        line.Draw("same")

    #Set custom x labels
    xaxis = significance.GetXaxis()
    if customLabels != None:
        number_of_bins = significance.GetNbinsX()

        if number_of_bins != len(customLabels):
            if DivideByLine is not None:
                for i in range(number_of_bins):
                    xaxis.SetBinLabel(
                        i + 1, customLabels[i % len(customLabels)]
                    )  #Only works when DivideByLine[0] is an integer
            else:
                print 'Please provide ' + str(
                    number_of_bins) + ' labels instead of ' + str(
                        len(customLabels))
                return
        else:
            for i, label in zip(range(number_of_bins), customLabels):
                xaxis.SetBinLabel(i + 1, label)

    #Throw CMs lumi at it
    cl.CMS_lumi(Canv, 4, 11, year, 'Simulation Preliminary', True)

    #Save everything
    savePlots(Canv, destination)
    ROOT.SetOwnership(
        Canv, False
    )  #https://root-forum.cern.ch/t/tlatex-crashing-in-pyroot-after-many-uses/21638/4
    return
Esempio n. 6
0
	def plotFractionsOfBxId(self):
		##BX right plotting pt
		canvasBxRightPt = TCanvas("cBxRightPt","cBxRightPt",1200,1200)
		canvasBxRightPt.cd().SetLeftMargin(0.15)
		hBxRightPt = self.fileHandler.getHistogram('BxRightGen_Pt').Clone()
		setPlotStyle()
		hBxRightPt.Rebin(50)
		hBxRightPt.GetXaxis().SetRangeUser(0,200)
		hBxRightPt.GetYaxis().SetTitle("normalized Entries / 5 GeV")
		hBxRightPt.GetXaxis().SetTitle("p_{T} Gen")
		hBxRightPt.GetYaxis().SetTitleOffset(2)
		hBxRightPt.SetTitle("Events with right BX ID vs. p_{T}")
		hBxRightPt.SetStats(0)
		hBxRightPt.SetLineWidth(2)
		hBxRightPt.Scale(1/hBxRightPt.Integral())
		hBxRightPt.Draw()
		label = getLabelCmsPrivateSimulation()
		label.Draw()
		self.storeCanvas(canvasBxRightPt,"bxRightPt")
		
		##BX wrong plotting pt
		canvasBxWrongPt = TCanvas("cBxWrongPt","cBxWrongPt",1200,1200)
		canvasBxWrongPt.cd().SetLeftMargin(0.15)
		hBxWrongPt = self.fileHandler.getHistogram('BxWrongGen_Pt').Clone()
		setPlotStyle()
		hBxWrongPt.Rebin(50)
		hBxWrongPt.GetXaxis().SetRangeUser(0,200)
		hBxWrongPt.GetYaxis().SetTitle("normalized Entries / 5 GeV")
		hBxWrongPt.GetXaxis().SetTitle("p_{T} Gen")
		hBxWrongPt.GetYaxis().SetTitleOffset(2)
		hBxWrongPt.SetTitle("Events with wrong BX ID vs. p_{T}")
		hBxWrongPt.SetStats(0)
		hBxWrongPt.SetLineWidth(2)
		hBxWrongPt.Scale(1/hBxWrongPt.Integral())
		hBxWrongPt.DrawCopy()
		label = getLabelCmsPrivateSimulation()
		label.Draw()
		self.storeCanvas(canvasBxWrongPt,"bxWrongPt")

		#Plot the histogram stack
		canvasStack = TCanvas("cStacked","cStacked",1200,1200)
		canvasStack.cd().SetLeftMargin(0.15)
		hWrong = self.fileHandler.getHistogram('BxWrongGen_Pt')
		hRight = self.fileHandler.getHistogram('BxRightGen_Pt')
		hRightFraction = TH1D('hRightFraction','',100,0,500)
		hWrongFraction = TH1D('hWrongFraction','',100,0,500)
		#hWrong.Rebin(50)
		#hRight.Rebin(50)
		#Fill the histograms with the bin wise fractions
		for i in range(0,hRight.GetNbinsX()):
			nRight = hRight.GetBinContent(i+1)
			nWrong = hWrong.GetBinContent(i+1)
			if(nRight + nWrong == 0):
				continue
			hRightFraction.SetBinContent(i+1,nRight/(nRight+nWrong))
			hWrongFraction.SetBinContent(i+1,nWrong/(nRight+nWrong))
		
		#Create the stack
		stack = THStack("hstack","Fractions of events for BX ID correct and wrong")
		nRight = hRight.Integral()
		nWrong = hWrong.Integral()
		hRightFraction.SetLineColor(colorRwthDarkBlue)
		hRightFraction.SetFillColor(colorRwthDarkBlue)
		hRightFraction.SetFillStyle(3002)
		hWrongFraction.SetLineColor(colorRwthMagenta)
		hWrongFraction.SetFillColor(colorRwthMagenta)
		hWrongFraction.SetFillStyle(3002)
		stack.Add(hRightFraction)
		stack.Add(hWrongFraction)
		stack.Draw()
		stack.GetXaxis().SetRangeUser(0.5,201)
		stack.GetYaxis().SetTitle('rel. fraction / 5 GeV')
		stack.GetYaxis().SetTitleOffset(2)
		stack.GetXaxis().SetTitle('p_{T} Gen')
		stack.SetMinimum(0.9)
		stack.SetMaximum(1)
		legend = TLegend(0.6,0.75,0.9,0.9)
		legend.AddEntry(hRightFraction,"BX ID right","f")
		legend.AddEntry(hWrongFraction,"BX ID wrong","f")
		legend.Draw()
		label = getLabelCmsPrivateSimulation()
		label.Draw()
		canvasStack.Update()
		self.storeCanvas(canvasStack,"bxStacked")
noData = False

oneLine = TF1("oneline", "1", -9e9, 9e9)
oneLine.SetLineColor(kBlack)
oneLine.SetLineWidth(1)
oneLine.SetLineStyle(2)

maxVal = stack.GetMaximum()
if not noData:
    maxVal = max(rebinnedData.GetMaximum(), maxVal)

minVal = 0
# minVal = max(stack.GetStack()[0].GetMinimum(),1)
stack.SetMaximum(1.25 * maxVal)
stack.SetMinimum(minVal)

errorband = stack.GetStack().Last().Clone("error")
errorband.Sumw2()
errorband.SetLineColor(kBlack)
errorband.SetFillColor(kBlack)
errorband.SetFillStyle(3245)
errorband.SetMarkerSize(0)

legend.AddEntry(rebinnedData, "Data", 'pe')
legend.AddEntry(errorband, "Uncertainty", "f")

for ih in rebinnedHist:
    legend.AddEntry(rebinnedHist[ih], ih, 'f')

pad1.cd()
Esempio n. 8
0
if drawLog == '1':
    gPad.SetLogy()

if not legOnly:
    hs.Draw("Hist")
for ihist in reversed(templates[4:10]):
    print ihist.GetName()
    if not legOnly:
	ihist.Draw("same, hist")

xTitle = h_top.GetXaxis().GetTitle()
yTitle = h_top.GetYaxis().GetTitle()

if var == 'hCutflow':
    hs.SetMinimum(10000)
    hs.GetXaxis().SetRangeUser(1,10)

setTitle(hs, xTitle, yTitle)
gPad.RedrawAxis()

ll = TLatex()
ll.SetNDC(kTRUE)
ll.SetTextSize(0.05)
ll.DrawLatex(0.63, 0.92, "3000 fb^{-1} (14 TeV)");

prel = TLatex()
prel.SetNDC(kTRUE)
prel.SetTextFont(52)
prel.SetTextFont(52)
prel.SetTextSize(0.05)
Esempio n. 9
0
def draw(hist,
         fit,
         channel,
         data,
         back,
         sign,
         snorm=1,
         lumi=-1,
         ratio=0,
         log=False):
    # If not present, create BkgSum
    if not 'BkgSum' in hist.keys():
        hist['BkgSum'] = hist['data_obs'].Clone(
            "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
        hist['BkgSum'].Reset("MICES")
        for i, s in enumerate(back):
            hist['BkgSum'].Add(hist[s])
    hist['BkgSum'].SetMarkerStyle(0)

    setHistStyle(hist['BkgSum'], 1.1 if ratio else 1.0)
    # Create stack
    bkg = THStack(
        "Bkg", ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";" +
        hist['BkgSum'].GetYaxis().GetTitle())
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    n = len([x for x in data + back + ['BkgSum'] + sign if sample[x]['plot']])
    leg = TLegend(0.69, 0.86 - 0.04 * n, 0.95, 0.86)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pl")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    for i, s in enumerate(sign):
        if sample[s]['plot']:
            leg.AddEntry(hist[s],
                         sample[s]['label'].replace("m_{#Chi}=1 GeV",
                                                    ""), "fl")

    ### data/bkg ratio and systematics
    err = hist['BkgSum'].Clone("BkgErr;")
    err.SetTitle("")
    err.GetYaxis().SetTitle("data / bkg")
    if fit == "prefit":
        err.SetFillColor(ROOT.kOrange - 2)
    elif fit == "postfit":
        err.SetFillColor(ROOT.kBlue)
    err.SetFillStyle(3001)
    for i in range(1, err.GetNbinsX() + 1):
        err.SetBinContent(i, 1)
        if hist['BkgSum'].GetBinContent(i) > 0:
            err.SetBinError(
                i, hist['BkgSum'].GetBinError(i) /
                hist['BkgSum'].GetBinContent(i))
    errLine = err.Clone("errLine")
    errLine.SetLineWidth(1)
    errLine.SetFillStyle(0)
    res = hist['data_obs'].Clone("residues")
    for i in range(0, res.GetNbinsX() + 1):
        if hist['BkgSum'].GetBinContent(i) > 0:
            res.SetBinContent(
                i,
                res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
            res.SetBinError(
                i,
                res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))

    # Legend
    leg1 = TLegend(0.12, 0.45, 0.25, 0.5)
    leg1.SetBorderSize(0)
    leg1.SetFillStyle(0)
    leg1.SetFillColor(0)
    leg1.SetTextSize(0.05)
    leg1.AddEntry(err, "systematic uncertainty (" + fit + ")", "f")

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if ratio else 600)
    gStyle.SetOptStat(0)
    if ratio:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), ratio)
        setBotPad(c1.GetPad(2), ratio)
    c1.cd(1)
    c1.GetPad(bool(ratio)).SetTopMargin(0.08)
    c1.GetPad(bool(ratio)).SetRightMargin(0.05)
    c1.GetPad(bool(ratio)).SetTicks(1, 1)
    if log:
        c1.GetPad(bool(ratio)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    #    hist['BkgSum'].Draw("SAME, E2") # sum of bkg
    if len(data) > 0: hist['data_obs'].Draw("SAME, PE")  # data
    for i, s in enumerate(sign):
        if sample[s]['plot']:
            hist[s].DrawNormalized("SAME, HIST",
                                   hist[s].Integral() * snorm)  # signals
        pass

    bkg.SetMaximum((2. if log else 1.1) * max(
        bkg.GetMaximum(), hist['data_obs'].GetMaximum() +
        hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist['data_obs'].GetMinimum()), 1.e-1) if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetMoreLogLabels(True)

    setHistStyle(bkg, 1.1 if ratio else 1.0)

    leg.Draw()
    #    drawCMS(lumi, "Preliminary",True)
    drawCMS(lumi, "Private", True)
    drawRegion(channel)
    drawAnalysis(channel)
    drawFit(fit)

    if ratio:
        c1.cd(2)
        setBotStyle(err, 3, True)
        setBotStyle(res)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if len(data) > 0:
            res.Draw("SAME, PE0")
            drawRatio(hist['data_obs'], hist['BkgSum'])
            drawKolmogorov(hist['data_obs'], hist['BkgSum'])

        leg1.Draw()

    c1.Update()

    # return list of objects created by the draw() function
    return [c1, bkg, leg, leg1, err, errLine, res]
Esempio n. 10
0
def plot(var, cut, norm=False, nm1=False):
    ### Preliminary Operations ###
    doBinned = False
    if options.mode == "binned": doBinned = True

    fileRead = os.path.exists("combinedCards_" + options.name +
                              "/fitDiagnostics_" + options.file + ".root")
    treeRead = not any(
        x == cut
        for x in ['0l', '1e', '1m', '2e', '2m', '1e1m', 'Gen', 'Trigger'
                  ])  #(var in variable.keys()) # Read from tree

    #signal definition
    if fileRead:
        sign = ['ttDM_MChi1_MPhi200_scalar',
                'tDM_MChi1_MPhi200_scalar']  #for postfit plot
    if not fileRead and not options.limit:
        sign = ['ttDM_MChi1_MPhi100_scalar',
                'tDM_MChi1_MPhi100_scalar']  #for normal plotting
    #bkg definition
    if fileRead or options.limit:
        back = [
            "QCD", "DYJetsToNuNu_HT", "DYJetsToLL_HT", "VV", "ST",
            "WJetsToLNu_HT", "TTbarSL"
        ]  #for postfit or limit
    if (cut).find('>250') or (cut).startswith('AH'):  #for hadronic selections
        back = [
            "QCD", "DYJetsToLL_HT", "VV", "ST", "WJetsToLNu_HT", "TTbarV",
            "TTbar2L", "TTbar1L", "DYJetsToNuNu_HT"
        ]
        if fileRead or options.limit:
            back = [
                "QCD", "DYJetsToLL_HT", "VV", "ST", "WJetsToLNu_HT", "TTbarSL",
                "DYJetsToNuNu_HT"
            ]  #for postfit or limit

    binLow = ""
    binHigh = ""
    binName = ""
    if "binned" in cut:
        binLow = cut[cut.find("LowVal") + 6:cut.find("HighVal") - 1]
        binHigh = cut[cut.find("HighVal") + 7:]
        binName = "bin_" + binLow + "_" + binHigh
        cut = cut[:cut.find("binned")]
    useformula = False
    if 'formula' in variable[var]:
        useformula = True
    channel = cut
    plotdir = cut
    plotname = var
    weight = "eventWeightLumi"  #*(2.2/35.9)
    isBlind = BLIND and ('SR' in channel or 'ps' in channel)
    if fileRead:
        isBlind = False
        options.saveplots = True
        RESIDUAL = True
    elif isBlind:
        RATIO = 0
        SIGNAL = 20
    else:
        RATIO = 4
        SIGNAL = 1
        RESIDUAL = False
    showSignal = True  #('SR' in channel)
    cutSplit = cut.split()
    for s in cutSplit:
        if s in selection.keys():
            plotdir = s
            cut = cut.replace(s, selection[s])
            if not binLow == "":
                cut = cut + " && " + var + " > " + binLow + " && " + var + " < " + binHigh
#if treeRead and cut in selection: cut  = cut.replace(cut, selection[cut])

# Determine Primary Dataset
    pd = []
    if any(w in cut
           for w in ['1l', '1m', '2m', 'isWtoMN', 'isZtoMM', 'isTtoEM']):
        pd += [x for x in sample['data_obs']['files'] if 'SingleMuon' in x]
    if any(w in cut for w in ['1l', '1e', '2e', 'isWtoEN', 'isZtoEE']):
        pd += [x for x in sample['data_obs']['files'] if 'SingleElectron' in x]
    if any(w in cut for w in ['0l', 'isZtoNN']):
        pd += [x for x in sample['data_obs']['files'] if 'MET' in x]
    if len(pd) == 0:
        raw_input("Warning: Primary Dataset not recognized, continue?")

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut
    print "  cut    :", weight

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}

    ### Create and fill MC histograms ###
    for i, s in enumerate(data + back + sign):
        if fileRead:
            var = 'MET_pt'
            if channel.startswith('SL'): var = 'MET_sign'
            if channel.endswith('ZR'): var = 'FakeMET_pt'
            plotname = var

            hist[s] = TH1F(
                s, ";" + variable[var]['title'] + ";Events;" +
                ('log' if variable[var]['log'] else ''),
                variable[var]['nbins'], variable[var]['min'],
                variable[var]['max'])

            if doBinned:
                bins = np.array([])
                if 'bins' in variable[var].keys():
                    bins = np.array(variable[var]['bins'])
                else:
                    binsize = (variable[var]['max'] -
                               variable[var]['min']) / variable[var]['nbins']
                    bins = np.arange(variable[var]['min'],
                                     variable[var]['max'] + binsize, binsize)
                bins = np.append(bins, 10000)

                for i in range(0, len(bins) - 1):
                    rbin = str(bins[i]) + "_" + str(bins[i + 1])
                    fileName = "combinedCards_" + options.name + "/fitDiagnostics_" + options.file + ".root" if not any(
                        t in s for t in ['data', 'tDM']
                    ) else "rootfiles_" + options.name + "/" + channel + "bin_" + rbin + ".root"
                    histName = "shapes_fit_b/" + channel + "bin_" + rbin + "/" + s if not any(
                        t in s for t in ['data', 'tDM']) else s
                    file[s] = TFile(fileName, "READ")
                    tmphist = file[s].Get(histName)

                    if 'data' not in s: hist[s].SetMarkerSize(0)
                    if tmphist:
                        hist[s].SetBinContent(i + 1, tmphist.GetBinContent(1))
                        hist[s].SetBinError(i + 1, tmphist.GetBinError(1))
                    else:
                        hist[s].SetBinContent(i + 1, 0.)
                        hist[s].SetBinError(i + 1, 0.)
            else:
                fileName = "combinedCards_" + options.name + "/fitDiagnostics_" + options.file + ".root" if not s == 'data_obs' else "rootfiles_" + options.name + "/" + channel + binName + ".root"
                histName = "shapes_fit_b/" + channel + "/" + s if not s == 'data_obs' else s
                file[s] = TFile(fileName, "READ")
                tmphist = file[s].Get(histName)

                if tmphist == None:
                    tmphist = hist[back[0]].Clone(s)
                    tmphist.Reset("MICES")
                    print "Histogram", histName, "not found in file", fileName

                if s == 'data_obs': hist[s] = tmphist
                else:
                    hist[s] = hist['data_obs'].Clone(s)
                    hist[s].SetMarkerSize(0)
                    for i in range(tmphist.GetNbinsX() + 1):
                        hist[s].SetBinContent(i + 1,
                                              tmphist.GetBinContent(i + 1))

        elif treeRead:  # Project from tree
            tree[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree[s].Add(NTUPLEDIR + ss + ".root")
            if not binLow == "":
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events;" +
                    ('log' if variable[var]['log'] else ''), 1, float(binLow),
                    float(binHigh))
            elif binLow == "" and variable[var]['nbins'] > 0:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], variable[var]['min'],
                    variable[var]['max'])
            else:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    len(variable[var]['bins']) - 1,
                    array('f', variable[var]['bins']))
            hist[s].Sumw2()
            redFactorString = ""
            redFactorValue = ""
            #if isBlind and 'data' in s:
            if isBlind and 'data' in s and options.limit:
                redFactorString = " && Entry$ % 15 == 1"
            #if isBlind and 'data' not in s:
            if isBlind and 'data' not in s and options.limit:
                redFactorValue = " / 15"
            cutstring = "(" + weight + redFactorValue + ")" + (
                "*(" + cut + redFactorString + ")" if len(cut) > 0 else "")
            if '-' in s:
                cutstring = cutstring.replace(
                    cut, cut + "&& nBQuarks==" + s.split('-')[1][0])
            if useformula == True:
                tree[s].Project(s, variable[var]['formula'], cutstring)
            else:
                tree[s].Project(s, var, cutstring)
            if not tree[s].GetTree() == None:
                hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())
        else:  # Histogram written to file
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    file[ss] = TFile(NTUPLEDIR + ss + ".root", "R")
                    if file[ss].IsZombie():
                        print "WARNING: file", NTUPLEDIR + ss + ".root", "does not exist"
                        continue
                    tmphist = file[ss].Get(cut + "/" + var)
                    if tmphist == None: continue
                    if not s in hist.keys(): hist[s] = tmphist
                    else: hist[s].Add(tmphist)
        if hist[s].Integral() < 0: hist[s].Scale(0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])
        #if 'WJetsToLNu' in s and 'SL' in channel and 'WR' in channel: hist[s].Scale(1.30)
        #if 'TTbar' in s and 'SL' in channel and 'TR' in channel: hist[s].Scale(0.91)

    hist['BkgSum'] = hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s], 1)

    if fileRead:
        hist['PreFit'] = hist['BkgSum'].Clone("PreFit")
        if doBinned:
            for i in range(0, len(bins) - 1):
                rbin = str(bins[i]) + "_" + str(bins[i + 1])
                tmphist = file[back[0]].Get("shapes_prefit/" + channel +
                                            "bin_" + rbin + "/" +
                                            "total_background")

                if tmphist:
                    hist['PreFit'].SetBinContent(i + 1,
                                                 tmphist.GetBinContent(1))
                else:
                    hist['PreFit'].SetBinContent(i + 1, 0.)
        else:
            tmphist = file[back[0]].Get("shapes_prefit/" + channel + "/" +
                                        "total_background")
            for i in range(tmphist.GetNbinsX() + 1):
                hist['PreFit'].SetBinContent(i + 1,
                                             tmphist.GetBinContent(i + 1))
        addOverflow(hist['PreFit'], False)
        hist['PreFit'].SetLineStyle(2)
        hist['PreFit'].SetLineColor(617)  #923
        hist['PreFit'].SetLineWidth(3)
        hist['PreFit'].SetFillStyle(0)
    hist['BkgSum'].SetFillStyle(3002)
    hist['BkgSum'].SetFillColor(1)

    # Create data and Bkg sum histograms
    #    if options.blind or 'SR' in channel:
    #        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
    #        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist[data[0]].SetMarkerStyle(20)
    hist[data[0]].SetMarkerSize(1.25)

    for i, s in enumerate(data + back + sign + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    #for i, s in enumerate(sign): sample[s]['plot'] = True#sample[s]['plot'] and s.startswith(channel[:2])

    if norm:
        for i, s in enumerate(sign):
            hist[s].Scale(hist['BkgSum'].Integral() / hist[s].Integral())
#        for i, s in enumerate(back):
#            hist[s].SetFillStyle(3005)
#            hist[s].SetLineWidth(2)
#        #for i, s in enumerate(sign):
#        #    hist[s].SetFillStyle(0)
#        if not var=="Events":
#            sfnorm = hist[data[0]].Integral()/hist['BkgSum'].Integral()
#            print "Applying SF:", sfnorm
#            for i, s in enumerate(back+['BkgSum']): hist[s].Scale(sfnorm)

    if SIGNAL > 1:
        if not var == "Events":
            for i, s in enumerate(sign):
                hist[s].Scale(SIGNAL)

    # Create stack
    bkg = THStack("Bkg",
                  ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    #leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg = TLegend(0.45, 0.63, 0.93, 0.92)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetNColumns(3)
    leg.SetTextFont(42)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if 'PreFit' not in hist:
        leg.AddEntry(hist['BkgSum'], sample['BkgSum']['label'], "f")
    else:
        leg.AddEntry(hist['BkgSum'], 'MC unc.', "l")
        leg.AddEntry(hist['PreFit'], sample['PreFit']['label'], "l")
    if showSignal:
        for i, s in enumerate(sign):
            if SIGNAL > 1:
                if sample[s]['plot']:
                    leg.AddEntry(hist[s],
                                 '%s (x%d)' % (sample[s]['label'], SIGNAL),
                                 "l")
            else:
                if sample[s]['plot']:
                    leg.AddEntry(hist[s], sample[s]['label'], "l")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        if RESIDUAL:
            c1.Divide(1, 3)
            setFitTopPad(c1.GetPad(1), RATIO)
            setFitBotPad(c1.GetPad(2), RATIO)
            setFitResPad(c1.GetPad(3), RATIO)
        else:
            c1.Divide(1, 2)
            setTopPad(c1.GetPad(1), RATIO)
            setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    log = ("log" in hist['BkgSum'].GetZaxis().GetTitle())
    if log: c1.GetPad(bool(RATIO)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if not isBlind and len(data) > 0:
        graph = fixData(hist[data[0]], USEGARWOOD)
        graph.Draw("SAME, PE")
    #data_graph.Draw("SAME, PE")
    if 'PreFit' in hist: hist['PreFit'].Draw("SAME, HIST")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                hist[s].Draw(
                    "SAME, HIST"
                )  # signals Normalized, hist[s].Integral()*sample[s]['weight']
    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1.075)
    bkg.SetMaximum((5. if log else 1.25) * max(
        bkg.GetMaximum(),
        hist[data[0]].GetBinContent(hist[data[0]].GetMaximumBin()) +
        hist[data[0]].GetBinError(hist[data[0]].GetMaximumBin())))
    if len(sign) > 0 and bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(),
                                                hist[sign[-1]].GetMaximum()):
        bkg.SetMaximum(
            max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()) *
            1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist[data[0]].GetMinimum()), 5.e-1) if log else 0.)
    if log:
        #bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e3)
        bkg.GetYaxis().SetMoreLogLabels(True)
    else:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e3)

    leg.Draw()
    if fileRead and 'SR' in channel:
        drawCMS(LUMI / 15., "Preliminary")
    else:
        drawCMS(LUMI, "Preliminary")
    drawRegion(channel, True)
    drawAnalysis("DM" + channel[:2])
    drawOverflow()

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        if RESIDUAL: setFitBotStyle(err)
        else: setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist[data[0]].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        if RESIDUAL: setFitBotStyle(res)
        else: setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        if 'PreFit' in hist:
            respre = hist[data[0]].Clone("ResiduesPreFit")
            respre.Divide(hist['PreFit'])
            respre.SetLineStyle(2)
            respre.SetLineColor(617)  #923
            respre.SetLineWidth(3)
            respre.SetFillStyle(0)
            respre.Draw("SAME, HIST")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])

    c1.Update()

    if RATIO and RESIDUAL:
        c1.cd(3)
        c1.SetGrid(1, 0)
        resFit = hist[data[0]].Clone("Residues")
        resFit.Reset("MICES")
        resFit.SetTitle("")
        #resFit.GetYaxis().SetTitle("Residuals")
        resFit.GetYaxis().SetTitle(
            "#frac{Data - Bkg}{#sqrt{#sigma_{Data}^{2}+#sigma_{Bkg}^{2}}}")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                resFit.SetBinContent(
                    i, (hist[data[0]].GetBinContent(i) -
                        hist['BkgSum'].GetBinContent(i)) / (math.sqrt(
                            math.pow(hist['BkgSum'].GetBinError(i), 2) +
                            math.pow(hist[data[0]].GetBinError(i), 2))))
        setFitResStyle(resFit)
        resFit.SetLineColor(15)
        resFit.SetFillColor(15)
        resFit.SetFillStyle(1001)
        resFit.Draw("HIST")

        resFitLine = resFit.Clone("resFitLine")
        resFitLine.SetLineWidth(1)
        resFitLine.SetFillStyle(0)
        resFitLine.Draw("SAME, HIST")

    c1.Update()

    if gROOT.IsBatch(
    ) and options.saveplots:  # and (treeRead and channel in selection.keys()):
        AddString = ""
        if not os.path.exists("plots_" + options.name + "/" + plotdir):
            os.makedirs("plots_" + options.name + "/" + plotdir)
        if fileRead:
            if RESIDUAL: AddString = "_PostFit_Residual"
            else: AddString = "_PostFit"
        #c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+AddString+".png")
        c1.Print("plots_" + options.name + "/" + plotdir + "/" + plotname +
                 binName + AddString + ".pdf")

    # Print table
    printTable(hist, sign)

    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")

    if gROOT.IsBatch() and not fileRead and (
            var == 'MET_pt' or
        (channel.startswith('SL') and var == 'MET_sign') or
        (channel.endswith('ZR') and var == 'FakeMET_pt')):
        saveHist(hist, channel + binName)
Esempio n. 11
0
        drawResoLin(histLin, legTitle, "#LT E_{pred}#GT/E_{true}",
                    "lin_stage_" + str(st), False, 0.95, 1.05, False)

    tfile.Close()

r.gROOT.SetBatch(False)
can = TCanvas("can", "")
multi.Draw("nostack")
multi.GetXaxis().SetTitle("E [GeV]")
multi.GetYaxis().SetTitle("#sigma_{E_{pred}}/#LT E_{pred}#GT")
multi.GetXaxis().SetRangeUser(0, 102)
multi.SetMaximum(0.1)
multi.Draw("nostack")
leg.Draw()
legT.Draw()
can.Print("plots/allStages_reso.pdf")

canLin = TCanvas("canLin", "")
multiLin.Draw("nostack")
multiLin.GetXaxis().SetTitle("E [GeV]")
multiLin.GetYaxis().SetTitle("#LT E_{pred}#GT/E_{true}")
multiLin.GetXaxis().SetRangeUser(0, 102)
multiLin.SetMaximum(1.15)
multiLin.SetMinimum(0.95)
multiLin.Draw("nostack")
leg.Draw()
legT.Draw()
canLin.Print("plots/allStages_lin.pdf")

input("Press a key!")
Esempio n. 12
0
def plot(var, cut, nm1=False):
    ### Preliminary Operations ###
    treeRead = True if not FILE else False  # Read from tree
    channel = cut
    isBlind = BLIND
    showSignal = False if 'SB' in cut or 'TR' in cut else True

    # Determine explicit cut
    if treeRead:
        for k in sorted(alias.keys(), key=len, reverse=True):
            if k in cut: cut = cut.replace(k, alias[k])

    # Determine Primary Dataset
    pd = []
    if "isSingleMuonPhotonTrigger" in cut:
        pd = [x for x in sample['data_obs']['files'] if "MuonEG" in x]
    elif "isJPsiTrigger" in cut:
        pd = [x for x in sample['data_obs']['files'] if "Charmonium" in x]
    else:
        print "Cannot determine Primary Dataset."
        exit()

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut

    if isBlind and "SR" in channel and var in ["H_mass"]:
        cut += " && ( isMC ? 1 : !(H_mass > 86 && H_mass < 96) && !(H_mass > 120 && H_mass < 130) )"

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}
    cutstring = "(eventWeightLumi)" + ("*(" + cut +
                                       ")" if len(cut) > 0 else "")

    ### Create and fill MC histograms ###
    for i, s in enumerate(data + back + sign):
        hist[s] = TH1F(
            s, ";" + variable[var]['title'] + ";Events;" +
            ('logx' if variable[var]['logx'] else '') +
            ('logy' if variable[var]['logy'] else ''), variable[var]['nbins'],
            variable[var]['min'], variable[var]['max'])
        hist[s].Sumw2()
        tree[s] = TChain("Events")
        for j, ss in enumerate(sample[s]['files']):
            if s in data and not ss in pd: continue
            if YEAR == 2016 and not ('Run2016' in ss or 'Summer16' in ss):
                continue
            if YEAR == 2017 and not ('Run2017' in ss or 'Fall17' in ss):
                continue
            if YEAR == 2018 and not ('Run2018' in ss or 'Autumn18' in ss):
                continue
            for f in os.listdir(NTUPLEDIR + '/' + ss):
                tree[s].Add(NTUPLEDIR + '/' + ss + '/' + f)
        tree[s].Project(s, var, cutstring)
        if not tree[s].GetTree() == None:
            hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())

#    jobs = []
#    queue = multiprocessing.Queue()
#    for i, s in enumerate(data+back+sign):
#        for j, ss in enumerate(sample[s]['files']):
#            if s in data and not ss in pd: continue
#            if YEAR == 2016 and not ('Run2016' in ss or 'Summer16' in ss): continue
#            if YEAR == 2017 and not ('Run2017' in ss or 'Fall17' in ss): continue
#            if YEAR == 2018 and not ('Run2018' in ss or 'Autumn18' in ss): continue
#            if treeRead: # Project from tree
##                hist[s] = loopProject(s, ss, variable[var], cutstring, True)
#                p = multiprocessing.Process(target=parallelProject, args=(queue, s, ss, variable[var], cutstring, ))
#                jobs.append(p)
#                p.start()
#            else: # Histogram written to file
#                hist[s] = readhist(FILE, s, var, cut)
#
#    # Wait for all jobs to finish
#    for job in jobs:
#        h = queue.get()
#        if not h.GetOption() in hist: hist[h.GetOption()] = h
#        else: hist[h.GetOption()].Add(h)
#    for job in jobs:
#        job.join()

# Histogram style
    for i, s in enumerate(data + back + sign):
        hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'] if not options.norm else 0)
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])
        hist[s].SetLineWidth(sample[s]['linewidth'])

    ### Create Bkg Sum histogram ###
    hist['BkgSum'] = hist['data_obs'].Clone(
        "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s])

    if options.norm:
        for i, s in enumerate(back + ['BkgSum']):
            hist[s].Scale(hist[data[0]].Integral() / hist['BkgSum'].Integral())
        for i, s in enumerate(sign):
            hist[s].Scale(hist[data[0]].Integral() / hist[s].Integral())

    # Create data and Bkg sum histograms
#    if BLIND: # or 'SR' in channel:
#        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
#        hist['data_obs'].Reset("MICES")
# Set histogram style
    hist['data_obs'].SetMarkerStyle(20)
    hist['data_obs'].SetMarkerSize(1.25)

    #    for i, s in enumerate(data+back+sign+['BkgSum']): addOverflow(hist[s], False) # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    for i, s in enumerate(sign):
        sample[s]['plot'] = True

    # Create stack
    bkg = THStack("Bkg",
                  ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                leg.AddEntry(hist[s], sample[s]['label'], "fl")

    leg.SetY1(0.9 - leg.GetNRows() * 0.04)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    logX, logY = "logx" in hist['BkgSum'].GetZaxis().GetTitle(
    ), "logy" in hist['BkgSum'].GetZaxis().GetTitle()
    if logY: c1.GetPad(bool(RATIO)).SetLogy()
    if logX: c1.GetPad(bool(RATIO)).SetLogx()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if len(data) > 0: hist['data_obs'].Draw("SAME, PE")  # data
    #data_graph.Draw("SAME, PE")
    #    if showSignal:
    #        smagn = 1. #if treeRead else 1.e2 #if logY else 1.e2
    for i, s in enumerate(sign):
        if sample[s]['plot']: hist[s].Draw("SAME, HIST")


#                hist[s].Scale(smagn)
#                hist[s].Draw("SAME, HIST") # signals Normalized, hist[s].Integral()*sample[s]['weight']
#        #textS = drawText(0.80, 0.9-leg.GetNRows()*0.05 - 0.02, stype+" (x%d)" % smagn, True)
    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1.075)
    bkg.SetMaximum((5. if logY else 1.25) * max(
        bkg.GetMaximum(),
        hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) +
        hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
    #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist['data_obs'].GetMinimum()), 5.e-1) if logY else 0.)
    if logY:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetMoreLogLabels(True)

    #if logY: bkg.SetMinimum(1)
    leg.Draw()
    drawCMS(LUMI[YEAR], "Preliminary")
    if channel in aliasNames: drawRegion(aliasNames[channel], True)
    #drawAnalysis(channel)

    #if nm1 and not cutValue is None: drawCut(cutValue, bkg.GetMinimum(), bkg.GetMaximum()) #FIXME
    #if len(sign) > 0:
    #    if channel.startswith('X') and len(sign)>0: drawNorm(0.9-0.05*(leg.GetNRows()+1), "#sigma(X) = %.1f pb" % 1.)

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        if logX: c1.GetPad(2).SetLogx()
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist['data_obs'].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])

    if var in ["H_mass"]:
        c1.cd(bool(RATIO))
        boxZ = drawBox(XZMIN, hist['data_obs'].GetMinimum(), XZMAX,
                       hist['data_obs'].GetMaximum() / 1.30, "Z")
        boxH = drawBox(XHMIN, hist['data_obs'].GetMinimum(), XHMAX,
                       hist['data_obs'].GetMaximum() / 1.30, "H")

    c1.Update()

    if True:  #gROOT.IsBatch():
        varname = var.replace('.', '_').replace('()', '')
        if not os.path.exists("plots/" + channel):
            os.makedirs("plots/" + channel)
        c1.Print("plots/" + channel + "/" + varname + ".png")
        c1.Print("plots/" + channel + "/" + varname + ".pdf")

    # Print table
    printTable(hist, sign)

    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
Esempio n. 13
0
def main():

   from optparse import OptionParser
   parser = OptionParser()
   parser.add_option("-i", "--inputfile", dest="inputfile")
   parser.add_option("-N", "--multiplicity", dest="N", type="int", default=3)
   parser.add_option("-x", "--exclusive", action="store_true",\
         dest="isExclusive", default=False)
   parser.add_option("-l", "--label", dest="label", type="string", default="")
   (options, args) = parser.parse_args()

   N = options.N
   isExclusive = options.isExclusive
   label_text = options.label

   if isExclusive and not (N == 2 or N == 3):
      parser.error("Exclusive plot only for N =2 or 3")

   import configurations as config
   from ROOT import TFile, TCanvas, THStack, TLegend, TPaveText, gStyle
   from ModelParser import ModelKey

   gStyle.SetPadTopMargin(0.05)
   gStyle.SetPadRightMargin(0.05)
   gStyle.SetErrorX(0.)

   suffix = ""
   if not isExclusive:
      suffix = "up"

   sm_files = []
   for model in config.sm_models:
      f = TFile("%s/%s.root" % (config.sm_dir, model), "READ")
      sm_files.append(f)

   bh_weights = []
   bh_files = []
   from BHXsec import BHXsec
   xsec = BHXsec()
   for model in config.bh_showcase:
      f = TFile("%s/%s.root" % (config.bh_dir, model), "READ")
      bh_files.append(f)
      h = f.Get("plotsNoCut/ST")
      nEvents= h.GetEntries()
      bh_weights.append(xsec.get(model) / nEvents * config.integrated_luminosity)

   c = TCanvas("ST_Mul%d%s" % (N, suffix),
         "ST_Mul%d%s" % (N, suffix), 500, 500)
   hs = THStack()
   hs1 = THStack()

   infile = TFile(options.inputfile, "READ")
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   gBkg = infile.Get("BackgroundGraph_N%d%s" % (N, suffix))
   hData = infile.Get("Data_N%d%s" % (N, suffix))
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   hBkg.SetMarkerSize(0)
   hBkg_ = hBkg.Clone("BkgLine")
   hBkg.SetFillColor(33)
   hBkg.SetLineColor(33)
   hBkg_.SetLineWidth(3)
   hBkg_.SetLineColor(862)
   hs.Add(hBkg, "e3")

   #legend = TLegend(0.2826613,0.4819492,0.6094355,0.9416102) # - only for N >= 2 and 3
   legend = TLegend(0.3026613,0.5519492,0.6094355,0.9416102) # was 0.4919...
   legend.SetTextSize(0.041); #was 0.02966102
   legend.SetTextFont(42);
   legend.SetFillColor(0)
   legend.SetLineColor(0)
   if isExclusive:
      legend.SetHeader("Multiplicity N = %d" % N)
   else:
      legend.SetHeader("Multiplicity N #geq %d" % N)
   legend.AddEntry(hData, "Data", "lep")
   legend.AddEntry(hBkg_, "Background", "l")
   legend.AddEntry(hBkg, "Uncertainty", "f")

   legend_sm = TLegend(0.6271774,0.7369492,0.8308065,0.8771186)
   legend_sm.SetTextSize(0.041);
   legend_sm.SetTextFont(42);
   legend_sm.SetFillColor(0)
   legend_sm.SetLineColor(0)

   for i, f in enumerate(bh_files):
      h = f.Get("plotsN%d%s/ST" % (N, suffix))
      h.Rebin(config.rebin)
      h.Scale(bh_weights[i])

      # Add background
      for ibin in range(h.GetNbinsX()):
         h.SetBinContent(ibin+1,\
               h.GetBinContent(ibin+1)\
               + hBkg.GetBinContent(ibin+1))

         h.SetLineWidth(3)
         #h.SetLineColor(i+2)
         h.SetLineStyle(i+2)

         if i == 0:
            h.SetLineColor(814)
         if i == 1:
            h.SetLineStyle(5)
            h.SetLineColor(899)
         if i == 2:
            h.SetLineStyle(9)
            h.SetLineColor(4)
         if i == 3:
            h.SetLineStyle(3)
            h.SetLineColor(614)

      hs.Add(h, "hist")
      model = ModelKey(config.bh_showcase[i])
      bh_legend = "M_{D} = %.1f TeV, M_{BH}^{ min} = %.1f TeV, n = %d" % (\
            model.parameter["MD"],
            model.parameter["M"],
            model.parameter["n"])
      if i == 3:
         bh_legend = "M_{D} = 3.0 TeV, M_{QBH}^{ min} = 4.0 TeV, n = 4"    

      legend.AddEntry(h, bh_legend, "l")

#      qbh_legend = "M_{D} = 4.0 TeV, M_{QBH}^{ min} = 5.0 TeV, n = 5"

#      legend.AddEntry(h, qbh_legend, "l")

   if isExclusive:
      for i, f in enumerate(sm_files):
         h = f.Get("plotsN%d%s/ST" % (N, suffix))
         h.Rebin(config.rebin)
         h.Scale(config.integrated_luminosity)
         h.SetFillColor(config.sm_colors[i])
         h.SetLineColor(config.sm_colors[i])
         hs1.Add(h, "hist")
         legend_sm.AddEntry(h, config.sm_models[i], "f")
   
   #hs.Add(hData, "e")   
   
   hs.Draw("nostack")
   hs1.Draw("same")  
   c.SetLogy(1)
   hs.GetXaxis().SetTitle("S_{T} (GeV)")
   hs.GetYaxis().SetTitle(hData.GetYaxis().GetTitle())
   hs.GetYaxis().SetTitleOffset(1.25)

   hs.GetYaxis().SetTitleSize(0.04)
   hs.GetYaxis().SetLabelSize(0.04)
   hs.GetXaxis().SetTitleSize(0.04)
   hs.GetXaxis().SetLabelSize(0.04)
    
   ibin = 0
   #if isExclusive:
   #   hs.GetXaxis().SetRangeUser(config.fit_range[0], config.maxST)
   #   ibin = hData.FindBin(config.fit_range[0])
   #else:
   #   hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
   #   ibin = hData.FindBin(config.norm_range[0])
   
   if isExclusive:
      hs.GetXaxis().SetRangeUser(1800, config.maxST)
      ibin = hData.FindBin(1800)
   else:
      hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
      ibin = hData.FindBin(config.norm_range[0])

   from Styles import formatUncertainty
   formatUncertainty(gBkg)
   gBkg.Draw("LX")
   hData.Draw("sameex0")

   hs.SetMinimum(5e-1)
   if isExclusive:
      hs.SetMaximum(hData.GetBinContent(ibin) * 40)
   else:
      #hs.SetMaximum(4e4)
      hs.SetMaximum(hData.GetBinContent(ibin) * 20) # or 1e7 for N>=3 and use 4 models

   legend.Draw("plain")
   if isExclusive:
      legend_sm.Draw("plain")

   if isExclusive:
      cmslabel =TPaveText(0.45,0.96,0.60,0.99,"brNDC");
   else:
      cmslabel = TPaveText(0.45,0.96,0.60,0.99,"brNDC")
   cmslabel.AddText(config.cmsTitle)
   #cmslabel.AddText(config.cmsSubtitle)
   cmslabel.SetFillColor(0)
   cmslabel.SetTextSize(0.041)
   cmslabel.Draw("plain")

   label = TPaveText(0.8891129,0.8644068,0.9435484,0.9258475,"brNDC")
   label.SetFillColor(0)
   label.SetTextSize(0.0529661);
   label.AddText(label_text);
   label.Draw("plain")

   #block1 =TPaveText(0.333,0.84,0.354,0.86,"brNDC"); # for N>=2 and >=3 only
   block1 =TPaveText(0.351,0.85,0.37,0.87,"brNDC");
   block1.SetFillColor(0)
   block1.Draw("plain")

   #block2 =TPaveText(0.295,0.84,0.315,0.86,"brNDC"); # for N>=2 and >=3 only
   block2 =TPaveText(0.314,0.85,0.332,0.87,"brNDC");
   block2.SetFillColor(0)
   block2.Draw("plain")
   
   if isExclusive:
     c.Print("ST_Mul%d.pdf" % N)
     c.Print("ST_Mul%d.png" % N)
   else:
     c.Print("ST_Mul%dup.pdf" % N)
     c.Print("ST_Mul%dup.png" % N)    
   c.Update()

   raw_input("Press Enter to continue...")
Esempio n. 14
0
def plotRatio(name, h1, v_hist, hs, log):
    c = Canvas('c')

    h_sum = TH1F('h_sum','sum',h1.GetNbinsX(), h1.GetXaxis().GetXmin(), h1.GetXaxis().GetXmax())
    for h in v_hist: h_sum.Add(h)
 
    pad1 = TPad('pad1', 'pad1', 0.0, 0.3, 1., 1.)
    pad1.SetBottomMargin(0.005)
    pad1.SetTicks(1,1)
    pad1.Draw()

    c.cd()
    pad2 = TPad('pad2', 'pad2', 0., 0.05, 1., 0.28)
    pad2.SetTopMargin(0.005)
    pad2.SetBottomMargin(0.3)
    pad2.SetTicks(1,1)
    pad2.Draw()

    pad1.cd()
    if log: pad1.SetLogy()
    stack = THStack('norm_stack','')
    h_mc_err = 0
    for h in v_hist: 
        stack.Add(h)
        if h_mc_err == 0: h_mc_err = h.Clone()
        else: h_mc_err.Add(h)
    stack.Draw('HIST')
    h_mc_err.Draw("e2 same")
    h_mc_err.SetMarkerSize(0)
    h_mc_err.SetFillColor(1)
    h_mc_err.SetFillStyle(3004)
    h_data = asym_error_bars(h1)
    h_data.SetLineColor(ROOT.kBlack)
    h_data.SetFillColor(ROOT.kBlack)
    h_data.SetLineWidth(2)
    h_data.Draw('p e2 same')
    hs.Draw('HIST same')
    ymax = h1.GetMaximum()*pow(10,1.0) if log else h1.GetMaximum()*1.5 
    stack.SetMaximum(ymax)
    if log: stack.SetMinimum(1)
    #stack.GetHistogram().GetYaxis().SetTitleFont(43)
    stack.GetHistogram().GetYaxis().SetTitleOffset(1.0)
    stack.GetHistogram().GetYaxis().SetTitleSize(0.05)
    stack.GetHistogram().GetYaxis().SetTitle('Events')
    pLabel, sLabel, lLabel = prelimLabel('left',log,h1.GetMaximum()), selectionLabel(lab,True,log,h1.GetMaximum()), lumiLabel(True,years)
    pLabel.Draw(), sLabel.Draw(), lLabel.Draw()
    legend = makeLegend(h1,v_hist,hs)
    legend.Draw()

    pad2.cd()
    h_ratio = h1.Clone('h_ratio')

    h_ratio.Sumw2()
    h_ratio.Divide(h_sum)
    h_ratio.SetMinimum(-0.499)
    h_ratio.SetMaximum(2.499)
    h_ratio.SetLineColor(ROOT.kBlack)
    h_ratio.SetMarkerStyle(20)
    h_ratio.SetMarkerColor(ROOT.kBlack)
    h_ratio.SetMarkerSize(0.7)
    h_ratio.Draw('p same')
    denom_err, denom_err2 = h_mc_err.Clone(), h_mc_err.Clone()
    denom_err2.Sumw2(False)
    denom_err.Divide(denom_err2)
    denom_err.Draw("e2same")
    denom_err.SetFillColor(1)
    denom_err.SetFillStyle(3004)

    l1 = TLine(h_ratio.GetXaxis().GetXmin(), 1, h_ratio.GetXaxis().GetXmax(), 1)
    l2 = TLine(h_ratio.GetXaxis().GetXmin(), 1.5, h_ratio.GetXaxis().GetXmax(), 1.5)
    l3 = TLine(h_ratio.GetXaxis().GetXmin(), 0.5, h_ratio.GetXaxis().GetXmax(), 0.5)
    l4 = TLine(h_ratio.GetXaxis().GetXmin(), 0., h_ratio.GetXaxis().GetXmax(), 0.)
    l5 = TLine(h_ratio.GetXaxis().GetXmin(), 2, h_ratio.GetXaxis().GetXmax(), 2.)
    l2.SetLineStyle(3), l3.SetLineStyle(3), l4.SetLineStyle(3), l5.SetLineStyle(3)
    l1.Draw(), l2.Draw(), l3.Draw(), l4.Draw(), l5.Draw()

    Prettify( h_ratio )

    #ax  = h_ratio.GetXaxis();
    #ax.SetNdivisions(506);

    if len(years) == 1: c.SaveAs('plots/%s/%s_%s.%s' % (years[0],name,selection,extension))
    else: c.SaveAs('plots/combined/%s_%s_%s.%s' % (name,selection,s_years,extension)) 
Esempio n. 15
0
def thresholds(var, title, xtitle, nbin, low, high, region):
    cutter = cuts[region]
    weight = cutter
    can = TCanvas("can", "can", 1000, 900)
    can.SetBottomMargin(0.3)
    can.SetRightMargin(0.06)
    can.cd()
    gPad.SetLogy(1)
    gStyle.SetOptStat(0)
    backs = TH1D('a', 'a', nbin, low, high)
    backs.SetFillStyle(3344)
    backs.SetFillColor(1)
    phojet = TH1D('b', 'b', nbin, low, high)
    phojet.SetFillColor(7)
    phojet.SetLineColor(7)
    qcd = TH1D('c', 'c', nbin, low, high)
    qcd.SetFillColor(5)
    qcd.SetLineColor(5)
    stack = THStack('d', title + ";" + xtitle + ";" + "Events")
    Variables = {}
    print "Variable: " + var
    for tre in List:
        histName = var + tre
        Variables[tre] = TH1F(histName, ";" + xtitle + ";" + "Events", nbin,
                              low, high)
        #		if tre == "dipho":
        #			weight = cutter+"&& (JetPho_dR<0.1)"
        treepj[tre].Draw(var + ">>" + histName, weight)
        Variables[tre].Sumw2()
        scalable = lumi * crossx[tre] * kFact[tre] / Nevents[tre]
        Variables[tre].Scale(scalable)
        if tre.startswith("df"):
            Variables[tre].SetLineColor(colors[tre])
            Variables[tre].SetLineWidth(3)
        if tre.startswith("GJets"):
            backs.Add(Variables[tre])
            phojet.Add(Variables[tre])
        if tre.startswith("QCD"):
            backs.Add(Variables[tre])
            qcd.Add(Variables[tre])
        if tre.startswith("DiPhoton"):
            Variables[tre].SetFillColor(3)
            Variables[tre].SetLineColor(3)
            backs.Add(Variables[tre])
    Variables['Data'] = TH1F('data', ";" + xtitle + ";" + "Events", nbin, low,
                             high)
    treepj['Data'].Draw(var + ">>data", cutter, "goff")
    Variables['Data'].SetLineColor(1)
    Variables['Data'].SetLineWidth(2)
    stack.Add(Variables['DiPhoton'])
    stack.Add(qcd)
    stack.Add(phojet)
    stack.SetMaximum(1000000.0)
    stack.SetMinimum(0.001)
    stack.Draw("hist")
    stack.GetXaxis().SetLabelSize(0)
    backs.Draw("e2same")
    Variables['Data'].Draw("e1same")
    Variables['df1en0'].Draw("histsame")
    Variables['df1en1'].Draw("histsame")
    Variables['df1en2'].Draw("histsame")
    Variables['df1en3'].Draw("histsame")
    pad = TPad("pad", "pad", 0.0, 0.0, 1.0, 1.0)
    pad.SetTopMargin(0.7)
    pad.SetFillColor(0)
    pad.SetGridy(1)
    pad.SetFillStyle(0)
    pad.Draw()
    pad.cd(0)
    pad.SetRightMargin(0.06)
    Pull = TH1D('Pull', 'Pull', nbin, low, high)
    Pull = Variables['Data'].Clone()
    Pull.Divide(backs)
    Pull.SetMarkerStyle(20)
    Pull.SetMaximum(2.0)
    Pull.SetMinimum(0.0)
    Pull.SetFillColor(2)
    Pull.GetXaxis().SetTitle(xtitle)
    Pull.GetYaxis().SetTitleSize(0.04)
    Pull.GetYaxis().SetTitle('Data/MC')
    Pull.SetMarkerSize(0.7)
    Pull.GetYaxis().SetNdivisions(5)
    line = TF1("line", "1", low, high)
    line.SetLineColor(2)
    line.SetLineWidth(2)
    Pull.Draw("e1")
    line.Draw('same')
    latex2 = TLatex()
    latex2.SetNDC()
    latex2.SetTextSize(0.035)
    latex2.SetTextAlign(31)  # align right
    latex2.DrawLatex(
        0.87, 0.95,
        "Work In Progress, " + str(lumi) + " fb^{-1} at #sqrt{s} = 13 TeV")
    led = TLegend(0.7, 0.7, 0.94, 0.9)
    led.AddEntry(phojet, "#gamma + Jet", 'f')
    led.AddEntry(qcd, "QCD DiJet", 'f')
    led.AddEntry(Variables['DiPhoton'], "#gamma + #gamma", 'f')
    led.AddEntry(Variables['df1en0'], "f_{D} = 1E0")
    led.AddEntry(Variables['df1en1'], "f_{D} = 1E-1")
    led.AddEntry(Variables['df1en2'], "f_{D} = 1E-2")
    led.AddEntry(Variables['df1en3'], "f_{D} = 1E-3")
    led.AddEntry(Variables['Data'], "Data")
    led.SetFillColor(0)
    led.Draw("same")
    can.SaveAs("plots/" + region + "-" + var + ".pdf")
    can.Close()
    phojet.Delete()
    qcd.Delete()
Esempio n. 16
0
def Direct_Estimator(var, cut, year):
    from root_numpy import root2array, fill_hist, array2root
    import numpy.lib.recfunctions as rfn
    ### Preliminary Operations ###
    treeRead = not cut in [
        "nnqq", "en", "enqq", "mn", "mnqq", "ee", "eeqq", "mm", "mmqq", "em",
        "emqq", "qqqq"
    ]  # Read from tree
    channel = cut
    unit = ''
    if "GeV" in variable[var]['title']: unit = ' GeV'
    isBlind = BLIND and 'SR' in channel
    isAH = False  #'qqqq' in channel or 'hp' in channel or 'lp' in channel
    showSignal = False if 'SB' in cut or 'TR' in cut else True  #'SR' in channel or channel=='qqqq'#or len(channel)==5
    stype = "HVT model B"
    if len(sign) > 0 and 'AZh' in sign[0]: stype = "2HDM"
    elif len(sign) > 0 and 'monoH' in sign[0]: stype = "Z'-2HDM m_{A}=300 GeV"
    if treeRead:
        for k in sorted(alias.keys(), key=len, reverse=True):
            if BTAGGING == 'semimedium':
                if k in cut:
                    cut = cut.replace(k, aliasSM[k])

            else:
                if k in cut:
                    cut = cut.replace(
                        k, alias[k].format(WP=working_points[BTAGGING]))

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  cut    :", cut

    if var == 'jj_deltaEta_widejet':
        if "jj_deltaEta_widejet<1.1 && " in cut:
            print
            print "omitting jj_deltaEta_widejet<1.1 cut to draw the deltaEta distribution"
            print
            cut = cut.replace("jj_deltaEta_widejet<1.1 && ", "")
        else:
            print
            print "no 'jj_deltaEta_widejet<1.1 && ' in the cut string detected, so it cannot be ommited explicitly"
            print

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}

    ### Create and fill MC histograms ###
    for i, s in enumerate(back + sign):
        if True:  #FIXME

            if variable[var]['nbins'] > 0:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events / ( " + str(
                        (variable[var]['max'] - variable[var]['min']) /
                        variable[var]['nbins']) + unit + " );" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], variable[var]['min'],
                    variable[var]['max'])
            else:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events" +
                    ('log' if variable[var]['log'] else ''),
                    len(variable[var]['bins']) - 1,
                    array('f', variable[var]['bins']))
            hist[s].Sumw2()

            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s:
                    if year == "run2" or year in ss:
                        arr = root2array(
                            NTUPLEDIR + ss + ".root",
                            branches=[
                                var, "jpt_1", "jpt_2", "eventWeightLumi",
                                "TMath::Abs(jflavour_1)==5 && TMath::Abs(jflavour_2)==5",
                                "TMath::Abs(jflavour_1)==5 && TMath::Abs(jflavour_2)!=5",
                                "TMath::Abs(jflavour_1)!=5 && TMath::Abs(jflavour_2)==5",
                                "TMath::Abs(jflavour_1)!=5 && TMath::Abs(jflavour_2)!=5"
                            ],
                            selection=cut if len(cut) > 0 else "")
                        print "imported " + NTUPLEDIR + ss + ".root"
                        arr.dtype.names = [
                            var, "jpt_1", "jpt_2", "eventWeightLumi", "bb",
                            "bq", "qb", "qq"
                        ]
                        MANtag_eff1 = np.array(map(MANtag_eff, arr["jpt_1"]))
                        MANtag_eff2 = np.array(map(MANtag_eff, arr["jpt_2"]))
                        MANtag_mis1 = np.array(map(MANtag_mis, arr["jpt_1"]))
                        MANtag_mis2 = np.array(map(MANtag_mis, arr["jpt_2"]))
                        MANtag_weight = np.multiply(
                            arr["eventWeightLumi"],
                            np.multiply(arr['bb'],
                                        np.multiply(MANtag_eff1, MANtag_eff2))
                            + np.multiply(
                                arr['bq'], np.multiply(MANtag_eff1,
                                                       MANtag_mis2)) +
                            np.multiply(arr['qb'],
                                        np.multiply(MANtag_mis1,
                                                    MANtag_eff2)) +
                            np.multiply(arr['qq'],
                                        np.multiply(MANtag_mis1, MANtag_mis2)))
                        fill_hist(hist[s], arr[var], weights=MANtag_weight)
                        deepCSV_eff1 = np.array(map(deepCSV_eff, arr["jpt_1"]))
                        deepCSV_eff2 = np.array(map(deepCSV_eff, arr["jpt_2"]))
                        deepCSV_mis1 = np.array(map(deepCSV_mis, arr["jpt_1"]))
                        deepCSV_mis2 = np.array(map(deepCSV_mis, arr["jpt_2"]))
                        deepCSV_weight = np.multiply(
                            arr["eventWeightLumi"],
                            np.multiply(
                                arr['bb'],
                                np.multiply(deepCSV_eff1, deepCSV_eff2)) +
                            np.multiply(
                                arr['bq'],
                                np.multiply(deepCSV_eff1, deepCSV_mis2)) +
                            np.multiply(
                                arr['qb'],
                                np.multiply(deepCSV_mis1, deepCSV_eff2)) +
                            np.multiply(
                                arr['qq'],
                                np.multiply(deepCSV_mis1, deepCSV_mis2)))

                        if var == "jj_mass_widejet" and options.save and not "data" in ss:
                            arr = rfn.append_fields(arr,
                                                    "MANtag_weight",
                                                    MANtag_weight,
                                                    usemask=False)
                            arr = rfn.append_fields(arr,
                                                    "deepCSV_weight",
                                                    deepCSV_weight,
                                                    usemask=False)
                            array2root(arr,
                                       NTUPLEDIR + "MANtag/" + ss + "_" +
                                       BTAGGING + ".root",
                                       treename="tree",
                                       mode='recreate')
                            print "saved as", NTUPLEDIR + "MANtag/" + ss + "_" + BTAGGING + ".root"
                        arr = None

        hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])

    if channel.endswith('TR') and channel.replace('TR', '') in topSF:
        hist['TTbarSL'].Scale(topSF[channel.replace('TR', '')][0])
        hist['ST'].Scale(topSF[channel.replace('TR', '')][0])

    hist['BkgSum'] = hist['data_obs'].Clone(
        "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s])

    # Create data and Bkg sum histograms
    if options.blind or 'SR' in channel:
        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist['data_obs'].SetMarkerStyle(20)
    hist['data_obs'].SetMarkerSize(1.25)

    for i, s in enumerate(back + sign + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    for i, s in enumerate(sign):
        sample[s][
            'plot'] = True  #sample[s]['plot'] and s.startswith(channel[:2])

    if isAH:
        for i, s in enumerate(back):
            hist[s].SetFillStyle(3005)
            hist[s].SetLineWidth(2)
        #for i, s in enumerate(sign):
        #    hist[s].SetFillStyle(0)
        if not var == "Events":
            sfnorm = hist[data[0]].Integral() / hist['BkgSum'].Integral()
            print "Applying SF:", sfnorm
            for i, s in enumerate(back + ['BkgSum']):
                hist[s].Scale(sfnorm)
        if BLIND and var.endswith("Mass"):
            for i, s in enumerate(data + back + ['BkgSum']):
                first, last = hist[s].FindBin(65), hist[s].FindBin(135)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)
        if BLIND and var.endswith("Tau21"):
            for i, s in enumerate(data):
                first, last = hist[s].FindBin(0), hist[s].FindBin(0.6)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)

    # Create stack
    if variable[var]['nbins'] > 0:
        bkg = THStack(
            "Bkg",
            ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events / ( " + str(
                (variable[var]['max'] - variable[var]['min']) /
                variable[var]['nbins']) + unit + " )")
    else:
        bkg = THStack("Bkg",
                      ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events; ")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                leg.AddEntry(hist[s], sample[s]['label'], "fl")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    log = variable[var]['log']  #"log" in hist['BkgSum'].GetZaxis().GetTitle()
    if log: c1.GetPad(bool(RATIO)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if not isBlind and len(data) > 0: hist['data_obs'].Draw("SAME, PE")  # data
    if 'sync' in hist: hist['sync'].Draw("SAME, PE")
    #data_graph.Draw("SAME, PE")
    if showSignal:
        smagn = 1.  #if treeRead else 1.e2 #if log else 1.e2
        for i, s in enumerate(sign):
            #        if sample[s]['plot']:
            hist[s].Scale(smagn)
            hist[s].Draw(
                "SAME, HIST"
            )  # signals Normalized, hist[s].Integral()*sample[s]['weight']
        textS = drawText(0.80, 0.9 - leg.GetNRows() * 0.05 - 0.02,
                         stype + " (x%d)" % smagn, True)
    #bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075)
    bkg.GetYaxis().SetTitleOffset(0.9)
    #bkg.GetYaxis().SetTitleOffset(2.)
    bkg.SetMaximum((5. if log else 1.25) * max(
        bkg.GetMaximum(),
        hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) +
        hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
    #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        #bkg.GetYaxis().SetMoreLogLabels(True)
    bkg.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])

    #if log: bkg.SetMinimum(1)
    leg.Draw()
    #drawCMS(LUMI[year], "Preliminary")
    drawCMS(LUMI[year], "Work in Progress", suppressCMS=True)
    drawRegion('XVH' + channel, True)
    drawAnalysis(channel)

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / MC")
        err.GetYaxis().SetTitleOffset(0.9)

        err.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist['data_obs'].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        if 'sync' in hist:
            res.SetMarkerColor(2)
            res.SetMarkerStyle(31)
            res.Reset()
            for i in range(0, res.GetNbinsX() + 1):
                x = hist['data_obs'].GetXaxis().GetBinCenter(i)
                if hist['sync'].GetBinContent(hist['sync'].FindBin(x)) > 0:
                    res.SetBinContent(
                        i, hist['data_obs'].GetBinContent(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
                    res.SetBinError(
                        i, hist['data_obs'].GetBinError(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])

    c1.Update()

    if gROOT.IsBatch():
        if channel == "": channel = "nocut"
        varname = var.replace('.', '_').replace('()', '')
        if not os.path.exists("plots/" + channel):
            os.makedirs("plots/" + channel)
        suffix = ''
        if "b" in channel or 'mu' in channel: suffix += "_" + BTAGGING
        c1.Print("plots/MANtag_study/" + channel + "/" + varname + "_" + year +
                 suffix + ".png")
        c1.Print("plots/MANtag_study/" + channel + "/" + varname + "_" + year +
                 suffix + ".pdf")

    # Print table
    printTable(hist, sign)
Esempio n. 17
0
def DeepCSV_pt_distribution(
        year):  ## everything below is jsut copy&past from above
    from root_numpy import root2array, fill_hist, array2root
    import numpy.lib.recfunctions as rfn
    from aliases import alias_deepCSV, WP_deepCSV
    ### Preliminary Operations ###
    treeRead = True
    var = 'jpt_1'
    channel = 'preselection'
    cut = alias_deepCSV['preselection']
    unit = ''
    if "GeV" in variable[var]['title']: unit = ' GeV'
    isBlind = BLIND and 'SR' in channel
    isAH = False
    showSignal = True
    stype = "HVT model B"
    if len(sign) > 0 and 'AZh' in sign[0]: stype = "2HDM"
    elif len(sign) > 0 and 'monoH' in sign[0]: stype = "Z'-2HDM m_{A}=300 GeV"
    if treeRead:
        for k in sorted(alias_deepCSV.keys(), key=len, reverse=True):
            if k in cut:
                cut = cut.replace(
                    k, alias_deepCSV[k].format(WP=WP_deepCSV[BTAGGING][year]))

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  cut    :", cut

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}
    N_signal_tot = 0.
    N_signal_tag = 0.

    ### Create and fill MC histograms ###
    for i, s in enumerate(back + sign):

        if variable[var]['nbins'] > 0:
            hist[s] = TH1F(
                s, ";jet p_{T};Events / ( " + str(
                    (variable[var]['max'] - variable[var]['min']) /
                    variable[var]['nbins']) + unit + " );" +
                ('log' if variable[var]['log'] else ''),
                variable[var]['nbins'], variable[var]['min'],
                variable[var]['max'])
        else:
            hist[s] = TH1F(
                s,
                ";jet p_{T};Events" + ('log' if variable[var]['log'] else ''),
                len(variable[var]['bins']) - 1,
                array('f', variable[var]['bins']))
        hist[s].Sumw2()

        for j, ss in enumerate(sample[s]['files']):
            if not 'data' in s:
                if year == "run2" or year in ss:
                    arr = root2array(NTUPLEDIR + ss + ".root",
                                     branches=["jpt_1", "eventWeightLumi"],
                                     selection=cut + " && jdeepCSV_1>" +
                                     str(WP_deepCSV[BTAGGING][year]))
                    if 'signal' in ss.lower():
                        N_signal_tag += len(arr['jpt_1'][arr['jpt_1'] > 3500])
                    print "imported " + NTUPLEDIR + ss + ".root"
                    fill_hist(hist[s],
                              arr["jpt_1"],
                              weights=arr["eventWeightLumi"])
                    arr = None

                    arr = root2array(NTUPLEDIR + ss + ".root",
                                     branches=["jpt_2", "eventWeightLumi"],
                                     selection=cut + " && jdeepCSV_2>" +
                                     str(WP_deepCSV[BTAGGING][year]))
                    print "imported " + NTUPLEDIR + ss + ".root"
                    if 'signal' in ss.lower():
                        N_signal_tag += len(arr['jpt_2'][arr['jpt_2'] > 3500])
                    fill_hist(hist[s],
                              arr["jpt_2"],
                              weights=arr["eventWeightLumi"])
                    arr = None

                    if 'signal' in ss.lower():
                        arr = root2array(NTUPLEDIR + ss + ".root",
                                         branches=["jpt_1", "eventWeightLumi"],
                                         selection=cut)
                        N_signal_tot += len(arr['jpt_1'][arr['jpt_1'] > 3500])
                        arr = None

                        arr = root2array(NTUPLEDIR + ss + ".root",
                                         branches=["jpt_2", "eventWeightLumi"],
                                         selection=cut)
                        N_signal_tot += len(arr['jpt_2'][arr['jpt_2'] > 3500])
                        arr = None

        hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])

    if channel.endswith('TR') and channel.replace('TR', '') in topSF:
        hist['TTbarSL'].Scale(topSF[channel.replace('TR', '')][0])
        hist['ST'].Scale(topSF[channel.replace('TR', '')][0])

    hist['BkgSum'] = hist['data_obs'].Clone(
        "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s])

    # Create data and Bkg sum histograms
    if options.blind or 'SR' in channel:
        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist['data_obs'].SetMarkerStyle(20)
    hist['data_obs'].SetMarkerSize(1.25)

    for i, s in enumerate(back + sign + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    for i, s in enumerate(sign):
        sample[s][
            'plot'] = True  #sample[s]['plot'] and s.startswith(channel[:2])

    if isAH:
        for i, s in enumerate(back):
            hist[s].SetFillStyle(3005)
            hist[s].SetLineWidth(2)
        #for i, s in enumerate(sign):
        #    hist[s].SetFillStyle(0)
        if not var == "Events":
            sfnorm = hist[data[0]].Integral() / hist['BkgSum'].Integral()
            print "Applying SF:", sfnorm
            for i, s in enumerate(back + ['BkgSum']):
                hist[s].Scale(sfnorm)
        if BLIND and var.endswith("Mass"):
            for i, s in enumerate(data + back + ['BkgSum']):
                first, last = hist[s].FindBin(65), hist[s].FindBin(135)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)
        if BLIND and var.endswith("Tau21"):
            for i, s in enumerate(data):
                first, last = hist[s].FindBin(0), hist[s].FindBin(0.6)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)

    # Create stack
    if variable[var]['nbins'] > 0:
        bkg = THStack(
            "Bkg",
            ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events / ( " + str(
                (variable[var]['max'] - variable[var]['min']) /
                variable[var]['nbins']) + unit + " )")
    else:
        bkg = THStack("Bkg",
                      ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events; ")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                leg.AddEntry(hist[s], sample[s]['label'], "fl")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    log = variable[var]['log']  #"log" in hist['BkgSum'].GetZaxis().GetTitle()
    if log: c1.GetPad(bool(RATIO)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if not isBlind and len(data) > 0: hist['data_obs'].Draw("SAME, PE")  # data
    if 'sync' in hist: hist['sync'].Draw("SAME, PE")
    #data_graph.Draw("SAME, PE")
    if showSignal:
        smagn = 1.  #if treeRead else 1.e2 #if log else 1.e2
        for i, s in enumerate(sign):
            #        if sample[s]['plot']:
            hist[s].Scale(smagn)
            hist[s].Draw(
                "SAME, HIST"
            )  # signals Normalized, hist[s].Integral()*sample[s]['weight']
        textS = drawText(0.80, 0.9 - leg.GetNRows() * 0.05 - 0.02,
                         stype + " (x%d)" % smagn, True)
    #bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075)
    bkg.GetYaxis().SetTitleOffset(0.9)
    #bkg.GetYaxis().SetTitleOffset(2.)
    bkg.SetMaximum((5. if log else 1.25) * max(
        bkg.GetMaximum(),
        hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) +
        hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
    #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        #bkg.GetYaxis().SetMoreLogLabels(True)
    bkg.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])

    #if log: bkg.SetMinimum(1)
    leg.Draw()
    #drawCMS(LUMI[year], "Preliminary")
    drawCMS(LUMI[year], "", suppressCMS=True)
    drawRegion('XVH' + channel, True)
    drawAnalysis(channel)

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / MC")
        err.GetYaxis().SetTitleOffset(0.9)

        err.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist['data_obs'].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        if 'sync' in hist:
            res.SetMarkerColor(2)
            res.SetMarkerStyle(31)
            res.Reset()
            for i in range(0, res.GetNbinsX() + 1):
                x = hist['data_obs'].GetXaxis().GetBinCenter(i)
                if hist['sync'].GetBinContent(hist['sync'].FindBin(x)) > 0:
                    res.SetBinContent(
                        i, hist['data_obs'].GetBinContent(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
                    res.SetBinError(
                        i, hist['data_obs'].GetBinError(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])

    c1.Update()

    if gROOT.IsBatch():
        if channel == "": channel = "nocut"
        varname = var.replace('.', '_').replace('()', '')
        if not os.path.exists("plots/" + channel):
            os.makedirs("plots/" + channel)
        suffix = ''
        if "b" in channel or 'mu' in channel: suffix += "_" + BTAGGING
        c1.Print("plots/MANtag_study/deepCSV_plots/pt_" + year + suffix +
                 ".png")
        c1.Print("plots/MANtag_study/deepCSV_plots/pt_" + year + suffix +
                 ".pdf")

    # Print table
    printTable(hist, sign)

    print 'deepCSV efficiency:', N_signal_tag / N_signal_tot
Esempio n. 18
0
def draw(hist,
         channel,
         data,
         back,
         sign,
         snorm=1,
         lumi=-1,
         ratio=0,
         log=False):
    # If not present, create BkgSum
    if not 'BkgSum' in hist.keys():
        hist['BkgSum'] = hist['data_obs'].Clone(
            "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
        hist['BkgSum'].Reset("MICES")
        for i, s in enumerate(back):
            hist['BkgSum'].Add(hist[s])
    hist['BkgSum'].SetMarkerStyle(0)

    # Set Poisson error bars
    #if len(data) > 0: hist['data_obs'].SetBinErrorOption(1) # doesn't work

    alpha = 1 - 0.6827
    hist['data_obs'].SetBinErrorOption(TH1.kPoisson)
    data_graph = TGraphAsymmErrors(hist['data_obs'].GetNbinsX())
    data_graph.SetMarkerStyle(hist['data_obs'].GetMarkerStyle())
    data_graph.SetMarkerSize(hist['data_obs'].GetMarkerSize())
    res_graph = data_graph.Clone()
    for i in range(hist['data_obs'].GetNbinsX()):
        N = hist['data_obs'].GetBinContent(i + 1)
        B = hist['BkgSum'].GetBinContent(i + 1)
        L = 0 if N == 0 else ROOT.Math.gamma_quantile(alpha / 2, N, 1.)
        U = ROOT.Math.gamma_quantile_c(alpha / 2, N + 1, 1)
        data_graph.SetPoint(i, hist['data_obs'].GetXaxis().GetBinCenter(i + 1),
                            N if not N == 0 else -1.e99)
        data_graph.SetPointError(
            i, hist['data_obs'].GetXaxis().GetBinWidth(i + 1) / 2.,
            hist['data_obs'].GetXaxis().GetBinWidth(i + 1) / 2., N - L, U - N)
        res_graph.SetPoint(i, hist['data_obs'].GetXaxis().GetBinCenter(i + 1),
                           N / B if not B == 0 and not N == 0 else -1.e99)
        res_graph.SetPointError(
            i, hist['data_obs'].GetXaxis().GetBinWidth(i + 1) / 2.,
            hist['data_obs'].GetXaxis().GetBinWidth(i + 1) / 2.,
            (N - L) / B if not B == 0 else -1.e99,
            (U - N) / B if not B == 0 else -1.e99)

    # Create stack
    bkg = THStack("Bkg",
                  ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    n = len([x for x in data + back + ['BkgSum'] + sign if sample[x]['plot']])
    leg = TLegend(0.7, 0.9 - 0.05 * n, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pl")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    for i, s in enumerate(sign):
        if sample[s]['plot']:
            leg.AddEntry(hist[s],
                         sample[s]['label'].replace("m_{#Chi}=1 GeV",
                                                    ""), "fl")

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if ratio else 600)

    if ratio:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), ratio)
        setBotPad(c1.GetPad(2), ratio)
    c1.cd(1)
    c1.GetPad(bool(ratio)).SetTopMargin(0.06)
    c1.GetPad(bool(ratio)).SetRightMargin(0.05)
    c1.GetPad(bool(ratio)).SetTicks(1, 1)
    if log:
        c1.GetPad(bool(ratio)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    #if len(data) > 0: hist['data_obs'].Draw("SAME, PE") # data
    data_graph.Draw("SAME, PE")
    for i, s in enumerate(sign):
        if sample[s]['plot']:
            hist[s].DrawNormalized("SAME, HIST",
                                   hist[s].Integral() * snorm)  # signals

    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1.075)
    bkg.SetMaximum((2. if log else 1.2) * max(
        bkg.GetMaximum(),
        hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) +
        hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetMoreLogLabels(True)

    #if log: bkg.SetMinimum(1)
    leg.Draw()
    drawCMS(lumi, "Preliminary")
    drawRegion(channel)
    drawAnalysis(channel)

    #if nm1 and not cutValue is None: drawCut(cutValue, bkg.GetMinimum(), bkg.GetMaximum()) #FIXME
    if len(sign) > 0:
        if channel.startswith('X') and len(sign) > 0:
            drawNorm(0.9 - 0.04 * (n + 1),
                     "#sigma(X) #times B(X #rightarrow Vh) = %.1f pb" % snorm)
            #elif "SR" in channel: drawNorm(0.9-0.04*(n+1), "DM+bb/tt, scaled by %.0f" % snorm, "m_{#chi}=1 GeV, scalar mediator")
        elif "SR" in channel:
            drawNorm(0.9 - 0.04 * (n + 1), "DM+bb/tt, m_{#chi}=1 GeV",
                     "scalar mediator")

    setHistStyle(bkg, 1.2 if ratio else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if ratio else 1.1)

    if ratio:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist['data_obs'].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if len(data) > 0:
            #res.Draw("SAME, PE0")
            res_graph.Draw("SAME, PE0")


#            if len(err.GetXaxis().GetBinLabel(1))==0: # Bin labels: not a ordinary plot
#                drawRatio(hist['data_obs'], hist['BkgSum'])
#                drawKolmogorov(hist['data_obs'], hist['BkgSum'])

    c1.Update()

    # return list of objects created by the draw() function
    return [c1, bkg, leg, err, errLine, res, data_graph, res_graph]
Esempio n. 19
0
def createPlots_(plot, geometry):
    """Cumulative material budget from simulation.
    
       Internal function that will produce a cumulative profile of the
       material budget inferred from the simulation starting from the
       single detectors that compose the tracker. It will iterate over
       all existing detectors contained in the DETECTORS
       dictionary. The function will automatically skip non-existent
       detectors.

    """

    IBs = ["InnerServices", "Phase2PixelBarrel", "TIB", "TIDF", "TIDB"]
    theDirname = "Figures"

    if plot not in plots.keys():
        print("Error: chosen plot name not known %s" % plot)
        return

    hist_X0_detectors = OrderedDict()
    hist_X0_IB = None
    hist_X0_elements = OrderedDict()

    for subDetector,color in six.iteritems(DETECTORS):
        h = get1DHisto_(subDetector,plots[plot].plotNumber,geometry)
        if not h: 
            print('Warning: Skipping %s'%subDetector)
            continue
        hist_X0_detectors[subDetector] = h


        # Merge together the "inner barrel detectors".
        if subDetector in IBs:
            hist_X0_IB = assignOrAddIfExists_(
                hist_X0_IB,
                hist_X0_detectors[subDetector]
                )

        # category profiles
        for label, [num, color, leg] in six.iteritems(hist_label_to_num):
            if label is 'SUM': continue
            hist_label = get1DHisto_(subDetector, num + plots[plot].plotNumber, geometry)
            hist_X0_elements[label] = assignOrAddIfExists_(
                hist_X0_elements.setdefault(label,None),
                hist_label,
                )
            hist_X0_elements[label].SetFillColor(color)


    cumulative_matbdg = TH1D("CumulativeSimulMatBdg",
                             "CumulativeSimulMatBdg",
                             hist_X0_IB.GetNbinsX(),
                             hist_X0_IB.GetXaxis().GetXmin(),
                             hist_X0_IB.GetXaxis().GetXmax())
    cumulative_matbdg.SetDirectory(0)

    # colors
    for det, color in six.iteritems(DETECTORS):
        setColorIfExists_(hist_X0_detectors, det,  color)

    # First Plot: BeamPipe + Pixel + TIB/TID + TOB + TEC + Outside
    # stack
    stackTitle_SubDetectors = "Tracker Material Budget;%s;%s" % (
        plots[plot].abscissa,plots[plot].ordinate)
    stack_X0_SubDetectors = THStack("stack_X0",stackTitle_SubDetectors)
    for det, histo in six.iteritems(hist_X0_detectors):
        stack_X0_SubDetectors.Add(histo)
        cumulative_matbdg.Add(histo, 1)

    # canvas
    can_SubDetectors = TCanvas("can_SubDetectors","can_SubDetectors",800,800)
    can_SubDetectors.Range(0,0,25,25)
    can_SubDetectors.SetFillColor(kWhite)

    # Draw
    stack_X0_SubDetectors.SetMinimum(plots[plot].ymin)
    stack_X0_SubDetectors.SetMaximum(plots[plot].ymax)
    stack_X0_SubDetectors.Draw("HIST")
    stack_X0_SubDetectors.GetXaxis().SetLimits(plots[plot].xmin, plots[plot].xmax)


    # Legenda
    theLegend_SubDetectors = TLegend(0.180,0.8,0.98,0.92)
    theLegend_SubDetectors.SetNColumns(3)
    theLegend_SubDetectors.SetFillColor(0)
    theLegend_SubDetectors.SetFillStyle(0)
    theLegend_SubDetectors.SetBorderSize(0)

    for det, histo in six.iteritems(hist_X0_detectors):
        theLegend_SubDetectors.AddEntry(histo, det,  "f")

    theLegend_SubDetectors.Draw()

    # text
    text_SubDetectors = TPaveText(0.180,0.727,0.402,0.787,"NDC")
    text_SubDetectors.SetFillColor(0)
    text_SubDetectors.SetBorderSize(0)
    text_SubDetectors.AddText("CMS Simulation")
    text_SubDetectors.SetTextAlign(11)
    text_SubDetectors.Draw()

    # Store
    can_SubDetectors.Update()
    if not checkFile_(theDirname):
        os.mkdir(theDirname)
    can_SubDetectors.SaveAs("%s/Tracker_SubDetectors_%s.pdf" % (theDirname, plot))
    can_SubDetectors.SaveAs("%s/Tracker_SubDetectors_%s.root" % (theDirname, plot))


    # Second Plot: BeamPipe + SEN + ELE + CAB + COL + SUP + OTH/AIR +
    # Outside stack
    stackTitle_Materials = "Tracker Material Budget;%s;%s" % (plots[plot].abscissa,
                                                              plots[plot].ordinate)
    stack_X0_Materials = THStack("stack_X0",stackTitle_Materials)
    stack_X0_Materials.Add(hist_X0_detectors["BeamPipe"])
    for label, [num, color, leg] in six.iteritems(hist_label_to_num):
        if label is 'SUM':
            continue
        stack_X0_Materials.Add(hist_X0_elements[label])

    # canvas
    can_Materials = TCanvas("can_Materials","can_Materials",800,800)
    can_Materials.Range(0,0,25,25)
    can_Materials.SetFillColor(kWhite)

    # Draw
    stack_X0_Materials.SetMinimum(plots[plot].ymin)
    stack_X0_Materials.SetMaximum(plots[plot].ymax)
    stack_X0_Materials.Draw("HIST")
    stack_X0_Materials.GetXaxis().SetLimits(plots[plot].xmin, plots[plot].xmax)

    # Legenda
    theLegend_Materials = TLegend(0.180,0.8,0.95,0.92)
    theLegend_Materials.SetNColumns(3)
    theLegend_Materials.SetFillColor(0)
    theLegend_Materials.SetBorderSize(0)

    theLegend_Materials.AddEntry(hist_X0_detectors["BeamPipe"],  "Beam Pipe", "f")
    for label, [num, color, leg] in six.iteritems(hist_label_to_num):
        if label is 'SUM':
            continue
        theLegend_Materials.AddEntry(hist_X0_elements[label], leg, "f")
    theLegend_Materials.Draw()

    # text
    text_Materials = TPaveText(0.180,0.727,0.402,0.787,"NDC")
    text_Materials.SetFillColor(0)
    text_Materials.SetBorderSize(0)
    text_Materials.AddText("CMS Simulation")
    text_Materials.SetTextAlign(11)
    text_Materials.Draw()

    # Store
    can_Materials.Update()
    can_Materials.SaveAs("%s/Tracker_Materials_%s.pdf" % (theDirname, plot))
    can_Materials.SaveAs("%s/Tracker_Materials_%s.root" % (theDirname, plot))

    return cumulative_matbdg
Esempio n. 20
0
def read_file(arr, meas_type, mapsa_fitter_inst, path):
    if not os.path.isfile(str(path + arr[0])):
        print "Root file not found at", str(path + arr[0])
        #sys.exit(1)
        return
    f = TFile(str(path + arr[0]), 'READ')
    if (f.IsZombie()):
        print "Error opening file"
        return
    else:
        print "Reading File ", arr[0]
    #f.ls()
    tree = f.Get('tree')
    f_GlobalData_Map = ROOT.TMap()
    f_GlobalData_Map.Add(ROOT.TObjString("tree"), tree)
    #tree.Print()
    outfile = TString(arr[0])
    outfile.ReplaceAll(".root", "")
    outfile.ReplaceAll(" ", "")
    # print outfile
    if (meas_type == 0):
        outfile = "pedestal"
    if (not g.FindKey(str(outfile))):
        g.mkdir(str(outfile))
        g.cd(str(outfile))
    else:
        return
    channels = 288
    if (arr[2] == 'inv'):
        channels = 96
        mapsa_mat = [[1, 0, 0], [1, 0, 0]]
    elif (arr[2] == 'norm'):
        channels = 288
        mapsa_mat = [[1, 1, 1], [1, 0, 1]]
    #print "channels", channels
    #print "mapsa_mat", mapsa_mat

    c1 = TCanvas('c1', 'Pixel Monitor ', 700, 900)
    c2 = TCanvas('c2', 'Pixel Monitor ', 500, 500)
    c3 = TCanvas('c3', 'Pixel Monitor ', 1280, 720)
    c4 = TCanvas('c4', 'Pixel Monitor ', 500, 500)
    c5 = TCanvas('c5', 'Pixel Monitor ', 500, 500)

    f_GlobalData_Map.Add(ROOT.TObjString("c1"), c1)
    f_GlobalData_Map.Add(ROOT.TObjString("c2"), c2)
    f_GlobalData_Map.Add(ROOT.TObjString("c3"), c3)
    f_GlobalData_Map.Add(ROOT.TObjString("c4"), c4)
    f_GlobalData_Map.Add(ROOT.TObjString("c5"), c5)

    # c2.Divide(2,1)
    #c2.cd(0)
    c1.Divide(3, 2)
    for i in range(1, 7):
        c1.cd(i)
        ROOT.gPad.SetGridx()
        ROOT.gPad.SetGridy()

    # channelcounts = TH2I('HitMap','Counts; Channel; DAC Value (1.456 mV)', 288, .5,288.5,256, .5, 256.5)
    channelcounts = TH2I('HitMap', 'Counts; Channel; DAC Value (a.u.)', 288,
                         .5, 288.5, 256, .5, 256.5)
    channelcounts_norm = TH2F('HitMap_norm',
                              'Occupancy ; Channel; DAC Value (a.u.)', 288, .5,
                              288.5, 256, .5, 256.5)
    f_GlobalData_Map.Add(ROOT.TObjString("HitMap"), channelcounts)
    f_GlobalData_Map.Add(ROOT.TObjString("HitMap"), channelcounts_norm)

    norm_2d = TH2F('Norm2D', 'Normalization; Column; Row', 48, .5, 48.5, 6, .5,
                   6.5)
    mean_2d = TH2F('Mean2D', 'Mean; Column; Row', 48, .5, 48.5, 6, .5, 6.5)
    sigma_2d = TH2F('Sigma2D', 'Sigma; Column; Row', 48, .5, 48.5, 6, .5, 6.5)
    chisquare = TH2F('Chisquare2D', 'Chisquare; Column; Row', 48, .5, 48.5, 6,
                     .5, 6.5)

    f_GlobalData_Map.Add(ROOT.TObjString("Norm2D"), norm_2d)
    f_GlobalData_Map.Add(ROOT.TObjString("Mean2D"), mean_2d)
    f_GlobalData_Map.Add(ROOT.TObjString("Sigma2D"), sigma_2d)
    f_GlobalData_Map.Add(ROOT.TObjString("Chisquare2D"), chisquare)

    objarr2d = []
    objarr2d.append(norm_2d)
    objarr2d.append(mean_2d)
    objarr2d.append(sigma_2d)
    objarr2d.append(chisquare)
    normgraph = TGraphErrors()
    meangraph = TGraphErrors()
    sigmagraph = TGraphErrors()
    chisquaregraph = TGraphErrors()
    mean_corrgraph = TGraphErrors()

    f_GlobalData_Map.Add(ROOT.TObjString("normgraph     "), normgraph)
    f_GlobalData_Map.Add(ROOT.TObjString("meangraph     "), meangraph)
    f_GlobalData_Map.Add(ROOT.TObjString("sigmagraph    "), sigmagraph)
    f_GlobalData_Map.Add(ROOT.TObjString("chisquaregraph"), chisquaregraph)
    f_GlobalData_Map.Add(ROOT.TObjString("mean_corrgraph"), mean_corrgraph)

    meanhist = TH1F('meanhist', 'Mean DAC; DAC Value (a.u.); counts', 2560, 0,
                    255)
    sigmahist = TH1F('sigmahist', 'Sigma DAC; DAC Value (a.u.); counts', 100,
                     0, 10)
    meanhist_std = TH1F('meanhist_std',
                        'Mean DAC Standard; DAC Value   (a.u.); counts', 2560,
                        0, 255)
    sigmahist_std = TH1F('sigmahist_std',
                         'Sigma DAC Standard; DAC Value (a.u.); counts', 100,
                         0, 10)
    meanhist_double = TH1F('meanhist_double',
                           'Mean DAC Double; DAC Value   (a.u.); counts', 2560,
                           0, 255)
    sigmahist_double = TH1F('sigmahist_double',
                            'Sigma DAC Double; DAC Value (a.u.); counts', 100,
                            0, 10)
    meanhist_double_neighbour = TH1F(
        'meanhist_double_neighbour',
        'Mean DAC Double Neighbour; DAC Value   (a.u.); counts', 2560, 0, 255)
    sigmahist_double_neighbour = TH1F(
        'sigmahist_double_neighbour',
        'Sigma DAC Double Neighbour; DAC Value (a.u.); counts', 100, 0, 10)
    objarr = []
    objarr.append(normgraph)
    objarr.append(meangraph)
    objarr.append(sigmagraph)
    objarr.append(chisquaregraph)
    objarr.append(mean_corrgraph)

    f_GlobalData_Map.Add(ROOT.TObjString('meanhist'), meanhist)
    f_GlobalData_Map.Add(ROOT.TObjString('sigmahist'), sigmahist)
    f_GlobalData_Map.Add(ROOT.TObjString('meanhist_std'), meanhist_std)
    f_GlobalData_Map.Add(ROOT.TObjString('sigmahist_std'), sigmahist_std)
    f_GlobalData_Map.Add(ROOT.TObjString('meanhist_double'), meanhist_double)
    f_GlobalData_Map.Add(ROOT.TObjString('sigmahist_double'), sigmahist_double)
    f_GlobalData_Map.Add(ROOT.TObjString('meanhist_double_neighbour'),
                         meanhist_double_neighbour)
    f_GlobalData_Map.Add(ROOT.TObjString('sigmahist_double_neighbour'),
                         sigmahist_double_neighbour)

    objarr.append(meanhist)
    objarr.append(sigmahist)
    objarr.append(meanhist_std)
    objarr.append(sigmahist_std)
    objarr.append(meanhist_double)
    objarr.append(sigmahist_double)
    objarr.append(meanhist_double_neighbour)
    objarr.append(sigmahist_double_neighbour)

    for objs in objarr:
        objs.SetMarkerColor(2)
        objs.SetMarkerStyle(20)
        objs.SetMarkerSize(1)
    normgraph.SetName('normgraph')
    meangraph.SetName('meangraph')
    sigmagraph.SetName('sigmagraph')
    chisquaregraph.SetName('chisquare')
    mean_corrgraph.SetName('mean_corr')
    normgraph.SetTitle('Normalization; Channel; Normalization')
    meangraph.SetTitle('Mean; Channel; DAC Value (a.u.)')
    sigmagraph.SetTitle('Sigma; Channel; DAC Value (a.u.)')
    chisquaregraph.SetTitle('Chisquared/NDF_gr; Channel; Chisquared/NDF ')
    ROOT.gStyle.SetOptFit(1111)
    stack = THStack('a', ';DAC Value (a.u.); Occupancy')
    f_GlobalData_Map.Add(ROOT.TObjString("stack"), stack)
    fitfuncs = []
    fitparams = []
    gr1 = []
    for pixel in range(0, channels):
        gr1.append(
            TH1D(
                str(pixel).zfill(3),
                str(pixel + 1).zfill(3) + ';DAC Value (a.u.); Occupancy ', 256,
                0.5, 256.5))
        f_GlobalData_Map.Add(ROOT.TObjString(str(pixel).zfill(3)), gr1[pixel])
        #gr2.append(TH1F('th1f_'+str(pixel).zfill(3),str(pixel+1).zfill(3)+';DAC Value (a.u.); Occupancy',256,0.5,256.5))
        color = pixel % 8 + 1
        formating_th1(gr1[pixel], color)
        if (meas_type == 0):
            fitfuncs.append(
                TF1('gauss' + str(pixel + 1).zfill(3), 'gaus(0)', 0, 256))
            fitfuncs[pixel].SetNpx(256)
            fitfuncs[pixel].SetLineColor(color)
            f_GlobalData_Map.Add(
                ROOT.TObjString('gauss' + str(pixel).zfill(3)),
                fitfuncs[pixel])
    #Here we read the data and fill the histogram
    for event in tree:
        eventstr = []
        for counter, vals in enumerate(tree.AR_MPA):
            #eventstr.append(vals)
            channelcounts.Fill(counter, tree.THRESHOLD, vals)
            if (counter < channels):
                gr1[counter].Fill(tree.THRESHOLD, vals)
        #if tree.THRESHOLD%20==0 and tree.REPETITION==0:
        #print eventstr
        #print ("Threshold %d Repetion %d" % (tree.THRESHOLD,tree.REPETITION))
        #print tree.AR_MPA
    #now we make a small analysis of the curves fitting different functions to it:
    print "Finished Reading the Tree\n Normalization of Histograms\n"
    for pixel in range(0, channels):
        #gr1[pixel].ResetStats()
        for j in range(0, gr1[pixel].GetXaxis().GetNbins() + 1):
            gr1[pixel].SetBinError(
                gr1[pixel].GetBin(j),
                TMath.Sqrt(gr1[pixel].GetBinContent(gr1[pixel].GetBin(j))))
        #if(pixel==0):
        #gr1[pixel].Print("all")
        #gr1[pixel].Sumw2(ROOT.kTRUE)
        gr1[pixel].Scale(1 / arr[1])
        #if(pixel==0):
        #gr1[pixel].Print("all")
        stack.Add(gr1[pixel])
    #first create a THStack with histograms:
    iterator = stack.GetHists()
    if (meas_type == 0):
        for idx, it in enumerate(iterator):
            fitparams.append([])
            if (it.Integral() > 0):
                if (idx < channels):
                    #fitfuncs.append(TF1('combined'+str(idx),combined, 0,256,5))
                    #fitfuncs.append(TF1('combined_same_mean'+str(idx),combined_mean, 0,256,4))
                    #fitfuncs.append(TF1('double_gauss'+str(idx),'gaus(0)+gaus(3)',0,256))
                    #fitfuncs.append(TF1('gauss'+str(idx),'gaus(0)',0,256))
                    #fitfuncs.append(TF1('double_gauss_same_mean'+str(idx),double_gauss, 0,256,5))
                    #print it.GetName(), idx
                    #fitfuncs[idx].SetParameters(it.GetMaximum(),it.GetMean()+1,it.GetRMS(),it.GetMean()-1,it.GetRMS());
                    #fitfuncs[idx].SetParameters(it.GetMaximum(),it.GetMean(),it.GetRMS()*0.1,it.GetRMS()*0.1);
                    #print ("Channels %f\t%f\t%f\n" % (it.GetMaximum(),it.GetMean(),it.GetRMS()))
                    fitfuncs[idx].SetParameters(it.GetMaximum(), it.GetMean(),
                                                it.GetRMS())
                    #fitfuncs[idx].SetParameters(0.999*it.GetMaximum(),it.GetMean(),.7*it.GetRMS(),0.001*it.GetMaximum(),it.GetMean(),10*it.GetRMS());
                    #fitfuncs[idx].SetParameters(0.999*it.GetMaximum(),it.GetMean(),.7*it.GetRMS(),0.001*it.GetMaximum(),10*it.GetRMS());
                    #it.Fit(fitfuncs[idx],'lr0 rob=0.95','same',0,256)
                    #it.Fit(fitfuncs[idx],'lr0q ','',0,256)
                    it.Fit(fitfuncs[idx], 'r0q ', '', 0, 256)
                    fitparams[idx].append(fitfuncs[idx].GetParameter(0))
                    fitparams[idx].append(fitfuncs[idx].GetParameter(1))
                    fitparams[idx].append(fitfuncs[idx].GetParameter(2))
                    fitparams[idx].append(fitfuncs[idx].GetParError(0))
                    fitparams[idx].append(fitfuncs[idx].GetParError(1))
                    fitparams[idx].append(fitfuncs[idx].GetParError(2))
                    if (fitfuncs[idx].GetNDF() > 0):
                        fitparams[idx].append(fitfuncs[idx].GetChisquare() /
                                              fitfuncs[idx].GetNDF())
            else:
                for kk in range(0, 7):
                    fitparams[idx].append(0)
        #print "fitparamarray"
        fitarray = np.array(fitparams)
        ## print fitarray
        for pointno, it in enumerate(fitarray):
            if (fitarray[pointno][0] > 0):
                normgraph.SetPoint(pointno, pointno + 1, fitarray[pointno][0])
                normgraph.SetPointError(pointno, 0, fitarray[pointno][3])
                meangraph.SetPoint(pointno, pointno + 1, fitarray[pointno][1])
                meangraph.SetPointError(pointno, 0, fitarray[pointno][4])
                meanhist.Fill(fitarray[pointno][1])
                sigmagraph.SetPoint(pointno, pointno + 1, fitarray[pointno][2])
                sigmagraph.SetPointError(pointno, 0, fitarray[pointno][5])
                sigmahist.Fill(fitarray[pointno][2])
                chisquaregraph.SetPoint(pointno, pointno + 1,
                                        fitarray[pointno][6])
                chisquaregraph.SetPointError(pointno, 0, 0)
        ## iterator.ls()
        # Map the data to the pixel layout:
        tmp_objarr = []
        tmp_objarr.extend(
            [meanhist_std, meanhist_double, meanhist_double_neighbour])
        tmp_objarr.extend(
            [sigmahist_std, sigmahist_double, sigmahist_double_neighbour])
        for i in tmp_objarr:
            print str(i.GetName())

        fill2d(fitarray[:, 0], mapsa_mat, objarr2d[0])
        fill2d(fitarray[:, 1], mapsa_mat, objarr2d[1])
        fill2d(fitarray[:, 2], mapsa_mat, objarr2d[2])
        fill2d(fitarray[:, 6], mapsa_mat, objarr2d[3])
        fill1d_edges(objarr2d[1], tmp_objarr[0:3])
        fill1d_edges(objarr2d[2], tmp_objarr[3:])

    g.cd(str(outfile))
    mapsa_fitter_inst.Make_dirs()
    mapsa_fitter_inst.Set_run_no(outfile)
    if (meas_type == 1):
        for idx, it in enumerate(iterator):
            if (it.Integral() > 0):
                if (idx < channels):
                    mapsa_fitter_inst.Find_signal(it, idx, 0.0025, 3)
    g.cd()
    #g.mkdir(str(outfile)+"/Channels")
    #g.cd(str(outfile)+"/Channels")
    #iterator.Write()

    g.cd(str(outfile))
    g.mkdir(str(outfile) + "/Overview")
    ## iterator.First().Print("all")
    Maximum = TMath.Power(10, (round(TMath.Log10(stack.GetMaximum())) - 1))
    #Minimum = TMath.Power(10,(round(TMath.Log10(stack.GetMinimum()))+1))
    Minimum = .1

    ROOT.gStyle.SetLabelSize(0.06, "xyz")
    ROOT.gStyle.SetTitleSize(0.06, "xyz")
    ROOT.gStyle.SetTitleOffset(1.2, "y")
    ROOT.gStyle.SetTitleOffset(.825, "x")
    ROOT.gStyle.SetPadGridX(1)
    ROOT.gStyle.SetPadGridY(1)
    ROOT.gStyle.SetOptStat(0)
    # ROOT.gStyle.SetPadLeftMargin(.2);
    # ROOT.gStyle.SetPadRightMargin(.1);
    c1.cd(1)
    stack.Draw("nostack hist e1 x0")
    stack.GetXaxis().SetRangeUser(0, 256)
    stack.SetMinimum(Minimum)
    stack.SetMaximum(Maximum)
    ROOT.gPad.SetLogy()
    c2.cd(0)
    stack.Draw("nostack hist e1 x0")
    #if(outfile.Contains("SR_90_on_top")):
    #stack.GetXaxis().SetRangeUser(0,256)
    #else:
    #stack.GetXaxis().SetRangeUser(0,100)
    stack.SetMinimum(Minimum)
    stack.SetMaximum(Maximum)
    ROOT.gPad.SetLeftMargin(.15)
    ROOT.gPad.SetRightMargin(.05)

    ROOT.gPad.SetLogy()
    ROOT.gPad.Update()
    #for idx, it in enumerate(fitfuncs):
    ## if idx>0 and idx<7:
    #c1.cd(1)
    #fitfuncs[idx].Draw("same")
    #c2.cd(0)
    #fitfuncs[idx].DrawCopy("psame")
    ## it.SetLineColor(idx%9+1)
    ## it.Draw("same")
    #g.cd(str(outfile)+"/Channels")
    #it.Write("HitMap_py_"+str(idx+1)+"_fit")
    g.cd(str(outfile) + "/Overview")
    c1.cd(2)
    chisquaregraph.Draw("ap")
    c1.cd(3)
    normgraph.Draw("ap")
    c1.cd(4)
    sigmagraph.Draw("ap")
    sigmagraph.GetYaxis().SetRangeUser(0, 5)
    sigmagraph.GetXaxis().SetRangeUser(0, channels + 1)
    c2.cd(2)
    sigmagraph.Draw("ap")
    ROOT.gPad.SetLeftMargin(.15)
    ROOT.gPad.SetRightMargin(.05)
    c1.cd(5)
    meangraph.Draw("ap")
    c1.cd(6)
    channelcounts.Draw("colz")
    channelcounts.GetXaxis().SetRangeUser(0, channels + 1)
    # c2.cd(3)
    c3.cd(0)
    ROOT.gStyle.SetOptStat(0)
    ROOT.gPad.SetRightMargin(.15)
    ROOT.gPad.SetLeftMargin(.15)
    ROOT.gPad.SetGrid(0)
    copy = channelcounts.DrawCopy("colz")
    #f_GlobalData_Map.Add(ROOT.TObjString("copy"),copy)
    #if(outfile.Contains("SR_90_on_top")):
    #copy.SetMaximum(100)
    #copy.SetMinimum(1)
    copy.GetYaxis().SetTitle("DAC Value (a.u.)")
    c4.cd(0)
    ROOT.gStyle.SetOptStat(0)
    ROOT.gPad.SetRightMargin(.15)
    ROOT.gPad.SetLeftMargin(.15)
    ROOT.gPad.SetGrid(0)
    copy1 = sigma_2d.DrawCopy("colz")
    #f_GlobalData_Map.Add(ROOT.TObjString("copy1"),copy1)
    copy1.GetZaxis().SetTitle("Sigma (a.u.)")
    copy1.GetZaxis().SetTitleOffset(1.2)
    ROOT.gPad.SetRightMargin(.2)
    if (arr[2] == 'inv'):
        copy1.GetXaxis().SetRangeUser(.5, 16.5)
    copy1.SetMaximum(5)
    copy1.SetMinimum(0)

    c5.cd(0)
    ROOT.gStyle.SetOptStat(0)
    ROOT.gPad.SetRightMargin(.15)
    ROOT.gPad.SetLeftMargin(.15)
    ROOT.gPad.SetGrid(0)
    #copy1 = chisquare.DrawCopy("colz")
    #f_GlobalData_Map.Add(ROOT.TObjString("copy2"),copy1)
    copy1 = sigma_2d.DrawCopy("colz")
    #f_GlobalData_Map.Add(ROOT.TObjString("copy2"),copy1)
    copy1.GetZaxis().SetTitle("sigma (a.u.)")
    if (arr[2] == 'inv'):
        copy1.GetXaxis().SetRangeUser(.5, 16.5)
    copy1.SetMaximum(5)
    copy1.SetMinimum(0)

    c1.Update()
    c1.Modified()
    c2.Update()
    c2.Modified()
    c3.Update()
    c3.Modified()
    c4.Update()
    c4.Modified()
    c5.Update()
    c5.Modified()

    ## c1.SaveAs("double_gauss_same_mean.pdf")
    ## time.sleep(2)
    g.cd(str(outfile) + "/Overview")
    #for objs in objarr:
    #objs.Write(objs.GetName())
    #norm_2d.GetZaxis().SetRangeUser(1E5,2E6)
    #mean_2d.GetZaxis().SetRangeUser(54,64)
    ## norm_2d.GetZaxis().SetRangeUser(TMath.Power(10,(round(TMath.Log10(norm_2d.GetStdDev(3))-2)), TMath.Power(10,(round(TMath.Log10(norm_2d.GetStdDev(3)))-1)))
    ## mean_2d.GetZaxis().SetRangeUser(TMath.Power(10,(round(TMath.Log10(mean_2d.mean_2d.GetStdDev(3)))-1))-5,TMath.Power(10,(round(TMath.Log10(mean_2d.GetStdDev(3)))-1))+5)
    #sigma_2d.GetZaxis().SetRangeUser(0,5)
    #chisquare.GetZaxis().SetRangeUser(0,10000 )
    #for objs in objarr2d:
    #objs.Write(objs.GetName())
    #c1.Write("c1")
    #outfile1=outfile+TString(".pdf")
    #c2.SaveAs(str(outfile1))
    #c2.Write("c2")
    #c3.SaveAs("c3"+str(outfile1))
    #c3.Write("c3")
    #c4.SaveAs("c4"+str(outfile1))
    #c4.Write("c4")
    ## while (TObject(iterator.Next())):
    ##       print iterator.Next().Title()
    #stack.Write("stack")
    #g.cd(str(outfile))
    #channelcounts.Write(str(outfile))
    #f.Close()
    c1.Close()
    c2.Close()
    c3.Close()
    c4.Close()
    c5.Close()
    f_GlobalData_Map.DeleteAll()
    f.Close()
Esempio n. 21
0
def main():

    extension = "pdf"

    luminosity = 44307.4
    ymin = 0
    ymax = 0.024

    data_set_names = [
        (["SZee_all_susy_2l_0jets.root",
          "SZmumu_all_susy_2l_0jets.root"], "Z#rightarrowll", 860 + 0, 23),
        (["ZZ_1in3_susy_2l_0jets.root"], "ZZ#rightarrowll#nu#nu", 416 + 3, 43),
        (["WZ_1in3_susy_2l_0jets.root"], "WZ#rightarrowl#null", 416 - 8, 33),
        (["WW_1in3_susy_2l_0jets.root"], "WW#rightarrowl#nul#nu", 416 + 0, 20),
        ([
            "ttbar_1in3_susy_2l_0jets.root", "top_1in3_susy_2l_0jets.root",
            "antitop_1in3_susy_2l_0jets.root"
        ], "Top", 800 + 4, 21),
    ]

    DATA_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Data", "SR_SUSY")

    ## Creating the plots
    gROOT.SetStyle("ATLAS")
    gROOT.ForceStyle()
    gStyle.SetErrorX(0.5)
    canvas = TCanvas("canvas", 'A basic canvas', 800, 600)
    # canvas.SetLogy()

    ## Adding in the legend
    leg = Plotting.Create_Legend(0.60, 0.60, 0.95, 0.95, ncols=1)

    ## Creating the stack
    stack = THStack("stack", "stack")
    stack.SetMinimum(ymin)
    stack.SetMaximum(ymax)

    ## Cycling through the different datasets
    for datasets, label, colour, style in data_set_names:

        ## Creating the total histogram which will be filled
        myhist = TH1D(label, label, 50, 0, 150)
        myhist.SetStats(True)
        myhist.StatOverflows(True)

        for dataset in datasets:

            ## The root file is opened
            rootfile_name = os.path.join(DATA_dir, dataset)
            print(rootfile_name)
            root_file = TFile.Open(rootfile_name, 'read')
            tree = root_file.Get("mt2_Truth")

            ## Creating the current histogram which will be filled
            thishist = TH1D(label, label, 50, 0, 150)
            thishist.SetStats(True)
            thishist.StatOverflows(True)

            ## Drawing the tree and saving the hist to the matrix
            execution = "mt2>>{}".format(label)
            tree.Draw(execution, "", "goff")

            thishist.SetDirectory(0)
            myhist.Add(thishist)

            ## We are dont with the file
            root_file.Close()
            del root_file

        ## Changing the properties of the histogram
        myhist.Scale(1 / myhist.Integral("width"))

        myhist.SetLineColor(colour)
        myhist.SetMarkerColor(colour)
        myhist.SetMarkerStyle(style)
        myhist.SetMarkerSize(1.5)

        ## Adding the legend entry
        leg.AddEntry(myhist, label, "p")

        ## Adding the object to the stack
        stack.Add(myhist)
        del myhist

    ## Drawing the stack on the currrent canvas
    stack.Draw("NOSTACK HIST P")
    leg.Draw()

    ## Setting axis labels
    stack.GetXaxis().SetTitle("m_{T2} (True #it{E}_{T}^{miss}) [GeV]")
    stack.GetYaxis().SetTitle("Normalised Distributions")

    ## Moving axis tick marks
    stack.GetYaxis().SetMaxDigits(3)
    stack.GetXaxis().SetLabelOffset(0.017)

    ## Drawing all text
    left = 0.2
    size = 1.0
    shift = 0.06
    pos = 0.88
    Plotting.Draw_ATLASLabel(left, pos, "Simulation", scale=1.0)
    pos -= shift
    Plotting.Draw_Text(left, pos, "work in progress", scale=size)
    pos -= shift
    Plotting.Draw_Lumi(left, pos, 0, scale=size)
    pos -= shift

    ## Updating the canvas
    canvas.Update()

    out_file = "true_strans.{}".format(extension)
    canvas.Print(out_file)

    del canvas

    return 0
Esempio n. 22
0
def main():

    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-i", "--inputfile", dest="inputfile")
    parser.add_option("-N", "--multiplicity", dest="N", type="int", default=3)
    parser.add_option("-x", "--exclusive", action="store_true",\
          dest="isExclusive", default=False)
    parser.add_option("-l", "--label", dest="label", type="string", default="")
    (options, args) = parser.parse_args()

    N = options.N
    isExclusive = options.isExclusive
    label_text = options.label

    if isExclusive and not (N == 2 or N == 3):
        parser.error("Exclusive plot only for N =2 or 3")

    import configurations as config
    from ROOT import TFile, TCanvas, THStack, TLegend, TPaveText, gStyle
    from ModelParser import ModelKey

    gStyle.SetPadTopMargin(0.05)
    gStyle.SetPadRightMargin(0.05)

    suffix = ""
    if not isExclusive:
        suffix = "up"

    sm_files = []
    for model in config.sm_models:
        f = TFile("%s/%s.root" % (config.sm_dir, model), "READ")
        sm_files.append(f)

    bh_weights = []
    bh_files = []
    from BHXsec import BHXsec
    xsec = BHXsec()
    for model in config.bh_showcase:
        f = TFile("%s/%s.root" % (config.bh_dir, model), "READ")
        bh_files.append(f)
        h = f.Get("plotsNoCut/ST")
        nEvents = h.GetEntries()
        bh_weights.append(
            xsec.get(model) / nEvents * config.integrated_luminosity)

    c = TCanvas("ST_Mul%d%s" % (N, suffix), "ST_Mul%d%s" % (N, suffix), 500,
                500)
    hs = THStack()

    infile = TFile(options.inputfile, "READ")
    hBkg = infile.Get("Background_N%d%s" % (N, suffix))
    gBkg = infile.Get("BackgroundGraph_N%d%s" % (N, suffix))
    hData = infile.Get("Data_N%d%s" % (N, suffix))
    hBkg = infile.Get("Background_N%d%s" % (N, suffix))
    hBkg.SetMarkerSize(0)
    hBkg_ = hBkg.Clone("BkgLine")
    hBkg.SetFillColor(33)
    hBkg.SetLineColor(33)
    hBkg_.SetLineWidth(3)
    hBkg_.SetLineColor(862)
    hs.Add(hBkg, "e3")

    legend = TLegend(0.3326613, 0.6419492, 0.9294355, 0.9216102)
    legend.SetTextSize(0.02966102)
    legend.SetTextFont(42)
    legend.SetFillColor(0)
    legend.SetLineColor(0)
    if isExclusive:
        legend.SetHeader("N = %d" % N)
    else:
        legend.SetHeader("N #geq %d" % N)
    legend.AddEntry(hData, "Data", "p")
    legend.AddEntry(hBkg_, "Background", "l")
    legend.AddEntry(hBkg, "Uncertainty", "f")

    legend_sm = TLegend(0.6471774, 0.7669492, 0.8508065, 0.8771186)
    legend_sm.SetTextSize(0.02966102)
    legend_sm.SetTextFont(42)
    legend_sm.SetFillColor(0)
    legend_sm.SetLineColor(0)

    for i, f in enumerate(bh_files):
        h = f.Get("plotsN%d%s/ST" % (N, suffix))
        h.Rebin(config.rebin)
        h.Scale(bh_weights[i])

        # Add background
        for ibin in range(h.GetNbinsX()):
            h.SetBinContent(ibin+1,\
                  h.GetBinContent(ibin+1)\
                  + hBkg.GetBinContent(ibin+1))

            h.SetLineWidth(2)
            h.SetLineStyle(i + 2)

        hs.Add(h, "hist")
        model = ModelKey(config.bh_showcase[i])
        bh_legend = "M_{D} = %.1f TeV, M_{BH}^{ min} = %.1f TeV, n = %d" % (\
              model.parameter["MD"],
              model.parameter["M"],
              model.parameter["n"])

        legend.AddEntry(h, bh_legend, "l")

    if isExclusive:
        for i, f in enumerate(sm_files):
            h = f.Get("plotsN%d%s/ST" % (N, suffix))
            h.Rebin(config.rebin)
            h.Scale(config.integrated_luminosity)
            h.SetFillColor(config.sm_colors[i])
            h.SetLineColor(config.sm_colors[i])
            hs.Add(h, "hist")
            legend_sm.AddEntry(h, config.sm_models[i], "f")

    #hs.Add(hData, "e")

    hs.Draw("nostack")
    c.SetLogy(1)
    hs.GetXaxis().SetTitle("S_{T} (GeV)")
    hs.GetYaxis().SetTitle(hData.GetYaxis().GetTitle())
    hs.GetYaxis().SetTitleOffset(1.2)

    ibin = 0
    if isExclusive:
        hs.GetXaxis().SetRangeUser(config.fit_range[0], config.maxST)
        ibin = hData.FindBin(config.fit_range[0])
    else:
        hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
        ibin = hData.FindBin(config.norm_range[0])
    from Styles import formatUncertainty
    formatUncertainty(gBkg)
    gBkg.Draw("LX")
    hData.Draw("esame")

    hs.SetMinimum(5e-2)
    if isExclusive:
        hs.SetMaximum(hData.GetBinContent(ibin) * 20)
    else:
        #hs.SetMaximum(4e4)
        hs.SetMaximum(hData.GetBinContent(ibin) * 20)

    legend.Draw("plain")
    if isExclusive:
        legend_sm.Draw("plain")

    if isExclusive:
        cmslabel = TPaveText(0.5544355, 0.5127119, 0.8991935, 0.6292373,
                             "brNDC")
    else:
        cmslabel = TPaveText(0.1955645, 0.1631356, 0.5403226, 0.279661,
                             "brNDC")
    cmslabel.AddText(config.cmsTitle)
    cmslabel.AddText(config.cmsSubtitle)
    cmslabel.SetFillColor(0)
    cmslabel.Draw("plain")

    label = TPaveText(0.6891129, 0.8644068, 0.9435484, 0.9258475, "brNDC")
    label.SetFillColor(0)
    label.SetTextSize(0.0529661)
    label.AddText(label_text)
    label.Draw("plain")

    c.Update()

    raw_input("Press Enter to continue...")
Esempio n. 23
0
def plotDataVSMC(DataHist,
                 MCHist,
                 xtitle,
                 legendNames,
                 DataName,
                 destination,
                 year,
                 ytitle_bottom='Data/MC',
                 ylog=False):
    GeneralSettings()

    Canv = TCanvas("Canv" + destination, "Canv" + destination, 1000, 1000)

    #Set Histogram Styles
    for i, h in enumerate(MCHist):
        h.SetFillColor(
            TColor.GetColor(GetStackColorTauPOGbyName(legendNames[i])))
        h.SetLineColor(
            TColor.GetColor(GetStackColorTauPOGbyName(legendNames[i])))

    DataHist.SetMarkerColor(ROOT.kBlack)
    DataHist.SetLineColor(ROOT.kBlack)
    DataHist.SetMarkerStyle(20)

    #Add all MC samples to use in ratios
    totBkgr = MCHist[0].Clone("totBkgr")
    for h in MCHist[1:]:
        totBkgr.Add(h)
    DataOverMC = DataHist.Clone("DataOverMC")
    DataOverMC.Divide(totBkgr)

    #Errors
    predStatError = totBkgr.Clone("PredictedStatError")
    predSystError = totBkgr.Clone("PredictedSystError")
    predTotError = totBkgr.Clone("PredictedTotalError")
    for b in xrange(predSystError.GetNbinsX() + 1):
        predSystError.SetBinError(b, 0.3 * totBkgr.GetBinContent(b))
        syst = predSystError.GetBinError(b)
        stat = predStatError.GetBinError(b)
        predTotError.SetBinError(b, np.sqrt(stat * stat + syst * syst))

    #predTotError.SetFillStyle(3013)
    #predTotError.SetFillColor(ROOT.kGray+2)
    #predTotError.SetMarkerStyle(0)
    #predTotError.Draw("E2 Same")

    #First pad
    plotpad = TPad("plotpad", "plotpad", 0, .3, 1, 0.98)
    plotpad.SetBottomMargin(0.025)
    plotpad.Draw()
    plotpad.cd()

    #Create Stack (Change with most logical ordering)
    hs = THStack("hs", "hs")
    for h in MCHist:
        hs.Add(h)
    hs.Draw(
        "EHist"
    )  #Draw before using GetHistogram, see https://root-forum.cern.ch/t/thstack-gethistogram-null-pointer-error/12892/4
    title = " ; ; Events / " + str(MCHist[0].GetBinWidth(1)) + " GeV"
    hs.SetTitle(title)
    #    hs.GetHistogram().GetXaxis().SetTickLength(0)
    hs.GetHistogram().GetXaxis().SetLabelOffset(9999999)
    hs.GetHistogram().SetMaximum(1)

    #Set range
    overallMin = GetOverallMinimum(MCHist + [DataHist])
    overallMax = GetOverallMaximum([totBkgr] + [DataHist])
    if ylog:
        plotpad.SetLogy()
        hs.SetMinimum(0.3 * overallMin)
        hs.SetMaximum(10 * overallMax)
    else:
        hs.SetMinimum(0.5 * overallMin)
        hs.SetMaximum(1.7 * overallMax)

    DataHist.Draw("EPSame")

    predTotError.SetFillStyle(3013)
    predTotError.SetFillColor(ROOT.kGray + 2)
    predTotError.SetMarkerStyle(0)
    predTotError.Draw("E2 Same")

    #Create Legend
    legend = TLegend(0.7, .7, .9, .9)
    for h, n in zip(MCHist, legendNames):
        legend.AddEntry(h, n)
    legend.AddEntry(DataHist, DataName)
    legend.SetFillStyle(0)
    legend.SetBorderSize(0)

    legend.Draw()

    #Return to canvas
    Canv.cd()

    #Second pad
    ratiopad = TPad("ratiopad", "ratiopad", 0, 0.05, 1, .3)
    ratiopad.SetTopMargin(0.05)
    ratiopad.SetBottomMargin(0.25)
    ratiopad.Draw()
    ratiopad.cd()

    #Errors
    StatErrorRatio = predStatError.Clone("StatErrorRatio")
    StatErrorRatio.SetFillStyle(1001)
    StatErrorRatio.SetFillColor(TColor.GetColor('#6EF9F5'))
    TotErrorRatio = predTotError.Clone("SystErrorRatio")
    TotErrorRatio.SetFillColor(TColor.GetColor('#63E2C6'))
    TotErrorRatio.SetFillStyle(1001)
    for b in xrange(StatErrorRatio.GetNbinsX() + 1):
        if (StatErrorRatio.GetBinContent(b) != 0):
            StatErrorRatio.SetBinError(
                b,
                StatErrorRatio.GetBinError(b) /
                StatErrorRatio.GetBinContent(b))
            TotErrorRatio.SetBinError(
                b,
                TotErrorRatio.GetBinError(b) / TotErrorRatio.GetBinContent(b))
            StatErrorRatio.SetBinContent(b, 1.)
            TotErrorRatio.SetBinContent(b, 1.)
        else:
            StatErrorRatio.SetBinContent(b, 0)
            TotErrorRatio.SetBinContent(b, 0)

    #Set Style for bottom plot
    DataOverMC.SetTitle(";" + xtitle + "; " + ytitle_bottom)
    DataOverMC.GetXaxis().SetTitleSize(.12)
    DataOverMC.GetYaxis().SetTitleSize(.12)
    DataOverMC.GetYaxis().SetTitleOffset(.6)
    DataOverMC.GetXaxis().SetLabelSize(.12)
    DataOverMC.GetYaxis().SetLabelSize(.12)
    DataOverMC.SetMinimum(0.3)
    DataOverMC.SetMaximum(1.7)
    DataOverMC.Draw("EP")
    TotErrorRatio.Draw("E2 same")
    StatErrorRatio.Draw("E2 same")
    DataOverMC.Draw("EPsame")

    #Draw a guide for the eye
    line = TLine(DataOverMC.GetXaxis().GetXmin(), 1,
                 DataOverMC.GetXaxis().GetXmax(), 1)
    line.SetLineColor(ROOT.kRed)
    line.SetLineWidth(1)
    line.SetLineStyle(1)
    line.Draw("Same")

    #Create Legend
    legend_bottom = TLegend(0.2, .8, .9, .9)
    legend_bottom.SetNColumns(3)
    legend_bottom.AddEntry(DataOverMC, "Obs./Pred.")
    legend_bottom.AddEntry(StatErrorRatio, "Stat. Unc.")
    legend_bottom.AddEntry(TotErrorRatio, "Tot. Unc.")
    legend_bottom.SetFillStyle(0)
    legend_bottom.SetBorderSize(0)

    legend_bottom.Draw()

    #Throw CMs lumi at it
    cl.CMS_lumi(Canv, 4, 11, year, 'Preliminary', True)
    print 'save'

    #Save everything
    savePlots(Canv, destination)
    print 'saved'
Esempio n. 24
0
def plotStack():
    inputfile = TFile(filename, "read")
    if inputfile.IsZombie():
        print("inputfile is Zombie")
        sys.exit()

    # loop over features
    for feature, values in features.items():
        file = open(
            "%s%s_%s_isNorm%s_wtStat%s_isBlind%s.txt" %
            (outputdir, feature, plotname, normalization, showStats, blind),
            "w")

        file.write(
            "\\begin{table}[]\n\\resizebox{!}{.33\\paperheight}{\n \\begin{tabular}{|l|l|l|}\n\\hline\nProcess & Yield & Entries \\\\ \\hline \n"
        )
        # set up legend
        legend = TLegend(0.2, 0.6, 0.7, 0.88)
        legend.SetHeader("%i  %s" % (year, region))
        legend.SetNColumns(4)
        legend.SetBorderSize(0)

        c, pad1, pad2 = createCanvasPads()

        hstack = THStack("hstack", "hstack")
        hstack.SetTitle(
            "#scale[0.9]{#font[22]{CMS} #font[12]{Preliminary}                                                            %i at %.2f fb^{-1}(13TeV)}"
            % (year, luminosity[year]))

        histName = "TTH_" + feature  # assuming TTH is always there
        if not inputfile.GetListOfKeys().Contains(histName):
            print("%s doesn't have histogram %s, please use another hist " %
                  (inputfile, histName))
            sys.exit()

        h0 = inputfile.Get(histName)

        h_totalsig = h0.Clone("h_totalsig")
        h_totalsig.SetDirectory(0)
        h_totalsig.Reset()
        h_totalsig.SetMarkerStyle(20)
        h_totalsig.Sumw2()

        h_totalbkg = h0.Clone("h_totalbkg")
        h_totalbkg.SetDirectory(0)
        h_totalbkg.Reset()
        h_totalbkg.SetMarkerStyle(20)
        h_totalbkg.Sumw2()

        h_totalmc = h0.Clone("h_totalmc")
        h_totalmc.SetDirectory(0)
        h_totalmc.Reset()
        h_totalmc.SetLineColor(kBlack)
        h_totalmc.SetFillColor(kGray + 3)
        h_totalmc.SetFillStyle(3001)
        h_totalmc.SetTitle("")
        #h_totalmc.SetMinimum(0.8)
        #h_totalmc.SetMaximum(1.35)
        h_totalmc.Sumw2()
        h_totalmc.SetStats(0)

        h_dataobs = h0.Clone("h_dataobs")
        h_dataobs.SetDirectory(0)
        h_dataobs.Reset()
        h_dataobs.SetMarkerStyle(20)

        # loop over samples
        for sample in Samples:
            hist = h_totalmc.Clone(sample)
            hist.SetDirectory(0)
            hist.Reset()
            if sample not in Process:
                print("sample %s is not in Process " % sample)
                continue
            # loop over data:
            if sample == "Data" or sample == "data":
                for p in Process[sample]:
                    if p not in sampleName:
                        print("process %s is not in sampleName " % s)
                        continue
                    hist_name = p + "_" + feature
                    if not inputfile.GetListOfKeys().Contains(hist_name):
                        print("%s doesn't have histogram %s" %
                              (inputfile, hist_name))
                        continue
                    h1 = inputfile.Get(hist_name).Clone(hist_name)
                    h1.SetDirectory(0)
                    h_dataobs.Add(h1)
                    error = Double(0)
                    h1.IntegralAndError(0, h1.GetNbinsX(), error)
                    if not blind:
                        file.write(
                            "%s &  %.2f +/- %.2f &   %i \\\\ \\hline \n" %
                            (p.replace('_', '\\_'), h1.Integral(), error,
                             h1.GetEntries()))
            # loop over mc
            # loop over signal
            elif sample in Signals:
                for p in Process[sample]:
                    if p not in sampleName:
                        print("process %s is not in sampleName " % s)
                        continue
                    hist_name = p + "_" + feature
                    if not inputfile.GetListOfKeys().Contains(hist_name):
                        print("%s doesn't have histogram %s" %
                              (filename, hist_name))
                        continue
                    h1 = inputfile.Get(hist_name).Clone(hist_name)
                    h1.SetDirectory(0)
                    if p == "FakeSub" and sample == "Fakes":
                        hist.Add(h1, -1)
                        h_totalsig.Add(h1, -1)
                        h_totalmc.Add(h1, -1)
                    else:
                        hist.Add(h1)
                        h_totalsig.Add(h1)
                        h_totalmc.Add(h1)
                    error = Double(0)
                    h1.IntegralAndError(0, h1.GetNbinsX(), error)
                    if h1.Integral() < 0.05 or h1.GetEntries() < 100:
                        file.write(
                            "\\textcolor{red}{%s} &  %.2f +/- %.2f &   %i \\\\ \\hline \n"
                            % (p.replace('_', '\\_'), h1.Integral(), error,
                               h1.GetEntries()))
                    else:
                        file.write(
                            "%s &  %.2f +/- %.2f &   %i \\\\ \\hline \n" %
                            (p.replace('_', '\\_'), h1.Integral(), error,
                             h1.GetEntries()))
                hist.SetFillColor(Color[sample])
                hist.SetLineColor(kBlack)
                hist.SetFillStyle(Style[sample])
                if sample == "TH" and tH != 1:
                    hist.Scale(tH)
                    hist.SetFillColor(Color[sample])
                    hist.SetLineColor(kBlack)
                    hist.SetFillStyle(Style[sample])
                    hstack.Add(hist)
                    legend.AddEntry(hist, "%s * %i" % (sample, tH), "f")
                else:
                    hstack.Add(hist)
                    legend.AddEntry(hist, sample, "f")

        # create required parts
        # loop over bkg
            else:
                for p in Process[sample]:
                    if p not in sampleName:
                        print("process %s is not in sampleName " % p)
                        continue
                    hist_name = p + "_" + feature
                    if not inputfile.GetListOfKeys().Contains(hist_name):
                        print("%s doesn't have histogram %s" %
                              (filename, hist_name))
                        continue
                    h1 = inputfile.Get(hist_name).Clone(hist_name)
                    h1.SetDirectory(0)
                    if p == "FakeSub" and sample == "Fakes":
                        hist.Add(h1, -1)
                        h_totalmc.Add(h1, -1)
                        h_totalbkg.Add(h1, -1)
                    else:
                        hist.Add(h1)
                        h_totalmc.Add(h1)
                        h_totalbkg.Add(h1)
                    error = Double(0)
                    h1.IntegralAndError(0, h1.GetNbinsX(), error)
                    if h1.Integral() < 0.05 or h1.GetEntries() < 100:
                        file.write(
                            "\\textcolor{red}{%s} &  %.2f +/- %.2f &   %i \\\\ \\hline \n"
                            % (p.replace('_', '\\_'), h1.Integral(), error,
                               h1.GetEntries()))
                    else:
                        file.write(
                            "%s &  %.2f +/- %.2f &   %i \\\\ \\hline \n" %
                            (p.replace('_', '\\_'), h1.Integral(), error,
                             h1.GetEntries()))
                hist.SetFillColor(Color[sample])
                hist.SetLineColor(kBlack)
                hist.SetFillStyle(Style[sample])
                if sample == "TH" and tH != 1:
                    hist.Scale(tH)
                    hist.SetFillColor(Color[sample])
                    hist.SetLineColor(kBlack)
                    hist.SetFillStyle(Style[sample])
                    hstack.Add(hist)
                    legend.AddEntry(hist, "%s * %i" % (sample, tH), "f")
                else:
                    hstack.Add(hist)
                    legend.AddEntry(hist, sample, "f")

        error = Double(0)
        h_totalsig.IntegralAndError(0, h_totalsig.GetNbinsX(), error)
        file.write("signal &  %.2f +/- %.2f &   %i \\\\ \\hline \n" %
                   (h_totalsig.Integral(), error, h_totalsig.GetEntries()))

        error = Double(0)
        h_totalbkg.IntegralAndError(0, h_totalbkg.GetNbinsX(), error)
        file.write("bkg &  %.2f +/- %.2f &   %i \\\\ \\hline \n" %
                   (h_totalbkg.Integral(), error, h_totalbkg.GetEntries()))

        # create required parts

        if blind:
            h_sqrtB = createSqrt(h_totalbkg)
            h_MCerr = createTotalMCErr(h_sqrtB, values["xlabel"])
            h_ratio = createRatio(h_totalsig, h_sqrtB, values["xlabel"],
                                  normalization)
        else:
            h_MCerr = createTotalMCErr(h_totalmc, feature)
            h_ratio = createRatio(h_dataobs, h_totalmc, values["xlabel"],
                                  normalization)
            legend.AddEntry(h_dataobs, "observed", "lep")

        legend.AddEntry(h_totalmc, "Uncertainty", "f")

        # draw everything

        pad1.cd()
        if values["logy"] == 1:
            pad1.SetLogy()
        maximum = h_dataobs.GetMaximum()
        upperbound = 2. * maximum
        lowerbound = -maximum / 40.
        if values["logy"] == 1:
            upperbound = 1000 * maximum
            lowerbound = 0.1

        hstack.SetMinimum(lowerbound)
        hstack.SetMaximum(upperbound)
        hstack.Draw("HISTY")
        # Adjust y-axis settings
        y = hstack.GetYaxis()
        y.SetTitle("Events ")
        y.SetTitleSize(25)
        y.SetTitleFont(43)
        y.SetTitleOffset(1.55)
        y.SetLabelFont(43)
        y.SetLabelSize(20)

        nbins = h_ratio.GetNbinsX()
        #hstack.GetXaxis().SetRange(0, nbins+1)
        hstack.GetXaxis().SetRangeUser(values["min"], values["max"])

        h_totalmc.Draw("e2same")
        if not blind:
            h_dataobs.Draw("same")
        legend.Draw("same")

        pad2.cd()
        if blind:
            h_ratio.SetMinimum(0.)
            #maximum = h_ratio.GetMaximum()
            #upperbound = 1.5*maximum
            #h_ratio.SetMaximum(upperbound)
            h_ratio.SetMaximum(3.)
            h_ratio.GetXaxis().SetRangeUser(values["min"], values["max"])
            h_ratio.Draw("")
        else:
            h_MCerr.SetMinimum(0.5)
            h_MCerr.SetMaximum(1.8)
            h_MCerr.GetXaxis().SetRangeUser(values["min"], values["max"])
            h_MCerr.Draw("e2")
            h_ratio.Draw("same")

        c.SaveAs(
            "%s%s_%s_isNorm%s_wtStat%s_isBlind%s_stack.png" %
            (outputdir, feature, plotname, normalization, showStats, blind))
        file.write("\\end{tabular}\n}\n\\end{table}\n")
        file.close()
    inputfile.Close()
Esempio n. 25
0
def Draw_1D_Comp(rootfile_name, var, min, max, WP_list, leg, isTail=False):
    gStyle.SetErrorX(0.5)

    ## The TStack is created with min and max set
    stack = THStack("stack", var.name)
    stack.SetMinimum(min)
    stack.SetMaximum(max)

    for wp in WP_list:

        ## Loading the graph using its name and file location
        graph_name = "{}_{}".format(var.name, wp.name)
        if isTail:
            graph_name += "_cumulative"

        graph = GetGraphFromFile(rootfile_name, graph_name)
        if graph == -1:
            continue

        ## Setting the colors specific to the working point
        graph.SetLineColor(wp.colour)
        graph.SetMarkerColor(wp.colour)
        graph.SetMarkerStyle((wp.marker))

        leg.AddEntry(graph, wp.name, "p")

        width = graph.GetBinWidth(0)

        ## We check if the legend requires values for statistics
        if leg.GetNColumns() == 2:

            ## Calculate the MEAN and RMSE and add to the legend
            mean = graph.GetMean()
            stdev = graph.GetStdDev()
            rms = np.sqrt(mean * mean + stdev * stdev)

            ## Adding the legend entry
            # leg.AddEntry( 0, "{:5.2f}".format(mean), "")
            leg.AddEntry(0, "{:5.2f}".format(rms), "")

        ## Adding the object to the stack
        stack.Add(graph)
        del graph

    ## Checking to see if any graphs were found for this variable
    if stack.GetNhists() == 0:
        print("\n\n\nNo graphs found for working point {}\n\n\n".format(
            wp.name))
        return -1

    ## Drawing the stack on the currrent canvas
    stack.Draw("nostack EP")
    leg.Draw()

    ## Setting axis labels
    if isTail:
        stack.GetXaxis().SetTitle(var.x_label + " threshold " + var.units)
        stack.GetYaxis().SetTitle("#it{f}_{tail}")
    else:
        stack_y_label = "Events per {:.0f} GeV".format(
            width) if width > 1 else "Events per GeV"

        stack.GetXaxis().SetTitle(var.x_label + " " + var.units)
        stack.GetYaxis().SetTitle(stack_y_label)

    ## Moving axis tick marks
    stack.GetYaxis().SetMaxDigits(3)
    stack.GetXaxis().SetLabelOffset(0.017)

    return stack
Esempio n. 26
0
def draw(hist, data, back, sign, snorm=1, ratio=0, poisson=False, log=False):
    # If not present, create BkgSum
    if not 'BkgSum' in hist.keys():
        hist['BkgSum'] = hist['data_obs'].Clone(
            "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
        hist['BkgSum'].Reset("MICES")
        for i, s in enumerate(back):
            hist['BkgSum'].Add(hist[s])
    hist['BkgSum'].SetMarkerStyle(0)

    # Some style
    for i, s in enumerate(data):
        hist[s].SetMarkerStyle(21)
        hist[s].SetMarkerSize(1.25)
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)

    for i, s in enumerate(data + back + sign + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow

    # Set Poisson error bars
    #if len(data) > 0: hist['data_obs'].SetBinErrorOption(1) # doesn't work

    # Poisson error bars for data
    if poisson:
        alpha = 1 - 0.6827
        hist['data_obs'].SetBinErrorOption(TH1.kPoisson)
        data_graph = TGraphAsymmErrors(hist['data_obs'].GetNbinsX())
        data_graph.SetMarkerStyle(hist['data_obs'].GetMarkerStyle())
        data_graph.SetMarkerSize(hist['data_obs'].GetMarkerSize())
        res_graph = data_graph.Clone()
        for i in range(hist['data_obs'].GetNbinsX()):
            N = hist['data_obs'].GetBinContent(i + 1)
            B = hist['BkgSum'].GetBinContent(i + 1)
            L = 0 if N == 0 else ROOT.Math.gamma_quantile(alpha / 2, N, 1.)
            U = ROOT.Math.gamma_quantile_c(alpha / 2, N + 1, 1)
            data_graph.SetPoint(
                i, hist['data_obs'].GetXaxis().GetBinCenter(i + 1),
                N if not N == 0 else -1.e99)
            data_graph.SetPointError(
                i, hist['data_obs'].GetXaxis().GetBinWidth(i + 1) / 2.,
                hist['data_obs'].GetXaxis().GetBinWidth(i + 1) / 2., N - L,
                U - N)
            res_graph.SetPoint(i,
                               hist['data_obs'].GetXaxis().GetBinCenter(i + 1),
                               N / B if not B == 0 and not N == 0 else -1.e99)
            res_graph.SetPointError(
                i, hist['data_obs'].GetXaxis().GetBinWidth(i + 1) / 2.,
                hist['data_obs'].GetXaxis().GetBinWidth(i + 1) / 2.,
                (N - L) / B if not B == 0 else -1.e99,
                (U - N) / B if not B == 0 else -1.e99)

    # Create stack
    bkg = THStack("Bkg",
                  ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    n = len([x for x in data + back + ['BkgSum'] + sign if samples[x]['plot']])
    for i, s in enumerate(sign):
        if 'sublabel' in samples[s]: n += 1
        if 'subsublabel' in samples[s]: n += 1
    #leg = TLegend(0.68, 0.9-0.05*n, 0.93, 0.9)
    leg = TLegend(0.68 - 0.05, 0.9 - 0.05 * n, 0.93, 0.9)  #DCMS
    leg.SetTextSize(0.03)  #DCMS
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetHeader("Signal x-sec=%.0f pb" % (1 * snorm))
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], samples[data[0]]['label'], "ple1")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], samples[s]['label'], "f")
    for i, s in enumerate(sign):
        leg.AddEntry(hist[s], samples[s]['label'], "f")

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[-1].GetXaxis().GetTitle(), 1000,
                 800 if ratio else 700)

    if ratio:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), ratio)
        setBotPad(c1.GetPad(2), ratio)
    c1.cd(1)
    c1.GetPad(bool(ratio)).SetTopMargin(0.06)
    c1.GetPad(bool(ratio)).SetRightMargin(0.05)
    c1.GetPad(bool(ratio)).SetTicks(1, 1)
    if log:
        c1.GetPad(bool(ratio)).SetLogy()
        #c1.GetPad(bool(ratio)).SetLogx()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if poisson: data_graph.Draw("SAME, PE")
    elif len(data) > 0: hist['data_obs'].Draw("SAME, PE")
    for i, s in enumerate(sign):
        if samples[s]['plot']:
            hist[s].DrawNormalized("SAME, HIST",
                                   hist[s].Integral() * snorm)  # signals

    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1.075)

    # Determine range
    if 'data_obs' in hist:
        bkg.SetMaximum((2.5 if log else 1.2) * max(
            bkg.GetMaximum(),
            hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) +
            hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
        bkg.SetMinimum(
            max(
                min(
                    hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
                    )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.)
    else:
        bkg.SetMaximum(bkg.GetMaximum() * (2.5 if log else 1.2))
        bkg.SetMinimum(5.e-1 if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetMoreLogLabels(True)

    leg.Draw()
    #drawCMS(LUMI, "Preliminary")
    #drawRegion(channel)
    #drawAnalysis("LL")

    setHistStyle(bkg, 1.2 if ratio else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if ratio else 1.1)

    if ratio:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(2)
        errLine.SetFillStyle(0)
        errLine.SetLineColor(2)  #L#
        errLine.SetLineStyle(2)  #L#
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if 'data_obs' in hist:
            res = hist['data_obs'].Clone("Residues")
            for i in range(0, res.GetNbinsX() + 1):
                if hist['BkgSum'].GetBinContent(i) > 0:
                    res.SetBinContent(
                        i,
                        res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                    res.SetBinError(
                        i,
                        res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
            setBotStyle(res)
            if poisson: res_graph.Draw("SAME, PE0")
            else: res.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawKolmogorov(hist['data_obs'], hist['BkgSum'])
        else:
            res = None
    c1.Update()

    # return list of objects created by the draw() function
    return [
        c1, bkg, leg, err if ratio else None, errLine if ratio else None,
        res if ratio else None, data_graph if poisson else None,
        res_graph if poisson else None
    ]
Esempio n. 27
0
def createPlots_(plot, compounddetectorname):
    """Cumulative material budget from simulation.
    
       Internal function that will produce a cumulative profile of the
       material budget inferred from the simulation starting from the
       single detectors that compose the tracker. It will iterate over
       all existing detectors contained in the DETECTORS
       dictionary. The function will automatically skip non-existent
       detectors.

    """

    theDirname = "Figures"

    hist_X0_detectors = OrderedDict()
    if plot not in plots.keys():
        print("Error: chosen plot name not known %s" % plot)
        return

    # We need to keep the file content alive for the lifetime of the
    # full function....
    subDetectorFiles = []

    hist_X0_elements = OrderedDict()
    prof_X0_elements = OrderedDict()
    for subDetector, color in DETECTORS.iteritems():
        subDetectorFilename = "matbdg_%s.root" % subDetector
        if not checkFile_(subDetectorFilename):
            print("Error opening file: %s" % subDetectorFilename)
            continue

        subDetectorFiles.append(TFile(subDetectorFilename))
        subDetectorFile = subDetectorFiles[-1]
        print("Opening file: %s" % subDetectorFilename)
        prof_X0_XXX = subDetectorFile.Get("%d" % plots[plot].plotNumber)

        hist_X0_detectors[subDetector] = prof_X0_XXX.ProjectionX()

        # category profiles
        for label, [num, color, leg] in hist_label_to_num.iteritems():
            prof_X0_elements[label] = subDetectorFile.Get(
                "%d" % (num + plots[plot].plotNumber))
            hist_X0_elements[label] = assignOrAddIfExists_(
                hist_X0_elements.setdefault(label, None),
                prof_X0_elements[label])

    cumulative_matbdg = TH1D(
        "CumulativeSimulMatBdg", "CumulativeSimulMatBdg",
        hist_X0_detectors["BeamPipe"].GetNbinsX(),
        hist_X0_detectors["BeamPipe"].GetXaxis().GetXmin(),
        hist_X0_detectors["BeamPipe"].GetXaxis().GetXmax())
    cumulative_matbdg.SetDirectory(0)

    # colors
    for det, color in DETECTORS.iteritems():
        setColorIfExists_(hist_X0_detectors, det, color)

    for label, [num, color, leg] in hist_label_to_num.iteritems():
        hist_X0_elements[label].SetFillColor(color)

    # First Plot: BeamPipe + Tracker + ECAL + HCal + HGCal + MB + MGNT
    # stack
    stackTitle_SubDetectors = "Material Budget;%s;%s" % (plots[plot].abscissa,
                                                         plots[plot].ordinate)
    stack_X0_SubDetectors = THStack("stack_X0", stackTitle_SubDetectors)
    for det, histo in hist_X0_detectors.iteritems():
        stack_X0_SubDetectors.Add(histo)
        cumulative_matbdg.Add(histo, 1)

    # canvas
    can_SubDetectors = TCanvas("can_SubDetectors", "can_SubDetectors", 800,
                               800)
    #can_SubDetectors.Range(0,0,25,25)
    can_SubDetectors.SetFillColor(kWhite)

    # Draw
    stack_X0_SubDetectors.SetMinimum(plots[plot].ymin)
    stack_X0_SubDetectors.SetMaximum(plots[plot].ymax)
    stack_X0_SubDetectors.Draw("HIST")
    #stack_X0_SubDetectors.GetXaxis().SetLimits(plots[plot].xmin, plots[plot].xmax)

    # Legenda
    theLegend_SubDetectors = TLegend(0.130, 0.7, 0.93,
                                     0.90)  #(0.180,0.8,0.98,0.90)
    theLegend_SubDetectors.SetNColumns(2)
    theLegend_SubDetectors.SetFillColor(0)
    theLegend_SubDetectors.SetFillStyle(0)
    theLegend_SubDetectors.SetBorderSize(0)

    for det, histo in hist_X0_detectors.iteritems():
        theLegend_SubDetectors.AddEntry(histo, det, "f")

    theLegend_SubDetectors.Draw()

    # text
    text_SubDetectors = TPaveText(0.130, 0.627, 0.352, 0.687,
                                  "NDC")  #(0.180,0.727,0.402,0.787,"NDC")
    text_SubDetectors.SetFillColor(0)
    text_SubDetectors.SetBorderSize(0)
    text_SubDetectors.AddText("CMS Simulation")
    text_SubDetectors.SetTextAlign(11)
    text_SubDetectors.Draw()

    # Store
    can_SubDetectors.Update()
    if not checkFile_(theDirname):
        os.mkdir(theDirname)
    can_SubDetectors.SaveAs("%s/MaterialBdg_%s_%s.pdf" %
                            (theDirname, compounddetectorname, plot))
    can_SubDetectors.SaveAs("%s/MaterialBdg_%s_%s.png" %
                            (theDirname, compounddetectorname, plot))
    can_SubDetectors.SaveAs("%s/MaterialBdg_%s_%s.root" %
                            (theDirname, compounddetectorname, plot))

    if plot == "x_vs_eta" or plot == "l_vs_eta":
        canname = "MBCan_1D_%s_%s_total" % (compounddetectorname, plot)
        can2 = TCanvas(canname, canname, 800, 800)
        can2.Range(0, 0, 25, 25)
        can2.SetFillColor(kWhite)
        gStyle.SetOptStat(0)
        gStyle.SetOptTitle(0)
        #title = TPaveLabel(.11,.95,.35,.99,"Total accumulated material budget","brndc")
        stack_X0_SubDetectors.GetStack().Last().SetMarkerStyle(34)
        stack_X0_SubDetectors.GetStack().Last().GetXaxis().SetRangeUser(
            1.0, 3.5)
        stack_X0_SubDetectors.GetStack().Last().Draw()
        stack_X0_SubDetectors.GetYaxis().SetTitleOffset(1.15)
        can2.Update()
        can2.Modified()
        can2.SaveAs("%s/%s_%s_total_Zplus.pdf" %
                    (theDirname, compounddetectorname, plot))
        can2.SaveAs("%s/%s_%s_total_Zplus.png" %
                    (theDirname, compounddetectorname, plot))
        stack_X0_SubDetectors.GetStack().Last().GetXaxis().SetRangeUser(
            -3.5, -1.0)
        stack_X0_SubDetectors.GetStack().Last().Draw()
        stack_X0_SubDetectors.GetYaxis().SetTitleOffset(1.15)
        can2.Update()
        can2.Modified()
        can2.SaveAs("%s/%s_%s_total_Zminus.pdf" %
                    (theDirname, compounddetectorname, plot))
        can2.SaveAs("%s/%s_%s_total_Zminus.png" %
                    (theDirname, compounddetectorname, plot))

        #Also print them to give them exact numbers
        etavalues = []
        matbudginX0 = []
        matbudginIntLen = []
        for binx in range(
                0,
                stack_X0_SubDetectors.GetStack().Last().GetXaxis().GetNbins()):
            bincontent = stack_X0_SubDetectors.GetStack().Last().GetBinContent(
                binx)
            if bincontent == 0: continue
            etavalues.append(
                stack_X0_SubDetectors.GetStack().Last().GetBinCenter(binx))
            if plot == "x_vs_eta":
                matbudginX0.append(bincontent)
                d1 = {'Eta': etavalues, 'MatBudInX0': matbudginX0}
                df1 = pd.DataFrame(data=d1).round(2)
                df1.to_csv(
                    r'/afs/cern.ch/work/a/apsallid/CMS/PFCalStudies/CMS-HGCAL/matbudV10fromVertexToBackofHGCal/CMSSW_11_0_X_2019-06-04-2300/src/Validation/Geometry/test/EtavsMatBudinXo.txt',
                    sep=' ',
                    index=False,
                    header=False)
                #print df1
            if plot == "l_vs_eta":
                matbudginIntLen.append(bincontent)
                d2 = {'Eta': etavalues, 'MatBudInIntLen': matbudginIntLen}
                df2 = pd.DataFrame(data=d2).round(2)
                df2.to_csv(
                    r'/afs/cern.ch/work/a/apsallid/CMS/PFCalStudies/CMS-HGCAL/matbudV10fromVertexToBackofHGCal/CMSSW_11_0_X_2019-06-04-2300/src/Validation/Geometry/test/EtavsMatBudInIntLen.txt',
                    sep=' ',
                    index=False,
                    header=False)
                #print df2

    return cumulative_matbdg
Esempio n. 28
0
def stackHistograms(t_plots, t_processList, l_histoList, l_legendList, l_textList, l_axisList, saveAs,l_ratioList,  saveString, tagText):

    ''' 
    PURPOSE:

    Function to create stacked plots and save them to the local disk.
    This function will:
        * Create a stacked plot
        * Create a ratio plot
        * Allow for Blinding
        * Save the canvas as requested
    '''
    # Release the variables from the lists
    h_st, location_suf, name, c_x, c_y, v_rebin, xMin, xMax, doBlinding, b_low, b_high = l_histoList
    l_st, l_x1, l_y1, l_x2, l_y2, l_textSize = l_legendList
    t_st, plotText, t_textSize, t_alignment, t_start, t_gap, t_x = l_textList
    a_st, xLabel, yLabel, xOffset, yOffset, a_labelSize, a_textSize, y_scale = l_axisList
    r_st, doRatio, r_yLabel, r_xOffset, r_yOffset, r_labelSize, r_textSize = l_ratioList
    len_plots = len(t_plots)

    plotText = plotText + tagText
    
    l_overlay = []
    l_data    = []
    sh        = THStack('sh', '')
    t_legend = []
    listMC = TList()
    for i in xrange(len_plots):
        process, location_pre, v_colour, v_scale, ovack, sigrouta, l_marker, sel = t_processList[i]
        histogram = rebin(t_plots[i][0], v_rebin)
        histogram.Scale(float(v_scale))
        histogram.SetLineWidth(1)
        histogram.SetLineColor(int(v_colour))
        l_legendLine = [t_plots[i][0], process, l_marker]
        t_legend.append(l_legendLine)
        
        if sigrouta == "DATA" and int(doBlinding):
            histogram = blindingPlots(histogram, b_low, b_high)
        if ovack == "STACK":
            if sigrouta == "BACKGROUND":
                listMC.Add(histogram)
            histogram.SetFillColor(int(v_colour))
            sh.Add(histogram)
        elif ovack == "OVERLAY" and sigrouta != "DATA":
            l_overlay.append(histogram)
        elif ovack == "OVERLAY" and sigrouta == "DATA":
            histogram.SetMarkerStyle(8)
            histogram.SetMarkerColor(1)
            l_data.append(histogram)
        else:
            print "\n%s or %s are not a valid options" % (sigrouta, ovack)
            sys.exit(1)
    mergeMC = histogram.Clone('mergeMC')
    mergeMC.Reset()
    mergeMC.Merge(listMC)

    gROOT.SetBatch(1)
    c = TCanvas('c', 'c', int(c_x), int(c_y))
    gStyle.SetOptStat(0)
    if int(doRatio):
        m_Pad = TPad('m_pad', 'm_pad', 0, 0.30, 1, 1)
        r_Pad = TPad('r_pad', 'r_pad', 0, 0, 1, 0.30)
        m_Pad.Draw()
        r_Pad.Draw()
        m_Pad.SetTopMargin(0.05)
        m_Pad.SetBottomMargin(0.01)
        r_Pad.SetTopMargin(0)
        r_Pad.SetBottomMargin(0.3)
        m_Pad.cd()
        m_Pad.SetTicks(1)
        m_Pad.Update()
    else:
        c.SetTicks(1)
        c.Update
    
    # Draw our plots we defined earlier. First the stacked plot, then any overlaid plots and finally data
    sh.Draw()
    if l_overlay:
        for i in xrange(len(l_overlay)):
            l_overlay[i].Draw('hist same')
    h_MCerr = getStatUncert(mergeMC)
    h_MCerr.SetMarkerSize(0)
    h_MCerr.SetFillColor(921)
    h_MCerr.SetFillStyle(3344)
    h_MCerr.Draw('e2s same')
    t_legend.append([h_MCerr, 'Stat Uncert.', 'f']) 

    if l_data:
        h_data = mergeData(l_data, doBlinding, b_low, b_high)
        h_data.Draw('epsames')
    
    ## Next up is to add the styling:
    ## 1, Axis labels
    ## 2. Text on the plot
    ## 3. Legend
    if not int(doRatio):
        setupAxes(sh, xLabel, yLabel, xOffset, 0.8, a_textSize, xMin, xMax, y_scale)
    else:
        sh.SetMinimum(0.001)
        setupAxes(sh, "", yLabel, -999, yOffset, a_textSize, xMin, xMax, y_scale)

    setupLegend(t_legend, l_x1, l_y1, l_x2, l_y2, l_textSize)
    setupTextOnPlot(plotText, t_textSize, t_alignment, t_x, t_start, t_gap)
    
    if int(doRatio):
        r_Pad.cd()
        r_Pad.SetTicks(1)
        r_Pad.Update()
        l_mc   = TList()
        l_data = TList()
        for i in xrange(len_plots):
            l_histogram = rebin(t_plots[i][0], v_rebin)
            process, location_pre, v_colour, v_scale, ovack, sigrouta, l_marker, tag = t_processList[i] 
            if sigrouta == "SIGNAL":
                l_mc.Add(l_histogram)
            elif sigrouta == "BACKGROUND":
                l_mc.Add(l_histogram)
            elif sigrouta == "DATA":
                l_data.Add(l_histogram)
            elif sigrouta == "OVERLAY":
                pass
            else:
                print "\nOption %s not available" % (sigrouta)

        mh_MC = l_histogram.Clone('mh_MC')
        mh_MC.Merge(l_mc)

        h_statMC = getRelError(mh_MC)
        h_statMC.SetMarkerSize(0)
        h_statMC.SetFillColor(921)
        h_statMC.SetFillStyle(3344)
        h_statMC.Draw('e2s')
        setupAxes(h_statMC, xLabel, r_yLabel, r_xOffset, r_yOffset, r_textSize, xMin, xMax, 1)
        h_statMC.GetYaxis().SetRangeUser(-0.38, 0.38)
       
        mh_data = l_histogram.Clone('mh_data')
        mh_data.Merge(l_data)
        h_dataMC = getRatio(mh_data, mh_MC, doBlinding, b_low, b_high)
        h_dataMC.Draw('epsames')
 
        h_line = TLine(float(xMin), 0, float(xMax), 0)
        h_line.SetLineWidth(2)
        h_line.SetLineColor(1)
        h_line.SetLineStyle(2)
        h_line.Draw('same')
    
    savePlots(c, saveString, saveAs)
Esempio n. 29
0
    #MC DY hist
    LepBDTMCDY_list[i].Scale(norm)  # MC normalization
    LepBDTMCDY_list[i].SetFillColor(kMagenta - 6)
    LepBDTMCDY_list[i].SetLineColor(kBlack)
    hs.Add(LepBDTMCDY_list[i])

    #upper plot pad
    pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0)
    pad1.Draw()
    pad1.cd()

    hs.SetMaximum(1.6 * max(hs.GetMaximum(), LepBDTDATA_list[i].GetMaximum()))
    LepBDTDATA_list[i].SetMaximum(
        1.6 * max(hs.GetMaximum(), LepBDTDATA_list[i].GetMaximum()))
    hs.SetMinimum(10)  #EF for ele BDT

    hs.Draw("histo")
    LepBDTDATA_list[i].Draw("sameEP")

    hs.SetTitle("")
    hs.GetXaxis().SetTitle("BDT score")
    hs.GetXaxis().SetRangeUser(0.75, 1)  #EF for ele BDT
    hs.GetXaxis().SetLabelFont(43)
    hs.GetXaxis().SetLabelSize(15)
    hs.GetYaxis().SetTitleSize(20)
    hs.GetYaxis().SetTitleFont(43)
    hs.GetYaxis().SetTitleOffset(1.8)
    hs.GetYaxis().SetLabelFont(43)
    hs.GetYaxis().SetLabelSize(15)
    hs.GetYaxis().SetTitle("Events")
Esempio n. 30
0
def main():

    extension = "pdf"

    ####### File names and locations ########
    network_name = "deep_ann"
    data_set_name = "ttbar_1in3_tt_strict"  #_NOJETS _SOMEJETS
    ######################################

    process = "#it{t#bar{t}}"
    luminosity = 44307.4

    ## Initialising plotting variables
    Var_Lin = Plotting.Variable(
        "Lin",
        "Linear_Response",
        "#Delta_{T}^{lin}",
        "",
    )
    Var_Truth = Plotting.Variable("Truth", "TrueET", "True #it{E}_{T}^{miss}",
                                  "[GeV]")

    ymin = -0.2
    ymax = 0.5

    plot_list = [
        ("main_ann", "Tight", "", 4, 21),
        ("main_ann", "Network", "", 2, 20),
        ("flat_ann", "Network", "(A) ", 3, 22),
        ("smpl_ann", "Network", "(B) ", 6, 23),
    ]

    ## Creating the plots
    gROOT.SetStyle("ATLAS")
    gROOT.ForceStyle()
    gStyle.SetErrorX(0.5)
    canvas = TCanvas("canvas_{}_{}".format(Var_Truth.name, Var_Lin.name),
                     'A basic canvas', 800, 600)

    ## Adding in the legend
    leg = Plotting.Create_Legend(0.65, 0.70, 0.95, 0.95)

    ## Creating the stack
    stack = THStack("stack", Var_Truth.name + Var_Lin.name)
    stack.SetMinimum(ymin)
    stack.SetMaximum(ymax)

    ## Cycling through the different network directories
    for network_name, wp_name, prefix, colour, marker in plot_list:

        OUTPUT_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Output")
        hist_dir = os.path.join(OUTPUT_dir, network_name, data_set_name)
        rootfile_name = os.path.join(hist_dir, "histograms.root")

        ## Creating the working point variable
        wp = Plotting.WorkingPoint(wp_name)

        ## Generating the graph name
        graph_name = "{}_vs_{}_{}".format(Var_Truth.name, Var_Lin.name,
                                          wp.name)

        ## Loading the graph using its name and file location
        graph = Plotting.GetGraphFromFile(rootfile_name, graph_name)
        if graph == -1:
            continue

        ## Setting the colors specific to the working point
        graph.SetLineColor(colour)
        graph.SetMarkerColor(colour)
        graph.SetMarkerStyle(marker)

        ## Adding the legend entry
        leg.AddEntry(graph, prefix + wp.name, "p")

        ## Adding the object to the stack
        stack.Add(graph)
        del graph

    ## Drawing the stack on the currrent canvas
    stack.Draw("nostack")
    leg.Draw()

    ## Setting axis labels
    stack.GetXaxis().SetTitle(Var_Truth.x_label + " " + Var_Truth.units)
    stack.GetYaxis().SetTitle(Var_Lin.x_label + " " + Var_Lin.units)

    ## Moving axis tick marks
    stack.GetYaxis().SetMaxDigits(3)
    stack.GetXaxis().SetLabelOffset(0.017)

    ## Drawing all text
    left = 0.29
    size = 1.0
    shift = 0.06
    pos = 0.88

    Plotting.Draw_ATLASLabel(left, pos, "Simulation", scale=1.0)
    pos -= shift
    Plotting.Draw_Text(left, pos, "work in progress", scale=size)
    pos -= shift
    Plotting.Draw_Lumi(left, pos, 0, scale=size)
    pos -= shift
    Plotting.Draw_Text(left, pos, process, scale=size)
    pos -= shift

    ## Updating the canvas
    canvas.Update()

    out_file = "response_comparison_zoom.{}".format(extension)
    canvas.Print(out_file)

    del canvas

    return 0