Esempio n. 1
0
def normal(varName, kind = 'folded'):
    #open the signal histogram in root of tw+ttbar powheg
    if kind=='folded':         
        tmptfile = r.TFile.Open('temp/{var}/ClosureTest_fold_DR_unif_{var}.root'.format(var = varName))        
        tmptfile1 = r.TFile.Open('temp/{var}/ClosureTest_fold_DS_unif_{var}.root'.format(var = varName))
    elif kind=='unfolded': 
        tmptfile = r.TFile.Open('temp/{var}/ClosureTest_DR_unif_{var}.root'.format(var = varName))       
        tmptfile1 = r.TFile.Open('temp/{var}/ClosureTest_DS_unif_{var}.root'.format(var = varName))
    else : 
        print 'The kind of histogram chosen is not valid'
        #continue
    true = copy.deepcopy(tmptfile.Get('tW'))
    true.SetLineWidth(3)
    true.SetLineColor(bp.colorMap[3]) 
    
    #normalisation
    MAX=true.GetMaximum()  
    T=true.Integral()
    #true.Scale(1/MAX)
    true.Scale(1/T)
     
    #Saving the plot
    #ttbar normal
    ttbar = copy.deepcopy(tmptfile.Get('t#bar{t}'))
    Tttbar=ttbar.Integral()
    ttbar.SetLineWidth(3)
    ttbar.SetLineColor(bp.colorMap[4])
    #ttbar.Scale(1/MAX)
    ttbar.Scale(1/Tttbar)
    #set legend for the histogram
    ttbar.GetXaxis().SetTitle('m_{b\\ell}^{minimax} (GeV)')
    ttbar.GetYaxis().SetTitle('u.a.')
    
    #tW normal
    tW = copy.deepcopy(tmptfile1.Get('tW'))
    tW.SetLineWidth(3)
    tW.SetLineColor(bp.colorMap[2])
    TtW=tW.Integral()
    #tW.Scale(1/MAX)
    tW.Scale(1/TtW)
    
    
    if kind=='folded' :     plot = bp.beautifulUnfoldingPlots('{var}_norm_fold_'.format(var = varName))
    elif kind=='unfolded':  plot = bp.beautifulUnfoldingPlots('{var}_norm_'.format(var = varName))
    
    
    plot.doFit          = False
    plot.plotspath      = "results/discriminante/"
    legloc = "TR"
    
    
    plot.addHisto(ttbar, 'L,same',     't#bar{t} Powheg',   'L', 'mc')
    plot.addHisto(true, 'L,same',     'tW DR',   'L', 'mc')
    plot.addHisto(tW, 'L,same',     'tW DS',   'L', 'mc')
    
    
    plot.saveCanvas(legloc)    
    tmptfile.Close()
Esempio n. 2
0
    def doScanPlots(self):
        # Plot the scan variable and the maximum
        t = r.Double(0)
        x = r.Double(0)
        y = r.Double(0)
        if not hasattr(self, 'logTauX'):
            self.doLCurveScan()

        # First: L-curve plot
        plot = bp.beautifulUnfoldingPlots('{var}_asimov_LCurve'.format(
            var=self.var) if (
                self.wearedoingasimov) else '{var}_LCurve'.format(
                    var=self.var))
        plot.isLCurve = True
        plot.doPreliminary = vl.doPre
        plot.plotspath = self.plotspath
        if not hasattr(self, 'scanRes'):
            self.logTauX.GetKnot(self.themax, t, x)
            self.logTauY.GetKnot(self.themax, t, y)

            self.lCurve.SetLineWidth(2)
            self.lCurve.SetLineColor(r.kBlue)
            r.TGaxis.SetMaxDigits(3)
            self.lCurve.GetXaxis().SetNoExponent(True)
            self.lCurve.GetYaxis().SetNoExponent(True)
            self.lCurve.GetXaxis().SetTitle('log(#chi^{2}_{R})')
            self.lCurve.GetYaxis().SetTitle('log(#chi^{2}_{reg.})')
            plot.addHisto(self.lCurve, 'ALnomin', '', 0)
        else:
            plot.addHisto(self.scanRes, 'ALnomin', 'L curve', 0)
        grph = r.TGraph(
            1, array.array('d', [r.Double(self.tunfolder.GetLcurveX())]),
            array.array('d', [r.Double(self.tunfolder.GetLcurveY())]))
        grph.SetMarkerColor(r.kRed)
        grph.SetMarkerSize(2)
        grph.SetMarkerStyle(29)
        #grph.Draw("P")
        plot.addTLatex(0.75, 0.9,
                       "#tau = {taupar}".format(taupar=round(self.tau, 10)))
        #plot.addTLatex(0.75, 0.9, "#tau = {taupar}".format(taupar = self.tau))
        plot.saveCanvas('TR', leg=False)
        del plot, grph

        # Second: L-curve curvature plot
        plot = bp.beautifulUnfoldingPlots('{var}_asimov_LogTauCurv'.format(
            var=self.var) if (
                self.wearedoingasimov) else '{var}_LogTauCurv'.format(
                    var=self.var))
        plot.plotspath = self.plotspath
        plot.doPreliminary = vl.doPre
        plot.initCanvasAndAll()
        #plot.addHisto(self.lCurve, 'AL','',0)
        plot.canvas.cd()
        self.logTauCurv.Draw("AL")
        plot.saveCanvas('TR')
        del plot
Esempio n. 3
0
    def doRegularizationComparison(self):
        self.prepareNominalHelper()
        self.doLCurveScan()
        regularized = self.helpers[''].tunfolder.GetOutput('forPlot_regu')
        self.helpers[''].tunfolder.DoUnfold(0.)
        unregularized = self.helpers[''].tunfolder.GetOutput('forPlot_nuregu')

        regularized.SetLineWidth(2)
        unregularized.SetLineWidth(2)
        regularized.SetLineColor(bp.colorMap[0])
        unregularized.SetLineColor(bp.colorMap[1])

        for bin in range(1, regularized.GetNbinsX() + 1):
            regularized.SetBinContent(
                bin,
                regularized.GetBinContent(bin) /
                unregularized.GetBinContent(bin))

        plot = bp.beautifulUnfoldingPlots(self.var + '_regcomp')
        #        regularized.Draw()
        regularized.GetXaxis().SetTitle(vl.varList[self.var]['xaxis'])
        regularized.GetYaxis().SetTitle('Reg./Unreg.')
        #regularized.GetYaxis().SetRangeUser(0, 1.1*regularized.GetMaximum())
        plot.addHisto(regularized, 'hist', 'Regularized', 'L')
        #        plot.addHisto(unregularized,'hist,same','UnRegularized','L')
        plot.plotspath = 'results/'
        plot.saveCanvas('BR', '', False)
Esempio n. 4
0
 def doAreaConstraintComparison(self):
     if self.doRegularisation and self.taulist[''] == 0: self.doLCurveScan()             # Different taus for all the response matrices not implemented.
     tauval = 0
     if self.doRegularisation: tauval = self.taulist['']             # Different taus for all the response matrices not implemented.
     self.prepareNominalHelper()
     self.helpers[''].tunfolder.SetConstraint(r.TUnfold.kEConstraintArea)
     self.helpers[''].tunfolder.DoUnfold(tauval)
     withareaconst    = self.helpers[''].tunfolder.GetOutput('forPlot_area')
     self.helpers[''].tunfolder.SetConstraint(r.TUnfold.kEConstraintNone)
     self.helpers[''].tunfolder.DoUnfold(tauval)
     withoutareaconst = self.helpers[''].tunfolder.GetOutput('forPlot_noarea')
     self.helpers[''].tunfolder.SetConstraint(r.TUnfold.kEConstraintArea)
     
     withareaconst   .SetLineWidth(2)
     withoutareaconst.SetLineWidth(2)
     withareaconst   .SetLineColor(bp.colorMap[0])
     withoutareaconst.SetLineColor(bp.colorMap[1])
     
     for bin in range(1, withareaconst.GetNbinsX()+1):
         if withoutareaconst.GetBinContent(bin) != 0: withareaconst.SetBinContent(bin, withareaconst.GetBinContent(bin)/withoutareaconst.GetBinContent(bin))
         else:                                        withoutareaconst.SetBinContent(bin, 0)
     
     plot = bp.beautifulUnfoldingPlots(self.var + '_asimov_areacomp' if (self.wearedoingasimov) else self.var + '_areacomp')
     withareaconst.GetXaxis().SetTitle(vl.varList[self.var]['xaxis'])
     withareaconst.GetYaxis().SetTitle('Area/No area')
     plot.addHisto(withareaconst  ,'hist'     ,'areacomp'  ,'L')
     plot.plotspath = 'results/'
     plot.saveCanvas('BR', '', False)
Esempio n. 5
0
    def doNominalPlot(self, tau=None):
        if tau:
            self.helpers[''].tunfolder.DoUnfold(tau)
        data = self.helpers[''].tunfolder.GetOutput('forPlot')

        print 'Unfolded distribution integral', data.Integral()
        plot = beautifulUnfoldingPlots.beautifulUnfoldingPlots(self.var)
        data.SetMarkerStyle(r.kFullCircle)
        data.GetXaxis().SetNdivisions(505, True)
        plot.plotspath = self.plotspath

        if self.doSanityCheck:
            #tmptfile = r.TFile.Open('/nfs/fanae/user/sscruz/TW_differential/AnalysisPAF/plotter/./Datacards/closuretest_TGenLeadingJet_TGen{var}.root'.format(var=self.var))
            if not os.path.isfile(
                    'temp/ClosureTest_{var}.root'.format(var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information does not exist'
                )
            tmptfile = r.TFile.Open(
                'temp/ClosureTest_{var}.root'.format(var=self.var))
            tru = copy.deepcopy(tmptfile.Get('tW'))
            tru.SetLineWidth(2)
            tru.SetLineColor(beautifulUnfoldingPlots.colorMap[0])
            plot.addHisto(tru, 'L', 'Truth', 'L')
            plot.addHisto(data, 'P,E,same', 'Pseudodata', 'P')
            plot.saveCanvas('TR')
            tmptfile.Close()
        else:
            plot.addHisto(data, 'P,E', 'Data', 'P')
            plot.saveCanvas('TR')
Esempio n. 6
0
    def doRegularizationComparison(self):
        self.prepareNominalHelper()
        if self.taulist[''] == 0:
            self.doLCurveScan(
            )  # Different taus for all the response matrices not implemented.
        self.helpers[''].tunfolder.DoUnfold(self.taulist[''])
        regularized = self.helpers[''].tunfolder.GetOutput('forPlot_regu')
        self.helpers[''].tunfolder.DoUnfold(0)
        unregularized = self.helpers[''].tunfolder.GetOutput('forPlot_nuregu')

        regularized.SetLineWidth(2)
        unregularized.SetLineWidth(2)
        regularized.SetLineColor(bp.colorMap[0])
        unregularized.SetLineColor(bp.colorMap[1])

        for bin in range(1, regularized.GetNbinsX() + 1):
            regularized.SetBinContent(
                bin,
                regularized.GetBinContent(bin) /
                unregularized.GetBinContent(bin))

        plot = bp.beautifulUnfoldingPlots(self.var + '_asimov_regcomp' if (
            self.wearedoingasimov) else self.var + '_regcomp')
        #        regularized.Draw()
        regularized.GetXaxis().SetTitle(vl.varList[self.var]['xaxis'])
        regularized.GetYaxis().SetTitle('Reg./Unreg.')
        #regularized.GetYaxis().SetRangeUser(0, 1.1*regularized.GetMaximum())
        plot.addHisto(regularized, 'hist', 'regcomp', 'L')
        #        plot.addHisto(unregularized,'hist,same','UnRegularized','L')
        plot.plotspath = 'results/'
        plot.doPreliminary = vl.doPre
        plot.saveCanvas('BR', '', False)
Esempio n. 7
0
    def doNominalPlot(self, tau=None):
        if tau:
            self.helpers[''].tunfolder.DoUnfold(tau)
        data = self.helpers[''].tunfolder.GetOutput('forPlot')

        print 'Unfolded distribution integral', data.Integral()
        plot = bp.beautifulUnfoldingPlots(self.var)
        data.SetMarkerStyle(r.kFullCircle)
        data.GetXaxis().SetNdivisions(510, True)
        plot.plotspath = self.plotspath
        plot.doPreliminary = vl.doPre
        plot.doSupplementary = False

        if self.doSanityCheck:
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information does not exist'
                )
            tmptfile = r.TFile.Open(
                'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var))
            tru = copy.deepcopy(tmptfile.Get('tW'))
            tru.SetLineWidth(2)
            tru.SetLineColor(bp.colorMap[0])
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                        var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information from an aMCatNLO sample does not exist'
                )
            tmptfile2 = r.TFile.Open(
                'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                    var=self.var))
            aMCatNLO = copy.deepcopy(tmptfile2.Get('tW'))
            aMCatNLO.SetLineWidth(2)
            aMCatNLO.SetLineColor(bp.colorMap[1])
            aMCatNLO.SetLineStyle(2)
            for bin in range(1, tru.GetNbinsX()):
                tru.SetBinError(bin, 0.)
                aMCatNLO.SetBinError(bin, 0.)
            plot.addHisto(tru, 'L,same', 'tW Powheg DR + Pythia8', 'L', 'mc')
            plot.addHisto(aMCatNLO, 'L,same', 'tW aMC@NLO DR + Pythia8', 'L',
                          'mc')
            plot.addHisto(
                data,
                'P,E,same{s}'.format(s=",X0" if "equalbinsunf" in vl.varList[
                    self.var.replace('_folded', '').replace('_asimov', '').
                    replace("_fiducial", "").replace('norm', '')] else ""),
                vl.labellegend, 'PE')
            plot.saveCanvas('TR')
            tmptfile.Close()
            tmptfile2.Close()
        else:
            plot.addHisto(
                data, 'P,E{s}'.format(s=",X0" if "equalbinsunf" in vl.varList[
                    self.var.replace('_folded', '').replace('_asimov', '').
                    replace("_fiducial", "").replace('norm', '')] else ""),
                vl.labellegend, 'PE')
            plot.saveCanvas('TR')
Esempio n. 8
0
    def doScanPlots(self):
        # Plot the scan variable and the maximum
        t = r.Double(0)
        x = r.Double(0)
        y = r.Double(0)
        if not hasattr(self, 'logTauX'): self.doLCurveScan()

        # First: L-curve plot
        plot = bp.beautifulUnfoldingPlots('{var}_LCurve'.format(var=self.var))
        plot.isLCurve = True
        plot.plotspath = self.plotspath
        if not hasattr(self, 'scanRes'):
            self.logTauX.GetKnot(self.themax, t, x)
            self.logTauY.GetKnot(self.themax, t, y)

            self.lCurve.SetLineWidth(2)
            self.lCurve.SetLineColor(r.kBlue)
            r.TGaxis.SetMaxDigits(3)
            self.lCurve.GetXaxis().SetNoExponent(True)
            self.lCurve.GetYaxis().SetNoExponent(True)
            self.lCurve.GetXaxis().SetTitle('log(#chi^{2}_{R})')
            self.lCurve.GetYaxis().SetTitle('log(#chi^{2}_{reg.})')
            plot.addHisto(self.lCurve, 'ALnomin', '', 0)

        else:
            plot.addHisto(self.scanRes, 'ALnomin', 'L curve', 0)

        plot.addTLatex(
            0.75, 0.9,
            "#tau = {taupar}".format(taupar=round(self.tunfolder.GetTau(), 5)))
        plot.saveCanvas('TR', leg=False)
        del plot

        # Second: L-curve curvature plot
        plot = bp.beautifulUnfoldingPlots(
            '{var}_LogTauCurv'.format(var=self.var))
        plot.plotspath = self.plotspath
        plot.initCanvasAndAll()
        #plot.addHisto(self.lCurve, 'AL','',0)
        plot.canvas.cd()
        self.logTauCurv.Draw("AL")
        plot.saveCanvas('TR')
        del plot
Esempio n. 9
0
    def doNominalPlot(self, tau=None):
        if tau:
            self.helpers[''].tunfolder.DoUnfold(tau)
        data = self.helpers[''].tunfolder.GetOutput('forPlot')

        print 'Unfolded distribution integral', data.Integral()
        plot = bp.beautifulUnfoldingPlots(self.var)
        data.SetMarkerStyle(r.kFullCircle)
        data.GetXaxis().SetNdivisions(505, True)
        plot.plotspath = self.plotspath

        if self.doSanityCheck:
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information does not exist'
                )
            tmptfile = r.TFile.Open(
                'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var))
            tru = copy.deepcopy(tmptfile.Get('tW'))
            tru.SetLineWidth(2)
            tru.SetLineColor(bp.colorMap[0])
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                        var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information from an aMCatNLO sample does not exist'
                )
            tmptfile2 = r.TFile.Open(
                'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                    var=self.var))
            aMCatNLO = copy.deepcopy(tmptfile2.Get('tW'))
            aMCatNLO.SetLineWidth(2)
            aMCatNLO.SetLineColor(bp.colorMap[1])
            for bin in range(1, tru.GetNbinsX()):
                tru.SetBinError(bin, 0.)
                aMCatNLO.SetBinError(bin, 0.)
            plot.addHisto(tru, 'L', 'tW Powheg', 'L')
            plot.addHisto(aMCatNLO, 'L,same', 'tW aMCatNLO', 'L')
            plot.addHisto(data, 'P,E,same', vl.labellegend, 'P')
            plot.saveCanvas('TR')
            tmptfile.Close()
            tmptfile2.Close()

        else:
            plot.addHisto(data, 'P,E', vl.labellegend, 'P')
            plot.saveCanvas('TR')
Esempio n. 10
0
    def doRegularizationComparison(self):
        self.prepareNominalHelper()
        self.doLCurveScan()
        regularized = self.helpers[''].tunfolder.GetOutput('forPlot_regu')
        self.helpers[''].tunfolder.DoUnfold(0.)
        unregularized = self.helpers[''].tunfolder.GetOutput('forPlot_nuregu')

        regularized.SetLineWidth(2)
        unregularized.SetLineWidth(2)
        regularized.SetLineColor(beautifulUnfoldingPlots.colorMap[0])
        unregularized.SetLineColor(beautifulUnfoldingPlots.colorMap[1])

        print regularized.GetBinContent(1), unregularized.GetBinContent(1)

        plot = beautifulUnfoldingPlots.beautifulUnfoldingPlots(self.var)
        regularized.Draw()
        regularized.GetYaxis().SetRangeUser(0, 1.1 * regularized.GetMaximum())
        plot.addHisto(regularized, 'hist', 'Regularized', 'L')
        plot.addHisto(unregularized, 'hist,same', 'UnRegularized', 'L')
        plot.saveCanvas('TR', 'comparison')
Esempio n. 11
0
    def doScanPlots(self):

        # Plot the scan variable and the maximum
        t = r.Double(0)
        x = r.Double(0)
        y = r.Double(0)

        plot = beautifulUnfoldingPlots.beautifulUnfoldingPlots('LCurve')
        if not hasattr(self, 'scanRes'):
            self.logTauX.GetKnot(self.themax, t, x)
            self.logTauY.GetKnot(self.themax, t, y)

            self.lCurve.SetLineWidth(2)
            self.lCurve.SetLineColor(r.kBlue)

            #self.lCurve.GetYaxis().SetDecimals(True)
            self.lCurve.GetYaxis().SetNdivisions(304, False)
            self.lCurve.GetXaxis().SetNdivisions(303, False)
            plot.addHisto(self.lCurve, 'AL', '', 0)

        else:
            plot.addHisto(self.scanRes, 'AL', '', 0)

        plot.saveCanvas('TR', '_%s' % self.var)
Esempio n. 12
0
def damelasmiovariacionesprofolded(variacion):
    for varname in variables:
        print "\n> Producing for variable", varname, "and variation", variacion, "for type folded"
        if "Fiducial" in varname: continue
        fullpath = pathtothings + varname + "_/" + "cutOutput_{var}.root".format(
            var=varname)
        cutout = r.TFile.Open(fullpath, "READ")

        textSize = 0.035 if "unc" in varname else 0.0435
        if "unc" in varname: legWidth = 0.13
        else: legWidth = 0.2
        height = (.18 + textSize * max(2 - 3, 0))

        corner = "TR"

        if isinstance(corner, str):
            if corner == "TR":
                (x1, y1, x2, y2) = (.72 - legWidth, .9 - height, .735, .9)
            elif corner == "TC":
                (x1, y1, x2, y2) = (.5, .9 - height, .55 + legWidth, .9)
            elif corner == "TL":
                (x1, y1, x2, y2) = (.18, .78 - height, .18 + legWidth, .78)
            elif corner == "MR":
                (x1, y1, x2, y2) = (.85 - legWidth, .3 + height, .90, .3)
            elif corner == "MC":
                (x1, y1, x2, y2) = (.5, .3 + height, .5 + legWidth, .3)
            elif corner == "ML":
                (x1, y1, x2, y2) = (.18, .3 + height, .18 + legWidth, .3)
            elif corner == "BR":
                (x1, y1, x2, y2) = (.85 - legWidth, .1 + height, .90, .1)
            elif corner == "BC":
                (x1, y1, x2, y2) = (.5, .1 + height, .5 + legWidth, .1)
            elif corner == "BL":
                (x1, y1, x2, y2) = (.18, .1 + height, .18 + legWidth, .1)

        histolist = []

        plot = bp.beautifulUnfoldingPlots((
            varname + "_" + ("norm" if (
                variacion == "ttbar" or variacion == "VVttbarV" or variacion ==
                "DY" or variacion == "Non-WorZ") else "") + variacion +
            "_values_folded"
        ) if "Colour" not in variacion else "ColourReconnection_values_folded")
        plot.doRatio = False
        plot.doFit = False
        plot.doPreliminary = False
        plot.noCMS = True
        plot.doSupplementary = False
        plot.plotspath = outpath + "/{var}/folded_distributions/".format(
            var=varname)

        legloc = ""

        if "legpos_unf" in vl.varList[varname]:
            legloc = vl.varList[varname]["legpos_unf"]
        else:
            legloc = "TR"

        legloc = "TR"

        found = False
        nominal = r.TH1F()
        for key in cutout.GetListOfKeys():
            if key.GetName() == "data_{vr}Up".format(
                    vr=variacion) and "Colour" not in variacion:
                histolist.append(r.TH1F())
                histolist[-1] = copy.deepcopy(
                    cutout.Get("data_{vr}Up".format(vr=variacion)).Clone())
                found = True
                #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                #histolist[-1].GetYaxis().SetRangeUser(0, maximafold[varname])
                histolist[-1].SetMaximum(maximafold[varname])
                histolist[-1].SetLineColor(r.kBlue)
                histolist[-1].SetFillStyle(0)
                histolist[-1].SetMarkerSize(0)
                plot.addHisto(histolist[-1], 'P,E,same', variacion + "_Up",
                              'PEL', variacion + "_Up")
            elif key.GetName() == "data_{vr}Down".format(
                    vr=variacion) and "Colour" not in variacion:
                histolist.append(r.TH1F())
                histolist[-1] = copy.deepcopy(
                    cutout.Get("data_{vr}Down".format(vr=variacion)).Clone())
                found = True
                #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                #histolist[-1].GetYaxis().SetRangeUser(0, maximafold[varname])
                histolist[-1].SetMaximum(maximafold[varname])
                histolist[-1].SetLineColor(r.kRed)
                histolist[-1].SetFillStyle(0)
                histolist[-1].SetMarkerSize(0)
                plot.addHisto(histolist[-1], 'P,E,same', variacion + "_Down",
                              'PEL', variacion + "_Down")
            elif (key.GetName() == "data_{vr}".format(
                    vr=colour_syst[0].replace("Colour", ""))
                  ) and "Colour" in variacion:  ### Colour reconnection
                histolist.append(r.TH1F())
                histolist[-1] = copy.deepcopy(
                    cutout.Get("data_{vr}".format(
                        vr=colour_syst[0].replace("Colour", ""))).Clone())
                found = True
                #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                #histolist[-1].GetYaxis().SetRangeUser(0, maximafold[varname])
                histolist[-1].SetMaximum(maximafold[varname])
                histolist[-1].SetLineColor(colour_colours[0])
                histolist[-1].SetFillStyle(0)
                histolist[-1].SetMarkerSize(0)
                plot.addHisto(histolist[-1], 'P,E,same',
                              colour_syst[0].replace("Colour", ""), 'PEL',
                              colour_syst[0].replace("Colour", ""))
            elif (key.GetName() == "data_{vr}".format(
                    vr=colour_syst[1].replace("Colour", ""))
                  ) and "Colour" in variacion:
                histolist.append(r.TH1F())
                histolist[-1] = copy.deepcopy(
                    cutout.Get("data_{vr}".format(
                        vr=colour_syst[1].replace("Colour", ""))).Clone())
                found = True
                #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                #histolist[-1].GetYaxis().SetRangeUser(0, maximafold[varname])
                histolist[-1].SetMaximum(maximafold[varname])
                histolist[-1].SetLineColor(colour_colours[1])
                histolist[-1].SetFillStyle(0)
                histolist[-1].SetMarkerSize(0)
                plot.addHisto(histolist[-1], 'P,E,same',
                              colour_syst[1].replace("Colour", ""), 'PEL',
                              colour_syst[1].replace("Colour", ""))
            elif (key.GetName() == "data_{vr}".format(
                    vr=colour_syst[2].replace("Colour", ""))
                  ) and "Colour" in variacion:
                histolist.append(r.TH1F())
                histolist[-1] = copy.deepcopy(
                    cutout.Get("data_{vr}".format(
                        vr=colour_syst[2].replace("Colour", ""))).Clone())
                found = True
                #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                #histolist[-1].GetYaxis().SetRangeUser(0, maximafold[varname])
                histolist[-1].SetMaximum(maximafold[varname])
                histolist[-1].SetLineColor(colour_colours[2])
                histolist[-1].SetFillStyle(0)
                histolist[-1].SetMarkerSize(0)
                plot.addHisto(histolist[-1], 'P,E,same',
                              colour_syst[2].replace("Colour", ""), 'PEL',
                              colour_syst[2].replace("Colour", ""))
            elif (key.GetName() == "data_{vr}".format(
                    vr=colour_syst[3].replace("Colour", ""))
                  ) and "Colour" in variacion:
                histolist.append(r.TH1F())
                histolist[-1] = copy.deepcopy(
                    cutout.Get("data_{vr}".format(
                        vr=colour_syst[3].replace("Colour", ""))).Clone())
                found = True
                #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                #histolist[-1].GetYaxis().SetRangeUser(0, maximafold[varname])
                histolist[-1].SetMaximum(maximafold[varname])
                histolist[-1].SetLineColor(colour_colours[3])
                histolist[-1].SetFillStyle(0)
                histolist[-1].SetMarkerSize(0)
                plot.addHisto(histolist[-1], 'P,E,same',
                              colour_syst[3].replace("Colour", "") + "_",
                              'PEL', colour_syst[3].replace("Colour", ""))
            elif key.GetName() == "data_":
                nominal = copy.deepcopy(cutout.Get("data_").Clone("nominal"))
                #nominal.GetYaxis().SetRangeUser(0, maximafold[varname])
                nominal.SetMaximum(maximafold[varname])
                nominal.SetLineColor(r.kBlack)
                nominal.SetFillStyle(0)
                nominal.SetMarkerSize(0)
                plot.addHisto(nominal, 'P,E,same', "Nominal", 'PEL', "Nominal")

        r.gPad.RedrawAxis()
        plot.saveCanvas(legloc)
        del plot

        legloc = "TC"

        histnom = copy.deepcopy(cutout.Get("data_").Clone("histnom"))
        unctotUp = copy.deepcopy(cutout.Get("nom0").Clone("unctotUp"))
        unctotDn = copy.deepcopy(cutout.Get("nom1").Clone("unctotDn"))

        unctotmax = copy.deepcopy(unctotUp.Clone("unctotmax"))
        for bin in range(1, unctotmax.GetNbinsX() + 1):
            unctotmax.SetBinError(
                bin, max(unctotUp.GetBinError(bin), unctotDn.GetBinError(bin)))
            #print "unctotmax bin", bin, ":", unctotmax.GetBinError(bin)

        unctotmaxrel = copy.deepcopy(cutout.Get("hincmax").Clone())
        uncsysrel = copy.deepcopy(cutout.Get("hincsyst").Clone())

        plotrel = bp.beautifulUnfoldingPlots((
            varname + "_" + ("norm" if (
                variacion == "ttbar" or variacion == "VVttbarV" or variacion ==
                "DY" or variacion == "Non-WorZ") else "") + variacion +
            "_uncrel_folded"
        ) if "Colour" not in variacion else "ColourReconnection_uncrel_folded")
        plotrel.doRatio = False
        plotrel.doFit = False
        plotrel.doPreliminary = False
        plotrel.noCMS = True
        plotrel.doSupplementary = False
        plotrel.plotspath = outpath + "/{var}/folded_distributions/".format(
            var=varname)

        unchistolist = []

        unctotmaxrel.SetFillStyle(0)
        uncsysrel.SetFillStyle(0)
        plotrel.addHisto(unctotmaxrel, 'H,same', "Total", "L")
        plotrel.addHisto(uncsysrel, 'H,same', "Total systematic", "L")
        for minivar in histolist:
            unchistolist.append(
                copy.deepcopy(
                    unctotmaxrel.Clone(minivar.GetName().replace("data_", "") +
                                       "_uncrel")))
            for bin in range(1, unchistolist[-1].GetNbinsX() + 1):
                unchistolist[-1].SetBinContent(
                    bin,
                    (abs(
                        minivar.GetBinContent(bin) -
                        histnom.GetBinContent(bin)) if
                     (minivar.GetBinContent(bin) > histnom.GetBinContent(bin))
                     or
                     (minivar.GetBinContent(bin) < histnom.GetBinContent(bin)
                      and minivar.GetBinContent(bin) >= 0) else
                     histnom.GetBinContent(bin)) / histnom.GetBinContent(bin))
                unchistolist[-1].SetBinError(bin, 0)
                unchistolist[-1].SetLineWidth(1)
                unchistolist[-1].SetMarkerSize(0)
                if "Down" in minivar.GetName():
                    unchistolist[-1].SetLineColor(r.kRed)
                elif "Colour" not in variacion:
                    unchistolist[-1].SetLineColor(r.kBlue)
                elif "Colour" in variacion:
                    unchistolist[-1].SetLineColor(minivar.GetLineColor())
                #print "histo", minivar.GetName(), "bin:", bin, ":", unchistolist[-1].GetBinContent(bin)
            unchistolist[-1].SetFillStyle(0)
            plotrel.addHisto(unchistolist[-1], 'H,same',
                             minivar.GetName().replace("data_", ""), 'L')

        if len(unchistolist) == 2:
            unchistolist.append(
                copy.deepcopy(unchistolist[-1].Clone("Average")))
            for bin in range(1, unctotmaxrel.GetNbinsX() + 1):
                unchistolist[-1].SetBinContent(
                    bin, (unchistolist[-1].GetBinContent(bin) +
                          unchistolist[-3].GetBinContent(bin)) / 2)
            unchistolist[-1].SetLineColor(r.kGreen)
            plotrel.addHisto(unchistolist[-1], 'H,same', "Average", 'L')

        r.gPad.RedrawAxis()
        plotrel.saveCanvas(legloc)
        del plotrel

        plotabs = bp.beautifulUnfoldingPlots((
            varname + "_" + ("norm" if (
                variacion == "ttbar" or variacion == "VVttbarV" or variacion ==
                "DY" or variacion == "Non-WorZ") else "") + variacion +
            "_uncabs_folded"
        ) if "Colour" not in variacion else "ColourReconnection_uncabs_folded")
        plotabs.doRatio = False
        plotabs.doFit = False
        plotabs.doPreliminary = False
        plotabs.noCMS = True
        plotabs.doSupplementary = False
        plotabs.plotspath = outpath + "/{var}/folded_distributions/".format(
            var=varname)

        unctotmaxabs = copy.deepcopy(unctotmaxrel.Clone("unctotmaxabs"))
        uncsysabs = copy.deepcopy(uncsysrel.Clone("uncsysabs"))

        absmaximum = 0

        for bin in range(1, unctotmaxabs.GetNbinsX() + 1):
            unctotmaxabs.SetBinContent(
                bin,
                unctotmaxabs.GetBinContent(bin) * histnom.GetBinContent(bin))
            uncsysabs.SetBinContent(
                bin,
                uncsysabs.GetBinContent(bin) * histnom.GetBinContent(bin))
            if unctotmaxabs.GetBinContent(bin) > absmaximum:
                absmaximum = unctotmaxabs.GetBinContent(bin)

        unctotmaxabs.SetMaximum(absmaximum * 1.1)

        unctotmaxabs.GetYaxis().SetTitle("Absolute uncertainty (pb/GeV)")

        unctotmaxabs.SetFillStyle(0)
        uncsysabs.SetFillStyle(0)
        plotabs.addHisto(unctotmaxabs, 'H,same', "Total", "L")
        plotabs.addHisto(uncsysabs, 'H,same', "Total systematic", "L")

        unchistoabslist = []

        for minivar in unchistolist:
            unchistoabslist.append(minivar.Clone(minivar.GetName() + "_abs"))
            for bin in range(1, unctotmaxabs.GetNbinsX() + 1):
                unchistoabslist[-1].SetBinContent(
                    bin, unchistoabslist[-1].GetBinContent(bin) *
                    histnom.GetBinContent(bin))
                #print "histo", minivar.GetName(), "bin:", bin, ":", unchistoabslist[-1].GetBinContent(bin)
                #print "nominal bin:", bin, ":", histnom.GetBinContent(bin)
            unchistoabslist[-1].SetFillStyle(0)
            plotabs.addHisto(unchistoabslist[-1], 'H,same',
                             minivar.GetName().replace(varname + "_", ""), 'L')

        if len(unchistoabslist) == 2:
            unchistoabslist.append(
                copy.deepcopy(unchistoabslist[-1].Clone("Average")))
            for bin in range(1, unctotmaxabs.GetNbinsX() + 1):
                unchistoabslist[-1].SetBinContent(
                    bin, (unchistoabslist[-1].GetBinContent(bin) +
                          unchistoabslist[-3].GetBinContent(bin)) / 2)
            unchistoabslist[-1].SetLineColor(r.kGreen)
            plotabs.addHisto(unchistoabslist[-1], 'H,same', "Average", 'L')

        r.gPad.RedrawAxis()
        plotabs.saveCanvas(legloc)
        del plotabs
        cutout.Close()
    return
Esempio n. 13
0
    def doUnfoldingForAllNuis(self):
        allHistos = {}
        self.prepareAllHelpers()
        if self.doRegularisation and self.tau == 0:
            print '> Performing regularisation...'
            self.doLCurveScan()
        tauval = 0
        if self.doRegularisation: tauval = self.taulist['']

        if verbose: print '\n> Unfolding nominal distribution'
        self.helpers[''].tunfolder.DoUnfold(tauval)
        nominal = copy.deepcopy(self.helpers[''].tunfolder.GetOutput(self.var))

        if "Fiducial" in self.var:
            print "\n"
            #wr.warn("WARNING: you are calculating the fiducial cross section for the Asimov dataset. For unknown reasons, the obtention of the full covariance matrix after the unfolding gives an error. As a by-pass of this situation, the statistical uncertainty that the nominal results will carry will be ONLY the ones introduced as input, NOT the ones related with the response matrix nor the signal efficiency / fiducial region efficiency detection. TAKE THIS INTO ACCOUNT.")

            mat = self.helpers[''].response.GetBinContent(1, 1)
            incmat = self.helpers[''].response.GetBinError(1, 1)

            ns = self.helpers[''].unfInput.GetBinContent(1)
            incns = self.helpers[''].unfInput.GetBinError(1)

            nf = self.helpers[''].bkg.GetBinContent(1)
            incnf = self.helpers[''].bkg.GetBinError(1)

            valor = (ns - nf) / mat
            inc = incns / mat + incnf / mat + (ns - nf) / mat**2 * incmat

            print "ns", ns, "nf", nf, "mat", mat, "ns-nf", ns - nf

            print "valor nom.", valor / vl.Lumi / 1000, "inc. nom.", inc / vl.Lumi / 1000, "\n"
            #sys.exit()

            nominal.SetBinContent(1, valor)
            nominal.SetBinError(1, inc)
            covnom = r.TH2F("CovMat", "", 1,
                            vl.varList[self.var]['genbinning'][0],
                            vl.varList[self.var]['genbinning'][-1], 1,
                            vl.varList[self.var]['genbinning'][0],
                            vl.varList[self.var]['genbinning'][-1])
            covnom.SetBinContent(1, 1, inc)
        else:
            if verbose:
                print '> Obtaining covariance matrix of all the statistical components that take part here.'
            covnom = copy.deepcopy(
                self.helpers[''].tunfolder.GetEmatrixTotal("CovMat"))
            for bin in range(1, nominal.GetNbinsX() + 1):
                nominal.SetBinError(bin,
                                    math.sqrt(covnom.GetBinContent(bin, bin)))

        for nuis in self.sysList:
            if verbose:
                print '> Unfolding distribution of {sys} systematic'.format(
                    sys=nuis)
            self.helpers[nuis].tunfolder.DoUnfold(tauval)
            allHistos[nuis] = self.helpers[nuis].tunfolder.GetOutput(self.var +
                                                                     '_' +
                                                                     nuis)
            if "Fiducial" in self.var:
                mat = self.helpers[nuis].response.GetBinContent(1, 1)
                incmat = self.helpers[nuis].response.GetBinError(1, 1)

                ns = self.helpers[nuis].unfInput.GetBinContent(1)
                incns = self.helpers[nuis].unfInput.GetBinError(1)

                nf = self.helpers[nuis].bkg.GetBinContent(1)
                incnf = self.helpers[nuis].bkg.GetBinError(1)

                valor = (ns - nf) / mat
                inc = incns / mat + incnf / mat + (ns - nf) / mat**2 * incmat

                allHistos[nuis].SetBinContent(1, 1, valor)
                allHistos[nuis].SetBinError(1, 1, inc)

        scaleval = 1 / vl.Lumi / 1000 if vl.doxsec else 1
        nominal.Scale(scaleval)
        for key in allHistos:
            allHistos[key].Scale(scaleval)

        if not self.wearedoingasimov:
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information does not exist'
                )
            tmptfile = r.TFile.Open(
                'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var))
            #tmptfile2 = r.TFile.Open('temp/{var}_/ClosureTest_recobinning_{var}.root'.format(var = self.var))
            tru = copy.deepcopy(tmptfile.Get('tW'))
            #tru2 = copy.deepcopy(tmptfile2.Get('tW'))
            #tru2.Scale(vl.Lumi*1000)
            for bin in range(1, allHistos['asimov'].GetNbinsX() + 1):
                #print "\nasimov:", allHistos['asimov'].GetBinContent(bin)
                #print "verdad:", tru.GetBinContent(bin)
                allHistos['asimov'].SetBinError(
                    bin,
                    abs(allHistos['asimov'].GetBinContent(bin) -
                        tru.GetBinContent(bin)))
                #allHistos['asimov'].SetBinContent(bin, nominal.GetBinContent(bin))
                #print "errorin:", allHistos['asimov'].GetBinError(bin)
                #print "nominal:", nominal.GetBinContent(bin)
                #print "relativo", round(allHistos['asimov'].GetBinError(bin)/allHistos['asimov'].GetBinContent(bin)*100, 1)
                #print "ratio:", allHistos['asimov'].GetBinContent(bin)/tru.GetBinContent(bin)
            #for bin in range(1, tru2.GetNbinsX() + 1):
            #print "verdad2:", tru2.GetBinContent(bin)
            #tmptfile2.Close()
            tmptfile.Close()
            del tru

        if not self.wearedoingasimov:
            savetfile = r.TFile(
                "temp/{var}_/unfOutput_{var}.root".format(var=self.var),
                "recreate")
            nominal.Write()
            for key in allHistos:
                allHistos[key].Write()
            covnom.Scale(scaleval**2)
            covnom.Write()
            #covitwo = copy.deepcopy(self.helpers[''].tunfolder.GetEmatrixInput("CovMatInput"))
            #covitwo.Scale(scaleval**2)
            #covitwo.Write()
            #covithree = copy.deepcopy(self.helpers[''].tunfolder.GetEmatrixSysUncorr("CovMatResponse"))
            #covithree.Scale(scaleval**2)
            #covithree.Write()
            savetfile.Close()

        if not self.wearedoingasimov:
            nominal_withErrors = ep.propagateHistoAsym(nominal,
                                                       allHistos,
                                                       self.doColorEnvelope,
                                                       True,
                                                       doSym=vl.doSym)
        else:
            nominal_withErrors = ep.propagateHistoAsym(nominal,
                                                       allHistos,
                                                       self.doColorEnvelope,
                                                       doSym=vl.doSym)
        plot = bp.beautifulUnfoldingPlots(
            self.var + "_asimov" if self.wearedoingasimov else self.var)
        plot.doRatio = True
        plot.doFit = False
        plot.doPreliminary = vl.doPre
        plot.doSupplementary = False
        plot.plotspath = self.plotspath

        nominal.SetMarkerStyle(r.kFullCircle)
        nominal.SetLineColor(r.kBlack)
        nominal.SetMarkerSize(1)
        nominal_withErrors[0].SetFillColorAlpha(r.kBlue, 0.35)
        nominal_withErrors[0].SetLineColor(0)
        nominal_withErrors[0].SetFillStyle(1001)

        if "yaxisuplimitunf" in vl.varList[self.var]:
            plot.yaxisuplimit = vl.varList[self.var]["yaxisuplimitunf"]

        if not self.wearedoingasimov:
            savetfile2 = r.TFile(
                "temp/{var}_/unfOutput_{var}.root".format(var=self.var),
                "update")
            nom0 = copy.deepcopy(nominal_withErrors[0].Clone("nom0"))
            nom1 = copy.deepcopy(nominal_withErrors[1].Clone("nom1"))
            nom0.Write()
            nom1.Write()
            savetfile2.Close()
            del nom0, nom1

        #############################
        print "\nLOS RESULTAOS - {uno} - {dos}".format(
            uno="ASIMOV" if self.wearedoingasimov else "DATOS", dos=self.var)
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            print "Bin", bin, "(abs.): (", round(
                nominal.GetBinContent(bin), 4), "+", round(
                    nominal_withErrors[0].GetBinError(bin),
                    4), "-", round(nominal_withErrors[1].GetBinError(bin),
                                   4), ") pb"
            print "Bin", bin, "(rel.): (", round(
                nominal.GetBinContent(bin), 4), "+", round(
                    nominal_withErrors[0].GetBinError(bin) /
                    nominal.GetBinContent(bin) * 100, 4), "-", round(
                        nominal_withErrors[1].GetBinError(bin) /
                        nominal.GetBinContent(bin) * 100, 4), ") pb\n"
        print "\n"
        #############################

        if not self.wearedoingasimov and varName != "Fiducial":
            tex.saveLaTeXfromhisto(nominal,
                                   varName,
                                   path=vl.tablespath,
                                   errhisto=nominal_withErrors[0],
                                   ty="unfolded")

        if "legpos_unf" in vl.varList[self.var] and not self.wearedoingasimov:
            legloc = vl.varList[self.var]["legpos_unf"]
        elif "legpos_unfas" in vl.varList[self.var] and self.wearedoingasimov:
            legloc = vl.varList[self.var]["legpos_unfas"]
        else:
            legloc = "TR"

        if self.doSanityCheck:
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information does not exist.'
                )
            tmptfile = r.TFile.Open(
                'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var))
            tru = copy.deepcopy(tmptfile.Get('tW').Clone('tru'))
            tru.SetLineWidth(2)
            tru.SetLineColor(bp.colorMap[0])

            #print "tW DR", tru.GetBinContent(1)

            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                        var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information from an aMCatNLO sample does not exist.'
                )
            tmptfile2 = r.TFile.Open(
                'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                    var=self.var))
            aMCatNLO = copy.deepcopy(tmptfile2.Get('tW').Clone('aMCatNLO'))
            aMCatNLO.SetLineWidth(2)
            aMCatNLO.SetLineColor(r.kAzure)
            aMCatNLO.SetLineStyle(2)

            #print "tW aMCatNLO DR", aMCatNLO.GetBinContent(1)

            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_DS_{var}.root'.format(
                        var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information with the DS variation does not exist.'
                )
            tmptfile3 = r.TFile.Open(
                'temp/{var}_/ClosureTest_DS_{var}.root'.format(var=self.var))
            hDS = copy.deepcopy(tmptfile3.Get('tW').Clone('hDS'))
            hDS.SetLineWidth(2)
            hDS.SetLineColor(r.kGreen)

            #print "tW DS", hDS.GetBinContent(1)
            #sys.exit()
            plot.addHisto(nominal_withErrors, 'hist', 'Uncertainty', 'F',
                          'unc')
            plot.addHisto(tru, 'L,same', 'tW Powheg DR + Pythia8', 'L', 'mc')
            plot.addHisto(hDS, 'L,same', 'tW Powheg DS + Pythia8', 'L', 'mc')
            plot.addHisto(aMCatNLO, 'L,same', 'tW aMC@NLO DR + Pythia8', 'L',
                          'mc')

            if self.wearedoingasimov:
                plot.addHisto(
                    nominal, 'P,E,same{s}'.format(
                        s=",X0" if "equalbinsunf" in
                        vl.varList[self.var.replace('_folded', '').replace(
                            '_asimov', '').replace("_fiducial", "").
                                   replace('norm', '')] else ""), "Pseudodata",
                    'PE{s}'.format(s="L" if not "equalbinsunf" in vl.varList[
                        self.var.replace('_folded', '').replace('_asimov', '').
                        replace("_fiducial", "").replace('norm', '')] else ""),
                    'data')
            else:
                plot.addHisto(
                    nominal, 'P,E,same{s}'.format(
                        s=",X0" if "equalbinsunf" in
                        vl.varList[self.var.replace('_folded', '').replace(
                            '_asimov', '').replace("_fiducial", "").
                                   replace('norm', '')] else ""),
                    vl.labellegend,
                    'PE{s}'.format(s="L" if not "equalbinsunf" in vl.varList[
                        self.var.replace('_folded', '').replace('_asimov', '').
                        replace("_fiducial", "").replace('norm', '')] else ""),
                    'data')
            plot.saveCanvas(legloc)
            tmptfile.Close()
            tmptfile2.Close()
            tmptfile3.Close()
        else:
            plot.addHisto(nominal_withErrors, 'E2', 'Uncertainty', 'F')
            if self.wearedoingasimov:
                plot.addHisto(
                    nominal,
                    'P,same{s}'.format(s=",X0" if "equalbinsunf" in vl.varList[
                        self.var.replace('_folded', '').replace('_asimov', '').
                        replace("_fiducial", "").replace('norm', '')] else ""),
                    "Pseudodata",
                    'PE{s}'.format(s="L" if not "equalbinsunf" in vl.varList[
                        self.var.replace('_folded', '').replace('_asimov', '').
                        replace("_fiducial", "").replace('norm', '')] else ""),
                    'data')
            else:
                plot.addHisto(
                    nominal,
                    'P,same{s}'.format(s=",X0" if "equalbinsunf" in vl.varList[
                        self.var.replace('_folded', '').replace('_asimov', '').
                        replace("_fiducial", "").replace('norm', '')] else ""),
                    vl.labellegend,
                    'PE{s}'.format(s="L" if not "equalbinsunf" in vl.varList[
                        self.var.replace('_folded', '').replace('_asimov', '').
                        replace("_fiducial", "").replace('norm', '')] else ""),
                    'data')
            plot.saveCanvas(legloc)

        del plot
        plot2 = bp.beautifulUnfoldingPlots(
            self.var +
            'uncertainties_asimov' if self.wearedoingasimov else self.var +
            'uncertainties')
        plot2.doFit = False
        plot2.doPreliminary = vl.doPre
        plot2.doSupplementary = False
        if not self.wearedoingasimov:
            uncListorig = ep.getUncList(nominal, allHistos,
                                        self.doColorEnvelope, False, True)
        else:
            uncListorig = ep.getUncList(nominal, allHistos,
                                        self.doColorEnvelope, False)
        print 'Full uncertainties list (ordered by impact):', uncListorig
        uncList = uncListorig[:vl.nuncs]

        incmax = []
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            if nominal_withErrors[1].GetBinError(
                    bin) > nominal_withErrors[0].GetBinContent(bin):
                incmax.append(
                    max([
                        nominal_withErrors[0].GetBinError(bin),
                        nominal_withErrors[0].GetBinContent(bin)
                    ]))
            else:
                incmax.append(
                    max([
                        nominal_withErrors[0].GetBinError(bin),
                        nominal_withErrors[1].GetBinError(bin)
                    ]))

        incsyst = []
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            if math.sqrt(nominal_withErrors[1].GetBinError(bin)**2 -
                         nominal.GetBinError(bin)**2
                         ) > nominal_withErrors[0].GetBinContent(bin):
                incsyst.append(
                    max([
                        math.sqrt(nominal_withErrors[0].GetBinError(bin)**2 -
                                  nominal.GetBinError(bin)**2),
                        nominal_withErrors[0].GetBinContent(bin)
                    ]))
            else:
                incsyst.append(
                    max([
                        math.sqrt(nominal_withErrors[0].GetBinError(bin)**2 -
                                  nominal.GetBinError(bin)**2),
                        math.sqrt(nominal_withErrors[1].GetBinError(bin)**2 -
                                  nominal.GetBinError(bin)**2)
                    ]))

        maxinctot = 0
        hincmax = nominal_withErrors[0].Clone('hincmax')
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            hincmax.SetBinContent(bin,
                                  incmax[bin - 1] / hincmax.GetBinContent(bin))
            hincmax.SetBinError(bin, 0)
            if (hincmax.GetBinContent(bin) > maxinctot):
                maxinctot = hincmax.GetBinContent(bin)

        hincsyst = copy.deepcopy(nominal.Clone('hincsyst'))
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            hincsyst.SetBinContent(
                bin, incsyst[bin - 1] / hincsyst.GetBinContent(bin))
            hincsyst.SetBinError(bin, 0.)

        hincmax.SetLineColor(r.kBlack)
        hincmax.SetLineWidth(2)
        hincmax.SetFillColorAlpha(r.kBlue, 0)
        hincsyst.SetLineColor(r.kBlack)
        hincsyst.SetLineWidth(2)
        hincsyst.SetLineStyle(3)
        hincsyst.SetFillColorAlpha(r.kBlue, 0.)

        #if (maxinctot >= 0.9):
        #if maxinctot >= 5:
        #uncList[0][1].GetYaxis().SetRangeUser(0, 5)
        #else:
        #uncList[0][1].GetYaxis().SetRangeUser(0, maxinctot + 0.1)

        #else:
        #uncList[0][1].GetYaxis().SetRangeUser(0, 0.9)

        yaxismax_unc = 2
        if "yaxismax_unf" in vl.varList[self.var]:
            yaxismax_unc = vl.varList[self.var]["yaxismax_unf"]

        hincmax.GetYaxis().SetRangeUser(0, yaxismax_unc)
        plot2.addHisto(hincmax, 'hist', 'Total', 'L')
        plot2.addHisto(hincsyst, 'hist,same', 'Systematic', 'L')
        actualindex = 0
        isstat = False
        for i in range(vl.nuncs):
            #if "Stat" in uncListorig[i][0]:
            #uncListorig[actualindex][1].SetLineColor(r.kBlack)
            #uncListorig[actualindex][1].SetLineStyle( 2 )
            #if "Stat" in uncListorig[actualindex][0]:
            #actualindex += 1
            uncListorig[actualindex][1].SetLineColor(
                vl.NewColorMap[uncListorig[actualindex][0]])
            uncListorig[actualindex][1].SetLineWidth(2)
            if "Lumi" in uncListorig[actualindex][0]:
                uncListorig[actualindex][1].SetLineColor(r.kBlack)
                uncListorig[actualindex][1].SetLineStyle(4)
            if "Stat" in uncListorig[actualindex][0]:
                isstat = True
                uncListorig[actualindex][1].SetLineColor(r.kBlack)
                uncListorig[actualindex][1].SetLineStyle(2)
                plot2.addHisto(
                    uncListorig[actualindex][1], 'hist,same',
                    vl.SysNameTranslator[uncListorig[actualindex][0]], 'L')
                actualindex += 1
            elif (not isstat and actualindex == vl.nuncs - 1):
                isstat = True
                for j in range(len(uncListorig)):
                    if "Stat" in uncListorig[j][0]:
                        uncListorig[j][1].SetLineColor(r.kBlack)
                        uncListorig[j][1].SetLineStyle(2)
                        plot2.addHisto(uncListorig[j][1], 'hist,same',
                                       vl.SysNameTranslator[uncListorig[j][0]],
                                       'L')
            else:
                plot2.addHisto(
                    uncListorig[actualindex][1], 'H,same',
                    vl.SysNameTranslator[uncListorig[actualindex][0]], 'L')
            actualindex += 1

        #for i in range(len(uncListorig)):
        #if "Stat" in uncListorig[i][0]:
        #uncListorig[i][1].SetLineColor(r.kBlack)
        #uncListorig[i][1].SetLineStyle( 2 )
        #plot2.addHisto(uncListorig[i][1], 'hist,same', 'Statistical', 'L')
        plot2.plotspath = self.plotspath

        if "uncleg_unf" in vl.varList[self.var] and not self.wearedoingasimov:
            unclegpos = vl.varList[self.var]["uncleg_unf"]
        elif "uncleg_unfas" in vl.varList[self.var]:
            unclegpos = vl.varList[self.var]["uncleg_unfas"]
        else:
            unclegpos = "TR"

        plot2.saveCanvas(unclegpos)
        del plot2
        return
Esempio n. 14
0
    def doUnfoldingForAllNuis(self):
        allHistos = {}
        self.prepareAllHelpers()
        if self.doRegularisation and self.tau == 0:
            print '> Performing regularisation...'
            self.doLCurveScan()
        tauval = 0
        if self.doRegularisation: tauval = self.taulist['']

        if verbose: print '> Unfolding nominal distribution'
        self.helpers[''].tunfolder.DoUnfold(tauval)
        nominal = copy.deepcopy(self.helpers[''].tunfolder.GetOutput(self.var))
        covnom = copy.deepcopy(
            self.helpers[''].tunfolder.GetEmatrixTotal("CovMat"))
        for bin in range(1, nominal.GetNbinsX() + 1):
            nominal.SetBinError(bin, math.sqrt(covnom.GetBinContent(bin, bin)))

        for nuis in self.sysList:
            if verbose:
                print '> Unfolding distribution of {sys} systematic'.format(
                    sys=nuis)
            self.helpers[nuis].tunfolder.DoUnfold(tauval)
            allHistos[nuis] = self.helpers[nuis].tunfolder.GetOutput(self.var +
                                                                     '_' +
                                                                     nuis)

        scaleval = 1 / vl.Lumi / 1000 if vl.doxsec else 1
        nominal.Scale(scaleval)
        for key in allHistos:
            allHistos[key].Scale(scaleval)

        if not self.wearedoingasimov:
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information does not exist'
                )
            tmptfile = r.TFile.Open(
                'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var))
            #tmptfile2 = r.TFile.Open('temp/{var}_/ClosureTest_recobinning_{var}.root'.format(var = self.var))
            tru = copy.deepcopy(tmptfile.Get('tW'))
            #tru2 = copy.deepcopy(tmptfile2.Get('tW'))
            #tru2.Scale(vl.Lumi*1000)
            for bin in range(1, allHistos['asimov'].GetNbinsX() + 1):
                #print "\nasimov:", allHistos['asimov'].GetBinContent(bin)
                #print "verdad:", tru.GetBinContent(bin)
                allHistos['asimov'].SetBinError(
                    bin,
                    abs(allHistos['asimov'].GetBinContent(bin) -
                        tru.GetBinContent(bin)))
                #allHistos['asimov'].SetBinContent(bin, nominal.GetBinContent(bin))
                #print "errorin:", allHistos['asimov'].GetBinError(bin)
                #print "nominal:", nominal.GetBinContent(bin)
                #print "relativo", round(allHistos['asimov'].GetBinError(bin)/allHistos['asimov'].GetBinContent(bin)*100, 1)
                #print "ratio:", allHistos['asimov'].GetBinContent(bin)/tru.GetBinContent(bin)
            #for bin in range(1, tru2.GetNbinsX() + 1):
            #print "verdad2:", tru2.GetBinContent(bin)
            #tmptfile2.Close()
            tmptfile.Close()
            del tru

        if not self.wearedoingasimov:
            savetfile = r.TFile(
                "temp/{var}_/unfOutput_{var}.root".format(var=self.var),
                "recreate")
            nominal.Write()
            for key in allHistos:
                allHistos[key].Write()
            covnom.Scale(scaleval**2)
            covnom.Write()
            #covitwo = copy.deepcopy(self.helpers[''].tunfolder.GetEmatrixInput("CovMatInput"))
            #covitwo.Scale(scaleval**2)
            #covitwo.Write()
            #covithree = copy.deepcopy(self.helpers[''].tunfolder.GetEmatrixSysUncorr("CovMatResponse"))
            #covithree.Scale(scaleval**2)
            #covithree.Write()
            savetfile.Close()

        if not self.wearedoingasimov:
            nominal_withErrors = ep.propagateHistoAsym(nominal, allHistos,
                                                       self.doColorEnvelope,
                                                       True)
        else:
            nominal_withErrors = ep.propagateHistoAsym(nominal, allHistos,
                                                       self.doColorEnvelope)
        plot = bp.beautifulUnfoldingPlots(
            self.var + "_asimov" if self.wearedoingasimov else self.var)
        plot.doRatio = True
        plot.doFit = False
        plot.plotspath = self.plotspath

        nominal.SetMarkerStyle(r.kFullCircle)
        nominal.SetLineColor(r.kBlack)
        nominal_withErrors[0].SetFillColorAlpha(r.kBlue, 0.35)
        nominal_withErrors[0].SetLineColor(r.kBlue)
        nominal_withErrors[0].SetFillStyle(1001)

        if not self.wearedoingasimov:
            savetfile2 = r.TFile(
                "temp/{var}_/unfOutput_{var}.root".format(var=self.var),
                "update")
            nom0 = copy.deepcopy(nominal_withErrors[0].Clone("nom0"))
            nom1 = copy.deepcopy(nominal_withErrors[1].Clone("nom1"))
            nom0.Write()
            nom1.Write()
            savetfile2.Close()
            del nom0, nom1

        #############################
        print "\nLOS RESULTAOS - {uno} - {dos}".format(
            uno="ASIMOV" if self.wearedoingasimov else "DATOS", dos=self.var)
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            print "Bin", bin, "(abs.): (", round(
                nominal.GetBinContent(bin), 4), "+", round(
                    nominal_withErrors[0].GetBinError(bin),
                    4), "-", round(nominal_withErrors[1].GetBinError(bin),
                                   4), ") pb"
            print "Bin", bin, "(rel.): (", round(
                nominal.GetBinContent(bin), 4), "+", round(
                    nominal_withErrors[0].GetBinError(bin) /
                    nominal.GetBinContent(bin) * 100, 4), "-", round(
                        nominal_withErrors[1].GetBinError(bin) /
                        nominal.GetBinContent(bin) * 100, 4), ") pb\n"
        print "\n"
        #############################

        if "legpos_unf" in vl.varList[self.var] and not self.wearedoingasimov:
            legloc = vl.varList[self.var]["legpos_unf"]
        elif "legpos_unfas" in vl.varList[self.var] and self.wearedoingasimov:
            legloc = vl.varList[self.var]["legpos_unfas"]
        else:
            legloc = "TR"

        if self.doSanityCheck:
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information does not exist.'
                )
            tmptfile = r.TFile.Open(
                'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var))
            tru = copy.deepcopy(tmptfile.Get('tW').Clone('tru'))
            tru.SetLineWidth(2)
            tru.SetLineColor(bp.colorMap[0])

            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                        var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information from an aMCatNLO sample does not exist.'
                )
            tmptfile2 = r.TFile.Open(
                'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                    var=self.var))
            aMCatNLO = copy.deepcopy(tmptfile2.Get('tW').Clone('aMCatNLO'))
            aMCatNLO.SetLineWidth(2)
            aMCatNLO.SetLineColor(r.kAzure)

            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_DS_{var}.root'.format(
                        var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information with the DS variation does not exist.'
                )
            tmptfile3 = r.TFile.Open(
                'temp/{var}_/ClosureTest_DS_{var}.root'.format(var=self.var))
            hDS = copy.deepcopy(tmptfile3.Get('tW').Clone('hDS'))
            hDS.SetLineWidth(2)
            hDS.SetLineColor(r.kGreen)

            plot.addHisto(nominal_withErrors, 'hist', 'Total unc.', 'F', 'unc')
            plot.addHisto(tru, 'L,same', 'tW Powheg', 'L', 'mc')
            plot.addHisto(aMCatNLO, 'L,same', 'tW aMCatNLO', 'L', 'mc')
            plot.addHisto(hDS, 'L,same', 'tW DS', 'L', 'mc')

            if self.wearedoingasimov:
                plot.addHisto(nominal, 'P,E,same', "Pseudodata", 'P', 'data')
            else:
                plot.addHisto(nominal, 'P,E,same', vl.labellegend, 'P', 'data')
            plot.saveCanvas(legloc)
            tmptfile.Close()
            tmptfile2.Close()
            tmptfile3.Close()
        else:
            plot.addHisto(nominal_withErrors, 'E2', 'Total unc.', 'F')
            if self.wearedoingasimov:
                plot.addHisto(nominal, 'P,same', "Pseudodata", 'P', 'data')
            else:
                plot.addHisto(nominal, 'P,same', vl.labellegend, 'P', 'data')
            plot.saveCanvas(legloc)

        del plot
        plot2 = bp.beautifulUnfoldingPlots(
            self.var +
            'uncertainties_asimov' if self.wearedoingasimov else self.var +
            'uncertainties')
        plot2.doFit = False
        if not self.wearedoingasimov:
            uncList = ep.getUncList(nominal, allHistos, self.doColorEnvelope,
                                    False, True)
        else:
            uncList = ep.getUncList(nominal, allHistos, self.doColorEnvelope,
                                    False)
        print 'Full uncertainties list (ordered by impact):', uncList
        uncList = uncList[:vl.nuncs]

        incmax = []
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            if nominal_withErrors[1].GetBinError(
                    bin) > nominal_withErrors[0].GetBinContent(bin):
                incmax.append(
                    max([
                        nominal_withErrors[0].GetBinError(bin),
                        nominal_withErrors[0].GetBinContent(bin)
                    ]))
            else:
                incmax.append(
                    max([
                        nominal_withErrors[0].GetBinError(bin),
                        nominal_withErrors[1].GetBinError(bin)
                    ]))

        incsyst = []
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            if math.sqrt(nominal_withErrors[1].GetBinError(bin)**2 -
                         nominal.GetBinError(bin)**2
                         ) > nominal_withErrors[0].GetBinContent(bin):
                incsyst.append(
                    max([
                        math.sqrt(nominal_withErrors[0].GetBinError(bin)**2 -
                                  nominal.GetBinError(bin)**2),
                        nominal_withErrors[0].GetBinContent(bin)
                    ]))
            else:
                incsyst.append(
                    max([
                        math.sqrt(nominal_withErrors[0].GetBinError(bin)**2 -
                                  nominal.GetBinError(bin)**2),
                        math.sqrt(nominal_withErrors[1].GetBinError(bin)**2 -
                                  nominal.GetBinError(bin)**2)
                    ]))

        maxinctot = 0
        hincmax = nominal_withErrors[0].Clone('hincmax')
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            hincmax.SetBinContent(bin,
                                  incmax[bin - 1] / hincmax.GetBinContent(bin))
            hincmax.SetBinError(bin, 0)
            if (hincmax.GetBinContent(bin) > maxinctot):
                maxinctot = hincmax.GetBinContent(bin)

        hincsyst = copy.deepcopy(nominal.Clone('hincsyst'))
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            hincsyst.SetBinContent(
                bin, incsyst[bin - 1] / hincsyst.GetBinContent(bin))
            hincsyst.SetBinError(bin, 0.)

        hincmax.SetLineColor(r.kBlack)
        hincmax.SetLineWidth(2)
        hincmax.SetFillColorAlpha(r.kBlue, 0)
        hincsyst.SetLineColor(r.kBlack)
        hincsyst.SetLineWidth(2)
        hincsyst.SetLineStyle(3)
        hincsyst.SetFillColorAlpha(r.kBlue, 0.)

        #if (maxinctot >= 0.9):
        #if maxinctot >= 5:
        #uncList[0][1].GetYaxis().SetRangeUser(0, 5)
        #else:
        #uncList[0][1].GetYaxis().SetRangeUser(0, maxinctot + 0.1)

        #else:
        #uncList[0][1].GetYaxis().SetRangeUser(0, 0.9)

        uncList[0][1].GetYaxis().SetRangeUser(0, 2)

        for i in range(vl.nuncs):
            uncList[i][1].SetLineColor(vl.NewColorMap[uncList[i][0]])
            uncList[i][1].SetLineWidth(2)
            if "Stat" in uncList[i][0]:
                uncList[i][1].SetLineColor(r.kBlack)
                uncList[i][1].SetLineStyle(2)
            elif "Lumi" in uncList[i][0]:
                uncList[i][1].SetLineColor(r.kBlack)
                uncList[i][1].SetLineStyle(4)

            plot2.addHisto(uncList[i][1], 'H,same' if i else 'H',
                           uncList[i][0], 'L')

        plot2.addHisto(hincsyst, 'hist,same', 'Syst.', 'L')
        plot2.addHisto(hincmax, 'hist,same', 'Total', 'L')
        plot2.plotspath = self.plotspath

        if "uncleg_unf" in vl.varList[self.var] and not self.wearedoingasimov:
            unclegpos = vl.varList[self.var]["uncleg_unf"]
        elif "uncleg_unfas" in vl.varList[self.var]:
            unclegpos = vl.varList[self.var]["uncleg_unfas"]
        else:
            unclegpos = "TR"

        plot2.saveCanvas(unclegpos)
        del plot2
        return
Esempio n. 15
0
allHistos = {}
for nuis in [i for i in vl.systMap
             ] + ['pdfUp', 'pdfDown', 'ttbarMEUp', 'ttbarMEDown']:
    if 'hFitResult_%s_%s' % (varName, nuis) not in listofkeys:
        raise RuntimeError('Variations of the variable ' + varName +
                           ' with the systematic ' + nuis + ' were not found.')
    allHistos[nuis] = copy.deepcopy(
        fitinfo.Get('hFitResult_%s_%s' % (varName, nuis)))
fitinfo.Close()

for bin in range(1, errors.GetNbinsX() + 1):
    nominal.SetBinError(bin, errors.GetBinContent(bin))

nominal_withErrors = ep.propagateHistoAsym(nominal, allHistos, True)
plot = bp.beautifulUnfoldingPlots(varName + '_folded')
plot.doRatio = True
plot.plotspath = "results/"

nominal.SetMarkerStyle(r.kFullCircle)
nominal_withErrors[0].SetFillColorAlpha(r.kBlue, 0.35)
nominal_withErrors[0].SetLineColor(r.kBlue)
nominal_withErrors[0].SetFillStyle(1001)

if doSanityCheck:
    print '> Adding generated distribution with used software and others.'
    if not os.path.isfile(
            'temp/{var}_/ClosureTest_recobinning_{var}.root'.format(
                var=varName)):
        raise RuntimeError(
            'The rootfile with the generated information does not exist')
Esempio n. 16
0
    def doUnfoldingForAllNuis(self, asym=False):
        self.prepareAllHelpers()
        allHistos = {}
        nominal = copy.deepcopy(self.helpers[''].tunfolder.GetOutput(self.var))
        for nuis in self.sysList:
            self.helpers[nuis].tunfolder.DoUnfold(
                self.helpers[''].tunfolder.GetTau())
            allHistos[nuis] = self.helpers[nuis].tunfolder.GetOutput(self.var +
                                                                     '_' +
                                                                     nuis)

        if asym:
            nominal_withErrors = errorPropagator.propagateHistoAsym(
                nominal, allHistos, self.doColorEnvelope)
        else:
            nominal_withErrors = errorPropagator.propagateHisto(
                nominal, allHistos, self.doColorEnvelope)

        plot = beautifulUnfoldingPlots.beautifulUnfoldingPlots(self.var)
        plot.plotspath = self.plotspath
        nominal.SetMarkerStyle(r.kFullCircle)
        nominal.GetXaxis().SetNdivisions(505, True)

        if asym:
            nominal_withErrors[0].SetFillColorAlpha(r.kBlue, 0.35)
            nominal_withErrors[0].SetLineColor(r.kBlue)
            nominal_withErrors[0].SetFillStyle(1001)
        else:
            nominal_withErrors.SetFillColorAlpha(r.kBlue, 0.35)
            nominal_withErrors.SetLineColor(r.kBlue)
            nominal_withErrors.SetFillStyle(1001)

        if self.doSanityCheck:
            #tmptfile = r.TFile.Open('/nfs/fanae/user/sscruz/TW_differential/AnalysisPAF/plotter/./Datacards/closuretest_TGenLeadingJet_TGen{var}.root'.format(var=self.var))
            if not os.path.isfile(
                    'temp/ClosureTest_{var}.root'.format(var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information does not exist'
                )
            tmptfile = r.TFile.Open(
                'temp/ClosureTest_{var}.root'.format(var=self.var))
            tru = copy.deepcopy(tmptfile.Get('tW'))
            tru.SetLineWidth(2)
            tru.SetLineColor(beautifulUnfoldingPlots.colorMap[0])
            if asym:
                plot.addHisto(nominal_withErrors, 'hist', 'Syst. unc.', 'F')
            else:
                plot.addHisto(nominal_withErrors, 'E2', 'Syst. unc.', 'F')
            plot.addHisto(nominal, 'P,same', 'Pseudodata', 'P')
            plot.addHisto(tru, 'L,same', 'Truth', 'L')
            plot.saveCanvas('TR')
            tmptfile.Close()
        else:
            if asym:
                plot.addHisto(nominal_withErrors, 'hist', 'Syst. unc.', 'F')
            else:
                plot.addHisto(nominal_withErrors, 'E2', 'Syst. unc.', 'F')
            plot.addHisto(nominal, 'P,same', 'Pseudodata', 'P')
            plot.saveCanvas('TR')

        plot2 = beautifulUnfoldingPlots.beautifulUnfoldingPlots(
            self.var + 'uncertainties')
        uncList = errorPropagator.getUncList(nominal, allHistos,
                                             self.doColorEnvelope)[:5]
        uncList[0][1].Draw()

        if uncList[0][1].GetMaximum() < 0.5:

            uncList[0][1].GetYaxis().SetRangeUser(0, 0.5)
        else:
            uncList[0][1].GetYaxis().SetRangeUser(0, 0.75)
        for i in range(5):
            uncList[i][1].SetLineColor(beautifulUnfoldingPlots.colorMap[i])
            uncList[i][1].SetLineWidth(2)
            plot2.addHisto(uncList[i][1], 'H,same' if i else 'H',
                           uncList[i][0], 'L')

        plot2.plotspath = self.plotspath
        plot2.saveCanvas('TR')
Esempio n. 17
0
def damelasmiovariaciones(tsk):
    variacion, ty = tsk
    #for varname in vl.varList["Names"]["Variables"]:
    if ty == "folded":
        damelasmiovariacionesprofolded(variacion)
        return

    for varname in variables:
        print "\n> Producing for variable", varname, "and variation", variacion, "for type", ty
        if "Fiducial" in varname: continue
        fullpath = pathtothings + varname + "_/" + "forBeautifulPlots_{var}.root".format(
            var=varname) * (ty == "mcdata") + (
                ty == "unf") * "normOutput_{var}.root".format(var=varname)
        cutout = r.TFile.Open(fullpath, "READ")

        #binning = array.array("f", vl.varList[varname]["recobinning"])

        textSize = 0.035 if "unc" in varname else 0.0435
        if "unc" in varname: legWidth = 0.13
        else: legWidth = 0.2
        height = (.18 + textSize * max(2 - 3, 0))

        corner = "TR"

        if isinstance(corner, str):
            if corner == "TR":
                (x1, y1, x2, y2) = (.72 - legWidth, .9 - height, .735, .9)
            elif corner == "TC":
                (x1, y1, x2, y2) = (.5, .9 - height, .55 + legWidth, .9)
            elif corner == "TL":
                (x1, y1, x2, y2) = (.18, .78 - height, .18 + legWidth, .78)
            elif corner == "MR":
                (x1, y1, x2, y2) = (.85 - legWidth, .3 + height, .90, .3)
            elif corner == "MC":
                (x1, y1, x2, y2) = (.5, .3 + height, .5 + legWidth, .3)
            elif corner == "ML":
                (x1, y1, x2, y2) = (.18, .3 + height, .18 + legWidth, .3)
            elif corner == "BR":
                (x1, y1, x2, y2) = (.85 - legWidth, .1 + height, .90, .1)
            elif corner == "BC":
                (x1, y1, x2, y2) = (.5, .1 + height, .5 + legWidth, .1)
            elif corner == "BL":
                (x1, y1, x2, y2) = (.18, .1 + height, .18 + legWidth, .1)

        histolist = []

        if ty == "unf":
            plot = bp.beautifulUnfoldingPlots((
                varname + "_" + ("norm" if (
                    variacion == "ttbar" or variacion == "VVttbarV"
                    or variacion == "DY" or variacion == "Non-WorZ") else "") +
                variacion + "_values_unf") if "Colour" not in variacion else
                                              "ColourReconnection_values_unf")
            plot.doRatio = False
            plot.doFit = False
            plot.doPreliminary = False
            plot.noCMS = True
            plot.doSupplementary = False
            plot.plotspath = outpath + "/{var}/".format(var=varname)

            legloc = ""

            if "legpos_unf" in vl.varList[varname]:
                legloc = vl.varList[varname]["legpos_unf"]
            else:
                legloc = "TR"

            legloc = "TR"

            found = False
            nominal = r.TH1F()
            for key in cutout.GetListOfKeys():
                if key.GetName() == varname + "_{vr}Up".format(
                        vr=variacion) and "Colour" not in variacion:
                    histolist.append(r.TH1F())
                    histolist[-1] = copy.deepcopy(
                        cutout.Get(varname +
                                   "_{vr}Up".format(vr=variacion)).Clone())
                    found = True
                    #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                    #histolist[-1].GetYaxis().SetRangeUser(0, maximaunf[varname])
                    histolist[-1].SetMaximum(maximaunf[varname])
                    histolist[-1].SetLineColor(r.kBlue)
                    histolist[-1].SetFillStyle(0)
                    plot.addHisto(histolist[-1], 'P,E,same', variacion + "_Up",
                                  'PEL', variacion + "_Up")
                elif key.GetName() == varname + "_{vr}Down".format(
                        vr=variacion) and "Colour" not in variacion:
                    histolist.append(r.TH1F())
                    histolist[-1] = copy.deepcopy(
                        cutout.Get(varname +
                                   "_{vr}Down".format(vr=variacion)).Clone())
                    found = True
                    #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                    #histolist[-1].GetYaxis().SetRangeUser(0, maximaunf[varname])
                    histolist[-1].SetMaximum(maximaunf[varname])
                    histolist[-1].SetLineColor(r.kRed)
                    histolist[-1].SetFillStyle(0)
                    plot.addHisto(histolist[-1], 'P,E,same',
                                  variacion + "_Down", 'PEL',
                                  variacion + "_Down")
                elif (key.GetName() == varname +
                      "_{vr}".format(vr=colour_syst[0].replace("Colour", ""))
                      ) and "Colour" in variacion:  ### Colour reconnection
                    histolist.append(r.TH1F())
                    histolist[-1] = copy.deepcopy(
                        cutout.Get(varname + "_{vr}".format(
                            vr=colour_syst[0].replace("Colour", ""))).Clone())
                    found = True
                    #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                    #histolist[-1].GetYaxis().SetRangeUser(0, maximaunf[varname])
                    histolist[-1].SetMaximum(maximaunf[varname])
                    histolist[-1].SetLineColor(colour_colours[0])
                    histolist[-1].SetFillStyle(0)
                    plot.addHisto(histolist[-1], 'P,E,same',
                                  colour_syst[0].replace("Colour", "") + "_",
                                  'PEL', colour_syst[0].replace("Colour", ""))
                elif (key.GetName() == varname +
                      "_{vr}".format(vr=colour_syst[1].replace("Colour", ""))
                      ) and "Colour" in variacion:
                    histolist.append(r.TH1F())
                    histolist[-1] = copy.deepcopy(
                        cutout.Get(varname + "_{vr}".format(
                            vr=colour_syst[1].replace("Colour", ""))).Clone())
                    found = True
                    #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                    #histolist[-1].GetYaxis().SetRangeUser(0, maximaunf[varname])
                    histolist[-1].SetMaximum(maximaunf[varname])
                    histolist[-1].SetLineColor(colour_colours[1])
                    histolist[-1].SetFillStyle(0)
                    plot.addHisto(histolist[-1], 'P,E,same',
                                  colour_syst[1].replace("Colour", ""), 'PEL',
                                  colour_syst[1].replace("Colour", ""))
                elif (key.GetName() == varname +
                      "_{vr}".format(vr=colour_syst[2].replace("Colour", ""))
                      ) and "Colour" in variacion:
                    histolist.append(r.TH1F())
                    histolist[-1] = copy.deepcopy(
                        cutout.Get(varname + "_{vr}".format(
                            vr=colour_syst[2].replace("Colour", ""))).Clone())
                    found = True
                    #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                    #histolist[-1].GetYaxis().SetRangeUser(0, maximaunf[varname])
                    histolist[-1].SetMaximum(maximaunf[varname])
                    histolist[-1].SetLineColor(colour_colours[2])
                    histolist[-1].SetFillStyle(0)
                    plot.addHisto(histolist[-1], 'P,E,same',
                                  colour_syst[2].replace("Colour", ""), 'PEL',
                                  colour_syst[2].replace("Colour", ""))
                elif (key.GetName() == varname +
                      "_{vr}".format(vr=colour_syst[3].replace("Colour", ""))
                      ) and "Colour" in variacion:
                    histolist.append(r.TH1F())
                    histolist[-1] = copy.deepcopy(
                        cutout.Get(varname + "_{vr}".format(
                            vr=colour_syst[3].replace("Colour", ""))).Clone())
                    found = True
                    #print "\nMASIMU:", histolist[-1].GetMaximum() + histolist[-1].GetMaximum()*0.075
                    #histolist[-1].GetYaxis().SetRangeUser(0, maximaunf[varname])
                    histolist[-1].SetMaximum(maximaunf[varname])
                    histolist[-1].SetLineColor(colour_colours[3])
                    histolist[-1].SetFillStyle(0)
                    plot.addHisto(histolist[-1], 'P,E,same',
                                  colour_syst[3].replace("Colour", "") + "_",
                                  'PEL', colour_syst[3].replace("Colour", ""))
                elif key.GetName() == varname:
                    nominal = copy.deepcopy(
                        cutout.Get(varname).Clone("nominal"))
                    #nominal.GetYaxis().SetRangeUser(0, maximaunf[varname])
                    nominal.SetMaximum(maximaunf[varname])
                    nominal.SetLineColor(r.kBlack)
                    nominal.SetFillStyle(0)
                    plot.addHisto(nominal, 'P,E,same', "Nominal", 'PEL',
                                  "Nominal")

            r.gPad.RedrawAxis()
            plot.saveCanvas(legloc)
            del plot

            legloc = "TC"

            histnom = copy.deepcopy(cutout.Get(varname).Clone("histnom"))
            unctotUp = copy.deepcopy(cutout.Get("nom0").Clone("unctotUp"))
            unctotDn = copy.deepcopy(cutout.Get("nom1").Clone("unctotDn"))

            unctotmax = copy.deepcopy(unctotUp.Clone("unctotmax"))
            for bin in range(1, unctotmax.GetNbinsX() + 1):
                unctotmax.SetBinError(
                    bin,
                    max(unctotUp.GetBinError(bin), unctotDn.GetBinError(bin)))
                #print "unctotmax bin", bin, ":", unctotmax.GetBinError(bin)

            unctotmaxrel = copy.deepcopy(cutout.Get("hincmax").Clone())
            uncsysrel = copy.deepcopy(cutout.Get("hincsyst").Clone())

            plotrel = bp.beautifulUnfoldingPlots(
                (varname + "_" + ("norm" if (
                    variacion == "ttbar" or variacion == "VVttbarV"
                    or variacion == "DY" or variacion == "Non-WorZ") else "") +
                 variacion + "_uncrel_unf") if "Colour" not in
                variacion else "ColourReconnection_uncrel_unf")
            plotrel.doRatio = False
            plotrel.doFit = False
            plotrel.doPreliminary = False
            plotrel.noCMS = True
            plotrel.doSupplementary = False
            plotrel.plotspath = outpath + "/{var}/".format(var=varname)

            unchistolist = []

            unctotmaxrel.SetFillStyle(0)
            uncsysrel.SetFillStyle(0)
            plotrel.addHisto(unctotmaxrel, 'H,same', "Total", "L")
            plotrel.addHisto(uncsysrel, 'H,same', "Total systematic", "L")
            for minivar in histolist:
                unchistolist.append(
                    copy.deepcopy(
                        unctotmaxrel.Clone(
                            minivar.GetName().replace(varname + "_", "") +
                            "_uncrel")))
                for bin in range(1, unchistolist[-1].GetNbinsX() + 1):
                    unchistolist[-1].SetBinContent(
                        bin,
                        (abs(
                            minivar.GetBinContent(bin) -
                            histnom.GetBinContent(bin)) if
                         (minivar.GetBinContent(bin) >
                          histnom.GetBinContent(bin)) or
                         (minivar.GetBinContent(bin) < histnom.GetBinContent(
                             bin) and minivar.GetBinContent(bin) >= 0) else
                         histnom.GetBinContent(bin)) /
                        histnom.GetBinContent(bin))
                    unchistolist[-1].SetBinError(bin, 0)
                    unchistolist[-1].SetLineWidth(1)
                    if "Down" in minivar.GetName():
                        unchistolist[-1].SetLineColor(r.kRed)
                    elif "Colour" not in variacion:
                        unchistolist[-1].SetLineColor(r.kBlue)
                    elif "Colour" in variacion:
                        unchistolist[-1].SetLineColor(minivar.GetLineColor())
                    #print "histo", minivar.GetName(), "bin:", bin, ":", unchistolist[-1].GetBinContent(bin)
                unchistolist[-1].SetFillStyle(0)
                plotrel.addHisto(unchistolist[-1], 'H,same',
                                 minivar.GetName().replace(varname + "_", ""),
                                 'L')

            if len(unchistolist) == 2:
                unchistolist.append(
                    copy.deepcopy(unchistolist[-1].Clone("Average")))
                for bin in range(1, unctotmaxrel.GetNbinsX() + 1):
                    unchistolist[-1].SetBinContent(
                        bin, (unchistolist[-1].GetBinContent(bin) +
                              unchistolist[-3].GetBinContent(bin)) / 2)
                unchistolist[-1].SetLineColor(r.kGreen)
                plotrel.addHisto(unchistolist[-1], 'H,same', "Average", 'L')

            r.gPad.RedrawAxis()
            plotrel.saveCanvas(legloc)
            del plotrel

            plotabs = bp.beautifulUnfoldingPlots(
                (varname + "_" + ("norm" if (
                    variacion == "ttbar" or variacion == "VVttbarV"
                    or variacion == "DY" or variacion == "Non-WorZ") else "") +
                 variacion + "_uncabs_unf") if "Colour" not in
                variacion else "ColourReconnection_uncabs_unf")
            plotabs.doRatio = False
            plotabs.doFit = False
            plotabs.doPreliminary = False
            plotabs.noCMS = True
            plotabs.doSupplementary = False
            plotabs.plotspath = outpath + "/{var}/".format(var=varname)

            unctotmaxabs = copy.deepcopy(unctotmaxrel.Clone("unctotmaxabs"))
            uncsysabs = copy.deepcopy(uncsysrel.Clone("uncsysabs"))

            absmaximum = 0

            for bin in range(1, unctotmaxabs.GetNbinsX() + 1):
                unctotmaxabs.SetBinContent(
                    bin,
                    unctotmaxabs.GetBinContent(bin) *
                    histnom.GetBinContent(bin))
                uncsysabs.SetBinContent(
                    bin,
                    uncsysabs.GetBinContent(bin) * histnom.GetBinContent(bin))
                if unctotmaxabs.GetBinContent(bin) > absmaximum:
                    absmaximum = unctotmaxabs.GetBinContent(bin)

            unctotmaxabs.SetMaximum(absmaximum * 1.1)

            unctotmaxabs.GetYaxis().SetTitle("Absolute uncertainty (pb/GeV)")

            unctotmaxabs.SetFillStyle(0)
            uncsysabs.SetFillStyle(0)
            plotabs.addHisto(unctotmaxabs, 'H,same', "Total", "L")
            plotabs.addHisto(uncsysabs, 'H,same', "Total systematic", "L")

            unchistoabslist = []

            for minivar in unchistolist:
                unchistoabslist.append(
                    minivar.Clone(minivar.GetName() + "_abs"))
                for bin in range(1, unctotmaxabs.GetNbinsX() + 1):
                    unchistoabslist[-1].SetBinContent(
                        bin, unchistoabslist[-1].GetBinContent(bin) *
                        histnom.GetBinContent(bin))
                    #print "histo", minivar.GetName(), "bin:", bin, ":", unchistoabslist[-1].GetBinContent(bin)
                    #print "nominal bin:", bin, ":", histnom.GetBinContent(bin)
                unchistoabslist[-1].SetFillStyle(0)
                plotabs.addHisto(unchistoabslist[-1], 'H,same',
                                 minivar.GetName().replace(varname + "_", ""),
                                 'L')

            if len(unchistoabslist) == 2:
                unchistoabslist.append(
                    copy.deepcopy(unchistoabslist[-1].Clone("Average")))
                for bin in range(1, unctotmaxabs.GetNbinsX() + 1):
                    unchistoabslist[-1].SetBinContent(
                        bin, (unchistoabslist[-1].GetBinContent(bin) +
                              unchistoabslist[-3].GetBinContent(bin)) / 2)
                unchistoabslist[-1].SetLineColor(r.kGreen)
                plotabs.addHisto(unchistoabslist[-1], 'H,same', "Average", 'L')

            r.gPad.RedrawAxis()
            plotabs.saveCanvas(legloc)
            del plotabs

        else:
            leg = r.TLegend(x1, y1, x2, y2)
            leg.SetTextFont(42)
            leg.SetTextSize(textSize)
            leg.SetBorderSize(0)
            leg.SetFillColor(10)
            leg.SetFillStyle(0)
            # transparent legend!
            stack = r.THStack("stack", "")

            for gr in groups:
                histolist.append(r.TH1F())
                found = False
                for key in cutout.GetListOfKeys():
                    if key.GetName() == gr + "_{vr}".format(
                            vr=variacion.replace("Colour", "")):
                        histolist[-1] = copy.deepcopy(
                            cutout.Get(gr + "_{vr}".format(
                                vr=variacion.replace("Colour", ""))).Clone())
                        found = True
                if not found:
                    histolist[-1] = copy.deepcopy(cutout.Get(gr).Clone())
                leg.AddEntry(histolist[-1], gr, "f")
                histolist[-1].SetFillColor(colours[gr])
                histolist[-1].SetLineWidth(0)
                stack.Add(histolist[-1])

            #stack.GetYaxis().SetRangeUser(0, maxima[varname])
            stack.SetMaximum(maxima[varname])

            datos = r.TH1F(cutout.Get("data_obs").Clone("datos"))
            leg.AddEntry(datos, "Data", "P")

            c = r.TCanvas("c", "", 600, 600)

            c.Divide(1, 2)
            c.GetPad(1).SetPad(*vl.plotlimits)
            c.GetPad(2).SetPad(*vl.ratiolimits)
            c.GetPad(1).SetTopMargin(0.08)
            c.GetPad(1).SetRightMargin(0.03)
            c.GetPad(1).SetLeftMargin(0.16)
            c.GetPad(1).SetBottomMargin(0.025)
            c.GetPad(2).SetBottomMargin(0.35)
            c.GetPad(2).SetBottomMargin(0.375)
            c.GetPad(2).SetRightMargin(0.03)
            c.GetPad(2).SetLeftMargin(0.16)

            c.cd(1)

            stack.Draw("Ehist")
            datos.Draw("Psame")
            leg.Draw("same")

            c.cd(2)

            hratio = copy.deepcopy(datos.Clone("ratio"))
            hratio.Divide(stack.GetStack().Last())

            unity = copy.deepcopy(datos.Clone("unity"))
            for bin in range(1, unity.GetNbinsX() + 1):
                unity.SetBinContent(bin, 1.0)
            unity.SetLineStyle(2)
            unity.SetLineColor(r.kBlack)

            hratio.GetXaxis().SetTitle(vl.varList[varname.replace(
                '_folded',
                '').replace('_asimov',
                            '').replace("_fiducial",
                                        "").replace('_norm',
                                                    '').replace("norm",
                                                                "")]['xaxis'])
            hratio.GetXaxis().SetTitleFont(43)
            hratio.GetXaxis().SetTitleSize(22)
            hratio.GetXaxis().SetTitleOffset(4)
            hratio.GetXaxis().SetLabelFont(43)
            hratio.GetXaxis().SetLabelSize(22)
            hratio.GetXaxis().SetLabelOffset(0.007)
            hratio.GetXaxis().SetNdivisions(510, True)

            hratio.GetYaxis().SetRangeUser(0.5, 1.5)
            hratio.SetLineColor(r.kBlack)
            hratio.GetYaxis().SetTitle('Data/MC')
            hratio.GetYaxis().SetTitleFont(43)
            hratio.GetYaxis().SetTitleSize(22)
            hratio.GetYaxis().SetTitleOffset(2.2)
            hratio.GetYaxis().CenterTitle(True)
            hratio.GetYaxis().SetLabelFont(43)
            hratio.GetYaxis().SetLabelSize(22)
            hratio.GetYaxis().SetLabelOffset(0.007)
            hratio.GetYaxis().SetNdivisions(505, True)
            hratio.Draw("sameL")

            unity.Draw("samehist")

            mkdir(outpath + "{var}".format(var=varname))
            c.SaveAs(outpath + "/{var}/{var}_{vr}{t}.png".format(
                var=varname,
                vr=("norm" if
                    (variacion == "ttbarUp" or variacion == "VVttbarVUp"
                     or variacion == "DYUp" or variacion == "Non-WorZUp"
                     or variacion == "ttbarDown" or variacion == "VVttbarVDown"
                     or variacion == "DYDown" or variacion == "Non-WorZDown"
                     ) else "") + variacion,
                t="_unf" if ty == "unf" else "_mcdata"))
            c.SaveAs(outpath + "/{var}/{var}_{vr}{t}.pdf".format(
                var=varname,
                vr=("norm" if
                    (variacion == "ttbarUp" or variacion == "VVttbarVUp"
                     or variacion == "DYUp" or variacion == "Non-WorZUp"
                     or variacion == "ttbarDown" or variacion == "VVttbarVDown"
                     or variacion == "DYDown" or variacion == "Non-WorZDown"
                     ) else "") + variacion,
                t="_unf" if ty == "unf" else "_mcdata"))
            c.SaveAs(outpath + "/{var}/{var}_{vr}{t}.root".format(
                var=varname,
                vr=("norm" if
                    (variacion == "ttbarUp" or variacion == "VVttbarVUp"
                     or variacion == "DYUp" or variacion == "Non-WorZUp"
                     or variacion == "ttbarDown" or variacion == "VVttbarVDown"
                     or variacion == "DYDown" or variacion == "Non-WorZDown"
                     ) else "") + variacion,
                t="_unf" if ty == "unf" else "_mcdata"))
            c.SaveAs(outpath + "/{var}/{var}_{vr}{t}.C".format(
                var=varname,
                vr=("norm" if
                    (variacion == "ttbarUp" or variacion == "VVttbarVUp"
                     or variacion == "DYUp" or variacion == "Non-WorZUp"
                     or variacion == "ttbarDown" or variacion == "VVttbarVDown"
                     or variacion == "DYDown" or variacion == "Non-WorZDown"
                     ) else "") + variacion,
                t="_unf" if ty == "unf" else "_mcdata"))
            c.Close()
            del c

        cutout.Close()
    return
Esempio n. 18
0
def GiveMeMyGoodGOFTests(tsk):
    var, ty = tsk
    print "\n====== Performing tests for variable", var, "and with type", ty, "\n"
    if ty != "fidunorm":
        f1 = r.TFile.Open(
            pathtothings + "{vr}_/unfOutput_{vr}.root".format(vr=var), "read")
        f2 = r.TFile.Open(
            pathtothings + "{vr}_/ClosureTest_{vr}.root".format(vr=var),
            "read")
        f3 = r.TFile.Open(
            pathtothings + "{vr}_/ClosureTest_DS_{vr}.root".format(vr=var),
            "read")
        f4 = r.TFile.Open(
            pathtothings +
            "{vr}_/ClosureTest_aMCatNLO_{vr}.root".format(vr=var), "read")
        f5 = r.TFile.Open(
            pathtothings + "{vr}_/CovMat_{vr}_unfolded.root".format(vr=var),
            "read")
    else:
        f1 = r.TFile.Open(
            pathtothings + "{vr}_/fidOutput_{vr}norm.root".format(vr=var),
            "read")
        f5 = r.TFile.Open(
            pathtothings +
            "{vr}_/CovMat_{vr}_unfolded_fiducialnorm.root".format(vr=var),
            "read")

    hData = copy.deepcopy(f1.Get(var).Clone("hData"))
    hDataCov = copy.deepcopy(f5.Get("finalmat").Clone("hDataCov"))
    if ty != "fidunorm":
        hDR = copy.deepcopy(f2.Get("tW").Clone("hDR"))
        hDS = copy.deepcopy(f3.Get("tW").Clone("hDS"))
        haMCatNLO = copy.deepcopy(f4.Get("tW").Clone("haMCatNLO"))
        f1.Close()
        f2.Close()
        f3.Close()
        f4.Close()
        f5.Close()
    else:
        hDR = copy.deepcopy(f1.Get("tru").Clone("hDR"))
        hDS = copy.deepcopy(f1.Get("hDS").Clone("hDS"))
        haMCatNLO = copy.deepcopy(f1.Get("aMCatNLO").Clone("haMCatNLO"))
        f1.Close()
        f5.Close()

    hData.SetFillColor(r.kWhite)
    hData.SetLineWidth(2)
    hData.SetLineColor(r.kBlack)
    hData.SetLineStyle(2)
    hDR.SetFillColor(r.kWhite)
    hDR.SetLineWidth(2)
    hDR.SetLineColor(r.kRed)

    hDS.SetFillColor(r.kWhite)
    hDS.SetLineWidth(2)
    hDS.SetLineColor(r.kGreen)
    hDS.GetXaxis().SetTitle(vl.varList[var]['xaxis'])

    haMCatNLO.SetFillColor(r.kWhite)
    haMCatNLO.SetLineWidth(2)
    haMCatNLO.SetLineColor(r.kBlue)

    covmat = np.zeros((hData.GetNbinsX(), hData.GetNbinsX()), dtype=np.double)

    for i in range(1, hData.GetNbinsX() + 1):
        for j in range(1, hData.GetNbinsX() + 1):
            #if i != j: continue
            covmat[i - 1, j - 1] = hDataCov.GetBinContent(i, j)

    DRcovmat = np.diag(
        np.array(
            [hDR.GetBinError(bin)**2 for bin in range(1,
                                                      hDR.GetNbinsX() + 1)],
            dtype=np.double))
    DScovmat = np.diag(
        np.array(
            [hDS.GetBinError(bin)**2 for bin in range(1,
                                                      hDS.GetNbinsX() + 1)],
            dtype=np.double))
    aMCatNLOcovmat = np.diag(
        np.array([
            haMCatNLO.GetBinError(bin)**2
            for bin in range(1,
                             haMCatNLO.GetNbinsX() + 1)
        ],
                 dtype=np.double))

    #covmat         *= 10000**2
    #DRcovmat       *= 10000**2
    #DScovmat       *= 10000**2
    #aMCatNLOcovmat *= 10000**2
    #hDR.Scale(10000)
    #hDS.Scale(10000)
    #haMCatNLO.Scale(10000)
    #hData.Scale(10000)
    #print "\ncovmat de DR\n", DRcovmat

    coses = {}
    coses["DR"] = {
        "hist": copy.deepcopy(hDR),
        "covmat": copy.deepcopy(DRcovmat)
    }
    coses["DS"] = {
        "hist": copy.deepcopy(hDS),
        "covmat": copy.deepcopy(DScovmat)
    }
    coses["aMCatNLO"] = {
        "hist": copy.deepcopy(haMCatNLO),
        "covmat": copy.deepcopy(aMCatNLOcovmat)
    }

    #for i in range(1, coses["DR"]["hist"].GetNbinsX() + 1):
    #print coses["DR"]["hist"].GetBinContent(

    vdata = np.array(
        [hData.GetBinContent(bin) for bin in range(1,
                                                   hData.GetNbinsX() + 1)],
        dtype=np.double)

    #print "\nWOLOLO\n", coses["aMCatNLO"]["covmat"]

    #covmatdiagonal = np.diag(np.array(np.linalg.eig(covmat)[0], dtype = np.double))
    #matriztransf   = np.linalg.eig(covmat)[1]
    #res2           = matriztransf.dot(vdata)
    #res2DR         = matriztransf.dot(np.array([coses["DR"]["hist"].GetBinContent(bin) for bin in range(1, coses["DR"]["hist"].GetNbinsX() + 1)], dtype = np.double))
    #covmatres2DR   = (np.linalg.inv(matriztransf).dot(coses["DR"]["covmat"])).dot(matriztransf)
    #res2DS         = matriztransf.dot(np.array([coses["DS"]["hist"].GetBinContent(bin) for bin in range(1, coses["DS"]["hist"].GetNbinsX() + 1)], dtype = np.double))
    #covmatres2DS   = (np.linalg.inv(matriztransf).dot(coses["DS"]["covmat"])).dot(matriztransf)
    #res2aMC        = matriztransf.dot(np.array([coses["aMCatNLO"]["hist"].GetBinContent(bin) for bin in range(1, coses["aMCatNLO"]["hist"].GetNbinsX() + 1)], dtype = np.double))
    #covmatres2aMC  = (np.linalg.inv(matriztransf).dot(coses["aMCatNLO"]["covmat"])).dot(matriztransf)

    #hData2 = copy.deepcopy(hData.Clone("hData2"))
    #hDR2   = copy.deepcopy(coses["DR"]["hist"].Clone("hDR2"))
    #hDS2   = copy.deepcopy(coses["DS"]["hist"].Clone("hDS2"))
    #haMC2  = copy.deepcopy(coses["aMCatNLO"]["hist"].Clone("haMC2"))

    #print res2
    #print res2DR
    #print res2DS
    #print "WOLOLO", covmatdiagonal
    #print covmatres2DR
    #print covmatres2DS
    #print covmatres2aMC

    #for bin in range(1, hData2.GetNbinsX() + 1):
    #hData2.SetBinContent(bin, res2[bin - 1])
    #hData2.SetBinError(bin, r.TMath.Sqrt(covmatdiagonal[bin-1, bin-1]))
    #hDR2.SetBinContent(bin, res2DR[bin - 1])
    #hDR2.SetBinError(bin, r.TMath.Sqrt(covmatres2DR[bin-1, bin-1]))
    ##print "dif. DR y datos al cuadrao", (hDR2.GetBinContent(bin) - hData2.GetBinContent(bin))**2
    ##print "cov. de datos en este bin", covmatdiagonal[bin-1, bin-1]
    ##print "Cov. de DR en este bin", covmatres2DR[bin-1, bin-1]
    #hDS2.SetBinContent(bin, res2DS[bin - 1])
    #hDS2.SetBinError(bin, r.TMath.Sqrt(covmatres2DS[bin-1, bin-1]))
    #haMC2.SetBinContent(bin, res2aMC[bin - 1])
    #haMC2.SetBinError(bin, r.TMath.Sqrt(covmatres2aMC[bin-1, bin-1]))

    #print "\nPRUEBINA", ty
    #print 'DR - p-val.:',        hData2.KolmogorovTest(hDR2)
    ##print 'DR - p-val. (toys):', hData2.KolmogorovTest(hDR2,'X')
    #print 'DR - p-val. (chi2):', hData2.Chi2Test(hDR2,'WW')
    #print 'DS - p-val.:',        hData2.KolmogorovTest(hDS2)
    ##print 'DS - p-val. (toys):', hData2.KolmogorovTest(hDS2,'X')
    #print 'DS - p-val. (chi2):', hData2.Chi2Test(hDS2,'WW')
    #print 'aMCatNLO - p-val.:',        hData2.KolmogorovTest(haMC2)
    ##print 'aMCatNLO - p-val. (toys):', hData2.KolmogorovTest(haMC2,'X')
    #print 'aMCatNLO - p-val. (chi2):', hData2.Chi2Test(haMC2,'WW')
    #print "\n"
    #print "\nPRUEBINA PERO CON LO DE ANTES"
    #print 'DR - p-val.:',        hData.KolmogorovTest(hDR)
    ##print 'DR - p-val. (toys):', hData.KolmogorovTest(hDR,'X')
    #print 'DR - p-val. (chi2):', hData.Chi2Test(hDR,'WW')
    #print 'DS - p-val.:',        hData.KolmogorovTest(hDS)
    ##print 'DS - p-val. (toys):', hData.KolmogorovTest(hDS,'X')
    #print 'DS - p-val. (chi2):', hData.Chi2Test(hDS,'WW')
    #print 'aMCatNLO - p-val.:',        hData.KolmogorovTest(haMCatNLO)
    ##print 'aMCatNLO - p-val. (toys):', hData.KolmogorovTest(haMCatNLO,'X')
    #print 'aMCatNLO - p-val. (chi2):', hData.Chi2Test(haMCatNLO,'WW')
    #print "\n"

    #print "Cosa de Serchio pa DR\n"
    #estad = ((res2 - res2DR).dot(np.linalg.inv(covmatdiagonal))).dot((res2 - res2DR).transpose())
    #print "estad.:", estad
    #print "p-valor:", 1 - chi2.cdf(estad, hData.GetNbinsX() - 1)

    #print "\nLo mismo pero no en lo de aquello"
    #estad = ((vdata - np.array([coses["DR"]["hist"].GetBinContent(bin) for bin in range(1, coses["DR"]["hist"].GetNbinsX() + 1)], dtype = np.double)).dot(np.linalg.inv(covmat))).dot((vdata - np.array([coses["DR"]["hist"].GetBinContent(bin) for bin in range(1, coses["DR"]["hist"].GetNbinsX() + 1)], dtype = np.double)).transpose())
    #print "estad.:", estad
    #print "p-valor:", 1 - chi2.cdf(estad, hData.GetNbinsX() - 1)

    #sys.exit()

    ## Valores de atlas manualmente: (E(b))
    #ATLASdata = np.array([ 0.00438, 0.00613, 0.00474, 0.00252, 0.00103 ], dtype = np.double)
    #ATLASmc   = np.array([ 0.0054, 0.0075, 0.005, 0.003, 0.0006 ], dtype = np.double)
    #ATLAScov  = np.diag(([ 0.41, 0.34, 0.44, 0.53, 0.18 ] * ATLASdata)**2)

    #pv, es = Chi2TestForMultivarNormal(ATLASdata, ATLAScov, ATLASmc, ATLAScov)
    #print "pv:", pv, "est", es
    #sys.exit()

    for key in coses:
        vtemp = np.array([
            coses[key]["hist"].GetBinContent(bin)
            for bin in range(1, coses[key]["hist"].GetNbinsX() + 1)
        ],
                         dtype=np.double)
        print "\ncomprobacion de lo que-y metemos al alg."
        print "covmat datos bin 1", covmat[0, 0]
        print "covmat datos bin 2", covmat[1, 1]
        print "covmat datos bin 3", covmat[2, 2]
        print "covmat datos bin 4", covmat[3, 3]
        print "datos", vdata
        print "covmat MC bin 1", coses[key]["covmat"][0, 0]
        print "covmat MC bin 2", coses[key]["covmat"][1, 1]
        print "covmat MC bin 3", coses[key]["covmat"][2, 2]
        print "covmat MC bin 4", coses[key]["covmat"][3, 3]
        print "mc", vtemp
        pv, es = Chi2TestForMultivarNormal(vdata, covmat, vtemp,
                                           coses[key]["covmat"])
        coses[key]["p-value"] = pv
        coses[key]["statistic"] = es

    plot = bp.beautifulUnfoldingPlots("GOF_{vr}{s}".format(vr=var,
                                                           s="_fidunorm" *
                                                           (ty == "fidunorm")))
    plot.doRatio = False
    plot.doFit = False
    plot.plotspath = outputpath + "/"

    hDS.GetYaxis().SetRangeUser(
        0, 1.1 * max([
            hDS.GetMaximum(),
            hDR.GetMaximum(),
            haMCatNLO.GetMaximum(),
            hData.GetMaximum()
        ]))

    plot.addHisto(hDS, 'hist', 'tW DS', 'L', 'mc')
    plot.addHisto(hDR, 'hist,same', 'tW Powheg', 'L', 'mc')
    plot.addHisto(haMCatNLO, 'hist,same', 'tW aMC@NLO', 'L', 'mc')
    plot.addHisto(hData, 'hist,same', 'Data', 'L', 'mc')

    l1 = r.TLatex(
        0.65, 0.650, '#scale[0.4]{DR - p-val.:                %4.10f}' %
        coses["DR"]["p-value"])
    l2 = r.TLatex(
        0.65, 0.625, '#scale[0.4]{DR - stat.:                 %4.10f}' %
        coses["DR"]["statistic"])
    l3 = r.TLatex(
        0.65, 0.600, '#scale[0.4]{DS - p-val.:                %4.10f}' %
        coses["DS"]["p-value"])
    l4 = r.TLatex(
        0.65, 0.575, '#scale[0.4]{DS - stat.:                 %4.10f}' %
        coses["DS"]["statistic"])
    l5 = r.TLatex(
        0.65, 0.550,
        '#scale[0.4]{aMC@NLO - p-val.: %4.10f}' % coses["aMCatNLO"]["p-value"])
    l6 = r.TLatex(
        0.65, 0.525, '#scale[0.4]{aMC@NLO - stat.:  %4.10f}' %
        coses["aMCatNLO"]["statistic"])

    l1.SetNDC(True)
    l2.SetNDC(True)
    l3.SetNDC(True)
    l4.SetNDC(True)
    l5.SetNDC(True)
    l6.SetNDC(True)

    l1.Draw('same')
    l2.Draw('same')
    l3.Draw('same')
    l4.Draw('same')
    l5.Draw('same')
    l6.Draw('same')

    plot.saveCanvas("TR")
    del plot

    if not os.path.isdir(vl.gofpath): os.system("mkdir -p " + vl.gofpath)
    outfile = open(vl.gofpath + "/" + var + "_goftests_" + ty + ".txt", "w")

    outtxt = ""
    outtxt += "Results of GOF tests\n"
    outtxt += "Variable: {vr} \n".format(vr=var)
    outtxt += "=========================================\n"

    for key in ["DR", "DS", "aMCatNLO"]:
        outtxt += key + " / p-value: " + str(coses[key]["p-value"]) + "\n"
        outtxt += key + " / test statistic: " + str(
            coses[key]["statistic"]) + "\n"

    outfile.write(outtxt)
    outfile.close()
    del outfile

    print '\nDR - p-val.:', coses["DR"]["p-value"]
    print 'DR - stat.:', coses["DR"]["statistic"]
    print 'DS - p-val.:', coses["DS"]["p-value"]
    print 'DS - stat.:', coses["DS"]["statistic"]
    print 'aMC@NLO - p-val.:', coses["aMCatNLO"]["p-value"]
    print 'aMC@NLO - stat.:', coses["aMCatNLO"]["statistic"], "\n"
    del coses
    return
Esempio n. 19
0
def makeFit(task):
    varName, syst = task
    print '\n> Fitting syst.', syst, 'of the variable', varName, '\n'
    binning = vl.varList[varName]['recobinning']
    if syst == '':
        cardList = [
            'datacard_{var}_{idx}.txt'.format(var=varName, idx=idx)
            for idx in range(1, len(binning))
        ]
    else:
        cardList = [
            'datacard_{var}_{sys}_{idx}.txt'.format(var=varName,
                                                    sys=syst,
                                                    idx=idx)
            for idx in range(1, len(binning))
        ]

    if verbose: print 'Cardlist:', cardList
    os.system(
        'cd temp/{var}_{sys}; combineCards.py {allCards} > {outCard}; cd -'.
        format(allCards=' '.join(cardList),
               var=varName,
               sys=syst,
               outCard='datacard_{var}_{sys}.txt'.format(var=varName,
                                                         sys=syst)))
    if verbose:
        print 'cd temp/{var}_{sys}; combineCards.py {allCards} > {outCard}; cd -'.format(
            allCards=' '.join(cardList),
            var=varName,
            sys=syst,
            outCard='datacard_{var}_{sys}.txt'.format(var=varName, sys=syst))
    physicsModel = 'text2workspace.py -m 125 -P HiggsAnalysis.CombinedLimit.PhysicsModel:multiSignalModel '

    for idx in range(1, len(binning)):
        physicsModel = physicsModel + "--PO 'map=.*/tW_{idx}:r_tW_{idx}[1,0,10]' ".format(
            idx=idx)
    physicsModel = physicsModel + 'temp/{var}_{sys}/datacard_{var}_{sys}.txt -o temp/{var}_{sys}/comb_fit_{var}_{sys}.root'.format(
        var=varName, sys=syst)

    if not os.path.isdir('temp/{var}_{sys}/fitdiagnostics'.format(var=varName,
                                                                  sys=syst)):
        os.system('mkdir -p temp/{var}_{sys}/fitdiagnostics'.format(
            var=varName, sys=syst))

    os.system(physicsModel)
    if verbose: print physicsModel
    #os.system('combine  -M FitDiagnostics --out temp/{var}_{sys}/fitdiagnostics  temp/{var}_{sys}/comb_fit_{var}_{sys}.root --saveWorkspace -n {var}_{sys} --X-rtd MINIMIZER_MaxCalls=5000000'.format(var=varName,sys=syst))
    if verbose:
        print 'combine  -M FitDiagnostics --out temp/{var}_{sys}/fitdiagnostics  temp/{var}_{sys}/comb_fit_{var}_{sys}.root --saveWorkspace -n {var}_{sys} --X-rtd MINIMIZER_analytic --X-rtd MINIMIZER_MaxCalls=5000000 --saveShapes'.format(
            var=varName, sys=syst)
    os.system(
        'combine  -M FitDiagnostics --out temp/{var}_{sys}/fitdiagnostics  temp/{var}_{sys}/comb_fit_{var}_{sys}.root --saveWorkspace -n {var}_{sys} --X-rtd MINIMIZER_analytic --X-rtd MINIMIZER_MaxCalls=5000000 --saveShapes'
        .format(var=varName, sys=syst))

    # Ahora recogemos la virutilla
    tfile = r.TFile.Open(
        'temp/{var}_{sys}/fitdiagnostics/fitDiagnostics{var}_{sys}.root'.
        format(var=varName, sys=syst))
    tfile2 = r.TFile.Open(
        'higgsCombine{var}_{sys}.FitDiagnostics.mH120.root'.format(var=varName,
                                                                   sys=syst))
    fitsb = tfile.Get('tree_fit_sb')
    fitsb.GetEntry(0)
    fitstatus = fitsb.fit_status
    if fitstatus == -1:
        raise RuntimeError(
            'Fit of variable {var} with syst. {sys} has not converged (fit status value: {fitv})'
            .format(var=varName, sys=syst, fitv=fitstatus))
    elif fitstatus != 0:
        wr.warn(
            'Fit of variable {var} with syst. {sys} has a nonzero fit status value: {fitv}'
            .format(var=varName, sys=syst, fitv=fitstatus),
            UserWarning,
            stacklevel=2)

    fitResult = tfile.Get('fit_s')
    if verbose: fitResult.Print()
    covar = fitResult.correlationHist('covar')

    # Tambien necesitamos el workspace
    w = tfile2.Get('w')
    poiList = r.RooArgList('poiList')
    for i in range(1, len(binning)):
        var = w.var('r_tW_%d' % i)
        poiList.add(var)
    cov = fitResult.reducedCovarianceMatrix(poiList)

    results = {}
    count = 0
    for var in fitResult.floatParsFinal():
        count = count + 1
        #if not 'r_tW' in var.GetName(): continue # se guarda todo
        results[var.GetName()] = [
            var.getVal(), var.getErrorLo(),
            var.getErrorHi()
        ]
        if count == fitResult.floatParsFinal().getSize(): break

    print '> Plotting covariance matrix'
    c = r.TCanvas(
        'c', '{var}_{sys} covariance matrix'.format(var=varName, sys=syst),
        1200, 1200)
    c.SetTopMargin(0.06)
    c.SetRightMargin(0.06)
    covar.GetXaxis().SetTitle('Free parameters of the fit')
    covar.GetXaxis().SetLabelSize(0.01)
    covar.GetXaxis().SetTitleSize(0.01)
    covar.GetXaxis().SetTitleOffset(3)
    covar.GetYaxis().SetTitle('Free parameters of the fit')
    covar.GetYaxis().SetLabelSize(0.01)
    covar.GetYaxis().SetTitleSize(0.01)
    covar.GetYaxis().SetTitleOffset(3)
    covar.SetTitle('{var}_{sys} covariance matrix'.format(var=varName,
                                                          sys=syst))
    covar.SetStats(False)
    covar.Draw('colz')
    if not os.path.isdir('results/covmatrices'):
        os.system('mkdir -p results/covmatrices')

    c.SaveAs('results/covmatrices/{var}_{sys}.pdf'.format(var=varName,
                                                          sys=syst))
    c.SaveAs('results/covmatrices/{var}_{sys}.png'.format(var=varName,
                                                          sys=syst))
    c.SaveAs('results/covmatrices/{var}_{sys}.root'.format(var=varName,
                                                           sys=syst))
    del c
    tfile2.Close()
    tfile.Close()

    toKeep = []
    for p in [('r_tW', 'tW'), ('DY', 'DY'), ('VVttbarV', 'VV+ttV'),
              ('ttbar', 't#bar{t}')]:
        graph = r.TGraphAsymmErrors(len(binning) - 1)
        graph.SetName(p[0])
        for i in range(1, len(binning)):
            if '%s_%d' % (p[0], i) not in results:
                graph.SetPoint(i - 1, (binning[i - 1] + binning[i]) / 2, 0)
                graph.SetPointError(i - 1, (binning[i] - binning[i - 1]) / 2,
                                    (binning[i] - binning[i - 1]) / 2, 0, 0)
            else:
                graph.SetPoint(i - 1, (binning[i - 1] + binning[i]) / 2,
                               results['%s_%d' % (p[0], i)][0])
                graph.SetPointError(i - 1, (binning[i] - binning[i - 1]) / 2,
                                    (binning[i] - binning[i - 1]) / 2,
                                    -results['%s_%d' % (p[0], i)][1],
                                    results['%s_%d' % (p[0], i)][2])
        toKeep.append((graph, p[1]))

    plot = bp.beautifulUnfoldingPlots('srs_{var}_{sys}'.format(var=varName,
                                                               sys=syst))
    plot.addHistoInPad(len(toKeep), toKeep[0][0], 'AP', toKeep[0][1], '')
    plot.addTLatex(0.7, 1 - 0.2, toKeep[0][1])
    plot.plotspath = "results/srs/"
    for p in range(1, len(toKeep)):
        plot.addHistoInPad(p + 1, toKeep[p][0], 'AP', toKeep[p][1], '')
        #toKeep[p][0].GetYaxis().SetTitle('Post/pre')
        #toKeep[p][0].GetYaxis().CenterTitle(True)

        plot.addTLatex(0.7, 1 - 1.23 * float(p) / (len(toKeep) + 1) - 0.2,
                       toKeep[p][1])
    setattr(plot, 'noCMS', True)
    plot.saveCanvas('TR', '', False)

    # Put results into histos
    outHisto = r.TH1F('hFitResult_%s_%s' % (varName, syst), '',
                      len(binning) - 1,
                      array('d', vl.varList[varName]['recobinning']))
    for i in range(1, len(binning)):
        if syst == '':
            card = r.TFile.Open('temp/{var}_/forCards_{var}_{ind}.root'.format(
                var=varName, ind=i))
        else:
            card = r.TFile.Open(
                'temp/{var}_{sys}/forCards_{var}_{sys}_{ind}.root'.format(
                    var=varName, sys=syst, ind=i))

        divquantity = 1
        if vl.doxsec: divquantity = vl.Lumi * 1000

        results['r_tW_%d' % i][0] = results['r_tW_%d' % i][0] * card.Get(
            'tW_%d' % i).Integral() / divquantity
        results['r_tW_%d' % i][1] = results['r_tW_%d' % i][1] * card.Get(
            'tW_%d' % i).Integral() / divquantity
        results['r_tW_%d' % i][2] = results['r_tW_%d' % i][2] * card.Get(
            'tW_%d' % i).Integral() / divquantity
        outHisto.SetBinContent(i, results['r_tW_%d' % i][0])
        card.Close()

    errors = outHisto.Clone('hFitResult_forPlotting_%s_%s' % (varName, syst))
    for i in range(1, len(binning)):
        errors.SetBinContent(
            i,
            results['r_tW_%d' % i][2])  # Reminder: they are symmetric. Always.
        errors.SetBinError(i, results['r_tW_%d' % i][2])

    # Put covariance matrix into yield parametrization instead of
    # cross section parametrization
    # Also the thing shouuld be in a th2
    hCov = r.TH2F('hCovar_%s_%s' % (varName, syst), '',
                  len(binning) - 1, -0.5,
                  len(binning) - 1.5,
                  len(binning) - 1, -0.5,
                  len(binning) - 1.5)

    for i in range(1, len(binning)):
        for j in range(1, len(binning)):
            if syst == '':
                cardx = r.TFile.Open(
                    'temp/{var}_/forCards_{var}_{ind}.root'.format(var=varName,
                                                                   ind=i))
                cardy = r.TFile.Open(
                    'temp/{var}_/forCards_{var}_{ind}.root'.format(var=varName,
                                                                   ind=j))
            else:
                cardx = r.TFile.Open(
                    'temp/{var}_{sys}/forCards_{var}_{sys}_{ind}.root'.format(
                        var=varName, sys=syst, ind=i))
                cardy = r.TFile.Open(
                    'temp/{var}_{sys}/forCards_{var}_{sys}_{ind}.root'.format(
                        var=varName, sys=syst, ind=j))

            scale = 1
            if vl.doxsec: scale = 1 / vl.Lumi / 1000

            normx = cardx.Get('tW_%d' % i).Integral() * scale
            normy = cardy.Get('tW_%d' % j).Integral() * scale

            cov[i - 1][j - 1] = cov[i - 1][j - 1] * normx * normy

            hCov.SetBinContent(hCov.GetBin(i, j), cov[i - 1][j - 1])

            cardx.Close()
            cardy.Close()

    print '\n> Syst.', syst, 'of the variable', varName, 'fitted!\n'
    return [outHisto, errors, hCov]
Esempio n. 20
0
                max(
                    map(abs, [
                        allResults[(binDn, binUp)][key][0] -
                        allResults[(binDn, binUp)][key][1],
                        allResults[(binDn, binUp)][key][0] -
                        allResults[(binDn, binUp)][key][2]
                    ])))

outCard = open('dummy_card_template.txt').read()
print outCard
outCard = outCard.format(obs=histoSyst[''].Integral(),
                         tW=histoSyst[''].Integral(),
                         ref=name)
out = open('temp/dummy_card_%s.txt' % name, 'w')
out.write(outCard)

cardInput = ROOT.TFile.Open('temp/cardFile_{ref}.root'.format(ref=name),
                            'recreate')

for key in histoSyst:
    histoSyst[key].Write()
cardInput.Close()

print "> Saving folded-space histogram of the variable"
plot = beautifulUnfoldingPlots.beautifulUnfoldingPlots(name)
plot.addHisto(histo, 'hist', 'Folded distribution', 'L')
plot.plotspath = "results/"
plot.saveCanvas('TR', '_folded')

print "> Fitting procedure done!"
Esempio n. 21
0
    def doUnfoldingForAllNuis(self):
        allHistos = {}
        tauval = 0.
        self.prepareAllHelpers()
        if self.doRegularisation:
            print '> Performing regularisation...'
            self.doLCurveScan()
            tauval = self.helpers[''].tunfolder.GetTau()

        if verbose: print '> Unfolding nominal distribution'
        self.helpers[''].tunfolder.DoUnfold(tauval)
        nominal = copy.deepcopy(self.helpers[''].tunfolder.GetOutput(self.var))

        for nuis in self.theoSysts:
            if verbose:
                print '> Unfolding distribution of {sys} systematic'.format(
                    sys=nuis)
            self.helpers[nuis].tunfolder.DoUnfold(tauval)
            allHistos[nuis] = self.helpers[nuis].tunfolder.GetOutput(self.var +
                                                                     '_' +
                                                                     nuis)

        nominal_withErrors = ep.propagateHistoAsym(nominal, allHistos,
                                                   self.doColorEnvelope)
        plot = bp.beautifulUnfoldingPlots(self.var)
        plot.doRatio = True
        plot.plotspath = self.plotspath

        nominal.SetMarkerStyle(r.kFullCircle)
        nominal_withErrors[0].SetFillColorAlpha(r.kBlue, 0.35)
        nominal_withErrors[0].SetLineColor(r.kBlue)
        nominal_withErrors[0].SetFillStyle(1001)

        if self.doSanityCheck:
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information does not exist'
                )
            tmptfile = r.TFile.Open(
                'temp/{var}_/ClosureTest_{var}.root'.format(var=self.var))
            tru = copy.deepcopy(tmptfile.Get('tW'))
            tru.SetLineWidth(2)
            tru.SetLineColor(bp.colorMap[0])
            if not os.path.isfile(
                    'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                        var=self.var)):
                raise RuntimeError(
                    'The rootfile with the generated information from an aMCatNLO sample does not exist'
                )
            tmptfile2 = r.TFile.Open(
                'temp/{var}_/ClosureTest_aMCatNLO_{var}.root'.format(
                    var=self.var))
            aMCatNLO = copy.deepcopy(tmptfile2.Get('tW'))
            aMCatNLO.SetLineWidth(2)
            aMCatNLO.SetLineColor(r.kAzure)

            plot.addHisto(nominal_withErrors, 'hist', 'Total unc.', 'F', 'unc')
            plot.addHisto(tru, 'L,same', 'tW Powheg', 'L', 'mc')
            plot.addHisto(aMCatNLO, 'L,same', 'tW aMCatNLO', 'L', 'mc')
            plot.addHisto(nominal, 'P,E,same', vl.labellegend, 'P', 'data')
            plot.saveCanvas('TR')
            tmptfile.Close()
            tmptfile2.Close()
        else:
            plot.addHisto(nominal_withErrors, 'E2', 'Total unc.', 'F')
            plot.addHisto(nominal, 'P,same', vl.labellegend, 'P', 'data')
            plot.saveCanvas('TR')

        del plot
        plot2 = bp.beautifulUnfoldingPlots(self.var + 'uncertainties')
        uncList = ep.getUncList(nominal, allHistos, self.doColorEnvelope)[:5]

        incmax = []
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            if nominal_withErrors[1].GetBinError(
                    bin) > nominal_withErrors[0].GetBinContent(bin):
                incmax.append(
                    max([
                        nominal_withErrors[0].GetBinError(bin),
                        nominal_withErrors[0].GetBinContent(bin)
                    ]))
            else:
                incmax.append(
                    max([
                        nominal_withErrors[0].GetBinError(bin),
                        nominal_withErrors[1].GetBinError(bin)
                    ]))

        maxinctot = 0
        hincmax = nominal_withErrors[0].Clone('hincmax')
        for bin in range(1, nominal_withErrors[0].GetNbinsX() + 1):
            hincmax.SetBinContent(bin,
                                  incmax[bin - 1] / hincmax.GetBinContent(bin))
            hincmax.SetBinError(bin, 0)
            if (hincmax.GetBinContent(bin) > maxinctot):
                maxinctot = hincmax.GetBinContent(bin)

        hincmax.SetLineColor(r.kBlack)
        hincmax.SetLineWidth(2)
        hincmax.SetFillColorAlpha(r.kBlue, 0)

        if (maxinctot >= 0.9):
            if maxinctot >= 5:
                uncList[0][1].GetYaxis().SetRangeUser(0, 5)
            else:
                uncList[0][1].GetYaxis().SetRangeUser(0, maxinctot + 0.1)

        else:
            uncList[0][1].GetYaxis().SetRangeUser(0, 0.9)

        for i in range(5):
            uncList[i][1].SetLineColor(vl.colorMap[uncList[i][0]])
            uncList[i][1].SetLineWidth(2)
            plot2.addHisto(uncList[i][1], 'H,same' if i else 'H',
                           uncList[i][0], 'L')

        plot2.addHisto(hincmax, 'H,same', 'Total', 'L')
        plot2.plotspath = self.plotspath
        plot2.saveCanvas('TR')
Esempio n. 22
0
def GiveMeMyGoodGOFTests(var):
    f1 = r.TFile.Open(
        pathtothings + "{vr}_/unfOutput_{vr}.root".format(vr=var), "read")
    f2 = r.TFile.Open(
        pathtothings + "{vr}_/ClosureTest_{vr}.root".format(vr=var), "read")
    f3 = r.TFile.Open(
        pathtothings + "{vr}_/ClosureTest_DS_{vr}.root".format(vr=var), "read")
    f4 = r.TFile.Open(
        pathtothings + "{vr}_/ClosureTest_aMCatNLO_{vr}.root".format(vr=var),
        "read")
    f5 = r.TFile.Open(pathtothings + "{vr}_/CovMat_{vr}.root".format(vr=var),
                      "read")

    hData = copy.deepcopy(f1.Get(var).Clone("hData"))
    hDataUp = copy.deepcopy(f1.Get("nom0").Clone("hDataUp"))
    hDataDown = copy.deepcopy(f1.Get("nom1").Clone("hDataDown"))
    hDataCov = copy.deepcopy(f5.Get("finalmat").Clone("hDataCov"))
    hDR = copy.deepcopy(f2.Get("tW").Clone("hDR"))
    hDS = copy.deepcopy(f3.Get("tW").Clone("hDS"))
    haMCatNLO = copy.deepcopy(f4.Get("tW").Clone("haMCatNLO"))

    f1.Close()
    f2.Close()
    f3.Close()
    f4.Close()
    f5.Close()

    hData.SetFillColor(r.kWhite)
    hData.SetLineWidth(2)
    hData.SetLineColor(r.kBlack)
    hData.SetLineStyle(2)
    for bin in range(1, hData.GetNbinsX() + 1):
        hData.SetBinError(
            bin, max([hDataUp.GetBinError(bin),
                      hDataDown.GetBinError(bin)]))

    hDR.SetFillColor(r.kWhite)
    hDR.SetLineWidth(2)
    hDR.SetLineColor(r.kRed)

    hDS.SetFillColor(r.kWhite)
    hDS.SetLineWidth(2)
    hDS.SetLineColor(r.kGreen)
    hDS.GetXaxis().SetTitle(vl.varList[var]['xaxis'])

    haMCatNLO.SetFillColor(r.kWhite)
    haMCatNLO.SetLineWidth(2)
    haMCatNLO.SetLineColor(r.kBlue)

    covmat = np.zeros((hData.GetNbinsX(), hData.GetNbinsX()))

    for i in range(1, hData.GetNbinsX() + 1):
        for j in range(1, hData.GetNbinsX() + 1):
            covmat[i - 1, j - 1] = hDataCov.GetBinContent(i, j)

    DRcovmat = np.diag(
        [hDR.GetBinError(bin)**2 for bin in range(1,
                                                  hDR.GetNbinsX() + 1)])
    DScovmat = np.diag(
        [hDS.GetBinError(bin)**2 for bin in range(1,
                                                  hDS.GetNbinsX() + 1)])
    aMCatNLOcovmat = np.diag([
        haMCatNLO.GetBinError(bin)**2
        for bin in range(1,
                         haMCatNLO.GetNbinsX() + 1)
    ])

    coses = {}
    coses["DR"] = {
        "hist": copy.deepcopy(hDR),
        "covmat": copy.deepcopy(DRcovmat)
    }
    coses["DS"] = {
        "hist": copy.deepcopy(hDS),
        "covmat": copy.deepcopy(DScovmat)
    }
    coses["aMCatNLO"] = {
        "hist": copy.deepcopy(haMCatNLO),
        "covmat": copy.deepcopy(aMCatNLOcovmat)
    }

    vdata = [
        hData.GetBinContent(bin) for bin in range(1,
                                                  hData.GetNbinsX() + 1)
    ]

    for key in coses:
        vtemp = [
            coses[key]["hist"].GetBinContent(bin)
            for bin in range(1, coses[key]["hist"].GetNbinsX() + 1)
        ]
        pv, es = Chi2TestForMultivarNormal(vdata, covmat, vtemp,
                                           coses[key]["covmat"])
        coses[key]["p-value"] = pv
        coses[key]["statistic"] = es

    plot = bp.beautifulUnfoldingPlots("GOF_{vr}".format(vr=var))
    plot.doRatio = False
    plot.doFit = False
    plot.plotspath = outputpath + "/"

    plot.addHisto(hDS, 'hist', 'tW DS', 'L', 'mc')
    plot.addHisto(hDR, 'hist,same', 'tW Powheg', 'L', 'mc')
    plot.addHisto(haMCatNLO, 'hist,same', 'tW aMC@NLO', 'L', 'mc')
    plot.addHisto(hData, 'hist,same', 'Data', 'L', 'mc')

    l1 = r.TLatex(0.65, 0.650,
                  '#scale[0.4]{DR - p-val.: %4.7f}' % coses["DR"]["p-value"])
    l2 = r.TLatex(0.65, 0.625,
                  '#scale[0.4]{DR - stat.: %4.7f}' % coses["DR"]["statistic"])
    l3 = r.TLatex(0.65, 0.600,
                  '#scale[0.4]{DS - p-val.: %4.7f}' % coses["DS"]["p-value"])
    l4 = r.TLatex(0.65, 0.575,
                  '#scale[0.4]{DS - stat.: %4.7f}' % coses["DS"]["statistic"])
    l5 = r.TLatex(
        0.65, 0.550,
        '#scale[0.4]{aMC@NLO - p-val.: %4.7f}' % coses["aMCatNLO"]["p-value"])
    l6 = r.TLatex(
        0.65, 0.525,
        '#scale[0.4]{aMC@NLO - stat.: %4.7f}' % coses["aMCatNLO"]["statistic"])

    l1.SetNDC(True)
    l2.SetNDC(True)
    l3.SetNDC(True)
    l4.SetNDC(True)
    l5.SetNDC(True)
    l6.SetNDC(True)

    l1.Draw('same')
    l2.Draw('same')
    l3.Draw('same')
    l4.Draw('same')
    l5.Draw('same')
    l6.Draw('same')

    plot.saveCanvas("TR")
    del plot

    print 'DR - p-val.:', coses["DR"]["p-value"]
    print 'DR - stat.:', coses["DR"]["statistic"]
    print 'DS - p-val.:', coses["DS"]["p-value"]
    print 'DS - stat.:', coses["DS"]["statistic"]
    print 'aMC@NLO - p-val.:', coses["aMCatNLO"]["p-value"]
    print 'aMC@NLO - stat.:', coses["aMCatNLO"]["statistic"]
    return
Esempio n. 23
0
del allHistos[varName]

savetfile = r.TFile("temp/{var}_/fidOutput_{var}.root".format(var=varName),
                    "recreate")
nominal.Write()
for key in allHistos:
    allHistos[key].Write()
savetfile.Close()
del savetfile

nominal_withErrors = ep.propagateHistoAsym(nominal,
                                           allHistos,
                                           True,
                                           False,
                                           doSym=vl.doSym)
plot = bp.beautifulUnfoldingPlots(varName + "_fiducial")
plot.doRatio = True
plot.doFit = False
plot.plotspath = 'results/'
plot.doPreliminary = vl.doPre

nominal.SetMarkerStyle(r.kFullCircle)
nominal.SetLineColor(r.kBlack)
nominal_withErrors[0].SetFillColorAlpha(r.kBlue, 0.35)
nominal_withErrors[0].SetLineColor(0)
nominal_withErrors[0].SetFillStyle(1001)

savetfile2 = r.TFile("temp/{var}_/fidOutput_{var}.root".format(var=varName),
                     "update")
nom0 = copy.deepcopy(nominal_withErrors[0].Clone("nom0"))
nom1 = copy.deepcopy(nominal_withErrors[1].Clone("nom1"))
Esempio n. 24
0
if not vl.asimov:
    asimov.Write()
for syst in sysList:
    variations[syst].Write()
out.Close()

scaleval = 1 / vl.Lumi / 1000 if vl.doxsec else 1
nominal.Scale(scaleval)
for key in variations:
    variations[key].Scale(scaleval)

nominal_withErrors = ep.propagateHistoAsym(nominal,
                                           variations,
                                           True,
                                           doSym=vl.doSym)
plot = bp.beautifulUnfoldingPlots('{var}_folded'.format(var=varName))
plot.doRatio = True
plot.doFit = False
plot.plotspath = "results/"
plot.doPreliminary = vl.doPre

nominal.SetMarkerStyle(r.kFullCircle)
nominal.GetXaxis().SetNdivisions(505, True)

nominal_withErrors[0].SetFillColorAlpha(r.kBlue, 0.35)
nominal_withErrors[0].SetLineColor(0)
nominal_withErrors[0].SetFillStyle(1001)

if "legpos_fold" in vl.varList[varName]:
    legloc = vl.varList[varName]["legpos_fold"]
else:
Esempio n. 25
0
def GiveMeMyGOFTests(var):
    f1 = r.TFile.Open(
        pathtothings + "{vr}_/unfOutput_{vr}.root".format(vr=var), "read")
    f2 = r.TFile.Open(
        pathtothings + "{vr}_/ClosureTest_{vr}.root".format(vr=var), "read")
    f3 = r.TFile.Open(
        pathtothings + "{vr}_/ClosureTest_DS_{vr}.root".format(vr=var), "read")
    f4 = r.TFile.Open(
        pathtothings + "{vr}_/ClosureTest_aMCatNLO_{vr}.root".format(vr=var),
        "read")

    hData = copy.deepcopy(f1.Get(var).Clone("hData"))
    hDataUp = copy.deepcopy(f1.Get("nom0").Clone("hDataUp"))
    hDataDown = copy.deepcopy(f1.Get("nom1").Clone("hDataDown"))
    hDR = copy.deepcopy(f2.Get("tW").Clone("hDR"))
    hDS = copy.deepcopy(f3.Get("tW").Clone("hDS"))
    haMCatNLO = copy.deepcopy(f4.Get("tW").Clone("haMCatNLO"))

    f1.Close()
    f2.Close()
    f3.Close()
    f4.Close()

    hData.SetFillColor(r.kWhite)
    hData.SetLineWidth(2)
    hData.SetLineColor(r.kBlack)
    hData.SetLineStyle(2)
    for bin in range(1, hData.GetNbinsX() + 1):
        hData.SetBinError(
            bin, max([hDataUp.GetBinError(bin),
                      hDataDown.GetBinError(bin)]))

    hDR.SetFillColor(r.kWhite)
    hDR.SetLineWidth(2)
    hDR.SetLineColor(r.kRed)

    hDS.SetFillColor(r.kWhite)
    hDS.SetLineWidth(2)
    hDS.SetLineColor(r.kGreen)
    hDS.GetXaxis().SetTitle(vl.varList[var]['xaxis'])

    haMCatNLO.SetFillColor(r.kWhite)
    haMCatNLO.SetLineWidth(2)
    haMCatNLO.SetLineColor(r.kBlue)

    plot = bp.beautifulUnfoldingPlots("GOF_{vr}".format(vr=var))
    plot.doRatio = False
    plot.doFit = False
    plot.plotspath = outputpath + "/"

    plot.addHisto(hDS, 'hist', 'tW DS', 'L', 'mc')
    plot.addHisto(hDR, 'hist,same', 'tW Powheg', 'L', 'mc')
    plot.addHisto(haMCatNLO, 'hist,same', 'tW aMC@NLO', 'L', 'mc')
    plot.addHisto(hData, 'hist,same', 'Data', 'L', 'mc')

    l1 = r.TLatex(0.65, 0.700,
                  '#scale[0.4]{DR - KS: %4.7f}' % hData.KolmogorovTest(hDR))
    l2 = r.TLatex(
        0.65, 0.675,
        '#scale[0.4]{DR - KS (toys): %4.7f}' % hData.KolmogorovTest(hDR, 'X'))
    l3 = r.TLatex(
        0.65, 0.650,
        '#scale[0.4]{DR - #chi^{2}: %4.7f}' % hData.Chi2Test(hDR, 'WW'))
    l4 = r.TLatex(0.65, 0.625,
                  '#scale[0.4]{DS - KS: %4.7f}' % hData.KolmogorovTest(hDS))
    l5 = r.TLatex(
        0.65, 0.600,
        '#scale[0.4]{DS - KS (toys): %4.7f}' % hData.KolmogorovTest(hDS, 'X'))
    l6 = r.TLatex(
        0.65, 0.575,
        '#scale[0.4]{DS - #chi^{2}: %4.7f}' % hData.Chi2Test(hDS, 'WW'))
    l7 = r.TLatex(
        0.65, 0.550,
        '#scale[0.4]{aMC@NLO - KS: %4.7f}' % hData.KolmogorovTest(haMCatNLO))
    l8 = r.TLatex(
        0.65, 0.525, '#scale[0.4]{aMC@NLO - KS (toys): %4.7f}' %
        hData.KolmogorovTest(haMCatNLO, 'X'))
    l9 = r.TLatex(
        0.65, 0.500, '#scale[0.4]{aMC@NLO - #chi^{2}: %4.7f}' %
        hData.Chi2Test(haMCatNLO, 'WW'))

    l1.SetNDC(True)
    l2.SetNDC(True)
    l3.SetNDC(True)
    l4.SetNDC(True)
    l5.SetNDC(True)
    l6.SetNDC(True)
    l7.SetNDC(True)
    l8.SetNDC(True)
    l9.SetNDC(True)

    l1.Draw('same')
    l2.Draw('same')
    l3.Draw('same')
    l4.Draw("same")
    l5.Draw("same")
    l6.Draw("same")
    l7.Draw("same")
    l8.Draw("same")
    l9.Draw("same")

    plot.saveCanvas("TR")
    del plot

    print 'DR - KS:', hData.KolmogorovTest(hDR)
    print 'DR - KS (toys):', hData.KolmogorovTest(hDR, 'X')
    print 'DR - chi2:', hData.Chi2Test(hDR, 'WW')
    print 'DS - KS:', hData.KolmogorovTest(hDS)
    print 'DS - KS (toys):', hData.KolmogorovTest(hDS, 'X')
    print 'DS - chi2:', hData.Chi2Test(hDS, 'WW')
    print 'aMC@NLO - KS:', hData.KolmogorovTest(haMCatNLO)
    print 'aMC@NLO - KS (toys):', hData.KolmogorovTest(haMCatNLO, 'X')
    print 'aMC@NLO - chi2:', hData.Chi2Test(haMCatNLO, 'WW')
    return