-
Notifications
You must be signed in to change notification settings - Fork 35
/
hist.go
40 lines (34 loc) · 1.04 KB
/
hist.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
// 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 fit
import (
"go-hep.org/x/hep/hbook"
"gonum.org/v1/gonum/optimize"
)
// H1D returns the fit of histogram h with function f and optimization method m.
//
// Only bins with at least an entry are considered for the fit.
// In case settings is nil, the optimize.DefaultSettings is used.
// In case m is nil, the same default optimization method than for Curve1D is used.
func H1D(h *hbook.H1D, f Func1D, settings *optimize.Settings, m optimize.Method) (*optimize.Result, error) {
var (
n = h.Len()
xdata = make([]float64, 0, n)
ydata = make([]float64, 0, n)
yerrs = make([]float64, 0, n)
bins = h.Binning().Bins()
)
for _, bin := range bins {
if bin.Entries() <= 0 {
continue
}
xdata = append(xdata, bin.XMid())
ydata = append(ydata, bin.SumW())
yerrs = append(yerrs, bin.ErrW())
}
f.X = xdata
f.Y = ydata
f.Err = yerrs
return Curve1D(f, settings, m)
}