Exemplo n.º 1
0
# Standard package
import imp, sys, os, time

cfgFileName = sys.argv[1]
file = open(cfgFileName, 'r')
cfg = imp.load_source('UFNTuple.__cfg_to_run__', cfgFileName, file)
componentList = cfg.componentList
outputInfo = cfg.outputInfo
endSequence = cfg.endSequence
mergeSampleDict = cfg.mergeSampleDict if hasattr(cfg,
                                                 "mergeSampleDict") else {}
mergeSigSampleDict = cfg.mergeSigSampleDict if hasattr(
    cfg, "mergeSigSampleDict") else {}
verbose = cfg.verbose if hasattr(cfg, "verbose") else False
skipGitDetail = cfg.skipGitDetail if hasattr(cfg, "skipGitDetail") else False

if verbose:
    pyPrint("Starting")
start_time = time.time()

pyPrint("\nBegin Summarising\n")
pyPrint("\nInput used: " + outputInfo.outputDir + "\n")
endSequence.run(outputInfo,
                componentList,
                mergeSampleDict=mergeSampleDict,
                mergeSigSampleDict=mergeSigSampleDict)

elapsed_time = time.time() - start_time
pyPrint("Time used: " + str(elapsed_time) + "s")
Exemplo n.º 2
0
componentList           = cfg.componentList
nEvents                 = cfg.nEvents
disableProgressBar      = cfg.disableProgressBar
sequence                = cfg.sequence
outputInfo              = cfg.outputInfo
endSequence             = cfg.endSequence
justEndSequence         = cfg.justEndSequence if hasattr(cfg,"justEndSequence") else False
mergeSampleDict         = cfg.mergeSampleDict if hasattr(cfg,"mergeSampleDict") else {}
mergeSigSampleDict      = cfg.mergeSigSampleDict if hasattr(cfg,"mergeSigSampleDict") else {}
verbose                 = cfg.verbose if hasattr(cfg,"verbose") else False
skipGitDetail           = cfg.skipGitDetail if hasattr(cfg,"skipGitDetail") else False
eventSelection          = cfg.eventSelection if hasattr(cfg,"eventSelection") else None
checkInputFile          = cfg.checkInputFile if hasattr(cfg,"checkInputFile") else False

if verbose:
    pyPrint("Starting")
start_time = time.time()

if not justEndSequence:
    if verbose:
        pyPrint("Initiating progress bar")
    progressBar = ProgressBar()
    
    if nCores != 1:
        progressMonitor      = BProgressMonitor(progressBar)
        communicationChannel = CommunicationChannel(nCores,progressMonitor)
    else:
        progressMonitor      = ProgressMonitor(progressBar)
        communicationChannel = CommunicationChannel0(progressMonitor)
        pass
        
    def drawDataMCPlot(self, collector, plot, outputDir, switch):
        c = ROOT.TCanvas("c_" + plot.key, "c_" + plot.key, 0, 0, 650, 750)

        axisLabel = self.getAxisTitle(plot)

        if collector.dataSamples:
            ROOT.gSystem.Load(os.environ["BASE_PATH"] +
                              "/Plotter/makePoissonHist_cc.so")
            dataHist, dataCount, dataCountErr = self.stackData(collector, plot)
            dataGraph = self.makeTGraph(dataHist,
                                        filter_func=lambda x: x < 4.0 or
                                        (x > 8.5 and x < 11.0))
            self.setDataHistStyle(dataGraph)

        if collector.bkgSamples:
            histList, stack, smCount, smCountErrSq, total, bkdgErr = self.stackMC(
                collector,
                plot,
                switch,
                histToScale=dataHist if self.scaleToData else None)
            stack.SetTitle("")

        if collector.signalSamples:
            sigHistList = self.makeSignalHist(collector, plot)
        else:
            sigHistList = []

        if collector.bkgSamples:
            bkgMax = stack.GetMaximum()
        else:
            bkgMax = 0.

        if collector.dataSamples:
            dataMax = dataHist.GetMaximum()
        else:
            dataMax = 0.

        if collector.signalSamples:
            sigMax = max(
                [hist.GetMaximum() for hist, sample, sigCount in sigHistList])
        else:
            sigMax = 0.

        maximum = max([bkgMax, dataMax, sigMax])

        c.SetBottomMargin(0.0)

        upperPad = ROOT.TPad("upperPad", "upperPad", .001, 0.25, .995, .995)
        lowerPad = ROOT.TPad("lowerPad", "lowerPad", .001, .001, .995, .32)

        upperPad.Draw()
        lowerPad.Draw()

        lowerPad.cd()
        lowerPad.SetGridy(1)
        ROOT.gPad.SetBottomMargin(0.24)

        _, bkdgErrRatio, line = self.makeRatioPlot(dataHist, total, bkdgErr)
        ratio = self.makeRatioTGraph(dataHist, total, bkdgErr)

        self.setDataHistStyle(ratio)
        self.setRatioHistStyle(
            bkdgErrRatio,
            axisLabel,
            plot,
        )
        ratio.GetYaxis().SetRangeUser(
            0.0,
            max([ratio.GetPointY(i) for i in range(1,
                                                   ratio.GetN() + 1)]) * 1.5)
        bkdgErrRatio.GetYaxis().SetRangeUser(
            0.0,
            max([ratio.GetPointY(i) for i in range(1,
                                                   ratio.GetN() + 1)]) * 1.5)
        if plot.plotSetting.ratio_range:
            ratio.GetYaxis().SetRangeUser(*plot.plotSetting.ratio_range)
            bkdgErrRatio.GetYaxis().SetRangeUser(*plot.plotSetting.ratio_range)
        ROOT.gStyle.SetErrorX(0)
        bkdgErrRatio.SetMarkerSize(0)
        bkdgErrRatio.Draw("e2")
        ratio.Draw("samePZ")
        line.Draw()

        upperPad.cd()

        upperPad.SetLogy(0)
        stack.SetMaximum(maximum * plot.plotSetting.linear_max_factor)
        dataHist.SetMaximum(maximum * plot.plotSetting.linear_max_factor)

        stack.Draw('hist')
        self.setStackAxisTitle(stack, axisLabel, plot)
        stack.GetXaxis().SetLabelSize(plot.plotSetting.stack_x_label_size)
        stack.GetXaxis().SetTitleOffset(1.00)
        stack.GetYaxis().SetLabelSize(0.05)
        stack.Draw('samee2')
        bkdgErrGraph = self.makeTGraph(bkdgErr,
                                       force_x_axis_err=True,
                                       filter_func=lambda x: x < 4.0 or
                                       (x > 8.5 and x < 11.0))
        for errHist in [
                bkdgErrGraph,
                bkdgErr,
        ]:
            errHist.SetMarkerStyle(1)
            errHist.SetLineWidth(1)
            errHist.SetFillColor(13)
            errHist.SetFillStyle(bkdgErrBarColor)
            errHist.SetFillStyle(3002)
            #errHist.SetFillColorAlpha(ROOT.kBlack,1)
        bkdgErrGraph.Draw("sameE2")
        for hist, sample, sigCount in sigHistList:
            hist.Draw('samehist')

        leg = self.makeLegend1D(
            histList,
            bkdgErr,
            smCount,
            switch,
            data=dataHist,
            dataCount=dataCount,
            histListSignal=sigHistList,
            smCountErr=math.sqrt(smCountErrSq),
            skipError=plot.plotSetting.skip_leg_err,
            leg_pos_list=plot.plotSetting.leg_pos,
            leg_text_size=plot.plotSetting.leg_text_size,
            skip_total=False,
            round_to_func=lambda x: str(int(round(x))),
        )

        leg.Draw()

        if smCount > 0.0 and dataCount > 0.:
            scaleFactor = dataCount * 1.0 / smCount
            scaleFactorErr = scaleFactor * math.sqrt(1 / dataCount +
                                                     smCountErrSq / smCount**2)
        else:
            pyPrint("Warning, smCount or dataCount is zero :" + plot.key)
            scaleFactor = 0.0
            scaleFactorErr = 0.0

        n1 = ROOT.TLatex()
        n1.SetNDC()
        n1.SetTextFont(42)
        n1.SetTextSize(0.05)
        if not self.skipSF:
            n1.DrawLatex(
                0.11, 0.92,
                "Data/MC = %.2f #pm %.2f" % (scaleFactor, scaleFactorErr))
        if plot.plotSetting.cms_lumi != None:
            plot.plotSetting.cms_lumi(upperPad,
                                      plot.plotSetting.cms_lumi_number, 0)

        ROOT.gStyle.SetErrorX(0)
        #dataHist.DrawCopy('sameE')
        dataGraph.Draw('sameP')
        #bkdgErr.DrawCopy("same")

        if plot.plotSetting.custom_latex_list:
            for latex_setting in plot.plotSetting.custom_latex_list:
                latex_setting.latex = ROOT.TLatex()
                latex_setting.latex.SetTextSize(latex_setting.text_size)
                latex_setting.latex.DrawLatex(latex_setting.x_pos,
                                              latex_setting.y_pos,
                                              latex_setting.text)

        ROOT.gPad.RedrawAxis()
        ROOT.gPad.RedrawAxis("G")

        c.SaveAs(outputDir + plot.key + ".png")
        c.SaveAs(outputDir + plot.key + ".pdf")

        upperPad.SetLogy(1)
        stack.SetMaximum(maximum * plot.plotSetting.log_max_factor)
        stack.SetMinimum(plot.plotSetting.log_min)
        stack.Draw('hist')
        for hist, sample, sigCount in sigHistList:
            hist.Draw('samehist')
        dataHist.Draw("sameE")
        bkdgErr.Draw("sameE2")
        leg.Draw('same')
        n1.DrawLatex(0.11, 0.92,
                     "Data/MC = %.2f #pm %.2f" % (scaleFactor, scaleFactorErr))
        ROOT.gPad.RedrawAxis()
        ROOT.gPad.RedrawAxis("G")

        c.SaveAs(outputDir + plot.key + "_log.png")
        c.SaveAs(outputDir + plot.key + "_log.pdf")
# ________________________________________________________________________________ ||
#out_path    = "ZPlusX/Systematic/2019-05-03_Run2016/"
#out_path    = "ZPlusX/Systematic/2019-05-03_Run2017/"
out_path = "ZPlusX/Systematic/2019-05-03_Run2018/"

User = os.environ['USER']
inputPath = system.getStoragePath(
) + "/" + User + "/Higgs/DarkZ/" + out_path + "/ZPlusX/DataMCDistribution.root"
histNames = [
    "Z2_4e_mass",
    "Z2_2mu2e_mass",
    "Z2_4mu_mass",
    "Z2_2e2mu_mass",
]

pyPrint("Input path: " + inputPath)

# ________________________________________________________________________________ ||
f = ROOT.TFile.Open(inputPath)
for histName in histNames:
    pyPrint("-" * 20)
    pyPrint(histName)
    h_nominal = f.Get(histName)
    integral_nominal, error_nominal = getIntegralAndError(h_nominal)
    pyPrint("Nominal: " + str(integral_nominal) + " +/- " + str(error_nominal))

    h_UniIso = f.Get(histName + "_UniIso")
    integral_UniIso, error_UniIso = getIntegralAndError(h_UniIso)
    pyPrint("Nominal: " + str(integral_UniIso) + " +/- " + str(error_UniIso))

    h_AsymIso = f.Get(histName + "_AsymIso")