Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-23 02:25:13

0001 from __future__ import print_function
0002 from array import array
0003 from copy import copy
0004 from copy import deepcopy
0005 import FWCore.ParameterSet.Config as cms
0006 import FWCore.PythonUtilities.LumiList as LumiList
0007 
0008 # Helper functions
0009 def getPSetDict(thePSet):
0010    return thePSet.parameters_()
0011 
0012 def insertValToPSet(name,val,thePSet):
0013    setattr(thePSet,name,val)
0014 
0015 def insertPSetToPSet(inPSet, outPSet):
0016    for key,val in getPSetDict(inPSet.items()):
0017       insertValToPSet(key,val,outPSet)
0018 
0019 def insertPSetToVPSet(inPSet, outVPSet):
0020    outVPSet.append(inPSet)
0021 
0022 
0023 def matchPSetsByRecord(ps1, ps2):
0024    if hasattr(ps1,"record") and hasattr(ps2,"record"):
0025       s1=ps1.record.value()
0026       s2=ps2.record.value()
0027       return (s1==s2)
0028    return False
0029 
0030 
0031 def mergeVPSets(inVPSet, overrideVPSet, matchrule=None):
0032    resvpset=overrideVPSet.copy()
0033    for iop in inVPSet.value():
0034       nomatch=True
0035       if matchrule is not None:
0036          for cps in overrideVPSet.value():
0037             if matchrule(cps,iop):
0038                nomatch=False
0039                break
0040       if nomatch:
0041          insertPSetToVPSet(iop,resvpset)
0042    return resvpset
0043 
0044 
0045 
0046 
0047 def parseBoolString(theString):
0048    return theString[0].upper()=='T'
0049 
0050 def isGoodEntry(var):
0051    if (var is None):
0052       return False
0053    elif (var == []):
0054       return False
0055    else:
0056       return True
0057 
0058 
0059 class HipPyOptionParser:
0060    def __init__(self, strflag, stropt):
0061       # input file
0062       self.flag=strflag.lower().strip()
0063       self.rawopt = stropt.strip()
0064       self.optdict=dict()
0065 
0066       self.datatype=-1
0067       self.CPEtype="template"
0068       self.getTrackDefaults()
0069 
0070       if self.rawopt.lower()!="noopts":
0071          self.parseOptions()
0072          self.interpretOptions()
0073 
0074 
0075    def getTrackDefaults(self):
0076       if self.flag=="mbvertex":
0077          self.trkcoll="ALCARECOTkAlMinBias"
0078          self.Bfield="3.8t"
0079       elif self.flag=="zmumu":
0080          self.trkcoll="ALCARECOTkAlZMuMu"
0081          self.Bfield="3.8t"
0082       elif self.flag=="ymumu":
0083          self.trkcoll="ALCARECOTkAlUpsilonMuMu"
0084          self.Bfield="3.8t"
0085       elif self.flag=="jpsimumu":
0086          self.trkcoll="ALCARECOTkAlJpsiMuMu"
0087          self.Bfield="3.8t"
0088       elif self.flag=="cosmics":
0089          self.trkcoll="ALCARECOTkAlCosmicsCTF0T"
0090          self.useTrkSplittingInCosmics=False
0091       elif self.flag=="cdcs":
0092          self.trkcoll="ALCARECOTkAlCosmicsInCollisions"
0093          self.useTrkSplittingInCosmics=False
0094       else:
0095          raise RuntimeError("Flag {} is unimplemented.".format(self.flag))
0096 
0097 
0098    def parseOptions(self):
0099       delimiter=' '
0100       optdelimiter=':'
0101       optlist=self.rawopt.split(delimiter)
0102       for sopt in optlist:
0103          olist=sopt.split(optdelimiter)
0104          if len(olist)==2:
0105             theKey=olist[0].lower()
0106             theVal=olist[1]
0107             self.optdict[theKey]=theVal
0108          else:
0109             raise RuntimeError("Option {} has an invalid number of delimiters {}".format(sopt,optdelimiter))
0110 
0111 
0112    def interpretOptions(self):
0113       gttogetpsets=[]
0114       for key,val in self.optdict.items():
0115          # Get GT name
0116          if key=="gt":
0117             autofind=val.find("auto")
0118             if autofind>-1:
0119                val=val[0:autofind+4]+":"+val[autofind+4:]
0120             self.GlobalTag = val
0121          # Get GT toGet PSets
0122          elif key=="gtspecs":
0123             vallist=val.split(';')
0124             for varset in vallist:
0125                apset = cms.PSet()
0126                specs = varset.split('|')
0127                for spec in specs:
0128                   namespec=spec.split('=')
0129                   if len(namespec)==2:
0130                      tmpspec=namespec[1]
0131                      frontierfind=tmpspec.find("frontier")
0132                      sqlitefind=tmpspec.find("sqlite_file")
0133                      if frontierfind>-1 and sqlitefind>-1:
0134                         raise RuntimeError("Inconsistent setting: Cannot specify frontier and sqlite_file at the same time!")
0135                      elif frontierfind>-1:
0136                         tmpspec=tmpspec[0:frontierfind+8]+":"+tmpspec[frontierfind+8:]
0137                      elif sqlitefind>-1:
0138                         tmpspec=tmpspec[0:sqlitefind+11]+":"+tmpspec[sqlitefind+11:]
0139                      elif namespec[0]=="connect":
0140                         if tmpspec.endswith(".db"):
0141                            tmpspec = str("sqlite_file:")+tmpspec
0142                         elif tmpspec.find("//")>-1:
0143                            tmpspec = str("frontier:")+tmpspec
0144                         else:
0145                            tmpspec = str("frontier://")+tmpspec
0146                      cmsstrspec = cms.string(tmpspec)
0147                      insertValToPSet(namespec[0],cmsstrspec,apset)
0148                   else:
0149                      raise RuntimeError("GT specification {} does not have size==2".format(namespec))
0150                gttogetpsets.append(apset)
0151          # Get hits to drop or keep
0152          elif key=="hitfiltercommands":
0153             vallist=val.split(';')
0154             for iv in range(0,len(vallist)):
0155                keepdrop_det_pair=vallist[iv].split('=')
0156                if len(keepdrop_det_pair)==2:
0157                   if (keepdrop_det_pair[0]=="keep" or keepdrop_det_pair[0]=="drop"):
0158                      strcmd = keepdrop_det_pair[0]
0159 
0160                      keepdrop_det_pair[1]=keepdrop_det_pair[1].replace('/',' ') # e.g. 'PIX/2' instead of 'PIX 2'
0161                      keepdrop_det_pair[1]=keepdrop_det_pair[1].upper()
0162 
0163                      strcmd = strcmd + " " + keepdrop_det_pair[1]
0164                      if not hasattr(self,"hitfiltercommands"):
0165                         self.hitfiltercommands=[]
0166                      self.hitfiltercommands.append(strcmd)
0167                   else:
0168                      raise RuntimeError("Keep/drop command {} is not keep or drop.".format(keepdrop_det_pair[0]))
0169                else:
0170                   raise RuntimeError("Keep/drop-det. pair {} does not have size==2 or has a command other than keep or drop.".format(vallist[iv]))
0171          # Get data type
0172          elif (key=="type" or key=="datatype" or key=="datagroup"):
0173             try:
0174                dtype=int(val)
0175                self.datatype=dtype
0176             except ValueError:
0177                print("Data type is not an integer")
0178          # Get lumi json file
0179          elif key=="lumilist":
0180             self.LumiJSON = LumiList.LumiList(filename = val).getVLuminosityBlockRange()
0181          # Get CPE type
0182          elif key=="cpe" or key=="cpetype":
0183             val=val.lower()
0184             self.CPEtype=val
0185          # Get non-standard track collection name
0186          elif key=="trackcollection":
0187             self.trkcoll=val
0188          # Get overall weight. Turns reweighting on
0189          elif key=="overallweight":
0190             try:
0191                fval=float(val)
0192                self.overallweight=fval
0193             except ValueError:
0194                print("Overall weight is not a float")
0195          # Get uniform eta formula. Turns reweighting on
0196          elif key=="uniformetaformula":
0197             self.uniformetaformula=val
0198          ## Options for mMin. bias
0199          # Apply vertex constraint
0200          elif (key=="primaryvertextype" or key=="pvtype"):
0201             val=val.lower()
0202             if (val=="nobs" or val=="withbs"):
0203                self.PVtype=val
0204             else:
0205                raise ValueError("PV type can only receive NoBS or WithBS.")
0206          elif (key=="primaryvertexconstraint" or key=="pvconstraint"):
0207             self.applyPVConstraint=parseBoolString(val)
0208             if not hasattr(self,"PVtype"):
0209                self.PVtype="nobs"
0210          # Get custom track selection for TBD
0211          elif (key=="twobodytrackselection" or key=="twobodydecayselection" or key=="tbdselection"):
0212             val=val.lower()
0213             if (val=="zsel" or val=="y1ssel"):
0214                self.TBDsel=val
0215             else:
0216                raise ValueError("TBD selection can only be Zsel or Y1Ssel at this time.")
0217          ## Get options common in min. bias, Zmumu and Ymumu
0218          # Get TBD constraint type
0219          elif (key=="twobodytrackconstraint" or key=="twobodydecayconstraint" or key=="tbdconstraint"):
0220             val=val.lower()
0221             if ("momconstr" in val or "fullconstr" in val):
0222                self.TBDconstraint=val
0223             else:
0224                raise ValueError("TBD constraint can only be momconstr... or fullconstr...")
0225          ## Options for cosmics
0226          # Get APV mode
0227          elif key=="apvmode":
0228             val=val.lower()
0229             if (val=="peak" or val=="deco"):
0230                self.APVmode=val
0231             else:
0232                raise ValueError("APV mode can only be peak or deco in cosmics")
0233          # Get magnetic field value
0234          elif key=="bfield":
0235             val=val.lower()
0236             if (val=="0t" or val=="zerotesla" or val=="3.8t"):
0237                self.Bfield=val
0238             else:
0239                raise ValueError("B field can only be 0T, ZEROTESLA or 3.8T")
0240          elif key=="usetracksplitting":
0241             self.useTrkSplittingInCosmics=parseBoolString(val)
0242          else:
0243             raise RuntimeError("Option {} is not implemented.".format(key))
0244 
0245       if len(gttogetpsets)>0:
0246          self.GTtoGet = cms.VPSet()
0247          for ps in gttogetpsets:
0248             insertPSetToVPSet(ps,self.GTtoGet)
0249 
0250 
0251    def doCheckOptions(self,optstocheck):
0252       # First check option consistencies overall
0253       if (hasattr(self,"TBDconstraint") and hasattr(self,"applyPVConstraint")):
0254          raise RuntimeError("Options TBDconstraint and applyPVConstraint cannot coexist.")
0255       # Force presence of the options passed
0256       for oc in optstocheck:
0257          if not hasattr(self,oc):
0258             raise RuntimeError("Option {} needs to specified in {}.".format(oc, self.flag))
0259 
0260 
0261    def checkOptions(self):
0262       optstocheck=[]
0263       checkcosmics=(self.flag=="cosmics" or self.flag=="cdcs")
0264       checkymumuconstr=(self.flag=="ymumu" and hasattr(self, "TBDconstraint"))
0265       if checkcosmics:
0266          optstocheck=[
0267             "Bfield",
0268             "APVmode",
0269             "useTrkSplittingInCosmics"
0270          ]
0271       if checkymumuconstr:
0272          optstocheck=[
0273             "TBDsel"
0274          ]
0275       self.doCheckOptions(optstocheck)
0276