Example #1
0
def main():

    if len(sys.argv) < 2:
        usage()

    dirs = []
    dirs.append(sys.argv[1])
        
    #datasets = dataset.getDatasetsFromMulticrabDirs(dirs)
    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra, searchMode=searchMode, analysisName=analysis)

 #   consistencyCheck.checkConsistencyStandalone(dirs[0],datasets,name="QCD inverted")
    
    datasets.loadLuminosities()
    datasets.updateNAllEventsToPUWeighted()

    # erik
#    datasets.remove(filter(lambda name: "TTJets_SemiLept" in name, datasets.getAllDatasetNames()))
#    datasets.remove(filter(lambda name: "TTJets_FullLept" in name, datasets.getAllDatasetNames()))
#    datasets.remove(filter(lambda name: "TTJets_Hadronic" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "WJetsToLNu" in name and not "WJetsToLNu_HT" in name, datasets.getAllDatasetNames()))
       
    plots.mergeRenameReorderForDataMC(datasets)

    datasets.merge("EWK", [
                    "TT",
                    "WJetsHT",
                    "DYJetsToLLHT",
                    "SingleTop",
                #    "Diboson"
                    ])
    style = tdrstyle.TDRStyle()

    plot = plots.PlotBase()
    

    legends = {}
    name_re = re.compile("SelectedTau_pT_(?P<name>\S+)")
    for i,histo in enumerate(HISTONAMES):
        plot.histoMgr.appendHisto(purityGraph(i,datasets,histo))
        name = histo
        match = name_re.search(histo)
        if match:
            name = match.group("name")
        legends["Purity%s"%i] = name
#        if "AfterMetCut"  in name:    
#            legends["Purity%s"%i] = "MET > 60 GeV"
        if "SelectedTau_pT_CollinearCuts"  in name:    
            legends["Purity%s"%i] = "Collinear cuts"
        if "AfterBtagging"  in name:    
            legends["Purity%s"%i] = "B tagging"
        if "AfterBveto"  in name:    
            legends["Purity%s"%i] = "B-jet veto"
        if "AfterBvetoPhiCuts"  in name:    
            legends["Purity%s"%i] = "B-jet veto, TailKiller"
        if "SelectedTau_pT_BackToBackCuts"  in name:    
            legends["Purity%s"%i] = "BackToBack cuts" 

   # plot.createFrame("purity_QCD_only", opts={"xmin": 40, "xmax": 160, "ymin": 0., "ymax": 1.05})
    plot.createFrame("purity_QCD_only", opts={"xmin": 40, "xmax": 400, "ymin": 0., "ymax":1})
    plot.frame.GetXaxis().SetTitle("p_{T}^{#tau jet} (GeV/c)")
    plot.frame.GetYaxis().SetTitle("QCD purity")
#    plot.setEnergy(datasets.getEnergies())

#    plot.histoMgr.appendHisto(histograms.Histo(histo,"InvertedTauPtAfterAllSelections"))
    
    plot.histoMgr.setHistoLegendLabelMany(legends)

###    plot.setLegend(histograms.createLegend(0.2, 0.35, 0.6, 0.5))
    
 
#    histograms.addText(0.2, 0.3, "TailKiller: MediumPlus", 18)
#    histograms.addText(0.35, 0.28, "BackToBack cuts: TightPlus", 20)
#    histograms.addText(0.35, 0.22, "2011B", 20)

    histograms.addText(0.3, 0.35, "QCD only", 20)
    histograms.addText(0.3, 0.3, "After all selection cuts", 20)
#    histograms.addText(0.2, 0.3, "QCD only", 18)



    histograms.addCmsPreliminaryText()
    histograms.addEnergyText(s="%s TeV"%(datasets.getEnergies()[0]))
    histograms.addLuminosityText(x=None, y=None, lumi=datasets.getDataset("Data").getLuminosity())

    plot.draw()
    plot.save()
Example #2
0
def main(argv):

    dirs = []
    if len(sys.argv) < 2:
        usage()

    dirs.append(sys.argv[1])

    dirs_signal = ["../../SignalAnalysis_140605_143702/"]

    QCDInvertedNormalization = sort(
        QCDInvertedNormalizationFactors.QCDInvertedNormalization)
    labels, QCDInvertedNormalizationFilteredEWKFakeTaus = getSortedLabelsAndFactors(
        QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.
        QCDInvertedNormalization)

    analysis_inverted = "signalAnalysisInvertedTau"
    analysis = "signalAnalysis"

    optModes = []
    #optModes.append("OptQCDTailKillerZeroPlus")
    optModes.append("OptQCDTailKillerLoosePlus")
    optModes.append("OptQCDTailKillerMediumPlus")
    optModes.append("OptQCDTailKillerTightPlus")

    varHistoName = "shapeEWKGenuineTausTransverseMass"
    nomHistoName = "shapeTransverseMass"
    signalHistoName = "shapeEWKFakeTausTransverseMass"

    #Optimal: 0.8, 0.82, 0.9

    ###w_list = [0.65, 0.7, 0.76] #old baseline ft
    w_list = [0.66, 0.67, 0.75]
    #w_list = [0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 1]

    #defaultBinning = systematics.getBinningForPlot("shapeTransverseMass")
    defaultBinning = [0, 20, 40, 60, 80, 100, 120, 140, 160, 200, 400]
    defaultBinning_array = array.array("d", defaultBinning)

    diff_opt = []
    for optMode in optModes:
        diff_list = []
        for w in w_list:
            var_values = []
            nom_values = []

            # baseline fake taus
            mt_baseline_faketaus_data = getDataSets(dirs_signal, dataEra,
                                                    searchMode, analysis,
                                                    optMode)
            mtplot_signalfaketaus = plots.DataMCPlot(mt_baseline_faketaus_data,
                                                     signalHistoName)
            mt_signalfaketaus = mtplot_signalfaketaus.histoMgr.getHisto(
                "EWK").getRootHisto().Clone(signalHistoName)

            # inverted fake taus
            mt_inverted_faketaus_data = getDataSets(dirs, dataEra, searchMode,
                                                    analysis_inverted, optMode)

            histonames_var = mt_inverted_faketaus_data.getDataset(
                "Data").getDirectoryContent(varHistoName)
            histonames_nom = mt_inverted_faketaus_data.getDataset(
                "Data").getDirectoryContent(nomHistoName)

            bins_var = getBins(histonames_var, varHistoName)
            bins_nom = getBins(histonames_nom, nomHistoName)

            normalization_var_qg = getNormalization(
                bins_var, w, QCDInvertedNormalizationFilteredEWKFakeTaus, True,
                True)
            normalization_var = getNormalization(
                bins_var, w, QCDInvertedNormalizationFilteredEWKFakeTaus, True,
                False)
            normalization_nom = getNormalization(bins_nom, w,
                                                 QCDInvertedNormalization,
                                                 False, False)

            mt_var_qg = getMt(mt_inverted_faketaus_data, bins_var,
                              varHistoName, normalization_var_qg)
            mt_var = getMt(mt_inverted_faketaus_data, bins_var, varHistoName,
                           normalization_var)
            mt_nom = getMt(mt_inverted_faketaus_data, bins_nom, nomHistoName,
                           normalization_nom)

            mt_nom.Add(mt_signalfaketaus)

            mt_var_qg.SetName(
                "QCD(Data)+EWK+t#bar{t}(Data, mis-ID. #tau), q-g bal.")
            mt_var.SetName("QCD(Data)+EWK+t#bar{t}(Data, mis-ID. #tau)")
            mt_nom.SetName("QCD(Data)+EWK+t#bar{t}(MC, mis-ID. #tau)")

            mt_var_qg.SetLineWidth(4)
            mt_var.SetLineColor(14)
            mt_nom.SetLineColor(2)

            mt_var_qg = mt_var_qg.Rebin(
                len(defaultBinning) - 1, "", defaultBinning_array)
            mt_var = mt_var.Rebin(
                len(defaultBinning) - 1, "", defaultBinning_array)
            mt_nom = mt_nom.Rebin(
                len(defaultBinning) - 1, "", defaultBinning_array)

            for i in range(0, mt_nom.GetSize()):
                var_values.append(mt_var_qg.GetBinContent(i))
                nom_values.append(mt_nom.GetBinContent(i))

            style = tdrstyle.TDRStyle()

            varPlots = [mt_var, mt_var_qg]
            plot = plots.ComparisonManyPlot(mt_nom, varPlots)
            plot.createFrame(optMode.replace(
                "Opt", "Mt_DataDrivenVsMC_" + "w=" + str(w) + "_"),
                             createRatio=True)

            moveLegend = {"dx": -0.35, "dy": 0.05}
            plot.setLegend(
                histograms.moveLegend(histograms.createLegend(), **moveLegend))
            histograms.addText(
                0.65, 0.3,
                optMode.replace("OptQCDTailKiller",
                                "R_{BB} ").replace("Plus", ""), 25)
            histograms.addCmsPreliminaryText()
            histograms.addEnergyText()
            lumi = mt_inverted_faketaus_data.getDataset("Data").getLuminosity()
            histograms.addLuminosityText(x=None, y=None, lumi=lumi)

            plot.draw()
            plot.save()

            mt_var_qg.Delete()
            mt_var.Delete()
            mt_nom.Delete()

            #TFile.CurrentFile().Close("R")
            mt_baseline_faketaus_data.close()
            mt_inverted_faketaus_data.close()
            ROOT.gROOT.CloseFiles()
            ROOT.gROOT.GetListOfCanvases().Delete()
            ROOT.gDirectory.GetList().Delete()

            #print var_values
            #print nom_values

            # difference metrics
            num = 0
            denom = 0
            for i in range(0, len(nom_values)):
                num += var_values[i] * (var_values[i] - nom_values[i])**2
                denom += var_values[i]
            diff = num / denom
            diff_list.append(diff)
        diff_opt.append(diff_list)

    os.system("rm MtOptimal/*")
    os.system("mkdir -p MtOptimal")
    print "\nWeights:\t", w_list, '\n'
    optimalWeights = {}
    for i in range(0, len(diff_opt)):
        print optModes[i]
        print "Differences:\t", diff_opt[i], "- Optimal: w =", w_list[
            diff_opt[i].index(min(diff_opt[i]))]
        optimalWeights[optModes[i]] = w_list[diff_opt[i].index(min(
            diff_opt[i]))]
        command = "cp *" + str(w_list[diff_opt[i].index(min(
            diff_opt[i]))]) + "*" + optModes[i].replace("Opt",
                                                        "") + ".eps MtOptimal"
        os.system(command)
    print optimalWeights
    writeWeightsToFile("OptimalWeights.py", optimalWeights)
    writeNormalizationToFile("QCDPlusEWKFakeTauNormalizationFactors.py",
                             normalization_var_qg, labels)
def main(argv):

    dirs = []
    if len(sys.argv) < 2:
	usage()

    dirs.append(sys.argv[1])

    dirs_signal = ["../../SignalAnalysis_140605_143702/"]

    QCDInvertedNormalization = sort(QCDInvertedNormalizationFactors.QCDInvertedNormalization)
    labels,QCDInvertedNormalizationFilteredEWKFakeTaus = getSortedLabelsAndFactors(QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.QCDInvertedNormalization)

    analysis_inverted = "signalAnalysisInvertedTau"
    analysis = "signalAnalysis"

    optModes = []
    #optModes.append("OptQCDTailKillerZeroPlus")
    optModes.append("OptQCDTailKillerLoosePlus") 
    optModes.append("OptQCDTailKillerMediumPlus") 
    optModes.append("OptQCDTailKillerTightPlus") 

    varHistoName = "shapeEWKGenuineTausTransverseMass"
    nomHistoName = "shapeTransverseMass"
    signalHistoName = "shapeEWKFakeTausTransverseMass"

    #Optimal: 0.8, 0.82, 0.9

    ###w_list = [0.65, 0.7, 0.76] #old baseline ft
    w_list = [0.66, 0.67, 0.75]
    #w_list = [0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 1]
    
    #defaultBinning = systematics.getBinningForPlot("shapeTransverseMass")
    defaultBinning = [0,20,40,60,80,100,120,140,160,200,400]
    defaultBinning_array = array.array("d",defaultBinning)

    diff_opt = []
    for optMode in optModes:
        diff_list = []
        for w in w_list:
            var_values = []
            nom_values = []

            # baseline fake taus
            mt_baseline_faketaus_data = getDataSets(dirs_signal, dataEra, searchMode, analysis, optMode)
            mtplot_signalfaketaus = plots.DataMCPlot(mt_baseline_faketaus_data, signalHistoName)
            mt_signalfaketaus = mtplot_signalfaketaus.histoMgr.getHisto("EWK").getRootHisto().Clone(signalHistoName)

            # inverted fake taus
            mt_inverted_faketaus_data = getDataSets(dirs, dataEra, searchMode, analysis_inverted, optMode)

            histonames_var = mt_inverted_faketaus_data.getDataset("Data").getDirectoryContent(varHistoName)
            histonames_nom = mt_inverted_faketaus_data.getDataset("Data").getDirectoryContent(nomHistoName)


            bins_var = getBins(histonames_var, varHistoName)
            bins_nom = getBins(histonames_nom, nomHistoName)

            normalization_var_qg = getNormalization(bins_var,w,QCDInvertedNormalizationFilteredEWKFakeTaus,True,True)
            normalization_var = getNormalization(bins_var,w,QCDInvertedNormalizationFilteredEWKFakeTaus,True,False)
            normalization_nom = getNormalization(bins_nom,w,QCDInvertedNormalization,False,False)

            mt_var_qg = getMt(mt_inverted_faketaus_data,bins_var,varHistoName,normalization_var_qg)
            mt_var = getMt(mt_inverted_faketaus_data,bins_var,varHistoName,normalization_var)
            mt_nom = getMt(mt_inverted_faketaus_data,bins_nom,nomHistoName,normalization_nom)
                
            mt_nom.Add(mt_signalfaketaus)

            mt_var_qg.SetName("QCD(Data)+EWK+t#bar{t}(Data, mis-ID. #tau), q-g bal.")
            mt_var.SetName("QCD(Data)+EWK+t#bar{t}(Data, mis-ID. #tau)")
            mt_nom.SetName("QCD(Data)+EWK+t#bar{t}(MC, mis-ID. #tau)")

            mt_var_qg.SetLineWidth(4)
            mt_var.SetLineColor(14)
            mt_nom.SetLineColor(2)
            
            mt_var_qg = mt_var_qg.Rebin(len(defaultBinning)-1,"",defaultBinning_array)
            mt_var = mt_var.Rebin(len(defaultBinning)-1,"",defaultBinning_array)
            mt_nom = mt_nom.Rebin(len(defaultBinning)-1,"",defaultBinning_array)

            for i in range(0,mt_nom.GetSize()):
                var_values.append(mt_var_qg.GetBinContent(i))
                nom_values.append(mt_nom.GetBinContent(i))
                
            style = tdrstyle.TDRStyle()

            varPlots = [mt_var, mt_var_qg]
            plot = plots.ComparisonManyPlot(mt_nom,varPlots)
            plot.createFrame(optMode.replace("Opt","Mt_DataDrivenVsMC_"+"w="+str(w)+"_"), createRatio=True)

            moveLegend={"dx": -0.35,"dy": 0.05}
            plot.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
            histograms.addText(0.65, 0.3, optMode.replace("OptQCDTailKiller","R_{BB} ").replace("Plus",""), 25)
            histograms.addCmsPreliminaryText()
            histograms.addEnergyText()
            lumi=mt_inverted_faketaus_data.getDataset("Data").getLuminosity()
            histograms.addLuminosityText(x=None, y=None, lumi=lumi)

            plot.draw()
            plot.save()

            mt_var_qg.Delete()
            mt_var.Delete()
            mt_nom.Delete()

            #TFile.CurrentFile().Close("R")
            mt_baseline_faketaus_data.close()
            mt_inverted_faketaus_data.close()
            ROOT.gROOT.CloseFiles()
            ROOT.gROOT.GetListOfCanvases().Delete()
            ROOT.gDirectory.GetList().Delete()

            #print var_values
            #print nom_values
            
            # difference metrics
            num = 0
            denom = 0
            for i in range(0,len(nom_values)):
                num += var_values[i]*(var_values[i]-nom_values[i])**2
                denom += var_values[i]
            diff = num/denom
            diff_list.append(diff)
        diff_opt.append(diff_list)

    os.system("rm MtOptimal/*")
    os.system("mkdir -p MtOptimal")
    print "\nWeights:\t",w_list,'\n'
    optimalWeights = {}
    for i in range(0,len(diff_opt)):
        print optModes[i]
        print "Differences:\t",diff_opt[i],"- Optimal: w =",w_list[diff_opt[i].index(min(diff_opt[i]))]
        optimalWeights[optModes[i]] = w_list[diff_opt[i].index(min(diff_opt[i]))]
        command = "cp *" + str(w_list[diff_opt[i].index(min(diff_opt[i]))])+"*"+optModes[i].replace("Opt","") + ".eps MtOptimal"
        os.system(command)
    print optimalWeights
    writeWeightsToFile("OptimalWeights.py",optimalWeights)
    writeNormalizationToFile("QCDPlusEWKFakeTauNormalizationFactors.py",normalization_var_qg,labels)
Example #4
0
def main(argv):

    dirs = []
    if len(sys.argv) < 2:
        usage()

    dirs.append(sys.argv[1])

    QCDInvertedNormalization = QCDInvertedNormalizationFactors.QCDInvertedNormalization
    QCDInvertedNormalizationFilteredEWKFakeTaus = QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.QCDInvertedNormalization
    analysis = "signalAnalysisInvertedTau"
    optModes = []
    #optModes.append("OptQCDTailKillerZeroPlus")
    optModes.append("OptQCDTailKillerLoosePlus")
    optModes.append("OptQCDTailKillerMediumPlus")
    optModes.append("OptQCDTailKillerTightPlus")
    #optModes.append("OptQCDTailKillerVeryTightPlus")
    #optModes.append("OnlyGenuineMCTausFalse")
    #optModes.append("OnlyGenuineMCTausTrue")

    #Optimal: 0.8, 0.82, 0.9
    #w1_list = [0.8, 0.82, 0.84, 0.87]
    #w1_list = [0.8, 0.82, 0.9, 1]
    w1_list = [0.9]

    defaultBinning = systematics.getBinningForPlot("shapeTransverseMass")

    diff_opt = []
    for optMode in optModes:
        diff_list = []
        for w1 in w1_list:
            var_values = []
            nom_values = []
            w2 = 1 - w1

            color = 1

            #signal

            dirs_signal = ["../../SignalAnalysis_140605_143702/"]
            datasets_signal = dataset.getDatasetsFromMulticrabDirs(
                dirs_signal,
                dataEra=dataEra,
                searchMode=searchMode,
                analysisName=analysis.replace("InvertedTau", ""),
                optimizationMode=optMode)

            datasets_signal.updateNAllEventsToPUWeighted()
            datasets_signal.loadLuminosities()

            datasets_signal.remove(
                filter(lambda name: "TTToHplus" in name,
                       datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(
                filter(lambda name: "HplusTB" in name,
                       datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(
                filter(lambda name: "Hplus_taunu_t-channel" in name,
                       datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(
                filter(lambda name: "Hplus_taunu_tW-channel" in name,
                       datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(
                filter(lambda name: "TTJets_SemiLept" in name,
                       datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(
                filter(lambda name: "TTJets_FullLept" in name,
                       datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(
                filter(lambda name: "TTJets_Hadronic" in name,
                       datasets_signal.getAllDatasetNames()))

            plots.mergeRenameReorderForDataMC(datasets_signal)

            datasets_signal.merge(
                "EWK",
                ["TTJets", "WJets", "DYJetsToLL", "SingleTop", "Diboson"])

            mtplot_signalfaketaus = plots.DataMCPlot(
                datasets_signal, "shapeEWKFakeTausTransverseMass")
            mt_signalfaketaus = mtplot_signalfaketaus.histoMgr.getHisto(
                "EWK").getRootHisto().Clone("shapeEWKFakeTausTransverseMass")
            mt_signalfaketaus.SetName("BaselineFakeTaus")

            myBinning = [0, 20, 40, 60, 80, 100, 120, 140, 160, 200, 400]
            myArray = array.array("d", myBinning)

            fitBinning = []
            for i in range(0, 45):
                fitBinning.append(i * 10)
            fitArray = array.array("d", fitBinning)

            mt_baseline = mt_signalfaketaus

            #rangeMin = mt_signalfaketaus.GetXaxis().GetXmin()
            #rangeMax = mt_signalfaketaus.GetXaxis().GetXmax()
            #theFit = TF1('theFit',FitFunction(),rangeMin,rangeMax,4)
            #theFit.SetParLimits(0,0.5,10000)
            #theFit.SetParLimits(1,90,10000)
            #theFit.SetParLimits(2,30,10000)
            #theFit.SetParLimits(3,0.001,10000)
            #mt_signalfaketaus.Fit(theFit,"R")
            #theFit.SetRange(mt_signalfaketaus.GetXaxis().GetXmin(),mt_signalfaketaus.GetXaxis().GetXmax())
            #theFit.SetLineStyle(2)
            #theFit.SetLineColor(4)
            #theFit.SetLineWidth(3)
            #theFit.Draw()
            #mt_corr = theFit.GetHistogram()
            #mt_corr = mt_corr.Rebin(len(fitBinning)-1,"",fitArray)
            #mt_corr.Scale(mt_baseline.GetMaximum()/mt_corr.GetMaximum())

            for HISTONAME in histoNameList:
                var = False
                if HISTONAME == "shapeEWKGenuineTausTransverseMass":
                    var = True
                datasets = dataset.getDatasetsFromMulticrabDirs(
                    dirs,
                    dataEra=dataEra,
                    searchMode=searchMode,
                    analysisName=analysis,
                    optimizationMode=optMode)

                datasets.updateNAllEventsToPUWeighted()
                datasets.loadLuminosities()

                plots.mergeRenameReorderForDataMC(datasets)

                datasets.merge(
                    "EWK",
                    ["TTJets", "WJets", "DYJetsToLL", "SingleTop", "Diboson"])

                histonames = datasets.getDataset("Data").getDirectoryContent(
                    HISTONAME)

                bins = []
                for histoname in histonames:
                    binname = histoname.replace(HISTONAME, "")
                    if not binname == "Inclusive":
                        bins.append(binname)

                for i, bin in enumerate(bins):
                    mtplot = plots.DataMCPlot(
                        datasets, HISTONAME + "/" + HISTONAME + bin)

                    if i == 0:
                        mt = mtplot.histoMgr.getHisto(
                            "Data").getRootHisto().Clone(HISTONAME + "/" +
                                                         HISTONAME + bin)
                        mt_ewk = mtplot.histoMgr.getHisto(
                            "EWK").getRootHisto().Clone(HISTONAME + "/" +
                                                        HISTONAME + bin)
                        mtn = mtplot.histoMgr.getHisto(
                            "Data").getRootHisto().Clone(HISTONAME + "/" +
                                                         HISTONAME + bin)
                        mtn_ewk = mtplot.histoMgr.getHisto(
                            "EWK").getRootHisto().Clone(HISTONAME + "/" +
                                                        HISTONAME + bin)

                        if var:
                            legendName = "QCD(Data)+EWK+t#bar{t}(Data, mis-ID. #tau)"
                        else:
                            legendName = "QCD(Data)+EWK+t#bar{t}(MC, mis-ID. #tau)"
                        legendName = legendName.replace("Plus", "")
                        mt.SetName(legendName)
                        mt.SetLineColor(color)
                        mt.Add(mt_ewk, -1)
                        mtn.Add(mtn_ewk, -1)
                        mtn.Scale(QCDInvertedNormalization[str(i)])

                        if var:
                            scale = w1 * QCDInvertedNormalizationFilteredEWKFakeTaus[
                                str(
                                    i
                                )] + w2 * QCDInvertedNormalizationFilteredEWKFakeTaus[
                                    str(i) + "EWK_FakeTaus"]
                            mt.Scale(scale)
                        else:
                            mt.Scale(QCDInvertedNormalization[str(i)])
                        color += 1
                        if color == 5:
                            color += 1
                    else:
                        h = mtplot.histoMgr.getHisto(
                            "Data").getRootHisto().Clone(HISTONAME + "/" +
                                                         HISTONAME + bin)
                        mt_ewk = mtplot.histoMgr.getHisto(
                            "EWK").getRootHisto().Clone(HISTONAME + "/" +
                                                        HISTONAME + bin)
                        hn = mtplot.histoMgr.getHisto(
                            "Data").getRootHisto().Clone(HISTONAME + "/" +
                                                         HISTONAME + bin)
                        mtn_ewk = mtplot.histoMgr.getHisto(
                            "EWK").getRootHisto().Clone(HISTONAME + "/" +
                                                        HISTONAME + bin)

                        h.Add(mt_ewk, -1)
                        hn.Add(mtn_ewk, -1)
                        hn.Scale(QCDInvertedNormalization[str(i)])

                        if var:
                            scale = w1 * QCDInvertedNormalizationFilteredEWKFakeTaus[
                                str(
                                    i
                                )] + w2 * QCDInvertedNormalizationFilteredEWKFakeTaus[
                                    str(i) + "EWK_FakeTaus"]
                            h.Scale(scale)
                        else:
                            h.Scale(QCDInvertedNormalization[str(i)])
                        mt.Add(h)
                        mtn.Add(hn)

                #mt = mt.Rebin(len(myBinning)-1,"",myArray)
                #mt_corr = mt_corr.Rebin(len(myBinning)-1,"",myArray)

                if not var:
                    mt.Add(mt_baseline)
                    #mt.Add(mt_corr)

                #myBinning = []
                #for i in range(0,11):
                #    myBinning.append(20*i)
                #myBinning.append(400)

                #myArray = array.array("d",defaultBinning)
                mt = mt.Rebin(len(myBinning) - 1, "", myArray)

                for i in range(0, mt.GetSize()):
                    if var:
                        var_values.append(mt.GetBinContent(i))
                    else:
                        nom_values.append(mt.GetBinContent(i))

                if var:
                    #mt.SetLineStyle(2)
                    var_hist = mt
                else:
                    #mt.SetLineStyle(2)
                    nom_hist = mt

                style = tdrstyle.TDRStyle()

                #gStyle.SetOptStat(1101)
                #mt_data.SetStats(1)
                #gPad.Update()
                bins = [0, 390, 400]
                arr = array.array("d", bins)
                mtn = mtn.Rebin(len(bins) - 1, "", arr)
                plot_data = plots.PlotBase()
                plot_data.histoMgr.appendHisto(histograms.Histo(mtn, "Data"))
                plot_data.createFrame("Data_" + HISTONAME + "_" + optMode +
                                      "_" + str(w1))
                plot_data.draw()
                plot_data.save()

            plot = plots.ComparisonPlot(nom_hist, var_hist)
            plot.createFrame(optMode.replace(
                "Opt", "Mt_" + "w1=" + str(w1) + "_w2=" + str(w2) +
                "_DataDrivenVsMC_"),
                             createRatio=True)

            moveLegend = {"dx": -0.295, "dy": 0.05}
            plot.setLegend(
                histograms.moveLegend(histograms.createLegend(), **moveLegend))
            histograms.addText(
                0.65, 0.20,
                optMode.replace("OptQCDTailKiller",
                                "R_{BB} ").replace("Plus", ""), 25)
            histograms.addCmsPreliminaryText()
            histograms.addEnergyText()
            lumi = datasets.getDataset("Data").getLuminosity()
            histograms.addLuminosityText(x=None, y=None, lumi=lumi)

            plot.draw()
            plot.save()

            num = 0
            denom = 0
            #print var_values
            for i in range(0, len(nom_values)):
                num += var_values[i] * (var_values[i] - nom_values[i])**2
                denom += var_values[i]
            diff = num / denom
            diff_list.append(diff)
        diff_opt.append(diff_list)

    print w1_list, '\n'
    for i in range(0, len(diff_opt)):
        print diff_opt[i]
        print w1_list[diff_opt[i].index(min(diff_opt[i]))]

    mt_baseline = mt_baseline.Rebin(len(bins) - 1, "", arr)
    plot_bft = plots.PlotBase()
    plot_bft.histoMgr.appendHisto(histograms.Histo(mt_baseline, "baseline"))
    #mt_corr.Scale(2)
    #plot_bft.histoMgr.appendHisto(histograms.Histo(mt_corr,"test"))

    #rangeMin = mt_signalfaketaus.GetXaxis().GetXmin()
    #rangeMax = mt_signalfaketaus.GetXaxis().GetXmax()
    #theFit = TF1('theFit',FitFunction(),rangeMin,rangeMax,4)
    #theFit.SetParLimits(0,0.5,10000)
    #theFit.SetParLimits(1,90,10000)
    #theFit.SetParLimits(2,30,10000)
    #theFit.SetParLimits(3,0.001,10000)
    #mt_signalfaketaus.Fit(theFit,"R")
    #theFit.SetRange(mt_signalfaketaus.GetXaxis().GetXmin(),mt_signalfaketaus.GetXaxis().GetXmax())
    #theFit.SetLineStyle(2)
    #theFit.SetLineColor(4)
    #theFit.SetLineWidth(3)
    #theFit.Draw()

    #mt_corr = theFit.GetHistogram()
    #mt_corr = mt_corr.Rebin(len(fitBinning)-1,"",fitArray)
    #mt_corr.Scale(mt_baseline.GetMaximum()/mt_corr.GetMaximum())
    #plot_bft.histoMgr.appendHisto(histograms.Histo(mt_corr,"test"))
    #plot_bft.histoMgr.appendHisto(histograms.Histo(theFit,"theFit"))

    plot_bft.createFrame('BaselineFakeTaus')
    plot_bft.draw()
    plot_bft.save()
Example #5
0
def main(argv):

    dirs = []
    if len(sys.argv) < 2:
        usage()

    dirs.append(sys.argv[1])

    QCDInvertedNormalization = QCDInvertedNormalizationFactors.QCDInvertedNormalization
    QCDInvertedNormalizationFilteredEWKFakeTaus = QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.QCDInvertedNormalization
    analysis = "signalAnalysisInvertedTau"
    optModes = []
    #optModes.append("OptQCDTailKillerZeroPlus")
    optModes.append("OptQCDTailKillerLoosePlus")
    optModes.append("OptQCDTailKillerMediumPlus")
    optModes.append("OptQCDTailKillerTightPlus")
    #optModes.append("OptQCDTailKillerVeryTightPlus")
    #optModes.append("OnlyGenuineMCTausFalse")
    #optModes.append("OnlyGenuineMCTausTrue")

    for optMode in optModes:
        plot = plots.PlotBase()
        color = 1

        dirs_signal = ["../../SignalAnalysis_140605_143702/"]
        datasets_signal = dataset.getDatasetsFromMulticrabDirs(
            dirs_signal,
            dataEra=dataEra,
            searchMode=searchMode,
            analysisName=analysis.replace("InvertedTau", ""),
            optimizationMode=optMode)

        datasets_signal.updateNAllEventsToPUWeighted()
        datasets_signal.loadLuminosities()

        datasets_signal.remove(
            filter(lambda name: "TTToHplus" in name,
                   datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(
            filter(lambda name: "HplusTB" in name,
                   datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(
            filter(lambda name: "Hplus_taunu_t-channel" in name,
                   datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(
            filter(lambda name: "Hplus_taunu_tW-channel" in name,
                   datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(
            filter(lambda name: "TTJets_SemiLept" in name,
                   datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(
            filter(lambda name: "TTJets_FullLept" in name,
                   datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(
            filter(lambda name: "TTJets_Hadronic" in name,
                   datasets_signal.getAllDatasetNames()))

        plots.mergeRenameReorderForDataMC(datasets_signal)

        datasets_signal.merge(
            "EWK", ["TTJets", "WJets", "DYJetsToLL", "SingleTop", "Diboson"])

        mtplot_signalfaketaus = plots.DataMCPlot(
            datasets_signal, "shapeEWKFakeTausTransverseMass")
        mt_signalfaketaus = mtplot_signalfaketaus.histoMgr.getHisto(
            "EWK").getRootHisto().Clone("shapeEWKFakeTausTransverseMass")

        for HISTONAME in histoNameList:
            datasets = dataset.getDatasetsFromMulticrabDirs(
                dirs,
                dataEra=dataEra,
                searchMode=searchMode,
                analysisName=analysis,
                optimizationMode=optMode)

            datasets.updateNAllEventsToPUWeighted()
            datasets.loadLuminosities()

            plots.mergeRenameReorderForDataMC(datasets)

            datasets.merge(
                "EWK",
                ["TTJets", "WJets", "DYJetsToLL", "SingleTop", "Diboson"])

            histonames = datasets.getDataset("Data").getDirectoryContent(
                HISTONAME)

            bins = []
            for histoname in histonames:
                binname = histoname.replace(HISTONAME, "")
                if not binname == "Inclusive":
                    bins.append(binname)

            invjet_name = "MTInvertedTauIdAfterAllSelectionsPlusJetFakeTau"
            basejet_name = "MTBaselineTauIdAfterAllSelectionsPlusJetFakeTau"

            for i, bin in enumerate(bins):
                mtplot = plots.DataMCPlot(datasets,
                                          HISTONAME + "/" + HISTONAME + bin)

                if i == 0:
                    mt = mtplot.histoMgr.getHisto("Data").getRootHisto().Clone(
                        HISTONAME + "/" + HISTONAME + bin)
                    mt_ewk = mtplot.histoMgr.getHisto(
                        "EWK").getRootHisto().Clone(HISTONAME + "/" +
                                                    HISTONAME + bin)

                    if HISTONAME == "shapeEWKGenuineTausTransverseMass":
                        legendName = "Data-driven Fake Taus"
                    else:
                        legendName = "Simulated Fake Taus"
                    legendName = legendName.replace("Plus", "")
                    mt.SetName(legendName)
                    mt.SetLineColor(color)
                    mt.Add(mt_ewk, -1)

                    if HISTONAME == "shapeEWKGenuineTausTransverseMass":
                        mt.Scale(QCDInvertedNormalizationFilteredEWKFakeTaus[
                            str(i)])
                    else:
                        mt.Scale(QCDInvertedNormalization[str(i)])
                    color += 1
                    if color == 5:
                        color += 1
                else:
                    h = mtplot.histoMgr.getHisto("Data").getRootHisto().Clone(
                        HISTONAME + "/" + HISTONAME + bin)
                    mt_ewk = mtplot.histoMgr.getHisto(
                        "EWK").getRootHisto().Clone(HISTONAME + "/" +
                                                    HISTONAME + bin)
                    h.Add(mt_ewk, -1)
                    if HISTONAME == "shapeEWKGenuineTausTransverseMass":
                        h.Scale(QCDInvertedNormalizationFilteredEWKFakeTaus[
                            str(i)])
                    else:
                        h.Scale(QCDInvertedNormalization[str(i)])
                    mt.Add(h)
            if HISTONAME == "shapeTransverseMass":
                mt.Add(mt_signalfaketaus)

            plot.histoMgr.appendHisto(histograms.Histo(mt, mt.GetName()))

            style = tdrstyle.TDRStyle()

        #plot.createFrame("mt")
        #plot.createFrame(HISTONAME.replace("shape","final"))
        plot.createFrame(optMode.replace("Opt", "Mt_DataDrivenVsMC_"))
        moveLegend = {"dx": -0.3, "dy": 0.}
        plot.setLegend(
            histograms.moveLegend(histograms.createLegend(), **moveLegend))

        histograms.addText(
            0.65, 0.20,
            optMode.replace("OptQCDTailKiller", "R_{BB} ").replace("Plus", ""),
            25)
        histograms.addCmsPreliminaryText()
        histograms.addEnergyText()
        lumi = datasets.getDataset("Data").getLuminosity()
        histograms.addLuminosityText(x=None, y=None, lumi=lumi)

        plot.draw()
        plot.save()
def main(argv):

    dirs = []
    if len(sys.argv) < 2:
	usage()

    dirs.append(sys.argv[1])

    QCDInvertedNormalization = QCDInvertedNormalizationFactors.QCDInvertedNormalization
    QCDInvertedNormalizationFilteredEWKFakeTaus = QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.QCDInvertedNormalization
    analysis = "signalAnalysisInvertedTau"
    optModes = []
    #optModes.append("OptQCDTailKillerZeroPlus")
    optModes.append("OptQCDTailKillerLoosePlus") 
    optModes.append("OptQCDTailKillerMediumPlus") 
    optModes.append("OptQCDTailKillerTightPlus") 
    #optModes.append("OptQCDTailKillerVeryTightPlus")
    #optModes.append("OnlyGenuineMCTausFalse")
    #optModes.append("OnlyGenuineMCTausTrue")

    for optMode in optModes:
        plot = plots.PlotBase()
        color = 1

        dirs_signal = ["../../SignalAnalysis_140605_143702/"]
        datasets_signal = dataset.getDatasetsFromMulticrabDirs(dirs_signal,dataEra=dataEra,  searchMode=searchMode, analysisName=analysis.replace("InvertedTau",""), optimizationMode=optMode)
        
        datasets_signal.updateNAllEventsToPUWeighted()
        datasets_signal.loadLuminosities()
        
        datasets_signal.remove(filter(lambda name: "TTToHplus" in name, datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(filter(lambda name: "HplusTB" in name, datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(filter(lambda name: "Hplus_taunu_t-channel" in name, datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(filter(lambda name: "Hplus_taunu_tW-channel" in name, datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(filter(lambda name: "TTJets_SemiLept" in name, datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(filter(lambda name: "TTJets_FullLept" in name, datasets_signal.getAllDatasetNames()))
        datasets_signal.remove(filter(lambda name: "TTJets_Hadronic" in name, datasets_signal.getAllDatasetNames()))
        
        plots.mergeRenameReorderForDataMC(datasets_signal)
        
        datasets_signal.merge("EWK", [
            "TTJets",
            "WJets",
            "DYJetsToLL",
            "SingleTop",
            "Diboson"
            ])
        
        mtplot_signalfaketaus = plots.DataMCPlot(datasets_signal, "shapeEWKFakeTausTransverseMass")
        mt_signalfaketaus = mtplot_signalfaketaus.histoMgr.getHisto("EWK").getRootHisto().Clone("shapeEWKFakeTausTransverseMass")
        
        for HISTONAME in histoNameList:
            datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra,  searchMode=searchMode, analysisName=analysis, optimizationMode=optMode)

            datasets.updateNAllEventsToPUWeighted()
            datasets.loadLuminosities()
            
            plots.mergeRenameReorderForDataMC(datasets)

            datasets.merge("EWK", [
                            "TTJets",
                            "WJets",
                            "DYJetsToLL",
                            "SingleTop",
                            "Diboson"
                          ])

            histonames = datasets.getDataset("Data").getDirectoryContent(HISTONAME)

            bins = []
            for histoname in histonames:
                binname = histoname.replace(HISTONAME,"")
                if not binname == "Inclusive":
                    bins.append(binname)

            invjet_name = "MTInvertedTauIdAfterAllSelectionsPlusJetFakeTau"
            basejet_name = "MTBaselineTauIdAfterAllSelectionsPlusJetFakeTau"

            for i,bin in enumerate(bins):
                mtplot = plots.DataMCPlot(datasets, HISTONAME+"/"+HISTONAME+bin)

                if i == 0:
                    mt = mtplot.histoMgr.getHisto("Data").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)
                    mt_ewk = mtplot.histoMgr.getHisto("EWK").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)
                    
                    if HISTONAME == "shapeEWKGenuineTausTransverseMass":
                        legendName = "Data-driven Fake Taus"
                    else:
                        legendName = "Simulated Fake Taus"
                    legendName = legendName.replace("Plus","")
                    mt.SetName(legendName)
                    mt.SetLineColor(color)
                    mt.Add(mt_ewk,-1)
                    
                    if HISTONAME == "shapeEWKGenuineTausTransverseMass":
                        mt.Scale(QCDInvertedNormalizationFilteredEWKFakeTaus[str(i)])
                    else:
                        mt.Scale(QCDInvertedNormalization[str(i)])
                    color += 1
                    if color == 5:
                        color += 1
                else:
                    h = mtplot.histoMgr.getHisto("Data").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)
                    mt_ewk = mtplot.histoMgr.getHisto("EWK").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)
                    h.Add(mt_ewk,-1)
                    if HISTONAME == "shapeEWKGenuineTausTransverseMass":
                        h.Scale(QCDInvertedNormalizationFilteredEWKFakeTaus[str(i)])
                    else:
                        h.Scale(QCDInvertedNormalization[str(i)])
                    mt.Add(h)
            if HISTONAME == "shapeTransverseMass":
                mt.Add(mt_signalfaketaus)

            
            plot.histoMgr.appendHisto(histograms.Histo(mt,mt.GetName()))
            
            style = tdrstyle.TDRStyle()

        #plot.createFrame("mt")
        #plot.createFrame(HISTONAME.replace("shape","final"))
        plot.createFrame(optMode.replace("Opt","Mt_DataDrivenVsMC_"))
        moveLegend={"dx": -0.3,"dy": 0.}
        plot.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))


        histograms.addText(0.65, 0.20, optMode.replace("OptQCDTailKiller","R_{BB} ").replace("Plus",""), 25)
        histograms.addCmsPreliminaryText()
        histograms.addEnergyText()
        lumi=datasets.getDataset("Data").getLuminosity()
        histograms.addLuminosityText(x=None, y=None, lumi=lumi)

        plot.draw()
        plot.save()
def main(argv):

    dirs = []
    if len(sys.argv) < 2:
	usage()

    dirs.append(sys.argv[1])

    QCDInvertedNormalization = QCDInvertedNormalizationFactors.QCDInvertedNormalization
    QCDInvertedNormalizationFilteredEWKFakeTaus = QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.QCDInvertedNormalization
    analysis = "signalAnalysisInvertedTau"
    optModes = []
    #optModes.append("OptQCDTailKillerZeroPlus")
    optModes.append("OptQCDTailKillerLoosePlus") 
    optModes.append("OptQCDTailKillerMediumPlus") 
    optModes.append("OptQCDTailKillerTightPlus") 
    #optModes.append("OptQCDTailKillerVeryTightPlus")
    #optModes.append("OnlyGenuineMCTausFalse")
    #optModes.append("OnlyGenuineMCTausTrue")

    #Optimal: 0.8, 0.82, 0.9
    #w1_list = [0.8, 0.82, 0.84, 0.87]
    #w1_list = [0.8, 0.82, 0.9, 1]
    w1_list = [0.9]

    defaultBinning = systematics.getBinningForPlot("shapeTransverseMass")

    diff_opt = []
    for optMode in optModes:
        diff_list = []
        for w1 in w1_list:
            var_values = []
            nom_values = []
            w2 = 1 - w1
            
            color = 1

            #signal

            dirs_signal = ["../../SignalAnalysis_140605_143702/"]
            datasets_signal = dataset.getDatasetsFromMulticrabDirs(dirs_signal,dataEra=dataEra,  searchMode=searchMode, analysisName=analysis.replace("InvertedTau",""), optimizationMode=optMode)
            
            datasets_signal.updateNAllEventsToPUWeighted()
            datasets_signal.loadLuminosities()
            
            datasets_signal.remove(filter(lambda name: "TTToHplus" in name, datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(filter(lambda name: "HplusTB" in name, datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(filter(lambda name: "Hplus_taunu_t-channel" in name, datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(filter(lambda name: "Hplus_taunu_tW-channel" in name, datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(filter(lambda name: "TTJets_SemiLept" in name, datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(filter(lambda name: "TTJets_FullLept" in name, datasets_signal.getAllDatasetNames()))
            datasets_signal.remove(filter(lambda name: "TTJets_Hadronic" in name, datasets_signal.getAllDatasetNames()))
            
            plots.mergeRenameReorderForDataMC(datasets_signal)
            
            datasets_signal.merge("EWK", [
                "TTJets",
                "WJets",
                "DYJetsToLL",
                "SingleTop",
                "Diboson"
                ])
            
            mtplot_signalfaketaus = plots.DataMCPlot(datasets_signal, "shapeEWKFakeTausTransverseMass")
            mt_signalfaketaus = mtplot_signalfaketaus.histoMgr.getHisto("EWK").getRootHisto().Clone("shapeEWKFakeTausTransverseMass")
            mt_signalfaketaus.SetName("BaselineFakeTaus")

            myBinning = [0, 20, 40, 60, 80, 100, 120, 140, 160, 200, 400]
            myArray = array.array("d",myBinning)

            fitBinning = []
            for i in range(0,45):
               fitBinning.append(i*10) 
            fitArray = array.array("d",fitBinning)

            mt_baseline = mt_signalfaketaus

            #rangeMin = mt_signalfaketaus.GetXaxis().GetXmin()
            #rangeMax = mt_signalfaketaus.GetXaxis().GetXmax()
            #theFit = TF1('theFit',FitFunction(),rangeMin,rangeMax,4)
            #theFit.SetParLimits(0,0.5,10000)
            #theFit.SetParLimits(1,90,10000)
            #theFit.SetParLimits(2,30,10000)
            #theFit.SetParLimits(3,0.001,10000)
            #mt_signalfaketaus.Fit(theFit,"R")
            #theFit.SetRange(mt_signalfaketaus.GetXaxis().GetXmin(),mt_signalfaketaus.GetXaxis().GetXmax())
            #theFit.SetLineStyle(2)
            #theFit.SetLineColor(4)
            #theFit.SetLineWidth(3)
            #theFit.Draw()
            #mt_corr = theFit.GetHistogram()
            #mt_corr = mt_corr.Rebin(len(fitBinning)-1,"",fitArray)
            #mt_corr.Scale(mt_baseline.GetMaximum()/mt_corr.GetMaximum())
            
            for HISTONAME in histoNameList:
                var = False
                if HISTONAME == "shapeEWKGenuineTausTransverseMass":
                    var = True
                datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra,  searchMode=searchMode, analysisName=analysis, optimizationMode=optMode)

                datasets.updateNAllEventsToPUWeighted()
                datasets.loadLuminosities()

                plots.mergeRenameReorderForDataMC(datasets)

                datasets.merge("EWK", [
                                "TTJets",
                                "WJets",
                                "DYJetsToLL",
                                "SingleTop",
                                "Diboson"
                              ])

                histonames = datasets.getDataset("Data").getDirectoryContent(HISTONAME)

                bins = []
                for histoname in histonames:
                    binname = histoname.replace(HISTONAME,"")
                    if not binname == "Inclusive":
                        bins.append(binname)

                for i,bin in enumerate(bins):
                    mtplot = plots.DataMCPlot(datasets, HISTONAME+"/"+HISTONAME+bin)

                    if i == 0:
                        mt = mtplot.histoMgr.getHisto("Data").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)
                        mt_ewk = mtplot.histoMgr.getHisto("EWK").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)
                        mtn = mtplot.histoMgr.getHisto("Data").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin) 
                        mtn_ewk = mtplot.histoMgr.getHisto("EWK").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)

                        if var:
                            legendName = "QCD(Data)+EWK+t#bar{t}(Data, mis-ID. #tau)"
                        else:
                            legendName = "QCD(Data)+EWK+t#bar{t}(MC, mis-ID. #tau)"
                        legendName = legendName.replace("Plus","")
                        mt.SetName(legendName)
                        mt.SetLineColor(color)
                        mt.Add(mt_ewk,-1)
                        mtn.Add(mtn_ewk,-1)
                        mtn.Scale(QCDInvertedNormalization[str(i)])

                        if var:
                            scale = w1*QCDInvertedNormalizationFilteredEWKFakeTaus[str(i)] + w2*QCDInvertedNormalizationFilteredEWKFakeTaus[str(i)+"EWK_FakeTaus"]
                            mt.Scale(scale)
                        else:
                            mt.Scale(QCDInvertedNormalization[str(i)])
                        color += 1
                        if color == 5:
                            color += 1
                    else:
                        h = mtplot.histoMgr.getHisto("Data").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)
                        mt_ewk = mtplot.histoMgr.getHisto("EWK").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)
                        hn = mtplot.histoMgr.getHisto("Data").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)
                        mtn_ewk = mtplot.histoMgr.getHisto("EWK").getRootHisto().Clone(HISTONAME+"/"+HISTONAME+bin)

                        h.Add(mt_ewk,-1)
                        hn.Add(mtn_ewk,-1)
                        hn.Scale(QCDInvertedNormalization[str(i)])

                        if var:
                            scale = w1*QCDInvertedNormalizationFilteredEWKFakeTaus[str(i)] + w2*QCDInvertedNormalizationFilteredEWKFakeTaus[str(i)+"EWK_FakeTaus"]
                            h.Scale(scale)
                        else:
                            h.Scale(QCDInvertedNormalization[str(i)])
                        mt.Add(h)
                        mtn.Add(hn)

                #mt = mt.Rebin(len(myBinning)-1,"",myArray)
                #mt_corr = mt_corr.Rebin(len(myBinning)-1,"",myArray)
                
                if not var:
                    mt.Add(mt_baseline)
                    #mt.Add(mt_corr) 
                    
                #myBinning = []    
                #for i in range(0,11):
                #    myBinning.append(20*i)
                #myBinning.append(400)
                
                #myArray = array.array("d",defaultBinning)
                mt = mt.Rebin(len(myBinning)-1,"",myArray)

                for i in range(0,mt.GetSize()):
                    if var:
                        var_values.append(mt.GetBinContent(i))
                    else:
                        nom_values.append(mt.GetBinContent(i))
                
                if var:
                    #mt.SetLineStyle(2)
                    var_hist = mt
                else:
                    #mt.SetLineStyle(2)
                    nom_hist = mt

                style = tdrstyle.TDRStyle()

                #gStyle.SetOptStat(1101)
                #mt_data.SetStats(1)
                #gPad.Update()
                bins = [0, 390, 400]
                arr = array.array("d",bins)
                mtn = mtn.Rebin(len(bins)-1,"",arr)
                plot_data = plots.PlotBase()
                plot_data.histoMgr.appendHisto(histograms.Histo(mtn,"Data"))
                plot_data.createFrame("Data_"+HISTONAME+"_"+optMode+"_"+str(w1))
                plot_data.draw()
                plot_data.save()


            plot = plots.ComparisonPlot(nom_hist,var_hist)
            plot.createFrame(optMode.replace("Opt","Mt_"+"w1="+str(w1)+"_w2="+str(w2)+"_DataDrivenVsMC_"), createRatio=True)

            moveLegend={"dx": -0.295,"dy": 0.05}
            plot.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
            histograms.addText(0.65, 0.20, optMode.replace("OptQCDTailKiller","R_{BB} ").replace("Plus",""), 25)
            histograms.addCmsPreliminaryText()
            histograms.addEnergyText()
            lumi=datasets.getDataset("Data").getLuminosity()
            histograms.addLuminosityText(x=None, y=None, lumi=lumi)

            plot.draw()
            plot.save()

            num = 0
            denom = 0
            #print var_values
            for i in range(0,len(nom_values)):
                num += var_values[i]*(var_values[i]-nom_values[i])**2
                denom += var_values[i]
            diff = num/denom
            diff_list.append(diff)
        diff_opt.append(diff_list)

    print w1_list,'\n'
    for i in range(0,len(diff_opt)):
        print diff_opt[i]
        print w1_list[diff_opt[i].index(min(diff_opt[i]))]

    mt_baseline = mt_baseline.Rebin(len(bins)-1,"",arr)
    plot_bft = plots.PlotBase()
    plot_bft.histoMgr.appendHisto(histograms.Histo(mt_baseline,"baseline"))
    #mt_corr.Scale(2)
    #plot_bft.histoMgr.appendHisto(histograms.Histo(mt_corr,"test"))


    #rangeMin = mt_signalfaketaus.GetXaxis().GetXmin()
    #rangeMax = mt_signalfaketaus.GetXaxis().GetXmax()
    #theFit = TF1('theFit',FitFunction(),rangeMin,rangeMax,4)
    #theFit.SetParLimits(0,0.5,10000)
    #theFit.SetParLimits(1,90,10000)
    #theFit.SetParLimits(2,30,10000)
    #theFit.SetParLimits(3,0.001,10000)
    #mt_signalfaketaus.Fit(theFit,"R")
    #theFit.SetRange(mt_signalfaketaus.GetXaxis().GetXmin(),mt_signalfaketaus.GetXaxis().GetXmax())
    #theFit.SetLineStyle(2)
    #theFit.SetLineColor(4)
    #theFit.SetLineWidth(3)
    #theFit.Draw()

    #mt_corr = theFit.GetHistogram()
    #mt_corr = mt_corr.Rebin(len(fitBinning)-1,"",fitArray)
    #mt_corr.Scale(mt_baseline.GetMaximum()/mt_corr.GetMaximum())
    #plot_bft.histoMgr.appendHisto(histograms.Histo(mt_corr,"test"))
    #plot_bft.histoMgr.appendHisto(histograms.Histo(theFit,"theFit"))

    plot_bft.createFrame('BaselineFakeTaus')
    plot_bft.draw()
    plot_bft.save()
Example #8
0
def main(argv):

    dirs = []
    if len(sys.argv) < 2:
        usage()

    dirs.append(sys.argv[1])

    analysis = "signalAnalysisInvertedTau"
    optModes = []
    #optModes.append("")
    #optModes.append("OptQCDTailKillerLoosePlus")
    #optModes.append("OptQCDTailKillerMediumPlus")
    optModes.append("OptQCDTailKillerTightPlus")

    color = 1
    #plot = plots.PlotBase()
    jetRatios = []

    for HISTONAME in histonameList:
        for optMode in optModes:
            plot = plots.PlotBase()
            datasets = dataset.getDatasetsFromMulticrabDirs(
                dirs,
                dataEra=dataEra,
                searchMode=searchMode,
                analysisName=analysis,
                optimizationMode=optMode)

            datasets.updateNAllEventsToPUWeighted()
            datasets.loadLuminosities()

            plots.mergeRenameReorderForDataMC(datasets)

            datasets.merge(
                "EWK",
                ["TTJets", "WJets", "DYJetsToLL", "SingleTop", "Diboson"])

            histonames = datasets.getDataset("EWK").getDirectoryContent(
                HISTONAME)
            mtplot = plots.DataMCPlot(datasets, HISTONAME)
            mt = mtplot.histoMgr.getHisto("EWK").getRootHisto().Clone(
                HISTONAME)
            #legendName = legendName.replace("Plus","")
            mt.SetName("JetBalance")
            mt.SetLineColor(color)
            if HISTONAME == "InvertedAllCutsJetBalance":
                qinv = mt.GetBinContent(1)
                ginv = mt.GetBinContent(3)
            else:
                qbase = mt.GetBinContent(1)
                gbase = mt.GetBinContent(3)

            jetRatios.append(
                mt.GetBinContent(1) /
                (mt.GetBinContent(1) + mt.GetBinContent(3)))

            plot.histoMgr.appendHisto(histograms.Histo(mt, mt.GetName()))
            color = color + 1

            style = tdrstyle.TDRStyle()

            plot.createFrame(HISTONAME)
            #plot.createFrame(HISTONAME.replace("shape","final"))
            #plot.createFrame(optMode.replace("Opt","Mt_DataDrivenVsMC_"))
            #moveLegend={"dx": -0.3,"dy": 0.}
            #plot.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

            histograms.addCmsPreliminaryText()
            histograms.addEnergyText()
            lumi = datasets.getDataset("Data").getLuminosity()
            histograms.addLuminosityText(x=None, y=None, lumi=lumi)

            plot.draw()
            plot.save()

    print "Baseline All Cuts", qbase + gbase
    print "Inverted All Cuts", qinv + ginv
    gsf = qinv * gbase / (ginv * qbase)
    #print "Gluon jet SF:", gsf
    #print "Corrected Inverted Jet Balance:", qinv/(qinv+gsf*ginv), ", Baseline Jet Balance:", qbase/(qbase+gbase)
    for i in range(0, len(jetRatios)):
        print histonameList[i], ":", jetRatios[i]
Example #9
0
def main(argv):

    dirs = []
    if len(sys.argv) < 2:
	usage()

    dirs.append(sys.argv[1])

    analysis = "signalAnalysisInvertedTau"
    optModes = []
    #optModes.append("")
    #optModes.append("OptQCDTailKillerLoosePlus") 
    #optModes.append("OptQCDTailKillerMediumPlus") 
    optModes.append("OptQCDTailKillerTightPlus") 

    color = 1
    #plot = plots.PlotBase()
    jetRatios = []
    
    for HISTONAME in histonameList:            
        for optMode in optModes:
            plot = plots.PlotBase()
            datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra,  searchMode=searchMode, analysisName=analysis, optimizationMode=optMode)

            datasets.updateNAllEventsToPUWeighted()
            datasets.loadLuminosities()

            plots.mergeRenameReorderForDataMC(datasets)

            datasets.merge("EWK", [
                "TTJets",
                "WJets",
                "DYJetsToLL",
                "SingleTop",
                "Diboson"
                ])

            histonames = datasets.getDataset("EWK").getDirectoryContent(HISTONAME)
            mtplot = plots.DataMCPlot(datasets, HISTONAME)
            mt = mtplot.histoMgr.getHisto("EWK").getRootHisto().Clone(HISTONAME)
            #legendName = legendName.replace("Plus","")
            mt.SetName("JetBalance")
            mt.SetLineColor(color)
            if HISTONAME == "InvertedAllCutsJetBalance":
                qinv = mt.GetBinContent(1)
                ginv = mt.GetBinContent(3)
            else:
                qbase = mt.GetBinContent(1)
                gbase = mt.GetBinContent(3)

            jetRatios.append(mt.GetBinContent(1)/(mt.GetBinContent(1)+mt.GetBinContent(3)))
            
            plot.histoMgr.appendHisto(histograms.Histo(mt,mt.GetName()))
            color = color + 1 
            
            style = tdrstyle.TDRStyle()
        
            plot.createFrame(HISTONAME)
        #plot.createFrame(HISTONAME.replace("shape","final"))
        #plot.createFrame(optMode.replace("Opt","Mt_DataDrivenVsMC_"))
        #moveLegend={"dx": -0.3,"dy": 0.}
        #plot.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

            histograms.addCmsPreliminaryText()
            histograms.addEnergyText()
            lumi=datasets.getDataset("Data").getLuminosity()
            histograms.addLuminosityText(x=None, y=None, lumi=lumi)

            plot.draw()
            plot.save()

    print "Baseline All Cuts",qbase+gbase
    print "Inverted All Cuts",qinv+ginv
    gsf = qinv*gbase/(ginv*qbase)
    #print "Gluon jet SF:", gsf
    #print "Corrected Inverted Jet Balance:", qinv/(qinv+gsf*ginv), ", Baseline Jet Balance:", qbase/(qbase+gbase)
    for i in range(0,len(jetRatios)):
        print histonameList[i],":",jetRatios[i]