00001 import os
00002 import configTemplates
00003 from helperFunctions import replaceByMap
00004 from TkAlExceptions import AllInOneError
00005
00006 class Alignment:
00007 def __init__(self, name, config, runGeomComp = "1"):
00008 self.condShorts = {
00009 "TrackerAlignmentErrorRcd":
00010 {"zeroAPE":{"connectString": ("frontier://FrontierProd"
00011 "/CMS_COND_31X_FROM21X"),
00012 "tagName": "TrackerIdealGeometryErrors210_mc",
00013 "labelName": ""}}}
00014 section = "alignment:%s"%name
00015 if not config.has_section( section ):
00016 raise AllInOneError, ("section %s not found. Please define the "
00017 "alignment!"%section)
00018 config.checkInput(section,
00019 knownSimpleOptions = ['globaltag', 'style', 'color'],
00020 knownKeywords = ['condition'])
00021 self.name = name
00022 self.runGeomComp = runGeomComp
00023 self.globaltag = config.get( section, "globaltag" )
00024 self.conditions = self.__getConditions( config, section )
00025 self.color = config.get(section,"color")
00026 self.style = config.get(section,"style")
00027
00028
00029 def __shorthandExists(self, theRcdName, theShorthand):
00030 """Method which checks, if `theShorthand` is a valid shorthand for the
00031 given `theRcdName`.
00032
00033 Arguments:
00034 - `theRcdName`: String which specifies the database record.
00035 - `theShorthand`: String which specifies the shorthand to check.
00036 """
00037
00038 if (theRcdName in self.condShorts) and \
00039 (theShorthand in self.condShorts[theRcdName]):
00040 return True
00041 else:
00042 return False
00043
00044
00045 def __getConditions( self, theConfig, theSection ):
00046 conditions = []
00047 for option in theConfig.options( theSection ):
00048 if option.startswith( "condition " ):
00049 rcdName = option.split( "condition " )[1]
00050 condPars = theConfig.get( theSection, option ).split( "," )
00051 if len(condPars) == 1:
00052 if len(condPars[0])==0:
00053 msg = ("In section [%s]: '%s' is used with too few "
00054 "arguments. A connect_string and a tag are "
00055 "required!"%(theSection, option))
00056 raise AllInOneError(msg)
00057 elif self.__shorthandExists(rcdName, condPars[0]):
00058 shorthand = condPars[0]
00059 condPars = [
00060 self.condShorts[rcdName][shorthand]["connectString"],
00061 self.condShorts[rcdName][shorthand]["tagName"],
00062 self.condShorts[rcdName][shorthand]["labelName"]]
00063 else:
00064 msg = ("In section [%s]: '%s' is used with '%s', "
00065 "which is an unknown shorthand for '%s'. Either "
00066 "provide at least a connect_string and a tag or "
00067 "use a known shorthand.\n"
00068 %(theSection, option, condPars[0], rcdName))
00069 if rcdName in self.condShorts:
00070 msg += "Known shorthands for '%s':\n"%(rcdName)
00071 theShorts = self.condShorts[rcdName]
00072 knownShorts = [("\t"+key+": "
00073 +theShorts[key]["connectString"]+","
00074 +theShorts[key]["tagName"]+","
00075 +theShorts[key]["labelName"]) \
00076 for key in theShorts]
00077 msg+="\n".join(knownShorts)
00078 else:
00079 msg += ("There are no known shorthands for '%s'."
00080 %(rcdName))
00081 raise AllInOneError(msg)
00082 if len( condPars ) == 2:
00083 condPars.append( "" )
00084 if len(condPars) > 3:
00085 msg = ("In section [%s]: '%s' is used with too many "
00086 "arguments. A maximum of 3 arguments is allowed."
00087 %(theSection, option))
00088 raise AllInOneError(msg)
00089 conditions.append({"rcdName": rcdName.strip(),
00090 "connectString": condPars[0].strip(),
00091 "tagName": condPars[1].strip(),
00092 "labelName": condPars[2].strip()})
00093 return conditions
00094
00095 def __testDbExist(self, dbpath):
00096
00097 return
00098 if not dbpath.startswith("sqlite_file:"):
00099 print "WARNING: could not check existence for",dbpath
00100 else:
00101 if not os.path.exists( dbpath.split("sqlite_file:")[1] ):
00102 raise "could not find file: '%s'"%dbpath.split("sqlite_file:")[1]
00103
00104 def restrictTo( self, restriction ):
00105 result = []
00106 if not restriction == None:
00107 for mode in self.mode:
00108 if mode in restriction:
00109 result.append( mode )
00110 self.mode = result
00111
00112 def getRepMap( self ):
00113 result = {
00114 "name": self.name,
00115 "color": self.color,
00116 "style": self.style,
00117 "runGeomComp": self.runGeomComp,
00118 "GlobalTag": self.globaltag
00119 }
00120 return result
00121
00122 def getConditions(self):
00123 """This function creates the configuration snippet to override
00124 global tag conditions.
00125 """
00126 if len( self.conditions ):
00127 loadCond = ("\nimport CalibTracker.Configuration."
00128 "Common.PoolDBESSource_cfi\n")
00129 for cond in self.conditions:
00130 if not cond["labelName"] == "":
00131 temp = configTemplates.conditionsTemplate.replace(
00132 "tag = cms.string('.oO[tagName]Oo.')",
00133 ("tag = cms.string('.oO[tagName]Oo.'),"
00134 "\nlabel = cms.untracked.string('.oO[labelName]Oo.')"))
00135 else:
00136 temp = configTemplates.conditionsTemplate
00137 loadCond += replaceByMap( temp, cond )
00138 else:
00139 loadCond = ""
00140 return loadCond