CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
generate2023Geometry.py
Go to the documentation of this file.
1 from optparse import OptionParser
2 import sys, os, operator
3 from pprint import pprint
4 import filecmp
5 
6 from dict2023Geometry import *
7 
8 # define global
9 detectorVersionDefault = 999
10 
11 def generateGeom(detectorTuple, options):
12  doTest = bool(options.doTest)
13 
14  detectorVersion = "D"+str(options.detectorVersionManual)
15  # reverse dict search if overall D# specified
16  if options.v_detector>0:
17  detectorVersion = "D"+str(options.v_detector)
18  if detectorVersion in detectorVersionDict.values():
19  detectorTuple = detectorVersionDict.keys()[detectorVersionDict.values().index(detectorVersion)]
20  else:
21  print "Unknown detector "+detectorVersion
22  sys.exit(1)
23  elif detectorTuple in detectorVersionDict.keys():
24  detectorVersion = detectorVersionDict[detectorTuple]
25  else:
26  if not doTest: print "Detector "+str(detectorTuple)+" not found in dictionary, using "+("default" if options.detectorVersionManual==detectorVersionDefault else "provided")+" version number "+str(detectorVersion)
27 
28  # check for deprecation
29  if detectorVersion in deprecatedDets:
30  print "Error: "+detectorVersion+" is deprecated and cannot be used."
31  sys.exit(1)
32  for subdet in detectorTuple:
33  if subdet in deprecatedSubdets:
34  print "Error: "+subdet+" is deprecated and cannot be used."
35  sys.exit(1)
36 
37  # create output files
38  xmlName = "cmsExtendedGeometry2023"+detectorVersion+"XML_cfi.py"
39  simName = "GeometryExtended2023"+detectorVersion+"_cff.py"
40  recoName = "GeometryExtended2023"+detectorVersion+"Reco_cff.py"
41 
42  # check directories
43  CMSSWBASE = os.getenv("CMSSW_BASE")
44  CMSSWRELBASE = os.getenv("CMSSW_RELEASE_BASE")
45  if CMSSWBASE is None: CMSSWBASE = ""
46  xmlDir = os.path.join(CMSSWBASE,"src","Geometry","CMSCommonData","python")
47  simrecoDir = os.path.join(CMSSWBASE,"src","Configuration","Geometry","python")
48  if doTest:
49  if not os.path.isdir(xmlDir):
50  xmlDir = os.path.join(CMSSWRELBASE,"src","Geometry","CMSCommonData","python")
51  else:
52  mvCommands = ""
53  if not os.path.isdir(xmlDir):
54  mvCommands += "mv "+xmlName+" "+xmlDir+"/\n"
55  else:
56  xmlName = os.path.join(xmlDir,xmlName)
57  if not os.path.isdir(simrecoDir):
58  mvCommands += "mv "+simName+" "+simrecoDir+"/\n"
59  mvCommands += "mv "+recoName+" "+simrecoDir+"/\n"
60  else:
61  simName = os.path.join(simrecoDir,simName)
62  recoName = os.path.join(simrecoDir,recoName)
63  if len(mvCommands)>0:
64  print "Warning: some geometry packages not checked out.\nOnce they are available, please execute the following commands manually:\n"+mvCommands
65 
66  # open files
67  xmlFile = open(xmlName,'w')
68  simFile = open(simName,'w')
69  recoFile = open(recoName,'w')
70 
71  # common preamble
72  preamble = "import FWCore.ParameterSet.Config as cms"+"\n"+"\n"
73  preamble += "# This config was generated automatically using generate2023Geometry.py"+"\n"
74  preamble += "# If you notice a mistake, please update the generating script, not just this config"+"\n"+"\n"
75 
76  # create XML config
77  xmlFile.write(preamble)
78  # extra preamble
79  xmlFile.write("XMLIdealGeometryESSource = cms.ESSource(\"XMLIdealGeometryESSource\","+"\n")
80  xmlFile.write(" geomXMLFiles = cms.vstring("+"\n")
81  for section in range(1,maxsections+1):
82  # midamble
83  if section==2:
84  xmlFile.write(" )+"+"\n"+" cms.vstring("+"\n")
85  for iDict,aDict in enumerate(allDicts):
86  if section in aDict[detectorTuple[iDict]].keys():
87  xmlFile.write('\n'.join([ " '"+aLine+"'," for aLine in aDict[detectorTuple[iDict]][section] ])+"\n")
88  # postamble
89  xmlFile.write(" ),"+"\n"+" rootNodeName = cms.string('cms:OCMS')"+"\n"+")"+"\n")
90  xmlFile.close()
91 
92  # create sim config
93  simFile.write(preamble)
94  # always need XML
95  simFile.write("from Geometry.CMSCommonData."+os.path.basename(xmlName).replace(".py","")+" import *"+"\n")
96  for iDict,aDict in enumerate(allDicts):
97  if "sim" in aDict[detectorTuple[iDict]].keys():
98  simFile.write('\n'.join([ aLine for aLine in aDict[detectorTuple[iDict]]["sim"] ])+"\n")
99  simFile.close()
100 
101  # create reco config
102  recoFile.write(preamble)
103  # always need sim
104  recoFile.write("from Configuration.Geometry."+os.path.basename(simName).replace(".py","")+" import *"+"\n\n")
105  for iDict,aDict in enumerate(allDicts):
106  if "reco" in aDict[detectorTuple[iDict]].keys():
107  recoFile.write("# "+aDict["name"]+"\n")
108  recoFile.write('\n'.join([ aLine for aLine in aDict[detectorTuple[iDict]]["reco"] ])+"\n\n")
109  recoFile.close()
110 
111  from Configuration.StandardSequences.GeometryConf import GeometryConf
112  if not doTest: # todo: include these in unit test somehow
113  # specify Era customizations
114  # must be checked manually in:
115  # Configuration/StandardSequences/python/Eras.py
116  # Configuration/Eras/python/
117  # Configuration/PyReleaseValidation/python/upgradeWorkflowComponents.py (workflow definitions)
118  eraLine = ""
119  eraLineItems = []
120  for iDict,aDict in enumerate(allDicts):
121  if "era" in aDict[detectorTuple[iDict]].keys():
122  eraLineItems.append(aDict[detectorTuple[iDict]]["era"])
123  eraLine += ", ".join([ eraLineItem for eraLineItem in eraLineItems ])
124  print "The Era for this detector should contain:"
125  print eraLine
126 
127  # specify GeometryConf
128  if not 'Extended2023'+detectorVersion in GeometryConf.keys():
129  print "Please add this line in Configuration/StandardSequences/python/GeometryConf.py:"
130  print " 'Extended2023"+detectorVersion+"' : 'Extended2023"+detectorVersion+",Extended2023"+detectorVersion+"Reco',"
131 
132  errorList = []
133 
134  if doTest:
135  # tests for Configuration/Geometry
136  if not filecmp.cmp(simName,os.path.join(simrecoDir,simName)):
137  errorList.append(simName+" differs");
138  if not filecmp.cmp(recoName,os.path.join(simrecoDir,recoName)):
139  errorList.append(recoName+" differs");
140  # test for Configuration/StandardSequences
141  if not 'Extended2023'+detectorVersion in GeometryConf.keys():
142  errorList.append('Extended2023'+detectorVersion+" missing from GeometryConf")
143  # test for Geometry/CMSCommonData
144  if not filecmp.cmp(xmlName,os.path.join(xmlDir,xmlName)):
145  errorList.append(xmlName+" differs");
146  return errorList
147 
148 if __name__ == "__main__":
149  # define options
150  parser = OptionParser()
151  for aDict in allDicts:
152  parser.add_option("-"+aDict["abbrev"],"--"+aDict["name"],dest="v_"+aDict["name"],default=aDict["default"],help="version for "+aDict["name"]+" (default = %default)")
153  parser.add_option("-V","--version",dest="detectorVersionManual",default=detectorVersionDefault,help="manual detector version number (default = %default)")
154  parser.add_option("-D","--detector",dest="v_detector",default=0,help="version for whole detector, ignored if 0, overrides subdet versions otherwise (default = %default)")
155  parser.add_option("-l","--list",dest="doList",default=False,action="store_true",help="list known detector versions and exit (default = %default)")
156  parser.add_option("-t","--test",dest="doTest",default=False,action="store_true",help="enable unit test mode (default = %default)")
157  (options, args) = parser.parse_args()
158 
159  # check options
160  if options.doList and not options.doTest:
161  pprint(sorted(detectorVersionDict.items(),key=operator.itemgetter(1)))
162  sys.exit(0)
163  elif options.doTest:
164  # list of errors
165  errorList = []
166  # run all known possibilities
167  for detectorTuple in detectorVersionDict:
168  errorTmp = generateGeom(detectorTuple,options)
169  errorList.extend(errorTmp)
170  if len(errorList)>0:
171  print '\n'.join([anError for anError in errorList])
172  sys.exit(1)
173  else:
174  sys.exit(0)
175  else:
176  detectorTuple = tuple([aDict["abbrev"]+str(getattr(options,"v_"+aDict["name"])) for aDict in allDicts])
177  generateGeom(detectorTuple,options)
178  sys.exit(0)
179 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18