예제 #1
0
    def _createSignificance(self, mass, datacardFiles):
        if self.opts.unblinded:
            fileName = "runCombineSignif_ObsAndExp_m" + mass
        else:
            fileName = "runCombineSignif_Exp_m" + mass

        opts = " -m %s" % mass
        opts += " -n signif_m%s" % mass
        if self.brlimit:
            opts += " --rMin %s --rMax %s" % (lhcAsymptoticRminBrLimit,
                                              lhcFreqRmaxBrLimit)
        else:
            opts += " --rMin %s --rMax %s" % (lhcAsymptoticRminSigmaBr,
                                              lhcFreqRmaxSigmaBr)

        command = ["#!/bin/sh", ""]
        # Combine cards and prepare workspace for physics model, if necessary
        myInputDatacardName = _addCombinePreparationCommands(
            self.brlimit, datacardFiles, mass, command)
        # Add command for running combine
        tmpfile = "signif_%s_data.txt" % mass
        # First expected
        optsExpected = lhcFreqSignificanceExpected + opts
        if self.brlimit:
            optsExpected += " --expectSignal %s" % lhcFreqSignificanceExpectedSignalBrLimit
        else:
            optsExpected += " --expectSignal %s" % lhcFreqSignificanceExpectedSignalSigmaBr

        command.append("combine %s -d %s" %
                       (optsExpected, myInputDatacardName))
        command.append("echo '#### PVALUE AFTER THIS LINE ###'")
        command.append("combine %s --pvalue -d %s" %
                       (optsExpected, myInputDatacardName))
        # then observed
        if self.opts.unblinded:
            command.append("echo '#### OBSERVED AFTER THIS LINE ###'")
            optsObserved = lhcFreqSignificanceObserved + opts
            command.append("combine %s -d %s" %
                           (optsObserved, myInputDatacardName))
            command.append("echo '#### PVALUE AFTER THIS LINE ###'")
            command.append("combine %s --pvalue -d %s" %
                           (optsObserved, myInputDatacardName))

        # command.append("combine %s -d %s >%s 2>&1" % (opts, myInputDatacardName, tmpfile))
        # command.append("echo '#### PVALUE AFTER THIS LINE ###' >>%s 2>&1" % tmpfile)
        # # For some reason with combine trying to append truncates...
        # command.append("combine %s -d %s --pvalue >%s_tmp 2>&1" % (opts, myInputDatacardName, tmpfile))
        # command.append("cat %s_tmp >> %s" % (tmpfile, tmpfile))
        # command.append("rm %s_tmp" % tmpfile)
        # command.append("combineReadSignificance.py -f %s -m %s -o significance.json" % (tmpfile, mass))
        aux.writeScript(os.path.join(self.dirname, fileName),
                        "\n".join(command) + "\n")
        self.significanceScripts[mass] = fileName
예제 #2
0
    def _createMLFit(self, mass, fileName, datacardName, blindedMode):
        if self.opts.nomlfit:
            print "skipping creation of ML fit scripts, to enable run with --mlfit"
            return

        fname = fileName.replace("runCombine", "runCombineMLFit")
        outputdir = "mlfit_m%s" % mass
        opts = "-M MaxLikelihoodFit"
        opts += " -m %s" % mass
        opts += " --out %s" % outputdir
        # From https://github.com/cms-analysis/HiggsAnalysis-HiggsToTauTau/blob/master/scripts/limit.py
        #opts += " --minimizerAlgo minuit"
        opts += " --robustFit=1 --X-rtd FITTER_NEW_CROSSING_ALGO --minimizerAlgoForMinos=Minuit2 --minimizerToleranceForMinos=0.01 --X-rtd FITTER_NEVER_GIVE_UP --X-rtd FITTER_BOUND --minimizerAlgo=Minuit2 --minimizerStrategy=0 --minimizerTolerance=0.001 --cminFallbackAlgo \"Minuit,0:0.001\" --keepFailures"  # following options may not suit for us? --preFitValue=1.
        command = ["#!/bin/sh", ""]
        command.append("mkdir -p %s" % outputdir)
        command.append("combine %s %s" % (opts, datacardName))

        opts = "-a"
        if self.brlimit:
            opts += " -p BR"
        opts2 = opts + " -g mlfit_m%s_pulls.png" % mass
        command.append(
            "python %s/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py %s %s/mlfit.root > %s/diffNuisances.txt"
            % (os.environ["CMSSW_BASE"], opts2, outputdir, outputdir))
        opts += " -A"
        opts2 = opts + " --vtol 1.0 --stol 0.99 --vtol2 2.0 --stol2 0.99"
        command.append(
            "python %s/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py %s %s/mlfit.root > %s/diffNuisances_largest_pulls.txt"
            % (os.environ["CMSSW_BASE"], opts2, outputdir, outputdir))
        opts2 = opts + " --vtol 99. --stol 0.50 --vtol2 99. --stol2 0.90"
        command.append(
            "python %s/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py %s %s/mlfit.root > %s/diffNuisances_largest_constraints.txt"
            % (os.environ["CMSSW_BASE"], opts2, outputdir, outputdir))
        command.append(
            "combineReadMLFit.py -f %s/diffNuisances.txt -c configuration.json -m %s -o mlfit.json"
            % (outputdir, mass))
        opts3 = ""
        if blindedMode:
            opts3 = " --bkgonlyfit"
        else:
            opts3 = " --sbfit"
        #if int(mass) > 173:
        #    opts3 += " --heavy"
        command.append(
            "python %s/src/HiggsAnalysis/NtupleAnalysis/test/brlimit/plotMLFits.py -m %s %s"
            % (os.environ["CMSSW_BASE"], mass, opts3))

        aux.writeScript(os.path.join(self.dirname, fname),
                        "\n".join(command) + "\n")

        self.mlfitScripts[mass] = fname
예제 #3
0
    def _createSignificance(self, mass, datacardFiles):
        if self.opts.unblinded:
            fileName = "runCombineSignif_ObsAndExp_m" + mass
        else:
            fileName = "runCombineSignif_Exp_m" + mass

        opts = " -m %s"%mass
        opts += " -n signif_m%s"%mass
        if self.brlimit:
            opts += " --rMin %s --rMax %s" % (lhcAsymptoticRminBrLimit, lhcFreqRmaxBrLimit)
        else:
            opts += " --rMin %s --rMax %s" % (lhcAsymptoticRminSigmaBr, lhcFreqRmaxSigmaBr)
            
        command = ["#!/bin/sh", ""]
        # Combine cards and prepare workspace for physics model, if necessary
        myInputDatacardName = _addCombinePreparationCommands(self.brlimit, datacardFiles, mass, command)
        # Add command for running combine
        tmpfile = "signif_%s_data.txt" % mass
        # First expected
        optsExpected = lhcFreqSignificanceExpected + opts
        if self.brlimit:
            optsExpected += " --expectSignal %s" % lhcFreqSignificanceExpectedSignalBrLimit
        else:
            optsExpected += " --expectSignal %s" % lhcFreqSignificanceExpectedSignalSigmaBr

        command.append("combine %s -d %s" % (optsExpected, myInputDatacardName))
        command.append("echo '#### PVALUE AFTER THIS LINE ###'")
        command.append("combine %s --pvalue -d %s" % (optsExpected, myInputDatacardName))
        # then observed
        if self.opts.unblinded:
            command.append("echo '#### OBSERVED AFTER THIS LINE ###'")
            optsObserved = lhcFreqSignificanceObserved + opts
            command.append("combine %s -d %s" % (optsObserved, myInputDatacardName))
            command.append("echo '#### PVALUE AFTER THIS LINE ###'")
            command.append("combine %s --pvalue -d %s" % (optsObserved, myInputDatacardName))

        # command.append("combine %s -d %s >%s 2>&1" % (opts, myInputDatacardName, tmpfile))
        # command.append("echo '#### PVALUE AFTER THIS LINE ###' >>%s 2>&1" % tmpfile)
        # # For some reason with combine trying to append truncates...
        # command.append("combine %s -d %s --pvalue >%s_tmp 2>&1" % (opts, myInputDatacardName, tmpfile))
        # command.append("cat %s_tmp >> %s" % (tmpfile, tmpfile))
        # command.append("rm %s_tmp" % tmpfile)
        # command.append("combineReadSignificance.py -f %s -m %s -o significance.json" % (tmpfile, mass))
        aux.writeScript(os.path.join(self.dirname, fileName), "\n".join(command)+"\n")
        self.significanceScripts[mass] = fileName
예제 #4
0
    def _createBlinded(self, mass, datacardFiles):
        fileName = "runCombine_LHCasy_Blinded_m" + mass
        opts = ""
        opts += " " + self.optionsBlinded.getValue(mass)
        opts += " --rMin %s"%self.rMin.getValue(mass)
        opts += " --rMax %s"%self.rMax.getValue(mass)
        opts += " -m %s"%mass
        opts += " -n blinded_m%s"%mass
#        if self.brlimit:
#            opts += " --rAbsAcc 0.00001" # increase accuracy of calculation for br limit
        command = ["#!/bin/sh", ""]
        # Combine cards and prepare workspace for physics model, if necessary
        myInputDatacardName = _addCombinePreparationCommands(self.brlimit, datacardFiles, mass, command)
        # Add command for running combine
        command.append("combine %s -d %s" % (opts, myInputDatacardName))
        aux.writeScript(os.path.join(self.dirname, fileName), "\n".join(command)+"\n")
        self.blindedScripts[mass] = fileName
        self._createMLFit(mass, fileName, myInputDatacardName, blindedMode=True)
예제 #5
0
    def _createMLFit(self, mass, fileName, datacardName, blindedMode):
        if self.opts.nomlfit:
#            print "skipping creation of ML fit scripts, to enable run with --mlfit"
            return
          
        fname = fileName.replace("runCombine", "runCombineMLFit")
        outputdir = "mlfit_m%s" % mass
        opts = "-M MaxLikelihoodFit"
        opts += " -m %s" % mass
        opts += " --out %s" % outputdir
        # From https://github.com/cms-analysis/HiggsAnalysis-HiggsToTauTau/blob/master/scripts/limit.py
        #opts += " --minimizerAlgo minuit"
        opts += " --robustFit=1 --X-rtd FITTER_NEW_CROSSING_ALGO --minimizerAlgoForMinos=Minuit2 --minimizerToleranceForMinos=0.01 --X-rtd FITTER_NEVER_GIVE_UP --X-rtd FITTER_BOUND --minimizerAlgo=Minuit2 --minimizerStrategy=0 --minimizerTolerance=0.001 --cminFallbackAlgo \"Minuit,0:0.001\" --keepFailures" # following options may not suit for us? --preFitValue=1.
        command = ["#!/bin/sh", ""]
        command.append("mkdir -p %s" % outputdir)
        command.append("combine %s %s" % (opts, datacardName))

        opts = "-a"
        if self.brlimit:
            opts += " -p BR"
        opts2 = opts + " -g mlfit_m%s_pulls.png" % mass
        command.append("python %s/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py %s %s/mlfit.root > %s/diffNuisances.txt" % (os.environ["CMSSW_BASE"], opts2, outputdir, outputdir))
        opts += " -A"
        opts2 = opts + " --vtol 1.0 --stol 0.99 --vtol2 2.0 --stol2 0.99" 
        command.append("python %s/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py %s %s/mlfit.root > %s/diffNuisances_largest_pulls.txt" % (os.environ["CMSSW_BASE"], opts2, outputdir, outputdir))
        opts2 = opts + " --vtol 99. --stol 0.50 --vtol2 99. --stol2 0.90" 
        command.append("python %s/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py %s %s/mlfit.root > %s/diffNuisances_largest_constraints.txt" % (os.environ["CMSSW_BASE"], opts2, outputdir, outputdir))
        command.append("combineReadMLFit.py -f %s/diffNuisances.txt -c configuration.json -m %s -o mlfit.json" % (outputdir, mass))
        opts3 = ""
        if blindedMode:
            opts3 = " --bkgonlyfit"
        else:
            opts3 = " --sbfit"
        #if int(mass) > 173:b
        #    opts3 += " --heavy"
        command.append("python %s/src/HiggsAnalysis/NtupleAnalysis/test/brlimit/plotMLFits.py -m %s %s" % (os.environ["CMSSW_BASE"], mass, opts3))
        
        aux.writeScript(os.path.join(self.dirname, fname), "\n".join(command)+"\n")

        self.mlfitScripts[mass] = fname
예제 #6
0
 def _createBlinded(self, mass, datacardFiles):
     fileName = "runCombine_LHCasy_Blinded_m" + mass
     opts = ""
     opts += " " + self.optionsBlinded.getValue(mass)
     opts += " --rMin %s" % self.rMin.getValue(mass)
     opts += " --rMax %s" % self.rMax.getValue(mass)
     opts += " -m %s" % mass
     opts += " -n blinded_m%s" % mass
     #        if self.brlimit:
     #            opts += " --rAbsAcc 0.00001" # increase accuracy of calculation for br limit
     command = ["#!/bin/sh", ""]
     # Combine cards and prepare workspace for physics model, if necessary
     myInputDatacardName = _addCombinePreparationCommands(
         self.brlimit, datacardFiles, mass, command)
     # Add command for running combine
     command.append("combine %s -d %s" % (opts, myInputDatacardName))
     aux.writeScript(os.path.join(self.dirname, fileName),
                     "\n".join(command) + "\n")
     self.blindedScripts[mass] = fileName
     self._createMLFit(mass,
                       fileName,
                       myInputDatacardName,
                       blindedMode=True)
예제 #7
0
    def _createInjection(self, mass, datacardFiles):
        if not self.brlimit:
            raise Exception(
                "Signal injection supported only for brlimit for now")
        if len(datacardFiles) != 1:
            raise Exception(
                "Signal injection supported only for one datacard for now (got %d)"
                % len(datacardFiles))
        if len(self.multicrabConfiguration["rootfiles"]) != 1:
            raise Exception(
                "Signal injection supported only for one root file for now (got %d)"
                % len(self.configuration["rootfiles"]))

        fileName = "runCombine_LHCasy_SignalInjected_m" + mass

        shutil.copy(
            os.path.join(os.environ["CMSSW_BASE"], "bin",
                         os.environ["SCRAM_ARCH"], "combine"), self.dirname)
        shutil.copy(
            os.path.join(os.environ["CMSSW_BASE"], "bin",
                         os.environ["SCRAM_ARCH"], "text2workspace.py"),
            self.dirname)
        shutil.copy(
            os.path.join(os.environ["CMSSW_BASE"], "src", "HiggsAnalysis",
                         "NtupleAnalysis", "scripts",
                         "combineInjectSignalLight.py"), self.dirname)
        tar = tarfile.open(os.path.join(self.dirname, "python.tar.gz"),
                           mode="w:gz",
                           dereference=True)
        tar.add(os.path.join(os.environ["CMSSW_BASE"], "python"),
                arcname="python")
        tar.close()

        datacard = datacardFiles[0]
        rootfile = self.multicrabConfiguration["rootfiles"][0] % mass
        rootfileSignal = self.multicrabConfiguration["rootfiles"][
            0] % self.opts.injectSignalMass

        rfs = ""
        if rootfileSignal != rootfile:
            rfs = rootfileSignal

        command = """
#!/bin/bash

SEED_START=1
NUMBER_OF_ITERATIONS={NTOYS}

if [ $# -ge 1 ]; then
    SEED_START=$(($1 * 10000))
fi

tar zxf python.tar.gz
export PYTHONPATH=$PWD/python:$PYTHONPATH

if [ ! -d original ]; then
    mkdir original
    mv {DATACARD} {ROOTFILE} {ROOTFILESIGNAL_OR_EMPTY} original
fi

function runcombine {{
    ./combineInjectSignalLight.py --inputDatacard original/{DATACARD} --inputRoot original/{ROOTFILE} --inputRootSignal original/{ROOTFILESIGNAL} --outputDatacard {DATACARD} --outputRoot {ROOTFILE} --brtop {BRTOP} --brh {BRHPLUS} -s $1
    ./text2workspace.py ./{DATACARD} -P HiggsAnalysis.CombinedLimit.ChargedHiggs:brChargedHiggs -o workspaceM{MASS}.root
#    combine  -M Asymptotic --picky -v 2 --rAbsAcc 0.00001 --rMin 0 --rMax 1.0 -m {MASS} -n obs_m{MASS} -d workspaceM{MASS}.root
    ./combine {OPTS} --rMin {RMIN} --rMax {RMAX} -m {MASS} -n inj_m{MASS} -d workspaceM{MASS}.root
    mv higgsCombineinj_m{MASS}.Asymptotic.mH{MASS}.root higgsCombineinj_m{MASS}.Asymptotic.mH{MASS}.seed$1.root
}}


for ((I=0; I<$NUMBER_OF_ITERATIONS; I++)); do
    runcombine $(($SEED_START+$I))
done

hadd higgsCombineinj_m{MASS}.Asymptotic.mH{MASS}.root higgsCombineinj_m{MASS}.Asymptotic.mH{MASS}.seed*.root

""".format(
            DATACARD=datacard,
            ROOTFILE=rootfile,
            ROOTFILESIGNAL=rootfileSignal,
            ROOTFILESIGNAL_OR_EMPTY=rfs,
            BRTOP=self.opts.injectSignalBRTop,
            BRHPLUS=self.opts.injectSignalBRHplus,
            NTOYS=self.opts.injectNumberToys,
            MASS=mass,
            OPTS=self.optionsObservedAndExpected.getValue(mass),
            RMIN=self.rMin.getValue(mass),
            RMAX=self.rMax.getValue(mass),
        )

        if "signalInjection" not in self.configuration:
            self.configuration["signalInjection"] = {
                "mass": self.opts.injectSignalMass,
                "brTop": self.opts.injectSignalBRTop,
                "brHplus": self.opts.injectSignalBRHplus
            }
        if not os.path.exists(os.path.join(self.dirname, "limits.json")):
            # Insert luminosity to limits.json already here
            limits = {
                "luminosity":
                commonLimitTools.readLuminosityFromDatacard(
                    self.dirname, datacard)
            }
            f = open(os.path.join(self.dirname, "limits.json"), "w")
            json.dump(limits, f, sort_keys=True, indent=2)
            f.close()

        aux.writeScript(os.path.join(self.dirname, fileName), command)
        self.signalInjectionScripts[mass] = fileName
예제 #8
0
def produceLHCAsymptotic(opts,
                         directory,
                         massPoints,
                         datacardPatterns,
                         rootfilePatterns,
                         clsType=None,
                         postfix="",
                         quietStatus=False):

    cls = clsType
    if clsType == None:
        cls = LHCTypeAsymptotic(opts.brlimit, opts.sigmabrlimit)

    print "Computing limits with %s CLs flavour" % cls.nameHuman()
    #print "Computing limits with Combine version %s" % Combine_tag

    mcc = MultiCrabCombine(opts, directory, massPoints, datacardPatterns,
                           rootfilePatterns, cls)
    mcc.createMultiCrabDir(postfix)
    myStatus = True
    if hasattr(opts, "creategridjobs") and opts.creategridjobs:
        myStatus = False
    if myStatus:
        mcc.copyInputFiles()
    myScripts = mcc.writeScripts()
    if opts.injectSignal:
        mcc.writeCrabCfg("arc", [], ["dummy"])
        mcc.writeMultiCrabCfg(aux.ValuePerMass(opts.injectNumberJobs))
        if opts.multicrabCreate:
            mcc.createMultiCrab()
    elif hasattr(opts, "creategridjobs") and opts.creategridjobs:
        # Works only on CRAB2 and intented for tan beta scans
        # Produce running script for each mass point
        myWorkspaces = []
        for m in massPoints:
            # Create list of input files
            myInputFiles = []
            for item in datacardPatterns:
                if item != None:
                    if "%s" in item:
                        name = item % m
                        if not name in myInputFiles:
                            myInputFiles.append(name)
                    else:
                        if not item in myInputFiles:
                            myInputFiles.append(item)
            # Create input workspace for combine
            print "Merging datacards for m=%s" % m
            combinedCardName = "combinedCardsM%s.txt" % m
            if os.path.exists(combinedCardName):
                os.system("rm %s" % combinedCardName)
            combineCardsCommand = "combineCards.py %s > %s" % (" ".join(
                map(str, myInputFiles)), combinedCardName)
            print combineCardsCommand
            os.system(combineCardsCommand)
            print "Creating combine workspace for m=%s" % m
            workspaceCommand = workspaceOptionsSigmaBrLimit % (
                combinedCardName, m)
            print workspaceCommand
            os.system(workspaceCommand)
            os.system("mv %s %s/." % (workspacePattern % m, mcc.dirname))
            #for i in range(len(myInputFiles)):
            #myInputFiles[i] = "%s/%s"%(mcc.dirname, myInputFiles[i])
            #os.system("rm %s %s"%(" ".join(map(str, myInputFiles)),os.path.join(mcc.dirname, combinedCardName)))
            myWorkspaces.append(workspacePattern % m)
            # Copy combine binary here
            os.system("cp %s/bin/%s/combine %s/." %
                      (os.environ["CMSSW_BASE"], os.environ["SCRAM_ARCH"],
                       mcc.dirname))
        if opts.gridRunAllMassesInOneJob:
            # Create crab task config
            mcc.writeCrabCfg(
                "remoteglidein", {
                    "GRID": [
                        "SE_white_list = T2_FI_HIP", "maxtarballsize = 50",
                        "virtual_organization = cms"
                    ],
                    "USER": [
                        "script_exe = runGridJob",
                        "additional_input_files = %s, combine" %
                        (", ".join(map(str, myWorkspaces)))
                    ]
                }, ["output.tgz"])
            os.system("mv %s %s/." %
                      (" ".join(map(str, myWorkspaces)), mcc.dirname))
            # Create script for running the grid job
            command = ["#!/bin/sh", "", "# Run combine"]
            for m in massPoints:
                f = open(os.path.join(mcc.dirname, myScripts[m]))
                myLines = f.readlines()
                f.close()
                for line in myLines:
                    if line.startswith("combine "):
                        command.append("./%s" % line.replace("\n", "").replace(
                            "combinedCardsM%s.txt" % m, workspacePattern % m))
            command.append("")
            command.append("# Collect output")
            command.append("ls -la")
            command.append("tar cfz output.tgz higgsCombine*.root")
            command.append("")
            command.append("# Do job report does not work")
            command.append(
                "#cmsRun -j $RUNTIME_AREA/crab_fjr_$NJob.xml -p pset.py")
            aux.writeScript(os.path.join(mcc.dirname, "runGridJob"),
                            "\n".join(command) + "\n")
        else:
            for m in massPoints:
                # Create crab task config
                mcc.writeCrabCfg(
                    "remoteglidein", {
                        "GRID": [
                            "SE_white_list = T2_FI_HIP", "maxtarballsize = 50",
                            "virtual_organization = cms"
                        ],
                        "USER": [
                            "script_exe = runGridJobM%s" % m,
                            "additional_input_files = %s, combine" %
                            (workspacePattern % m)
                        ]
                    }, ["output.tgz"])
                os.system("mv %s %s/." % (workspacePattern % m, mcc.dirname))
                # Create script for running the grid job
                command = ["#!/bin/sh", "", "# Run combine"]
                f = open(os.path.join(mcc.dirname, myScripts[m]))
                myLines = f.readlines()
                f.close()
                for line in myLines:
                    if line.startswith("combine "):
                        command.append("./%s" % line.replace("\n", "").replace(
                            "combinedCardsM%s.txt" % m, workspacePattern % m))
                command.append("")
                command.append("# Collect output")
                command.append("tar cfz output.tgz higgsCombine*.root")
                command.append("ls -la")
                command.append("")
                command.append("# Do job report does not work")
                command.append(
                    "#cmsRun -j $RUNTIME_AREA/crab_fjr_$NJob.xml -p pset.py")
                aux.writeScript(os.path.join(mcc.dirname, "runGridJobM%s" % m),
                                "\n".join(command) + "\n")
    else:
        mcc.runCombineForAsymptotic(quietStatus=quietStatus)
        return mcc.getResults()
예제 #9
0
    def _createInjection(self, mass, datacardFiles):
        if not self.brlimit:
            raise Exception("Signal injection supported only for brlimit for now")
        if len(datacardFiles) != 1:
            raise Exception("Signal injection supported only for one datacard for now (got %d)" % len(datacardFiles))
        if len(self.multicrabConfiguration["rootfiles"]) != 1:
            raise Exception("Signal injection supported only for one root file for now (got %d)" % len(self.configuration["rootfiles"]))

        fileName = "runCombine_LHCasy_SignalInjected_m" + mass

        shutil.copy(os.path.join(os.environ["CMSSW_BASE"], "bin", os.environ["SCRAM_ARCH"], "combine"), self.dirname)
        shutil.copy(os.path.join(os.environ["CMSSW_BASE"], "bin", os.environ["SCRAM_ARCH"], "text2workspace.py"), self.dirname)
        shutil.copy(os.path.join(os.environ["CMSSW_BASE"], "src", "HiggsAnalysis", "NtupleAnalysis", "scripts", "combineInjectSignalLight.py"), self.dirname)
        tar = tarfile.open(os.path.join(self.dirname, "python.tar.gz"), mode="w:gz", dereference=True)
        tar.add(os.path.join(os.environ["CMSSW_BASE"], "python"), arcname="python")
        tar.close()

        datacard = datacardFiles[0]
        rootfile = self.multicrabConfiguration["rootfiles"][0] % mass
        rootfileSignal = self.multicrabConfiguration["rootfiles"][0] % self.opts.injectSignalMass

        rfs = ""
        if rootfileSignal != rootfile:
            rfs = rootfileSignal

        command = """
#!/bin/bash

SEED_START=1
NUMBER_OF_ITERATIONS={NTOYS}

if [ $# -ge 1 ]; then
    SEED_START=$(($1 * 10000))
fi

tar zxf python.tar.gz
export PYTHONPATH=$PWD/python:$PYTHONPATH

if [ ! -d original ]; then
    mkdir original
    mv {DATACARD} {ROOTFILE} {ROOTFILESIGNAL_OR_EMPTY} original
fi

function runcombine {{
    ./combineInjectSignalLight.py --inputDatacard original/{DATACARD} --inputRoot original/{ROOTFILE} --inputRootSignal original/{ROOTFILESIGNAL} --outputDatacard {DATACARD} --outputRoot {ROOTFILE} --brtop {BRTOP} --brh {BRHPLUS} -s $1
    ./text2workspace.py ./{DATACARD} -P HiggsAnalysis.CombinedLimit.ChargedHiggs:brChargedHiggs -o workspaceM{MASS}.root
#    combine  -M Asymptotic --picky -v 2 --rAbsAcc 0.00001 --rMin 0 --rMax 1.0 -m {MASS} -n obs_m{MASS} -d workspaceM{MASS}.root
    ./combine {OPTS} --rMin {RMIN} --rMax {RMAX} -m {MASS} -n inj_m{MASS} -d workspaceM{MASS}.root
    mv higgsCombineinj_m{MASS}.Asymptotic.mH{MASS}.root higgsCombineinj_m{MASS}.Asymptotic.mH{MASS}.seed$1.root
}}


for ((I=0; I<$NUMBER_OF_ITERATIONS; I++)); do
    runcombine $(($SEED_START+$I))
done

hadd higgsCombineinj_m{MASS}.Asymptotic.mH{MASS}.root higgsCombineinj_m{MASS}.Asymptotic.mH{MASS}.seed*.root

""".format(
    DATACARD=datacard, ROOTFILE=rootfile, ROOTFILESIGNAL=rootfileSignal, ROOTFILESIGNAL_OR_EMPTY=rfs,
    BRTOP=self.opts.injectSignalBRTop, BRHPLUS=self.opts.injectSignalBRHplus,
    NTOYS=self.opts.injectNumberToys, MASS=mass,
    OPTS=self.optionsObservedAndExpected.getValue(mass),
    RMIN=self.rMin.getValue(mass),
    RMAX=self.rMax.getValue(mass),
)

        if "signalInjection" not in self.configuration:
            self.configuration["signalInjection"] = {
                "mass": self.opts.injectSignalMass,
                "brTop": self.opts.injectSignalBRTop,
                "brHplus": self.opts.injectSignalBRHplus
            }
        if not os.path.exists(os.path.join(self.dirname, "limits.json")):
            # Insert luminosity to limits.json already here
            limits = {"luminosity": commonLimitTools.readLuminosityFromDatacard(self.dirname, datacard)}
            f = open(os.path.join(self.dirname, "limits.json"), "w")
            json.dump(limits, f, sort_keys=True, indent=2)
            f.close()

        aux.writeScript(os.path.join(self.dirname, fileName), command)
        self.signalInjectionScripts[mass] = fileName
예제 #10
0
def produceLHCAsymptotic(opts, directory,
                         massPoints,
                         datacardPatterns,
                         rootfilePatterns,
                         clsType = None,
                         postfix="",
                         quietStatus=False
                         ):

    cls = clsType
    if clsType == None:
        cls = LHCTypeAsymptotic(opts.brlimit, opts.sigmabrlimit)

    print "Computing limits with %s CLs flavour" % cls.nameHuman()
    #print "Computing limits with Combine version %s" % Combine_tag

    mcc = MultiCrabCombine(opts, directory, massPoints, datacardPatterns, rootfilePatterns, cls)
    mcc.createMultiCrabDir(postfix)
    myStatus = True
    if hasattr(opts, "creategridjobs") and opts.creategridjobs:
        myStatus = False
    if myStatus:
        mcc.copyInputFiles()
    myScripts = mcc.writeScripts()
    if opts.injectSignal:
        mcc.writeCrabCfg("arc", [], ["dummy"])
        mcc.writeMultiCrabCfg(aux.ValuePerMass(opts.injectNumberJobs))
        if opts.multicrabCreate:
            mcc.createMultiCrab()
    elif hasattr(opts, "creategridjobs") and opts.creategridjobs:
        # Works only on CRAB2 and intented for tan beta scans
        # Produce running script for each mass point
        myWorkspaces = []
        for m in massPoints:
            # Create list of input files
            myInputFiles = []
            for item in datacardPatterns:
                if item != None:
                    if "%s" in item:
                        name = item%m
                        if not name in myInputFiles:
                            myInputFiles.append(name)
                    else:
                        if not item in myInputFiles:
                            myInputFiles.append(item)
            # Create input workspace for combine
            print "Merging datacards for m=%s"%m
            combinedCardName = "combinedCardsM%s.txt"%m
            if os.path.exists(combinedCardName):
                os.system("rm %s"%combinedCardName)
            combineCardsCommand = "combineCards.py %s > %s"%(" ".join(map(str, myInputFiles)), combinedCardName)
            print combineCardsCommand
            os.system(combineCardsCommand)
            print "Creating combine workspace for m=%s"%m
            workspaceCommand = workspaceOptionsSigmaBrLimit%(combinedCardName,m)
            print workspaceCommand
            os.system(workspaceCommand)
            os.system("mv %s %s/."%(workspacePattern%m, mcc.dirname))
            #for i in range(len(myInputFiles)):
                #myInputFiles[i] = "%s/%s"%(mcc.dirname, myInputFiles[i])
            #os.system("rm %s %s"%(" ".join(map(str, myInputFiles)),os.path.join(mcc.dirname, combinedCardName)))
            myWorkspaces.append(workspacePattern%m)
            # Copy combine binary here
            os.system("cp %s/bin/%s/combine %s/."%(os.environ["CMSSW_BASE"], os.environ["SCRAM_ARCH"], mcc.dirname))
        if opts.gridRunAllMassesInOneJob:
            # Create crab task config
            mcc.writeCrabCfg("remoteglidein", {"GRID": ["SE_white_list = T2_FI_HIP", "maxtarballsize = 50", "virtual_organization = cms"],
                                                "USER": ["script_exe = runGridJob", "additional_input_files = %s, combine"%(", ".join(map(str, myWorkspaces)))]},
                            ["output.tgz"])
            os.system("mv %s %s/."%(" ".join(map(str,myWorkspaces)), mcc.dirname))
            # Create script for running the grid job
            command = ["#!/bin/sh", "", "# Run combine"]
            for m in massPoints:
                f = open(os.path.join(mcc.dirname, myScripts[m]))
                myLines = f.readlines()
                f.close()
                for line in myLines:
                    if line.startswith("combine "):
                        command.append("./%s"%line.replace("\n","").replace("combinedCardsM%s.txt"%m,workspacePattern%m))
            command.append("")
            command.append("# Collect output")
            command.append("ls -la")
            command.append("tar cfz output.tgz higgsCombine*.root")
            command.append("")
            command.append("# Do job report does not work")
            command.append("#cmsRun -j $RUNTIME_AREA/crab_fjr_$NJob.xml -p pset.py")
            aux.writeScript(os.path.join(mcc.dirname, "runGridJob"), "\n".join(command)+"\n")
        else:
            for m in massPoints:
                # Create crab task config
                mcc.writeCrabCfg("remoteglidein", {"GRID": ["SE_white_list = T2_FI_HIP", "maxtarballsize = 50", "virtual_organization = cms"],
                                                    "USER": ["script_exe = runGridJobM%s"%m, "additional_input_files = %s, combine"%(workspacePattern%m)]},
                                ["output.tgz"])
                os.system("mv %s %s/."%(workspacePattern%m, mcc.dirname))
                # Create script for running the grid job
                command = ["#!/bin/sh", "", "# Run combine"]
                f = open(os.path.join(mcc.dirname, myScripts[m]))
                myLines = f.readlines()
                f.close()
                for line in myLines:
                    if line.startswith("combine "):
                        command.append("./%s"%line.replace("\n","").replace("combinedCardsM%s.txt"%m,workspacePattern%m))
                command.append("")
                command.append("# Collect output")
                command.append("tar cfz output.tgz higgsCombine*.root")
                command.append("ls -la")
                command.append("")
                command.append("# Do job report does not work")
                command.append("#cmsRun -j $RUNTIME_AREA/crab_fjr_$NJob.xml -p pset.py")
                aux.writeScript(os.path.join(mcc.dirname, "runGridJobM%s"%m), "\n".join(command)+"\n")
    else:
        mcc.runCombineForAsymptotic(quietStatus=quietStatus)
        return mcc.getResults()