CMS 3D CMS Logo

Functions | Variables
helperFunctions Namespace Reference

Functions

def addIndex (filename, njobs, index=None)
 
def boolfromstring (string, name)
 
def cache (function)
 
def castorDirExists (path)
 
def cppboolstring (string, name)
 
def getCommandOutput2 (command)
 
def parsecolor (color)
 
def parsestyle (style)
 
def pythonboolstring (string, name)
 
def recursivesubclasses (cls)
 
def replaceByMap (target, the_map)
 — Helpers —############################ More...
 
def replacelast (string, old, new, count=1)
 

Variables

list fileExtensions = ["_cfg.py", ".sh", ".root"]
 

Function Documentation

def helperFunctions.addIndex (   filename,
  njobs,
  index = None 
)

Definition at line 87 of file helperFunctions.py.

References replacelast(), and harvestTrackValidationPlots.str.

Referenced by genericValidation.GenericValidation.createFiles(), genericValidation.GenericValidationData.createScript(), and genericValidation.GenericValidationData.getRepMap().

87 def addIndex(filename, njobs, index = None):
88  if index is None:
89  return [addIndex(filename, njobs, i) for i in range(njobs)]
90  if njobs == 1:
91  return filename
92 
93  fileExtension = None
94  for extension in fileExtensions:
95  if filename.endswith(extension):
96  fileExtension = extension
97  if fileExtension is None:
98  raise AllInOneError(fileName + " does not end with any of the extensions "
99  + str(fileExtensions))
100  return replacelast(filename, fileExtension, "_" + str(index) + fileExtension)
101 
def addIndex(filename, njobs, index=None)
def replacelast(string, old, new, count=1)
def helperFunctions.boolfromstring (   string,
  name 
)
Takes a string from the configuration file
and makes it into a bool

Definition at line 163 of file helperFunctions.py.

References createfilelist.int, and harvestTrackValidationPlots.str.

Referenced by pythonboolstring().

163 def boolfromstring(string, name):
164  """
165  Takes a string from the configuration file
166  and makes it into a bool
167  """
168  #try as a string, not case sensitive
169  if string.lower() == "true": return True
170  if string.lower() == "false": return False
171  #try as a number
172  try:
173  return str(bool(int(string)))
174  except ValueError:
175  pass
176  #out of options
177  raise ValueError("{} has to be true or false!".format(name))
178 
179 
def boolfromstring(string, name)
def helperFunctions.cache (   function)
def helperFunctions.castorDirExists (   path)
This function checks if the directory given by `path` exists.

Arguments:
- `path`: Path to castor directory

Definition at line 60 of file helperFunctions.py.

References getCommandOutput2().

60 def castorDirExists(path):
61  """This function checks if the directory given by `path` exists.
62 
63  Arguments:
64  - `path`: Path to castor directory
65  """
66 
67  if path[-1] == "/":
68  path = path[:-1]
69  containingPath = os.path.join( *path.split("/")[:-1] )
70  dirInQuestion = path.split("/")[-1]
71  try:
72  rawLines = getCommandOutput2("rfdir /"+containingPath).splitlines()
73  except RuntimeError:
74  return False
75  for line in rawLines:
76  if line.split()[0][0] == "d":
77  if line.split()[8] == dirInQuestion:
78  return True
79  return False
80 
def getCommandOutput2(command)
def castorDirExists(path)
def helperFunctions.cppboolstring (   string,
  name 
)
Takes a string from the configuration file
and makes it into a bool string for a C++ template

Definition at line 187 of file helperFunctions.py.

References pythonboolstring().

Referenced by plottingOptions.PlottingOptionsZMuMu.__init__(), plottingOptions.PlottingOptionsOffline.__init__(), and plottingOptions.PlottingOptionsPrimaryVertex.__init__().

187 def cppboolstring(string, name):
188  """
189  Takes a string from the configuration file
190  and makes it into a bool string for a C++ template
191  """
192  return pythonboolstring(string, name).lower()
193 
def pythonboolstring(string, name)
def cppboolstring(string, name)
def helperFunctions.getCommandOutput2 (   command)
This function executes `command` and returns it output.

Arguments:
- `command`: Shell command to be invoked by this function.

Definition at line 45 of file helperFunctions.py.

Referenced by castorDirExists(), geometryComparison.GeometryComparison.createScript(), validateAlignments.main(), and validateAlignments.ValidationJob.runJob().

45 def getCommandOutput2(command):
46  """This function executes `command` and returns it output.
47 
48  Arguments:
49  - `command`: Shell command to be invoked by this function.
50  """
51 
52  child = os.popen(command)
53  data = child.read()
54  err = child.close()
55  if err:
56  raise RuntimeError('%s failed w/ exit code %d' % (command, err))
57  return data
58 
59 
def getCommandOutput2(command)
def helperFunctions.parsecolor (   color)

Definition at line 102 of file helperFunctions.py.

References createfilelist.int, and harvestTrackValidationPlots.str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

102 def parsecolor(color):
103  try: #simplest case: it's an int
104  return int(color)
105  except ValueError:
106  pass
107 
108  try: #kRed, kBlue, ...
109  color = str(getattr(ROOT, color))
110  return int(color)
111  except (AttributeError, ValueError):
112  pass
113 
114  if color.count("+") + color.count("-") == 1: #kRed+5, kGreen-2
115  if "+" in color: #don't want to deal with nonassociativity of -
116  split = color.split("+")
117  color1 = parsecolor(split[0])
118  color2 = parsecolor(split[1])
119  return color1 + color2
120 
121  if "-" in color:
122  split = color.split("-")
123  color1 = parsecolor(split[0])
124  color2 = parsecolor(split[1])
125  return color1 - color2
126 
127  raise AllInOneError("color has to be an integer, a ROOT constant (kRed, kBlue, ...), or a two-term sum or difference (kGreen-5)!")
128 
def parsecolor(color)
def helperFunctions.parsestyle (   style)

Definition at line 129 of file helperFunctions.py.

References createfilelist.int, and harvestTrackValidationPlots.str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

129 def parsestyle(style):
130  try: #simplest case: it's an int
131  return int(style)
132  except ValueError:
133  pass
134 
135  try: #kStar, kDot, ...
136  style = str(getattr(ROOT,style))
137  return int(style)
138  except (AttributeError, ValueError):
139  pass
140 
141  raise AllInOneError("style has to be an integer or a ROOT constant (kDashed, kStar, ...)!")
142 
def parsestyle(style)
def helperFunctions.pythonboolstring (   string,
  name 
)
Takes a string from the configuration file
and makes it into a bool string for a python template

Definition at line 180 of file helperFunctions.py.

References boolfromstring(), and harvestTrackValidationPlots.str.

Referenced by primaryVertexValidation.PrimaryVertexValidation.__init__(), offlineValidation.OfflineValidation.__init__(), cppboolstring(), and geometryComparison.GeometryComparison.createConfiguration().

180 def pythonboolstring(string, name):
181  """
182  Takes a string from the configuration file
183  and makes it into a bool string for a python template
184  """
185  return str(boolfromstring(string, name))
186 
def pythonboolstring(string, name)
def boolfromstring(string, name)
def helperFunctions.recursivesubclasses (   cls)

Definition at line 143 of file helperFunctions.py.

Referenced by presentation.ValidationPlots.validationclass().

144  result = [cls]
145  for subcls in cls.__subclasses__():
146  result += recursivesubclasses(subcls)
147  return result
148 
def recursivesubclasses(cls)
def helperFunctions.replaceByMap (   target,
  the_map 
)

— Helpers —############################

This function replaces `.oO[key]Oo.` by `the_map[key]` in target.

Arguments:
- `target`: String which contains symbolic tags of the form `.oO[key]Oo.`
- `the_map`: Dictionary which has to contain the `key`s in `target` as keys

Definition at line 6 of file helperFunctions.py.

References harvestTrackValidationPlots.str.

Referenced by zMuMuValidation.ZMuMuValidation.appendToPlots(), geometryComparison.GeometryComparison.createConfiguration(), genericValidation.GenericValidation.createFiles(), validateAlignments.createMergeScript(), genericValidation.ValidationWithPlots.createPlottingScript(), geometryComparison.GeometryComparison.createScript(), genericValidation.ValidationWithComparison.doComparison(), genericValidation.ParallelValidation.doInitMerge(), genericValidation.ParallelValidation.doMerge(), genericValidation.ValidationWithPlots.doRunPlots(), zMuMuValidation.ZMuMuValidation.filesToCompare(), alignment.Alignment.getConditions(), genericValidation.GenericValidationData.getRepMap(), offlineValidation.OfflineValidation.initMerge(), and plottingOptions.PlottingOptionsTrackSplitting.validsubdets().

6 def replaceByMap(target, the_map):
7  """This function replaces `.oO[key]Oo.` by `the_map[key]` in target.
8 
9  Arguments:
10  - `target`: String which contains symbolic tags of the form `.oO[key]Oo.`
11  - `the_map`: Dictionary which has to contain the `key`s in `target` as keys
12  """
13 
14  result = target
15  for key in the_map:
16  lifeSaver = 10e3
17  iteration = 0
18  while ".oO[" in result and "]Oo." in result:
19  for key in the_map:
20  try:
21  result = result.replace(".oO["+key+"]Oo.",the_map[key])
22  except TypeError: #try a dict
23  try:
24  for keykey, value in the_map[key].iteritems():
25  result = result.replace(".oO[" + key + "['" + keykey + "']]Oo.", value)
26  result = result.replace(".oO[" + key + '["' + keykey + '"]]Oo.', value)
27  except AttributeError: #try a list
28  try:
29  for index, value in enumerate(the_map[key]):
30  result = result.replace(".oO[" + key + "[" + str(index) + "]]Oo.", value)
31  except TypeError:
32  raise TypeError("Something is wrong in replaceByMap! Need a string, dict, or list, but the_map(%s)=%s!"%(repr(key), repr(the_map[key])))
33  iteration += 1
34  if iteration > lifeSaver:
35  problematicLines = ""
36  for line in result.splitlines():
37  if ".oO[" in result and "]Oo." in line:
38  problematicLines += "%s\n"%line
39  msg = ("Oh Dear, there seems to be an endless loop in "
40  "replaceByMap!!\n%s\n%s"%(problematicLines, the_map))
41  raise AllInOneError(msg)
42  return result
43 
44 
def replaceByMap(target, the_map)
— Helpers —############################
def helperFunctions.replacelast (   string,
  old,
  new,
  count = 1 
)
Replace the last occurances of a string

Definition at line 81 of file helperFunctions.py.

Referenced by addIndex().

81 def replacelast(string, old, new, count = 1):
82  """Replace the last occurances of a string"""
83  return new.join(string.rsplit(old,count))
84 
def replacelast(string, old, new, count=1)

Variable Documentation

list helperFunctions.fileExtensions = ["_cfg.py", ".sh", ".root"]

Definition at line 85 of file helperFunctions.py.