Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:45:59

0001 from __future__ import absolute_import
0002 import os
0003 import configparser as ConfigParser # needed for exceptions in this module
0004 from . import configTemplates
0005 from .genericValidation import GenericValidation
0006 from .helperFunctions import replaceByMap, getCommandOutput2, cppboolstring, pythonboolstring, clean_name
0007 from .TkAlExceptions import AllInOneError
0008 
0009 
0010 class GeometryComparison(GenericValidation):
0011     """
0012     Object representing a geometry comparison job.
0013     """
0014     defaults = {
0015         "3DSubdetector1":"1",
0016         "3DSubdetector2":"2",
0017         "3DTranslationalScaleFactor":"50",
0018         "modulesToPlot":"all",
0019         "moduleList": "./CREATE_NEW/emptyModuleList.txt",
0020         "useDefaultRange":"false",
0021         "plotOnlyGlobal":"true",
0022         "plotPng":"true",
0023         "makeProfilePlots":"true",
0024         "dx_min":"-99999",
0025         "dx_max":"-99999",
0026         "dy_min":"-99999",
0027         "dy_max":"-99999",
0028         "dz_min":"-99999",
0029         "dz_max":"-99999",
0030         "dr_min":"-99999",
0031         "dr_max":"-99999",
0032         "rdphi_min":"-99999",
0033         "rdphi_max":"-99999",
0034         "dalpha_min":"-99999",
0035         "dalpha_max":"-99999",
0036         "dbeta_min":"-99999",
0037         "dbeta_max":"-99999",
0038         "dgamma_min":"-99999",
0039         "dgamma_max":"-99999",
0040         "multiIOV":"False",
0041         }
0042     mandatories = {"levels", "dbOutput"}
0043     valType = "compare"
0044     def __init__( self, valName, alignment, referenceAlignment,
0045                   config, copyImages = True):
0046         """
0047         Constructor of the GeometryComparison class.
0048 
0049         Arguments:
0050         - `valName`: String which identifies individual validation instances
0051         - `alignment`: `Alignment` instance to validate
0052         - `referenceAlignment`: `Alignment` instance which is compared
0053                                 with `alignment`
0054         - `config`: `BetterConfigParser` instance which includes the
0055                     configuration of the validations
0056         - `copyImages`: Boolean which indicates whether png- and pdf-files
0057                         should be copied back from the batch farm
0058         """
0059         super(GeometryComparison, self).__init__(valName, alignment, config)
0060         self.referenceAlignment = referenceAlignment
0061         referenceName = "IDEAL"
0062         if not self.referenceAlignment == "IDEAL":
0063             referenceName = self.referenceAlignment.name
0064 
0065         allCompares = config.getCompares()
0066         self.__compares = {}
0067         self.__filesToCompare = {}
0068         if valName in allCompares:
0069             self.__compares[valName] = allCompares[valName]
0070         else:
0071             msg = ("Could not find compare section '%s' in '%s'"
0072                    %(valName, allCompares))
0073             raise AllInOneError(msg)
0074         self.copyImages = copyImages
0075 
0076         for name in "useDefaultRange", "plotOnlyGlobal", "plotPng":
0077             self.general[name] = cppboolstring(self.general[name], name)
0078 
0079 
0080     def getRepMap(self, alignment = None):
0081         if alignment == None:
0082             alignment = self.alignmentToValidate
0083         repMap = super(GeometryComparison, self).getRepMap( alignment )
0084         referenceName  = "IDEAL"
0085         referenceTitle = "IDEAL"
0086         if not self.referenceAlignment == "IDEAL":
0087             referenceName  = self.referenceAlignment.name
0088             referenceTitle = self.referenceAlignment.title
0089 
0090         assert len(self.__compares) == 1 #? not sure how it can be anything else, but just in case
0091         common = list(self.__compares.keys())[0]
0092 
0093         repMap.update({
0094             "common": clean_name(common),
0095             "comparedGeometry": (".oO[alignmentName]Oo."
0096                                  "ROOTGeometry.root"),
0097             "referenceGeometry": "IDEAL", # will be replaced later
0098                                           #  if not compared to IDEAL
0099             "reference": clean_name(referenceName),
0100             "referenceTitle": referenceTitle,
0101             "alignmentTitle": self.alignmentToValidate.title,
0102             "moduleListBase": os.path.basename(repMap["moduleList"]),
0103             })
0104         if not referenceName == "IDEAL":
0105             repMap["referenceGeometry"] = (".oO[reference]Oo."
0106                                            "ROOTGeometry.root")
0107         repMap["name"] += "_vs_.oO[reference]Oo."
0108         return repMap
0109 
0110     @property
0111     def filesToCompare(self):
0112         return self.__filesToCompare
0113 
0114     def createConfiguration(self, path ):
0115         # self.__compares
0116         repMap = self.getRepMap()
0117         cfgFileName = "TkAlCompareToNTuple.%s_cfg.py"%(
0118             self.alignmentToValidate.name)
0119         cfgs = {cfgFileName: configTemplates.intoNTuplesTemplate}
0120         repMaps = {cfgFileName: repMap}
0121         if not self.referenceAlignment == "IDEAL":
0122             referenceRepMap = self.getRepMap( self.referenceAlignment )
0123             cfgFileName = "TkAlCompareToNTuple.%s_cfg.py"%(
0124                 self.referenceAlignment.name )
0125             cfgs[cfgFileName] = configTemplates.intoNTuplesTemplate
0126             repMaps[cfgFileName] = referenceRepMap
0127 
0128         cfgSchedule = list(cfgs.keys())
0129         for common in self.__compares:
0130             repMap.update({
0131                            "levels": self.__compares[common][0],
0132                            "dbOutput": pythonboolstring(self.__compares[common][1], "dbOutput")
0133                            })
0134             if self.__compares[common][1].split()[0] == "true":
0135                 repMap["dbOutputService"] = configTemplates.dbOutputTemplate
0136             else:
0137                 repMap["dbOutputService"] = ""
0138             cfgName = replaceByMap(("TkAlCompareCommon.oO[common]Oo.."
0139                                     ".oO[name]Oo._cfg.py"),repMap)
0140             cfgs[cfgName] = configTemplates.compareTemplate
0141             repMaps[cfgName] = repMap
0142 
0143             cfgSchedule.append( cfgName )
0144         super(GeometryComparison, self).createConfiguration(cfgs, path, cfgSchedule, repMaps = repMaps)
0145 
0146     def createScript(self, path):
0147         repMap = self.getRepMap()
0148         repMap["runComparisonScripts"] = ""
0149         scriptName = replaceByMap(("TkAlGeomCompare.%s..oO[name]Oo..sh"
0150                                    %self.name), repMap)
0151 
0152         y_ranges = ""
0153         plottedDifferences = ["dx","dy","dz","dr","rdphi","dalpha","dbeta","dgamma"]
0154         for diff in plottedDifferences:
0155                         y_ranges += ","+repMap["%s_min"%diff]
0156                         y_ranges += ","+repMap["%s_max"%diff]
0157 
0158         for name in self.__compares:
0159             if  '"DetUnit"' in self.__compares[name][0].split(","):
0160                 repMap["outputFile"] = (".oO[name]Oo..Comparison_common"+name+".root")
0161                 repMap["nIndex"] = ("")
0162                 repMap["runComparisonScripts"] += \
0163                     ("cp .oO[Alignment/OfflineValidation]Oo."
0164                      "/scripts/comparisonScript.C .\n"
0165                      "cp .oO[Alignment/OfflineValidation]Oo."
0166                      "/scripts/GeometryComparisonPlotter.h .\n"
0167                      "cp .oO[Alignment/OfflineValidation]Oo."
0168                      "/scripts/GeometryComparisonPlotter.cc .\n"
0169                      "root -b -q 'comparisonScript.C+(\""
0170                      ".oO[name]Oo..Comparison_common"+name+".root\",\""
0171                      "./\",\".oO[modulesToPlot]Oo.\",\".oO[alignmentName]Oo.\",\".oO[reference]Oo.\",.oO[useDefaultRange]Oo.,.oO[plotOnlyGlobal]Oo.,.oO[plotPng]Oo.,.oO[makeProfilePlots]Oo."+y_ranges+")'\n"
0172                      "cp "+path+"/TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C .\n"
0173                      "root -l -b -q TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C+\n")
0174                 if  self.copyImages:
0175                    repMap["runComparisonScripts"] += \
0176                        ("mkdir -p .oO[datadir]Oo./.oO[name]Oo."
0177                         ".Comparison_common"+name+"_Images/Translations\n")
0178                    repMap["runComparisonScripts"] += \
0179                        ("mkdir -p .oO[datadir]Oo./.oO[name]Oo."
0180                         ".Comparison_common"+name+"_Images/Rotations\n")
0181 
0182 
0183                    ### At the moment translations are images with suffix _1 and _2, rotations _3 and _4
0184                    ### The numeration depends on the order of the MakePlots(x, y) commands in comparisonScript.C
0185                    ### If comparisonScript.C is changed, check if the following lines need to be changed as well
0186 
0187                    if repMap["plotPng"] == "true":
0188                            repMap["runComparisonScripts"] += \
0189                                ("find . -maxdepth 1 -name \"*_1*\" "
0190                                 "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
0191                                 "/.oO[name]Oo..Comparison_common"+name+"_Images/Translations/\" \n")
0192                            repMap["runComparisonScripts"] += \
0193                                ("find . -maxdepth 1 -name \"*_2*\" "
0194                                 "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
0195                                 "/.oO[name]Oo..Comparison_common"+name+"_Images/Translations/\" \n")
0196 
0197                            repMap["runComparisonScripts"] += \
0198                                ("find . -maxdepth 1 -name \"*_3*\" "
0199                                 "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
0200                                 "/.oO[name]Oo..Comparison_common"+name+"_Images/Rotations/\" \n")
0201                            repMap["runComparisonScripts"] += \
0202                                ("find . -maxdepth 1 -name \"*_4*\" "
0203                                 "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
0204                                 "/.oO[name]Oo..Comparison_common"+name+"_Images/Rotations/\" \n")
0205 
0206                    else:
0207                            repMap["runComparisonScripts"] += \
0208                                ("find . -maxdepth 1 -name \"*_1*\" "
0209                                 "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
0210                                 "/.oO[name]Oo..Comparison_common"+name+"_Images/Translations/\" \n")
0211 
0212                            repMap["runComparisonScripts"] += \
0213                                ("find . -maxdepth 1 -name \"*_2*\" "
0214                                 "-print | xargs -I {} bash -c \"cp {} .oO[datadir]Oo."
0215                                 "/.oO[name]Oo..Comparison_common"+name+"_Images/Rotations/\" \n")
0216 
0217                    repMap["runComparisonScripts"] += \
0218                        ("find . -maxdepth 1 -name "
0219                         "\"*.tex\" -print | xargs -I {} bash -c"
0220                         " \"cp {} .oO[datadir]Oo./.oO[name]Oo."
0221                         ".Comparison_common"+name+"_Images/\" \n")
0222                    repMap["runComparisonScripts"] += \
0223                        ("find . -maxdepth 1 -name "
0224                         "\"TkMap_SurfDeform*.pdf\" -print | xargs -I {} bash -c"
0225                         " \"cp {} .oO[datadir]Oo./.oO[name]Oo."
0226                         ".Comparison_common"+name+"_Images/\" \n")
0227                    repMap["runComparisonScripts"] += \
0228                        ("find . -maxdepth 1 -name "
0229                         "\"TkMap_SurfDeform*.png\" -print | xargs -I {} bash -c"
0230                         " \"cp {} .oO[datadir]Oo./.oO[name]Oo."
0231                         ".Comparison_common"+name+"_Images/\" \n")
0232                    repMap["runComparisonScripts"] += \
0233                        ("cp .oO[Alignment/OfflineValidation]Oo."
0234                         "/macros/makeArrowPlots.C "
0235                         ".\n"
0236                         "root -b -q 'makeArrowPlots.C(\""
0237                         ".oO[name]Oo..Comparison_common"+name
0238                         +".root\",\".oO[name]Oo.."
0239                         +name+"_ArrowPlots\")'\n")
0240                    repMap["runComparisonScripts"] += \
0241                        ("mkdir -p .oO[datadir]Oo./.oO[name]Oo."
0242                         ".Comparison_common"+name+"_Images/ArrowPlots\n")
0243                    repMap["runComparisonScripts"] += \
0244                        ("find .oO[name]Oo.."+name+"_ArrowPlots "
0245                         "-maxdepth 1 -name \"*.png\" -print | xargs -I {} bash "
0246                         "-c \"cp {} .oO[datadir]Oo./.oO[name]Oo."
0247                         ".Comparison_common"+name+"_Images/ArrowPlots\"\n")
0248                    repMap["runComparisonScripts"] += \
0249                        ("find .oO[name]Oo.."+name+"_ArrowPlots "
0250                         "-maxdepth 1 -name \"*.pdf\" -print | xargs -I {} bash "
0251                         "-c \"cp {} .oO[datadir]Oo./.oO[name]Oo."
0252                         ".Comparison_common"+name+"_Images/ArrowPlots\"\n")
0253                    repMap["runComparisonScripts"] += \
0254                        ("find . "
0255                         "-maxdepth 1 -name \".oO[common]Oo._.oO[name]Oo..Visualization_rotated.gif\" -print | xargs -I {} bash "
0256                         "-c \"cp {} .oO[datadir]Oo./.oO[name]Oo."
0257                         ".Comparison_common"+name+"_Images/.oO[common]Oo._.oO[name]Oo..Visualization.gif\"\n")
0258 
0259                    # TkAlMap inFile=tree.root compAl=UL2018 refAl=StartGeom savePNG=True TkVersion=phase1 outDir=./test_plots/tanh colPal=2
0260                    range_str = ''
0261                    plottedDifferences = ["dx","dy","dz","dr","rdphi","dalpha","dbeta","dgamma"]
0262                    for diff in plottedDifferences:
0263                        range_str += diff+'_range=['+str(repMap[diff+'_min'])+','+str(repMap[diff+'_max'])+'];'
0264                    repMap["runComparisonScripts"] += \
0265                        ("mkdir -p .oO[datadir]Oo./.oO[name]Oo."
0266                         ".Comparison_common"+name+"_Images/TkAlMapPlots\n")
0267                    repMap["runComparisonScripts"] += \
0268                        ("python .oO[Alignment/OfflineValidation]Oo./python/runGCPTkAlMap.py -b " 
0269                         "inFile=.oO[name]Oo..Comparison_common"+name+".root "
0270                         "refAl=\".oO[reference]Oo.\" " 
0271                         "compAl=\".oO[alignmentName]Oo.\" "
0272                         "savePNG=True "
0273                         "TkVersion=\"phase0\" "
0274                         "colPal=2 "
0275                         "defRanges=\""+range_str+"\" "
0276                         "outDir=.oO[datadir]Oo./.oO[name]Oo..Comparison_common"+name+"_Images/TkAlMapPlots\n")
0277                         #"outDir=.oO[name]Oo.."+name+"_TkMapPlots "
0278                         #"useDefaultRanges=.oO[useDefaultRange]Oo. "+range_str+"\n")
0279 
0280                    # Copy root file for check
0281                    repMap["runComparisonScripts"] += \
0282                        ("cp .oO[name]Oo..Comparison_common"+name+".root "
0283                         ".oO[datadir]Oo./.oO[name]Oo..Comparison_common"+name+"_Images/TkAlMapPlots/GCP.root\n")
0284                    #repMap["runComparisonScripts"] += \
0285                    #    ("cp .oO[alignmentName]Oo.ROOTGeometry.root "
0286                    #     ".oO[datadir]Oo./.oO[name]Oo..Comparison_common"+name+"_Images/TkAlMapPlots/comparedGeometry.root\n")
0287 
0288                 resultingFile = replaceByMap(("/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./compared%s_"
0289                                               ".oO[name]Oo..root"%name), repMap)
0290                 resultingFile = os.path.expandvars( resultingFile )
0291                 resultingFile = os.path.abspath( resultingFile )
0292                 resultingFile = "root://eoscms//eos/cms" + resultingFile   #needs to be AFTER abspath so that it doesn't eat the //
0293                 self.__filesToCompare[ name ] = resultingFile
0294 
0295             else:
0296                 raise AllInOneError("Need to have DetUnit in levels!")
0297 
0298         repMap["CommandLine"]=""
0299         repMap["CommandLine"]+= \
0300                  "# copy module list required for comparison script \n"
0301         if repMap["moduleList"].startswith("/store"):
0302             repMap["CommandLine"]+= \
0303                  "xrdcp root://eoscms//eos/cms.oO[moduleList]Oo. .\n"
0304         elif repMap["moduleList"].startswith("root://"):
0305             repMap["CommandLine"]+= \
0306                  "xrdcp .oO[moduleList]Oo. .\n"
0307         elif repMap["moduleList"].startswith("./CREATE_NEW/"):
0308             repMap["CommandLine"]+= \
0309                  "touch .oO[moduleListBase]Oo.\n"
0310         else:
0311             repMap["CommandLine"]+= \
0312                      "cp .oO[moduleList]Oo. .\n"
0313 
0314         try:
0315             getCommandOutput2(replaceByMap("cd $(mktemp -d)\n.oO[CommandLine]Oo.\ncat .oO[moduleListBase]Oo.", repMap))
0316         except RuntimeError:
0317             raise AllInOneError(replaceByMap(".oO[moduleList]Oo. does not exist!", repMap))
0318 
0319         for cfg in self.configFiles:
0320             # FIXME: produce this line only for enabled dbOutput
0321             # postProcess = "cp .oO[workdir]Oo./*.db .oO[datadir]Oo.\n"
0322             # postProcess = "cp *.db .oO[datadir]Oo.\n"
0323             postProcess = ""
0324             repMap["CommandLine"]+= \
0325                 repMap["CommandLineTemplate"]%{"cfgFile":cfg,
0326                                                "postProcess":postProcess}
0327         repMap["CommandLine"]+= ("# overall postprocessing\n"
0328                                  ".oO[runComparisonScripts]Oo.\n"
0329                                  )
0330 
0331         #~ print configTemplates.scriptTemplate
0332         scripts = {scriptName: replaceByMap( configTemplates.scriptTemplate, repMap )}
0333         files = {replaceByMap("TkAl3DVisualization_.oO[common]Oo._.oO[name]Oo..C", repMap ): replaceByMap(configTemplates.visualizationTrackerTemplate, repMap )}
0334         self.createFiles(files, path)
0335         return super(GeometryComparison, self).createScript(scripts, path)
0336 
0337     def createCrabCfg(self, path):
0338         msg = ("Parallelization not supported for geometry comparison. Please "
0339                "choose another 'jobmode'.")
0340         raise AllInOneError(msg)