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.

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

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

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 
def addIndex(filename, njobs, index=None)
def replacelast(string, old, new, count=1)
#define str(s)

◆ 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.

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

Referenced by pythonboolstring().

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 
def boolfromstring(string, name)
#define str(s)

◆ cache()

def helperFunctions.cache (   function)

Definition at line 155 of file helperFunctions.py.

References HiBiasedCentrality_cfi.function, print(), and mkLumiAveragedPlots.tuple.

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 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def cache(function)

◆ 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.

References getCommandOutput2().

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 
def getCommandOutput2(command)
def castorDirExists(path)

◆ 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.

Referenced by geometryComparison.GeometryComparison.getRepMap().

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
226 

◆ 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.

References pythonboolstring().

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

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 
def pythonboolstring(string, name)
def cppboolstring(string, name)

◆ 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.

Referenced by castorDirExists(), and geometryComparison.GeometryComparison.createScript().

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 
def getCommandOutput2(command)

◆ getTagsMap()

def helperFunctions.getTagsMap (   db)

Definition at line 200 of file helperFunctions.py.

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

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

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 
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
def make_url(database='pro', read_only=True)
Definition: conddblib.py:487
def connect(url, authPath=None, verbose=0, as_admin=False)
Definition: conddblib.py:533

◆ parsecolor()

def helperFunctions.parsecolor (   color)

Definition at line 108 of file helperFunctions.py.

References createfilelist.int, and str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

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 
def parsecolor(color)
#define str(s)

◆ parsestyle()

def helperFunctions.parsestyle (   style)

Definition at line 135 of file helperFunctions.py.

References createfilelist.int, and str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

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 
def parsestyle(style)
#define str(s)

◆ 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.

References boolfromstring(), and str.

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

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 
def pythonboolstring(string, name)
def boolfromstring(string, name)
#define str(s)

◆ recursivesubclasses()

def helperFunctions.recursivesubclasses (   cls)

Definition at line 149 of file helperFunctions.py.

Referenced by presentation.ValidationPlots.validationclass().

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

◆ 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.

References mps_monitormerge.items, and str.

Referenced by zMuMuValidation.ZMuMuValidation.appendToPlots(), geometryComparison.GeometryComparison.createConfiguration(), genericValidation.GenericValidation.createFiles(), 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 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 
def replaceByMap(target, the_map)
— Helpers —############################
#define str(s)

◆ replacelast()

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 def replacelast(string, old, new, count = 1):
88  """Replace the last occurances of a string"""
89  return new.join(string.rsplit(old,count))
90 
def replacelast(string, old, new, count=1)

Variable Documentation

◆ fileExtensions

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

Definition at line 91 of file helperFunctions.py.