PyROOT
- pyROOT provides bindings for the classes in the root libraries
- it allows you to replace CINT with its interpreted c++ with (i)python
Installation
- compile ROOT with python support
- make sure that $ROOTSYS/lib is in your PYTHONPATH and LD_LIBRARY_PATH variables
Example 1 - TBrowser
- In [2]: import ROOT
-
-
In [3]: tb = ROOT.TBrowser()
Example 2 - Opening TFiles
- In [4]: f = ROOT.TFile("examples/tree.root")
-
In [5]: f.ls()
-
TFile** examples/tree.root
-
TFile* examples/tree.root
-
KEY: TTree aTree;1 tree title
-
In [6]: t = f.Get("aTree")
-
In [7]: t.Print()
-
******************************************************************************
-
*Tree :aTree : tree title *
-
*Entries : 1000000 : Total = 24071514 bytes File Size = 19821249 *
-
* : : Tree compression factor = 1,21 *
-
******************************************************************************
-
*Br 0 :normal : normal/D *
-
*Entries : 1000000 : Total Size= 8023900 bytes File Size = 7227246 *
-
*Baskets : 251 : Basket Size= 32000 bytes Compression= 1,11 *
-
*............................................................................*
-
*Br 1 :uniform : uniform/D *
-
*Entries : 1000000 : Total Size= 8024155 bytes File Size = 5452842 *
-
*Baskets : 251 : Basket Size= 32000 bytes Compression= 1,47 *
-
*............................................................................*
-
*Br 2 :var : var/D *
-
*Entries : 1000000 : Total Size= 8023135 bytes File Size = 7134158 *
-
*Baskets : 251 : Basket Size= 32000 bytes Compression= 1,12 *
-
*............................................................................*
-
In [8]: t.
-
Display all 326 possibilities? (y or n)
Example III - simple graphs
- import ROOT
-
-
import numpy as n
-
-
x = n.linspace(0, 4*n.pi,101)
-
-
y = n.cos(x)
-
-
g = ROOT.TGraph(len(x), x,y)
-
-
g.SetTitle("cosine in x=[%.1f, %.1f]" % (x[0], x[-1]))
-
-
g.GetXaxis().SetTitle("x")
-
g.GetYaxis().SetTitle("y")
-
-
g.Draw("AL")
Example IV - writing treesin C++:
- {
-
TFile* f = new TFile("tree.root", "recreate");
-
TTree* t = new TTree("aTree", "tree title");
-
-
double n;
-
double u;
-
double x;
-
-
t->Branch("normal", &n, "normal/D");
-
t->Branch("uniform", &u, "uniform/D");
-
t->Branch("var", &x, "var/D");
-
-
for (int i=0; i < 1000000; ++i) {
-
n = gRandom->Gaus();
-
u = gRandom->Uniform();
-
x = 2*u + 3 + n;
-
t->Fill();
-
}
-
-
f->Write();
-
f->Close();
-
}
in python:
- #!/usr/bin/env python
-
-
import ROOT
-
import numpy as n
-
-
print "Writing a tree"
-
-
f = ROOT.TFile("tree.root", "recreate")
-
t = ROOT.TTree("name_of_tree", "tree title")
-
-
-
# create 1 dimensional float arrays (python's float datatype corresponds to c++ doubles)
-
# as fill variables
-
n = n.zeros(1, dtype=float)
-
-
u = n.zeros(1, dtype=float)
-
-
# create the branches and assign the fill-variables to them
-
t.Branch('normal', n, 'normal/D')
-
t.Branch('uniform', u, 'uniform/D')
-
-
# create some random numbers, fill them into the fill varibles and call Fill()
-
for i in xrange(100000):
-
n[0] = ROOT.gRandom.Gaus()
-
-
u[0] = ROOT.gRandom.Uniform()
-
t.Fill()
-
-
# write the tree into the output file and close the file
-
f.Write()
-
f.Close()
Examples V - Drawing
- In [1]: import ROOT
-
-
In [2]: f = ROOT.TFile("examples/tree.root")
-
-
In [3]: t = f.Get("aTree")
-
-
In [4]: t.Draw("uniform")
Example VI - ProcessLine
- #!/usr/bin/env python
-
from ROOT import TTree, TFile, AddressOf, gROOT
-
-
# Make a tree
-
f = TFile('myTest.root','RECREATE')
-
t = TTree('MyTree','My test tree')
-
-
# Create a struct
-
gROOT.ProcessLine(\
-
"struct MyStruct{\
-
Int_t someInt;\
-
Double_t someDouble;\
-
-
};")
-
-
from ROOT import MyStruct
-
-
# Create branches in the tree
-
s = MyStruct()
-
-
t.Branch('rootInt',AddressOf(s,'someInt'),'someInt/I')
-
t.Branch('rootDouble',AddressOf(s,'someDouble'),'someDouble/D')
-
-
# Fill tree
-
for i in range(100):
-
s.someInt = i
-
-
s.someDouble = i
-
t.Fill()
-
-
f.Write()
-
-
f.Close()
阅读(3565) | 评论(0) | 转发(0) |