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  if indices.Type == 1000:
40  # nothing is stored here
41  # see https://github.com/cms-sw/cmssw/blob/8be445ac6fd9983d69156199d4d1fd3350f05d92/DQMServices/FwkIO/plugins/DQMRootOutputModule.cc#L437
42  continue
43 
44  object_type = self.DQMIO_TYPES[indices.Type]
45  t_tree = self._root_file.Get(object_type)
46 
47  for i in xrange(indices.FirstIndex, indices.LastIndex + 1):
48  t_tree.GetEntry(i)
49 
50  fullname = str(t_tree.FullName)
51  yield (fullname, t_tree.Value, )
52 
53  def read_objects_root(self):
54  xml_re = re.compile(r"^<(.+)>(.+)=(.+)<\/\1>$")
55  def parse_directory(di):
56  directory = self._root_file.GetDirectory(di)
57  for key in directory.GetListOfKeys():
58  entry = key.GetName()
59  rtype = key.GetClassName()
60  fullpath = "%s/%s" % (di, entry)
61 
62  if (rtype == "TDirectoryFile"):
63  for k, v in parse_directory(fullpath):
64  yield (k, v, )
65  else:
66  obj = self._root_file.Get(fullpath)
67  if obj:
68  yield (fullpath, obj, )
69  else:
70  # special case to parse the xml abomination
71  m = xml_re.search(entry)
72  if m:
73  name = m.group(1)
74  typecode = m.group(2)
75  value = m.group(3)
76 
77  fp = "%s/%s" % (di, name)
78  yield (fp, value, )
79  else:
80  raise Exception("Invalid xml:" + entry)
81 
82 
83  path_fix = re.compile(r"^\/Run \d+")
84  for fullname, obj in parse_directory(""):
85  f = fullname.replace("/DQMData", "")
86  f = f.replace("/Run summary", "")
87  f = path_fix.sub(r"", f)
88  if f[0] == "/":
89  f = f[1:]
90 
91  yield f, obj
92 
93  def close(self):
94  self._root_file.Close()
95 
96 if __name__ == '__main__':
97  import argparse
98 
99  parser = argparse.ArgumentParser()
100  parser.add_argument("-i", "--input", help = "Input DQMIO ROOT file")
101  args = parser.parse_args()
102 
103  reader = DQMReader(args.input)
104 
105  for (fn, v) in reader.read_objects():
106  if (hasattr(v, "ClassName")):
107  print fn, v.ClassName()
108  else:
109  print fn, type(v)
110 
111  reader.close()
def read_objects(self)
Definition: DQM.py:26
def close(self)
Definition: DQM.py:93
def read_objects_dqmio(self)
Definition: DQM.py:32
def __init__(self, input_filename)
Definition: DQM.py:17
_root_file
Definition: DQM.py:18
#define str(s)
def read_objects_root(self)
Definition: DQM.py:53