Esempio n. 1
0
 def addToDatacards(myDir, massPoints, dataCardList, rootFileList, dataCardPattern, rootFilePattern):
     m = DatacardReader.getMassPointsForDatacardPattern(myDir, dataCardPattern)
     if len(m) > 0:
         m = DatacardReader.getMassPointsForDatacardPattern(myDir, dataCardPattern, massPoints)
         del massPoints[:]
         massPoints.extend(m)
         dataCardList.append(dataCardPattern)
         rootFileList.append(rootFilePattern)
 def addToDatacards(myDir, massPoints, dataCardList, rootFileList, dataCardPattern, rootFilePattern):
     if rootFilePattern != None and "%s" in dataCardPattern:
         m = DatacardReader.getMassPointsForDatacardPattern(myDir, dataCardPattern)
         if len(m) > 0:
             m = DatacardReader.getMassPointsForDatacardPattern(myDir, dataCardPattern, massPoints)
             del massPoints[:]
             massPoints.extend(m)
             dataCardList.append(dataCardPattern)
             rootFileList.append(rootFilePattern)
     else:
         if os.path.exists(dataCardPattern):
             dataCardList.append(dataCardPattern)
             rootFileList.append(rootFilePattern)
 def addToDatacards(myDir, massPoints, dataCardList, rootFileList,
                    dataCardPattern, rootFilePattern):
     if rootFilePattern != None and "%s" in dataCardPattern:
         m = DatacardReader.getMassPointsForDatacardPattern(
             myDir, dataCardPattern)
         if len(m) > 0:
             m = DatacardReader.getMassPointsForDatacardPattern(
                 myDir, dataCardPattern, massPoints)
             del massPoints[:]
             massPoints.extend(m)
             dataCardList.append(dataCardPattern)
             rootFileList.append(rootFilePattern)
     else:
         if os.path.exists(dataCardPattern):
             dataCardList.append(dataCardPattern)
             rootFileList.append(rootFilePattern)
Esempio n. 4
0
def doPlot(opts,mass,nameList,allShapeNuisances,luminosity,myDatacardPattern,rootFilePattern,signalTable):
    f = ROOT.TFile.Open(rootFilePattern%mass)

    content = f.GetListOfKeys()
    # Suppress the warning message of missing dictionary for some iterator
    backup = ROOT.gErrorIgnoreLevel
    ROOT.gErrorIgnoreLevel = ROOT.kError
    diriter = content.MakeIterator()
    ROOT.gErrorIgnoreLevel = backup

    # Find the datacard and nuisance names
    myCardReader = DatacardReader.DataCardReader(".", mass, myDatacardPattern, rootFilePattern)
    myDatasetNames = myCardReader.getDatasetNames()
    # Find the name stem and the name of the uncertainties
    datasets = []
    shapes = []
    for d in myDatasetNames:
        myLabel = d
        myStatus = not d in nameList
        if d == myDatasetNames[0]:
            myStatus = True
            if not str(mass) in d:
                myLabel = "%sm%d"%(d,mass)
        
        myShapeNuisanceNames = myCardReader.getShapeNuisanceNames(d)
        myFilteredShapeNuisances = []
        for n in myShapeNuisanceNames:
            if not "statBin" in n and not n.endswith("_statUp") and not n.endswith("_statDown"):
                myFilteredShapeNuisances.append(n)
        if myStatus:
            myDataset = DatasetContainer(column=d, label=myLabel, nuisances=myFilteredShapeNuisances, cardReader=myCardReader)
            datasets.append(myDataset)
            nameList.append(d)
        for n in myFilteredShapeNuisances:
            if not n in shapes:
                shapes.append(n)

    rebinList = None
    #rebinList = [0,200,250,300,350,400,450,500,550,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,2100,2200,2300,2400,2500]

    ## Do the actual plots
    for d in datasets:
        #d.debug()
        d.doPlot(opts,shapes,f,mass,luminosity,signalTable,rebinList)
    # Close the file
    f.Close()
Esempio n. 5
0
    def produceScaledCards(self, mHp, tanbeta):
        if self.resultExists(mHp, tanbeta):
            return
        # Obtain branching and sigma from MSSM model database
        self._readFromDatabase(mHp, tanbeta)
        if self._results[constructResultKey(mHp,
                                            tanbeta)]["sigmaTheory"] == None:
            return

        #print "    Scaled '%s/%s' signal in datacards by branching %f (mHp=%s, tanbeta=%.1f)"%(mySignalScaleFactor, mHp, tanbeta)
        # Obtain theoretical uncertinties from MSSM model database
        myDbInputName = _modelPattern % self._mssmModel
        if not os.path.exists(myDbInputName):
            raise Exception("Error: Cannot find file '%s'!" % myDbInputName)
        # Scale datacards
        myResult = self.getResult(mHp, tanbeta)
        for fskey in self._decayModeMatrix.keys():
            print "    . final state %10s:" % fskey
            myOriginalRates = []
            myPrimaryReader = None
            for dmkey in self._decayModeMatrix[fskey].keys():
                myDatacardPattern = self._decayModeMatrix[fskey][dmkey][0]
                myRootFilePattern = self._decayModeMatrix[fskey][dmkey][1]
                if myRootFilePattern != None:
                    mySignalScaleFactor = myResult["%sTheory" % dmkey]
                    # Leave reader for first key open
                    if dmkey == self._decayModeMatrix[fskey].keys()[0]:
                        if not "_Control_" in myDatacardPattern:  # Skip control cards (they have no signal)
                            myPrimaryReader = DatacardReader.DataCardReader(
                                ".",
                                mHp,
                                myDatacardPattern,
                                myRootFilePattern,
                                rootFileDirectory="",
                                readOnly=False)
                            myPrimaryReader.scaleSignal(mySignalScaleFactor)
                            myOriginalRates.append(
                                float(
                                    myPrimaryReader.getRateValue(
                                        myPrimaryReader.getDatasetNames()[0])))
                            #print fskey,dmkey,myOriginalRates[len(myOriginalRates)-1]
                    else:
                        # Scale according to br and add signal to primary (i.e. only one datacard for the decay modes)
                        myReader = DatacardReader.DataCardReader(
                            ".",
                            mHp,
                            myDatacardPattern,
                            myRootFilePattern,
                            rootFileDirectory="",
                            readOnly=False)
                        myReader.scaleSignal(mySignalScaleFactor)
                        myOriginalRates.append(
                            float(
                                myReader.getRateValue(
                                    myReader.getDatasetNames()[0])))
                        #print fskey,dmkey,myOriginalRates[len(myOriginalRates)-1]
                        myPrimaryReader.addSignal(myReader)
                        myReader.close()
                        # Remove datacard from current directory so that it is not used for limit calculation (a copy of them is at originalDatacards directory)
                        if os.path.exists(myDatacardPattern % mHp):
                            os.system("rm %s" % (myDatacardPattern % mHp))
                            os.system("rm %s" % (myRootFilePattern % mHp))
            if myPrimaryReader != None:
                mySignalColumnName = myPrimaryReader.getDatasetNames()[0]
                myUpdatedRate = float(
                    myPrimaryReader.getRateValue(
                        myPrimaryReader.getDatasetNames()[0]))
                myTheorUncertPrefix = "theory_"
                # Add theoretical cross section uncertainties to datacard
                db = BRXSDB.BRXSDatabaseInterface(myDbInputName,
                                                  silentStatus=True)
                myXsecUncert = [0.0, 0.0]
                if float(mHp) > 179:
                    myXsecUncert = [
                        db.xsecUncertOrig("mHp", "tanb", "", mHp, tanbeta,
                                          "-"),
                        db.xsecUncertOrig("mHp", "tanb", "", mHp, tanbeta, "+")
                    ]
                    if self._separateTheoreticalXsectionAndBrUncertainties:
                        myNuisanceName = "%sxsectionHp" % myTheorUncertPrefix
                        myUncertValueString = "%.3f/%.3f" % (
                            1.0 - myXsecUncert[0], 1.0 + myXsecUncert[1])
                        myPrimaryReader.addNuisance(myNuisanceName, "lnN",
                                                    mySignalColumnName,
                                                    myUncertValueString)
                        print "      . H+ xsec uncert: %s" % myUncertValueString
                else:
                    self._separateTheoreticalXsectionAndBrUncertainties = True
                # Add theoretical branching ratio uncertainties to datacard (depends on how many decay modes are combined)
                myDecayModeKeys = self._decayModeMatrix[fskey].keys()
                for i in range(len(myDecayModeKeys)):
                    myDecayModeKeys[i] = "BR_%s" % myDecayModeKeys[i]
                myBrUncert = None
                if float(mHp) < 179:
                    myBrUncert = db.brUncertLight(
                        "mHp",
                        "tanb",
                        myDecayModeKeys,
                        mHp,
                        tanbeta,
                        linearSummation=_linearSummingForTheoryUncertainties,
                        silentStatus=True)
                else:
                    myBrUncert = db.brUncertHeavy(
                        "mHp",
                        "tanb",
                        myDecayModeKeys,
                        mHp,
                        tanbeta,
                        linearSummation=_linearSummingForTheoryUncertainties,
                        silentStatus=True)
                for i in range(len(myDecayModeKeys)):
                    for k in myBrUncert.keys():
                        if myDecayModeKeys[i] in k:
                            # Scale uncertainty according to amount of signal from that decay mode
                            myUncertValue = myBrUncert[k] * myOriginalRates[
                                i] / myUpdatedRate
                            if self._separateTheoreticalXsectionAndBrUncertainties:
                                myNuisanceName = "%s%s" % (myTheorUncertPrefix,
                                                           k)
                                myUncertValueString = "%.3f" % (1.0 +
                                                                myUncertValue)
                                if float(mHp) < 179 and (
                                        "HH" in mySignalColumnName
                                        or "ttHpHp" in mySignalColumnName):
                                    # Add for HH
                                    myUncertValueStringHH = "%.3f" % (
                                        1.0 + myUncertValue * 2.0)
                                    myPrimaryReader.addNuisance(
                                        myNuisanceName, "lnN",
                                        mySignalColumnName,
                                        myUncertValueStringHH)
                                    # Add for HW
                                    myPrimaryReader.addNuisance(
                                        myNuisanceName, "lnN",
                                        myPrimaryReader.getDatasetNames()[1],
                                        myUncertValueString)
                                    print "      . H+ HH Br uncert(%s): %s" % (
                                        k, myUncertValueStringHH)
                                    print "      . H+ HW Br uncert(%s): %s" % (
                                        k, myUncertValueString)
                                else:
                                    myPrimaryReader.addNuisance(
                                        myNuisanceName, "lnN",
                                        mySignalColumnName,
                                        myUncertValueString)
                                    print "      . H+ Br uncert(%s): %s" % (
                                        k, myUncertValueString)
                            else:
                                if _linearSummingForTheoryUncertainties:
                                    myXsecUncert[0] += myUncertValue
                                    myXsecUncert[1] += myUncertValue
                                else:
                                    myXsecUncert[0] = math.sqrt(
                                        myXsecUncert[0]**2 + myUncertValue**2)
                                    myXsecUncert[1] = math.sqrt(
                                        myXsecUncert[1]**2 + myUncertValue**2)
                if not self._separateTheoreticalXsectionAndBrUncertainties:
                    myNuisanceName = "%sxsectionHp_and_Br" % myTheorUncertPrefix
                    myUncertValueString = "%.3f/%.3f" % (1.0 - myXsecUncert[0],
                                                         1.0 + myXsecUncert[1])
                    myPrimaryReader.addNuisance(myNuisanceName, "lnN",
                                                mySignalColumnName,
                                                myUncertValueString)
                    print "      . %s: %s" % (myNuisanceName,
                                              myUncertValueString)
                # Write changes to datacard
                myPrimaryReader.close()
                # Something in memory management leaks - the following helps dramatically to recude the leak
                ROOT.gROOT.CloseFiles()
                ROOT.gROOT.GetListOfCanvases().Delete()
                ROOT.gDirectory.GetList().Delete()
            else:
                print "      . no changes to datacard needed"
Esempio n. 6
0
    # Apply TDR style
    style = tdrstyle.TDRStyle()
    histograms.createLegend.moveDefaults(dx=-0.1, dh=-0.15)
    histograms.uncertaintyMode.set(histograms.Uncertainty.StatOnly)
    styles.ratioLineStyle.append(styles.StyleLine(lineColor=13))
    # Find out the mass points
   
    nameList = []
    allShapeNuisances = []
    signalTable = {}
    myDatacardPattern = ""
    myRootfilePattern = ""
    if opts.cardPattern == None:
        mySettings = limitTools.GeneralSettings(".",[])
        myDatacardPattern = mySettings.getDatacardPattern(limitTools.LimitProcessType.TAUJETS)
        myRootfilePattern = mySettings.getRootfilePattern(limitTools.LimitProcessType.TAUJETS)
    else:
        myDatacardPattern = opts.cardPattern.replace("MMM","M%s").replace("MM","%s")
        myRootfilePattern = opts.rootfilePattern.replace("MMM","M%s").replace("MM","%s")
    massPoints = DatacardReader.getMassPointsForDatacardPattern(".", myDatacardPattern)
    print "The following masses are considered:",massPoints
    for m in massPoints:
        # Obtain luminosity from datacard
        myLuminosity = float(limitTools.readLuminosityFromDatacard(".",myDatacardPattern%m))
        # Do plots
        doPlot(opts,int(m),nameList,allShapeNuisances,myLuminosity,myDatacardPattern,myRootfilePattern,signalTable)
    # Print signal table
    print "Max contracted uncertainty for signal:"
    for k in signalTable.keys():
        print "%s, %.3f--%.3f"%(k, signalTable[k]["min"],signalTable[k]["max"])
Esempio n. 7
0
    dirs = opts.directoryList
    if dirs == None:
        dirs = ["."]

    if opts.recursive:
        allDirs = []
        for d in dirs:
            if os.path.exists(d):
                if d.startswith("datacards_combine_"):
                    allDirs.append(d)
                else:
                    allDirs.extend(findDirList(d))
            else:
                raise Exception(
                    "The directory '%s' does not exist! Check your command line parameters!"
                    % d)
        dirs = allDirs[:]

    nTests = 0
    nMassPoints = 0
    nDirs = 0
    for d in dirs:
        (a, b) = DatacardReader.validateDatacards(d)
        nTests += a
        nMassPoints += b
        nDirs += 1

    print "\nDatacard consistency checks passed"
    print ".. checked %d datacards in %d directories" % (nMassPoints, nDirs)
    print ".. passed %d unit tests" % nTests
def hplusTauNuToTauJets(myDir, doCorrelation, nobtagcorr):
    print "*** H+ -> taunu, tau+jets final state ***"
    datacardPattern = "combine_datacard_hplushadronic_m%s.txt"
    rootFilePattern = "combine_histograms_hplushadronic_m%s.root"
    myMgr = DatacardReader.DataCardDirectoryManager(myDir,
                                                    datacardPattern,
                                                    rootFilePattern,
                                                    readOnly=False)
    myNuisanceReplaces = {}
    myNuisanceReplaces["QCD_metshape"] = "CMS_Hptntj_QCDbkg_metshape"
    myNuisanceReplaces["QCDinvTemplateFit"] = "CMS_Hptntj_QCDbkg_templateFit"
    myNuisanceReplaces["Emb_QCDcontam"] = "CMS_Hptntj_taubkg_QCDcontam"
    myNuisanceReplaces[
        "Emb_hybridCaloMET"] = "CMS_Hptntj_taubkg_HybridCaloMETApprox"
    myNuisanceReplaces["Emb_WtauTomu"] = "CMS_Hptntj_taubkg_WTauToMu"
    myNuisanceReplaces["Emb_reweighting"] = "CMS_Hptntj_taubkg_Reweighting"
    myNuisanceReplaces["probBtag"] = "CMS_Hptntj_fakebkg_probabilisticBTag"
    myNuisanceReplaces["higherOrderCorr"] = "ttbar_higherOrderCorrections"
    myNuisanceReplaces["e_mu_veto"] = "CMS_Hptntj_leptonVeto"
    myNuisanceReplaces["Emb_mu_ID"] = "CMS_id_m"
    myNuisanceReplaces[
        "EWKnontt_faketau_TailFit_par0"] = "CMS_Hptntj_fakebkg_tailFitPar0"
    myNuisanceReplaces[
        "EWKnontt_faketau_TailFit_par1"] = "CMS_Hptntj_fakebkg_tailFitPar1"
    myNuisanceReplaces["QCDinv_TailFit_par0"] = "CMS_Hptntj_QCDbkg_tailFitPar0"
    myNuisanceReplaces["QCDinv_TailFit_par1"] = "CMS_Hptntj_QCDbkg_tailFitPar1"
    myNuisanceReplaces[
        "EWK_Tau_TailFit_par0"] = "CMS_Hptntj_taubkg_tailFitPar0"
    myNuisanceReplaces[
        "EWK_Tau_TailFit_par1"] = "CMS_Hptntj_taubkg_tailFitPar1"
    myNuisanceReplaces["b_tag"] = "CMS_btag_CSVT"
    myNuisanceReplaces["top_pt"] = "CMS_Hptntj_topPtReweighting"
    myMgr.replaceNuisanceNames(myNuisanceReplaces)

    # Replace column names
    myColumnReplaces = {}
    myColumnReplaces["EWKnontt_faketau"] = "CMS_Hptntj_fakebkg"
    myColumnReplaces["EWK_Tau"] = "CMS_Hptntj_taubkg"
    myColumnReplaces["QCDinv"] = "CMS_Hptntj_QCDbkg"
    for m in myMgr._massPoints:
        myColumnReplaces["Hp%s_a" % m] = "CMS_Hptntj_Hptn"
        myColumnReplaces["HW%s_a" % m] = "CMS_ttHpW_signal"
        myColumnReplaces["HH%s_a" % m] = "CMS_ttHpHp_signal"
    myMgr.replaceColumnNames(myColumnReplaces)
    myMgr.replaceNuisanceNames(_CommonNuisanceReplaces)

    # Remove lines
    myMgr.removeNuisance("CMS_fake_eToTauEndcap")
    myMgr.removeNuisance("CMS_id_m")
    myMgr.removeNuisance("CMS_trg_mu_dataEff")
    myMgr.removeNuisance("CMS_trg_taumet_L1ETMMCEff")

    myMgr.removeNuisance("CMS_Hptntj_taubkg_QCDcontam")
    myMgr.removeNuisance("CMS_Hptntj_taubkg_HybridCaloMETApprox")
    myMgr.removeNuisance("CMS_Hptntj_taubkg_WTauToMu")
    myMgr.addNuisance("CMS_Hptntj_taubkg_combined",
                      distribution="lnN",
                      columns=["CMS_Hptntj_taubkg"],
                      value="1.122")

    # Convert not affected items to hyphens
    myMgr.replaceNuisanceValue("CMS_trg_taumet_tauMCEff", "-",
                               "CMS_Hptntj_taubkg")
    myMgr.replaceNuisanceValue("CMS_trg_taumet_L1ETMMCEff", "-",
                               "CMS_Hptntj_taubkg")
    myMgr.replaceNuisanceValue(
        "CMS_trg_mu_dataEff", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_fakebkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue("CMS_fake_eToTauEndcap", "-",
                               "CMS_Hptntj_taubkg")
    myMgr.replaceNuisanceValue("CMS_scale_j", "-", "CMS_Hptntj_taubkg")
    myMgr.replaceNuisanceValue("CMS_res_j", "-", "CMS_Hptntj_taubkg")
    myMgr.replaceNuisanceValue("CMS_scale_met", "-", "CMS_Hptntj_taubkg")
    myMgr.replaceNuisanceValue(
        "CMS_id_m", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_fakebkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue("CMS_Hptntj_leptonVeto", "-",
                               ["CMS_Hptntj_taubkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue(
        "CMS_Hptntj_taubkg_QCDcontam", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_fakebkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue(
        "CMS_Hptntj_taubkg_HybridCaloMETApprox", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_fakebkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue(
        "CMS_Hptntj_taubkg_WTauToMu", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_fakebkg", "CMS_Hptntj_QCDbkg"])
    #myMgr.replaceNuisanceValue("CMS_Hptntj_taubkg_Reweighting", "-", ["CMS_Hptntj_Hptn","CMS_Hptntj_fakebkg","CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue("xsect_tt_8TeV", "-",
                               ["CMS_Hptntj_Hptn", "CMS_Hptntj_taubkg"])
    myMgr.replaceNuisanceValue(
        "xsect_Wjets", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_taubkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue(
        "xsect_singleTop", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_taubkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue(
        "xsect_DYtoll", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_taubkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue(
        "xsect_VV", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_taubkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue("lumi_8TeV", "-", "CMS_Hptntj_taubkg")
    myMgr.replaceNuisanceValue("pileup", "-", "CMS_Hptntj_taubkg")
    myMgr.replaceNuisanceValue(
        "CMS_Hptntj_fakebkg_probabilisticBTag", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_taubkg", "CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue(
        "CMS_Hptntj_QCDbkg_templateFit", "-",
        ["CMS_Hptntj_Hptn", "CMS_Hptntj_taubkg", "CMS_Hptntj_fakebkg"])

    myMgr.removeNuisance("xsect_tt_8TeV")
    myMgr.addNuisance("xsect_tt_8TeV_scale",
                      distribution="lnN",
                      columns=["CMS_Hptntj_Hptn"],
                      value="0.9659/1.0253")
    myMgr.replaceNuisanceValue("xsect_tt_8TeV_scale", "1.010/0.993",
                               ["CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue("xsect_tt_8TeV_scale", "0.971/1.022",
                               ["CMS_Hptntj_fakebkg"])
    myMgr.addNuisance("xsect_tt_8TeV_pdf_alphaS",
                      distribution="lnN",
                      columns=["CMS_Hptntj_Hptn"],
                      value="1.0463")
    myMgr.replaceNuisanceValue("xsect_tt_8TeV_pdf_alphaS", "0.984",
                               ["CMS_Hptntj_QCDbkg"])
    myMgr.replaceNuisanceValue("xsect_tt_8TeV_pdf_alphaS", "1.040",
                               ["CMS_Hptntj_fakebkg"])

    #myMgr.addNuisance("ttbar_higherOrderCorrections", distribution="lnN", columns=["CMS_Hptntj_fakebkg"], value="1.026")
    #myMgr.replaceNuisanceValue("ttbar_higherOrderCorrections", "-", ["CMS_Hptntj_Hptn","CMS_Hptntj_taubkg","CMS_Hptntj_QCDbkg"])
    #myMgr.replaceNuisanceValue("ttbar_higherOrderCorrections", "0.992", "CMS_Hptntj_QCDbkg")
    myMgr.removeManyNuisances(_removeUncertainties)
    myMgr.keepManyNuisances(_keepUncertainties)

    myMgr.recreateShapeStatUncert(threshold=0.001)

    myMgr.close()
def hplusTBToSingleLepton(myDir, label):
    print "*** H+ -> tb, single lepton %s category ***" % (label)
    rootFilePattern = None
    datacardPattern = None
    if label.startswith("Control_"):
        rootFilePattern = "HpToSingleLepton_HT_CR_" + label + ".root"
        datacardPattern = "HpToSingleLepton_datacard_" + label + ".txt"
    else:
        rootFilePattern = "HpToSingleLepton_HT_M%s_" + label + ".root"
        datacardPattern = "HpToSingleLepton_datacard_" + label + "_%s.txt"
    rootFileDirectory = ""
    myMgr = DatacardReader.DataCardDirectoryManager(
        myDir,
        datacardPattern,
        rootFilePattern,
        rootFileDirectory=rootFileDirectory,
        readOnly=False,
        outSuffix=label)
    myMgr.replaceNuisanceNames(_CommonNuisanceReplaces)
    #myMgr.addNuisance("xsect_tt_8TeV", distribution="lnN", columns=["ttbar","otherttbar"], value="0.940/1.052")
    #myMgr.addNuisance("xsect_singleTop", distribution="lnN", columns=["st"], value="1.091")
    #myMgr.addNuisance("xsect_DYtoll", distribution="lnN", columns=qaqq["dy"], value="1.040")
    #myMgr.addNuisance("xsect_VV", distribution="lnN", columns=["vv"], value="1.040")
    #if suffix in ["ee","emu"]:
    #    myMgr.addNuisance("xsect_Wjets", distribution="lnN", columns=["wjets"], value="0.963/1.040")
    #myMgr.removeColumn("wjets")
    #myMgr.removeColumn("vv")
    #myMgr.removeColumn("otherttbar")

    # Merge columns
    #myMgr.mergeColumns("OtherTop",["Wlight","Wheavy","ZDMerge","qcd"])
    #myMgr.removeNuisance("wheavy_SF_mu")
    #myMgr.removeNuisance("wheavy_SF_el")

    # Replace column names
    myColumnReplaces = {}
    if myMgr._massPoints != None:
        for m in myMgr._massPoints:
            myColumnReplaces["Hplus%s" % m] = "CMS_Hptbsl_%s" % (label)
    myColumnReplaces["Wlight"] = "CMS_Hptbsl_%s_Wlight" % (label)
    #myColumnReplaces["Wheavy"] = "CMS_Hptbsl_%s_Wheavy"%(label)
    myColumnReplaces["Wc"] = "CMS_Hptbsl_%s_Wc" % (label)
    myColumnReplaces["Wb"] = "CMS_Hptbsl_%s_Wb" % (label)
    myColumnReplaces["qcd"] = "CMS_Hptbsl_%s_qcd" % (label)
    myColumnReplaces["ZDMerge"] = "CMS_Hptbsl_%s_DY_VV" % (label)
    myColumnReplaces["OtherTop"] = "CMS_Hptbsl_%s_OtherTop" % (label)
    myColumnReplaces["OtherEwk"] = "CMS_Hptbsl_%s_OtherEwk" % (label)
    myColumnReplaces["TTbar"] = "CMS_Hptbsl_%s_ttbar" % (label)
    myMgr.replaceColumnNames(myColumnReplaces)

    # Replace nuisance names
    myNuisanceReplaces = {}
    myNuisanceReplaces["trigger_mu"] = "CMS_trg_m"
    myNuisanceReplaces["lepton_mu"] = "CMS_eff_m"
    if label in ["nB1_mu", "nB2p_mu"]:
        myNuisanceReplaces["trigger_el"] = "CMS_trg_m"
    else:
        myNuisanceReplaces["trigger_el"] = "CMS_trg_e"
    myNuisanceReplaces["lepton_el"] = "CMS_eff_e"
    myNuisanceReplaces["xsec_OTop"] = "xsect_singleTop"
    myNuisanceReplaces["xsec_ZJets"] = "xsect_DYtoll"
    myNuisanceReplaces["xsec_QCD"] = "xsect_QCD"
    myNuisanceReplaces["xsec_OEwk"] = "xsect_VV"
    myNuisanceReplaces["xsec_ZDMerge"] = "xsect_ZDMerge"
    myNuisanceReplaces["ttbar_SF_mu"] = "CMS_Hptbsl_muX_ttbarNorm"
    myNuisanceReplaces["wlight_SF_mu"] = "CMS_Hptbsl_muX_wlightNorm"
    #myNuisanceReplaces["wheavy_SF_mu"] = "CMS_Hptbsl_muX_wheavyNorm"
    myNuisanceReplaces["wc_SF_mu"] = "CMS_Hptbsl_muX_wcNorm"
    myNuisanceReplaces["wb_SF_mu"] = "CMS_Hptbsl_muX_wbNorm"
    myNuisanceReplaces["ttbar_SF_el"] = "CMS_Hptbsl_eX_ttbarNorm"
    myNuisanceReplaces["wlight_SF_el"] = "CMS_Hptbsl_eX_wlightNorm"
    #myNuisanceReplaces["wheavy_SF_el"] = "CMS_Hptbsl_eX_wheavyNorm"
    myNuisanceReplaces["wc_SF_el"] = "CMS_Hptbsl_eX_wcNorm"
    myNuisanceReplaces["wb_SF_el"] = "CMS_Hptbsl_eX_wbNorm"
    myNuisanceReplaces["jes"] = "CMS_scale_j"
    myNuisanceReplaces["jer"] = "CMS_res_j"
    #myNuisanceReplaces["pdf"] = "ttbarPDFVariation"
    #myNuisanceReplaces["top_ljets"] = "CMS_Hptbsl_%s_topPtReweighting"%label #FIXME
    #myNuisanceReplaces["top_ljets"] = "CMS_Hptbsl_topPtReweighting"
    myNuisanceReplaces[
        "top_ljets"] = "CMS_Hptbsl_%s_topPtReweighting" % label.replace(
            "nB1_", "").replace("nB2p_", "")
    myNuisanceReplaces["top_ljets_el"] = "CMS_Hptbsl_el_topPtReweighting"
    myNuisanceReplaces["top_ljets_mu"] = "CMS_Hptbsl_mu_topPtReweighting"
    myNuisanceReplaces["btag"] = "CMS_btaguntag_CSVM"
    myNuisanceReplaces["pdf"] = "CMS_ttbar_pdfShape"
    myNuisanceReplaces["matching_ljets"] = "CMS_ttbar_matchingVariation"
    myNuisanceReplaces["scale_ljets"] = "CMS_ttbar_q2"
    if label.startswith("Control_"):
        for l in ["el", "mu"]:
            myNuisanceReplaces["xsec_TTbar__%s" %
                               l] = "CMS_Hptbsl_%s_ttbarNormUncert" % l
            myNuisanceReplaces["xsec_Wlight__%s" %
                               l] = "CMS_Hptbsl_%s_wlightNormUncert" % l
            myNuisanceReplaces["xsec_Wc__%s" %
                               l] = "CMS_Hptbsl_%s_wcNormUncert" % l
            myNuisanceReplaces["xsec_Wb__%s" %
                               l] = "CMS_Hptbsl_%s_wbNormUncert" % l

    myMgr.replaceNuisanceNames(myNuisanceReplaces)

    myMgr.replaceNuisanceValue("xsect_singleTop", "1.069", ["OtherTop"])
    #myMgr.addNuisance("btagshapePDF", distribution="lnN", columns=["ttbar","otherttbar"], value="1.050")

    #myMgr.convertShapeToNormalizationNuisance(["CMS_scale_j","CMS_res_j","CMS_scale_met","pileup"])
    #myMgr.convertShapeToNormalizationNuisance(["CMS_btag_CSVL","CMS_unbtag_CSVL"])
    #myMgr.convertShapeToNormalizationNuisance(["CMS_btag_CSVL","CMS_unbtag_CSVL"],columnList=myNonTTColumns)
    #myMgr.convertShapeToNormalizationNuisance(["CMS_Hptb%s_topPtReweighting"%suffix],columnList=myNonTTColumns)
    #myMgr.convertShapeToNormalizationNuisance(["btag","unbtag","top_pt","b_tag"]) # TMP

    #myMgr.convertShapeToNormalizationNuisance(["CMS_scale_j","CMS_res_j"])

    # Rebin
    if False:
        myCard = myMgr._datacards[myMgr._datacards.keys()[0]]
        hCache = myCard._hCache
        if len(hCache) > 0:
            h = hCache[0]
            s = []
            for i in range(1, h.GetNbinsX() + 1):
                s.append(h.GetXaxis().GetBinLowEdge(i))
            rebinList = [0]
            for i in range(1, len(s)):
                if i % 2 == 1:
                    rebinList.append(s[i])
            rebinList.append(h.GetXaxis().GetXmax())
            #if label == "nB1":
            #rebinList = [0,195,235,275,315,355,395,435,475,515,555,595,635,675,715,755,795,835,875,915,955,995,1095,1195,1305,1425,1545,1690,1800,1910,2500]
            #elif label == "nB2p":
            #rebinList = [0,190,230,270,310,350,390,430,470,510,550,590,630,670,710,750,790,830,870,910,950,990,1090,1190,1290,1390,1490,1590,1670,1830,2500]
            if len(rebinList) > 2:
                print "Rebinning:"
                print "  original: %s" % ", ".join(map(str, s))
                print "  new: %s" % ", ".join(map(str, rebinList))
                myMgr.rebinShapes(rebinList)

    # Smoothen QCD background
    #myMgr.smoothBackgroundByLinearExtrapolation("CMS_HptbmuX_%s_qcd"%label)
    # Redo stat. uncert. shape histograms

    # Insert QCD events to the tail
    if False:
        column = "qcd"
        for m in myMgr._datacards.keys():
            dcard = myMgr._datacards[m]
            datasetIndex = 0
            for c in dcard.getDatasetNames():
                if c == column:
                    hRate = dcard.getRateHisto(c)
                    nbins = hRate.GetNbinsX()
                    hRate.SetBinContent(nbins - 1, 10)
                    hRate.SetBinError(nbins - 1, 10)
                    # Update rate number in table
                    print "sample yield changed %s -> %f" % (
                        dcard._rateValues[datasetIndex], hRate.Integral())
                    dcard._rateValues[datasetIndex] = "%f" % hRate.Integral()
                    # Update stat uncert
                    for h in dcard._hCache:
                        if column in h.GetName() and h.GetName().endswith(
                                "statUp"):
                            h.SetBinContent(
                                nbins - 1,
                                hRate.GetBinContent(nbins - 1) +
                                hRate.GetBinError(nbins - 1))
                            print "stat.uncert. updated"
                        if column in h.GetName() and h.GetName().endswith(
                                "statDown"):
                            h.SetBinContent(
                                nbins - 1,
                                hRate.GetBinContent(nbins - 1) -
                                hRate.GetBinError(nbins - 1))
                            if h.GetBinContent(nbins - 1) < 0:
                                h.SetBinContent(nbins - 1, 0.0)
                datasetIndex += 1

    #myMgr.fixTooSmallStatUncertProblem(signalMinimumAbsStatValue=0.2, bkgMinimumAbsStatValue=_MinimumStatUncertByBkg)

    myMgr.removeManyNuisances(_removeUncertainties)
    myMgr.keepManyNuisances(_keepUncertainties)

    if not label.startswith("Control_"):
        myMgr.recreateShapeStatUncert(
            recreateShapeStatUncertForLargestBkgOnly=True)
        #myMgr.recreateShapeStatUncert(threshold=0.10)
        #myMgr.recreateShapeStatUncert()
    else:
        myMgr.recreateShapeStatUncert()

    myMgr.close()
def hplusTbToDilepton(myDir, doCorrelation, nobtagcorr):
    for suffix in ["ee", "emu", "mumu"]:
        print "*** H+ -> tb, %s final state ***" % suffix
        datacardPattern = "DataCard_" + suffix + "_tb_m%s.txt"
        #rootFilePattern = "CrossSectionShapes_tb_m%s.root"
        #rootFileDirectory = suffix
        #myMgr = DatacardReader.DataCardDirectoryManager(myDir, datacardPattern, rootFilePattern, rootFileDirectory=rootFileDirectory, readOnly=False)
        rootFilePattern = "CrossSectionShapes_tb_m%s_" + suffix + ".root"
        myMgr = DatacardReader.DataCardDirectoryManager(myDir,
                                                        datacardPattern,
                                                        rootFilePattern,
                                                        readOnly=False)

        myMgr.fixTooSmallStatUncertProblem(
            signalMinimumAbsStatValue=0.2,
            bkgMinimumAbsStatValue=_MinimumStatUncertByBkg)

        myMgr.removeNuisance("theoryUncXS_vv")
        myMgr.removeNuisance("theoryUncXS_wjets")
        myMgr.removeNuisance("theoryUncXS_otherttbar")
        myMgr.removeNuisance("theoryUncXS_st")
        myMgr.removeNuisance("theoryUncXS_dy")
        myMgr.removeNuisance("theoryUncXS_ttbar")

        # Replace nuisance names
        myNuisanceReplaces = {}
        myNuisanceReplaces["tes"] = "ES_taus"
        myNuisanceReplaces["topptunc%s" %
                           suffix] = "CMS_Hptb%s_topPtReweighting" % suffix
        myNuisanceReplaces["jes"] = "ES_jets"
        myNuisanceReplaces["jer"] = "JER"
        myNuisanceReplaces["umet"] = "ES_METunclustered"
        myNuisanceReplaces["dy_additional_8TeV"] = "dyAdditional_8TeV"
        myNuisanceReplaces["btag"] = "CMS_btag_CSVL"
        myNuisanceReplaces["unbtag"] = "CMS_unbtag_CSVL"
        myNuisanceReplaces["pdf"] = "CMS_ttbar_pdfShape"
        myNuisanceReplaces["ttbbmeps"] = "CMS_ttbb_matchingVariation"
        myNuisanceReplaces["dilemeps"] = "CMS_ttbar_matchingVariation"
        myNuisanceReplaces["ttbbq2"] = "CMS_ttbb_q2"
        myNuisanceReplaces["dileq2"] = "CMS_ttbar_q2"
        #myNuisanceReplaces["CMS_meps_dile"] = "CMS_ttbar_matchingVariation"
        #myNuisanceReplaces["CMS_q2_dile"] = "CMS_ttbar_q2" #%suffix
        #myNuisanceReplaces["CMS_meps_ttbb"] = "CMS_ttbb_matchingVariation"
        #myNuisanceReplaces["CMS_q2_ttbb"] = "CMS_ttbb_q2" #%suffix
        myNuisanceReplaces["trigeff_ee_8TeV"] = "CMS_trg_ee"
        myNuisanceReplaces["trigeff_emu_8TeV"] = "CMS_trg_em"
        myNuisanceReplaces["trigeff_mumu_8TeV"] = "CMS_trg_mm"
        myMgr.replaceNuisanceNames(myNuisanceReplaces)
        myMgr.replaceNuisanceNames(_CommonNuisanceReplaces)

        if suffix == "ee":
            myMgr.removeNuisance("CMS_eff_m")
        if suffix == "mumu":
            myMgr.removeNuisance("CMS_eff_e")

        myMgr.addNuisance("xsect_tt_8TeV_scale",
                          distribution="lnN",
                          columns=["ttbar", "otherttbar"],
                          value="0.9659/1.0253")
        myMgr.addNuisance("xsect_tt_8TeV_pdf_alphaS",
                          distribution="lnN",
                          columns=["ttbar", "otherttbar"],
                          value="1.0463")
        myMgr.addNuisance("xsect_singleTop",
                          distribution="lnN",
                          columns=["st"],
                          value="1.069")
        myMgr.addNuisance("xsect_DYtoll",
                          distribution="lnN",
                          columns=["dy"],
                          value="1.040")
        #myMgr.addNuisance("xsect_VV", distribution="lnN", columns=["vv"], value="1.040")
        #if suffix in ["ee","emu"]:
        #myMgr.addNuisance("xsect_Wjets", distribution="lnN", columns=["wjets"], value="0.963/1.040")
        #myMgr.removeColumn("wjets")
        #myMgr.removeColumn("vv")
        #myMgr.removeColumn("otherttbar")

        # Replace column names
        myColumnReplaces = {}
        myColumnReplaces["HTB"] = "CMS_Hptb%s_Hp%s" % (suffix, suffix)
        myColumnReplaces["vv"] = "CMS_Hptb%s_vv" % suffix
        myColumnReplaces["wjets"] = "CMS_Hptb%s_wjets" % suffix
        myColumnReplaces["otherttbar"] = "CMS_Hptb%s_otherttbar" % suffix
        myColumnReplaces["st"] = "CMS_Hptb%s_st" % suffix
        myColumnReplaces["dy"] = "CMS_Hptb%s_dy" % suffix
        myColumnReplaces["ttbar"] = "CMS_Hptb%s_ttbar" % suffix
        myColumnReplaces["ttbb"] = "CMS_Hptb%s_ttbb" % suffix
        myMgr.replaceColumnNames(myColumnReplaces)
        myNonTTColumns = [
            "CMS_Hptb%s_Hp%s" % (suffix, suffix),
            "CMS_Hptb%s_vv" % suffix,
            "CMS_Hptb%s_wjets" % suffix,
            "CMS_Hptb%s_otherttbar" % suffix,
            "CMS_Hptb%s_st" % suffix,
            "CMS_Hptb%s_dy" % suffix
        ]

        myMgr.replaceNuisanceValue("ttbarQ2Scale", "-",
                                   "CMS_Hptb%s_Hp%s" % (suffix, suffix))
        myMgr.replaceNuisanceValue("ttbarMatchingVariation", "-",
                                   "CMS_Hptb%s_Hp%s" % (suffix, suffix))
        myMgr.replaceNuisanceValue("ttbarQ2Scale", "1.030",
                                   "CMS_Hptb%s_otherttbar" % (suffix))
        myMgr.replaceNuisanceValue("ttbarMatchingVariation", "1.010",
                                   "CMS_Hptb%s_otherttbar" % (suffix))

        #myMgr.addNuisance("btagshapePDF", distribution="lnN", columns=["ttbar","otherttbar"], value="1.050")

        #myColumnReplaces = {}
        #for c in myMgr.getColumnNames():
        #myColumnReplaces["%s_%s"%(c,c)] = c
        #myMgr.replaceColumnNames(myColumnReplaces)

        myNuisanceReplaces = {}
        myNuisanceReplaces["st%s_%s_st%sat" %
                           (suffix, suffix, suffix)] = "st_%s_stat" % suffix
        myMgr.replaceNuisanceNames(myNuisanceReplaces)

        myMgr.convertShapeToNormalizationNuisance(
            ["CMS_scale_j", "CMS_res_j", "CMS_scale_met", "pileup"])
        #myMgr.convertShapeToNormalizationNuisance(["CMS_btag_CSVL","CMS_unbtag_CSVL"])
        #myMgr.convertShapeToNormalizationNuisance(["CMS_btag_CSVL","CMS_unbtag_CSVL"],columnList=myNonTTColumns)
        #myMgr.convertShapeToNormalizationNuisance(["CMS_Hptb%s_topPtReweighting"%suffix],columnList=myNonTTColumns)
        #myMgr.convertShapeToNormalizationNuisance(["btag","unbtag","top_pt","b_tag"]) # TMP

        myMgr.removeNuisance("othemeps")
        myMgr.removeNuisance("otheq2")
        myMgr.removeNuisance("CMS_eff_dilepton")
        #if suffix != "ee":
        #myMgr.addNuisance("CMS_eff_m", distribution="lnN", columns=["CMS_Hptb%s_Hp%s"%(suffix,suffix),"CMS_Hptb%s_ttbar"%suffix,"CMS_Hptb%s_st"%suffix,"CMS_Hptb%s_ttbb"%suffix,"CMS_Hptb%s_dy"%suffix], value="1.01")
        #if suffix != "mumu":
        #myMgr.addNuisance("CMS_eff_e", distribution="lnN", columns=["CMS_Hptb%s_Hp%s"%(suffix,suffix),"CMS_Hptb%s_ttbar"%suffix,"CMS_Hptb%s_st"%suffix,"CMS_Hptb%s_ttbb"%suffix,"CMS_Hptb%s_dy"%suffix], value="1.02")
        #if suffix == "ee":
        #myMgr.addNuisance("CMS_trg_ee", distribution="lnN", columns=["CMS_Hptb%s_Hp%s"%(suffix,suffix),"CMS_Hptb%s_ttbar"%suffix,"CMS_Hptb%s_st"%suffix,"CMS_Hptb%s_ttbb"%suffix,"CMS_Hptb%s_dy"%suffix], value="1.03")
        #elif suffix == "emu":
        #myMgr.addNuisance("CMS_trg_em", distribution="lnN", columns=["CMS_Hptb%s_Hp%s"%(suffix,suffix),"CMS_Hptb%s_ttbar"%suffix,"CMS_Hptb%s_st"%suffix,"CMS_Hptb%s_ttbb"%suffix,"CMS_Hptb%s_dy"%suffix], value="1.03")
        #elif suffix == "mumu":
        #myMgr.addNuisance("CMS_trg_mm", distribution="lnN", columns=["CMS_Hptb%s_Hp%s"%(suffix,suffix),"CMS_Hptb%s_ttbar"%suffix,"CMS_Hptb%s_st"%suffix,"CMS_Hptb%s_ttbb"%suffix,"CMS_Hptb%s_dy"%suffix], value="1.03")
        #myMgr.replaceNuisanceValue("lumi_8TeV", "1.026")

        # Redo stat. uncert. shape histograms
        myMgr.recreateShapeStatUncert()

        myMgr.removeManyNuisances(_removeUncertainties)
        myMgr.keepManyNuisances(_keepUncertainties)
        myMgr.close()
def hplusTbToTauMu(myDir, doCorrelation, nobtagcorr):
    print "*** H+ -> tb, tau+mu final state ***"
    datacardPattern = "datacard_mutau_tb_m%s_mutau.txt"
    rootFilePattern = "shapes_tb_m%s_btagmultiplicity_j.root"

    myMgr = DatacardReader.DataCardDirectoryManager(myDir,
                                                    datacardPattern,
                                                    rootFilePattern,
                                                    readOnly=False)
    myMgr.removeNuisance("theoryUncXS_vv")
    myMgr.removeNuisance("theoryUncXS_diboson")
    myMgr.removeNuisance("theoryUncXS_wjets")
    myMgr.removeNuisance("theoryUncXS_zll")
    myMgr.removeNuisance("theoryUncXS_ztautau")
    myMgr.removeNuisance("theoryUncXS_otherttbar")
    myMgr.removeNuisance("theoryUncXS_st")
    myMgr.removeNuisance("theoryUncXS_singletop")
    myMgr.removeNuisance("theoryUncXS_dy")
    myMgr.removeNuisance("theoryUncXS_ttbar")

    # Replace column names
    myColumnReplaces = {}
    myColumnReplaces["HTB"] = "CMS_Hptbmt_Hptb"
    myColumnReplaces["tau_fake"] = "CMS_Hptbmt_taufake"
    myColumnReplaces["tt_ltau"] = "CMS_Hptbmt_ttltau"
    myColumnReplaces["tt_ll"] = "CMS_Hptbmt_ttll"
    myColumnReplaces["tt_bb"] = "CMS_Hptbmt_ttbb"
    myColumnReplaces["singleTop"] = "CMS_Hptbmt_singleTop"
    myColumnReplaces["di_boson"] = "CMS_Hptbmt_vv"
    myColumnReplaces["Z_tautau"] = "CMS_Hptbmt_Ztt"
    myColumnReplaces["Z_eemumu"] = "CMS_Hptbmt_Zeemm"
    myColumnReplaces["Z_eemumu"] = "CMS_Hptbmt_Zeemm"
    myMgr.replaceColumnNames(myColumnReplaces)
    myMgr.replaceNuisanceNames(_CommonNuisanceReplaces)

    # Replace nuisance names
    myNuisanceReplaces = {}
    myNuisanceReplaces["tauId"] = "tau_ID_shape"
    myNuisanceReplaces["jetTauMisId"] = "tau_ID_jetToTau_shape"
    #myNuisanceReplaces["fakesSyst"] = "???"
    myNuisanceReplaces["tes"] = "ES_taus"
    myNuisanceReplaces["topptunc"] = "CMS_Hptbmt_topPtReweighting"
    if nobtagcorr:
        myNuisanceReplaces["btagMed"] = "CMS_btag_CSVM"
        myNuisanceReplaces["unbtagMed"] = "CMS_unbtag_CSVM"
    myNuisanceReplaces["Z_tautau_embedded"] = "CMS_Hptbmt_DYtautau_embedding"
    #myNuisanceReplaces["matching"] = "ttbarMatchingVariation"
    myNuisanceReplaces["fakesSyst"] = "CMS_Hptbmt_fakebkg_syst"
    #myNuisanceReplaces["q2scale"] = "ttbarQ2Scale"
    myNuisanceReplaces["leptEff"] = "CMS_eff_m"
    myNuisanceReplaces["pdf"] = "CMS_ttbar_pdfShape"
    myNuisanceReplaces["match"] = "CMS_ttbar_matchingVariation"
    myNuisanceReplaces["scale"] = "CMS_ttbar_q2"  #%suffix
    myNuisanceReplaces["ttbbmeps"] = "CMS_ttbb_matchingVariation"
    myNuisanceReplaces["meps"] = "CMS_ttbar_matchingVariation"
    myNuisanceReplaces["ttbbq2"] = "CMS_ttbb_q2"
    myNuisanceReplaces["q2"] = "CMS_ttbar_q2"

    myMgr.replaceNuisanceNames(myNuisanceReplaces)

    myMgr.removeNuisance("xsect_tt_8TeV_scale")
    myMgr.removeNuisance("xsect_tt_8TeV_pdf_alphaS")
    myMgr.addNuisance("xsect_tt_8TeV_scale",
                      distribution="lnN",
                      columns=["CMS_Hptbmt_ttltau", "CMS_Hptbmt_ttll"],
                      value="0.9659/1.0253")
    myMgr.addNuisance("xsect_tt_8TeV_pdf_alphaS",
                      distribution="lnN",
                      columns=["CMS_Hptbmt_ttltau", "CMS_Hptbmt_ttll"],
                      value="1.0463")
    myMgr.addNuisance("xsect_singleTop",
                      distribution="lnN",
                      columns=["CMS_Hptbmt_singleTop"],
                      value="1.069")

    #myMgr.addNuisance("xsect_tt_8TeV", distribution="lnN", columns=["CMS_Hptbmt_ttltau","CMS_Hptbmt_ttll"], value="0.940/1.052")
    #myMgr.addNuisance("xsect_singleTop", distribution="lnN", columns=["CMS_Hptbmt_singleTop"], value="1.091")
    #myMgr.addNuisance("xsect_DYtoll", distribution="lnN", columns=["CMS_Hptbmt_Ztt","CMS_Hptbmt_Zeemm"], value="1.040")
    #myMgr.addNuisance("xsect_VV", distribution="lnN", columns=["CMS_Hptbmt_vv"], value="1.040")

    #myMgr.replaceNuisanceValue("CMS_eff_t", "1.060", "CMS_Hptbmt_Ztt")
    myMgr.replaceNuisanceValue(
        "CMS_eff_t", "-",
        ["CMS_Hptbmt_taufake", "CMS_Hptbmt_Zeemm", "CMS_Hptbmt_ttll"])
    myMgr.replaceNuisanceValue("CMS_fake_jetToTau", "-", [
        "CMS_Hptbmt_Hptb", "CMS_Hptbmt_taufake", "CMS_Hptbmt_ttltau",
        "CMS_Hptbmt_singleTop", "CMS_Hptbmt_vv", "CMS_Hptbmt_Ztt"
    ])
    myMgr.replaceNuisanceValue("CMS_Hptbmt_fakebkg_syst", "-", [
        "CMS_Hptbmt_Hptb", "CMS_Hptbmt_ttltau", "CMS_Hptbmt_ttll",
        "CMS_Hptbmt_singleTop", "CMS_Hptbmt_vv", "CMS_Hptbmt_Ztt",
        "CMS_Hptbmt_Zeemm"
    ])
    myMgr.replaceNuisanceValue("CMS_eff_m", "-", "CMS_Hptbmt_taufake")
    myMgr.replaceNuisanceValue("lumi_8TeV", "-", "CMS_Hptbmt_taufake")
    myMgr.replaceNuisanceValue("CMS_pileup", "-", "CMS_Hptbmt_taufake")
    myMgr.replaceNuisanceValue("CMS_eff_e", "-", "CMS_Hptbmt_taufake")
    myMgr.replaceNuisanceValue("CMS_trg_m", "-", "CMS_Hptbmt_taufake")
    myMgr.replaceNuisanceValue("fakesSyst", "-", [
        "CMS_Hptbmt_Hptb", "CMS_Hptbmt_ttltau", "CMS_Hptbmt_singleTop",
        "CMS_Hptbmt_ttll"
    ])

    myMgr.convertShapeToNormalizationNuisance(
        ["CMS_scale_j", "CMS_res_j",
         "CMS_scale_met"])  #,"CMS_Hptbmt_topPtReweighting"])
    #myMgr.replaceNuisanceValue("CMS_scale_j", "1.040", ["CMS_Hptbmt_Ztt","CMS_Hptbmt_Zeemm"])
    #myMgr.replaceNuisanceValue("CMS_res_j", "1.010", ["CMS_Hptbmt_Ztt","CMS_Hptbmt_Zeemm"])
    #myMgr.replaceNuisanceValue("CMS_scale_met", "1.020", ["CMS_Hptbmt_Ztt","CMS_Hptbmt_Zeemm"])

    # Redo stat. uncert. shape histograms
    myMgr.fixTooSmallStatUncertProblem(
        signalMinimumAbsStatValue=0.2,
        bkgMinimumAbsStatValue=_MinimumStatUncertByBkg)
    myMgr.recreateShapeStatUncert()

    #myMgr.removeStatUncert()

    myMgr.removeManyNuisances(_removeUncertainties)
    myMgr.keepManyNuisances(_keepUncertainties)
    #myMgr.convertShapeToNormalizationNuisance(["ttbarPDFVariation","CMS_scale_t","CMS_topPtReweighting","CMS_btag_CSVM","CMS_unbtag_CSVM"])
    #myMgr.convertShapeToNormalizationNuisance(["CMS_scale_t"])
    #myMgr.convertShapeToNormalizationNuisance(["CMS_btag_CSVM","CMS_unbtag_CSVM"])
    myMgr.close()
Esempio n. 12
0
    style = tdrstyle.TDRStyle()
    histograms.createLegend.moveDefaults(dx=-0.1, dh=-0.15)
    histograms.uncertaintyMode.set(histograms.Uncertainty.StatOnly)
    styles.ratioLineStyle.append(styles.StyleLine(lineColor=13))
    # Find out the mass points
   
    nameList = []
    allShapeNuisances = []
    signalTable = {}
    myDatacardPattern = ""
    myRootfilePattern = ""
    if opts.cardPattern == None:
        mySettings = limitTools.GeneralSettings(".",[])
        myDatacardPattern = mySettings.getDatacardPattern(limitTools.LimitProcessType.TAUJETS)
        myRootfilePattern = mySettings.getRootfilePattern(limitTools.LimitProcessType.TAUJETS)
    else:
        myDatacardPattern = opts.cardPattern.replace("MMM","M%s").replace("MM","%s")
        myRootfilePattern = opts.rootfilePattern.replace("MMM","M%s").replace("MM","%s")
    massPoints = DatacardReader.getMassPointsForDatacardPattern(".", myDatacardPattern)
    print "The following masses are considered:",massPoints
    for m in massPoints:
        # Obtain luminosity from datacard
        myLuminosity = float(limitTools.readLuminosityFromDatacard(".",myDatacardPattern%m))
        # Do plots
        doPlot(opts,int(m),nameList,allShapeNuisances,myLuminosity,myDatacardPattern,myRootfilePattern,signalTable)
    # Print signal table
    print "Max contracted uncertainty for signal:"
    for k in signalTable.keys():
#        print "Key: "+str(k)
        print "%s, %.3f--%.3f"%(k, signalTable[k]["min"],signalTable[k]["max"])
    dirs = opts.directoryList
    if dirs == None:
        dirs = ["."]

    
    if opts.recursive:
        allDirs = []
        for d in dirs:
            if os.path.exists(d):
                if d.startswith("datacards_combine_"):
                    allDirs.append(d)
                else:
                    allDirs.extend(findDirList(d))
            else:
                raise Exception("The directory '%s' does not exist! Check your command line parameters!"%d)
        dirs = allDirs[:]

    nTests = 0
    nMassPoints = 0
    nDirs = 0
    for d in dirs:
        (a,b) = DatacardReader.validateDatacards(d)
        nTests += a
        nMassPoints += b
        nDirs += 1

    print "\nDatacard consistency checks passed"
    print ".. checked %d datacards in %d directories"%(nMassPoints, nDirs)
    print ".. passed %d unit tests"%nTests
Esempio n. 14
0
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setGridX(opts.gridx)
    style.setGridY(opts.gridy)
    style.setLogX(opts.logx)
    style.setLogY(opts.logy)

    # Create legend and set style
    histograms.createLegend.moveDefaults(dx=-0.1, dh=-0.15)
    histograms.uncertaintyMode.set(histograms.Uncertainty.StatOnly)
    styles.ratioLineStyle.append(styles.StyleLine(lineColor=13))

    # Define some variables
    nameList = []
    allShapeNuisances = []
    signalTable = {}
    myDatacardPattern = ""
    myRootfilePattern = ""

    # Find out the mass points
    if opts.cardPattern == None:
        mySettings = limitTools.GeneralSettings(".", [])
        myDatacardPattern = mySettings.getDatacardPattern(
            limitTools.LimitProcessType.TAUJETS)
        myRootfilePattern = mySettings.getRootfilePattern(
            limitTools.LimitProcessType.TAUJETS)
    else:
        myDatacardPattern = opts.cardPattern.replace("MMM", "M%s").replace(
            "MM", "%s")
        myRootfilePattern = opts.rootfilePattern.replace("MMM", "M%s").replace(
            "MM", "%s")

    # Get mass points to consider
    massPoints = DatacardReader.getMassPointsForDatacardPattern(
        ".", myDatacardPattern)
    Print(
        "The following masses will be considered: %s" %
        (ShellStyles.HighlightAltStyle() + ", ".join(massPoints) +
         ShellStyles.NormalStyle()), True)

    # For-loop: All mass points
    for i, m in enumerate(massPoints, 1):
        # Obtain luminosity from the datacard
        myLuminosity = float(
            limitTools.readLuminosityFromDatacard(".", myDatacardPattern % m))

        # Do the plots
        doPlot(opts, int(m), nameList, allShapeNuisances, myLuminosity,
               myDatacardPattern, myRootfilePattern, signalTable)

    # Print signal table
    Print("Max contracted uncertainty for signal:", True)
    table = []
    align = "{:>15} {:>15} {:>15}"
    hLine = "=" * 50
    table.append(hLine)
    table.append(align.format("Systematic", "Minimum", "Maximum"))
    table.append(hLine)
    # For-loop: All signal
    for i, k in enumerate(signalTable.keys(), 1):
        # Print("Key = %s" % (k), False)
        minVal = "%.3f" % (signalTable[k]["min"])
        maxVal = "%.3f" % (signalTable[k]["max"])
        msg = align.format(k, minVal, maxVal)
        table.append(msg)
    table.append(hLine)
    for row in table:
        Print(row, False)

    msg = "All results under directory %s" % (
        ShellStyles.SuccessStyle() + opts.dirName + ShellStyles.NormalStyle())
    Print(msg, True)

    return
Esempio n. 15
0
def doPlot(opts, mass, nameList, allShapeNuisances, luminosity,
           myDatacardPattern, rootFilePattern, signalTable):
    fName = rootFilePattern % mass
    f = ROOT.TFile.Open(fName)

    content = f.GetListOfKeys()
    # Suppress the warning message of missing dictionary for some iterator
    backup = ROOT.gErrorIgnoreLevel
    ROOT.gErrorIgnoreLevel = ROOT.kError
    diriter = content.MakeIterator()
    ROOT.gErrorIgnoreLevel = backup

    # Find the datacard and nuisance names
    myCardReader = DatacardReader.DataCardReader(".", mass, myDatacardPattern,
                                                 rootFilePattern)
    myDatasetNames = myCardReader.getDatasetNames()
    # Find the name stem and the name of the uncertainties
    datasets = []
    shapes = []
    for d in myDatasetNames:
        myLabel = d
        myStatus = not d in nameList
        if d == myDatasetNames[0]:
            myStatus = True
            if not str(mass) in d:
                myLabel = "%sm%d" % (d, mass)

        myShapeNuisanceNames = myCardReader.getShapeNuisanceNames(d)
        myFilteredShapeNuisances = []
        for n in myShapeNuisanceNames:
            if not "statBin" in n and not n.endswith(
                    "_statUp") and not n.endswith("_statDown"):
                myFilteredShapeNuisances.append(n)
        if myStatus:
            myDataset = DatasetContainer(column=d,
                                         label=myLabel,
                                         nuisances=myFilteredShapeNuisances,
                                         cardReader=myCardReader,
                                         verbose=opts.verbose)
            datasets.append(myDataset)
            nameList.append(d)
        for n in myFilteredShapeNuisances:
            if not n in shapes:
                shapes.append(n)

    rebinList = None
    if opts.h2tb:
        rebinList = systematics._dataDrivenCtrlPlotBinning[
            "LdgTetrajetMass_AfterAllSelections"]

    ## Do the actual plots
    for i, d in enumerate(datasets, 1):
        if opts.verbose:
            d.debug()
        msg = "{:>10}, {:<20}".format("m = %d GeV" % (mass), d.GetName())
        if i < len(datasets):
            Print(
                ShellStyles.HighlightAltStyle() + msg +
                ShellStyles.NormalStyle(), False)
        else:
            Print(ShellStyles.SuccessStyle() + msg + ShellStyles.NormalStyle(),
                  False)

        d.doPlot(opts, shapes, f, mass, luminosity, signalTable, rebinList)
    Verbose("Closing ROOT file %s" % (fName), True)
    f.Close()