plotOpts = { 'comment':False }

    regionsOrder = ["0BSusySync", "1BSusySync", "2BSusySync"]
    regions = {
            "0BSusySync":Analysis("SusySync", tag=tag, nbMax=0),
            "1BSusySync":Analysis("SusySync", tag=tag, nbMin=1),
            "2BSusySync":Analysis("SusySync", tag=tag, nbMin=2),
            }

    for region in regionsOrder:
        analysis = regions[region]
        name = region
        #apply extra lepton cuts
        if args.muons:
            analysis.cutsData += " && abs(lep1Type) == 13"
            analysis.cutsMC += " && abs(lep1Type) == 13"
            name += "_Muons"
        if args.electrons:
            analysis.cutsData += " && abs(lep1Type) == 11"
            analysis.cutsMC += " && abs(lep1Type) == 11"
            name += "_Electrons"
        #make output directory
        outdir = 'Plots/'+tag+'/'+name
        os.system('mkdir -p '+outdir)
        #perform analysis
        hists = makeControlSampleHistsForAnalysis( analysis, plotOpts=plotOpts, 
                printdir=outdir, debugLevel=debugLevel ) 
        #export histograms
        macro.exportHists(hists, outFileName='controlHistograms'+name+'.root', 
            outDir=outdir, debugLevel=debugLevel)
        "data/ScaleFactors/RazorMADD2015/RazorTTJetsDileptonCrossCheck_%s.root"
        % (tag), "RECREATE")

    for region in regionsOrder:
        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')
        #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()
         "data/ScaleFactors/RazorMADD2015/RazorNJetsScaleFactors_%s.root"%(tag))
 sfHists['NJetsInv'] = sfNJetsFile.Get("GJetsInvScaleFactors")
 razorWeights.loadPhotonPurityHists(sfHists, tag, debugLevel)
 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"
예제 #4
0
        os.system('mkdir -p '+outdir)
        #prepare analysis
        auxSFs = razorWeights.getNJetsSFs(analysis,jetName='NJets_NoZ')
        auxSFs = razorWeights.addAllBTagSFs(analysis, auxSFs)
        auxSFs = razorWeights.addAllBTagSFs(analysis, auxSFs, var='MR_NoZ', 
                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)
예제 #5
0
def makeSMSTemplates(box, inFile, uncertainties=[], debugLevel=0,
        tag="Razor2016_MoriondRereco", opts=None, boostCuts=True,
        doUncorrelate=True):
    """Returns a dictionary of histograms representing predicted
        yields for the indicated signal sample.
        'opts' should be an SMSOpts instance containing desired
        values for configurable parameters."""
    print '\nInput file is %s' % inFile
    uncerts = copy.copy(uncertainties)

    # process configuration options
    if opts is None:
        opts = SMSOpts()
    xBR = opts.xBR
    yBR = opts.yBR
    doMCStat = True
    if opts.doNPVExtrap:
        uncerts.append('npvextrap')
        doMCStat = False
    if opts.doGenMetVsPFMet:
        uncerts.append('genmetvspfmet')

    # get mass point information
    model, m1, m2 = getModelInfoFromFilename(inFile, xBR, yBR)
    isGluinos = ("T1" in model or "T5" in model)
    if isGluinos:
        thyXsec, _ = getTheoryCrossSectionAndError(
                mGluino=m1)
    else:
        thyXsec, _ = getTheoryCrossSectionAndError(
                mStop=m1)
    print "\n--- %s %s %s ---\n"%(model, m1, m2)
    print "Theory cross section: %.3f pb"%thyXsec

    minBtags = 0
    maxBtags = getMaxBtags(box)
    # special case: 1L control regions
    if box == "TTJetsSingleLeptonForSignal":
        minBtags = maxBtags = 1
    elif box == "WJetsSingleLeptonForSignal":
        minBtags = maxBtags = 0
    hists = []
    unrollBins = []
    for nb in range(minBtags, maxBtags+1):
        # get analysis info
        nbMax = nb
        if nb == maxBtags:
            nbMax = -1
        # special case: 1L control regions
        if box == "WJetsSingleLeptonForSignal":
            nbMax = 0
        analysis = Analysis(box, tag, nbMin=nb, nbMax=nbMax,
                boostCuts=boostCuts)
        unrollBins.append(analysis.unrollBins)

        # modify for signal sample
        analysis.filenames = { 'Signal':inFile }
        analysis.samples = ['Signal']
        analysis.samplesReduced = analysis.samples
            
        # scale according to cross section
        f = rt.TFile.Open(inFile)
        nEvents = f.Get('NEvents').Integral()
        globalScaleFactor = thyXsec/nEvents 
        nisrFactor = getGlobalNISRScaleFactor(f)
        globalScaleFactor *= nisrFactor
        print "Number of events: %d"%nEvents
        print "Integrated luminosity: %d /pb"%analysis.lumi
        print "Overall NISR scale factor: {}".format(nisrFactor)
        print "Overall scale factor: %.3f"%(
                analysis.lumi * globalScaleFactor)

        # fill the histograms
        hists.append(makeControlSampleHistsForAnalysis(analysis, 
                treeName="RazorInclusive", shapeErrors=uncerts,
                boxName=box, btags=nb, makePlots=False, 
                exportShapeErrs=True, propagateScaleFactorErrs=False,
                lumiMC=1./globalScaleFactor, debugLevel=debugLevel))

        makeRazor2DTable(pred=None, 
                boxName='{}-{}-{}-{}-'.format(box, model, m1, m2), 
                mcNames=['{} {} {}'.format(model, m1, m2)], 
                mcHists=[hists[-1]['Signal'][('MR','Rsq')]], 
                btags=nb, unrollBins=unrollBins[-1], listAllMC=True)
            
    signalHists = unrollAndStitch(hists, box, samples=analysis.samples,
            debugLevel=debugLevel, unrollBins=unrollBins, 
            addStatUnc=doMCStat)

    # apply pileup weight extrapolation procedure
    if 'Signal_npvextrapUp' in signalHists:
        npvLowHighHist = getNPVLowHighHist(analysis)
        npvHist = getNPVHist(tag)
        doNPVExtrapolation(signalHists, npvHist, npvLowHighHist,
                scale=analysis.lumi*globalScaleFactor)
        # need this extrapolation to be uncorrelated across bins & boxes
        for sysName in ['Signal_npvextrapUp', 'Signal_npvextrapDown']:
            signalHists[sysName.replace(
                'npvextrap','npvextrap'+box)] = signalHists[sysName]
            del signalHists[sysName]
        if doUncorrelate:
            uncorrelate(signalHists, 'npvextrap'+box, suppressLevel=0.1)

    # apply gen-MET vs PF MET systematic
    if 'Signal_genmetvspfmetUp' in signalHists:
        doGenMetVsPFMetSystematic(signalHists)

    return signalHists
 if region == "WJetsInvForNJets":
     auxSFs["WJetsInv"] = {}
 if region == "GJetsInvForNJets":
     dataDrivenQCD = True
     razorWeights.getPhotonPuritySFs(auxSFs)
     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
    sfHists['NJetsTTJets'] = sfNJetsFile.Get("TTJetsScaleFactors")
    sfHists['NJetsWJets'] = sfNJetsFile.Get("WJetsScaleFactors")

    for region in regionsOrder:
        print "\nRegion:", region, "\n"
        outdir = 'Plots/' + tag + '/' + region
        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",
        if args.noSys:
            shapesToUse = []
        if args.noSFs:
            print "Ignoring all Data/MC scale factors"
            sfHistsToUse = {}
            auxSFsToUse = {}
            shapesToUse = removeSFShapes(shapesToUse)
        if args.fineGrained:
            (analysis, sfHistsToUse, auxSFsToUse, shapesToUse, 
                    plotOpts) = adjustForFineGrainedMCPred(
                            analysis, sfHistsToUse, auxSFsToUse, shapesToUse, plotOpts)

        print "\nBox:",region,"("+boxName,str(btags),"B-tag)"

        #make output directory
        outdir = "Plots/"+tag+"/"+region
        os.system('mkdir -p '+outdir)

        #run analysis
        hists = makeControlSampleHistsForAnalysis(analysis,
                sfHists=sfHistsToUse, treeName="RazorInclusive", 
                shapeErrors=shapesToUse, fitToyFiles=toysToUse, boxName=boxName, 
                blindBins=blindBins, btags=btags, debugLevel=debugLevel, 
                auxSFs=auxSFsToUse, dataDrivenQCD=dataDrivenQCD, printdir=outdir, 
                plotOpts=plotOpts, noFill=args.noFill, exportShapeErrs=True, 
                propagateScaleFactorErrs=False, makePlots=False)
        if not args.noSave:
            #export histograms
            macro.exportHists(hists, outFileName='razorHistograms'+region+'.root', 
                    outDir=outdir, debugLevel=debugLevel)
 for region, analysis in regions.iteritems():
     #make output directory
     outdir = 'Plots/' + tag + '/' + region
     os.system('mkdir -p ' + outdir)
     #set up analysis
     process = sfNames[region]
     (xbins, cols) = analysis.unrollBins
     sfHistsToUse = sfHists[region]
     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,
예제 #10
0
         for proc in analysis.samples:
             auxSFsToUse[proc][region.replace(
                 'PtCorr', '')] = ("leadingGenLeptonPt",
                                   "abs(leadingGenLeptonType) == 15")
 #sanity check
 print "\nRegion:", region
 print "Tree name:", treeName
 print "Aux SFs to use:", auxSFsToUse
 print "Variable for correction:", varForCorrection
 print "Signal region variable for correction:", sigVarForCorrection, "\n"
 #perform analysis
 hists[region] = makeControlSampleHistsForAnalysis(
     analysis,
     plotOpts=plotOpts,
     sfHists=sfHistsToUse,
     sfVars=sfVars,
     printdir=outdir,
     auxSFs=auxSFsToUse,
     treeName=treeName,
     debugLevel=debugLevel)
 #export histograms
 macro.exportHists(hists[region],
                   outFileName='controlHistograms' + region + '.root',
                   outDir=outdir,
                   debugLevel=debugLevel,
                   delete=False)
 #compute correction factors
 if region.startswith('Veto'):
     #make control region scale factors
     sfHists[region] = makeVetoLeptonCorrectionHist(
         hists[region],
예제 #11
0
 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)
     #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,
     analysis.cutsData += " && abs(%s) < 2.8" % (dPhiVar)
     analysis.cutsMC += " && abs(%s) < 2.8" % (dPhiVar)
 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,