go-hep.org/x/hep@v0.38.1/groot/rhist/graph_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 rhist_test 6 7 import ( 8 "testing" 9 10 "go-hep.org/x/hep/groot" 11 "go-hep.org/x/hep/groot/rbase" 12 "go-hep.org/x/hep/groot/rhist" 13 "go-hep.org/x/hep/groot/root" 14 "go-hep.org/x/hep/hbook" 15 ) 16 17 func TestGraph(t *testing.T) { 18 f, err := groot.Open("../testdata/graphs.root") 19 if err != nil { 20 t.Fatal(err) 21 } 22 defer f.Close() 23 24 obj, err := f.Get("tg") 25 if err != nil { 26 t.Fatal(err) 27 } 28 g, ok := obj.(rhist.Graph) 29 if !ok { 30 t.Fatalf("'tg' not a rhist.Graph: %T", obj) 31 } 32 33 if n, want := g.Len(), int(4); n != want { 34 t.Errorf("npts=%d. want=%d\n", n, want) 35 } 36 37 for i, v := range []float64{1, 2, 3, 4} { 38 x, y := g.XY(i) 39 if x != v { 40 t.Errorf("x[%d]=%v. want=%v", i, x, v) 41 } 42 if y != 2*v { 43 t.Errorf("y[%d]=%v. want=%v", i, y, 2*v) 44 } 45 } 46 } 47 48 func TestGraphErrors(t *testing.T) { 49 f, err := groot.Open("../testdata/graphs.root") 50 if err != nil { 51 t.Fatal(err) 52 } 53 defer f.Close() 54 55 obj, err := f.Get("tge") 56 if err != nil { 57 t.Fatal(err) 58 } 59 g, ok := obj.(rhist.GraphErrors) 60 if !ok { 61 t.Fatalf("'tge' not a rhist.GraphErrors: %T", obj) 62 } 63 64 if n, want := g.Len(), int(4); n != want { 65 t.Errorf("npts=%d. want=%d\n", n, want) 66 } 67 68 for i, v := range []float64{1, 2, 3, 4} { 69 x, y := g.XY(i) 70 if x != v { 71 t.Errorf("x[%d]=%v. want=%v", i, x, v) 72 } 73 if y != 2*v { 74 t.Errorf("y[%d]=%v. want=%v", i, y, 2*v) 75 } 76 xlo, xhi := g.XError(i) 77 if want := 0.1 * v; want != xlo { 78 t.Errorf("xerr[%d].low=%v want=%v", i, xlo, want) 79 } 80 if want := 0.1 * v; want != xhi { 81 t.Errorf("xerr[%d].high=%v want=%v", i, xhi, want) 82 } 83 ylo, yhi := g.YError(i) 84 if want := 0.2 * v; want != ylo { 85 t.Errorf("yerr[%d].low=%v want=%v", i, ylo, want) 86 } 87 if want := 0.2 * v; want != yhi { 88 t.Errorf("yerr[%d].high=%v want=%v", i, yhi, want) 89 } 90 } 91 } 92 93 func TestGraphAsymmErrors(t *testing.T) { 94 f, err := groot.Open("../testdata/graphs.root") 95 if err != nil { 96 t.Fatal(err) 97 } 98 defer f.Close() 99 100 obj, err := f.Get("tgae") 101 if err != nil { 102 t.Fatal(err) 103 } 104 g, ok := obj.(rhist.GraphErrors) 105 if !ok { 106 t.Fatalf("'tgae' not a rhist.GraphErrors: %T", obj) 107 } 108 109 if n, want := g.Len(), int(4); n != want { 110 t.Errorf("npts=%d. want=%d\n", n, want) 111 } 112 113 for i, v := range []float64{1, 2, 3, 4} { 114 x, y := g.XY(i) 115 if x != v { 116 t.Errorf("x[%d]=%v. want=%v", i, x, v) 117 } 118 if y != 2*v { 119 t.Errorf("y[%d]=%v. want=%v", i, y, 2*v) 120 } 121 xlo, xhi := g.XError(i) 122 if want := 0.1 * v; want != xlo { 123 t.Errorf("xerr[%d].low=%v want=%v", i, xlo, want) 124 } 125 if want := 0.2 * v; want != xhi { 126 t.Errorf("xerr[%d].high=%v want=%v", i, xhi, want) 127 } 128 ylo, yhi := g.YError(i) 129 if want := 0.3 * v; want != ylo { 130 t.Errorf("yerr[%d].low=%v want=%v", i, ylo, want) 131 } 132 if want := 0.4 * v; want != yhi { 133 t.Errorf("yerr[%d].high=%v want=%v", i, yhi, want) 134 } 135 } 136 } 137 138 func TestGraphMultiErrors(t *testing.T) { 139 f, err := groot.Open("../testdata/tgme.root") 140 if err != nil { 141 t.Fatal(err) 142 } 143 defer f.Close() 144 145 obj, err := f.Get("gme") 146 if err != nil { 147 t.Fatal(err) 148 } 149 g, ok := obj.(rhist.GraphErrors) 150 if !ok { 151 t.Fatalf("'gme' not a rhist.GraphErrors: %T", obj) 152 } 153 154 if n, want := g.Len(), int(5); n != want { 155 t.Errorf("npts=%d. want=%d\n", n, want) 156 } 157 158 var ( 159 xs = []float64{0, 1, 2, 3, 4} 160 ys = []float64{0, 2, 4, 1, 3} 161 xlos = []float64{0.3, 0.3, 0.3, 0.3, 0.3} 162 xhis = []float64{0.3, 0.3, 0.3, 0.3, 0.3} 163 ylos = []float64{1, 0.5, 1, 0.5, 1} 164 yhis = []float64{0.5, 1, 0.5, 1, 2} 165 ) 166 for i := range g.Len() { 167 x, y := g.XY(i) 168 if x != xs[i] { 169 t.Errorf("x[%d]=%v. want=%v", i, x, xs[i]) 170 } 171 if y != ys[i] { 172 t.Errorf("y[%d]=%v. want=%v", i, y, ys[i]) 173 } 174 xlo, xhi := g.XError(i) 175 if want := xlos[i]; want != xlo { 176 t.Errorf("xerr[%d].low=%v want=%v", i, xlo, want) 177 } 178 if want := xhis[i]; want != xhi { 179 t.Errorf("xerr[%d].high=%v want=%v", i, xhi, want) 180 } 181 ylo, yhi := g.YError(i) 182 if want := ylos[i]; want != ylo { 183 t.Errorf("yerr[%d].low=%v want=%v", i, ylo, want) 184 } 185 if want := yhis[i]; want != yhi { 186 t.Errorf("yerr[%d].high=%v want=%v", i, yhi, want) 187 } 188 } 189 } 190 191 func TestInvalidGraphMerger(t *testing.T) { 192 var ( 193 gr = hbook.NewS2D([]hbook.Point2D{ 194 {X: 0, Y: 0, ErrX: hbook.Range{Min: 1, Max: 1}, ErrY: hbook.Range{Min: 2, Max: 2}}, 195 {X: 1, Y: 1, ErrX: hbook.Range{Min: 1, Max: 1}, ErrY: hbook.Range{Min: 2, Max: 2}}, 196 }...) 197 src = rbase.NewObjString("foo") 198 ) 199 for _, tc := range []struct { 200 name string 201 obj root.Merger 202 want string 203 }{ 204 { 205 name: "graph", 206 obj: rhist.NewGraphFrom(gr).(root.Merger), 207 want: "rhist: can not merge *rbase.ObjString into *rhist.tgraph", 208 }, 209 { 210 name: "graph-errs", 211 obj: rhist.NewGraphErrorsFrom(gr).(root.Merger), 212 want: "rhist: can not merge *rbase.ObjString into *rhist.tgrapherrs", 213 }, 214 { 215 name: "graph-asymmerr", 216 obj: rhist.NewGraphAsymmErrorsFrom(gr).(root.Merger), 217 want: "rhist: can not merge *rbase.ObjString into *rhist.tgraphasymmerrs", 218 }, 219 } { 220 t.Run(tc.name, func(t *testing.T) { 221 err := tc.obj.ROOTMerge(src) 222 if err == nil { 223 t.Fatalf("expected an error") 224 } 225 226 if got, want := err.Error(), tc.want; got != want { 227 t.Fatalf("invalid ROOTMerge error. got=%q, want=%q", got, want) 228 } 229 }) 230 } 231 }