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 clean_name (s)
 
def cppboolstring (string, name)
 
def getCommandOutput2 (command)
 
def getTagsMap (db)
 
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

◆ addIndex()

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

Definition at line 93 of file helperFunctions.py.

93 def addIndex(filename, njobs, index = None):
94  if index is None:
95  return [addIndex(filename, njobs, i) for i in range(njobs)]
96  if njobs == 1:
97  return filename
98 
99  fileExtension = None
100  for extension in fileExtensions:
101  if filename.endswith(extension):
102  fileExtension = extension
103  if fileExtension is None:
104  raise AllInOneError(fileName + " does not end with any of the extensions "
105  + str(fileExtensions))
106  return replacelast(filename, fileExtension, "_" + str(index) + fileExtension)
107 

References FastTimerService_cff.range, replacelast(), and str.

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

◆ boolfromstring()

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.

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

References electrons_cff.bool, createfilelist.int, and str.

Referenced by pythonboolstring().

◆ cache()

def helperFunctions.cache (   function)

Definition at line 155 of file helperFunctions.py.

155 def cache(function):
156  cache = {}
157  def newfunction(*args, **kwargs):
158  try:
159  return cache[args, tuple(sorted(kwargs.items()))]
160  except TypeError:
161  print(args, tuple(sorted(kwargs.items())))
162  raise
163  except KeyError:
164  cache[args, tuple(sorted(kwargs.items()))] = function(*args, **kwargs)
165  return newfunction(*args, **kwargs)
166  newfunction.__name__ = function.__name__
167  return newfunction
168 

References HiBiasedCentrality_cfi.function, and print().

◆ castorDirExists()

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.

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

References getCommandOutput2().

◆ clean_name()

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.

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

Referenced by geometryComparison.GeometryComparison.getRepMap().

◆ cppboolstring()

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.

193 def cppboolstring(string, name):
194  """
195  Takes a string from the configuration file
196  and makes it into a bool string for a C++ template
197  """
198  return pythonboolstring(string, name).lower()
199 

References pythonboolstring().

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

◆ getCommandOutput2()

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.

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

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

◆ getTagsMap()

def helperFunctions.getTagsMap (   db)

Definition at line 200 of file helperFunctions.py.

200 def getTagsMap(db):
201  con = conddblib.connect(url = conddblib.make_url(db))
202  session = con.session()
203  TAG = session.get_dbtype(conddblib.Tag)
204  dictionary = {}
205  for i in range(0,len(session.query(TAG.object_type).order_by(TAG.name).all())):
206  q1 = session.query(TAG.object_type).order_by(TAG.name).all()[i][0]
207  q2 = session.query(TAG.name).order_by(TAG.name).all()[i][0]
208  dictionary[q1]=q2
209 
210  return dictionary
211 

References python.cmstools.all(), conddblib.connect(), conddblib.make_url(), and FastTimerService_cff.range.

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

◆ parsecolor()

def helperFunctions.parsecolor (   color)

Definition at line 108 of file helperFunctions.py.

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

References createfilelist.int, and str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

◆ parsestyle()

def helperFunctions.parsestyle (   style)

Definition at line 135 of file helperFunctions.py.

135 def parsestyle(style):
136  try: #simplest case: it's an int
137  return int(style)
138  except ValueError:
139  pass
140 
141  try: #kStar, kDot, ...
142  style = str(getattr(ROOT,style))
143  return int(style)
144  except (AttributeError, ValueError):
145  pass
146 
147  raise AllInOneError("style has to be an integer or a ROOT constant (kDashed, kStar, ...)!")
148 

References createfilelist.int, and str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

◆ pythonboolstring()

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.

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

References boolfromstring(), and str.

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

◆ recursivesubclasses()

def helperFunctions.recursivesubclasses (   cls)

Definition at line 149 of file helperFunctions.py.

149 def recursivesubclasses(cls):
150  result = [cls]
151  for subcls in cls.__subclasses__():
152  result += recursivesubclasses(subcls)
153  return result
154 

Referenced by presentation.ValidationPlots.validationclass().

◆ replaceByMap()

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.

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

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().

◆ replacelast()

def helperFunctions.replacelast (   string,
  old,
  new,
  count = 1 
)
Replace the last occurances of a string

Definition at line 87 of file helperFunctions.py.

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

Referenced by addIndex().

Variable Documentation

◆ fileExtensions

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

Definition at line 91 of file helperFunctions.py.

helperFunctions.castorDirExists
def castorDirExists(path)
Definition: helperFunctions.py:66
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
electrons_cff.bool
bool
Definition: electrons_cff.py:366
conddblib.Tag
Definition: conddblib.py:235
helperFunctions.replacelast
def replacelast(string, old, new, count=1)
Definition: helperFunctions.py:87
helperFunctions.boolfromstring
def boolfromstring(string, name)
Definition: helperFunctions.py:169
helperFunctions.replaceByMap
def replaceByMap(target, the_map)
— Helpers —############################
Definition: helperFunctions.py:12
helperFunctions.recursivesubclasses
def recursivesubclasses(cls)
Definition: helperFunctions.py:149
python.cmstools.all
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
mps_monitormerge.items
list items
Definition: mps_monitormerge.py:29
helperFunctions.cache
def cache(function)
Definition: helperFunctions.py:155
helperFunctions.clean_name
def clean_name(s)
Definition: helperFunctions.py:212
str
#define str(s)
Definition: TestProcessor.cc:53
helperFunctions.parsestyle
def parsestyle(style)
Definition: helperFunctions.py:135
helperFunctions.getCommandOutput2
def getCommandOutput2(command)
Definition: helperFunctions.py:51
helperFunctions.parsecolor
def parsecolor(color)
Definition: helperFunctions.py:108
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
helperFunctions.cppboolstring
def cppboolstring(string, name)
Definition: helperFunctions.py:193
createfilelist.int
int
Definition: createfilelist.py:10
helperFunctions.getTagsMap
def getTagsMap(db)
Definition: helperFunctions.py:200
helperFunctions.pythonboolstring
def pythonboolstring(string, name)
Definition: helperFunctions.py:186
helperFunctions.addIndex
def addIndex(filename, njobs, index=None)
Definition: helperFunctions.py:93
conddblib.connect
def connect(url, authPath=None, verbose=0, as_admin=False)
Definition: conddblib.py:533
format
HiBiasedCentrality_cfi.function
function
Definition: HiBiasedCentrality_cfi.py:4
conddblib.make_url
def make_url(database='pro', read_only=True)
Definition: conddblib.py:487