def main(opts):
    Verbose("main function")

    #optModes = ["", "OptChiSqrCutValue40", "OptChiSqrCutValue60", "OptChiSqrCutValue80", "OptChiSqrCutValue100", "OptChiSqrCutValue120", "OptChiSqrCutValue140"]
    #optModes = ["", "OptChiSqrCutValue50", "OptChiSqrCutValue100", "OptChiSqrCutValue150", "OptChiSqrCutValue200"]
    optModes = ["OptChiSqrCutValue100"]

    if opts.optMode != None:
        optModes = [opts.Mode]

    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Remove datasets
        datasetsMgr.remove(filter(lambda name: "QCD" in name, datasetsMgr.getAllDatasetNames()))
        datasetsMgr.remove(filter(lambda name: "QCD-b" in name, datasetsMgr.getAllDatasetNames()))
        datasetsMgr.remove(filter(lambda name: "Charged" in name, datasetsMgr.getAllDatasetNames()))
        
        # Re-order datasets (different for inverted than default=baseline)
        newOrder = ["Data"] #, "TT", "DYJetsToQQHT", "TTWJetsToQQ", "WJetsToQQ_HT_600ToInf", "SingleTop", "Diboson", "TTZToQQ", "TTTT"]
        newOrder.extend(GetListOfEwkDatasets())
        datasetsMgr.selectAndReorder(newOrder)

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()
        else:
            Print("Cannot draw the histograms without the option --mergeEWK. Exit", True)
            sys.exit()
            
        # Print dataset information
        datasetsMgr.PrintInfo()
        
        # Apply TDR style
        style = tdrstyle.TDRStyle()
        style.setOptStat(True)
        
        # Do the fit
        hName = "topSelection_Baseline/LdgTrijetMass_After" #"topSelection_Baseline/LdgTrijetMass_Before"
        PlotAndFitTemplates(datasetsMgr, hName.split("/")[-1], True, opts)
        
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50", "OptChiSqrCutValue100"]
    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0) # ATLAS 13 TeV H->tb exclusion limits
                
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
   
        # Custom Filtering of datasets 
        datasetsMgr.remove(filter(lambda name: "QCD-b" in name, datasetsMgr.getAllDatasetNames()))
        # datasetsMgr.remove(filter(lambda name: "Charged" in name, datasetsMgr.getAllDatasetNames()))

        # Re-order datasets (different for inverted than default=baseline)
        newOrder = ["Data"]
        if opts.signalMass != 0:
            signal = "ChargedHiggs_HplusTB_HplusToTB_M_%.0f" % opts.signalMass
            newOrder.extend([signal])
        else:
            newOrder.extend(["QCD-Data"])
        newOrder.extend(["QCD"])
        newOrder.extend(GetListOfEwkDatasets())
        datasetsMgr.selectAndReorder(newOrder)
        
        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do Data-MC histograms with DataDriven QCD
        DataMCHistograms(datasetsMgr)
    return
Exemple #3
0
def main(opts):
    Verbose("main function")

    comparisonList = ["AfterStdSelections"]

    # Setup & configure the dataset manager 
    datasetsMgr = GetDatasetsFromDir(opts)
    datasetsMgr.updateNAllEventsToPUWeighted()
    datasetsMgr.loadLuminosities() # from lumi.json
    if opts.verbose:
        datasetsMgr.PrintCrossSections()
        datasetsMgr.PrintLuminosities()

    # Custom Filtering of datasets 
    if 1:
         datasetsMgr.remove(filter(lambda name: "HplusTB" in name and not "M_500" in name, datasetsMgr.getAllDatasetNames()))
               
    # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
    plots.mergeRenameReorderForDataMC(datasetsMgr) 
   
    # Get Integrated Luminosity
    if opts.mcOnly:
        # Determine integrated lumi
        if opts.intLumi < 0.0:
            opts.intLumi = GetLumi(datasetsMgr)
        # Remove data datasets
        datasetsMgr.remove(filter(lambda name: "Data" in name, datasetsMgr.getAllDatasetNames()))

    # Re-order datasets (different for inverted than default=baseline)
    newOrder = ["Data"] #, "TT", "DYJetsToQQHT", "TTWJetsToQQ", "WJetsToQQ_HT_600ToInf", "SingleTop", "Diboson", "TTZToQQ", "TTTT"]
    newOrder.extend(GetListOfEwkDatasets())
    if opts.mcOnly:
        newOrder.remove("Data")
    datasetsMgr.selectAndReorder(newOrder)

    # Set/Overwrite cross-sections                                                                                                                                                                                             
    for d in datasetsMgr.getAllDatasets():
        if "ChargedHiggs" in d.getName():
            datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

    # Merge EWK samples
    if opts.mergeEWK:
        datasetsMgr.merge("EWK", GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

    # Print dataset information
    datasetsMgr.PrintInfo()

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

    # Do the Baseline Vs Inverted histograms
    if opts.mergeEWK:
        for hName in getTopSelectionHistos(opts.histoLevel):
            BaselineVsInvertedComparison(datasetsMgr, hName.split("/")[-1])
    else:
        Print("Cannot draw the Baseline Vs Inverted histograms without the option --mergeEWK. Exit", True)
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50p0", "OptChiSqrCutValue100p0", "OptChiSqrCutValue200p0"]
    optModes = ["OptChiSqrCutValue100"]

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        
        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            
        # Custom Filtering of datasets 
        if 0:
            datasetsMgr.remove(filter(lambda name: "Charged" in name and not "M_500" in name, datasetsMgr.getAllDatasetNames()))
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
   
        # Re-order datasets (different for inverted than default=baseline)
        if 0:
            newOrder = ["Data"]
            newOrder.extend(GetListOfEwkDatasets())
            datasetsMgr.selectAndReorder(newOrder)

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the purity plots
        if not opts.mergeEWK:        
            Print("Cannot draw the Data/QCD/EWK histograms without the option --mergeEWK. Exit", True)
            return
        for hName in GetHistoList(analysisType="Inverted", bType=""):
            PurityPlots(datasetsMgr, hName, "Inverted")
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50", "OptChiSqrCutValue100"]
    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(
                    1.0)  # ATLAS 13 TeV H->tb exclusion limits

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Custom Filtering of datasets
        removeList = ["Data", "QCD-b", "Charged", "EWK"]
        for d in removeList:
            datasetsMgr.remove(
                filter(lambda name: d in name,
                       datasetsMgr.getAllDatasetNames()))

        # Replace QCD MC with QCD-DataDriven
        qcdDatasetName = "FakeBMeasurementTrijetMass"
        qcdDatasetNameNew = "QCD-Data"
        replaceQCDFromData(datasetsMgr, qcdDatasetName, qcdDatasetNameNew)

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do Purity histograms with DataDriven QCD
        PurityHistograms(datasetsMgr, qcdDatasetNameNew)
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50", "OptChiSqrCutValue100"]
    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0) # ATLAS 13 TeV H->tb exclusion limits
                
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()
   
        # Custom Filtering of datasets 
        removeList = ["Data", "QCD-b", "Charged", "EWK"]
        for d in removeList:
            datasetsMgr.remove(filter(lambda name: d in name, datasetsMgr.getAllDatasetNames()))

        # Replace QCD MC with QCD-DataDriven
        qcdDatasetName    = "FakeBMeasurementTrijetMass"
        qcdDatasetNameNew = "QCD-Data"
        replaceQCDFromData(datasetsMgr, qcdDatasetName, qcdDatasetNameNew)

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do Purity histograms with DataDriven QCD
        PurityHistograms(datasetsMgr, qcdDatasetNameNew)
    return
def main(opts, signalMass):

    optModes = ["OptChiSqrCutValue100"]                                                                                                                             

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        datasetsMgr1 = GetDatasetsFromDir_secondDir(opts)
        datasetsMgr1.updateNAllEventsToPUWeighted()
        datasetsMgr1.loadLuminosities() # from lumi.json

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        for d in datasetsMgr1.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr1.getDataset(d.getName()).setCrossSection(1.0)
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        plots.mergeRenameReorderForDataMC(datasetsMgr1) 
        
        # Determine integrated Lumi before removing data
        #intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        intLumi = 0

        # Remove datasets
        if 1:
            datasetsMgr.remove(filter(lambda name: "Data" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "QCD-b" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "QCD" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "TTZToQQ" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "TTWJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "TTTT" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "FakeBMeasurementTrijetMass" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "M_" in name and "M_" + str(opts.signalMass) not in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr1.remove(filter(lambda name: "Data" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr1.remove(filter(lambda name: "QCD-b" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr1.remove(filter(lambda name: "QCD" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr1.remove(filter(lambda name: "TTZToQQ" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr1.remove(filter(lambda name: "TTWJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr1.remove(filter(lambda name: "TTTT" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr1.remove(filter(lambda name: "FakeBMeasurementTrijetMass" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "M_" in name and "M_" + str(opts.signalMass) not in name, datasetsMgr.getAllDatasetNames()))
        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

#        datasetsMgr.merge("QCD", GetListOfQCDatasets())
#        plots._plotStyles["QCD"] = styles.getAltEWKStyle()
#        Background1_Dataset = datasetsMgr.getDataset("QCD")
        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            if "M_" in d.getName():
                if d not in signalMass:
                    continue
            datasetOrder.append(d.getName())
            
        for m in signalMass:
            datasetOrder.insert(0, m)
        datasetsMgr.selectAndReorder(datasetOrder)
        datasetsMgr1.selectAndReorder(datasetOrder)

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the topSelection histos
        folder      = opts.folder 
        histoPaths1 = []
        if folder != "":
            histoList  = datasetsMgr.getDataset(datasetOrder[0]).getDirectoryContent(folder)
            histoPaths1 = [os.path.join(folder, h) for h in histoList]
        
        numerators   = [#"TopQuarkPt_InTopDirBDT",
                        #"AllTopQuarkPt_InTopDirBDT",
                        "AllTopQuarkPt_MatchedBDT",
                        #"TrijetNotInTopDirPt_BDT",
                        "TrijetFakePt_BDT",
                        #"AllTopQuarkPt_InTopDir",
                        "AllTopQuarkPt_Matched",
                        "EventTrijetPt2T_MatchedBDT",
                        "EventTrijetPt2T_MatchedBDT",
                        "EventTrijetPt2T_MatchedBDT",
                        #"AllTopQuarkPt_NotInTopDir",
                        #"EventTrijetPt_InTopDirBDT",
                        "AllTopQuarkPt_MatchedBDT",
                        #"TrijetPtMaxMVASameFakeObj_BjetPassCSV",
                        "SelectedTrijetsPt_BjetPassCSVdisc_afterCuts",
                        "TrijetPt_PassBDT_BJetPassCSV",
#                        "TopFromHiggsPt_isLdgMVATrijet_afterCuts",
#                        "TopFromHiggsPt_isSubldgMVATrijet_afterCuts",
#                        "TopFromHiggsPt_isMVATrijet_afterCuts",
#                        "LdgBjetPt_isLdgFreeBjet",
#                        "ChHiggsBjetPt_TetrajetBjetPt_Matched_afterCuts",
#                        "TopFromHiggsPt_notMVATrijet_afterCuts",
#                        "HiggsBjetPt_isTrijetSubjet_afterCuts",
                        ]
        denominators = [#"TrijetPt_BDT",
                        #"AllTopQuarkPt_InTopDir",
                        "AllTopQuarkPt_Matched",
                        #"TrijetNotInTopDirPt",
                        "TrijetFakePt",
                        #"TopQuarkPt",
                        "TopQuarkPt",
                        "EventTrijetPt2T_BDT",
                        "EventTrijetPt2T_Matched",
                        "EventTrijetPt2T",
                        #"TopQuarkPt",
                        #"EventTrijetPt_BDT",
                        "TopQuarkPt",
                        #"TrijetPtMaxMVASameFakeObj",
                        "SelectedTrijetsPt_afterCuts",
                        "TrijetPt_PassBDT",
#                        "TopFromHiggsPt_afterCuts",
#                        "TopFromHiggsPt_afterCuts",
#                        "TopFromHiggsPt_afterCuts",
#                        "LdgBjetPt",
#                        "ChHiggsBjetPt_foundTetrajetBjet_afterCuts",
#                        "TopFromHiggsPt_afterCuts",
#                        "HiggsBjetPt_afterCuts",
                        ]
        
        datasets  = datasetsMgr.getAllDatasets()
        datasets1 = datasetsMgr1.getAllDatasets()

        for dataset in datasets1:
            datasets.append(dataset)
        for i in range(len(numerators)):
            
#            PlotProb(datasetsMgr.getAllDatasets(), folder+"/"+numerators[i], folder+"/"+denominators[i])
            PlotProb(datasets, folder+"/"+numerators[i], folder+"/"+denominators[i])

        myHistoList = ["AllTopQuarkPt_MatchedBDT", "AllTopQuarkPt_Matched", "TrijetFakePt_BDT", "TrijetFakePt", "EventTrijetPt2T_MatchedBDT", "EventTrijetPt2T_BDT", "TopQuarkPt", "TrijetPt_BDT",
                       "LdgTrijetPt", "SubldgTrijetPt", "TopQuarkPt_BDT",]
        for i in range(len(myHistoList)):
            PlotMC(datasetsMgr, datasetsMgr1, folder+"/"+myHistoList[i], intLumi)


        folder     = "ForDataDrivenCtrlPlots"
        histoList  = datasetsMgr.getDataset(datasetOrder[0]).getDirectoryContent(folder)
        hList0     = [x for x in histoList if "TrijetMass" in x]
        hList1     = [x for x in histoList if "TetrajetMass" in x]
        hList2     = [x for x in histoList if "TetrajetBjetPt" in x]
        histoPaths2 = [os.path.join(folder, h) for h in hList0+hList1+hList2]

        '''
        histoPaths = histoPaths1 + histoPaths2
        for h in histoPaths:
            
            if "Vs" in h:                     # Skip TH2D
                continue
            PlotMC(datasetsMgr, h, intLumi)
            '''
    return
Exemple #8
0
def main(opts, signalMass):

    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Determine integrated Lumi before removing data
        #intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        intLumi = 5747.588 + 2573.399 + 4248.384 + 4008.663 + 2704.118 + 405.222 + 7539.457 + 8390.5 + 215.149

        # Remove datasets
        if 0:
            datasetsMgr.remove(
                filter(lambda name: "Data" in name,
                       datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(
                filter(lambda name: "QCD-b" in name,
                       datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(
                filter(lambda name: "QCD" in name,
                       datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "SingleTop" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "DYJetsToQQHT" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TTZToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TTWJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "WJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "Diboson" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TTTT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(
                filter(lambda name: "FakeBMeasurementTrijetMass" in name,
                       datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "M_" in name and "M_" + str(opts.signalMass) not in name, datasetsMgr.getAllDatasetNames()))

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Re-order datasets (different for inverted than default=baseline)
        newOrder = []
        if opts.mergeEWK:
            newOrder = ["EWK", "QCD"]
        else:
            newOrder.extend(GetListOfEwkDatasets())
            newOrder.append("QCD")
        for d in datasetsMgr.getAllDatasetNames():
            if "ChargedHiggs" in d:
                newOrder.extend([d])
        datasetsMgr.selectAndReorder(reversed(newOrder))

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

        # Do the topSelection histos
        folder = opts.folder
        histoPaths = []
        histoList = datasetsMgr.getDataset(
            datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)
        hList = [x for x in histoList if "_Vs_" not in x]  # remove TH2
        histoPaths = [os.path.join(folder, h) for h in hList]
        skipMe = ["counters", "Weighting", "config", "configInfo"]
        for h in histoPaths:
            if h in skipMe:
                continue
            PlotMC(datasetsMgr, h, intLumi)
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setGridX(opts.gridX)
    style.setGridY(opts.gridY)

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)

    # Get list of eras, modes, and optimisation modes
    erasList = dsetMgrCreator.getDataEras()
    modesList = dsetMgrCreator.getSearchModes()
    optList = dsetMgrCreator.getOptimizationModes()
    sysVarList = dsetMgrCreator.getSystematicVariations()
    sysVarSrcList = dsetMgrCreator.getSystematicVariationSources()

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        if len(optList) < 1:
            optList.append("")
        else:
            pass
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json

        # Print PSets used for FakeBMeasurement
        if 0:
            datasetsMgr.printSelections()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ZJetsToQQ_HT600toInf" in d.getName():
                datasetsMgr.remove(d.getName())

            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
                if d.getName() not in opts.signal:
                    if not opts.acceptance:
                        datasetsMgr.remove(d.getName())

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            datasetsMgr.PrintInfo()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Get Luminosity
        # For acceptance plot use all available masses
        if opts.acceptance:
            opts.signal = []
            opts.signalMasses = []
            for d in datasetsMgr.getAllDatasets():
                if "ChargedHiggs" in d.getName():
                    #dName = d.getName().replace("_ext1", "").split("M_")
                    dName = d.getName().split("M_")
                    m = int(dName[-1])
                    opts.signal.append(d.getName())
                    opts.signalMasses.append(m)

        if opts.intLumi < 0:
            if "Data" in datasetsMgr.getAllDatasetNames():
                opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            else:
                opts.intLumi = 1.0

        # Merge EWK samples
        if 1:
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print post EWK-merge dataset summary
        datasetsMgr.PrintInfo()

        # Get the efficiency graphs
        hGraphList = []
        histoName = os.path.join(opts.folder, "counter")
        hGraphList, _kwargs = GetHistoGraphs(datasetsMgr, opts.folder,
                                             histoName)

        # Plot the histo graphs
        PlotHistoGraphs(hGraphList, _kwargs)

    Print(
        "All plots saved under directory %s" %
        (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) +
         ShellStyles.NormalStyle()), True)
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(False)
    style.setGridX(False)
    style.setGridY(False)

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab1)

    # Get list of eras, modes, and optimisation modes
    erasList = dsetMgrCreator.getDataEras()
    modesList = dsetMgrCreator.getSearchModes()
    optList = dsetMgrCreator.getOptimizationModes()
    sysVarList = dsetMgrCreator.getSystematicVariations()
    sysVarSrcList = dsetMgrCreator.getSystematicVariationSources()

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Get the datasets from the directory
        datasetsMgr1 = GetDatasetsFromDir(opts.mcrab1, opts)
        datasetsMgr2 = GetDatasetsFromDir(opts.mcrab2, opts)
        datasetsMgr3 = GetDatasetsFromDir(opts.mcrab3, opts)

        # Setup the dataset managers
        datasetsMgr1.updateNAllEventsToPUWeighted()
        datasetsMgr1.loadLuminosities()  # from lumi.json
        datasetsMgr2.updateNAllEventsToPUWeighted()
        datasetsMgr2.loadLuminosities()  # from lumi.json
        datasetsMgr3.updateNAllEventsToPUWeighted()
        datasetsMgr3.loadLuminosities()  # from lumi.json

        # Print dataset info?
        if opts.verbose:

            datasetsMgr1.PrintCrossSections()
            datasetsMgr1.PrintLuminosities()

            datasetsMgr2.PrintCrossSections()
            datasetsMgr2.PrintLuminosities()

            datasetsMgr2.PrintCrossSections()
            datasetsMgr2.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr1)
        plots.mergeRenameReorderForDataMC(datasetsMgr2)
        plots.mergeRenameReorderForDataMC(datasetsMgr3)

        # Get Luminosity
        lumi1 = datasetsMgr1.getDataset("Data").getLuminosity()
        lumi2 = datasetsMgr2.getDataset("Data").getLuminosity()
        lumi3 = datasetsMgr3.getDataset("Data").getLuminosity()
        if lumi1 != lumi2 != lumi3:
            raise Exception("Lumi1 (=%.2f) != Lumi2 (=%.2f) != Lumi3 (=%.2f" %
                            (lumi1, lumi2, lumi3))
        else:
            opts.intLumi = datasetsMgr1.getDataset("Data").getLuminosity()

        # Merge EWK samples
        datasetsMgr1.merge("EWK", aux.GetListOfEwkDatasets())
        datasetsMgr2.merge("EWK", aux.GetListOfEwkDatasets())
        datasetsMgr3.merge("EWK", aux.GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr1.PrintInfo()
        if 0:
            datasetsMgr2.PrintInfo()
            datasetsMgr3.PrintInfo()

        # Get all the histograms and their paths (e.g. ForFakeBMeasurement/Baseline_DeltaRLdgTrijetBJetTetrajetBJet_AfterCRSelections)
        hList = datasetsMgr1.getDataset(
            datasetsMgr1.getAllDatasetNames()[0]).getDirectoryContent(
                opts.folder)
        hPaths = [os.path.join(opts.folder, h) for h in hList]

        # Create a smaller list with only histos of interest
        hListS = []
        for h in hList:
            if "StandardSelections" in h:
                continue
            if "IsGenuineB" in h:
                continue
            if "_Bjet" in h:
                continue
            if "_Jet" in h:
                continue
            if "_SubLdg" in h:
                continue
            if "_Njets" in h:
                continue
            if "_NBjets" in h:
                continue
            if "_Delta" in h:
                continue
            if "Dijet" in h:
                continue
            if "Bdisc" in h:
                continue
            #if "MVA" in h:
            #    continue
            if "MET" in h:
                continue
            if "HT" in h:
                continue
            # Otherwise keep the histogram
            hListS.append(h)

        hPathsS = [os.path.join(opts.folder, h) for h in hListS]

        # Create two lists of paths: one for "Baseline" (SR)  and one for "Inverted" (CR)
        path_SR = []  # baseline, _AfterAllSelections
        path_CR1 = []  # baseline, _AfterCRSelections
        path_VR = []  # inverted, _AfterAllSelections
        path_CR2 = []  # inverted, _AfterCRSelections

        # For-loop: All histogram paths
        for p in hPathsS:  #hPaths:
            if "Baseline" in p:
                if "AllSelections" in p:
                    path_SR.append(p)
                if "CRSelections" in p:
                    path_CR1.append(p)
            if "Inverted" in p:
                if "AllSelections" in p:
                    path_VR.append(p)
                if "CRSelections" in p:
                    path_CR2.append(p)

        counter = 1
        # For-loop: All histogram pairs
        for hCR1, hCR2 in zip(path_CR1, path_CR2):
            if "IsGenuineB" in hCR1:
                continue
            #hName = hCR1.replace("_AfterCRSelections", "_CR1vCR2").replace("ForFakeBMeasurement/Baseline_", "")
            hName = hCR1.replace("_AfterCRSelections",
                                 " (CR1 and R2)").replace(
                                     "ForFakeBMeasurement/Baseline_", "")
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format(
                "Histogram", "%i" % counter, "/", "%s:" % (len(path_CR1)),
                hName)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(),
                  counter == 1)

            PlotComparison(datasetsMgr1, datasetsMgr2, datasetsMgr3, hCR1,
                           hCR2, "CR1")
            PlotComparison(datasetsMgr1, datasetsMgr2, datasetsMgr3, hCR1,
                           hCR2, "CR2")  #iro
            counter += 1

        # WARNING! This unblinds the Signal Region (SR)
        for hSR, hVR in zip(path_SR, path_VR):
            if "IsGenuineB" in hSR:
                continue
            if 1:
                continue
            #hName = hCR1.replace("_AfterCRSelections", "_SRvVR").replace("ForFakeBMeasurement/Baseline_", "")
            hName = hCR1.replace("_AfterCRSelections", " (SR and VR)").replace(
                "ForFakeBMeasurement/Baseline_", "")
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format(
                "Histogram", "%i" % counter, "/", "%s:" % (len(path_CR1)),
                hName)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(),
                  counter == 1)

            PlotComparison(datasetsMgr1, datasetsMgr2, datasetsMgr3, hSR, hVR,
                           "SR")
            PlotComparison(datasetsMgr1, datasetsMgr2, datasetsMgr3, hSR, hVR,
                           "VR")
            counter += 1

    Print(
        "All plots saved under directory %s" %
        (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) +
         ShellStyles.NormalStyle()), True)
    return
Exemple #11
0
def main(opts):

    optModes = [""]
    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities(fname="lumi.json")

        # Get Luminosity
        if opts.intLumi < 0:
            if "Data" in datasetsMgr.getAllDatasetNames():
                opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            else:
                opts.intLumi = datasetsMgr.loadLumi()

        # Set/Overwrite cross-sections
        datasetsToRemove = []
        for d in datasetsMgr.getAllDatasets():
            mass = "M_%s" % (opts.signalMass)
            if mass in d.getName():
                if ("%s" % opts.signalMass) != d.getName().split("M_")[-1]:
                    datasetsMgr.remove(d.getName())
                else:
                    datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
            else:
                #datasetsToRemove.append(d.getName())
                datasetsMgr.remove(d.getName())

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        #         # Custom Filtering of datasets
        #         for i, d in enumerate(datasetsToRemove, 0):
        #             msg = "Removing dataset %s" % d
        #             Verbose(ShellStyles.WarningLabel() + msg + ShellStyles.NormalStyle(), i==0)
        #             datasetsMgr.remove(filter(lambda name: d == name, datasetsMgr.getAllDatasetNames()))

        if opts.verbose:
            datasetsMgr.PrintInfo()

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Apply TDR style
        style = tdrstyle.TDRStyle()
        style.setOptStat(True)
        style.setGridX(opts.gridX)
        style.setGridY(opts.gridY)

        # Do Data-MC histograms with DataDriven QCD
        folder = opts.folder
        histoList = datasetsMgr.getDataset(
            datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)
        histoPaths = [os.path.join(folder, h) for h in histoList]
        keepList = ["LdgTetrajetMass_AfterAllSelections"]
        #keepList   = ["LdgTetrajetMass_AfterStandardSelections"]
        myHistos = []
        for h in histoPaths:
            if h.split("/")[-1] not in keepList:
                continue
            else:
                myHistos.append(h)

        for i, h in enumerate(myHistos, 1):
            PlotHistograms(datasetsMgr, h)

    Print(
        "All plots saved under directory %s" %
        (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) +
         ShellStyles.NormalStyle()), True)
    return
def main(opts):
    Verbose("main function")

    comparisonList = ["AfterStdSelections"]

    # Setup & configure the dataset manager
    datasetsMgr = GetDatasetsFromDir(opts)
    datasetsMgr.updateNAllEventsToPUWeighted()
    datasetsMgr.loadLuminosities()  # from lumi.json

    # Set/Overwrite cross-sections
    for d in datasetsMgr.getAllDatasets():
        if "ChargedHiggs" in d.getName():
            print d.getName()
            datasetsMgr.getDataset(d.getName()).setCrossSection(10.0)

    if opts.verbose:
        datasetsMgr.PrintCrossSections()
        datasetsMgr.PrintLuminosities()

    # Check multicrab consistency
    if 0:
        consistencyCheck.checkConsistencyStandalone(dirs[0],
                                                    datasets,
                                                    name="CorrelationAnalysis")

    # Custom Filtering of datasets
    if 0:
        datasetsMgr.remove(
            filter(lambda name: "ST" in name,
                   datasetsMgr.getAllDatasetNames()))

    # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
    plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Get Integrated Luminosity
    if opts.mcOnly:
        # Determine integrated lumi
        if opts.intLumi < 0.0:
            opts.intLumi = GetLumi(datasetsMgr)
        else:
            pass
        # Remove data datasets
        datasetsMgr.remove(
            filter(lambda name: "Data" in name,
                   datasetsMgr.getAllDatasetNames()))

    # Re-order datasets (different for inverted than default=baseline)
    newOrder = ["Data"]
    newOrder.extend(["ChargedHiggs_HplusTB_HplusToTB_M_500"])
    newOrder.extend(GetListOfEwkDatasets())
    datasetsMgr.selectAndReorder(newOrder)

    if opts.mcOnly:
        newOrder.remove("Data")

    # Merge EWK samples
    if opts.mergeEWK:
        datasetsMgr.merge("EWK", GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

    # Print dataset information
    datasetsMgr.PrintInfo()

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

    # Do the standard top-selections
    analysisTypes = ["Baseline", "Inverted"]
    for analysis in analysisTypes:
        Print("Plotting Top Selection Histograms (%s)" % (analysis), True)
        TopSelectionHistograms(opts, datasetsMgr, analysis)

    # Do Data-MC histograms
    #for analysis in analysisTypes:
    #    Print("Plotting Other Histograms (%s)" % (analysis), True)
    #    DataMCHistograms(datasetsMgr, analysis)
    return
def PlotAndFitTemplates(datasetsMgr, histoName, inclusiveFolder, opts):
    Verbose("PlotAndFitTemplates()")

    # Variable definition
    bkgName = "QCD"
    genuineBFolder = inclusiveFolder + "EWKGenuineB"
    fakeBFolder = inclusiveFolder + "EWKFakeB"
    baselineHisto = "%s/%s" % (inclusiveFolder, "Baseline_" + histoName)
    invertedHisto = "%s/%s" % (inclusiveFolder, "Inverted_" + histoName)

    # Create the histograms
    pBaseline = plots.DataMCPlot(datasetsMgr, baselineHisto)
    pInverted = plots.DataMCPlot(datasetsMgr, invertedHisto)

    # Get the desired histograms (Baseline)
    Data_baseline = pBaseline.histoMgr.getHisto("Data").getRootHisto().Clone(
        "Baseline Data")  #also legend entry name
    FakeB_baseline = pBaseline.histoMgr.getHisto("Data").getRootHisto().Clone(
        "Baseline " + bkgName)
    EWK_baseline = pBaseline.histoMgr.getHisto("EWK").getRootHisto().Clone(
        "Baseline EWK")

    # Get the desired histograms (Inverted)
    # Data_inverted  = pInverted.histoMgr.getHisto("Data").getRootHisto().Clone("Inverted Data") #not needed
    FakeB_inverted = pInverted.histoMgr.getHisto("Data").getRootHisto().Clone(
        "Inverted " + bkgName)
    EWK_inverted = pInverted.histoMgr.getHisto("EWK").getRootHisto().Clone(
        "Inverted EWK")

    # Subtact EWK-M Cfrom Data (QCD = Data - EWK)
    FakeB_baseline.Add(EWK_baseline, -1)
    FakeB_inverted.Add(EWK_inverted, -1)

    # Rebin the EWK-MC histo (significant fit improvement - opposite effect for QCD fit)
    EWK_baseline.RebinX(2)
    EWK_inverted.RebinX(2)
    FakeB_inverted.RebinX(1)
    if 0:
        print "+" * 100
        Print("FIXME: Optimal is 1 (i.e. bin width of 10 GeV/c^{2})")
        print "+" * 100
        FakeB_inverted.RebinX(2)

    # Create the final plot object
    compareHistos = [EWK_baseline]
    p = plots.ComparisonManyPlot(FakeB_inverted, compareHistos, saveFormats=[])

    # Apply styles
    p.histoMgr.forHisto("Inverted " + bkgName, styles.getFakeBStyle())
    p.histoMgr.forHisto("Baseline EWK", styles.getAltEWKStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle("Inverted " + bkgName, "P")
    p.histoMgr.setHistoDrawStyle("Baseline EWK", "AP")

    # Set legend style
    p.histoMgr.setHistoLegendStyle("Inverted " + bkgName, "P")
    p.histoMgr.setHistoLegendStyle("Baseline EWK", "LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
        "Baseline EWK": "EWK",
        "Inverted " + bkgName: "QCD",
    })

    #=========================================================================================
    # Set Minimizer Options
    #=========================================================================================
    '''
    https://root.cern.ch/root/htmldoc/guides/users-guide/FittingHistograms.html#the-th1fit-method
    https://root.cern.ch/root/html/src/ROOT__Math__MinimizerOptions.h.html#a14deB
    '''
    if 0:
        ROOT.Math.MinimizerOptions.SetDefaultMinimizer("Minuit", "Migrad")
        ROOT.Math.MinimizerOptions.SetDefaultStrategy(
            1)  # Speed = 0, Balance = 1, Robustness = 2
        ROOT.Math.MinimizerOptions.SetDefaultMaxFunctionCalls(
            5000)  # set maximum of function calls
        ROOT.Math.MinimizerOptions.SetDefaultMaxIterations(
            5000
        )  # set maximum iterations (one iteration can have many function calls)
    if 0:
        ROOT.Math.MinimizerOptions.SetDefaultMinimizer("Minuit", "Simplex")
        ROOT.Math.MinimizerOptions.SetDefaultMinimizer("Minuit", "Minimize")
        ROOT.Math.MinimizerOptions.SetDefaultMinimizer("Minuit",
                                                       "MigradImproved")
        ROOT.Math.MinimizerOptions.SetDefaultMinimizer("Minuit", "Scan")
        ROOT.Math.MinimizerOptions.SetDefaultMinimizer("Minuit", "Seek")
        ROOT.Math.MinimizerOptions.SetDefaultErrorDef(
            1
        )  # error definition (=1. for getting 1 sigma error for chi2 fits)
        ROOT.Math.MinimizerOptions.SetDefaultMaxFunctionCalls(
            1000000)  # set maximum of function calls
        ROOT.Math.MinimizerOptions.SetDefaultMaxIterations(
            1000000
        )  # set maximum iterations (one iteration can have many function calls)
        ROOT.Math.MinimizerOptions.SetDefaultPrecision(
            -1
        )  # precision in the objective function calculation (value <= 0 means left to default)
        ROOT.Math.MinimizerOptions.SetDefaultPrintLevel(
            1
        )  # None = -1, Reduced = 0, Normal = 1, ExtraForProblem = 2, Maximum = 3
        ROOT.Math.MinimizerOptions.SetDefaultTolerance(
            1e-03
        )  # Minuit/Minuit2 converge when the EDM is less a given tolerance. (default 1e-03)
    if 1:
        hLine = "=" * 45
        title = "{:^45}".format("Minimizer Options")
        print "\t", hLine
        print "\t", title
        print "\t", hLine
        minOpt = ROOT.Math.MinimizerOptions()
        minOpt.Print()
        print "\t", hLine, "\n"

    #=========================================================================================
    # Start fit process
    #=========================================================================================
    binLabels = ["Inclusive"]
    moduleInfoString = opts.optMode  #opts.dataEra + "_" + opts.searchMode + "_" + opts.optMode

    #=========================================================================================
    # Create templates (EWK fakes, EWK genuine, QCD; data template is created by manager)
    #=========================================================================================
    manager = QCDNormalization.QCDNormalizationManagerDefault(
        binLabels, opts.mcrab, moduleInfoString)

    Print(
        "Creating the normalization templates with appropriate template names",
        True)
    template_EWKFakeB_Baseline = manager.createTemplate(
        "EWKFakeB_Baseline")  #fixme-unused
    template_EWKFakeB_Inverted = manager.createTemplate(
        "EWKFakeB_Inverted")  #fixme-unused

    template_EWKInclusive_Baseline = manager.createTemplate(
        "EWKInclusive_Baseline")
    template_EWKInclusive_Inverted = manager.createTemplate(
        "EWKInclusive_Inverted")

    template_FakeB_Baseline = manager.createTemplate("QCD_Baseline")
    template_FakeB_Inverted = manager.createTemplate("QCD_Inverted")

    #========================================================================================
    # EWK
    #=========================================================================================
    '''
    Optimal fit (Chi2/D.O.F = 3.2)
    FITMIN  ;  80
    FITMAX  : 800
    BinWidth:  10 GeV/c^2
    par3    : -3.9174e-01 (fixed)
    '''
    Print("Setting the fit-function to the EWK template", False)
    FITMIN_EWK = 80
    FITMAX_EWK = 800
    par0 = [+7.1817e-01, 0.0, 1.0]  # cb_norm
    par1 = [+1.7684e+02, 150.0, 200.0]  # cb_mean
    par2 = [+2.7287e+01, 20.0, 40.0]  # cb_sigma (fixed for chiSq=2)
    par3 = [-3.9174e-01, -1.0, 0.0]  # cb_alpha (fixed for chiSq=2)
    par4 = [+2.5104e+01, 0.0, 50.0]  # cb_n
    par5 = [+7.4724e-05, 0.0, 1.0]  # expo_norm
    par6 = [-4.6848e-02, -1.0, 0.0]  # expo_a
    par7 = [+2.1672e+02, 200.0, 250.0]  # gaus_mean (fixed for chiSq=2)
    par8 = [+6.3201e+01, 20.0, 80.0]  # gaus_sigma
    template_EWKInclusive_Baseline.setFitter(
        QCDNormalization.FitFunction("EWKFunction",
                                     boundary=0,
                                     norm=1,
                                     rejectPoints=0), FITMIN_EWK, FITMAX_EWK)
    template_EWKInclusive_Baseline.setDefaultFitParam(
        defaultInitialValue=None,
        defaultLowerLimit=[
            par0[1], par1[1], par2[1], par3[0], par4[1], par5[1], par6[1],
            par7[1], par8[1]
        ],
        defaultUpperLimit=[
            par0[2], par1[2], par2[2], par3[0], par4[2], par5[2], par6[2],
            par7[2], par8[2]
        ])

    #=========================================================================================
    # QCD
    #=========================================================================================
    '''
    Optimal fit (Chi2/D.O.F = 25.6)
    FITMIN  :   80
    FITMAX  : 1000
    BinWidth:  5 GeV/c^2
    f_{QCD} : ~0.75118 +/- 0.00740 (data-driven plots looked great with this value)
    '''
    Print("Setting the fit-function to the QCD template", False)
    FITMIN_QCD = 90  #  120
    FITMAX_QCD = 1000  # 1000
    bPtochos = False
    if bPtochos:
        par0 = [0.92, 0.0, 1.0]  # lognorm_norm
        par1 = [220.00, 180.0, 500.0]  # lognorm_mean
        par2 = [1.44, 0.4, 10.0]  # lognorm_shape
        par3 = [0.0, 0.0, 1.0]  # exp_const
        par4 = [-0.01, -2.0, 0.0]  # exp_coeff
        par5 = [220.00, 100.0, 400.0]  # gaus_mean
        par6 = [40.00, 0.0, 60.0]  # gaus_sigma
        template_FakeB_Inverted.setFitter(
            QCDNormalization.FitFunction("QCDFunction",
                                         boundary=0,
                                         norm=1,
                                         rejectPoints=0), FITMIN_QCD,
            FITMAX_QCD)
        template_FakeB_Inverted.setDefaultFitParam(
            defaultInitialValue=[
                par0[0], par1[0], par2[0], par3[0], par4[0], par5[0], par6[0]
            ],
            defaultLowerLimit=[
                par0[1], par1[1], par2[1], par3[1], par4[1], par5[1], par6[1]
            ],
            defaultUpperLimit=[
                par0[2], par1[2], par2[2], par3[2], par4[2], par5[2], par6[2]
            ])
    else:
        # par0 = [9.2578e-01,   0.0 ,    1.0] # lognorm_norm
        # par1 = [2.3662e+02, 200.0 , 1000.0] # lognorm_mean
        # par2 = [1.4436e+00,   0.5,    10.0] # lognorm_shape
        # par3 = [2.2575e+02, 100.0 ,  500.0] # gaus_mean
        # par4 = [3.6716e+01,   0.0 ,   60.0] # gaus_sigma
        par0 = [0.92, 0.0, 1.0]  # lognorm_norm
        par1 = [220.00, 180.0, 500.0]  # lognorm_mean
        par2 = [1.44, 0.4, 10.0]  # lognorm_shape
        par3 = [220.00, 100.0, 400.0]  # gaus_mean
        par4 = [40.00, 0.0, 60.0]  # gaus_sigma
        template_FakeB_Inverted.setFitter(
            QCDNormalization.FitFunction("QCDFunctionAlt",
                                         boundary=0,
                                         norm=1,
                                         rejectPoints=0), FITMIN_QCD,
            FITMAX_QCD)
        template_FakeB_Inverted.setDefaultFitParam(
            defaultInitialValue=[par0[0], par1[0], par2[0], par3[0], par4[0]],
            defaultLowerLimit=[par0[1], par1[1], par2[1], par3[1], par4[1]],
            defaultUpperLimit=[par0[2], par1[2], par2[2], par3[2], par4[2]])

    #=========================================================================================
    # Set histograms to the templates
    #=========================================================================================
    Print("Adding the appropriate histogram to each of the the templates",
          False)
    template_EWKFakeB_Baseline.setHistogram(EWK_baseline, "Inclusive")  #fixme
    template_EWKFakeB_Inverted.setHistogram(EWK_inverted, "Inclusive")  #fixme

    template_EWKInclusive_Baseline.setHistogram(EWK_baseline, "Inclusive")
    template_EWKInclusive_Inverted.setHistogram(EWK_inverted, "Inclusive")

    template_FakeB_Baseline.setHistogram(FakeB_baseline, "Inclusive")
    template_FakeB_Inverted.setHistogram(FakeB_inverted, "Inclusive")

    #=========================================================================================
    # Fit individual templates to histogram "Data_baseline", with custom fit options
    #=========================================================================================
    fitOptions = "R L W 0 Q"  #"R B L W 0 Q M"
    FITMIN_DATA = 80
    FITMAX_DATA = 1000
    manager.calculateNormalizationCoefficients(Data_baseline, fitOptions,
                                               FITMIN_DATA, FITMAX_DATA)

    Verbose("Write the normalisation factors to a python file", True)
    fileName = os.path.join(
        opts.mcrab,
        "QCDInvertedNormalizationFactors%s.py" % (getModuleInfoString(opts)))
    manager.writeNormFactorFile(fileName, opts)

    # Not really needed to plot the histograms again
    if 1:
        saveName = histoName
        plots.drawPlot(
            p, saveName,
            **GetHistoKwargs(histoName))  #the "**" unpacks the kwargs_
        SavePlot(p, saveName, os.path.join(opts.saveDir, "Fit", opts.optMode))
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50", "OptChiSqrCutValue100"]
    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        if 0:
            datasetsMgr.printSelections()
            sys.exit()

        # Define datasets to remove by default
        QCD_list = ["QCD_HT700to1000", "QCD_HT50to100", "QCD_HT500to700", "QCD_HT300to500", 
                    "QCD_HT200to300", "QCD_HT2000toInf", "QCD_HT1500to2000", "QCD_HT100to200", "QCD_HT1000to1500"]
        QCDExt_list = [x+"_ext1" for x in QCD_list]
        datasetsToRemove = ["QCD-b"]
        # datasetsToRemove.extend(QCD_list)
        # datasetsToRemove.extend(QCDExt_list)

        # ZJets and DYJets overlap
        if "ZJetsToQQ_HT600toInf" in datasetsMgr.getAllDatasetNames() and "DYJetsToQQ_HT180" in datasetsMgr.getAllDatasetNames():
            Print("Cannot use both ZJetsToQQ and DYJetsToQQ due to duplicate events? Investigate. Removing ZJetsToQQ datasets for now ..", True)
            datasetsMgr.remove(filter(lambda name: "ZJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "DYJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
        
        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0) # ATLAS 13 TeV H->tb exclusion limits
                if d.getName() != opts.signal:
                    datasetsToRemove.append(d.getName())

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Custom Filtering of datasets 
        for i, d in enumerate(datasetsToRemove, 0):
            msg = "Removing dataset %s" % d
            Verbose(ShellStyles.WarningLabel() + msg + ShellStyles.NormalStyle(), i==0)
            datasetsMgr.remove(filter(lambda name: d == name, datasetsMgr.getAllDatasetNames()))

        if opts.verbose:
            datasetsMgr.PrintInfo()
  
        # Re-order datasets (different for inverted than default=baseline)
        newOrder = ["Data"]
        for i, d in enumerate(datasetsMgr.getAllDatasets(), 0):
            if d.isData():
                continue
            else:
                newOrder.append(d.getName())

        # Re-arrange dataset order?
        if 0:
            s = newOrder.pop( newOrder.index("noTop") )
            newOrder.insert(len(newOrder), s) #after "Data"

        # Move signal to top
        if opts.signal in newOrder:
            s = newOrder.pop( newOrder.index(opts.signal) )
            newOrder.insert(1, s)
        datasetsMgr.selectAndReorder(newOrder)
        
        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Apply TDR style
        style = tdrstyle.TDRStyle()
        style.setOptStat(True)
        style.setGridX(opts.gridX)
        style.setGridY(opts.gridY)

        # Do Data-MC histograms with DataDriven QCD
        folder     = opts.folder
        histoList  = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)        
        histoPaths = [os.path.join(folder, h) for h in histoList]
        ignoreList = ["Aplanarity", "Planarity", "Sphericity", "FoxWolframMoment", "Circularity", "ThirdJetResolution", "Centrality", "_Vs_"]
        myHistos   = []
        for h in histoPaths:
            skip = False

            # Skip unwanted histos
            for i in ignoreList:
                if i in h:
                    skip = True

            if skip:
                continue
            else:
                myHistos.append(h)

        for i, h in enumerate(myHistos, 1):
            # Plot the histograms!
            msg   = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i, "/", "%s:" % (len(myHistos)), h)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), i==1)

            DataMCHistograms(datasetsMgr, h)
        
    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)    
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(False)
    style.setGridX(True) #opts.gridX)
    style.setGridY(True) #opts.gridY)
    style.setLogX(False) #opts.logX)
    style.setLogY(False) #opts.logY)

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)

    # Get list of eras, modes, and optimisation modes
    erasList      = dsetMgrCreator.getDataEras()
    modesList     = dsetMgrCreator.getSearchModes()
    optList       = dsetMgrCreator.getOptimizationModes()
    sysVarList    = dsetMgrCreator.getSystematicVariations()
    sysVarSrcList = dsetMgrCreator.getSystematicVariationSources()

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        if len(optList) < 1:
            optList.append("")
        else:
            pass
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        if 0:
            datasetsMgr.printSelections()
        
        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            
        # Custom Filtering of datasets 
        if 0:
            datasetsMgr.remove(filter(lambda name: "Charged" in name and not "M_500" in name, datasetsMgr.getAllDatasetNames()))

        # ZJets and DYJets overlap!
        if "ZJetsToQQ_HT600toInf" in datasetsMgr.getAllDatasetNames() and "DYJetsToQQ_HT180" in datasetsMgr.getAllDatasetNames():
            Print("Cannot use both ZJetsToQQ and DYJetsToQQ due to duplicate events? Investigate. Removing ZJetsToQQ datasets for now ..", True)
            datasetsMgr.remove(filter(lambda name: "ZJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        datasetsMgr.PrintInfo()
   
        # Get Luminosity
        if opts.intLumi < 0.0:
            if "Data" in datasetsMgr.getAllDatasetNames():
                opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            else:
                opts.intLumi = 1.0

        # Merge EWK samples
        if opts.dataset == "EWK":
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Get all histogram names in the given ROOT folder
        histoNames = datasetsMgr.getAllDatasets()[0].getDirectoryContent(opts.folder)
        histoList_ = [os.path.join(opts.folder, h) for h in histoNames if "_SR" in h]
        histoList  = [h for h in histoList_ if "Pt" in h] + [h for h in histoList_ if "Eta" in h]

        # For-loop: All histos in SR
        nHistos = len(histoList)
        for i, h in enumerate(histoList, 1):

            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i,"/", "%s:" % (nHistos), h)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), i==1)
            # print "*** h = ", h
            PlotHistograms(datasetsMgr, h)
            #break

    # Inform user where the plots where saved
    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
Exemple #16
0
def EWKvQCD(datasetsMgr, histoName, analysisType=""):
    Verbose("Plotting EWK Vs QCD unity-normalised histograms for %s" %
            analysisType)

    # Sanity check
    IsBaselineOrInverted(analysisType)

    p1 = plots.ComparisonPlot(
        *getHistos(datasetsMgr, "Data", "topSelection_Baseline/%s" %
                   histoName, "topSelection_Inverted/%s" % histoName))
    p1.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

    p2 = plots.ComparisonPlot(
        *getHistos(datasetsMgr, "EWK", "topSelection_Baseline/%s" %
                   histoName, "topSelection_Inverted/%s" % histoName))
    p2.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

    # Get histos
    data = p1.histoMgr.getHisto(analysisType +
                                "-Data").getRootHisto().Clone(analysisType +
                                                              " -Data")
    EWK = p2.histoMgr.getHisto(analysisType +
                               "-EWK").getRootHisto().Clone(analysisType +
                                                            "-EWK")
    # Create QCD histos: QCD = Data-EWK
    QCD = p1.histoMgr.getHisto(analysisType +
                               "-Data").getRootHisto().Clone(analysisType +
                                                             "-QCD")
    QCD.Add(EWK, -1)

    # Normalize histograms to unit area
    QCD.Scale(1.0 / QCD.Integral())
    EWK.Scale(1.0 / EWK.Integral())

    # Create the final plot object
    p = plots.ComparisonManyPlot(QCD, [EWK],
                                 saveFormats=[])  #[".C", ".png", ".pdf"])
    p.setLuminosity(GetLumi(datasetsMgr))

    # Apply styles
    p.histoMgr.forHisto(analysisType + "-QCD", styles.getQCDLineStyle())
    p.histoMgr.forHisto(analysisType + "-EWK", styles.getAltEWKStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle(analysisType + "-QCD", "LP")
    p.histoMgr.setHistoLegendStyle(analysisType + "-QCD", "LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
        analysisType + "-QCD":
        analysisType + " (QCD)",
        analysisType + "-EWK":
        analysisType + " (EWK)",
    })

    # Append analysisType to histogram name
    saveName = histoName + "_" + analysisType

    # Draw the histograms #alex
    plots.drawPlot(p, saveName,
                   **GetHistoKwargs(histoName))  #the "**" unpacks the kwargs_

    # _kwargs = {"lessThan": True}
    # p.addCutBoxAndLine(cutValue=200, fillColor=ROOT.kRed, box=False, line=True, ***_kwargs)

    # Save plot in all formats
    saveDir = os.path.join(opts.saveDir, "EWKvQCD", opts.optMode)
    SavePlot(p, saveName, saveDir)
    return
Exemple #17
0
def main(opts):

    optModes = [
        "", "OptChiSqrCutValue40", "OptChiSqrCutValue60",
        "OptChiSqrCutValue80", "OptChiSqrCutValue100", "OptChiSqrCutValue120",
        "OptChiSqrCutValue140"
    ]

    if opts.optMode != None:
        optModes = [opts.Mode]

    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        if 0:
            datasetsMgr.remove(
                filter(lambda name: "Data" in name,
                       datasetsMgr.getAllDatasetNames()))

        # Re-order datasets (different for inverted than default=baseline)
        newOrder = [
            "Data"
        ]  #, "TT", "DYJetsToQQHT", "TTWJetsToQQ", "WJetsToQQ_HT_600ToInf", "SingleTop", "Diboson", "TTZToQQ", "TTTT"]
        newOrder.extend(GetListOfEwkDatasets())
        # newOrder.extend("M_500")
        datasetsMgr.selectAndReorder(newOrder)

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()
        else:
            Print(
                "Cannot draw the EWKvQCD histograms without the option --mergeEWK. Exit",
                True)
            sys.exit()

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the template comparisons
        analysisTypes = ["Baseline", "Inverted"]
        for analysis in analysisTypes:
            for hName in getTopSelectionHistos(opts.histoLevel, analysis):
                if "ldgtrijetmass_after" not in hName.lower():
                    continue
                EWKvQCD(datasetsMgr, hName.split("/")[-1], analysis)
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setGridX(False)
    style.setGridY(False)
    style.setOptStat(False)
    
    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)

    # Get list of eras, modes, and optimisation modes
    erasList      = dsetMgrCreator.getDataEras()
    modesList     = dsetMgrCreator.getSearchModes()
    optList       = dsetMgrCreator.getOptimizationModes()
    sysVarList    = dsetMgrCreator.getSystematicVariations()
    sysVarSrcList = dsetMgrCreator.getSystematicVariationSources()

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        if len(optList) < 1:
            optList.append("")
        else:
            pass
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        
        if 0:
            datasetsMgr.printSelections()
        
        # Print PSets used for FakeBMeasurement
        if 0:
            datasetsMgr.printSelections()
            PrintPSet("BJetSelection", datasetsMgr)
            PrintPSet("TopSelectionBDT", datasetsMgr)
            PrintPSet("FakeBMeasurement", datasetsMgr)
            sys.exit()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            datasetsMgr.PrintInfo()

        # Filter the datasets 
        datasetsMgr.remove(filter(lambda name: "Charged" in name, datasetsMgr.getAllDatasetNames()))
        # datasetsMgr.remove(filter(lambda name: "Charged" in name and not "M_500" in name, datasetsMgr.getAllDatasetNames()))

        # ZJets and DYJets overlap!
        if "ZJetsToQQ_HT600toInf" in datasetsMgr.getAllDatasetNames() and "DYJetsToQQ_HT180" in datasetsMgr.getAllDatasetNames():
            Print("Cannot use both ZJetsToQQ and DYJetsToQQ due to duplicate events? Investigate. Removing ZJetsToQQ datasets for now ..", True)
            datasetsMgr.remove(filter(lambda name: "ZJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Get Luminosity
        if opts.intLumi < 0:
            if "Data" in datasetsMgr.getAllDatasetNames():
                opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            else:
                opts.intLumi = 1.0

        # Re-order datasets (different for inverted than default=baseline)
        if 0:
            newOrder = ["Data"]
            newOrder.extend(aux.GetListOfEwkDatasets())
            datasetsMgr.selectAndReorder(newOrder)

        # Print post-merged data dataset summary
        if 0:
            datasetsMgr.PrintInfo()

        # Merge EWK samples
        datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()
            
        # Print post EWK-merge dataset summary
        if 1:
            datasetsMgr.PrintInfo()

        # Get all histograms from the  in the selected folder inside the ROOT files 
        allHistos = datasetsMgr.getAllDatasets()[0].getDirectoryContent(opts.folder)
        hList     = [h for h in allHistos if "CRSelections" in h and "_Vs" not in h]
        hList.extend([h for h in allHistos if "AllSelections" in h and "_Vs" not in h])
        # hList.extend([h for h in allHistos if "StandardSelections" in h and "_Vs" not in h])

        # Create a list with strings included in the histogram names you want to plot
        myHistos = ["LdgTrijetPt", "LdgTrijetMass",  "TetrajetBJetPt", "TetrajetBJetEta", "LdgTetrajetPt", "LdgTetrajetMass", "MVAmax2", "MVAmax1", "HT", "MET"]
        
        # For-loop: All histos
        for i, h in enumerate(myHistos, 1):
            hGraphList = []
            for b in ["Baseline_", "Inverted_"]:
                for r in [ "_AfterCRSelections", "_AfterAllSelections"]:
                    histoName = b + h + r
                    hgQCD, kwargs = GetPurityHistoGraph(datasetsMgr, opts.folder, histoName)

                    # Do not draw SR in multigraph plot!
                    if GetControlRegionLabel(histoName) != "SR":
                        hGraphList.append(hgQCD)

                    # Plot individual purity graphs?
                    if 0:
                        PlotHistoGraph(hgQCD, kwargs)

            msg   = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i, "/", "%s:" % (len(myHistos)), h)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), i==1)
            PlotHistoGraphs(hGraphList, kwargs)
    
    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setGridX(opts.gridX)
    style.setGridY(opts.gridY)
    style.setOptStat(False)

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)
    
    # Get list of eras, modes, and optimisation modes
    erasList      = dsetMgrCreator.getDataEras()
    modesList     = dsetMgrCreator.getSearchModes()
    optList       = dsetMgrCreator.getOptimizationModes()
    sysVarList    = dsetMgrCreator.getSystematicVariations()
    sysVarSrcList = dsetMgrCreator.getSystematicVariationSources()

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        if 0:
            datasetsMgr.printSelections()
            # PrintPSet("BJetSelection", datasetsMgr)
            # PrintPSet("TopSelectionBDT", datasetsMgr)
            # PrintPSet("FakeBMeasurement", datasetsMgr)
            sys.exit()

        # Print dataset info?
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set signal cross-section
        if 0:
            datasetsMgr.getDataset(opts.signal).setCrossSection(1.0)

        # Remove unwanted datasets
        if 0:
            datasetsMgr.remove(filter(lambda name: "QCD-b" in name, datasetsMgr.getAllDatasetNames()))
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Get Luminosity
        opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
   
        # Merge EWK samples
        datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Get all the histograms and their paths (e.g. ForFakeBMeasurement/Baseline_DeltaRLdgTrijetBJetTetrajetBJet_AfterCRSelections)
        hList  = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(opts.folder)
        hPaths = [os.path.join(opts.folder, h) for h in hList]

        # Create two lists of paths: one for "Baseline" (SR)  and one for "Inverted" (CR)
        path_SR  = []  # baseline, _AfterAllSelections
        path_CR1 = []  # baseline, _AfterCRSelections
        path_VR  = []  # inverted, _AfterAllSelections
        path_CR2 = []  # inverted, _AfterCRSelections
        
        # For-loop: All histogram paths
        for p in hPaths:
            if "AfterStandardSelections" in p:
                continue
            
            if "Baseline" in p:
                if "AllSelections" in p:
                    path_SR.append(p)
                if "CRSelections" in p:
                    path_CR1.append(p)
            if "Inverted" in p:
                if "AllSelections" in p:
                    path_VR.append(p)
                if "CRSelections" in p:
                    path_CR2.append(p)

        # For-loop: All histogram pairs
        for hVR, hCR2, hCR1 in zip(path_VR, path_CR2, path_CR1):
            break
            if "IsGenuineB" in hVR:
                continue
            PlotComparison(datasetsMgr, hVR, hCR2, "VRvCR2")

        # For-loop: All histogram pairs
        counter = 1
        for hCR1, hCR2 in zip(path_CR1, path_CR2):
            if "IsGenuineB" in hCR1:
                continue
            
            hName = hCR1.replace("_AfterCRSelections", "_CR1vCR2").replace("ForFakeBMeasurement/Baseline_", "")
            msg   = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % counter, "/", "%s:" % (len(path_CR1)), hName)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), counter==1)
            PlotComparison(datasetsMgr, hCR1, hCR2, "CR1vCR2")
            counter+=1

        # For-loop: All histogram pairs
        for hSR, hVR in zip(path_SR, path_VR):
            break
            # Print("UNBLINDING SR! Are you nuts ? BREAK!", False)
            if "IsGenuineB" in hSR:
                continue
            PlotComparison(datasetsMgr, hSR, hVR, "SRvVR")

    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
def main(opts):

    optModes = ["OptChiSqrCutValue100"]                                                                                                                             

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Determine integrated Lumi before removing data
        intLumi = datasetsMgr.getDataset("Data").getLuminosity()

        # Remove datasets
        if 1:
            datasetsMgr.remove(filter(lambda name: "Data" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "QCD-b" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "SingleTop" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "DYJetsToQQHT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "TTZToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TTWJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "WJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "Diboson" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "TTTT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "FakeBMeasurementTrijetMass" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "M_" in name and "M_" + str(opts.signalMass) not in name, datasetsMgr.getAllDatasetNames()))

        # Re-order datasets
        if 1:
            #newOrder = ["QCD", "TT", "TTZToQQ", opts.signalMass]
            newOrder = ["QCD", "TT", opts.signalMass]
            newOrder.reverse()
            #newOrder.extend(GetListOfEwkDatasets())
            datasetsMgr.selectAndReorder(newOrder)

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the topSelection histos
        folder     = opts.folder 
        histoList  = datasetsMgr.getDataset("QCD").getDirectoryContent(folder)
        histoPaths = [os.path.join(folder, h) for h in histoList]
        for h in histoPaths:
            #print h
            if "Vs" in h: #skip TH2
                continue
            if "JetEtaPhi" in h: #skip TH2
                continue
            PlotMC(datasetsMgr, h, intLumi)
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue40", "OptChiSqrCutValue60", "OptChiSqrCutValue80", "OptChiSqrCutValue100", "OptChiSqrCutValue120", "OptChiSqrCutValue140"]
    optModes = [
        "OptChiSqrCutValue250", "OptChiSqrCutValue150", "OptChiSqrCutValue200",
        "OptChiSqrCutValue180", "OptChiSqrCutValue300"
    ]

    if opts.optMode != None:
        optModes = [opts.Mode]

    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        if 0:
            datasetsMgr.remove(
                filter(lambda name: "ST" in name,
                       datasetsMgr.getAllDatasetNames()))

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Get Integrated Luminosity
        if 0:
            datasetsMgr.remove(
                filter(lambda name: "Data" in name,
                       datasetsMgr.getAllDatasetNames()))

        # Re-order datasets (different for inverted than default=baseline)
        newOrder = ["Data"]
        newOrder.extend(GetListOfEwkDatasets())
        datasetsMgr.selectAndReorder(newOrder)

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()
        else:
            Print(
                "Cannot draw the histograms without the option --mergeEWK. Exit",
                True)

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the template comparisons
        for hName in getTopSelectionHistos(opts.histoLevel, "Baseline"):
            PlotBaselineVsInvertedTemplates(datasetsMgr,
                                            hName.split("/")[-1],
                                            addQcdBaseline=False)
    return
def main(opts):

    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(False)
    style.setGridX(opts.gridX)
    style.setGridY(opts.gridY)
    style.setLogX(opts.logX)
    style.setLogY(opts.logY)
    style.setLogZ(opts.logZ)


    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            datasetsMgr.PrintInfo()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Print merged datasets and MC samples
        if 0:
            datasetsMgr.PrintInfo()

        # Get Luminosity
        if opts.intLumi < 0:
            if "Data" in datasetsMgr.getAllDatasetNames():
                opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            else:
                opts.intLumi = 1.0
         
        # Set/Overwrite cross-sections. Remove all but 1 signal mass
        removeList = []
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
                if d.getName() != opts.signal:
                    removeList.append(d.getName())

        # Custom filtering of datasets
        for i, d in enumerate(removeList, 1):
            msg = "Removing datasets %s from dataset manager" % (ShellStyles.NoteStyle() + d + ShellStyles.NormalStyle())
            Verbose(msg, i==1)
            datasetsMgr.remove(filter(lambda name: d == name, datasetsMgr.getAllDatasetNames()))


        # Merge EWK samples
        if opts.mergeEWK == "EWK":
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        if 1:
            datasetsMgr.PrintInfo()
            
        # Get list of histogram paths
        folder     = opts.folder
        histoList  = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)
        histoPaths = [os.path.join(folder, h) for h in histoList]

        # For-loop: All histograms
        for i, h in enumerate(histoPaths, 1):
            if "_Vs_" in h:
                continue

            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i, "/", "%s:" % (len(histoPaths)), h)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), i==1)
            PlotHistograms(datasetsMgr, h)
            break

    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setGridX(False)
    style.setGridY(False)
    style.setOptStat(False)

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)

    # Get list of eras, modes, and optimisation modes
    erasList = dsetMgrCreator.getDataEras()
    modesList = dsetMgrCreator.getSearchModes()
    optList = dsetMgrCreator.getOptimizationModes()
    sysVarList = dsetMgrCreator.getSystematicVariations()
    sysVarSrcList = dsetMgrCreator.getSystematicVariationSources()

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json

        if 0:
            datasetsMgr.printSelections()
            # PrintPSet("BJetSelection", datasetsMgr)
            # PrintPSet("TopSelectionBDT", datasetsMgr)
            # PrintPSet("FakeBMeasurement", datasetsMgr)
            sys.exit()

        # Print dataset info?
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set signal cross-section
        if 0:
            datasetsMgr.getDataset(opts.signal).setCrossSection(1.0)

        # Remove unwanted datasets
        if 0:
            datasetsMgr.remove(
                filter(lambda name: "QCD-b" in name,
                       datasetsMgr.getAllDatasetNames()))

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Get Luminosity
        opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()

        # Merge EWK samples
        datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Get all the histograms and their paths (e.g. ForFakeBMeasurement/Baseline_DeltaRLdgTrijetBJetTetrajetBJet_AfterCRSelections)
        hList = datasetsMgr.getDataset(
            datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(
                opts.folder)
        hPaths = [os.path.join(opts.folder, h) for h in hList]

        # Create two lists of paths: one for "Baseline" (SR)  and one for "Inverted" (CR)
        path_SR = []  # baseline, _AfterAllSelections
        path_CR1 = []  # baseline, _AfterCRSelections
        path_VR = []  # inverted, _AfterAllSelections
        path_CR2 = []  # inverted, _AfterCRSelections

        # For-loop: All histogram paths
        for p in hPaths:
            if "AfterStandardSelections" in p:
                #print p
                continue

            if "Baseline" in p:
                if "AllSelections" in p:
                    path_SR.append(p)
                if "CRSelections" in p:
                    path_CR1.append(p)
            if "Inverted" in p:
                if "AllSelections" in p:
                    path_VR.append(p)
                if "CRSelections" in p:
                    path_CR2.append(p)

        # For-loop: All histogram pairs
        for hVR, hCR2, hCR1 in zip(path_VR, path_CR2, path_CR1):
            break  # not needed now
            if "IsGenuineB" in hVR:
                continue
            PlotComparison(datasetsMgr, hVR, hCR2, "VRvCR2")

        # For-loop: All histogram pairs
        counter = 1
        for hCR1, hCR2 in zip(path_CR1, path_CR2):
            if "IsGenuineB" in hCR1:
                continue

            hName = hCR1.replace("_AfterCRSelections", "_CR1vCR2").replace(
                "ForFakeBMeasurement/Baseline_", "")
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format(
                "Histogram", "%i" % counter, "/", "%s:" % (len(path_CR1)),
                hName)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(),
                  counter == 1)
            PlotComparison(datasetsMgr, hCR1, hCR2, "CR1vCR2")
            counter += 1

        # For-loop: All histogram pairs
        for hSR, hVR in zip(path_SR, path_VR):
            break
            Print("UNBLINDING SR! Are you nuts ? BREAK!", False)
            if "IsGenuineB" in hSR:
                continue
            PlotComparison(datasetsMgr, hSR, hVR, "SRvVR")


#        # For-loop: All histogram pairs
#        for hSR, hCR1 in zip(path_SR, path_CR1):
#            #break
#            Print("UNBLINDING SR! Are you nuts ? BREAK!", False)
#            raw_input("Press any key to continue")
#            if "IsGenuineB" in hSR:
#                continue
#            PlotComparison(datasetsMgr, hSR, hCR1, "SRvCR1")

    Print(
        "All plots saved under directory %s" %
        (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) +
         ShellStyles.NormalStyle()), True)
    return
def main(opts, signalMass):

    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Determine integrated Lumi before removing data
        #intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        intLumi = 5747.588 + 2573.399 + 4248.384 + 4008.663 + 2704.118 + 405.222 + 7539.457 + 8390.5 + 215.149

        # Remove datasets
        if 0:
            datasetsMgr.remove(filter(lambda name: "Data" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "QCD-b" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "QCD" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "SingleTop" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "DYJetsToQQHT" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TTZToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TTWJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "WJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "Diboson" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TTTT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "FakeBMeasurementTrijetMass" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "M_" in name and "M_" + str(opts.signalMass) not in name, datasetsMgr.getAllDatasetNames()))

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()


        # Re-order datasets (different for inverted than default=baseline)
        newOrder = []
        if opts.mergeEWK:
            newOrder = ["EWK", "QCD"]
        else:
            newOrder.extend(GetListOfEwkDatasets())
            newOrder.append("QCD")
        for d in datasetsMgr.getAllDatasetNames():
            if "ChargedHiggs" in d:
                newOrder.extend([d])        
        datasetsMgr.selectAndReorder( reversed(newOrder) )


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

        # Do the topSelection histos
        folder     = opts.folder
        histoPaths = []
        histoList  = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)
        hList      = [x for x in histoList if "_Vs_" not in x] # remove TH2
        histoPaths = [os.path.join(folder, h) for h in hList]
        skipMe     = ["counters", "Weighting", "config", "configInfo"]
        for h in histoPaths:
            if h in skipMe:
                continue
            PlotMC(datasetsMgr, h, intLumi)
    return
Exemple #25
0
def main(opts):

    # optModes = [""]
    # optModes = ["", "OptChiSqrCutValue50", "OptChiSqrCutValue100", "OptChiSqrCutValue200"]
    optModes = [""]  #,
    #"OptTriggerOR['HLT_PFHT400_SixJet30_DoubleBTagCSV_p056']",
    #"OptTriggerOR['HLT_PFHT450_SixJet40_BTagCSV_p056']"]
    #"OptInvertedBJetsDiscrMaxCutValue0p82",
    #"OptInvertedBJetsDiscrMaxCutValue0p8",
    #"OptInvertedBJetsDiscrMaxCutValue0p75",
    #"OptInvertedBJetsDiscrMaxCutValue0p7"]
    #"OptInvertedBJetsDiscrMaxCutValue1p0InvertedBJetsSortTypeRandom",
    #"OptInvertedBJetsDiscrMaxCutValue1p0InvertedBJetsSortTypeDescendingBDiscriminator",
    #"OptInvertedBJetsDiscrMaxCutValue0p8InvertedBJetsSortTypeRandom",
    #"OptInvertedBJetsDiscrMaxCutValue0p8InvertedBJetsSortTypeDescendingBDiscriminator"]
    #"OptInvertedBJetsDiscrMaxCutValue0p85InvertedBJetsSortTypeRandom",
    #"OptInvertedBJetsDiscrMaxCutValue0p85InvertedBJetsSortTypeDescendingBDiscriminator",
    #"OptInvertedBJetsDiscrMaxCutValue0p8InvertedBJetsSortTypeRandom",
    #"OptInvertedBJetsDiscrMaxCutValue0p8InvertedBJetsSortTypeDescendingBDiscriminator",
    #"OptInvertedBJetsDiscrMaxCutValue0p75InvertedBJetsSortTypeRandom",
    #"OptInvertedBJetsDiscrMaxCutValue0p75InvertedBJetsSortTypeDescendingBDiscriminator"]
    # "OptNumberOfInvertedBJetsCutValue1InvertedBJetsDiscrMaxCutValue1p0InvertedBJetsSortTypeRandom",
    # "OptNumberOfInvertedBJetsCutValue1InvertedBJetsDiscrMaxCutValue1p0InvertedBJetsSortTypeDescendingBDiscriminator",
    # "OptNumberOfInvertedBJetsCutValue1InvertedBJetsDiscrMaxCutValue0p8InvertedBJetsSortTypeRandom",
    # "OptNumberOfInvertedBJetsCutValue1InvertedBJetsDiscrMaxCutValue0p8InvertedBJetsSortTypeDescendingBDiscriminator",
    # "OptNumberOfInvertedBJetsCutValue1InvertedBJetsDiscrMaxCutValue0p75InvertedBJetsSortTypeRandom",
    # "OptNumberOfInvertedBJetsCutValue1InvertedBJetsDiscrMaxCutValue0p75InvertedBJetsSortTypeDescendingBDiscriminator"]

    #if opts.optMode != None:
    #    optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt
        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        # Remove dataset ?
        datasetsMgr.remove(
            filter(lambda name: "Charged" in name,
                   datasetsMgr.getAllDatasetNames()))
        if not opts.useMC:
            datasetsMgr.remove(
                filter(lambda name: "QCD" in name,
                       datasetsMgr.getAllDatasetNames()))

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Re-order datasets (different for inverted than default=baseline)
        newOrder = ["Data"]
        newOrder.extend(GetListOfEwkDatasets())
        if opts.useMC:
            newOrder.append("QCD")
        datasetsMgr.selectAndReorder(newOrder)

        # Merge EWK samples
        datasetsMgr.merge("EWK", GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # 1) Do the StandardSelections/AllSelections
        bType = ""
        folder = "ForFakeBMeasurement" + bType
        hList = datasetsMgr.getDataset(
            datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)
        hPaths = [os.path.join(folder, h) for h in hList]
        baselinePaths = []
        invertedPath = []
        if 1:
            for p in hPaths:
                if "Baseline" in p:
                    baselinePaths.append(p)
                if "Inverted" in p:
                    invertedPath.append(p)
            for hBaseline, hInverted in zip(baselinePaths, invertedPath):
                # print "--- Plotting histogram", hBaseline
                PlotBaselineVsInverted(datasetsMgr, hBaseline, hInverted)

        # 2) Do the topSelection histos
        if 0:
            analysisType = "Inverted"
            folder = "topSelection_%s" % (analysisType)
            invertedList = datasetsMgr.getDataset(
                datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(
                    folder)
            invertedPaths = [os.path.join(folder, h) for h in invertedList]
            baselinePaths = [
                p.replace("Inverted", "Baseline") for p in invertedPaths
            ]
            for hBaseline, hInverted in zip(baselinePaths, invertedPaths):
                if "Vs" in hBaseline:  #skip TH2
                    continue
                PlotBaselineVsInverted(datasetsMgr, hBaseline, hInverted)
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50p0", "OptChiSqrCutValue100p0", "OptChiSqrCutValue150p0", "OptChiSqrCutValue200p0"]
    optModes = ["OptChiSqrCutValue100"]

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                print d.getName()
                datasetsMgr.getDataset(d.getName()).setCrossSection(
                    1.0)  # ATLAS 13 TeV H->tb exclusion limits

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Check multicrab consistency
        if 0:
            consistencyCheck.checkConsistencyStandalone(
                dirs[0], datasets, name="CorrelationAnalysis")

        # Custom Filtering of datasets
        if 0:
            datasetsMgr.remove(
                filter(lambda name: "ST" in name,
                       datasetsMgr.getAllDatasetNames()))

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Re-order datasets (different for inverted than default=baseline)
        if 1:
            newOrder = ["Data"]
            newOrder.extend(["ChargedHiggs_HplusTB_HplusToTB_M_500"])
            newOrder.extend(["QCD"])
            newOrder.extend(GetListOfEwkDatasets())
            datasetsMgr.selectAndReorder(newOrder)

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the standard top-selections
        analysisTypes = ["Baseline", "Inverted"]
        for analysis in analysisTypes:
            Print("Plotting Top Selection Histograms (%s)" % (analysis), True)
            PlotSignal(datasetsMgr, analysis, opts)

    # Do Data-MC histograms
    # for analysis in analysisTypes:
    #    Print("Plotting Other Histograms (%s)" % (analysis), True)
    #    DataMCHistograms(datasetsMgr, analysis)
    return
def main(opts):

    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(False)
    style.setGridX(opts.gridX)
    style.setGridY(opts.gridY)
    style.setLogX(opts.logX)
    style.setLogY(opts.logY)
    style.setLogZ(opts.logZ)
    style.setWide(True, 0.15)
    # style.setPadRightMargin()#0.13)


    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            datasetsMgr.PrintInfo()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Print merged datasets and MC samples
        if 0:
            datasetsMgr.PrintInfo()

        # Get Luminosity
        if opts.intLumi < 0:
            opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            
        # Merge EWK samples
        if opts.dataset == "EWK":
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Re-order datasets (different for inverted than default=baseline)
        newOrder = []
        for d in datasetsMgr.getAllDatasets():
            if d.getName() == opts.dataset:
                newOrder.append(d.getName())
        
        # Sanity check on selected dataset
        nDatasets = len(newOrder)
        if nDatasets < 1:
            msg = "Please select a valid dataset. Dataset \"%s\" does not exist!" % (opts.dataset)
            Print(ShellStyles.ErrorStyle() + msg + ShellStyles.NormalStyle(), True)
            datasetsMgr.PrintInfo()
            sys.exit()
        if nDatasets > 1:
            msg = "Please select only 1 valid dataset. Requested %i datasets for plotting!" % (nDatasets)
            Print(ShellStyles.ErrorStyle() + msg + ShellStyles.NormalStyle(), True)
            datasetsMgr.PrintInfo()
            sys.exit()

        # Select only given dataset         
        datasetsMgr.selectAndReorder(newOrder)

        # Print dataset information
        msg = "Plotting for single dataset \"%s\". Integrated luminosity is %.2f 1/fb" % (opts.dataset, opts.intLumi)
        Print(ShellStyles.NoteStyle() + msg + ShellStyles.NormalStyle(), True)
        datasetsMgr.PrintInfo()
            
        # Get list of histogram paths
        folder     = opts.folder
        histoList  = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)
        histoPaths = [os.path.join(folder, h) for h in histoList]

        # For-loop: All histograms
        for h in histoPaths:
            if "_vs_" not in h.lower():
                continue
            Plot2dHistograms(datasetsMgr, h)

    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)    
    return
def main(opts):

    optModes = [""]
    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr    = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0) # ATLAS 13 TeV H->tb exclusion limits
            
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Apply TDR style
        style = tdrstyle.TDRStyle()
        style.setOptStat(True)
        style.setWide(True, 0.15)
# style.setPadRightMargin()#0.13)

        # Do 2D histos
        histoNames  = []
        saveFormats = [".png"] #[".C", ".png", ".pdf"]
        histoList   = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(opts.folder)
        histoPaths  = [opts.folder +"/" + h for h in histoList]
        #histoKwargs = GetHistoKwargs(histoPaths)

        # Axes divisions!
        ROOT.gStyle.SetNdivisions(5, "X")
        ROOT.gStyle.SetNdivisions(5, "Y")

        # For-loop: All histogram
        for histoName in histoPaths:

            myKwargs = GetHistoKwargs(histoName)
            
            # For-loop: All datasets
            for d in datasetsMgr.getAllDatasetNames():
                #if "M_500" not in d:
                #    continue

                Plot2d(datasetsMgr, d, histoName, myKwargs, opts)
                
                # Avoid replacing canvas with same name warning
                for o in gROOT.GetListOfCanvases():
                    # print o.GetName()
                    o.SetName(o.GetName() + "_" + d)
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50", "OptChiSqrCutValue100"]
    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        if 0:
            datasetsMgr.printSelections()
            sys.exit()

        # Define datasets to remove by default
        QCD_list = ["QCD_HT700to1000", "QCD_HT50to100", "QCD_HT500to700", "QCD_HT300to500", 
                    "QCD_HT200to300", "QCD_HT2000toInf", "QCD_HT1500to2000", "QCD_HT100to200", "QCD_HT1000to1500"]
        QCDExt_list = [x+"_ext1" for x in QCD_list]
        datasetsToRemove = ["QCD-b"]
        # datasetsToRemove.extend(QCD_list)
        # datasetsToRemove.extend(QCDExt_list)

        # ZJets and DYJets overlap
        if "ZJetsToQQ_HT600toInf" in datasetsMgr.getAllDatasetNames() and "DYJetsToQQ_HT180" in datasetsMgr.getAllDatasetNames():
            Print("Cannot use both ZJetsToQQ and DYJetsToQQ due to duplicate events? Investigate. Removing ZJetsToQQ datasets for now ..", True)
            datasetsMgr.remove(filter(lambda name: "ZJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "DYJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
        
        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0) # ATLAS 13 TeV H->tb exclusion limits
                if d.getName() != opts.signal:
                    datasetsToRemove.append(d.getName())

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Custom Filtering of datasets 
        for i, d in enumerate(datasetsToRemove, 0):
            msg = "Removing dataset %s" % d
            Verbose(ShellStyles.WarningLabel() + msg + ShellStyles.NormalStyle(), i==0)
            datasetsMgr.remove(filter(lambda name: d == name, datasetsMgr.getAllDatasetNames()))

        if opts.verbose:
            datasetsMgr.PrintInfo()
  
        # Re-order datasets (different for inverted than default=baseline)
        newOrder = ["Data"]
        for i, d in enumerate(datasetsMgr.getAllDatasets(), 0):
            if d.isData():
                continue
            else:
                newOrder.append(d.getName())

        # Re-arrange dataset order?
        if 0:
            s = newOrder.pop( newOrder.index("noTop") )
            newOrder.insert(len(newOrder), s) #after "Data"

        # Move signal to top
        if opts.signal in newOrder:
            s = newOrder.pop( newOrder.index(opts.signal) )
            newOrder.insert(1, s)
        datasetsMgr.selectAndReorder(newOrder)
        
        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Apply TDR style
        style = tdrstyle.TDRStyle()
        style.setOptStat(True)
        style.setGridX(opts.gridX)
        style.setGridY(opts.gridY)

        # Do Data-MC histograms with DataDriven QCD
        folder     = opts.folder
        histoList  = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)        
        histoPaths = [os.path.join(folder, h) for h in histoList]
        ignoreList = ["Aplanarity", "Planarity", "Sphericity", "FoxWolframMoment", "Circularity", "ThirdJetResolution", "Centrality", "_Vs_"]
        myHistos   = []
        for h in histoPaths:
            skip = False

            # Skip unwanted histos
            for i in ignoreList:
                if i in h:
                    skip = True

            if skip:
                continue
            else:
                myHistos.append(h)

        for i, h in enumerate(myHistos, 1):
            # Plot the histograms!
            msg   = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i, "/", "%s:" % (len(myHistos)), h)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), i==1)

            DataMCHistograms(datasetsMgr, h)
        
    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)    
    return
Exemple #30
0
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(False)
    style.setGridX(True) #opts.gridX)
    style.setGridY(True) #opts.gridY)
    style.setLogX(False) #opts.logX)
    style.setLogY(False) #opts.logY)

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)

    # Get list of eras, modes, and optimisation modes
    erasList      = dsetMgrCreator.getDataEras()
    modesList     = dsetMgrCreator.getSearchModes()
    optList       = dsetMgrCreator.getOptimizationModes()
    sysVarList    = dsetMgrCreator.getSystematicVariations()
    sysVarSrcList = dsetMgrCreator.getSystematicVariationSources()

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        if len(optList) < 1:
            optList.append("")
        else:
            pass
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        if 0:
            datasetsMgr.printSelections()
        
        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            
        # Custom Filtering of datasets 
        if 0:
            datasetsMgr.remove(filter(lambda name: "Charged" in name and not "M_500" in name, datasetsMgr.getAllDatasetNames()))

        # ZJets and DYJets overlap!
        if "ZJetsToQQ_HT600toInf" in datasetsMgr.getAllDatasetNames() and "DYJetsToQQ_HT180" in datasetsMgr.getAllDatasetNames():
            Print("Cannot use both ZJetsToQQ and DYJetsToQQ due to duplicate events? Investigate. Removing ZJetsToQQ datasets for now ..", True)
            datasetsMgr.remove(filter(lambda name: "ZJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        datasetsMgr.PrintInfo()
   
        # Get Luminosity
        if opts.intLumi < 0.0:
            if "Data" in datasetsMgr.getAllDatasetNames():
                opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            else:
                opts.intLumi = 1.0

        # Merge EWK samples
        if opts.dataset == "EWK":
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Get all histogram names in the given ROOT folder
        histoNames = datasetsMgr.getAllDatasets()[0].getDirectoryContent(opts.folder)
        # histoList  = [os.path.join(opts.folder, h) for h in histoNames if "_" + opts.region in h and opts.refBdisc in h]
        histoList  = [os.path.join(opts.folder, h) for h in histoNames if opts.refBdisc in h]

        # For-loop: All histos in CR of interest
        nHistos = len(histoList)
        for i, h in enumerate(histoList, 1):
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i,"/", "%s:" % (nHistos), h)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), i==1)
            PlotHistograms(datasetsMgr, h)

    # Inform user where the plots where saved
    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50p0", "OptChiSqrCutValue100p0", "OptChiSqrCutValue200p0"]
    optModes = [
        "", "OptInvertedBJetsDiscrMaxCutValue0p82",
        "OptInvertedBJetsDiscrMaxCutValue0p8",
        "OptInvertedBJetsDiscrMaxCutValue0p75",
        "OptInvertedBJetsDiscrMaxCutValue0p7"
    ]
    #"OptInvertedBJetsDiscrMaxCutValue1p0InvertedBJetsSortTypeRandom",
    #"OptInvertedBJetsDiscrMaxCutValue1p0InvertedBJetsSortTypeDescendingBDiscriminator",
    #"OptInvertedBJetsDiscrMaxCutValue0p8InvertedBJetsSortTypeRandom",
    #"OptInvertedBJetsDiscrMaxCutValue0p8InvertedBJetsSortTypeDescendingBDiscriminator"]

    #if opts.optMode != None:
    #   optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Custom Filtering of datasets
        if 0:
            datasetsMgr.remove(
                filter(lambda name: "Charged" in name and not "M_500" in name,
                       datasetsMgr.getAllDatasetNames()))

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Re-order datasets (different for inverted than default=baseline)
        if 0:
            newOrder = ["Data"]
            newOrder.extend(GetListOfEwkDatasets())
            datasetsMgr.selectAndReorder(newOrder)

        # Merge EWK samples
        datasetsMgr.merge("EWK", GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the Purity Triplets?
        if 0:
            bType = ""  # ["", "EWKFakeB", "EWKGenuineB"]
            folder = "FakeBPurity" + bType
            hList = datasetsMgr.getDataset("EWK").getDirectoryContent(folder)
            for hName in hList:
                PlotPurity(datasetsMgr, os.path.join(folder, hName))

        # Do the Std Selections Purity plots
        folder = "ForDataDrivenCtrlPlots"
        allHistos = datasetsMgr.getDataset("EWK").getDirectoryContent(folder)
        hList = [
            h for h in allHistos
            if "StandardSelections" in h and "_Vs" not in h
        ]
        hList.extend(
            [h for h in allHistos if "AllSelections" in h and "_Vs" not in h])

        # Only do these histos
        myHistos = [
            "Njets", "LdgTrijetMass", "TetrajetBjetPt", "LdgTetrajetMass",
            "LdgTetrajetMass"
        ]

        # For-loop: All histos
        for h in hList:

            if h.split("_")[0] not in myHistos:
                continue
            if "JetEtaPhi" in h:
                continue
            PlotPurity(datasetsMgr, os.path.join(folder, h))

    return
Exemple #32
0
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50", "OptChiSqrCutValue100"]
    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0) # ATLAS 13 TeV H->tb exclusion limits
                
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
   
        # Custom Filtering of datasets 
        datasetsMgr.remove(filter(lambda name: "QCD-b" in name, datasetsMgr.getAllDatasetNames()))
        # datasetsMgr.remove(filter(lambda name: "Charged" in name, datasetsMgr.getAllDatasetNames()))

        # Replace QCD MC with QCD-DataDriven
        qcdDatasetName    = "FakeBMeasurementTrijetMass"
        qcdDatasetNameNew = "QCD-Data"
        if opts.mcQCD:
            pass
        else:
            replaceQCDFromData(datasetsMgr, qcdDatasetName, qcdDatasetNameNew)

        # Re-order datasets (different for inverted than default=baseline)
        newOrder = ["Data"]
        if opts.signalMass != 0:
            signal = "ChargedHiggs_HplusTB_HplusToTB_M_%.0f" % opts.signalMass
            newOrder.extend([signal])
        if opts.mcQCD:
            newOrder.extend(["QCD"])
        else:
            newOrder.extend(["QCD-Data"])
        newOrder.extend(GetListOfEwkDatasets())
        datasetsMgr.selectAndReorder(newOrder)
        
        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do Data-MC histograms with DataDriven QCD
        DataMCHistograms(datasetsMgr, qcdDatasetNameNew)
    return
Exemple #33
0
def main(opts):

    optModes = ["", "OptNumberOfBJetsCutValue0", "OptNumberOfBJetsCutValue1"]

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Custom Filtering of datasets
        if 0:
            datasetsMgr.remove(
                filter(lambda name: "HplusTB" in name and not "M_500" in name,
                       datasetsMgr.getAllDatasetNames()))

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Re-order datasets (different for inverted than default=baseline)
        newOrder = [
            "Data"
        ]  #, "TT", "DYJetsToQQHT", "TTWJetsToQQ", "WJetsToQQ_HT_600ToInf", "SingleTop", "Diboson", "TTZToQQ", "TTTT"]
        newOrder.extend(GetListOfEwkDatasets())
        datasetsMgr.selectAndReorder(newOrder)

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        # Sanity check
        if not opts.mergeEWK:
            Print(
                "Cannot draw the Baseline Vs Inverted histograms without the option --mergeEWK. Exit",
                True)
            sys.exit()

        # Merge EWK samples
        datasetsMgr.merge("EWK", GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the Baseline Vs Inverted histograms
        folder = "ForFakeBMeasurement"
        hName = "%s/Inverted_TopMassReco_LdgTrijetM_AfterAllSelections" % folder
        DoPlots(datasetsMgr, hName)
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(False)
    style.setGridX(False)
    style.setGridY(False)

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab1)
    
    # Get list of eras, modes, and optimisation modes
    erasList      = dsetMgrCreator.getDataEras()
    modesList     = dsetMgrCreator.getSearchModes()
    optList       = dsetMgrCreator.getOptimizationModes()
    sysVarList    = dsetMgrCreator.getSystematicVariations()
    sysVarSrcList = dsetMgrCreator.getSystematicVariationSources()

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Get the datasets from the directory
        datasetsMgr1 = GetDatasetsFromDir(opts.mcrab1, opts)
        datasetsMgr2 = GetDatasetsFromDir(opts.mcrab2, opts)
        datasetsMgr3 = GetDatasetsFromDir(opts.mcrab3, opts)
        
        # Setup the dataset managers
        datasetsMgr1.updateNAllEventsToPUWeighted()
        datasetsMgr1.loadLuminosities() # from lumi.json
        datasetsMgr2.updateNAllEventsToPUWeighted()
        datasetsMgr2.loadLuminosities() # from lumi.json
        datasetsMgr3.updateNAllEventsToPUWeighted()
        datasetsMgr3.loadLuminosities() # from lumi.json

        # Print dataset info?
        if opts.verbose:

            datasetsMgr1.PrintCrossSections()
            datasetsMgr1.PrintLuminosities()

            datasetsMgr2.PrintCrossSections()
            datasetsMgr2.PrintLuminosities()

            datasetsMgr2.PrintCrossSections()
            datasetsMgr2.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr1) 
        plots.mergeRenameReorderForDataMC(datasetsMgr2) 
        plots.mergeRenameReorderForDataMC(datasetsMgr3) 
        
        # Get Luminosity
        lumi1 = datasetsMgr1.getDataset("Data").getLuminosity()
        lumi2 = datasetsMgr2.getDataset("Data").getLuminosity()
        lumi3 = datasetsMgr3.getDataset("Data").getLuminosity()
        if lumi1 != lumi2 != lumi3:
            raise Exception("Lumi1 (=%.2f) != Lumi2 (=%.2f) != Lumi3 (=%.2f" % (lumi1, lumi2, lumi3))
        else:
            opts.intLumi = datasetsMgr1.getDataset("Data").getLuminosity()
   
        # Merge EWK samples
        datasetsMgr1.merge("EWK", aux.GetListOfEwkDatasets())
        datasetsMgr2.merge("EWK", aux.GetListOfEwkDatasets())
        datasetsMgr3.merge("EWK", aux.GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr1.PrintInfo()
        if 0:
            datasetsMgr2.PrintInfo()
            datasetsMgr3.PrintInfo()

        # Get all the histograms and their paths (e.g. ForFakeBMeasurement/Baseline_DeltaRLdgTrijetBJetTetrajetBJet_AfterCRSelections)
        hList  = datasetsMgr1.getDataset(datasetsMgr1.getAllDatasetNames()[0]).getDirectoryContent(opts.folder)
        hPaths = [os.path.join(opts.folder, h) for h in hList]

        # Create a smaller list with only histos of interest
        hListS = []
        for h in hList:
            if "StandardSelections" in h:
                continue
            if "IsGenuineB" in h:
                continue
            if "_Bjet" in h:
                continue
            if "_Jet" in h:
                continue
            if "_SubLdg" in h:
                continue
            if "_Njets" in h:
                continue
            if "_NBjets" in h:
                continue
            if "_Delta" in h:
                continue
            if "Dijet" in h:
                continue
            if "Bdisc" in h:
                continue
            #if "MVA" in h:
            #    continue
            if "MET" in h:
                continue
            if "HT" in h:
                continue
            # Otherwise keep the histogram
            hListS.append(h)

        hPathsS = [os.path.join(opts.folder, h) for h in hListS]
        
        # Create two lists of paths: one for "Baseline" (SR)  and one for "Inverted" (CR)
        path_SR  = []  # baseline, _AfterAllSelections
        path_CR1 = []  # baseline, _AfterCRSelections
        path_VR  = []  # inverted, _AfterAllSelections
        path_CR2 = []  # inverted, _AfterCRSelections

        # For-loop: All histogram paths
        for p in hPathsS: #hPaths:
            if "Baseline" in p:
                if "AllSelections" in p:
                    path_SR.append(p)
                if "CRSelections" in p:
                    path_CR1.append(p)
            if "Inverted" in p:
                if "AllSelections" in p:
                    path_VR.append(p)
                if "CRSelections" in p:
                    path_CR2.append(p)

        counter = 1
        # For-loop: All histogram pairs
        for hCR1, hCR2 in zip(path_CR1, path_CR2):
            if "IsGenuineB" in hCR1:
                continue
            #hName = hCR1.replace("_AfterCRSelections", "_CR1vCR2").replace("ForFakeBMeasurement/Baseline_", "")
            hName = hCR1.replace("_AfterCRSelections", " (CR1 and R2)").replace("ForFakeBMeasurement/Baseline_", "")
            msg   = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % counter, "/", "%s:" % (len(path_CR1)), hName)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), counter==1)

            PlotComparison(datasetsMgr1, datasetsMgr2, datasetsMgr3, hCR1, hCR2, "CR1")
            PlotComparison(datasetsMgr1, datasetsMgr2, datasetsMgr3, hCR1, hCR2, "CR2") #iro
            counter+=1

        
        # WARNING! This unblinds the Signal Region (SR)        
        for hSR, hVR in zip(path_SR, path_VR):
            if "IsGenuineB" in hSR:
                continue
            if 1:
                continue
            #hName = hCR1.replace("_AfterCRSelections", "_SRvVR").replace("ForFakeBMeasurement/Baseline_", "")
            hName = hCR1.replace("_AfterCRSelections", " (SR and VR)").replace("ForFakeBMeasurement/Baseline_", "")
            msg   = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % counter, "/", "%s:" % (len(path_CR1)), hName)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), counter==1)

            PlotComparison(datasetsMgr1, datasetsMgr2, datasetsMgr3, hSR, hVR, "SR")
            PlotComparison(datasetsMgr1, datasetsMgr2, datasetsMgr3, hSR, hVR, "VR")
            counter+=1

    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
def main(opts, signalMass):
    
    print "Main Function"

    # Setup & configure the dataset manager 
    datasetsMgr = GetDatasetsFromDir(opts)
    datasetsMgr.updateNAllEventsToPUWeighted()
    
    # Get Luminosity
    datasetsMgr.loadLuminosities()
    
    # Print dataset cross sections
    datasetsMgr.PrintCrossSections()
    # Print luminosities
    datasetsMgr.PrintLuminosities()
        
    
    # Determine integrated Lumi before removing data
    # intLumi = datasetsMgr.getDataset("Data").getLuminosity()
    intLumi = 0
    
    # Remove datasets
    if 1:
        datasetsMgr.remove(filter(lambda name: "TTWJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
        datasetsMgr.remove(filter(lambda name: "QCD_bEnriched" in name, datasetsMgr.getAllDatasetNames()))
        
    # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
    plots.mergeRenameReorderForDataMC(datasetsMgr) 

        
    # Merge EWK samples
    if opts.mergeEWK:
        datasetsMgr.merge("EWK", GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

    # Set plot Styles
    plots._plotStyles["QCD"] = styles.getAltEWKStyle()
    plots._plotStyles["WJetsToQQ_HT_600ToInf"] = styles.getBaselineLineStyle()
    
    # Print dataset information
    datasetsMgr.PrintInfo()
    
    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)
    style.setGridX(True)
    style.setGridY(False)


    JetTypes = ["Light", "Gluon"]

    PtRange = ["30pt40", "40pt50", "50pt65", "65pt80", "80pt100", "100pt125", "125pt160", "160pt200", "200pt250", 
               "250pt320", "320pt400", "400pt630", "630pt800", "800ptInf"] 

    histos = ["LightJetsQGL_"+ pt for pt in PtRange]
    histos.extend("GluonJetsQGL_"+ pt for pt in PtRange)
    #histos.extend("JetsQGL")
    #histos.extend("GluonJetsQGL")
    #histos.extend("LightJetsQGL")

    for h in histos:
        # Produce & save the plots
        PlotMC(datasetsMgr, h, intLumi)
        
    
    # Dump the pdfs in a JSON file
    for dataset in datasetsMgr.getAllDatasets():
        
        for JetType in JetTypes:
            
            jsonhistos = [JetType+"JetsQGL_"+ pt for pt in PtRange]

            results = []
            
            for h in jsonhistos:
            
                dsetHisto = dataset.getDatasetRootHisto(h)
                dsetHisto.normalizeToOne()
                histo = dsetHisto.getHistogram()
                
                ptbin   = h.split("_")[-1]
                minPt   = ptbin.split("pt")[0]
                maxPt   = ptbin.split("pt")[-1]
                
                if maxPt == "Inf":
                    maxPt = 9999999999.9
                

                for k in range(1, histo.GetNbinsX()+1):
                    
                    resultObject = {}
                    resultObject["Jet"]       = JetType
                    resultObject["QGLmin"]    = histo.GetBinLowEdge(k) 
                    resultObject["QGLmax"]    = histo.GetBinLowEdge(k)+histo.GetBinWidth(k)
                    resultObject["Ptmin"]     = minPt
                    resultObject["Ptmax"]     = maxPt
                    resultObject["prob"]      = histo.GetBinContent(k)
                    resultObject["probError"] = histo.GetBinError(k)
                    results.append(resultObject)
                                
            filename = "QGLdiscriminator_%s_%sJets.json"%(dataset.name, JetType)
            with open(filename, 'w') as outfile:
                json.dump(results, outfile)
            
            print "Written results to %s"%filename


    return
def main(opts, signalMass):

    optModes = ["OptChiSqrCutValue100"]                                                                                                                             

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
                       
        # Determine integrated Lumi before removing data
#        intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        intLumi = 35920
        datasetsMgr.PrintInfo()

    
        #datasetsMgr.merge("QCD", GetListOfQCDatasets())
        #plots._plotStyles["QCD"] = styles.getQCDLineStyle()

        
        #if opts.noQCD:
            #datasetsMgr.remove(filter(lambda name: "QCD_b" in name, datasetsMgr.getAllDatasetNames()))  
            #datasetsMgr.remove(filter(lambda name: "QCD_HT" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "QCD" in name, datasetsMgr.getAllDatasetNames()))


        # Remove datasets
        if 1:
            datasetsMgr.remove(filter(lambda name: "Data" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "Diboson" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "QCD_b" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "QCD_HT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "SingleTop" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "DYJetsToQQHT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "TTZToQQ" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "TTWJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "WJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "Diboson" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "TTTT" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "TT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "FakeBMeasurementTrijetMass" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "M_" in name and "M_" + str(opts.signalMass) not in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(filter(lambda name: "ZJets" in name, datasetsMgr.getAllDatasetNames()))
            
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 

        plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Determine integrated Lumi before removing data
        intLumi = 35920
        #intLumi = datasetsMgr.getDataset("Data").getLuminosity()

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            if "M_" in d.getName():
#            if "M_" in d.getName() and "200" not in d.getName():
                if d not in signalMass:
                    continue
            datasetOrder.append(d.getName())

            #newOrder = ["TT", "QCD"]
            #newOrder = ["TT", "QCD"]
        for m in signalMass:
            #newOrder.insert(0, m)
            datasetOrder.insert(0, m)
            #datasetsMgr.selectAndReorder(newOrder)
        datasetsMgr.selectAndReorder(datasetOrder)

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the topSelection histos
        folder      = opts.folder 
        histoPaths1 = []

        if folder != "":
            histoList  = datasetsMgr.getDataset(datasetOrder[0]).getDirectoryContent(folder)
            histoPaths1 = [os.path.join(folder, h) for h in histoList]
        
        folder     = ""
        histoList  = datasetsMgr.getDataset(datasetOrder[0]).getDirectoryContent(folder)
        hList0     = [x for x in histoList if "TrijetMass" in x]
        hList1     = [x for x in histoList if "TetrajetMass" in x]
        hList2     = [x for x in histoList if "TetrajetBjetPt" in x]
        histoPaths2 = [os.path.join(folder, h) for h in hList0+hList1+hList2]

        histoPaths = histoPaths1 + histoPaths2

        for h in histoPaths:
            if "Vs" in h: # Skip TH2D
                continue
            if "VS" in h: # Skip TH2D
                continue

            PlotMC(datasetsMgr, h, intLumi)
        ROOT.gStyle.SetNdivisions(10, "X")

        
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setGridX(opts.gridX)
    style.setGridY(opts.gridY)
    
    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)

    # Get list of eras, modes, and optimisation modes
    erasList      = dsetMgrCreator.getDataEras()
    modesList     = dsetMgrCreator.getSearchModes()
    optList       = dsetMgrCreator.getOptimizationModes()
    sysVarList    = dsetMgrCreator.getSystematicVariations()
    sysVarSrcList = dsetMgrCreator.getSystematicVariationSources()

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        if len(optList) < 1:
            optList.append("")
        else:
            pass
        optModes = optList
    else:
        optModes = [opts.optMode]


    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        
        # Print PSets used for FakeBMeasurement
        if 0:
            datasetsMgr.printSelections()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ZJetsToQQ_HT600toInf" in d.getName():
                datasetsMgr.remove(d.getName())

            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
                if d.getName() not in opts.signal:
                    if not opts.acceptance:
                        datasetsMgr.remove(d.getName())

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            datasetsMgr.PrintInfo()
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Get Luminosity
        # For acceptance plot use all available masses
        if opts.acceptance:
            opts.signal = []
            opts.signalMasses = []
            for d in datasetsMgr.getAllDatasets():
                if "ChargedHiggs" in d.getName():
                    #dName = d.getName().replace("_ext1", "").split("M_")
                    dName = d.getName().split("M_")
                    m = int(dName[-1])
                    opts.signal.append(d.getName())
                    opts.signalMasses.append(m)

        if opts.intLumi < 0:
            if "Data" in datasetsMgr.getAllDatasetNames():
                opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            else:
                opts.intLumi = 1.0

        # Merge EWK samples
        if 1:
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()
            
        # Print post EWK-merge dataset summary
        datasetsMgr.PrintInfo()

        # Get the efficiency graphs
        hGraphList = []
        histoName  = os.path.join(opts.folder, "counter")
        hGraphList, _kwargs = GetHistoGraphs(datasetsMgr, opts.folder, histoName)

        # Plot the histo graphs
        PlotHistoGraphs(hGraphList, _kwargs)

    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
def DoPlots(datasetsMgr, histoName, analysisType="Inverted", bType="GenuineB"):

    # Sanity checks
    IsBaselineOrInverted(analysisType)
    IsGenuineOrFake(bType)

    # Definitions
    defaultFolder = ""
    if "FakeBPurity" in histoName:
        defaultFolder = "FakeBPurity"
    elif "ForFakeBMeasurement" + histoName:
        defaultFolder = "ForFakeBMeasurement"
    else:
        raise Exception("This should never happen")

    # Define folders for inclusive/genuine/fakes
    inclusiveFolder = defaultFolder
    genuineFolder = defaultFolder + "EWKGenuineB"
    fakeFolder = defaultFolder + "EWKFakeB"
    inclusiveHisto = histoName.replace(defaultFolder, inclusiveFolder)
    genuineHisto = histoName.replace(defaultFolder, genuineFolder)
    fakeHisto = histoName.replace(defaultFolder, fakeFolder)

    # Get the inclusive histograms
    p0 = plots.DataMCPlot(datasetsMgr, inclusiveHisto)
    Data = p0.histoMgr.getHisto("Data").getRootHisto().Clone("Data")

    # Get the genuine-b histograms
    p1 = plots.DataMCPlot(datasetsMgr, genuineHisto)
    EWKGenuineB = p1.histoMgr.getHisto("EWK").getRootHisto().Clone(
        "EWKGenuineB")
    QCDGenuineB = p1.histoMgr.getHisto("QCD").getRootHisto().Clone(
        "QCDGenuineB")

    # Get the fake-b histograms
    p2 = plots.DataMCPlot(datasetsMgr, fakeHisto)
    EWKFakeB = p2.histoMgr.getHisto("EWK").getRootHisto().Clone("EWKFakeB")
    QCDFakeB = p2.histoMgr.getHisto("QCD").getRootHisto().Clone("QCDFakeB")

    # Normalize histograms to unit area
    if 0:
        Data.Scale(1.0 / Data.Integral())
        EWKGenuineB.Scale(1.0 / EWKGenuineB.Integral())
        EWKFakeB.Scale(1.0 / EWKFakeB.Integral())
        QCDGenuineB.Scale(1.0 / QCDGenuineB.Integral())
        QCDFakeB.Scale(1.0 / EWKFakeB.Integral())

    # Create the final plot object
    comparisonList = [EWKGenuineB, QCDGenuineB, EWKFakeB, QCDFakeB]
    p = plots.ComparisonManyPlot(Data, comparisonList, saveFormats=[])
    p.setLuminosity(GetLumi(datasetsMgr))

    # Apply styles
    p.histoMgr.forHisto("Data", styles.getDataStyle())
    p.histoMgr.forHisto("EWKGenuineB",
                        styles.getAltEWKStyle())  #GenuineBStyle()
    p.histoMgr.forHisto("QCDGenuineB", styles.getQCDStyle())
    p.histoMgr.forHisto("EWKFakeB", styles.getGenuineBStyle())
    p.histoMgr.forHisto("QCDFakeB", styles.getFakeBStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle("Data", "AP")
    p.histoMgr.setHistoDrawStyle("EWKGenuineB", "AP")
    p.histoMgr.setHistoDrawStyle("QCDGenuineB", "AP")
    p.histoMgr.setHistoDrawStyle("EWKFakeB", "AP")
    p.histoMgr.setHistoDrawStyle("QCDFakeB", "AP")

    # Set legend style
    p.histoMgr.setHistoLegendStyle("Data", "P")
    p.histoMgr.setHistoLegendStyle("EWKGenuineB", "P")
    p.histoMgr.setHistoLegendStyle("QCDGenuineB", "P")
    p.histoMgr.setHistoLegendStyle("EWKFakeB", "P")
    p.histoMgr.setHistoLegendStyle("QCDFakeB", "P")
    # p.histoMgr.setHistoLegendStyleAll("LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
        "Data": "Data",
        "EWKGenuineB": "EWK-GenuineB",
        "QCDGenuineB": "QCD-GenuineB",
        "EWKFakeB": "EWK-FakeB",
        "QCDFakeB": "QCD-FakeB",
        #"Data"       : "Data (%s)"         % (analysisType),
        #"EWKGenuineB": "EWK-GenuineB (%s)" % (analysisType),
        #"QCDGenuineB": "QCD-GenuineB (%s)" % (analysisType),
        #"EWKFakeB"   : "EWK-FakeB (%s)"    % (analysisType),
        #"QCDFakeB"   : "QCD-FakeB (%s)"    % (analysisType),
    })

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _opts = {"ymin": 1e0, "ymaxfactor": 2.0}
    _format = "%0.0f"
    _xlabel = None

    if "dijetm" in histoName.lower():
        _rebinX = 2
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jj} (%s)" % (_units)
        _cutBox = {
            "cutValue": 80.399,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
        _opts["xmax"] = 400.0
    if "trijetm" in histoName.lower():
        _rebinX = 5
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjb} (%s)" % _units
        _cutBox = {
            "cutValue": 173.21,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
        _opts["xmax"] = 1500.0
    if "pt" in histoName.lower():
        _rebinX = 2
        _format = "%0.0f GeV/c"
    if "eta" in histoName.lower():
        _format = "%0.2f"
        _cutBox = {
            "cutValue": 0.,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
        _opts["xmin"] = -3.0
        _opts["xmax"] = +3.0
    if "deltaeta" in histoName.lower():
        _format = "%0.2f"
        _opts["xmin"] = 0.0
        _opts["xmax"] = 6.0
    if "bdisc" in histoName.lower():
        _format = "%0.2f"
    if "tetrajetm" in histoName.lower():
        _rebinX = 10
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjjb} (%s)" % (_units)
        _opts["xmax"] = 3500.0
    if "ancestry" in histoName.lower():
        _rebinX = 1
        _units = ""
        _format = "%0.0f " + _units
        _opts["xmax"] = 32.0
    plots.drawPlot(
        p,
        histoName,
        xlabel=_xlabel,
        ylabel="Arbitrary Units / %s" % (_format),
        log=True,
        rebinX=_rebinX,
        cmsExtraText="Preliminary",
        createLegend={
            "x1": 0.62,
            "y1": 0.72,
            "x2": 0.92,
            "y2": 0.92
        },
        opts=_opts,
        opts2={
            "ymin": 0.0,
            "ymax": 1.5
        },  #{"ymin": 0.6, "ymax": 1.4},
        ratio=True,
        ratioInvert=False,
        ratioYlabel="Ratio",
        cutBox=_cutBox,
    )
    # Save plot in all formats
    SavePlot(p, histoName,
             os.path.join(opts.saveDir, "GenuineVsFake", opts.optMode))
    return
def main(opts):

    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # Apply TDR style
    global style
    style = tdrstyle.TDRStyle()
    style.setOptStat(False)
    style.setGridX(opts.gridX)
    style.setGridY(opts.gridY)
    style.setLogX(opts.logX)
    style.setLogY(opts.logY)

    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            datasetsMgr.PrintInfo()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Print merged datasets and MC samples
        if 0:
            datasetsMgr.PrintInfo()

        # Get Luminosity
        if opts.intLumi == 0.0:
            if "Data" in datasetsMgr.getAllDatasetNames():
                opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            else:
                opts.intLumi = GetLumi(datasetsMgr)

        # Set/Overwrite cross-sections. Remove all but 1 signal mass
        signalsList = []
        for s in opts.signalList:
            signalsList.append("ChargedHiggs_HplusTB_HplusToTB_M_%s" % s)

        # Remove datasets with overlap?
        removeList = ["QCD-b"]
        dsetDY     = "DYJetsToQQ_HT180"
        dsetZJ     = "ZJetsToQQ_HT600toInf"
        dsetRM     = dsetZJ # datasets with overlap
        removeList.append(dsetRM)
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
                if d.getName() not in signalsList:
                    removeList.append(d.getName())

        # Custom filtering of datasets
        for i, d in enumerate(removeList, 1):
            msg = "Removing datasets %s from dataset manager" % (ShellStyles.NoteStyle() + d + ShellStyles.NormalStyle())
            Verbose(msg, i==1)
            datasetsMgr.remove(filter(lambda name: d == name, datasetsMgr.getAllDatasetNames()))


        # Merge EWK samples
        if opts.mergeEWK == "EWK":
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        if 1:
            datasetsMgr.PrintInfo()
            
        # Get list of histogram paths
        folder      = opts.folder
        histoList   = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)
        histoPaths  = [os.path.join(folder, h) for h in histoList if "AllSelections" in h]
        myHistos    = []
        skipStrings = ["StandardSelections", "JetPt", "JetEta", "JetEtaPhi", "BJetPt", "BJetEta", 
                       "BtagDiscriminator", "Eta", "LdgTrijetTopMassWMassRatio", "Subldg", "Dijet", 
                       "MET", "MHT", "MinDeltaPhiJetMHT", "MaxDeltaPhiJetMHT", "MinDeltaRJetMHT", "MinDeltaRJetMHTReversed"]

        for h in histoPaths:
            bSkip = False
            for s in skipStrings:
                if s in h:
                    bSkip = True
                    break
            if bSkip:
                continue
            else:
                myHistos.append(h)

        # For-loop: All histograms
        for i, h in enumerate(myHistos, 1):

            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i, "/", "%s:" % (len(myHistos)), h)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), i==1)
            PlotHistograms(datasetsMgr, h, signalsList, opts.cutDir)

    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50", "OptChiSqrCutValue100"]
    optModes = [""]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        datasetsMgr_matched = GetDatasetsFromDir(opts)
        datasetsMgr_matched.updateNAllEventsToPUWeighted()
        datasetsMgr_matched.loadLuminosities() # from lumi.json

        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        datasetsMgr.remove(filter(lambda name: "QCD_b" in name, datasetsMgr.getAllDatasetNames())) #soti
        datasetsMgr_matched.remove(filter(lambda name: "QCD" in name, datasetsMgr_matched.getAllDatasetNames())) #soti
        # Set/Overwrite cross-sections
        datasetsToRemove = ["QCD-b"]#, "QCD_HT50to100", "QCD_HT100to200"]#, "QCD_HT200to300"]#, "QCD_HT300to500"]
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0) # ATLAS 13 TeV H->tb exclusion limits
                #if d.getName() != opts.signal:
                #if "M_650" in d.getName():  #soti fixmi
                #    datasetsToRemove.append(d.getName())
                if "M_800" in d.getName():
                    datasetsToRemove.append(d.getName())
                #if "M_200" in d.getName():
                #    datasetsToRemove.append(d.getName())

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Custom Filtering of datasets 
        for i, d in enumerate(datasetsToRemove, 0):
            msg = "Removing dataset %s" % d
            Print(ShellStyles.WarningLabel() + msg + ShellStyles.NormalStyle(), i==0)
            datasetsMgr.remove(filter(lambda name: d in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr_matched.remove(filter(lambda name: d in name, datasetsMgr_matched.getAllDatasetNames())) #soti
        if opts.verbose:
            datasetsMgr.PrintInfo()

        # ZJets and DYJets overlap
        if "ZJetsToQQ_HT600toInf" in datasetsMgr.getAllDatasetNames() and "DYJetsToQQ_HT180" in datasetsMgr.getAllDatasetNames():
            Print("Cannot use both ZJetsToQQ and DYJetsToQQ due to duplicate events? Investigate. Removing ZJetsToQQ datasets for now ..", True)
            datasetsMgr.remove(filter(lambda name: "ZJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))


        #datasetsMgr.merge("QCD", GetListOfQCDatasets())
        #plots._plotStyles["QCD"] = styles.getQCDLineStyle()
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        # Get Luminosity
        #intLumi = datasetsMgr.getDataset("Data").getLuminosity() Soti
        intLumi = 35920

        # Re-order datasets (different for inverted than default=baseline)

        newOrder = []
        # For-loop: All MC datasets
        for d in datasetsMgr.getMCDatasets():
            newOrder.append(d.getName())
        
        # Move signal to top
#        if opts.signal in newOrder:
#            s = newOrder.pop( newOrder.index(opts.signal) )
#            newOrder.insert(0, s)
        print len(newOrder), "newOrder"
        signalMass = ["M_200", "M_500", "M_1000", "M_650"]
        for d in datasetsMgr.getMCDatasets():
            for m in signalMass:
                if m in d.getName():
                    s = newOrder.pop( newOrder.index(d.getName()) )
                    newOrder.insert(0, s)
                    #datasetsMgr.selectAndReorder(newOrder)
        print len(newOrder), "newOrder"
        # Add Data to list of samples!
        if not opts.onlyMC:
            newOrder.insert(0, "Data")
            
        # Apply new dataset order!
        datasetsMgr.selectAndReorder(newOrder)

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()
        
        # Apply TDR style
        style = tdrstyle.TDRStyle()
        style.setOptStat(True)
        style.setGridX(opts.gridX)
        style.setGridY(opts.gridY)

        # Do Data-MC histograms with DataDriven QCD
        folder     = opts.folder
        histoList  = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)
        histoPaths1 = [os.path.join(folder, h) for h in histoList]
        histoPaths2 = [h for h in histoPaths1]# if "jet" not in h.lower()]
        nHistos     = len(histoPaths2)

        # For-loop: All histograms
        for i, h in enumerate(histoPaths2, 1):
            msg   = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i, "/", "%s:" % (nHistos), h)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), i==1)
            PlotHistograms(datasetsMgr, datasetsMgr_matched, h, intLumi)
        ROOT.gStyle.SetNdivisions(10, "X")
    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
def main(opts):

    optModes = [""]
    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # For-loop: All opt Mode
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities(fname="lumi.json")

        # Get Luminosity
        if opts.intLumi < 0:
            if "Data" in datasetsMgr.getAllDatasetNames():
                opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
            else:
                opts.intLumi =  datasetsMgr.loadLumi()

        # Set/Overwrite cross-sections
        datasetsToRemove = []
        for d in datasetsMgr.getAllDatasets():
            if "M_%s" % (opts.signalMass) in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
            else:
                datasetsToRemove.append(d.getName())

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Custom Filtering of datasets 
        for i, d in enumerate(datasetsToRemove, 0):
            msg = "Removing dataset %s" % d
            Verbose(ShellStyles.WarningLabel() + msg + ShellStyles.NormalStyle(), i==0)
            datasetsMgr.remove(filter(lambda name: d == name, datasetsMgr.getAllDatasetNames()))

        if opts.verbose:
            datasetsMgr.PrintInfo()
  
        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Apply TDR style
        style = tdrstyle.TDRStyle()
        style.setOptStat(True)
        style.setGridX(opts.gridX)
        style.setGridY(opts.gridY)

        # Do Data-MC histograms with DataDriven QCD
        folder     = opts.folder
        histoList  = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(folder)        
        histoPaths = [os.path.join(folder, h) for h in histoList]
        keepList   = ["LdgTetrajetMass_AfterAllSelections"]
        #keepList   = ["LdgTetrajetMass_AfterStandardSelections"]
        myHistos   = []
        for h in histoPaths:
            if h.split("/")[-1] not in keepList:
                continue
            else:
                myHistos.append(h)

        for i, h in enumerate(myHistos, 1):
            PlotHistograms(datasetsMgr, h)
        
    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)    
    return
Exemple #42
0
def DoPlots(datasetsMgr, histoName):
    analysisType = "Inverted"

    # Get the Inclusive (Data, EWK)
    p1 = plots.ComparisonPlot(*getHistos(datasetsMgr, histoName, analysisType))
    p1.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

    # Get histos
    inverted_Data = p1.histoMgr.getHisto("Data").getRootHisto().Clone(
        "Inverted-Data")
    inverted_EWK = p1.histoMgr.getHisto("EWK").getRootHisto().Clone(
        "Inverted-EWK")
    inverted_QCD = p1.histoMgr.getHisto("Data").getRootHisto().Clone(
        "Inverted-QCD")

    # Normalize histograms to unit area
    inverted_Data.Scale(1.0 / inverted_QCD.Integral())
    inverted_QCD.Scale(1.0 / inverted_QCD.Integral())
    inverted_EWK.Scale(1.0 / inverted_EWK.Integral())

    # Create the final plot object (Cannot subtract EWK from Data since the trigger in the MC samples is NOT prescaled. In data it is)
    p = plots.ComparisonManyPlot(inverted_Data, [inverted_EWK], saveFormats=[])
    #p = plots.PlotBase([inverted_Data], saveFormats=[])

    # Apply styles
    p.histoMgr.forHisto("Inverted-Data", styles.getDataStyle())
    #p.histoMgr.forHisto("Inverted-QCD"  , styles.getInvertedLineStyle() )
    p.histoMgr.forHisto("Inverted-EWK", styles.getAltEWKStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle("Inverted-Data", "AP")
    #p.histoMgr.setHistoDrawStyle("Inverted-QCD" , "HIST")
    p.histoMgr.setHistoDrawStyle("Inverted-EWK", "AP")

    # Set legend style
    p.histoMgr.setHistoLegendStyle("Inverted-Data", "P")
    #p.histoMgr.setHistoLegendStyle("Inverted-QCD" , "L")
    p.histoMgr.setHistoLegendStyle("Inverted-EWK", "P")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
        "Inverted-Data": "Data",
        "Inverted-EWK": "EWK",
        #"Inverted-QCD" : "QCD=Data-EWK",
    })

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    #_opts   = {"ymin": 1e-4, "ymaxfactor": 2.0}
    _opts = {"ymin": 1e-4, "ymax": 1e-1}
    _xlabel = None
    _units = "GeV/c^{2}"
    _xlabel = "m_{jjb} (%s)" % _units
    _ylabel = "Arbitrary Units / %0.0f %s " % (inverted_QCD.GetBinWidth(0),
                                               _units)
    _cutBox = {
        "cutValue": 173.21,
        "fillColor": 16,
        "box": False,
        "line": True,
        "greaterThan": True
    }
    _opts["xmax"] = 1500.0

    # Complains (SysError in <TFile::TFile>: file ...)
    if 0:
        plots.drawPlot(p,
                       histoName,
                       xlabel=_xlabel,
                       ylabel=_ylabel,
                       log=True,
                       rebinX=_rebinX,
                       cmsExtraText="Preliminary",
                       createLegend={
                           "x1": 0.62,
                           "y1": 0.78,
                           "x2": 0.92,
                           "y2": 0.92
                       },
                       opts=_opts)

    if 1:
        plots.drawPlot(
            p,
            histoName,
            xlabel=_xlabel,
            ylabel=_ylabel,
            log=True,
            rebinX=_rebinX,
            cmsExtraText="Preliminary",
            createLegend={
                "x1": 0.62,
                "y1": 0.78,
                "x2": 0.92,
                "y2": 0.92
            },
            opts=_opts,
            opts2={
                "ymin": 0.0,
                "ymax": 2.0
            },
            ratio=True,
            ratioInvert=False,
            ratioYlabel="Ratio",
            cutBox=_cutBox,
        )
    # Save to ROOT file
    inverted_QCD.SaveAs("Inverted_QCD.root")

    # Save plot in all formats
    SavePlot(p, histoName, os.path.join(opts.saveDir, "Test", opts.optMode))
    return
def main(opts):
    Verbose("main function")

    comparisonList = ["AfterStdSelections"]

    # Setup & configure the dataset manager
    datasetsMgr = GetDatasetsFromDir(opts)
    datasetsMgr.updateNAllEventsToPUWeighted()
    datasetsMgr.loadLuminosities()  # from lumi.json
    if opts.verbose:
        datasetsMgr.PrintCrossSections()
        datasetsMgr.PrintLuminosities()

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

    # Custom Filtering of datasets
    # datasetsMgr.remove(filter(lambda name: "HplusTB" in name and not "M_500" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "ST" in name, datasetsMgr.getAllDatasetNames()))

    # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
    plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Get Integrated Luminosity
    if opts.mcOnly:
        # Determine integrated lumi
        if opts.intLumi < 0.0:
            opts.intLumi = GetLumi(datasetsMgr)
        else:
            pass
        # Remove data datasets
        datasetsMgr.remove(
            filter(lambda name: "Data" in name,
                   datasetsMgr.getAllDatasetNames()))

    # Re-order datasets (different for inverted than default=baseline)
    newOrder = [
        "Data"
    ]  #, "TT", "DYJetsToQQHT", "TTWJetsToQQ", "WJetsToQQ_HT_600ToInf", "SingleTop", "Diboson", "TTZToQQ", "TTTT"]
    newOrder.extend(GetListOfEwkDatasets())

    if opts.mcOnly:
        newOrder.remove("Data")
    datasetsMgr.selectAndReorder(newOrder)

    # Set/Overwrite cross-sections
    for d in datasetsMgr.getAllDatasets():
        if "ChargedHiggs" in d.getName():
            datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

    # Merge EWK samples
    if opts.mergeEWK:
        datasetsMgr.merge("EWK", GetListOfEwkDatasets())
        plots._plotStyles["EWK"] = styles.getAltEWKStyle()

    # Print dataset information
    datasetsMgr.PrintInfo()

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

    # Do the Purity Triples analysisTypes = ["Baseline", "Inverted"]
    analysisTypes = ["", "EWKFakeB", "EWKGenuineB"]
    for analysis in analysisTypes:
        Print("Plotting Purity Triplet Histograms (%s)" % (analysis), True)
        PurityTripletPlots(datasetsMgr, analysisType=analysis)

    return
    # Do the Data/QCD/EWK histograms
    if opts.mergeEWK:
        analysisTypes = ["Baseline", "Inverted"]
        for analysis in analysisTypes:
            Print("Plotting DataEWkQcd Histograms (%s)" % (analysis), True)
            for hName in getTopSelectionHistos(opts.histoLevel, analysis):
                DataEwkQcd(datasetsMgr, hName.split("/")[-1], analysis)
    else:
        Print(
            "Cannot draw the Data/QCD/EWK histograms without the option --mergeEWK. Exit",
            True)
    return
Exemple #44
0
def main(opts, signalMass):

    optModes = ["OptChiSqrCutValue100"]

    if opts.optMode != None:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)

        # Determine integrated Lumi before removing data
        #intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        intLumi = 35800

        # Remove datasets
        if 1:
            datasetsMgr.remove(
                filter(lambda name: "Data" in name,
                       datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(
                filter(lambda name: "QCD-b" in name,
                       datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(
                filter(lambda name: "QCD" in name,
                       datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "SingleTop" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "DYJetsToQQHT" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(
                filter(lambda name: "TTZToQQ" in name,
                       datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(
                filter(lambda name: "TTWJetsToQQ" in name,
                       datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "WJetsToQQ" in name, datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "Diboson" in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(
                filter(lambda name: "TTTT" in name,
                       datasetsMgr.getAllDatasetNames()))
            datasetsMgr.remove(
                filter(lambda name: "FakeBMeasurementTrijetMass" in name,
                       datasetsMgr.getAllDatasetNames()))
            #datasetsMgr.remove(filter(lambda name: "M_" in name and "M_" + str(opts.signalMass) not in name, datasetsMgr.getAllDatasetNames()))

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets())
            plots._plotStyles["EWK"] = styles.getAltEWKStyle()

        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            if "M_" in d.getName():
                if d not in signalMass:
                    continue
            datasetOrder.append(d.getName())
            #newOrder = ["TT", "QCD"]
            #newOrder = ["TT", "QCD"]
        for m in signalMass:
            #newOrder.insert(0, m)
            datasetOrder.insert(0, m)
            #datasetsMgr.selectAndReorder(newOrder)
        datasetsMgr.selectAndReorder(datasetOrder)

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        # Do the topSelection histos
        folder = opts.folder
        histoPaths1 = []
        if folder != "":
            histoList = datasetsMgr.getDataset(
                datasetOrder[0]).getDirectoryContent(folder)
            # hList0     = [x for x in histoList if "TrijetMass" in x]
            # hList1     = [x for x in histoList if "TetrajetMass" in x]
            # hList2     = [x for x in histoList if "TetrajetBJetPt" in x]
            # histoPaths1 = [os.path.join(folder, h) for h in hList0+hList1+hList2]
            histoPaths1 = [os.path.join(folder, h) for h in histoList]

        folder = "TrijetCandidate"
        histoList = datasetsMgr.getDataset(
            datasetOrder[0]).getDirectoryContent(folder)
        hList0 = [x for x in histoList if "TrijetMass" in x]
        hList1 = [x for x in histoList if "TetrajetMass" in x]
        hList2 = [x for x in histoList if "TetrajetBjetPt" in x]
        histoPaths2 = [
            os.path.join(folder, h) for h in hList0 + hList1 + hList2
        ]

        histoPaths = histoPaths1 + histoPaths2
        for h in histoPaths:
            if "Vs" in h:  # Skip TH2D
                continue
            PlotMC(datasetsMgr, h, intLumi)

        HistoFit = [
            "TrijetDPtOverGenPt",
        ]

        for i in range(len(HistoFit)):
            Fit(datasetsMgr.getAllDatasets(), folder + "/" + HistoFit[i],
                "gaus")

    return
def DataEwkQcd(datasetsMgr, histoName, analysisType):
    '''
    Create plots with "Data", "QCD=Data-EWK", and "EWK" on the same canvas
    Mostly for sanity checks and visualisation purposes
    '''
    Verbose(
        "Plotting histogram %s for Data, EWK, QCD for %s" %
        (histoName, analysisType), True)

    # Sanity check
    IsBaselineOrInverted(analysisType)

    # Define histogram names (full path)
    h1 = "topSelection_Baseline/%s" % (histoName)
    h2 = "topSelection_Inverted/%s" % (histoName)

    # Create plot object for Data
    p1 = plots.ComparisonPlot(*getHistos(datasetsMgr, "Data", h1, h2))
    p1.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())
    # Create plot object for EWK
    p2 = plots.ComparisonPlot(*getHistos(datasetsMgr, "EWK", h1, h2))
    p2.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

    # Get Data and EWK histos
    Data = p1.histoMgr.getHisto(analysisType +
                                "-Data").getRootHisto().Clone(analysisType +
                                                              "-Data")
    EWK = p2.histoMgr.getHisto(analysisType +
                               "-EWK").getRootHisto().Clone(analysisType +
                                                            "-EWK")

    # Create QCD histo: QCD = Data-EWK
    QCD = p1.histoMgr.getHisto(analysisType +
                               "-Data").getRootHisto().Clone(analysisType +
                                                             "-QCD")
    QCD.Add(EWK, -1)

    # Create the final plot object. The Data is treated as the reference histo.
    # All other histograms are compared with respect to that.
    p = plots.ComparisonManyPlot(Data, [QCD, EWK], saveFormats=[])
    p.setLuminosity(GetLumi(datasetsMgr))

    # Apply histo styles
    p.histoMgr.forHisto(analysisType + "-Data", styles.getDataStyle())
    p.histoMgr.forHisto(analysisType + "-QCD", styles.getQCDLineStyle())
    p.histoMgr.forHisto(analysisType + "-EWK", styles.getAltEWKStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle(analysisType + "-Data", "P")
    p.histoMgr.setHistoLegendStyle(analysisType + "-Data", "P")
    p.histoMgr.setHistoDrawStyle(analysisType + "-QCD", "LP")
    p.histoMgr.setHistoLegendStyle(analysisType + "-QCD", "LP")
    p.histoMgr.setHistoDrawStyle(analysisType + "-EWK", "HIST")
    p.histoMgr.setHistoLegendStyle(analysisType + "-EWK", "LFP")
    # p.histoMgr.setHistoLegendStyleAll("LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
        analysisType + "-Data": "Data",
        analysisType + "-QCD": "QCD",  #=Data-EWK",
        analysisType + "-EWK": "EWK",
        # analysisType + "-Data": "%s (Data)" % (analysisType),
        # analysisType + "-QCD" : "%s (QCD)"  % (analysisType),
        # analysisType + "-EWK" : "%s (EWK)"  % (analysisType),
    })

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _opts = {"ymin": 1e0, "ymaxfactor": 10}
    _format = "%0.0f"

    if "mass" in histoName.lower():
        _rebinX = 2
        _format = "%0.0f GeV/c^{2}"
        if "TrijetMass" in histoName:
            _cutBox = {
                "cutValue": 173.21,
                "fillColor": 16,
                "box": False,
                "line": True,
                "greaterThan": True
            }
        if "DijetMass" in histoName:
            _cutBox = {
                "cutValue": 80.399,
                "fillColor": 16,
                "box": False,
                "line": True,
                "greaterThan": True
            }
    if "pt" in histoName.lower():
        _rebinX = 2
        _format = "%0.0f GeV/c"
    if "eta" in histoName.lower():
        _rebinX = 2
        _format = "%0.2f"
        _cutBox = {
            "cutValue": 0.,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
    if "bdisc" in histoName.lower():
        _format = "%0.2f"
        _rebinX = 1
    if "chisqr" in histoName.lower():
        _format = "%0.0f"
        _rebinX = 10
        if "after" in histoName.lower():
            _rebinX = 1
            _opts["xmax"] = 20.0
    if "tetrajet" in histoName.lower():
        if "mass" in histoName.lower():
            _rebinX = 10
            _opts["xmax"] = 3500.0

    else:
        pass

    histoName += "_" + analysisType
    plots.drawPlot(
        p,
        histoName,
        ylabel="Events /  %s" % (_format),
        log=True,
        rebinX=_rebinX,
        cmsExtraText="Preliminary",
        createLegend={
            "x1": 0.75,
            "y1": 0.76,
            "x2": 0.92,
            "y2": 0.92
        },
        opts=_opts,
        opts2={
            "ymin": 1e-5,
            "ymax": 1e0
        },
        ratio=True,
        ratioInvert=False,
        ratioYlabel="Ratio",
        cutBox=_cutBox,
    )

    # Save plot in all formats
    SavePlot(p,
             histoName,
             os.path.join(opts.saveDir, "DataEwkQcd"),
             saveFormats=[".png"])
    return