-
Notifications
You must be signed in to change notification settings - Fork 35
/
yoda_test.go
126 lines (104 loc) · 2.17 KB
/
yoda_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright ©2017 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package yodacnv_test
import (
"bytes"
"reflect"
"testing"
"go-hep.org/x/hep/hbook"
"go-hep.org/x/hep/hbook/yodacnv"
)
var (
rdata []byte
h1 *hbook.H1D
h2 *hbook.H2D
p1 *hbook.P1D
s2 *hbook.S2D
)
func TestReadWrite(t *testing.T) {
r := bytes.NewReader(rdata)
objs, err := yodacnv.Read(r)
if err != nil {
t.Fatal(err)
}
w := new(bytes.Buffer)
for _, v := range objs {
err = yodacnv.Write(w, v.(yodacnv.Marshaler))
if err != nil {
t.Fatal(err)
}
}
if !reflect.DeepEqual(w.Bytes(), rdata) {
t.Fatalf("got:\n%s\nwant:\n%s\n", w.String(), string(rdata))
}
}
func TestReadCounter(t *testing.T) {
r := bytes.NewReader([]byte(`BEGIN YODA_COUNTER /_EVTCOUNT
Path=/_EVTCOUNT
Title=
Type=Counter
# sumW sumW2 numEntries
3.255092e+09 1.059749e+15 10000
END YODA_COUNTER
`))
objs, err := yodacnv.Read(r)
if err != nil {
t.Fatal(err)
}
if len(objs) != 0 {
t.Fatalf("got %d values. want %d (COUNTER not implemented)", len(objs), 0)
}
}
func TestReadScatter1D(t *testing.T) {
r := bytes.NewReader([]byte(`BEGIN YODA_SCATTER1D /_XSEC
Path=/_XSEC
Title=
Type=Scatter1D
# xval xerr- xerr+
2.966429e+04 2.828319e+02 2.828319e+02
END YODA_SCATTER1D
`))
objs, err := yodacnv.Read(r)
if err != nil {
t.Fatal(err)
}
if len(objs) != 0 {
t.Fatalf("got %d values. want %d (SCATTER1D not implemented)", len(objs), 0)
}
}
func init() {
add := func(o yodacnv.Marshaler) {
raw, err := o.MarshalYODA()
if err != nil {
panic(err)
}
rdata = append(rdata, raw...)
}
h1 = hbook.NewH1D(10, -4, 4)
h1.Annotation()["name"] = "histo-1d"
h1.Fill(1, 1)
h1.Fill(2, 1)
h1.Fill(-3, 1)
h1.Fill(-4, 1)
h1.Fill(0, 1)
h1.Fill(0, 1)
h1.Fill(10, 1)
h1.Fill(-10, 1)
add(h1)
h2 = hbook.NewH2D(5, -1, 1, 5, -2, +2)
h2.Annotation()["name"] = "histo-2d"
h2.Fill(+0.5, +1, 1)
h2.Fill(-0.5, +1, 1)
h2.Fill(+0.0, -1, 1)
add(h2)
p1 = hbook.NewP1D(10, -4, +4)
for i := 0; i < 10; i++ {
v := float64(i)
p1.Fill(v, v*2, 1)
}
p1.Fill(-10, 10, 1)
add(p1)
s2 = hbook.NewS2DFromH1D(h1)
add(s2)
}