CMS 3D CMS Logo

HipPyOptionParser.py
Go to the documentation of this file.
1 from array import array
2 from copy import copy
3 from copy import deepcopy
4 import FWCore.ParameterSet.Config as cms
5 import FWCore.PythonUtilities.LumiList as LumiList
6 
7 # Helper functions
8 def getPSetDict(thePSet):
9  return thePSet.parameters_()
10 
11 def insertValToPSet(name,val,thePSet):
12  setattr(thePSet,name,val)
13 
14 def insertPSetToPSet(inPSet, outPSet):
15  for key,val in getPSetDict(inPSet).iteritems():
16  insertValToPSet(key,val,outPSet)
17 
18 def insertPSetToVPSet(inPSet, outVPSet):
19  outVPSet.append(inPSet)
20 
21 
22 def matchPSetsByRecord(ps1, ps2):
23  if hasattr(ps1,"record") and hasattr(ps2,"record"):
24  s1=ps1.record.value()
25  s2=ps2.record.value()
26  return (s1==s2)
27  return False
28 
29 
30 def mergeVPSets(inVPSet, overrideVPSet, matchrule=None):
31  resvpset=overrideVPSet.copy()
32  for iop in inVPSet.value():
33  nomatch=True
34  if matchrule is not None:
35  for cps in overrideVPSet.value():
36  if matchrule(cps,iop):
37  nomatch=False
38  break
39  if nomatch:
40  insertPSetToVPSet(iop,resvpset)
41  return resvpset
42 
43 
44 
45 
46 def parseBoolString(theString):
47  return theString[0].upper()=='T'
48 
49 def isGoodEntry(var):
50  if (var is None):
51  return False
52  elif (var == []):
53  return False
54  else:
55  return True
56 
57 
59  def __init__(self, strflag, stropt):
60  # input file
61  self.flag=strflag.lower()
62  self.rawopt = stropt
63  self.optdict=dict()
64 
65  self.datatype=-1
66  self.CPEtype="template"
67  self.getTrackDefaults()
68 
69  if self.rawopt.lower()!="noopts":
70  self.parseOptions()
71  self.interpretOptions()
72 
73 
74  def getTrackDefaults(self):
75  if self.flag=="mbvertex":
76  self.trkcoll="ALCARECOTkAlMinBias"
77  self.Bfield="3.8t"
78  elif self.flag=="zmumu":
79  self.trkcoll="ALCARECOTkAlZMuMu"
80  self.Bfield="3.8t"
81  elif self.flag=="ymumu":
82  self.trkcoll="ALCARECOTkAlUpsilonMuMu"
83  self.Bfield="3.8t"
84  elif self.flag=="jpsimumu":
85  self.trkcoll="ALCARECOTkAlJpsiMuMu"
86  self.Bfield="3.8t"
87  elif self.flag=="cosmics":
88  self.trkcoll="ALCARECOTkAlCosmicsCTF0T"
90  elif self.flag=="cdcs":
91  self.trkcoll="ALCARECOTkAlCosmicsInCollisions"
92  self.useTrkSplittingInCosmics=False
93  else:
94  raise RuntimeError("Flag {} is unimplemented.".format(self.flag))
95 
96 
97  def parseOptions(self):
98  delimiter=' '
99  optdelimiter=':'
100  optlist=self.rawopt.split(delimiter)
101  for sopt in optlist:
102  olist=sopt.split(optdelimiter)
103  if len(olist)==2:
104  theKey=olist[0].lower()
105  theVal=olist[1]
106  self.optdict[theKey]=theVal
107  else:
108  raise RuntimeError("Option {} has an invalid number of delimiters {}".format(sopt,optdelimiter))
109 
110 
111  def interpretOptions(self):
112  gttogetpsets=[]
113  for key,val in self.optdict.iteritems():
114  # Get GT name
115  if key=="gt":
116  autofind=val.find("auto")
117  if autofind>-1:
118  val=val[0:autofind+4]+":"+val[autofind+4:]
119  self.GlobalTag = val
120  # Get GT toGet PSets
121  elif key=="gtspecs":
122  vallist=val.split(';')
123  for varset in vallist:
124  apset = cms.PSet()
125  specs = varset.split('|')
126  for spec in specs:
127  namespec=spec.split('=')
128  if len(namespec)==2:
129  tmpspec=namespec[1]
130  frontierfind=tmpspec.find("frontier")
131  sqlitefind=tmpspec.find("sqlite_file")
132  if frontierfind>-1 and sqlitefind>-1:
133  raise RuntimeError("Inconsistent setting: Cannot specify frontier and sqlite_file at the same time!")
134  elif frontierfind>-1:
135  tmpspec=tmpspec[0:frontierfind+8]+":"+tmpspec[frontierfind+8:]
136  elif sqlitefind>-1:
137  tmpspec=tmpspec[0:sqlitefind+11]+":"+tmpspec[sqlitefind+11:]
138  elif namespec[0]=="connect":
139  if tmpspec.endswith(".db"):
140  tmpspec = str("sqlite_file:")+tmpspec
141  elif tmpspec.find("//")>-1:
142  tmpspec = str("frontier:")+tmpspec
143  else:
144  tmpspec = str("frontier://")+tmpspec
145  cmsstrspec = cms.string(tmpspec)
146  insertValToPSet(namespec[0],cmsstrspec,apset)
147  else:
148  raise RuntimeError("GT specification {} does not have size==2".format(namespec))
149  gttogetpsets.append(apset)
150  # Get data type
151  elif (key=="type" or key=="datatype" or key=="datagroup"):
152  try:
153  dtype=int(val)
154  self.datatype=dtype
155  except ValueError:
156  print "Data type is not an integer"
157  # Get lumi json file
158  elif key=="lumilist":
159  self.LumiJSON = LumiList.LumiList(filename = val).getVLuminosityBlockRange()
160  # Get CPE type
161  elif key=="cpe" or key=="cpetype":
162  val=val.lower()
163  self.CPEtype=val
164  # Get non-standard track collection name
165  elif key=="trackcollection":
166  self.trkcoll=val
167  # Get overall weight. Turns reweighting on
168  elif key=="overallweight":
169  try:
170  fval=float(val)
171  self.overallweight=fval
172  except ValueError:
173  print "Overall weight is not a float"
174  # Get uniform eta formula. Turns reweighting on
175  elif key=="uniformetaformula":
177  ## Options for mMin. bias
178  # Get custom track selection for TBD
179  elif (key=="twobodytrackselection" or key=="twobodydecayselection" or key=="tbdselection"):
180  val=val.lower()
181  if (val=="zsel" or val=="y1sel"):
182  self.TBDsel=val
183  else:
184  raise ValueError("TBD selection can only be Zsel or Y1sel at this time.")
185  ## Get options common in min. bias, Zmumu and Ymumu
186  # Get TBD constraint type
187  elif (key=="twobodytrackconstraint" or key=="twobodydecayconstraint" or key=="tbdconstraint"):
188  val=val.lower()
189  if ("momconstr" in val or "fullconstr" in val):
190  self.TBDconstraint=val
191  else:
192  raise ValueError("TBD constraint can only be momconstr... or fullconstr...")
193  ## Options for cosmics
194  # Get APV mode
195  elif key=="apvmode":
196  val=val.lower()
197  if (val=="peak" or val=="deco"):
198  self.APVmode=val
199  else:
200  raise ValueError("APV mode can only be peak or deco in cosmics")
201  # Get magnetic field value
202  elif key=="bfield":
203  val=val.lower()
204  if (val=="0t" or val=="zerotesla" or val=="3.8t"):
205  self.Bfield=val
206  else:
207  raise ValueError("B field can only be 0T, ZEROTESLA or 3.8T")
208  elif key=="usetracksplitting":
210  else:
211  raise RuntimeError("Option {} is not implemented.".format(key))
212 
213  if len(gttogetpsets)>0:
214  self.GTtoGet = cms.VPSet()
215  for ps in gttogetpsets:
216  insertPSetToVPSet(ps,self.GTtoGet)
217 
218 
219  def doCheckOptions(self,optstocheck):
220  for oc in optstocheck:
221  if not hasattr(self,oc):
222  raise RuntimeError("Option {} needs to specified in {}.".format(oc, self.flag))
223 
224 
225  def checkOptions(self):
226  optstocheck=[]
227  checkcosmics=(self.flag=="cosmics" or self.flag=="cdcs")
228  checkymumuconstr=(self.flag=="ymumu" and hasattr(self, "TBDconstraint"))
229  if checkcosmics:
230  optstocheck=[
231  "Bfield",
232  "APVmode",
233  "useTrkSplittingInCosmics"
234  ]
235  if checkymumuconstr:
236  optstocheck=[
237  "TBDsel"
238  ]
239  self.doCheckOptions(optstocheck)
240 
def insertPSetToPSet(inPSet, outPSet)
def matchPSetsByRecord(ps1, ps2)
TBDconstraint
Get options common in min.
def mergeVPSets(inVPSet, overrideVPSet, matchrule=None)
def insertPSetToVPSet(inPSet, outVPSet)
def getPSetDict(thePSet)
def insertValToPSet(name, val, thePSet)
def parseBoolString(theString)
def __init__(self, strflag, stropt)
APVmode
Options for cosmics Get APV mode.