CMS 3D CMS Logo

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