for region in regionsOrder:
     analysis = regions[region]
     print "\nRegion:",region,"\n"
     #make output directory
     outdir = 'Plots/'+tag+'/'+region
     os.system('mkdir -p '+outdir)
     #set up analysis
     (xbins,cols) = analysis.unrollBins
     auxSFs = razorWeights.getNJetsSFs(analysis, jetName=analysis.jetVar)
     razorWeights.getPhotonPuritySFs(auxSFs)
     #perform analysis
     hists = makeControlSampleHistsForAnalysis( analysis, plotOpts=plotOpts, sfHists=sfHists,
             sfVars=sfVars, printdir=outdir, auxSFs=auxSFs, btags=analysis.nbMin,
             dataDrivenQCD=True, debugLevel=debugLevel, noFill=args.noFill )
     #compute scale factors
     appendScaleFactors( region+"MR", hists, sfHists, lumiData=analysis.lumi, 
             var="MR_NoPho", debugLevel=debugLevel, signifThreshold=1.0, printdir=outdir )
     appendScaleFactors( region+"Rsq", hists, sfHists, lumiData=analysis.lumi, 
             var="Rsq_NoPho", debugLevel=debugLevel, signifThreshold=1.0, printdir=outdir )
     #export histograms
     macro.exportHists( hists, outFileName='controlHistograms'+region+'.root',
             outDir=outdir, debugLevel=debugLevel )
     if not args.noSave:
         #write out scale factors
         outfile = rt.TFile(
                 "data/ScaleFactors/RazorMADD2015/RazorGJetsBTagClosureTests_%s.root"%(tag),
                 "UPDATE")
         print "Writing scale factor histogram",sfHists[region+"MR"].GetName(),"to file"
         print "Writing scale factor histogram",sfHists[region+"Rsq"].GetName(),"to file"
         outfile.cd()
         sfHists[region+"MR"].Write( sfHists[region+"MR"].GetName() )
         sfHists[region+"Rsq"].Write( sfHists[region+"Rsq"].GetName() )
                gjets=True)
        bclosure.adjustForRegionBInclusive(analysis, sfHists, auxSFs)
        bclosure.adjustForRegionBInclusive(analysis, sfHists, auxSFs, gjets=True)
        #use the correct set of scale factors
        if 'NoSFs' in region:
            sfHistsToUse = {}
            auxSFs = { proc:{} for proc in auxSFs }
        else:
            sfHistsToUse = sfHists
        #perform analysis
        hists = makeControlSampleHistsForAnalysis( analysis, plotOpts=plotOpts, sfHists=sfHistsToUse,
            sfVars=sfVars, printdir=outdir, auxSFs=auxSFs, debugLevel=debugLevel, noFill=args.noFill )
        for v in ['MR', 'Rsq']:
            tmpSFHists = copy.copy(sfHists)
            del tmpSFHists["DYJetsInv"]
            appendScaleFactors("DYJetsInv", hists, tmpSFHists, lumiData=analysis.lumi, 
                debugLevel=debugLevel, var=v+'_NoZ', printdir=outdir)
            histName = region+v+'ScaleFactors'
            tmpSFHists['DYJetsInv'].SetName(histName)
            if not args.noSave:
                print "Writing histogram",tmpSFHists["DYJetsInv"].GetName(),"to file"
                outfile.cd()
                tmpSFHists["DYJetsInv"].Write(histName)

        #in the first pass, update the normalization of the G+jets scale factor histogram
        if updateNorm:
            dataNorm = hists["Data"]["1"].GetBinContent(1)
            dataNormErr = hists["Data"]["1"].GetBinError(1)
            mcNorm = 0
            mcNormErr = 0
            for proc in analysis.samples:
                mcNorm += hists[proc]["1"].GetBinContent(1)
        #prepare analysis
        auxSFs = razorWeights.getNJetsSFs(analysis, jetName='NJets40')
        #perform analysis
        hists = makeControlSampleHistsForAnalysis(analysis,
                                                  plotOpts=plotOpts,
                                                  sfHists=sfHists,
                                                  printdir=outdir,
                                                  auxSFs=auxSFs,
                                                  debugLevel=debugLevel)
        #record discrepancies > 1 sigma
        tmpSFHists = copy.copy(sfHists)
        if 'TTJets2L' in tmpSFHists: del tmpSFHists["TTJets2L"]
        appendScaleFactors("TTJets2L",
                           hists,
                           tmpSFHists,
                           lumiData=analysis.lumi,
                           debugLevel=debugLevel,
                           signifThreshold=1.0,
                           printdir=outdir)
        #write out scale factors
        print "Writing histogram", tmpSFHists["TTJets2L"].GetName(), "to file"
        outfile.cd()
        tmpSFHists["TTJets2L"].Write(region + "ScaleFactors")
        #export histograms
        macro.exportHists(hists,
                          outFileName='controlHistograms' + region + '.root',
                          outDir=outdir,
                          debugLevel=debugLevel)

    outfile.Close()
                                                  sfVars=sfVars,
                                                  printdir=outdir,
                                                  auxSFs=auxSFs,
                                                  btags=analysis.nbMin,
                                                  debugLevel=debugLevel,
                                                  noFill=args.noFill)

        sfHistName = getSFHistName(analysis)
        sfProcs = ['TTJets', 'WJets']
        sfHistsTmp = sfHists.copy()
        if 'MRCorr' in region:
            sfHistName = sfHistName.replace('MR', 'Rsq')
            appendScaleFactors(sfProcs,
                               hists,
                               sfHistsTmp,
                               lumiData=analysis.lumi,
                               var="Rsq",
                               useUncertainty=True,
                               debugLevel=debugLevel,
                               printdir=outdir)
        else:
            appendScaleFactors(sfProcs,
                               hists,
                               sfHistsTmp,
                               lumiData=analysis.lumi,
                               var="MR",
                               debugLevel=debugLevel,
                               printdir=outdir)
        sfHists[sfHistName] = sfHistsTmp['_'.join(sfProcs)]
        sfHists[sfHistName].SetName(sfHistName)
        if not args.noSave:
            macro.exportHists(hists,
        if args.njets80Cut:
            njets80Var = "NJets80"
            if region == 'GJetsInv':
                njets80Var += '_NoPho'
            analysis.cutsData += " && %s >= 2"%(njets80Var)
            analysis.cutsMC += " && %s >= 2"%(njets80Var)
        analysis, plotOpts = adjustForFineGrainedMCPred(analysis, plotOpts=plotOpts)
        #perform analysis
        hists = makeControlSampleHistsForAnalysis( analysis, plotOpts=plotOpts, 
                sfHists=sfHists, sfVars=sfVars, printdir=outdir, debugLevel=debugLevel, 
                auxSFs=auxSFs, noFill=args.noFill, dataDrivenQCD=dataDrivenQCD) 
        #compute scale factors
        normErrFractions = getNormErrFractions()
        normErrFractions['QCD'] = 0.02
        appendScaleFactors(process, hists, sfHists, lumiData=analysis.lumi, th2PolyXBins=xbins, 
                th2PolyCols=cols, debugLevel=debugLevel, var=sfVars, printdir=outdir,
                normErrFraction=normErrFractions)
        #export histograms
        if not args.noSave:
            macro.exportHists(hists, outFileName='controlHistograms'+region+'.root', 
                outDir=outdir, debugLevel=debugLevel)

    #write scale factors
    if not args.noSave:
        outname = "data/ScaleFactors/RazorMADD2015/RazorScaleFactors_%s.root"%(tag)
        if args.deltaPhiCut:
            outname = outname.replace('.root','_DPhiCut.root')
        if args.njets80Cut:
            outname = outname.replace('.root','_NJets80Cut.root')
        outfile = rt.TFile(outname, "RECREATE")
        for name in sfHists:
Beispiel #6
0
 analysis = regions[region]
 print "\nRegion:",region,"\n"
 #make output directory
 outdir = 'Plots/'+tag+'/'+region
 os.system('mkdir -p '+outdir)
 #set up analysis
 (xbins,cols) = analysis.unrollBins
 auxSFs = razorWeights.getNJetsSFs(analysis)
 if noCorr:
     auxSFs = {}
 #perform analysis
 hists = makeControlSampleHistsForAnalysis( analysis, plotOpts=plotOpts, sfHists=sfHists,
         sfVars=sfVars, printdir=outdir, auxSFs=auxSFs, btags=analysis.nbMin,
         debugLevel=debugLevel, noFill=args.noFill )
 #compute scale factors
 appendScaleFactors( region+"MR", hists, sfHists, lumiData=analysis.lumi, var="MR",
         debugLevel=debugLevel, signifThreshold=1.0, printdir=outdir )
 appendScaleFactors( region+"Rsq", hists, sfHists, lumiData=analysis.lumi, var="Rsq",
         debugLevel=debugLevel, signifThreshold=1.0, printdir=outdir )
 if not args.noSave:
     #export histograms
     macro.exportHists( hists, outFileName='controlHistograms'+region+'.root',
             outDir=outdir, debugLevel=debugLevel )
     #write out scale factors
     if not noCorr:
         outfile = rt.TFile("data/ScaleFactors/RazorMADD2015/RazorBTagClosureTests_%s.root"%(tag),
                 "UPDATE")
         print "Writing scale factor histogram",sfHists[region+"MR"].GetName(),"to file"
         outfile.cd()
         sfHists[region+"MR"].Write( sfHists[region+"MR"].GetName() )
         sfHists[region+"Rsq"].Write( sfHists[region+"Rsq"].GetName() )
         outfile.Close()
        else:
            jetName = 'NJets_NoZ'
        auxSFs = razorWeights.getNJetsSFs(analysis,jetName=jetName)
        if 'WJetsInv' in auxSFs:
            # W+jets invisible njets SFs are not computed yet
            auxSFs['WJetsInv'] = {}
        sfHistsToUse = sfHists

        hists = makeControlSampleHistsForAnalysis(analysis, plotOpts=plotOpts, 
                sfHists=sfHistsToUse, sfVars=sfVars, printdir=outdir, 
                auxSFs=auxSFs, debugLevel=debugLevel, noFill=args.noFill)

        if region == 'WJetsSingleLeptonInvNLO':
            xbins, cols = analysis.unrollBins
            appendScaleFactors('WJetsInv', hists, sfHists, lumiData=analysis.lumi,
                    th2PolyXBins=xbins, th2PolyCols=cols, debugLevel=debugLevel,
                    var=sfVars['WJetsInv'], printdir=outdir)
            sfHists['DYJetsInv'] = sfHists['WJetsInv']
            histToWrite = sfHists['WJetsInv']
        elif region == 'WJetsSingleLeptonInvNLOForNJets':
            sfHistsCopy = sfHists.copy()
            appendScaleFactors('WJetsInv', hists, sfHistsCopy, 
                    lumiData=analysis.lumi, debugLevel=debugLevel,
                    var=jetName, printdir=outdir)
            sfHists['NJetsInv'] = sfHistsCopy['WJetsInv']
            histToWrite = sfHistsCopy['WJetsInv']
        else:
            sfHistsCopy = sfHists.copy()
            del sfHistsCopy['DYJetsInv']
            appendScaleFactors('DYJetsInv', hists, sfHistsCopy,
                    lumiData=analysis.lumi, debugLevel=debugLevel,
        sfVarsToUse = sfVars[region]
        njetsName = njetsNames[region]
        dataDrivenQCD = (region == "GJetsInvForNJets")
        #perform analysis
        hists = makeControlSampleHistsForAnalysis(analysis,
                                                  plotOpts=plotOpts,
                                                  sfHists=sfHistsToUse,
                                                  sfVars=sfVarsToUse,
                                                  printdir=outdir,
                                                  debugLevel=debugLevel,
                                                  dataDrivenQCD=dataDrivenQCD)
        #compute scale factors
        appendScaleFactors(process,
                           hists,
                           sfHistsToUse,
                           lumiData=analysis.lumi,
                           debugLevel=debugLevel,
                           var=njetsName,
                           printdir=outdir)
        #export histograms
        macro.exportHists(hists,
                          outFileName='controlHistograms' + region + '.root',
                          outDir=outdir,
                          debugLevel=debugLevel)
        #write out scale factors
        print "Writing scale factor histogram", sfHistsToUse[process].GetName(
        ), "to file"
        outfile.cd()
        sfHistsToUse[process].Write(sfHistsToUse[process].GetName())

    outfile.Close()
            razorWeights.loadPhotonPurityHists(sfHists, tag, debugLevel)
        else:
            dataDrivenQCD = False
        if args.tightCuts:
            if (region == "GJetsInvForNJets" or region == "WJetsInvForNJets"):
                continue
            analysis.cutsData += " && MR > 500 && Rsq > 0.25 && NJets80 >= 2"
            analysis.cutsMC += " && MR > 500 && Rsq > 0.25 && NJets80 >= 2"
        #perform analysis
        hists = makeControlSampleHistsForAnalysis( analysis, plotOpts=plotOpts, 
                sfHists=sfHists, sfVars=sfVarsToUse, 
                printdir=outdir, auxSFs=auxSFs, noFill=args.noFill,
                dataDrivenQCD=dataDrivenQCD, debugLevel=debugLevel )
        #compute scale factors
        sfHistsCopy = sfHists.copy()
        appendScaleFactors( process, hists, sfHistsCopy, lumiData=analysis.lumi, 
                debugLevel=debugLevel, var=njetsName, printdir=outdir )
        if region == "TTJetsForNJets":
            sfHists["NJetsTTJets"] = sfHistsCopy["TTJets"]
            tmpTTJets = sfHists["NJetsTTJets"].Clone()
        elif region == "WJetsForNJets":
            sfHists["NJetsWJets"] = sfHistsCopy["WJets"]
        if not args.noSave:
            #export histograms
            macro.exportHists( hists, outFileName='controlHistograms'+region+'.root',
                    outDir=outdir, debugLevel=debugLevel )
            #write out scale factors
            print "Writing scale factor histogram",sfHistsCopy[process].GetName(),"to file"
            outfile.cd()
            sfHistsCopy[process].Write( sfHistsCopy[process].GetName() )

    outfile.Close()
        analysis = regions[region]
        analysis.weightOpts.append('ttbardileptonmt')
        analysis.dataWeightOpts.append('ttbardileptonmt')
        #make output directory
        outdir = 'Plots/'+tag+'/'+region
        os.system('mkdir -p '+outdir)
        #prepare analysis
        auxSFs = razorWeights.getNJetsSFs(analysis,jetName='NJets40')
        auxSFs = razorWeights.addAllBTagSFs(analysis, auxSFs)
        bclosure.adjustForRegionBInclusive(analysis, sfHists, auxSFs)
        #perform analysis
        hists = makeControlSampleHistsForAnalysis( analysis, plotOpts=plotOpts, sfHists=sfHists,
            printdir=outdir, auxSFs=auxSFs, debugLevel=debugLevel, noFill=args.noFill )
        #record discrepancies > 1 sigma
        tmpSFHists = copy.copy(sfHists)
        if 'TTJets2L' in tmpSFHists: del tmpSFHists["TTJets2L"]
        appendScaleFactors("TTJets2L", hists, tmpSFHists, lumiData=analysis.lumi, 
            var='MR', debugLevel=debugLevel, printdir=outdir)
        #write out scale factors
        print "Writing histogram",tmpSFHists["TTJets2L"].GetName(),"to file"
        if not args.noSave:
            outfile.cd()
            tmpSFHists["TTJets2L"].Write(region+"ScaleFactors")
            #export histograms
            macro.exportHists( hists, outFileName='controlHistograms'+region+'.root',
                    outDir=outdir, debugLevel=debugLevel )

    if not args.noSave:
        outfile.Close()

Beispiel #11
0
                                           sfHists,
                                           auxSFs,
                                           gjets=True)
        sfHistsToUse = sfHists
        hists = makeControlSampleHistsForAnalysis(analysis,
                                                  plotOpts=plotOpts,
                                                  sfHists=sfHistsToUse,
                                                  sfVars=sfVars,
                                                  printdir=outdir,
                                                  auxSFs=auxSFs,
                                                  debugLevel=debugLevel,
                                                  noFill=args.noFill)
        sfHistsTmp = copy.copy(sfHists)
        appendScaleFactors("DYJetsInv",
                           hists,
                           sfHistsTmp,
                           lumiData=analysis.lumi,
                           debugLevel=debugLevel,
                           var='NBJetsMedium',
                           printdir=outdir)
        if not args.noSave:
            macro.exportHists(hists,
                              outFileName='controlHistograms' + region +
                              '.root',
                              outDir=outdir,
                              debugLevel=debugLevel)
            outfile = rt.TFile(
                "data/ScaleFactors/RazorMADD2015/RazorDYJetsDileptonInvBCheck_{}.root"
                .format(tag), "RECREATE")
            sfHistsTmp["DYJetsInv"].Write(region + "ScaleFactors")
        os.system('mkdir -p '+outdir)
        analysis = regions[region]
        auxSFs = razorWeights.getNJetsSFs(analysis)
        auxSFs = razorWeights.addBTagSFs(analysis, auxSFs)
        adjustForRegion(analysis, sfHists, auxSFs)
        hists = makeControlSampleHistsForAnalysis( analysis, plotOpts=plotOpts, sfHists=sfHists,
                sfVars=sfVars, printdir=outdir, auxSFs=auxSFs, btags=analysis.nbMin,
                debugLevel=debugLevel, noFill=args.noFill )

        sfHistName = getSFHistName(analysis)
        sfProcs = ['TTJets', 'WJets']
        sfHistsTmp = sfHists.copy()
        if 'MRCorr' in region:
            sfHistName = sfHistName.replace('MR', 'Rsq')
            appendScaleFactors( sfProcs, hists, sfHistsTmp, 
                    lumiData=analysis.lumi, var="Rsq", useUncertainty=True,
                    debugLevel=debugLevel, printdir=outdir )
        else:
            appendScaleFactors( sfProcs, hists, sfHistsTmp, 
                    lumiData=analysis.lumi, var="MR",
                    debugLevel=debugLevel, printdir=outdir )
        sfHists[sfHistName] = sfHistsTmp['_'.join(sfProcs)]
        sfHists[sfHistName].SetName(sfHistName)
        if not args.noSave:
            macro.exportHists( hists, outFileName='controlHistograms'+region+'.root',
                    outDir=outdir, debugLevel=debugLevel )
            outfile = rt.TFile(getOutputFilename(tag), "UPDATE")
            histToWrite = sfHists[sfHistName]
            print "Writing scale factor histogram",histToWrite.GetName(),"to file"
            outfile.cd()
            histToWrite.Write( histToWrite.GetName() )
Beispiel #13
0
            sfHistsToUse = sfHists
        #perform analysis
        hists = makeControlSampleHistsForAnalysis(analysis,
                                                  plotOpts=plotOpts,
                                                  sfHists=sfHistsToUse,
                                                  sfVars=sfVars,
                                                  printdir=outdir,
                                                  auxSFs=auxSFs,
                                                  debugLevel=debugLevel)
        #record discrepancies > 1 sigma
        tmpSFHists = copy.copy(sfHists)
        del tmpSFHists["DYJetsInv"]
        appendScaleFactors("DYJetsInv",
                           hists,
                           tmpSFHists,
                           lumiData=analysis.lumi,
                           debugLevel=debugLevel,
                           var=sfVars["DYJetsInv"],
                           signifThreshold=1.0,
                           printdir=outdir)
        #write out scale factors
        print "Writing histogram", tmpSFHists["DYJetsInv"].GetName(), "to file"
        outfile.cd()
        tmpSFHists["DYJetsInv"].Write(region + "ScaleFactors")

        #in the first pass, update the normalization of the G+jets scale factor histogram
        if updateNorm:
            dataNorm = hists["Data"]["1"].GetBinContent(1)
            dataNormErr = hists["Data"]["1"].GetBinError(1)
            mcNorm = 0
            mcNormErr = 0
            for proc in analysis.samples:
Beispiel #14
0
                                                  sfVars=sfVars,
                                                  printdir=outdir,
                                                  auxSFs=auxSFs,
                                                  btags=analysis.nbMin,
                                                  dataDrivenQCD=True,
                                                  debugLevel=debugLevel,
                                                  noFill=args.noFill)

        sfHistName = bclosure.getSFHistName(analysis, gjets=True)
        sfHistsTmp = copy.copy(sfHists)
        if 'MRCorr' in region:
            sfHistName = sfHistName.replace('MR', 'Rsq')
            appendScaleFactors('GJetsInv',
                               hists,
                               sfHistsTmp,
                               lumiData=analysis.lumi,
                               var="Rsq_NoPho",
                               debugLevel=debugLevel,
                               printdir=outdir)
        else:
            appendScaleFactors('GJetsInv',
                               hists,
                               sfHistsTmp,
                               lumiData=analysis.lumi,
                               var="MR_NoPho",
                               debugLevel=debugLevel,
                               printdir=outdir)
        sfHists[sfHistName] = sfHistsTmp['GJetsInv']
        if not args.noSave:
            macro.exportHists(hists,
                              outFileName='controlHistograms' + region +
Beispiel #15
0
        hists = makeControlSampleHistsForAnalysis(analysis,
                                                  plotOpts=plotOpts,
                                                  sfHists=sfHistsToUse,
                                                  sfVars=sfVars,
                                                  printdir=outdir,
                                                  auxSFs=auxSFs,
                                                  debugLevel=debugLevel,
                                                  noFill=args.noFill)

        if region == 'WJetsSingleLeptonInvNLO':
            xbins, cols = analysis.unrollBins
            appendScaleFactors('WJetsInv',
                               hists,
                               sfHists,
                               lumiData=analysis.lumi,
                               th2PolyXBins=xbins,
                               th2PolyCols=cols,
                               debugLevel=debugLevel,
                               var=sfVars['WJetsInv'],
                               printdir=outdir)
            sfHists['DYJetsInv'] = sfHists['WJetsInv']
            histToWrite = sfHists['WJetsInv']
        elif region == 'WJetsSingleLeptonInvNLOForNJets':
            sfHistsCopy = sfHists.copy()
            appendScaleFactors('WJetsInv',
                               hists,
                               sfHistsCopy,
                               lumiData=analysis.lumi,
                               debugLevel=debugLevel,
                               var=jetName,
                               printdir=outdir)
                                                  plotOpts=plotOpts,
                                                  sfHists=sfHists,
                                                  sfVars=sfVars,
                                                  printdir=outdir,
                                                  debugLevel=debugLevel,
                                                  auxSFs=auxSFs,
                                                  noFill=args.noFill,
                                                  dataDrivenQCD=dataDrivenQCD)
        #compute scale factors
        normErrFractions = getNormErrFractions()
        normErrFractions['QCD'] = 0.02
        appendScaleFactors(process,
                           hists,
                           sfHists,
                           lumiData=analysis.lumi,
                           th2PolyXBins=xbins,
                           th2PolyCols=cols,
                           debugLevel=debugLevel,
                           var=sfVars,
                           printdir=outdir,
                           normErrFraction=normErrFractions)
        #export histograms
        if not args.noSave:
            macro.exportHists(hists,
                              outFileName='controlHistograms' + region +
                              '.root',
                              outDir=outdir,
                              debugLevel=debugLevel)

    #write scale factors
    if not args.noSave:
        outname = "data/ScaleFactors/RazorMADD2015/RazorScaleFactors_%s.root" % (