Skip to content

Commit 80389c4

Browse files
committed
groot: add plot
1 parent da43ec8 commit 80389c4

File tree

6 files changed

+106
-4
lines changed

6 files changed

+106
-4
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.13
44

55
require (
66
github.com/pkg/profile v1.4.0
7-
go-hep.org/x/hep v0.23.1
7+
go-hep.org/x/hep v0.24.1
88
golang.org/x/exp v0.0.0-20200228211341-fcea875c7e85
99
gonum.org/v1/gonum v0.7.0
1010
gonum.org/v1/plot v0.7.0

go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,15 @@ gitlab.com/cznic/golex v1.0.0/go.mod h1:vkWdDgqbbThjRHoOLU7yNPgMxaubAkwnvF/4zeG8
104104
go-hep.org/x/exp v0.3.0/go.mod h1:j7kUcLWPIO1Qb+yoWtOuYSqpnN1JsZE0LqDD9yDJOnI=
105105
go-hep.org/x/exp v0.4.0 h1:xqGt7K9UrgdP6adzEKQtCdoPj7EpBfAxAQRjS8gEKyY=
106106
go-hep.org/x/exp v0.4.0/go.mod h1:eo+NW3dHsADQnqP4Rt3gnelmjVKPngV68+ku0fBImXQ=
107+
go-hep.org/x/exp v0.5.0 h1:8wixwymPKvJ9lYlUsrUOVoTSBRiAXbyXV6g+TLBjyio=
108+
go-hep.org/x/exp v0.5.0/go.mod h1:KDxj3uk3sU6NqrrTFR5RuxexPvBE74o+c4lbc+WMQLY=
107109
go-hep.org/x/hep v0.21.0/go.mod h1:gbp4SbxCTtqu91WY/OsD+1bK0W3gpQqEt1IVQLjiMn4=
108110
go-hep.org/x/hep v0.23.1 h1:0YfZHawi8DjhCapdhM77NkNj8M1S0wSlU5/chiMCe4g=
109111
go-hep.org/x/hep v0.23.1/go.mod h1:+/LhRnY/keU6GBS0mbgpFvH3LNOOHQXZ5csL0pnMets=
112+
go-hep.org/x/hep v0.24.0 h1:P1IVmSbAmwjdDVbYb2NDUBqeMpKt44VZqALr1j4rd44=
113+
go-hep.org/x/hep v0.24.0/go.mod h1:QfwdePsnlwnYV7yvgWSCvcfdcutNwHcuDf94aTTrEe8=
114+
go-hep.org/x/hep v0.24.1 h1:eGnw6Aaku33lG66YVSeu7NjCieeDopJgLKaJNgCYcuM=
115+
go-hep.org/x/hep v0.24.1/go.mod h1:QfwdePsnlwnYV7yvgWSCvcfdcutNwHcuDf94aTTrEe8=
110116
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
111117
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
112118
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@@ -198,6 +204,7 @@ gopkg.in/jcmturner/gokrb5.v6 v6.1.1/go.mod h1:NFjHNLrHQiruory+EmqDXCGv6CrjkeYeA+
198204
gopkg.in/jcmturner/rpc.v1 v1.1.0/go.mod h1:YIdkC4XfD6GXbzje11McwsDuOlZQSb9W4vfLvuNnlv8=
199205
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
200206
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
207+
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
201208
modernc.org/b v1.0.0/go.mod h1:uZWcZfRj1BpYzfN9JTerzlNUnnPsV9O2ZA8JsRcubNg=
202209
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
203210
modernc.org/db v1.0.0/go.mod h1:kYD/cO29L/29RM0hXYl4i3+Q5VojL31kTUVpVJDw0s8=

groot/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ groot: evt.a.t= 0.304
4949
groot: evt.b.e= 1.310
5050
groot: evt.b.t= 0.094
5151
```
52+
53+
![event](https://github.com/go-hep/examples/raw/master/groot/testdata/event.png)

groot/ex-tree-00.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"flag"
1111
"fmt"
1212
"log"
13+
"math"
1314
"math/rand"
1415

1516
"go-hep.org/x/hep/groot"
@@ -64,6 +65,7 @@ func tree0(fname string, evtmax int64) error {
6465
if err != nil {
6566
return fmt.Errorf("could not create tree writer: %w", err)
6667
}
68+
defer tree.Close()
6769

6870
log.Printf("-- created tree %q:", tree.Name())
6971
for i, b := range tree.Branches() {
@@ -75,11 +77,10 @@ func tree0(fname string, evtmax int64) error {
7577
log.Printf("processing event %d...", i)
7678
}
7779
e.I = i
78-
e.A.E = rand.NormFloat64()
79-
e.B.E = rand.NormFloat64()
80+
e.A.E, e.B.E = rannor()
8081

8182
e.A.T = rand.Float64()
82-
e.B.T = e.A.T * rand.NormFloat64()
83+
e.B.T = e.A.T + rand.NormFloat64()*0.1
8384

8485
if i%1000 == 0 {
8586
log.Printf("evt.i= %8d", e.I)
@@ -106,3 +107,13 @@ func tree0(fname string, evtmax int64) error {
106107

107108
return nil
108109
}
110+
111+
func rannor() (float64, float64) {
112+
y := rand.Float64()
113+
z := rand.Float64()
114+
x := z * 2 * math.Pi
115+
r := math.Sqrt(-2 * math.Log(y))
116+
a := r * math.Sin(x)
117+
b := r * math.Cos(x)
118+
return a, b
119+
}

groot/ex-tree-01.go

+82
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313

1414
"go-hep.org/x/hep/groot"
1515
"go-hep.org/x/hep/groot/rtree"
16+
"go-hep.org/x/hep/hbook"
17+
"go-hep.org/x/hep/hplot"
18+
"gonum.org/v1/plot/plotutil"
19+
"gonum.org/v1/plot/vg"
20+
"gonum.org/v1/plot/vg/draw"
1621
)
1722

1823
func main() {
@@ -76,6 +81,15 @@ func tree1(fname string, evtmax int64) error {
7681
evtmax = tree.Entries()
7782
}
7883

84+
var (
85+
h00 = hbook.NewH1D(100, -5, 5)
86+
h01 = hbook.NewH1D(100, -5, 5)
87+
h10 = hbook.NewH2D(100, -5, 5, 100, -5, 5)
88+
h20 = hbook.NewH1D(100, 0, 1)
89+
h21 = hbook.NewH1D(100, -0.5, 1.5)
90+
h30 = hbook.NewH2D(100, 0, 1, 100, -0.5, 1.5)
91+
)
92+
7993
for sc.Next() && sc.Entry() < evtmax {
8094
iev := sc.Entry()
8195
if iev%1000 == 0 {
@@ -87,6 +101,15 @@ func tree1(fname string, evtmax int64) error {
87101
return fmt.Errorf("could not read event #%d: %w", iev, err)
88102
}
89103

104+
h00.Fill(e.A.E, 1)
105+
if -0.2 < e.B.E && e.B.E < 0.2 {
106+
h01.Fill(e.A.E, 3)
107+
}
108+
h10.Fill(e.A.E, e.B.E, 1)
109+
h20.Fill(e.A.T, 1)
110+
h21.Fill(e.B.T, 1)
111+
h30.Fill(e.A.T, e.B.T, 1)
112+
90113
if iev%1000 == 0 {
91114
log.Printf("ievt: %d", iev)
92115
log.Printf("evt.a.e= %8.3f", e.A.E)
@@ -101,5 +124,64 @@ func tree1(fname string, evtmax int64) error {
101124
return fmt.Errorf("could not read tree: %w", err)
102125
}
103126

127+
{
128+
tp := hplot.NewTiledPlot(draw.Tiles{
129+
Rows: 2, Cols: 2,
130+
})
131+
tp.Align = true
132+
133+
p0 := tp.Plot(0, 0)
134+
p0.Title.Text = "A.E"
135+
p0.X.Label.Text = "A.E"
136+
hh00 := hplot.NewH1D(h00)
137+
hh00.LineStyle.Color = plotutil.SoftColors[0]
138+
hh01 := hplot.NewH1D(h01)
139+
hh01.LineStyle.Color = plotutil.SoftColors[2]
140+
p0.Add(hh00, hh01)
141+
p0.Add(hplot.NewGrid())
142+
p0.Legend.Add("A.E", hh00)
143+
p0.Legend.Add("A.E (w/ cut)", hh01)
144+
p0.Legend.Top = true
145+
146+
p1 := tp.Plot(0, 1)
147+
p1.Title.Text = "B.E:A.E"
148+
p1.X.Label.Text = "A.E"
149+
p1.Y.Label.Text = "B.E"
150+
p1.Add(hplot.NewH2D(h10, nil))
151+
152+
p2 := tp.Plot(1, 0)
153+
p2.Title.Text = "Time"
154+
p2.X.Label.Text = "time"
155+
hh20 := hplot.NewH1D(h20)
156+
hh20.LineStyle.Color = plotutil.SoftColors[0]
157+
hh21 := hplot.NewH1D(h21)
158+
hh21.LineStyle.Color = plotutil.SoftColors[2]
159+
p2.Add(hh20, hh21)
160+
p2.Add(hplot.NewGrid())
161+
p2.Legend.Add("A.T", hh20)
162+
p2.Legend.Add("B.T", hh21)
163+
p2.Legend.Top = true
164+
165+
p3 := tp.Plot(1, 1)
166+
p3.Title.Text = "B.T:A.T"
167+
p3.X.Label.Text = "A.T"
168+
p3.Y.Label.Text = "B.T"
169+
p3.Add(hplot.NewH2D(h30, nil))
170+
171+
err = tp.Save(20*vg.Centimeter, -1, "testdata/event.png")
172+
if err != nil {
173+
return fmt.Errorf("could not save plot: %w", err)
174+
}
175+
}
176+
return nil
177+
}
178+
179+
func plot(fname string) error {
180+
f, err := groot.Open(fname)
181+
if err != nil {
182+
return fmt.Errorf("could not open ROOT file: %w", err)
183+
}
184+
defer f.Close()
185+
104186
return nil
105187
}

groot/testdata/event.png

124 KB
Loading

0 commit comments

Comments
 (0)