Example #1
0
def GenerateHerwigInput(config_params, outputdir, events):
    fname = "{}/herwig.in".format(outputdir)

    with open(fname, "w") as myfile:
        myfile.write("read snippets/PPCollider.in\n")
        myfile.write(
            "set /Herwig/Generators/EventGenerator:EventHandler:LuminosityFunction:Energy {}.0\n"
            .format(GetCMSEnergy(config_params)))
        if config_params["proc"] == "beauty_lo":
            myfile.write(
                "set /Herwig/MatrixElements/MEHeavyQuark:QuarkType 5\n")
            myfile.write(
                "insert /Herwig/MatrixElements/SubProcess:MatrixElements[0] /Herwig/MatrixElements/MEHeavyQuark\n"
            )
        elif config_params["proc"] == "charm_lo":
            myfile.write(
                "set /Herwig/MatrixElements/MEHeavyQuark:QuarkType 4\n")
            myfile.write(
                "insert /Herwig/MatrixElements/SubProcess:MatrixElements[0] /Herwig/MatrixElements/MEHeavyQuark\n"
            )
        elif config_params["proc"] == "dijet_lo":
            myfile.write(
                "insert /Herwig/MatrixElements/SubProcess:MatrixElements[0] /Herwig/MatrixElements/MEQCD2to2\n"
            )
        elif config_params["proc"] == "mb":
            myfile.write("read snippets/MB.in\n")
            myfile.write("read snippets/SoftTune.in\n")
        else:
            print("Process '{}' not implemented for HERWIG!".format(
                config_params["proc"]))
            exit(1)
        # See: https://arxiv.org/abs/0803.0883
        # Chapter B.2
        myfile.write("set /Herwig/Cuts/JetKtCut:MinKT 0.0*GeV\n")
        myfile.write("set /Herwig/Cuts/Cuts:MHatMin 0.0*GeV\n")
        myfile.write(
            "create ThePEG::LHAPDF /Herwig/Partons/PDFSet ThePEGLHAPDF.so\n")
        myfile.write("set /Herwig/Partons/PDFSet:PDFName {}\n".format(
            lhapdf_utils.GetPDFName(config_params["lhans"])))
        myfile.write(
            "set /Herwig/Partons/PDFSet:RemnantHandler /Herwig/Partons/HadronRemnants\n"
        )
        myfile.write("set /Herwig/Particles/p+:PDF /Herwig/Partons/PDFSet\n")
        myfile.write(
            "set /Herwig/Particles/pbar-:PDF /Herwig/Partons/PDFSet\n")
        myfile.write("read snippets/HepMC.in\n")
        myfile.write("set /Herwig/Analysis/HepMC:Filename events.hepmc\n")
        myfile.write(
            "set /Herwig/Analysis/HepMC:PrintEvent {}\n".format(events))
        myfile.write('saverun herwig /Herwig/Generators/EventGenerator\n')
def GenerateHerwigInput(config_params, outputdir, events):
    # See (minimum-bias): http://mcplots.cern.ch/dat/pp/jets/pt/atlas3-akt4/7000/herwig++/2.7.1/default.params
    # See (jet): http://mcplots.cern.ch/dat/pp/jets/pt/cms2011-y0.5/7000/herwig++/2.7.1/default.params
    # See also for minimum-bias: Chapter B.2 https://arxiv.org/abs/0803.0883
    fname = "{}/herwig.in".format(outputdir)
    cms_energy = GetCMSEnergy(config_params)
    with open(fname, "w") as myfile:
        myfile.write("read PPCollider.in\n")
        myfile.write(
            "set /Herwig/Generators/EventGenerator:EventHandler:LuminosityFunction:Energy {}.0\n"
            .format(cms_energy))
        if config_params["proc"] == "beauty_lo":
            myfile.write(
                "set /Herwig/MatrixElements/MEHeavyQuark:QuarkType 5\n")
            myfile.write(
                "insert /Herwig/MatrixElements/SubProcess:MatrixElements[0] /Herwig/MatrixElements/MEHeavyQuark\n"
            )
            myfile.write("set /Herwig/Cuts/JetKtCut:MinKT 0.0*GeV\n")
            myfile.write(
                "set /Herwig/UnderlyingEvent/MPIHandler:IdenticalToUE -1\n")
        elif config_params["proc"] == "charm_lo":
            myfile.write(
                "set /Herwig/MatrixElements/MEHeavyQuark:QuarkType 4\n")
            myfile.write(
                "insert /Herwig/MatrixElements/SubProcess:MatrixElements[0] /Herwig/MatrixElements/MEHeavyQuark\n"
            )
            myfile.write("set /Herwig/Cuts/JetKtCut:MinKT 0.0*GeV\n")
            myfile.write(
                "set /Herwig/UnderlyingEvent/MPIHandler:IdenticalToUE -1\n")
        elif config_params["proc"] == "dijet_lo":
            myfile.write(
                "insert /Herwig/MatrixElements/SubProcess:MatrixElements[0] /Herwig/MatrixElements/MEQCD2to2\n"
            )
            myfile.write("set /Herwig/Cuts/JetKtCut:MinKT 5.0*GeV\n")
            myfile.write(
                "set /Herwig/UnderlyingEvent/MPIHandler:IdenticalToUE -1\n")
        elif "pthard" in config_params["proc"]:
            pthardbins = {1: [10., 20.], 2: [20., 30.]}
            procstr = config_params["proc"]
            pthardbin = int(procstr.replace("pthard_", ""))
            myfile.write(
                "insert /Herwig/MatrixElements/SubProcess:MatrixElements[0] /Herwig/MatrixElements/MEQCD2to2\n"
            )
            myfile.write("set /Herwig/Cuts/JetKtCut:MinKT %f*GeV\n" %
                         (pthardbins[pthard][0]))
            myfile.write("set /Herwig/Cuts/JetKtCut:MaxKT %f*GeV\n" %
                         (pthardbins[pthard][1]))
            myfile.write(
                "set /Herwig/UnderlyingEvent/MPIHandler:IdenticalToUE -1\n")
        elif config_params["proc"] == "mb":
            myfile.write("read MB.in\n")
            myfile.write("set /Herwig/Cuts/JetKtCut:MinKT 0.0*GeV\n")
            myfile.write(
                "set /Herwig/UnderlyingEvent/MPIHandler:IdenticalToUE 0\n")
        else:
            print("Process '{}' not implemented for HERWIG!".format(
                config_params["proc"]))
            exit(1)
        myfile.write(
            "set /Herwig/Cuts/JetKtCut:MaxKT {}.0*GeV\n".format(cms_energy))
        myfile.write(
            "set /Herwig/Cuts/Cuts:MHatMax {}.0*GeV\n".format(cms_energy))
        myfile.write("set /Herwig/Cuts/Cuts:MHatMin 0.0*GeV\n")
        myfile.write("read SoftTune.in\n")

        # PDF selection
        myfile.write(
            "create ThePEG::LHAPDF /Herwig/Partons/PDFSet ThePEGLHAPDF.so\n")
        myfile.write("set /Herwig/Partons/PDFSet:PDFName {}\n".format(
            lhapdf_utils.GetPDFName(config_params["lhans"])))
        myfile.write(
            "set /Herwig/Partons/PDFSet:RemnantHandler /Herwig/Partons/HadronRemnants\n"
        )
        myfile.write("set /Herwig/Particles/p+:PDF /Herwig/Partons/PDFSet\n")
        #myfile.write("set /Herwig/Particles/p:PDF /Herwig/Partons/PDFSet\n")

        if "herwig_config" in config_params and "tune" in config_params[
                "herwig_config"]:
            myfile.write("read {}\n".format(
                config_params["herwig_config"]["tune"]))

        #HEP MC writer
        myfile.write("read snippets/HepMC.in\n")
        myfile.write("set /Herwig/Analysis/HepMC:Filename events.hepmc\n")
        myfile.write(
            "set /Herwig/Analysis/HepMC:PrintEvent {}\n".format(events))
        myfile.write('saverun herwig /Herwig/Generators/EventGenerator\n')
def RunHerwig(nevents, pdfid, load_packages_separately):
    print("Running HERWIG simulation!")

    rnd = random.randint(0, 1073741824)  # 2^30

    with open("herwig.in", 'r') as fin:
        herwig_input = fin.read().splitlines()
    for line in herwig_input:
        print(line)

    if not "HERWIG_ROOT" in os.environ and "HERWIGPATH" in os.environ:
        os.environ["HERWIG_ROOT"] = os.environ["HERWIGPATH"]

    pdfname = lhapdf_utils.GetPDFName(pdfid, False)
    if load_packages_separately:
        herwig_pkg = "VO_ALICE@Herwig::v7.1.2-alice1-3"
        print("Starting a separate shell to load the Herwig package...")
        with open("herwig_stdout.log", "w") as myfile:
            cmd = "which Herwig"
            rc, out, err = alienv_exec(cmd, herwig_pkg)
            myfile.write("Command '{}' exited with return code '{}'\n".format(
                cmd, rc))
            myfile.write(out)
            myfile.write("\n")
            myfile.write(err)
            myfile.write("\n")

            # Verify that PDF is installed
            cmd = "lhapdf list --installed"
            rc, out, err = alienv_exec(cmd, herwig_pkg)
            myfile.write("Command '{}' exited with return code '{}'\n".format(
                cmd, rc))
            myfile.write(out)
            myfile.write("\n")
            myfile.write(err)
            myfile.write("\n")
            if pdfname in out:
                print("PDF '{}' already installed.".format(pdfname))
            else:
                if "LHAPDF_DATA_PATH" in os.environ:
                    os.environ["LHAPDF_DATA_PATH"] = "./:" + os.environ[
                        "LHAPDF_DATA_PATH"]
                else:
                    os.environ["LHAPDF_DATA_PATH"] = "./"
                os.environ["LHAPDF_PDFSETS_ROOT"] = "./"
                cmd = "lhapdf --pdfdir=./ install {}".format(pdfname)
                rc, out, err = alienv_exec(cmd, herwig_pkg)
                myfile.write(
                    "Command '{}' exited with return code '{}'\n".format(
                        cmd, rc))
                myfile.write(out)
                myfile.write("\n")
                myfile.write(err)
                myfile.write("\n")
                out = subprocess.check_output(["ls"])
                myfile.write(out)
                myfile.write("\n")
            cmd = "export LHAPDF_DATA_PATH=./:$LHAPDF_DATA_PATH;export LHAPDF_PDFSETS_ROOT=./;Herwig read --repo=$HERWIG_ROOT/share/Herwig/HerwigDefaults.rpo herwig.in"
            rc, out, err = alienv_exec(cmd, herwig_pkg)
            myfile.write("Command '{}' exited with return code '{}'\n".format(
                cmd, rc))
            myfile.write(out)
            myfile.write("\n")
            myfile.write(err)
            myfile.write("\n")
            out = subprocess.check_output(["ls"])
            myfile.write(out)
            myfile.write("\n")
            cmd = "export LHAPDF_DATA_PATH=./:$LHAPDF_DATA_PATH;export LHAPDF_PDFSETS_ROOT=./;Herwig run herwig.run -s {} -N {}\n".format(
                rnd, nevents)
            rc, out, err = alienv_exec(cmd, herwig_pkg)
            myfile.write("Command '{}' exited with return code '{}'\n".format(
                cmd, rc))
            myfile.write(out)
            myfile.write("\n")
            myfile.write(err)
            myfile.write("\n")
    else:
        if TestHerwig():
            print("Running HERWIG...")
            with open("herwig_stdout.log", "w") as myfile:
                # Verify that PDF is installed
                pdfsetlist = subprocess.check_output(
                    ["lhapdf", "list", "--installed"]).splitlines()
                if pdfname in pdfsetlist:
                    print("PDF '{}' already installed.".format(pdfname))
                else:
                    os.environ["LHAPDF_DATA_PATH"] = "./"
                    subprocess.call(
                        ["lhapdf", "--pdfdir=./", "install", pdfname],
                        stdout=myfile,
                        stderr=myfile)

                subprocess.call(["Herwig", "read", "herwig.in"],
                                stdout=myfile,
                                stderr=myfile)
                if os.path.isfile("herwig.run"):
                    subprocess.call([
                        "Herwig", "run", "herwig.run", "-s",
                        str(rnd), "-N",
                        str(nevents)
                    ],
                                    stdout=myfile,
                                    stderr=myfile)
                else:
                    print(
                        "Something went wrong in the HERWIG run configuration."
                    )
        else:
            print("HERWIG not found. Aborting...")

    hepfile = "events.hepmc"
    if os.path.isfile(hepfile):
        nevents_generated = GetNumberOfHerwigEvents(hepfile)
    else:
        print("Something went wrong in the HERWIG simulation.")
        nevents_generated = 0

    result = HerwigResult(nevents_generated, hepfile, "herwig_stdout.log")

    return result