CMS 3D CMS Logo

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