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 94 of file helperFunctions.py.

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)
108 

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 170 of file helperFunctions.py.

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 
186 

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

Referenced by pythonboolstring().

◆ cache()

def helperFunctions.cache (   function)

Definition at line 156 of file helperFunctions.py.

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

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 67 of file helperFunctions.py.

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
87 

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 213 of file helperFunctions.py.

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

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 194 of file helperFunctions.py.

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

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 52 of file helperFunctions.py.

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 
66 

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

◆ getTagsMap()

def helperFunctions.getTagsMap (   db)

Definition at line 201 of file helperFunctions.py.

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
212 

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 109 of file helperFunctions.py.

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)!")
135 

References createfilelist.int, and str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

◆ parsestyle()

def helperFunctions.parsestyle (   style)

Definition at line 136 of file helperFunctions.py.

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, ...)!")
149 

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 187 of file helperFunctions.py.

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))
193 

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 150 of file helperFunctions.py.

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

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 13 of file helperFunctions.py.

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 six.iteritems(the_map[key]):
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 
51 

References 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 88 of file helperFunctions.py.

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

Referenced by addIndex().

Variable Documentation

◆ fileExtensions

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

Definition at line 92 of file helperFunctions.py.

helperFunctions.castorDirExists
def castorDirExists(path)
Definition: helperFunctions.py:67
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
electrons_cff.bool
bool
Definition: electrons_cff.py:366
conddblib.Tag
Definition: conddblib.py:229
helperFunctions.replacelast
def replacelast(string, old, new, count=1)
Definition: helperFunctions.py:88
helperFunctions.boolfromstring
def boolfromstring(string, name)
Definition: helperFunctions.py:170
helperFunctions.replaceByMap
def replaceByMap(target, the_map)
— Helpers —############################
Definition: helperFunctions.py:13
helperFunctions.recursivesubclasses
def recursivesubclasses(cls)
Definition: helperFunctions.py:150
python.cmstools.all
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:26
helperFunctions.cache
def cache(function)
Definition: helperFunctions.py:156
helperFunctions.clean_name
def clean_name(s)
Definition: helperFunctions.py:213
str
#define str(s)
Definition: TestProcessor.cc:52
helperFunctions.parsestyle
def parsestyle(style)
Definition: helperFunctions.py:136
helperFunctions.getCommandOutput2
def getCommandOutput2(command)
Definition: helperFunctions.py:52
helperFunctions.parsecolor
def parsecolor(color)
Definition: helperFunctions.py:109
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:194
createfilelist.int
int
Definition: createfilelist.py:10
helperFunctions.getTagsMap
def getTagsMap(db)
Definition: helperFunctions.py:201
conddblib.connect
def connect(url, authPath=None, verbose=0)
Definition: conddblib.py:518
helperFunctions.pythonboolstring
def pythonboolstring(string, name)
Definition: helperFunctions.py:187
helperFunctions.addIndex
def addIndex(filename, njobs, index=None)
Definition: helperFunctions.py:94
format
HiBiasedCentrality_cfi.function
function
Definition: HiBiasedCentrality_cfi.py:4
conddblib.make_url
def make_url(database='pro', read_only=True)
Definition: conddblib.py:472