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 
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 
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 
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 
LumiList.LumiList
Definition: LumiList.py:22
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
HipPyOptionParser.mergeVPSets
def mergeVPSets(inVPSet, overrideVPSet, matchrule=None)
Definition: HipPyOptionParser.py:32
HipPyOptionParser.insertValToPSet
def insertValToPSet(name, val, thePSet)
Definition: HipPyOptionParser.py:13
digitizers_cfi.strip
strip
Definition: digitizers_cfi.py:19
HipPyOptionParser.HipPyOptionParser.useTrkSplittingInCosmics
useTrkSplittingInCosmics
Definition: HipPyOptionParser.py:91
HipPyOptionParser.insertPSetToVPSet
def insertPSetToVPSet(inPSet, outVPSet)
Definition: HipPyOptionParser.py:20
HipPyOptionParser.insertPSetToPSet
def insertPSetToPSet(inPSet, outPSet)
Definition: HipPyOptionParser.py:16
HipPyOptionParser.HipPyOptionParser.TBDsel
TBDsel
Definition: HipPyOptionParser.py:215
HipPyOptionParser.HipPyOptionParser.trkcoll
trkcoll
Definition: HipPyOptionParser.py:78
HipPyOptionParser.HipPyOptionParser.getTrackDefaults
def getTrackDefaults(self)
Definition: HipPyOptionParser.py:76
HipPyOptionParser.HipPyOptionParser.doCheckOptions
def doCheckOptions(self, optstocheck)
Definition: HipPyOptionParser.py:252
HipPyOptionParser.matchPSetsByRecord
def matchPSetsByRecord(ps1, ps2)
Definition: HipPyOptionParser.py:24
HipPyOptionParser.HipPyOptionParser.APVmode
APVmode
Options for cosmics Get APV mode.
Definition: HipPyOptionParser.py:231
HipPyOptionParser.HipPyOptionParser.interpretOptions
def interpretOptions(self)
Definition: HipPyOptionParser.py:113
HipPyOptionParser.HipPyOptionParser.rawopt
rawopt
Definition: HipPyOptionParser.py:64
HipPyOptionParser.HipPyOptionParser
Definition: HipPyOptionParser.py:60
submitPVValidationJobs.split
def split(sequence, size)
Definition: submitPVValidationJobs.py:352
HipPyOptionParser.HipPyOptionParser.flag
flag
Definition: HipPyOptionParser.py:63
str
#define str(s)
Definition: TestProcessor.cc:52
HipPyOptionParser.HipPyOptionParser.CPEtype
CPEtype
Definition: HipPyOptionParser.py:68
HipPyOptionParser.HipPyOptionParser.GTtoGet
GTtoGet
Definition: HipPyOptionParser.py:247
HipPyOptionParser.HipPyOptionParser.GlobalTag
GlobalTag
Definition: HipPyOptionParser.py:121
HipPyOptionParser.getPSetDict
def getPSetDict(thePSet)
Definition: HipPyOptionParser.py:10
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
mps_setup.append
append
Definition: mps_setup.py:85
createfilelist.int
int
Definition: createfilelist.py:10
HipPyOptionParser.HipPyOptionParser.TBDconstraint
TBDconstraint
Get options common in min.
Definition: HipPyOptionParser.py:223
HipPyOptionParser.HipPyOptionParser.LumiJSON
LumiJSON
Definition: HipPyOptionParser.py:181
HipPyOptionParser.HipPyOptionParser.checkOptions
def checkOptions(self)
Definition: HipPyOptionParser.py:262
HipPyOptionParser.HipPyOptionParser.datatype
datatype
Definition: HipPyOptionParser.py:67
HipPyOptionParser.HipPyOptionParser.__init__
def __init__(self, strflag, stropt)
Definition: HipPyOptionParser.py:61
HipPyOptionParser.HipPyOptionParser.applyPVConstraint
applyPVConstraint
Definition: HipPyOptionParser.py:208
HipPyOptionParser.HipPyOptionParser.hitfiltercommands
hitfiltercommands
Definition: HipPyOptionParser.py:166
HipPyOptionParser.HipPyOptionParser.overallweight
overallweight
Definition: HipPyOptionParser.py:193
HipPyOptionParser.HipPyOptionParser.Bfield
Bfield
Definition: HipPyOptionParser.py:79
format
pileupCalc.upper
upper
Definition: pileupCalc.py:214
HipPyOptionParser.HipPyOptionParser.PVtype
PVtype
Options for mMin.
Definition: HipPyOptionParser.py:204
HipPyOptionParser.parseBoolString
def parseBoolString(theString)
Definition: HipPyOptionParser.py:48
HipPyOptionParser.isGoodEntry
def isGoodEntry(var)
Definition: HipPyOptionParser.py:51
HipPyOptionParser.HipPyOptionParser.uniformetaformula
uniformetaformula
Definition: HipPyOptionParser.py:198
HipPyOptionParser.HipPyOptionParser.parseOptions
def parseOptions(self)
Definition: HipPyOptionParser.py:99
python.rootplot.root2matplotlib.replace
def replace(string, replacements)
Definition: root2matplotlib.py:444
HipPyOptionParser.HipPyOptionParser.optdict
optdict
Definition: HipPyOptionParser.py:65