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 conddb (args)
 
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

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

Function Documentation

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

Definition at line 91 of file helperFunctions.py.

References replacelast(), and str.

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

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

Definition at line 167 of file helperFunctions.py.

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

Referenced by pythonboolstring().

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

Definition at line 153 of file helperFunctions.py.

References edm.print().

Referenced by EcalHitResponse.analogSignalAmplitude(), SiStripLAProfileBooker.analyze(), DTCalibrationMap.getConsts(), ConvBremPFTrackFinder.getConvBremPFRecTracks(), edm::RootTree.getEntry(), BeamConditionsMonitor.globalBeginLuminosityBlock(), RectangularEtaPhiTrackingRegion.hits(), CosmicTrackingRegion.hits_(), DeepFlavourTFJetTagsProducer.initializeGlobalCache(), DeepDoubleBTFJetTagsProducer.initializeGlobalCache(), DeepBoostedJetTagsProducer.initializeGlobalCache(), PhysicsTools::MVAModuleHelper< Record, Object, Filler >.operator()(), ClusterShapeTrackFilterProducer.produce(), HIPixelTrackFilterProducer.produce(), SiPixelDigiToRaw.produce(), edm::PtrVectorBase.PtrVectorBase(), edm::RootEmbeddedFileSequence.readOneEvent(), edm::RootFile.RootFile(), edm::EmbeddedRootSource.runHelper(), ClusterShapeTrajectoryFilter.setEvent(), PhysicsTools::MVAModuleHelper< Record, Object, Filler >.setEventSetup(), PhysicsTools::MVAModuleHelper< Record, Object, Filler >.train(), edm::refcoreimpl.tryToSetCacheItemForFirstTime(), and reco::FlavorHistoryEvent.~FlavorHistoryEvent().

153 def cache(function):
154  cache = {}
155  def newfunction(*args, **kwargs):
156  try:
157  return cache[args, tuple(sorted(six.iteritems(kwargs)))]
158  except TypeError:
159  print(args, tuple(sorted(six.iteritems(kwargs))))
160  raise
161  except KeyError:
162  cache[args, tuple(sorted(six.iteritems(kwargs)))] = function(*args, **kwargs)
163  return newfunction(*args, **kwargs)
164  newfunction.__name__ = function.__name__
165  return newfunction
166 
Definition: vlib.h:256
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def 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 64 of file helperFunctions.py.

References getCommandOutput2().

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

Arguments:
- `s`: input string

Definition at line 242 of file helperFunctions.py.

Referenced by geometryComparison.GeometryComparison.getRepMap().

242 def clean_name(s):
243  """Transforms a string into a valid variable or method name.
244 
245  Arguments:
246  - `s`: input string
247  """
248 
249  # Remove invalid characters
250  s = re.sub(r"[^0-9a-zA-Z_]", "", s)
251 
252  # Remove leading characters until we find a letter or underscore
253  s = re.sub(r"^[^a-zA-Z_]+", "", s)
254 
255  return s
256 
def helperFunctions.conddb (   args)
Wrapper for conddb, so that you can run
conddb("--db", "myfile.db", "listTags"),
like from the command line, without explicitly
dealing with all the functions in CondCore/Utilities.
getcommandoutput2(conddb ...) doesn't work, it imports
the wrong sqlalchemy in CondCore/Utilities/python/conddblib.py

Definition at line 199 of file helperFunctions.py.

References getCommandOutput2(), and digitizers_cfi.strip.

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

199 def conddb(*args):
200  """
201  Wrapper for conddb, so that you can run
202  conddb("--db", "myfile.db", "listTags"),
203  like from the command line, without explicitly
204  dealing with all the functions in CondCore/Utilities.
205  getcommandoutput2(conddb ...) doesn't work, it imports
206  the wrong sqlalchemy in CondCore/Utilities/python/conddblib.py
207  """
208  global conddbcode
209  from tempfile import mkdtemp, NamedTemporaryFile
210 
211  if conddbcode is None:
212  conddbfile = getCommandOutput2("which conddb").strip()
213  tmpdir = mkdtemp()
214  getCommandOutput2("2to3 -f print -o " + tmpdir + " -n -w " + conddbfile)
215 
216  with open(os.path.join(tmpdir, "conddb")) as f:
217  conddb = f.read()
218 
219  conddbcode = conddb.replace("sys.exit", "sysexit")
220 
221  def sysexit(number):
222  if number != 0:
223  raise AllInOneError("conddb exited with status {}".format(number))
224  namespace = {"sysexit": sysexit, "conddboutput": ""}
225 
226  bkpargv = sys.argv
227  sys.argv[1:] = args
228  bkpstdout = sys.stdout
229  try:
230  with NamedTemporaryFile(bufsize=0) as sys.stdout:
231  exec(conddbcode, namespace)
232  namespace["main"]()
233  with open(sys.stdout.name) as f:
234  result = f.read()
235  finally:
236  sys.argv[:] = bkpargv
237  sys.stdout = bkpstdout
238 
239  return result
240 
241 
def getCommandOutput2(command)
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 191 of file helperFunctions.py.

References pythonboolstring().

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

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

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

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

Definition at line 106 of file helperFunctions.py.

References createfilelist.int, and str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

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

Definition at line 133 of file helperFunctions.py.

References createfilelist.int, and str.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

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

References boolfromstring(), and str.

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

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

Definition at line 147 of file helperFunctions.py.

Referenced by presentation.ValidationPlots.validationclass().

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

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

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

Definition at line 85 of file helperFunctions.py.

Referenced by addIndex().

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

Variable Documentation

helperFunctions.conddbcode = None

Definition at line 198 of file helperFunctions.py.

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

Definition at line 89 of file helperFunctions.py.