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/03/2021. License: Free
https://datamelt.org/code/cache/io_read_root_5610.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("https://datamelt.org/examples/data/"+file)


# 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");