00001 """
00002 Print information about objects in a ROOT file.
00003 """
00004
00005 from version import __version__
00006
00007 from ROOT import Double
00008 import copy
00009 import argparse
00010 import sys
00011 import os
00012
00013
00014 saved_argv = sys.argv[:]
00015 sys.argv = [sys.argv[0], '-b']
00016 from ROOT import TFile, TH1, TDirectory, gDirectory
00017 sys.argv = saved_argv
00018
00019 def recurse_thru_file(in_tfile, options, full_path='/'):
00020 '''Recursive function to find all contents in a given ROOT file'''
00021 keys = in_tfile.GetDirectory(full_path).GetListOfKeys()
00022 for key in keys:
00023 name = key.GetName()
00024 classname = key.GetClassName()
00025 if 'TDirectory' in classname:
00026 gDirectory.cd(name)
00027 recurse_thru_file(in_tfile, options, '/'.join([full_path,name]))
00028 gDirectory.cd("..")
00029 else:
00030 if options.name and name != options.name: continue
00031 full_name = '/'.join([full_path,name])
00032 obj = in_tfile.Get(full_name)
00033 if not obj:
00034 continue
00035 simple_name = full_name[2:]
00036 print "%s" % simple_name,
00037 for arg in [x[2:] for x in sys.argv if x.startswith("--")]:
00038 if "classname" == arg:
00039 print "%s" % classname,
00040 if obj.InheritsFrom('TH1'):
00041 if "entries" == arg:
00042 print " %i" % obj.GetEntries(),
00043 if "contents" == arg:
00044 if obj.InheritsFrom('TH2'):
00045
00046
00047 for j in reversed(range(obj.GetNbinsY())):
00048 print
00049 print " %s" % ' '.join(
00050 [str(obj.GetBinContent(i+1, j+1)) for i in range(obj.GetNbinsX())]),
00051 else:
00052 print " %s" % ' '.join(
00053 [str(obj.GetBinContent(i+1)) for i in range(obj.GetNbinsX())]),
00054 if "errors" == arg:
00055 if obj.InheritsFrom('TH2'):
00056 for j in reversed(range(obj.GetNbinsY())):
00057 print
00058 print " %s" % ' '.join(
00059 [str(obj.GetBinError(i+1, j+1)) for i in range(obj.GetNbinsX())]),
00060 else:
00061 print " %s" % ' '.join(
00062 [str(obj.GetBinError(i+1)) for i in range(obj.GetNbinsX())]),
00063 if "bincenter" == arg:
00064 print " %s" % ' '.join(
00065 [str(obj.GetBinCenter(i+1)) for i in range(obj.GetNbinsX())]),
00066 if "max" == arg:
00067 print " %i" % obj.GetMaximum(),
00068 if "min" == arg:
00069 print " %i" % obj.GetMinimum(),
00070 if "overflow" == arg:
00071 print " %i" % obj.GetBinContent(obj.GetNbinsX()),
00072 if "underflow" == arg:
00073 print " %i" % obj.GetBinContent(0),
00074 if obj.InheritsFrom('TGraph'):
00075 if "contents" == arg:
00076 x, y = Double(0), Double(0)
00077 xvals = []
00078 yvals = []
00079 for i in range(obj.GetN()):
00080 obj.GetPoint(i, x, y)
00081 xvals.append(copy.copy(x))
00082 yvals.append(copy.copy(y))
00083 for point in zip(xvals,yvals):
00084 print " (%d, %d)" % point,
00085 print ""
00086
00087 def main():
00088 parser = argparse.ArgumentParser(description='Print information from an SC2 replay file.')
00089 parser.add_argument('filenames', metavar='filename', type=str, nargs='+',
00090 help="Names of one or more root files")
00091 parser.add_argument('--bincenter', action="store_true", default=False,
00092 help="Get Bin Centers from each bin in each histogram")
00093 parser.add_argument('--classname', action="store_true", default=False,
00094 help="Get type from each object in root file")
00095 parser.add_argument('--contents', action="store_true", default=False,
00096 help="Get Bin Contents from each bin in each histogram")
00097 parser.add_argument('--errors', action="store_true", default=False,
00098 help="Get Bin Errors from each bin in each histogram")
00099 parser.add_argument('--entries', action="store_true", default=False,
00100 help="Get Entries from each histogram")
00101 parser.add_argument('--max', action="store_true", default=False,
00102 help="Get Maximum value from each histogram")
00103 parser.add_argument('--min', action="store_true", default=False,
00104 help="Get Minimum value from each histogram")
00105 parser.add_argument('--name', default=None,
00106 help="Get information only from object with matching name")
00107 parser.add_argument('--overflow', action="store_true", default=False,
00108 help="Get value of overflow bin from each histogram")
00109 parser.add_argument('--underflow', action="store_true", default=False,
00110 help="Get value of underflow bin from each histogram")
00111 arguments = parser.parse_args()
00112 for arg in arguments.filenames:
00113 if arg[-5:] != ".root":
00114 raise TypeError("Arguments must include root file names")
00115 filenames_from_interface = [x for x in arguments.filenames if x[-5:] == ".root"]
00116 if len(filenames_from_interface) == 0:
00117 parser.print_help()
00118 sys.exit(0)
00119 for filename in filenames_from_interface:
00120 if not os.path.exists(filename):
00121 print "%s does not exist." % filename
00122 sys.exit(0)
00123 tfile = TFile(filename, "read")
00124 try:
00125 recurse_thru_file(tfile, arguments)
00126 except IOError, e:
00127 if e.errno != 32:
00128 raise
00129
00130 if __name__ == '__main__':
00131 main()