go-hep.org/x/hep@v0.38.1/fit/hist.go (about) 1 // Copyright ©2017 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package fit 6 7 import ( 8 "go-hep.org/x/hep/hbook" 9 "gonum.org/v1/gonum/optimize" 10 ) 11 12 // H1D returns the fit of histogram h with function f and optimization method m. 13 // 14 // Only bins with at least an entry are considered for the fit. 15 // In case settings is nil, the optimize.DefaultSettingsLocal is used. 16 // In case m is nil, the same default optimization method than for Curve1D is used. 17 func H1D(h *hbook.H1D, f Func1D, settings *optimize.Settings, m optimize.Method) (*optimize.Result, error) { 18 var ( 19 n = h.Len() 20 xdata = make([]float64, 0, n) 21 ydata = make([]float64, 0, n) 22 yerrs = make([]float64, 0, n) 23 bins = h.Binning.Bins 24 ) 25 26 for _, bin := range bins { 27 if bin.Entries() <= 0 { 28 continue 29 } 30 xdata = append(xdata, bin.XMid()) 31 ydata = append(ydata, bin.SumW()) 32 yerrs = append(yerrs, bin.ErrW()) 33 } 34 35 f.X = xdata 36 f.Y = ydata 37 f.Err = yerrs 38 39 return Curve1D(f, settings, m) 40 }