go-hep.org/x/hep@v0.38.1/hbook/p1d_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  	"fmt"
     9  	"log"
    10  	"math/rand/v2"
    11  
    12  	"go-hep.org/x/hep/hbook"
    13  	"gonum.org/v1/gonum/mat"
    14  	"gonum.org/v1/gonum/stat/distmv"
    15  )
    16  
    17  func ExampleP1D() {
    18  	const npoints = 1000
    19  
    20  	p := hbook.NewP1D(100, -10, 10)
    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  		p.Fill(v[0], v[1], 1)
    36  	}
    37  
    38  	fmt.Printf("mean:    %v\n", p.XMean())
    39  	fmt.Printf("rms:     %v\n", p.XRMS())
    40  	fmt.Printf("std-dev: %v\n", p.XStdDev())
    41  	fmt.Printf("std-err: %v\n", p.XStdErr())
    42  
    43  	// Output:
    44  	// mean:    0.0965063103552738
    45  	// rms:     2.0024868956390707
    46  	// std-dev: 2.0011608991313086
    47  	// std-err: 0.06328226405725404
    48  }