GRSISort
Created by P.C. Bender
Developement Team: P.C. Bender, R. Dunlop, V. Bildstein
An extension of the ROOT analysis Framework
util.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 
3 import re
4 
5 import ROOT
6 ROOT.PyConfig.IgnoreCommandLineOptions = True
7 
8 def unpack_tdirectory(tdir):
9  if tdir.GetListOfKeys():
10  for key in tdir.GetListOfKeys():
11  yield key.ReadObj()
12  else:
13  for obj in tdir.GetList():
14  yield obj
15 
16 def update_tcanvases(objects=None):
17  if objects is None:
18  objects = ROOT.gROOT.GetListOfCanvases()
19 
20  for obj in objects:
21  if isinstance(obj, ROOT.TPad):
22  obj.Modified()
23  obj.Update()
24  update_tcanvases(obj.GetListOfPrimitives())
25 
26 
27 def increment_name(name):
28  res = re.search('[0-9]+$', name)
29  if res:
30  prefix = name[:-len(res.group())]
31  number = int(res.group()) + 1
32  return prefix + str(number)
33  else:
34  return name + '_1'
35 
36 
37 class PreserveGDir(object):
38 
39  def __init__(self, directory = None):
40  self.directory = directory
41 
42  def __enter__(self):
43  self.gdir = ROOT.gDirectory
44  if self.directory is not None:
45  self.directory.cd()
46  return self
47 
48  def __exit__(self, exc_type, exc_val, exc_tb):
49  self.gdir.cd()
50 
51 
52 class TKeyDict(dict):
53  def __getitem__(self, key):
54  output = super(TKeyDict,self).__getitem__(key)
55  if isinstance(output, ROOT.TKey):
56  output = output.ReadObj()
57  if (isinstance(output, ROOT.TH2) and
58  not isinstance(output, ROOT.GH2Base)):
59  output = ROOT.GH2D(output)
60  self[key] = output
61 
62  return output
63 
64  def is_tkey(self, key):
65  output = super(TKeyDict,self).__getitem__(key)
66  try:
67  return isinstance(output, ROOT.TKey)
68  except AttributeError:
69  return False
def __enter__(self)
Definition: util.py:42
def __getitem__(self, key)
Definition: util.py:53
def increment_name(name)
Definition: util.py:27
def unpack_tdirectory(tdir)
Definition: util.py:8
def __init__(self, directory=None)
Definition: util.py:39
def __exit__(self, exc_type, exc_val, exc_tb)
Definition: util.py:48
def update_tcanvases(objects=None)
Definition: util.py:16
def is_tkey(self, key)
Definition: util.py:64