def getDataSets(dirs, dataEra, searchMode, analysis, optMode):
    datasets = dataset.getDatasetsFromMulticrabDirs(dirs,dataEra=dataEra,  searchMode=searchMode, analysisName=analysis, optimizationMode=optMode)
            
    datasets.updateNAllEventsToPUWeighted()
    datasets.loadLuminosities()
    
    datasets.remove(filter(lambda name: "TTToHplus" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_t-channel" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "Hplus_taunu_tW-channel" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "TTJets_SemiLept" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "TTJets_FullLept" in name, datasets.getAllDatasetNames()))
    datasets.remove(filter(lambda name: "TTJets_Hadronic" in name, datasets.getAllDatasetNames()))
    
    plots.mergeRenameReorderForDataMC(datasets)
    
    datasets.merge("EWK", [
        "TTJets",
        "WJets",
        "DYJetsToLL",
        "SingleTop",
        "Diboson"
        ])

    return datasets
Esempio n. 2
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()))

        # 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
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
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs_HplusTB_HplusToTB_M_%s" % (opts.signalMass) in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
            #else:
            #    datasetsMgr.remove(d.getName())
                
        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        else:
            opts.intLumi = 35920

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

        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            
            if str(opts.signalMass) not in d.getName():
            #if d not in "%s" % (opts.signalMass):
                continue
            datasetOrder.append(d.getName())

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        for dName in ["TT", "QCD", "ChargedHiggs_HplusTB_HplusToTB_M_%s" % (opts.signalMass)]:
            for var in ["M", "Pt", "BDT", "Eta"]:
                # Do the histograms
                PlotHistograms(datasetsMgr, dName, var)

    return
def main():
    style = tdrstyle.TDRStyle()
    # Object for selecting data eras, search modes, and optimization modes
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector()

    parser = OptionParser(usage="Usage: %prog [options]",add_help_option=True,conflict_handler="resolve")
    myModuleSelector.addParserOptions(parser)
    parser.add_option("-m", "--mcrab", dest="mcrab", action="store", help="Path to the multicrab directory for input")
    parser.add_option("-d", "--dataset", dest="dataset", action="store", help="Name of the dataset to be plotted")
    parser.add_option("-e", "--error", dest="errorlevel", default=0.10, action="store", help="Maximum relative uncertainty per bin (default=10%%)")
    (opts, args) = parser.parse_args()

    if opts.mcrab == None:
        raise Exception("Please provide input multicrab directory with -m")
    if not os.path.exists(opts.mcrab):
        raise Exception("The input root file '%s' does not exist!"%opts.mcrab)
    if opts.dataset == None:
        raise Exception("Please provide dataset name with -d")

    # Find module names
    #myNames = findModuleNames(opts.mcrab, "BTagEfficiency")

    # Get dataset managers
    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)
    myModuleSelector.setPrimarySource("analysis", dsetMgrCreator)
    # Select modules
    myModuleSelector.doSelect(opts)
    myModuleSelector.printSelectedCombinationCount()
    #for n in myNames:
    results = []
    for era in myModuleSelector.getSelectedEras():
        for searchMode in myModuleSelector.getSelectedSearchModes():
            for optimizationMode in myModuleSelector.getSelectedOptimizationModes():
                dsetMgr = dsetMgrCreator.createDatasetManager(dataEra=era,searchMode=searchMode,optimizationMode=optimizationMode)
                #datasets = dataset.getDatasetsFromMulticrabDirs(dirs=[opts.mcrab], analysisName=n)
                dsetMgr.loadLuminosities()
                dsetMgr.updateNAllEventsToPUWeighted()
                plots.mergeRenameReorderForDataMC(dsetMgr)
                lumi = dsetMgr.getDataset("Data").getLuminosity()
                for dset in dsetMgr.getMCDatasets():
                    print dset.name
                    if dset.name.startswith(opts.dataset):
                        results.extend(doPlot("btageff_%s_%s_%s"%(era, searchMode, optimizationMode), dset, opts.errorlevel, optimizationMode, lumi))
                  #dsetMgr.close()
    print "\nFigures generated"
    
    for item in results:
        print item
    
    # Write results to a json file
    filename = "btageff_%s.json"%opts.dataset
    with open(filename, 'w') as outfile:
        json.dump(results, outfile)
    print "Written results to %s"%filename
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):

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

    # Setup & configure the dataset manager 
    datasetsMgr = GetDatasetsFromDir(opts, 0)
    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) 
    if 1:
        plots.mergeRenameReorderForDataMC(datasetsMgr) 

    # Print datasets info summary
    datasetsMgr.PrintInfo()

    # Define the mapping histograms in numerator->denominator pairs
    VariableList = ["TetrajetMass"]

    counter =  0
    opts.nDatasets = len(datasetsMgr.getAllDatasets())
    nPlots  = len(VariableList)*opts.nDatasets

    # For-loop: All datasets
    for dataset in datasetsMgr.getAllDatasets():
        # For-looop: All variables
        for hName in VariableList:
            hPath = os.path.join(opts.folder, hName)

            counter+=1
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % counter, "/", "%s:" % (nPlots), "%s" % (dataset.getName()))
            aux.Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), counter==1)
        
            PlotHistos(dataset.getName(), hPath) # For each dataset: Plot histos from different multicrabs on same canvas

    aux.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")
    
    optModes = ["", "OptChiSqrCutValue50p0", "OptChiSqrCutValue100p0", "OptChiSqrCutValue200p0"]
    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()))
            datasetsMgr.remove(filter(lambda name: "ST" in name, datasetsMgr.getAllDatasetNames()))
               
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr)
   
        # Print dataset information
        datasetsMgr.PrintInfo()

        # 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)

        # Merge EWK samples (done later on)
        # if opts.mergeEWK:
        #    datasetsMgr.merge("EWK", GetListOfEwkDatasets())

        # Do default counters
        doCounters(datasetsMgr)

    return
 def createDsetMgr(self, multicrabDir, era, searchMode, optimizationMode=None, systematicVariation=None):
     self._era = era
     self._searchMode = searchMode
     self._optimizationMode = optimizationMode
     self._systematicVariation = systematicVariation
     # Construct info string of module
     self._moduleInfoString = "%s_%s_%s"%(era, searchMode, optimizationMode)
     # Obtain dataset manager
     self._dsetMgrCreator = dataset.readFromMulticrabCfg(directory=multicrabDir)
     self._dsetMgr = self._dsetMgrCreator.createDatasetManager(dataEra=era,searchMode=searchMode,optimizationMode=optimizationMode,systematicVariation=systematicVariation)
     # Do the usual normalisation
     self._dsetMgr.updateNAllEventsToPUWeighted()
     self._dsetMgr.loadLuminosities()
     plots.mergeRenameReorderForDataMC(self._dsetMgr)
     self._dsetMgr.merge("EWK", _generalOptions["ewkDatasetsForMerging"])
     # Obtain luminosity
     self._luminosity = self._dsetMgr.getDataset("Data").getLuminosity()
def main(opts):

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

    # Setup & configure the dataset manager 
    datasetsMgr = GetDatasetsFromDir(opts)
    datasetsMgr.updateNAllEventsToPUWeighted()
    datasetsMgr.loadLuminosities() # from lumi.json
    plots.mergeRenameReorderForDataMC(datasetsMgr) 
    opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()

    # Do the plot
    doPlot()

    # Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
Esempio n. 10
0
def Plot(jsonfile, opts):
    Verbose("Plotting")

    with open(os.path.abspath(jsonfile)) as jfile:
        j = json.load(jfile)

        Print("Plotting %s in %s" % (j["title"], j["saveDir"]), True)

        # Setup the style
        style = tdrstyle.TDRStyle()
        style.setGridX(j["gridX"]=="True")
        style.setGridY(j["gridY"]=="True")
    
        # Set ROOT batch mode boolean
        ROOT.gROOT.SetBatch(opts.batchMode)

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts, j)
        datasetsMgr.loadLuminosities()
        datasetsMgr.updateNAllEventsToPUWeighted()
        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)

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Get Integrated Luminosity
        lumi = GetLumi(datasetsMgr)
        
        # Plot the histogram
        DataMCPlot(datasetsMgr, j)
        return
def plot(resultdir,jsonfile):
    with open(os.path.abspath(jsonfile)) as jfile:
        j = json.load(jfile)
        print "Plotting",j["title"],"in",resultdir

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


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

        datasets = dataset.getDatasetsFromMulticrabDirs(paths)

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

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

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

        if len(j["samples"])>0:
           for s in j["samples"]:
               h = datasets.getDataset(s).getDatasetRootHisto(j["histogram"]).getHistogram()
               name = j["histogram"]+s
               plotgraph([h],lumi,j,name)
def main(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(argv):

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

    FAKEHISTO = "OnlyEWKFakeTaus"
    GENUINEHISTO = "PlusFilteredEWKFakeTaus"

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

    dirs.append(sys.argv[1])

    
    # Create all datasets from a multicrab task

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


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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

        fitOptions = "LRB"

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

        invertedQCD.getNormalization()

        #separated

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

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

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

    mergeNormalizations("QCDInvertedNormalizationFactors.py","QCDInvertedNormalizationFactorsSeparatedFakeTaus.py")
def main(opts):

    # 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()

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

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

    # Define genuineB and fakeB datasets
    mergeMap = {"TT": "GenuineB",
                "QCD_HT50to100"   : "FakeB", #v. poor stats!
                "QCD_HT200to300"  : "FakeB", #v. poor stats!
                "QCD_HT100to200"  : "FakeB", #v. poor stats!
                "QCD_HT200to300"  : "FakeB", #v. poor stats!
                "QCD_HT300to500"  : "FakeB",
                "QCD_HT500to700"  : "FakeB", 
                "QCD_HT700to1000" : "FakeB",
                "QCD_HT1000to1500": "FakeB",
                "QCD_HT1500to2000": "FakeB", 
                "QCD_HT2000toInf" : "FakeB",
                "WJetsToQQ_HT_600ToInf": "FakeB"
                }
    # Definitions
    results  = []

    # For-loop: All optimisation modes
    for index, opt in enumerate(optModes, 1):
        opts.optMode = opt
        
        # Definitions
        genuineB = None
        fakeB    = None

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

        # Remove unwanted datasets
        removeList = ["QCD_HT50to100", "QCD_HT100to200", "QCD_HT200to300", "QCD_HT200to300_ext1", "WJetsToQQ_HT_600ToInf"]
        for k, d in enumerate(datasetsMgr.getAllDatasets(), 1):
            if d.getName() in removeList:
                datasetsMgr.remove(d.getName())
                Verbose(ShellStyles.ErrorStyle() + "Removing dataset %s" % d.getName() + ShellStyles.NormalStyle(), k==1)
        if index == 1:
            datasetsMgr.PrintInfo()

        # Merge datasets into two groups: Genuine-B and Fake-B
        datasetsMgr.mergeMany(mergeMap, addition=False)
        for d in datasetsMgr.getAllDatasets():
            if d.getName() == "GenuineB":
                genuineB = d
            if d.getName() == "FakeB":
                fakeB = d

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

        # Get integrated luminosity
        intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        datasetsMgr.remove(filter(lambda name: "Data" in name, datasetsMgr.getAllDatasetNames()))
                    
        # Print dataset information
        if index == 1:
            datasetsMgr.PrintInfo()

        # Do the plot
        name = GetPlotName(opts)
        myResults = doPlot(name, genuineB, fakeB, opts.errorlevel, opts.optMode, intLumi) 
        
        # Save results and msgs
        results.extend(myResults)
        
        # For-loop: All points
        if opts.verbose:
            for item in results:
                print item
    
    # Print path of all saved plots
    msg = "All plots saved under directory %s" % (aux.convertToURL(opts.saveDir, opts.url))
    Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), True)

    # Write results to a json file
    with open(opts.json, 'w') as outfile:
        json.dump(results, outfile)
    msg = "Wrote results to \"%s\"" % (opts.json)
    Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), True)
    return
Esempio n. 15
0
def main():

    style    = tdrstyle.TDRStyle()

    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(parseOpts.batchMode)
    
    # Get all datasets from the mcrab dir
    datasetsMgr  = GetDatasetsFromDir(parseOpts.mcrab, kwargs.get("analysis"))

    # Determine Integrated Luminosity (If Data datasets present)
    intLumi = GetLumi(datasetsMgr)
    
    # Update to PU
    datasetsMgr.updateNAllEventsToPUWeighted()

    # Remove datasets
    datasetsMgr.remove(kwargs.get("rmDataset"))
    # datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "QCD" in name in name, datasetsMgr.getAllDatasetNames()))
    
    # Set custom XSections
    # d.getDataset("TT_ext3").setCrossSection(831.76)
    
    # Default merging & ordering: "Data", "QCD", "SingleTop", "Diboson"
    plots.mergeRenameReorderForDataMC(datasetsMgr) #WARNING: Merged MC histograms must be normalized to something!

    # Remove datasets (for merged names)
    datasetsMgr.remove(kwargs.get("rmDataset"))

    
    # For-loop: All Histogram names
    for counter, hName in enumerate(hNames):
        
        # Get the save path and name
        savePath, saveName = GetSavePathAndName(hName, **kwargs)

        # Get Histos for Plotter
        refHisto, otherHistos = GetHistosForPlotter(datasetsMgr, hName, **kwargs)
        
        # Create a comparison plot
        p = plots.ComparisonManyPlot(refHisto, otherHistos)


        # Remove negative contributions
        #RemoveNegativeBins(datasetsMgr, hName, p)

        # Create a frame
        if kwargs.get("logY"):
            opts = {"ymin": 1e-5, "ymaxfactor": 10}
        else:
            opts = {"ymin": 0.0, "ymaxfactor": 1.2}
        ratioOpts = {"ymin": 0.0, "ymax": 2.0}
        p.createFrame(saveName, createRatio=kwargs.get("createRatio"), opts=opts, opts2=ratioOpts)
        
        # Customise Legend
        moveLegend = {"dx": -0.1, "dy": +0.0, "dh": -0.1}
        p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
        #p.removeLegend()

        # Customise frame
        p.getFrame().GetYaxis().SetTitle( getTitleY(refHisto, **kwargs) )
        #p.setEnergy("13")
        if kwargs.get("createRatio"):
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)

        # SetLog
        SetLogAndGrid(p, **kwargs)

        # Add cut line/box
        _kwargs = { "lessThan": kwargs.get("cutLessThan")}
        p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"), fillColor=kwargs.get("cutFillColour"), box=kwargs.get("cutBox"), line=kwargs.get("cutLine"), **_kwargs)

        # Move the refDataset to first in the draw order (back)
        histoNames = [h.getName() for h in p.histoMgr.getHistos()]
        p.histoMgr.reorder(filter(lambda n: plots._legendLabels[kwargs.get("refDataset") ] not in n, histoNames))
                
        #  Draw plots
        p.draw()

        # Customise text
        histograms.addStandardTexts(lumi=intLumi)
        # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
        # histograms.addText(0.4, 0.11, "Runs " + datasetsMgr.loadRunRange(), 17)
        
        # Save canvas under custom dir
        SaveAs(p, savePath, saveName, kwargs.get("saveFormats"))

    return
Esempio n. 16
0
def main(opts):

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

    # 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("")
        optModes = optList
    else:
        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 opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Get the PSets:
        if 0:
            datasetsMgr.printSelections()
            #PrintPSet("BJetSelection", datasetsMgr, depth=150)

        # 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 a value is not specified
        if opts.intLumi < 0:
            opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()

        # Remove datasets
        removeList = ["QCD-b", "Charged"]
        if not opts.useMC:
            removeList.append("QCD")
        for i, d in enumerate(removeList, 0):
            msg = "Removing dataset %s" % d
            Verbose(
                ShellStyles.WarningLabel() + msg + ShellStyles.NormalStyle(),
                i == 0)
            datasetsMgr.remove(
                filter(lambda name: d in name,
                       datasetsMgr.getAllDatasetNames()))

        # Print summary of datasets to be used
        if 0:
            datasetsMgr.PrintInfo()

        # Merge EWK samples
        datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Do the fit on the histo after ALL selections (incl. topology cuts)
        folderList = datasetsMgr.getDataset(
            datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(
                opts.folder)
        #folderList1 = [h for h in folderList if "TetrajetMass" in h]
        #folderList1 = [h for h in folderList if "TetrajetBJetPt" in h]
        #folderList1 = [h for h in folderList if "TetrajetBJetEta" in h]
        folderList1 = [h for h in folderList if opts.histoKey in h]
        folderList2 = [
            h for h in folderList1 if "VR" in h or "SR" in h or "CRone" in h
            or "CRtwo" in h or "CRthree" in h or "CRfour" in h
        ]

        # For-loop: All folders
        histoPaths = []
        for f in folderList2:
            folderPath = os.path.join(opts.folder, f)
            histoList = datasetsMgr.getDataset(
                datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(
                    folderPath)
            pathList = [os.path.join(folderPath, h) for h in histoList]
            histoPaths.extend(pathList)

        binLabels = GetBinLabels("CRone", histoPaths)
        PlotHistosAndCalculateTF(datasetsMgr, histoPaths, binLabels, opts)
    return
Esempio n. 17
0
def main(opts):

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

    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)
            if d.getName() not in opts.signalMass:
                datasetsMgr.remove(d.getName())

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

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Do the topSelection histos
        folder = opts.folder
        histoList = datasetsMgr.getDataset(
            opts.signalMass[0]).getDirectoryContent(folder)
        histoPaths = [os.path.join(folder, h) for h in histoList]
        myHistos = [
            "Htb_tbW_WBoson_Quark_Htb_tbW_WBoson_AntiQuark_dEta",
            "Htb_tbW_WBoson_Quark_Htb_tbW_WBoson_AntiQuark_dPhi",
            "Htb_tbW_WBoson_Quark_Htb_tbW_WBoson_AntiQuark_dR",
            "Htb_tbW_BQuark_Htb_tbW_Wqq_Quark_dEta",
            "Htb_tbW_BQuark_Htb_tbW_Wqq_Quark_dPhi",
            "Htb_tbW_BQuark_Htb_tbW_Wqq_Quark_dR",
            "Htb_tbW_BQuark_Htb_tbW_Wqq_AntiQuark_dEta",
            "Htb_tbW_BQuark_Htb_tbW_Wqq_AntiQuark_dPhi",
            "Htb_tbW_BQuark_Htb_tbW_Wqq_AntiQuark_dR",
            "Htb_tbW_WBoson_Htb_tbW_BQuark_dEta",
            "Htb_tbW_WBoson_Htb_tbW_BQuark_dPhi",
            "Htb_tbW_WBoson_Htb_tbW_BQuark_dR"
        ]

        # For-loop: All histos in folder
        counter = 0
        for i, histo in enumerate(histoPaths, 1):
            h = histo.split("/")[-1]
            if h not in myHistos:
                continue
            counter += 1
            # Print("Plotting histogram %s" % (histo), counter==1)
            PlotMC(datasetsMgr, histo)
    return
Esempio n. 18
0
def main(hName, opts):

    # Setup the style
    style = tdrstyle.TDRStyle()
    
    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(opts.batchMode)

    # Setup & configure the dataset manager
    datasetsMgr = GetDatasetsFromDir(opts.mcrab, opts, **kwargs)
    datasetsMgr.updateNAllEventsToPUWeighted()
    # 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 datasets: All JetHT to "Data", QCD_Pt to "QCD", QCD_bEnriched to "QCD_b",  single-top to "SingleTop", WW, WZ, ZZ to "Diboson"           
    plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Remove datasets
    # datasetsMgr.remove("QCD-b") 
    
    # Print dataset information
    # datasetsMgr.PrintInfo()
    
    # Create  plot, with the default 
    s = {"normalizeToOne": True}
    p = plots.MCPlot(datasetsMgr, hName, **s)
    p.histoMgr.setHistoLegendStyleAll("LP")
    p.histoMgr.setHistoDrawStyleAll("EP")
    p.histoMgr.setHistoLegendStyle("ChargedHiggs_HplusTB_HplusToTB_M_500", "F")
    p.histoMgr.setHistoDrawStyle ("ChargedHiggs_HplusTB_HplusToTB_M_500", "HIST")

    # Create a comparison plot
    ratioOpts = {"ymin": 0.0, "ymax": 2.0}
    if kwargs.get("logY")==True:
        canvOpts = {"xmin": 0.0, "ymin": 1e-5, "ymaxfactor": 10}
    else:
        canvOpts = {"ymin": 0.0, "ymaxfactor": 1.2}

    # Draw a customised plot & Save it
    plots.drawPlot(p, 
                   os.path.join(kwargs.get("savePath"), hName.replace("/", "_").replace(" ", "_").replace("(", "_").replace(")", "") ),
                   xlabel=kwargs.get("xlabel"), 
                   ylabel=kwargs.get("ylabel"),
                   rebinX=kwargs.get("rebinX"), 
                   rebinY=kwargs.get("rebinY"),
                   xlabelsize=kwargs.get("xlabelsize"),
                   ratio=kwargs.get("ratio"), 
                   stackMCHistograms=kwargs.get("stackMCHistograms"), 
                   ratioYlabel=kwargs.get("ratioYlabel"),
                   ratioInvert=kwargs.get("ratioInvert"),
                   addMCUncertainty=kwargs.get("addMCUncertainty"), 
                   addLuminosityText=kwargs.get("addLuminosityText"),
                   addCmsText=kwargs.get("addCmsText"),
                   opts=canvOpts, opts2=ratioOpts, 
                   log=kwargs.get("logY"), 
                   errorBarsX=kwargs.get("errorBarsX"),
                   cmsExtraText=kwargs.get("cmsExtraText"),
                   moveLegend=kwargs.get("moveLegend"),
                   drawStyle="P",
                   legendStyle="LP",
                   #cutLine=kwargs.get("cutValue"),
                   cutBox={"cutValue": kwargs.get("cutValue"), "fillColor": kwargs.get("cutFillColour"), "box": kwargs.get("cutBox"), "line": kwargs.get("cutLine"), "lessThan": kwargs.get("cutLessthan")},
                   )
    
    # Remove legend?
    if kwargs.get("removeLegend"):
        p.removeLegend()

    # Additional text
    # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
    # histograms.addText(0.4, 0.11, "Runs " + datasetsMgr.loadRunRange(), 17)
    
    if not opts.batchMode:
        raw_input("=== plotEventVars.py:\n\tPress any key to quit ROOT ...")

    return
Esempio n. 19
0
def main(opts):

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

        # Quick and dirty way to get total int lumi
        allDatasetsMgr = GetAllDatasetsFromDir(opts)
        allDatasetsMgr.updateNAllEventsToPUWeighted()
        allDatasetsMgr.loadLuminosities() # from lumi.json
        plots.mergeRenameReorderForDataMC(allDatasetsMgr) 
        opts.intLumi = GetLumi(allDatasetsMgr)

        # 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 d != "TT":
                #    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
Esempio n. 20
0
def main():

    style = tdrstyle.TDRStyle()

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

    # Get all datasets from the mcrab dir
    datasetsMgr = GetDatasetsFromDir(parseOpts.mcrab, kwargs.get("analysis"))

    # Determine Integrated Luminosity (If Data datasets present)
    intLumi = GetLumi(datasetsMgr)

    # Update to PU
    datasetsMgr.updateNAllEventsToPUWeighted()

    # Remove datasets
    datasetsMgr.remove(kwargs.get("rmDataset"))
    # datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "QCD" in name in name, datasetsMgr.getAllDatasetNames()))

    # Set custom XSections
    # d.getDataset("TT_ext3").setCrossSection(831.76)

    # Default merging & ordering: "Data", "QCD", "SingleTop", "Diboson"
    plots.mergeRenameReorderForDataMC(
        datasetsMgr
    )  #WARNING: Merged MC histograms must be normalized to something!

    # Remove datasets (for merged names)
    datasetsMgr.remove(kwargs.get("rmDataset"))

    # For-loop: All Histogram names
    for counter, hName in enumerate(hNames):

        # Get the save path and name
        savePath, saveName = GetSavePathAndName(hName, **kwargs)

        # Get Histos for Plotter
        refHisto, otherHistos = GetHistosForPlotter(datasetsMgr, hName,
                                                    **kwargs)

        # Create a comparison plot
        p = plots.ComparisonManyPlot(refHisto, otherHistos)

        # Remove negative contributions
        #RemoveNegativeBins(datasetsMgr, hName, p)

        # Create a frame
        if kwargs.get("logY"):
            opts = {"ymin": 1e-5, "ymaxfactor": 10}
        else:
            opts = {"ymin": 0.0, "ymaxfactor": 1.2}
        ratioOpts = {"ymin": 0.0, "ymax": 2.0}
        p.createFrame(saveName,
                      createRatio=kwargs.get("createRatio"),
                      opts=opts,
                      opts2=ratioOpts)

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

        # Customise frame
        p.getFrame().GetYaxis().SetTitle(getTitleY(refHisto, **kwargs))
        #p.setEnergy("13")
        if kwargs.get("createRatio"):
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)

        # SetLog
        SetLogAndGrid(p, **kwargs)

        # Add cut line/box
        _kwargs = {"lessThan": kwargs.get("cutLessThan")}
        p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"),
                           fillColor=kwargs.get("cutFillColour"),
                           box=kwargs.get("cutBox"),
                           line=kwargs.get("cutLine"),
                           **_kwargs)

        # Move the refDataset to first in the draw order (back)
        histoNames = [h.getName() for h in p.histoMgr.getHistos()]
        p.histoMgr.reorder(
            filter(
                lambda n: plots._legendLabels[kwargs.get("refDataset")] not in
                n, histoNames))

        #  Draw plots
        p.draw()

        # Customise text
        histograms.addStandardTexts(lumi=intLumi)
        # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
        # histograms.addText(0.4, 0.11, "Runs " + datasetsMgr.loadRunRange(), 17)

        # Save canvas under custom dir
        SaveAs(p, savePath, saveName, kwargs.get("saveFormats"))

    return
def main(opts):

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

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

        # Numerator & Denominator dataset manager
        noSF_datasetsMgr      = GetDatasetsFromDir(opts, opts.noSFcrab)
        withCR1SF_datasetsMgr = GetDatasetsFromDir(opts, opts.withCR1SFcrab) 
        withCR2SF_datasetsMgr = GetDatasetsFromDir(opts, opts.withCR2SFcrab) 
        
        # Update all events to PU weighting
        noSF_datasetsMgr.updateNAllEventsToPUWeighted()
        withCR1SF_datasetsMgr.updateNAllEventsToPUWeighted()
        withCR2SF_datasetsMgr.updateNAllEventsToPUWeighted()
        
        # Load Luminosities
        noSF_datasetsMgr.loadLuminosities()
        withCR1SF_datasetsMgr.loadLuminosities()
        withCR2SF_datasetsMgr.loadLuminosities()
        
        
        if 0:
            noSF_datasetsMgr.PrintCrossSections()
            noSF_datasetsMgr.PrintLuminosities()
 
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(noSF_datasetsMgr) 
        plots.mergeRenameReorderForDataMC(withCR1SF_datasetsMgr) 
        plots.mergeRenameReorderForDataMC(withCR2SF_datasetsMgr) 
        
        # Get luminosity if a value is not specified
        if opts.intLumi < 0:
            opts.intLumi = noSF_datasetsMgr.getDataset("Data").getLuminosity()
            
        # Remove datasets
        removeList = []
        #removeList = ["TTWJetsToLNu_", "TTWJetsToQQ"]
        for i, d in enumerate(removeList, 0):
            msg = "Removing dataset %s" % d
            Print(ShellStyles.WarningLabel() + msg + ShellStyles.NormalStyle(), i==0)
            noSF_datasetsMgr.remove(filter(lambda name: d in name, noSF_datasetsMgr.getAllDatasetNames()))
            withCR1SF_datasetsMgr.remove(filter(lambda name: d in name, withCR1SF_datasetsMgr.getAllDatasetNames()))
        
        # Print summary of datasets to be used
        if 0:
            noSF_datasetsMgr.PrintInfo()
            withCR1SF_datasetsMgr.PrintInfo()
            withCR2SF_datasetsMgr.PrintInfo()
            
        # Merge EWK samples
        EwkDatasets = ["Diboson", "DYJetsToLL", "WJetsHT"]
        noSF_datasetsMgr.merge("EWK", EwkDatasets)
        withCR1SF_datasetsMgr.merge("EWK", EwkDatasets)
        withCR2SF_datasetsMgr.merge("EWK", EwkDatasets)
        
        # Get histosgram names
        folderListIncl = withCR1SF_datasetsMgr.getDataset(withCR1SF_datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(opts.folder)
        folderList = [h for h in folderListIncl if "AfterAllSelections_LeadingTrijet_Pt" in h ]

        # For-loop: All histo paths
        for h in folderList:
            if "lowMET" in h:
                folderList.remove(h)
        
        folderPath    = os.path.join(opts.folder, "")
        folderPathGen = os.path.join(opts.folder + "Genuine")
        folderPathFake =os.path.join(opts.folder + "Fake"   )
        
        histoList = folderList
        num_pathList = [os.path.join(folderPath, h) for h in histoList]
        num_pathList.extend([os.path.join(folderPathGen, h) for h in histoList])
        num_pathList.extend([os.path.join(folderPathFake, h) for h in histoList])
        
        # Denominator Histogram (To be used in the estimation of QCD Data-Driven)
        histoList = [h for h in folderListIncl if "AfterStandardSelections_LeadingTrijet_Pt" in h]
        den_pathList = [os.path.join(folderPath, h) for h in histoList]
        den_pathList.extend([os.path.join(folderPathGen, h) for h in histoList])
        den_pathList.extend([os.path.join(folderPathFake, h) for h in histoList])

        # For-loop: All histo paths
        for h in den_pathList:
            if "lowMET" in h:
                den_pathList.remove(h)
        
        # Do the histograms
        PlotHistos(noSF_datasetsMgr, withCR1SF_datasetsMgr, withCR2SF_datasetsMgr, num_pathList, den_pathList,  opts)
        
    return
Esempio n. 22
0
def main(count, runRange, dsetList, opts):

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

    # 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("")
        optModes = optList
    else:
        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 opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Remove datasets
        removeList = ["QCD-b", "Charged", "QCD", "ZJetsToQQ_HT600toInf"]
        opts.intLumi = 0.0

        # For-loop: All datasets in the manager
        for d in datasetsMgr.getAllDatasets():
            if d.isMC():
                continue

            # Inclusive data
            if len(dsetList) == 1 and dsetList[0] == "Run2016":
                Verbose("Inclusive data. Will not remove anything", True)
                opts.intLumi += GetLumi(datasetsMgr)
                break

            # Special combinations
            for rr in dsetList:
                if rr not in d.getName():
                    Verbose(
                        "\"%s\" is not in dataset name \"%s\"" %
                        (rr, d.getName()), False)
                    if d.getName() not in removeList:
                        # Ensure dataset to be removed is not in the dsetList
                        if not any(rr in d.getName() for rr in dsetList):
                            removeList.append(d.getName())
                else:
                    Verbose(
                        "\"%s\" is in dataset name \"%s\"" % (rr, d.getName()),
                        False)
                    # Get luminosity if a value is not specified
                    opts.intLumi += d.getLuminosity()

        # For-loop: All dataset names to be removed
        for i, d in enumerate(removeList, 0):
            Verbose(
                ShellStyles.HighlightAltStyle() + "Removing dataset %s" % d +
                ShellStyles.NormalStyle(), False)
            datasetsMgr.remove(
                filter(lambda name: d in name,
                       datasetsMgr.getAllDatasetNames()))

        # Inform user of dataset and corresponding integrated lumi
        Print("%d) %s (%.1f 1/pb)" % (count, runRange, opts.intLumi),
              count == 1)
        #Print("%d) %s (%.1f 1/pb)" % (count, ", ".join(dsetList), opts.intLumi), count==1)

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

        # Print dataset information
        # datasetsMgr.PrintInfo()

        # Merge EWK samples
        datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())

        # Do the fit on the histo after ALL selections (incl. topology cuts)
        folderList = datasetsMgr.getDataset(
            datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(
                opts.folder)
        folderList1 = [h for h in folderList if opts.histoKey in h]
        folderList2 = [
            h for h in folderList1 if "VR" in h or "SR" in h or "CRone" in h
            or "CRtwo" in h or "CRthree" in h or "CRfour" in h
        ]

        # For-loop: All folders
        histoPaths = []
        for f in folderList2:
            folderPath = os.path.join(opts.folder, f)
            histoList = datasetsMgr.getDataset(
                datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(
                    folderPath)
            pathList = [os.path.join(folderPath, h) for h in histoList]
            histoPaths.extend(pathList)

        binLabels = GetBinLabels("CRone", histoPaths)
        # Calculate TF and plot histo (Data Vs Fake b + Genuine b)
        stackListForRunRange = PlotHistosAndCalculateTF(
            runRange, datasetsMgr, histoPaths, binLabels, opts)
    return stackListForRunRange
Esempio n. 23
0
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):

    # 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
Esempio n. 25
0
def main(opts):

    style    = tdrstyle.TDRStyle()
    
    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(opts.batchMode)
    
    # Get all datasets from the mcrab dir
    # def GetDatasetsFromDir(mcrab, opts, **kwargs): #iro
    datasetsMgr       = GetDatasetsFromDir(opts.mcrab, opts, **kwargs) #kwargs.get("analysis"))

    # Determine Integrated Luminosity (If Data datasets present)
    intLumi = GetLumi(datasetsMgr)
    
    # Update to PU
    datasetsMgr.updateNAllEventsToPUWeighted()

    # Remove datasets
    datasetsMgr.remove(kwargs.get("rmDataset"))
    # datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "QCD" in name in name, datasetsMgr.getAllDatasetNames()))
    
    # Set custom XSections
    #datasetsMgr.getDataset("QCD_bEnriched_HT1000to1500").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT1500to2000").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT2000toInf").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT300to500").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT500to700").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT700to1000").setCrossSection(1.0)
    
    # Default merging & ordering: "Data", "QCD", "SingleTop", "Diboson"
    plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Remove datasets (for merged names)
    datasetsMgr.remove(kwargs.get("rmDataset"))
    
    # Print the cross
    datasetsMgr.PrintCrossSections()

    for h_prefix in ["reco"]:
        # Get ref histo here and the fixed histos (TT, QCD, QCD-b
        inclusiveHisto, fixedHistos = GetHistosForPlotter(datasetsMgr, h_prefix + "MET_Et", **kwargs)
        inclusiveHisto.setName("Inclusive")
        inclusiveHisto.setLegendLabel("Inclusive")
        #for hi in fixedHistos:
        #    print(type(hi), hi.getName())
        #return
        # For-loop: All Histogram names
        for counter, hName in enumerate(hNames):
            # Get the save path and name
            savePath, saveName = GetSavePathAndName(h_prefix + hName[0], **kwargs)
            saveName = savePath + h_prefix + hSaveNames[counter]

            # Get Histos for Plotter
            refHisto_, otherHistos_ = GetHistosForPlotter(datasetsMgr, h_prefix + hName[0], **kwargs)
            refHisto1, otherHistos2 = GetHistosForPlotter(datasetsMgr, h_prefix + hName[1], **kwargs)
            refHisto2, otherHistos2 = GetHistosForPlotter(datasetsMgr, h_prefix + hName[2], **kwargs)
            refHisto3, otherHistos2 = GetHistosForPlotter(datasetsMgr, h_prefix + hName[3], **kwargs)
            refHisto4, otherHistos2 = GetHistosForPlotter(datasetsMgr, h_prefix + hName[4], **kwargs)
            refHisto5, otherHistos2 = GetHistosForPlotter(datasetsMgr, h_prefix + hName[5], **kwargs)
            refHisto = inclusiveHisto

            # customize histos
            refHisto_.setName(hLegends[counter][0])
            refHisto_.setLegendLabel(hLegends[counter][0])
            refHisto_.setDrawStyle("P")
            refHisto_.setLegendStyle("P")
            styleDict[hStyles[0]].apply(refHisto_.getRootHisto())

            refHisto1.setName(hLegends[counter][1])
            refHisto1.setLegendLabel(hLegends[counter][1])
            refHisto1.setDrawStyle("P")
            refHisto1.setLegendStyle("P")
            styleDict[hStyles[1]].apply(refHisto1.getRootHisto())

            refHisto2.setName(hLegends[counter][2])
            refHisto2.setLegendLabel(hLegends[counter][2])
            refHisto2.setDrawStyle("P")
            refHisto2.setLegendStyle("P")
            styleDict[hStyles[2]].apply(refHisto2.getRootHisto())

            refHisto3.setName(hLegends[counter][3])
            refHisto3.setLegendLabel(hLegends[counter][3])
            refHisto3.setDrawStyle("P")
            refHisto3.setLegendStyle("P")
            styleDict[hStyles[3]].apply(refHisto3.getRootHisto())

            refHisto4.setName(hLegends[counter][4])
            refHisto4.setLegendLabel(hLegends[counter][4])
            refHisto4.setDrawStyle("P")
            refHisto4.setLegendStyle("P")
            styleDict[hStyles[4]].apply(refHisto4.getRootHisto())

            refHisto5.setName(hLegends[counter][5])
            refHisto5.setLegendLabel(hLegends[counter][5])
            refHisto5.setDrawStyle("P")
            refHisto5.setLegendStyle("P")
            styleDict[hStyles[5]].apply(refHisto5.getRootHisto())

            otherHistos = [refHisto_, refHisto1, refHisto2, refHisto3, refHisto4, refHisto5]
            #for hi in otherHistos:
            #     print hi.getName()
            #return

            # Create a comparison plot
            p = plots.ComparisonManyPlot(refHisto, otherHistos)

            # Remove negative contributions
            #RemoveNegativeBins(datasetsMgr, hName, p)

            # Create a frame
            if kwargs.get("logY")==True:
                opts = {"ymin": 8e-5, "ymax": 2}
                #opts = {"ymin": 1e-3, "ymax": 1}
            else:
                opts = {"ymin": 8.e-5, "ymax": 2}
            ratioOpts = {"ymin": 0.1, "ymax": 10.0}
            p.createFrame(saveName, createRatio=kwargs.get("createRatio"), opts=opts, opts2=ratioOpts)
            
            # Customise Legend
            moveLegend = {"dx": -0.2, "dy": +0.0, "dh": -0.1}
            p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
            #p.removeLegend()

            # Customise frame
            p.getFrame().GetYaxis().SetTitle( getTitleY(refHisto, **kwargs) )
            #p.setEnergy("13")
            if kwargs.get("createRatio"):
                p.getFrame2().GetYaxis().SetTitle("Ratio")
                p.getFrame2().GetYaxis().SetTitleOffset(1.7)

            # SetLog
            SetLogAndGrid(p, **kwargs)

            # Add cut line/box
            _kwargs = { "lessThan": kwargs.get("cutLessThan")}
            p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"), fillColor=kwargs.get("cutFillColour"), box=kwargs.get("cutBox"), line=kwargs.get("cutLine"), **_kwargs)
            
            # Move the refDataset to first in the draw order (back)
            histoNames = [h.getName() for h in p.histoMgr.getHistos()]
            p.histoMgr.reorder(filter(lambda n: plots._legendLabels[kwargs.get("refDataset") ] not in n, histoNames))
            p.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(2))

            #  Draw plots
            p.draw()

            # Customise text
            histograms.addStandardTexts(lumi=intLumi)
            # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
            histograms.addText(0.2, 0.88, plots._legendLabels[kwargs.get("refDataset")], 17)

            # Save canvas under custom dir
            if not os.path.exists(savePath):
                    os.mkdir(savePath)
            SaveAs(p, savePath, saveName, kwargs.get("saveFormats"))

    return
def main():

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

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

    # Take QCD from data
    datasetsQCD = None

    if QCDfromData:

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

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

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

# Remove signals other than M120
    datasets.remove(
        filter(lambda name: "TTToHplus" in name,
               datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "HplusTB" in name, datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "Hplus_taunu_t-channel" in name,
               datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "Hplus_taunu_tW-channel" in name,
               datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "TTJets_SemiLept" in name,
               datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "TTJets_FullLept" in name,
               datasets.getAllDatasetNames()))
    datasets.remove(
        filter(lambda name: "TTJets_Hadronic" in name,
               datasets.getAllDatasetNames()))

    plots.mergeRenameReorderForDataMC(datasets)

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

    datasets_lands = datasets.deepCopy()

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

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

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

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

    # Apply TDR style
    style = tdrstyle.TDRStyle()

    # Create plots
    doPlots(datasets)

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

    # Print counters
    doCounters(datasets)
def main(opts):

    style = tdrstyle.TDRStyle()
    #style.setWide(True)
    style.setPaletteMy()
    ROOT.gStyle.SetNumberContours(20)
    # tdrstyle.setDeepSeaPalette()
    # tdrstyle.setRainBowPalette()
    # tdrstyle.setDarkBodyRadiatorPalette()
    # tdrstyle.setGreyScalePalette()
    # tdrstyle.setTwoColorHuePalette()
 
    
    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(opts.batchMode)
    
    # ========================================
    # Datasets
    # ========================================
    # Setup & configure the dataset manager
    datasetsMgr = GetDatasetsFromDir(opts.mcrab, opts, **kwargs)
    intLumi     = GetLumi(datasetsMgr)
    datasetsMgr.updateNAllEventsToPUWeighted()
    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 datasts (Note: Merged MC histograms must be normalized to something)
    plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Remove datasets
    if 0:
        datasetsMgr.remove("TTJets")
        datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    
    # Print dataset information
    datasetsMgr.PrintInfo()
                  
    # For-loop: All Histogram names
    for counter, hName in enumerate(hNames):
        savePath, saveName = GetSavePathAndName(hName, **kwargs)                

        # Get Histos for Plotter
        refHisto, otherHistos = GetHistosForPlotter(datasetsMgr, hName, **kwargs)

        # Create a plot
        p = plots.PlotBase([refHisto], kwargs.get("saveFormats"))
        
        # Remove negative contributions
        #RemoveNegativeBins(datasetsMgr, hName, p)

        # Customize
        # p.histoMgr.setHistoDrawStyleAll("COL") #"CONT4" "COLZ" "COL"
        p.histoMgr.forEachHisto(lambda h: h.getRootHisto().RebinX(kwargs.get("rebinX")))
        p.histoMgr.forEachHisto(lambda h: h.getRootHisto().RebinY(kwargs.get("rebinY")))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().GetXaxis().SetRangeUser(1.0, 5.0))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().GetYaxis().SetRangeUser(1.0, 5.0))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().GetZaxis().SetRangeUser(0.0, 0.015))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMinimum(kwargs.get("zMin")))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMaximum(kwargs.get("zMax")))
        p.histoMgr.forEachHisto(lambda h: h.getRootHisto().RebinX(2))
        p.histoMgr.forEachHisto(lambda h: h.getRootHisto().RebinY(2))

#        p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMinimum(0.0015))
#        p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMaximum(0.0045))



        
        # Create a frame
        opts = {"ymin": 0.0, "ymaxfactor": 1.0}
        p.createFrame(saveName, opts=opts)


        # Customise frame
        p.getFrame().GetXaxis().SetTitle( getTitleX(refHisto, **kwargs) )
        #p.getFrame().GetYaxis().SetTitle( getTitleY(refHisto, **kwargs) )
        
        # SetLog
        SetLogAndGrid(p, **kwargs)

        # Add cut line/box
        _kwargs = { "lessThan": kwargs.get("cutLessThan")}
        p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"), fillColor=kwargs.get("cutFillColour"), box=kwargs.get("cutBox"), line=kwargs.get("cutLine"), **_kwargs)
        
        # Customise Legend
        moveLegend = {"dx": -0.1, "dy": +0.0, "dh": -0.1}
        p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
        p.removeLegend()

        # Add MC Uncertainty (Invalid method for a 2-d histogram)
        #p.addMCUncertainty()
        
        #  Draw plots
        p.draw()

        # Customise text
        histograms.addStandardTexts(lumi=intLumi)
        # histograms.addText(0.17, 0.95, plots._legendLabels[kwargs.get("refDataset")], 22)
        histograms.addText(0.17, 0.88, plots._legendLabels[kwargs.get("refDataset")], 17)
        
        # Save canvas under custom dir
        SaveAs(p, savePath, saveName, kwargs.get("saveFormats"), counter==0)

    return
def main(opts):

    #optModes = ["", "OptChiSqrCutValue50p0", "OptChiSqrCutValue100p0", "OptChiSqrCutValue200p0"]
    optModes = [
        "", "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)

        # Only do these histos
        myHistos = [
            "FailedBJet1BDisc",
            "FailedBJet1Pt",
            "FailedBJet1Eta",
            #"FailedBJet1PdgId",
            #"FailedBJet1PartonFlavour",
            #"FailedBJet1HadronFlavour",
            #"FailedBJet1Ancestry"
        ]

        # For-loop: All histos
        folders = ["", "FakeB", "GenuineB"]
        for f in folders:

            folder = "FailedBJet" + f
            hList = datasetsMgr.getDataset("EWK").getDirectoryContent(folder)

            for hName in hList:
                if hName.split("_")[-2] not in myHistos:
                    continue
                PlotHisto(datasetsMgr, os.path.join(folder, hName))
    return
Esempio n. 29
0
def main(opts):

    optModes = [""]

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

        # Quick and dirty way to get total int lumi
        opts2 = copy.deepcopy(opts)
        opts2.includeOnlyTasks = ""
        allDatasetsMgr = GetDatasetsFromDir(opts2)
        allDatasetsMgr.updateNAllEventsToPUWeighted()
        allDatasetsMgr.loadLuminosities() # from lumi.json
        opts.intLumi = GetLumi(allDatasetsMgr)/2

        # 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"]
        allHistos   = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(opts.folder)
        histoList   = [h for h in allHistos if "_Vs_" in h]
        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 histograms in list        
        for histoName in histoPaths:
            Plot2d(datasetsMgr, histoName, histoKwargs[histoName], opts)
    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
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for d in datasetsMgr.getAllDatasets():
            if "ChargedHiggs_HplusTB_HplusToTB_M_%s" % (
                    opts.signalMass) in d.getName():
                datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
            #else:
            #    datasetsMgr.remove(d.getName())

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        else:
            opts.intLumi = 35920

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

        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():

            if str(opts.signalMass) not in d.getName():
                #if d not in "%s" % (opts.signalMass):
                continue
            datasetOrder.append(d.getName())

        # Print dataset information
        datasetsMgr.PrintInfo()

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

        for dName in [
                "TT", "QCD",
                "ChargedHiggs_HplusTB_HplusToTB_M_%s" % (opts.signalMass)
        ]:
            for var in ["M", "Pt", "BDT", "Eta"]:
                # Do the histograms
                PlotHistograms(datasetsMgr, dName, var)

    return
Esempio n. 31
0
def main(opts):
    
    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)
    style.setGridX(False)
    style.setGridY(False)
    
     # 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() 
        
        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()
            
        # Print dataset information before removing anything?
        if 1:
            datasetsMgr.PrintInfo()
            
        # Remove datasets
        filterKeys = ["TTW"]
        for key in filterKeys:
            datasetsMgr.remove(filter(lambda name: key in name, datasetsMgr.getAllDatasetNames()))
            
        # Re-order datasets
        datasetOrder = []
        haveQCD = False
        for d in datasetsMgr.getAllDatasets():
            if "QCD" in d.getName():
                haveQCD = True
            datasetOrder.append(d.getName())
        datasetsMgr.selectAndReorder(datasetOrder)
        
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Merge EWK datasets
        EWKlist = ["WJetsHT", "DYJetsToLL", "Diboson"]
        datasetsMgr.merge("EWK", EWKlist)
        
        # Print dataset information after merging
        datasetsMgr.PrintInfo()
        
        # Determine integrated Lumi before removing data
        intLumi = datasetsMgr.getDataset('Data').getLuminosity()
        
        # Define Numerator & Denominator Histograms
        numerator_name   = "AfterAllSelections_LeadingTrijet_Pt"
        denominator_name = "AfterStandardSelections_LeadingTrijet_Pt" 
        
        # Do the fit on the histo after ALL selections (incl. topology cuts)
        folderListIncl = datasetsMgr.getDataset(datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(opts.folder)
        folderList = [h for h in folderListIncl if "AfterAllSelections_LeadingTrijet_Pt" in h ]
        
        folderPath = os.path.join(opts.folder, "")
        folderPathGen = os.path.join(opts.folder+"Genuine", "")
        folderPathFake =os.path.join(opts.folder+"Fake", "")
        
        histoList = folderList
        num_pathList = [os.path.join(folderPath, h) for h in histoList]
        num_pathList.extend([os.path.join(folderPathGen, h) for h in histoList])
        num_pathList.extend([os.path.join(folderPathFake, h) for h in histoList])
        
        for h in num_pathList:
            if "lowMET" in h:
                num_pathList.remove(h)

        histoList = [h for h in folderListIncl if "AfterStandardSelections_LeadingTrijet_Pt" in h]
        den_pathList = [os.path.join(folderPath, h) for h in histoList]
        den_pathList.extend([os.path.join(folderPathGen, h) for h in histoList])
        den_pathList.extend([os.path.join(folderPathFake, h) for h in histoList])
        
        for h in den_pathList:
            if "lowMET" in h:
                den_pathList.remove(h)
                
        # Calculate Scale Factors
        GetScaleFactors(datasetsMgr, num_pathList, den_pathList, intLumi)
        
        
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)
    style.setGridX(False)
    style.setGridY(False)
    
    # 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 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)

        # Print dataset information before removing anything?
        datasetsMgr.PrintInfo()

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            intLumi = datasetsMgr.getDataset("Data").getLuminosity()

        # Remove datasets
        filterKeys = ["TTW"]
        for key in filterKeys:
            datasetsMgr.remove(filter(lambda name: key in name, datasetsMgr.getAllDatasetNames()))
        else:
            intLumi = 35920

        for key in filterKeys:
            datasetsMgr.remove(filter(lambda name: key in name, datasetsMgr.getAllDatasetNames()))
        
        # Re-order datasets
        datasetOrder = []
        haveQCD = False
        for d in datasetsMgr.getAllDatasets():
            if "QCD" in d.getName():
                haveQCD = True
            datasetOrder.append(d.getName())
        datasetsMgr.selectAndReorder(datasetOrder)

        # Define the mapping histograms in numerator->denominator pairs
        VariableList = [
            "LdgTop_Pt",
            ]

        minRunRange, maxRunRange, runRange = GetRunRange(datasetsMgr)

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        datasets_  = datasetsMgr.getAllDatasets()
        dataset_Data = datasetsMgr.getDataDatasets()
        dataset_MC   = datasetsMgr.getMCDatasets()

        # Print dataset information
        datasetsMgr.PrintInfo()

        # For-loop: All numerator-denominator pairs
        counter =  0
        nPlots  = len(VariableList)

        for var in VariableList:
            histoN = "AfterAllSelections_"+var
            histoD = "AfterStandardSelections_"+var
            numerator   = os.path.join(opts.folder, histoN)
            denominator = os.path.join(opts.folder, histoD)

            counter+=1
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % counter, "/", "%s:" % (nPlots), "%s" % (var))
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), counter==1)
            
            for dataset in datasets_:

                if dataset.isMC():
                    n = dataset.getDatasetRootHisto(numerator)
                    n.normalizeToLuminosity(intLumi)
                    num = n.getHistogram()
                    d = dataset.getDatasetRootHisto(denominator)
                    d.normalizeToLuminosity(intLumi)
                    den = d.getHistogram()
                    
                else:
                    num = dataset.getDatasetRootHisto(numerator).getHistogram()
                    den = dataset.getDatasetRootHisto(denominator).getHistogram()
                                                            
                x_bins = num.GetXaxis().GetNbins()
                
                i = 1
                while i < x_bins:
                    xvalue = num.GetXaxis().GetBinLowEdge(i)+0.5*num.GetXaxis().GetBinWidth(i)
                    if xvalue < 20:
                        my_bin = i
                    i+=1
                        
                my_xvalue = num.GetXaxis().GetBinUpEdge(my_bin)+0.5*num.GetXaxis().GetBinWidth(my_bin)
                total = den.Integral(0, x_bins) #my_bin
                selected = num.Integral(0, x_bins) #my_bin
                            
            plotName     = "Eff_%s" % (var)
            # Get Efficiency Plots  
            _kwargs  = GetHistoKwargs(var, opts)
            eff_Data = GetEfficiency(datasetsMgr, dataset_Data, numerator, denominator, intLumi)
            eff_MC   = GetEfficiency(datasetsMgr, dataset_MC, numerator, denominator, intLumi)

            # Apply Styles 
            styles.dataStyle.apply(eff_Data)
            styles.qcdStyle.apply(eff_MC)
            # Create the plot
            p = plots.ComparisonPlot(histograms.HistoGraph(eff_Data, "eff_Data", "p", "P"),
                                     histograms.HistoGraph(eff_MC,   "eff_MC"  , "p", "P"),
                                     saveFormats=[])
            # Define the legend entries 
            p.histoMgr.setHistoLegendLabelMany(
                {
                    "eff_Data": "Data",
                    "eff_MC"  : "QCD"
                    }
                )

            # Append in list
            myList = []
            myList.append(histograms.HistoGraph(eff_Data, plots._legendLabels["Data"], "lp", "P"))

            p.setLuminosity(intLumi)
            _kwargs["ratio"] = True
            _kwargs["opts"]   = {"xmin": 0.0, "xmax": 600.0, "ymin": 0.0, "ymax": 0.16, "ymaxfactor": 1.8}
            _kwargs["cutBoxY"] = {"cutValue": 1.10, "fillColor": ROOT.kGray+1, "fillStyle": 3001, "box": False, "line": True, "greaterThan": True, "mainCanvas": False, 
                                  "ratioCanvas": True, "mirror": True}
            plots.drawPlot(p, plotName, **_kwargs)

            # Draw 
            savePath = os.path.join(opts.saveDir, opts.optMode)
            SavePlot(p, plotName, savePath, saveFormats = [".png", ".pdf", ".C"])
        
    # Save results in JSON
    name = opts.mcrab.split("_")[-3]
    name = name.replace(opts.analysisName, "")
    jsonName = "topMisID_"+ name +"_TopMassCut400.json"
    analysis = opts.analysisName
    label = "2016"
    plotDir =  os.path.join(opts.folder, jsonName)
    pythonWriter.addParameters(plotDir, label, runRange, opts.intLumi, eff_Data)
    pythonWriter.addMCParameters(label, eff_MC)
    fileName_json = jsonName
    pythonWriter.writeJSON(fileName_json)


        
    return
Esempio n. 33
0
def main(opts):

    # Setup the style
    style = tdrstyle.TDRStyle()

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

    # ========================================
    # Datasets
    # ========================================
    # Setup & configure the dataset manager
    datasetsMgr = GetDatasetsFromDir(opts.mcrab, opts, **kwargs)
    intLumi = GetLumi(datasetsMgr)
    datasetsMgr.updateNAllEventsToPUWeighted()
    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 datasets:All JetHT to "Data", QCD_Pt to "QCD",
    # QCD_bEnriched to "QCD_b",  single-top to "SingleTop", WW, WZ, ZZ to "Diboson"
    plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Remove datasets
    if 0:
        datasetsMgr.remove("TTJets")
        datasetsMgr.remove(
            filter(lambda name: not "QCD" in name,
                   datasetsMgr.getAllDatasetNames()))

    # Print dataset information
    datasetsMgr.PrintInfo()

    # ========================================
    # Histograms
    # ========================================
    for counter, hName in enumerate(hNames):

        # Get the save path and name, Get Histos for Plotter
        savePath, saveName = GetSavePathAndName(hName, **kwargs)
        refHisto, otherHistos = GetHistosForPlotter(datasetsMgr, hName,
                                                    **kwargs)

        # Create a comparison plot
        #p = plots.ComparisonManyPlot(refHisto, otherHistos)
        p = plots.DataMCPlot(datasetsMgr, hName)

        # Remove negative contributions
        if 0:
            RemoveNegativeBins(datasetsMgr, hName, p)

        # Y-axis
        ratioOpts = {"ymin": 0.0, "ymax": 2.0}
        if kwargs.get("logY") == True:
            opts = {"ymin": 1e-2, "ymaxfactor": 100}
        else:
            opts = {"ymin": 0.0, "ymaxfactor": 1.2}

        # ========================================
        # Frame
        # ========================================
        p.createFrame(saveName,
                      createRatio=kwargs.get("createRatio"),
                      opts=opts,
                      opts2=ratioOpts)

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

        # Move the refDataset to first in the draw order (back)
        histoNames = [h.getName() for h in p.histoMgr.getHistos()]
        p.histoMgr.reorder(
            filter(
                lambda n: plots._legendLabels[kwargs.get("refDataset")] not in
                n, histoNames))
        if 0:
            p.removeLegend()

        # Axes
        #p.getFrame().GetYaxis().SetTitle( getTitleY(refHisto, **kwargs) )
        #p.getFrame().GetYaxis().SetTitle( getTitleY(p.histoMgr.getHistos()[0], **kwargs) )
        if kwargs.get("createRatio"):
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)

        # Set Log and Grid
        SetLogAndGrid(p, **kwargs)

        # Cut line / Cut box
        _kwargs = {"lessThan": kwargs.get("cutLessthan")}
        p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"),
                           fillColor=kwargs.get("cutFillColour"),
                           box=kwargs.get("cutBox"),
                           line=kwargs.get("cutLine"),
                           **_kwargs)

        # Draw the final plot
        p.draw()

        # ========================================
        # Add Text
        # ========================================
        histograms.addStandardTexts(lumi=intLumi)
        # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
        # histograms.addText(0.4, 0.11, "Runs " + datasetsMgr.loadRunRange(), 17)

        # Save the canvas to a file
        SaveAs(p, savePath, saveName, kwargs.get("saveFormats"), counter == 0)

    return
Esempio n. 34
0
def main(opts):

    # Setup the style
    style = tdrstyle.TDRStyle()
    
    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(opts.batchMode)

    # ========================================
    # Datasets
    # ========================================
    # Setup & configure the dataset manager
    datasetsMgr = GetDatasetsFromDir(opts.mcrab, opts, **kwargs)
    intLumi     = GetLumi(datasetsMgr)
    datasetsMgr.updateNAllEventsToPUWeighted()
    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 datasets:All JetHT to "Data", QCD_Pt to "QCD", 
    # QCD_bEnriched to "QCD_b",  single-top to "SingleTop", WW, WZ, ZZ to "Diboson"           
    plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Remove datasets
    if 0:
        datasetsMgr.remove("TTJets")
        datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    
    # Print dataset information
    datasetsMgr.PrintInfo()

    # ========================================
    # Histograms
    # ========================================
    for counter, hName in enumerate(hNames):
        
        # Get the save path and name, Get Histos for Plotter
        savePath, saveName    = GetSavePathAndName(hName, **kwargs)
        refHisto, otherHistos = GetHistosForPlotter(datasetsMgr, hName, **kwargs)
        
        # Create a comparison plot
        #p = plots.ComparisonManyPlot(refHisto, otherHistos)
        p = plots.DataMCPlot(datasetsMgr, hName)

        # Remove negative contributions
        if 0:
            RemoveNegativeBins(datasetsMgr, hName, p)

        # Y-axis
        ratioOpts = {"ymin": 0.0, "ymax": 2.0}
        if kwargs.get("logY")==True:
            opts = {"ymin": 1e-2, "ymaxfactor": 100}
        else:
            opts = {"ymin": 0.0, "ymaxfactor": 1.2}

        # ========================================
        # Frame
        # ========================================
        p.createFrame(saveName, createRatio=kwargs.get("createRatio"), opts=opts, opts2=ratioOpts)

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

        # Move the refDataset to first in the draw order (back)
        histoNames = [h.getName() for h in p.histoMgr.getHistos()]
        p.histoMgr.reorder(filter(lambda n: plots._legendLabels[kwargs.get("refDataset") ] not in n, histoNames))
        if 0:
            p.removeLegend()

        # Axes
        #p.getFrame().GetYaxis().SetTitle( getTitleY(refHisto, **kwargs) )
        #p.getFrame().GetYaxis().SetTitle( getTitleY(p.histoMgr.getHistos()[0], **kwargs) )
        if kwargs.get("createRatio"):
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)

        # Set Log and Grid
        SetLogAndGrid(p, **kwargs)

        # Cut line / Cut box
        _kwargs = {"lessThan": kwargs.get("cutLessthan")}
        p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"), fillColor=kwargs.get("cutFillColour"), box=kwargs.get("cutBox"), line=kwargs.get("cutLine"), **_kwargs)

        # Draw the final plot
        p.draw()


        # ========================================
        # Add Text
        # ========================================
        histograms.addStandardTexts(lumi=intLumi)
        # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
        # histograms.addText(0.4, 0.11, "Runs " + datasetsMgr.loadRunRange(), 17)

        # Save the canvas to a file
        SaveAs(p, savePath, saveName, kwargs.get("saveFormats"), counter==0)

    return
Esempio n. 35
0
def main(opts):

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

    # 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
        datasetsMgr40 = GetDatasetsFromDir_second(
            opts, opts.mcrab.replace("_BDT85", "_BDT40"))
        datasetsMgr40.updateNAllEventsToPUWeighted()
        datasetsMgr40.loadLuminosities()  # from lumi.json

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

        # Set/Overwrite cross-sections
        for datasetsMgr_ in [datasetsMgr, datasetsMgr40]:
            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)
        if 0:
            plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Print dataset information before removing anything?
        if 0:
            datasetsMgr.PrintInfo()

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        else:
            intLumi = 35920

        # Remove datasets
        filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        #filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        for key in filterKeys:
            datasetsMgr.remove(
                filter(lambda name: key in name,
                       datasetsMgr.getAllDatasetNames()))
            datasetsMgr40.remove(
                filter(lambda name: key in name,
                       datasetsMgr40.getAllDatasetNames()))
        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            datasetOrder.append(d.getName())

        # Select and re-order
        datasetsMgr.selectAndReorder(datasetOrder)
        datasetsMgr40.selectAndReorder(datasetOrder)

        # Print dataset information
        datasetsMgr.PrintInfo()

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

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

        # For-loop: All numerator-denominator pairs
        PlotEfficiency(datasetsMgr, datasetsMgr40, intLumi)
    return
Esempio n. 36
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() # from lumi.json
        
        # Set/Overwrite cross-sections
        datasetsToRemove = ["QCD-b", "TTTT"]#, "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
                    
        # 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)
        

        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()))
        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()))
            
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Get Luminosity
        intLumi = datasetsMgr.getDataset("Data").getLuminosity()

        # Merge EWK samples
        if opts.mergeEWK:
            datasetsMgr.merge("EWK", GetListOfEwkDatasets(datasetsMgr))
            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)

        # Get histogram list
        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)
        
        # Calculate Signal Significance for all histograms
        for h in histoList:
            PlotCutFlowEfficiency(h, datasetsMgr, intLumi) 
    return
Esempio n. 37
0
def main(opts, signalMass):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)
    style.setGridX(True)
    style.setGridY(True)
    
    # 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 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) 
        
        # Print dataset information before removing anything?
        if 0:
            datasetsMgr.PrintInfo()

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            intLumi = datasetsMgr.getDataset("Data").getLuminosity()

        else:
            intLumi = 35920
        # Remove datasets
        filterKeys = ["Data", "QCD", "TTZToQQ", "TTWJets", "TTTT", "ZJetsToQQ_HT600toInf", "DYJetsToQQHT", "SingleTop", "WJetsToQQ_HT_600ToInf", "Diboson"]
        for key in filterKeys:
            datasetsMgr.remove(filter(lambda name: key in name, datasetsMgr.getAllDatasetNames()))

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

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Define the mapping histograms in numerator->denominator pairs
        HistoMap = {
            "AllTopQuarkPt_MatchedBDT"  : "AllTopQuarkPt_Matched",
            "TrijetFakePt_BDT"          : "TrijetFakePt",
            "AllTopQuarkPt_Matched"     : "TopQuarkPt",
            "EventTrijetPt2T_MatchedBDT": "EventTrijetPt2T_BDT",
            "EventTrijetPt2T_MatchedBDT": "EventTrijetPt2T_Matched",
            "EventTrijetPt2T_MatchedBDT": "EventTrijetPt2T",
            "AllTopQuarkPt_MatchedBDT"  : "TopQuarkPt",
            #"SelectedTrijetsPt_BjetPassCSVdisc_afterCuts": "SelectedTrijetsPt_afterCuts",
            #"TrijetPt_PassBDT_BJetPassCSV": "TrijetPt_PassBDT",
            }
        
        # For-loop: All numerator-denominator pairs
        for key in HistoMap:
            numerator   = os.path.join(opts.folder, key)
            denominator = os.path.join(opts.folder, HistoMap[key])
            PlotEfficiency(datasetsMgr, numerator, denominator, intLumi)
    return
Esempio n. 38
0
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
Esempio n. 39
0
def main():

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

    multicrabdir = sys.argv[1]

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

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

    print "Using",signalAnalysisDir
    print "     ",QCDAnalysisDir

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

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

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

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

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

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

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

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

    #myStackList = divideByBinWidth(myStackList)

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

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

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

    # 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 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)
        if 0:
            plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Print dataset information before removing anything?
        if 0:
            datasetsMgr.PrintInfo()

        # Print datasets info summary
        datasetsMgr.PrintInfo()

        # Re-order datasets
        datasetOrder = []
        haveQCD = False
        for d in datasetsMgr.getAllDatasets():
            if "QCD" in d.getName():
                haveQCD = True
            datasetOrder.append(d.getName())

        # Append signal datasets
        datasetsMgr.selectAndReorder(datasetOrder)

        # Define the mapping histograms in numerator->denominator pairs
        VariableList = ["LeadingTrijet_Pt"]
        # VariableList = ["LeadingTrijet_Pt", "LeadingTrijet_Eta", "LeadingTrijet_Phi"]

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

        counter = 0
        opts.nDatasets = len(datasetsMgr.getAllDatasets())
        nPlots = len(VariableList)
        # For-loop: All numerator-denominator pairs
        for var in VariableList:
            hNumerator = "AfterAllSelections_" + var + "_SR"
            hDenominator = "AfterStandardSelections_" + var + "_SR"
            numerator = os.path.join(opts.folder, hNumerator)
            denFolder = opts.folder
            #denFolder    = denFolder.replace("Genuine", "")
            #print "denFolder", denFolder
            denominator = os.path.join(denFolder, hDenominator)

            counter += 1
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format(
                "Histogram", "%i" % counter, "/", "%s:" % (nPlots),
                "%s" % (var))
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(),
                  counter == 1)

            PlotEfficiency(datasetsMgr, numerator, denominator)

    Print(
        "All plots saved under directory %s" %
        (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) +
         ShellStyles.NormalStyle()), True)
    return
Esempio n. 41
0
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
Esempio n. 42
0
def main(argv, dsetMgr, moduleInfoString):
    COMBINEDHISTODIR = "ForQCDNormalization"
    FAKEHISTODIR = "ForQCDNormalizationEWKFakeTaus"
    GENUINEHISTODIR = "ForQCDNormalizationEWKGenuineTaus"
    comparisonList = ["AfterStdSelections"]

    dirs = []
    dirs.append(sys.argv[1])

    # Check multicrab consistency
    # consistencyCheck.checkConsistencyStandalone(dirs[0],dsetMgr,name="QCD inverted") #FIXME needs to be updated

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

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

    if verbose:
        print "Datasets list (initial):"
        print dsetMgr.getMCDatasetNames()

    # Include only 120 mass bin of HW and HH dsetMgr
    dsetMgr.remove(
        filter(lambda name: "TTToHplus" in name and not "M120" in name,
               dsetMgr.getAllDatasetNames()))
    dsetMgr.remove(
        filter(lambda name: "HplusTB" in name, dsetMgr.getAllDatasetNames()))
    dsetMgr.remove(
        filter(lambda name: "DY2JetsToLL" in name,
               dsetMgr.getAllDatasetNames()))
    dsetMgr.remove(
        filter(lambda name: "DY3JetsToLL" in name,
               dsetMgr.getAllDatasetNames()))
    dsetMgr.remove(
        filter(lambda name: "DY4JetsToLL" in name,
               dsetMgr.getAllDatasetNames()))
    # Ignore DY dataset with HERWIG hadronization (it's only for testing)
    dsetMgr.remove(filter(lambda name: "DYJetsToLL_M_50_HERWIGPP" in name,
                          dsetMgr.getAllDatasetNames()),
                   close=False)

    if verbose:
        print "Datasets after filter removals:"
        print dsetMgr.getMCDatasetNames()

        # Default merging nad ordering of data and MC dsetMgr
    # All data dsetMgr to "Data"
    # All QCD dsetMgr to "QCD"
    # All single top dsetMgr to "SingleTop"
    # WW, WZ, ZZ to "Diboson"
    plots.mergeRenameReorderForDataMC(dsetMgr)

    if verbose:
        print "Datasets after mergeRenameReorderForDataMC:"
        print dsetMgr.getMCDatasetNames()

    # Only WJets/WJetsToLNu or WJetsToLNu_HT_* should be used (not both)
    if useWJetsHT:
        dsetMgr.remove(filter(lambda name: "WJets" == name,
                              dsetMgr.getAllDatasetNames()),
                       close=False)
    else:
        dsetMgr.remove(filter(lambda name: "WJetsHT" in name,
                              dsetMgr.getAllDatasetNames()),
                       close=False)

    print "Datasets used for EWK (after choosing between WJets or WJetsHT sample):"
    print dsetMgr.getMCDatasetNames()

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

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

    # Merge MC EWK samples as one EWK sample
    myMergeList = []

    # Always use TT (or TTJets) as a part of the EWK background
    if "TT" in dsetMgr.getMCDatasetNames():
        myMergeList.append("TT")  # Powheg, no neg. weights -> large stats.
    else:
        myMergeList.append("TTJets")  # Madgraph with negative weights
        print "Warning: using TTJets as input, but this is suboptimal. Please switch to the TT sample (much more stats.)."

    # Always use WJets as a part of the EWK background
    if useWJetsHT:
        myMergeList.append("WJetsHT")
    else:
        myMergeList.append("WJets")

    # For SY, single top and diboson, use only if available:
    if "DYJetsToQQHT" in dsetMgr.getMCDatasetNames():
        myMergeList.append("DYJetsToQQHT")

    if "DYJetsToLL" in dsetMgr.getMCDatasetNames():
        myMergeList.append("DYJetsToLL")
    else:
        print "Warning: ignoring DYJetsToLL sample (since merged sample does not exist) ..."

    if "SingleTop" in dsetMgr.getMCDatasetNames():
        myMergeList.append("SingleTop")
    else:
        print "Warning: ignoring single top sample (since merged sample does not exist) ..."

    if "Diboson" in dsetMgr.getMCDatasetNames():
        myMergeList.append("Diboson")
    else:
        print "Warning: ignoring diboson sample (since merged sample does not exist) ..."

    for item in myMergeList:
        if not item in dsetMgr.getMCDatasetNames():
            raise Exception(
                "Error: tried to use dataset '%s' as part of the merged EWK dataset, but the dataset '%s' does not exist!"
                % (item, item))
    dsetMgr.merge("EWK", myMergeList)

    if verbose:
        print "\nFinal merged dataset list:\n"
        print dsetMgr.getMCDatasetNames()

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

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

        #===== Infer binning information and labels
        histonames = dsetMgr.getDataset("Data").getDirectoryContent(
            COMBINEDHISTODIR + "/NormalizationMETBaselineTau" + HISTONAME)
        bins = []
        binLabels = []
        if histonames == None:
            # Assume that only inclusive bin exists
            name = COMBINEDHISTODIR + "/NormalizationMETBaselineTau" + HISTONAME
            if not dsetMgr.getDataset("Data").hasRootHisto(name):
                raise Exception(
                    "Error: Cannot find histogram or directory of name '%s'!" %
                    name)
            BASELINETAUHISTONAME = "NormalizationMETBaselineTau" + HISTONAME
            INVERTEDTAUHISTONAME = "NormalizationMETInvertedTau" + HISTONAME
            bins = [""]
            binLabels = ["Inclusive"]
        else:
            for hname in histonames:
                binIndex = hname.replace(
                    "NormalizationMETBaselineTau" + HISTONAME, "")
                #                print "DEBUG: We are looking for hisrogram "+COMBINEDHISTODIR+"/"+BASELINETAUHISTONAME+binIndex
                hDummy = dsetMgr.getDataset("Data").getDatasetRootHisto(
                    COMBINEDHISTODIR + "/" + BASELINETAUHISTONAME +
                    binIndex).getHistogram()
                title = hDummy.GetTitle()
                title = title.replace("METBaseline" + HISTONAME, "")
                if hDummy.Integral() > 0.0:
                    bins.append(binIndex)
                    if binIndex == "Inclusive":
                        binLabels.append(binIndex)
                    else:
                        binLabels.append(
                            QCDNormalization.getModifiedBinLabelString(title))
                    if FITMIN == None:
                        FITMIN = hDummy.GetXaxis().GetXmin()
                        FITMAX = hDummy.GetXaxis().GetXmax()
                    hDummy.Delete()
                else:
                    print "Skipping bin '%s' (%s) because it has no entries" % (
                        binIndex,
                        QCDNormalization.getModifiedBinLabelString(title))
        print "\nHistogram bins available", bins
        # Select bins by filter
        if len(selectOnlyBins) > 0:
            oldBinLabels = binLabels[:]
            oldBins = bins[:]
            binLabels = []
            bins = []
            for k in selectOnlyBins:
                for i in range(len(oldBinLabels)):
                    if k == oldBinLabels[i] or k == oldBins[i]:
                        binLabels.append(oldBinLabels[i])
                        bins.append(oldBins[i])
        print "Using bins              ", bins
        print "\nBin labels"
        for i in range(len(binLabels)):
            line = bins[i]
            while len(line) < 10:
                line += " "
            line += ": " + binLabels[i]
            print line
        print

        #===== Initialize normalization calculator
        #manager = QCDNormalization.QCDNormalizationManagerExperimental1(binLabels)
        manager = QCDNormalization.QCDNormalizationManagerDefault(
            binLabels, dirs[0], moduleInfoString)

        #===== Create templates (EWK fakes, EWK genuine, QCD; data template is created by manager)
        template_EWKFakeTaus_Baseline = manager.createTemplate(
            "EWKFakeTaus_Baseline")
        template_EWKFakeTaus_Inverted = manager.createTemplate(
            "EWKFakeTaus_Inverted")
        template_EWKGenuineTaus_Baseline = manager.createTemplate(
            "EWKGenuineTaus_Baseline")
        template_EWKGenuineTaus_Inverted = manager.createTemplate(
            "EWKGenuineTaus_Inverted")
        template_EWKInclusive_Baseline = manager.createTemplate(
            "EWKInclusive_Baseline")
        template_EWKInclusive_Inverted = manager.createTemplate(
            "EWKInclusive_Inverted")
        template_QCD_Baseline = manager.createTemplate("QCD_Baseline")
        template_QCD_Inverted = manager.createTemplate("QCD_Inverted")

        #===== Define fit functions and fit parameters
        # The available functions are defined in the FitFunction class in the QCDMeasurement/python/QCDNormalization.py file

        # commented out fitter for EWK fake taus, since only the fit on inclusive EWK is used to obtain w_QCD
        #boundary = 100
        #template_EWKFakeTaus_Baseline.setFitter(QCDNormalization.FitFunction("EWKFunctionInv", boundary=boundary, norm=1, rejectPoints=1),
        #FITMIN, FITMAX)
        #template_EWKFakeTaus_Baseline.setDefaultFitParam(defaultInitialValue=[10.0, 100, 45,   0.02],
        #defaultLowerLimit=  [ 0.1,  70,  10,  0.001],
        #defaultUpperLimit=  [ 30, 300,  100,    0.1])
        # commented out fitter for EWK genuine taus, since only the fit on inclusive EWK is used to obtain w_QCD
        #boundary = 150
        #template_EWKGenuineTaus_Baseline.setFitter(QCDNormalization.FitFunction("EWKFunction", boundary=boundary, norm=1, rejectPoints=1),
        #FITMIN, FITMAX)
        #template_EWKGenuineTaus_Baseline.setDefaultFitParam(defaultLowerLimit=[0.5,  90, 30, 0.0001],
        #defaultUpperLimit=[ 20, 150, 50,    1.0])
        # Inclusive EWK
        boundary = 150
        template_EWKInclusive_Baseline.setFitter(
            QCDNormalization.FitFunction("EWKFunction",
                                         boundary=boundary,
                                         norm=1,
                                         rejectPoints=1), FITMIN, FITMAX)
        template_EWKInclusive_Baseline.setDefaultFitParam(
            defaultLowerLimit=[0.5, 90, 30, 0.0001],
            defaultUpperLimit=[20, 150, 60, 1.0])

        # Fake tau and QCD
        # Note that the same function is used for QCD only and QCD+EWK fakes (=Fake Tau)

        # Old function, used until May 2017
        #        template_QCD_Inverted.setFitter(QCDNormalization.FitFunction("QCDFunction", norm=1), FITMIN, FITMAX)
        #        template_QCD_Inverted.setDefaultFitParam(defaultLowerLimit=[0.0001, 0.001, 0.1, 0.0,  10, 0.0001, 0.001],
        #                                                 defaultUpperLimit=[   200,    10,  10, 150, 100,      1, 0.05])
        #        template_QCD_Inverted.setDefaultFitParam(defaultLowerLimit=[ 30, 0.1, 0.1,    0,  10,     0.0, 0.0001], # new default limits to make fits more stable,
        #                                                 defaultUpperLimit=[ 130, 20,  20,  200, 200,     1.0,    1.0]) # corresponding to the 7 free param. of the fit function

        # New dunction with one more d.o.f in Rayleigh distribution WARNING!!! UNSTABLE!!!
        #        template_QCD_Inverted.setFitter(QCDNormalization.FitFunction("QCDFunctionWithPeakShift", norm=1), FITMIN, FITMAX)
        #        template_QCD_Inverted.setDefaultFitParam(defaultLowerLimit=[ 30, 0.1, -10,   0,  -20,  10,   0.0001, 0.0001],
        #                                                 defaultUpperLimit=[ 130, 20,  10,  20,  200, 100,     1.0,   0.05])

        # As the factor multiplicative for exponential function (p6 in QCDFunctionWithPeakShift tends to 0 in fitting,
        # we drop that term and use only this:
        template_QCD_Inverted.setFitter(
            QCDNormalization.FitFunction("RayleighShiftedPlusGaussian",
                                         norm=1), FITMIN, FITMAX)
        template_QCD_Inverted.setDefaultFitParam(
            defaultLowerLimit=[30, 0.1, -10, 0, -20, 10],
            defaultUpperLimit=[130, 20, 10, 20, 200, 100])

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

            #===== Reset bin results
            manager.resetBinResults()

            #===== Obtain histograms for normalization
            # Data
            histoName = COMBINEDHISTODIR + "/" + BASELINETAUHISTONAME + binStr
            hmetBase_data = plots.DataMCPlot(
                dsetMgr, histoName).histoMgr.getHisto(
                    "Data").getRootHisto().Clone(histoName)
            histoName = COMBINEDHISTODIR + "/" + INVERTEDTAUHISTONAME + binStr
            hmetInverted_data = plots.DataMCPlot(
                dsetMgr, histoName).histoMgr.getHisto(
                    "Data").getRootHisto().Clone(histoName)

            # EWK genuine taus
            histoName = GENUINEHISTODIR + "/" + BASELINETAUHISTONAME + binStr
            hmetBase_EWK_GenuineTaus = plots.DataMCPlot(
                dsetMgr, histoName).histoMgr.getHisto(
                    "EWK").getRootHisto().Clone(histoName)
            histoName = GENUINEHISTODIR + "/" + INVERTEDTAUHISTONAME + binStr
            hmetInverted_EWK_GenuineTaus = plots.DataMCPlot(
                dsetMgr, histoName).histoMgr.getHisto(
                    "EWK").getRootHisto().Clone(histoName)

            # EWK fake taus
            histoName = FAKEHISTODIR + "/" + BASELINETAUHISTONAME + binStr
            hmetBase_EWK_FakeTaus = plots.DataMCPlot(
                dsetMgr, histoName).histoMgr.getHisto(
                    "EWK").getRootHisto().Clone(histoName)
            histoName = FAKEHISTODIR + "/" + INVERTEDTAUHISTONAME + binStr
            hmetInverted_EWK_FakeTaus = plots.DataMCPlot(
                dsetMgr, histoName).histoMgr.getHisto(
                    "EWK").getRootHisto().Clone(histoName)

            #===== Obtain inclusive EWK histograms
            hmetBase_EWKinclusive = hmetBase_EWK_GenuineTaus.Clone(
                "EWKinclusiveBase")
            hmetBase_EWKinclusive.Add(hmetBase_EWK_FakeTaus, 1.0)

            hmetInverted_EWKinclusive = hmetInverted_EWK_GenuineTaus.Clone(
                "EWKinclusiveInv")
            hmetInverted_EWKinclusive.Add(hmetInverted_EWK_FakeTaus, 1.0)

            # Finalize histograms by rebinning
            for histogram in [
                    hmetBase_data, hmetInverted_data, hmetBase_EWK_GenuineTaus,
                    hmetInverted_EWK_GenuineTaus, hmetBase_EWKinclusive,
                    hmetBase_EWK_FakeTaus, hmetInverted_EWK_FakeTaus,
                    hmetInverted_EWKinclusive
            ]:
                histogram.Rebin(_rebinFactor)

            #===== Obtain histograms for QCD (subtract MC EWK events from data)
            # QCD from baseline is usable only as a cross check
            hmetBase_QCD = hmetBase_data.Clone("QCDbase")
            hmetBase_QCD.Add(hmetBase_EWKinclusive, -1)

            hmetInverted_QCD = hmetInverted_data.Clone("QCDinv")
            hmetInverted_QCD.Add(hmetInverted_EWKinclusive, -1)

            #===== Set histograms to the templates
            template_EWKFakeTaus_Inverted.setHistogram(
                hmetInverted_EWK_FakeTaus, binLabels[i])
            template_EWKGenuineTaus_Inverted.setHistogram(
                hmetInverted_EWK_GenuineTaus, binLabels[i])
            template_EWKInclusive_Inverted.setHistogram(
                hmetInverted_EWKinclusive, binLabels[i])
            template_QCD_Inverted.setHistogram(hmetInverted_QCD, binLabels[i])

            template_EWKFakeTaus_Baseline.setHistogram(hmetBase_EWK_FakeTaus,
                                                       binLabels[i])
            template_EWKGenuineTaus_Baseline.setHistogram(
                hmetBase_EWK_GenuineTaus, binLabels[i])
            template_EWKInclusive_Baseline.setHistogram(
                hmetBase_EWKinclusive, binLabels[i])
            template_QCD_Baseline.setHistogram(hmetBase_QCD, binLabels[i])

            #===== Make plots of templates
            manager.plotTemplates()

            #===== Fit individual templates to data
            fitOptions = "R B L W M"  # RBLWM

            manager.calculateNormalizationCoefficients(hmetBase_data,
                                                       fitOptions, FITMIN,
                                                       FITMAX)

            #===== Calculate combined normalisation coefficient (f_fakes = w*f_QCD + (1-w)*f_EWKfakes)
            # Obtain histograms
            histoName = "ForDataDrivenCtrlPlots/shapeTransverseMass/shapeTransverseMass" + binStr
            dataMt = plots.DataMCPlot(dsetMgr, histoName).histoMgr.getHisto(
                "Data").getRootHisto().Clone(histoName)
            treatNegativeBins(dataMt, "Data_inverted mT")
            histoName = "ForDataDrivenCtrlPlotsEWKFakeTaus/shapeTransverseMass/shapeTransverseMass" + binStr
            ewkFakeTausMt = plots.DataMCPlot(
                dsetMgr, histoName).histoMgr.getHisto(
                    "EWK").getRootHisto().Clone(histoName)
            treatNegativeBins(ewkFakeTausMt, "ewkFakeTaus_inverted mT")
            histoName = "ForDataDrivenCtrlPlotsEWKGenuineTaus/shapeTransverseMass/shapeTransverseMass" + binStr
            ewkGenuineTausMt = plots.DataMCPlot(
                dsetMgr, histoName).histoMgr.getHisto(
                    "EWK").getRootHisto().Clone(histoName)
            treatNegativeBins(ewkGenuineTausMt, "ewkGenuineTaus_inverted mT")
            qcdMt = dataMt.Clone("QCD")
            qcdMt.Add(ewkFakeTausMt, -1)
            qcdMt.Add(ewkGenuineTausMt, -1)
            treatNegativeBins(qcdMt, "QCD_inverted mT")
            # Do calculation
            manager.calculateCombinedNormalizationCoefficient(
                qcdMt, ewkFakeTausMt)

        #===== Save normalization
        outFileName = "QCDNormalizationFactors_%s_%s.py" % (HISTONAME,
                                                            moduleInfoString)
        outFileFullName = os.path.join(argv[1], outFileName)
        manager.writeScaleFactorFile(outFileFullName, moduleInfoString)
def main(opts):
    
    # Suppress warnings about weight being re-applied
    ROOT.gErrorIgnoreLevel = ROOT.kError 

    # 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]

    opts.optMode = ""
    mcrabName    = opts.mcrab
    RunEra       = mcrabName.split("_")[1]

    # Setup ROOT and style
    ROOT.gROOT.SetBatch(opts.batchMode)
    style = tdrstyle.TDRStyle()
    style.setGridX(opts.gridX)
    style.setGridY(opts.gridY)
    if opts.paper:
        style.setGridX(False)
        style.setGridY(False)
    
    # Setup & configure the dataset manager
    datasetsMgr = GetDatasetsFromDir(opts)

    # Remove some QCD samples (the cross sections of these samples are not calculated)
    if 0:
        msg = "Removing following samples:"
        Print(ShellStyles.ErrorStyle() + msg + ShellStyles.NormalStyle(), True)
        for d in getDatasetsToExclude():
            Print(d, False)
            datasetsMgr.remove(d)

    # Get run-range 
    minRunRange, maxRunRange, runRange = GetRunRange(datasetsMgr)

    # Get int lumi
    intLumi  = GetLumi(datasetsMgr)

    # Update to PU & load luminosities 
    datasetsMgr.updateNAllEventsToPUWeighted()
    datasetsMgr.loadLuminosities()
    #datasetsMgr.normalizeMCByLuminosity()

    # Print luminisoties and cross-sections
    datasetsMgr.PrintLuminosities()
    datasetsMgr.PrintCrossSections()

    # Default merging & ordering: "Data", "QCD", "SingleTop", "Diboson"
    plots.mergeRenameReorderForDataMC(datasetsMgr)
    
    # Get datasets
    datasetsMgr.mergeMC()
    dataset_Data = datasetsMgr.getDataDatasets()
    dataset_MC   = datasetsMgr.getMCDatasets()

    # Define lists of Triggers to be plotted and Variables 
    xVars   = ["pt6thJet", "eta6thJet", "phi6thJet", "Ht", "nBTagJets", "pu", "JetMulti", "BJetMulti"]
    trgList = ["1BTag", "2BTag", "OR", "OR_PFJet450"]
    if opts.fast:
        trgList = ["OR_PFJet450"]
        xVars   = ["pt6thJet", "Ht"]
    nPlots  = len(trgList)*len(xVars)
    counter =  0

    # For-loop: All signal triggers
    for i, trg in enumerate(trgList, 1):
        
        # For-loop: All x-variables
        for j, xVar in enumerate(xVars, 1):
            counter+=1
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % counter, "/", "%s:" % (nPlots), "%s Vs %s" % (trg, xVar))
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), counter==1)


            # Define names
            hNumerator   = "hNum_%s_RefTrg_OfflineSel_Signal%s" % (xVar, trg)
            hDenominator = "hDen_%s_RefTrg_OfflineSel" % (xVar)
            plotName     = "Eff_%s_%s" % (xVar, trg)
            
            # Get Efficiency Plots
            _kwargs  = GetHistoKwargs(xVar, opts)
            eff_Data = GetEfficiency(datasetsMgr, dataset_Data, hNumerator, hDenominator , **_kwargs)
            eff_MC   = GetEfficiency(datasetsMgr, dataset_MC, hNumerator, hDenominator, **_kwargs) 
                       
            # Apply Styles
            styles.dataStyle.apply(eff_Data)
            styles.mcStyle.apply(eff_MC)
        
            # Create the plot
            p = plots.ComparisonPlot(histograms.HistoGraph(eff_Data, "eff_Data", "p", "P"),
                                     histograms.HistoGraph(eff_MC,   "eff_MC"  , "p", "P"),
                                     saveFormats=[])                  

            # Define the legend entries
            p.histoMgr.setHistoLegendLabelMany(
                {
                    "eff_Data": "Data", 
                    "eff_MC"  : "Simulation"
                    }
                )
            
            # Draw and save the plot
            p.setLuminosity(intLumi)
            plots.drawPlot(p, plotName, **_kwargs)
                                                   
            # Draw
            histograms.addText(0.65, 0.06, "Runs "+ runRange, 17)
            histograms.addText(0.65, 0.10, "2016", 17)

            # Save the canvas to a file
            SavePlot(p, plotName, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".pdf", ".png", ".C"] )

    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    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
Esempio n. 45
0
def main():

    style = tdrstyle.TDRStyle()
    #style.setWide(True)
    style.setPaletteMy()
    ROOT.gStyle.SetNumberContours(20)
    # tdrstyle.setDeepSeaPalette()
    # tdrstyle.setRainBowPalette()
    # tdrstyle.setDarkBodyRadiatorPalette()
    # tdrstyle.setGreyScalePalette()
    # tdrstyle.setTwoColorHuePalette()
 
    
    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(parseOpts.batchMode)
    
    # Get all datasets from the mcrab dir
    #datasetsMgr  = GetDatasetsFromDir(parseOpts.mcrab, kwargs.get("analysis"))
    datasetsMgr  = GetDatasetsFromDir(parseOpts.mcrab, parseOpts, **kwargs)

    # Determine Integrated Luminosity (If Data datasets present)
    intLumi = GetLumi(datasetsMgr)
    
    # Update to PU
    datasetsMgr.updateNAllEventsToPUWeighted()

    # Remove datasets
    #datasetsMgr.remove(kwargs.get("rmDataset"))
    datasetsMgr.remove(filter(lambda name: not kwargs.get("refDataset") in name, datasetsMgr.getAllDatasetNames()))

    # Set custom XSections
    # d.getDataset("TT_ext3").setCrossSection(831.76)
    
    # Default merging & ordering: "Data", "QCD", "SingleTop", "Diboson"
    plots.mergeRenameReorderForDataMC(datasetsMgr) #WARNING: Merged MC histograms must be normalized to something!

    # Remove datasets (for merged names)
    # datasetsMgr.remove(kwargs.get("rmDataset"))
                  
    # For-loop: All Histogram names
    for hName in hNames:
        savePath, saveName = GetSavePathAndName(hName, **kwargs)                

        # Get Histos for Plotter
        refHisto, otherHistos = GetHistosForPlotter(datasetsMgr, hName, **kwargs)

        # Create a plot
        p = plots.PlotBase([refHisto], kwargs.get("saveFormats"))
        
        # Remove negative contributions
        #RemoveNegativeBins(datasetsMgr, hName, p)

        # Customize
        # p.histoMgr.setHistoDrawStyleAll("COL") #"CONT4" "COLZ" "COL"
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().RebinX(kwargs.get("rebinX")))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().RebinY(kwargs.get("rebinY")))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().GetXaxis().SetRangeUser(1.0, 5.0))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().GetYaxis().SetRangeUser(1.0, 5.0))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().GetZaxis().SetRangeUser(0.0, 0.015))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMinimum(kwargs.get("zMin")))
        # p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMaximum(kwargs.get("zMax")))
        
        # Create a frame
        opts = {"ymin": 0.0, "ymaxfactor": 1.0}
        p.createFrame(saveName, opts=opts)


        # Customise frame
        p.getFrame().GetXaxis().SetTitle( getTitleX(refHisto, **kwargs) )
        p.getFrame().GetYaxis().SetTitle( getTitleY(refHisto, **kwargs) )
        # p.getFrame().GetZaxis().SetTitle( getTitleZ(refHisto, **kwargs) ) #does not work
        
        # SetLog
        SetLogAndGrid(p, **kwargs)

        # Add cut line/box
        _kwargs = { "lessThan": kwargs.get("cutLessThan")}
        p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"), fillColor=kwargs.get("cutFillColour"), box=kwargs.get("cutBox"), line=kwargs.get("cutLine"), **_kwargs)
        
        # Customise Legend
        moveLegend = {"dx": -0.1, "dy": +0.0, "dh": -0.1}
        p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
        p.removeLegend()

        # Add MC Uncertainty (Invalid method for a 2-d histogram)
        #p.addMCUncertainty()
        
        #  Draw plots
        p.draw()

        # Customise text
        histograms.addStandardTexts(lumi=intLumi)
        #histograms.addText(0.16, 0.95, plots._legendLabels[kwargs.get("refDataset")], 22)
        histograms.addText(0.20, 0.88, plots._legendLabels[kwargs.get("refDataset")], 17)
        
        # Save canvas under custom dir
	if not os.path.exists(savePath):
		os.mkdir(savePath)
        SaveAs(p, savePath, saveName, kwargs.get("saveFormats"))

    return
Esempio n. 46
0
def main(hName, opts):

    # Setup the style
    style = tdrstyle.TDRStyle()

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

    # Setup & configure the dataset manager
    datasetsMgr = GetDatasetsFromDir(opts.mcrab, opts, **kwargs)
    datasetsMgr.updateNAllEventsToPUWeighted()
    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 datasets: All JetHT to "Data", QCD_Pt to "QCD", QCD_bEnriched to "QCD_b",  single-top to "SingleTop", WW, WZ, ZZ to "Diboson"
    plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Remove datasets
    # datasetsMgr.remove("QCD-b")
    # datasetsMgr.remove("QCD")

    # Print dataset information
    datasetsMgr.PrintInfo()

    # Create data-MC comparison plot, with the default
    p = plots.DataMCPlot(datasetsMgr, hName)

    # Create a comparison plot
    ratioOpts = {"ymin": 0.0, "ymax": 2.0}
    if kwargs.get("logY") == True:
        canvOpts = {"xmin": 0.0, "ymin": 1e-1, "ymaxfactor": 10}
    else:
        canvOpts = {"ymin": 0.0, "ymaxfactor": 1.2}

    # Draw a customised plot & Save it
    plots.drawPlot(
        p,
        "Plots/" + hName.replace("/", "_").replace(" ", "_").replace(
            "(", "_").replace(")", ""),
        xlabel=kwargs.get("xlabel"),
        ylabel=kwargs.get("ylabel"),
        rebinX=kwargs.get("rebinX"),
        rebinY=kwargs.get("rebinY"),
        xlabelsize=kwargs.get("xlabelsize"),
        ratio=kwargs.get("ratio"),
        stackMCHistograms=kwargs.get("stackMCHistograms"),
        ratioYlabel=kwargs.get("ratioYlabel"),
        ratioInvert=kwargs.get("ratioInvert"),
        addMCUncertainty=kwargs.get("addMCUncertainty"),
        addLuminosityText=kwargs.get("addLuminosityText"),
        addCmsText=kwargs.get("addCmsText"),
        opts=canvOpts,
        opts2=ratioOpts,
        log=kwargs.get("logY"),
        errorBarsX=kwargs.get("errorBarsX"),
        cmsExtraText=kwargs.get("cmsExtraText"),
        moveLegend=kwargs.get("moveLegend"),
        #cutLine=kwargs.get("cutValue"),
        cutBox={
            "cutValue": kwargs.get("cutValue"),
            "fillColor": kwargs.get("cutFillColour"),
            "box": kwargs.get("cutBox"),
            "line": kwargs.get("cutLine"),
            "lessThan": kwargs.get("cutLessthan")
        },
    )

    # Remove legend?
    if kwargs.get("removeLegend"):
        p.removeLegend()

    # Additional text
    # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
    # histograms.addText(0.4, 0.11, "Runs " + datasetsMgr.loadRunRange(), 17)

    if not opts.batchMode:
        raw_input("=== plotCounters.py:\n\tPress any key to quit ROOT ...")

    return
    myModuleSelector.doSelect(opts)

    myDisplayStatus = True
    # Loop over era/searchMode/optimizationMode options
    for era in myModuleSelector.getSelectedEras():
        for searchMode in myModuleSelector.getSelectedSearchModes():
            for optimizationMode in myModuleSelector.getSelectedOptimizationModes():
                # Construct info string of module
                myModuleInfoString = "%s_%s_%s"%(era, searchMode, optimizationMode)
                print HighlightStyle()+"Module:",myModuleInfoString,NormalStyle()
                # Obtain dataset manager
                dsetMgr = dsetMgrCreator.createDatasetManager(dataEra=era,searchMode=searchMode,optimizationMode=optimizationMode)
                # Do the usual normalisation
                dsetMgr.updateNAllEventsToPUWeighted()
                dsetMgr.loadLuminosities()
                plots.mergeRenameReorderForDataMC(dsetMgr)
                dsetMgr.merge("EWK", [
                              "TTJets",
                              "WJets",
                              "DYJetsToLL",
                              "SingleTop",
                              "Diboson"
                              ])
                # Remove MC QCD
                dsetMgr.remove("QCD")
                # Make a directory for output
                myDir = ""
                #myDir = "plots_%s"%myModuleInfoString
                #createOutputdirectory(myDir)
                # Obtain luminosity
                myLuminosity = dsetMgr.getDataset("Data").getLuminosity()
Esempio n. 48
0
def main(opts):

    # 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)

    # Setup & configure the dataset manager
    datasetsMgr = GetDatasetsFromDir(opts)
    datasetsMgr.updateNAllEventsToPUWeighted()
    datasetsMgr.loadLuminosities()  # from lumi.json
    plots.mergeRenameReorderForDataMC(datasetsMgr)
    opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()

    # Do the plot
    moduleDict = {}
    moduleNames = list(
        filter(
            lambda x: "FakeBTransferFactors" in x and x.endswith(".py") and
            not x.startswith("#"), os.listdir(opts.mcrab)))
    sKeys = []
    Verbose(
        "Found %d import files:%s%s%s" %
        (len(moduleNames), hs, "\n\t" + "\n\t".join(moduleNames), ns), True)

    # For-loop: All module files to be imported
    for i, m in enumerate(moduleNames, 1):
        if not opts.verbose:
            Print(
                "Importing module %s" %
                (hs + os.path.join(opts.mcrab, m) + ns), i == 1)
        else:
            Print(
                "Importing module %s" %
                (hs + os.path.join(opts.mcrab, m) + ns), True)

        # Define the path to the .py file (module Path) and the name to be imported as (module name)
        mPath = os.path.join(os.getcwd(), opts.mcrab, m)
        mName = m.split("_")[-1].replace(".py", "")
        sKeys.append(mName)
        mObj = imp.load_source(mName, mPath)
        moduleDict[mName] = mObj

        # Debugging
        Verbose(moduleDict[mName].FakeBNormalisation_Value.keys(), False)
        Verbose(moduleDict[mName].FakeBNormalisation_Value.values(), False)

    gList = []
    gListR = []
    # For-loop: All modules imported
    for i, k in enumerate(sorted(sKeys, key=natural_keys), 0):
        g = PlotTFs(i, k, moduleDict[k])
        gList.extend(g)
        gListR.extend(g)

    # Make sure you use correct graph as reference
    refPos = 0
    for i, g in enumerate(gList, 0):
        if g.getName() == opts.refHisto:
            refPos = i
            break

    # Reference histograph
    gList.insert(0, gList.pop(refPos))
    gListR.insert(0, gListR.pop(refPos))

    # Create comparison plot
    PlotTFsCompare("transferFactors", sKeys, gList)

    # Create comparison ratio plot
    gListR = [
        divideGraph(g, gListR[0], errorY=True, invRatio=False) for g in gListR
    ]
    PlotTFsCompare("transferFactorsR", sKeys, gListR, isRatio=True)

    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)

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

        # Numerator & Denominator dataset manager
        noSF_datasetsMgr      = GetDatasetsFromDir(opts, opts.noSFcrab)
        withCR1SF_datasetsMgr = GetDatasetsFromDir(opts, opts.withCR1SFcrab) 
        withCR2SF_datasetsMgr = GetDatasetsFromDir(opts, opts.withCR2SFcrab) 
        
        # Update all events to PU weighting
        noSF_datasetsMgr.updateNAllEventsToPUWeighted()
        withCR1SF_datasetsMgr.updateNAllEventsToPUWeighted()
        withCR2SF_datasetsMgr.updateNAllEventsToPUWeighted()
        
        # Load Luminosities
        noSF_datasetsMgr.loadLuminosities()
        withCR1SF_datasetsMgr.loadLuminosities()
        withCR2SF_datasetsMgr.loadLuminosities()
        
        if 0:
            noSF_datasetsMgr.PrintCrossSections()
            noSF_datasetsMgr.PrintLuminosities()
 
        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(noSF_datasetsMgr) 
        plots.mergeRenameReorderForDataMC(withCR1SF_datasetsMgr) 
        plots.mergeRenameReorderForDataMC(withCR2SF_datasetsMgr) 
        
        # Get luminosity if a value is not specified
        if opts.intLumi < 0:
            opts.intLumi = noSF_datasetsMgr.getDataset("Data").getLuminosity()
            
        # Remove datasets
        removeList = []
        #removeList = ["TTWJetsToLNu_", "TTWJetsToQQ"]
        for i, d in enumerate(removeList, 0):
            msg = "Removing dataset %s" % d
            Print(ShellStyles.WarningLabel() + msg + ShellStyles.NormalStyle(), i==0)
            noSF_datasetsMgr.remove(filter(lambda name: d in name, noSF_datasetsMgr.getAllDatasetNames()))
            withCR1SF_datasetsMgr.remove(filter(lambda name: d in name, withCR1SF_datasetsMgr.getAllDatasetNames()))
        
        # Print summary of datasets to be used
        if 0:
            noSF_datasetsMgr.PrintInfo()
            withCR1SF_datasetsMgr.PrintInfo()
            withCR2SF_datasetsMgr.PrintInfo()
            
        # Merge EWK samples
        EwkDatasets = ["Diboson", "DYJetsToLL", "WJetsHT"]
        noSF_datasetsMgr.merge("EWK", EwkDatasets)
        withCR1SF_datasetsMgr.merge("EWK", EwkDatasets)
        withCR2SF_datasetsMgr.merge("EWK", EwkDatasets)
        
        # Get histosgram names
        folderListIncl = withCR1SF_datasetsMgr.getDataset(withCR1SF_datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(opts.folder)
        folderList = [h for h in folderListIncl if "AfterAllSelections_LeadingTrijet_Pt" in h ]

        # For-loop: All histo paths
        for h in folderList:
            if "lowMET" in h:
                folderList.remove(h)
        
        folderPath    = os.path.join(opts.folder, "")
        folderPathGen = os.path.join(opts.folder + "Genuine")
        folderPathFake =os.path.join(opts.folder + "Fake"   )
        
        histoList = folderList
        num_pathList = [os.path.join(folderPath, h) for h in histoList]
        num_pathList.extend([os.path.join(folderPathGen, h) for h in histoList])
        num_pathList.extend([os.path.join(folderPathFake, h) for h in histoList])
        
        # Denominator Histogram (To be used in the estimation of QCD Data-Driven)
        histoList = [h for h in folderListIncl if "AfterStandardSelections_LeadingTrijet_Pt" in h]
        den_pathList = [os.path.join(folderPath, h) for h in histoList]
        den_pathList.extend([os.path.join(folderPathGen, h) for h in histoList])
        den_pathList.extend([os.path.join(folderPathFake, h) for h in histoList])

        # For-loop: All histo paths
        for h in den_pathList:
            if "lowMET" in h:
                den_pathList.remove(h)
        
        # Do the histograms
        PlotHistos(noSF_datasetsMgr, withCR1SF_datasetsMgr, withCR2SF_datasetsMgr, num_pathList, den_pathList,  opts)
        
    return
Esempio n. 50
0
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, 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
Esempio n. 52
0
def main(opts, signalMass):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)
    style.setGridX(True)
    style.setGridY(True)
    
    # 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

        dir = opts.mcrab.replace("_BDT85", "_BDT")
        print dir

        datasetsMgr30 = GetDatasetsFromDir_second(opts, dir+"30")
        datasetsMgr30.updateNAllEventsToPUWeighted()
        datasetsMgr30.loadLuminosities() # from lumi.json                                                                                      

        datasetsMgr40 = GetDatasetsFromDir_second(opts, dir+"40")
        datasetsMgr40.updateNAllEventsToPUWeighted()
        datasetsMgr40.loadLuminosities() # from lumi.json                                                                                                                           

        datasetsMgr50 = GetDatasetsFromDir_second(opts, dir+"50")
        datasetsMgr50.updateNAllEventsToPUWeighted()
        datasetsMgr50.loadLuminosities() # from lumi.json                                                                                                                           
 
        datasetsMgr60 = GetDatasetsFromDir_second(opts, dir+"60")
        datasetsMgr60.updateNAllEventsToPUWeighted()
        datasetsMgr60.loadLuminosities() # from lumi.json                                                                                                                               

        datasetsMgr70 = GetDatasetsFromDir_second(opts, dir+"70")
        datasetsMgr70.updateNAllEventsToPUWeighted()
        datasetsMgr70.loadLuminosities() # from lumi.json                                                                                                                               

        datasetsMgr80 = GetDatasetsFromDir_second(opts, dir+"80")
        datasetsMgr80.updateNAllEventsToPUWeighted()
        datasetsMgr80.loadLuminosities() # from lumi.json                                                                                                                               

        datasetsMgr90 = GetDatasetsFromDir_second(opts, dir+"90")
        datasetsMgr90.updateNAllEventsToPUWeighted()
        datasetsMgr90.loadLuminosities() # from lumi.json                                                                                                                               

        datasetsMgr95 = GetDatasetsFromDir_second(opts, dir+"95")
        datasetsMgr95.updateNAllEventsToPUWeighted()
        datasetsMgr95.loadLuminosities() # from lumi.json             

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

        # Set/Overwrite cross-sections
        for datasetsMgr_ in [datasetsMgr, datasetsMgr30, datasetsMgr40, datasetsMgr50, datasetsMgr60, datasetsMgr70, datasetsMgr80, datasetsMgr90, datasetsMgr95]:
            for d in datasetsMgr_.getAllDatasets():
                if "ChargedHiggs" in d.getName():
                    datasetsMgr_.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr50.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr60.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr70.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr80.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr90.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr95.getDataset(d.getName()).setCrossSection(1.0)

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        #plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Print dataset information before removing anything?
        if 0:
            datasetsMgr.PrintInfo()

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        else:
            intLumi = 35920
        # Remove datasets
        filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        #filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        for key in filterKeys:
            datasetsMgr.remove(filter(lambda name: key in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr30.remove(filter(lambda name: key in name, datasetsMgr30.getAllDatasetNames()))
            datasetsMgr40.remove(filter(lambda name: key in name, datasetsMgr40.getAllDatasetNames()))
            datasetsMgr50.remove(filter(lambda name: key in name, datasetsMgr50.getAllDatasetNames()))
            datasetsMgr60.remove(filter(lambda name: key in name, datasetsMgr60.getAllDatasetNames()))
            datasetsMgr70.remove(filter(lambda name: key in name, datasetsMgr70.getAllDatasetNames()))
            datasetsMgr80.remove(filter(lambda name: key in name, datasetsMgr80.getAllDatasetNames()))
            datasetsMgr90.remove(filter(lambda name: key in name, datasetsMgr90.getAllDatasetNames()))
            datasetsMgr95.remove(filter(lambda name: key in name, datasetsMgr95.getAllDatasetNames()))
        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            if "M_" in d.getName():
                if d not in signalMass:
                    continue
            datasetOrder.append(d.getName())
            
        # Append signal datasets
        for m in signalMass:
            datasetOrder.insert(0, m)
        datasetsMgr.selectAndReorder(datasetOrder)
        datasetsMgr30.selectAndReorder(datasetOrder)
        datasetsMgr40.selectAndReorder(datasetOrder)
        datasetsMgr50.selectAndReorder(datasetOrder)
        datasetsMgr60.selectAndReorder(datasetOrder)
        datasetsMgr70.selectAndReorder(datasetOrder)
        datasetsMgr80.selectAndReorder(datasetOrder)
        datasetsMgr90.selectAndReorder(datasetOrder)
        datasetsMgr95.selectAndReorder(datasetOrder)

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Define the mapping histograms in numerator->denominator pairs

        Numerators = ["AllTopQuarkPt_MatchedBDT",
                      "AllTopQuarkPt_Matched",
                      "EventTrijetPt2T_MatchedBDT",
                      "EventTrijetPt2T_MatchedBDT",
                      "EventTrijetPt2T_MatchedBDT",
                      "AllTopQuarkPt_MatchedBDT",
                      "SelectedTrijetsPt_BjetPassCSVdisc_afterCuts",
                      "TrijetFakePt_BDT",
                      ]
        Denominators = ["AllTopQuarkPt_Matched",
                        "TopQuarkPt",
                        "EventTrijetPt2T_BDT",
                        "EventTrijetPt2T_Matched",
                        "EventTrijetPt2T",
                        "TopQuarkPt",
                        "SelectedTrijetsPt_afterCuts",
                        "TrijetFakePt",
                        ]

        
        if 1:
            datasetsMgr.merge("QCD", GetListOfQCDatasets())
            datasetsMgr30.merge("QCD", GetListOfQCDatasets())
            datasetsMgr40.merge("QCD", GetListOfQCDatasets())
            datasetsMgr50.merge("QCD", GetListOfQCDatasets())
            datasetsMgr60.merge("QCD", GetListOfQCDatasets())
            datasetsMgr70.merge("QCD", GetListOfQCDatasets())
            datasetsMgr80.merge("QCD", GetListOfQCDatasets())
            datasetsMgr90.merge("QCD", GetListOfQCDatasets())
            datasetsMgr95.merge("QCD", GetListOfQCDatasets())
            
            plots._plotStyles["QCD"] = styles.getQCDLineStyle()
        #Background1_Dataset = datasetsMgr.getDataset("QCD")

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # For-loop: All numerator-denominator pairs
        for i in range(len(Numerators)):
            numerator = os.path.join(opts.folder, Numerators[i])
            denominator = os.path.join(opts.folder, Denominators[i])
            PlotEfficiency_comparison(datasetsMgr, datasetsMgr30, datasetsMgr40, datasetsMgr50, datasetsMgr60, datasetsMgr70, datasetsMgr80, datasetsMgr90, datasetsMgr95, numerator, denominator, intLumi)
    return
Esempio n. 53
0
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
Esempio n. 54
0
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
Esempio n. 55
0
def main():

    style = tdrstyle.TDRStyle()
    
    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(parseOpts.batchMode)
    ROOT.gErrorIgnoreLevel = 3000
    
    # Get all datasets from the mcrab dir
    datasetsMgr  = GetDatasetsFromDir(parseOpts.mcrab, kwargs.get("analysis"))
    # datasetsMgr  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=kwargs.get("analysis"), includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTB_M_200")
    # datasetsMgr  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=kwargs.get("analysis"), excludeTasks="M_200")

    # Determine Integrated Luminosity
    intLumi = GetLumi(datasetsMgr)

    # Update to PU
    datasetsMgr.updateNAllEventsToPUWeighted()
    
    # Remove datasets
    datasetsMgr.remove(kwargs.get("rmDataset"))
    # datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "QCD" in name in name, datasetsMgr.getAllDatasetNames()))
    
    # Set custom XSections
    # datasetsMgr.getDataset("TT_ext3").setCrossSection(831.76)
    
    
    # Default merging & ordering: "Data", "QCD", "SingleTop", "Diboson"
    plots.mergeRenameReorderForDataMC(datasetsMgr) #WARNING: Merged MC histograms must be normalized to something!

    # Remove datasets (for merged names)
    datasetsMgr.remove(kwargs.get("rmDataset"))
    # datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "QCD" in name in name, datasetsMgr.getAllDatasetNames()))

    
    # For-loop: All Histogram names
    for hName in hNames:
    
        savePath, saveName = GetSavePathAndName(hName, **kwargs)
    
        # Get efficiency histos
        refEff, otherEff = GetCutEfficiencyTGraphs(datasetsMgr, hName, "kFCP", **kwargs)
        # refEff, otherEff = GetCutEfficiencyTGraphs(datasetsMgr, hName, "kFFC", **kwargs)
        # refEff, otherEff = GetCutEfficiencyTGraphs(datasetsMgr, hName, "kBJeffrey", **kwargs)
        # refEff, otherEff = GetCutEfficiencyTGraphs(datasetsMgr, hName, "kFNormal", **kwargs)

        # Plot the efficiencies
        p = plots.ComparisonManyPlot(refEff, otherEff)        
                
        # Create a frame
        opts      = {"ymin": 0.0, "ymaxfactor": 1.2} #"ymax": 5e-1}
        ratioOpts = {"ymin": 0.0, "ymaxfactor": 1.2}
        p.createFrame(saveName, createRatio=kwargs.get("createRatio"), opts=opts, opts2=ratioOpts)
        
        # Customise Legend
        moveLegend = {"dx": -0.1 , "dy": +0.0, "dh": -0.1}
        p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
        #p.removeLegend()
        
        # Customise frame
        # p.setEnergy("13")
        if kwargs.get("createRatio"):
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)

        # SetLog
        SetLogAndGrid(p, **kwargs)

        
        # Add cut line/box
        _kwargs = { "lessThan": kwargs.get("cutLessThan")}
        p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"), fillColor=kwargs.get("cutFillColour"), box=kwargs.get("cutBox"), line=kwargs.get("cutLine"), **_kwargs)

        # Move the refDataset to first in the draw order (back)
        histoNames = [h.getName() for h in p.histoMgr.getHistos()]
        p.histoMgr.reorder(filter(lambda n: plots._legendLabels[kwargs.get("refDataset") ] not in n, histoNames))
        
        #  Draw plots
        p.draw()

        # Customise text
        histograms.addStandardTexts(lumi=intLumi)
        # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
        # histograms.addText(0.4, 0.11, "Runs " + datasetsMgr.loadRunRange(), 17)

        
        # Save canvas under custom dir
        SaveAs(p, savePath, saveName, kwargs.get("saveFormats"))

    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("")
        optModes = optList
    else:
        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 opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Get the PSets:
        if 0:
            datasetsMgr.printSelections()
            #PrintPSet("BJetSelection", datasetsMgr, depth=150)
            #PrintPSet("fakeBMeasurement", datasetsMgr, depth=150)
            sys.exit()

        # 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 a value is not specified
        if opts.intLumi < 0:
            opts.intLumi = datasetsMgr.getDataset("Data").getLuminosity()

        # Remove datasets
        removeList = ["QCD-b", "Charged"]
        if not opts.useMC:
            removeList.append("QCD")
        for i, d in enumerate(removeList, 0):
            msg = "Removing dataset %s" % d
            Verbose(
                ShellStyles.WarningLabel() + msg + ShellStyles.NormalStyle(),
                i == 0)
            datasetsMgr.remove(
                filter(lambda name: d in name,
                       datasetsMgr.getAllDatasetNames()))

        # Print summary of datasets to be used
        if 0:
            datasetsMgr.PrintInfo()

        # Merge EWK samples
        datasetsMgr.merge("EWK", aux.GetListOfEwkDatasets())

        # Print dataset information
        datasetsMgr.PrintInfo()

        # List of TDirectoryFile (_CRone, _CRtwo, _VR, _SR)
        tdirs = [
            "LdgTetrajetMass_", "LdgTetrajetPt_", "LdgTrijetPt_",
            "LdgTrijetMass_", "TetrajetBJetPt_", "TetrajetBJetEta_"
        ]
        region = ["CRone", "CRtwo", "SR", "VR"]
        hList = []
        for d in tdirs:
            for r in region:
                hList.append(d + r)

        # Get the folders with the binned histograms
        folderList_ = datasetsMgr.getDataset(
            datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(
                opts.folder)
        folderList = [h for h in folderList_ if h in hList]

        # For-loop: All folders
        histoPaths = []
        for f in folderList:
            folderPath = os.path.join(opts.folder, f)
            histoList = datasetsMgr.getDataset(
                datasetsMgr.getAllDatasetNames()[0]).getDirectoryContent(
                    folderPath)
            pathList = [os.path.join(folderPath, h) for h in histoList]
            histoPaths.extend(pathList)

        # Get all the bin labels
        binLabels = GetBinLabels("CRone", histoPaths)

        for i, t in enumerate(tdirs, 1):
            myList = []
            for p in histoPaths:
                if t in p:
                    myList.append(p)
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format(
                "Histogram", "%i" % i, "/", "%s:" % (len(tdirs)),
                t.replace("_", ""))
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(),
                  i == 1)

            PlotHistograms(datasetsMgr, myList, binLabels, opts)

    # Save the plots
    Print(
        "All plots saved under directory %s" %
        (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) +
         ShellStyles.NormalStyle()), True)
    return
Esempio n. 57
0
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
Esempio n. 58
0
def main(argv):

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

    dirs.append(sys.argv[1])

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

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

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

        datasets_signal.updateNAllEventsToPUWeighted()
        datasets_signal.loadLuminosities()

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

        plots.mergeRenameReorderForDataMC(datasets_signal)

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

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

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

            datasets.updateNAllEventsToPUWeighted()
            datasets.loadLuminosities()

            plots.mergeRenameReorderForDataMC(datasets)

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

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

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

            invjet_name = "MTInvertedTauIdAfterAllSelectionsPlusJetFakeTau"
            basejet_name = "MTBaselineTauIdAfterAllSelectionsPlusJetFakeTau"

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

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

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

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

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

            style = tdrstyle.TDRStyle()

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

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

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

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(False)
    style.setGridX(False)
    style.setGridY(False)
    
    # 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 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) 
        if 0:
            plots.mergeRenameReorderForDataMC(datasetsMgr) 

        # Print dataset information before removing anything?
        if 0:
            datasetsMgr.PrintInfo()

        # Print datasets info summary
        datasetsMgr.PrintInfo()

        # Re-order datasets
        datasetOrder = []
        haveQCD = False
        for d in datasetsMgr.getAllDatasets():
            if "QCD" in d.getName():
                haveQCD = True
            datasetOrder.append(d.getName())
            
        # Append signal datasets
        datasetsMgr.selectAndReorder(datasetOrder)

        # Define the mapping histograms in numerator->denominator pairs
        VariableList = ["LeadingTrijet_Pt"]
        # VariableList = ["LeadingTrijet_Pt", "LeadingTrijet_Eta", "LeadingTrijet_Phi"]

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        counter =  0
        opts.nDatasets = len(datasetsMgr.getAllDatasets())
        nPlots  = len(VariableList)
        # For-loop: All numerator-denominator pairs
        for var in VariableList:
            hNumerator   = "AfterAllSelections_" + var + "_SR"
            hDenominator = "AfterStandardSelections_" +var + "_SR"
            numerator    = os.path.join(opts.folder, hNumerator)
            denFolder    = opts.folder
            #denFolder    = denFolder.replace("Genuine", "")
            #print "denFolder", denFolder
            denominator  = os.path.join(denFolder, hDenominator)

            counter+=1
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % counter, "/", "%s:" % (nPlots), "%s" % (var))
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), counter==1)
            
            PlotEfficiency(datasetsMgr, numerator, denominator)            
            
    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)
    return
def main(opts):

    style    = tdrstyle.TDRStyle()
    
    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(opts.batchMode)
    
    # Get all datasets from the mcrab dir
    # def GetDatasetsFromDir(mcrab, opts, **kwargs): #iro
    datasetsMgr       = GetDatasetsFromDir(opts.mcrab, opts, **kwargs) #kwargs.get("analysis"))

    # Determine Integrated Luminosity (If Data datasets present)
    intLumi = GetLumi(datasetsMgr)
    
    # Update to PU
    datasetsMgr.updateNAllEventsToPUWeighted()

    # Remove datasets
    datasetsMgr.remove(kwargs.get("rmDataset"))
    # datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "QCD" in name in name, datasetsMgr.getAllDatasetNames()))
    
    # Set custom XSections
    #datasetsMgr.getDataset("QCD_bEnriched_HT1000to1500").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT1500to2000").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT2000toInf").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT300to500").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT500to700").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT700to1000").setCrossSection(1.0)
    
    # Default merging & ordering: "Data", "QCD", "SingleTop", "Diboson"
    plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Remove datasets (for merged names)
    datasetsMgr.remove(kwargs.get("rmDataset"))
    
    # Print the cross
    datasetsMgr.PrintCrossSections()

    for h_prefix in ["reco", "gen"]:
        # Get ref histo here and the fixed histos (TT, QCD, QCD-b
        inclusiveHisto, fixedHistos = GetHistosForPlotter(datasetsMgr, h_prefix + "MET_Et", **kwargs)
        inclusiveHisto.setName("Inclusive")
        inclusiveHisto.setLegendLabel("Inclusive")
        #for hi in fixedHistos:
        #    print(type(hi), hi.getName())
        #return
        # For-loop: All Histogram names
        for counter, hName in enumerate(hNames):
            # Get the save path and name
            savePath, saveName = GetSavePathAndName(h_prefix + hName[0], **kwargs)
            saveName = savePath + h_prefix + hSaveNames[counter]

            # Get Histos for Plotter
            refHisto_, otherHistos_ = GetHistosForPlotter(datasetsMgr, h_prefix + hName[0], **kwargs)
            refHisto2, otherHistos2 = GetHistosForPlotter(datasetsMgr, h_prefix + hName[1], **kwargs)
            refHisto = inclusiveHisto

            # customize histos
            refHisto_.setName(hLegends[counter][0])
            refHisto_.setLegendLabel(hLegends[counter][0])
            refHisto_.setDrawStyle("P")
            refHisto_.setLegendStyle("P")
            styleDict[hStyles[0]].apply(refHisto_.getRootHisto())
            refHisto2.setName(hLegends[counter][1])
            refHisto2.setLegendLabel(hLegends[counter][1])
            refHisto2.setDrawStyle("P")
            refHisto2.setLegendStyle("P")
            styleDict[hStyles[1]].apply(refHisto2.getRootHisto())

            otherHistos = [refHisto_, refHisto2]
            #for hi in otherHistos:
            #     print hi.getName()
            #return

            # Create a comparison plot
            p = plots.ComparisonManyPlot(refHisto, otherHistos)

            # Remove negative contributions
            #RemoveNegativeBins(datasetsMgr, hName, p)

            # Create a frame
            if kwargs.get("logY")==True:
                opts = {"ymin": 8e-5, "ymax": 2}
                #opts = {"ymin": 1e-3, "ymax": 1}
            else:
                opts = {"ymin": 8.e-5, "ymax": 2}
            ratioOpts = {"ymin": 0.1, "ymax": 10.0}
            p.createFrame(saveName, createRatio=kwargs.get("createRatio"), opts=opts, opts2=ratioOpts)
            
            # Customise Legend
            moveLegend = {"dx": -0.2, "dy": +0.0, "dh": -0.1}
            p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
            #p.removeLegend()

            # Customise frame
            p.getFrame().GetYaxis().SetTitle( getTitleY(refHisto, **kwargs) )
            #p.setEnergy("13")
            if kwargs.get("createRatio"):
                p.getFrame2().GetYaxis().SetTitle("Ratio")
                p.getFrame2().GetYaxis().SetTitleOffset(1.7)

            # SetLog
            SetLogAndGrid(p, **kwargs)

            # Add cut line/box
            _kwargs = { "lessThan": kwargs.get("cutLessThan")}
            p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"), fillColor=kwargs.get("cutFillColour"), box=kwargs.get("cutBox"), line=kwargs.get("cutLine"), **_kwargs)
            
            # Move the refDataset to first in the draw order (back)
            histoNames = [h.getName() for h in p.histoMgr.getHistos()]
            p.histoMgr.reorder(filter(lambda n: plots._legendLabels[kwargs.get("refDataset") ] not in n, histoNames))

            #  Draw plots
            p.draw()

            # Customise text
            histograms.addStandardTexts(lumi=intLumi)
            # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
            histograms.addText(0.2, 0.88, plots._legendLabels[kwargs.get("refDataset")], 17)

            # Save canvas under custom dir
            if not os.path.exists(savePath):
                    os.mkdir(savePath)
            SaveAs(p, savePath, saveName, kwargs.get("saveFormats"))

    return