Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- """\
- Usage: %prog <rootfile>
- List the contents of a ROOT file, and their types.
- The format can be controlled a bit -- see the options.
- TODO:
- * Add regex path filtering
- """
- ## Set up argument parser
- import optparse, sys
- op = optparse.OptionParser(usage=__doc__)
- fchoices = "fancy|plain|minimal"
- op.add_option("-f", "--format", dest="FORMAT", type="choice", choices=fchoices.split("|"),
- help="choose the printout format from %s (default: %%default)" % fchoices, default="fancy")
- ## Handle arguments
- opts, args = op.parse_args()
- try:
- fname = args[0]
- except:
- op.print_usage()
- sys.exit(1)
- import ROOT
- ROOT.gROOT.SetBatch(True)
- def fancylistall(d, indent=""):
- "Print out all contents of a "
- for key in d.GetListOfKeys():
- kname = key.GetName()
- if key.IsFolder():
- print indent+"D", kname
- fancylistall(d.Get(kname), indent+" ")
- else:
- print "%s%s %s" % (indent, d.Get(kname).ClassName(), kname)
- def getall(d, basepath="/"):
- for key in d.GetListOfKeys():
- kname = key.GetName()
- if key.IsFolder():
- # TODO: -> "yield from" in Py3
- for i in getall(d.Get(kname), basepath+kname+"/"):
- yield i
- else:
- yield basepath+kname, d.Get(kname)
- ## Loop over the file contents
- f = ROOT.TFile(fname)
- print "Listing for ROOT file '%s':\n" % fname
- if opts.FORMAT == "fancy":
- fancylistall(f)
- else:
- for k, o in getall(f):
- t = o.ClassName()+" " if opts.FORMAT != "minimal" else ""
- print t+k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement