Пример #1
0
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    ROOT.gErrorIgnoreLevel = ROOT.kFatal  # [Options: Print, kInfo, kWarning, kError, kBreak, kSysError, kFatal]
    ROOT.gROOT.SetBatch()
    ROOT.gStyle.SetOptStat(0)
    ROOT.gStyle.SetOptTitle(0)
    ROOT.gStyle.SetNdivisions(5, "X")

    # Find GoF directories (generated by GoF.sh script)
    myDirs = []
    for d in os.listdir('.'):
        if not os.path.isdir(d):
            continue
        if "GoF_" in d:
            myDirs.append(d)

    if len(myDirs) < 1:
        raise Exception(
            "No goodness-of-fit directories found. Did your run the GoF.sh script to create them?"
        )
    else:
        Verbose(
            "Found %d GoF directories: %s" % (len(myDirs), ", ".join(myDirs)),
            True)

    # For-loop: All GoF directories
    myAlgos = []
    allowedAlgos = ["saturated", "KS",
                    "AD"]  # KS = Kolmogorov-Smirnov, AD = Anderson-Darling
    for d in myDirs:
        algo = d.split("_")[-1]
        if algo not in allowedAlgos:
            raise Exception(
                "The algorithm \"%s\" is invalid. Expected one of the following: %s"
                % (opts.algorithm, ", ".join(allowedAlgos)))
        else:
            myAlgos.append(algo)

    # For-loop: All GoF algorithms ran
    Print(
        "Found %d GoF algorithm results: %s" %
        (len(myDirs), ", ".join(myAlgos)), True)
    for i, algo in enumerate(myAlgos, 1):

        # Definitions
        opts.algorithm = algo
        opts.inputDir = "%s/GoF_%s" % (os.getcwd(), algo)
        opts.inputfile = "GoF_%s/higgsCombinetoys*.GoodnessOfFit.mH%s.*.root" % (
            algo, opts.mass)
        opts.outputfile = "GoF_%s/GoF_%s_mH%s.root" % (algo, algo, opts.mass)
        doPlots(i, opts)

    Verbose(
        "All plots saved under directory %s" %
        (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) +
         ShellStyles.NormalStyle()), True)

    return
Пример #2
0
    def __init__(self, opts, basedir):
        self._opts = opts
        self._basedir = basedir
        self._allRetrieved = False
        self._limitCalculated = False
        self._output = ""
        self._findJobDir(basedir)
        if self._jobDir == None:
            if self._opts.printonly:
                msg = "Need to create and submit jobs first! Skipping ..."
                Print(ShellStyles.ErrorLabel() + msg, True)
            else:
                msg = "Creating and submitting " + basedir
                Verbose(msg, True)
                self._createAndSubmit()
        else:
            msg = "Check if limits have already been calculated ..."
            Verbose(msg, True)

            lumiPath = "%s/%s/limits.json" % (self._basedir, self._jobDir)
            if os.path.exists(lumiPath):
                msg = "File \"%s\" already exists!\n\tThe limit has already been calculated.\n\tSkipping ..." % (
                    lumiPath)
                Print(
                    ShellStyles.NoteStyle() + msg + ShellStyles.NormalStyle(),
                    True)
                self._limitCalculated = True
            else:
                msg = "Creating and submitting " + basedir
                Verbose(msg, True)
                self._createAndSubmit()
                #if not self._opts.printonly and not self._opts.lhcTypeAsymptotic:
                #    self._getOutput()
        return
    def __init__(self,
                 dsetMgr,
                 dsetLabelData,
                 dsetLabelEwk,
                 histoName,
                 dataPath,
                 ewkPath,
                 luminosity,
                 optionUseInclusiveNorm,
                 verbose=False):
        self._verbose = verbose
        self._uniqueN = 0
        self._splittedHistoReader = splittedHistoReader.SplittedHistoReader(
            dsetMgr, dsetLabelData)
        self._histoName = histoName
        self._optionUseInclusiveNorm = optionUseInclusiveNorm
        dataFullName = os.path.join(dataPath, histoName)
        ewkFullName = os.path.join(ewkPath, histoName)

        if (self._optionUseInclusiveNorm):
            msg = "Will create \"Inclusive\"-histogram results"
            self.Verbose(
                ShellStyles.NoteStyle() + msg + ShellStyles.NormalStyle(),
                False)
            self._dataList = list(
                self._getInclusiveHistogramsFromSingleSource(
                    dsetMgr, dsetLabelData, dataFullName,
                    luminosity))  # was called by default
            self._ewkList = list(
                self._getInclusiveHistogramsFromSingleSource(
                    dsetMgr, dsetLabelEwk, ewkFullName,
                    luminosity))  # was called by default
        else:
            msg = "Will create \"Splitted\"-histogram results"
            self.Verbose(
                ShellStyles.NoteStyle() + msg + ShellStyles.NormalStyle(),
                False)
            self._dataList = list(
                self._splittedHistoReader.getSplittedBinHistograms(
                    dsetMgr, dsetLabelData, dataFullName,
                    luminosity))  #FIXME: Does this work for Inclusive?
            self._ewkList = list(
                self._splittedHistoReader.getSplittedBinHistograms(
                    dsetMgr, dsetLabelEwk, ewkFullName,
                    luminosity))  #FIXME: Does this work for Inclusive?
        return
Пример #4
0
    def _GetHistoPaths(self, dsetMgr, dataset, folderPath, keyList):
        '''
        Determine list of histograms to consider (full paths)
        The keyList contains strings that if ALL 2are contained in 
        a histogram name the histogram will be added to the list of
        objects to be returned.
        '''
        # Get all objects inside the ROOT file under specific directory 
        msg = "Obtaining all ROOT file contents for dataset %s from folder %s (these must be filled in the Verification Region (VR))" % (dataset, ShellStyles.NoteStyle() + folderPath + ShellStyles.NormalStyle())
        self.Verbose(msg, True)
        allObjects  = dsetMgr.getDataset(dataset).getDirectoryContent(folderPath)
        keepObjects = []
        skipObjects = []

        # Remove anything which does not contain any string from the keyList
        for o in allObjects:
            if all(k in o for k in keyList):
                keepObjects.append(o)
                #print o
            elif any(k in o for k in keyList):
                pass
            else:
                skipObjects.append(o)

        # Count histograms in lists
        nAll  = len(allObjects)
        nKeep = len(keepObjects)
        nSkip = len(skipObjects)

        if nKeep == 0:
            msg = "Did not find any compatible object under dir %s. Check the \"keyList\" provided" % (folderPath)
            raise Exception(ShellStyles.ErrorLabel() + msg + ShellStyles.NormalStyle())

        # Now remove anything which does not contain any string from the keyList
        msg = "Skipping %i histograms for dataset %s. Did not match all keys %s" % (nSkip, dataset, ", ".join(keyList) )
        self.Verbose(ShellStyles.NoteStyle() + msg + ShellStyles.NormalStyle(), True)
        for hName in skipObjects:
            self.Verbose(os.path.join(folderPath, hName), False)

        # Now remove anything which does not contain any string from the keyList
        msg = "Found %i histograms for dataset %s. Matched all keys %s" % (nKeep, dataset, ", ".join(keyList) )
        self.Verbose(ShellStyles.NoteStyle() + msg + ShellStyles.NormalStyle(), True)
        for hName in keepObjects:
            self.Verbose(os.path.join(folderPath, hName), False)
        return keepObjects
Пример #5
0
    def __init__(self,
                 binLabels,
                 resultDirName,
                 moduleInfoString,
                 verbose=False):
        self._verbose = verbose
        self._templates = {}
        self._binLabels = binLabels
        self._sources = {}
        self._commentLines = []
        self._NEvtsCR1 = {}
        self._NEvtsCR1_Error = {}
        self._NEvtsCR2 = {}
        self._NEvtsCR2_Error = {}
        self._NEvtsCR3 = {}
        self._NEvtsCR3_Error = {}
        self._NEvtsCR4 = {}
        self._NEvtsCR4_Error = {}
        self._TF = {}  # Transfer Factor (TF)
        self._TF_Error = {}
        self._TF_Up = {}
        self._TF_Down = {}
        self._dqmKeys = OrderedDict()
        self._myPath = os.path.join(resultDirName, "normalisationPlots")
        self._BinLabelMap = {}
        self._FakeBNormalization = {}  # for the time being same as TF
        self._FakeBNormalizationError = {
        }  # for the time being same as TF_Error
        self._FakeBNormalizationUp = {}  # for the time being same as TF_Up
        self._FakeBNormalizationDown = {}  # for the time being same as TF_Down
        if not isinstance(binLabels, list):
            raise Exception("Error: binLabels needs to be a list of strings")
        self.Verbose("__init__")

        # No optimisation mode
        if moduleInfoString == "":
            moduleInfoString = "Default"

        if not os.path.exists(self._myPath):
            self.Print("Creating new directory %s" % (self._myPath), True)
            os.mkdir(self._myPath)
        self._plotDirName = os.path.join(resultDirName, "normalisationPlots",
                                         moduleInfoString)

        # If already exists, Delete an entire directory tree
        if os.path.exists(self._plotDirName):
            msg = "Removing directory tree %s" % (self._plotDirName)
            self.Verbose(
                ShellStyles.NoteStyle() + msg + ShellStyles.NormalStyle(),
                True)
            shutil.rmtree(self._plotDirName)
        msg = "Creating directory %s" % (self._plotDirName)
        self.Verbose(
            ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(),
            False)
        os.mkdir(self._plotDirName)
        return
Пример #6
0
    def __init__(self, dataPath, ewkPath, dsetMgr, luminosity, moduleInfoString, normFactors,
                 optionDoFakeBNormalisationSyst=True, normDataSrc=None, normEWKSrc=None,
                 optionUseInclusiveNorm=False, keyList=[], verbose=False):
        self._verbose = verbose
        self._shapePlots = []
        self._shapePlotLabels = []
        self._QCDNormalizationSystPlots = []
        self._QCDNormalizationSystPlotLabels = []
        self._moduleInfoString = moduleInfoString
        self._useInclusiveNorm = optionUseInclusiveNorm
        if len(normFactors.keys()) == 1 and normFactors.keys()[0] == "Inclusive":
            self._useInclusiveNorm = True
        self._histoPathsData= self._GetHistoPaths(dsetMgr, "Data", dataPath, keyList)
        if ewkPath == dataPath:
            self._histoPathsEWK = self._histoPathsData
        else:
            self._histoPathsEWK  = self._GetHistoPaths(dsetMgr, "EWK" , ewkPath , keyList)
        
        # Sanity check
        if len(self._histoPathsEWK) != len(self._histoPathsData):
            msg = "List of histograms for EWK does not match in size that of Data"
            raise Exception(ShellStyles.ErrorLabel() + msg + ShellStyles.NormalStyle())
            
        # For-Loop: All plots to consider
        for i, plotName in enumerate(self._histoPathsData, 1):

            # Inform user of progress
            msg = "{:<9} {:>3} {:<1} {:<3} {:<80}".format("Histogram", "%i" % i, "/", "%s:" % (len(self._histoPathsData)), os.path.join(dataPath, plotName) )
            self.PrintFlushed(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), False)

            if "JetEtaPhi_AfterAllSelections" in plotName:
                continue 

            # Ensure that histograms exist && pass other sanity checks
            dataOk = self._sanityChecks(dsetMgr, dataPath, plotName) 
            ewkOk  = self._sanityChecks(dsetMgr, ewkPath , plotName)

            if dataOk*ewkOk == False:
                self.Print(ShellStyles.ErrorStyle() + msg + ShellStyles.NormalStyle(), i==1)
                continue
            
            self.Verbose("Obtaining shape plots (the returned object is not owned)", True)
            myShapeHisto = self._obtainShapeHistograms(i, dataPath, ewkPath, dsetMgr, plotName, luminosity, normFactors)

            # Obtain plots for systematics coming from invariant mass shape difference
            if optionDoFakeBNormalisationSyst:
                if isinstance(myShapeHisto, ROOT.TH2):
                    msg = "Skipping invariant mass shape uncertainty because histogram has more than 1 dimensions!"
                    self.Print(ShellStyles.WarningLabel() + msg, True)
                else:
                    self._obtainQCDNormalizationSystHistograms(myShapeHisto, dsetMgr, plotName, luminosity, normDataSrc, normEWKSrc) #iro: fixme (missing plots)
            
        msg = "Obtaining final shape from data path %s" % (ShellStyles.NoteStyle() + dataPath + ShellStyles.NormalStyle())
        self.Verbose(msg, True)
        return
def getTransferFactorsSrcFilename(dirName, fileName):
    src = os.path.join(dirName, fileName)
    if not os.path.exists(src):
        msg = "Normalisation factors ('%s') not found!\nRun script \"./getABCD_TF.py\" to auto-generate the transfer factors (TFs) file." % src
        raise Exception(ShellStyles.ErrorStyle() + msg +
                        ShellStyles.NormalStyle())
    else:
        Print(
            "Importing transfer factors (TFs) from file %s" %
            (ShellStyles.NoteStyle() + os.path.basename(src) +
             ShellStyles.NormalStyle()), True)
    return src
Пример #8
0
 def GetEnergy(self, dsetMgr):
     '''
     Obtain luminosity information 
     from the dataset manager
     '''
     if isinstance(dsetMgr.getDataset("Data"), dataset.Dataset):
         myEnergy = dsetMgr.getDataset("Data").getEnergy()
     else:
         myEnergy = dsetMgr.getDataset("Data").datasets[0].getEnergy()
     msg = "Centre-of-Mass energy is %s TeV" % (
         ShellStyles.NoteStyle() + myEnergy + ShellStyles.NormalStyle())
     self.Verbose(msg, True)
     return float(myEnergy)
Пример #9
0
    def GetLuminosity(self, dsetMgr):
        '''
        Obtain luminosity information from the
        dataset manager by looping over all data 
        datasets
        '''
        myLuminosity = 0.0
        myDataDatasets = dsetMgr.getDataDatasets()
        for d in myDataDatasets:
            myLuminosity += d.getLuminosity()

        msg = "Integrated luminosity is %s%.3f%s" % (
            ShellStyles.NoteStyle(), myLuminosity, ShellStyles.NormalStyle())
        self.Verbose(msg, True)
        return myLuminosity
Пример #10
0
    def GetDataVersion(self, dsetMgr):
        '''
        Copy data version and set it to pseudo
        '''
        objs = None
        realNames = None
        if isinstance(dsetMgr.getDataset("Data"), dataset.Dataset):
            (objs, realNames) = dsetMgr.getDataset("Data").getRootObjects(
                "../configInfo/dataVersion")
        else:
            (objs, realNames) = dsetMgr.getDataset(
                "Data").datasets[0].getRootObjects("../configInfo/dataVersion")
        dataVersion = objs[0].Clone()

        msg = "The data-version is %s" % (ShellStyles.NoteStyle() +
                                          dataVersion.GetTitle() +
                                          ShellStyles.NormalStyle())
        self.Verbose(msg, True)
        return dataVersion
def getSystematicsNames(mySystematicsNamesRaw, opts):
    mySystematicsNames = []
    # Sanity check
    if len(mySystematicsNamesRaw) < 1:
        Print("There are 0 systematic variation sources", True)
        return mySystematicsNames
    if opts.test:
        Print("Disabling systematic variations", True)
        return mySystematicsNames

    # For-loop: All systematics raw names
    for i, item in enumerate(mySystematicsNamesRaw, 0):
        # Print("Using systematic %s" % (ShellStyles.NoteStyle() + item + ShellStyles.NormalStyle()), i==0)
        mySystematicsNames.append("%sPlus" % item)
        mySystematicsNames.append("%sMinus" % item)

    Print(
        "There are %d systematic variation sources:%s\n\t%s%s" %
        (len(mySystematicsNames), ShellStyles.NoteStyle(),
         "\n\t".join(mySystematicsNames), ShellStyles.NormalStyle()), True)
    return mySystematicsNames
Пример #12
0
    def __init__(self, resultDirName, moduleInfoString, verbose=False):
        self._verbose = verbose
        self._templates = {}
        self._sources = {}
        self._commentLines = []
        self._BinLabelMap = {}
        self._TF = {}  # Transfer Factor (TF)
        self._TF_Error = {}
        self._TF_Up = {}
        self._TF_Down = {}
        self._dqmKeys = OrderedDict()
        self._myPath = os.path.join(resultDirName, "normalisationPlots")
        self.Verbose("__init__")

        # No optimisation mode
        if moduleInfoString == "":
            moduleInfoString = "Default"

        if not os.path.exists(self._myPath):
            self.Print("Creating new directory %s" % (self._myPath), True)
            os.mkdir(self._myPath)
        self._plotDirName = os.path.join(resultDirName, "normalisationPlots",
                                         moduleInfoString)

        # If already exists, Delete an entire directory tree
        if os.path.exists(self._plotDirName):
            msg = "Removing directory tree %s" % (self._plotDirName)
            self.Verbose(
                ShellStyles.NoteStyle() + msg + ShellStyles.NormalStyle(),
                True)
            shutil.rmtree(self._plotDirName)
        msg = "Creating directory %s" % (self._plotDirName)
        self.Verbose(
            ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(),
            False)
        os.mkdir(self._plotDirName)
        return
Пример #13
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

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

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

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

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

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


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

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

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

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

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

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

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

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

    # Set text mode
    histograms.cmsTextMode = histograms.CMSMode.PRELIMINARY
    # histograms.cmsTextMode = histograms.CMSMode.NONE

    # Definitions
    savePath = opts.saveDir
    if opts.url:
        savePath = opts.saveDir.replace(
            "/afs/cern.ch/user/a/attikis/public/html",
            "https://cmsdoc.cern.ch/~%s" % getpass.getuser())

    # Do the Resolved H->tb fully hadronic final state comparison
    myList1 = [
        ("H^{+}#rightarrow tb (#chi^{2})",
         "*limits2017/datacards_default_170827_075947_noLumi/CombineResults_taujets_*"
         ),
        #("p_{T}^{b3}#geq40, BDT#geq0.85"        , "limits2018/datacards_Hplus2tbAnalysis_NewLeptonVeto_PreSel_3bjets40_SigSel_MVA0p85_180126_030205/CombineResults*"),
        #("p_{T}^{b3}#geq40, BDT#geq0.85 (lev1)" , "limits2018/datacards_Hplus2tbAnalysis_NewLeptonVeto_PreSel_3bjets40_SigSel_MVA0p85_180126_030205_level1/CombineResults*"),
        ("p_{T}^{b3}#geq40, BDT#geq0.85 (lev2)",
         "limits2018/datacards_Hplus2tbAnalysis_NewLeptonVeto_PreSel_3bjets40_SigSel_MVA0p85_180126_030205_level2/CombineResults*"
         ),
        #("p_{T}^{b3}#geq40, BDT#geq0.85 (lev3)"         , "limits2018/datacards_Hplus2tbAnalysis_NewLeptonVeto_PreSel_3bjets40_SigSel_MVA0p85_180126_030205_level3/CombineResults*"),
        #("p_{T}^{b3}#geq40, BDT#geq0.85 (lev2, BugFix)" , "limits2018/datacards_Hplus2tbAnalysis_PreSel_3CSVv2M_Pt40_SigSel_MVA0p85_180214_074442_level2_MajorBugFix/CombineResults*"),
        ("p_{T}^{b3}#geq40, BDT#geq0.85 (lev2, BugFix)",
         "limits2018/datacards_Hplus2tbAnalysis_PreSel_3CSVv2M_Pt40_SigSel_MVA0p85_180214_074442_level2_MajorBugFix_StatOnly/CombineResults*"
         ),
    ]
    opts.name = "h2tb"
    doCompare(opts.name, myList1)

    # Do all H->tb fully hadronic final states comparison
    myList2 = [
        ("H^{+}#rightarrow tb (#chi^{2})",
         "*limits2017/datacards_default_170827_075947_noLumi/CombineResults_taujets_*"
         ),
        #("H^{+}#rightarrow tb (Fake-b binned)", "limits2018/datacards_Hplus2tbAnalysis_PreSel_3bjets40_SigSel_MVA0p85_180126_030205_level3/CombineResults*"),
        #("H^{+}#rightarrow tb (MC)"           , "limits2018/datacards_NewLeptonVeto_3bjets40_MVA0p85_MVA0p85_TopMassCutOff600GeV_180122_022900/CombineResults*"),
        #("H^{+}#rightarrow tb"                , "limits2018/datacards_Hplus2tbAnalysis_NewLeptonVeto_PreSel_3bjets40_SigSel_MVA0p85_180126_030205_level3/CombineResults*"),
        ("H^{+}#rightarrow tb",
         "limits2018/datacards_Hplus2tbAnalysis_NewLeptonVeto_PreSel_3bjets40_SigSel_MVA0p85_180126_030205_level3/CombineResults*"
         ),
        ("H^{+}#rightarrow tb (BugFix)",
         "limits2018/datacards_Hplus2tbAnalysis_PreSel_3CSVv2M_Pt40_SigSel_MVA0p85_180214_074442_level2_MajorBugFix_StatOnly/CombineResults*"
         ),
        #("H^{+}#rightarrow tb (BugFix, Lumi)" , "limits2018/datacards_Hplus2tbAnalysis_PreSel_3CSVv2M_Pt40_SigSel_MVA0p85_180214_074442_level2_MajorBugFix/CombineResults*"),
        ("H^{+}#rightarrow tb (~boosted)",
         "limits2017/*datacards_combine_MIT_approximate/CombineResults_taujets_*"
         ),
        # ("Single Lepton"                 , "limits2017/*datacards_combine_SingleLepton_approximate/CombineResults_taujets_*"),
    ]

    # Do the second list of plots
    opts.name = "all"
    doCompare(opts.name, myList2)

    # Inform user and exit
    Print(
        "All plots saved under directory %s" %
        (ShellStyles.NoteStyle() + savePath + ShellStyles.NormalStyle()), True)
    return
Пример #15
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)
    # If you want BOTH pads (main and ratio) in log scale 
    if 0:
        style.setLogY(opts.logY) 

    # Overwrite default legends
    plots._legendLabels["MCStatError"] = "Bkg. stat."
    plots._legendLabels["MCStatSystError"] = "Bkg. stat.#oplussyst."
    plots._legendLabels["BackgroundStatError"] = "Bkg. stat. unc"
    plots._legendLabels["BackgroundStatSystError"] = "Bkg. stat.#oplussyst. unc."
    
    # Define optimisatio modes to run on
    optModes = [""] #["", "OptChiSqrCutValue50", "OptChiSqrCutValue100"]

    if opts.optMode != None:
        optModes = [opts.optMode]
        
    # Inform user of EWK datasets used
    Print("The EWK datasets used are the following:", True)
    for i,d in enumerate(aux.GetListOfEwkDatasets(), 1):
        Print(ShellStyles.NoteStyle() + d + ShellStyles.NormalStyle(), i==0)

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

        # Setup & configure the dataset manager 
        dsetMgr1 = GetDatasetsFromDir(opts, False) 
        dsetMgr2 = GetDatasetsFromDir(opts, True)

        # Setup the dataset managers
        dsetMgr1.updateNAllEventsToPUWeighted()
        dsetMgr2.updateNAllEventsToPUWeighted()

        # Load luminosities
        dsetMgr1.loadLuminosities() # from lumi.json
        # dsetMgr2.loadLuminosities()

        # Print PSets. Perhaps i can use this to ensure parameters are matching!
        if 0:
            dsetMgr1.printSelections()
            dsetMgr2.printSelections()
            PrintPSet("FakeBMeasurement", dsetMgr1)
            PrintPSet("TopSelectionBDT" , dsetMgr2)

        # Remove datasets with overlap?
        removeList = ["QCD-b"]
        dsetDY     = "DYJetsToQQ_HT180"
        dsetZJ     = "ZJetsToQQ_HT600toInf"
        dsetRM     = dsetZJ # datasets with overlap
        removeList.append(dsetRM)

        # Set/Overwrite cross-sections. Remove all but 1 signal mass 
        for d in dsetMgr1.getAllDatasets():
            if "ChargedHiggs" in d.getName():
                dsetMgr1.getDataset(d.getName()).setCrossSection(1.0) # ATLAS 13 TeV H->tb exclusion limits
                if d.getName() != opts.signal:
                    removeList.append(d.getName())

        # Print useful information?
        if opts.verbose:
            dsetMgr1.PrintCrossSections()
            dsetMgr1.PrintLuminosities()
            dsetMgr2.PrintCrossSections()
            dsetMgr2.PrintLuminosities()

        # Merge histograms
        plots.mergeRenameReorderForDataMC(dsetMgr1) 
   
        # Get the luminosity
        if opts.intLumi < 0:
            opts.intLumi = dsetMgr1.getDataset("Data").getLuminosity()

        # 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)
            dsetMgr1.remove(filter(lambda name: d == name, dsetMgr1.getAllDatasetNames()))

        # Print dataset information
        dsetMgr1.PrintInfo()
        dsetMgr2.PrintInfo()

        # Replace MC datasets with data-driven
        if not opts.useMC:
            replaceQCD(dsetMgr1, dsetMgr2, "FakeBMeasurementTrijetMass", "FakeB") #dsetMgr1 now contains "FakeB" pseudo-dataset

        # Definitions
        allHistos   = dsetMgr2.getAllDatasets()[0].getDirectoryContent(opts.folder)
        histoPaths  = []
        ignoreKeys  = ["MCEWK", "Purity", "BJetPt", "BJetEta", "BtagDiscriminator", "METPhi", "MHT", "NBjets", "Njets", "_Vs_", "JetEta"]
        # For-loop: All histograms in directory
        for h in allHistos:
            bKeep = True
            for k in ignoreKeys:
                if k in h:
                    bKeep = False
                    continue
            if bKeep:
                histoPaths.append(os.path.join(opts.folder, h))

        # For-loop: All histograms in list
        for i, hName in enumerate(histoPaths, 1):
            
            msg = "{:<9} {:>3} {:<1} {:<3} {:<50}".format("Histogram", "%i" % i, "/", "%s:" % (len(histoPaths)), hName)
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(), i==1)

            PlotHistogram(dsetMgr1, hName, opts)

    Print("All plots saved under directory %s" % (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) + ShellStyles.NormalStyle()), True)    
    return
Пример #16
0
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 = [
            "LdgTrijetPt_", "LdgTrijetMass_", "TetrajetBJetPt_",
            "TetrajetBJetEta_", "LdgTetrajetPt_", "LdgTetrajetMass_"
        ]
        region = ["CRone", "CRtwo"]
        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
def main(opts):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        # Plot the histo graphs
        PlotHistoGraphs(hGraphList, _kwargs)

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

    # Object for selecting data eras, search modes, and optimization modes
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector()

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

    # Obtain systematics names
    mySystematicsNamesRaw = dsetMgrCreator.getSystematicVariationSources()
    mySystematicsNames = getSystematicsNames(mySystematicsNamesRaw, opts)

    # Set the primary source
    Verbose(
        "Setting the primary source (label=%s)" %
        (ShellStyles.NoteStyle() + opts.analysisName +
         ShellStyles.NormalStyle()), True)
    myModuleSelector.setPrimarySource(label=opts.analysisName,
                                      dsetMgrCreator=dsetMgrCreator)

    # Select modules
    myModuleSelector.doSelect(opts=None)

    # Loop over era/searchMode/optimizationMode combos
    myTotalModules = myModuleSelector.getSelectedCombinationCount() * (
        len(mySystematicsNames) + 1)
    count, nEras, nSearchModes, nOptModes, nSysVars = myModuleSelector.getSelectedCombinationCountIndividually(
    )
    if nSysVars > 0:
        msg = "Running over %d modules (%d eras x %d searchModes x %d optimizationModes x %d systematic variations)" % (
            count, nEras, nSearchModes, nOptModes, nSysVars)
    else:
        msg = "Running over %d modules (%d eras x %d searchModes x %d optimizationModes)" % (
            count, nEras, nSearchModes, nOptModes)
    Verbose(msg, True)

    # Create pseudo-multicrab creator
    msg = "Creating pseudo-dataset \"%s\" inside the pseudo-multicrab directory \"%s\"" % (
        opts.newDsetName, opts.mcrab)
    Verbose(msg, True)
    myOutputCreator = pseudoMultiCrabCreator.PseudoMultiCrabCreator(
        opts.newDsetName, opts.mcrab, verbose=opts.verbose)

    # Make time stamp for start time
    myGlobalStartTime = time.time()

    # Initialize
    myOutputCreator.initialize(subTitle="", prefix="")

    # Get lists of settings
    erasList = myModuleSelector.getSelectedEras()
    modesList = myModuleSelector.getSelectedSearchModes()
    optList = myModuleSelector.getSelectedOptimizationModes()
    if 0:
        optList.append(
            "")  #append the default opt mode iff more optimization modes exist
    Verbose(
        "Found %d eras, %d modes, %d optimisations" %
        (len(erasList), len(modesList), len(optList)), True)

    iModule = 0
    # For-loop: All eras
    for era in erasList:
        # For-loop: All searchModes
        for searchMode in modesList:
            # For-loop: AlloptimizationModes
            for optimizationMode in optList:
                Verbose(
                    "era = %s, searchMode = %s, optMode = %s" %
                    (era, searchMode, optimizationMode), True)

                # If an optimization mode is defined in options skip the rest
                if opts.optMode != None:
                    if optimizationMode != opts.optMode:
                        continue

                # Nominal module
                myModuleInfoString = getModuleInfoString(
                    opts.analysisName, era, searchMode, optimizationMode)
                iModule += 1

                # Inform user of what is being processes
                msg = "{:<10} {:>3} {:^1} {:<3}: {:<20}".format(
                    "Module", iModule, "/", myTotalModules, myModuleInfoString)
                Print(hs + msg + ns, iModule == 1)

                Verbose("Creating dataset manager for nominal module", True)
                myStartTime = time.time()

                nominalModule = ModuleBuilder(opts, myOutputCreator,
                                              opts.verbose)
                nominalModule.createDsetMgr(opts.mcrab, opts.analysisName, era,
                                            searchMode, optimizationMode)

                # Build the module
                nominalModule.buildModule(opts.dsetSrc)

                Verbose("Deleting nominal module", True)
                nominalModule.delete()

                Verbose("Printing time estimate", True)
                printTimeEstimate(myGlobalStartTime, myStartTime, iModule,
                                  myTotalModules)

                Verbose("Now do the rest of systematics variations", True)
                for syst in mySystematicsNames:

                    myModuleInfoString = getModuleInfoString(
                        opts.analysisName, era, searchMode, optimizationMode,
                        syst)

                    iModule += 1
                    msg = "{:<10} {:>3} {:^1} {:<3}: {:<20}".format(
                        "Module", iModule, "/", myTotalModules,
                        myModuleInfoString)
                    Print(hs + msg + ns, False)

                    Verbose(
                        "Creating dataset manager for systematics module %s" %
                        (syst), True)
                    myStartTime = time.time()
                    systModule = ModuleBuilder(opts, myOutputCreator,
                                               opts.verbose)
                    systModule.createDsetMgr(opts.mcrab,
                                             opts.analysisName,
                                             era,
                                             searchMode,
                                             optimizationMode,
                                             systematicVariation=syst)

                    # Build the module
                    systModule.buildModule(opts.dsetSrc)

                    Verbose("Deleting nominal module", True)
                    systModule.delete()

                    Verbose("Printing time estimate", True)
                    printTimeEstimate(myGlobalStartTime, myStartTime, iModule,
                                      myTotalModules)

        Verbose(
            "New dataset %s added to pseud-multicrab %s" %
            (hs + opts.newDsetName + ns, hs + opts.mcrab + ns), True)

    # Print some timing statistics
    Verbose(
        "Average processing time per module was %.1f seconds" %
        getAvgProcessTimeForOneModule(myGlobalStartTime, myTotalModules), True)
    Verbose(
        "Total elapsed time was %.1f seconds" %
        getTotalElapsedTime(myGlobalStartTime), True)

    # Create rest of pseudo multicrab directory
    myOutputCreator.finalize(silent=False)

    # Update the multicrab.cfg file
    updateMulticrabCfg()

    return
Пример #19
0
def PlotHistosAndCalculateTF(datasetsMgr, histoList, binLabels, opts):

    # Get the histogram customisations (keyword arguments)
    _kwargs = GetHistoKwargs(histoList[0])

    # Get the root histos for all datasets and Control Regions (CRs)
    regions = ["SR", "VR", "CRone", "CRtwo"]
    rhDict = GetRootHistos(datasetsMgr, histoList, regions, binLabels)

    #=========================================================================================
    # Calculate the Transfer Factor (TF) and save to file
    #=========================================================================================
    manager = FakeBNormalization.FakeBNormalizationManager(binLabels,
                                                           opts.mcrab,
                                                           opts.optMode,
                                                           verbose=False)
    if opts.inclusiveOnly:
        #manager.CalculateTransferFactor(binLabels[0], rhDict["CRone-FakeB"], rhDict["CRtwo-FakeB"])
        binLabel = "Inclusive"
        manager.CalculateTransferFactor("Inclusive",
                                        rhDict["FakeB-CRone-Inclusive"],
                                        rhDict["FakeB-CRtwo-Inclusive"])
    else:
        for bin in binLabels:
            manager.CalculateTransferFactor(bin,
                                            rhDict["FakeB-CRone-%s" % bin],
                                            rhDict["FakeB-CRtwo-%s" % bin])

    # Get unique a style for each region
    for k in rhDict:
        dataset = k.split("-")[0]
        region = k.split("-")[1]
        styles.getABCDStyle(region).apply(rhDict[k])
        if "FakeB" in k:
            styles.getFakeBStyle().apply(rhDict[k])
        # sr.apply(rhDict[k])

    # =========================================================================================
    # Create the final plot object
    # =========================================================================================
    rData_SR = rhDict["Data-SR-Inclusive"]
    rEWKGenuineB_SR = rhDict["EWK-SR-Inclusive-EWKGenuineB"]
    rBkgSum_SR = rhDict["FakeB-VR-Inclusive"].Clone("BkgSum-SR-Inclusive")
    rBkgSum_SR.Reset()

    if opts.inclusiveOnly:
        bin = "Inclusive"
        # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) )
        binHisto_VR = rhDict["FakeB-VR-%s" % (bin)]
        VRtoSR_TF = manager.GetTransferFactor(bin)
        Print(
            "Applying TF = %s%0.6f%s to VR shape" %
            (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()),
            True)
        binHisto_VR.Scale(VRtoSR_TF)
        # Add the normalised histogram to the final Inclusive SR (predicted) histo
        rBkgSum_SR.Add(binHisto_VR, +1)
    else:
        # For-loop: All bins
        for i, bin in enumerate(binLabels, 1):
            if bin == "Inclusive":
                continue
            # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) )
            binHisto_VR = rhDict["FakeB-VR-%s" % (bin)]
            VRtoSR_TF = manager.GetTransferFactor(bin)
            Print(
                "Applying TF = %s%0.6f%s to VR shape" %
                (ShellStyles.NoteStyle(), VRtoSR_TF,
                 ShellStyles.NormalStyle()), i == 1)
            binHisto_VR.Scale(VRtoSR_TF)
            # Add the normalised histogram to the final Inclusive SR (predicted) histo
            rBkgSum_SR.Add(binHisto_VR, +1)

    #Print("Got Verification Region (VR) shape %s%s%s" % (ShellStyles.NoteStyle(), rFakeB_VR.GetName(), ShellStyles.NormalStyle()), True)

    # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) )
    #VRtoSR_TF = manager.GetTransferFactor("Inclusive")
    #Print("Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), True)
    #rBkgSum_SR.Scale(VRtoSR_TF)

    # Plot histograms
    if opts.altPlot:
        # Add the SR EWK Genuine-b to the SR FakeB ( BkgSum = [FakeB] + [GenuineB-MC] = [VR * (CR1/CR2)] + [GenuineB-MC] )
        rBkgSum_SR.Add(rEWKGenuineB_SR, +1)

        # Change style
        styles.getGenuineBStyle().apply(rBkgSum_SR)

        # Remove unsupported settings of kwargs
        _kwargs["stackMCHistograms"] = False
        _kwargs["addLuminosityText"] = False

        # Create the plot
        p = plots.ComparisonManyPlot(rData_SR, [rBkgSum_SR], saveFormats=[])

        # Set draw / legend style
        p.histoMgr.setHistoDrawStyle("Data-SR-Inclusive", "P")
        p.histoMgr.setHistoLegendStyle("Data-SR-Inclusive", "LP")
        p.histoMgr.setHistoDrawStyle("BkgSum-SR-Inclusive", "HIST")
        p.histoMgr.setHistoLegendStyle("BkgSum-SR-Inclusive", "F")

        # Set legend labels
        p.histoMgr.setHistoLegendLabelMany({
            "Data-SR": "Data",
            "BkgSum-SR": "Fake-b + Gen-b",
        })
    else:
        # Create empty histogram stack list
        myStackList = []

        # Signal
        p2 = plots.DataMCPlot(datasetsMgr,
                              "ForTestQGLR/QGLR_SR/QGLR_SRInclusive",
                              saveFormats=[])

        hSignal_800 = p2.histoMgr.getHisto(
            'ChargedHiggs_HplusTB_HplusToTB_M_800').getRootHisto()
        hhSignal_800 = histograms.Histo(
            hSignal_800, 'ChargedHiggs_HplusTB_HplusToTB_M_800',
            "H^{+} m_{H^+}=800 GeV")
        hhSignal_800.setIsDataMC(isData=False, isMC=True)
        myStackList.append(hhSignal_800)

        hSignal_250 = p2.histoMgr.getHisto(
            'ChargedHiggs_HplusTB_HplusToTB_M_250').getRootHisto()
        hhSignal_250 = histograms.Histo(
            hSignal_250, 'ChargedHiggs_HplusTB_HplusToTB_M_250',
            "H^{+} m_{H^+}=250 GeV"
        )  #plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_250'])
        hhSignal_250.setIsDataMC(isData=False, isMC=True)
        #myStackList.append(hhSignal_250)

        hSignal_500 = p2.histoMgr.getHisto(
            'ChargedHiggs_HplusTB_HplusToTB_M_500').getRootHisto()
        hhSignal_500 = histograms.Histo(
            hSignal_500, 'ChargedHiggs_HplusTB_HplusToTB_M_500',
            "H^{+} m_{H^+}=500 GeV"
        )  #plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_500'])
        hhSignal_500.setIsDataMC(isData=False, isMC=True)
        #myStackList.append(hhSignal_500)

        hSignal_1000 = p2.histoMgr.getHisto(
            'ChargedHiggs_HplusTB_HplusToTB_M_1000').getRootHisto()
        hhSignal_1000 = histograms.Histo(
            hSignal_1000, 'ChargedHiggs_HplusTB_HplusToTB_M_1000',
            "H^{+} m_{H^+}=1000 GeV"
        )  #plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_500'])
        hhSignal_1000.setIsDataMC(isData=False, isMC=True)
        #myStackList.append(hhSignal_1000)

        # Add the FakeB data-driven background to the histogram list
        hFakeB = histograms.Histo(rBkgSum_SR, "FakeB", "Fake-b")
        hFakeB.setIsDataMC(isData=False, isMC=True)
        myStackList.append(hFakeB)

        # Add the EWKGenuineB MC background to the histogram list
        hGenuineB = histograms.Histo(rEWKGenuineB_SR, "GenuineB",
                                     "EWK Genuine-b")
        hGenuineB.setIsDataMC(isData=False, isMC=True)
        myStackList.append(hGenuineB)

        # Add the collision datato the histogram list
        hData = histograms.Histo(rData_SR, "Data", "Data")
        hData.setIsDataMC(isData=True, isMC=False)
        myStackList.insert(0, hData)

        p = plots.DataMCPlot2(myStackList, saveFormats=[])
        p.setLuminosity(opts.intLumi)
        p.setDefaultStyles()

    # Draw the plot and save it
    hName = "test"
    plots.drawPlot(p, hName, **_kwargs)
    SavePlot(p,
             hName,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png", ".pdf"])

    #==========================================================================================
    # Calculate Cut-Flow Efficiency
    #==========================================================================================
    kwargs = {
        "rebinX": 1,
        "xlabel": "QGLR",
        "ylabel": "Significance / %.02f ",
        "opts": {
            "ymin": 0.0,
            "ymaxfactor": 1.3
        },
        "createLegend": {
            "x1": 0.55,
            "y1": 0.70,
            "x2": 0.92,
            "y2": 0.92
        },
        #            "cutBox"           : {"cutValue": 0.0, "fillColor" : 16, "box": False, "line": False, "greaterThan": True},
    }

    efficiencyList = []
    xValues = []

    yValues_250 = []
    yValues_500 = []
    yValues_800 = []
    yValues_1000 = []
    yValues_Bkg = []

    nBins = hSignal_250.GetNbinsX() + 1

    hBkg = p.histoMgr.getHisto(
        "ChargedHiggs_HplusTB_HplusToTB_M_800").getRootHisto().Clone("Bkg")
    hBkg.Reset()

    # Bkg: FakeB + Genuine B
    hBkg.Add(hFakeB.getRootHisto(), +1)
    hBkg.Add(hGenuineB.getRootHisto(), +1)

    for i in range(0, nBins):

        # Cut value
        cut = hSignal_250.GetBinCenter(i)

        passed_250 = hSignal_250.Integral(i, hSignal_250.GetXaxis().GetNbins())
        passed_500 = hSignal_500.Integral(i, hSignal_500.GetXaxis().GetNbins())
        passed_800 = hSignal_800.Integral(i, hSignal_800.GetXaxis().GetNbins())
        passed_1000 = hSignal_1000.Integral(i,
                                            hSignal_1000.GetXaxis().GetNbins())

        passed_Bkg = hBkg.Integral(i, hBkg.GetXaxis().GetNbins())

        total_250 = hSignal_250.Integral()
        total_500 = hSignal_500.Integral()
        total_800 = hSignal_800.Integral()
        total_1000 = hSignal_1000.Integral()
        total_Bkg = hBkg.Integral()

        eff_250 = float(passed_250) / total_250
        eff_500 = float(passed_500) / total_500
        eff_800 = float(passed_800) / total_800
        eff_1000 = float(passed_1000) / total_1000

        eff_Bkg = float(passed_Bkg) / total_Bkg

        xValues.append(cut)
        yValues_250.append(eff_250)
        yValues_500.append(eff_500)
        yValues_800.append(eff_800)
        yValues_1000.append(eff_1000)

        yValues_Bkg.append(eff_Bkg)

    # Create the Efficiency Plot
    tGraph_250 = ROOT.TGraph(len(xValues), array.array("d", xValues),
                             array.array("d", yValues_250))
    tGraph_500 = ROOT.TGraph(len(xValues), array.array("d", xValues),
                             array.array("d", yValues_500))
    tGraph_800 = ROOT.TGraph(len(xValues), array.array("d", xValues),
                             array.array("d", yValues_800))
    tGraph_1000 = ROOT.TGraph(len(xValues), array.array("d", xValues),
                              array.array("d", yValues_1000))
    tGraph_Bkg = ROOT.TGraph(len(xValues), array.array("d", xValues),
                             array.array("d", yValues_Bkg))

    styles.getSignalStyleHToTB_M("200").apply(tGraph_250)
    styles.getSignalStyleHToTB_M("500").apply(tGraph_500)
    styles.getSignalStyleHToTB_M("800").apply(tGraph_800)
    styles.getSignalStyleHToTB_M("1000").apply(tGraph_1000)
    styles.getQCDLineStyle().apply(tGraph_Bkg)

    drawStyle = "CPE"
    effGraph_250 = histograms.HistoGraph(tGraph_250,
                                         "H^{+} m_{H^{+}} = 250 GeV", "lp",
                                         drawStyle)
    effGraph_500 = histograms.HistoGraph(tGraph_500,
                                         "H^{+} m_{H^{+}} = 500 GeV", "lp",
                                         drawStyle)
    effGraph_800 = histograms.HistoGraph(tGraph_800,
                                         "H^{+} m_{H^{+}} = 800 GeV", "lp",
                                         drawStyle)
    effGraph_1000 = histograms.HistoGraph(tGraph_1000,
                                          "H^{+} m_{H^{+}} = 1000 GeV", "lp",
                                          drawStyle)
    effGraph_Bkg = histograms.HistoGraph(tGraph_Bkg, "Bkg", "lp", drawStyle)

    efficiencyList.append(effGraph_250)
    efficiencyList.append(effGraph_500)
    efficiencyList.append(effGraph_800)
    efficiencyList.append(effGraph_1000)
    efficiencyList.append(effGraph_Bkg)

    # Efficiency plot
    pE = plots.PlotBase(efficiencyList, saveFormats=["pdf"])
    pE.createFrame("QGLR_Efficiency")
    pE.setEnergy("13")
    pE.getFrame().GetYaxis().SetLabelSize(18)
    pE.getFrame().GetXaxis().SetLabelSize(20)

    pE.getFrame().GetYaxis().SetTitle("Efficiency / 0.01")
    pE.getFrame().GetXaxis().SetTitle("QGLR Cut")

    # Add Standard Texts to plot
    histograms.addStandardTexts()

    # Customise Legend
    moveLegend = {"dx": -0.50, "dy": -0.5, "dh": -0.1}
    pE.setLegend(histograms.moveLegend(histograms.createLegend(),
                                       **moveLegend))

    pE.draw()
    #    plots.drawPlot(pE, "QGLR_Efficiency", **kwargs)
    SavePlot(pE,
             "QGLR_Efficiency",
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png", ".pdf"])

    #==========================================================================================
    # Calculate Significance
    #==========================================================================================

    SignalName = "ChargedHiggs_HplusTB_HplusToTB_M_800"

    hSignif_250 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(
        SignalName)
    hSignif_500 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(
        SignalName)
    hSignif_800 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(
        SignalName)
    hSignif_1000 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(
        SignalName)

    hSignif_250.Reset()
    hSignif_500.Reset()
    hSignif_800.Reset()
    hSignif_1000.Reset()

    hBkg = p.histoMgr.getHisto(SignalName).getRootHisto().Clone("Bkg")
    hBkg.Reset()

    # Bkg: FakeB + Genuine B
    hBkg.Add(hFakeB.getRootHisto(), +1)
    hBkg.Add(hGenuineB.getRootHisto(), +1)

    nBins = hSignif_250.GetNbinsX() + 1

    # For-loop: All histo bins
    for i in range(1, nBins + 1):

        sigmaB = ROOT.Double(0)

        b = hBkg.IntegralAndError(i, nBins, sigmaB)

        s_250 = hSignal_250.Integral(i, nBins)
        s_500 = hSignal_500.Integral(i, nBins)
        s_800 = hSignal_800.Integral(i, nBins)
        s_1000 = hSignal_1000.Integral(i, nBins)

        # Calculate significance
        signif_250 = stat.significance(s_250, b, sigmaB,
                                       option="Simple")  #Asimov")
        signif_500 = stat.significance(s_500, b, sigmaB,
                                       option="Simple")  #Asimov")
        signif_800 = stat.significance(s_800, b, sigmaB,
                                       option="Simple")  #Asimov")
        signif_1000 = stat.significance(s_1000, b, sigmaB,
                                        option="Simple")  #"Asimov")

        # Set signif for this bin
        hSignif_250.SetBinContent(i, signif_250)
        hSignif_500.SetBinContent(i, signif_500)
        hSignif_800.SetBinContent(i, signif_800)
        hSignif_1000.SetBinContent(i, signif_1000)

        # Apply style
        s_250 = styles.getSignalStyleHToTB_M("200")
        s_250.apply(hSignif_250)

        s_500 = styles.getSignalStyleHToTB_M("500")
        s_500.apply(hSignif_500)

        s_800 = styles.getSignalStyleHToTB_M("800")
        s_800.apply(hSignif_800)

        s_1000 = styles.getSignalStyleHToTB_M("1000")
        s_1000.apply(hSignif_1000)

        hList = []
        hList.append(hSignif_250)
        hList.append(hSignif_500)
        hList.append(hSignif_800)
        hList.append(hSignif_1000)

        hSignif_250.SetName("H^{+} m_{H^{+}} = 250 GeV")
        hSignif_500.SetName("H^{+} m_{H^{+}} = 500 GeV")
        hSignif_800.SetName("H^{+} m_{H^{+}} = 800 GeV")
        hSignif_1000.SetName("H^{+} m_{H^{+}} = 1000 GeV")

    pS = plots.PlotBase(hList, saveFormats=["png", "pdf"])
    pS.setLuminosity(opts.intLumi)

    # Drawing style
    pS.histoMgr.setHistoDrawStyleAll("HIST")
    pS.histoMgr.setHistoLegendStyleAll("L")
    pS.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMarkerSize(1.0))
    pS.histoMgr.forEachHisto(
        lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))
    pS.histoMgr.forEachHisto(
        lambda h: h.getRootHisto().SetMarkerStyle(ROOT.kFullCircle))

    # Draw the plot
    name = "QGLR_Signif" + "GE"

    plots.drawPlot(pS, name, **kwargs)
    SavePlot(pS,
             name,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png", ".pdf"])

    #=========================================================================================
    # Calculate the Transfer Factor (TF) and save to file
    #=========================================================================================
    Verbose("Write the normalisation factors to a python file", True)
    fileName = os.path.join(
        opts.mcrab, "FakeBTransferFactors%s.py" % (getModuleInfoString(opts)))
    manager.writeNormFactorFile(fileName, opts)
    return
Пример #20
0
def main(opts):

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

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

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

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

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

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

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

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

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

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

        # Print dataset information
        datasetsMgr.PrintInfo()

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

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

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

    # 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
Пример #22
0
def main(opts):

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

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

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

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

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

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

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

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

        if opts.verbose:
            datasetsMgr.PrintInfo()

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

        # Print dataset information
        datasetsMgr.PrintInfo()

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

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

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

    Print(
        "All plots saved under directory %s" %
        (ShellStyles.NoteStyle() + aux.convertToURL(opts.saveDir, opts.url) +
         ShellStyles.NormalStyle()), True)
    return
Пример #23
0
def doPlots(i, opts):

    Print(
        "Processing %s%s%s algorithm" % (
            ShellStyles.NoteStyle(),
            opts.algorithm,
            ShellStyles.NormalStyle(),
        ), True)
    # Settings
    if opts.h2tb:
        analysis = "H^{+}#rightarrow tb fully hadronic"
    else:
        analysis = "H^{+}#rightarrow#tau_{h}#nu fully hadronic"

    # Hadd ROOT files
    #rootFiles = glob.glob1(filePath, "higgsCombinetoys*.root")
    rootFiles = glob.glob1(opts.inputDir, "higgsCombinetoys*.root")

    Verbose("Attempting to merge all toy ROOT files", True)
    if len(rootFiles) > 0:
        if os.path.isfile(opts.outputfile):
            os.remove(opts.outputfile)
        Print(
            "Merging \"%s\" ROOT files into \"%s\"" %
            (opts.inputfile, opts.inputfile), True)
        call("hadd %s %s" % (opts.outputfile, opts.inputfile), shell=True)
    else:
        msg = "%sFound %d toy ROOT files to merge%s" % (
            ShellStyles.NoteStyle(), len(rootFiles), ShellStyles.NormalStyle())
        Print(msg, True)
        sys.exit()

    # Clean auxiliary jobs files?
    CleanFiles(opts)

    # Perform GoF calculations
    if not os.path.isfile(opts.outputfile):
        raise Exception("The output ROOT file \"%s\" does not exist!" %
                        (opts.outputfile))
    else:
        Print(
            "Opening merged ROOT file \"%s\" to read results" %
            (opts.outputfile), False)
    fToys = ROOT.TFile(opts.outputfile)
    fData = ROOT.TFile(opts.outputfile)
    tToys = fToys.Get("limit")
    tData = fData.Get("limit")
    nToys = tToys.GetEntries()

    if 1:  #opts.verbose:
        aux.PrintTH1Info(tData)

    if opts.verbose:
        tData.Print()

    # Store the goodness-of-fit value observed in data
    Verbose(
        "NData = %.1f, NToys = %.1f" %
        (tData.GetEntries(), tToys.GetEntries()), False)
    tData.GetEntry(0)
    GoF_DATA = tData.limit
    Verbose(GoF_DATA, True)

    # Setting (Toys)
    GoF_TOYS_TOT = 0
    pval_cum = 0
    toys = []
    minToy = +99999999
    maxToy = -99999999

    # For-loop: All toys
    for i in range(0, tToys.GetEntries()):
        tToys.GetEntry(i)

        # Toys counter
        GoF_TOYS_TOT += tToys.limit
        toys.append(tToys.limit)

        # Accumulate p-Value if GoF_toy > GoF_data
        if tToys.limit > GoF_DATA:
            Verbose(
                "GoF (toy) = %.3f, GoF (data) = %.3f, p-Value += %d (%d)" %
                (tToys.limit, GoF_DATA, tToys.limit, pval_cum), i == 1)
            pval_cum += tToys.limit

    # Finalise p-value calculation by dividing by number of toys total
    pval = pval_cum / GoF_TOYS_TOT
    Print("p-Value = %d/%d = %.3f" % (pval_cum, GoF_TOYS_TOT, pval), False)

    # Create GoF histo & fill it
    hist = ROOT.TH1D("GoF", "", 50, round(min(toys)), round(max(toys)))
    # For-loop: Toys
    for k in toys:
        hist.Fill(k)

    # Customise canvas & histogram
    c = ROOT.TCanvas("canvas", "canvas")
    hist.GetYaxis().SetTitle("Entries")
    hist.GetXaxis().SetTitle("#chi^{2}_{%s}" % (opts.algorithm))
    hist.SetLineColor(ROOT.kRed)
    hist.SetLineWidth(3)
    hist.Draw()

    # Customise arrow indicating data-observed
    arr = ROOT.TArrow(GoF_DATA, 0.0001, GoF_DATA,
                      hist.GetMaximum() / 8, 0.02, "<|")
    arr.SetLineColor(ROOT.kBlue)
    arr.SetFillColor(ROOT.kBlue)
    arr.SetFillStyle(1001)
    arr.SetLineWidth(3)
    arr.SetLineStyle(1)
    arr.SetAngle(60)
    arr.Draw("<|same")

    # Add data observed value
    left = ROOT.TLatex()
    #left.SetNDC()
    left.SetTextFont(43)
    left.SetTextSize(22)
    left.SetTextAlign(11)
    #left.DrawLatex(GoF_DATA*0.9, (hist.GetMaximum()/8.0)*1.05, "#color[4]{data_{obs}}")
    left.DrawLatex(GoF_DATA * 0.9, (hist.GetMaximum() / 8.0) * 1.05,
                   "#color[4]{data}")

    # Analysis text
    anaText = ROOT.TLatex()
    anaText.SetNDC()
    anaText.SetTextFont(43)
    anaText.SetTextSize(22)
    anaText.SetTextAlign(31)
    anaText.DrawLatex(0.92, 0.86, analysis)

    # p-value
    pvalText = ROOT.TLatex()
    pvalText.SetNDC()
    pvalText.SetTextFont(43)
    pvalText.SetTextSize(22)
    pvalText.SetTextAlign(31)  #11
    pvalText.DrawLatex(0.92, 0.80, "# toys: %d" % nToys)
    pvalText.DrawLatex(0.92, 0.74, "p-value: %.2f" % pval)
    pvalText.DrawLatex(0.92, 0.68, "H^{+} m_{H^{+}}= %s GeV" % opts.mass)
    # pvalText.DrawLatex(0.92, 0.74, "  #alpha=#int^{#infty}_{#chi^{2}_{obs}} f(#chi^{2}) d#chi^{2}=%.2f" % pval)
    # pvalText.DrawLatex(0.92, 0.58, "1-#alpha=#int^{#chi^{2}_{obs}}_{0} f(#chi^{2}) d#chi^{2}=%.2f" % (1.0-pval))

    # Print some info
    Verbose("Toys = %.0f" % (nToys), True)
    Verbose("GoF_TOYS_TOT = %.0f" % (GoF_TOYS_TOT), True)
    Verbose("p-value = %.2f" % (pval), False)

    # Add default texts
    histograms.addStandardTexts(lumi=opts.lumi,
                                sqrts="13 TeV",
                                addCmsText=True,
                                cmsTextPosition=None,
                                cmsExtraTextPosition=None,
                                cmsText="CMS",
                                cmsExtraText="Internal   ")
    #histograms.addStandardTexts(lumi=opts.lumi, sqrts="13 TeV", addCmsText=True, cmsTextPosition=None, cmsExtraTextPosition=None, cmsText="CMS", cmsExtraText="Preliminary")

    # Save the plot (not needed - drawPlot saves the canvas already)
    saveName = "GoF_m%s_%s" % (opts.mass, opts.algorithm)
    SavePlot(c, saveName, opts.saveDir, saveFormats=[".C", ".png", ".pdf"])

    return
Пример #24
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
def importNormFactors(era, searchMode, optimizationMode, multicrabDirName):
    '''
    Imports the auto-generates  FakeBTranserFactors.py file, which is 
    created by the plotting/fitting templates script  (plotQCD_Fit.py)
    
    This containsthe results  of fitting to the Baseline Data the templates m_{jjb} 
    shapes from the QCD (Inverted Data) and EWK (Baseline MC).
 
    Results include the fit details for each shape and the QCD NormFactor for moving 
    from the ControlRegion (CR) to the Signal Region (SR).
    
    The aforementioned python file and a folder with the histogram ROOT files and the individual
    fits. The foler name will be normalisationPlots/<OptsMode> and will be placed inside the
    <pseudomulticrab_dir>. The autogenerated file file be place in the cwd (i.e. work/)
    '''
    # Find candidates for normalisation scripts
    scriptList = getNormFactorFileList(dirName=multicrabDirName,
                                       fileBaseName=opts.normFactorsSrc)

    # Create a string with the module information used
    moduleInfoString = getModuleInfoString(era, searchMode, optimizationMode)

    # Construct source file name
    src = getTransferFactorsSrcFilename(multicrabDirName,
                                        opts.normFactorsSrc % moduleInfoString)

    # Check if normalization coefficients are suitable for the choses era
    Verbose("Reading normalisation factors from:\n\t%s" % src, True)

    # Split the path to get just the file name of src
    pathList = src.replace(".py", "").split("/")

    # Insert the directory where the normFactor files reside into the path so that they are found
    if len(pathList) > 1:
        cwd = os.getenv("PWD")
        # Get directories to src in a list [i.e. remove the last entry (file-name) from the pathList]
        dirList = map(str, pathList[:(len(pathList) - 1)])
        srcDir = "/".join(dirList)
        sys.path.insert(0, os.path.join(cwd, srcDir))

    # Import the (normFactor) src file
    Verbose(
        "Importing the transfer factors from src file %s" %
        (ShellStyles.NoteStyle() + src + ShellStyles.NormalStyle()), True)
    srcBase = os.path.basename("/".join(pathList))
    normFactorsImport = __import__(srcBase)

    # Get the function definition and check if the eta, searchMode, and optimizationMode are correct
    Verbose(
        "Check that the era=%s, searchMode=%s, optimizationMode=%s info matches!"
        % (era, searchMode, optimizationMode))
    myNormFactorsSafetyCheck = getattr(normFactorsImport,
                                       "FakeBNormalisationSafetyCheck")
    myNormFactorsSafetyCheck(era, searchMode, optimizationMode)

    # Obtain the normalization (tranfer factors) and their systematic variations
    myNormFactorsImport = getattr(normFactorsImport,
                                  "FakeBNormalisation_Value")
    myNormFactorsImportError = getattr(normFactorsImport,
                                       "FakeBNormalisation_Error")
    myNormFactorsImportSystVarUp = getattr(normFactorsImport,
                                           "FakeBNormalisation_ErrorUp")
    myNormFactorsImportSystVar2xUp = getattr(normFactorsImport,
                                             "FakeBNormalisation_ErrorUp2x")
    myNormFactorsImportSystVar3xUp = getattr(normFactorsImport,
                                             "FakeBNormalisation_ErrorUp3x")
    myNormFactorsImportSystVarDown = getattr(normFactorsImport,
                                             "FakeBNormalisation_ErrorDown")
    myNormFactorsImportSystVar2xDown = getattr(
        normFactorsImport, "FakeBNormalisation_ErrorDown2x")
    myNormFactorsImportSystVar3xDown = getattr(
        normFactorsImport, "FakeBNormalisation_ErrorDown3x")
    msg = "Obtained transfer factors (TFs) from file %s. The values are:" % (
        ShellStyles.NoteStyle() + srcBase + ShellStyles.NormalStyle())
    Verbose(msg, True)

    # Import the normalisation factors and inform user
    myNormFactors = {}
    myNormFactors["Nominal"] = myNormFactorsImport
    myNormFactors["Error"] = myNormFactorsImportError
    myNormFactors["SystVarUp"] = myNormFactorsImportSystVarUp
    myNormFactors["SystVar2xUp"] = myNormFactorsImportSystVar2xUp
    myNormFactors["SystVar3xUp"] = myNormFactorsImportSystVar3xUp
    myNormFactors["SystVarDown"] = myNormFactorsImportSystVarDown
    myNormFactors["SystVar2xDown"] = myNormFactorsImportSystVar2xDown
    myNormFactors["SystVar3xDown"] = myNormFactorsImportSystVar3xDown

    # Print the Normalisation Factors aka Transfer Factors (TF)
    if 0:
        printNormFactors(myNormFactors)
    return myNormFactors
ROOT.gROOT.SetBatch(True)
ROOT.PyConfig.IgnoreCommandLineOptions = True

import HiggsAnalysis.NtupleAnalysis.tools.dataset as dataset
import HiggsAnalysis.NtupleAnalysis.tools.plots as plots
import HiggsAnalysis.NtupleAnalysis.tools.aux as aux
import HiggsAnalysis.NtupleAnalysis.tools.analysisModuleSelector as analysisModuleSelector
import HiggsAnalysis.NtupleAnalysis.tools.ShellStyles as ShellStyles
import HiggsAnalysis.NtupleAnalysis.tools.pseudoMultiCrabCreator as pseudoMultiCrabCreator

#================================================================================================
# Definitions
#================================================================================================
ss = ShellStyles.SuccessStyle()
ns = ShellStyles.NormalStyle()
ts = ShellStyles.NoteStyle()
hs = ShellStyles.HighlightAltStyle()
es = ShellStyles.ErrorStyle()


#================================================================================================
# Class Definition
#================================================================================================
class NewShape:
    '''
    Container class for information of data and MC at certain point of the selection flow
    '''
    def __init__(self,
                 dsetMgr,
                 newDsetName,
                 dsetsToMerge,
Пример #27
0
import HiggsAnalysis.NtupleAnalysis.tools.histograms as histograms
import HiggsAnalysis.NtupleAnalysis.tools.tdrstyle as tdrstyle
import HiggsAnalysis.NtupleAnalysis.tools.plots as plots
import HiggsAnalysis.NtupleAnalysis.tools.styles as styles
import HiggsAnalysis.Keras_ANN.results as _results
import HiggsAnalysis.NtupleAnalysis.tools.ShellStyles as ShellStyles
import HiggsAnalysis.NtupleAnalysis.tools.aux as aux

#================================================================================================
# Shell Types
#================================================================================================
sh_e = ShellStyles.ErrorStyle()
sh_s = ShellStyles.SuccessStyle()
sh_h = ShellStyles.HighlightStyle()
sh_a = ShellStyles.HighlightAltStyle()
sh_t = ShellStyles.NoteStyle()
sh_n = ShellStyles.NormalStyle()
sh_w = ShellStyles.WarningStyle()


#================================================================================================
# Function definition
#================================================================================================
def Verbose(msg, printHeader=False):
    '''
    Calls Print() only if verbose options is set to true.
    '''
    if not opts.verbose:
        return
    Print(msg, printHeader)
    return
    if opts.mcrab == None:
        Print(
            "Not enough arguments passed to script execution. Printing docstring & EXIT."
        )
        parser.print_help()
        #print __doc__
        sys.exit(1)
    else:
        if not os.path.exists("%s/multicrab.cfg" % opts.mcrab):
            msg = "No pseudo-multicrab directory found at path '%s'! Please check path or specify it with --mcrab!" % (
                opts.mcrab)
            raise Exception(ShellStyles.ErrorLabel() + msg +
                            ShellStyles.NormalStyle())
        else:
            msg = "Using pseudo-multicrab directory %s" % (
                ShellStyles.NoteStyle() + opts.mcrab +
                ShellStyles.NormalStyle())
            Verbose(msg, True)

    # Sanity check: fixme
    if len(opts.shape) == 0:
        msg = "Provide a shape identifier with --shape (e.g.: TrijetMass)!"
        raise Exception(ShellStyles.ErrorLabel() + msg +
                        ShellStyles.NormalStyle())

    if opts.useInclusiveNorm:
        msg = "Will only use " + ShellStyles.NoteStyle(
        ) + " inclusive " + ShellStyles.NormalStyle(
        ) + " weight instead of binning (no splitted histograms)"
        Print(msg, True)
Пример #29
0
#================================================================================================
import subprocess
from subprocess import Popen, PIPE
import os
import sys
import datetime
from optparse import OptionParser

import HiggsAnalysis.NtupleAnalysis.tools.ShellStyles as ShellStyles

#================================================================================================
# Variable definition
#================================================================================================
ss = ShellStyles.SuccessStyle()
ns = ShellStyles.NormalStyle()
ts = ShellStyles.NoteStyle()
hs = ShellStyles.HighlightAltStyle()
es = ShellStyles.ErrorStyle()


#================================================================================================
# Function Definitions
#================================================================================================
def Verbose(msg, printHeader=False):
    if not VERBOSE:
        return
    if printHeader:
        print "=== submitCondor.py:"

    if msg != "":
        print "\t", msg
def main(opts):

    # Object for selecting data eras, search modes, and optimization modes
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector()

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

    # Obtain systematics names
    mySystematicsNamesRaw = dsetMgrCreator.getSystematicVariationSources()
    mySystematicsNames = getSystematicsNames(mySystematicsNamesRaw, opts)

    # Set the primary source
    Verbose(
        "Setting the primary source (label=%s)" %
        (ShellStyles.NoteStyle() + opts.analysisName +
         ShellStyles.NormalStyle()), True)
    myModuleSelector.setPrimarySource(label=opts.analysisName,
                                      dsetMgrCreator=dsetMgrCreator)

    # Select modules
    myModuleSelector.doSelect(opts=None)  #fixme: (opts=opts)?

    # Loop over era/searchMode/optimizationMode combos
    myTotalModules = myModuleSelector.getSelectedCombinationCount() * (
        len(mySystematicsNames) + 1) * len(opts.shape)
    Verbose("Found %s modules in total" % (myTotalModules), True)

    count, nEras, nSearchModes, nOptModes, nSysVars = myModuleSelector.getSelectedCombinationCountIndividually(
    )
    if nSysVars > 0:
        msg = "Running over %d modules (%d eras x %d searchModes x %d optimizationModes x %d systematic variations)" % (
            count, nEras, nSearchModes, nOptModes, nSysVars)
    else:
        msg = "Running over %d modules (%d eras x %d searchModes x %d optimizationModes)" % (
            count, nEras, nSearchModes, nOptModes)
    Verbose(msg, True)

    # Create pseudo-multicrab creator
    msg = "Will create pseudo-dataset %s inside the pseudo-multicrab directory" % (
        ShellStyles.NoteStyle() + opts.analysisName +
        ShellStyles.NormalStyle())
    Verbose(msg, True)
    myOutputCreator = pseudoMultiCrabCreator.PseudoMultiCrabCreator(
        opts.analysisName, opts.mcrab, verbose=opts.verbose)

    # Make time stamp for start time
    myGlobalStartTime = time.time()

    iModule = 0
    # For-loop: All Shapes
    for iShape, shapeType in enumerate(opts.shape, 1):

        msg = "Shape %d/%d:%s %s" % (iShape, len(
            opts.shape), ShellStyles.NormalStyle(), shapeType)
        Verbose(ShellStyles.HighlightAltStyle() + msg, True)

        # Initialize
        myOutputCreator.initialize(
            subTitle=shapeType,
            prefix="")  #fixme: remove shapeType from sub-directory name?

        # Get lists of settings
        erasList = myModuleSelector.getSelectedEras()
        modesList = myModuleSelector.getSelectedSearchModes()
        optList = myModuleSelector.getSelectedOptimizationModes()
        if 0:
            optList.append(
                ""
            )  #append the default opt mode iff more optimization modes exist

        # For-loop: All eras
        for era in erasList:
            # For-loop: All searchModes
            for searchMode in modesList:
                # For-loop: AlloptimizationModes
                for optimizationMode in optList:
                    Verbose(
                        "era = %s, searchMode = %s, optMode = %s" %
                        (era, searchMode, optimizationMode), True)

                    # If an optimization mode is defined in options skip the rest
                    if opts.optMode != None:
                        if optimizationMode != opts.optMode:
                            continue

                    # Obtain normalization factors for given Era, SearchMode, and OptimizationMode!
                    myNormFactors = importNormFactors(era, searchMode,
                                                      optimizationMode,
                                                      opts.mcrab)

                    # Nominal module
                    myModuleInfoString = getModuleInfoString(
                        era, searchMode, optimizationMode)
                    iModule += 1

                    # Inform user of what is being processes
                    if optimizationMode != "":
                        msg = "Module %d/%d: %s_%s_%s_%s" % (
                            iModule, myTotalModules, opts.analysisName,
                            searchMode, era, optimizationMode)
                    else:
                        msg = "Module %d/%d: %s_%s_%s" % (
                            iModule, myTotalModules, opts.analysisName,
                            searchMode, era)
                    Print(
                        ShellStyles.HighlightAltStyle() + msg +
                        ShellStyles.NormalStyle(), iModule == 1)

                    # Keep time
                    myStartTime = time.time()

                    Verbose("Create dataset manager with given settings", True)
                    nominalModule = ModuleBuilder(opts, myOutputCreator,
                                                  opts.verbose)
                    nominalModule.createDsetMgr(opts.mcrab, era, searchMode,
                                                optimizationMode)

                    if (iModule == 1):
                        if opts.verbose:
                            nominalModule.debug()

                    # Build the module
                    doFakeBNormalisationSyst = False
                    nominalModule.buildModule(
                        opts.dataSrc, opts.ewkSrc,
                        myNormFactors[opts.normFactorKey],
                        doFakeBNormalisationSyst, opts.normDataSrc,
                        opts.normEwkSrc)

                    # Do TF variations named "SystVarUp" and "SystVarDown" (i.e. (Get results using TF+Error and TF-Error instead of TF)
                    if len(mySystematicsNames) > 0:
                        Verbose(
                            "Adding FakeB normalization systematics (iff also other systematics  present) ",
                            True)
                        #nominalModule.buildTransferFactorVarSystModule(opts.dataSrc, opts.ewkSrc, myNormFactors["SystVarUp"], myNormFactors["SystVarDown"])
                        nominalModule.buildTransferFactorVarSystModule(
                            opts.dataSrc, opts.ewkSrc, myNormFactors)

                    Verbose("Deleting nominal module", True)
                    nominalModule.delete()

                    Verbose("Printing time estimate", True)
                    printTimeEstimate(myGlobalStartTime, myStartTime, iModule,
                                      myTotalModules)

                    Verbose("Now do the rest of systematics variations", True)
                    for syst in mySystematicsNames:
                        iModule += 1
                        msg = "Module %d/%d: %s/%s" % (
                            iModule, myTotalModules, myModuleInfoString, syst)
                        print
                        Print(
                            ShellStyles.HighlightAltStyle() + msg +
                            ShellStyles.NormalStyle(), False)
                        myStartTime = time.time()
                        systModule = ModuleBuilder(opts, myOutputCreator)
                        # Create dataset manager with given settings
                        systModule.createDsetMgr(opts.mcrab,
                                                 era,
                                                 searchMode,
                                                 optimizationMode,
                                                 systematicVariation=syst)

                        # Build systematics module
                        Verbose(
                            "Building systematics module (opts.normFactorKey = %s)"
                            % (opts.normFactorKey), True)
                        systModule.buildModule(
                            opts.dataSrc, opts.ewkSrc,
                            myNormFactors[opts.normFactorKey], False,
                            opts.normDataSrc, opts.normEwkSrc)
                        printTimeEstimate(myGlobalStartTime, myStartTime,
                                          iModule, myTotalModules)
                        systModule.delete()
        print
        Verbose("Pseudo-multicrab ready for %s" % shapeType, True)

    # Print some timing statistics
    Verbose(
        "Average processing time per module was %.1f seconds" %
        getAvgProcessTimeForOneModule(myGlobalStartTime, myTotalModules), True)
    Print(
        "Total elapsed time was %.1f seconds" %
        getTotalElapsedTime(myGlobalStartTime), True)

    # Create rest of pseudo multicrab directory
    myOutputCreator.finalize(silent=False)

    return