DMelt:IO/ROOT IO

From HandWiki
Member

Reading and viewing ROOT files

One can natively reads ROOT files including files created using ROOT https://root.cern/ files. Versions 3, 4, 5 and 6 are supported). The original library is RootIO FreeHep (http://java.freehep.org/freehep-rootio/), but it was modernized for new versions of ROOT.

Here is a Jython/Python example showing how to read a ROOT tree using <javadoc>hep.io.root import RootFileReader</javdoc>:

from  hep.io.root.interfaces import TTree
from  hep.io.root import RootFileReader
reader = RootFileReader("ntuple_tree.root")
tree = reader.get("tree");
maxevents=tree.getEntries()
leaves = tree.getLeaves()
nrleaves=leaves.size()
print "Nr of events=",maxevents
print "Nr of leaves=",nrleaves
print "Leaves:"
for l in xrange( nrleaves ):
   print "Leaf=",(leaves.get(l)).getName()
print "Run over events"
f0=leaves.get(0)
f1=leaves.get(1)
f2=leaves.get(2)
for i in xrange(tree.getEntries()):
      print f0.getValue(i), f1.getValue(i), f2.getValue(i)

The example directory also shows how to read histograms. Similar examples can be made using Java or Groovy scripting.

One can browser histogram (or ROOT objects) using this using the <javadoc>rootio</javdoc> class:

import rootio
rootio.HBrowser("histograms_root5.root") # browser and plot ROOT histograms 
rootio.Browser("histograms_root5.root")  # browser for ROOT objects

The script that call these browsers can be put into Jython or Groovy files and executed as binary programs.