-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
236 lines (203 loc) · 4.28 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
233
234
235
236
// 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-print prints ROOT files contents to PDF, PNG, ... files.
//
// Examples:
//
// $> root-print -f pdf ./testdata/histos.root
// $> root-print -f pdf ./testdata/histos.root:h1
// $> root-print -f pdf ./testdata/histos.root:h.*
// $> root-print -f pdf -o output ./testdata/histos.root:h1
//
// $> root-print -h
// Usage: root-print [options] file.root [file.root [...]]
//
// options:
// -f string
// output format for plots (pdf, png, svg, ...) (default "pdf")
// -o string
// output directory for plots
// -v enable verbose mode
//
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"go-hep.org/x/hep/hbook/rootcnv"
"go-hep.org/x/hep/hplot"
"go-hep.org/x/hep/rootio"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)
var (
odirFlag = flag.String("o", "", "output directory for plots")
fmtFlag = flag.String("f", "pdf", "output format for plots (pdf, png, svg, ...)")
verboseFlag = flag.Bool("v", false, "enable verbose mode")
colors = plotutil.SoftColors
)
func main() {
log.SetPrefix("root-print: ")
log.SetFlags(0)
flag.Usage = func() {
fmt.Fprintf(
os.Stderr,
`Usage: root-print [options] file.root [file.root [...]]
ex:
$> root-print -f pdf ./testdata/histos.root
$> root-print -f pdf ./testdata/histos.root:h1
$> root-print -f pdf ./testdata/histos.root:h.*
$> root-print -f pdf -o output ./testdata/histos.root:h1
options:
`,
)
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
log.Fatalf("need at least 1 input ROOT file")
}
_ = os.MkdirAll(*odirFlag, 0755)
for _, fname := range flag.Args() {
err := process(fname)
if err != nil {
log.Fatal(err)
}
}
}
func process(name string) error {
var (
fname = ""
hname = ""
)
toks := strings.Split(name, ":")
switch len(toks) {
case 2:
fname = toks[0]
hname = toks[1]
case 1:
fname = toks[0]
hname = ".*"
case 0:
fname = name
hname = ".*"
default:
return fmt.Errorf(
"invalid input file format. got %q. want: \"file.root:histo\"",
name,
)
}
f, err := rootio.Open(fname)
if err != nil {
return err
}
defer f.Close()
re, err := regexp.CompilePOSIX(hname)
if err != nil {
return err
}
var objs []rootio.Object
for _, k := range f.Keys() {
if !re.MatchString(k.Name()) {
continue
}
o, err := k.Object()
if err != nil {
return err
}
if !filter(o) {
continue
}
objs = append(objs, o)
}
for _, obj := range objs {
err := printObject(f, obj)
if err != nil {
return err
}
}
return err
}
func printObject(f *rootio.File, obj rootio.Object) error {
p := hplot.New()
name := obj.(rootio.Named).Name()
title := obj.(rootio.Named).Title()
if title == "" {
title = name
}
p.Title.Text = title
oname := filepath.Join(*odirFlag, name+"."+*fmtFlag)
if *verboseFlag {
log.Printf("printing %q to %s...", name, oname)
}
switch o := obj.(type) {
case rootio.H1:
h, err := rootcnv.H1D(o)
if err != nil {
return err
}
hh := hplot.NewH1D(h)
hh.Color = colors[2]
hh.LineStyle.Color = colors[2]
hh.LineStyle.Width = vg.Points(1.5)
hh.Infos.Style = hplot.HInfoSummary
p.Add(hh)
case rootio.H2:
h, err := rootcnv.H2D(o)
if err != nil {
return err
}
p.Add(hplot.NewH2D(h, nil))
case rootio.GraphErrors:
h, err := rootcnv.S2D(o)
if err != nil {
return err
}
if name := h.Name(); name != "" {
p.Title.Text = name
}
g := hplot.NewS2D(h, hplot.WithXErrBars, hplot.WithYErrBars)
g.Color = colors[0]
p.Add(g)
case rootio.Graph:
h, err := rootcnv.S2D(o)
if err != nil {
return err
}
if name := h.Name(); name != "" {
p.Title.Text = name
}
g := hplot.NewS2D(h)
g.Color = colors[0]
p.Add(g)
default:
return fmt.Errorf("unknown type %T for %q", o, name)
}
p.Add(hplot.NewGrid())
ext := strings.ToLower(filepath.Ext(oname))
if len(ext) > 0 {
ext = ext[1:]
}
err := p.Save(20*vg.Centimeter, -1, oname)
if err != nil {
return err
}
return err
}
func filter(obj rootio.Object) bool {
switch obj.(type) {
case rootio.H1:
return true
case rootio.H2:
return true
case rootio.Graph, rootio.GraphErrors:
return true
}
return false
}