Exemplo n.º 1
0
                   'recreate')
nominal.Write()

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]:
Exemplo n.º 2
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')
Exemplo n.º 3
0
out = r.TFile.Open('temp/{var}/cutOutput_{var}.root'.format(var = varName), 'recreate')
nominal.Write()

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)
plot                = bp.beautifulUnfoldingPlots('{var}_folded'.format(var = varName))
plot.doRatio        = True
plot.doFit          = False
plot.plotspath      = "results/"

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

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

if "legpos_fold" in vl.varList[varName]: legloc = vl.varList[varName]["legpos_fold"]
else:                                    legloc = "TR"
Exemplo n.º 4
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
Exemplo n.º 5
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
Exemplo n.º 6
0
nominal = copy.deepcopy(dirfinal[varName])
allHistos = {key.replace(varName + "_", ""): dirfinal[key] for key in dirfinal}
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),
Exemplo n.º 7
0
errors = copy.deepcopy(fitinfo.Get('hFitResult_forPlotting_%s_' % varName))

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(
Exemplo n.º 8
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')