/
axis.go
218 lines (184 loc) · 4.29 KB
/
axis.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// 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 rhist
import (
"fmt"
"reflect"
"go-hep.org/x/hep/groot/rbase"
"go-hep.org/x/hep/groot/rbytes"
"go-hep.org/x/hep/groot/rcont"
"go-hep.org/x/hep/groot/root"
"go-hep.org/x/hep/groot/rtypes"
"go-hep.org/x/hep/groot/rvers"
)
type taxis struct {
rbase.Named
attaxis rbase.AttAxis
nbins int // number of bins
xmin float64 // low edge of first bin
xmax float64 // upper edge of last bin
xbins rcont.ArrayD // bin edges array in X
first int // first bin to display
last int // last bin to display
bits2 uint16 // second bit status word
time bool // on/off displaying time values instead of numerics
tfmt string // date&time format
labels *rcont.HashList // list of labels
modlabs *rcont.List // list of modified labels
}
func NewAxis(name string) *taxis {
return &taxis{
Named: *rbase.NewNamed(name, ""),
attaxis: *rbase.NewAttAxis(),
nbins: 1,
xmin: 0,
xmax: 1,
}
}
func (*taxis) RVersion() int16 {
return rvers.Axis
}
func (a *taxis) Class() string {
return "TAxis"
}
func (a *taxis) XMin() float64 {
return a.xmin
}
func (a *taxis) XMax() float64 {
return a.xmax
}
func (a *taxis) NBins() int {
return a.nbins
}
func (a *taxis) XBins() []float64 {
return a.xbins.Data
}
func (a *taxis) BinCenter(i int) float64 {
if len(a.xbins.Data) == 0 || i < 1 || i > a.nbins {
width := (a.xmax - a.xmin) / float64(a.nbins)
return a.xmin + (float64(i)-0.5)*width
}
width := a.xbins.Data[i] - a.xbins.Data[i-1]
return a.xbins.Data[i-1] + 0.5*width
}
func (a *taxis) BinLowEdge(i int) float64 {
if len(a.xbins.Data) == 0 || i < 1 || i > a.nbins {
width := (a.xmax - a.xmin) / float64(a.nbins)
return a.xmin + float64(i-1)*width
}
return a.xbins.Data[i-1]
}
func (a *taxis) BinWidth(i int) float64 {
if a.nbins <= 0 {
return 0
}
if len(a.xbins.Data) <= 0 {
return (a.xmax - a.xmin) / float64(a.nbins)
}
if i > a.nbins {
i = a.nbins
}
if i < 1 {
i = 1
}
return a.xbins.Data[i] - a.xbins.Data[i-1]
}
func (a *taxis) MarshalROOT(w *rbytes.WBuffer) (int, error) {
if w.Err() != nil {
return 0, w.Err()
}
pos := w.WriteVersion(a.RVersion())
for _, v := range []rbytes.Marshaler{
&a.Named,
&a.attaxis,
} {
if _, err := v.MarshalROOT(w); err != nil {
return 0, err
}
}
w.WriteI32(int32(a.nbins))
w.WriteF64(a.xmin)
w.WriteF64(a.xmax)
if _, err := a.xbins.MarshalROOT(w); err != nil {
return 0, err
}
w.WriteI32(int32(a.first))
w.WriteI32(int32(a.last))
w.WriteU16(a.bits2)
w.WriteBool(a.time)
w.WriteString(a.tfmt)
if err := w.WriteObjectAny(a.labels); err != nil {
return 0, err
}
if a.RVersion() >= 10 {
if err := w.WriteObjectAny(a.modlabs); err != nil {
return 0, err
}
}
return w.SetByteCount(pos, a.Class())
}
func (a *taxis) UnmarshalROOT(r *rbytes.RBuffer) error {
if r.Err() != nil {
return r.Err()
}
beg := r.Pos()
vers, pos, bcnt := r.ReadVersion(a.Class())
const minVers = 6
if vers < minVers {
return fmt.Errorf("rhist: TAxis version too old (%d<%d)", vers, minVers)
}
for _, v := range []rbytes.Unmarshaler{
&a.Named,
&a.attaxis,
} {
if err := v.UnmarshalROOT(r); err != nil {
return err
}
}
a.nbins = int(r.ReadI32())
a.xmin = r.ReadF64()
a.xmax = r.ReadF64()
if err := a.xbins.UnmarshalROOT(r); err != nil {
return err
}
a.first = int(r.ReadI32())
a.last = int(r.ReadI32())
if vers >= 9 {
a.bits2 = r.ReadU16()
}
a.time = r.ReadBool()
a.tfmt = r.ReadString()
a.labels = nil
if vers >= 9 {
labels := r.ReadObjectAny()
if labels != nil {
a.labels = labels.(*rcont.HashList)
}
}
a.modlabs = nil
if vers >= 10 {
modlabs := r.ReadObjectAny()
if modlabs != nil {
a.modlabs = modlabs.(*rcont.List)
}
}
r.CheckByteCount(pos, bcnt, beg, a.Class())
return r.Err()
}
func init() {
{
f := func() reflect.Value {
o := NewAxis("")
return reflect.ValueOf(o)
}
rtypes.Factory.Add("TAxis", f)
}
}
var (
_ root.Object = (*taxis)(nil)
_ root.Named = (*taxis)(nil)
_ Axis = (*taxis)(nil)
_ rbytes.Marshaler = (*taxis)(nil)
_ rbytes.Unmarshaler = (*taxis)(nil)
)