test
CMS 3D CMS Logo

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