go-hep.org/x/hep@v0.38.1/hbook/h1d_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  	"math"
    10  	"math/rand/v2"
    11  
    12  	"go-hep.org/x/hep/hbook"
    13  	"gonum.org/v1/gonum/stat/distuv"
    14  )
    15  
    16  func ExampleH1D() {
    17  	const npoints = 10000
    18  
    19  	// Create a normal distribution.
    20  	dist := distuv.Normal{
    21  		Mu:    0,
    22  		Sigma: 1,
    23  		Src:   rand.New(rand.NewPCG(0, 0)),
    24  	}
    25  
    26  	// Draw some random values from the standard
    27  	// normal distribution.
    28  	h := hbook.NewH1D(20, -4, +4)
    29  	for range npoints {
    30  		v := dist.Rand()
    31  		h.Fill(v, 1)
    32  	}
    33  	// fill h with a slice of values and their weights
    34  	h.FillN([]float64{1, 2, 3}, []float64{1, 1, 1})
    35  	h.FillN([]float64{1, 2, 3}, nil) // all weights are 1.
    36  
    37  	fmt.Printf("mean:    %.12f\n", h.XMean())
    38  	fmt.Printf("rms:     %.12f\n", h.XRMS())
    39  	fmt.Printf("std-dev: %.12f\n", h.XStdDev())
    40  	fmt.Printf("std-err: %.12f\n", h.XStdErr())
    41  
    42  	// Output:
    43  	// mean:    0.002104228518
    44  	// rms:     1.000617135827
    45  	// std-dev: 1.000664927794
    46  	// std-err: 0.010003648633
    47  }
    48  
    49  func ExampleAddH1D() {
    50  
    51  	h1 := hbook.NewH1D(6, 0, 6)
    52  	h1.Fill(-0.5, 1)
    53  	h1.Fill(0, 1.5)
    54  	h1.Fill(0.5, 1)
    55  	h1.Fill(1.2, 1)
    56  	h1.Fill(2.1, 2)
    57  	h1.Fill(4.2, 1)
    58  	h1.Fill(5.9, 1)
    59  	h1.Fill(6, 0.5)
    60  
    61  	h2 := hbook.NewH1D(6, 0, 6)
    62  	h2.Fill(-0.5, 0.7)
    63  	h2.Fill(0.2, 1)
    64  	h2.Fill(0.7, 1.2)
    65  	h2.Fill(1.5, 0.8)
    66  	h2.Fill(2.2, 0.7)
    67  	h2.Fill(4.3, 1.3)
    68  	h2.Fill(5.2, 2)
    69  	h2.Fill(6.8, 1)
    70  
    71  	hsum := hbook.AddH1D(h1, h2)
    72  	fmt.Printf("Under: %.1f +/- %.1f\n", hsum.Binning.Outflows[0].SumW(), math.Sqrt(hsum.Binning.Outflows[0].SumW2()))
    73  	for i := range hsum.Len() {
    74  		fmt.Printf("Bin %v: %.1f +/- %.1f\n", i, hsum.Binning.Bins[i].SumW(), math.Sqrt(hsum.Binning.Bins[i].SumW2()))
    75  	}
    76  	fmt.Printf("Over : %.1f +/- %.1f\n", hsum.Binning.Outflows[1].SumW(), math.Sqrt(hsum.Binning.Outflows[1].SumW2()))
    77  
    78  	// Output:
    79  	// Under: 1.7 +/- 1.2
    80  	// Bin 0: 4.7 +/- 2.4
    81  	// Bin 1: 1.8 +/- 1.3
    82  	// Bin 2: 2.7 +/- 2.1
    83  	// Bin 3: 0.0 +/- 0.0
    84  	// Bin 4: 2.3 +/- 1.6
    85  	// Bin 5: 3.0 +/- 2.2
    86  	// Over : 1.5 +/- 1.1
    87  }
    88  
    89  func ExampleAddScaledH1D() {
    90  
    91  	h1 := hbook.NewH1D(6, 0, 6)
    92  	h1.Fill(-0.5, 1)
    93  	h1.Fill(0, 1.5)
    94  	h1.Fill(0.5, 1)
    95  	h1.Fill(1.2, 1)
    96  	h1.Fill(2.1, 2)
    97  	h1.Fill(4.2, 1)
    98  	h1.Fill(5.9, 1)
    99  	h1.Fill(6, 0.5)
   100  
   101  	h2 := hbook.NewH1D(6, 0, 6)
   102  	h2.Fill(-0.5, 0.7)
   103  	h2.Fill(0.2, 1)
   104  	h2.Fill(0.7, 1.2)
   105  	h2.Fill(1.5, 0.8)
   106  	h2.Fill(2.2, 0.7)
   107  	h2.Fill(4.3, 1.3)
   108  	h2.Fill(5.2, 2)
   109  	h2.Fill(6.8, 1)
   110  
   111  	hsum := hbook.AddScaledH1D(h1, 10, h2)
   112  	fmt.Printf("Under: %.1f +/- %.1f\n", hsum.Binning.Outflows[0].SumW(), math.Sqrt(hsum.Binning.Outflows[0].SumW2()))
   113  	for i := range hsum.Len() {
   114  		fmt.Printf("Bin %v: %.1f +/- %.1f\n", i, hsum.Binning.Bins[i].SumW(), math.Sqrt(hsum.Binning.Bins[i].SumW2()))
   115  	}
   116  	fmt.Printf("Over : %.1f +/- %.1f\n", hsum.Binning.Outflows[1].SumW(), math.Sqrt(hsum.Binning.Outflows[1].SumW2()))
   117  
   118  	// Output:
   119  	// Under: 8.0 +/- 7.1
   120  	// Bin 0: 24.5 +/- 15.7
   121  	// Bin 1: 9.0 +/- 8.1
   122  	// Bin 2: 9.0 +/- 7.3
   123  	// Bin 3: 0.0 +/- 0.0
   124  	// Bin 4: 14.0 +/- 13.0
   125  	// Bin 5: 21.0 +/- 20.0
   126  	// Over : 10.5 +/- 10.0
   127  }
   128  
   129  func ExampleSubH1D() {
   130  
   131  	h1 := hbook.NewH1D(6, 0, 6)
   132  	h1.Fill(-0.5, 1)
   133  	h1.Fill(0, 1.5)
   134  	h1.Fill(0.5, 1)
   135  	h1.Fill(1.2, 1)
   136  	h1.Fill(2.1, 2)
   137  	h1.Fill(4.2, 1)
   138  	h1.Fill(5.9, 1)
   139  	h1.Fill(6, 0.5)
   140  
   141  	h2 := hbook.NewH1D(6, 0, 6)
   142  	h2.Fill(-0.5, 0.7)
   143  	h2.Fill(0.2, 1)
   144  	h2.Fill(0.7, 1.2)
   145  	h2.Fill(1.5, 0.8)
   146  	h2.Fill(2.2, 0.7)
   147  	h2.Fill(4.3, 1.3)
   148  	h2.Fill(5.2, 2)
   149  	h2.Fill(6.8, 1)
   150  
   151  	hsub := hbook.SubH1D(h1, h2)
   152  	under := hsub.Binning.Outflows[0]
   153  	fmt.Printf("Under: %.1f +/- %.1f\n", under.SumW(), math.Sqrt(under.SumW2()))
   154  	for i, bin := range hsub.Binning.Bins {
   155  		fmt.Printf("Bin %v: %.1f +/- %.1f\n", i, bin.SumW(), math.Sqrt(bin.SumW2()))
   156  	}
   157  	over := hsub.Binning.Outflows[1]
   158  	fmt.Printf("Over : %.1f +/- %.1f\n", over.SumW(), math.Sqrt(over.SumW2()))
   159  
   160  	// Output:
   161  	// Under: 0.3 +/- 1.2
   162  	// Bin 0: 0.3 +/- 2.4
   163  	// Bin 1: 0.2 +/- 1.3
   164  	// Bin 2: 1.3 +/- 2.1
   165  	// Bin 3: 0.0 +/- 0.0
   166  	// Bin 4: -0.3 +/- 1.6
   167  	// Bin 5: -1.0 +/- 2.2
   168  	// Over : -0.5 +/- 1.1
   169  }