Uploaded by ze zhuge

TTree

advertisement
TTree
September 25, 2023
[1]: import ctypes
from array import array
import numpy as np
import ROOT
from ROOT import (
TF1,
TH1F,
TCanvas,
TFormula,
TPad,
TPaveLabel,
TPaveText,
addressof,
gBenchmark,
kBlue,
)
%jsroot off
Welcome to JupyROOT 6.28/04
[2]: # ����Tree��tree1.root
# �����Tree������
f = ROOT.TFile("tree1.root", "recreate")
t1 = ROOT.TTree("t1", "�����������Tree")
px, py, pz = ctypes.c_double(0), ctypes.c_double(0), ctypes.c_double(0)
random = ctypes.c_double(0)
ev = ctypes.c_int(0)
t1.Branch("px", px, "px/D")
t1.Branch("py", py, "py/D")
t1.Branch("pz", pz, "pz/D")
t1.Branch("random", random, "random/D")
t1.Branch("ev", ev, "ev/I")
# ��Tree
1
for i in range(10000):
ROOT.gRandom.Rannor(px, py)
pz.value = px.value**2 + py.value**2
random.value = ROOT.gRandom.Rndm()
ev.value = i
t1.Fill()
# ��Tree��������������������
t1.Write()
[2]: 794
[3]: # ��tree1w���Tree��������
# ��ROOT������ROOT�����
import ctypes
import ROOT
# ���� "tree1.root" �ROOT��
f = ROOT.TFile("tree1.root")
# �������� "t1" �TTree��
t1 = f.Get("t1")
# �������������
px, py, pz = ctypes.c_double(0), ctypes.c_double(0), ctypes.c_double(0)
random = ctypes.c_double(0)
ev = ctypes.c_int(0)
# ����TTree��������
t1.SetBranchAddress("px", px)
t1.SetBranchAddress("py", py)
t1.SetBranchAddress("pz", pz)
t1.SetBranchAddress("random", random)
t1.SetBranchAddress("ev", ev)
# ���������
hpx = ROOT.TH1F("hpx", "px��", 100, -3, 3)
hpxpy = ROOT.TH2F("hpxpy", "py vs px", 30, -3, 3, 30, -3, 3)
# ��TTree�����������������
nentries = t1.GetEntries()
for i in range(nentries):
t1.GetEntry(i) # ����������������������������
hpx.Fill(px.value)
hpxpy.Fill(px.value, py.value)
2
# ������ "c1" ���������������
c1 = ROOT.TCanvas("c1", "c1", 600, 600)
c1.Divide(1, 2)
# ����������
c1.cd(1)
# ������������ "hpx"
hpx.Draw()
# ����������
c1.cd(2)
# ������������ "hpxpy"
hpxpy.Draw()
# ����
c1.Draw()
t1.Delete()
Warning in <TFile::Init>: file tree1.root probably not closed, trying to recover
Info in <TFile::Recover>: tree1.root, recovered key TTree:t1 at address 307892
Warning in <TFile::Init>: successfully recovered 1 keys
3
[4]: ## NOTE: comparing the results of this macro with those of staff.C, you'll
## notice that the resultant file is a couple of bytes smaller, because the
## code below strips all white-spaces, whereas the .C version does not.
import array
import os
import re
import ROOT
from ROOT import TFile, TTree, addressof, gROOT
# gROOT.ProcessLine�������staff_t�C/C++����
# �����������ASCII����������������
# �����������Category�Flag�Age�Service�Children�Grade�Step�Hrweek�Cost�
4
# �����������Division�Nation��
gROOT.ProcessLine(
"struct staff_t {\
Int_t
Category;\
UInt_t
Flag;\
Int_t
Age;\
Int_t
Service;\
Int_t
Children;\
Int_t
Grade;\
Int_t
Step;\
Int_t
Hrweek;\
Int_t
Cost;\
Char_t
Division[4];\
Char_t
Nation[3];\
};"
)
## Function to read in data from ASCII file and fill the ROOT tree
def staff():
# staff()������ASCII���������ROOT Tree
staff = ROOT.staff_t()
# The input file cern.dat is a copy of the CERN staff data base
# from 1988
f = TFile("staff.root", "RECREATE")
tree = TTree("T", "staff data from ascii file")
# ��tree.Branch()�����Tree��������������������������
# ������������'staff'����������������������
# 'staff'������������Tree��������
# staff�����������������staff�������������������
# 'Category/I:Flag:Age:Service:Children:Grade:Step:Hrweek:
↪Cost'��������������������������������������
# Category/I��������I������������'Category'�
# Flag�Age�Service�Children�Grade�Step�Hrweek�Cost�����������������staff�����������
tree.Branch(
"staff", staff, "Category/I:Flag:Age:Service:Children:Grade:Step:Hrweek:
↪Cost"
)
#
#
#
#
������������'Divisions'�������������'Division'���������������������
'Divisions'������������������
addressof( staff, 'Division' )�����staff���'Division'�������������������������
'Division/C'������������������������������C�������'Division'�
5
tree.Branch("Divisions", addressof(staff, "Division"), "Division/C")
tree.Branch("Nation", addressof(staff, "Nation"), "Nation/C")
# print(str(ROOT.gROOT.GetTutorialDir())) # �����
# note that the branches Division and Nation cannot be on the first branch
fname = os.path.join(str(ROOT.gROOT.GetTutorialDir()), "tree", "cernstaff.
↪dat")
for line in open(fname).readlines():
t = list(filter(lambda x: x, re.split("\s+", line)))
staff.Category = int(t[0]) # assign as integers
staff.Flag = int(t[1])
staff.Age = int(t[2])
staff.Service = int(t[3])
staff.Children = int(t[4])
staff.Grade = int(t[5])
staff.Step = int(t[6])
staff.Hrweek = int(t[7])
staff.Cost = int(t[8])
staff.Division = t[9] # assign as strings
staff.Nation = t[10]
# print(t[9],t[10])
# ���������������������������� TTree �������
tree.Fill()
tree.Print()
tree.Write()
#### run fill function if invoked on CLI
if __name__ == "__main__":
staff()
******************************************************************************
*Tree
:T
: staff data from ascii file
*
*Entries :
3354 : Total =
172140 bytes File Size =
48245 *
*
:
: Tree compression factor =
2.84
*
******************************************************************************
*Br
0 :staff
: Category/I:Flag:Age:Service:Children:Grade:Step:Hrweek:*
*
| Cost
*
*Entries :
3354 : Total Size=
122254 bytes File Size =
29072 *
*Baskets :
3 : Basket Size=
32000 bytes Compression=
3.30
*
*…*
*Br
1 :Divisions : Division/C
*
*Entries :
3354 : Total Size=
25330 bytes File Size =
9839 *
*Baskets :
1 : Basket Size=
32000 bytes Compression=
2.11
*
*…*
6
*Br
2 :Nation
: Nation/C
*Entries :
3354 : Total Size=
*Baskets :
1 : Basket Size=
*…*
24209 bytes
32000 bytes
File Size =
Compression=
*
9334 *
2.19
*
[5]: # ������ "c2" ��������� "The FillRandom example"������� (200, 10)���� 700���� 900�
# �������������18�
c2 = TCanvas("c2", "The FillRandom exmple", 200, 10, 700, 900)
c2.SetFillColor(18)
# TPad
# �������������
# �������������
# ���������������������������X�������Y�������X�������Y���
# ��������������������21�
# ���� Draw() ��������������� c2 ��
pad1 = TPad("pad1", "The pad with the function", 0.05, 0.50, 0.95, 0.95, 21)
pad2 = TPad("pad2", "The pad with the histogram", 0.05, 0.05, 0.95, 0.45, 21)
pad1.Draw()
pad2.Draw()
gBenchmark.Start("fillrandom")
# cd ��������������
pad1.cd()
pad1.SetGridx()
pad1.SetGridy()
pad1.GetFrame().SetFillColor(42)
pad1.GetFrame().SetBorderMode(-1)
pad1.GetFrame().SetBorderSize(5)
# ������� formula
form1 = TFormula("form1", "abs(sin(x)/x)")
sqroot = TF1("sqroot", "x*gaus(0)+[3]*form1", 0, 10)
sqroot.SetParameters(10, 4, 1, 20)
sqroot.SetLineColor(4)
sqroot.SetLineWidth(6)
sqroot.Draw()
# 0,10������
# 5, 39������� 9.8, 46������
lfunction = TPaveLabel(5, 39, 9.8, 46, "The sqroot function")
lfunction.SetFillColor(41)
lfunction.Draw()
c2.Update()
pad2.cd()
7
pad2.GetFrame().SetFillColor(42)
pad2.GetFrame().SetBorderMode(-1)
pad2.GetFrame().SetBorderSize(5)
# 200���0-10�������� (0,10)����
h1f = TH1F("h1f", "Test random numbers", 200, 0, 10)
h1f.SetFillColor(45)
# � sqroot ���������
h1f.FillRandom("sqroot", 10000)
# ��
# h1f.SetFillColor(45)
# h1f.Fit("sqroot")
# h1f.Draw()
# c2.Update()
# c2.Draw()
#
# Open a ROOT file and save the formula, function and histogram
#
myfile = TFile("py-fillrandom.root", "RECREATE")
form1.Write()
sqroot.Write()
h1f.Write()
myfile.Close()
gBenchmark.Show("fillrandom")
fillrandom: Real Time =
0.35 seconds Cpu Time =
0.24 seconds
[6]: c3 = TCanvas("c3", "The Fit Canvas", 200, 10, 700, 500)
c3.SetGridx()
c3.SetGridy()
c3.GetFrame().SetFillColor(21)
c3.GetFrame().SetBorderMode(-1)
c3.GetFrame().SetBorderSize(5)
gBenchmark.Start("fit1")
FilePath = "./py-fillrandom.root"
if ROOT.gSystem.AccessPathName(FilePath):
ROOT.Info(f"{FilePath} does not exist")
exit()
fill = TFile(FilePath)
8
fill.ls()
sqroot = gROOT.FindObject("sqroot")
sqroot.Print()
# ����� sqroot ����� h1f
h1f = gROOT.FindObject("h1f")
h1f.SetFillColor(45)
h1f.Fit("sqroot")
fitlabel = TPaveText(0.6, 0.3, 0.9, 0.80, "NDC")
fitlabel.SetTextAlign(12)
fitlabel.SetFillColor(42)
fitlabel.AddText("---text1").SetTextColor(kBlue)
fitlabel.AddText("---text2").SetTextColor(5)
fitlabel.AddText("---text3").SetTextSize(0.04)
fitlabel.AddText("---text4")
fitlabel.AddText("---text5")
fitlabel.AddText("---text6")
fitlabel.AddText("***text7***")
fitlabel.SetAllWith("**", "color", 2)
# fitlabel.ReadFile(path.join(str(gROOT.GetTutorialDir()), 'pyroot', 'fit1_py.
↪py'))
fitlabel.Draw()
c3.Update()
c3.Draw()
gBenchmark.Show("fit1")
TFile**
./py-fillrandom.root
TFile*
./py-fillrandom.root
KEY: TFormula form1;1 abs(sin(x)/x)
KEY: TF1
sqroot;1
x*gaus(0)+[3]*form1
KEY: TH1F
h1f;1
Test random numbers
Formula based function:
sqroot
sqroot : x*gaus(0)+[3]*form1 Ndim= 1, Npar= 4, Number= 0
Formula expression:
x*[p0]*exp(-0.5*((x-[p1])/[p2])*((x-[p1])/[p2]))+[p3]*(abs(sin(x)/x))
Contained histogram
TH1.Print Name = Func, Entries= 100, Total sum= 1514.36
FCN=172.13 FROM MIGRAD
STATUS=CONVERGED
150 CALLS
151 TOTAL
EDM=5.15834e-07
STRATEGY= 1
ERROR MATRIX ACCURATE
EXT PARAMETER
STEP
FIRST
NO.
NAME
VALUE
ERROR
SIZE
DERIVATIVE
1 p0
3.38484e+01
5.48221e-01
2.86464e-03 -5.08033e-04
2 p1
4.01311e+00
1.63652e-02
8.60322e-05 -7.51054e-02
3 p2
9.55277e-01
1.19748e-02
5.28490e-05 -5.46868e-02
4 p3
6.50605e+01
1.33707e+00
8.22891e-03 -6.40866e-06
9
fit1
: Real Time =
0.14 seconds Cpu Time =
[ ]:
[ ]:
[ ]:
10
0.12 seconds
Download