CMS 3D CMS Logo

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