CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
geometryComparison.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 import os
3 import configparser as 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 
10 class GeometryComparison(GenericValidation):
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": "./CREATE_NEW/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  "multiIOV":"False",
41  }
42  mandatories = {"levels", "dbOutput"}
43  valType = "compare"
44  def __init__( self, valName, alignment, referenceAlignment,
45  config, copyImages = True):
46  """
47  Constructor of the GeometryComparison class.
48 
49  Arguments:
50  - `valName`: String which identifies individual validation instances
51  - `alignment`: `Alignment` instance to validate
52  - `referenceAlignment`: `Alignment` instance which is compared
53  with `alignment`
54  - `config`: `BetterConfigParser` instance which includes the
55  configuration of the validations
56  - `copyImages`: Boolean which indicates whether png- and pdf-files
57  should be copied back from the batch farm
58  """
59  super(GeometryComparison, self).__init__(valName, alignment, config)
60  self.referenceAlignment = referenceAlignment
61  referenceName = "IDEAL"
62  if not self.referenceAlignment == "IDEAL":
63  referenceName = self.referenceAlignment.name
64 
65  allCompares = config.getCompares()
66  self.__compares = {}
67  self.__filesToCompare = {}
68  if valName in allCompares:
69  self.__compares[valName] = allCompares[valName]
70  else:
71  msg = ("Could not find compare section '%s' in '%s'"
72  %(valName, allCompares))
73  raise AllInOneError(msg)
74  self.copyImages = copyImages
75 
76  for name in "useDefaultRange", "plotOnlyGlobal", "plotPng":
77  self.general[name] = cppboolstring(self.general[name], name)
78 
79 
80  def getRepMap(self, alignment = None):
81  if alignment == None:
82  alignment = self.alignmentToValidate
83  repMap = super(GeometryComparison, self).getRepMap( alignment )
84  referenceName = "IDEAL"
85  referenceTitle = "IDEAL"
86  if not self.referenceAlignment == "IDEAL":
87  referenceName = self.referenceAlignment.name
88  referenceTitle = self.referenceAlignment.title
89 
90  assert len(self.__compares) == 1 #? not sure how it can be anything else, but just in case
91  common = list(self.__compares.keys())[0]
92 
93  repMap.update({
94  "common": clean_name(common),
95  "comparedGeometry": (".oO[alignmentName]Oo."
96  "ROOTGeometry.root"),
97  "referenceGeometry": "IDEAL", # will be replaced later
98  # if not compared to IDEAL
99  "reference": clean_name(referenceName),
100  "referenceTitle": referenceTitle,
101  "alignmentTitle": self.alignmentToValidate.title,
102  "moduleListBase": os.path.basename(repMap["moduleList"]),
103  })
104  if not referenceName == "IDEAL":
105  repMap["referenceGeometry"] = (".oO[reference]Oo."
106  "ROOTGeometry.root")
107  repMap["name"] += "_vs_.oO[reference]Oo."
108  return repMap
109 
110  @property
111  def filesToCompare(self):
112  return self.__filesToCompare
113 
114  def createConfiguration(self, path ):
115  # self.__compares
116  repMap = self.getRepMap()
117  cfgFileName = "TkAlCompareToNTuple.%s_cfg.py"%(
118  self.alignmentToValidate.name)
119  cfgs = {cfgFileName: configTemplates.intoNTuplesTemplate}
120  repMaps = {cfgFileName: repMap}
121  if not self.referenceAlignment == "IDEAL":
122  referenceRepMap = self.getRepMap( self.referenceAlignment )
123  cfgFileName = "TkAlCompareToNTuple.%s_cfg.py"%(
124  self.referenceAlignment.name )
125  cfgs[cfgFileName] = configTemplates.intoNTuplesTemplate
126  repMaps[cfgFileName] = referenceRepMap
127 
128  cfgSchedule = list(cfgs.keys())
129  for common in self.__compares:
130  repMap.update({
131  "levels": self.__compares[common][0],
132  "dbOutput": pythonboolstring(self.__compares[common][1], "dbOutput")
133  })
134  if self.__compares[common][1].split()[0] == "true":
135  repMap["dbOutputService"] = configTemplates.dbOutputTemplate
136  else:
137  repMap["dbOutputService"] = ""
138  cfgName = replaceByMap(("TkAlCompareCommon.oO[common]Oo.."
139  ".oO[name]Oo._cfg.py"),repMap)
140  cfgs[cfgName] = configTemplates.compareTemplate
141  repMaps[cfgName] = repMap
142 
143  cfgSchedule.append( cfgName )
144  super(GeometryComparison, self).createConfiguration(cfgs, path, cfgSchedule, repMaps = repMaps)
145 
146  def createScript(self, path):
147  repMap = self.getRepMap()
148  repMap["runComparisonScripts"] = ""
149  scriptName = replaceByMap(("TkAlGeomCompare.%s..oO[name]Oo..sh"
150  %self.name), repMap)
151 
152  y_ranges = ""
153  plottedDifferences = ["dx","dy","dz","dr","rdphi","dalpha","dbeta","dgamma"]
154  for diff in plottedDifferences:
155  y_ranges += ","+repMap["%s_min"%diff]
156  y_ranges += ","+repMap["%s_max"%diff]
157 
158  for name in self.__compares:
159  if '"DetUnit"' in self.__compares[name][0].split(","):
160  repMap["outputFile"] = (".oO[name]Oo..Comparison_common"+name+".root")
161  repMap["nIndex"] = ("")
162  repMap["runComparisonScripts"] += \
163  ("cp .oO[Alignment/OfflineValidation]Oo."
164  "/scripts/comparisonScript.C .\n"
165  "cp .oO[Alignment/OfflineValidation]Oo."
166  "/scripts/GeometryComparisonPlotter.h .\n"
167  "cp .oO[Alignment/OfflineValidation]Oo."
168  "/scripts/GeometryComparisonPlotter.cc .\n"
169  "root -b -q 'comparisonScript.C+(\""
170  ".oO[name]Oo..Comparison_common"+name+".root\",\""
171  "./\",\".oO[modulesToPlot]Oo.\",\".oO[alignmentName]Oo.\",\".oO[reference]Oo.\",.oO[useDefaultRange]Oo.,.oO[plotOnlyGlobal]Oo.,.oO[plotPng]Oo.,.oO[makeProfilePlots]Oo."+y_ranges+")'\n"
172  "cp "+path+"/TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C .\n"
173  "root -l -b -q TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C+\n")
174  if self.copyImages:
175  repMap["runComparisonScripts"] += \
176  ("mkdir -p .oO[datadir]Oo./.oO[name]Oo."
177  ".Comparison_common"+name+"_Images/Translations\n")
178  repMap["runComparisonScripts"] += \
179  ("mkdir -p .oO[datadir]Oo./.oO[name]Oo."
180  ".Comparison_common"+name+"_Images/Rotations\n")
181 
182 
183  ### At the moment translations are images with suffix _1 and _2, rotations _3 and _4
184  ### The numeration depends on the order of the MakePlots(x, y) commands in comparisonScript.C
185  ### If comparisonScript.C is changed, check if the following lines need to be changed as well
186 
187  if repMap["plotPng"] == "true":
188  repMap["runComparisonScripts"] += \
189  ("find . -maxdepth 1 -name \"*_1*\" "
190  "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
191  "/.oO[name]Oo..Comparison_common"+name+"_Images/Translations/\" \n")
192  repMap["runComparisonScripts"] += \
193  ("find . -maxdepth 1 -name \"*_2*\" "
194  "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
195  "/.oO[name]Oo..Comparison_common"+name+"_Images/Translations/\" \n")
196 
197  repMap["runComparisonScripts"] += \
198  ("find . -maxdepth 1 -name \"*_3*\" "
199  "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
200  "/.oO[name]Oo..Comparison_common"+name+"_Images/Rotations/\" \n")
201  repMap["runComparisonScripts"] += \
202  ("find . -maxdepth 1 -name \"*_4*\" "
203  "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
204  "/.oO[name]Oo..Comparison_common"+name+"_Images/Rotations/\" \n")
205 
206  else:
207  repMap["runComparisonScripts"] += \
208  ("find . -maxdepth 1 -name \"*_1*\" "
209  "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
210  "/.oO[name]Oo..Comparison_common"+name+"_Images/Translations/\" \n")
211 
212  repMap["runComparisonScripts"] += \
213  ("find . -maxdepth 1 -name \"*_2*\" "
214  "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
215  "/.oO[name]Oo..Comparison_common"+name+"_Images/Rotations/\" \n")
216 
217  repMap["runComparisonScripts"] += \
218  ("find . -maxdepth 1 -name "
219  "\"*.tex\" -print | xargs -I {} bash -c"
220  " \"cp {} .oO[datadir]Oo./.oO[name]Oo."
221  ".Comparison_common"+name+"_Images/\" \n")
222  repMap["runComparisonScripts"] += \
223  ("find . -maxdepth 1 -name "
224  "\"TkMap_SurfDeform*.pdf\" -print | xargs -I {} bash -c"
225  " \"cp {} .oO[datadir]Oo./.oO[name]Oo."
226  ".Comparison_common"+name+"_Images/\" \n")
227  repMap["runComparisonScripts"] += \
228  ("find . -maxdepth 1 -name "
229  "\"TkMap_SurfDeform*.png\" -print | xargs -I {} bash -c"
230  " \"cp {} .oO[datadir]Oo./.oO[name]Oo."
231  ".Comparison_common"+name+"_Images/\" \n")
232  repMap["runComparisonScripts"] += \
233  ("cp .oO[Alignment/OfflineValidation]Oo."
234  "/macros/makeArrowPlots.C "
235  ".\n"
236  "root -b -q 'makeArrowPlots.C(\""
237  ".oO[name]Oo..Comparison_common"+name
238  +".root\",\".oO[name]Oo.."
239  +name+"_ArrowPlots\")'\n")
240  repMap["runComparisonScripts"] += \
241  ("mkdir -p .oO[datadir]Oo./.oO[name]Oo."
242  ".Comparison_common"+name+"_Images/ArrowPlots\n")
243  repMap["runComparisonScripts"] += \
244  ("find .oO[name]Oo.."+name+"_ArrowPlots "
245  "-maxdepth 1 -name \"*.png\" -print | xargs -I {} bash "
246  "-c \"cp {} .oO[datadir]Oo./.oO[name]Oo."
247  ".Comparison_common"+name+"_Images/ArrowPlots\"\n")
248  repMap["runComparisonScripts"] += \
249  ("find .oO[name]Oo.."+name+"_ArrowPlots "
250  "-maxdepth 1 -name \"*.pdf\" -print | xargs -I {} bash "
251  "-c \"cp {} .oO[datadir]Oo./.oO[name]Oo."
252  ".Comparison_common"+name+"_Images/ArrowPlots\"\n")
253  repMap["runComparisonScripts"] += \
254  ("find . "
255  "-maxdepth 1 -name \".oO[common]Oo._.oO[name]Oo..Visualization_rotated.gif\" -print | xargs -I {} bash "
256  "-c \"cp {} .oO[datadir]Oo./.oO[name]Oo."
257  ".Comparison_common"+name+"_Images/.oO[common]Oo._.oO[name]Oo..Visualization.gif\"\n")
258 
259  # TkAlMap inFile=tree.root compAl=UL2018 refAl=StartGeom savePNG=True TkVersion=phase1 outDir=./test_plots/tanh colPal=2
260  range_str = ''
261  plottedDifferences = ["dx","dy","dz","dr","rdphi","dalpha","dbeta","dgamma"]
262  for diff in plottedDifferences:
263  range_str += diff+'_range=['+str(repMap[diff+'_min'])+','+str(repMap[diff+'_max'])+'];'
264  repMap["runComparisonScripts"] += \
265  ("mkdir -p .oO[datadir]Oo./.oO[name]Oo."
266  ".Comparison_common"+name+"_Images/TkAlMapPlots\n")
267  repMap["runComparisonScripts"] += \
268  ("python .oO[Alignment/OfflineValidation]Oo./python/runGCPTkAlMap.py -b "
269  "inFile=.oO[name]Oo..Comparison_common"+name+".root "
270  "refAl=\".oO[reference]Oo.\" "
271  "compAl=\".oO[alignmentName]Oo.\" "
272  "savePNG=True "
273  "TkVersion=\"phase0\" "
274  "colPal=2 "
275  "defRanges=\""+range_str+"\" "
276  "outDir=.oO[datadir]Oo./.oO[name]Oo..Comparison_common"+name+"_Images/TkAlMapPlots\n")
277  #"outDir=.oO[name]Oo.."+name+"_TkMapPlots "
278  #"useDefaultRanges=.oO[useDefaultRange]Oo. "+range_str+"\n")
279 
280  # Copy root file for check
281  repMap["runComparisonScripts"] += \
282  ("cp .oO[name]Oo..Comparison_common"+name+".root "
283  ".oO[datadir]Oo./.oO[name]Oo..Comparison_common"+name+"_Images/TkAlMapPlots/GCP.root\n")
284  #repMap["runComparisonScripts"] += \
285  # ("cp .oO[alignmentName]Oo.ROOTGeometry.root "
286  # ".oO[datadir]Oo./.oO[name]Oo..Comparison_common"+name+"_Images/TkAlMapPlots/comparedGeometry.root\n")
287 
288  resultingFile = replaceByMap(("/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./compared%s_"
289  ".oO[name]Oo..root"%name), repMap)
290  resultingFile = os.path.expandvars( resultingFile )
291  resultingFile = os.path.abspath( resultingFile )
292  resultingFile = "root://eoscms//eos/cms" + resultingFile #needs to be AFTER abspath so that it doesn't eat the //
293  self.__filesToCompare[ name ] = resultingFile
294 
295  else:
296  raise AllInOneError("Need to have DetUnit in levels!")
297 
298  repMap["CommandLine"]=""
299  repMap["CommandLine"]+= \
300  "# copy module list required for comparison script \n"
301  if repMap["moduleList"].startswith("/store"):
302  repMap["CommandLine"]+= \
303  "xrdcp root://eoscms//eos/cms.oO[moduleList]Oo. .\n"
304  elif repMap["moduleList"].startswith("root://"):
305  repMap["CommandLine"]+= \
306  "xrdcp .oO[moduleList]Oo. .\n"
307  elif repMap["moduleList"].startswith("./CREATE_NEW/"):
308  repMap["CommandLine"]+= \
309  "touch .oO[moduleListBase]Oo.\n"
310  else:
311  repMap["CommandLine"]+= \
312  "cp .oO[moduleList]Oo. .\n"
313 
314  try:
315  getCommandOutput2(replaceByMap("cd $(mktemp -d)\n.oO[CommandLine]Oo.\ncat .oO[moduleListBase]Oo.", repMap))
316  except RuntimeError:
317  raise AllInOneError(replaceByMap(".oO[moduleList]Oo. does not exist!", repMap))
318 
319  for cfg in self.configFiles:
320  # FIXME: produce this line only for enabled dbOutput
321  # postProcess = "cp .oO[workdir]Oo./*.db .oO[datadir]Oo.\n"
322  # postProcess = "cp *.db .oO[datadir]Oo.\n"
323  postProcess = ""
324  repMap["CommandLine"]+= \
325  repMap["CommandLineTemplate"]%{"cfgFile":cfg,
326  "postProcess":postProcess}
327  repMap["CommandLine"]+= ("# overall postprocessing\n"
328  ".oO[runComparisonScripts]Oo.\n"
329  )
330 
331  #~ print configTemplates.scriptTemplate
332  scripts = {scriptName: replaceByMap( configTemplates.scriptTemplate, repMap )}
333  files = {replaceByMap("TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C", repMap ): replaceByMap(configTemplates.visualizationTrackerTemplate, repMap )}
334  self.createFiles(files, path)
335  return super(GeometryComparison, self).createScript(scripts, path)
336 
337  def createCrabCfg(self, path):
338  msg = ("Parallelization not supported for geometry comparison. Please "
339  "choose another 'jobmode'.")
340  raise AllInOneError(msg)
def replaceByMap
— Helpers —############################
#define str(s)