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, clean_name
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":"true",
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": clean_name(common),
93  "comparedGeometry": (".oO[alignmentName]Oo."
94  "ROOTGeometry.root"),
95  "referenceGeometry": "IDEAL", # will be replaced later
96  # if not compared to IDEAL
97  "reference": clean_name(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  ("rfcp .oO[Alignment/OfflineValidation]Oo."
235  "/macros/makeArrowPlots.C "
236  ".\n"
237  "root -b -q 'makeArrowPlots.C(\""
238  ".oO[name]Oo..Comparison_common"+name
239  +".root\",\".oO[name]Oo.."
240  +name+"_ArrowPlots\")'\n")
241  repMap["runComparisonScripts"] += \
242  ("rfmkdir -p .oO[datadir]Oo./.oO[name]Oo."
243  ".Comparison_common"+name+"_Images/ArrowPlots\n")
244  repMap["runComparisonScripts"] += \
245  ("find .oO[name]Oo.."+name+"_ArrowPlots "
246  "-maxdepth 1 -name \"*.png\" -print | xargs -I {} bash "
247  "-c \"rfcp {} .oO[datadir]Oo./.oO[name]Oo."
248  ".Comparison_common"+name+"_Images/ArrowPlots\"\n")
249  repMap["runComparisonScripts"] += \
250  ("find .oO[name]Oo.."+name+"_ArrowPlots "
251  "-maxdepth 1 -name \"*.pdf\" -print | xargs -I {} bash "
252  "-c \"rfcp {} .oO[datadir]Oo./.oO[name]Oo."
253  ".Comparison_common"+name+"_Images/ArrowPlots\"\n")
254  repMap["runComparisonScripts"] += \
255  ("find . "
256  "-maxdepth 1 -name \".oO[common]Oo._.oO[name]Oo..Visualization_rotated.gif\" -print | xargs -I {} bash "
257  "-c \"rfcp {} .oO[datadir]Oo./.oO[name]Oo."
258  ".Comparison_common"+name+"_Images/.oO[common]Oo._.oO[name]Oo..Visualization.gif\"\n")
259 
260  resultingFile = replaceByMap(("/store/caf/user/$USER/.oO[eosdir]Oo./compared%s_"
261  ".oO[name]Oo..root"%name), repMap)
262  resultingFile = os.path.expandvars( resultingFile )
263  resultingFile = os.path.abspath( resultingFile )
264  resultingFile = "root://eoscms//eos/cms" + resultingFile #needs to be AFTER abspath so that it doesn't eat the //
265  self.__filesToCompare[ name ] = resultingFile
266 
267  else:
268  raise AllInOneError("Need to have DetUnit in levels!")
269 
270  repMap["CommandLine"]=""
271  repMap["CommandLine"]+= \
272  "# copy module list required for comparison script \n"
273  if repMap["moduleList"].startswith("/store"):
274  repMap["CommandLine"]+= \
275  "xrdcp root://eoscms//eos/cms.oO[moduleList]Oo. .\n"
276  elif repMap["moduleList"].startswith("root://"):
277  repMap["CommandLine"]+= \
278  "xrdcp .oO[moduleList]Oo. .\n"
279  else:
280  repMap["CommandLine"]+= \
281  "rfcp .oO[moduleList]Oo. .\n"
282 
283  try:
284  getCommandOutput2(replaceByMap("cd $(mktemp -d)\n.oO[CommandLine]Oo.\ncat .oO[moduleListBase]Oo.", repMap))
285  except RuntimeError:
286  raise AllInOneError(replaceByMap(".oO[moduleList]Oo. does not exist!", repMap))
287 
288  for cfg in self.configFiles:
289  # FIXME: produce this line only for enabled dbOutput
290  # postProcess = "rfcp .oO[workdir]Oo./*.db .oO[datadir]Oo.\n"
291  # postProcess = "rfcp *.db .oO[datadir]Oo.\n"
292  postProcess = ""
293  repMap["CommandLine"]+= \
294  repMap["CommandLineTemplate"]%{"cfgFile":cfg,
295  "postProcess":postProcess}
296  repMap["CommandLine"]+= ("# overall postprocessing\n"
297  ".oO[runComparisonScripts]Oo.\n"
298  )
299 
300  #~ print configTemplates.scriptTemplate
301  scripts = {scriptName: replaceByMap( configTemplates.scriptTemplate, repMap )}
302  files = {replaceByMap("TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C", repMap ): replaceByMap(configTemplates.visualizationTrackerTemplate, repMap )}
303  self.createFiles(files, path)
304  return super(GeometryComparison, self).createScript(scripts, path)
305 
306  def createCrabCfg(self, path):
307  msg = ("Parallelization not supported for geometry comparison. Please "
308  "choose another 'jobmode'.")
309  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