README

hbook

GoDoc

hbook is a set of data analysis tools for HEP (histograms (1D, 2D, 3D), profiles and ntuples).

hbook is a work in progress of a concurrent friendly histogram filling toolkit. It is loosely based on AIDA interfaces and concepts as well as the "simplicity" of HBOOK and the previous work of YODA.

Installation

$ go get go-hep.org/x/hep/hbook

Documentation

Documentation is available on godoc:

https://godoc.org/go-hep.org/x/hep/hbook

Example

H1D
func ExampleH1D() {
	const npoints = 10000

	// Create a normal distribution.
	dist := distuv.Normal{
		Mu:    0,
		Sigma: 1,
		Src:   rand.New(rand.NewSource(0)),
	}

	// Draw some random values from the standard
	// normal distribution.
	h := hbook.NewH1D(20, -4, +4)
	for i := 0; i < npoints; i++ {
		v := dist.Rand()
		h.Fill(v, 1)
	}
	// fill h with a slice of values and their weights
	h.FillN([]float64{1, 2, 3}, []float64{1, 1, 1})
	h.FillN([]float64{1, 2, 3}, nil) // all weights are 1.

	fmt.Printf("mean:    %v\n", h.XMean())
	fmt.Printf("rms:     %v\n", h.XRMS())
	fmt.Printf("std-dev: %v\n", h.XStdDev())
	fmt.Printf("std-err: %v\n", h.XStdErr())

	// Output:
	// mean:    0.005589967511734562
	// rms:     1.0062596231244403
	// std-dev: 1.0062943821322063
	// std-err: 0.010059926295994191
}
func ExampleAddH1D() {

	h1 := hbook.NewH1D(6, 0, 6)
	h1.Fill(-0.5, 1)
	h1.Fill(0, 1.5)
	h1.Fill(0.5, 1)
	h1.Fill(1.2, 1)
	h1.Fill(2.1, 2)
	h1.Fill(4.2, 1)
	h1.Fill(5.9, 1)
	h1.Fill(6, 0.5)

	h2 := hbook.NewH1D(6, 0, 6)
	h2.Fill(-0.5, 0.7)
	h2.Fill(0.2, 1)
	h2.Fill(0.7, 1.2)
	h2.Fill(1.5, 0.8)
	h2.Fill(2.2, 0.7)
	h2.Fill(4.3, 1.3)
	h2.Fill(5.2, 2)
	h2.Fill(6.8, 1)

	hsum := hbook.AddH1D(h1, h2)
	fmt.Printf("Under: %.1f +/- %.1f\n", hsum.Binning.Outflows[0].SumW(), math.Sqrt(hsum.Binning.Outflows[0].SumW2()))
	for i := 0; i < hsum.Len(); i++ {
		fmt.Printf("Bin %v: %.1f +/- %.1f\n", i, hsum.Binning.Bins[i].SumW(), math.Sqrt(hsum.Binning.Bins[i].SumW2()))
	}
	fmt.Printf("Over : %.1f +/- %.1f\n", hsum.Binning.Outflows[1].SumW(), math.Sqrt(hsum.Binning.Outflows[1].SumW2()))

	// Output:
	// Under: 1.7 +/- 1.2
	// Bin 0: 4.7 +/- 2.4
	// Bin 1: 1.8 +/- 1.3
	// Bin 2: 2.7 +/- 2.1
	// Bin 3: 0.0 +/- 0.0
	// Bin 4: 2.3 +/- 1.6
	// Bin 5: 3.0 +/- 2.2
	// Over : 1.5 +/- 1.1
}
func ExampleAddScaledH1D() {

	h1 := hbook.NewH1D(6, 0, 6)
	h1.Fill(-0.5, 1)
	h1.Fill(0, 1.5)
	h1.Fill(0.5, 1)
	h1.Fill(1.2, 1)
	h1.Fill(2.1, 2)
	h1.Fill(4.2, 1)
	h1.Fill(5.9, 1)
	h1.Fill(6, 0.5)

	h2 := hbook.NewH1D(6, 0, 6)
	h2.Fill(-0.5, 0.7)
	h2.Fill(0.2, 1)
	h2.Fill(0.7, 1.2)
	h2.Fill(1.5, 0.8)
	h2.Fill(2.2, 0.7)
	h2.Fill(4.3, 1.3)
	h2.Fill(5.2, 2)
	h2.Fill(6.8, 1)

	hsum := hbook.AddScaledH1D(h1, 10, h2)
	fmt.Printf("Under: %.1f +/- %.1f\n", hsum.Binning.Outflows[0].SumW(), math.Sqrt(hsum.Binning.Outflows[0].SumW2()))
	for i := 0; i < hsum.Len(); i++ {
		fmt.Printf("Bin %v: %.1f +/- %.1f\n", i, hsum.Binning.Bins[i].SumW(), math.Sqrt(hsum.Binning.Bins[i].SumW2()))
	}
	fmt.Printf("Over : %.1f +/- %.1f\n", hsum.Binning.Outflows[1].SumW(), math.Sqrt(hsum.Binning.Outflows[1].SumW2()))

	// Output:
	// Under: 8.0 +/- 7.1
	// Bin 0: 24.5 +/- 15.7
	// Bin 1: 9.0 +/- 8.1
	// Bin 2: 9.0 +/- 7.3
	// Bin 3: 0.0 +/- 0.0
	// Bin 4: 14.0 +/- 13.0
	// Bin 5: 21.0 +/- 20.0
	// Over : 10.5 +/- 10.0
}
func ExampleSubH1D() {

	h1 := hbook.NewH1D(6, 0, 6)
	h1.Fill(-0.5, 1)
	h1.Fill(0, 1.5)
	h1.Fill(0.5, 1)
	h1.Fill(1.2, 1)
	h1.Fill(2.1, 2)
	h1.Fill(4.2, 1)
	h1.Fill(5.9, 1)
	h1.Fill(6, 0.5)

	h2 := hbook.NewH1D(6, 0, 6)
	h2.Fill(-0.5, 0.7)
	h2.Fill(0.2, 1)
	h2.Fill(0.7, 1.2)
	h2.Fill(1.5, 0.8)
	h2.Fill(2.2, 0.7)
	h2.Fill(4.3, 1.3)
	h2.Fill(5.2, 2)
	h2.Fill(6.8, 1)

	hsub := hbook.SubH1D(h1, h2)
	under := hsub.Binning.Outflows[0]
	fmt.Printf("Under: %.1f +/- %.1f\n", under.SumW(), math.Sqrt(under.SumW2()))
	for i, bin := range hsub.Binning.Bins {
		fmt.Printf("Bin %v: %.1f +/- %.1f\n", i, bin.SumW(), math.Sqrt(bin.SumW2()))
	}
	over := hsub.Binning.Outflows[1]
	fmt.Printf("Over : %.1f +/- %.1f\n", over.SumW(), math.Sqrt(over.SumW2()))

	// Output:
	// Under: 0.3 +/- 1.2
	// Bin 0: 0.3 +/- 2.4
	// Bin 1: 0.2 +/- 1.3
	// Bin 2: 1.3 +/- 2.1
	// Bin 3: 0.0 +/- 0.0
	// Bin 4: -0.3 +/- 1.6
	// Bin 5: -1.0 +/- 2.2
	// Over : -0.5 +/- 1.1
}
H2D
func ExampleH2D() {
	h := hbook.NewH2D(100, -10, 10, 100, -10, 10)

	const npoints = 10000

	dist, ok := distmv.NewNormal(
		[]float64{0, 1},
		mat.NewSymDense(2, []float64{4, 0, 0, 2}),
		rand.New(rand.NewSource(1234)),
	)
	if !ok {
		log.Fatalf("error creating distmv.Normal")
	}

	v := make([]float64, 2)
	// Draw some random values from the standard
	// normal distribution.
	for i := 0; i < npoints; i++ {
		v = dist.Rand(v)
		h.Fill(v[0], v[1], 1)
	}

	// fill h with slices of values and their weights
	h.FillN(
		[]float64{1, 2, 3}, // xs
		[]float64{1, 2, 3}, // ys
		[]float64{1, 1, 1}, // ws
	)

	// fill h with slices of values. all weights are 1.
	h.FillN(
		[]float64{1, 2, 3}, // xs
		[]float64{1, 2, 3}, // ys
		nil,                // ws
	)
}
S2D
func ExampleS2D() {
	s := hbook.NewS2D(hbook.Point2D{X: 1, Y: 1}, hbook.Point2D{X: 2, Y: 1.5}, hbook.Point2D{X: -1, Y: +2})
	if s == nil {
		log.Fatal("nil pointer to S2D")
	}

	fmt.Printf("len=%d\n", s.Len())

	s.Fill(hbook.Point2D{X: 10, Y: -10, ErrX: hbook.Range{Min: 5, Max: 5}, ErrY: hbook.Range{Min: 6, Max: 6}})
	fmt.Printf("len=%d\n", s.Len())
	fmt.Printf("pt[%d]=%+v\n", 3, s.Point(3))

	// Output:
	// len=3
	// len=4
	// pt[3]={X:10 Y:-10 ErrX:{Min:5 Max:5} ErrY:{Min:6 Max:6}}
}
Ntuple
Open an existing n-tuple
package main

import (
	"database/sql"
	"fmt"

	_ "go-hep.org/x/hep/csvutil/csvdriver"
	"go-hep.org/x/hep/hbook/ntup"
)

func main() {
	db, err := sql.Open("csv", "data.csv")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	nt, err := ntup.Open(db, "csv")
	if err != nil {
		panic(err)
	}

	h1, err := nt.ScanH1D("px where pt>100", nil)
	if err != nil {
		panic(err)
	}
	fmt.Printf("h1: %v\n", h1)

	h2 := hbook.NewH1D(100, -10, 10)
	h2, err = nt.ScanH1D("px where pt>100 && pt < 1000", h2)
	if err != nil {
		panic(err)
	}
	fmt.Printf("h2: %v\n", h2)

	h11 := hbook.NewH1D(100, -10, 10)
	h22 := hbook.NewH1D(100, -10, 10)
	err = nt.Scan("px, py where pt>100", func(px, py float64) error {
		h11.Fill(px, 1)
		h22.Fill(py, 1)
		return nil
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("h11: %v\n", h11)
	fmt.Printf("h22: %v\n", h22)
}

Documentation

Overview

Package hbook is a set of data analysis tools for HEP (histograms (1D, 2D, 3D), profiles and ntuples). hbook is a work in progress of a concurrent friendly histogram filling toolkit. It is loosely based on AIDA interfaces and concepts as well as the "simplicity" of HBOOK and the previous work of YODA.

Index

Examples

Constants

View Source
const (
	UnderflowBin1D = -1
	OverflowBin1D  = -2
)

Indices for the under- and over-flow 1-dim bins.

View Source
const (
	BngNW int = 1 + iota
	BngN
	BngNE
	BngE
	BngSE
	BngS
	BngSW
	BngW
)

indices for the 2D-binning overflows

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotation

type Annotation map[string]interface{}

Annotation is a bag of attributes that are attached to a histogram.

func (*Annotation) MarshalBinary

func (ann *Annotation) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler

func (Annotation) MarshalYODA

func (ann Annotation) MarshalYODA() ([]byte, error)

MarshalYODA implements the YODAMarshaler interface.

func (*Annotation) UnmarshalBinary

func (ann *Annotation) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*Annotation) UnmarshalYODA

func (ann *Annotation) UnmarshalYODA(data []byte) error

UnmarshalYODA implements the YODAUnmarshaler interface.

type Bin

type Bin interface {
	Rank() int           // Number of dimensions of the bin
	Entries() int64      // Number of entries in the bin
	EffEntries() float64 // Effective number of entries in the bin
	SumW() float64       // sum of weights
	SumW2() float64      // sum of squared weights
}

Bin models 1D, 2D, ... bins.

type Bin1D

type Bin1D struct {
	Range Range
	Dist  Dist1D
}

Bin1D models a bin in a 1-dim space.

func (*Bin1D) EffEntries

func (b *Bin1D) EffEntries() float64

EffEntries returns the effective number of entries \f$ = (\sum w)^2 / \sum w^2 \f$

func (*Bin1D) Entries

func (b *Bin1D) Entries() int64

Entries returns the number of entries in this bin.

func (*Bin1D) ErrW

func (b *Bin1D) ErrW() float64

ErrW returns the absolute error on SumW()

func (*Bin1D) MarshalBinary

func (o *Bin1D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (Bin1D) Rank

func (Bin1D) Rank() int

Rank returns the number of dimensions for this bin.

func (*Bin1D) SumW

func (b *Bin1D) SumW() float64

SumW returns the sum of weights in this bin.

func (*Bin1D) SumW2

func (b *Bin1D) SumW2() float64

SumW2 returns the sum of squared weights in this bin.

func (*Bin1D) UnmarshalBinary

func (o *Bin1D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*Bin1D) XEdges

func (b *Bin1D) XEdges() Range

XEdges returns the [low,high] edges of this bin.

func (*Bin1D) XFocus

func (b *Bin1D) XFocus() float64

XFocus returns the mean position in the bin, or the midpoint (if the sum of weights for this bin is 0).

func (*Bin1D) XMax

func (b *Bin1D) XMax() float64

XMax returns the upper limit of the bin (exclusive).

func (*Bin1D) XMean

func (b *Bin1D) XMean() float64

XMean returns the mean X.

func (*Bin1D) XMid

func (b *Bin1D) XMid() float64

XMid returns the geometric center of the bin. i.e.: 0.5*(high+low)

func (*Bin1D) XMin

func (b *Bin1D) XMin() float64

XMin returns the lower limit of the bin (inclusive).

func (*Bin1D) XRMS

func (b *Bin1D) XRMS() float64

XRMS returns the RMS in X.

func (*Bin1D) XStdDev

func (b *Bin1D) XStdDev() float64

XStdDev returns the standard deviation in X.

func (*Bin1D) XStdErr

func (b *Bin1D) XStdErr() float64

XStdErr returns the standard error in X.

func (*Bin1D) XVariance

func (b *Bin1D) XVariance() float64

XVariance returns the variance in X.

func (*Bin1D) XWidth

func (b *Bin1D) XWidth() float64

XWidth returns the (signed) width of the bin

type Bin1Ds

type Bin1Ds []Bin1D

Bin1Ds is a sorted slice of Bin1D implementing sort.Interface.

func (Bin1Ds) IndexOf

func (p Bin1Ds) IndexOf(v float64) int

IndexOf returns the index of the Bin1D containing the value v. It returns UndeflowBin if v is smaller than the smallest bin value. It returns OverflowBin if v is greater than the greatest bin value. It returns len(bins) if v falls within a bins gap.

func (Bin1Ds) Len

func (p Bin1Ds) Len() int

func (Bin1Ds) Less

func (p Bin1Ds) Less(i, j int) bool

func (Bin1Ds) Swap

func (p Bin1Ds) Swap(i, j int)

type Bin2D

type Bin2D struct {
	XRange Range
	YRange Range
	Dist   Dist2D
}

Bin2D models a bin in a 2-dim space.

func (*Bin2D) EffEntries

func (b *Bin2D) EffEntries() float64

EffEntries returns the effective number of entries \f$ = (\sum w)^2 / \sum w^2 \f$

func (*Bin2D) Entries

func (b *Bin2D) Entries() int64

Entries returns the number of entries in this bin.

func (*Bin2D) MarshalBinary

func (o *Bin2D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (Bin2D) Rank

func (Bin2D) Rank() int

Rank returns the number of dimensions for this bin.

func (*Bin2D) SumW

func (b *Bin2D) SumW() float64

SumW returns the sum of weights in this bin.

func (*Bin2D) SumW2

func (b *Bin2D) SumW2() float64

SumW2 returns the sum of squared weights in this bin.

func (*Bin2D) UnmarshalBinary

func (o *Bin2D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*Bin2D) XEdges

func (b *Bin2D) XEdges() Range

XEdges returns the [low,high] edges of this bin.

func (*Bin2D) XFocus

func (b *Bin2D) XFocus() float64

XFocus returns the mean position in the bin, or the midpoint (if the sum of weights for this bin is 0).

func (*Bin2D) XMax

func (b *Bin2D) XMax() float64

XMax returns the upper limit of the bin (exclusive).

func (*Bin2D) XMean

func (b *Bin2D) XMean() float64

XMean returns the mean X.

func (*Bin2D) XMid

func (b *Bin2D) XMid() float64

XMid returns the geometric center of the bin. i.e.: 0.5*(high+low)

func (*Bin2D) XMin

func (b *Bin2D) XMin() float64

XMin returns the lower limit of the bin (inclusive).

func (*Bin2D) XRMS

func (b *Bin2D) XRMS() float64

XRMS returns the RMS in X.

func (*Bin2D) XStdDev

func (b *Bin2D) XStdDev() float64

XStdDev returns the standard deviation in X.

func (*Bin2D) XStdErr

func (b *Bin2D) XStdErr() float64

XStdErr returns the standard error in X.

func (*Bin2D) XVariance

func (b *Bin2D) XVariance() float64

XVariance returns the variance in X.

func (*Bin2D) XWidth

func (b *Bin2D) XWidth() float64

XWidth returns the (signed) width of the bin

func (*Bin2D) XYFocus

func (b *Bin2D) XYFocus() (float64, float64)

XYFocus returns the mean position in the bin, or the midpoint (if the sum of weights for this bin is 0).

func (*Bin2D) XYMid

func (b *Bin2D) XYMid() (float64, float64)

XYMid returns the (x,y) coordinates of the geometric center of the bin. i.e.: 0.5*(high+low)

func (*Bin2D) XYWidth

func (b *Bin2D) XYWidth() (float64, float64)

XYWidth returns the (signed) (x,y) widths of the bin

func (*Bin2D) YEdges

func (b *Bin2D) YEdges() Range

YEdges returns the [low,high] edges of this bin.

func (*Bin2D) YFocus

func (b *Bin2D) YFocus() float64

YFocus returns the mean position in the bin, or the midpoint (if the sum of weights for this bin is 0).

func (*Bin2D) YMax

func (b *Bin2D) YMax() float64

YMax returns the upper limit of the bin (exclusive).

func (*Bin2D) YMean

func (b *Bin2D) YMean() float64

YMean returns the mean Y.

func (*Bin2D) YMid

func (b *Bin2D) YMid() float64

YMid returns the geometric center of the bin. i.e.: 0.5*(high+low)

func (*Bin2D) YMin

func (b *Bin2D) YMin() float64

YMin returns the lower limit of the bin (inclusive).

func (*Bin2D) YRMS

func (b *Bin2D) YRMS() float64

YRMS returns the RMS in Y.

func (*Bin2D) YStdDev

func (b *Bin2D) YStdDev() float64

YStdDev returns the standard deviation in Y.

func (*Bin2D) YStdErr

func (b *Bin2D) YStdErr() float64

YStdErr returns the standard error in Y.

func (*Bin2D) YVariance

func (b *Bin2D) YVariance() float64

YVariance returns the variance in Y.

func (*Bin2D) YWidth

func (b *Bin2D) YWidth() float64

YWidth returns the (signed) width of the bin

type BinP1D

type BinP1D struct {
	// contains filtered or unexported fields
}

BinP1D models a bin in a 1-dim space.

func (*BinP1D) EffEntries

func (b *BinP1D) EffEntries() float64

EffEntries returns the effective number of entries \f$ = (\sum w)^2 / \sum w^2 \f$

func (*BinP1D) Entries

func (b *BinP1D) Entries() int64

Entries returns the number of entries in this bin.

func (*BinP1D) MarshalBinary

func (o *BinP1D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (BinP1D) Rank

func (BinP1D) Rank() int

Rank returns the number of dimensions for this bin.

func (*BinP1D) SumW

func (b *BinP1D) SumW() float64

SumW returns the sum of weights in this bin.

func (*BinP1D) SumW2

func (b *BinP1D) SumW2() float64

SumW2 returns the sum of squared weights in this bin.

func (*BinP1D) UnmarshalBinary

func (o *BinP1D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*BinP1D) XEdges

func (b *BinP1D) XEdges() Range

XEdges returns the [low,high] edges of this bin.

func (*BinP1D) XFocus

func (b *BinP1D) XFocus() float64

XFocus returns the mean position in the bin, or the midpoint (if the sum of weights for this bin is 0).

func (*BinP1D) XMax

func (b *BinP1D) XMax() float64

XMax returns the upper limit of the bin (exclusive).

func (*BinP1D) XMean

func (b *BinP1D) XMean() float64

XMean returns the mean X.

func (*BinP1D) XMid

func (b *BinP1D) XMid() float64

XMid returns the geometric center of the bin. i.e.: 0.5*(high+low)

func (*BinP1D) XMin

func (b *BinP1D) XMin() float64

XMin returns the lower limit of the bin (inclusive).

func (*BinP1D) XRMS

func (b *BinP1D) XRMS() float64

XRMS returns the RMS in X.

func (*BinP1D) XStdDev

func (b *BinP1D) XStdDev() float64

XStdDev returns the standard deviation in X.

func (*BinP1D) XStdErr

func (b *BinP1D) XStdErr() float64

XStdErr returns the standard error in X.

func (*BinP1D) XVariance

func (b *BinP1D) XVariance() float64

XVariance returns the variance in X.

func (*BinP1D) XWidth

func (b *BinP1D) XWidth() float64

XWidth returns the (signed) width of the bin

type Binning1D

type Binning1D struct {
	Bins     []Bin1D
	Dist     Dist1D
	Outflows [2]Dist1D
	XRange   Range
}

Binning1D is a 1-dim binning of the x-axis.

func (*Binning1D) MarshalBinary

func (o *Binning1D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Binning1D) Overflow

func (bng *Binning1D) Overflow() *Dist1D

func (*Binning1D) Underflow

func (bng *Binning1D) Underflow() *Dist1D

func (*Binning1D) UnmarshalBinary

func (o *Binning1D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

type Binning2D

type Binning2D struct {
	Bins     []Bin2D
	Dist     Dist2D
	Outflows [8]Dist2D
	XRange   Range
	YRange   Range
	Nx       int
	Ny       int
	XEdges   []Bin1D
	YEdges   []Bin1D
}

func (*Binning2D) MarshalBinary

func (o *Binning2D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Binning2D) UnmarshalBinary

func (o *Binning2D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

type Count

type Count struct {
	XRange Range
	Val    float64
	Err    struct {
		Low  float64
		High float64
	}
}

Count is a 1-dim binned information, made of a X range, a value and its uncertainties.

Example
package main

import (
	"fmt"

	"go-hep.org/x/hep/hbook"
)

func main() {
	c := hbook.Count{}
	c.XRange = hbook.Range{Min: 0, Max: 1}
	c.Val = 10
	c.Err.Low = 2
	c.Err.High = 3

	fmt.Printf("[%v, %v] -> %v +%v -%v",
		c.XRange.Min, c.XRange.Max,
		c.Val, c.Err.High, c.Err.Low)

}
Output:

[0, 1] -> 10 +3 -2
Example (WithH1D)
package main

import (
	"fmt"

	"go-hep.org/x/hep/hbook"
)

func main() {

	h := hbook.NewH1D(6, 0, 6)
	h.Fill(-0.5, 1)
	h.Fill(0, 1.5)
	h.Fill(0.5, 1)
	h.Fill(1.2, 1)
	h.Fill(2.1, 2)
	h.Fill(4.2, 1)
	h.Fill(5.9, 1)
	h.Fill(6, 0.5)

	for _, c := range h.Counts() {
		fmt.Printf("[%v, %v] -> %v +%.1f -%.1f\n",
			c.XRange.Min, c.XRange.Max,
			c.Val, c.Err.High, c.Err.Low)
	}

}
Output:

[0, 1] -> 2.5 +0.9 -0.9
[1, 2] -> 1 +0.5 -0.5
[2, 3] -> 2 +1.0 -1.0
[3, 4] -> 0 +0.0 -0.0
[4, 5] -> 1 +0.5 -0.5
[5, 6] -> 1 +0.5 -0.5

type Dist0D

type Dist0D struct {
	N     int64   // number of entries
	SumW  float64 // sum of weights
	SumW2 float64 // sum of squared weights
}

Dist0D is a 0-dim distribution.

func (*Dist0D) EffEntries

func (d *Dist0D) EffEntries() float64

EffEntries returns the number of weighted entries, such as:

(\sum w)^2 / \sum w^2

func (*Dist0D) Entries

func (d *Dist0D) Entries() int64

Entries returns the number of entries in the distribution.

func (*Dist0D) MarshalBinary

func (o *Dist0D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Dist0D) Rank

func (*Dist0D) Rank() int

Rank returns the number of dimensions of the distribution.

func (*Dist0D) UnmarshalBinary

func (o *Dist0D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

type Dist1D

type Dist1D struct {
	Dist  Dist0D // weight moments
	Stats struct {
		SumWX  float64 // 1st order weighted x moment
		SumWX2 float64 // 2nd order weighted x moment
	}
}

Dist1D is a 1-dim distribution.

func (*Dist1D) EffEntries

func (d *Dist1D) EffEntries() float64

EffEntries returns the effective number of entries in the distribution.

func (*Dist1D) Entries

func (d *Dist1D) Entries() int64

Entries returns the number of entries in the distribution.

func (*Dist1D) MarshalBinary

func (o *Dist1D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Dist1D) Rank

func (*Dist1D) Rank() int

Rank returns the number of dimensions of the distribution.

func (*Dist1D) SumW

func (d *Dist1D) SumW() float64

SumW returns the sum of weights of the distribution.

func (*Dist1D) SumW2

func (d *Dist1D) SumW2() float64

SumW2 returns the sum of squared weights of the distribution.

func (*Dist1D) SumWX

func (d *Dist1D) SumWX() float64

SumWX returns the 1st order weighted x moment.

func (*Dist1D) SumWX2

func (d *Dist1D) SumWX2() float64

SumWX2 returns the 2nd order weighted x moment.

func (*Dist1D) UnmarshalBinary

func (o *Dist1D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

type Dist2D

type Dist2D struct {
	X     Dist1D // x moments
	Y     Dist1D // y moments
	Stats struct {
		SumWXY float64 // 2nd-order cross-term
	}
}

Dist2D is a 2-dim distribution.

func (*Dist2D) EffEntries

func (d *Dist2D) EffEntries() float64

EffEntries returns the effective number of entries in the distribution.

func (*Dist2D) Entries

func (d *Dist2D) Entries() int64

Entries returns the number of entries in the distribution.

func (*Dist2D) MarshalBinary

func (o *Dist2D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Dist2D) Rank

func (*Dist2D) Rank() int

Rank returns the number of dimensions of the distribution.

func (*Dist2D) SumW

func (d *Dist2D) SumW() float64

SumW returns the sum of weights of the distribution.

func (*Dist2D) SumW2

func (d *Dist2D) SumW2() float64

SumW2 returns the sum of squared weights of the distribution.

func (*Dist2D) SumWX

func (d *Dist2D) SumWX() float64

SumWX returns the 1st order weighted x moment

func (*Dist2D) SumWX2

func (d *Dist2D) SumWX2() float64

SumWX2 returns the 2nd order weighted x moment

func (*Dist2D) SumWXY

func (d *Dist2D) SumWXY() float64

SumWXY returns the 2nd-order cross-term.

func (*Dist2D) SumWY

func (d *Dist2D) SumWY() float64

SumWY returns the 1st order weighted y moment

func (*Dist2D) SumWY2

func (d *Dist2D) SumWY2() float64

SumWY2 returns the 2nd order weighted y moment

func (*Dist2D) UnmarshalBinary

func (o *Dist2D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

type DivOptions

type DivOptions func(c *divConfig)

DivOptions allows to customize the behaviour of DivideH1D

func DivIgnoreNaNs

func DivIgnoreNaNs() DivOptions

DivIgnoreNaNs function configures DivideH1D to ignore data points with NaNs.

func DivReplaceNaNs

func DivReplaceNaNs(v float64) DivOptions

DivReplaceNaNs function configures DivideH1D to replace NaN raised during divisions with the provided value.

type H1D

type H1D struct {
	Binning Binning1D
	Ann     Annotation
}

H1D is a 1-dim histogram with weighted entries.

Example
package main

import (
	"fmt"

	"go-hep.org/x/hep/hbook"
	"golang.org/x/exp/rand"
	"gonum.org/v1/gonum/stat/distuv"
)

func main() {
	const npoints = 10000

	// Create a normal distribution.
	dist := distuv.Normal{
		Mu:    0,
		Sigma: 1,
		Src:   rand.New(rand.NewSource(0)),
	}

	// Draw some random values from the standard
	// normal distribution.
	h := hbook.NewH1D(20, -4, +4)
	for i := 0; i < npoints; i++ {
		v := dist.Rand()
		h.Fill(v, 1)
	}
	// fill h with a slice of values and their weights
	h.FillN([]float64{1, 2, 3}, []float64{1, 1, 1})
	h.FillN([]float64{1, 2, 3}, nil) // all weights are 1.

	fmt.Printf("mean:    %v\n", h.XMean())
	fmt.Printf("rms:     %v\n", h.XRMS())
	fmt.Printf("std-dev: %v\n", h.XStdDev())
	fmt.Printf("std-err: %v\n", h.XStdErr())

}
Output:

mean:    0.005589967511734562
rms:     1.0062596231244403
std-dev: 1.0062943821322063
std-err: 0.010059926295994191

func AddH1D

func AddH1D(h1, h2 *H1D) *H1D

AddH1D returns the bin-by-bin summed histogram of h1 and h2 assuming their statistical uncertainties are uncorrelated.

Example
package main

import (
	"fmt"
	"math"

	"go-hep.org/x/hep/hbook"
)

func main() {

	h1 := hbook.NewH1D(6, 0, 6)
	h1.Fill(-0.5, 1)
	h1.Fill(0, 1.5)
	h1.Fill(0.5, 1)
	h1.Fill(1.2, 1)
	h1.Fill(2.1, 2)
	h1.Fill(4.2, 1)
	h1.Fill(5.9, 1)
	h1.Fill(6, 0.5)

	h2 := hbook.NewH1D(6, 0, 6)
	h2.Fill(-0.5, 0.7)
	h2.Fill(0.2, 1)
	h2.Fill(0.7, 1.2)
	h2.Fill(1.5, 0.8)
	h2.Fill(2.2, 0.7)
	h2.Fill(4.3, 1.3)
	h2.Fill(5.2, 2)
	h2.Fill(6.8, 1)

	hsum := hbook.AddH1D(h1, h2)
	fmt.Printf("Under: %.1f +/- %.1f\n", hsum.Binning.Outflows[0].SumW(), math.Sqrt(hsum.Binning.Outflows[0].SumW2()))
	for i := 0; i < hsum.Len(); i++ {
		fmt.Printf("Bin %v: %.1f +/- %.1f\n", i, hsum.Binning.Bins[i].SumW(), math.Sqrt(hsum.Binning.Bins[i].SumW2()))
	}
	fmt.Printf("Over : %.1f +/- %.1f\n", hsum.Binning.Outflows[1].SumW(), math.Sqrt(hsum.Binning.Outflows[1].SumW2()))

}
Output:

Under: 1.7 +/- 1.2
Bin 0: 4.7 +/- 2.4
Bin 1: 1.8 +/- 1.3
Bin 2: 2.7 +/- 2.1
Bin 3: 0.0 +/- 0.0
Bin 4: 2.3 +/- 1.6
Bin 5: 3.0 +/- 2.2
Over : 1.5 +/- 1.1

func AddScaledH1D

func AddScaledH1D(h1 *H1D, alpha float64, h2 *H1D) *H1D

AddScaledH1D returns the histogram with the bin-by-bin h1+alpha*h2 operation, assuming statistical uncertainties are uncorrelated.

Example
package main

import (
	"fmt"
	"math"

	"go-hep.org/x/hep/hbook"
)

func main() {

	h1 := hbook.NewH1D(6, 0, 6)
	h1.Fill(-0.5, 1)
	h1.Fill(0, 1.5)
	h1.Fill(0.5, 1)
	h1.Fill(1.2, 1)
	h1.Fill(2.1, 2)
	h1.Fill(4.2, 1)
	h1.Fill(5.9, 1)
	h1.Fill(6, 0.5)

	h2 := hbook.NewH1D(6, 0, 6)
	h2.Fill(-0.5, 0.7)
	h2.Fill(0.2, 1)
	h2.Fill(0.7, 1.2)
	h2.Fill(1.5, 0.8)
	h2.Fill(2.2, 0.7)
	h2.Fill(4.3, 1.3)
	h2.Fill(5.2, 2)
	h2.Fill(6.8, 1)

	hsum := hbook.AddScaledH1D(h1, 10, h2)
	fmt.Printf("Under: %.1f +/- %.1f\n", hsum.Binning.Outflows[0].SumW(), math.Sqrt(hsum.Binning.Outflows[0].SumW2()))
	for i := 0; i < hsum.Len(); i++ {
		fmt.Printf("Bin %v: %.1f +/- %.1f\n", i, hsum.Binning.Bins[i].SumW(), math.Sqrt(hsum.Binning.Bins[i].SumW2()))
	}
	fmt.Printf("Over : %.1f +/- %.1f\n", hsum.Binning.Outflows[1].SumW(), math.Sqrt(hsum.Binning.Outflows[1].SumW2()))

}
Output:

Under: 8.0 +/- 7.1
Bin 0: 24.5 +/- 15.7
Bin 1: 9.0 +/- 8.1
Bin 2: 9.0 +/- 7.3
Bin 3: 0.0 +/- 0.0
Bin 4: 14.0 +/- 13.0
Bin 5: 21.0 +/- 20.0
Over : 10.5 +/- 10.0

func NewH1D

func NewH1D(n int, xmin, xmax float64) *H1D

NewH1D returns a 1-dim histogram with n bins between xmin and xmax.

func NewH1DFromBins

func NewH1DFromBins(bins ...Range) *H1D

NewH1DFromBins returns a 1-dim histogram given a slice of 1-dim bins. It panics if the length of bins is < 1. It panics if the bins overlap.

func NewH1DFromEdges

func NewH1DFromEdges(edges []float64) *H1D

NewH1DFromEdges returns a 1-dim histogram given a slice of edges. The number of bins is thus len(edges)-1. It panics if the length of edges is <= 1. It panics if the edges are not sorted. It panics if there are duplicate edge values.

func SubH1D

func SubH1D(h1, h2 *H1D) *H1D

SubH1D returns the bin-by-bin subtracted histogram of h1 and h2 assuming their statistical uncertainties are uncorrelated.

Example
package main

import (
	"fmt"
	"math"

	"go-hep.org/x/hep/hbook"
)

func main() {

	h1 := hbook.NewH1D(6, 0, 6)
	h1.Fill(-0.5, 1)
	h1.Fill(0, 1.5)
	h1.Fill(0.5, 1)
	h1.Fill(1.2, 1)
	h1.Fill(2.1, 2)
	h1.Fill(4.2, 1)
	h1.Fill(5.9, 1)
	h1.Fill(6, 0.5)

	h2 := hbook.NewH1D(6, 0, 6)
	h2.Fill(-0.5, 0.7)
	h2.Fill(0.2, 1)
	h2.Fill(0.7, 1.2)
	h2.Fill(1.5, 0.8)
	h2.Fill(2.2, 0.7)
	h2.Fill(4.3, 1.3)
	h2.Fill(5.2, 2)
	h2.Fill(6.8, 1)

	hsub := hbook.SubH1D(h1, h2)
	under := hsub.Binning.Outflows[0]
	fmt.Printf("Under: %.1f +/- %.1f\n", under.SumW(), math.Sqrt(under.SumW2()))
	for i, bin := range hsub.Binning.Bins {
		fmt.Printf("Bin %v: %.1f +/- %.1f\n", i, bin.SumW(), math.Sqrt(bin.SumW2()))
	}
	over := hsub.Binning.Outflows[1]
	fmt.Printf("Over : %.1f +/- %.1f\n", over.SumW(), math.Sqrt(over.SumW2()))

}
Output:

Under: 0.3 +/- 1.2
Bin 0: 0.3 +/- 2.4
Bin 1: 0.2 +/- 1.3
Bin 2: 1.3 +/- 2.1
Bin 3: 0.0 +/- 0.0
Bin 4: -0.3 +/- 1.6
Bin 5: -1.0 +/- 2.2
Over : -0.5 +/- 1.1

func (*H1D) Annotation

func (h *H1D) Annotation() Annotation

Annotation returns the annotations attached to this histogram

func (*H1D) Bin

func (h *H1D) Bin(x float64) *Bin1D

Bin returns the bin at coordinates (x) for this 1-dim histogram. Bin returns nil for under/over flow bins.

func (*H1D) Clone

func (h *H1D) Clone() *H1D

Clone returns a deep copy of this 1-dim histogram.

func (*H1D) Counts

func (h *H1D) Counts() []Count

Counts return a slice of Count, ignoring outerflow. The low and high error is equal to 0.5 * sqrt(sum(w^2)).

func (*H1D) DataRange

func (h *H1D) DataRange() (xmin, xmax, ymin, ymax float64)

DataRange implements the gonum/plot.DataRanger interface

func (*H1D) EffEntries

func (h *H1D) EffEntries() float64

EffEntries returns the number of effective entries in this histogram

func (*H1D) Entries

func (h *H1D) Entries() int64

Entries returns the number of entries in this histogram

func (*H1D) Error

func (h *H1D) Error(i int) float64

Error returns the error, defined as sqrt(sumW2), of the idx-th bin.

func (*H1D) Fill

func (h *H1D) Fill(x, w float64)

Fill fills this histogram with x and weight w.

func (*H1D) FillN

func (h *H1D) FillN(xs, ws []float64)

FillN fills this histogram with the provided slices of xs and weight ws. if ws is nil, the histogram will be filled with entries of weight 1. Otherwise, FillN panics if the slices lengths differ.

func (*H1D) Integral

func (h *H1D) Integral(args ...float64) float64

Integral computes the integral of the histogram.

The number of parameters can be 0 or 2. If 0, overflows are included. If 2, the first parameter must be the lower bound of the range in which the integral is computed and the second one the upper range.

If the lower bound is math.Inf(-1) then the underflow bin is included. If the upper bound is math.Inf(+1) then the overflow bin is included.

Examples:

// integral of all in-range bins + overflows
v := h.Integral()

// integral of all in-range bins, underflow and overflow bins included.
v := h.Integral(math.Inf(-1), math.Inf(+1))

// integrall of all in-range bins, overflow bin included
v := h.Integral(h.Binning.XRange.Min, math.Inf(+1))

// integrall of all bins for which the lower edge is in [0.5, 5.5)
v := h.Integral(0.5, 5.5)

func (*H1D) Len

func (h *H1D) Len() int

Len returns the number of bins for this histogram

Len implements gonum/plot/plotter.Valuer

func (*H1D) MarshalBinary

func (o *H1D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*H1D) MarshalYODA

func (h *H1D) MarshalYODA() ([]byte, error)

MarshalYODA implements the YODAMarshaler interface.

func (*H1D) Name

func (h *H1D) Name() string

Name returns the name of this histogram, if any

func (*H1D) Rank

func (h *H1D) Rank() int

Rank returns the number of dimensions for this histogram

func (*H1D) RioMarshal

func (h *H1D) RioMarshal(w io.Writer) error

RioMarshal implements rio.RioMarshaler

func (*H1D) RioUnmarshal

func (h *H1D) RioUnmarshal(r io.Reader) error

RioUnmarshal implements rio.RioUnmarshaler

func (*H1D) RioVersion

func (h *H1D) RioVersion() rio.Version

RioVersion implements rio.RioStreamer

func (*H1D) Scale

func (h *H1D) Scale(factor float64)

Scale scales the content of each bin by the given factor.

func (*H1D) SumW

func (h *H1D) SumW() float64

SumW returns the sum of weights in this histogram. Overflows are included in the computation.

func (*H1D) SumW2

func (h *H1D) SumW2() float64

SumW2 returns the sum of squared weights in this histogram. Overflows are included in the computation.

func (*H1D) SumWX

func (h *H1D) SumWX() float64

SumWX returns the 1st order weighted x moment

func (*H1D) SumWX2

func (h *H1D) SumWX2() float64

SumWX2 returns the 2nd order weighted x moment

func (*H1D) UnmarshalBinary

func (o *H1D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*H1D) UnmarshalYODA

func (h *H1D) UnmarshalYODA(data []byte) error

UnmarshalYODA implements the YODAUnmarshaler interface.

func (*H1D) Value

func (h *H1D) Value(i int) float64

Value returns the content of the idx-th bin.

Value implements gonum/plot/plotter.Valuer

func (*H1D) XMax

func (h *H1D) XMax() float64

XMax returns the high edge of the X-axis of this histogram.

func (*H1D) XMean

func (h *H1D) XMean() float64

XMean returns the mean X. Overflows are included in the computation.

func (*H1D) XMin

func (h *H1D) XMin() float64

XMin returns the low edge of the X-axis of this histogram.

func (*H1D) XRMS

func (h *H1D) XRMS() float64

XRMS returns the XRMS in X. Overflows are included in the computation.

func (*H1D) XStdDev

func (h *H1D) XStdDev() float64

XStdDev returns the standard deviation in X. Overflows are included in the computation.

func (*H1D) XStdErr

func (h *H1D) XStdErr() float64

XStdErr returns the standard error in X. Overflows are included in the computation.

func (*H1D) XVariance

func (h *H1D) XVariance() float64

XVariance returns the variance in X. Overflows are included in the computation.

func (*H1D) XY

func (h *H1D) XY(i int) (float64, float64)

XY returns the x,y values for the i-th bin

XY implements gonum/plot/plotter.XYer

type H2D

type H2D struct {
	Binning Binning2D
	Ann     Annotation
}

H2D is a 2-dim histogram with weighted entries.

Example
package main

import (
	"log"

	"go-hep.org/x/hep/hbook"
	"golang.org/x/exp/rand"
	"gonum.org/v1/gonum/mat"
	"gonum.org/v1/gonum/stat/distmv"
)

func main() {
	h := hbook.NewH2D(100, -10, 10, 100, -10, 10)

	const npoints = 10000

	dist, ok := distmv.NewNormal(
		[]float64{0, 1},
		mat.NewSymDense(2, []float64{4, 0, 0, 2}),
		rand.New(rand.NewSource(1234)),
	)
	if !ok {
		log.Fatalf("error creating distmv.Normal")
	}

	v := make([]float64, 2)
	// Draw some random values from the standard
	// normal distribution.
	for i := 0; i < npoints; i++ {
		v = dist.Rand(v)
		h.Fill(v[0], v[1], 1)
	}

	// fill h with slices of values and their weights
	h.FillN(
		[]float64{1, 2, 3}, // xs
		[]float64{1, 2, 3}, // ys
		[]float64{1, 1, 1}, // ws
	)

	// fill h with slices of values. all weights are 1.
	h.FillN(
		[]float64{1, 2, 3}, // xs
		[]float64{1, 2, 3}, // ys
		nil,                // ws
	)
}
Output:

func NewH2D

func NewH2D(nx int, xlow, xhigh float64, ny int, ylow, yhigh float64) *H2D

NewH2D creates a new 2-dim histogram.

func NewH2DFromEdges

func NewH2DFromEdges(xedges []float64, yedges []float64) *H2D

NewH2DFromEdges creates a new 2-dim histogram from slices of edges in x and y. The number of bins in x and y is thus len(edges)-1. It panics if the length of edges is <=1 (in any dimension.) It panics if the edges are not sorted (in any dimension.) It panics if there are duplicate edge values (in any dimension.)

func (*H2D) Annotation

func (h *H2D) Annotation() Annotation

Annotation returns the annotations attached to this histogram

func (*H2D) Bin

func (h *H2D) Bin(x, y float64) *Bin2D

Bin returns the bin at coordinates (x,y) for this 2-dim histogram. Bin returns nil for under/over flow bins.

func (*H2D) EffEntries

func (h *H2D) EffEntries() float64

EffEntries returns the number of effective entries in this histogram

func (*H2D) Entries

func (h *H2D) Entries() int64

Entries returns the number of entries in this histogram

func (*H2D) Fill

func (h *H2D) Fill(x, y, w float64)

Fill fills this histogram with (x,y) and weight w.

func (*H2D) FillN

func (h *H2D) FillN(xs, ys, ws []float64)

FillN fills this histogram with the provided slices (xs,ys) and weights ws. if ws is nil, the histogram will be filled with entries of weight 1. Otherwise, FillN panics if the slices lengths differ.

func (*H2D) GridXYZ

func (h *H2D) GridXYZ() h2dGridXYZ

GridXYZ returns an anonymous struct value that implements gonum/plot/plotter.GridXYZ and is ready to plot.

func (*H2D) Integral

func (h *H2D) Integral() float64

Integral computes the integral of the histogram.

Overflows are included in the computation.

func (*H2D) MarshalBinary

func (o *H2D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*H2D) MarshalYODA

func (h *H2D) MarshalYODA() ([]byte, error)

MarshalYODA implements the YODAMarshaler interface.

func (*H2D) Name

func (h *H2D) Name() string

Name returns the name of this histogram, if any

func (*H2D) Rank

func (h *H2D) Rank() int

Rank returns the number of dimensions for this histogram

func (*H2D) SumW

func (h *H2D) SumW() float64

SumW returns the sum of weights in this histogram. Overflows are included in the computation.

func (*H2D) SumW2

func (h *H2D) SumW2() float64

SumW2 returns the sum of squared weights in this histogram. Overflows are included in the computation.

func (*H2D) SumWX

func (h *H2D) SumWX() float64

SumWX returns the 1st order weighted x moment Overflows are included in the computation.

func (*H2D) SumWX2

func (h *H2D) SumWX2() float64

SumWX2 returns the 2nd order weighted x moment Overflows are included in the computation.

func (*H2D) SumWXY

func (h *H2D) SumWXY() float64

SumWXY returns the 1st order weighted x*y moment Overflows are included in the computation.

func (*H2D) SumWY

func (h *H2D) SumWY() float64

SumWY returns the 1st order weighted y moment Overflows are included in the computation.

func (*H2D) SumWY2

func (h *H2D) SumWY2() float64

SumWY2 returns the 2nd order weighted y moment Overflows are included in the computation.

func (*H2D) UnmarshalBinary

func (o *H2D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*H2D) UnmarshalYODA

func (h *H2D) UnmarshalYODA(data []byte) error

UnmarshalYODA implements the YODAUnmarshaler interface.

func (*H2D) XMax

func (h *H2D) XMax() float64

XMax returns the high edge of the X-axis of this histogram.

func (*H2D) XMean

func (h *H2D) XMean() float64

XMean returns the mean X. Overflows are included in the computation.

func (*H2D) XMin

func (h *H2D) XMin() float64

XMin returns the low edge of the X-axis of this histogram.

func (*H2D) XRMS

func (h *H2D) XRMS() float64

XRMS returns the RMS in X. Overflows are included in the computation.

func (*H2D) XStdDev

func (h *H2D) XStdDev() float64

XStdDev returns the standard deviation in X. Overflows are included in the computation.

func (*H2D) XStdErr

func (h *H2D) XStdErr() float64

XStdErr returns the standard error in X. Overflows are included in the computation.

func (*H2D) XVariance

func (h *H2D) XVariance() float64

XVariance returns the variance in X. Overflows are included in the computation.

func (*H2D) YMax

func (h *H2D) YMax() float64

YMax returns the high edge of the Y-axis of this histogram.

func (*H2D) YMean

func (h *H2D) YMean() float64

YMean returns the mean Y. Overflows are included in the computation.

func (*H2D) YMin

func (h *H2D) YMin() float64

YMin returns the low edge of the Y-axis of this histogram.

func (*H2D) YRMS

func (h *H2D) YRMS() float64

YRMS returns the RMS in Y. Overflows are included in the computation.

func (*H2D) YStdDev

func (h *H2D) YStdDev() float64

YStdDev returns the standard deviation in Y. Overflows are included in the computation.

func (*H2D) YStdErr

func (h *H2D) YStdErr() float64

YStdErr returns the standard error in Y. Overflows are included in the computation.

func (*H2D) YVariance

func (h *H2D) YVariance() float64

YVariance returns the variance in Y. Overflows are included in the computation.

type Histogram

type Histogram interface {
	// Annotation returns the annotations attached to the
	// histogram. (e.g. name, title, ...)
	Annotation() Annotation

	// Name returns the name of this histogram
	Name() string

	// Rank returns the number of dimensions of this histogram.
	Rank() int

	// Entries returns the number of entries of this histogram.
	Entries() int64
}

Histogram is an n-dim histogram (with weighted entries)

type Object

type Object interface {
	Annotation() Annotation
	Name() string
}

Object is the general handle to any hbook data analysis object.

type P1D

type P1D struct {
	// contains filtered or unexported fields
}

P1D is a 1-dim profile histogram.

Example
package main

import (
	"fmt"
	"log"

	"go-hep.org/x/hep/hbook"
	"golang.org/x/exp/rand"
	"gonum.org/v1/gonum/mat"
	"gonum.org/v1/gonum/stat/distmv"
)

func main() {
	const npoints = 1000

	p := hbook.NewP1D(100, -10, 10)
	dist, ok := distmv.NewNormal(
		[]float64{0, 1},
		mat.NewSymDense(2, []float64{4, 0, 0, 2}),
		rand.New(rand.NewSource(1234)),
	)
	if !ok {
		log.Fatalf("error creating distmv.Normal")
	}

	v := make([]float64, 2)
	// Draw some random values from the standard
	// normal distribution.
	for i := 0; i < npoints; i++ {
		v = dist.Rand(v)
		p.Fill(v[0], v[1], 1)
	}

	fmt.Printf("mean:    %v\n", p.XMean())
	fmt.Printf("rms:     %v\n", p.XRMS())
	fmt.Printf("std-dev: %v\n", p.XStdDev())
	fmt.Printf("std-err: %v\n", p.XStdErr())

}
Output:

mean:    0.11198383683853215
rms:     2.0240892891977125
std-dev: 2.0220003848882695
std-err: 0.06394126645984038

func NewP1D

func NewP1D(n int, xmin, xmax float64) *P1D

NewP1D returns a 1-dim profile histogram with n bins between xmin and xmax.

func NewP1DFromH1D

func NewP1DFromH1D(h *H1D) *P1D

NewP1DFromH1D creates a 1-dim profile histogram from a 1-dim histogram's binning.

func (*P1D) Annotation

func (p *P1D) Annotation() Annotation

Annotation returns the annotations attached to this profile histogram

func (*P1D) Binning

func (p *P1D) Binning() *binningP1D

Binning returns the binning of this profile histogram

func (*P1D) EffEntries

func (p *P1D) EffEntries() float64

EffEntries returns the number of effective entries in this profile histogram

func (*P1D) Entries

func (p *P1D) Entries() int64

Entries returns the number of entries in this profile histogram

func (*P1D) Fill

func (p *P1D) Fill(x, y, w float64)

Fill fills this histogram with x,y and weight w.

func (*P1D) MarshalBinary

func (o *P1D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*P1D) MarshalYODA

func (p *P1D) MarshalYODA() ([]byte, error)

MarshalYODA implements the YODAMarshaler interface.

func (*P1D) Name

func (p *P1D) Name() string

Name returns the name of this profile histogram, if any

func (*P1D) Rank

func (p *P1D) Rank() int

Rank returns the number of dimensions for this profile histogram

func (*P1D) Scale

func (p *P1D) Scale(factor float64)

Scale scales the content of each bin by the given factor.

func (*P1D) SumW

func (p *P1D) SumW() float64

SumW returns the sum of weights in this profile histogram. Overflows are included in the computation.

func (*P1D) SumW2

func (p *P1D) SumW2() float64

SumW2 returns the sum of squared weights in this profile histogram. Overflows are included in the computation.

func (*P1D) UnmarshalBinary

func (o *P1D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*P1D) UnmarshalYODA

func (p *P1D) UnmarshalYODA(data []byte) error

UnmarshalYODA implements the YODAUnmarshaler interface.

func (*P1D) XMax

func (p *P1D) XMax() float64

XMax returns the high edge of the X-axis of this profile histogram.

func (*P1D) XMean

func (p *P1D) XMean() float64

XMean returns the mean X. Overflows are included in the computation.

func (*P1D) XMin

func (p *P1D) XMin() float64

XMin returns the low edge of the X-axis of this profile histogram.

func (*P1D) XRMS

func (p *P1D) XRMS() float64

XRMS returns the XRMS in X. Overflows are included in the computation.

func (*P1D) XStdDev

func (p *P1D) XStdDev() float64

XStdDev returns the standard deviation in X. Overflows are included in the computation.

func (*P1D) XStdErr

func (p *P1D) XStdErr() float64

XStdErr returns the standard error in X. Overflows are included in the computation.

func (*P1D) XVariance

func (p *P1D) XVariance() float64

XVariance returns the variance in X. Overflows are included in the computation.

type Point2D

type Point2D struct {
	X    float64 // x-position
	Y    float64 // y-position
	ErrX Range   // error on x-position
	ErrY Range   // error on y-position
}

Point2D is a position in a 2-dim space

func (*Point2D) MarshalBinary

func (o *Point2D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Point2D) ScaleX

func (p *Point2D) ScaleX(f float64)

ScaleX rescales the X value by a factor f.

func (*Point2D) ScaleXY

func (p *Point2D) ScaleXY(f float64)

ScaleXY rescales the X and Y values by a factor f.

func (*Point2D) ScaleY

func (p *Point2D) ScaleY(f float64)

ScaleY rescales the Y value by a factor f.

func (*Point2D) UnmarshalBinary

func (o *Point2D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (Point2D) XMax

func (p Point2D) XMax() float64

XMax returns the X value plus positive X-error

func (Point2D) XMin

func (p Point2D) XMin() float64

XMin returns the X value minus negative X-error

func (Point2D) YMax

func (p Point2D) YMax() float64

YMax returns the Y value plus positive Y-error

func (Point2D) YMin

func (p Point2D) YMin() float64

YMin returns the Y value minus negative Y-error

type Rand1D

type Rand1D struct {
	// contains filtered or unexported fields
}

Rand1D represents a 1D distribution created from a hbook.H1D histogram.

Example
package main

import (
	"fmt"
	"image/color"
	"log"

	"go-hep.org/x/hep/hbook"
	"go-hep.org/x/hep/hplot"
	"golang.org/x/exp/rand"
	"gonum.org/v1/gonum/stat/distuv"
	"gonum.org/v1/plot/vg"
)

func main() {
	const N = 10000
	var (
		h1  = hbook.NewH1D(100, -10, 10)
		src = rand.New(rand.NewSource(1234))
		rnd = distuv.Normal{
			Mu:    0,
			Sigma: 2,
			Src:   src,
		}
	)

	for i := 0; i < N; i++ {
		h1.Fill(rnd.Rand(), 1)
	}

	var (
		h2 = hbook.NewH1D(100, -10, 10)
		hr = hbook.NewRand1D(h1, rand.NewSource(5678))
	)

	for i := 0; i < N; i++ {
		h2.Fill(hr.Rand(), 1)
	}

	fmt.Printf(
		"h1: mean=%+8f std-dev=%+8f +/- %8f\n",
		h1.XMean(), h1.XStdDev(), h1.XStdErr(),
	)
	fmt.Printf(
		"cdf(0)= %+8f\ncdf(1)= %+8f\n",
		rnd.CDF(0), rnd.CDF(1),
	)
	fmt.Printf(
		"h2: mean=%+8f std-dev=%+8f +/- %8f\n",
		h2.XMean(), h2.XStdDev(), h2.XStdErr(),
	)
	fmt.Printf(
		"cdf(0)= %+8f\ncdf(1)= %+8f\n",
		hr.CDF(0), hr.CDF(1),
	)

	h1.Scale(1. / h1.Integral(h1.XMin(), h1.XMax()))
	h2.Scale(1. / h2.Integral(h2.XMin(), h2.XMax()))

	{
		rp := hplot.NewRatioPlot()
		rp.Ratio = 0.3

		rp.Top.Title.Text = "Distributions"
		rp.Top.Y.Label.Text = "Y"

		hh1 := hplot.NewH1D(h1)
		hh1.FillColor = color.NRGBA{R: 255, A: 100}

		hh2 := hplot.NewH1D(h2)
		hh2.FillColor = color.NRGBA{B: 255, A: 100}

		rp.Top.Add(
			hh1, hh2,
			hplot.NewGrid(),
		)

		rp.Top.Legend.Add("template", hh1)
		rp.Top.Legend.Add("monte-carlo", hh2)
		rp.Top.Legend.Top = true

		rp.Bottom.X.Label.Text = "X"
		rp.Bottom.Y.Label.Text = "Diff"
		rp.Bottom.Add(
			hplot.NewH1D(hbook.SubH1D(h1, h2)),
			hplot.NewGrid(),
		)

		err := hplot.Save(rp, 15*vg.Centimeter, -1, "testdata/rand_h1d.png")
		if err != nil {
			log.Fatal(err)
		}
	}

}
Output:

h1: mean=+0.020436 std-dev=+1.992307 +/- 0.019923
cdf(0)= +0.500000
cdf(1)= +0.691462
h2: mean=-0.003631 std-dev=+2.008359 +/- 0.020084
cdf(0)= +0.499700
cdf(1)= +0.687800

func NewRand1D

func NewRand1D(h *H1D, src rand.Source) Rand1D

NewRand1D creates a Rand1D from the provided histogram. If src is nil, the global x/exp/rand source will be used.

func (*Rand1D) CDF

func (d *Rand1D) CDF(x float64) float64

CDF computes the value of the cumulative density function at x.

func (*Rand1D) Rand

func (d *Rand1D) Rand() float64

Rand returns a random sample drawn from the distribution.

type Range

type Range struct {
	Min float64
	Max float64
}

Range is a 1-dim interval [Min, Max].

func (*Range) MarshalBinary

func (o *Range) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Range) UnmarshalBinary

func (o *Range) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (Range) Width

func (r Range) Width() float64

Width returns the size of the range.

type S2D

type S2D struct {
	// contains filtered or unexported fields
}

S2D is a collection of 2-dim data points with errors.

Example
package main

import (
	"fmt"
	"log"

	"go-hep.org/x/hep/hbook"
)

func main() {
	s := hbook.NewS2D(hbook.Point2D{X: 1, Y: 1}, hbook.Point2D{X: 2, Y: 1.5}, hbook.Point2D{X: -1, Y: +2})
	if s == nil {
		log.Fatal("nil pointer to S2D")
	}

	fmt.Printf("len=%d\n", s.Len())

	s.Fill(hbook.Point2D{X: 10, Y: -10, ErrX: hbook.Range{Min: 5, Max: 5}, ErrY: hbook.Range{Min: 6, Max: 6}})
	fmt.Printf("len=%d\n", s.Len())
	fmt.Printf("pt[%d]=%+v\n", 3, s.Point(3))

}
Output:

len=3
len=4
pt[3]={X:10 Y:-10 ErrX:{Min:5 Max:5} ErrY:{Min:6 Max:6}}
Example (NewS2DFrom)
package main

import (
	"fmt"
	"log"

	"go-hep.org/x/hep/hbook"
)

func main() {
	s := hbook.NewS2DFrom([]float64{1, 2, -1}, []float64{1, 1.5, 2})
	if s == nil {
		log.Fatal("nil pointer to S2D")
	}

	fmt.Printf("len=%d\n", s.Len())

	s.Fill(hbook.Point2D{X: 10, Y: -10, ErrX: hbook.Range{Min: 5, Max: 5}, ErrY: hbook.Range{Min: 6, Max: 6}})
	fmt.Printf("len=%d\n", s.Len())
	fmt.Printf("pt[%d]=%+v\n", 3, s.Point(3))

}
Output:

len=3
len=4
pt[3]={X:10 Y:-10 ErrX:{Min:5 Max:5} ErrY:{Min:6 Max:6}}
Example (NewS2DFromH1D)
package main

import (
	"fmt"

	"go-hep.org/x/hep/hbook"
)

func main() {
	h := hbook.NewH1D(20, -4, +4)
	h.Fill(1, 2)
	h.Fill(2, 3)
	h.Fill(3, 1)
	h.Fill(1, 1)
	h.Fill(-2, 1)
	h.Fill(-3, 1)

	s := hbook.NewS2DFromH1D(h)
	s.Sort()
	for _, pt := range s.Points() {
		fmt.Printf("point=(%+3.2f +/- (%+3.2f,%+3.2f), %+3.2f +/- (%+3.2f, %+3.2f))\n", pt.X, pt.ErrX.Min, pt.ErrX.Max, pt.Y, pt.ErrY.Min, pt.ErrY.Max)
	}

}
Output:

point=(-3.80 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(-3.40 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(-3.00 +/- (+0.20,+0.20), +2.50 +/- (+2.50, +2.50))
point=(-2.60 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(-2.20 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(-1.80 +/- (+0.20,+0.20), +2.50 +/- (+2.50, +2.50))
point=(-1.40 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(-1.00 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(-0.60 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(-0.20 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(+0.20 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(+0.60 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(+1.00 +/- (+0.20,+0.20), +7.50 +/- (+5.59, +5.59))
point=(+1.40 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(+1.80 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(+2.20 +/- (+0.20,+0.20), +7.50 +/- (+7.50, +7.50))
point=(+2.60 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(+3.00 +/- (+0.20,+0.20), +2.50 +/- (+2.50, +2.50))
point=(+3.40 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
point=(+3.80 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))

func DivideH1D

func DivideH1D(num, den *H1D, opts ...DivOptions) (*S2D, error)

DivideH1D divides 2 1D-histograms and returns a 2D scatter. DivideH1D returns an error if the binning of the 1D histograms are not compatible. If no DivOptions is passed, NaN raised during division are kept.

Example
h1 := NewH1D(5, 0, 5)
h1.Fill(0, 1)
h1.Fill(1, 2)
h1.Fill(2, 3)
h1.Fill(3, 3)
h1.Fill(4, 4)

h2 := NewH1D(5, 0, 5)
h2.Fill(0, 11)
h2.Fill(1, 22)
h2.Fill(2, 0)
h2.Fill(3, 33)
h2.Fill(4, 44)

s0, err := DivideH1D(h1, h2)
if err != nil {
	panic(err)
}
s1, err := DivideH1D(h1, h2, DivIgnoreNaNs())
if err != nil {
	panic(err)
}
s2, err := DivideH1D(h1, h2, DivReplaceNaNs(1.0))
if err != nil {
	panic(err)
}

fmt.Println("Default:")
for i, pt := range s0.Points() {
	fmt.Printf("Point %v: %.2f  + %.2f  - %.2f\n", i, pt.Y, pt.ErrY.Min, pt.ErrY.Min)
}

fmt.Println("\nDivIgnoreNaNs:")
for i, pt := range s1.Points() {
	fmt.Printf("Point %v: %.2f  + %.2f  - %.2f\n", i, pt.Y, pt.ErrY.Min, pt.ErrY.Min)
}

fmt.Println("\nDivReplaceNaNs with v=1.0:")
for i, pt := range s2.Points() {
	fmt.Printf("Point %v: %.2f  + %.2f  - %.2f\n", i, pt.Y, pt.ErrY.Min, pt.ErrY.Min)
}
Output:

Default:
Point 0: 0.09  + 0.13  - 0.13
Point 1: 0.09  + 0.13  - 0.13
Point 2: NaN  + 0.00  - 0.00
Point 3: 0.09  + 0.13  - 0.13
Point 4: 0.09  + 0.13  - 0.13

DivIgnoreNaNs:
Point 0: 0.09  + 0.13  - 0.13
Point 1: 0.09  + 0.13  - 0.13
Point 2: 0.09  + 0.13  - 0.13
Point 3: 0.09  + 0.13  - 0.13

DivReplaceNaNs with v=1.0:
Point 0: 0.09  + 0.13  - 0.13
Point 1: 0.09  + 0.13  - 0.13
Point 2: 1.00  + 0.00  - 0.00
Point 3: 0.09  + 0.13  - 0.13
Point 4: 0.09  + 0.13  - 0.13

func NewS2D

func NewS2D(pts ...Point2D) *S2D

NewS2D creates a new 2-dim scatter with pts as an optional initial set of data points.

If n <= 0, the initial capacity is 0.

func NewS2DFrom

func NewS2DFrom(x, y []float64) *S2D

NewS2DFrom creates a new 2-dim scatter with x,y data slices.

It panics if the lengths of the 2 slices don't match.

func NewS2DFromH1D

func NewS2DFromH1D(h *H1D, opts ...S2DOpts) *S2D

NewS2DFromH1D creates a new 2-dim scatter from the given H1D. NewS2DFromH1D optionally takes a S2DOpts slice: only the first element is considered.

func NewS2DFromP1D

func NewS2DFromP1D(p *P1D, opts ...S2DOpts) *S2D

NewS2DFromP1D creates a new 2-dim scatter from the given P1D. NewS2DFromP1D optionally takes a S2DOpts slice: only the first element is considered.

func (*S2D) Annotation

func (s *S2D) Annotation() Annotation

Annotation returns the annotations attached to the scatter. (e.g. name, title, ...)

func (*S2D) DataRange

func (s *S2D) DataRange() (xmin, xmax, ymin, ymax float64)

DataRange returns the minimum and maximum x and y values, implementing the gonum/plot.DataRanger interface.

func (*S2D) Entries

func (s *S2D) Entries() int64

Entries returns the number of entries of this histogram.

func (*S2D) Fill

func (s *S2D) Fill(pts ...Point2D)

Fill adds new points to the scatter.

func (*S2D) Len

func (s *S2D) Len() int

Len returns the number of points in the scatter.

Len implements the gonum/plot/plotter.XYer interface.

func (*S2D) MarshalBinary

func (o *S2D) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*S2D) MarshalYODA

func (s *S2D) MarshalYODA() ([]byte, error)

MarshalYODA implements the YODAMarshaler interface.

func (*S2D) Name

func (s *S2D) Name() string

Name returns the name of this scatter

func (*S2D) Point

func (s *S2D) Point(i int) Point2D

Point returns the point at index i.

Point panics if i is out of bounds.

func (*S2D) Points

func (s *S2D) Points() []Point2D

Points returns the points of the scatter.

Users may not modify the returned slice. Users may not rely on the stability of the indices as the slice of points may be re-sorted at any point in time.

func (*S2D) Rank

func (*S2D) Rank() int

Rank returns the number of dimensions of this scatter.

func (*S2D) ScaleX

func (s *S2D) ScaleX(f float64)

ScaleX rescales the X values by a factor f.

func (*S2D) ScaleXY

func (s *S2D) ScaleXY(f float64)

ScaleXY rescales the X and Y values by a factor f.

func (*S2D) ScaleY

func (s *S2D) ScaleY(f float64)

ScaleY rescales the Y values by a factor f.

func (*S2D) Sort

func (s *S2D) Sort()

Sort sorts the data points by x,y and x-err,y-err.

func (*S2D) UnmarshalBinary

func (o *S2D) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*S2D) UnmarshalYODA

func (s *S2D) UnmarshalYODA(data []byte) error

UnmarshalYODA implements the YODAUnmarshaler interface.

func (*S2D) XError

func (s *S2D) XError(i int) (float64, float64)

XError returns the two error values for X data.

XError implements the gonum/plot/plotter.XErrorer interface.

func (*S2D) XY

func (s *S2D) XY(i int) (x, y float64)

XY returns the x, y pair at index i.

XY panics if i is out of bounds. XY implements the gonum/plot/plotter.XYer interface.

func (*S2D) YError

func (s *S2D) YError(i int) (float64, float64)

YError returns the two error values for Y data.

YError implements the gonum/plot/plotter.YErrorer interface.

type S2DOpts

type S2DOpts struct {
	UseFocus  bool
	UseStdDev bool
}

S2DOpts controls how S2D scatters are created from H1D and P1D.

Directories

Path Synopsis
Package ntup provides a way to create, open and iterate over n-tuple data.
Package ntup provides a way to create, open and iterate over n-tuple data.
ntcsv
Package ntcsv provides a convenient access to CSV files as n-tuple data.
Package ntcsv provides a convenient access to CSV files as n-tuple data.
ntroot
Package ntroot provides convenience functions to access ROOT trees as n-tuple data.
Package ntroot provides convenience functions to access ROOT trees as n-tuple data.
Package rootcnv provides tools to convert ROOT histograms and graphs to go-hep/hbook ones.
Package rootcnv provides tools to convert ROOT histograms and graphs to go-hep/hbook ones.
Package yodacnv provides tools to read/write YODA archive files.
Package yodacnv provides tools to read/write YODA archive files.