Ex3.tartoy_sigbkg.rootTreeSvar1 and var2var1 and var2Open a new python file for instance Ex3.py. Execute python Ex3.py.
As for every python program, you need to import the needed modules.
Since the ROOT library is huge, it is advisable to specify the needed classes:
import os,sys
from ROOT import TH1D,TH2D,TFile,TTree,TCanvas
Now read in the root file and tree.
Remember: No need to specify types in python (dynamic type) and no semicolons are need:
fFile = TFile("toy_sigbkg.root", "READ")
fTree = fFile.Get("TreeS")
Similarly, we can define a canvas and a 2D histogram:
fCanvas = TCanvas("c", "c", 600, 600)
fHist = TH2D("var1var2", "", 20, -6, 6, 20, -6, 6)
Now, like in the C++ version, get the number of events in the tree (GetEntries()) and loop over the tree like this
for i in range(0, nEntries):
in order to fill the histogram.
In contrast to C++ ROOT, you don't need to link the branches to variables, they are directly available via tree.var
nEntries = fTree.GetEntries()
for i in range(0, nEntries):
fTree.GetEntry(i)
fHist.Fill(fTree.var1, fTree.var2)
fHist.Print()
You can actually do this even faster in PyROOT, just loop over the object TTree:
fHist.Reset();
for i in fTree:
fHist.Fill(fTree.var1, fTree.var2)
fHist.Print()
Wow, that is really much easier!
Ok, almost done, we can now draw the histogram and print it to a file:
fHist.Draw("COLZ")
fCanvas.Draw()
fCanvas.Print("toy_sigbkg_corr.eps")
And finally we can get the covariance of the two variables, which quantifies how strongly the are correlated:
print fHist.GetCovariance()