-
Notifications
You must be signed in to change notification settings - Fork 35
/
dump.go
168 lines (151 loc) · 4.11 KB
/
dump.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2019 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 rcmd
import (
"errors"
"fmt"
"io"
"reflect"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/rdict"
"go-hep.org/x/hep/groot/rhist"
"go-hep.org/x/hep/groot/riofs"
"go-hep.org/x/hep/groot/root"
"go-hep.org/x/hep/groot/rtree"
"go-hep.org/x/hep/hbook/rootcnv"
"go-hep.org/x/hep/hbook/yodacnv"
)
// Dump dumps the content of the fname ROOT file to the provided io.Writer.
// If deep is true, Dump will recursively inspect directories and trees.
// Dump only display the content of ROOT objects satisfying the provided filter function.
//
// If filter is nil, Dump will consider all ROOT objects.
func Dump(w io.Writer, fname string, deep bool, filter func(name string) bool) error {
f, err := groot.Open(fname)
if err != nil {
return fmt.Errorf("could not open file with read-access: %w", err)
}
defer f.Close()
if filter == nil {
filter = func(string) bool { return true }
}
cmd := dumpCmd{
w: w,
deep: deep,
match: filter,
}
return cmd.dumpDir(f)
}
type dumpCmd struct {
w io.Writer
deep bool
match func(name string) bool
}
func (cmd *dumpCmd) dumpDir(dir riofs.Directory) error {
for i, key := range dir.Keys() {
fmt.Fprintf(cmd.w, "key[%03d]: %s;%d %q (%s)", i, key.Name(), key.Cycle(), key.Title(), key.ClassName())
if !(cmd.deep && cmd.match(key.Name())) {
fmt.Fprint(cmd.w, "\n")
continue
}
obj, err := key.Object()
if err != nil {
return fmt.Errorf("could not decode object %q from dir %q: %w", key.Name(), dir.(root.Named).Name(), err)
}
err = cmd.dumpObj(obj)
if errors.Is(err, errIgnoreKey) {
continue
}
if err != nil {
return fmt.Errorf("error dumping key %q: %w", key.Name(), err)
}
}
return nil
}
var errIgnoreKey = fmt.Errorf("rcmd: ignore key")
func (cmd *dumpCmd) dumpObj(obj root.Object) error {
var err error
switch obj := obj.(type) {
case rtree.Tree:
fmt.Fprintf(cmd.w, "\n")
err = cmd.dumpTree(obj)
case riofs.Directory:
fmt.Fprintf(cmd.w, "\n")
err = cmd.dumpDir(obj)
case rhist.H2:
fmt.Fprintf(cmd.w, "\n")
err = cmd.dumpH2(obj)
case rhist.H1: // keep after rhist.H2
fmt.Fprintf(cmd.w, "\n")
err = cmd.dumpH1(obj)
case rhist.Graph:
fmt.Fprintf(cmd.w, "\n")
err = cmd.dumpGraph(obj)
case root.List:
fmt.Fprintf(cmd.w, "\n")
err = cmd.dumpList(obj)
case *rdict.Object:
fmt.Fprintf(cmd.w, " => %v\n", obj)
case fmt.Stringer:
fmt.Fprintf(cmd.w, " => %q\n", obj.String())
default:
fmt.Fprintf(cmd.w, " => ignoring key of type %T\n", obj)
return errIgnoreKey
}
return err
}
func (cmd *dumpCmd) dumpList(lst root.List) error {
for i := 0; i < lst.Len(); i++ {
fmt.Fprintf(cmd.w, "lst[%s][%d]: ", lst.Name(), i)
err := cmd.dumpObj(lst.At(i))
if err != nil && !errors.Is(err, errIgnoreKey) {
return fmt.Errorf("could not dump list: %w", err)
}
}
return nil
}
func (cmd *dumpCmd) dumpTree(t rtree.Tree) error {
vars := rtree.NewReadVars(t)
sc, err := rtree.NewScannerVars(t, vars...)
if err != nil {
return fmt.Errorf("could not create scanner-vars: %w", err)
}
defer sc.Close()
for sc.Next() {
err = sc.Scan()
if err != nil {
return fmt.Errorf("error scanning entry %d: %w", sc.Entry(), err)
}
for _, v := range vars {
rv := reflect.Indirect(reflect.ValueOf(v.Value))
name := v.Name
if v.Leaf != "" && v.Leaf != v.Name {
name = v.Name + "." + v.Leaf
}
fmt.Fprintf(cmd.w, "[%03d][%s]: %v\n", sc.Entry(), name, rv.Interface())
}
}
return nil
}
func (cmd *dumpCmd) dumpH1(h1 rhist.H1) error {
h, err := rootcnv.H1D(h1)
if err != nil {
return fmt.Errorf("could not convert TH1x to hbook: %w", err)
}
return yodacnv.Write(cmd.w, h)
}
func (cmd *dumpCmd) dumpH2(h2 rhist.H2) error {
h, err := rootcnv.H2D(h2)
if err != nil {
return fmt.Errorf("could not convert TH2x to hbook: %w", err)
}
return yodacnv.Write(cmd.w, h)
}
func (cmd *dumpCmd) dumpGraph(gr rhist.Graph) error {
g, err := rootcnv.S2D(gr)
if err != nil {
return fmt.Errorf("could not convert TGraph to hbook: %w", err)
}
return yodacnv.Write(cmd.w, g)
}