Reading ROOT file and accessing histograms
Source code name: "io_read_root.py"
Programming language: Python
Topic: File IO/Root
DMelt Version 1. Last modified: 03/07/2016. License: Free
http://datamelt.org/code/cache/io_read_root_7632.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


 
from java.awt import Color
from jhplot  import *
from jhplot.io  import *

file="Example.root"
Web.get("http://datamelt.org/examples/data/"+file)

# local location of ROOT file
# it is in "macros/examples/data" Set this manually if run without DataMelt IDE
#file=SystemDir+fSep+"macros"+fSep+"examples"+fSep+"data"+fSep+"Example.root"

# location of ROOT file
rfr = FileRoot( file )
print "number of histograms=",rfr.getNKeys()
print "ROOT version=",rfr.getVersion()
print "ROOT version=",rfr.getTitle()
print "No of histograms=",rfr.toString()

# get ROOT histograms 
h1 = rfr.getH1D("mainHistogram")
h2 = rfr.getH1D("totalHistogram") 
# create canvas 1x1
c1 = HPlot("Canvas",600,400,1, 1)
c1.setGTitle("Show ROOT histograms from Example.root")
c1.visible(1)
c1.setMarginLeft(90)

c1.setRange(0, -4, 4)
# set autorange for Y
c1.setAutoRange(1,1)

h1.setColor(Color.blue)
c1.draw(h1)

h2.setColor(Color.red)
c1.draw(h2)

c1.setNameX("Xaxis")
c1.setNameY("Yaxis")
c1.setName("Canvas title")
c1.update()


# export to some image (png,eps,pdf,jpeg...)
# c1.export(Editor.DocMasterName()+".png");