CMS 3D CMS Logo

Classes | Functions | Variables
python.rootplot.utilities Namespace Reference

Classes

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

Functions

def _rootglob0 (tdirectory, dirname, basename)
 
def _rootglob1 (tdirectory, dirname, pattern)
 
def find_num_processors ()
 
def get (object_name)
 
def has_glob_magic (s)
 
def irootglob (tdirectory, pathname)
 
def loadROOT (batch=True)
 Define additional helping functions. More...
 
def ls (directory=None)
 
def process_bin_labels (binlabels)
 
def pwd ()
 
def replace (string, replacements)
 
def rootglob (tdirectory, pathname)
 
def testfile ()
 
def wilson_interval (numerator_array, denominator_array)
 

Variables

 __license__
 
 glob_magic_check
 Functions for globbing within root files. More...
 

Detailed Description

Utilities for rootplot including histogram classes.

Function Documentation

◆ _rootglob0()

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

Definition at line 554 of file utilities.py.

554 def _rootglob0(tdirectory, dirname, basename):
555  if tdirectory.Get(os.path.join(dirname, basename)):
556  return [basename]
557  return []
558 
def _rootglob0(tdirectory, dirname, basename)
Definition: utilities.py:554

◆ _rootglob1()

def python.rootplot.utilities._rootglob1 (   tdirectory,
  dirname,
  pattern 
)
private

Definition at line 547 of file utilities.py.

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

◆ find_num_processors()

def python.rootplot.utilities.find_num_processors ( )

Definition at line 512 of file utilities.py.

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

512 def find_num_processors():
513  import os
514  try:
515  num_processors = os.sysconf('SC_NPROCESSORS_ONLN')
516  except:
517  try:
518  num_processors = os.environ['NUMBER_OF_PROCESSORS']
519  except:
520  num_processors = 1
521  return num_processors
522 

◆ get()

def python.rootplot.utilities.get (   object_name)
Return a Hist object with the given name.

Definition at line 428 of file utilities.py.

428 def get(object_name):
429  """Return a Hist object with the given name."""
430  return Hist(ROOT.gDirectory.Get(object_name))
431 
432 
cms::alpakatools::HistoContainer< uint8_t, 256, 16000, 8, uint16_t > Hist

◆ has_glob_magic()

def python.rootplot.utilities.has_glob_magic (   s)

Definition at line 539 of file utilities.py.

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

539 def has_glob_magic(s):
540  return glob_magic_check.search(s) is not None
541 
542 # These 2 helper functions non-recursively glob inside a literal directory.
543 # They return a list of basenames. `_rootglob1` accepts a pattern while
544 # `_rootglob0` takes a literal basename (so it only has to check for its
545 # existence).
546 

◆ irootglob()

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

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

◆ loadROOT()

def python.rootplot.utilities.loadROOT (   batch = True)

Define additional helping functions.

Definition at line 435 of file utilities.py.

References join(), and print().

435 def loadROOT(batch=True):
436 
438  saved_argv = sys.argv[:]
439  argstring = ' '.join(sys.argv)
440  sys.argv = [sys.argv[0]]
441  try:
442  import ROOT
443  except ImportError:
444  print("""\
445 The program was unable to access PyROOT. Usually, this just requires switching
446 to the same major version of python that used when compiling ROOT. To
447 determine which version that is, try the following command:
448  root -config 2>&1 | tr ' ' '\\n' | egrep 'python|PYTHON'
449 If this is different from the python version you are currently using, try
450 changing your PATH to point to the new one.""")
451  sys.exit(1)
452 
454  if batch:
455  ROOT.gROOT.SetBatch()
456  ROOT.gErrorIgnoreLevel = ROOT.kWarning
457 
458  if os.path.exists('rootlogon.C'):
459  ROOT.gROOT.Macro('rootlogon.C')
460  sys.argv = saved_argv[:]
461  return ROOT
462 
def loadROOT(batch=True)
Define additional helping functions.
Definition: utilities.py:435
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:21

◆ ls()

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.

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

◆ process_bin_labels()

def python.rootplot.utilities.process_bin_labels (   binlabels)

Definition at line 486 of file utilities.py.

486 def process_bin_labels(binlabels):
487  has_labels = False
488  for binlabel in binlabels:
489  if binlabel:
490  has_labels = True
491  if has_labels:
492  return binlabels
493  else:
494  return None
495 
def process_bin_labels(binlabels)
Definition: utilities.py:486

◆ pwd()

def python.rootplot.utilities.pwd ( )
Return ROOT's present working directory.

Definition at line 424 of file utilities.py.

424 def pwd():
425  """Return ROOT's present working directory."""
426  return ROOT.gDirectory.GetPath()
427 

◆ replace()

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.

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

◆ rootglob()

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

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

◆ testfile()

def python.rootplot.utilities.testfile ( )

Definition at line 523 of file utilities.py.

References FastTimerService_cff.range.

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

◆ wilson_interval()

def python.rootplot.utilities.wilson_interval (   numerator_array,
  denominator_array 
)

Definition at line 496 of file utilities.py.

References nano_mu_digi_cff.float, and reco.zip().

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

496 def wilson_interval(numerator_array, denominator_array):
497  eff, upper_err, lower_err = [], [], []
498  for n, d in zip(numerator_array, denominator_array):
499  try:
500  p = float(n) / d
501  s = math.sqrt(p * (1 - p) / d + 1 / (4 * d * d))
502  t = p + 1 / (2 * d)
503  eff.append(p)
504  upper_err.append(-p + 1/(1 + 1/d) * (t + s))
505  lower_err.append(+p - 1/(1 + 1/d) * (t - s))
506  except ZeroDivisionError:
507  eff.append(0)
508  upper_err.append(0)
509  lower_err.append(0)
510  return eff, upper_err, lower_err
511 
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr float zip(ConstView const &tracks, int32_t i)
Definition: TracksSoA.h:90
def wilson_interval(numerator_array, denominator_array)
Definition: utilities.py:496

Variable Documentation

◆ __license__

python.rootplot.utilities.__license__
private

Definition at line 7 of file utilities.py.

◆ glob_magic_check

python.rootplot.utilities.glob_magic_check

Functions for globbing within root files.

Definition at line 537 of file utilities.py.