CMS 3D CMS Logo

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