-
Notifications
You must be signed in to change notification settings - Fork 35
/
hist_test.go
125 lines (103 loc) · 2.76 KB
/
hist_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright ©2018 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rhist_test
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/internal/rtests"
"go-hep.org/x/hep/groot/rhist"
"go-hep.org/x/hep/groot/riofs"
_ "go-hep.org/x/hep/groot/riofs/plugin/http"
)
func TestRWHist(t *testing.T) {
dir, err := os.MkdirTemp("", "groot-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
for i, tc := range rhist.HistoTestCases {
fname := filepath.Join(dir, fmt.Sprintf("histos-%d.root", i))
t.Run(tc.Name, func(t *testing.T) {
const kname = "my-key"
w, err := groot.Create(fname)
if err != nil {
t.Fatal(err)
}
err = w.Put(kname, tc.Want)
if err != nil {
t.Fatal(err)
}
if got, want := len(w.Keys()), 1; got != want {
t.Fatalf("invalid number of keys. got=%d, want=%d", got, want)
}
err = w.Close()
if err != nil {
t.Fatalf("error closing file: %v", err)
}
r, err := riofs.Open(fname)
if err != nil {
t.Fatal(err)
}
defer r.Close()
si := r.StreamerInfos()
if len(si) == 0 {
t.Fatalf("empty list of streamers")
}
if got, want := len(r.Keys()), 1; got != want {
t.Fatalf("invalid number of keys. got=%d, want=%d", got, want)
}
rgot, err := r.Get(kname)
if err != nil {
t.Fatal(err)
}
if got, want := rgot.(rtests.ROOTer), tc.Want; !reflect.DeepEqual(got, want) {
t.Fatalf("error reading back objstring.\ngot = %#v\nwant= %#v", got, want)
}
err = r.Close()
if err != nil {
t.Fatalf("error closing file: %v", err)
}
if !rtests.HasROOT {
t.Logf("skip test with ROOT/C++")
return
}
const rootls = `#include <iostream>
#include "TFile.h"
#include "TNamed.h"
void rootls(const char *fname, const char *kname) {
auto f = TFile::Open(fname);
auto o = f->Get<TNamed>(kname);
if (o == NULL) {
std:cerr << "could not retrieve [" << kname << "]" << std::endl;
o->ClassName();
}
std::cout << "retrieved object: [" << o->GetName() << "]" << std::endl;
}
`
out, err := rtests.RunCxxROOT("rootls", []byte(rootls), fname, kname)
if err != nil {
t.Fatalf("ROOT/C++ could not open file %q:\n%s", fname, string(out))
}
})
}
}
func TestROOT4Hist(t *testing.T) {
f, err := groot.Open("https://github.com/scikit-hep/uproot/raw/master/tests/samples/from-geant4.root")
if err != nil {
t.Fatalf("could not open uproot geant4 test file: %+v", err)
}
defer f.Close()
obj, err := f.Get("edep_inner")
if err != nil {
t.Fatalf("%+v", err)
}
h := obj.(*rhist.H1D)
if got, want := h.Name(), "edep_inner"; got != want {
t.Fatalf("invalid H1D name: got=%q, want=%q", got, want)
}
}