sfFilename="data/ScaleFactors/RazorMADD2015/RazorScaleFactors_%s_Uncorr.root"%(tag), 
         processNames=regions.itervalues().next().samples, debugLevel=debugLevel)
 sfVars = ("MR_NoPho","Rsq_NoPho")
 sfNJetsFile = rt.TFile.Open(
         "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
         err = sfHists["DYJetsInv"].GetBinError(bn)
         sysErr = sfHists["DYJetsInv"].GetBinContent(bn) - downHist.GetBinContent(bn)
         newErr = ( err*err + sysErr*sysErr )**(0.5)
         sfHists["DYJetsInv"].SetBinError(bn-1, newErr) # adjust bin number by 1 to account for bug in ROOT
         print "Increasing error on DYJets scale factor bin",bn,"from",err,"to",sfHists["DYJetsInv"].GetBinError(bn)
 
 updateNorm = (not closure)
 for region in regionsOrder:
     analysis = regions[region]
     #make output directory
     outdir = 'Plots/'+tag+'/'+region
     if closure:
         outdir += '_Closure'
     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']:
        (tag))
    sfHists['NJetsTTJets'] = sfNJetsFile.Get("TTJetsScaleFactors")
    sfHists['NJetsWJets'] = sfNJetsFile.Get("WJetsScaleFactors")
    outfile = rt.TFile(
        "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,
                'DYJetsInv'].replace('DYJets_', 'DYJetsPtBinned_')

    if not args.noSave:
        outfile = rt.TFile(
            "data/ScaleFactors/RazorMADD2015/RazorDYJetsDileptonInvNLOCrossCheck_%s.root"%(tag), "RECREATE")

    for region in regionsOrder:
        analysis = regions[region]
        outdir = 'Plots/'+tag+'/'+region
        os.system('mkdir -p '+outdir)

        if region in wjetsRegionNames:
            jetName = 'NJets_NoW'
        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']
        % (tag),
        processNames=regions.itervalues().next().samples,
        debugLevel=debugLevel)
    sfVars = ("MR", "Rsq")
    sfNJetsFile = rt.TFile.Open(
        "data/ScaleFactors/RazorMADD2015/RazorNJetsScaleFactors_%s.root" %
        (tag))
    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()
     debugLevel=debugLevel)
 sfVars = ("MR_NoPho", "Rsq_NoPho")
 sfNJetsFile = rt.TFile.Open(
     "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,
    for region in regionsOrder:
        analysis = regions[region]
        #make output directory
        outdir = 'Plots/'+tag+'/'+region
        if args.tightCuts:
            outdir += "_TightCuts"
        os.system('mkdir -p '+outdir)
        #set up analysis
        process = sfNames[region]
        (xbins,cols) = analysis.unrollBins
        sfVarsToUse = sfVars[region]
        njetsName = njetsNames[region]
        #use proper set of NJets correction factors.
        #do not correct WJets until WJets scale factors have been derived
        auxSFs = razorWeights.getNJetsSFs(analysis,
                jetName=njetsNames[region])
        if region == "TTJetsForNJets" or region == "WJetsForNJets":
            auxSFs["WJets"] = {}
        if region == "TTJetsForNJets":
            auxSFs["TTJets"] = {}
        if region == "GJetsInvForNJets":
            auxSFs["GJetsInv"] = {}
        if region == "WJetsInvForNJets":
            auxSFs["WJetsInv"] = {}
        if region == "GJetsInvForNJets":
            dataDrivenQCD = True
            razorWeights.getPhotonPuritySFs(auxSFs)
            razorWeights.loadPhotonPurityHists(sfHists, tag, debugLevel)
        else:
            dataDrivenQCD = False
        if args.tightCuts:
def getAllAuxSFs(analysis):
    auxSFs = razorWeights.getNJetsSFs(analysis, jetName='nSelectedJets')
    auxSFs = razorWeights.addBTagSFs(analysis, auxSFs)
    auxSFs = razorWeights.addBTagSFs(analysis, auxSFs, gjets=True)
    auxSFs = razorWeights.addBTagDoubleRatioSFs(analysis, auxSFs)
    return auxSFs
 #modify cuts and output dir if running with ele/mu separately
 if args.electrons:
     analysis.cutsData += " && abs(lep1Type) == 11"
     analysis.cutsMC += " && abs(lep1Type) == 11"
     outdir = outdir.replace('Lepton','Electron')
     if 'Tau' in region: continue
 if args.muons:
     analysis.cutsData += " && abs(lep1Type) == 13"
     analysis.cutsMC += " && abs(lep1Type) == 13"
     outdir = outdir.replace('Lepton','Muon')
     if 'Tau' in region: continue
 os.system('mkdir -p '+outdir)
 #set up analysis
 if region.startswith('Veto'):
     sfHistsToUse = sfHists
     auxSFsToUse = razorWeights.getNJetsSFs(analysis,jetName='NJets40')
     treeName = "ControlSampleEvent"
     bjetsName = 'NBJetsMedium'
 else:
     sfHistsToUse = sfHistsSignal
     auxSFsToUse = razorWeights.getNJetsSFs(analysis,jetName='nSelectedJets')
     treeName = "RazorInclusive"
     bjetsName = 'nBTaggedJets'
 auxSFsToUse = razorWeights.addAllBTagSFs(analysis, auxSFsToUse, bjetsName=bjetsName)
 bclosure.adjustForRegionBInclusive(analysis, sfHistsToUse, auxSFsToUse)
 #set up lepton pt correction
 varForCorrection = "lep1.Pt()"
 # The non-closure on the lepton pt distribution is taken as a 
 # correlated shape systematic. For lepton eta, on the other hand,
 # in the absence of any systematic non-closure we take the
 # uncertainty on the data/MC ratio as a systematic in each bin
Example #10
0
 #modify cuts and output dir if running with ele/mu separately
 if args.electrons:
     analysis.cutsData += " && abs(lep1Type) == 11"
     analysis.cutsMC += " && abs(lep1Type) == 11"
     outdir = outdir.replace('Lepton', 'Electron')
     if 'Tau' in region: continue
 if args.muons:
     analysis.cutsData += " && abs(lep1Type) == 13"
     analysis.cutsMC += " && abs(lep1Type) == 13"
     outdir = outdir.replace('Lepton', 'Muon')
     if 'Tau' in region: continue
 os.system('mkdir -p ' + outdir)
 #set up analysis
 if region.startswith('Veto'):
     sfHistsToUse = sfHists
     auxSFsToUse = razorWeights.getNJetsSFs(analysis, jetName='NJets40')
     treeName = "ControlSampleEvent"
 else:
     sfHistsToUse = sfHistsSignal
     auxSFsToUse = razorWeights.getNJetsSFs(analysis,
                                            jetName='nSelectedJets')
     treeName = "RazorInclusive"
 #set up lepton pt correction
 varForCorrection = "lep1.Pt()"
 sigVarForCorrection = "leadingGenLeptonPt"
 if "PtCorr" in region:
     varForCorrection = "abs(lep1.Eta())"
     sigVarForCorrection = "abs(leadingGenLeptonEta)"
     if region.startswith('VetoLepton'):
         for proc in analysis.samples:
             auxSFsToUse[proc][region.replace(
    jetsOrder = ["SevenJet", "MultiJet", "DiJet"]
    jetsLimit = [(7,-1),(4,6),(2,3)]
    for name,jets in zip(jetsOrder, jetsLimit):
        regionName = 'OneLepton'+name
        regions[regionName] = razor.Analysis("SingleLepton",tag=tag,
                njetsMin=jets[0], njetsMax=jets[1], boostCuts=boostCuts)
        regionsOrder.append(regionName)

    sfHists = macro.loadScaleFactorHists(
            sfFilename="data/ScaleFactors/RazorMADD2015/RazorScaleFactors_%s.root"%(tag), 
            processNames=regions.itervalues().next().samples, debugLevel=debugLevel)
    sfVars = ("MR","Rsq")
    sfNJetsFile = rt.TFile.Open(
            "data/ScaleFactors/RazorMADD2015/RazorNJetsScaleFactors_%s.root"%(tag))
    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)
        adjustForRegion(analysis)
        hists = makeControlSampleHistsForAnalysis( analysis, plotOpts=plotOpts, sfHists=sfHists,
                sfVars=sfVars, printdir=outdir, auxSFs=auxSFs, 
                debugLevel=debugLevel, noFill=args.noFill )
        if not args.noSave:
            macro.exportHists( hists, outFileName='controlHistograms'+region+'.root',
                    outDir=outdir, debugLevel=debugLevel )
Example #12
0
            sfHists['ZInv' + jtype + bs + 'BDown'] = macro.invertHistogram(
                sfHists['ZInv' + jtype + bs + 'BUp'])

    #check that everything came out correctly
    for h, hist in sfHists.iteritems():
        if debugLevel > 0:
            print "Checking scale factor histogram:", h
        assert hist

    #estimate yields in signal region
    for region in regionsOrder:
        analysis = regions[region]
        boxName = region.replace(dirSuffix, '')[:-2]
        btags = int(region.replace(dirSuffix, '')[-2])
        #get correct NJets scale factors
        auxSFs = razorWeights.getNJetsSFs(analysis, jetName='nSelectedJets')

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

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

        blindBins = [(x, y) for x in range(2,
                                           len(analysis.binning["MR"]) + 1)
                     for y in range(2,
                                    len(analysis.binning["Rsq"]) + 1)]

        #apply options
        dataDrivenQCD = True
        if args.unblind: blindBins = None