CMS 3D CMS Logo

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