go-hep.org/x/hep@v0.38.1/hbook/h2d_example_test.go (about)

     1  // Copyright ©2020 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 hbook_test
     6  
     7  import (
     8  	"log"
     9  	"math/rand/v2"
    10  
    11  	"go-hep.org/x/hep/hbook"
    12  	"gonum.org/v1/gonum/mat"
    13  	"gonum.org/v1/gonum/stat/distmv"
    14  )
    15  
    16  func ExampleH2D() {
    17  	h := hbook.NewH2D(100, -10, 10, 100, -10, 10)
    18  
    19  	const npoints = 10000
    20  
    21  	dist, ok := distmv.NewNormal(
    22  		[]float64{0, 1},
    23  		mat.NewSymDense(2, []float64{4, 0, 0, 2}),
    24  		rand.New(rand.NewPCG(1234, 1234)),
    25  	)
    26  	if !ok {
    27  		log.Fatalf("error creating distmv.Normal")
    28  	}
    29  
    30  	v := make([]float64, 2)
    31  	// Draw some random values from the standard
    32  	// normal distribution.
    33  	for range npoints {
    34  		v = dist.Rand(v)
    35  		h.Fill(v[0], v[1], 1)
    36  	}
    37  
    38  	// fill h with slices of values and their weights
    39  	h.FillN(
    40  		[]float64{1, 2, 3}, // xs
    41  		[]float64{1, 2, 3}, // ys
    42  		[]float64{1, 1, 1}, // ws
    43  	)
    44  
    45  	// fill h with slices of values. all weights are 1.
    46  	h.FillN(
    47  		[]float64{1, 2, 3}, // xs
    48  		[]float64{1, 2, 3}, // ys
    49  		nil,                // ws
    50  	)
    51  }