/
main.go
232 lines (205 loc) · 5.43 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright ©2017 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.
// root2csv converts the content of a ROOT TTree to a CSV file.
//
// Usage of root2csv:
// -f string
// path to input ROOT file name
// -o string
// path to output CSV file name (default "output.csv")
// -t string
// name of the tree to convert (default "tree")
//
// By default, root2csv will write out a CSV file with ';' as a column delimiter.
// root2csv ignores the branches of the TTree that are not supported by CSV:
// - slices/arrays
// - C++ objects
//
// Example:
// $> root2csv -o out.csv -t tree -f testdata/small-flat-tree.root
// $> head out.csv
// ## Automatically generated from "testdata/small-flat-tree.root"
// Int32;Int64;UInt32;UInt64;Float32;Float64;Str;N
// 0;0;0;0;0;0;evt-000;0
// 1;1;1;1;1;1;evt-001;1
// 2;2;2;2;2;2;evt-002;2
// 3;3;3;3;3;3;evt-003;3
// 4;4;4;4;4;4;evt-004;4
// 5;5;5;5;5;5;evt-005;5
// 6;6;6;6;6;6;evt-006;6
// 7;7;7;7;7;7;evt-007;7
package main
import (
"flag"
"fmt"
"log"
"reflect"
"strings"
"go-hep.org/x/hep/csvutil"
"go-hep.org/x/hep/groot"
_ "go-hep.org/x/hep/groot/riofs/plugin/http"
_ "go-hep.org/x/hep/groot/riofs/plugin/xrootd"
"go-hep.org/x/hep/groot/rtree"
)
func main() {
log.SetPrefix("root2csv: ")
log.SetFlags(0)
fname := flag.String("f", "", "path to input ROOT file name")
oname := flag.String("o", "output.csv", "path to output CSV file name")
tname := flag.String("t", "tree", "name of the tree to convert")
flag.Parse()
if *fname == "" {
flag.Usage()
log.Fatalf("missing input ROOT filename argument")
}
err := process(*oname, *fname, *tname)
if err != nil {
log.Fatal(err)
}
}
func process(oname, fname, tname string) error {
f, err := groot.Open(fname)
if err != nil {
return fmt.Errorf("could not open ROOT file: %w", err)
}
defer f.Close()
obj, err := f.Get(tname)
if err != nil {
return fmt.Errorf("could not get ROOT object: %w", err)
}
tree, ok := obj.(rtree.Tree)
if !ok {
return fmt.Errorf("object %q in file %q is not a rtree.Tree", tname, fname)
}
var nt = ntuple{n: tree.Entries()}
log.Printf("scanning leaves...")
for _, leaf := range tree.Leaves() {
kind := leaf.Type().Kind()
switch kind {
case reflect.Array, reflect.Map, reflect.Slice, reflect.Struct:
log.Printf(">>> %q %v not supported (%v)", leaf.Name(), leaf.Class(), kind)
continue
case reflect.String:
// ok
case reflect.Bool,
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
if leaf.LeafCount() != nil {
log.Printf(">>> %q %v not supported (slice)", leaf.Name(), leaf.Class())
continue
}
if leaf.Len() > 1 {
log.Printf(">>> %q %v not supported (array)", leaf.Name(), leaf.Class())
continue
}
default:
log.Printf(">>> %q %v not supported (%v) (unknown!)", leaf.Name(), leaf.Class(), kind)
continue
}
nt.add(leaf.Name(), leaf)
}
log.Printf("scanning leaves... [done]")
r, err := rtree.NewReader(tree, nt.args)
if err != nil {
return fmt.Errorf("could not create tree reader: %w", err)
}
defer r.Close()
nrows := 0
err = r.Read(func(ctx rtree.RCtx) error {
nt.fill()
nrows++
return nil
})
if err != nil {
return fmt.Errorf("could not read tree: %w", err)
}
tbl, err := csvutil.Create(oname)
if err != nil {
return fmt.Errorf("could not create output CSV file: %w", err)
}
defer tbl.Close()
tbl.Writer.Comma = ';'
names := make([]string, len(nt.cols))
for i, col := range nt.cols {
names[i] = col.name
}
err = tbl.WriteHeader(fmt.Sprintf(
"## Automatically generated from %q\n%s\n",
fname,
strings.Join(names, string(tbl.Writer.Comma)),
))
if err != nil {
return fmt.Errorf("could not write CSV header: %w", err)
}
row := make([]interface{}, len(nt.cols))
for irow := 0; irow < nrows; irow++ {
for i, col := range nt.cols {
row[i] = col.slice.Index(irow).Interface()
}
err = tbl.WriteRow(row...)
if err != nil {
return fmt.Errorf("could not write row %d to CSV file: %w", irow, err)
}
}
err = tbl.Close()
if err != nil {
return fmt.Errorf("could not close CSV output file: %w", err)
}
return nil
}
type ntuple struct {
n int64
cols []column
args []rtree.ReadVar
vars []interface{}
}
func (nt *ntuple) add(name string, leaf rtree.Leaf) {
n := len(nt.cols)
nt.cols = append(nt.cols, newColumn(name, leaf, nt.n))
col := &nt.cols[n]
nt.args = append(nt.args, rtree.ReadVar{
Name: name,
Leaf: leaf.Name(),
Value: col.data.Addr().Interface(),
})
nt.vars = append(nt.vars, col.data.Addr().Interface())
}
func (nt *ntuple) fill() {
for i := range nt.cols {
col := &nt.cols[i]
col.fill()
}
}
type column struct {
name string
i int64
leaf rtree.Leaf
etype reflect.Type
shape []int
data reflect.Value
slice reflect.Value
}
func newColumn(name string, leaf rtree.Leaf, n int64) column {
etype := leaf.Type()
shape := []int{int(n)}
if leaf.Len() > 1 && leaf.Kind() != reflect.String {
etype = reflect.ArrayOf(leaf.Len(), etype)
shape = append(shape, leaf.Len())
}
rtype := reflect.SliceOf(etype)
return column{
name: name,
i: 0,
leaf: leaf,
etype: etype,
shape: shape,
data: reflect.New(etype).Elem(),
slice: reflect.MakeSlice(rtype, int(n), int(n)),
}
}
func (col *column) fill() {
col.slice.Index(int(col.i)).Set(col.data)
col.i++
}