CMS 3D CMS Logo

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