-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin1d.go
152 lines (124 loc) · 2.46 KB
/
bin1d.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
// Copyright 2015 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 hbook
import (
"bytes"
"encoding/gob"
"math"
"github.com/go-hep/dtypes"
)
type Bin1D struct {
entries int64
sw float64 // sum of weights
swc float64 // sum of weights times coord
sw2 float64 // sum of squared weights
}
func (b *Bin1D) Entries() int64 {
return b.entries
}
func (b *Bin1D) fill(coord, weight float64) {
b.entries += 1
b.sw += weight
b.swc += weight * coord
b.sw2 += weight * weight
}
func (b *Bin1D) increment(n int64, height, error, centre float64) {
b.entries += n
b.sw += height
b.swc += centre * height
b.sw2 += error * error
}
func (b *Bin1D) Set(n int64, height, error, centre float64) {
b.entries = n
b.sw = height
b.swc = centre * height
b.sw2 = error * error
}
func (b *Bin1D) Err() float64 {
return math.Sqrt(b.sw2)
}
func (b *Bin1D) Err2() float64 {
return b.sw2
}
func (b *Bin1D) Centre() float64 {
if b.sw == 0. {
return 0.
}
return b.swc / b.sw
}
func (b *Bin1D) Scale(factor float64) {
b.sw *= factor
b.swc *= factor
b.sw2 *= factor * factor
}
func (b *Bin1D) MarshalBinary(buf *bytes.Buffer) error {
enc := gob.NewEncoder(buf)
err := b.gobEncode(enc)
return err
}
func (b *Bin1D) UnmarshalBinary(buf *bytes.Buffer) error {
dec := gob.NewDecoder(buf)
err := b.gobDecode(dec)
return err
}
func (b *Bin1D) GobEncode() ([]byte, error) {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := b.gobEncode(enc)
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
func (b *Bin1D) gobEncode(enc *gob.Encoder) error {
var err error
err = enc.Encode(b.entries)
if err != nil {
return err
}
err = enc.Encode(b.sw)
if err != nil {
return err
}
err = enc.Encode(b.swc)
if err != nil {
return err
}
err = enc.Encode(b.sw2)
if err != nil {
return err
}
return err
}
func (b *Bin1D) GobDecode(data []byte) error {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
err := b.gobDecode(dec)
return err
}
func (b *Bin1D) gobDecode(dec *gob.Decoder) error {
var err error
err = dec.Decode(&b.entries)
if err != nil {
return err
}
err = dec.Decode(&b.sw)
if err != nil {
return err
}
err = dec.Decode(&b.swc)
if err != nil {
return err
}
err = dec.Decode(&b.sw2)
if err != nil {
return err
}
return err
}
func init() {
gob.Register((*Bin1D)(nil))
dtypes.Register((*Bin1D)(nil))
}
// EOF