Esempio n. 1
0
def GetDatasetsFromDir(mcrab, opts, **kwargs):

    dataEra = kwargs.get("dataEra")
    searchMode = kwargs.get("searchMode")
    analysis = kwargs.get("analysis")
    optMode = kwargs.get("optMode")

    if opts.includeTasks != "":
        datasets = dataset.getDatasetsFromMulticrabDirs(
            [mcrab],
            dataEra=dataEra,
            searchMode=searchMode,
            analysisName=analysis,
            includeOnlyTasks=opts.includeTasks,
            optimizationMode=optMode)
    elif opts.excludeTasks != "":
        datasets = dataset.getDatasetsFromMulticrabDirs(
            [mcrab],
            dataEra=dataEra,
            searchMode=searchMode,
            analysisName=analysis,
            excludeTasks=opts.excludeTasks,
            optimizationMode=optMode)
        # excludeTasks="M_180|M_220|M_250"
    else:
        datasets = dataset.getDatasetsFromMulticrabDirs(
            [mcrab],
            dataEra=dataEra,
            searchMode=searchMode,
            analysisName=analysis,
            optimizationMode=optMode)
    return datasets
def GetDatasetsFromDir(opts):
    Verbose("Getting datasets")
    
    if (not opts.includeOnlyTasks and not opts.excludeTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs([opts.mcrab],
                                                        dataEra=opts.dataEra,
                                                        searchMode=opts.searchMode, 
                                                        analysisName=opts.analysisName,
                                                        optimizationMode=opts.optMode)
    elif (opts.includeOnlyTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs([opts.mcrab],
                                                        dataEra=opts.dataEra,
                                                        searchMode=opts.searchMode,
                                                        analysisName=opts.analysisName,
                                                        includeOnlyTasks=opts.includeOnlyTasks,
                                                        optimizationMode=opts.optMode)
    elif (opts.excludeTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs([opts.mcrab],
                                                        dataEra=opts.dataEra,
                                                        searchMode=opts.searchMode,
                                                        analysisName=opts.analysisName,
                                                        excludeTasks=opts.excludeTasks,
                                                        optimizationMode=opts.optMode)
    else:
        raise Exception("This should never be reached")
    return datasets
def GetDatasetsFromDir_secondDir(opts):
    Verbose("Getting datasets")
    usemcrab = "MyHplusAnalysis_180201_TrainingDRQuarks08"
    if (not opts.includeOnlyTasks and not opts.excludeTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs([usemcrab],
                                                        dataEra=opts.dataEra,
                                                        searchMode=opts.searchMode, 
                                                        analysisName=opts.analysisName,
                                                        optimizationMode=opts.optMode)
    elif (opts.includeOnlyTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs([usemcrab],
                                                        dataEra=opts.dataEra,
                                                        searchMode=opts.searchMode,
                                                        analysisName=opts.analysisName,
                                                        includeOnlyTasks=opts.includeOnlyTasks,
                                                        optimizationMode=opts.optMode)
    elif (opts.excludeTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs([usemcrab],
                                                        dataEra=opts.dataEra,
                                                        searchMode=opts.searchMode,
                                                        analysisName=opts.analysisName,
                                                        excludeTasks=opts.excludeTasks,
                                                        optimizationMode=opts.optMode)
    else:
        raise Exception("This should never be reached")
    return datasets
def GetDatasetsFromDir_secondDir(opts):
    Verbose("Getting datasets")
    usemcrab = "MyHplusAnalysis_180201_TrainingDRQuarks08"
    if (not opts.includeOnlyTasks and not opts.excludeTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs(
            [usemcrab],
            dataEra=opts.dataEra,
            searchMode=opts.searchMode,
            analysisName=opts.analysisName,
            optimizationMode=opts.optMode)
    elif (opts.includeOnlyTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs(
            [usemcrab],
            dataEra=opts.dataEra,
            searchMode=opts.searchMode,
            analysisName=opts.analysisName,
            includeOnlyTasks=opts.includeOnlyTasks,
            optimizationMode=opts.optMode)
    elif (opts.excludeTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs(
            [usemcrab],
            dataEra=opts.dataEra,
            searchMode=opts.searchMode,
            analysisName=opts.analysisName,
            excludeTasks=opts.excludeTasks,
            optimizationMode=opts.optMode)
    else:
        raise Exception("This should never be reached")
    return datasets
Esempio n. 5
0
def GetDatasetsFromDir(opts):
    Verbose("Getting datasets")

    if (not opts.includeOnlyTasks and not opts.excludeTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs(
            [opts.mcrab],
            dataEra=opts.dataEra,
            searchMode=opts.searchMode,
            analysisName=opts.analysisName,
            optimizationMode=opts.optMode)
    elif (opts.includeOnlyTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs(
            [opts.mcrab],
            dataEra=opts.dataEra,
            searchMode=opts.searchMode,
            analysisName=opts.analysisName,
            includeOnlyTasks=opts.includeOnlyTasks,
            optimizationMode=opts.optMode)
    elif (opts.excludeTasks):
        datasets = dataset.getDatasetsFromMulticrabDirs(
            [opts.mcrab],
            dataEra=opts.dataEra,
            searchMode=opts.searchMode,
            analysisName=opts.analysisName,
            excludeTasks=opts.excludeTasks,
            optimizationMode=opts.optMode)
    else:
        raise Exception("This should never be reached")
    return datasets
Esempio n. 6
0
def main():

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

    paths = [sys.argv[1]]
    analysis = "Hplus2tbAnalysis"
    hName = "associatedTPt"
    plotname = analysis + "_" + hName

    datasetsHiggs = dataset.getDatasetsFromMulticrabDirs(
        paths,
        analysisName=analysis,
        includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTauB_M_")
    datasetsTT = dataset.getDatasetsFromMulticrabDirs(paths,
                                                      analysisName=analysis,
                                                      includeOnlyTasks="TT")
    datasetsTT.merge("MC", ["TT", "TT_ext"], keepSources=True)

    style = tdrstyle.TDRStyle()

    dataset1 = datasetsHiggs.getDataset(
        "ChargedHiggs_HplusTB_HplusToTauB_M_200").getDatasetRootHisto(hName)
    dataset2 = datasetsTT.getDataset("MC").getDatasetRootHisto(hName)
    #    dataset1.normalizeToOne()
    dataset2.normalizeToOne()

    histo1 = dataset1.getHistogram()
    histo1.SetMarkerColor(2)
    histo1.SetMarkerStyle(20)
    removeNegatives(histo1)
    histo1.Scale(1. / histo1.Integral())

    histo2 = dataset2.getHistogram()
    histo2.SetMarkerColor(4)
    histo2.SetMarkerStyle(21)

    p = plots.ComparisonPlot(
        histograms.Histo(histo1, "m_{H^{#pm}} = 200 GeV/c^{2}", "p", "P"),
        histograms.Histo(histo2, "t#bar{t}", "p", "P"))

    opts = {"ymin": 0, "ymax": 0.2}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    p.createFrame(os.path.join(plotDir, plotname),
                  createRatio=True,
                  opts=opts,
                  opts2=opts2)

    moveLegend = {"dx": -0.2, "dy": -0.1, "dh": -0.1}
    p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("Arbitrary units")
    p.getFrame().GetXaxis().SetTitle("Top p_{T} (GeV/c)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
Esempio n. 7
0
def main():

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

    paths = [sys.argv[1]]
    analysis = "Hplus2tbAnalysis"
    hName = "associatedTPt"
    plotname = analysis+"_"+hName


    datasetsHiggs = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTauB_M_")
    datasetsTT    = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="TT")
    datasetsTT.merge("MC", ["TT","TT_ext"], keepSources=True)

    style = tdrstyle.TDRStyle()

    dataset1 = datasetsHiggs.getDataset("ChargedHiggs_HplusTB_HplusToTauB_M_200").getDatasetRootHisto(hName)
    dataset2 = datasetsTT.getDataset("MC").getDatasetRootHisto(hName)
#    dataset1.normalizeToOne()
    dataset2.normalizeToOne()

    histo1 = dataset1.getHistogram()
    histo1.SetMarkerColor(2)
    histo1.SetMarkerStyle(20)
    removeNegatives(histo1)
    histo1.Scale(1./histo1.Integral())

    histo2 = dataset2.getHistogram()
    histo2.SetMarkerColor(4)
    histo2.SetMarkerStyle(21)

    p = plots.ComparisonPlot(histograms.Histo(histo1, "m_{H^{#pm}} = 200 GeV/c^{2}", "p", "P"),
                             histograms.Histo(histo2, "t#bar{t}", "p", "P"))

    opts = {"ymin": 0, "ymax": 0.2}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    p.createFrame(os.path.join(plotDir, plotname), createRatio=True, opts=opts, opts2=opts2)

    moveLegend = {"dx": -0.2, "dy": -0.1, "dh": -0.1}
    p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("Arbitrary units")
    p.getFrame().GetXaxis().SetTitle("Top p_{T} (GeV/c)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
Esempio n. 8
0
def GetDatasetsFromDir(mcrab, opts, **kwargs):
    dataEra    = kwargs.get("dataEra")
    searchMode = kwargs.get("searchMode")
    analysis   = kwargs.get("analysis")
    optMode    = kwargs.get("optMode")

    if opts.includeTasks != "":
        datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], dataEra=dataEra, searchMode=searchMode, analysisName=analysis, includeOnlyTasks=opts.includeTasks, optimizationMode=optMode)
    elif opts.excludeTasks != "":
        datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], dataEra=dataEra, searchMode=searchMode, analysisName=analysis, excludeTasks=opts.excludeTasks, optimizationMode=optMode)
        # excludeTasks="M_180|M_220|M_250"
    else:
        datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], dataEra=dataEra, searchMode=searchMode, analysisName=analysis, optimizationMode=optMode)
    return datasets
Esempio n. 9
0
def GetAllDatasetsFromDir(opts):
    datasets = dataset.getDatasetsFromMulticrabDirs([opts.mcrab],
                                                    dataEra=opts.dataEra,
                                                    searchMode=opts.searchMode, 
                                                    analysisName=opts.analysisName,
                                                    optimizationMode=opts.optMode)
    return datasets
def getDataSets(dirs, dataEra, searchMode, analysis, optMode):
    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra,  searchMode=searchMode, analysisName=analysis, optimizationMode=optMode)
            
    datasets.updateNAllEventsToPUWeighted()
    datasets.loadLuminosities()
    
    datasets.remove(filter(lambda name: "TTToHplus" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_t-channel" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_tW-channel" in name, datasets.getAllDatasetNames()))
    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()))
    
    plots.mergeRenameReorderForDataMC(datasets)
    
    datasets.merge("EWK", [
        "TTJets",
        "WJets",
        "DYJetsToLL",
        "SingleTop",
        "Diboson"
        ])

    return datasets
Esempio n. 11
0
def getDataSets(dirs, dataEra, searchMode, analysis, optMode):
    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra,  searchMode=searchMode, analysisName=analysis, optimizationMode=optMode)
            
    datasets.updateNAllEventsToPUWeighted()
    datasets.loadLuminosities()
    
    datasets.remove(filter(lambda name: "TTToHplus" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_t-channel" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_tW-channel" in name, datasets.getAllDatasetNames()))
    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()))
    
    plots.mergeRenameReorderForDataMC(datasets)
    
    datasets.merge("EWK", [
        "TTJets",
        "WJets",
        "DYJetsToLL",
        "SingleTop",
        "Diboson"
        ])

    return datasets
Esempio n. 12
0
def GetDatasetsFromDir(mcrab, opts, **kwargs):

    dataEra = kwargs.get("dataEra")
    searchMode = kwargs.get("searchMode")
    analysis = kwargs.get("analysis")
    optMode = kwargs.get("optMode")

    #    if opts.includeTasks != "":
    #    datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], dataEra=dataEra, searchMode=searchMode, analysisName=analysis, includeOnlyTasks=opts.includeTasks, optimizationMode=optMode)
    #   elif opts.excludeTasks != "":
    #      datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], dataEra=dataEra, searchMode=searchMode, analysisName=analysis, excludeTasks=opts.excludeTasks, optimizationMode=optMode)
    # excludeTasks="M_180|M_220|M_250"
    # else:
    #    datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], dataEra=dataEra, searchMode=searchMode, analysisName=analysis, optimizationMode=optMode)
    datasets = dataset.getDatasetsFromMulticrabDirs([mcrab],
                                                    dataEra=dataEra,
                                                    searchMode=searchMode,
                                                    analysisName=analysis,
                                                    optimizationMode=optMode)

    # Inform user of datasets retrieved
    Verbose("Got the following datasets from multicrab dir \"%s\"" % mcrab)
    for d in datasets.getAllDatasets():
        Verbose("\t", d.getName(), False)
    return datasets
Esempio n. 13
0
def GetTTBackgroundDatasets(opts):
    Verbose("Getting TT background datasets")
    return dataset.getDatasetsFromMulticrabDirs([opts.mcrab],
                                                dataEra=opts.dataEra,
                                                searchMode=opts.searchMode,
                                                analysisName=opts.analysisName,
                                                includeOnlyTasks="TT|ST_t|ttbb|TTZToQQ|TTWJetsToQQ|TTTT",
                                                optimizationMode=opts.optMode)
Esempio n. 14
0
def GetSignalDatasets(opts):
    Verbose("Getting signal datasets")
    return dataset.getDatasetsFromMulticrabDirs([opts.mcrab],
                                                dataEra=opts.dataEra,
                                                searchMode=opts.searchMode,
                                                analysisName=opts.analysisName,
                                                includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTB_M_",
                                                optimizationMode=opts.optMode)
Esempio n. 15
0
def GetQCDBackgroundDatasets(opts):
    Verbose("Getting QCD background datasets")
    return dataset.getDatasetsFromMulticrabDirs([opts.mcrab],
                                                dataEra=opts.dataEra,
                                                searchMode=opts.searchMode,
                                                analysisName=opts.analysisName,
                                                includeOnlyTasks="QCD_HT",
                                                optimizationMode=opts.optMode)
Esempio n. 16
0
def GetDatasetsFromDir(mcrab, opts, **kwargs):

    dataEra    = kwargs.get("dataEra")
    searchMode = kwargs.get("searchMode")
    analysis   = kwargs.get("analysis")
    optMode    = kwargs.get("optMode")

    if opts.includeTasks != "":
        datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], dataEra=dataEra, searchMode=searchMode, analysisName=analysis, includeOnlyTasks=opts.includeTasks, optimizationMode=optMode)
    elif opts.excludeTasks != "":
        datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], dataEra=dataEra, searchMode=searchMode, analysisName=analysis, excludeTasks=opts.excludeTasks, optimizationMode=optMode)
        # excludeTasks="M_180|M_220|M_250"
    else:
        datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], dataEra=dataEra, searchMode=searchMode, analysisName=analysis, optimizationMode=optMode)

    # Inform user of datasets retrieved
    Verbose("Got the following datasets from multicrab dir \"%s\"" % mcrab)
    for d in datasets.getAllDatasets():
        Verbose( "\t", d.getName(), False)
    return datasets
Esempio n. 17
0
def GetDatasetsFromDir(mcrab, analysis):

    datasets = dataset.getDatasetsFromMulticrabDirs([mcrab], analysisName=analysis)
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTB_M_")
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, excludeTasks="Tau_Run2015C|Tau\S+25ns_Silver$|DYJetsToLL|WJetsToLNu$")
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, excludeTasks="M_180|M_220|M_250")

    # Inform user of datasets retrieved
    Verbose("Got the following datasets from multicrab dir \"%s\"" % mcrab)
    for d in datasets.getAllDatasets():
        Verbose( "\t", d.getName(), False)
    return datasets
Esempio n. 18
0
def GetDatasetsFromDir(mcrab, analysis):

    datasets = dataset.getDatasetsFromMulticrabDirs([mcrab],
                                                    analysisName=analysis)
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTB_M_")
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, excludeTasks="Tau_Run2015C|Tau\S+25ns_Silver$|DYJetsToLL|WJetsToLNu$")
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, excludeTasks="M_180|M_220|M_250")

    # Inform user of datasets retrieved
    Verbose("Got the following datasets from multicrab dir \"%s\"" % mcrab)
    for d in datasets.getAllDatasets():
        Verbose("\t", d.getName(), False)
    return datasets
Esempio n. 19
0
def GetDatasetsFromDir(opts, otherDir=False):
    
    myDir    = opts.mcrab1
    analysis = opts.analysisName
    if otherDir:
        myDir    = opts.mcrab2        
        #analysis = "FakeBMeasurement"
    datasets = dataset.getDatasetsFromMulticrabDirs([myDir],
                                                    dataEra=opts.dataEra,
                                                    searchMode=opts.searchMode, 
                                                    analysisName=analysis,
                                                    optimizationMode=opts.optMode)
    return datasets
Esempio n. 20
0
def GetDatasetsFromDir(opts, json):
    Verbose("Getting datasets")

    if opts.optMode == None:
        optMode = json["optMode"]
    else:
        optMode = opts.optMode

    if (opts.includeOnlyTasks):
        return dataset.getDatasetsFromMulticrabDirs(
            [opts.mcrab],
            dataEra=json["dataEra"],
            searchMode=json["searchMode"],
            analysisName=json["analysis"],
            includeOnlyTasks=opts.includeOnlyTasks,
            optimizationMode=optMode)
    elif (opts.excludeTasks):
        return dataset.getDatasetsFromMulticrabDirs(
            [opts.mcrab],
            dataEra=json["dataEra"],
            searchMode=json["searchMode"],
            analysisName=json["analysis"],
            excludeTasks=opts.excludeTasks,
            optimizationMode=optMode)
    else:
        #return process.addDatasetsFromMulticrab(opts.mcrab)
        if len(json["samples"]) < 1:
            Print("No samples defined in the JSON file. Exit", True)
            print __doc__
            sys.exit()
        else:
            return dataset.getDatasetsFromMulticrabDirs(
                [opts.mcrab],
                dataEra=json["dataEra"],
                searchMode=json["searchMode"],
                analysisName=json["analysis"],
                includeOnlyTasks="|".join(json["samples"]),
                optimizationMode=optMode)
Esempio n. 21
0
def GetDatasetsFromDir(opts, json):
    Verbose("Getting datasets")
    
    if len(json["samples"])<1:
        Print("No samples defined in the JSON file. Exit", True)
        print __doc__
        sys.exit()
    else:
        return dataset.getDatasetsFromMulticrabDirs([opts.mcrab], 
                                                    dataEra=json["dataEra"],
                                                    searchMode=json["searchMode"],
                                                    analysisName=json["analysis"],
                                                    includeOnlyTasks="|".join(json["samples"]),
                                                    optimizationMode=json["optMode"])
Esempio n. 22
0
def GetSpecificDatasetsFromDir(mcrab, opts, RunEra, samples, **kwargs):
    '''
    
    '''
    HasKeys(["dataEra", "searchMode", "analysis", "optMode"], **kwargs)
    dataEra = kwargs.get("dataEra")
    searchMode = kwargs.get("searchMode")
    analysis = kwargs.get("analysis")
    optMode = kwargs.get("optMode")

    datasets = dataset.getDatasetsFromMulticrabDirs([mcrab],
                                                    dataEra=dataEra,
                                                    searchMode=searchMode,
                                                    analysisName=analysis +
                                                    "_" + RunEra,
                                                    includeOnlyTasks=samples,
                                                    optimizationMode=optMode)
    return datasets
Esempio n. 23
0
def plot(resultdir,jsonfile):
    with open(os.path.abspath(jsonfile)) as jfile:
        j = json.load(jfile)
        print "Plotting",j["title"],"in",resultdir

        if "outputdir" in j:
            global plotDir
            plotDir = j["outputdir"]
        multicrabPaths = PathFinder.MulticrabPathFinder(resultdir)
        print "multicrabPaths = ", multicrabPaths.

        paths = []
        if os.path.exists(multicrabPaths.getSignalPath()):
            paths.append(multicrabPaths.getSignalPath())
        if os.path.exists(multicrabPaths.getQCDInvertedPath()):
            paths.append(multicrabPaths.getQCDInvertedPath())
        if os.path.exists(multicrabPaths.getEWKPath()):
            paths.append(multicrabPaths.getEWKPath())

        datasets = dataset.getDatasetsFromMulticrabDirs(paths)

        #datasets.loadLuminosities()
        style = tdrstyle.TDRStyle()
        plots.mergeRenameReorderForDataMC(datasets)

        alldsets = datasets.getAllDatasets()
        print "Merged datasets"
        for d in alldsets:
            print "       ",d.getName()

        # lumi = 0.0
        # for d in datasets.getDataDatasets():
        #     print "luminosity",d.getName(),d.getLuminosity()
        #     lumi += d.getLuminosity()
        # print "luminosity, sum",lumi

        if len(j["samples"])>0:
           for s in j["samples"]:
               #print s
               h = datasets.getDataset(s).getDatasetRootHisto(j["histogram"]).getHistogram()
               name = j["histogram"]+s
               plotgraph([h],lumi,j,name)
def plot(resultdir,jsonfile):
    with open(os.path.abspath(jsonfile)) as jfile:
        j = json.load(jfile)
        print "Plotting",j["title"],"in",resultdir

        if "outputdir" in j:
            global plotDir
            plotDir = j["outputdir"]
        multicrabPaths = PathFinder.MulticrabPathFinder(resultdir)


        paths = []
        if os.path.exists(multicrabPaths.getSignalPath()):
            paths.append(multicrabPaths.getSignalPath())
        if os.path.exists(multicrabPaths.getQCDInvertedPath()):
            paths.append(multicrabPaths.getQCDInvertedPath())
        if os.path.exists(multicrabPaths.getEWKPath()):
            paths.append(multicrabPaths.getEWKPath())

        datasets = dataset.getDatasetsFromMulticrabDirs(paths)

        datasets.loadLuminosities()
        style = tdrstyle.TDRStyle()
        plots.mergeRenameReorderForDataMC(datasets)

        alldsets = datasets.getAllDatasets()
        print "Merged datasets"
        for d in alldsets:
            print "       ",d.getName()

        lumi = 0.0
        for d in datasets.getDataDatasets():
            print "luminosity",d.getName(),d.getLuminosity()
            lumi += d.getLuminosity()
        print "luminosity, sum",lumi

        if len(j["samples"])>0:
           for s in j["samples"]:
               h = datasets.getDataset(s).getDatasetRootHisto(j["histogram"]).getHistogram()
               name = j["histogram"]+s
               plotgraph([h],lumi,j,name)
def main():

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

    dirs = []
    dirs.append(sys.argv[1])
    # Read the datasets
#    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,counters=counters, dataEra=dataEra, analysisBaseName="signalAnalysisInvertedTau")
    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra, searchMode=searchMode, analysisName=analysis, optimizationMode=optMode) 
#    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,counters=counters)
#    datasets = dataset.getDatasetsFromMulticrabCfg(counters=counters, dataEra=dataEra)
#    datasets.updateNAllEventsToPUWeighted()
    datasets.loadLuminosities()
    datasets.updateNAllEventsToPUWeighted()

    
    # Take QCD from data
    datasetsQCD = None

    if QCDfromData:

        #datasetsQCD = dataset.getDatasetsFromMulticrabCfg(cfgfile="/home/rkinnune/signalAnalysis/CMSSW_4_2_8_patch2/src/HiggsAnalysis/NtupleAnalysis/test/multicrab_111123_132128/multicrab.cfg", counters=counters)
        datasetsQCD = dataset.getDatasetsFromMulticrabCfg(cfgfile="/afs/cern.ch/work/e/epekkari/DataDrivenFakeTaus/CMSSW_5_3_9_patch3/src/HiggsAnalysis/NtupleAnalysis/test/multicrab_140526_122821/multicrab.cfg")
        datasetsQCD.loadLuminosities()
        print "QCDfromData", QCDfromData
        datasetsQCD.mergeData()
        datasetsQCD.remove(datasetsQCD.getMCDatasetNames())
        datasetsQCD.rename("Data", "QCD")
    

    #for d in datasets.getAllDatasets():
    #    print d.getName()
    #print "-------"            
    #plots.mergeRenameReorderForDataMC(datasets)

#    print "Int.Lumi",datasets.getDataset("Data").getLuminosity()

    # Remove signals other than M120
    datasets.remove(filter(lambda name: "TTToHplus" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_t-channel" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_tW-channel" in name, datasets.getAllDatasetNames()))
    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()))

    plots.mergeRenameReorderForDataMC(datasets)
    
    datasets.merge("EWK", ["WJets", "DYJetsToLL", "SingleTop", "Diboson", "TTJets"], keepSources=True)
    datasets.remove(filter(lambda name: "W2Jets" in name, datasets.getAllDatasetNames()))        
    datasets.remove(filter(lambda name: "W3Jets" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "W4Jets" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_s-channel" in name, datasets.getAllDatasetNames()))
    # Remove QCD
    #datasets.remove(filter(lambda name: "QCD" in name, datasets.getAllDatasetNames()))

    
    datasets_lands = datasets.deepCopy()

    # Set the signal cross sections to the ttbar for datasets for lands
#    xsect.setHplusCrossSectionsToTop(datasets_lands)

    # Set the signal cross sections to a given BR(t->H), BR(h->taunu)
    xsect.setHplusCrossSectionsToBR(datasets, br_tH=0.01, br_Htaunu=1)

    # Set the signal cross sections to a value from MSSM
#    xsect.setHplusCrossSectionsToMSSM(datasets, tanbeta=20, mu=200)

    plots.mergeWHandHH(datasets) # merging of WH and HH signals must be done after setting the cross section


    # Apply TDR style
    style = tdrstyle.TDRStyle()

    # Create plots
    doPlots(datasets)

    # Write mt histograms to ROOT file
#    writeTransverseMass(datasets_lands)

    # Print counters
    doCounters(datasets)
Esempio n. 26
0
def main():

    style    = tdrstyle.TDRStyle()
    hNames   = getHistoNames(kinVar)
    # hName    = hNames[0]

    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(parseOpts.batchMode)

    # Get all datasets from the mcrab dir
    datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis)
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTB_M_")
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTB_M_")
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, excludeTasks="Tau_Run2015C|Tau\S+25ns_Silver$|DYJetsToLL|WJetsToLNu$")

    
    # Inform user of datasets retrieved
    Print("Got following datasets from multicrab dir \"%s\"" % parseOpts.mcrab)
    for d in datasets.getAllDatasets():
        print "\t", d.getName()


    # Determine Integrated Luminosity (If Data datasets present)
    intLumi = 0.0
    if len(datasets.getDataDatasets()) != 0:
        # Load Luminosity JSON file
        datasets.loadLuminosities(fname="lumi.json")

        # Load RUN range
        # runRange = datasets.loadRunRange(fname="runrange.json")

        # Calculate Integrated Luminosity
        intLumi = GetLumi(datasets)
    
    
    # For-loop: All Histogram names
    for counter, hName in enumerate(hNames):
        plotName = hName #analysis + "_" + hName
        saveName = os.path.join(savePath, plotName)
        
        # Get Data or MC datasets
        # dataDatasets = datasets.getDataDatasets()
        # mcDatasets   = datasets.getMCDatasets()

        # Build ROOT histos from individual datasets
        dataset1 = datasets.getDataset("ChargedHiggs_HplusTB_HplusToTB_M_400").getDatasetRootHisto(hName)
        dataset2 = datasets.getDataset("ChargedHiggs_HplusTB_HplusToTB_M_300").getDatasetRootHisto(hName)
        # dataset2 = datasets.getDataset("TT_ext3").getDatasetRootHisto(hName)
        # datasets.getDataset("TT_ext3").setCrossSection(831.76)
        
        
        # Normalise datasets
        if normalizeTo == "One":
            dataset1.normalizeToOne()
            dataset2.normalizeToOne()
        elif normalizeTo == "XSection":
            dataset1.normalizeByCrossSection()
            dataset2.normalizeByCrossSection()
        elif normalizeTo == "Luminosity":
            dataset1.normalizeToLumi(intLumi)
            dataset2.normalizeToLumi(intLumi)
        else:
            isValidNorm(normalizeTo)
        
    
        # Customise histos
        histo1 = dataset1.getHistogram()
        styles.signal200Style.apply(histo1)
        # histo1.SetMarkerStyle(ROOT.kFullCircle)
        # histo1.SetFillStyle(3001)
        # histo1.SetFillColor(histo2.GetMarkerColor())
        # removeNegatives(histo1)
        # removeErrorBars(histo1)
        histo1.Rebin(rebinFactor)
        
        # Customise histos
        histo2 = dataset2.getHistogram()
        styles.ttStyle.apply(histo2)
        # histo2.SetMarkerStyle(ROOT.kFullCross)
        histo2.SetFillStyle(3001)
        histo2.SetFillColor(styles.ttStyle.color)
        # removeNegatives(histo2)
        # removeErrorBars(histo2)
        histo2.Rebin(rebinFactor)


        # Create a comparison plot
        p = plots.ComparisonPlot(histograms.Histo(histo1, "m_{H^{#pm}} = 400 GeV/c^{2}", "p", "P"),
                                 histograms.Histo(histo2, "m_{H^{#pm}} = 300 GeV/c^{2}", "F", "HIST,E,9"))
#                                 histograms.Histo(histo2, "t#bar{t}", "F", "HIST,E,9"))
        
        # Create a comparison plot (One histogram is treated as a reference histogram, and all other histograms are compared with respect to that)
        # p = plots.ComparisonManyPlot(histograms.Histo(histo1, "m_{H^{#pm}} = 200 GeV/c^{2}", "p", "P"),
        #                             [histograms.Histo(histo2, "m_{H^{#pm}} = 300 GeV/c^{2}", "F", "HIST9"),
        #                              histograms.Histo(histo3, "t#bar{t}", "F", "HIST9")])

    
        # Customise plots
        opts      = {"ymin": 0.0, "binWidthX": histo1.GetXaxis().GetBinWidth(0), "xUnits": getUnitsX(kinVar)}
        ratioOpts = {"ymin": 0.0, "ymax": 2.0 , "binWidthX": histo1.GetXaxis().GetBinWidth(0), "xUnits": getUnitsX(kinVar)}
        p.createFrame(os.path.join(savePath, plotName), createRatio=ratio, opts=opts, opts2=ratioOpts)
        

        # Customise Legend
        moveLegend = {"dx": -0.1, "dy": +0.0, "dh": -0.2}
        p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))


        # Customise text
        if intLumi > 0.0:
            histograms.addStandardTexts(lumi=intLumi)
        else:
            histograms.addStandardTexts()
        # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
        # histograms.addText(0.4, 0.11, "Runs " + datasets.loadRunRange(), 17)


        # Customise frame
        p.setEnergy("13")
        p.getFrame().GetYaxis().SetTitle( getTitleY(normalizeTo, kinVar, opts) )
        p.getFrame().GetXaxis().SetTitle( getTitleX(kinVar, opts) )
        if ratio:
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)


        #  Draw plots
        p.draw()

    
        # Save canvas under custom dir
        if counter == 0:
            Print("Saving plots in %s format(s)" % (len(saveFormats)) )
        SavePlotterCanvas(p, savePath, saveName, saveFormats)

    return
def main(opts):

    datasetsMgr = None
    # Fixme: This options is currently invalid
    if len(opts.files) > 0:
        datasetsMgr = dataset.getDatasetsFromRootFiles( [(x,x) for x in opts.files], opts=opts, weightedCounters=opts.weighted)
    else:
        # Define the cwd as the multicrab dir containing the ROOT files with the counters
        multicrabDir = os.getcwd()
        
        # For-loop: All arguments passed during script execution
        for d in sys.argv:
            # Look for a directory that exists
            if os.path.exists(d) and os.path.isdir(d):
                multicrabDir = os.path.abspath(d)

        Print("The multicrab directory to be used is %s" % (multicrabDir), True)
        # Get the datasets    
        datasetsMgr = dataset.getDatasetsFromMulticrabDirs([multicrabDir],opts=opts, weightedCounters=opts.weighted)

    # Optional: Apply include/exclude datasets
    datasetsMgr = FilterDatasets(datasetsMgr, opts)
    Print("The tasks to be included are:\n\t%s" % ("\n\t".join(d.getName() for d in datasetsMgr.getAllDatasets())), True)


    # Optional: Print info on Data and MC samples
    if opts.verbose:
        datasetsMgr.PrintCrossSections()
        datasetsMgr.PrintLuminosities()
    
    # Load the luminosities
    if os.path.exists(opts.lumifile):
        datasetsMgr.loadLuminosities(opts.lumifile)

    # Optional: Apply PU-reweighting
    if opts.weighted and opts.PUreweight:
        Print("Updating all events to PU-weighted (opts.weighted=%s, opts.PUreweight=%s)\n" % (opts.weighted, opts.PUreweight), True)
        datasetsMgr.updateNAllEventsToPUWeighted(era=opts.dataEra)
    
    # Optional: Merge data
    Verbose("Merging Data and/or MC datatets (opts.mergeData=%s, opts.mergeData=%s, opts.mergeData=%s)" % (opts.mergeData, opts.mergeMC, opts.mergeForDataMC), True)
    if opts.mergeData:
        datasetsMgr.mergeData()
    if opts.mergeMC:
        datasetsMgr.mergeMC()
    if opts.mergeForDataMC:
        plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Optional: Print dataset info
    if opts.printInfo:
        datasetsMgr.PrintInfo() #datasetsMgr.printInfo()

    # Create the event counter
    eventCounter = counter.EventCounter(datasetsMgr)

    # Proceed differently depending on operation mode (opts.mode= 'events', 'xsect', 'eff')
    quantity = "events"
    if opts.mode == "events":        
        if opts.mergeForDataMC:
            Print("Normalising the MC histograms to the data luminosity (opts.mergeForDataMC=%s)" % (opts.mergeForDataMC) )
            eventCounter.normalizeMCByLuminosity()
        else:
            pass
    elif opts.mode in ["xsect", "xsection", "crosssection", "crossSection", "eff"]:
        if not opts.PUreweight:
            Print("Mode '%s' works only with PU reweighting, which you disabled with --noPUreweight" % opts.mode)
            return 1
        Print("Normalising MC by cross-section (opt.mode=%s)" % opts.mode)
        eventCounter.normalizeMCByCrossSection()
        quantity = "MC by cross section, data by events"
    else:
        Print("Printing mode '%s' doesn't exist! The following ones are available 'events', 'xsect', 'eff'" % opts.mode)
        return 1

    # Optional: Produce table in Text or LaTeX format?
    if opts.latex:
        cellFormat  = counter.CellFormatTeX(valueOnly=opts.valueOnly, valueFormat=opts.format)
        formatFunc = lambda table: table.format(counter.TableFormatLaTeX(cellFormat))
    else:
        cellFormat  = counter.CellFormatText(valueOnly=opts.valueOnly, valueFormat=opts.format)
        formatFunc = lambda table: table.format(counter.TableFormatText(cellFormat))
    csvSplitter = counter.TableSplitter([" +- ", " +", " -"])

    # Optional: Format as comma-separated-variables (csv), presubambly for exporting to a spreadsheet
    if opts.csv:
        formatFunc = lambda table: table.format(counter.TableFormatText(cellFormat, columnSeparator=","), csvSplitter)

    # Optional: Convert to (relative) efficienies
    if opts.mode == "eff":
        if opts.weighted:
            Print("Cannot operate in \"eff\" mode while using weighted counters (opts.mode=\'%s\', opts.weighted=%s)" % (opts.mode, opts.weighted) )
            return 1
        else:            
            Print("Converting to efficiencies (opts.mode=%s)" % (opts.mode) )

        cellFormat = counter.CellFormatText(valueFormat="%.4f", valueOnly=opts.valueOnly)
        if opts.latex:
            formatFunc = lambda table: counter.counterEfficiency(table).format(counter.TableFormatLaTeX(cellFormat))
        else:
            formatFunc = lambda table: counter.counterEfficiency(table).format(counter.TableFormatText(cellFormat))
        quantity = "Cut efficiencies"
        # Optional: Format as comma-separated-variables (csv), presubambly for exporting to a spreadsheet
        if opts.csv:
            formatFunc = lambda table: counter.counterEfficiency(table).format(counter.TableFormatText(cellFormat, columnSeparator=","), csvSplitter)

    # Optional: Print only this sub-counters
    if opts.subCounter is not None:
        msg = "Subcounter %s %s: " % (opts.subCounter, quantity)
        Print(msg, True)
        print formatFunc(eventCounter.getSubCounterTable(opts.subCounter))
        print
        return 0

    # Print the main counters
    hLine = "="*10
    msg = " Main counter %s: " % quantity
    print "\n" + hLine + msg + hLine
    print formatFunc(eventCounter.getMainCounterTable())
    print 

    # Optional: Print sub-counters (only if --mainCounterOnly is not called)
    if not opts.mainCounterOnly:
        names = eventCounter.getSubCounterNames()
        names.sort()
        for name in names:
            hLine = "="*10
            msg = " Subcounter %s %s: " % (name, quantity)
            print "\n" + hLine + msg + hLine
            print formatFunc(eventCounter.getSubCounterTable(name) )

    return 0
def main(argv):
    COMBINEDHISTODIR = "ForQCDNormalization"
    FAKEHISTODIR = "ForQCDNormalizationEWKFakeTaus"
    GENUINEHISTODIR = "ForQCDNormalizationEWKGenuineTaus"
    comparisonList = ["AfterStdSelections"]

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

    dirs.append(sys.argv[1])

    # Create all dsetMgr from a multicrab task
    dsetMgr = dataset.getDatasetsFromMulticrabDirs(dirs,
                                                   dataEra=dataEra,
                                                   searchMode=searchMode,
                                                   analysisName=analysis)

    #print dsetMgr
    # Check multicrab consistency
    consistencyCheck.checkConsistencyStandalone(dirs[0],
                                                dsetMgr,
                                                name="QCD inverted")

    # As we use weighted counters for MC normalisation, we have to
    # update the all event count to a separately defined value because
    # the analysis job uses skimmed pattuple as an input
    dsetMgr.updateNAllEventsToPUWeighted()

    # Read integrated luminosities of data dsetMgr from lumi.json
    dsetMgr.loadLuminosities()

    # Include only 120 mass bin of HW and HH dsetMgr
    dsetMgr.remove(
        filter(lambda name: "TTToHplus" in name and not "M120" in name,
               dsetMgr.getAllDatasetNames()))
    dsetMgr.remove(
        filter(lambda name: "HplusTB" in name, dsetMgr.getAllDatasetNames()))
    # Default merging nad ordering of data and MC dsetMgr
    # All data dsetMgr to "Data"
    # All QCD dsetMgr to "QCD"
    # All single top dsetMgr to "SingleTop"
    # WW, WZ, ZZ to "Diboson"
    plots.mergeRenameReorderForDataMC(dsetMgr)

    # Set BR(t->H) to 0.05, keep BR(H->tau) in 1
    xsect.setHplusCrossSectionsToBR(dsetMgr, br_tH=0.05, br_Htaunu=1)

    # Merge WH and HH dsetMgr to one (for each mass bin)
    plots.mergeWHandHH(dsetMgr)

    dsetMgr.merge(
        "EWK",
        [
            "TTJets",
            "WJetsHT",
            "DYJetsToLL",
            "SingleTop",
            #"Diboson"
        ])

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)

    for HISTONAME in comparisonList:
        BASELINETAUHISTONAME = "NormalizationMETBaselineTau" + HISTONAME + "/NormalizationMETBaselineTau" + HISTONAME
        INVERTEDTAUHISTONAME = "NormalizationMETInvertedTau" + HISTONAME + "/NormalizationMETInvertedTau" + HISTONAME

        #===== Infer binning information and labels
        histonames = dsetMgr.getDataset("Data").getDirectoryContent(
            COMBINEDHISTODIR + "/NormalizationMETBaselineTau" + HISTONAME)
        bins = []
        binLabels = []
        if histonames == None:
            # Assume that only inclusive bin exists
            name = COMBINEDHISTODIR + "/NormalizationMETBaselineTau" + HISTONAME
            if not dsetMgr.getDataset("Data").hasRootHisto(name):
                raise Exception(
                    "Error: Cannot find histogram or directory of name '%s'!" %
                    name)
            BASELINETAUHISTONAME = "NormalizationMETBaselineTau" + HISTONAME
            INVERTEDTAUHISTONAME = "NormalizationMETInvertedTau" + HISTONAME
            bins = [""]
            binLabels = ["Inclusive"]
        else:
            for hname in histonames:
                bins.append(
                    hname.replace("NormalizationMETBaselineTau" + HISTONAME,
                                  ""))
                title = dsetMgr.getDataset("Data").getDatasetRootHisto(
                    COMBINEDHISTODIR + "/" + BASELINETAUHISTONAME + "/" +
                    hname).getHistogram().GetTitle()
                title = title.replace("METBaseline" + HISTONAME, "")
                binLabels.append(formatHistoTitle(title))

        print "\nHistogram bins available", bins
        print "Using bins              ", bins
        print "\nBin labels"
        for i in range(len(binLabels)):
            line = bins[i]
            while len(line) < 10:
                line += " "
            line += ": " + binLabels[i]
            print line
        print

        #===== Initialize normalization calculator
        invertedQCD = InvertedTauID()
        invertedQCD.setLumi(dsetMgr.getDataset("Data").getLuminosity())
        invertedQCD.setInfo([dataEra, searchMode, HISTONAME])

        #===== Loop over tau pT bins
        for i, binStr in enumerate(bins):
            print "\n********************************"
            print "*** Fitting bin %s" % binLabels[i]
            print "********************************\n"
            invertedQCD.resetBinResults()
            invertedQCD.setLabel(binLabels[i])

            #===== Obtain histograms for normalization
            metBase = plots.DataMCPlot(
                dsetMgr,
                COMBINEDHISTODIR + "/" + BASELINETAUHISTONAME + binStr)
            metInver = plots.DataMCPlot(
                dsetMgr,
                COMBINEDHISTODIR + "/" + INVERTEDTAUHISTONAME + binStr)
            metBase_GenuineTaus = plots.DataMCPlot(
                dsetMgr, GENUINEHISTODIR + "/" + BASELINETAUHISTONAME + binStr)
            metInver_GenuineTaus = plots.DataMCPlot(
                dsetMgr, GENUINEHISTODIR + "/" + INVERTEDTAUHISTONAME + binStr)
            metBase_FakeTaus = plots.DataMCPlot(
                dsetMgr, FAKEHISTODIR + "/" + BASELINETAUHISTONAME + binStr)
            metInver_FakeTaus = plots.DataMCPlot(
                dsetMgr, FAKEHISTODIR + "/" + INVERTEDTAUHISTONAME + binStr)

            #===== Rebin histograms before subtracting
            RebinFactor = 2  # Aim for 10 GeV binning
            metBase.histoMgr.forEachHisto(
                lambda h: h.getRootHisto().Rebin(RebinFactor))
            metInver.histoMgr.forEachHisto(
                lambda h: h.getRootHisto().Rebin(RebinFactor))
            metBase_GenuineTaus.histoMgr.forEachHisto(
                lambda h: h.getRootHisto().Rebin(RebinFactor))
            metInver_GenuineTaus.histoMgr.forEachHisto(
                lambda h: h.getRootHisto().Rebin(RebinFactor))
            metBase_FakeTaus.histoMgr.forEachHisto(
                lambda h: h.getRootHisto().Rebin(RebinFactor))
            metInver_FakeTaus.histoMgr.forEachHisto(
                lambda h: h.getRootHisto().Rebin(RebinFactor))

            #===== Obtain templates for data and EWK
            metInverted_data = metInver.histoMgr.getHisto(
                "Data").getRootHisto().Clone(COMBINEDHISTODIR + "/" +
                                             INVERTEDTAUHISTONAME + binStr)
            treatHistogram(metInverted_data, "Data, inverted")
            metInverted_EWK_GenuineTaus = metInver_GenuineTaus.histoMgr.getHisto(
                "EWK").getRootHisto().Clone(GENUINEHISTODIR + "/" +
                                            INVERTEDTAUHISTONAME + binStr)
            treatHistogram(metInverted_EWK_GenuineTaus,
                           "EWK genuine taus, inverted")
            metInverted_EWK_FakeTaus = metInver_FakeTaus.histoMgr.getHisto(
                "EWK").getRootHisto().Clone(FAKEHISTODIR + "/" +
                                            INVERTEDTAUHISTONAME + binStr)
            treatHistogram(metInverted_EWK_FakeTaus, "EWK fake taus, inverted")

            metBase_data = metBase.histoMgr.getHisto(
                "Data").getRootHisto().Clone(COMBINEDHISTODIR + "/" +
                                             BASELINETAUHISTONAME + binStr)
            treatHistogram(metBase_data, "Data, baseline")
            metBase_EWK_GenuineTaus = metBase_GenuineTaus.histoMgr.getHisto(
                "EWK").getRootHisto().Clone(GENUINEHISTODIR + "/" +
                                            BASELINETAUHISTONAME + binStr)
            treatHistogram(metBase_EWK_GenuineTaus,
                           "EWK genuine taus, baseline")
            metBase_EWK_FakeTaus = metBase_FakeTaus.histoMgr.getHisto(
                "EWK").getRootHisto().Clone(FAKEHISTODIR + "/" +
                                            BASELINETAUHISTONAME + binStr)
            treatHistogram(metBase_EWK_FakeTaus, "EWK fake taus, baseline")

            #===== Obtain templates for QCD (subtract MC EWK events from data)
            # QCD from baseline is usable only as a cross check
            #metBase_QCD = metBase_data.Clone("QCD")
            #metBase_QCD.Add(metBase_EWK_GenuineTaus,-1)
            #metBase_QCD.Add(metBase_EWK_FakeTaus,-1)
            #addLabels(metBase_QCD, "QCD, baseline")

            metInverted_QCD = metInverted_data.Clone("QCD")
            metInverted_QCD.Add(metInverted_EWK_GenuineTaus, -1)
            metInverted_QCD.Add(metInverted_EWK_FakeTaus, -1)
            treatHistogram(metInverted_QCD, "QCD, inverted")

            #===== Make plots of templates
            print "\n*** Integrals of plotted templates"
            #invertedQCD.plotHisto(metInverted_data,"template_Data_Inverted")
            #invertedQCD.plotHisto(metInverted_EWK_GenuineTaus,"template_EWKGenuineTaus_Inverted")
            #invertedQCD.plotHisto(metInverted_EWK_FakeTaus,"template_EWKFakeTaus_Inverted")
            invertedQCD.plotHisto(metInverted_QCD, "template_QCD_Inverted")
            invertedQCD.plotHisto(metBase_data, "template_Data_Baseline")
            invertedQCD.plotHisto(metBase_EWK_GenuineTaus,
                                  "template_EWKGenuineTaus_Baseline")
            invertedQCD.plotHisto(metBase_EWK_FakeTaus,
                                  "template_EWKFakeTaus_Baseline")
            #invertedQCD.plotHisto(metBase_QCD,"template_QCD_Baseline")

            #===== Fit individual templates and
            # Fit first templates for QCD, EWK_genuine_taus, and EWK_fake_taus
            # Then fit the shape of those parametrizations to baseline data to obtain normalization coefficients
            fitOptions = "RB"

            # Strategy: take EWK templates from baseline and QCD template from inverted; then fit to baseline data
            invertedQCD.fitEWK_GenuineTaus(metInverted_EWK_GenuineTaus,
                                           fitOptions)
            invertedQCD.fitEWK_GenuineTaus(metBase_EWK_GenuineTaus, fitOptions)
            invertedQCD.fitEWK_FakeTaus(metInverted_EWK_FakeTaus, fitOptions)
            invertedQCD.fitEWK_FakeTaus(metBase_EWK_FakeTaus, fitOptions)
            invertedQCD.fitQCD(metInverted_QCD, fitOptions)
            invertedQCD.fitData(metBase_data)

            #===== Calculate normalization
            invertedQCD.getNormalization()

        invertedQCD.Summary()
        invertedQCD.WriteNormalizationToFile(
            "QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.py")
        invertedQCD.WriteLatexOutput("fits.tex")
Esempio n. 29
0
def main():

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

    paths = [sys.argv[1]]

    analysis = "METLeg_2015D_MET80"
#    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis)
#    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="Tau\S+25ns$|TTJets$")
    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="Tau_Run2015C|Tau\S+25ns_Silver$|DYJetsToLL|WJetsToLNu$")
#    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="Tau_Run2015D_PromptReco_v4_246908_260426_25ns$|DYJetsToLL_M_50$")

    for d in datasets.getAllDatasets():
        print d.getName()
    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
    dataset2 = datasets.getMCDatasets()

    eff1_MET80 = getEfficiency(dataset1)
    eff2_MET80 = getEfficiency(dataset2)

    styles.dataStyle.apply(eff1_MET80)
    styles.mcStyle.apply(eff2_MET80)
    eff1_MET80.SetMarkerSize(1)
    eff2_MET80.SetMarkerSize(1.5)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff1_MET80, "eff1_MET80", "p", "P"),
                             histograms.HistoGraph(eff2_MET80, "eff2_MET80", "p", "P"))

    opts = {"ymin": 0, "ymax": 1.1}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    moveLegend = {"dx": -0.55, "dy": -0.15}

    name = "TauMET_"+analysis+"_DataVsMC_PFMET"

    legend1 = "Data"
#    legend2 = "MC (TTJets)"
    legend2 = "MC"
    p.histoMgr.setHistoLegendLabelMany({"eff1_MET80": legend1, "eff2_MET80": legend2})

    p.createFrame(os.path.join(plotDir, name), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1_MET80", 17)
#    histograms.addText(0.2, 0.53, analysis.split("_")[len(analysis.split("_")) -1], 17)
    label = analysis.split("_")[1]
    histograms.addText(0.2, 0.53, label, 17)
    runRange = datasets.loadRunRange()
    histograms.addText(0.2, 0.46, "Runs "+runRange, 17)

    p.draw()
    lumi = 0.0
    for d in datasets.getDataDatasets():
        print "luminosity",d.getName(),d.getLuminosity()
        lumi += d.getLuminosity()
    print "luminosity, sum",lumi
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)

    pythonWriter.addParameters(plotDir,label,runRange,lumi,eff1_MET80)
    pythonWriter.addMCParameters(label,eff2_MET80)

    pythonWriter.writeJSON(os.path.join(plotDir,"metLegTriggerEfficiency2015.json"))

    """
    #### MET120

    analysis = "METLeg_2015CD_MET120"
    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis)
    for d in datasets.getAllDatasets():
        print d.getName()
    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
    dataset2 = datasets.getMCDatasets()

    eff1_MET120 = getEfficiency(dataset1)
    eff2_MET120 = getEfficiency(dataset2)

    styles.dataStyle.apply(eff1_MET120)
    styles.mcStyle.apply(eff2_MET120)
    eff1_MET120.SetMarkerSize(1)
    eff2_MET120.SetMarkerSize(1.5)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff1_MET120, "eff1_MET120", "p", "P"),
                             histograms.HistoGraph(eff2_MET120, "eff2_MET120", "p", "P"))

    opts = {"ymin": 0, "ymax": 1.1}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    moveLegend = {"dx": -0.55, "dy": -0.15}

    name = "DataVsMC_L1HLTMET_PFMET_MET120"

    legend1 = "Data"
    legend2 = "MC"
    p.histoMgr.setHistoLegendLabelMany({"eff1_MET120": legend1, "eff2_MET120": legend2})

    p.createFrame(os.path.join(plotDir, name), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    lumi = 0.0
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
    """

    # CaloMET

    #### MET80

    analysisc = "METLeg_2015D_CaloMET_MET80"
    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc)
    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc,excludeTasks="Tau\S+25ns_Silver$")
#    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc,includeOnlyTasks="Tau\S+25ns$|TTJets$")

    style = tdrstyle.TDRStyle()

    dataset1c = datasetsc.getDataDatasets()
    dataset2c = datasetsc.getMCDatasets()

#    eff1c_MET80 = getEfficiency(dataset1c)
    eff2c_MET80 = getEfficiency(dataset2c)

#    styles.dataStyle.apply(eff1c_MET80)
    styles.mcStyle.apply(eff2c_MET80)
#    eff1c_MET80.SetMarkerSize(1)
    eff2c_MET80.SetMarkerSize(1.5)
    eff2c_MET80.SetMarkerColor(4)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff2_MET80, "eff2_MET80", "p", "P"),
                             histograms.HistoGraph(eff2c_MET80, "eff2c_MET80", "p", "P"))

    namec = "TauMET_"+analysis+"_MC_TrgBitVsCaloMET80_PFMET"

    legend1c = "MC, trigger bit"
    legend2c = "MC, CaloMET > 80"
    p.histoMgr.setHistoLegendLabelMany({"eff2_MET80": legend1c, "eff2c_MET80": legend2c})

    p.createFrame(os.path.join(plotDir, namec), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    lumi = 0.0
    for d in datasets.getDataDatasets():
        print "luminosity",d.getName(),d.getLuminosity()
        lumi += d.getLuminosity()
    print "luminosity, sum",lumi
    histograms.addStandardTexts(lumi=lumi)
    histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1_MET80", 17)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)

    """
    #### MET120 

    analysisc = "METLeg_2015A_CaloMET_MET120"
    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc)

    style = tdrstyle.TDRStyle()

    dataset1c = datasetsc.getDataDatasets()
    dataset2c = datasetsc.getMCDatasets()

    eff1c_MET120 = getEfficiency(dataset1c)
    eff2c_MET120 = getEfficiency(dataset2c)

    styles.dataStyle.apply(eff1c_MET120)
    styles.mcStyle.apply(eff1c_MET120)
    eff1c_MET120.SetMarkerSize(1)
    eff2c_MET120.SetMarkerSize(1.5)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff2_MET120, "eff2_MET120", "p", "P"),
                             histograms.HistoGraph(eff2c_MET120, "eff2c_MET120", "p", "P"))

    namec = "MC_TrgBitVsCaloMET120_L1HLTMET_PFMET"

    legend1c = "MC, trigger bit"
    legend2c = "MC, CaloMET > 120"
    p.histoMgr.setHistoLegendLabelMany({"eff2_MET120": legend1c, "eff2c_MET120": legend2c})

    p.createFrame(os.path.join(plotDir, namec), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    lumi = 0.0
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
    """

    #########################################################################                                                                                                                             

    namePU = "TauMET_"+analysis+"_DataVsMC_nVtx"

    eff1PU = getEfficiency(dataset1,"NumeratorPU","DenominatorPU")
    eff2PU = getEfficiency(dataset2,"NumeratorPU","DenominatorPU")

    styles.dataStyle.apply(eff1PU)
    styles.mcStyle.apply(eff2PU)
    eff1PU.SetMarkerSize(1)
    eff2PU.SetMarkerSize(1.5)

    pPU = plots.ComparisonManyPlot(histograms.HistoGraph(eff1PU, "eff1", "p", "P"),
                                   [histograms.HistoGraph(eff2PU, "eff2", "p", "P")])


    pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2})

    opts = {"ymin": 0.001, "ymax": 0.1}
    pPU.createFrame(os.path.join(plotDir, namePU), createRatio=True, opts=opts, opts2=opts2)
    pPU.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
    pPU.getPad1().SetLogy(True)

    pPU.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    pPU.getFrame().GetXaxis().SetTitle("Number of reco vertices")
    pPU.getFrame2().GetYaxis().SetTitle("Ratio")
    pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.4, 0.25, "LooseIsoPFTau50_Trk30_eta2p1_MET80", 17)
    histograms.addText(0.4, 0.18, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.4, 0.11, "Runs "+datasets.loadRunRange(), 17)

    pPU.draw()
    histograms.addStandardTexts(lumi=lumi)

    pPU.save(formats)

    print "Output written in",plotDir
Esempio n. 30
0
def analyze(analysis=None):

    paths = [sys.argv[1]]

    datasets = dataset.getDatasetsFromMulticrabDirs(paths)
    #    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="SingleNeutrino")
    #    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="QCD")

    #    analysis = datasets.getAllDatasets()[0].getAnalysisName()

    #datasetsMC = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="ZeroBias")

    createRatio = False

    #    for d in datasets.getAllDatasets():
    #        print d.getName()

    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
    ####    dataset1 = datasets.getMCDatasets()
    rateETM120 = 5521.35  # Hz
    #effETM120 = 0.000611208781402 #8.75017364672e-05
    #effETM120 = 0.000619219298648
    effETM120 = 0.000203698623826
    ####    effETM120 = 0.186701136914 # QCD
    scale = rateETM120 / effETM120 * 0.001  #(Hz->kHz)
    #    for d in dataset1:
    #        d.scale(scale)
    dataset2 = dataset1
    createRatio = False

    #if isinstance(datasetsMC,dataset.DatasetManager):
    #    dataset2 = datasetsMC.getMCDatasets()
    #    createRatio = True

    eff1PU = getEfficiency(dataset1, "NumeratorPU", "DenominatorPU")

    scaleGraph(eff1PU, scale)

    namePU = "TauMET_" + analysis + "_nVtx"
    legend1 = "Data"
    legend2 = "Simulation"

    styles.dataStyle.apply(eff1PU)
    eff1PU.SetMarkerSize(1)
    #eff2PU.SetMarkerSize(1.5)

    pPU = plots.PlotBase([histograms.HistoGraph(eff1PU, "eff1", "p", "P")])
    pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1})

    # Fit
    #    yval = fit("Data",pPU,eff1PU,30,59)
    yval = fit("Data", pPU, eff1PU, 5, 59)

    ####    opts = {"ymin": 0, "ymax": 6, "xmax": 60}
    opts = {"ymin": 0, "ymax": 20, "xmax": 60}
    ####    opts = {"ymin": 0, "ymax": 300, "xmax": 60}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    moveLegend = {"dx": -0.5, "dy": -0.1, "dh": -0.1}

    if createRatio:
        pPU.createFrame(os.path.join(plotDir, namePU),
                        createRatio=True,
                        opts=opts,
                        opts2=opts2)
    else:
        pPU.createFrame(os.path.join(plotDir, namePU), opts=opts, opts2=opts2)
    pPU.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegend))

    pPU.getFrame().GetYaxis().SetTitle("L1 rate (kHz)")
    pPU.getFrame().GetXaxis().SetTitle("n vertices")
    if createRatio:
        pPU.getFrame2().GetYaxis().SetTitle("Ratio")
        pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    pPU.draw()

    print "check frame min,max", pPU.getFrame().GetYaxis().GetXmin(
    ), pPU.getFrame().GetYaxis().GetXmax()
    x = array.array('d', [55, 55, 0])
    y = array.array('d', [0, yval, yval])
    n = 3
    vert = ROOT.TGraph(n, x, y)
    vert.SetLineStyle(2)
    vert.SetLineColor(2)
    vert.SetLineWidth(2)
    vert.Draw("L")

    lumi = 0.0
    for d in datasets.getDataDatasets():
        print "luminosity", d.getName(), d.getLuminosity()
        lumi += d.getLuminosity()
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    pPU.save(formats)

    print "Output written in", plotDir
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()
Esempio n. 32
0
def analyze(analysis=None):

    paths = [sys.argv[1]]

    if (len(sys.argv) == 3):
        howAnalyse = sys.argv[2]
    else:
        howAnalyse = "--fit"
#        howAnalyse = "--bin"

    if not analysis == None:
        datasets = dataset.getDatasetsFromMulticrabDirs(
            paths,
            analysisName=analysis,
            excludeTasks="Silver|GluGluHToTauTau_M125")
    else:
        datasets = dataset.getDatasetsFromMulticrabDirs(
            paths, excludeTasks="Silver|GluGluHToTauTau_M125")
        analysis = datasets.getAllDatasets()[0].getAnalysisName()

#    datasetsDY = None
    datasetsDY = dataset.getDatasetsFromMulticrabDirs(
        paths, analysisName=analysis, includeOnlyTasks="DYJetsToLL")
    datasetsDY = dataset.getDatasetsFromMulticrabDirs(
        paths, analysisName=analysis, includeOnlyTasks="DYJetsToLL|Zprime")
    #    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="GluGluHToTauTau_M125|TTJets")
    datasetsH125 = None
    #    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125",emptyDatasetsAsNone=True)
    #    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125")

    datasets.loadLuminosities()

    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
    dataset2 = dataset1
    #    dataset2 = datasets.getMCDatasets()
    if not datasetsDY == None:
        dataset2 = datasetsDY.getMCDatasets()

    histeff1 = getEfficiency(dataset1)
    histeff2 = getEfficiency(dataset2)

    eff1 = convert2TGraph(histeff1)
    eff2 = convert2TGraph(histeff2)

    if isinstance(datasetsH125, dataset.DatasetManager):
        histeff3 = getEfficiency(datasetsH125.getMCDatasets())
        eff3 = convert2TGraph(histeff3)

    styles.dataStyle.apply(eff1)
    styles.mcStyle.apply(eff2)
    eff1.SetMarkerSize(1)
    #    eff2.SetMarkerSize(1.5)
    if isinstance(datasetsH125, dataset.DatasetManager):
        styles.mcStyle.apply(eff3)
        eff3.SetMarkerSize(1.5)
        eff3.SetMarkerColor(4)
        eff3.SetLineColor(4)

#    p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
#                             histograms.HistoGraph(eff2, "eff2", "p", "P"))

    if isinstance(datasetsH125, dataset.DatasetManager):
        p = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1, "eff1", "p", "P"), [
                histograms.HistoGraph(eff2, "eff2", "p", "P"),
                histograms.HistoGraph(eff3, "eff3", "p", "P")
            ])
    elif isinstance(datasetsDY, dataset.DatasetManager):
        p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
                                 histograms.HistoGraph(eff2, "eff2", "p", "P"))
    else:
        p = plots.PlotBase([histograms.HistoGraph(eff1, "eff1", "p", "P")])

## FIT FUNCTIONS: "Sigmoid", "Error", "Gompertz", "Richard","Crystal" ##
## FIT TYPES: binned max likelihood: "ML" , Chi2-fit: "Chi" ##

    if (howAnalyse == "--fit"):
        datafit = fitType("Data", p, histeff1, eff1, 20, 500, "Crystal", "ML")
        mcfit = fitType("MC", p, histeff2, eff2, 20, 500, "Crystal", "ML")

    if isinstance(datasetsH125, dataset.DatasetManager):
        fit("H125", p, eff3, 20, 200)

    opts = {"ymin": 0, "ymax": 1.1}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    #    moveLegend = {"dx": -0.55, "dy": -0.15, "dh": -0.1}
    moveLegend = {"dx": -0.2, "dy": -0.5, "dh": -0.1}
    name = "TauMET_" + analysis + "_DataVsMC_PFTauPt"

    legend1 = "Data"
    #    legend2 = "MC (DY)"
    legend2 = "Simulation"
    legend3 = "MC (H125)"
    createRatio = False
    p.histoMgr.setHistoLegendLabelMany({"eff1": legend1})
    if isinstance(datasetsDY, dataset.DatasetManager):
        p.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2})
        createRatio = True
    if isinstance(datasetsH125, dataset.DatasetManager):
        p.histoMgr.setHistoLegendLabelMany({
            "eff1": legend1,
            "eff2": legend2,
            "eff3": legend3
        })

    if createRatio:
        p.createFrame(os.path.join(plotDir, name),
                      createRatio=createRatio,
                      opts=opts,
                      opts2=opts2)
    else:
        p.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    #    p.getFrame().GetXaxis().SetTitle("#tau-jet p_{T} (GeV/c)")
    p.getFrame().GetXaxis().SetTitle("#tau_{h} p_{T} (GeV/c)")
    if createRatio:
        p.getFrame2().GetYaxis().SetTitle("Ratio")
        p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.5, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    #    histograms.addText(0.5, 0.6, "VLooseIsoPFTau120_Trk50_eta2p1", 17)
    #    histograms.addText(0.5, 0.6, "VLooseIsoPFTau140_Trk50_eta2p1", 17)
    #    label = analysis.split("_")[len(analysis.split("_")) -1]
    label = "2016"

    histograms.addText(0.5, 0.53, label, 17)
    runRange = datasets.loadRunRange()
    histograms.addText(0.5, 0.46, "Runs " + runRange, 17)

    p.draw()

    ## does the ratio of the fits
    if (howAnalyse == "--fit"):
        funcRatio = ROOT.TH1F("", "", 480, 20, 500)
        for i in range(0, 480):
            ratio = datafit.Eval(i + 20 - 1) / mcfit.Eval(i + 20 - 1)
            funcRatio.SetBinContent(i, ratio)
        p.getPad().GetCanvas().cd(2)
        funcRatio.Draw("SAME")
        p.getPad().GetCanvas().cd(1)

##
    lumi = 0.0
    for d in datasets.getDataDatasets():
        if (d.getName() != "SingleMuon_Run2016F_03Feb2017_v1_277932_278800"
                and d.getName() !=
                "SingleMuon_Run2016C_03Feb2017_v1_275656_276283"):
            print "luminosity", d.getName(), d.getLuminosity()
            lumi += d.getLuminosity()
    print "luminosity, sum", lumi
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)

    if (howAnalyse == "--fit"):
        pythonWriter.addParameters(plotDir, label, runRange, lumi, datafit)
        pythonWriter.addMCParameters(label, mcfit)
        pythonWriter.writeJSON(
            os.path.join(plotDir,
                         "tauLegTriggerEfficiency_" + label + "_fit.json"))
        pythonWriter.__init__()  #need to clear the Json arrays


#    if (howAnalyse == "--bin"):
    pythonWriter.addParameters(plotDir, label, runRange, lumi, eff1)
    pythonWriter.addMCParameters(label, eff2)
    pythonWriter.writeJSON(
        os.path.join(plotDir,
                     "tauLegTriggerEfficiency_" + label + "_bin.json"))

    #    if not createRatio:
    #        sys.exit()

    #########################################################################

    histeff1eta = getEfficiency(dataset1, "NumeratorEta", "DenominatorEta")
    histeff2eta = getEfficiency(dataset2, "NumeratorEta", "DenominatorEta")

    eff1eta = convert2TGraph(histeff1eta)
    eff2eta = convert2TGraph(histeff2eta)

    if isinstance(datasetsH125, dataset.DatasetManager):
        histeff3eta = getEfficiency(datasetsH125.getMCDatasets(),
                                    "NumeratorEta", "DenominatorEta")
        eff3eta = convert2TGraph(histeff3eta)
    styles.dataStyle.apply(eff1eta)
    styles.mcStyle.apply(eff2eta)
    eff1eta.SetMarkerSize(1)

    if isinstance(datasetsH125, dataset.DatasetManager):
        styles.mcStyle.apply(eff3eta)
        eff3eta.SetMarkerSize(1.5)
        eff3eta.SetMarkerColor(4)
        eff3eta.SetLineColor(4)

    if isinstance(datasetsH125, dataset.DatasetManager):
        p_eta = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"), [
                histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"),
                histograms.HistoGraph(eff3eta, "eff3eta", "p", "P")
            ])
    elif isinstance(datasetsDY, dataset.DatasetManager):
        p_eta = plots.ComparisonPlot(
            histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"),
            histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"))
    else:
        p_eta = plots.PlotBase(
            [histograms.HistoGraph(eff1eta, "eff1eta", "p", "P")])

    p_eta.histoMgr.setHistoLegendLabelMany({"eff1eta": legend1})
    if isinstance(datasetsDY, dataset.DatasetManager):
        p_eta.histoMgr.setHistoLegendLabelMany({
            "eff1eta": legend1,
            "eff2eta": legend2
        })
    if isinstance(datasetsH125, dataset.DatasetManager):
        p_eta.histoMgr.setHistoLegendLabelMany({
            "eff1eta": legend1,
            "eff2eta": legend2,
            "eff3eta": legend3
        })

    name = "TauMET_" + analysis + "_DataVsMC_PFTauEta"

    if createRatio:
        p_eta.createFrame(os.path.join(plotDir, name),
                          createRatio=createRatio,
                          opts=opts,
                          opts2=opts2)
    else:
        p_eta.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

    moveLegendEta = {"dx": -0.5, "dy": -0.65, "dh": -0.1}
    p_eta.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegendEta))

    p_eta.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p_eta.getFrame().GetXaxis().SetTitle("#tau-jet #eta")
    if createRatio:
        p_eta.getFrame2().GetYaxis().SetTitle("Ratio")
        p_eta.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.46, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.38, label, 17)
    histograms.addText(0.2, 0.31, "Runs " + datasets.loadRunRange(), 17)

    p_eta.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_eta.save(formats)

    #########################################################################

    histeff1phi = getEfficiency(dataset1, "NumeratorPhi", "DenominatorPhi")
    histeff2phi = getEfficiency(dataset2, "NumeratorPhi", "DenominatorPhi")

    eff1phi = convert2TGraph(histeff1phi)
    eff2phi = convert2TGraph(histeff2phi)

    if isinstance(datasetsH125, dataset.DatasetManager):
        histeff3phi = getEfficiency(datasetsH125.getMCDatasets(),
                                    "NumeratorPhi", "DenominatorPhi")
        eff3phi = convert2TGraph(histeff3phi)

    styles.dataStyle.apply(eff1phi)
    styles.mcStyle.apply(eff2phi)
    eff1phi.SetMarkerSize(1)

    if isinstance(datasetsH125, dataset.DatasetManager):
        styles.mcStyle.apply(eff3phi)
        eff3phi.SetMarkerSize(1.5)
        eff3phi.SetMarkerColor(4)
        eff3phi.SetLineColor(4)

    if isinstance(datasetsH125, dataset.DatasetManager):
        p_phi = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"), [
                histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"),
                histograms.HistoGraph(eff3phi, "eff3phi", "p", "P")
            ])
    elif isinstance(datasetsDY, dataset.DatasetManager):
        p_phi = plots.ComparisonPlot(
            histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"),
            histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"))
    else:
        p_phi = plots.PlotBase(
            [histograms.HistoGraph(eff1phi, "eff1phi", "p", "P")])

    p_phi.histoMgr.setHistoLegendLabelMany({"eff1phi": legend1})
    if isinstance(datasetsDY, dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({
            "eff1phi": legend1,
            "eff2phi": legend2
        })
    if isinstance(datasetsH125, dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({
            "eff1phi": legend1,
            "eff2phi": legend2,
            "eff3phi": legend3
        })

    name = "TauMET_" + analysis + "_DataVsMC_PFTauPhi"

    if createRatio:
        p_phi.createFrame(os.path.join(plotDir, name),
                          createRatio=createRatio,
                          opts=opts,
                          opts2=opts2)
    else:
        p_phi.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

    moveLegendPhi = {"dx": -0.5, "dy": -0.65, "dh": -0.1}
    p_phi.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegendPhi))

    p_phi.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p_phi.getFrame().GetXaxis().SetTitle("#tau-jet #phi")
    if createRatio:
        p_phi.getFrame2().GetYaxis().SetTitle("Ratio")
        p_phi.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.46, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.38, label, 17)
    histograms.addText(0.2, 0.31, "Runs " + datasets.loadRunRange(), 17)

    p_phi.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_phi.save(formats)

    #########################################################################

    namePU = "TauMET_" + analysis + "_DataVsMC_nVtx"

    histeff1PU = getEfficiency(dataset1, "NumeratorPU", "DenominatorPU")
    histeff2PU = getEfficiency(dataset2, "NumeratorPU", "DenominatorPU")

    eff1PU = convert2TGraph(histeff1PU)
    eff2PU = convert2TGraph(histeff2PU)

    styles.dataStyle.apply(eff1PU)
    styles.mcStyle.apply(eff2PU)
    eff1PU.SetMarkerSize(1)
    eff2PU.SetMarkerSize(1.5)

    if isinstance(datasetsDY, dataset.DatasetManager):
        pPU = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1PU, "eff1", "p", "P"),
            [histograms.HistoGraph(eff2PU, "eff2", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({
            "eff1": legend1,
            "eff2": legend2
        })
    else:
        pPU = plots.PlotBase([histograms.HistoGraph(eff1PU, "eff1", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1})

    optsPU = {"ymin": 0.01, "ymax": 1.0}
    createRatio = False
    if createRatio:
        pPU.createFrame(os.path.join(plotDir, namePU),
                        createRatio=True,
                        opts=optsPU,
                        opts2=opts2)
    else:
        pPU.createFrame(os.path.join(plotDir, namePU),
                        opts=optsPU,
                        opts2=opts2)

    moveLegend = {"dx": -0.5, "dy": -0.5, "dh": -0.1}
    pPU.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegend))
    #    if createRatio:
    #        pPU.getPad1().SetLogy(True)
    #    else:
    #        pPU.getPad().SetLogy(True)

    pPU.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    pPU.getFrame().GetXaxis().SetTitle("Number of reco vertices")
    if createRatio:
        pPU.getFrame2().GetYaxis().SetTitle("Ratio")
        pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.53, label, 17)
    histograms.addText(0.2, 0.46, "Runs " + datasets.loadRunRange(), 17)

    pPU.draw()
    histograms.addStandardTexts(lumi=lumi)

    pPU.save(formats)

    #########################################################################
    """
    hName = "Pull"
#    hName = "Sub"
    namePull = "TauMET_"+analysis+"_DataVsMC_"+hName+"s"

    plots.mergeRenameReorderForDataMC(datasets)
    datasets.merge("MC", ["TT","WJets","DYJetsToLL","SingleTop","QCD"], keepSources=True)

    drh1 = datasets.getDataset("Data").getDatasetRootHisto(hName)
    drh2 = datasets.getDataset("MC").getDatasetRootHisto(hName)
    drh1.normalizeToOne()
    drh2.normalizeToOne()
    pull1 = drh1.getHistogram()
    pull2 = drh2.getHistogram()

    if isinstance(datasetsH125,dataset.DatasetManager):
        plots.mergeRenameReorderForDataMC(datasetsH125)
        drh3 = datasetsH125.getMCDatasets()[0].getDatasetRootHisto(hName)
        drh3.normalizeToOne()
        pull3 = drh3.getHistogram()

    styles.dataStyle.apply(pull1)
    styles.mcStyle.apply(pull2)
    pull1.SetMarkerSize(1)

    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(pull3)
        pull3.SetMarkerSize(1.5)
        pull3.SetMarkerColor(4)
        pull3.SetLineColor(4)

    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull = plots.ComparisonManyPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                         [histograms.Histo(pull2, "pull2", "p", "P"),
                                          histograms.Histo(pull3, "pull3", "p", "P")])
    else:
        p_pull = plots.ComparisonPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                      histograms.Histo(pull2, "pull2", "p", "P"))

    p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2})
    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2, "pull3": legend3})

    p_pull.createFrame(os.path.join(plotDir, namePull), createRatio=True, opts=opts, opts2=opts2)
    moveLegendPull = {"dx": -0.5, "dy": -0.35, "dh": -0.1}
    p_pull.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegendPull))

    p_pull.getFrame().GetYaxis().SetTitle("Arbitrary units")
#    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T} - #tau-jet p_{T} (GeV/c)")
    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T}/ #tau-jet p_{T} - 1")                                                                                                                                     
    p_pull.getFrame2().GetYaxis().SetTitle("Ratio")
    p_pull.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.75, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.68, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.2, 0.61, "Runs "+runRange, 17)

    p_pull.draw()

    histograms.addStandardTexts(lumi=lumi)
    p_pull.save(formats)
    """
    #########################################################################
    print "Output written in", plotDir
def main():

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

    dirs = []
    dirs.append(sys.argv[1])
    # Read the datasets
    #    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,counters=counters, dataEra=dataEra, analysisBaseName="signalAnalysisInvertedTau")
    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,
                                                    dataEra=dataEra,
                                                    searchMode=searchMode,
                                                    analysisName=analysis,
                                                    optimizationMode=optMode)
    #    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,counters=counters)
    #    datasets = dataset.getDatasetsFromMulticrabCfg(counters=counters, dataEra=dataEra)
    #    datasets.updateNAllEventsToPUWeighted()
    datasets.loadLuminosities()
    datasets.updateNAllEventsToPUWeighted()

    # Take QCD from data
    datasetsQCD = None

    if QCDfromData:

        #datasetsQCD = dataset.getDatasetsFromMulticrabCfg(cfgfile="/home/rkinnune/signalAnalysis/CMSSW_4_2_8_patch2/src/HiggsAnalysis/NtupleAnalysis/test/multicrab_111123_132128/multicrab.cfg", counters=counters)
        datasetsQCD = dataset.getDatasetsFromMulticrabCfg(
            cfgfile=
            "/afs/cern.ch/work/e/epekkari/DataDrivenFakeTaus/CMSSW_5_3_9_patch3/src/HiggsAnalysis/NtupleAnalysis/test/multicrab_140526_122821/multicrab.cfg"
        )
        datasetsQCD.loadLuminosities()
        print "QCDfromData", QCDfromData
        datasetsQCD.mergeData()
        datasetsQCD.remove(datasetsQCD.getMCDatasetNames())
        datasetsQCD.rename("Data", "QCD")

    #for d in datasets.getAllDatasets():
    #    print d.getName()
    #print "-------"
    #plots.mergeRenameReorderForDataMC(datasets)

#    print "Int.Lumi",datasets.getDataset("Data").getLuminosity()

# Remove signals other than M120
    datasets.remove(
        filter(lambda name: "TTToHplus" in name,
               datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "Hplus_taunu_t-channel" in name,
               datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "Hplus_taunu_tW-channel" in name,
               datasets.getAllDatasetNames()))
    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()))

    plots.mergeRenameReorderForDataMC(datasets)

    datasets.merge("EWK",
                   ["WJets", "DYJetsToLL", "SingleTop", "Diboson", "TTJets"],
                   keepSources=True)
    datasets.remove(
        filter(lambda name: "W2Jets" in name, datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "W3Jets" in name, datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "W4Jets" in name, datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "Hplus_taunu_s-channel" in name,
               datasets.getAllDatasetNames()))
    # Remove QCD
    #datasets.remove(filter(lambda name: "QCD" in name, datasets.getAllDatasetNames()))

    datasets_lands = datasets.deepCopy()

    # Set the signal cross sections to the ttbar for datasets for lands
    #    xsect.setHplusCrossSectionsToTop(datasets_lands)

    # Set the signal cross sections to a given BR(t->H), BR(h->taunu)
    xsect.setHplusCrossSectionsToBR(datasets, br_tH=0.01, br_Htaunu=1)

    # Set the signal cross sections to a value from MSSM
    #    xsect.setHplusCrossSectionsToMSSM(datasets, tanbeta=20, mu=200)

    plots.mergeWHandHH(
        datasets
    )  # merging of WH and HH signals must be done after setting the cross section

    # Apply TDR style
    style = tdrstyle.TDRStyle()

    # Create plots
    doPlots(datasets)

    # Write mt histograms to ROOT file
    #    writeTransverseMass(datasets_lands)

    # Print counters
    doCounters(datasets)
Esempio n. 34
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()
Esempio n. 35
0
def analyze(analysis):

    paths = [sys.argv[1]]

    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="Silver|GluGluHToTauTau_M125")
    datasetsDY = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="DYJetsToLL")
#    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="GluGluHToTauTau_M125|TTJets")
    datasetsH125 = None
#    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125",emptyDatasetsAsNone=True)
    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125")

    datasets.loadLuminosities()

    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
#    dataset2 = datasets.getMCDatasets()
    dataset2 = datasetsDY.getMCDatasets()

    eff1 = getEfficiency(dataset1)
    eff2 = getEfficiency(dataset2)
    if isinstance(datasetsH125,dataset.DatasetManager):
        eff3 = getEfficiency(datasetsH125.getMCDatasets())


    styles.dataStyle.apply(eff1)
    styles.mcStyle.apply(eff2)
    eff1.SetMarkerSize(1)
#    eff2.SetMarkerSize(1.5)
    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(eff3)
        eff3.SetMarkerSize(1.5)
        eff3.SetMarkerColor(4)
        eff3.SetLineColor(4)

#    p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
#                             histograms.HistoGraph(eff2, "eff2", "p", "P"))

    if isinstance(datasetsH125,dataset.DatasetManager):
        p = plots.ComparisonManyPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
                                    [histograms.HistoGraph(eff2, "eff2", "p", "P"),
                                     histograms.HistoGraph(eff3, "eff3", "p", "P")])
    else:
        p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
                                 histograms.HistoGraph(eff2, "eff2", "p", "P"))

    fit("Data",p,eff1,20,200)
    fit("MC",p,eff2,20,200)
    if isinstance(datasetsH125,dataset.DatasetManager):
        fit("H125",p,eff3,20,200)

    opts = {"ymin": 0, "ymax": 1.1}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
#    moveLegend = {"dx": -0.55, "dy": -0.15, "dh": -0.1}
    moveLegend = {"dx": -0.2, "dy": -0.5, "dh": -0.1}
    name = "TauMET_"+analysis+"_DataVsMC_PFTauPt"

    legend1 = "Data"
    legend2 = "MC (DY)"
    legend3 = "MC (H125)"
    p.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2})
    if isinstance(datasetsH125,dataset.DatasetManager):
        p.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2, "eff3": legend3})

    p.createFrame(os.path.join(plotDir, name), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p.getFrame().GetXaxis().SetTitle("#tau-jet p_{T} (GeV/c)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.5, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    label = analysis.split("_")[len(analysis.split("_")) -1]
    histograms.addText(0.5, 0.53, label, 17)
    runRange = datasets.loadRunRange()
    histograms.addText(0.5, 0.46, "Runs "+runRange, 17)

    p.draw()
    lumi = 0.0
    for d in datasets.getDataDatasets():
        print "luminosity",d.getName(),d.getLuminosity()
        lumi += d.getLuminosity()
    print "luminosity, sum",lumi
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)


    pythonWriter.addParameters(plotDir,label,runRange,lumi,eff1)
    pythonWriter.addMCParameters(label,eff2)

    pythonWriter.writeJSON(os.path.join(plotDir,"tauLegTriggerEfficiency2015.json"))

    #########################################################################                                                                                                                              

    eff1eta = getEfficiency(dataset1,"NumeratorEta","DenominatorEta")
    eff2eta = getEfficiency(dataset2,"NumeratorEta","DenominatorEta")
    if isinstance(datasetsH125,dataset.DatasetManager):
        eff3eta = getEfficiency(datasetsH125.getMCDatasets(),"NumeratorEta","DenominatorEta")

    styles.dataStyle.apply(eff1eta)
    styles.mcStyle.apply(eff2eta)
    eff1eta.SetMarkerSize(1)

    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(eff3eta)
        eff3eta.SetMarkerSize(1.5)
        eff3eta.SetMarkerColor(4)
        eff3eta.SetLineColor(4)


    if isinstance(datasetsH125,dataset.DatasetManager):
        p_eta = plots.ComparisonManyPlot(histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"),
                                        [histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"),
                                         histograms.HistoGraph(eff3eta, "eff3eta", "p", "P")])
    else:
        p_eta = plots.ComparisonPlot(histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"),
                                     histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"))

    p_eta.histoMgr.setHistoLegendLabelMany({"eff1eta": legend1, "eff2eta": legend2})
    if isinstance(datasetsH125,dataset.DatasetManager):
        p_eta.histoMgr.setHistoLegendLabelMany({"eff1eta": legend1, "eff2eta": legend2, "eff3eta": legend3})

    name = "TauMET_"+analysis+"_DataVsMC_PFTauEta"
    p_eta.createFrame(os.path.join(plotDir, name), createRatio=True, opts=opts, opts2=opts2)
    moveLegendEta = {"dx": -0.5, "dy": -0.65, "dh": -0.1}
    p_eta.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegendEta))

    p_eta.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p_eta.getFrame().GetXaxis().SetTitle("#tau-jet #eta")
    p_eta.getFrame2().GetYaxis().SetTitle("Ratio")
    p_eta.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.46, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.38, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.2, 0.31, "Runs "+datasets.loadRunRange(), 17)

    p_eta.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_eta.save(formats)

    #########################################################################

    namePU = "TauMET_"+analysis+"_DataVsMC_nVtx"

    eff1PU = getEfficiency(dataset1,"NumeratorPU","DenominatorPU")
    eff2PU = getEfficiency(dataset2,"NumeratorPU","DenominatorPU")

    styles.dataStyle.apply(eff1PU)
    styles.mcStyle.apply(eff2PU)
    eff1PU.SetMarkerSize(1)
    eff2PU.SetMarkerSize(1.5)

    pPU = plots.ComparisonManyPlot(histograms.HistoGraph(eff1PU, "eff1", "p", "P"),
                                   [histograms.HistoGraph(eff2PU, "eff2", "p", "P")])


    pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2})

    optsPU = {"ymin": 0.001, "ymax": 0.1}
    pPU.createFrame(os.path.join(plotDir, namePU), createRatio=True, opts=optsPU, opts2=opts2)
    pPU.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
    pPU.getPad1().SetLogy(True)

    pPU.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    pPU.getFrame().GetXaxis().SetTitle("Number of reco vertices")
    pPU.getFrame2().GetYaxis().SetTitle("Ratio")
    pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.5, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.5, 0.53, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.5, 0.46, "Runs "+datasets.loadRunRange(), 17)

    pPU.draw()
    histograms.addStandardTexts(lumi=lumi)

    pPU.save(formats)

    #########################################################################

    hName = "Pull"
#    hName = "Sub"
    namePull = "TauMET_"+analysis+"_DataVsMC_"+hName+"s"

    plots.mergeRenameReorderForDataMC(datasets)
    datasets.merge("MC", ["TTJets","WJets","DYJetsToLL","SingleTop","QCD"], keepSources=True)

    drh1 = datasets.getDataset("Data").getDatasetRootHisto(hName)
    drh2 = datasets.getDataset("MC").getDatasetRootHisto(hName)
    drh1.normalizeToOne()
    drh2.normalizeToOne()
    pull1 = drh1.getHistogram()
    pull2 = drh2.getHistogram()

    if isinstance(datasetsH125,dataset.DatasetManager):
        plots.mergeRenameReorderForDataMC(datasetsH125)
        drh3 = datasetsH125.getMCDatasets()[0].getDatasetRootHisto(hName)
        drh3.normalizeToOne()
        pull3 = drh3.getHistogram()

    styles.dataStyle.apply(pull1)
    styles.mcStyle.apply(pull2)
    pull1.SetMarkerSize(1)

    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(pull3)
        pull3.SetMarkerSize(1.5)
        pull3.SetMarkerColor(4)
        pull3.SetLineColor(4)

    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull = plots.ComparisonManyPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                         [histograms.Histo(pull2, "pull2", "p", "P"),
                                          histograms.Histo(pull3, "pull3", "p", "P")])
    else:
        p_pull = plots.ComparisonPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                      histograms.Histo(pull2, "pull2", "p", "P"))

    p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2})
    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2, "pull3": legend3})

    p_pull.createFrame(os.path.join(plotDir, namePull), createRatio=True, opts=opts, opts2=opts2)
    moveLegendPull = {"dx": -0.5, "dy": -0.35, "dh": -0.1}
    p_pull.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegendPull))

    p_pull.getFrame().GetYaxis().SetTitle("Arbitrary units")
#    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T} - #tau-jet p_{T} (GeV/c)")
    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T}/ #tau-jet p_{T} - 1")                                                                                                                                     
    p_pull.getFrame2().GetYaxis().SetTitle("Ratio")
    p_pull.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.75, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.68, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.2, 0.61, "Runs "+runRange, 17)

    p_pull.draw()

    histograms.addStandardTexts(lumi=lumi)
    p_pull.save(formats)

    #########################################################################                                                                                                                               
    print "Output written in",plotDir
Esempio n. 36
0
def analyze(analysis=None):

    paths = [sys.argv[1]]

    if not analysis == None:
        #        datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis, includeOnlyTasks="Tau|TT")
        datasets = dataset.getDatasetsFromMulticrabDirs(paths,
                                                        analysisName=analysis)
    else:
        datasets = dataset.getDatasetsFromMulticrabDirs(paths)
        analysis = datasets.getAllDatasets()[0].getAnalysisName()

    analysisList = []
    #    analysisList.append("METLeg_2016MET80_MET80")
    #    analysisList.append("METLeg_2016ICHEP_MET90")
    #    analysisList.append("METLeg_2016_MET90")

    met_re = re.compile("_(?P<met>MET\d+)$")
    if True:
        #for analysis in analysisList:

        met = "METX"
        met_match = met_re.search(analysis)
        if met_match:
            met = met_match.group("met")

        print "Analysis", analysis
        datasets = dataset.getDatasetsFromMulticrabDirs(paths,
                                                        analysisName=analysis)
        #    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="Tau\S+25ns$|TTJets$")
        #        datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="Tau_Run2015C|Tau\S+25ns_Silver$|DYJetsToLL|WJetsToLNu$")
        #    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="Tau_Run2015D_PromptReco_v4_246908_260426_25ns$|DYJetsToLL_M_50$")
        #    datasetsMC = None
        datasetsMC = dataset.getDatasetsFromMulticrabDirs(
            paths, analysisName=analysis, excludeTasks="Tau_")

        for d in datasets.getAllDatasets():
            print d.getName()
            style = tdrstyle.TDRStyle()

            dataset1 = datasets.getDataDatasets()
            dataset2 = dataset1
            createRatio = False
            if isinstance(datasetsMC, dataset.DatasetManager):
                dataset2 = datasetsMC.getMCDatasets()
                createRatio = True

        eff1_MET80 = getEfficiency(dataset1)
        eff2_MET80 = getEfficiency(dataset2)

        styles.dataStyle.apply(eff1_MET80)
        eff1_MET80.SetMarkerSize(1)
        if createRatio:
            styles.mcStyle.apply(eff2_MET80)
            eff2_MET80.SetMarkerSize(1.5)

        if createRatio:
            p = plots.ComparisonPlot(
                histograms.HistoGraph(eff1_MET80, "eff1_MET80", "p", "P"),
                histograms.HistoGraph(eff2_MET80, "eff2_MET80", "p", "P"))
        else:
            p = plots.PlotBase(
                [histograms.HistoGraph(eff1_MET80, "eff1_MET80", "p", "P")])

        #from plotTauLegEfficiency import fit
        #fit("Data",p,eff1_MET80,20,300)
        #fit("MC",p,eff2_MET80,20,300)

        opts = {"ymin": 0, "ymax": 1.1}
        opts2 = {"ymin": 0.5, "ymax": 1.5}
        moveLegend = {"dx": -0.55, "dy": -0.15}

        name = "TauMET_" + analysis + "_DataVsMC_PFMET"

        legend1 = "Data"
        #    legend2 = "MC (TTJets)"
        #legend2 = "MC"
        legend2 = "Simulation"

        if createRatio:
            p.histoMgr.setHistoLegendLabelMany({
                "eff1_MET80": legend1,
                "eff2_MET80": legend2
            })
            p.createFrame(os.path.join(plotDir, name),
                          createRatio=True,
                          opts=opts,
                          opts2=opts2)
        else:
            p.histoMgr.setHistoLegendLabelMany({"eff1_MET80": legend1})
            p.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

        p.setLegend(
            histograms.moveLegend(histograms.createLegend(y1=0.8),
                                  **moveLegend))

        p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
        p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
        if createRatio:
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)

#        histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1_"+met, 17)
        histograms.addText(0.4, 0.87, "LooseIsoPFTau50_Trk30_eta2p1_" + met,
                           17)
        #    histograms.addText(0.2, 0.53, analysis.split("_")[len(analysis.split("_")) -1], 17)
        #        label = analysis.split("_")[1]
        #        label = "2016"
        label = string.replace(analysis, "METLeg_", "")
        histograms.addText(0.2, 0.53, label, 17)
        runRange = datasets.loadRunRange()
        histograms.addText(0.2, 0.46, "Runs " + runRange, 17)

        p.draw()
        lumi = 0.0
        for d in datasets.getDataDatasets():
            print "luminosity", d.getName(), d.getLuminosity()
            lumi += d.getLuminosity()
        print "luminosity, sum", lumi
        histograms.addStandardTexts(lumi=lumi)

        if not os.path.exists(plotDir):
            os.mkdir(plotDir)
        p.save(formats)

        pythonWriter.addParameters(plotDir, label, runRange, lumi, eff1_MET80)
        pythonWriter.addMCParameters(label, eff2_MET80)

    pythonWriter.writeJSON(
        os.path.join(plotDir, "metLegTriggerEfficiency_" + label + ".json"))

    #########################################################################

    eff1phi = getEfficiency(dataset1, "NumeratorPhi", "DenominatorPhi")
    eff2phi = getEfficiency(dataset2, "NumeratorPhi", "DenominatorPhi")

    styles.dataStyle.apply(eff1phi)
    styles.mcStyle.apply(eff2phi)
    eff1phi.SetMarkerSize(1)

    if isinstance(datasetsMC, dataset.DatasetManager):
        p_phi = plots.ComparisonPlot(
            histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"),
            histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"))
    else:
        p_phi = plots.PlotBase(
            [histograms.HistoGraph(eff1phi, "eff1phi", "p", "P")])

    p_phi.histoMgr.setHistoLegendLabelMany({"eff1phi": legend1})
    if isinstance(datasetsMC, dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({
            "eff1phi": legend1,
            "eff2phi": legend2
        })

    opts = {"ymin": 0, "ymax": 0.3}
    name = "TauMET_" + analysis + "_DataVsMC_PFMETPhi"

    if createRatio:
        p_phi.createFrame(os.path.join(plotDir, name),
                          createRatio=createRatio,
                          opts=opts,
                          opts2=opts2)
    else:
        p_phi.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)


#    moveLegendPhi = {"dx": -0.5, "dy": -0.6, "dh": -0.1}
    moveLegendPhi = {"dx": -0.55, "dy": -0.15, "dh": -0.1}
    p_phi.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegendPhi))

    p_phi.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p_phi.getFrame().GetXaxis().SetTitle("MET Type 1 #phi")
    if createRatio:
        p_phi.getFrame2().GetYaxis().SetTitle("Ratio")
        p_phi.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.4, 0.86, "LooseIsoPFTau50_Trk30_eta2p1_" + met, 17)
    #   histograms.addText(0.4, 0.78, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.4, 0.78, "2016", 17)
    histograms.addText(0.4, 0.71, "Runs " + datasets.loadRunRange(), 17)

    p_phi.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_phi.save(formats)

    #########################################################################
    """
    #### MET120

    analysis = "METLeg_2015CD_MET120"
    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis)
    for d in datasets.getAllDatasets():
        print d.getName()
    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
    dataset2 = datasets.getMCDatasets()

    eff1_MET120 = getEfficiency(dataset1)
    eff2_MET120 = getEfficiency(dataset2)

    styles.dataStyle.apply(eff1_MET120)
    styles.mcStyle.apply(eff2_MET120)
    eff1_MET120.SetMarkerSize(1)
    eff2_MET120.SetMarkerSize(1.5)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff1_MET120, "eff1_MET120", "p", "P"),
                             histograms.HistoGraph(eff2_MET120, "eff2_MET120", "p", "P"))

    opts = {"ymin": 0, "ymax": 1.1}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    moveLegend = {"dx": -0.55, "dy": -0.15}

    name = "DataVsMC_L1HLTMET_PFMET_MET120"

    legend1 = "Data"
    legend2 = "MC"
    p.histoMgr.setHistoLegendLabelMany({"eff1_MET120": legend1, "eff2_MET120": legend2})

    p.createFrame(os.path.join(plotDir, name), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    lumi = 0.0
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
    """

    # CaloMET
    """
    #### MET80

    analysisc = "METLeg_2016B_CaloMET_MET80"

    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc)
    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc,excludeTasks="Tau\S+25ns_Silver$")
#    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc,includeOnlyTasks="Tau\S+25ns$|TTJets$")

    style = tdrstyle.TDRStyle()

    dataset1c = datasetsc.getDataDatasets()
    dataset2c = datasetsc.getMCDatasets()

#    eff1c_MET80 = getEfficiency(dataset1c)
    eff2c_MET80 = getEfficiency(dataset2c)

#    styles.dataStyle.apply(eff1c_MET80)
    styles.mcStyle.apply(eff2c_MET80)
#    eff1c_MET80.SetMarkerSize(1)
    eff2c_MET80.SetMarkerSize(1.5)
    eff2c_MET80.SetMarkerColor(4)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff2_MET80, "eff2_MET80", "p", "P"),
                             histograms.HistoGraph(eff2c_MET80, "eff2c_MET80", "p", "P"))

    namec = "TauMET_"+analysis+"_MC_TrgBitVsCaloMET80_PFMET"

    legend1c = "MC, trigger bit"
    legend2c = "MC, CaloMET > 80"
    p.histoMgr.setHistoLegendLabelMany({"eff2_MET80": legend1c, "eff2c_MET80": legend2c})

    p.createFrame(os.path.join(plotDir, namec), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    lumi = 0.0
    for d in datasets.getDataDatasets():
        print "luminosity",d.getName(),d.getLuminosity()
        lumi += d.getLuminosity()
    print "luminosity, sum",lumi
    histograms.addStandardTexts(lumi=lumi)
    histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1_MET80", 17)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
    """
    """
    #### MET120 

    analysisc = "METLeg_2015A_CaloMET_MET120"
    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc)

    style = tdrstyle.TDRStyle()

    dataset1c = datasetsc.getDataDatasets()
    dataset2c = datasetsc.getMCDatasets()

    eff1c_MET120 = getEfficiency(dataset1c)
    eff2c_MET120 = getEfficiency(dataset2c)

    styles.dataStyle.apply(eff1c_MET120)
    styles.mcStyle.apply(eff1c_MET120)
    eff1c_MET120.SetMarkerSize(1)
    eff2c_MET120.SetMarkerSize(1.5)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff2_MET120, "eff2_MET120", "p", "P"),
                             histograms.HistoGraph(eff2c_MET120, "eff2c_MET120", "p", "P"))

    namec = "MC_TrgBitVsCaloMET120_L1HLTMET_PFMET"

    legend1c = "MC, trigger bit"
    legend2c = "MC, CaloMET > 120"
    p.histoMgr.setHistoLegendLabelMany({"eff2_MET120": legend1c, "eff2c_MET120": legend2c})

    p.createFrame(os.path.join(plotDir, namec), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    lumi = 0.0
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
    """

    #########################################################################

    namePU = "TauMET_" + analysis + "_DataVsMC_nVtx"

    eff1PU = getEfficiency(dataset1, "NumeratorPU", "DenominatorPU")
    eff2PU = getEfficiency(dataset2, "NumeratorPU", "DenominatorPU")

    styles.dataStyle.apply(eff1PU)
    styles.mcStyle.apply(eff2PU)
    eff1PU.SetMarkerSize(1)
    eff2PU.SetMarkerSize(1.5)

    if isinstance(datasetsMC, dataset.DatasetManager):
        pPU = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1PU, "eff1", "p", "P"),
            [histograms.HistoGraph(eff2PU, "eff2", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({
            "eff1": legend1,
            "eff2": legend2
        })
    else:
        pPU = plots.PlotBase([histograms.HistoGraph(eff1PU, "eff1", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1})

    opts = {"ymin": 0.001, "ymax": 1.0}
    moveLegend = {"dx": -0.33, "dy": -0.25, "dh": -0.1}
    createRatio = False
    if createRatio:
        pPU.createFrame(os.path.join(plotDir, namePU),
                        createRatio=True,
                        opts=opts,
                        opts2=opts2)
    else:
        pPU.createFrame(os.path.join(plotDir, namePU), opts=opts, opts2=opts2)
    pPU.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegend))
    #    if createRatio:
    #        pPU.getPad1().SetLogy(True)
    #    else:
    #        pPU.getPad().SetLogy(True)

    pPU.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    pPU.getFrame().GetXaxis().SetTitle("Number of reco vertices")
    if createRatio:
        pPU.getFrame2().GetYaxis().SetTitle("Ratio")
        pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.4, 0.85, "LooseIsoPFTau50_Trk30_eta2p1_MET90", 17)
    #    histograms.addText(0.4, 0.78, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.4, 0.78, "2016", 17)
    histograms.addText(0.4, 0.71, "Runs " + datasets.loadRunRange(), 17)

    pPU.draw()
    histograms.addStandardTexts(lumi=lumi)

    pPU.save(formats)

    print "Output written in", plotDir
def analyze(analysis=None):

    paths = [sys.argv[1]]

    datasets = dataset.getDatasetsFromMulticrabDirs(paths)
#    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="SingleNeutrino")
#    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="QCD")

#    analysis = datasets.getAllDatasets()[0].getAnalysisName()

    #datasetsMC = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="ZeroBias")


    createRatio = False

#    for d in datasets.getAllDatasets():
#        print d.getName()

    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
####    dataset1 = datasets.getMCDatasets()
    rateETM120 = 5521.35 # Hz
    #effETM120 = 0.000611208781402 #8.75017364672e-05
    #effETM120 = 0.000619219298648
    effETM120 = 0.000203698623826
####    effETM120 = 0.186701136914 # QCD
    scale = rateETM120/effETM120*0.001 #(Hz->kHz)
#    for d in dataset1:
#        d.scale(scale)
    dataset2 = dataset1
    createRatio = False

        #if isinstance(datasetsMC,dataset.DatasetManager):
        #    dataset2 = datasetsMC.getMCDatasets()
        #    createRatio = True

    eff1PU = getEfficiency(dataset1,"NumeratorPU","DenominatorPU")

    scaleGraph(eff1PU,scale)


    namePU = "TauMET_"+analysis+"_nVtx"
    legend1 = "Data"
    legend2 = "Simulation"


    styles.dataStyle.apply(eff1PU)
    eff1PU.SetMarkerSize(1)
    #eff2PU.SetMarkerSize(1.5)

    pPU = plots.PlotBase([histograms.HistoGraph(eff1PU, "eff1", "p", "P")])
    pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1})


    # Fit
#    yval = fit("Data",pPU,eff1PU,30,59)
    yval = fit("Data",pPU,eff1PU,5,59)

####    opts = {"ymin": 0, "ymax": 6, "xmax": 60}
    opts = {"ymin": 0, "ymax": 20, "xmax": 60}
####    opts = {"ymin": 0, "ymax": 300, "xmax": 60}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    moveLegend = {"dx": -0.5, "dy": -0.1, "dh": -0.1}

    if createRatio:
        pPU.createFrame(os.path.join(plotDir, namePU), createRatio=True, opts=opts, opts2=opts2)
    else:
        pPU.createFrame(os.path.join(plotDir, namePU), opts=opts, opts2=opts2)
    pPU.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

    pPU.getFrame().GetYaxis().SetTitle("L1 rate (kHz)")
    pPU.getFrame().GetXaxis().SetTitle("n vertices")
    if createRatio:
        pPU.getFrame2().GetYaxis().SetTitle("Ratio")
        pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    pPU.draw()

    print "check frame min,max",pPU.getFrame().GetYaxis().GetXmin(),pPU.getFrame().GetYaxis().GetXmax()
    x = array.array('d',[55,55,0])
    y = array.array('d',[0,yval,yval])
    n = 3
    vert = ROOT.TGraph(n,x,y)
    vert.SetLineStyle(2)
    vert.SetLineColor(2)
    vert.SetLineWidth(2)
    vert.Draw("L")

    lumi = 0.0
    for d in datasets.getDataDatasets():
        print "luminosity",d.getName(),d.getLuminosity()
        lumi += d.getLuminosity()
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    pPU.save(formats)

    print "Output written in",plotDir
def analyze(analysis=None):

    paths = [sys.argv[1]]

    if (len(sys.argv) == 3):
        howAnalyse = sys.argv[2]
    else:
       howAnalyse = "--fit" 
#        howAnalyse = "--bin"

    if not analysis == None:
        datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="Silver|GluGluHToTauTau_M125")
    else:
        datasets = dataset.getDatasetsFromMulticrabDirs(paths,excludeTasks="Silver|GluGluHToTauTau_M125")
        analysis = datasets.getAllDatasets()[0].getAnalysisName()

#    datasetsDY = None
    datasetsDY = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="DYJetsToLL")
    datasetsDY = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="DYJetsToLL|Zprime")
#    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="GluGluHToTauTau_M125|TTJets")
    datasetsH125 = None
#    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125",emptyDatasetsAsNone=True)
#    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125")

    datasets.loadLuminosities()

    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
    dataset2 = dataset1
#    dataset2 = datasets.getMCDatasets()
    if not datasetsDY == None:
        dataset2 = datasetsDY.getMCDatasets()


    histeff1 = getEfficiency(dataset1)
    histeff2 = getEfficiency(dataset2)

    eff1 = convert2TGraph(histeff1)
    eff2 = convert2TGraph(histeff2)

    if isinstance(datasetsH125,dataset.DatasetManager):
        histeff3 = getEfficiency(datasetsH125.getMCDatasets())
        eff3 = convert2TGraph(histeff3)

    styles.dataStyle.apply(eff1)
    styles.mcStyle.apply(eff2)
    eff1.SetMarkerSize(1)
#    eff2.SetMarkerSize(1.5)
    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(eff3)
        eff3.SetMarkerSize(1.5)
        eff3.SetMarkerColor(4)
        eff3.SetLineColor(4)

#    p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
#                             histograms.HistoGraph(eff2, "eff2", "p", "P"))

    if isinstance(datasetsH125,dataset.DatasetManager):
        p = plots.ComparisonManyPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
                                    [histograms.HistoGraph(eff2, "eff2", "p", "P"),
                                     histograms.HistoGraph(eff3, "eff3", "p", "P")])
    elif isinstance(datasetsDY,dataset.DatasetManager):
        p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
                                 histograms.HistoGraph(eff2, "eff2", "p", "P"))
    else:
        p = plots.PlotBase([histograms.HistoGraph(eff1, "eff1", "p", "P")])


    
## FIT FUNCTIONS: "Sigmoid", "Error", "Gompertz", "Richard","Crystal" ##
## FIT TYPES: binned max likelihood: "ML" , Chi2-fit: "Chi" ##
    
    if (howAnalyse == "--fit" ):
        datafit = fitType("Data",p,histeff1,eff1,20,500,"Crystal","ML")
        mcfit = fitType("MC",p,histeff2,eff2,20,500,"Crystal","ML")
    
    if isinstance(datasetsH125,dataset.DatasetManager):
        fit("H125",p,eff3,20,200)
    
    opts = {"ymin": 0, "ymax": 1.1}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
#    moveLegend = {"dx": -0.55, "dy": -0.15, "dh": -0.1}
    moveLegend = {"dx": -0.2, "dy": -0.5, "dh": -0.1}
    name = "TauMET_"+analysis+"_DataVsMC_PFTauPt"

    legend1 = "Data"
#    legend2 = "MC (DY)"
    legend2 = "Simulation"
    legend3 = "MC (H125)"
    createRatio = False
    p.histoMgr.setHistoLegendLabelMany({"eff1": legend1})
    if isinstance(datasetsDY,dataset.DatasetManager):
        p.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2})
        createRatio = True
    if isinstance(datasetsH125,dataset.DatasetManager):
        p.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2, "eff3": legend3})

    if createRatio:
        p.createFrame(os.path.join(plotDir, name), createRatio=createRatio, opts=opts, opts2=opts2)
    else:
        p.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
#    p.getFrame().GetXaxis().SetTitle("#tau-jet p_{T} (GeV/c)")
    p.getFrame().GetXaxis().SetTitle("#tau_{h} p_{T} (GeV/c)")
    if createRatio:
        p.getFrame2().GetYaxis().SetTitle("Ratio")
        p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    
    
    histograms.addText(0.5, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
#    histograms.addText(0.5, 0.6, "VLooseIsoPFTau120_Trk50_eta2p1", 17)
#    histograms.addText(0.5, 0.6, "VLooseIsoPFTau140_Trk50_eta2p1", 17)
#    label = analysis.split("_")[len(analysis.split("_")) -1]
    label = "2016"

    histograms.addText(0.5, 0.53, label, 17)
    runRange = datasets.loadRunRange()
    histograms.addText(0.5, 0.46, "Runs "+runRange, 17)

    p.draw()


## does the ratio of the fits
    if (howAnalyse=="--fit"):
        funcRatio = ROOT.TH1F("","",480,20,500)
        for i in range(0,480):
            ratio = datafit.Eval(i+20-1)/mcfit.Eval(i+20-1) 
            funcRatio.SetBinContent(i,ratio)
        p.getPad().GetCanvas().cd(2)
        funcRatio.Draw("SAME")
        p.getPad().GetCanvas().cd(1)

##
    lumi = 0.0
    for d in datasets.getDataDatasets():
      if(d.getName() != "SingleMuon_Run2016F_03Feb2017_v1_277932_278800" and d.getName() != "SingleMuon_Run2016C_03Feb2017_v1_275656_276283"):
        print "luminosity",d.getName(),d.getLuminosity()
        lumi += d.getLuminosity()
    print "luminosity, sum",lumi
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
   
    if (howAnalyse == "--fit"):
        pythonWriter.addParameters(plotDir,label,runRange,lumi,datafit)
        pythonWriter.addMCParameters(label,mcfit)
        pythonWriter.writeJSON(os.path.join(plotDir,"tauLegTriggerEfficiency_"+label+"_fit.json"))
        pythonWriter.__init__() #need to clear the Json arrays
#    if (howAnalyse == "--bin"):
    pythonWriter.addParameters(plotDir,label,runRange,lumi,eff1)
    pythonWriter.addMCParameters(label,eff2)
    pythonWriter.writeJSON(os.path.join(plotDir,"tauLegTriggerEfficiency_"+label+"_bin.json"))

#    if not createRatio:
#        sys.exit()

    #########################################################################                                                                                                                              

    histeff1eta = getEfficiency(dataset1,"NumeratorEta","DenominatorEta")
    histeff2eta = getEfficiency(dataset2,"NumeratorEta","DenominatorEta")

    eff1eta = convert2TGraph(histeff1eta)
    eff2eta = convert2TGraph(histeff2eta)

    if isinstance(datasetsH125,dataset.DatasetManager):
        histeff3eta = getEfficiency(datasetsH125.getMCDatasets(),"NumeratorEta","DenominatorEta")
        eff3eta = convert2TGraph(histeff3eta)
    styles.dataStyle.apply(eff1eta)
    styles.mcStyle.apply(eff2eta)
    eff1eta.SetMarkerSize(1)

    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(eff3eta)
        eff3eta.SetMarkerSize(1.5)
        eff3eta.SetMarkerColor(4)
        eff3eta.SetLineColor(4)


    if isinstance(datasetsH125,dataset.DatasetManager):
        p_eta = plots.ComparisonManyPlot(histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"),
                                        [histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"),
                                         histograms.HistoGraph(eff3eta, "eff3eta", "p", "P")])
    elif isinstance(datasetsDY,dataset.DatasetManager):
        p_eta = plots.ComparisonPlot(histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"),
                                     histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"))
    else:
        p_eta = plots.PlotBase([histograms.HistoGraph(eff1eta, "eff1eta", "p", "P")])

    p_eta.histoMgr.setHistoLegendLabelMany({"eff1eta": legend1})
    if isinstance(datasetsDY,dataset.DatasetManager):
        p_eta.histoMgr.setHistoLegendLabelMany({"eff1eta": legend1, "eff2eta": legend2})
    if isinstance(datasetsH125,dataset.DatasetManager):
        p_eta.histoMgr.setHistoLegendLabelMany({"eff1eta": legend1, "eff2eta": legend2, "eff3eta": legend3})

    name = "TauMET_"+analysis+"_DataVsMC_PFTauEta"

    if createRatio:
        p_eta.createFrame(os.path.join(plotDir, name), createRatio=createRatio, opts=opts, opts2=opts2)
    else:
        p_eta.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

    moveLegendEta = {"dx": -0.5, "dy": -0.65, "dh": -0.1}
    p_eta.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegendEta))

    p_eta.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p_eta.getFrame().GetXaxis().SetTitle("#tau-jet #eta")
    if createRatio:
        p_eta.getFrame2().GetYaxis().SetTitle("Ratio")
        p_eta.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.46, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.38, label, 17)
    histograms.addText(0.2, 0.31, "Runs "+datasets.loadRunRange(), 17)

    p_eta.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_eta.save(formats)

    #########################################################################
    
    histeff1phi = getEfficiency(dataset1,"NumeratorPhi","DenominatorPhi")
    histeff2phi = getEfficiency(dataset2,"NumeratorPhi","DenominatorPhi")

    eff1phi = convert2TGraph(histeff1phi)
    eff2phi = convert2TGraph(histeff2phi)

    if isinstance(datasetsH125,dataset.DatasetManager):
        histeff3phi = getEfficiency(datasetsH125.getMCDatasets(),"NumeratorPhi","DenominatorPhi")
        eff3phi = convert2TGraph(histeff3phi)
    

    styles.dataStyle.apply(eff1phi)
    styles.mcStyle.apply(eff2phi)
    eff1phi.SetMarkerSize(1)

    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(eff3phi)
        eff3phi.SetMarkerSize(1.5)
        eff3phi.SetMarkerColor(4)
        eff3phi.SetLineColor(4)

    if isinstance(datasetsH125,dataset.DatasetManager):
        p_phi = plots.ComparisonManyPlot(histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"),
                                        [histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"),
                                         histograms.HistoGraph(eff3phi, "eff3phi", "p", "P")])
    elif isinstance(datasetsDY,dataset.DatasetManager):
        p_phi = plots.ComparisonPlot(histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"),
                                     histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"))
    else:
        p_phi = plots.PlotBase([histograms.HistoGraph(eff1phi, "eff1phi", "p", "P")])

    p_phi.histoMgr.setHistoLegendLabelMany({"eff1phi": legend1})
    if isinstance(datasetsDY,dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({"eff1phi": legend1, "eff2phi": legend2})
    if isinstance(datasetsH125,dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({"eff1phi": legend1, "eff2phi": legend2, "eff3phi": legend3})

    name = "TauMET_"+analysis+"_DataVsMC_PFTauPhi"

    if createRatio:
        p_phi.createFrame(os.path.join(plotDir, name), createRatio=createRatio, opts=opts, opts2=opts2)
    else:
        p_phi.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

    moveLegendPhi = {"dx": -0.5, "dy": -0.65, "dh": -0.1}
    p_phi.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegendPhi))

    p_phi.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p_phi.getFrame().GetXaxis().SetTitle("#tau-jet #phi")
    if createRatio:
        p_phi.getFrame2().GetYaxis().SetTitle("Ratio")
        p_phi.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.46, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.38, label, 17)
    histograms.addText(0.2, 0.31, "Runs "+datasets.loadRunRange(), 17)

    p_phi.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_phi.save(formats)
    
    ######################################################################### 

    namePU = "TauMET_"+analysis+"_DataVsMC_nVtx"

    histeff1PU = getEfficiency(dataset1,"NumeratorPU","DenominatorPU")
    histeff2PU = getEfficiency(dataset2,"NumeratorPU","DenominatorPU")

    eff1PU = convert2TGraph(histeff1PU)
    eff2PU = convert2TGraph(histeff2PU)

    styles.dataStyle.apply(eff1PU)
    styles.mcStyle.apply(eff2PU)
    eff1PU.SetMarkerSize(1)
    eff2PU.SetMarkerSize(1.5)

    if isinstance(datasetsDY,dataset.DatasetManager):
        pPU = plots.ComparisonManyPlot(histograms.HistoGraph(eff1PU, "eff1", "p", "P"),
                                      [histograms.HistoGraph(eff2PU, "eff2", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2})
    else:
        pPU = plots.PlotBase([histograms.HistoGraph(eff1PU, "eff1", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1})

    optsPU = {"ymin": 0.01, "ymax": 1.0}
    createRatio = False
    if createRatio:
        pPU.createFrame(os.path.join(plotDir, namePU), createRatio=True, opts=optsPU, opts2=opts2)
    else:
        pPU.createFrame(os.path.join(plotDir, namePU), opts=optsPU, opts2=opts2)

    moveLegend = {"dx": -0.5, "dy": -0.5, "dh": -0.1}
    pPU.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
#    if createRatio:
#        pPU.getPad1().SetLogy(True)
#    else:
#        pPU.getPad().SetLogy(True)

    pPU.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    pPU.getFrame().GetXaxis().SetTitle("Number of reco vertices")
    if createRatio:
        pPU.getFrame2().GetYaxis().SetTitle("Ratio")
        pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.53, label, 17)
    histograms.addText(0.2, 0.46, "Runs "+datasets.loadRunRange(), 17)

    pPU.draw()
    histograms.addStandardTexts(lumi=lumi)

    pPU.save(formats)

    #########################################################################
    """
    hName = "Pull"
#    hName = "Sub"
    namePull = "TauMET_"+analysis+"_DataVsMC_"+hName+"s"

    plots.mergeRenameReorderForDataMC(datasets)
    datasets.merge("MC", ["TT","WJets","DYJetsToLL","SingleTop","QCD"], keepSources=True)

    drh1 = datasets.getDataset("Data").getDatasetRootHisto(hName)
    drh2 = datasets.getDataset("MC").getDatasetRootHisto(hName)
    drh1.normalizeToOne()
    drh2.normalizeToOne()
    pull1 = drh1.getHistogram()
    pull2 = drh2.getHistogram()

    if isinstance(datasetsH125,dataset.DatasetManager):
        plots.mergeRenameReorderForDataMC(datasetsH125)
        drh3 = datasetsH125.getMCDatasets()[0].getDatasetRootHisto(hName)
        drh3.normalizeToOne()
        pull3 = drh3.getHistogram()

    styles.dataStyle.apply(pull1)
    styles.mcStyle.apply(pull2)
    pull1.SetMarkerSize(1)

    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(pull3)
        pull3.SetMarkerSize(1.5)
        pull3.SetMarkerColor(4)
        pull3.SetLineColor(4)

    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull = plots.ComparisonManyPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                         [histograms.Histo(pull2, "pull2", "p", "P"),
                                          histograms.Histo(pull3, "pull3", "p", "P")])
    else:
        p_pull = plots.ComparisonPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                      histograms.Histo(pull2, "pull2", "p", "P"))

    p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2})
    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2, "pull3": legend3})

    p_pull.createFrame(os.path.join(plotDir, namePull), createRatio=True, opts=opts, opts2=opts2)
    moveLegendPull = {"dx": -0.5, "dy": -0.35, "dh": -0.1}
    p_pull.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegendPull))

    p_pull.getFrame().GetYaxis().SetTitle("Arbitrary units")
#    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T} - #tau-jet p_{T} (GeV/c)")
    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T}/ #tau-jet p_{T} - 1")                                                                                                                                     
    p_pull.getFrame2().GetYaxis().SetTitle("Ratio")
    p_pull.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.75, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.68, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.2, 0.61, "Runs "+runRange, 17)

    p_pull.draw()

    histograms.addStandardTexts(lumi=lumi)
    p_pull.save(formats)
    """
    #########################################################################                                                                                                                               
    print "Output written in",plotDir
Esempio n. 39
0
def main():

    style = tdrstyle.TDRStyle()
    hNames = getHistoNames(kinVar)
    # hName    = hNames[0]

    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(parseOpts.batchMode)

    # Get all datasets from the mcrab dir
    datasets = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab],
                                                    analysisName=analysis)
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTB_M_")
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTB_M_")
    # datasets  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=analysis, excludeTasks="Tau_Run2015C|Tau\S+25ns_Silver$|DYJetsToLL|WJetsToLNu$")

    # Inform user of datasets retrieved
    Print("Got following datasets from multicrab dir \"%s\"" % parseOpts.mcrab)
    for d in datasets.getAllDatasets():
        print "\t", d.getName()

    # Determine Integrated Luminosity (If Data datasets present)
    intLumi = 0.0
    if len(datasets.getDataDatasets()) != 0:
        # Load Luminosity JSON file
        datasets.loadLuminosities(fname="lumi.json")

        # Load RUN range
        # runRange = datasets.loadRunRange(fname="runrange.json")

        # Calculate Integrated Luminosity
        intLumi = GetLumi(datasets)

    # For-loop: All Histogram names
    for counter, hName in enumerate(hNames):
        plotName = hName  #analysis + "_" + hName
        saveName = os.path.join(savePath, plotName)

        # Get Data or MC datasets
        # dataDatasets = datasets.getDataDatasets()
        # mcDatasets   = datasets.getMCDatasets()

        # Build ROOT histos from individual datasets
        dataset1 = datasets.getDataset(
            "ChargedHiggs_HplusTB_HplusToTB_M_400").getDatasetRootHisto(hName)
        dataset2 = datasets.getDataset(
            "ChargedHiggs_HplusTB_HplusToTB_M_300").getDatasetRootHisto(hName)
        # dataset2 = datasets.getDataset("TT_ext3").getDatasetRootHisto(hName)
        # datasets.getDataset("TT_ext3").setCrossSection(831.76)

        # Normalise datasets
        if normalizeTo == "One":
            dataset1.normalizeToOne()
            dataset2.normalizeToOne()
        elif normalizeTo == "XSection":
            dataset1.normalizeByCrossSection()
            dataset2.normalizeByCrossSection()
        elif normalizeTo == "Luminosity":
            dataset1.normalizeToLumi(intLumi)
            dataset2.normalizeToLumi(intLumi)
        else:
            isValidNorm(normalizeTo)

        # Customise histos
        histo1 = dataset1.getHistogram()
        styles.signal200Style.apply(histo1)
        # histo1.SetMarkerStyle(ROOT.kFullCircle)
        # histo1.SetFillStyle(3001)
        # histo1.SetFillColor(histo2.GetMarkerColor())
        # removeNegatives(histo1)
        # removeErrorBars(histo1)
        histo1.Rebin(rebinFactor)

        # Customise histos
        histo2 = dataset2.getHistogram()
        styles.ttStyle.apply(histo2)
        # histo2.SetMarkerStyle(ROOT.kFullCross)
        histo2.SetFillStyle(3001)
        histo2.SetFillColor(styles.ttStyle.color)
        # removeNegatives(histo2)
        # removeErrorBars(histo2)
        histo2.Rebin(rebinFactor)

        # Create a comparison plot
        p = plots.ComparisonPlot(
            histograms.Histo(histo1, "m_{H^{#pm}} = 400 GeV/c^{2}", "p", "P"),
            histograms.Histo(histo2, "m_{H^{#pm}} = 300 GeV/c^{2}", "F",
                             "HIST,E,9"))
        #                                 histograms.Histo(histo2, "t#bar{t}", "F", "HIST,E,9"))

        # Create a comparison plot (One histogram is treated as a reference histogram, and all other histograms are compared with respect to that)
        # p = plots.ComparisonManyPlot(histograms.Histo(histo1, "m_{H^{#pm}} = 200 GeV/c^{2}", "p", "P"),
        #                             [histograms.Histo(histo2, "m_{H^{#pm}} = 300 GeV/c^{2}", "F", "HIST9"),
        #                              histograms.Histo(histo3, "t#bar{t}", "F", "HIST9")])

        # Customise plots
        opts = {
            "ymin": 0.0,
            "binWidthX": histo1.GetXaxis().GetBinWidth(0),
            "xUnits": getUnitsX(kinVar)
        }
        ratioOpts = {
            "ymin": 0.0,
            "ymax": 2.0,
            "binWidthX": histo1.GetXaxis().GetBinWidth(0),
            "xUnits": getUnitsX(kinVar)
        }
        p.createFrame(os.path.join(savePath, plotName),
                      createRatio=ratio,
                      opts=opts,
                      opts2=ratioOpts)

        # Customise Legend
        moveLegend = {"dx": -0.1, "dy": +0.0, "dh": -0.2}
        p.setLegend(
            histograms.moveLegend(histograms.createLegend(), **moveLegend))

        # Customise text
        if intLumi > 0.0:
            histograms.addStandardTexts(lumi=intLumi)
        else:
            histograms.addStandardTexts()
        # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
        # histograms.addText(0.4, 0.11, "Runs " + datasets.loadRunRange(), 17)

        # Customise frame
        p.setEnergy("13")
        p.getFrame().GetYaxis().SetTitle(getTitleY(normalizeTo, kinVar, opts))
        p.getFrame().GetXaxis().SetTitle(getTitleX(kinVar, opts))
        if ratio:
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)

        #  Draw plots
        p.draw()

        # Save canvas under custom dir
        if counter == 0:
            Print("Saving plots in %s format(s)" % (len(saveFormats)))
        SavePlotterCanvas(p, savePath, saveName, saveFormats)

    return
Esempio n. 40
0
def main():
    if len(sys.argv) < 2:
        usage()
    analysis = _analysis
    if "--QCD" in sys.argv:
        analysis = "QCDMeasurement"

    dirs = []
    dirs.append(sys.argv[1])
    dsetMgr = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra, searchMode=searchMode, analysisName=analysis, optimizationMode=optMode) 
    dsetMgr.loadLuminosities()
    dsetMgr.updateNAllEventsToPUWeighted()
    plots.mergeRenameReorderForDataMC(dsetMgr)
    #dsetMgr.normalizeToLuminosity()
    lumi = dsetMgr.getDataset("Data").getLuminosity()

    # Apply TDR style
    style = tdrstyle.TDRStyle()

    # Format: list of [denominator, numerator] pairs
    plotSources = OrderedDict()
    plotSources["trg_vs_vtx"] = ["PUDependency/NvtxTrg","PUDependency/NvtxVtx"]
    plotSources["vtx_vs_antiIsolTau"] = ["PUDependency/NvtxVtx","PUDependency/NvtxAntiIsolatedTau"]
    plotSources["vtx_vs_tau"] = ["PUDependency/NvtxVtx","PUDependency/NvtxTau"]
    if not "--QCD" in sys.argv:
        plotSources["tau_vs_eveto"] = ["PUDependency/NvtxTau","PUDependency/NvtxElectronVeto"]
    else:
        plotSources["tau_vs_eveto"] = ["PUDependency/NvtxAntiIsolatedTau","PUDependency/NvtxElectronVeto"]
    plotSources["eveto_vs_muveto"] = ["PUDependency/NvtxElectronVeto","PUDependency/NvtxMuonVeto"]
    plotSources["jet_vs_muveto"] = ["PUDependency/NvtxMuonVeto","PUDependency/NvtxJetSelection"]
    plotSources["rcoll_vs_jet"] = ["PUDependency/NvtxJetSelection","PUDependency/NvtxAngularCutsCollinear"]
    plotSources["btag_vs_rcoll"] = ["PUDependency/NvtxAngularCutsCollinear","PUDependency/NvtxBtagging"]
    plotSources["met_vs_btag"] = ["PUDependency/NvtxBtagging","PUDependency/NvtxMETSelection"]
    plotSources["rbb_vs_met"] = ["PUDependency/NvtxMETSelection","PUDependency/NvtxAngularCutsBackToBack"]
    plotSources["allsel_vs_rbb"] = ["PUDependency/NvtxAngularCutsBackToBack","PUDependency/NvtxAllSelections"]
    if not "--QCD" in sys.argv:
        plotSources["propbtag_vs_btag"] = ["PUDependency/NvtxBtagging","PUDependency/NvtxAllSelectionsWithProbabilisticBtag"]
    plotSources["allsel_vs_trg"] = ["PUDependency/NvtxTrg","PUDependency/NvtxAllSelections"]
    #plotSources["tau_isol_pt"] = ["tauSelection_/IsolPtBefore","tauSelection_/IsolPtAfter"]
    #plotSources["tau_isol_eta"] = ["tauSelection_/IsolEtaBefore","tauSelection_/IsolEtaAfter"]
    #plotSources["tau_isol_vtx"] = ["tauSelection_/IsolVtxBefore","tauSelection_/IsolVtxAfter"]
    #plotSources["e_isol_pt"] = ["eSelection_Veto/IsolPtBefore","eSelection_Veto/IsolPtAfter"]
    #plotSources["e_isol_eta"] = ["eSelection_Veto/IsolEtaBefore","eSelection_Veto/IsolEtaAfter"]
    #plotSources["e_isol_vtx"] = ["eSelection_Veto/IsolVtxBefore","eSelection_Veto/IsolVtxAfter"]
    #plotSources["mu_isol_pt"] = ["muSelection_Veto/IsolPtBefore","muSelection_Veto/IsolPtAfter"]
    #plotSources["mu_isol_eta"] = ["muSelection_Veto/IsolEtaBefore","muSelection_Veto/IsolEtaAfter"]
    #plotSources["mu_isol_vtx"] = ["muSelection_Veto/IsolVtxBefore","muSelection_Veto/IsolVtxAfter"]
    dsetInputs = {
        #"TTJets": ["TTJets"], # Madgraph with negative weights
        "TT": ["TT"], # Powheg, no neg. weights -> large stats.
        "TTJets": ["TTJets"],
        "WJets": ["WJetsHT"],
        "EWK": ["TTJets", "WJetsHT", "DYJetsToLL", "SingleTop"],
        "QCD": ["QCD"],
        "Data": ["Data"],
    }
    summarySources = ["vtx_vs_antiIsolTau",
                      "vtx_vs_tau",
                      "tau_vs_eveto",
                      "eveto_vs_muveto",
                      "jet_vs_muveto",
                      "btag_vs_rcoll",
                      "met_vs_btag",
                      "allsel_vs_trg"]

    # Create plots (MC vs. MC)
    doPlots(dsetMgr, lumi, plotSources, dsetInputs, summarySources)
    # Create plots (data vs. MC)
    doPlots(dsetMgr, lumi, plotSources, dsetInputs, summarySources, "Data")
def main(argv):

#    HISTONAME = "TauIdJets"
#    HISTONAME = "TauIdJetsCollinear"
#    HISTONAME = "TauIdBtag"
#    HISTONAME = "TauIdBvetoCollinear"
#    HISTONAME = "TauIdBveto"
    HISTONAME = "TauIdAfterCollinearCuts"

    FAKEHISTO = "OnlyEWKFakeTaus"
    GENUINEHISTO = "PlusFilteredEWKFakeTaus"

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

    dirs.append(sys.argv[1])

    
    # Create all datasets from a multicrab task

    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra,  searchMode=searchMode, analysisName="signalAnalysisInvertedTau",optimizationMode="") #no collinear
    #datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra,  searchMode=searchMode, analysisName="signalAnalysisInvertedTau",optimizationMode="OptQCDTailKillerLoosePlus") #collinear


    # Check multicrab consistency
    consistencyCheck.checkConsistencyStandalone(dirs[0],datasets,name="QCD inverted")
   
    # As we use weighted counters for MC normalisation, we have to
    # update the all event count to a separately defined value because
    # the analysis job uses skimmed pattuple as an input
    datasets.updateNAllEventsToPUWeighted()

    # Read integrated luminosities of data datasets from lumi.json
    datasets.loadLuminosities()

    # Include only 120 mass bin of HW and HH datasets
    datasets.remove(filter(lambda name: "TTToHplus" in name and not "M120" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_t-channel" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_tW-channel" in name, datasets.getAllDatasetNames()))
    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()))
    # Default merging nad ordering of data and MC datasets
    # All data datasets to "Data"
    # All QCD datasets to "QCD"
    # All single top datasets to "SingleTop"
    # WW, WZ, ZZ to "Diboson"
    plots.mergeRenameReorderForDataMC(datasets)

    # Set BR(t->H) to 0.05, keep BR(H->tau) in 1
    xsect.setHplusCrossSectionsToBR(datasets, br_tH=0.05, br_Htaunu=1)

    # Merge WH and HH datasets to one (for each mass bin)
    # TTToHplusBWB_MXXX and TTToHplusBHminusB_MXXX to "TTToHplus_MXXX"
    plots.mergeWHandHH(datasets)

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

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)

    invertedQCD = InvertedTauID(False)
    invertedQCD.setLumi(datasets.getDataset("Data").getLuminosity())
    invertedQCD.setInfo([dataEra,searchMode,HISTONAME])

    invertedQCD_separatedFakes = InvertedTauID(True)
    invertedQCD_separatedFakes.setLumi(datasets.getDataset("Data").getLuminosity())
    invertedQCD_separatedFakes.setInfo([dataEra,searchMode,HISTONAME])
    
    histonames = datasets.getDataset("Data").getDirectoryContent("baseline/METBaseline"+HISTONAME)
    bins = []
    binLabels = []
    for histoname in histonames:
        bins.append(histoname.replace("METBaseline"+HISTONAME,""))
        title = datasets.getDataset("Data").getDatasetRootHisto("baseline/METBaseline"+HISTONAME+"/"+histoname).getHistogram().GetTitle()
        title = title.replace("METBaseline"+HISTONAME,"")
        title = title.replace("#tau p_{T}","taup_T")
        title = title.replace("#tau eta","taueta")
        title = title.replace("<","lt")
        title = title.replace(">","gt")
        title = title.replace("=","eq")
        title = title.replace("..","to")
        title = title.replace(".","p")
        title = title.replace("/","_")
        binLabels.append(title)
    #binLabels = bins # for this data set
    print
    print "Histogram bins available",bins
    
    #    bins = ["Inclusive"]
    #    bins = ["taup_Tleq50","taup_Teq50to60"]
    print "Using bins              ",bins
    print
    print "Bin labels"
    for i in range(len(binLabels)):
        line = bins[i]
        while len(line) < 10:
            line += " "
        line += ": "+binLabels[i]
        print line
    print

    for i,bin in enumerate(bins):
        invertedQCD.setLabel(binLabels[i])
        invertedQCD_separatedFakes.setLabel(binLabels[i])

        metBase = plots.DataMCPlot(datasets, "baseline/METBaseline"+HISTONAME+"/METBaseline"+HISTONAME+bin)
        metInver = plots.DataMCPlot(datasets, "Inverted/METInverted"+HISTONAME+"/METInverted"+HISTONAME+bin)

        #metBase_EWK = plots.DataMCPlot(datasets, "baseline/METBaseline"+HISTONAME+"/METBaseline"+HISTONAME+bin)
        #metInver_EWK = plots.DataMCPlot(datasets, "Inverted/METInverted"+HISTONAME+"/METInverted"+HISTONAME+bin)
        metBase_GenuineTaus = plots.DataMCPlot(datasets, "baseline/METBaseline"+HISTONAME+GENUINEHISTO+"/METBaseline"+HISTONAME+GENUINEHISTO+bin)
        metInver_GenuineTaus = plots.DataMCPlot(datasets, "Inverted/METInverted"+HISTONAME+GENUINEHISTO+"/METInverted"+HISTONAME+GENUINEHISTO+bin)
        metBase_FakeTaus = plots.DataMCPlot(datasets, "baseline/METBaseline"+HISTONAME+FAKEHISTO+"/METBaseline"+HISTONAME+FAKEHISTO+bin)
        metInver_FakeTaus = plots.DataMCPlot(datasets, "Inverted/METInverted"+HISTONAME+FAKEHISTO+"/METInverted"+HISTONAME+FAKEHISTO+bin)


        # Rebin before subtracting
        
        metBase.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) #5
        metInver.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) #5
        #metBase_EWK.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) #5         
        #metInver_EWK.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) #5
        metBase_GenuineTaus.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) #5
        metInver_GenuineTaus.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) #5
        metBase_FakeTaus.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) #5
        metInver_FakeTaus.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) #5
        
        metInverted_data = metInver.histoMgr.getHisto("Data").getRootHisto().Clone("Inverted/METInverted"+HISTONAME+"/METInverted"+HISTONAME+bin)
        metBase_data = metBase.histoMgr.getHisto("Data").getRootHisto().Clone("baseline/METBaseLine"+HISTONAME+"/METBaseline"+HISTONAME+bin)

        metInverted_EWK = metInver.histoMgr.getHisto("EWK").getRootHisto().Clone("Inverted/METInverted"+HISTONAME+"/METInverted"+HISTONAME+bin)
        metBase_EWK = metBase.histoMgr.getHisto("EWK").getRootHisto().Clone("baseline/METBaseLine"+HISTONAME+"/METBaseline"+HISTONAME+bin)

        metInverted_EWK_GenuineTaus = metInver_GenuineTaus.histoMgr.getHisto("EWK").getRootHisto().Clone("Inverted/METInverted"+HISTONAME+GENUINEHISTO+"/METInverted"+HISTONAME+GENUINEHISTO+bin)
        metBase_EWK_GenuineTaus = metBase_GenuineTaus.histoMgr.getHisto("EWK").getRootHisto().Clone("baseline/METBaseLine"+HISTONAME+GENUINEHISTO+"/METBaseline"+HISTONAME+GENUINEHISTO+bin)

        metInverted_EWK_FakeTaus = metInver_FakeTaus.histoMgr.getHisto("EWK").getRootHisto().Clone("Inverted/METInverted"+HISTONAME+FAKEHISTO+"/METInverted"+HISTONAME+FAKEHISTO+bin)
        metBase_EWK_FakeTaus = metBase_FakeTaus.histoMgr.getHisto("EWK").getRootHisto().Clone("baseline/METBaseLine"+HISTONAME+FAKEHISTO+"/METBaseline"+HISTONAME+FAKEHISTO+bin)

        metBase_QCD = metBase_data.Clone("QCD")
        metBase_QCD.Add(metBase_EWK,-1)
        metInverted_QCD = metInverted_data.Clone("QCD")
        metInverted_QCD.Add(metInverted_EWK,-1)

        metBase_QCD_separatedFakes = metBase_data.Clone("QCD")
        metBase_QCD_separatedFakes.Add(metBase_EWK_GenuineTaus,-1)
        metBase_QCD_separatedFakes.Add(metBase_EWK_FakeTaus,-1)
        metInverted_QCD_separatedFakes = metInverted_data.Clone("QCD")
        metInverted_QCD_separatedFakes.Add(metInverted_EWK_GenuineTaus,-1)
        metInverted_QCD_separatedFakes.Add(metInverted_EWK_FakeTaus,-1)

        metInverted_data = addlabels(metInverted_data)
        metInverted_EWK  = addlabels(metInverted_EWK)
        metInverted_EWK_GenuineTaus  = addlabels(metInverted_EWK_GenuineTaus)
        metInverted_EWK_FakeTaus  = addlabels(metInverted_EWK_FakeTaus)
        
        metBase_data     = addlabels(metBase_data)
        metBase_EWK  = addlabels(metBase_EWK)
        metBase_EWK_GenuineTaus = addlabels(metBase_EWK_GenuineTaus)
        metBase_EWK_FakeTaus = addlabels(metBase_EWK_FakeTaus)

        metInverted_QCD  = addlabels(metInverted_QCD)
        metInverted_QCD_separatedFakes  = addlabels(metInverted_QCD_separatedFakes)
        
        #non-separated

        invertedQCD.plotHisto(metInverted_data,"inverted")
        invertedQCD.plotHisto(metInverted_EWK,"invertedEWK")
        invertedQCD.plotHisto(metBase_data,"baseline")
        invertedQCD.plotHisto(metBase_EWK,"baselineEWK")

        fitOptions = "LRB"

        invertedQCD.fitEWK(metInverted_EWK,fitOptions)
        invertedQCD.fitEWK(metBase_EWK,fitOptions)
        invertedQCD.fitQCD(metInverted_QCD,fitOptions)
        invertedQCD.fitData(metBase_data)

        invertedQCD.getNormalization()

        #separated

        invertedQCD_separatedFakes.plotHisto(metInverted_data,"inverted")
        invertedQCD_separatedFakes.plotHisto(metInverted_EWK_GenuineTaus,"invertedEWKGenuineTaus")
        invertedQCD_separatedFakes.plotHisto(metInverted_EWK_FakeTaus,"invertedEWKFakeTaus")
        
        invertedQCD_separatedFakes.plotHisto(metBase_data,"baseline")
        invertedQCD_separatedFakes.plotHisto(metBase_EWK_GenuineTaus,"baselineEWKGenuineTaus")
        invertedQCD_separatedFakes.plotHisto(metBase_EWK_FakeTaus,"baselineEWKFakeTaus")
        
        invertedQCD_separatedFakes.fitEWK_GenuineTaus(metInverted_EWK_GenuineTaus,fitOptions) 
        invertedQCD_separatedFakes.fitEWK_GenuineTaus(metBase_EWK_GenuineTaus,fitOptions)

        invertedQCD_separatedFakes.fitEWK_FakeTaus(metInverted_EWK_FakeTaus,fitOptions)
        invertedQCD_separatedFakes.fitEWK_FakeTaus(metBase_EWK_FakeTaus,fitOptions)

        invertedQCD_separatedFakes.fitQCD(metInverted_QCD_separatedFakes,fitOptions)
        invertedQCD_separatedFakes.fitData(metBase_data)
        
        invertedQCD_separatedFakes.getNormalization()
        
    invertedQCD.Summary()
    invertedQCD.WriteNormalizationToFile("QCDInvertedNormalizationFactors.py")
    invertedQCD.WriteLatexOutput("fits.tex")
    
    invertedQCD_separatedFakes.Summary()
    invertedQCD_separatedFakes.WriteNormalizationToFile("QCDInvertedNormalizationFactorsSeparatedFakeTaus.py")
    invertedQCD_separatedFakes.WriteLatexOutput("fits_separatedfaketaus.tex")

    mergeNormalizations("QCDInvertedNormalizationFactors.py","QCDInvertedNormalizationFactorsSeparatedFakeTaus.py")
Esempio n. 42
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()
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()
Esempio n. 44
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()
def main(argv):
    COMBINEDHISTODIR = "ForQCDNormalization"
    FAKEHISTODIR = "ForQCDNormalizationEWKFakeTaus"
    GENUINEHISTODIR = "ForQCDNormalizationEWKGenuineTaus"
    comparisonList = ["AfterStdSelections"]

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

    dirs.append(sys.argv[1])
    
    # Create all dsetMgr from a multicrab task
    dsetMgr = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra,  searchMode=searchMode, analysisName=analysis)

    #print dsetMgr
    # Check multicrab consistency
    consistencyCheck.checkConsistencyStandalone(dirs[0],dsetMgr,name="QCD inverted")
   
    # As we use weighted counters for MC normalisation, we have to
    # update the all event count to a separately defined value because
    # the analysis job uses skimmed pattuple as an input
    dsetMgr.updateNAllEventsToPUWeighted()

    # Read integrated luminosities of data dsetMgr from lumi.json
    dsetMgr.loadLuminosities()

    # Include only 120 mass bin of HW and HH dsetMgr
    dsetMgr.remove(filter(lambda name: "TTToHplus" in name and not "M120" in name, dsetMgr.getAllDatasetNames()))
    dsetMgr.remove(filter(lambda name: "HplusTB" in name, dsetMgr.getAllDatasetNames()))
    # Default merging nad ordering of data and MC dsetMgr
    # All data dsetMgr to "Data"
    # All QCD dsetMgr to "QCD"
    # All single top dsetMgr to "SingleTop"
    # WW, WZ, ZZ to "Diboson"
    plots.mergeRenameReorderForDataMC(dsetMgr)

    # Set BR(t->H) to 0.05, keep BR(H->tau) in 1
    xsect.setHplusCrossSectionsToBR(dsetMgr, br_tH=0.05, br_Htaunu=1)

    # Merge WH and HH dsetMgr to one (for each mass bin)
    plots.mergeWHandHH(dsetMgr)

    dsetMgr.merge("EWK", [
	    "TTJets",
            "WJetsHT",
            "DYJetsToLL",
            "SingleTop",
            #"Diboson"
            ])

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)

    for HISTONAME in comparisonList:
        BASELINETAUHISTONAME = "NormalizationMETBaselineTau"+HISTONAME+"/NormalizationMETBaselineTau"+HISTONAME
        INVERTEDTAUHISTONAME = "NormalizationMETInvertedTau"+HISTONAME+"/NormalizationMETInvertedTau"+HISTONAME
      
        #===== Infer binning information and labels
        histonames = dsetMgr.getDataset("Data").getDirectoryContent(COMBINEDHISTODIR+"/NormalizationMETBaselineTau"+HISTONAME)
        bins = []
        binLabels = []
        if histonames == None:
            # Assume that only inclusive bin exists
            name = COMBINEDHISTODIR+"/NormalizationMETBaselineTau"+HISTONAME
            if not dsetMgr.getDataset("Data").hasRootHisto(name):
                raise Exception("Error: Cannot find histogram or directory of name '%s'!"%name)
            BASELINETAUHISTONAME = "NormalizationMETBaselineTau"+HISTONAME
            INVERTEDTAUHISTONAME = "NormalizationMETInvertedTau"+HISTONAME
            bins = [""]
            binLabels = ["Inclusive"]
        else:
            for hname in histonames:
                bins.append(hname.replace("NormalizationMETBaselineTau"+HISTONAME,""))
                title = dsetMgr.getDataset("Data").getDatasetRootHisto(COMBINEDHISTODIR+"/"+BASELINETAUHISTONAME+"/"+hname).getHistogram().GetTitle()
                title = title.replace("METBaseline"+HISTONAME,"")
                binLabels.append(formatHistoTitle(title))
        
        print "\nHistogram bins available",bins
        print "Using bins              ",bins
        print "\nBin labels"
        for i in range(len(binLabels)):
            line = bins[i]
            while len(line) < 10:
                line += " "
            line += ": "+binLabels[i]
            print line
        print
        
        #===== Initialize normalization calculator
        invertedQCD = InvertedTauID()
        invertedQCD.setLumi(dsetMgr.getDataset("Data").getLuminosity())
        invertedQCD.setInfo([dataEra,searchMode,HISTONAME])
        
        #===== Loop over tau pT bins
        for i,binStr in enumerate(bins):
            print "\n********************************"
            print "*** Fitting bin %s"%binLabels[i]
            print "********************************\n"
            invertedQCD.resetBinResults()
            invertedQCD.setLabel(binLabels[i])

            #===== Obtain histograms for normalization
            metBase = plots.DataMCPlot(dsetMgr, COMBINEDHISTODIR+"/"+BASELINETAUHISTONAME+binStr)
            metInver = plots.DataMCPlot(dsetMgr, COMBINEDHISTODIR+"/"+INVERTEDTAUHISTONAME+binStr)
            metBase_GenuineTaus = plots.DataMCPlot(dsetMgr, GENUINEHISTODIR+"/"+BASELINETAUHISTONAME+binStr)
            metInver_GenuineTaus = plots.DataMCPlot(dsetMgr, GENUINEHISTODIR+"/"+INVERTEDTAUHISTONAME+binStr)
            metBase_FakeTaus = plots.DataMCPlot(dsetMgr, FAKEHISTODIR+"/"+BASELINETAUHISTONAME+binStr)
            metInver_FakeTaus = plots.DataMCPlot(dsetMgr, FAKEHISTODIR+"/"+INVERTEDTAUHISTONAME+binStr)

            #===== Rebin histograms before subtracting
            RebinFactor = 2 # Aim for 10 GeV binning
            metBase.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(RebinFactor))
            metInver.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(RebinFactor))
            metBase_GenuineTaus.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(RebinFactor))
            metInver_GenuineTaus.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(RebinFactor))
            metBase_FakeTaus.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(RebinFactor))
            metInver_FakeTaus.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(RebinFactor))
            
            #===== Obtain templates for data and EWK
            metInverted_data = metInver.histoMgr.getHisto("Data").getRootHisto().Clone(COMBINEDHISTODIR+"/"+INVERTEDTAUHISTONAME+binStr)
            treatHistogram(metInverted_data, "Data, inverted")
            metInverted_EWK_GenuineTaus = metInver_GenuineTaus.histoMgr.getHisto("EWK").getRootHisto().Clone(GENUINEHISTODIR+"/"+INVERTEDTAUHISTONAME+binStr)
            treatHistogram(metInverted_EWK_GenuineTaus, "EWK genuine taus, inverted")
            metInverted_EWK_FakeTaus = metInver_FakeTaus.histoMgr.getHisto("EWK").getRootHisto().Clone(FAKEHISTODIR+"/"+INVERTEDTAUHISTONAME+binStr)
            treatHistogram(metInverted_EWK_FakeTaus, "EWK fake taus, inverted")
            
            metBase_data = metBase.histoMgr.getHisto("Data").getRootHisto().Clone(COMBINEDHISTODIR+"/"+BASELINETAUHISTONAME+binStr)
            treatHistogram(metBase_data, "Data, baseline")
            metBase_EWK_GenuineTaus = metBase_GenuineTaus.histoMgr.getHisto("EWK").getRootHisto().Clone(GENUINEHISTODIR+"/"+BASELINETAUHISTONAME+binStr)
            treatHistogram(metBase_EWK_GenuineTaus, "EWK genuine taus, baseline")
            metBase_EWK_FakeTaus = metBase_FakeTaus.histoMgr.getHisto("EWK").getRootHisto().Clone(FAKEHISTODIR+"/"+BASELINETAUHISTONAME+binStr)
            treatHistogram(metBase_EWK_FakeTaus, "EWK fake taus, baseline")

            #===== Obtain templates for QCD (subtract MC EWK events from data)
            # QCD from baseline is usable only as a cross check
            #metBase_QCD = metBase_data.Clone("QCD")
            #metBase_QCD.Add(metBase_EWK_GenuineTaus,-1)
            #metBase_QCD.Add(metBase_EWK_FakeTaus,-1)
            #addLabels(metBase_QCD, "QCD, baseline")
            
            metInverted_QCD = metInverted_data.Clone("QCD")
            metInverted_QCD.Add(metInverted_EWK_GenuineTaus,-1)
            metInverted_QCD.Add(metInverted_EWK_FakeTaus,-1)
            treatHistogram(metInverted_QCD, "QCD, inverted")
            
            #===== Make plots of templates
            print "\n*** Integrals of plotted templates"
            #invertedQCD.plotHisto(metInverted_data,"template_Data_Inverted")
            #invertedQCD.plotHisto(metInverted_EWK_GenuineTaus,"template_EWKGenuineTaus_Inverted")
            #invertedQCD.plotHisto(metInverted_EWK_FakeTaus,"template_EWKFakeTaus_Inverted")
            invertedQCD.plotHisto(metInverted_QCD,"template_QCD_Inverted")
            invertedQCD.plotHisto(metBase_data,"template_Data_Baseline")
            invertedQCD.plotHisto(metBase_EWK_GenuineTaus,"template_EWKGenuineTaus_Baseline")
            invertedQCD.plotHisto(metBase_EWK_FakeTaus,"template_EWKFakeTaus_Baseline")
            #invertedQCD.plotHisto(metBase_QCD,"template_QCD_Baseline")
            
            #===== Fit individual templates and
            # Fit first templates for QCD, EWK_genuine_taus, and EWK_fake_taus
            # Then fit the shape of those parametrizations to baseline data to obtain normalization coefficients
            fitOptions = "RB"
            
            # Strategy: take EWK templates from baseline and QCD template from inverted; then fit to baseline data
            invertedQCD.fitEWK_GenuineTaus(metInverted_EWK_GenuineTaus,fitOptions)
            invertedQCD.fitEWK_GenuineTaus(metBase_EWK_GenuineTaus,fitOptions)
            invertedQCD.fitEWK_FakeTaus(metInverted_EWK_FakeTaus,fitOptions)
            invertedQCD.fitEWK_FakeTaus(metBase_EWK_FakeTaus,fitOptions)
            invertedQCD.fitQCD(metInverted_QCD,fitOptions)
            invertedQCD.fitData(metBase_data)

            #===== Calculate normalization
            invertedQCD.getNormalization()
            
        invertedQCD.Summary()
        invertedQCD.WriteNormalizationToFile("QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.py")
        invertedQCD.WriteLatexOutput("fits.tex")
def analyze(analysis=None):

    paths = [sys.argv[1]]

    if not analysis == None:
#        datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis, includeOnlyTasks="Tau|TT")
        datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis)
    else:
        datasets = dataset.getDatasetsFromMulticrabDirs(paths)
        analysis = datasets.getAllDatasets()[0].getAnalysisName()

    analysisList = []
#    analysisList.append("METLeg_2016MET80_MET80")
#    analysisList.append("METLeg_2016ICHEP_MET90")
#    analysisList.append("METLeg_2016_MET90")

    met_re = re.compile("_(?P<met>MET\d+)$")
    if True:
    #for analysis in analysisList:

        met = "METX"
        met_match = met_re.search(analysis)
        if met_match:
            met = met_match.group("met")

        print "Analysis",analysis
        datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis)
#    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="Tau\S+25ns$|TTJets$")
#        datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="Tau_Run2015C|Tau\S+25ns_Silver$|DYJetsToLL|WJetsToLNu$")
#    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="Tau_Run2015D_PromptReco_v4_246908_260426_25ns$|DYJetsToLL_M_50$")
#    datasetsMC = None
        datasetsMC = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="Tau_")

        for d in datasets.getAllDatasets():
            print d.getName()
            style = tdrstyle.TDRStyle()

            dataset1 = datasets.getDataDatasets()
            dataset2 = dataset1
            createRatio = False
            if isinstance(datasetsMC,dataset.DatasetManager):
                dataset2 = datasetsMC.getMCDatasets()
                createRatio = True

        eff1_MET80 = getEfficiency(dataset1)
        eff2_MET80 = getEfficiency(dataset2)

        styles.dataStyle.apply(eff1_MET80)
        eff1_MET80.SetMarkerSize(1)
        if createRatio:
            styles.mcStyle.apply(eff2_MET80)
            eff2_MET80.SetMarkerSize(1.5)

        if createRatio:
            p = plots.ComparisonPlot(histograms.HistoGraph(eff1_MET80, "eff1_MET80", "p", "P"),
                                     histograms.HistoGraph(eff2_MET80, "eff2_MET80", "p", "P"))
        else:
            p = plots.PlotBase([histograms.HistoGraph(eff1_MET80, "eff1_MET80", "p", "P")])


        #from plotTauLegEfficiency import fit
        #fit("Data",p,eff1_MET80,20,300)
        #fit("MC",p,eff2_MET80,20,300)

        opts = {"ymin": 0, "ymax": 1.1}
        opts2 = {"ymin": 0.5, "ymax": 1.5}
        moveLegend = {"dx": -0.55, "dy": -0.15}

        name = "TauMET_"+analysis+"_DataVsMC_PFMET"

        legend1 = "Data"
        #    legend2 = "MC (TTJets)"
        #legend2 = "MC"
        legend2 = "Simulation"

        if createRatio:
            p.histoMgr.setHistoLegendLabelMany({"eff1_MET80": legend1, "eff2_MET80": legend2})
            p.createFrame(os.path.join(plotDir, name), createRatio=True, opts=opts, opts2=opts2)
        else:
            p.histoMgr.setHistoLegendLabelMany({"eff1_MET80": legend1})
            p.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

        p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

        p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
        p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
        if createRatio:
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)

#        histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1_"+met, 17)
        histograms.addText(0.4, 0.87, "LooseIsoPFTau50_Trk30_eta2p1_"+met, 17)
        #    histograms.addText(0.2, 0.53, analysis.split("_")[len(analysis.split("_")) -1], 17)
#        label = analysis.split("_")[1]
#        label = "2016"
        label = string.replace(analysis,"METLeg_","")
        histograms.addText(0.2, 0.53, label, 17)
        runRange = datasets.loadRunRange()
        histograms.addText(0.2, 0.46, "Runs "+runRange, 17)

        p.draw()
        lumi = 0.0
        for d in datasets.getDataDatasets():
            print "luminosity",d.getName(),d.getLuminosity()
            lumi += d.getLuminosity()
        print "luminosity, sum",lumi
        histograms.addStandardTexts(lumi=lumi)

        if not os.path.exists(plotDir):
            os.mkdir(plotDir)
        p.save(formats)

        pythonWriter.addParameters(plotDir,label,runRange,lumi,eff1_MET80)
        pythonWriter.addMCParameters(label,eff2_MET80)

    pythonWriter.writeJSON(os.path.join(plotDir,"metLegTriggerEfficiency_"+label+".json"))

    #########################################################################                                             

    eff1phi = getEfficiency(dataset1,"NumeratorPhi","DenominatorPhi")
    eff2phi = getEfficiency(dataset2,"NumeratorPhi","DenominatorPhi")

    styles.dataStyle.apply(eff1phi)
    styles.mcStyle.apply(eff2phi)
    eff1phi.SetMarkerSize(1)

    if isinstance(datasetsMC,dataset.DatasetManager):
        p_phi = plots.ComparisonPlot(histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"),
                                     histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"))
    else:
        p_phi = plots.PlotBase([histograms.HistoGraph(eff1phi, "eff1phi", "p", "P")])

    p_phi.histoMgr.setHistoLegendLabelMany({"eff1phi": legend1})
    if isinstance(datasetsMC,dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({"eff1phi": legend1, "eff2phi": legend2})

    opts = {"ymin": 0, "ymax": 0.3}
    name = "TauMET_"+analysis+"_DataVsMC_PFMETPhi"

    if createRatio:
        p_phi.createFrame(os.path.join(plotDir, name), createRatio=createRatio, opts=opts, opts2=opts2)
    else:
        p_phi.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

#    moveLegendPhi = {"dx": -0.5, "dy": -0.6, "dh": -0.1}
    moveLegendPhi = {"dx": -0.55, "dy": -0.15, "dh": -0.1}
    p_phi.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegendPhi))

    p_phi.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p_phi.getFrame().GetXaxis().SetTitle("MET Type 1 #phi")
    if createRatio:
        p_phi.getFrame2().GetYaxis().SetTitle("Ratio")
        p_phi.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.4, 0.86, "LooseIsoPFTau50_Trk30_eta2p1_"+met, 17)
#   histograms.addText(0.4, 0.78, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.4, 0.78, "2016", 17)
    histograms.addText(0.4, 0.71, "Runs "+datasets.loadRunRange(), 17)

    p_phi.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_phi.save(formats)

    #########################################################################                                             
    """
    #### MET120

    analysis = "METLeg_2015CD_MET120"
    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis)
    for d in datasets.getAllDatasets():
        print d.getName()
    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
    dataset2 = datasets.getMCDatasets()

    eff1_MET120 = getEfficiency(dataset1)
    eff2_MET120 = getEfficiency(dataset2)

    styles.dataStyle.apply(eff1_MET120)
    styles.mcStyle.apply(eff2_MET120)
    eff1_MET120.SetMarkerSize(1)
    eff2_MET120.SetMarkerSize(1.5)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff1_MET120, "eff1_MET120", "p", "P"),
                             histograms.HistoGraph(eff2_MET120, "eff2_MET120", "p", "P"))

    opts = {"ymin": 0, "ymax": 1.1}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    moveLegend = {"dx": -0.55, "dy": -0.15}

    name = "DataVsMC_L1HLTMET_PFMET_MET120"

    legend1 = "Data"
    legend2 = "MC"
    p.histoMgr.setHistoLegendLabelMany({"eff1_MET120": legend1, "eff2_MET120": legend2})

    p.createFrame(os.path.join(plotDir, name), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    lumi = 0.0
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
    """

    # CaloMET
    """
    #### MET80

    analysisc = "METLeg_2016B_CaloMET_MET80"

    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc)
    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc,excludeTasks="Tau\S+25ns_Silver$")
#    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc,includeOnlyTasks="Tau\S+25ns$|TTJets$")

    style = tdrstyle.TDRStyle()

    dataset1c = datasetsc.getDataDatasets()
    dataset2c = datasetsc.getMCDatasets()

#    eff1c_MET80 = getEfficiency(dataset1c)
    eff2c_MET80 = getEfficiency(dataset2c)

#    styles.dataStyle.apply(eff1c_MET80)
    styles.mcStyle.apply(eff2c_MET80)
#    eff1c_MET80.SetMarkerSize(1)
    eff2c_MET80.SetMarkerSize(1.5)
    eff2c_MET80.SetMarkerColor(4)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff2_MET80, "eff2_MET80", "p", "P"),
                             histograms.HistoGraph(eff2c_MET80, "eff2c_MET80", "p", "P"))

    namec = "TauMET_"+analysis+"_MC_TrgBitVsCaloMET80_PFMET"

    legend1c = "MC, trigger bit"
    legend2c = "MC, CaloMET > 80"
    p.histoMgr.setHistoLegendLabelMany({"eff2_MET80": legend1c, "eff2c_MET80": legend2c})

    p.createFrame(os.path.join(plotDir, namec), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    lumi = 0.0
    for d in datasets.getDataDatasets():
        print "luminosity",d.getName(),d.getLuminosity()
        lumi += d.getLuminosity()
    print "luminosity, sum",lumi
    histograms.addStandardTexts(lumi=lumi)
    histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1_MET80", 17)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
    """
    """
    #### MET120 

    analysisc = "METLeg_2015A_CaloMET_MET120"
    datasetsc = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysisc)

    style = tdrstyle.TDRStyle()

    dataset1c = datasetsc.getDataDatasets()
    dataset2c = datasetsc.getMCDatasets()

    eff1c_MET120 = getEfficiency(dataset1c)
    eff2c_MET120 = getEfficiency(dataset2c)

    styles.dataStyle.apply(eff1c_MET120)
    styles.mcStyle.apply(eff1c_MET120)
    eff1c_MET120.SetMarkerSize(1)
    eff2c_MET120.SetMarkerSize(1.5)

    p = plots.ComparisonPlot(histograms.HistoGraph(eff2_MET120, "eff2_MET120", "p", "P"),
                             histograms.HistoGraph(eff2c_MET120, "eff2c_MET120", "p", "P"))

    namec = "MC_TrgBitVsCaloMET120_L1HLTMET_PFMET"

    legend1c = "MC, trigger bit"
    legend2c = "MC, CaloMET > 120"
    p.histoMgr.setHistoLegendLabelMany({"eff2_MET120": legend1c, "eff2c_MET120": legend2c})

    p.createFrame(os.path.join(plotDir, namec), createRatio=True, opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.8), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    p.getFrame().GetXaxis().SetTitle("MET Type 1 (GeV)")
    p.getFrame2().GetYaxis().SetTitle("Ratio")
    p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    p.draw()
    lumi = 0.0
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)
    """

    #########################################################################                                                                                                                             

    namePU = "TauMET_"+analysis+"_DataVsMC_nVtx"

    eff1PU = getEfficiency(dataset1,"NumeratorPU","DenominatorPU")
    eff2PU = getEfficiency(dataset2,"NumeratorPU","DenominatorPU")

    styles.dataStyle.apply(eff1PU)
    styles.mcStyle.apply(eff2PU)
    eff1PU.SetMarkerSize(1)
    eff2PU.SetMarkerSize(1.5)

    if isinstance(datasetsMC,dataset.DatasetManager):
        pPU = plots.ComparisonManyPlot(histograms.HistoGraph(eff1PU, "eff1", "p", "P"),
                                      [histograms.HistoGraph(eff2PU, "eff2", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2})
    else:
        pPU = plots.PlotBase([histograms.HistoGraph(eff1PU, "eff1", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1})


    opts = {"ymin": 0.001, "ymax": 1.0}
    moveLegend = {"dx": -0.33, "dy": -0.25, "dh": -0.1}
    createRatio = False
    if createRatio:
        pPU.createFrame(os.path.join(plotDir, namePU), createRatio=True, opts=opts, opts2=opts2)
    else:
        pPU.createFrame(os.path.join(plotDir, namePU), opts=opts, opts2=opts2)
    pPU.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
#    if createRatio:
#        pPU.getPad1().SetLogy(True)
#    else:
#        pPU.getPad().SetLogy(True)

    pPU.getFrame().GetYaxis().SetTitle("L1+HLT MET efficiency")
    pPU.getFrame().GetXaxis().SetTitle("Number of reco vertices")
    if createRatio:
        pPU.getFrame2().GetYaxis().SetTitle("Ratio")
        pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.4, 0.85, "LooseIsoPFTau50_Trk30_eta2p1_MET90", 17)
#    histograms.addText(0.4, 0.78, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.4, 0.78, "2016", 17)
    histograms.addText(0.4, 0.71, "Runs "+datasets.loadRunRange(), 17)

    pPU.draw()
    histograms.addStandardTexts(lumi=lumi)

    pPU.save(formats)

    print "Output written in",plotDir
def analyze(analysis=None):

    paths = [sys.argv[1]]

    if not analysis == None:
        datasets = dataset.getDatasetsFromMulticrabDirs(
            paths,
            analysisName=analysis,
            excludeTasks="Silver|GluGluHToTauTau_M125")
    else:
        datasets = dataset.getDatasetsFromMulticrabDirs(
            paths, excludeTasks="Silver|GluGluHToTauTau_M125")
        analysis = datasets.getAllDatasets()[0].getAnalysisName()

#    datasetsDY = None
    datasetsDY = dataset.getDatasetsFromMulticrabDirs(
        paths, analysisName=analysis, includeOnlyTasks="DYJetsToLL")
    #    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="GluGluHToTauTau_M125|TTJets")
    datasetsH125 = None
    #    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125",emptyDatasetsAsNone=True)
    #    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125")

    datasets.loadLuminosities()

    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
    dataset2 = dataset1
    #    dataset2 = datasets.getMCDatasets()
    if not datasetsDY == None:
        dataset2 = datasetsDY.getMCDatasets()

    eff1 = getEfficiency(dataset1)
    eff2 = getEfficiency(dataset2)
    if isinstance(datasetsH125, dataset.DatasetManager):
        eff3 = getEfficiency(datasetsH125.getMCDatasets())

    styles.dataStyle.apply(eff1)
    styles.mcStyle.apply(eff2)
    eff1.SetMarkerSize(1)
    #    eff2.SetMarkerSize(1.5)
    if isinstance(datasetsH125, dataset.DatasetManager):
        styles.mcStyle.apply(eff3)
        eff3.SetMarkerSize(1.5)
        eff3.SetMarkerColor(4)
        eff3.SetLineColor(4)


#    p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
#                             histograms.HistoGraph(eff2, "eff2", "p", "P"))

    if isinstance(datasetsH125, dataset.DatasetManager):
        p = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1, "eff1", "p", "P"), [
                histograms.HistoGraph(eff2, "eff2", "p", "P"),
                histograms.HistoGraph(eff3, "eff3", "p", "P")
            ])
    elif isinstance(datasetsDY, dataset.DatasetManager):
        p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
                                 histograms.HistoGraph(eff2, "eff2", "p", "P"))
    else:
        p = plots.PlotBase([histograms.HistoGraph(eff1, "eff1", "p", "P")])

    fit("Data", p, eff1, 20, 200)
    fit("MC", p, eff2, 20, 200)
    if isinstance(datasetsH125, dataset.DatasetManager):
        fit("H125", p, eff3, 20, 200)

    opts = {"ymin": 0, "ymax": 1.1}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    #    moveLegend = {"dx": -0.55, "dy": -0.15, "dh": -0.1}
    moveLegend = {"dx": -0.2, "dy": -0.5, "dh": -0.1}
    name = "TauMET_" + analysis + "_DataVsMC_PFTauPt"

    legend1 = "Data"
    #    legend2 = "MC (DY)"
    legend2 = "Simulation"
    legend3 = "MC (H125)"
    createRatio = False
    p.histoMgr.setHistoLegendLabelMany({"eff1": legend1})
    if isinstance(datasetsDY, dataset.DatasetManager):
        p.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2})
        createRatio = True
    if isinstance(datasetsH125, dataset.DatasetManager):
        p.histoMgr.setHistoLegendLabelMany({
            "eff1": legend1,
            "eff2": legend2,
            "eff3": legend3
        })

    if createRatio:
        p.createFrame(os.path.join(plotDir, name),
                      createRatio=createRatio,
                      opts=opts,
                      opts2=opts2)
    else:
        p.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    #    p.getFrame().GetXaxis().SetTitle("#tau-jet p_{T} (GeV/c)")
    p.getFrame().GetXaxis().SetTitle("#tau_{h} p_{T} (GeV/c)")
    if createRatio:
        p.getFrame2().GetYaxis().SetTitle("Ratio")
        p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.5, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    #    label = analysis.split("_")[len(analysis.split("_")) -1]
    label = "2016"

    histograms.addText(0.5, 0.53, label, 17)
    runRange = datasets.loadRunRange()
    histograms.addText(0.5, 0.46, "Runs " + runRange, 17)

    p.draw()
    lumi = 0.0
    for d in datasets.getDataDatasets():
        print "luminosity", d.getName(), d.getLuminosity()
        lumi += d.getLuminosity()
    print "luminosity, sum", lumi
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)

    pythonWriter.addParameters(plotDir, label, runRange, lumi, eff1)
    pythonWriter.addMCParameters(label, eff2)

    pythonWriter.writeJSON(
        os.path.join(plotDir, "tauLegTriggerEfficiency_" + label + ".json"))

    #    if not createRatio:
    #        sys.exit()

    #########################################################################

    eff1eta = getEfficiency(dataset1, "NumeratorEta", "DenominatorEta")
    eff2eta = getEfficiency(dataset2, "NumeratorEta", "DenominatorEta")
    if isinstance(datasetsH125, dataset.DatasetManager):
        eff3eta = getEfficiency(datasetsH125.getMCDatasets(), "NumeratorEta",
                                "DenominatorEta")

    styles.dataStyle.apply(eff1eta)
    styles.mcStyle.apply(eff2eta)
    eff1eta.SetMarkerSize(1)

    if isinstance(datasetsH125, dataset.DatasetManager):
        styles.mcStyle.apply(eff3eta)
        eff3eta.SetMarkerSize(1.5)
        eff3eta.SetMarkerColor(4)
        eff3eta.SetLineColor(4)

    if isinstance(datasetsH125, dataset.DatasetManager):
        p_eta = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"), [
                histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"),
                histograms.HistoGraph(eff3eta, "eff3eta", "p", "P")
            ])
    elif isinstance(datasetsDY, dataset.DatasetManager):
        p_eta = plots.ComparisonPlot(
            histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"),
            histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"))
    else:
        p_eta = plots.PlotBase(
            [histograms.HistoGraph(eff1eta, "eff1eta", "p", "P")])

    p_eta.histoMgr.setHistoLegendLabelMany({"eff1eta": legend1})
    if isinstance(datasetsDY, dataset.DatasetManager):
        p_eta.histoMgr.setHistoLegendLabelMany({
            "eff1eta": legend1,
            "eff2eta": legend2
        })
    if isinstance(datasetsH125, dataset.DatasetManager):
        p_eta.histoMgr.setHistoLegendLabelMany({
            "eff1eta": legend1,
            "eff2eta": legend2,
            "eff3eta": legend3
        })

    name = "TauMET_" + analysis + "_DataVsMC_PFTauEta"

    if createRatio:
        p_eta.createFrame(os.path.join(plotDir, name),
                          createRatio=createRatio,
                          opts=opts,
                          opts2=opts2)
    else:
        p_eta.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

    moveLegendEta = {"dx": -0.5, "dy": -0.65, "dh": -0.1}
    p_eta.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegendEta))

    p_eta.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p_eta.getFrame().GetXaxis().SetTitle("#tau-jet #eta")
    if createRatio:
        p_eta.getFrame2().GetYaxis().SetTitle("Ratio")
        p_eta.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.46, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.38, label, 17)
    histograms.addText(0.2, 0.31, "Runs " + datasets.loadRunRange(), 17)

    p_eta.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_eta.save(formats)

    #########################################################################

    eff1phi = getEfficiency(dataset1, "NumeratorPhi", "DenominatorPhi")
    eff2phi = getEfficiency(dataset2, "NumeratorPhi", "DenominatorPhi")
    if isinstance(datasetsH125, dataset.DatasetManager):
        eff3phi = getEfficiency(datasetsH125.getMCDatasets(), "NumeratorPhi",
                                "DenominatorPhi")

    styles.dataStyle.apply(eff1phi)
    styles.mcStyle.apply(eff2phi)
    eff1phi.SetMarkerSize(1)

    if isinstance(datasetsH125, dataset.DatasetManager):
        styles.mcStyle.apply(eff3phi)
        eff3phi.SetMarkerSize(1.5)
        eff3phi.SetMarkerColor(4)
        eff3phi.SetLineColor(4)

    if isinstance(datasetsH125, dataset.DatasetManager):
        p_phi = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"), [
                histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"),
                histograms.HistoGraph(eff3phi, "eff3phi", "p", "P")
            ])
    elif isinstance(datasetsDY, dataset.DatasetManager):
        p_phi = plots.ComparisonPlot(
            histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"),
            histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"))
    else:
        p_phi = plots.PlotBase(
            [histograms.HistoGraph(eff1phi, "eff1phi", "p", "P")])

    p_phi.histoMgr.setHistoLegendLabelMany({"eff1phi": legend1})
    if isinstance(datasetsDY, dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({
            "eff1phi": legend1,
            "eff2phi": legend2
        })
    if isinstance(datasetsH125, dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({
            "eff1phi": legend1,
            "eff2phi": legend2,
            "eff3phi": legend3
        })

    name = "TauMET_" + analysis + "_DataVsMC_PFTauPhi"

    if createRatio:
        p_phi.createFrame(os.path.join(plotDir, name),
                          createRatio=createRatio,
                          opts=opts,
                          opts2=opts2)
    else:
        p_phi.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

    moveLegendPhi = {"dx": -0.5, "dy": -0.65, "dh": -0.1}
    p_phi.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegendPhi))

    p_phi.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p_phi.getFrame().GetXaxis().SetTitle("#tau-jet #phi")
    if createRatio:
        p_phi.getFrame2().GetYaxis().SetTitle("Ratio")
        p_phi.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.46, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.38, label, 17)
    histograms.addText(0.2, 0.31, "Runs " + datasets.loadRunRange(), 17)

    p_phi.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_phi.save(formats)

    #########################################################################

    namePU = "TauMET_" + analysis + "_DataVsMC_nVtx"

    eff1PU = getEfficiency(dataset1, "NumeratorPU", "DenominatorPU")
    eff2PU = getEfficiency(dataset2, "NumeratorPU", "DenominatorPU")

    styles.dataStyle.apply(eff1PU)
    styles.mcStyle.apply(eff2PU)
    eff1PU.SetMarkerSize(1)
    eff2PU.SetMarkerSize(1.5)

    if isinstance(datasetsDY, dataset.DatasetManager):
        pPU = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1PU, "eff1", "p", "P"),
            [histograms.HistoGraph(eff2PU, "eff2", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({
            "eff1": legend1,
            "eff2": legend2
        })
    else:
        pPU = plots.PlotBase([histograms.HistoGraph(eff1PU, "eff1", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1})

    optsPU = {"ymin": 0.01, "ymax": 1.0}
    createRatio = False
    if createRatio:
        pPU.createFrame(os.path.join(plotDir, namePU),
                        createRatio=True,
                        opts=optsPU,
                        opts2=opts2)
    else:
        pPU.createFrame(os.path.join(plotDir, namePU),
                        opts=optsPU,
                        opts2=opts2)

    moveLegend = {"dx": -0.5, "dy": -0.5, "dh": -0.1}
    pPU.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegend))
    #    if createRatio:
    #        pPU.getPad1().SetLogy(True)
    #    else:
    #        pPU.getPad().SetLogy(True)

    pPU.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    pPU.getFrame().GetXaxis().SetTitle("Number of reco vertices")
    if createRatio:
        pPU.getFrame2().GetYaxis().SetTitle("Ratio")
        pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.53, label, 17)
    histograms.addText(0.2, 0.46, "Runs " + datasets.loadRunRange(), 17)

    pPU.draw()
    histograms.addStandardTexts(lumi=lumi)

    pPU.save(formats)

    #########################################################################
    """
    hName = "Pull"
#    hName = "Sub"
    namePull = "TauMET_"+analysis+"_DataVsMC_"+hName+"s"

    plots.mergeRenameReorderForDataMC(datasets)
    datasets.merge("MC", ["TT","WJets","DYJetsToLL","SingleTop","QCD"], keepSources=True)

    drh1 = datasets.getDataset("Data").getDatasetRootHisto(hName)
    drh2 = datasets.getDataset("MC").getDatasetRootHisto(hName)
    drh1.normalizeToOne()
    drh2.normalizeToOne()
    pull1 = drh1.getHistogram()
    pull2 = drh2.getHistogram()

    if isinstance(datasetsH125,dataset.DatasetManager):
        plots.mergeRenameReorderForDataMC(datasetsH125)
        drh3 = datasetsH125.getMCDatasets()[0].getDatasetRootHisto(hName)
        drh3.normalizeToOne()
        pull3 = drh3.getHistogram()

    styles.dataStyle.apply(pull1)
    styles.mcStyle.apply(pull2)
    pull1.SetMarkerSize(1)

    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(pull3)
        pull3.SetMarkerSize(1.5)
        pull3.SetMarkerColor(4)
        pull3.SetLineColor(4)

    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull = plots.ComparisonManyPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                         [histograms.Histo(pull2, "pull2", "p", "P"),
                                          histograms.Histo(pull3, "pull3", "p", "P")])
    else:
        p_pull = plots.ComparisonPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                      histograms.Histo(pull2, "pull2", "p", "P"))

    p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2})
    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2, "pull3": legend3})

    p_pull.createFrame(os.path.join(plotDir, namePull), createRatio=True, opts=opts, opts2=opts2)
    moveLegendPull = {"dx": -0.5, "dy": -0.35, "dh": -0.1}
    p_pull.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegendPull))

    p_pull.getFrame().GetYaxis().SetTitle("Arbitrary units")
#    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T} - #tau-jet p_{T} (GeV/c)")
    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T}/ #tau-jet p_{T} - 1")                                                                                                                                     
    p_pull.getFrame2().GetYaxis().SetTitle("Ratio")
    p_pull.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.75, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.68, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.2, 0.61, "Runs "+runRange, 17)

    p_pull.draw()

    histograms.addStandardTexts(lumi=lumi)
    p_pull.save(formats)
    """
    #########################################################################
    print "Output written in", plotDir
Esempio n. 48
0
def main():

    if len(sys.argv) < 2 and not os.path.isdir(sys.argv[1]):
        usage()

    multicrabdir = sys.argv[1]

    signalAnalysisDir = ""
    QCDAnalysisDir    = os.path.join(multicrabdir,"pseudoMulticrab_QCDMeasurement")

    candDirs = execute("ls %s"%multicrabdir)
    for d in candDirs:
        if "SignalAnalysis_" in d:
            signalAnalysisDir = os.path.join(multicrabdir,d)

    print "Using",signalAnalysisDir
    print "     ",QCDAnalysisDir

    style    = tdrstyle.TDRStyle()
    ROOT.gROOT.SetBatch(True)
    datasets    = dataset.getDatasetsFromMulticrabDirs([signalAnalysisDir], analysisName=analysis)

    datasetsQCD = dataset.getDatasetsFromMulticrabDirs([QCDAnalysisDir], analysisName="signalAnalysis")
    datasets.extend(datasetsQCD)

    plots.mergeRenameReorderForDataMC(datasets)
    luminosity = datasets.getDataset("Data").getLuminosity()

    # Semi-ugly hack for approval homework, remember improve for the next round
    totalErrorFile = ROOT.TFile("outputLight.root")
    #totalErrorFile = ROOT.TFile("outputHeavy.root")
    ## Remove superfluous shape variation uncertainties                                                                  
    totalErrorHistoUp = totalErrorFile.Get("total_background")
    rebin = [0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,400,420,440,460,480,500,600,700,800]
    import array
    #totalErrorHistoUp.Sumw2()
    totalErrorHistoUp = totalErrorHistoUp.Rebin(len(rebin)-1,"foo",array.array("d",rebin))
    totalErrorHistoDown = totalErrorHistoUp.Clone("foo2")
    for i in xrange(1, totalErrorHistoUp.GetNbinsX()+1):
        #print i, totalErrorHistoUp.GetBinContent(i), totalErrorHistoUp.GetBinError(i)
        totalErrorHistoUp.SetBinContent(i, totalErrorHistoUp.GetBinError(i))
        totalErrorHistoDown.SetBinContent(i, -totalErrorHistoDown.GetBinError(i))
        #print i, totalErrorHistoUp.GetBinContent(i), totalErrorHistoDown.GetBinContent(i)
    # semi-ugly hack continues below in QCD

    myStackList = []
    h_data = datasets.getDataset("Data").getDatasetRootHisto("ForDataDrivenCtrlPlots/shapeTransverseMass_POSTFIT").getHistogram()
    myRHWU = dataset.RootHistoWithUncertainties(h_data)
    myRHWU.makeFlowBinsVisible()
    myHisto = histograms.Histo(myRHWU, "Data")
    myHisto.setIsDataMC(isData=True, isMC=False)
    myStackList.insert(0, myHisto)

    h_FakeTau = datasets.getDataset("QCDMeasurementMT").getDatasetRootHisto("ForDataDrivenCtrlPlots/shapeTransverseMass_POSTFIT").getHistogram()
    myRHWU = dataset.RootHistoWithUncertainties(h_FakeTau)
    # semi-ugly hack continues
    htemp=myRHWU.getRootHisto()
    myRHWU.addAbsoluteShapeUncertainty("toterr", totalErrorHistoUp, totalErrorHistoDown)
    #myRHWU.addShapeUncertaintyFromVariation("toterr", totalErrorHistoUp, totalErrorHistoDown)
    # semi-ugly hack ends
    myRHWU.makeFlowBinsVisible()
    myHisto = histograms.Histo(myRHWU, "QCDdata")
    myHisto.setIsDataMC(isData=False, isMC=True)
    myStackList.insert(1, myHisto)

    expectedList = []
    expectedList.append("TT")
    expectedList.append("WJets")
    expectedList.append("SingleTop")
    expectedList.append("DYJetsToLL")
    expectedList.append("Diboson")

    for i in range(0,len(expectedList)):
        drh = datasets.getDataset(expectedList[i]).getDatasetRootHisto("ForDataDrivenCtrlPlots/shapeTransverseMass_POSTFIT")
        h_bgr = drh.getHistogram()
        myRHWU = dataset.RootHistoWithUncertainties(h_bgr)
#        myRHWU.addShapeUncertaintyRelative("syst", th1Plus=self._expectedListSystUp[i], th1Minus=self._expectedListSystDown[i])
        myRHWU.makeFlowBinsVisible()
        myHisto = histograms.Histo(myRHWU, expectedList[i])
        myHisto.setIsDataMC(isData=False, isMC=True)
        myStackList.append(myHisto)

    #myStackList = divideByBinWidth(myStackList)

    # no, ugly hack continues here
    histograms.uncertaintyMode.set(histograms.Uncertainty.SystOnly)
    plots._legendLabels["MCSystError"] = "Bkg. stat.#oplus syst. unc."
    plots._legendLabels["BackgroundSystError"] = "Bkg. stat.#oplus syst. unc."
    # and stops again here
    myStackPlot = plots.DataMCPlot2(myStackList)
    myStackPlot.setLuminosity(luminosity)
    myStackPlot.setDefaultStyles()
    myParams = {}   
    myParams["ylabel"] = "Events / bin"
    myParams["ratioYlabel"] = "Data/Bkg."
    myParams["xlabel"] = "m_{T} (GeV)" 

    myParams["log"] = True
    myParams["ratio"] = True
    myParams["cmsTextPosition"] = "outframe"
    myParams["opts"] = {"ymin": 0.0001, "ymax": 3000.0}
    myParams["opts2"] = {"ymin": 0., "ymax":1.99}
    myParams["moveLegend"] = {"dx": -0.15, "dy": 0., "dh":0.05} # for data-driven
    myParams["ratioMoveLegend"] = {"dx": -0.51, "dy": 0.03}
    myParams["stackMCHistograms"] = True
    myParams["divideByBinWidth"] = True
    myParams["addMCUncertainty"] = True
    myParams["ratioType"] = "errorScale"
    myParams["ratioCreateLegend"] = True
    myParams["ratioMoveLegend"] = dict(dy=-0.45, dh=-0.1, dx=-0.5)
    plots.drawPlot(myStackPlot, "TransVerseMassPOSTFIT", **myParams)
def main(argv):
    dirs = []
    if len(sys.argv) < 2:
	usage()

    dirs.append(sys.argv[1])

    comparisonList = ["AfterStdSelections"]
    
    # Create all datasets from a multicrab task
    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra, searchMode=searchMode, analysisName=analysis)
    #print datasets.getDatasetNames()

    print " dirs ",dirs[0]
    # Check multicrab consistency
#    consistencyCheck.checkConsistencyStandalone(dirs[0],datasets,name="CorrelationAnalysis")
   
  # As we use weighted counters for MC normalisation, we have to
    # update the all event count to a separately defined value because
    # the analysis job uses skimmed pattuple as an input
    datasets.updateNAllEventsToPUWeighted()

    # Read integrated luminosities of data datasets from lumi.json
    datasets.loadLuminosities()

    # Include only 120 mass bin of HW and HH datasets
    #datasets.remove(filter(lambda name: "TTToHplus" in name and not "M120" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "TTToHplusBWB" in name, datasets.getAllDatasetNames()))

    datasets.remove(filter(lambda name: "HplusTB" in name and not "M_500" in name, datasets.getAllDatasetNames()))
   # datasets.remove(filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_t-channel" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_tW-channel" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "TTJets_SemiLept" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "TTJets" in name, datasets.getAllDatasetNames()))
    #datasets.remove(filter(lambda name: "DYJetsToLL_M_50_HT" in name, datasets.getAllDatasetNames()))
#    datasets.remove(filter(lambda name: "QCD" in name, datasets.getAllDatasetNames()))
    #datasets.remove(filter(lambda name: "WJetsToLNu" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: ("DYJetsToLL_M_10to50" in name or "DYJetsToLL_M_50" in name) and not "DYJetsToLL_M_50_HT" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "WJetsToLNu" in name and not "WJetsToLNu_HT" in name, datasets.getAllDatasetNames()))  
    datasets.remove(filter(lambda name: "DY2JetsToLL" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "DY3JetsToLL" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "DY4JetsToLL" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "ST" in name, datasets.getAllDatasetNames()))
               
        # Default merging nad ordering of data and MC datasets
    # All data datasets to "Data"
    # All QCD datasets to "QCD"
    # All single top datasets to "SingleTop"
    # WW, WZ, ZZ to "Diboson"
    plots.mergeRenameReorderForDataMC(datasets)

    # Set BR(t->H) to 0.05, keep BR(H->tau) in 1
    xsect.setHplusCrossSectionsToBR(datasets, br_tH=0.05, br_Htaunu=1)

    # Merge WH and HH datasets to one (for each mass bin)
    # TTToHplusBWB_MXXX and TTToHplusBHminusB_MXXX to "TTToHplus_MXXX"
    plots.mergeWHandHH(datasets)

#    datasets.getDataset("TTbar_HBWB_HToTauNu_M_160_13TeV_pythia6").setCrossSection(0.336902*2*0.955592) # pb   

    # At the moment the collision energy must be set by hand
#    for dset in datasets.getMCDatasets():
#        dset.setEnergy("13")

    # At the moment the cross sections must be set by hand
    #xsect.setBackgroundCrossSections(datasets)


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

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)


    dataMCExample(datasets)
#    MtComparison(datasets)
    MetComparisonBaselineVsInverted(datasets)
#    MetComparison(datasets)
#    TauPtComparison(datasets)

   # Print counters
    doCounters(datasets)



    # Script execution can be paused like this, it will continue after
    # user has given some input (which must include enter)
    if drawToScreen:
        raw_input("Hit enter to continue")
Esempio n. 50
0
def main(argv):
    dirs = []
    if len(sys.argv) < 2:
        usage()

    dirs.append(sys.argv[1])

    comparisonList = ["AfterStdSelections"]

    # Create all datasets from a multicrab task
    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,
                                                    dataEra=dataEra,
                                                    searchMode=searchMode,
                                                    analysisName=analysis)
    #print datasets.getDatasetNames()

    #print datasets
    # Check multicrab consistency
    consistencyCheck.checkConsistencyStandalone(dirs[0],
                                                datasets,
                                                name="QCD inverted")

    # As we use weighted counters for MC normalisation, we have to
    # update the all event count to a separately defined value because
    # the analysis job uses skimmed pattuple as an input
    datasets.updateNAllEventsToPUWeighted()

    # Read integrated luminosities of data datasets from lumi.json
    datasets.loadLuminosities()

    # Include only 120 mass bin of HW and HH datasets
    datasets.remove(
        filter(lambda name: "TTToHplus" in name and not "M120" in name,
               datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "Hplus_taunu_t-channel" in name,
               datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "Hplus_taunu_tW-channel" in name,
               datasets.getAllDatasetNames()))
    #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()))
    # Default merging nad ordering of data and MC datasets
    # All data datasets to "Data"
    # All QCD datasets to "QCD"
    # All single top datasets to "SingleTop"
    # WW, WZ, ZZ to "Diboson"
    plots.mergeRenameReorderForDataMC(datasets)

    # Set BR(t->H) to 0.05, keep BR(H->tau) in 1
    xsect.setHplusCrossSectionsToBR(datasets, br_tH=0.05, br_Htaunu=1)

    # Merge WH and HH datasets to one (for each mass bin)
    # TTToHplusBWB_MXXX and TTToHplusBHminusB_MXXX to "TTToHplus_MXXX"
    plots.mergeWHandHH(datasets)

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

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)

    for HISTONAME in comparisonList:
        invertedQCD = InvertedTauID()
        invertedQCD.setLumi(datasets.getDataset("Data").getLuminosity())
        invertedQCD.setInfo([dataEra, searchMode, HISTONAME])

        histonames = datasets.getDataset("Data").getDirectoryContent(
            "ForQCDNormalization/NormalizationMETBaselineTau" + HISTONAME)
        bins = []
        binLabels = []
        for histoname in histonames:
            bins.append(
                histoname.replace("NormalizationMETBaselineTau" + HISTONAME,
                                  ""))
            title = datasets.getDataset("Data").getDatasetRootHisto(
                "ForQCDNormalization/NormalizationMETBaselineTau" + HISTONAME +
                "/" + histoname).getHistogram().GetTitle()
            title = title.replace("METBaseline" + HISTONAME, "")
            title = title.replace("#tau p_{T}", "taup_T")
            title = title.replace("#tau eta", "taueta")
            title = title.replace("<", "lt")
            title = title.replace(">", "gt")
            title = title.replace("=", "eq")
            title = title.replace("..", "to")
            title = title.replace(".", "p")
            title = title.replace("/", "_")
            binLabels.append(title)
        binLabels = bins  # for this data set
        print
        print "Histogram bins available", bins

        #    bins = ["Inclusive"]
        #    bins = ["taup_Tleq50","taup_Teq50to60"]
        print "Using bins              ", bins
        print
        print "Bin labels"
        for i in range(len(binLabels)):
            line = bins[i]
            while len(line) < 10:
                line += " "
            line += ": " + binLabels[i]
            print line
        print

        for i, bin in enumerate(bins):

            invertedQCD.setLabel(binLabels[i])

            metBase = plots.DataMCPlot(
                datasets, "ForQCDNormalization/NormalizationMETBaselineTau" +
                HISTONAME + "/NormalizationMETBaselineTau" + HISTONAME + bin)
            metInver = plots.DataMCPlot(
                datasets, "ForQCDNormalization/NormalizationMETInvertedTau" +
                HISTONAME + "/NormalizationMETInvertedTau" + HISTONAME + bin)
            # Rebin before subtracting
            RebinFactor = 10
            metBase.histoMgr.forEachHisto(
                lambda h: h.getRootHisto().Rebin(RebinFactor))
            metInver.histoMgr.forEachHisto(
                lambda h: h.getRootHisto().Rebin(RebinFactor))

            metInverted_data = metInver.histoMgr.getHisto("Data").getRootHisto(
            ).Clone("ForQCDNormalization/NormalizationMETInvertedTau" +
                    HISTONAME + "/NormalizationMETInvertedTau" + HISTONAME +
                    bin)
            metInverted_EWK = metInver.histoMgr.getHisto("EWK").getRootHisto(
            ).Clone("ForQCDNormalization/NormalizationMETInvertedTau" +
                    HISTONAME + "/NormalizationMETInvertedTau" + HISTONAME +
                    bin)
            metBase_data = metBase.histoMgr.getHisto("Data").getRootHisto(
            ).Clone("ForQCDNormalization/NormalizationMETBaselineTau" +
                    HISTONAME + "/NormalizationMETBaselineTau" + HISTONAME +
                    bin)
            metBase_EWK = metBase.histoMgr.getHisto("EWK").getRootHisto(
            ).Clone("ForQCDNormalization/NormalizationMETBaselineTau" +
                    HISTONAME + "/NormalizationMETBaselineTau" + HISTONAME +
                    bin)

            metBase_QCD = metBase_data.Clone("QCD")
            metBase_QCD.Add(metBase_EWK, -1)

            metInverted_QCD = metInverted_data.Clone("QCD")
            metInverted_QCD.Add(metInverted_EWK, -1)

            metInverted_data = addlabels(metInverted_data)
            metInverted_EWK = addlabels(metInverted_EWK)
            metBase_data = addlabels(metBase_data)
            metBase_EWK = addlabels(metBase_EWK)
            metInverted_QCD = addlabels(metInverted_QCD)

            invertedQCD.plotHisto(metInverted_data, "inverted")
            invertedQCD.plotHisto(metInverted_EWK, "invertedEWK")
            invertedQCD.plotHisto(metBase_data, "baseline")
            invertedQCD.plotHisto(metBase_EWK, "baselineEWK")

            fitOptions = "RB"

            invertedQCD.fitEWK(metInverted_EWK, fitOptions)
            invertedQCD.fitEWK(metBase_EWK, fitOptions)
            invertedQCD.fitQCD(metInverted_QCD, fitOptions)
            invertedQCD.fitData(metBase_data)

            invertedQCD.getNormalization()

        invertedQCD.Summary()
        normalizationFileName = HISTONAME  #.replace("TauIdAfterCollinearCuts","")
        if HISTONAME == "TauIdAfterCollinearCutsPlusFilteredEWKFakeTaus":
            normalizationFileName = normalizationFileName.replace("Plus", "")
        invertedQCD.WriteNormalizationToFile(
            "QCDInvertedNormalizationFactors" + normalizationFileName + ".py")
        invertedQCD.WriteLatexOutput("fits" + normalizationFileName + ".tex")
Esempio n. 51
0
def main():
    if len(sys.argv) < 2:
        usage()
    analysis = _analysis
    if "--QCD" in sys.argv:
        analysis = "QCDMeasurement"

    dirs = []
    dirs.append(sys.argv[1])
    dsetMgr = dataset.getDatasetsFromMulticrabDirs(dirs,
                                                   dataEra=dataEra,
                                                   searchMode=searchMode,
                                                   analysisName=analysis,
                                                   optimizationMode=optMode)
    dsetMgr.loadLuminosities()
    dsetMgr.updateNAllEventsToPUWeighted()
    plots.mergeRenameReorderForDataMC(dsetMgr)
    #dsetMgr.normalizeToLuminosity()
    lumi = dsetMgr.getDataset("Data").getLuminosity()

    # Apply TDR style
    style = tdrstyle.TDRStyle()

    # Format: list of [denominator, numerator] pairs
    plotSources = OrderedDict()
    plotSources["trg_vs_vtx"] = [
        "PUDependency/NvtxTrg", "PUDependency/NvtxVtx"
    ]
    plotSources["vtx_vs_antiIsolTau"] = [
        "PUDependency/NvtxVtx", "PUDependency/NvtxAntiIsolatedTau"
    ]
    plotSources["vtx_vs_tau"] = [
        "PUDependency/NvtxVtx", "PUDependency/NvtxTau"
    ]
    if not "--QCD" in sys.argv:
        plotSources["tau_vs_eveto"] = [
            "PUDependency/NvtxTau", "PUDependency/NvtxElectronVeto"
        ]
    else:
        plotSources["tau_vs_eveto"] = [
            "PUDependency/NvtxAntiIsolatedTau", "PUDependency/NvtxElectronVeto"
        ]
    plotSources["eveto_vs_muveto"] = [
        "PUDependency/NvtxElectronVeto", "PUDependency/NvtxMuonVeto"
    ]
    plotSources["jet_vs_muveto"] = [
        "PUDependency/NvtxMuonVeto", "PUDependency/NvtxJetSelection"
    ]
    plotSources["rcoll_vs_jet"] = [
        "PUDependency/NvtxJetSelection",
        "PUDependency/NvtxAngularCutsCollinear"
    ]
    plotSources["btag_vs_rcoll"] = [
        "PUDependency/NvtxAngularCutsCollinear", "PUDependency/NvtxBtagging"
    ]
    plotSources["met_vs_btag"] = [
        "PUDependency/NvtxBtagging", "PUDependency/NvtxMETSelection"
    ]
    plotSources["rbb_vs_met"] = [
        "PUDependency/NvtxMETSelection",
        "PUDependency/NvtxAngularCutsBackToBack"
    ]
    plotSources["allsel_vs_rbb"] = [
        "PUDependency/NvtxAngularCutsBackToBack",
        "PUDependency/NvtxAllSelections"
    ]
    if not "--QCD" in sys.argv:
        plotSources["propbtag_vs_btag"] = [
            "PUDependency/NvtxBtagging",
            "PUDependency/NvtxAllSelectionsWithProbabilisticBtag"
        ]
    plotSources["allsel_vs_trg"] = [
        "PUDependency/NvtxTrg", "PUDependency/NvtxAllSelections"
    ]
    #plotSources["tau_isol_pt"] = ["tauSelection_/IsolPtBefore","tauSelection_/IsolPtAfter"]
    #plotSources["tau_isol_eta"] = ["tauSelection_/IsolEtaBefore","tauSelection_/IsolEtaAfter"]
    #plotSources["tau_isol_vtx"] = ["tauSelection_/IsolVtxBefore","tauSelection_/IsolVtxAfter"]
    #plotSources["e_isol_pt"] = ["eSelection_Veto/IsolPtBefore","eSelection_Veto/IsolPtAfter"]
    #plotSources["e_isol_eta"] = ["eSelection_Veto/IsolEtaBefore","eSelection_Veto/IsolEtaAfter"]
    #plotSources["e_isol_vtx"] = ["eSelection_Veto/IsolVtxBefore","eSelection_Veto/IsolVtxAfter"]
    #plotSources["mu_isol_pt"] = ["muSelection_Veto/IsolPtBefore","muSelection_Veto/IsolPtAfter"]
    #plotSources["mu_isol_eta"] = ["muSelection_Veto/IsolEtaBefore","muSelection_Veto/IsolEtaAfter"]
    #plotSources["mu_isol_vtx"] = ["muSelection_Veto/IsolVtxBefore","muSelection_Veto/IsolVtxAfter"]
    dsetInputs = {
        #"TTJets": ["TTJets"], # Madgraph with negative weights
        "TT": ["TT"],  # Powheg, no neg. weights -> large stats.
        "TTJets": ["TTJets"],
        "WJets": ["WJetsHT"],
        "EWK": ["TTJets", "WJetsHT", "DYJetsToLL", "SingleTop"],
        "QCD": ["QCD"],
        "Data": ["Data"],
    }
    summarySources = [
        "vtx_vs_antiIsolTau", "vtx_vs_tau", "tau_vs_eveto", "eveto_vs_muveto",
        "jet_vs_muveto", "btag_vs_rcoll", "met_vs_btag", "allsel_vs_trg"
    ]

    # Create plots (MC vs. MC)
    doPlots(dsetMgr, lumi, plotSources, dsetInputs, summarySources)
    # Create plots (data vs. MC)
    doPlots(dsetMgr, lumi, plotSources, dsetInputs, summarySources, "Data")
Esempio n. 52
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]