go-hep.org/x/hep@v0.38.1/hbook/rand_h1d_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
     6  
     7  import (
     8  	"math/rand/v2"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  )
    14  
    15  func TestRand1D(t *testing.T) {
    16  	edges := []float64{0, 1, 2, 3, 4}
    17  	h1 := NewH1DFromEdges(edges)
    18  	h1.FillN([]float64{
    19  		0,
    20  		1, 1, 1,
    21  		2,
    22  		3, 3, 3, 3, 3,
    23  	}, nil)
    24  
    25  	hr := NewRand1D(h1, rand.NewPCG(1234, 1234))
    26  	h2 := NewH1DFromEdges(edges)
    27  
    28  	if got, want := hr.cdf, []float64{0, 0.1, 0.4, 0.5, 1}; !reflect.DeepEqual(got, want) {
    29  		t.Errorf("invalid cdf:\ngot= %g\nwant=%g", got, want)
    30  	}
    31  
    32  	for _, test := range []struct{ v, want float64 }{
    33  		{-10, 0},
    34  		{+10, 1},
    35  	} {
    36  		if got, want := hr.CDF(test.v), test.want; got != want {
    37  			t.Errorf("CDF(%g): got=%g, want=%g", test.v, got, want)
    38  		}
    39  	}
    40  
    41  	const N = 1000
    42  	for range N {
    43  		h2.Fill(hr.Rand(), 1)
    44  	}
    45  
    46  	h1.Scale(1. / h1.Integral(h1.XMin(), h1.XMax()))
    47  	h2.Scale(1. / h2.Integral(h2.XMin(), h2.XMax()))
    48  
    49  	txt1, err := h1.MarshalYODA()
    50  	if err != nil {
    51  		t.Fatalf("could not marshal h1: %+v", err)
    52  	}
    53  
    54  	const wantTxt1 = `BEGIN YODA_HISTO1D_V2 /
    55  Path: /
    56  Title: ""
    57  Type: Histo1D
    58  ---
    59  # Mean: 2.000000e+00
    60  # Area: 1.000000e+00
    61  # ID	 ID	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
    62  Total   	Total   	1.000000e+00	1.000000e-01	2.000000e+00	5.200000e+00	1.000000e+01
    63  Underflow	Underflow	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
    64  Overflow	Overflow	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
    65  # xlow	 xhigh	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
    66  0.000000e+00	1.000000e+00	1.000000e-01	1.000000e-02	0.000000e+00	0.000000e+00	1.000000e+00
    67  1.000000e+00	2.000000e+00	3.000000e-01	3.000000e-02	3.000000e-01	3.000000e-01	3.000000e+00
    68  2.000000e+00	3.000000e+00	1.000000e-01	1.000000e-02	2.000000e-01	4.000000e-01	1.000000e+00
    69  3.000000e+00	4.000000e+00	5.000000e-01	5.000000e-02	1.500000e+00	4.500000e+00	5.000000e+00
    70  END YODA_HISTO1D_V2
    71  
    72  `
    73  	if got, want := string(txt1), wantTxt1; got != want {
    74  		t.Errorf(
    75  			"invalid h1 distribution:\n%s",
    76  			cmp.Diff(want, got),
    77  		)
    78  	}
    79  
    80  	const wantTxt2 = `BEGIN YODA_HISTO1D_V2 /
    81  Path: /
    82  Title: ""
    83  Type: Histo1D
    84  ---
    85  # Mean: 2.494672e+00
    86  # Area: 1.000000e+00
    87  # ID	 ID	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
    88  Total   	Total   	1.000000e+00	1.000000e-03	2.494672e+00	7.510616e+00	1.000000e+03
    89  Underflow	Underflow	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
    90  Overflow	Overflow	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00
    91  # xlow	 xhigh	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
    92  0.000000e+00	1.000000e+00	9.800000e-02	9.800000e-05	4.971524e-02	3.505977e-02	9.800000e+01
    93  1.000000e+00	2.000000e+00	3.110000e-01	3.110000e-04	4.633349e-01	7.148637e-01	3.110000e+02
    94  2.000000e+00	3.000000e+00	8.900000e-02	8.900000e-05	2.272178e-01	5.882321e-01	8.900000e+01
    95  3.000000e+00	4.000000e+00	5.020000e-01	5.020000e-04	1.754404e+00	6.172460e+00	5.020000e+02
    96  END YODA_HISTO1D_V2
    97  
    98  `
    99  	txt2, err := h2.MarshalYODA()
   100  	if err != nil {
   101  		t.Fatalf("could not marshal h2: %+v", err)
   102  	}
   103  
   104  	if got, want := string(txt2), wantTxt2; got != want {
   105  		t.Errorf(
   106  			"invalid h2 distribution:\n%s",
   107  			cmp.Diff(want, got),
   108  		)
   109  	}
   110  }