The tutorial Danilo linked gives all information you need. But here a short snipplet for you:
import ROOT
# Open file remotely via http (TMVA classification example)
f = ROOT.TFile.Open("http://root.cern.ch/files/tmva_class_example.root")
# The file has a tree called "TreeS"
t = f.Get("TreeS")
# Get branch "var1" as numpy array
data = t.AsMatrix(["var1"])
# Plot it!
import matplotlib.pyplot as plt
plt.hist(data)
plt.savefig("var1.png")
@swunsch@Danilo Hi guys, I encountered another problem. I’ve installed Root 6.14 and try the as matrix, and this error message appeared
data = t.AsMatrix(['gx0'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/<root-directory>/root-6.14.04/obj/lib/ROOT.py", line 339, in _TTreeAsMatrix
[invalid_cols_leafname[k] for k in invalid_cols_leafname])))
Exception: Reading of branch ['gx0'] is not supported (name of leaf is different from name of branch [2]).
Hmm… The TTree.AsMatrix feature does allow only to convert flat trees to numpy. It assumes that the leaf of the branch that you pass has the same name than the branch (because it cannot know the name). How is gx0 structured? I cannot see it in the tree.Print() output above.
So a tree like the one below:
>>> f = ROOT.TFile.Open("http://root.cern.ch/files/tmva_class_example.root")
>>> t = f.Get("TreeS")
>>> t.Print()
******************************************************************************
*Tree :TreeS : TreeS *
*Entries : 6000 : Total = 98896 bytes File Size = 89768 *
* : : Tree compression factor = 1.00 *
******************************************************************************
*Br 0 :var1 : var1/F *
*Entries : 6000 : Total Size= 24641 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 1 :var2 : var2/F *
*Entries : 6000 : Total Size= 24641 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 2 :var3 : var3/F *
*Entries : 6000 : Total Size= 24641 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 3 :var4 : var4/F *
*Entries : 6000 : Total Size= 24641 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
Oh, indeed, that could solve your problem! Does something like this work for you?
import ROOT
# Open file remotely (TMVA classification example)
f = ROOT.TFile.Open("http://root.cern.ch/files/tmva_class_example.root")
# The file has a tree called "TreeS"
t = f.Get("TreeS")
# Rename branch and leaf
t.GetBranch("var1").SetName("var1_renamed")
t.GetLeaf("var1").SetName("var1_renamed")
# Get branch "var1_renamed" as numpy array
data = t.AsMatrix(["var1_renamed"])
# Plot it!
import matplotlib.pyplot as plt
plt.hist(data)
plt.savefig("x.png")
@swunsch I am really grateful, thank you so so much. Finally with this program I can move to the next step, again thank you so much. I hope somehow in the future I can help people, just like you
By the way this is the result, only possible thanks to you
To get the best performance out of TTree.AsMatrix, you can write the following:
data = t.AsMatrix(["gx0", "gx1", "gx2"])
plt.scatter(data[:,0], data[:,1], s=0.0005)
Using only one call to TTree.AsMatrix does only one loop over the tree and writes out all data at once. One more tip, with adding ROOT.ROOT.EnableImplicitMT() before, you can even run the data-loading on multiple threads (in case it takes significant time to load the data).
I support the nice solution @swunsch proposed and you adapted to your problem.
If you can use the ROOT head, the feature will be available in the ROOT version 6.16 foreseen for november, you can produce your scatter plot directly with RDataFrame:
import ROOT
// open file, get tree, setting the leaf names (if you want)
rdf = ROOT.ROOT.RDataFrame (t)
g = rdf.Graph("gx0", "gx1")
# the explicit handling of the canvas are there to inline the plot in the notebook
c = ROOT.TCanvas()
g.Draw()
c.Draw()
Thanks but I think my computer doesn’t like the ROOT.ROOT.EnableImplicitMT() because I tried running it on Jupyter and the terminal, both got the same error
Hello @Danilo thank you for your help and suggestion, this will really cut the process of plotting the points, because sometimes changing the name of the leaf produce an error. I wonder if its my laptop or the jupyter notebook.
I think the issue is that presently the bleeding edge stack is associated to the last 6.14 release and not master. I need to check with our librarians.
Did you check the online SWAN help for the sharing instructions? It’s accessible from the very web interface.
note that on SWAN, it might be easier to install uproot (installable via pip install) to try out these numpy-eries. (uproot is pure-Python and was built with interop with the numpy/scipy stack to start with.)