Advertisement
andybuckley

rootls -- list the contents of ROOT files from the terminal

Nov 18th, 2015
350
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None
  1. #! /usr/bin/env python
  2.  
  3. """\
  4. Usage: %prog <rootfile>
  5.  
  6. List the contents of a ROOT file, and their types.
  7. The format can be controlled a bit -- see the options.
  8.  
  9. TODO:
  10.  * Add regex path filtering
  11. """
  12.  
  13. ## Set up argument parser
  14. import optparse, sys
  15. op = optparse.OptionParser(usage=__doc__)
  16. fchoices = "fancy|plain|minimal"
  17. op.add_option("-f", "--format", dest="FORMAT", type="choice", choices=fchoices.split("|"),
  18.               help="choose the printout format from %s (default: %%default)" % fchoices, default="fancy")
  19.  
  20. ## Handle arguments
  21. opts, args = op.parse_args()
  22. try:
  23.     fname = args[0]
  24. except:
  25.     op.print_usage()
  26.     sys.exit(1)
  27.  
  28.  
  29.  
  30. import ROOT
  31. ROOT.gROOT.SetBatch(True)
  32.  
  33. def fancylistall(d, indent=""):
  34.     "Print out all contents of a "
  35.     for key in d.GetListOfKeys():
  36.         kname = key.GetName()
  37.         if key.IsFolder():
  38.             print indent+"D", kname
  39.             fancylistall(d.Get(kname), indent+"  ")
  40.         else:
  41.             print "%s%s %s" % (indent, d.Get(kname).ClassName(), kname)
  42.  
  43. def getall(d, basepath="/"):
  44.     for key in d.GetListOfKeys():
  45.         kname = key.GetName()
  46.         if key.IsFolder():
  47.             # TODO: -> "yield from" in Py3
  48.             for i in getall(d.Get(kname), basepath+kname+"/"):
  49.                 yield i
  50.         else:
  51.             yield basepath+kname, d.Get(kname)
  52.  
  53.  
  54.  
  55. ## Loop over the file contents
  56. f = ROOT.TFile(fname)
  57. print "Listing for ROOT file '%s':\n" % fname
  58. if opts.FORMAT == "fancy":
  59.     fancylistall(f)
  60. else:
  61.     for k, o in getall(f):
  62.         t = o.ClassName()+"  " if opts.FORMAT != "minimal" else ""
  63.         print t+k
Advertisement
RAW Paste Data Copied
Advertisement