CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Classes | Functions | Variables
python.rootplot.utilities Namespace Reference

Classes

class  Hist
 
class  Hist2D
 Define classes. More...
 
class  HistStack
 
class  RootFile
 

Functions

def _rootglob0
 
def _rootglob1
 
def find_num_processors
 
def get
 
def has_glob_magic
 
def irootglob
 
def loadROOT
 Define additional helping functions. More...
 
def ls
 
def process_bin_labels
 
def pwd
 
def replace
 
def rootglob
 
def testfile
 
def wilson_interval
 

Variables

string __license__
 
tuple glob_magic_check = re.compile('[*?[]')
 Functions for globbing within root files. More...
 

Detailed Description

Utilities for rootplot including histogram classes.

Function Documentation

def python.rootplot.utilities._rootglob0 (   tdirectory,
  dirname,
  basename 
)
private

Definition at line 554 of file utilities.py.

555 def _rootglob0(tdirectory, dirname, basename):
556  if tdirectory.Get(os.path.join(dirname, basename)):
557  return [basename]
558  return []
def python.rootplot.utilities._rootglob1 (   tdirectory,
  dirname,
  pattern 
)
private

Definition at line 547 of file utilities.py.

548 def _rootglob1(tdirectory, dirname, pattern):
549  if not tdirectory.GetDirectory(dirname):
550  return []
551  names = [key.GetName() for key in
552  tdirectory.GetDirectory(dirname).GetListOfKeys()]
553  return fnmatch.filter(names, pattern)
def python.rootplot.utilities.find_num_processors ( )

Definition at line 512 of file utilities.py.

Referenced by python.rootplot.core.parse_arguments().

513 def find_num_processors():
514  import os
515  try:
516  num_processors = os.sysconf('SC_NPROCESSORS_ONLN')
517  except:
518  try:
519  num_processors = os.environ['NUMBER_OF_PROCESSORS']
520  except:
521  num_processors = 1
522  return num_processors
def python.rootplot.utilities.get (   object_name)
Return a Hist object with the given name.

Definition at line 428 of file utilities.py.

429 def get(object_name):
430  """Return a Hist object with the given name."""
431  return Hist(ROOT.gDirectory.Get(object_name))
432 
def python.rootplot.utilities.has_glob_magic (   s)

Definition at line 539 of file utilities.py.

Referenced by python.rootplot.utilities.irootglob().

540 def has_glob_magic(s):
541  return glob_magic_check.search(s) is not None
542 
543 # These 2 helper functions non-recursively glob inside a literal directory.
544 # They return a list of basenames. `_rootglob1` accepts a pattern while
545 # `_rootglob0` takes a literal basename (so it only has to check for its
546 # existence).
def python.rootplot.utilities.irootglob (   tdirectory,
  pathname 
)
Return an iterator which yields the paths matching a pathname pattern.

The pattern may contain simple shell-style wildcards a la fnmatch.

Definition at line 577 of file utilities.py.

References python.rootplot.utilities.has_glob_magic().

Referenced by python.rootplot.utilities.rootglob().

578 def irootglob(tdirectory, pathname):
579  """Return an iterator which yields the paths matching a pathname pattern.
580 
581  The pattern may contain simple shell-style wildcards a la fnmatch.
582 
583  """
584  if not has_glob_magic(pathname):
585  if tdirectory.Get(pathname):
586  yield pathname
587  return
588  dirname, basename = os.path.split(pathname)
589  if has_glob_magic(dirname):
590  dirs = irootglob(tdirectory, dirname)
591  else:
592  dirs = [dirname]
593  if has_glob_magic(basename):
594  glob_in_dir = _rootglob1
595  else:
596  glob_in_dir = _rootglob0
597  for dirname in dirs:
598  for name in glob_in_dir(tdirectory, dirname, basename):
599  yield os.path.join(dirname, name)
def python.rootplot.utilities.loadROOT (   batch = True)

Define additional helping functions.

Definition at line 435 of file utilities.py.

References join(), and print().

436 def loadROOT(batch=True):
437  ## We need to temporarily change sys.argv so that ROOT doesn't intercept
438  ## options from the command-line
439  saved_argv = sys.argv[:]
440  argstring = ' '.join(sys.argv)
441  sys.argv = [sys.argv[0]]
442  try:
443  import ROOT
444  except ImportError:
445  print("""\
446 The program was unable to access PyROOT. Usually, this just requires switching
447 to the same major version of python that used when compiling ROOT. To
448 determine which version that is, try the following command:
449  root -config 2>&1 | tr ' ' '\\n' | egrep 'python|PYTHON'
450 If this is different from the python version you are currently using, try
451 changing your PATH to point to the new one.""")
452  sys.exit(1)
453  ## Enter batch mode, unless outputting to C macros
454  ## There is a bug in pyROOT that fails to export colors in batch mode
455  if batch:
456  ROOT.gROOT.SetBatch()
457  ROOT.gErrorIgnoreLevel = ROOT.kWarning
458  ## PyROOT picks up ~/.rootlogon if it exists, but not ./rootlogon.C
459  if os.path.exists('rootlogon.C'):
460  ROOT.gROOT.Macro('rootlogon.C')
461  sys.argv = saved_argv[:]
462  return ROOT
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def loadROOT
Define additional helping functions.
Definition: utilities.py:435
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def python.rootplot.utilities.ls (   directory = None)
Return a python list of ROOT object names from the given directory.

Definition at line 410 of file utilities.py.

411 def ls(directory=None):
412  """Return a python list of ROOT object names from the given directory."""
413  if directory == None:
414  keys = ROOT.gDirectory.GetListOfKeys()
415  else:
416  keys = ROOT.gDirectory.GetDirectory(directory).GetListOfKeys()
417  key = keys[0]
418  names = []
419  while key:
420  obj = key.ReadObj()
421  key = keys.After(key)
422  names.append(obj.GetName())
423  return names
def python.rootplot.utilities.process_bin_labels (   binlabels)

Definition at line 486 of file utilities.py.

487 def process_bin_labels(binlabels):
488  has_labels = False
489  for binlabel in binlabels:
490  if binlabel:
491  has_labels = True
492  if has_labels:
493  return binlabels
494  else:
495  return None
def python.rootplot.utilities.pwd ( )
Return ROOT's present working directory.

Definition at line 424 of file utilities.py.

425 def pwd():
426  """Return ROOT's present working directory."""
427  return ROOT.gDirectory.GetPath()
def python.rootplot.utilities.replace (   string,
  replacements 
)
Modify a string based on a list of patterns and substitutions.

replacements should be a list of two-entry tuples, the first entry giving
a string to search for and the second entry giving the string with which
to replace it.  If replacements includes a pattern entry containing
'use_regexp', then all patterns will be treated as regular expressions
using re.sub.

Definition at line 463 of file utilities.py.

464 def replace(string, replacements):
465  """
466  Modify a string based on a list of patterns and substitutions.
467 
468  replacements should be a list of two-entry tuples, the first entry giving
469  a string to search for and the second entry giving the string with which
470  to replace it. If replacements includes a pattern entry containing
471  'use_regexp', then all patterns will be treated as regular expressions
472  using re.sub.
473  """
474  if not replacements:
475  return string
476  if 'use_regexp' in [x for x,y in replacements]:
477  for pattern, repl in [x for x in replacements
478  if x[0] != 'use_regexp']:
479  string = re.sub(pattern, repl, string)
480  else:
481  for pattern, repl in replacements:
482  string = string.replace(pattern, repl)
483  if re.match(_all_whitespace_string, string):
484  return ""
485  return string
def python.rootplot.utilities.rootglob (   tdirectory,
  pathname 
)
Return a list of paths matching a pathname pattern.

The pattern may contain simple shell-style wildcards a la fnmatch.

>>> import rootplot.utilities
>>> f = rootplot.utilities.testfile()
>>> rootglob(f, '*')
['dir1', 'dir2', 'dir3', 'dir4']
>>> rootglob(f, 'dir1/*')
['dir1/hist1', 'dir1/hist2', 'dir1/hist3', 'dir1/hist4']
>>> rootglob(f, '*/hist1')
['dir1/hist1', 'dir2/hist1', 'dir3/hist1', 'dir4/hist1']
>>> rootglob(f, 'dir1/hist[1-2]')
['dir1/hist1', 'dir1/hist2']

Definition at line 559 of file utilities.py.

References python.rootplot.utilities.irootglob().

Referenced by python.rootplot.rootmath.main(), and python.rootplot.rootmath.newadd().

560 def rootglob(tdirectory, pathname):
561  """Return a list of paths matching a pathname pattern.
562 
563  The pattern may contain simple shell-style wildcards a la fnmatch.
564 
565  >>> import rootplot.utilities
566  >>> f = rootplot.utilities.testfile()
567  >>> rootglob(f, '*')
568  ['dir1', 'dir2', 'dir3', 'dir4']
569  >>> rootglob(f, 'dir1/*')
570  ['dir1/hist1', 'dir1/hist2', 'dir1/hist3', 'dir1/hist4']
571  >>> rootglob(f, '*/hist1')
572  ['dir1/hist1', 'dir2/hist1', 'dir3/hist1', 'dir4/hist1']
573  >>> rootglob(f, 'dir1/hist[1-2]')
574  ['dir1/hist1', 'dir1/hist2']
575  """
576  return list(irootglob(tdirectory, pathname))
def python.rootplot.utilities.testfile ( )

Definition at line 523 of file utilities.py.

References sistrip::SpyUtilities.range().

524 def testfile():
525  outfile = ROOT.TFile("test.root", "recreate")
526  for i in range(4):
527  d = outfile.mkdir("dir%i" % (i + 1))
528  d.cd()
529  for j in range(4):
530  hist = ROOT.TH1F("hist%i" % (j + 1), "A Histogram", 10, 0, 10)
531  hist.Fill(j)
532  hist.Write()
533  outfile.Write()
534  return outfile
const uint16_t range(const Frame &aFrame)
def python.rootplot.utilities.wilson_interval (   numerator_array,
  denominator_array 
)

Definition at line 496 of file utilities.py.

References ComparisonHelper.zip().

Referenced by python.rootplot.utilities.Hist.divide_wilson().

497 def wilson_interval(numerator_array, denominator_array):
498  eff, upper_err, lower_err = [], [], []
499  for n, d in zip(numerator_array, denominator_array):
500  try:
501  p = float(n) / d
502  s = math.sqrt(p * (1 - p) / d + 1 / (4 * d * d))
503  t = p + 1 / (2 * d)
504  eff.append(p)
505  upper_err.append(-p + 1/(1 + 1/d) * (t + s))
506  lower_err.append(+p - 1/(1 + 1/d) * (t - s))
507  except ZeroDivisionError:
508  eff.append(0)
509  upper_err.append(0)
510  lower_err.append(0)
511  return eff, upper_err, lower_err
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)

Variable Documentation

string python.rootplot.utilities.__license__
Initial value:
1 = '''\
2 Copyright (c) 2009-2010 Jeff Klukas <klukas@wisc.edu>
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 '''

Definition at line 7 of file utilities.py.

tuple python.rootplot.utilities.glob_magic_check = re.compile('[*?[]')

Functions for globbing within root files.

Definition at line 537 of file utilities.py.