CMS 3D CMS Logo

DQM.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 import ROOT as R
4 import os, re
5 
7  """
8  Reader for DQM IO and DQM root files.
9  """
10 
11  #defined DQMIO types, index is important!
12  DQMIO_TYPES = ["Ints","Floats","Strings",
13  "TH1Fs","TH1Ss","TH1Ds",
14  "TH2Fs", "TH2Ss", "TH2Ds",
15  "TH3Fs", "TProfiles","TProfile2Ds", "kNIndicies"]
16 
17  def __init__(self, input_filename):
18  self._root_file = R.TFile.Open(input_filename)
19 
20  ioTest = self._root_file.Get("Indices")
21  if bool(ioTest):
22  self.type = "DQMIO"
23  else:
24  self.type = "ROOT"
25 
26  def read_objects(self):
27  if (self.type == "DQMIO"):
28  return self.read_objects_dqmio()
29  else:
30  return self.read_objects_root()
31 
32  def read_objects_dqmio(self):
33  indices = self._root_file.Get("Indices")
34 
35  for y in xrange(indices.GetEntries()):
36  indices.GetEntry(y)
37  # print indices.Run, indices.Lumi, indices.Type
38 
39  object_type = self.DQMIO_TYPES[indices.Type]
40  t_tree = self._root_file.Get(object_type)
41 
42  for i in xrange(indices.FirstIndex, indices.LastIndex + 1):
43  t_tree.GetEntry(i)
44 
45  fullname = str(t_tree.FullName)
46  yield (fullname, t_tree.Value, )
47 
48  def read_objects_root(self):
49  xml_re = re.compile(r"^<(.+)>(.+)=(.+)<\/\1>$")
50  def parse_directory(di):
51  directory = self._root_file.GetDirectory(di)
52  for key in directory.GetListOfKeys():
53  entry = key.GetName()
54  rtype = key.GetClassName()
55  fullpath = "%s/%s" % (di, entry)
56 
57  if (rtype == "TDirectoryFile"):
58  for k, v in parse_directory(fullpath):
59  yield (k, v, )
60  else:
61  obj = self._root_file.Get(fullpath)
62  if obj:
63  yield (fullpath, obj, )
64  else:
65  # special case to parse the xml abomination
66  m = xml_re.search(entry)
67  if m:
68  name = m.group(1)
69  typecode = m.group(2)
70  value = m.group(3)
71 
72  fp = "%s/%s" % (di, name)
73  yield (fp, value, )
74  else:
75  raise Exception("Invalid xml:" + entry)
76 
77 
78  path_fix = re.compile(r"^\/Run \d+")
79  for fullname, obj in parse_directory(""):
80  f = fullname.replace("/DQMData", "")
81  f = f.replace("/Run summary", "")
82  f = path_fix.sub(r"", f)
83  if f[0] == "/":
84  f = f[1:]
85 
86  yield f, obj
87 
88  def close(self):
89  self._root_file.Close()
90 
91 if __name__ == '__main__':
92  import argparse
93 
94  parser = argparse.ArgumentParser()
95  parser.add_argument("-i", "--input", help = "Input DQMIO ROOT file")
96  args = parser.parse_args()
97 
98  reader = DQMReader(args.input)
99 
100  for (fn, v) in reader.read_objects():
101  if (hasattr(v, "ClassName")):
102  print fn, v.ClassName()
103  else:
104  print fn, type(v)
105 
106  reader.close()
def read_objects(self)
Definition: DQM.py:26
def close(self)
Definition: DQM.py:88
def read_objects_dqmio(self)
Definition: DQM.py:32
def __init__(self, input_filename)
Definition: DQM.py:17
_root_file
Definition: DQM.py:18
def read_objects_root(self)
Definition: DQM.py:48