CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
alignment.py
Go to the documentation of this file.
1 import configTemplates
2 from helperFunctions import replaceByMap
3 import os
4 import ROOT
5 from TkAlExceptions import AllInOneError
6 
7 class Alignment:
8  def __init__(self, name, config, runGeomComp = "1"):
9  self.condShorts = {
10  "TrackerAlignmentErrorExtendedRcd": {
11  "zeroAPE": {
12  "connectString":("frontier://FrontierProd"
13  "/CMS_CONDITIONS"),
14  "tagName": "TrackerIdealGeometryErrorsExtended210_mc",
15  "labelName": ""
16  }
17  },
18  "TrackerSurfaceDeformationRcd": {
19  "zeroDeformations": {
20  "connectString":("frontier://FrontierProd"
21  "/CMS_CONDITIONS"),
22  "tagName": "TrackerSurfaceDeformations_zero",
23  "labelName": ""
24  }
25  },
26  }
27  section = "alignment:%s"%name
28  if not config.has_section( section ):
29  raise AllInOneError("section %s not found. Please define the "
30  "alignment!"%section)
31  config.checkInput(section,
32  knownSimpleOptions = ['globaltag', 'style', 'color', 'title'],
33  knownKeywords = ['condition'])
34  self.name = name
35  if config.exists(section,"title"):
36  self.title = config.get(section,"title")
37  else:
38  self.title = self.name
39  if (int(runGeomComp) != 1):
40  self.name += "_run" + runGeomComp
41  self.title += " run " + runGeomComp
42  if "|" in self.title or "," in self.title or '"' in self.title:
43  msg = "The characters '|', '\"', and ',' cannot be used in the alignment title!"
44  raise AllInOneError(msg)
45  self.runGeomComp = runGeomComp
46  self.globaltag = config.get( section, "globaltag" )
47  self.conditions = self.__getConditions( config, section )
48 
49  self.color = config.get(section,"color")
50  self.style = config.get(section,"style")
51  try: #make sure color is an int
52  int(self.color)
53  except ValueError:
54  try: #kRed, kBlue, ...
55  self.color = str(getattr(ROOT, self.color))
56  int(self.color)
57  except (AttributeError, ValueError):
58  raise ValueError("color has to be an integer or a ROOT constant (kRed, kBlue, ...)!")
59  try: #make sure style is an int
60  int(self.style)
61  except ValueError:
62  raise ValueError("style has to be an integer!")
63 
64  def __shorthandExists(self, theRcdName, theShorthand):
65  """Method which checks, if `theShorthand` is a valid shorthand for the
66  given `theRcdName`.
67 
68  Arguments:
69  - `theRcdName`: String which specifies the database record.
70  - `theShorthand`: String which specifies the shorthand to check.
71  """
72 
73  if (theRcdName in self.condShorts) and \
74  (theShorthand in self.condShorts[theRcdName]):
75  return True
76  else:
77  return False
78 
79 
80  def __getConditions( self, theConfig, theSection ):
81  conditions = []
82  for option in theConfig.options( theSection ):
83  if option.startswith( "condition " ):
84  rcdName = option.split( "condition " )[1]
85  condPars = theConfig.get( theSection, option ).split( "," )
86  if len(condPars) == 1:
87  if len(condPars[0])==0:
88  msg = ("In section [%s]: '%s' is used with too few "
89  "arguments. A connect_string and a tag are "
90  "required!"%(theSection, option))
91  raise AllInOneError(msg)
92  elif self.__shorthandExists(rcdName, condPars[0]):
93  shorthand = condPars[0]
94  condPars = [
95  self.condShorts[rcdName][shorthand]["connectString"],
96  self.condShorts[rcdName][shorthand]["tagName"],
97  self.condShorts[rcdName][shorthand]["labelName"]]
98  else:
99  msg = ("In section [%s]: '%s' is used with '%s', "
100  "which is an unknown shorthand for '%s'. Either "
101  "provide at least a connect_string and a tag or "
102  "use a known shorthand.\n"
103  %(theSection, option, condPars[0], rcdName))
104  if rcdName in self.condShorts:
105  msg += "Known shorthands for '%s':\n"%(rcdName)
106  theShorts = self.condShorts[rcdName]
107  knownShorts = [("\t"+key+": "
108  +theShorts[key]["connectString"]+","
109  +theShorts[key]["tagName"]+","
110  +theShorts[key]["labelName"]) \
111  for key in theShorts]
112  msg+="\n".join(knownShorts)
113  else:
114  msg += ("There are no known shorthands for '%s'."
115  %(rcdName))
116  raise AllInOneError(msg)
117  if len( condPars ) == 2:
118  condPars.append( "" )
119  if len(condPars) > 3:
120  msg = ("In section [%s]: '%s' is used with too many "
121  "arguments. A maximum of 3 arguments is allowed."
122  %(theSection, option))
123  raise AllInOneError(msg)
124  conditions.append({"rcdName": rcdName.strip(),
125  "connectString": condPars[0].strip(),
126  "tagName": condPars[1].strip(),
127  "labelName": condPars[2].strip()})
128  return conditions
129 
130  def __testDbExist(self, dbpath):
131  #FIXME delete return to end train debuging
132  return
133  if not dbpath.startswith("sqlite_file:"):
134  print "WARNING: could not check existence for",dbpath
135  else:
136  if not os.path.exists( dbpath.split("sqlite_file:")[1] ):
137  raise "could not find file: '%s'"%dbpath.split("sqlite_file:")[1]
138 
139  def restrictTo( self, restriction ):
140  result = []
141  if not restriction == None:
142  for mode in self.mode:
143  if mode in restriction:
144  result.append( mode )
145  self.mode = result
146 
147  def getRepMap( self ):
148  result = {
149  "name": self.name,
150  "title": self.title,
151  "color": self.color,
152  "style": self.style,
153  "runGeomComp": self.runGeomComp,
154  "GlobalTag": self.globaltag
155  }
156  return result
157 
158  def getConditions(self):
159  """This function creates the configuration snippet to override
160  global tag conditions.
161  """
162  if len( self.conditions ):
163  loadCond = ("\nimport CalibTracker.Configuration."
164  "Common.PoolDBESSource_cfi\n")
165  for cond in self.conditions:
166  if not cond["labelName"] == "":
167  temp = configTemplates.conditionsTemplate.replace(
168  "tag = cms.string('.oO[tagName]Oo.')",
169  ("tag = cms.string('.oO[tagName]Oo.'),"
170  "\nlabel = cms.untracked.string('.oO[labelName]Oo.')"))
171  else:
172  temp = configTemplates.conditionsTemplate
173  loadCond += replaceByMap( temp, cond )
174  else:
175  loadCond = ""
176  return loadCond
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def replaceByMap
— Helpers —############################
double split
Definition: MVATrainer.cc:139