Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:02

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