CMS 3D CMS Logo

DQMIO2histo.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 """
4 Script converting DQM I/O format input file into folder structured ROOT file.
5 Ouput files historgrams are easy browseable by ROOT. When there are more than 1 run
6 in input file it creates a Run X named folder for each run.
7 Thanks for Marco Rovere for giving example script/class needed to browse DQM I/O
8 formatted input.
9 """
10 from __future__ import print_function
11 
12 import ROOT as R
13 import sys
14 import re
15 import os
16 import argparse
17 
18 class DQMIO:
19  """
20  Class responsible for browsing the content of a DQM file produced
21  with the DQMIO I/O framework of CMSSW
22  """
23  types=["Ints","Floats","Strings", ##defined DQMIO types
24  "TH1Fs","TH1Ss","TH1Ds",
25  "TH2Fs", "TH2Ss", "TH2Ds",
26  "TH3Fs", "TProfiles","TProfile2Ds", "kNIndicies"]
27 
28  def __init__(self, input_filename, output_filename):
29  self._filename = input_filename
30  self._canvas = None
31  self.f = R.TFile(output_filename, "RECREATE")
32  self.already_defined = {"TProfiles" : False, "TProfile2Ds" : False,
33  "TH2Fs" : False, "TH2Ds" : False}
34 
35  if os.path.exists(self._filename): #we try to open input file if fail
36  self._root_file = R.TFile.Open(self._filename) #-> close script
37  if args.debug:
38  print("## DEBUG ##:")
39  print(" Input: %s\n Output: %s" % (input_filename,
40  output_filename))
41 
42  else:
43  print("File %s does not exists" % self._filename)
44  sys.exit(1)
45 
46  def print_index(self):
47  """
48  Loop over the complete index and dump it on the screen.
49  """
50  indices = self._root_file.Get("Indices")
51  if args.debug:
52  print("## DEBUG ##:")
53  print("Run,\tLumi,\tType,\t\tFirstIndex,\tLastIndex")
54  for i in xrange(indices.GetEntries()):
55  indices.GetEntry(i)
56  print('{0:4d}\t{1:4d}\t{2:4d}({3:s})\t\t{4:4d}\t{5:4d}'.format(
57  indices.Run, indices.Lumi, indices.Type,
58  DQMIO.types[indices.Type], indices.FirstIndex, indices.LastIndex))
59 
60  for i in xrange(indices.GetEntries()):
61  indices.GetEntry(i)
62  if indices.Type < len(DQMIO.types):
63  self.write_to_file(self.types[indices.Type],
64  [indices.FirstIndex,indices.LastIndex], str(indices.Run))
65 
66  else:
67  print("Unknown histogram type. Type numer: %s" % (indices.Type))
68  self.f.Close()
69 
70  def write_to_file(self, hist_type, index_range, run):
71  """
72  Method looping over entries for specified histogram type and
73  writing to FullName path to output ROOT File
74  """
75  print("Working on: %s indexes: %s..%s" % (hist_type ,index_range[0],
76  index_range[1]))
77  t_tree = self._root_file.Get(hist_type)
78  __run_dir = "Run %s" % (run)
79  ###we set Branch for the needed type
80  if hist_type == "TProfiles":
81  if not self.already_defined["TProfiles"]:
82  R.gROOT.ProcessLine("TProfile* _tprof;")
83  self.already_defined["TProfiles"] = True
84  t_tree.SetBranchAddress("Value", R._tprof)
85  t_tree.GetEntry(index_range[0])
86  elif hist_type == "TProfile2Ds":
87  if not self.already_defined["TProfile2Ds"]:
88  R.gROOT.ProcessLine("TProfile2D* _tprof2d;")
89  self.already_defined["TProfile2Ds"] = True
90  t_tree.SetBranchAddress("Value", R._tprof2d)
91  t_tree.GetEntry(index_range[0])
92  elif hist_type == "TH2Fs":
93  if not self.already_defined["TH2Fs"]:
94  R.gROOT.ProcessLine("TH2F* _th2f;")
95  self.already_defined["TH2Fs"] = True
96  t_tree.SetBranchAddress("Value", R._th2f)
97  t_tree.GetEntry(index_range[0])
98  elif hist_type == "TH2Ds":
99  if not self.already_defined["TH2Ds"]:
100  R.gROOT.ProcessLine("TH2D* _th2d;")
101  self.already_defined["TH2Ds"] = True
102  t_tree.SetBranchAddress("Value", R._th2d)
103  t_tree.GetEntry(index_range[0])
104 
105  for i in range(0,t_tree.GetEntries()+1): ##iterate on entries for specified type
106  if i >= index_range[0] and i <= index_range[1]: ##if entries as in range:
107  t_tree.GetEntry(i)
108  name = str(t_tree.FullName)
109  # print " %s: %s" % (i, name)
110  file_path = name.split("/")[:-1] ## same run/lumi histograms
111  __directory = "%s/%s" % (os.path.join("DQMData", __run_dir),
112  "/".join(file_path))
113  directory_ret = self.f.GetDirectory(__directory)
114  if not directory_ret:
115  self.f.mkdir(os.path.join(__directory))
116  self.f.cd(os.path.join(__directory))
117  if hist_type == "Strings":
118  construct_str = '<%s>s=%s</%s>' % (name.split("/")[-1:][0],
119  t_tree.Value, name.split("/")[-1:][0])
120  tmp_str = R.TObjString(construct_str)
121  tmp_str.Write()
122  elif hist_type == "Ints":
123  construct_str = '<%s>i=%s</%s>' % (name.split("/")[-1:][0],
124  t_tree.Value, name.split("/")[-1:][0])
125  tmp_str = R.TObjString(construct_str)
126  tmp_str.Write()
127  elif hist_type == "Floats":
128  construct_str = '<%s>f=%s</%s>' % (name.split("/")[-1:][0],
129  t_tree.Value, name.split("/")[-1:][0])
130  tmp_str = R.TObjString(construct_str)
131  tmp_str.Write()
132  else:
133  if hist_type in ["TProfiles", "TProfile2Ds", "TH2Fs", "TH2Ds"]:
134  if hist_type == "TProfiles": #if type is specific we write it.
135  R._tprof.Write()
136  elif hist_type == "TProfile2Ds":
137  R._tprof2d.Write()
138  elif hist_type == "TH2Fs":
139  R._th2f.Write()
140  elif hist_type == "TH2Ds":
141  R._th2d.Write()
142  else: #else we wirte Leafs Value which is a histogram
143  t_tree.Value.Write()
144 
145 if __name__ == '__main__':
146  parser = argparse.ArgumentParser()
147  parser.add_argument("-in", "--input", help = "Input DQMIO ROOT file")
148  parser.add_argument("-o", "--output", help = "Output filename",
149  default = "DQMIO_converter_output.root")
150  parser.add_argument("--debug", help = "Debug mode to spam you console",
151  action = "store_true")
152 
153  args = parser.parse_args()
154  __in_file = args.input
155  __out_file = args.output
156  dqmio = DQMIO(__in_file, __out_file)
157  dqmio.print_index()
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def print_index(self)
Definition: DQMIO2histo.py:46
def __init__(self, input_filename, output_filename)
defined DQMIO types
Definition: DQMIO2histo.py:28
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def write_to_file(self, hist_type, index_range, run)
Definition: DQMIO2histo.py:70
#define str(s)