Beispiel #1
0
    def compute(self,
                sqrts,
                slhafile,
                lhefile=None,
                unlink=True,
                loFromSlha=None,
                pythiacard=None):
        """
        Run pythia and compute SUSY cross sections for the input SLHA file.

        :param sqrts: sqrt{s} to run Pythia, given as a unum (e.g. 7.*TeV)
        :param slhafile: SLHA file
        :param lhefile: LHE file. If None, do not write pythia output to file. If
                        file does not exist, write pythia output to this file name. If
                        file exists, read LO xsecs from this file (does not run pythia).
        :param unlink: Clean up temp directory after running pythia

        :param loFromSlha: If True, uses the LO xsecs from the SLHA file to compute the
                           higher order xsecs
        :param pythiaCard: Optional path to pythia.card. If None, uses /etc/pythia.card

        :returns: XSectionList object

        """
        sqrts = self._checkSqrts(sqrts)
        self._checkSLHA(slhafile)

        if lhefile:
            if os.path.isfile(lhefile):
                logger.warning("Using LO cross sections from " + lhefile)
                logger.error(
                    "Cross section retrieval from lhefile currently not implemented"
                )
                sys.exit()
            else:
                logger.info("Writing pythia LHE output to " + lhefile)
        if loFromSlha:
            logger.info("Using LO cross sections from " + slhafile)
            xsecsInfile = crossSection.getXsecFromSLHAFile(slhafile)
            loXsecs = crossSection.XSectionList()
            for xsec in xsecsInfile:
                if xsec.info.order == 0 and xsec.info.sqrts == sqrts:
                    loXsecs.add(xsec)

        else:
            logger.info("get LO cross sections from pythia%d" %
                        self.pythiaVersion)
            tool = toolBox.ToolBox().get("pythia%d" % self.pythiaVersion)
            tool.nevents = self.nevents
            tool.sqrts = sqrts / TeV
            tool.pythiacard = pythiacard
            loXsecs = tool.run(slhafile, lhefile, unlink=unlink)
        self.loXsecs = loXsecs
        self.loXsecs.sort()
        self.xsecs = self.addHigherOrders(sqrts, slhafile)
        self.xsecs.sort()
        #for xsec in self.loXsecs:
        #    logger.debug ( "now writing out xsecs: %s" % xsec.value )
        logger.debug("how many NLL xsecs? %d" % len(self.xsecs))
        return self.xsecs
Beispiel #2
0
 def queryCrossSections(self, filename):
     if os.path.isdir(filename):
         logger.error("Cannot query cross sections for a directory.")
         sys.exit(-1)
     xsecsInfile = crossSection.getXsecFromSLHAFile(filename)
     if xsecsInfile:
         print("1")
     else:
         print("0")
Beispiel #3
0
 def testSSJokers(self):
     """ test the signal strength multipliers, with jokers """
     
     slhafile="./testFiles/slha/simplyGluino.slha"
     f = open(slhafile,'r')
     fdata = f.read()
     fdata = fdata[:fdata.find('XSECTION')]
     f.close()
     fnew = tempfile.mkstemp()
     os.close(fnew[0])
     tmpfile = fnew[1]
     fnew = open(tmpfile,'w')
     fnew.write(fdata)
     fnew.close()        
     self.logger.info ("test NLL xsecs @ 8 and 13 TeV" )
     #Set overall options:
     #Options for cross section calculation:
     xargs = argparse.Namespace()
     xargs.sqrts = [[8.,13.]]
     xargs.noautocompile = True
     xargs.ncpus = 1
     xargs.nevents = 5000
     #Compute LO xsecs:
     xargs.query = False
     xargs.NLL = False
     xargs.NLO = False
     xargs.LOfromSLHA = False
     xargs.keep = False
     xargs.tofile = True
     xargs.alltofile = False
     xargs.pythia6 = True
     xargs.filename = tmpfile
     xargs.colors = False
     xargs.ssmultipliers = { ('*100002?','*1000021'): 4. }
     # xargs.ssmultipliers = { 1000021: 2. }
     xargs.verbosity = "warning"
     #Compute LO cross sections
     xsecComputer.main(xargs)
     #Compute NLL cross sections
     xargs.NLL = True
     xargs.LOfromSLHA = True
     xsecComputer.main(xargs)
     #Read xsecs:
     xsecsInfile = crossSection.getXsecFromSLHAFile(tmpfile)
     os.remove(tmpfile)
     
     #Check 8 TeV xsecs:
     lo = xsecsInfile.getXsecsFor('8 TeV (LO)')[0].value.asNumber(fb)
     nll = xsecsInfile.getXsecsFor('8 TeV (NLL)')[0].value.asNumber(fb)
     self.assertAlmostEqual(lo/1056.,1.,2)
     self.assertAlmostEqual(nll/2294.,1.,2)
     #Check 13 TeV xsecs:
     lo = xsecsInfile.getXsecsFor('13 TeV (LO)')[0].value.asNumber(fb)
     nll = xsecsInfile.getXsecsFor('13 TeV (NLL)')[0].value.asNumber(fb)
     self.assertAlmostEqual(lo/8910.76,1.,2 )
     self.assertAlmostEqual(nll/17215.5906, 1.,1)
Beispiel #4
0
    def findLonglivedParticles(self, findLonglived):
        """
        Find meta-stable particles that decay to visible particles
        and stable charged particles.
        
        :returns: status flag, message
        
        """
        if not findLonglived:
            return 0, "Did not check for long lived particles"

        # Get list of cross sections:
        xsecList = crossSection.getXsecFromSLHAFile(self.filename)
        # Check if any of particles being produced have visible displaced vertices
        # with a weight > sigmacut
        chargedList = []
        missingList = []
        ltstr = ""
        from smodels.particlesLoader import rEven
        for pid in xsecList.getPIDs():
            if pid in rEven: continue
            if pid == self.findLSP(): continue
            xsecmax = xsecList.getXsecsFor(pid).getMaxXsec()
            if xsecmax < self.sigmacut: continue
            lt = self.getLifetime(pid, ctau=True)
            if lt < 0:
                # error for stable charged particles
                if self.visible(abs(pid)):
                    if not abs(pid) in chargedList:
                        chargedList.append(abs(pid))
                        if not str(abs(pid)) in ltstr: ltstr += "#%s : c*tau = inf\n" % str(abs(pid))
            if lt < self.maxDisplacement: continue
            brvalue = 0.
            daughters = []
            # Sum all BRs which contain at least one visible particle
            for decay in self.slha.decays[abs(pid)].decays:
                for pidb in decay.ids:
                    if self.visible(abs(pidb), decay=True):
                        brvalue += decay.br
                        daughters.append(decay.ids)
                        break
                    elif self.visible(abs(pidb), decay=True) == None:
                        if not abs(pidb) in missingList:
                            missingList.append(abs(pidb))
            if xsecmax * brvalue > self.sigmacut:
                if not abs(pid) in chargedList:
                    chargedList.append(abs(pid))
                    if not str(abs(pid)) in ltstr: ltstr += "#%s : c*tau = %s\n" % (str(abs(pid)), str(lt))
        if not chargedList and not missingList: return 1, "no long lived particles found"
        else:
            msg = ""
            if chargedList:
                msg += "#Visible decays of longlived particles / stable charged particles: %s\n%s" % (str(chargedList), ltstr)
            if missingList:
                msg += "#Missing decay blocks of new r-Even particles appearing in displaced vertices: %s\n" % (str(missingList))
        return -1, msg
Beispiel #5
0
    def findLonglivedParticles(self, findLonglived):
        """
        Find meta-stable particles that decay to visible particles
        and stable charged particles.
        
        :returns: status flag, message
        
        """
        if not findLonglived:
            return 0, "Did not check for long lived particles"

        # Get list of cross sections:
        xsecList = crossSection.getXsecFromSLHAFile(self.filename)
        # Check if any of particles being produced have visible displaced vertices
        # with a weight > sigmacut
        chargedList = []
        missingList = []
        ltstr = ""
        for pid in xsecList.getPIDs():
            if pid in rEven: continue
            if pid == self.findLSP(): continue
            xsecmax = xsecList.getXsecsFor(pid).getMaxXsec()
            if xsecmax < self.sigmacut: continue
            lt = self.getLifetime(pid, ctau=True)
            if lt < 0:
                # error for stable charged particles
                if self.visible(abs(pid)):
                    if not abs(pid) in chargedList:
                        chargedList.append(abs(pid))
                        if not str(abs(pid)) in ltstr: ltstr += "#%s : c*tau = inf\n" % str(abs(pid))
            if lt < self.maxDisplacement: continue
            brvalue = 0.
            daughters = []
            # Sum all BRs which contain at least one visible particle
            for decay in self.slha.decays[abs(pid)].decays:
                for pidb in decay.ids:
                    if self.visible(abs(pidb), decay=True):
                        brvalue += decay.br
                        daughters.append(decay.ids)
                        break
                    elif self.visible(abs(pidb), decay=True) == None:
                        if not abs(pidb) in missingList:
                            missingList.append(abs(pidb))
            if xsecmax * brvalue > self.sigmacut:
                if not abs(pid) in chargedList:
                    chargedList.append(abs(pid))
                    if not str(abs(pid)) in ltstr: ltstr += "#%s : c*tau = %s\n" % (str(abs(pid)), str(lt))
        if not chargedList and not missingList: return 1, "no long lived particles found"
        else:
            msg = ""
            if chargedList:
                msg += "#Visible decays of longlived particles / stable charged particles: %s\n%s" % (str(chargedList), ltstr)
            if missingList:
                msg += "#Missing decay blocks of new r-Even particles appearing in displaced vertices: %s\n" % (str(missingList))
        return -1, msg
Beispiel #6
0
 def testXSecMain(self):
     """ test the main routine for computation of LO and NLL cross section for several sqrts"""
     
     slhafile="./testFiles/slha/simplyGluino.slha"
     f = open(slhafile,'r')
     fdata = f.read()
     fdata = fdata[:fdata.find('XSECTION')]
     f.close()
     fnew = tempfile.mkstemp()
     os.close(fnew[0])
     tmpfile = fnew[1]
     fnew = open(tmpfile,'w')
     fnew.write(fdata)
     fnew.close()        
     self.logger.info ("test NLL xsecs @ 8 and 13 TeV" )
     #Set overall options:
     #Options for cross section calculation:
     xargs = argparse.Namespace()
     xargs.sqrts = [[8.,13.]]
     xargs.ncpus = 1
     xargs.noautocompile = True
     xargs.nevents = 2000
     #Compute LO xsecs:
     xargs.query = False
     xargs.NLL = False
     xargs.NLO = False
     xargs.LOfromSLHA = False
     xargs.keep = False
     xargs.tofile = True
     xargs.alltofile = False
     xargs.pythia6 = True
     xargs.filename = tmpfile
     xargs.colors = False
     xargs.ssmultipliers = None
     xargs.verbosity = "warning"
     #Compute LO cross sections
     xsecComputer.main(xargs)
     #Compute NLL cross sections
     xargs.NLL = True
     xargs.LOfromSLHA = True
     xsecComputer.main(xargs)
     #Read xsecs:
     xsecsInfile = crossSection.getXsecFromSLHAFile(tmpfile)
     os.remove(tmpfile)
     
     #Check 8 TeV xsecs:
     lo = xsecsInfile.getXsecsFor('8 TeV (LO)')[0].value.asNumber(fb)
     nll = xsecsInfile.getXsecsFor('8 TeV (NLL)')[0].value.asNumber(fb)
     self.assertAlmostEqual(lo/264.,1.,2)
     self.assertAlmostEqual(nll/573.6,1.,2)
     #Check 13 TeV xsecs:
     lo = xsecsInfile.getXsecsFor('13 TeV (LO)')[0].value.asNumber(fb)
     nll = xsecsInfile.getXsecsFor('13 TeV (NLL)')[0].value.asNumber(fb)
     self.assertAlmostEqual(lo / 2230., 1., 1 )
     self.assertAlmostEqual(nll / 4308., 1., 1 )
Beispiel #7
0
def checkFiles(slha6,slha8,Nevents = 50000):
    
    xsecs8 = getXsecFromSLHAFile(slha8)
    if not xsecs8:
        return (slha6,slha8,True)
    xsecs6 = getXsecFromSLHAFile(slha6)    
    w6 = xsecs6.getXsecsFor('8 TeV (LO)').getDictionary()
    w8 = xsecs8.getXsecsFor('8 TeV (LO)').getDictionary()


    #Remove the antisbottom-gluino xsec (seems to be missing in Pythia 8):
#    if (-1000005, 1000021) in w6:
#        w6.pop((-1000005, 1000021))
    #Remove the antisdown-gluino xsec (seems to be missing in Pythia 8):
#    if (-1000001, 1000021) in w6:
#        w6.pop((-1000001, 1000021))
#    if (-2000001, 1000021) in w6:
#        w6.pop((-2000001, 1000021))
#    if (-1000003, 1000021) in w6:
#        w6.pop((-1000003, 1000021))
    #Remove the antisbottom-gluino xsec (seems to be missing in Pythia 8):
#    if (-1000024, 1000021) in w6:
#        totxsec = w6[(-1000024, 1000021)].values()[0]
#        if (1000021, 1000024) in w6:
#            totxsec += w6[(1000021, 1000024)].values()[0]
#        w6.pop((-1000024, 1000021))
#        w6[(1000021, 1000024)] = {'8 TeV (LO)' : totxsec}

    comp = compareXSections(w6,w8,Nevents,relError=0.1)
    
    if not comp:
        return (slha6,slha8,True)
    else:
        #Check for the case of degenerate squarks
        comp = debugFile(slha6,nevts=Nevents,forceDegenerate=True)
        
    if comp:
        logger.error(str(comp))
        return (slha6,slha8,False)
    else:        
        return (slha6,slha8,True)
Beispiel #8
0
 def testXSecMain(self):
     """ test the main routine for computation of LO and NLL cross section for several sqrts"""
     
     slhafile="./testFiles/slha/simplyGluino.slha"
     f = open(slhafile,'r')
     fdata = f.read()
     fdata = fdata[:fdata.find('XSECTION')]
     f.close()
     fnew = tempfile.mkstemp()
     os.close(fnew[0])
     tmpfile = fnew[1]
     fnew = open(tmpfile,'w')
     fnew.write(fdata)
     fnew.close()        
     self.logger.info ("test NLL xsecs @ 8 and 13 TeV" )
     #Set overall options:
     #Options for cross section calculation:
     xargs = argparse.Namespace()
     xargs.sqrts = [[8.,13.]]
     xargs.ncpus = 1
     xargs.nevents = 100
     #Compute LO xsecs:
     xargs.query = False
     xargs.NLL = False
     xargs.NLO = False
     xargs.LOfromSLHA = False
     xargs.keep = False
     xargs.tofile = True
     xargs.alltofile = False
     xargs.pythia6 = True
     xargs.filename = tmpfile
     xargs.colors = False
     xargs.verbosity = "warning"
     #Compute LO cross sections
     xsecComputer.main(xargs)
     #Compute NLL cross sections
     xargs.NLL = True
     xargs.LOfromSLHA = True
     xsecComputer.main(xargs)
     #Read xsecs:
     xsecsInfile = crossSection.getXsecFromSLHAFile(tmpfile)
     os.remove(tmpfile)
     
     #Check 8 TeV xsecs:
     lo = xsecsInfile.getXsecsFor('8 TeV (LO)')[0].value.asNumber(fb)
     nll = xsecsInfile.getXsecsFor('8 TeV (NLL)')[0].value.asNumber(fb)
     self.assertAlmostEqual(lo,268.2255,3)
     self.assertAlmostEqual(nll,582.612609,3)
     #Check 13 TeV xsecs:
     lo = xsecsInfile.getXsecsFor('13 TeV (LO)')[0].value.asNumber(fb)
     nll = xsecsInfile.getXsecsFor('13 TeV (NLL)')[0].value.asNumber(fb)
     self.assertAlmostEqual(lo,2240.7303,2 )
     self.assertAlmostEqual(nll,4329.09094,2)
Beispiel #9
0
def checkFiles(slha6, slha8, Nevents=50000):

    xsecs8 = getXsecFromSLHAFile(slha8)
    if not xsecs8:
        return (slha6, slha8, True)
    xsecs6 = getXsecFromSLHAFile(slha6)
    w6 = xsecs6.getXsecsFor('8 TeV (LO)').getDictionary()
    w8 = xsecs8.getXsecsFor('8 TeV (LO)').getDictionary()

    #Remove the antisbottom-gluino xsec (seems to be missing in Pythia 8):
    #    if (-1000005, 1000021) in w6:
    #        w6.pop((-1000005, 1000021))
    #Remove the antisdown-gluino xsec (seems to be missing in Pythia 8):
    #    if (-1000001, 1000021) in w6:
    #        w6.pop((-1000001, 1000021))
    #    if (-2000001, 1000021) in w6:
    #        w6.pop((-2000001, 1000021))
    #    if (-1000003, 1000021) in w6:
    #        w6.pop((-1000003, 1000021))
    #Remove the antisbottom-gluino xsec (seems to be missing in Pythia 8):
    #    if (-1000024, 1000021) in w6:
    #        totxsec = w6[(-1000024, 1000021)].values()[0]
    #        if (1000021, 1000024) in w6:
    #            totxsec += w6[(1000021, 1000024)].values()[0]
    #        w6.pop((-1000024, 1000021))
    #        w6[(1000021, 1000024)] = {'8 TeV (LO)' : totxsec}

    comp = compareXSections(w6, w8, Nevents, relError=0.1)

    if not comp:
        return (slha6, slha8, True)
    else:
        #Check for the case of degenerate squarks
        comp = debugFile(slha6, nevts=Nevents, forceDegenerate=True)

    if comp:
        logger.error(str(comp))
        return (slha6, slha8, False)
    else:
        return (slha6, slha8, True)
Beispiel #10
0
    def getModelDataFrom(self,inputFile):
        """
        Reads the input file (LHE or SLHA) and extract the relevant information
        (masses, widths, BRs and cross-sections). If a http address is given, it will
        attempt to download the file.

        :param inputFile: input file (SLHA or LHE)

        :return: dictionary with masses, dictionary with decays and XSectionList object
        """

        #Download input file, if requested
        if inputFile.startswith("http") or inputFile.startswith("ftp"):
            logger.info("Asked for remote slhafile %s. Fetching it." % inputFile)
            import requests
            import os.path
            r = requests.get(inputFile)
            if r.status_code != 200:
                logger.error("Could not retrieve remote file %d: %s" %(r.status_code, r.reason))
                raise SModelSError()
            basename = os.path.basename(inputFile)
            f = open(basename, "w")
            f.write(r.text)
            f.close()
            inputFile = basename

        #Trick to suppress pyslha error messages:
        import sys
        storeErr = sys.stderr
        #Try to read file assuming it is an SLHA file:
        try:
            sys.stderr = None
            res = pyslha.readSLHAFile(inputFile)
            massDict = res.blocks['MASS']
            #Make sure both PDG signs appear in massDict
            for pdg,mass in massDict.items():
                if not -pdg in massDict:
                    massDict[-pdg] = abs(mass)
            decaysDict = res.decays
            xsections = crossSection.getXsecFromSLHAFile(inputFile)
        #If fails assume it is an LHE file:
        except (IOError,AttributeError,KeyError):
            massDict,decaysDict = lheReader.getDictionariesFrom(inputFile)
            xsections = crossSection.getXsecFromLHEFile(inputFile)
            logger.info("Using LHE input. All unstable particles will be assumed to have prompt decays.")
            logger.info("Using LHE input. All particles not appearing in the events will be removed from the model.")
        finally:
            sys.stderr = storeErr

        return massDict,decaysDict,xsections
Beispiel #11
0
    def addXSecToFile(self, xsecs, slhafile, comment=None, complain=True):
        """
        Write cross sections to an SLHA file.

        :param xsecs: a XSectionList object containing the cross sections
        :param slhafile: target file for writing the cross sections in SLHA format
        :param comment: optional comment to be added to each cross section block
        :param complain: complain if there are already cross sections in file

        """

        if not os.path.isfile(slhafile):
            line = f"SLHA file {slhafile} not found."
            logger.error(line)
            raise SModelSError(line)
        if len(xsecs) == 0:
            self.countNoXSecs += 1
            if self.countNoXSecs < 3:
                logger.warning("No cross sections available.")
            if self.countNoXSecs == 3:
                logger.warning(
                    "No cross sections available (will quench such warnings in future)."
                )
            return False
        # Check if file already contain cross section blocks
        xSectionList = crossSection.getXsecFromSLHAFile(slhafile)
        if xSectionList and complain:
            logger.info("SLHA file already contains XSECTION blocks. Adding "
                        "only missing cross sections.")

        # Write cross sections to file, if they do not overlap any cross section in
        # the file
        outfile = open(slhafile, 'a')
        nxsecs = 0
        for xsec in xsecs:
            writeXsec = True
            for oldxsec in xSectionList:
                if oldxsec.info == xsec.info and set(oldxsec.pid) == set(
                        xsec.pid):
                    writeXsec = False
                    break
            if writeXsec:
                nxsecs += 1
                outfile.write(
                    self.xsecToBlock(xsec, (2212, 2212), comment) + "\n")
        outfile.close()

        return nxsecs
Beispiel #12
0
def addXSecToFile(xsecs, slhafile, comment=None, complain=True):
    """
    Write cross-sections to an SLHA file.
    
    :param xsecs: a XSectionList object containing the cross-sections
    :param slhafile: target file for writing the cross-sections in SLHA format
    :param comment: optional comment to be added to each cross-section block
    :param complain: complain if there are already cross sections in file
    
    """
    if not os.path.isfile(slhafile):
        logger.error("SLHA file not found.")
        import sys
        sys.exit()
    if len(xsecs) == 0:
        logger.warning("No cross-sections available.")
        return False
    # Check if file already contain cross-section blocks
    xSectionList = crossSection.getXsecFromSLHAFile(slhafile)
    if xSectionList and complain:
        logger.warning("SLHA file already contains XSECTION blocks. Adding "
                       "only missing cross-sections.")

    # Write cross-sections to file, if they do not overlap any cross-section in
    # the file
    outfile = open(slhafile, 'a')
    for xsec in xsecs:
        writeXsec = True
        for oldxsec in xSectionList:
            if oldxsec.info == xsec.info and set(oldxsec.pid) == set(xsec.pid):
                writeXsec = False
                break
        if writeXsec:
            outfile.write(xsecToBlock(xsec, (2212, 2212), comment) + "\n")
    outfile.close()

    return True
Beispiel #13
0
    def compute(self,
                sqrts,
                slhafile,
                lhefile=None,
                unlink=True,
                loFromSlha=None,
                pythiacard=None,
                ssmultipliers=None):
        """
        Run pythia and compute SUSY cross sections for the input SLHA file.

        :param sqrts: sqrt{s} to run Pythia, given as a unum (e.g. 7.*TeV)
        :param slhafile: SLHA file
        :param lhefile: LHE file. If None, do not write pythia output to file. If
                     file does not exist, write pythia output to this file name. If
                     file exists, read LO xsecs from this file (does not run pythia).
        :param unlink: Clean up temp directory after running pythia
        :param loFromSlha: If True, uses the LO xsecs from the SLHA file to
                           compute the higher order xsecs
        :param pythiaCard: Optional path to pythia.card. If None, uses
                           smodels/etc/pythia.card
        :param ssmultipliers: optionally supply signal strengh multipliers,
                given as dictionary of the tuple of the mothers' pids as keys and
                multipliers as values, e.g { (1000001,1000021):1.1 }.
        :returns: XSectionList object

        """
        sqrts = self._checkSqrts(sqrts)
        self._checkSLHA(slhafile)

        if lhefile:
            if os.path.isfile(lhefile):
                logger.warning("Using LO cross sections from " + lhefile)
                logger.error(
                    "Cross section retrieval from lhefile currently not implemented"
                )
                sys.exit()
            else:
                logger.info("Writing pythia LHE output to " + lhefile)
        if loFromSlha:
            logger.info("Using LO cross sections from " + slhafile)
            xsecsInfile = crossSection.getXsecFromSLHAFile(slhafile)
            loXsecs = crossSection.XSectionList()
            for xsec in xsecsInfile:
                if xsec.info.order == 0 and abs(xsec.info.sqrts -
                                                sqrts).asNumber(TeV) < 1e-5:
                    loXsecs.add(xsec)
            if ssmultipliers != None:
                logger.warning(
                    "supplied signal strength multipliers, but LO xsecs are taken from file. Will not apply them"
                )
        else:
            logger.info("get LO cross sections from pythia%d" %
                        self.pythiaVersion)
            tool = self.getPythia()
            tool.tempdir = None  # reset, to make sure it works in parallel mode
            tool.nevents = self.nevents
            tool.sqrts = sqrts / TeV
            tool.pythiacard = pythiacard
            loXsecs = tool.run(slhafile, lhefile, unlink=unlink)
            if ssmultipliers != None:
                loXsecs = self.applyMultipliers(loXsecs, ssmultipliers)
        self.loXsecs = loXsecs
        self.loXsecs.sort()
        self.xsecs = self.addHigherOrders(sqrts, slhafile)
        self.xsecs.sort()
        #for xsec in self.loXsecs:
        #    logger.debug ( "now writing out xsecs: %s" % xsec.value )
        logger.debug("how many NLL xsecs? %d" % len(self.xsecs))
        return self.xsecs
Beispiel #14
0
        logger.warning("sqrt(s) given as scalar, will add TeV as unit." )
        sqrts=float(sqrts)*TeV

    smaxorder={ "LO": 0, "NLO": 1, "NLL": 2 }
    if maxOrder in smaxorder.keys():
        logger.warning("maxorder given as string, please supply integer.")
        maxOrder=smaxorder[maxOrder]

    if lhefile:
        if os.path.isfile(lhefile):
            logger.warning("Using LO cross-sections from " + lhefile)
        else:
            logger.info("Writing pythia LHE output to " + lhefile)
    if loFromSlha:
        logger.info("Using LO cross-sections from " + slhafile)
        xsecsInfile = crossSection.getXsecFromSLHAFile(slhafile)
        loXsecs = crossSection.XSectionList()
        for xsec in xsecsInfile:
            if xsec.info.order == 0: loXsecs.add(xsec)
    else:
        if not lhefile or not os.path.isfile(lhefile):
            lheFile = runPythia(slhafile, nevts, sqrts / TeV, lhefile, unlink=unlink)
        else:
            lheFile = open(lhefile, 'r')
        loXsecs = crossSection.getXsecFromLHEFile(lheFile)

    xsecs = loXsecs
    wlabel = str(int(sqrts / TeV)) + ' TeV'
    if maxOrder == 0:
        wlabel += ' (LO)'
    elif maxOrder == 1:
Beispiel #15
0
    def run( self, slhafile, lhefile=None, unlink=True ):
        """
        Execute pythia_lhe with n events, at sqrt(s)=sqrts.

        :param slhafile: input SLHA file
        :param lhefile: option to write LHE output to file; if None, do not write
                        output to disk. If lhe file exists, use its events for
                        xsecs calculation.
        :param unlink: Clean up temp directory after running pythia

        :returns: List of cross sections

        """
        if lhefile and os.path.isfile ( lhefile ):
            lheFile = open(lhefile, 'r')
            xsecsInfile = crossSection.getXsecFromSLHAFile(slhafile)
            loXsecs = crossSection.XSectionList()
            for xsec in xsecsInfile:
                if xsec.info.order == LO and \
                        float (xsec.info.sqrts.asNumber(TeV)) == self.sqrts:
                    loXsecs.add(xsec)
            return loXsecs

        #Change pythia card, if defined:
        if self.pythiacard:
            pythiacard_default = self.cfgfile
            self.cfgfile = self.pythiacard
        # Check if template config file exists
        if unlink:
            self.unlink()
        else:
            self.tempdir = None
        self.replaceInCfgFile({"NEVENTS": self.nevents, "SQRTS":1000 * self.sqrts})
        self.setParameter("MSTP(163)", "6")

        if unlink==False:
            logger.info ( "keeping temporary directory at %s" % self.tempDirectory() )
        r = self.checkInstallation()
        if r == False:
            logger.info ( "Installation check failed." )
            sys.exit()
        self.replaceInCfgFile({"NEVENTS": self.nevents, "SQRTS":1000 * self.sqrts})
        self.setParameter("MSTP(163)", "6")
        lhedata = self._run(slhafile, unlink=unlink )
        if not "<LesHouchesEvents" in lhedata:
            pythiadir = "%s/log" % self.tempDirectory()
            logger.error("No LHE events found in pythia output %s" % pythiadir )
            if not os.path.exists ( pythiadir ):
                logger.error ("Will dump pythia output to %s" % pythiadir )
                f=open ( pythiadir, "w" )
                for line in lhedata:
                    f.write ( line )
                f.close()
            raise SModelSError( "No LHE events found in %s" % pythiadir )

        #Reset pythia card to its default value
        if self.pythiacard:
            self.cfgfile = pythiacard_default

        #if not unlink:
        #    lhefile = self.tempdir + "/events.lhe"
        # Generate file object with lhe events
        if lhefile:
            lheFile = open(lhefile, 'w')
            lheFile.write(lhedata)
            lheFile.close()
            lheFile = open(lhefile, 'r')
        else:
            # Create memory only file object
            if sys.version[0]=="2":
                lhedata = unicode ( lhedata )
            lheFile = io.StringIO(lhedata)
        return crossSection.getXsecFromLHEFile ( lheFile )
def computeXSec(sqrts,
                maxOrder,
                nevts,
                slhafile,
                lhefile=None,
                unlink=True,
                loFromSlha=None,
                pythiacard=None):
    """
    Run pythia and compute SUSY cross sections for the input SLHA file.

    :param sqrts: sqrt{s} to run Pythia, given as a unum (e.g. 7.*TeV)
    :param maxOrder: maximum order to compute the cross section, given as an integer
                if maxOrder == 0, compute only LO pythia xsecs
                if maxOrder == 1, apply NLO K-factors from NLLfast (if available)
                if maxOrder == 2, apply NLO+NLL K-factors from NLLfast (if available)
    :param nevts: number of events for pythia run
    :param slhafile: SLHA file
    :param lhefile: LHE file. If None, do not write pythia output to file. If
                    file does not exist, write pythia output to this file name. If
                    file exists, read LO xsecs from this file (does not run pythia).
    :param unlink: Clean up temp directory after running pythia

    :param loFromSlha: If True, uses the LO xsecs from the SLHA file to compute the
                       higher order xsecs
    :param pythiaCard: Optional path to pythia.card. If None, uses /etc/pythia.card

    :returns: XSectionList object

    """
    if not os.path.isfile(slhafile):
        logger.error("SLHA file %s not found.", slhafile)
        raise SModelSError()
    try:
        f = pyslha.readSLHAFile(slhafile)
    except pyslha.ParseError as e:
        logger.error("File cannot be parsed as SLHA file: %s" % e)
        raise SModelSError()

    if type(sqrts) == type(float) or type(sqrts) == type(int):
        logger.warning("sqrt(s) given as scalar, will add TeV as unit.")
        sqrts = float(sqrts) * TeV

    smaxorder = {"LO": 0, "NLO": 1, "NLL": 2}
    if maxOrder in smaxorder.keys():
        logger.warning("maxorder given as string, please supply integer.")
        maxOrder = smaxorder[maxOrder]

    if lhefile:
        if os.path.isfile(lhefile):
            logger.warning("Using LO cross sections from " + lhefile)
        else:
            logger.info("Writing pythia LHE output to " + lhefile)
    if loFromSlha:
        logger.info("Using LO cross sections from " + slhafile)
        xsecsInfile = crossSection.getXsecFromSLHAFile(slhafile)
        loXsecs = crossSection.XSectionList()
        for xsec in xsecsInfile:
            if xsec.info.order == 0 and xsec.info.sqrts == sqrts:
                loXsecs.add(xsec)

    else:
        if not lhefile or not os.path.isfile(lhefile):
            lheFile = runPythia(slhafile,
                                nevts,
                                sqrts / TeV,
                                lhefile,
                                unlink=unlink,
                                pythiacard=pythiacard)
        else:
            lheFile = open(lhefile, 'r')
        loXsecs = crossSection.getXsecFromLHEFile(lheFile)
    xsecs = loXsecs
    wlabel = str(int(sqrts / TeV)) + ' TeV'
    if maxOrder == 0:
        wlabel += ' (LO)'
    elif maxOrder == 1:
        wlabel += ' (NLO)'
    elif maxOrder >= 2:
        wlabel += ' (NLO+NLL)'
    for ixsec, xsec in enumerate(xsecs):
        xsecs[ixsec].info.label = wlabel
        xsecs[ixsec].info.order = maxOrder

    if maxOrder > 0:
        pIDs = loXsecs.getPIDpairs()
        for pID in pIDs:
            k = 0.
            kNLO, kNLL = nllFast.getKfactorsFor(pID, sqrts, slhafile)
            if maxOrder == 1 and kNLO:
                k = kNLO
            elif maxOrder == 2 and kNLL and kNLO:
                k = kNLO * kNLL
            elif maxOrder > 2 and kNLL and kNLO:
                logger.warning("Unkown xsec order, using NLL+NLO k-factor, "
                               "if available")
                k = kNLO * kNLL
            k = float(k)
            for i, xsec in enumerate(xsecs):
                if set(xsec.pid) == set(pID):
                    # Apply k-factor
                    xsecs[i] = xsec * k

    # Remove zero cross sections
    while len(xsecs) > 0 and xsecs.getMinXsec() == 0. * pb:
        for xsec in xsecs:
            if xsec.value == 0. * pb:
                xsecs.delete(xsec)
                break
    if maxOrder > 0 and len(xsecs) == 0:
        logger.warning("No NLO or NLL cross sections available.")

    return xsecs
def decompose(slhafile,
              sigcut=.1 * fb,
              doCompress=False,
              doInvisible=False,
              minmassgap=-1. * GeV,
              useXSecs=None):
    """
    Perform SLHA-based decomposition.
    
    :param sigcut: minimum sigma*BR to be generated, by default sigcut = 0.1 fb
    :param doCompress: turn mass compression on/off
    :param doInvisible: turn invisible compression on/off
    :param minmassgap: maximum value (in GeV) for considering two R-odd particles
                       degenerate (only revelant for doCompress=True )
    :param useXSecs: optionally a dictionary with cross sections for pair
                 production, by default reading the cross sections
                 from the SLHA file.
    :returns: list of topologies (TopologyList object)

    """
    t1 = time.time()

    if doCompress and minmassgap / GeV < 0.:
        logger.error(
            "Asked for compression without specifying minmassgap. Please set minmassgap."
        )
        raise SModelSError()

    if type(sigcut) == type(1.):
        sigcut = sigcut * fb

    try:
        f = pyslha.readSLHAFile(slhafile)
    except pyslha.ParseError as e:
        logger.error("The file %s cannot be parsed as an SLHA file: %s" %
                     (slhafile, e))
        raise SModelSError()

    # Get cross section from file
    xSectionList = crossSection.getXsecFromSLHAFile(slhafile, useXSecs)
    # Get BRs and masses from file
    brDic, massDic = _getDictionariesFromSLHA(slhafile)
    # Only use the highest order cross sections for each process
    xSectionList.removeLowerOrder()
    # Order xsections by PDGs to improve performance
    xSectionList.order()

    # Get maximum cross sections (weights) for single particles (irrespective
    # of sqrtS)
    maxWeight = {}
    for pid in xSectionList.getPIDs():
        maxWeight[pid] = xSectionList.getXsecsFor(pid).getMaxXsec()

    # Generate dictionary, where keys are the PIDs and values
    # are the list of cross sections for the PID pair (for performance)
    xSectionListDict = {}
    for pids in xSectionList.getPIDpairs():
        xSectionListDict[pids] = xSectionList.getXsecsFor(pids)

    # Create 1-particle branches with all possible mothers
    branchList = []
    for pid in maxWeight:
        branchList.append(Branch())
        branchList[-1].PIDs = [[pid]]
        if not pid in massDic:
            logger.error(
                "pid %d does not appear in masses dictionary %s in slhafile %s"
                % (pid, massDic, slhafile))
        branchList[-1].masses = [massDic[pid]]
        branchList[-1].maxWeight = maxWeight[pid]

    # Generate final branches (after all R-odd particles have decayed)
    finalBranchList = decayBranches(branchList, brDic, massDic, sigcut)
    # Generate dictionary, where keys are the PIDs and values are the list of branches for the PID (for performance)
    branchListDict = {}
    for branch in finalBranchList:
        if len(branch.PIDs) != 1:
            logger.error("During decomposition the branches should \
                            not have multiple PID lists!")
            return False
        if branch.PIDs[0][0] in branchListDict:
            branchListDict[branch.PIDs[0][0]].append(branch)
        else:
            branchListDict[branch.PIDs[0][0]] = [branch]
    for pid in xSectionList.getPIDs():
        if not pid in branchListDict: branchListDict[pid] = []

    #Sort the branch lists by max weight to improve performance:
    for pid in branchListDict:
        branchListDict[pid] = sorted(branchListDict[pid],
                                     key=lambda br: br.maxWeight,
                                     reverse=True)

    smsTopList = topology.TopologyList()
    # Combine pairs of branches into elements according to production
    # cross section list
    for pids in xSectionList.getPIDpairs():
        weightList = xSectionListDict[pids]
        minBR = (sigcut / weightList.getMaxXsec()).asNumber()
        if minBR > 1.: continue
        for branch1 in branchListDict[pids[0]]:
            BR1 = branch1.maxWeight / maxWeight[
                pids[0]]  #Branching ratio for first branch
            if BR1 < minBR: break  #Stop loop if BR1 is already too low
            for branch2 in branchListDict[pids[1]]:
                BR2 = branch2.maxWeight / maxWeight[
                    pids[1]]  #Branching ratio for second branch
                if BR2 < minBR: break  #Stop loop if BR2 is already too low

                finalBR = BR1 * BR2
                if type(finalBR) == type(1. * fb):
                    finalBR = finalBR.asNumber()
                if finalBR < minBR:
                    continue  # Skip elements with xsec below sigcut

                if len(branch1.PIDs) != 1 or len(branch2.PIDs) != 1:
                    logger.error("During decomposition the branches should \
                            not have multiple PID lists!")
                    return False

                newElement = element.Element([branch1, branch2])
                newElement.weight = weightList * finalBR
                allElements = [newElement]
                # Perform compression
                if doCompress or doInvisible:
                    allElements += newElement.compressElement(
                        doCompress, doInvisible, minmassgap)

                for el in allElements:
                    el.sortBranches(
                    )  #Make sure elements are sorted BEFORE adding them
                    smsTopList.addElement(el)
    smsTopList._setElementIds()

    logger.debug("slhaDecomposer done in %.2f s." % (time.time() - t1))
    return smsTopList
Beispiel #18
0
            "Asked for compression without specifying minmassgap. Please set minmassgap."
        )
        sys.exit()

    if type(sigcut) == type(1.):
        sigcut = sigcut * fb

    from smodels.tools import modpyslha
    try:
        f = modpyslha.readSLHAFile(slhafile)
    except modpyslha.ParseError, e:
        logger.error("This file cannot be parsed as an SLHA file: %s" % e)
        sys.exit()

    # Get cross-section from file
    xSectionList = crossSection.getXsecFromSLHAFile(slhafile, useXSecs)
    # Get BRs and masses from file
    brDic, massDic = _getDictionariesFromSLHA(slhafile)
    # Only use the highest order cross-sections for each process
    xSectionList.removeLowerOrder()
    # Order xsections by PDGs to improve performance
    xSectionList.order()

    # Get maximum cross-sections (weights) for single particles (irrespective
    # of sqrtS)
    maxWeight = {}
    for pid in xSectionList.getPIDs():
        maxWeight[pid] = xSectionList.getXsecsFor(pid).getMaxXsec()

    # Generate dictionary, where keys are the PIDs and values
    # are the list of cross-sections for the PID pair (for performance)
Beispiel #19
0
def decompose(slhafile, sigcut=.1 * fb, doCompress=False, doInvisible=False,
              minmassgap=-1.*GeV, useXSecs=None):
    """
    Perform SLHA-based decomposition.

    :param slhafile: the slha input file. May be an URL (though http, ftp only).
    :param sigcut: minimum sigma*BR to be generated, by default sigcut = 0.1 fb
    :param doCompress: turn mass compression on/off
    :param doInvisible: turn invisible compression on/off
    :param minmassgap: maximum value (in GeV) for considering two R-odd particles
                       degenerate (only revelant for doCompress=True )
    :param useXSecs: optionally a dictionary with cross sections for pair
                 production, by default reading the cross sections
                 from the SLHA file.
    :returns: list of topologies (TopologyList object)

    """
    if slhafile.startswith("http") or slhafile.startswith("ftp"):
        logger.info ( "asked for remote slhafile %s. will fetch it." % slhafile )
        import requests
        import os.path
        r=requests.get(slhafile)
        if r.status_code != 200:
            logger.error ( "could not retrieve remote file %d: %s" % ( r.status_code, r.reason ) )
            raise SModelSError()
        basename = os.path.basename ( slhafile )
        f=open ( basename, "w" )
        f.write ( r.text )
        f.close()
        slhafile = basename
    t1 = time.time()

    if doCompress and minmassgap / GeV < 0.:
        logger.error("Asked for compression without specifying minmassgap. Please set minmassgap.")        
        raise SModelSError()

    if type(sigcut) == type(1.):
        sigcut = sigcut * fb

    try:
        f=pyslha.readSLHAFile ( slhafile )
    except pyslha.ParseError as e:
        logger.error ( "The file %s cannot be parsed as an SLHA file: %s" % (slhafile, e) )
        raise SModelSError()

    # Get cross section from file
    xSectionList = crossSection.getXsecFromSLHAFile(slhafile, useXSecs)
    # Get BRs and masses from file
    brDic, massDic = _getDictionariesFromSLHA(slhafile)
    # Only use the highest order cross sections for each process
    xSectionList.removeLowerOrder()
    # Order xsections by PDGs to improve performance
    xSectionList.order()
    #Reweight decays by fraction of prompt decays and add fraction of long-lived
    brDic = _getPromptDecays(slhafile,brDic)

    # Get maximum cross sections (weights) for single particles (irrespective
    # of sqrtS)
    maxWeight = {}
    for pid in xSectionList.getPIDs():
        maxWeight[pid] = xSectionList.getXsecsFor(pid).getMaxXsec()    

    # Generate dictionary, where keys are the PIDs and values 
    # are the list of cross sections for the PID pair (for performance)
    xSectionListDict = {}    
    for pids in xSectionList.getPIDpairs():
        xSectionListDict[pids] = xSectionList.getXsecsFor(pids)

    # Create 1-particle branches with all possible mothers
    branchList = []
    for pid in maxWeight:
        branchList.append(Branch())
        branchList[-1].PIDs = [[pid]]
        if not pid in massDic:
            logger.error ( "pid %d does not appear in masses dictionary %s in slhafile %s" % 
                    ( pid, massDic, slhafile ) )
        branchList[-1].masses = [massDic[pid]]
        branchList[-1].maxWeight = maxWeight[pid]

    # Generate final branches (after all R-odd particles have decayed)
    finalBranchList = decayBranches(branchList, brDic, massDic, sigcut)
    # Generate dictionary, where keys are the PIDs and values are the list of branches for the PID (for performance)
    branchListDict = {}
    for branch in finalBranchList:
        if len(branch.PIDs) != 1:
            logger.error("During decomposition the branches should \
                            not have multiple PID lists!")
            return False   
        if branch.PIDs[0][0] in branchListDict:
            branchListDict[branch.PIDs[0][0]].append(branch)
        else:
            branchListDict[branch.PIDs[0][0]] = [branch]
    for pid in xSectionList.getPIDs():
        if not pid in branchListDict: branchListDict[pid] = []

    #Sort the branch lists by max weight to improve performance:
    for pid in branchListDict:
        branchListDict[pid] = sorted(branchListDict[pid], 
                                     key=lambda br: br.maxWeight, reverse=True)
    
    smsTopList = topology.TopologyList()
    # Combine pairs of branches into elements according to production
    # cross section list
    for pids in xSectionList.getPIDpairs():
        weightList = xSectionListDict[pids]
        minBR = (sigcut/weightList.getMaxXsec()).asNumber()
        if minBR > 1.: continue
        for branch1 in branchListDict[pids[0]]:
            BR1 = branch1.maxWeight/maxWeight[pids[0]]  #Branching ratio for first branch            
            if BR1 < minBR: break  #Stop loop if BR1 is already too low            
            for branch2 in branchListDict[pids[1]]:
                BR2 = branch2.maxWeight/maxWeight[pids[1]]  #Branching ratio for second branch
                if BR2 < minBR: break  #Stop loop if BR2 is already too low
                
                finalBR = BR1*BR2                
                if type(finalBR) == type(1.*fb):
                    finalBR = finalBR.asNumber()
                if finalBR < minBR: continue # Skip elements with xsec below sigcut

                if len(branch1.PIDs) != 1 or len(branch2.PIDs) != 1:
                    logger.error("During decomposition the branches should \
                            not have multiple PID lists!")
                    return False    

                newElement = element.Element([branch1, branch2])
                newElement.weight = weightList*finalBR
                newElement.sortBranches()  #Make sure elements are sorted BEFORE adding them
                smsTopList.addElement(newElement)
    
    smsTopList.compressElements(doCompress, doInvisible, minmassgap)
    smsTopList._setElementIds()

    logger.debug("slhaDecomposer done in %.2f s." % (time.time() -t1 ) )
    return smsTopList
Beispiel #20
0
    if doCompress and minmassgap / GeV < 0.:
        logger.error("Asked for compression without specifying minmassgap. Please set minmassgap.")        
        sys.exit()

    if type(sigcut) == type(1.):
        sigcut = sigcut * fb

    from smodels.tools import modpyslha
    try:
        f=modpyslha.readSLHAFile ( slhafile )
    except modpyslha.ParseError,e:
        logger.error ( "This file cannot be parsed as an SLHA file: %s" % e )
        sys.exit()

    # Get cross-section from file
    xSectionList = crossSection.getXsecFromSLHAFile(slhafile, useXSecs)
    # Get BRs and masses from file
    brDic, massDic = _getDictionariesFromSLHA(slhafile)
    # Only use the highest order cross-sections for each process
    xSectionList.removeLowerOrder()
    # Order xsections by PDGs to improve performance
    xSectionList.order()

    # Get maximum cross-sections (weights) for single particles (irrespective
    # of sqrtS)
    maxWeight = {}
    for pid in xSectionList.getPIDs():
        maxWeight[pid] = xSectionList.getXsecsFor(pid).getMaxXsec()    

    # Generate dictionary, where keys are the PIDs and values 
    # are the list of cross-sections for the PID pair (for performance)