CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Public Member Functions | Public Attributes
generateGeometry.GeometryGenerator Class Reference
Inheritance diagram for generateGeometry.GeometryGenerator:

Public Member Functions

def __init__
 
def generateGeom
 
def run
 

Public Attributes

 allDicts
 
 dd4hepDetectorVersion
 
 deprecatedDets
 
 deprecatedSubdets
 
 detectorPrefix
 
 detectorVersionDefault
 
 detectorVersionDict
 
 detectorVersionNull
 
 detectorVersionType
 
 detectorYear
 
 maxSections
 
 scriptName
 

Detailed Description

Definition at line 16 of file generateGeometry.py.

Constructor & Destructor Documentation

def generateGeometry.GeometryGenerator.__init__ (   self,
  scriptName,
  detectorVersionDefault,
  detectorPrefix,
  detectorYear,
  maxSections,
  allDicts,
  detectorVersionDict,
  deprecatedDets = None,
  deprecatedSubdets = None,
  detectorVersionType = int 
)

Definition at line 17 of file generateGeometry.py.

17 
18  def __init__(self, scriptName, detectorVersionDefault, detectorPrefix, detectorYear, maxSections, allDicts, detectorVersionDict, deprecatedDets = None, deprecatedSubdets = None, detectorVersionType = int):
19  self.scriptName = scriptName
20  self.detectorVersionDefault = detectorVersionDefault
21  self.detectorPrefix = detectorPrefix
22  self.detectorYear = detectorYear
23  self.maxSections = maxSections
24  self.allDicts = allDicts
25  self.detectorVersionDict = detectorVersionDict
26  self.deprecatedDets = deprecatedDets
27  self.deprecatedSubdets = deprecatedSubdets
28  self.detectorVersionType = detectorVersionType
30  self.dd4hepDetectorVersion = 91 #If detectorVersion > 91, add warning to add also DD4hep in Configuration/StandardSequences/python/GeometryConf.py

Member Function Documentation

def generateGeometry.GeometryGenerator.generateGeom (   self,
  detectorTuple,
  args 
)

Definition at line 31 of file generateGeometry.py.

References generateGeometry.GeometryGenerator.allDicts, generateGeometry.GeometryGenerator.dd4hepDetectorVersion, generateGeometry.GeometryGenerator.deprecatedDets, generateGeometry.GeometryGenerator.deprecatedSubdets, generateGeometry.GeometryGenerator.detectorPrefix, generateGeometry.GeometryGenerator.detectorVersionDefault, generateGeometry.GeometryGenerator.detectorVersionDict, generateGeometry.GeometryGenerator.detectorVersionNull, generateGeometry.GeometryGenerator.detectorYear, join(), relativeConstraints.keys, generateGeometry.GeometryGenerator.maxSections, print(), sistrip::SpyUtilities.range(), python.rootplot.root2matplotlib.replace(), generateGeometry.GeometryGenerator.scriptName, and str.

Referenced by generateGeometry.GeometryGenerator.run().

31 
32  def generateGeom(self, detectorTuple, args):
33  detectorVersion = self.detectorPrefix+str(args.detectorVersionManual)
34  # reverse dict search if overall D# specified
35  if args.v_detector!=self.detectorVersionNull:
36  detectorVersion = self.detectorPrefix+str(args.v_detector)
37  if detectorVersion in self.detectorVersionDict.values():
38  detectorTuple = list(self.detectorVersionDict.keys())[list(self.detectorVersionDict.values()).index(detectorVersion)]
39  else:
40  print("Unknown detector "+detectorVersion)
41  sys.exit(1)
42  elif detectorTuple in self.detectorVersionDict.keys():
43  detectorVersion = self.detectorVersionDict[detectorTuple]
44  else:
45  if not args.doTest: print("Detector "+str(detectorTuple)+" not found in dictionary, using "+("default" if args.detectorVersionManual==self.detectorVersionDefault else "provided")+" version number "+str(detectorVersion))
46 
47  # check for deprecation
48  if self.deprecatedDets is not None and detectorVersion in self.deprecatedDets:
49  print("Error: "+detectorVersion+" is deprecated and cannot be used.")
50  sys.exit(1)
51  if self.deprecatedSubdets is not None:
52  for subdet in detectorTuple:
53  if subdet in self.deprecatedSubdets:
54  print("Error: "+subdet+" is deprecated and cannot be used.")
55  sys.exit(1)
56 
57  # create output files
58  xmlName = "cmsExtendedGeometry"+self.detectorYear+detectorVersion+"XML_cfi.py"
59  xmlDD4hepName = "cmsExtendedGeometry"+self.detectorYear+detectorVersion+".xml"
60  simName = "GeometryExtended"+self.detectorYear+detectorVersion+"_cff.py"
61  simDD4hepName = "GeometryDD4hepExtended"+self.detectorYear+detectorVersion+"_cff.py"
62  recoName = "GeometryExtended"+self.detectorYear+detectorVersion+"Reco_cff.py"
63  recoDD4hepName = "GeometryDD4hepExtended"+self.detectorYear+detectorVersion+"Reco_cff.py"
64 
65  # check directories
66  CMSSWBASE = os.getenv("CMSSW_BASE")
67  CMSSWRELBASE = os.getenv("CMSSW_RELEASE_BASE")
68  if CMSSWBASE is None: CMSSWBASE = ""
69  xmlDir = os.path.join(CMSSWBASE,"src","Geometry","CMSCommonData","python")
70  xmlDD4hepDir = os.path.join(CMSSWBASE,"src","Geometry","CMSCommonData","data","dd4hep")
71  simrecoDir = os.path.join(CMSSWBASE,"src","Configuration","Geometry","python")
72  simrecoDD4hepDir = os.path.join(CMSSWBASE,"src","Configuration","Geometry","python")
73  if args.doTest:
74  if not os.path.isdir(xmlDir):
75  xmlDir = os.path.join(CMSSWRELBASE,"src","Geometry","CMSCommonData","python")
76  xmlDD4hepDir = os.path.join(CMSSWRELBASE,"src","Geometry","CMSCommonData","data","dd4hep")
77  else:
78  mvCommands = ""
79  if not os.path.isdir(xmlDir):
80  mvCommands += "mv "+xmlName+" "+xmlDir+"/\n"
81  else:
82  xmlName = os.path.join(xmlDir,xmlName)
83  if not os.path.isdir(xmlDD4hepDir):
84  mvCommands += "mv "+xmlDD4hepName+" "+xmlDD4hepDir+"/\n"
85  else:
86  xmlDD4hepName = os.path.join(xmlDD4hepDir,xmlDD4hepName)
87  if not os.path.isdir(simrecoDir):
88  mvCommands += "mv "+simName+" "+simrecoDir+"/\n"
89  mvCommands += "mv "+recoName+" "+simrecoDir+"/\n"
90  else:
91  simName = os.path.join(simrecoDir,simName)
92  recoName = os.path.join(simrecoDir,recoName)
93  if not os.path.isdir(simrecoDD4hepDir):
94  mvCommands += "mv "+simDD4hepName+" "+simrecoDD4hepDir+"/\n"
95  mvCommands += "mv "+recoDD4hepName+" "+simrecoDD4hepDir+"/\n"
96  else:
97  simDD4hepName = os.path.join(simrecoDD4hepDir,simDD4hepName)
98  recoDD4hepName = os.path.join(simrecoDD4hepDir,recoDD4hepName)
99  if len(mvCommands)>0:
100  print("Warning: some geometry packages not checked out.\nOnce they are available, please execute the following commands manually:\n"+mvCommands)
101 
102  # open files
103  xmlFile = open(xmlName,'w')
104  xmlDD4hepFile = open(xmlDD4hepName,'w')
105  simFile = open(simName,'w')
106  simDD4hepFile = open(simDD4hepName,'w')
107  recoFile = open(recoName,'w')
108  recoDD4hepFile = open(recoDD4hepName,'w')
109 
110  # common preamble
111  preamble = "import FWCore.ParameterSet.Config as cms"+"\n"+"\n"
112  preamble += "# This config was generated automatically using "+self.scriptName+"\n"
113  preamble += "# If you notice a mistake, please update the generating script, not just this config"+"\n"+"\n"
114 
115  # create XML config
116  xmlFile.write(preamble)
117  # extra preamble
118  xmlFile.write("XMLIdealGeometryESSource = cms.ESSource(\"XMLIdealGeometryESSource\","+"\n")
119  xmlFile.write(" geomXMLFiles = cms.vstring("+"\n")
120  for section in range(1,self.maxSections+1):
121  # midamble
122  if section==2:
123  xmlFile.write(" )+"+"\n"+" cms.vstring("+"\n")
124  for iDict,aDict in enumerate(self.allDicts):
125  if section in aDict[detectorTuple[iDict]].keys():
126  xmlFile.write('\n'.join([ " '"+aLine+"'," for aLine in aDict[detectorTuple[iDict]][section] ])+"\n")
127  # postamble
128  xmlFile.write(" ),"+"\n"+" rootNodeName = cms.string('cms:OCMS')"+"\n"+")"+"\n")
129  xmlFile.close()
130 
131  # create DD4hep XML config
132  xmlDD4hepFile.write("<?xml version=\"1.0\"?>\n"+
133  "<DDDefinition>\n"+
134  " <open_geometry/>\n"+
135  " <close_geometry/>\n"+
136  "\n"+
137  " <IncludeSection>\n")
138  for section in range(1,self.maxSections+1):
139  # midamble
140  for iDict,aDict in enumerate(self.allDicts):
141  if section in aDict[detectorTuple[iDict]].keys():
142  xmlDD4hepFile.write('\n'.join([ " <Include ref='"+aLine+"'/>" for aLine in aDict[detectorTuple[iDict]][section] ])+"\n")
143  # postamble
144  xmlDD4hepFile.write(" </IncludeSection>\n"+
145  "</DDDefinition>"+"\n")
146  xmlDD4hepFile.close()
147 
148  # create sim config
149  simFile.write(preamble)
150  # always need XML
151  simFile.write("from Geometry.CMSCommonData."+os.path.basename(xmlName).replace(".py","")+" import *"+"\n")
152  for iDict,aDict in enumerate(self.allDicts):
153  if "sim" in aDict[detectorTuple[iDict]].keys():
154  simFile.write('\n'.join([ aLine for aLine in aDict[detectorTuple[iDict]]["sim"] ])+"\n")
155  simFile.close()
156 
157  # create simDD4hep config
158  simDD4hepFile.write(preamble)
159  # always need XML
160  simDD4hepFile.write("from Configuration.Geometry.GeometryDD4hep_cff"+" import *"+"\n")
161  simDD4hepFile.write("DDDetectorESProducer.confGeomXMLFiles = cms.FileInPath(\"Geometry/CMSCommonData/data/dd4hep/"+os.path.basename(xmlDD4hepName)+"\")\n\n")
162  for iDict,aDict in enumerate(self.allDicts):
163  if "sim" in aDict[detectorTuple[iDict]].keys():
164  simDD4hepFile.write('\n'.join([ aLine for aLine in aDict[detectorTuple[iDict]]["sim"] ])+"\n")
165  simDD4hepFile.close()
166 
167  # create reco config
168  recoFile.write(preamble)
169  # always need sim
170  recoFile.write("from Configuration.Geometry."+os.path.basename(simName).replace(".py","")+" import *"+"\n\n")
171  for iDict,aDict in enumerate(self.allDicts):
172  if "reco" in aDict[detectorTuple[iDict]].keys():
173  recoFile.write("# "+aDict["name"]+"\n")
174  recoFile.write('\n'.join([ aLine for aLine in aDict[detectorTuple[iDict]]["reco"] ])+"\n\n")
175  recoFile.close()
176 
177  # create recoDD4hep config
178  recoDD4hepFile.write(preamble)
179  # always need sim
180  recoDD4hepFile.write("from Configuration.Geometry."+os.path.basename(simDD4hepName).replace(".py","")+" import *"+"\n\n")
181  for iDict,aDict in enumerate(self.allDicts):
182  if "reco" in aDict[detectorTuple[iDict]].keys():
183  recoDD4hepFile.write("# "+aDict["name"]+"\n")
184  recoDD4hepFile.write('\n'.join([ aLine for aLine in aDict[detectorTuple[iDict]]["reco"] ])+"\n\n")
185  recoDD4hepFile.close()
186 
187  from Configuration.StandardSequences.GeometryConf import GeometryConf
188  if not args.doTest: # todo: include these in unit test somehow
189  # specify Era customizations
190  # must be checked manually in:
191  # Configuration/StandardSequences/python/Eras.py
192  # Configuration/Eras/python/
193  # Configuration/PyReleaseValidation/python/upgradeWorkflowComponents.py (workflow definitions)
194  eraLine = ""
195  eraLineItems = []
196  for iDict,aDict in enumerate(self.allDicts):
197  if "era" in aDict[detectorTuple[iDict]].keys():
198  eraLineItems.append(aDict[detectorTuple[iDict]]["era"])
199  eraLine += ", ".join([ eraLineItem for eraLineItem in eraLineItems ])
200  print("The Era for this detector should contain:")
201  print(eraLine)
202 
203  # specify GeometryConf
204  if not 'Extended'+self.detectorYear+detectorVersion in GeometryConf.keys():
205  print("Please add this line in Configuration/StandardSequences/python/GeometryConf.py:")
206  print(" 'Extended"+self.detectorYear+detectorVersion+"' : 'Extended"+self.detectorYear+detectorVersion+",Extended"+self.detectorYear+detectorVersion+"Reco',")
207  if self.detectorYear == "2026" and int(args.v_detector) > self.dd4hepDetectorVersion:
208  print(" 'DD4hepExtended"+self.detectorYear+detectorVersion+"' : 'DD4hepExtended"+self.detectorYear+detectorVersion+",DD4hepExtended"+self.detectorYear+detectorVersion+"Reco',")
209 
210  errorList = []
211 
212  if args.doTest:
213  # tests for Configuration/Geometry
214  simFile = os.path.join(simrecoDir,simName)
215  if not os.path.isfile(simFile):
216  errorList.append(simName+" missing")
217  elif not filecmp.cmp(simName,simFile):
218  errorList.append(simName+" differs")
219  simDD4hepFile = os.path.join(simrecoDD4hepDir,simDD4hepName)
220  if not os.path.isfile(simDD4hepFile):
221  errorList.append(simDD4hepName+" missing")
222  elif not filecmp.cmp(simDD4hepName,simDD4hepFile):
223  errorList.append(simDD4hepName+" differs")
224  recoFile = os.path.join(simrecoDir,recoName)
225  if not os.path.isfile(recoFile):
226  errorList.append(recoName+" missing")
227  elif not filecmp.cmp(recoName,recoFile):
228  errorList.append(recoName+" differs")
229  recoDD4hepFile = os.path.join(simrecoDD4hepDir,recoDD4hepName)
230  if not os.path.isfile(recoDD4hepFile):
231  errorList.append(recoDD4hepName+" missing")
232  elif not filecmp.cmp(recoDD4hepName,recoDD4hepFile):
233  errorList.append(recoDD4hepName+" differs")
234  # test for Configuration/StandardSequences
235  if not 'Extended'+self.detectorYear+detectorVersion in GeometryConf.keys():
236  errorList.append('Extended'+self.detectorYear+detectorVersion+" missing from GeometryConf")
237  # test for Geometry/CMSCommonData
238  xmlFile = os.path.join(xmlDir,xmlName)
239  if not os.path.isfile(xmlFile):
240  errorList.append(xmlName+" missing")
241  elif not filecmp.cmp(xmlName,xmlFile):
242  errorList.append(xmlName+" differs")
243  # test for dd4hep xml
244  xmlDD4hepFile = os.path.join(xmlDD4hepDir,xmlDD4hepName)
245  if not os.path.exists(xmlDD4hepFile):
246  errorList.append(xmlDD4hepName+" differs")
247  elif not filecmp.cmp(xmlDD4hepName,xmlDD4hepFile):
248  errorList.append(xmlDD4hepName+" differs")
249  return errorList
const uint16_t range(const Frame &aFrame)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
#define str(s)
def generateGeometry.GeometryGenerator.run (   self)

Definition at line 250 of file generateGeometry.py.

References generateGeometry.GeometryGenerator.allDicts, generateGeometry.GeometryGenerator.detectorVersionDefault, generateGeometry.GeometryGenerator.detectorVersionDict, generateGeometry.GeometryGenerator.detectorVersionNull, generateGeometry.GeometryGenerator.detectorVersionType, generateGeometry.GeometryGenerator.generateGeom(), join(), print(), and str.

Referenced by Types.EventID.cppID(), and Types.LuminosityBlockID.cppID().

251  def run(self):
252  # define options
253  parser = ArgumentParser(formatter_class=ArgumentDefaultsRawHelpFormatter)
254  for aDict in self.allDicts:
255  parser.add_argument("-"+aDict["abbrev"], "--"+aDict["name"], dest="v_"+aDict["name"], default=aDict["default"], type=int, help="version for "+aDict["name"])
256  parser.add_argument("-V", "--version", dest="detectorVersionManual", default=self.detectorVersionDefault, type=int, help="manual detector version number")
257  parser.add_argument("-D", "--detector", dest="v_detector", default=self.detectorVersionNull, type=self.detectorVersionType, help="version for whole detector, ignored if 0, overrides subdet versions otherwise")
258  parser.add_argument("-l", "--list", dest="doList", default=False, action="store_true", help="list known detector versions and exit")
259  parser.add_argument("-t", "--test", dest="doTest", default=False, action="store_true", help="enable unit test mode")
260  args = parser.parse_args()
261 
262  # check options
263  if args.doList and not args.doTest:
264  pprint(sorted(self.detectorVersionDict.items(),key=operator.itemgetter(1)))
265  sys.exit(0)
266  elif args.doTest:
267  # list of errors
268  errorList = []
269  # run all known possibilities
270  for detectorTuple in self.detectorVersionDict:
271  errorTmp = self.generateGeom(detectorTuple,args)
272  errorList.extend(errorTmp)
273  if len(errorList)>0:
274  print('\n'.join([anError for anError in errorList]))
275  sys.exit(1)
276  else:
277  sys.exit(0)
278  else:
279  detectorTuple = tuple([aDict["abbrev"]+str(getattr(args,"v_"+aDict["name"])) for aDict in self.allDicts])
280  self.generateGeom(detectorTuple,args)
281  sys.exit(0)
282 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
#define str(s)

Member Data Documentation

generateGeometry.GeometryGenerator.allDicts

Definition at line 23 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom(), and generateGeometry.GeometryGenerator.run().

generateGeometry.GeometryGenerator.dd4hepDetectorVersion

Definition at line 29 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom().

generateGeometry.GeometryGenerator.deprecatedDets

Definition at line 25 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom().

generateGeometry.GeometryGenerator.deprecatedSubdets

Definition at line 26 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom().

generateGeometry.GeometryGenerator.detectorPrefix

Definition at line 20 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom().

generateGeometry.GeometryGenerator.detectorVersionDefault

Definition at line 19 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom(), and generateGeometry.GeometryGenerator.run().

generateGeometry.GeometryGenerator.detectorVersionDict

Definition at line 24 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom(), and generateGeometry.GeometryGenerator.run().

generateGeometry.GeometryGenerator.detectorVersionNull

Definition at line 28 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom(), and generateGeometry.GeometryGenerator.run().

generateGeometry.GeometryGenerator.detectorVersionType

Definition at line 27 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.run().

generateGeometry.GeometryGenerator.detectorYear

Definition at line 21 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom().

generateGeometry.GeometryGenerator.maxSections

Definition at line 22 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom().

generateGeometry.GeometryGenerator.scriptName

Definition at line 18 of file generateGeometry.py.

Referenced by generateGeometry.GeometryGenerator.generateGeom().