go-hep.org/x/hep@v0.38.1/fmom/pxpypze_test.go (about)

     1  // Copyright ©2017 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 fmom
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  
    11  	"gonum.org/v1/gonum/floats/scalar"
    12  )
    13  
    14  func TestPxPyPzE(t *testing.T) {
    15  	{
    16  		var p4 PxPyPzE
    17  		if got, want := p4.Px(), 0.0; got != want {
    18  			t.Fatalf("p4.Px=%v, want=%v", got, want)
    19  		}
    20  		if got, want := p4.Py(), 0.0; got != want {
    21  			t.Fatalf("p4.Py=%v, want=%v", got, want)
    22  		}
    23  		if got, want := p4.Pz(), 0.0; got != want {
    24  			t.Fatalf("p4.Pz=%v, want=%v", got, want)
    25  		}
    26  		if got, want := p4.E(), 0.0; got != want {
    27  			t.Fatalf("p4.E=%v, want=%v", got, want)
    28  		}
    29  		if got, want := p4.String(), "fmom.P4{Px:0, Py:0, Pz:0, E:0}"; got != want {
    30  			t.Fatalf("p4=%v, want=%v", got, want)
    31  		}
    32  	}
    33  
    34  	{
    35  		p4 := NewPxPyPzE(10, 11, 12, 20)
    36  		if got, want := p4.Px(), 10.0; got != want {
    37  			t.Fatalf("p4.Px=%v, want=%v", got, want)
    38  		}
    39  		if got, want := p4.Py(), 11.0; got != want {
    40  			t.Fatalf("p4.Py=%v, want=%v", got, want)
    41  		}
    42  		if got, want := p4.Pz(), 12.0; got != want {
    43  			t.Fatalf("p4.Pz=%v, want=%v", got, want)
    44  		}
    45  		if got, want := p4.E(), 20.0; got != want {
    46  			t.Fatalf("p4.E=%v, want=%v", got, want)
    47  		}
    48  		if got, want := p4.X(), 10.0; got != want {
    49  			t.Fatalf("p4.X=%v, want=%v", got, want)
    50  		}
    51  		if got, want := p4.Y(), 11.0; got != want {
    52  			t.Fatalf("p4.Y=%v, want=%v", got, want)
    53  		}
    54  		if got, want := p4.Z(), 12.0; got != want {
    55  			t.Fatalf("p4.Z=%v, want=%v", got, want)
    56  		}
    57  		if got, want := p4.T(), 20.0; got != want {
    58  			t.Fatalf("p4.T=%v, want=%v", got, want)
    59  		}
    60  		if got, want := p4.String(), "fmom.P4{Px:10, Py:11, Pz:12, E:20}"; got != want {
    61  			t.Fatalf("p4=%v, want=%v", got, want)
    62  		}
    63  
    64  		p1 := NewPxPyPzE(10, 11, 12, 20)
    65  		if p1 != p4 {
    66  			t.Fatalf("p4=%v, want=%v", p1, p4)
    67  		}
    68  
    69  		var p2 PxPyPzE = p1
    70  		if p1 != p2 {
    71  			t.Fatalf("p4=%v, want=%v", p1, p2)
    72  		}
    73  	}
    74  
    75  	{
    76  		p1 := NewPxPyPzE(10, 11, 12, 20)
    77  		var p2 PxPyPzE
    78  		p2.Set(&p1)
    79  		if p1 != p2 {
    80  			t.Fatalf("p4=%v want=%v", p2, p1)
    81  		}
    82  	}
    83  
    84  	p := NewPxPyPzE(10, 11, 12, 20)
    85  
    86  	// values obtained with ROOT-6.14.00
    87  	for _, tc := range []struct {
    88  		name string
    89  		got  float64
    90  		want float64
    91  		ulp  uint
    92  	}{
    93  		{
    94  			name: "phi",
    95  			got:  p.Phi(),
    96  			want: 8.329812666744317e-01,
    97  			ulp:  1,
    98  		},
    99  		{
   100  			name: "sin-phi",
   101  			got:  p.SinPhi(),
   102  			want: 7.399400733959437e-01,
   103  			ulp:  1,
   104  		},
   105  		{
   106  			name: "cos-phi",
   107  			got:  p.CosPhi(),
   108  			want: 6.726727939963124e-01,
   109  			ulp:  1,
   110  		},
   111  		{
   112  			name: "eta",
   113  			got:  p.Eta(),
   114  			want: 7.382863647914931e-01,
   115  			ulp:  1,
   116  		},
   117  		{
   118  			name: "tan-th",
   119  			got:  p.TanTh(),
   120  			want: 1.238839062276542e+00,
   121  			ulp:  1,
   122  		},
   123  		{
   124  			name: "cos-th",
   125  			got:  p.CosTh(),
   126  			want: 6.281087071082564e-01,
   127  			ulp:  1,
   128  		},
   129  		{
   130  			name: "rapidity",
   131  			got:  p.Rapidity(),
   132  			want: 6.931471805599453e-01,
   133  			ulp:  1,
   134  		},
   135  	} {
   136  		t.Run(tc.name, func(t *testing.T) {
   137  			if !scalar.EqualWithinULP(tc.got, tc.want, tc.ulp) {
   138  				t.Fatalf("error (ulp=%d)\ngot = %v\nwant= %v", tc.ulp, tc.got, tc.want)
   139  			}
   140  		})
   141  	}
   142  
   143  	const epsilon = 1e-12
   144  	t.Run("set-PtEtaPhiM", func(t *testing.T) {
   145  		p1 := NewPxPyPzE(10, 20, 30, 40)
   146  		p1.SetPtEtaPhiM(100, 1.5, 1/3.*math.Pi, 10)
   147  		want := NewPxPyPzE(
   148  			49.99999999999999,
   149  			86.60254037844388,
   150  			212.9279455094818,
   151  			235.45341360636257,
   152  		)
   153  		if got := p1; !p4equal(&got, &want, epsilon) {
   154  			t.Fatalf("invalid p4:\ngot= %v\nwant=%v", got, want)
   155  		}
   156  	})
   157  
   158  	t.Run("set-PtEtaPhiE", func(t *testing.T) {
   159  		p1 := NewPxPyPzE(10, 20, 30, 40)
   160  		p1.SetPtEtaPhiE(100, 1.5, 1/3.*math.Pi, 10)
   161  		want := NewPxPyPzE(
   162  			49.99999999999999,
   163  			86.60254037844388,
   164  			212.9279455094818,
   165  			10,
   166  		)
   167  		if got := p1; !p4equal(&got, &want, epsilon) {
   168  			t.Fatalf("invalid p4:\ngot= %v\nwant=%v", got, want)
   169  		}
   170  	})
   171  }