CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functions | Variables
helperFunctions Namespace Reference

Functions

def addIndex
 
def boolfromstring
 
def cache
 
def castorDirExists
 
def clean_name
 
def cppboolstring
 
def getCommandOutput2
 
def getTagsMap
 
def parsecolor
 
def parsestyle
 
def pythonboolstring
 
def recursivesubclasses
 
def replaceByMap
 — Helpers —############################ More...
 
def replacelast
 

Variables

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

Function Documentation

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

Definition at line 93 of file helperFunctions.py.

References sistrip::SpyUtilities.range(), replacelast(), and str.

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

93 
94 def addIndex(filename, njobs, index = None):
95  if index is None:
96  return [addIndex(filename, njobs, i) for i in range(njobs)]
97  if njobs == 1:
98  return filename
99 
100  fileExtension = None
101  for extension in fileExtensions:
102  if filename.endswith(extension):
103  fileExtension = extension
104  if fileExtension is None:
105  raise AllInOneError(fileName + " does not end with any of the extensions "
106  + str(fileExtensions))
107  return replacelast(filename, fileExtension, "_" + str(index) + fileExtension)
const uint16_t range(const Frame &aFrame)
#define str(s)
def helperFunctions.boolfromstring (   string,
  name 
)
Takes a string from the configuration file
and makes it into a bool

Definition at line 169 of file helperFunctions.py.

References str.

Referenced by pythonboolstring().

170 def boolfromstring(string, name):
171  """
172  Takes a string from the configuration file
173  and makes it into a bool
174  """
175  #try as a string, not case sensitive
176  if string.lower() == "true": return True
177  if string.lower() == "false": return False
178  #try as a number
179  try:
180  return str(bool(int(string)))
181  except ValueError:
182  pass
183  #out of options
184  raise ValueError("{} has to be true or false!".format(name))
185 
#define str(s)
def helperFunctions.cache (   function)

Definition at line 155 of file helperFunctions.py.

References callgraph.function, and print().

156 def cache(function):
157  cache = {}
158  def newfunction(*args, **kwargs):
159  try:
160  return cache[args, tuple(sorted(kwargs.items()))]
161  except TypeError:
162  print(args, tuple(sorted(kwargs.items())))
163  raise
164  except KeyError:
165  cache[args, tuple(sorted(kwargs.items()))] = function(*args, **kwargs)
166  return newfunction(*args, **kwargs)
167  newfunction.__name__ = function.__name__
168  return newfunction
string function
Definition: callgraph.py:50
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def helperFunctions.castorDirExists (   path)
This function checks if the directory given by `path` exists.

Arguments:
- `path`: Path to castor directory

Definition at line 66 of file helperFunctions.py.

References getCommandOutput2().

66 
67 def castorDirExists(path):
68  """This function checks if the directory given by `path` exists.
69 
70  Arguments:
71  - `path`: Path to castor directory
72  """
73 
74  if path[-1] == "/":
75  path = path[:-1]
76  containingPath = os.path.join( *path.split("/")[:-1] )
77  dirInQuestion = path.split("/")[-1]
78  try:
79  rawLines = getCommandOutput2("rfdir /"+containingPath).splitlines()
80  except RuntimeError:
81  return False
82  for line in rawLines:
83  if line.split()[0][0] == "d":
84  if line.split()[8] == dirInQuestion:
85  return True
86  return False
def helperFunctions.clean_name (   s)
Transforms a string into a valid variable or method name.

Arguments:
- `s`: input string

Definition at line 212 of file helperFunctions.py.

Referenced by geometryComparison.GeometryComparison.getRepMap().

213 def clean_name(s):
214  """Transforms a string into a valid variable or method name.
215 
216  Arguments:
217  - `s`: input string
218  """
219 
220  # Remove invalid characters
221  s = re.sub(r"[^0-9a-zA-Z_]", "", s)
222 
223  # Remove leading characters until we find a letter or underscore
224  s = re.sub(r"^[^a-zA-Z_]+", "", s)
225 
226  return s
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 193 of file helperFunctions.py.

References pythonboolstring().

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

194 def cppboolstring(string, name):
195  """
196  Takes a string from the configuration file
197  and makes it into a bool string for a C++ template
198  """
199  return pythonboolstring(string, name).lower()
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 51 of file helperFunctions.py.

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

51 
52 def getCommandOutput2(command):
53  """This function executes `command` and returns it output.
54 
55  Arguments:
56  - `command`: Shell command to be invoked by this function.
57  """
58 
59  child = os.popen(command)
60  data = child.read()
61  err = child.close()
62  if err:
63  raise RuntimeError('%s failed w/ exit code %d' % (command, err))
64  return data
65 
def helperFunctions.getTagsMap (   db)

Definition at line 200 of file helperFunctions.py.

References python.cmstools.all(), conddblib.connect(), conddblib.make_url(), and sistrip::SpyUtilities.range().

Referenced by alignment.Alignment.__getConditions(), and alignment.Alignment.__testDbExist().

201 def getTagsMap(db):
202  con = conddblib.connect(url = conddblib.make_url(db))
203  session = con.session()
204  TAG = session.get_dbtype(conddblib.Tag)
205  dictionary = {}
206  for i in range(0,len(session.query(TAG.object_type).order_by(TAG.name).all())):
207  q1 = session.query(TAG.object_type).order_by(TAG.name).all()[i][0]
208  q2 = session.query(TAG.name).order_by(TAG.name).all()[i][0]
209  dictionary[q1]=q2
210 
211  return dictionary
const uint16_t range(const Frame &aFrame)
def all
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
def make_url
Definition: conddblib.py:487
def connect
Definition: conddblib.py:533
def helperFunctions.parsecolor (   color)

Definition at line 108 of file helperFunctions.py.

References str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

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

Definition at line 135 of file helperFunctions.py.

References str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

136 def parsestyle(style):
137  try: #simplest case: it's an int
138  return int(style)
139  except ValueError:
140  pass
141 
142  try: #kStar, kDot, ...
143  style = str(getattr(ROOT,style))
144  return int(style)
145  except (AttributeError, ValueError):
146  pass
147 
148  raise AllInOneError("style has to be an integer or a ROOT constant (kDashed, kStar, ...)!")
#define str(s)
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 186 of file helperFunctions.py.

References boolfromstring(), and str.

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

187 def pythonboolstring(string, name):
188  """
189  Takes a string from the configuration file
190  and makes it into a bool string for a python template
191  """
192  return str(boolfromstring(string, name))
#define str(s)
def helperFunctions.recursivesubclasses (   cls)

Definition at line 149 of file helperFunctions.py.

Referenced by presentation.ValidationPlots.validationclass().

150 def recursivesubclasses(cls):
151  result = [cls]
152  for subcls in cls.__subclasses__():
153  result += recursivesubclasses(subcls)
154  return result
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 12 of file helperFunctions.py.

References mps_monitormerge.items, and 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().

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

Definition at line 87 of file helperFunctions.py.

Referenced by addIndex().

87 
88 def replacelast(string, old, new, count = 1):
89  """Replace the last occurances of a string"""
90  return new.join(string.rsplit(old,count))

Variable Documentation

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

Definition at line 91 of file helperFunctions.py.