go-hep.org/x/hep@v0.38.1/groot/rphys/lorentzvector_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 rphys_test
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  
    11  	"go-hep.org/x/hep/groot/rcmd"
    12  	"go-hep.org/x/hep/groot/rphys"
    13  	"go-hep.org/x/hep/internal/diff"
    14  )
    15  
    16  func TestLorentzVector(t *testing.T) {
    17  	p4 := rphys.NewLorentzVector(1, 2, 3, 4)
    18  
    19  	for _, tc := range []struct {
    20  		name string
    21  		fct  func() float64
    22  		want float64
    23  	}{
    24  		{
    25  			name: "px",
    26  			fct:  p4.Px,
    27  			want: 1,
    28  		},
    29  		{
    30  			name: "py",
    31  			fct:  p4.Py,
    32  			want: 2,
    33  		},
    34  		{
    35  			name: "pz",
    36  			fct:  p4.Pz,
    37  			want: 3,
    38  		},
    39  		{
    40  			name: "e",
    41  			fct:  p4.E,
    42  			want: 4,
    43  		},
    44  	} {
    45  		t.Run(tc.name, func(t *testing.T) {
    46  			got := tc.fct()
    47  			if got != tc.want {
    48  				t.Fatalf("invalid getter value: got=%v, want=%v", got, tc.want)
    49  			}
    50  		})
    51  	}
    52  
    53  	p4.SetPxPyPzE(-1, -2, -3, 44)
    54  
    55  	for _, tc := range []struct {
    56  		name string
    57  		fct  func() float64
    58  		want float64
    59  	}{
    60  		{
    61  			name: "px",
    62  			fct:  p4.Px,
    63  			want: -1,
    64  		},
    65  		{
    66  			name: "py",
    67  			fct:  p4.Py,
    68  			want: -2,
    69  		},
    70  		{
    71  			name: "pz",
    72  			fct:  p4.Pz,
    73  			want: -3,
    74  		},
    75  		{
    76  			name: "e",
    77  			fct:  p4.E,
    78  			want: 44,
    79  		},
    80  	} {
    81  		t.Run(tc.name, func(t *testing.T) {
    82  			got := tc.fct()
    83  			if got != tc.want {
    84  				t.Fatalf("invalid getter value: got=%v, want=%v", got, tc.want)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestLorentzVectorRStreamer(t *testing.T) {
    91  	const (
    92  		deep = true
    93  		want = `key[000]: tlv;1 "A four vector with (-,-,-,+) metric" (TLorentzVector) => "TLorentzVector{P: {10, 20, 30}, E: 40}"
    94  key[001]: tree;1 "my tree title" (TTree)
    95  [000][p4]: {{0 50331648} {{0 50331648} 0 1 2} 3}
    96  [001][p4]: {{0 50331648} {{0 50331648} 1 2 3} 4}
    97  [002][p4]: {{0 50331648} {{0 50331648} 2 3 4} 5}
    98  [003][p4]: {{0 50331648} {{0 50331648} 3 4 5} 6}
    99  [004][p4]: {{0 50331648} {{0 50331648} 4 5 6} 7}
   100  [005][p4]: {{0 50331648} {{0 50331648} 5 6 7} 8}
   101  [006][p4]: {{0 50331648} {{0 50331648} 6 7 8} 9}
   102  [007][p4]: {{0 50331648} {{0 50331648} 7 8 9} 10}
   103  [008][p4]: {{0 50331648} {{0 50331648} 8 9 10} 11}
   104  [009][p4]: {{0 50331648} {{0 50331648} 9 10 11} 12}
   105  `
   106  	)
   107  
   108  	for _, fname := range []string{
   109  		"../testdata/tlv-split00.root", // exercizes T{Branch,Leaf}Object
   110  		"../testdata/tlv-split01.root",
   111  		"../testdata/tlv-split99.root",
   112  	} {
   113  		t.Run(fname, func(t *testing.T) {
   114  			got := new(strings.Builder)
   115  			err := rcmd.Dump(got, fname, deep, nil)
   116  			if err != nil {
   117  				t.Fatalf("could not run root-dump: %+v", err)
   118  			}
   119  
   120  			if got, want := got.String(), want; got != want {
   121  				t.Fatalf("invalid root-dump output:\n%s", diff.Format(got, want))
   122  			}
   123  		})
   124  	}
   125  }