CMS 3D CMS Logo

geometryComparison.py
Go to the documentation of this file.
1 import os
2 import ConfigParser # needed for exceptions in this module
3 import configTemplates
4 from genericValidation import GenericValidation
5 from helperFunctions import replaceByMap, getCommandOutput2, cppboolstring, pythonboolstring
6 from TkAlExceptions import AllInOneError
7 
8 
10  """
11  Object representing a geometry comparison job.
12  """
13  defaults = {
14  "3DSubdetector1":"1",
15  "3DSubdetector2":"2",
16  "3DTranslationalScaleFactor":"50",
17  "modulesToPlot":"all",
18  "moduleList": "/store/caf/user/cschomak/emptyModuleList.txt",
19  "useDefaultRange":"false",
20  "plotOnlyGlobal":"false",
21  "plotPng":"true",
22  "makeProfilePlots":"true",
23  "dx_min":"-99999",
24  "dx_max":"-99999",
25  "dy_min":"-99999",
26  "dy_max":"-99999",
27  "dz_min":"-99999",
28  "dz_max":"-99999",
29  "dr_min":"-99999",
30  "dr_max":"-99999",
31  "rdphi_min":"-99999",
32  "rdphi_max":"-99999",
33  "dalpha_min":"-99999",
34  "dalpha_max":"-99999",
35  "dbeta_min":"-99999",
36  "dbeta_max":"-99999",
37  "dgamma_min":"-99999",
38  "dgamma_max":"-99999",
39  }
40  mandatories = {"levels", "dbOutput"}
41  valType = "compare"
42  def __init__( self, valName, alignment, referenceAlignment,
43  config, copyImages = True):
44  """
45  Constructor of the GeometryComparison class.
46 
47  Arguments:
48  - `valName`: String which identifies individual validation instances
49  - `alignment`: `Alignment` instance to validate
50  - `referenceAlignment`: `Alignment` instance which is compared
51  with `alignment`
52  - `config`: `BetterConfigParser` instance which includes the
53  configuration of the validations
54  - `copyImages`: Boolean which indicates whether png- and pdf-files
55  should be copied back from the batch farm
56  """
57  super(GeometryComparison, self).__init__(valName, alignment, config)
58  self.referenceAlignment = referenceAlignment
59  referenceName = "IDEAL"
60  if not self.referenceAlignment == "IDEAL":
61  referenceName = self.referenceAlignment.name
62 
63  allCompares = config.getCompares()
64  self.__compares = {}
65  self.__filesToCompare = {}
66  if valName in allCompares:
67  self.__compares[valName] = allCompares[valName]
68  else:
69  msg = ("Could not find compare section '%s' in '%s'"
70  %(valName, allCompares))
71  raise AllInOneError(msg)
72  self.copyImages = copyImages
73 
74  for name in "useDefaultRange", "plotOnlyGlobal", "plotPng":
75  self.general[name] = cppboolstring(self.general[name], name)
76 
77 
78  def getRepMap(self, alignment = None):
79  if alignment == None:
80  alignment = self.alignmentToValidate
81  repMap = super(GeometryComparison, self).getRepMap( alignment )
82  referenceName = "IDEAL"
83  referenceTitle = "IDEAL"
84  if not self.referenceAlignment == "IDEAL":
85  referenceName = self.referenceAlignment.name
86  referenceTitle = self.referenceAlignment.title
87 
88  assert len(self.__compares) == 1 #? not sure how it can be anything else, but just in case
89  common = self.__compares.keys()[0]
90 
91  repMap.update({
92  "common": common,
93  "comparedGeometry": (".oO[alignmentName]Oo."
94  "ROOTGeometry.root"),
95  "referenceGeometry": "IDEAL", # will be replaced later
96  # if not compared to IDEAL
97  "reference": referenceName,
98  "referenceTitle": referenceTitle,
99  "alignmentTitle": self.alignmentToValidate.title,
100  "moduleListBase": os.path.basename(repMap["moduleList"]),
101  })
102  if not referenceName == "IDEAL":
103  repMap["referenceGeometry"] = (".oO[reference]Oo."
104  "ROOTGeometry.root")
105  repMap["name"] += "_vs_.oO[reference]Oo."
106  return repMap
107 
108  @property
109  def filesToCompare(self):
110  return self.__filesToCompare
111 
112  def createConfiguration(self, path ):
113  # self.__compares
114  repMap = self.getRepMap()
115  cfgFileName = "TkAlCompareToNTuple.%s_cfg.py"%(
116  self.alignmentToValidate.name)
117  cfgs = {cfgFileName: configTemplates.intoNTuplesTemplate}
118  repMaps = {cfgFileName: repMap}
119  if not self.referenceAlignment == "IDEAL":
120  referenceRepMap = self.getRepMap( self.referenceAlignment )
121  cfgFileName = "TkAlCompareToNTuple.%s_cfg.py"%(
122  self.referenceAlignment.name )
123  cfgs[cfgFileName] = configTemplates.intoNTuplesTemplate
124  repMaps[cfgFileName] = referenceRepMap
125 
126  cfgSchedule = cfgs.keys()
127  for common in self.__compares:
128  repMap.update({
129  "levels": self.__compares[common][0],
130  "dbOutput": pythonboolstring(self.__compares[common][1], "dbOutput")
131  })
132  if self.__compares[common][1].split()[0] == "true":
133  repMap["dbOutputService"] = configTemplates.dbOutputTemplate
134  else:
135  repMap["dbOutputService"] = ""
136  cfgName = replaceByMap(("TkAlCompareCommon.oO[common]Oo.."
137  ".oO[name]Oo._cfg.py"),repMap)
138  cfgs[cfgName] = configTemplates.compareTemplate
139  repMaps[cfgName] = repMap
140 
141  cfgSchedule.append( cfgName )
142  super(GeometryComparison, self).createConfiguration(cfgs, path, cfgSchedule, repMaps = repMaps)
143 
144  def createScript(self, path):
145  repMap = self.getRepMap()
146  repMap["runComparisonScripts"] = ""
147  scriptName = replaceByMap(("TkAlGeomCompare.%s..oO[name]Oo..sh"
148  %self.name), repMap)
149 
150  y_ranges = ""
151  plottedDifferences = ["dx","dy","dz","dr","rdphi","dalpha","dbeta","dgamma"]
152  for diff in plottedDifferences:
153  y_ranges += ","+repMap["%s_min"%diff]
154  y_ranges += ","+repMap["%s_max"%diff]
155 
156  for name in self.__compares:
157  if '"DetUnit"' in self.__compares[name][0].split(","):
158  repMap["outputFile"] = (".oO[name]Oo..Comparison_common"+name+".root")
159  repMap["nIndex"] = ("")
160  repMap["runComparisonScripts"] += \
161  ("rfcp .oO[Alignment/OfflineValidation]Oo."
162  "/scripts/comparisonScript.C .\n"
163  "rfcp .oO[Alignment/OfflineValidation]Oo."
164  "/scripts/GeometryComparisonPlotter.h .\n"
165  "rfcp .oO[Alignment/OfflineValidation]Oo."
166  "/scripts/GeometryComparisonPlotter.cc .\n"
167  "root -b -q 'comparisonScript.C+(\""
168  ".oO[name]Oo..Comparison_common"+name+".root\",\""
169  "./\",\".oO[modulesToPlot]Oo.\",\".oO[alignmentName]Oo.\",\".oO[reference]Oo.\",.oO[useDefaultRange]Oo.,.oO[plotOnlyGlobal]Oo.,.oO[plotPng]Oo.,.oO[makeProfilePlots]Oo."+y_ranges+")'\n"
170  "rfcp "+path+"/TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C .\n"
171  "root -l -b -q TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C+\n")
172  if self.copyImages:
173  repMap["runComparisonScripts"] += \
174  ("rfmkdir -p .oO[datadir]Oo./.oO[name]Oo."
175  ".Comparison_common"+name+"_Images\n")
176  repMap["runComparisonScripts"] += \
177  ("rfmkdir -p .oO[datadir]Oo./.oO[name]Oo."
178  ".Comparison_common"+name+"_Images/Translations\n")
179  repMap["runComparisonScripts"] += \
180  ("rfmkdir -p .oO[datadir]Oo./.oO[name]Oo."
181  ".Comparison_common"+name+"_Images/Rotations\n")
182 
183 
184  ### At the moment translations are images with suffix _1 and _2, rotations _3 and _4
185  ### The numeration depends on the order of the MakePlots(x, y) commands in comparisonScript.C
186  ### If comparisonScript.C is changed, check if the following lines need to be changed as well
187 
188  if repMap["plotPng"] == "true":
189  repMap["runComparisonScripts"] += \
190  ("find . -maxdepth 1 -name \"*_1*\" "
191  "-print | xargs -I {} bash -c \"rfcp {} .oO[datadir]Oo."
192  "/.oO[name]Oo..Comparison_common"+name+"_Images/Translations/\" \n")
193  repMap["runComparisonScripts"] += \
194  ("find . -maxdepth 1 -name \"*_2*\" "
195  "-print | xargs -I {} bash -c \"rfcp {} .oO[datadir]Oo."
196  "/.oO[name]Oo..Comparison_common"+name+"_Images/Translations/\" \n")
197 
198  repMap["runComparisonScripts"] += \
199  ("find . -maxdepth 1 -name \"*_3*\" "
200  "-print | xargs -I {} bash -c \"rfcp {} .oO[datadir]Oo."
201  "/.oO[name]Oo..Comparison_common"+name+"_Images/Rotations/\" \n")
202  repMap["runComparisonScripts"] += \
203  ("find . -maxdepth 1 -name \"*_4*\" "
204  "-print | xargs -I {} bash -c \"rfcp {} .oO[datadir]Oo."
205  "/.oO[name]Oo..Comparison_common"+name+"_Images/Rotations/\" \n")
206 
207  else:
208  repMap["runComparisonScripts"] += \
209  ("find . -maxdepth 1 -name \"*_1*\" "
210  "-print | xargs -I {} bash -c \"rfcp {} .oO[datadir]Oo."
211  "/.oO[name]Oo..Comparison_common"+name+"_Images/Translations/\" \n")
212 
213  repMap["runComparisonScripts"] += \
214  ("find . -maxdepth 1 -name \"*_2*\" "
215  "-print | xargs -I {} bash -c \"rfcp {} .oO[datadir]Oo."
216  "/.oO[name]Oo..Comparison_common"+name+"_Images/Rotations/\" \n")
217 
218  repMap["runComparisonScripts"] += \
219  ("find . -maxdepth 1 -name "
220  "\"*.tex\" -print | xargs -I {} bash -c"
221  " \"rfcp {} .oO[datadir]Oo./.oO[name]Oo."
222  ".Comparison_common"+name+"_Images/\" \n")
223  repMap["runComparisonScripts"] += \
224  ("find . -maxdepth 1 -name "
225  "\"TkMap_SurfDeform*.pdf\" -print | xargs -I {} bash -c"
226  " \"rfcp {} .oO[datadir]Oo./.oO[name]Oo."
227  ".Comparison_common"+name+"_Images/\" \n")
228  repMap["runComparisonScripts"] += \
229  ("find . -maxdepth 1 -name "
230  "\"TkMap_SurfDeform*.png\" -print | xargs -I {} bash -c"
231  " \"rfcp {} .oO[datadir]Oo./.oO[name]Oo."
232  ".Comparison_common"+name+"_Images/\" \n")
233  repMap["runComparisonScripts"] += \
234  ("if [[ $HOSTNAME = lxplus[0-9]*[.a-z0-9]* ]]\n"
235  "then\n"
236  " rfmkdir -p .oO[workdir]Oo./.oO[name]Oo.."+name
237  +"_ArrowPlots\n"
238  "else\n"
239  " mkdir -p $CWD/TkAllInOneTool/.oO[name]Oo.."+name
240  +"_ArrowPlots\n"
241  "fi\n")
242  repMap["runComparisonScripts"] += \
243  ("rfcp .oO[Alignment/OfflineValidation]Oo."
244  "/scripts/makeArrowPlots.C "
245  ".\n"
246  "root -b -q 'makeArrowPlots.C(\""
247  ".oO[name]Oo..Comparison_common"+name
248  +".root\",\".oO[name]Oo.."
249  +name+"_ArrowPlots\")'\n")
250  repMap["runComparisonScripts"] += \
251  ("rfmkdir -p .oO[datadir]Oo./.oO[name]Oo."
252  ".Comparison_common"+name+"_Images/ArrowPlots\n")
253  repMap["runComparisonScripts"] += \
254  ("find .oO[name]Oo.."+name+"_ArrowPlots "
255  "-maxdepth 1 -name \"*.png\" -print | xargs -I {} bash "
256  "-c \"rfcp {} .oO[datadir]Oo./.oO[name]Oo."
257  ".Comparison_common"+name+"_Images/ArrowPlots\"\n")
258  repMap["runComparisonScripts"] += \
259  ("find . "
260  "-maxdepth 1 -name \".oO[common]Oo._.oO[name]Oo..Visualization_rotated.gif\" -print | xargs -I {} bash "
261  "-c \"rfcp {} .oO[datadir]Oo./.oO[name]Oo."
262  ".Comparison_common"+name+"_Images/.oO[common]Oo._.oO[name]Oo..Visualization.gif\"\n")
263 
264  resultingFile = replaceByMap(("/store/caf/user/$USER/.oO[eosdir]Oo./compared%s_"
265  ".oO[name]Oo..root"%name), repMap)
266  resultingFile = os.path.expandvars( resultingFile )
267  resultingFile = os.path.abspath( resultingFile )
268  resultingFile = "root://eoscms//eos/cms" + resultingFile #needs to be AFTER abspath so that it doesn't eat the //
269  self.__filesToCompare[ name ] = resultingFile
270 
271  else:
272  raise AllInOneError("Need to have DetUnit in levels!")
273 
274  repMap["CommandLine"]=""
275  repMap["CommandLine"]+= \
276  "# copy module list required for comparison script \n"
277  if repMap["moduleList"].startswith("/store"):
278  repMap["CommandLine"]+= \
279  "xrdcp root://eoscms//eos/cms.oO[moduleList]Oo. .\n"
280  elif repMap["moduleList"].startswith("root://"):
281  repMap["CommandLine"]+= \
282  "xrdcp .oO[moduleList]Oo. .\n"
283  else:
284  repMap["CommandLine"]+= \
285  "rfcp .oO[moduleList]Oo. .\n"
286 
287  try:
288  getCommandOutput2(replaceByMap("cd $(mktemp -d)\n.oO[CommandLine]Oo.\ncat .oO[moduleListBase]Oo.", repMap))
289  except RuntimeError:
290  raise AllInOneError(replaceByMap(".oO[moduleList]Oo. does not exist!", repMap))
291 
292  for cfg in self.configFiles:
293  # FIXME: produce this line only for enabled dbOutput
294  # postProcess = "rfcp .oO[workdir]Oo./*.db .oO[datadir]Oo.\n"
295  # postProcess = "rfcp *.db .oO[datadir]Oo.\n"
296  postProcess = ""
297  repMap["CommandLine"]+= \
298  repMap["CommandLineTemplate"]%{"cfgFile":cfg,
299  "postProcess":postProcess}
300  repMap["CommandLine"]+= ("# overall postprocessing\n"
301  ".oO[runComparisonScripts]Oo.\n"
302  )
303 
304  #~ print configTemplates.scriptTemplate
305  scripts = {scriptName: replaceByMap( configTemplates.scriptTemplate, repMap )}
306  files = {replaceByMap("TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C", repMap ): replaceByMap(configTemplates.visualizationTrackerTemplate, repMap )}
307  self.createFiles(files, path)
308  return super(GeometryComparison, self).createScript(scripts, path)
309 
310  def createCrabCfg(self, path):
311  msg = ("Parallelization not supported for geometry comparison. Please "
312  "choose another 'jobmode'.")
313  raise AllInOneError(msg)
def pythonboolstring(string, name)
def getCommandOutput2(command)
def cppboolstring(string, name)
def replaceByMap(target, the_map)
— Helpers —############################
def createFiles(self, fileContents, path, repMap=None, repMaps=None)
def __init__(self, valName, alignment, referenceAlignment, config, copyImages=True)
def getRepMap(self, alignment=None)
double split
Definition: MVATrainer.cc:139