go-hep.org/x/hep@v0.38.1/hbook/s2d_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  
    11  	"go-hep.org/x/hep/hbook"
    12  )
    13  
    14  func ExampleS2D() {
    15  	s := hbook.NewS2D(hbook.Point2D{X: 1, Y: 1}, hbook.Point2D{X: 2, Y: 1.5}, hbook.Point2D{X: -1, Y: +2})
    16  	if s == nil {
    17  		log.Fatal("nil pointer to S2D")
    18  	}
    19  
    20  	fmt.Printf("len=%d\n", s.Len())
    21  
    22  	s.Fill(hbook.Point2D{X: 10, Y: -10, ErrX: hbook.Range{Min: 5, Max: 5}, ErrY: hbook.Range{Min: 6, Max: 6}})
    23  	fmt.Printf("len=%d\n", s.Len())
    24  	fmt.Printf("pt[%d]=%+v\n", 3, s.Point(3))
    25  
    26  	// Output:
    27  	// len=3
    28  	// len=4
    29  	// pt[3]={X:10 Y:-10 ErrX:{Min:5 Max:5} ErrY:{Min:6 Max:6}}
    30  }
    31  
    32  func ExampleS2D_newS2DFrom() {
    33  	s := hbook.NewS2DFrom([]float64{1, 2, -1}, []float64{1, 1.5, 2})
    34  	if s == nil {
    35  		log.Fatal("nil pointer to S2D")
    36  	}
    37  
    38  	fmt.Printf("len=%d\n", s.Len())
    39  
    40  	s.Fill(hbook.Point2D{X: 10, Y: -10, ErrX: hbook.Range{Min: 5, Max: 5}, ErrY: hbook.Range{Min: 6, Max: 6}})
    41  	fmt.Printf("len=%d\n", s.Len())
    42  	fmt.Printf("pt[%d]=%+v\n", 3, s.Point(3))
    43  
    44  	// Output:
    45  	// len=3
    46  	// len=4
    47  	// pt[3]={X:10 Y:-10 ErrX:{Min:5 Max:5} ErrY:{Min:6 Max:6}}
    48  }
    49  
    50  func ExampleS2D_newS2DFromH1D() {
    51  	h := hbook.NewH1D(20, -4, +4)
    52  	h.Fill(1.1, 2)
    53  	h.Fill(2.1, 3)
    54  	h.Fill(3.1, 1)
    55  	h.Fill(1.1, 1)
    56  	h.Fill(-2.1, 1)
    57  	h.Fill(-3.1, 1)
    58  
    59  	s := hbook.NewS2DFromH1D(h)
    60  	s.Sort()
    61  	for _, pt := range s.Points() {
    62  		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)
    63  	}
    64  
    65  	// Output:
    66  	// point=(-3.80 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    67  	// point=(-3.40 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    68  	// point=(-3.00 +/- (+0.20,+0.20), +2.50 +/- (+2.50, +2.50))
    69  	// point=(-2.60 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    70  	// point=(-2.20 +/- (+0.20,+0.20), +2.50 +/- (+2.50, +2.50))
    71  	// point=(-1.80 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    72  	// point=(-1.40 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    73  	// point=(-1.00 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    74  	// point=(-0.60 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    75  	// point=(-0.20 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    76  	// point=(+0.20 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    77  	// point=(+0.60 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    78  	// point=(+1.00 +/- (+0.20,+0.20), +7.50 +/- (+5.59, +5.59))
    79  	// point=(+1.40 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    80  	// point=(+1.80 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    81  	// point=(+2.20 +/- (+0.20,+0.20), +7.50 +/- (+7.50, +7.50))
    82  	// point=(+2.60 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    83  	// point=(+3.00 +/- (+0.20,+0.20), +2.50 +/- (+2.50, +2.50))
    84  	// point=(+3.40 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    85  	// point=(+3.80 +/- (+0.20,+0.20), +0.00 +/- (+0.00, +0.00))
    86  }