-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
133 lines (116 loc) · 3.05 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
// 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.
// root-dump dumps the content of a ROOT file, including the content of
// the Trees (for all entries), if any.
//
// Example:
//
// $> root-dump ./testdata/small-flat-tree.root
// >>> file[./testdata/small-flat-tree.root]
// key[000]: tree;1 "my tree title" (TTree)
// [000][Int32]: 0
// [000][Int64]: 0
// [000][UInt32]: 0
// [000][UInt64]: 0
// [000][Float32]: 0
// [000][Float64]: 0
// [000][Str]: evt-000
// [000][ArrayInt32]: [0 0 0 0 0 0 0 0 0 0]
// [000][ArrayInt64]: [0 0 0 0 0 0 0 0 0 0]
// [000][ArrayInt32]: [0 0 0 0 0 0 0 0 0 0]
// [000][ArrayInt64]: [0 0 0 0 0 0 0 0 0 0]
// [000][ArrayFloat32]: [0 0 0 0 0 0 0 0 0 0]
// [000][ArrayFloat64]: [0 0 0 0 0 0 0 0 0 0]
// [000][N]: 0
// [000][SliceInt32]: []
// [000][SliceInt64]: []
// [...]
//
// $> root-dump -h
// Usage: root-dump [options] f0.root [f1.root [...]]
//
// ex:
// $> root-dump ./testdata/small-flat-tree.root
// $> root-dump -deep=0 ./testdata/small-flat-tree.root
//
// options:
// -cpu-profile string
// path to CPU profile output file
// -deep
// enable deep dumping of values (including Trees' entries) (default true)
// -name string
// regex of object names to dump
//
package main // import "go-hep.org/x/hep/groot/cmd/root-dump"
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"regexp"
"runtime/pprof"
"go-hep.org/x/hep/groot/rcmd"
_ "go-hep.org/x/hep/groot/riofs/plugin/http"
_ "go-hep.org/x/hep/groot/riofs/plugin/xrootd"
)
var (
deepFlag = flag.Bool("deep", true, "enable deep dumping of values (including Trees' entries)")
nameFlag = flag.String("name", "", "regex of object names to dump")
cpuFlag = flag.String("cpu-profile", "", "path to CPU profile output file")
)
func main() {
log.SetPrefix("root-dump: ")
log.SetFlags(0)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage: root-dump [options] f0.root [f1.root [...]]
ex:
$> root-dump ./testdata/small-flat-tree.root
$> root-dump -deep=0 ./testdata/small-flat-tree.root
options:
`,
)
flag.PrintDefaults()
}
flag.Parse()
if *nameFlag != "" {
reName = regexp.MustCompile(*nameFlag)
}
if flag.NArg() == 0 {
flag.Usage()
log.Fatalf("need at least one input ROOT file")
}
if *cpuFlag != "" {
f, err := os.Create(*cpuFlag)
if err != nil {
log.Fatalf("%+v", err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatalf("could not start CPU profiling: %+v", err)
}
defer pprof.StopCPUProfile()
}
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
for _, fname := range flag.Args() {
err := dump(out, fname, *deepFlag)
if err != nil {
out.Flush()
log.Fatalf("error dumping file %q: %+v", fname, err)
}
}
}
func dump(w io.Writer, fname string, deep bool) error {
fmt.Fprintf(w, ">>> file[%s]\n", fname)
return rcmd.Dump(w, fname, deep, match)
}
var reName *regexp.Regexp
func match(name string) bool {
if reName == nil {
return true
}
return reName.MatchString(name)
}