-
Notifications
You must be signed in to change notification settings - Fork 35
/
hmgr.go
161 lines (133 loc) · 3.19 KB
/
hmgr.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
// 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.
package main
import (
"fmt"
"log"
"strings"
"go-hep.org/x/hep/hbook"
"go-hep.org/x/hep/hplot"
)
type histMgr struct {
msg *log.Logger
hmap map[string]hbook.Histogram
}
func newHistMgr(msg *log.Logger) *histMgr {
return &histMgr{
msg: msg,
hmap: make(map[string]hbook.Histogram),
}
}
func (mgr *histMgr) find(fmgr *fileMgr, path string) (hbook.Histogram, error) {
var err error
const prefix = "/file/id/"
if !strings.HasPrefix(path, prefix) {
return nil, fmt.Errorf("invalid path [%s] (missing prefix [%s])", path, prefix)
}
var toks []string
for _, tok := range strings.Split(path[len(prefix):], "/") {
tok = strings.TrimSpace(tok)
if tok == "" {
continue
}
toks = append(toks, tok)
}
if len(toks) < 2 {
return nil, fmt.Errorf("invalid path [%s] (missing file-id and histo-name)", path)
}
fid := toks[0]
r, ok := fmgr.rfds[fid]
if !ok {
return nil, fmt.Errorf("unknown file-id [%s]", fid)
}
hname := strings.Join(toks[1:], "/")
switch r.typ(hname) {
case "*go-hep.org/x/hep/hbook.H1D":
var h1 hbook.H1D
err = r.read(hname, &h1)
if err != nil {
return nil, err
}
return &h1, nil
case "*go-hep.org/x/hep/hbook.H2D":
var h2 hbook.H2D
err = r.read(hname, &h2)
if err != nil {
return nil, err
}
return &h2, nil
default:
return nil, fmt.Errorf("%q not an histogram (%s)", path, r.typ(hname))
}
}
func (mgr *histMgr) open(fmgr *fileMgr, hid, path string) error {
h, err := mgr.find(fmgr, path)
if err != nil {
return err
}
mgr.hmap[hid] = h
return nil
}
func (mgr *histMgr) plot(fmgr *fileMgr, wmgr *winMgr, hid string) (*window, error) {
var (
h hbook.Histogram
err error
)
if strings.HasPrefix(hid, "/file/id/") {
// directly plot from file
h, err = mgr.find(fmgr, hid)
if err != nil {
return nil, err
}
} else {
var ok bool
h, ok = mgr.hmap[hid]
if !ok {
return nil, fmt.Errorf("unknown histogram [id=%s]", hid)
}
}
switch h := h.(type) {
case *hbook.H1D:
return mgr.plotH1D(wmgr, h)
case *hbook.H2D:
return mgr.plotH2D(wmgr, h)
}
return nil, fmt.Errorf("unknown histogram type %T [id=%s]", h, hid)
}
func (mgr *histMgr) plotH1D(wmgr *winMgr, h *hbook.H1D) (*window, error) {
fmt.Fprintf(
mgr.msg.Writer(),
"== h1d: name=%q\nentries=%d\nmean=%+8.3f\nRMS= %+8.3f\n",
h.Name(), h.Entries(), h.XMean(), h.XRMS(),
)
p := hplot.New()
p.Title.Text = h.Name()
p.X.Label.Text = "x"
p.Y.Label.Text = "y"
hh := hplot.NewH1D(h)
hh.Infos.Style = hplot.HInfoSummary
p.Add(hh)
p.Add(hplot.NewGrid())
win := wmgr.newPlot(p)
return win, nil
}
func (mgr *histMgr) plotH2D(wmgr *winMgr, h *hbook.H2D) (*window, error) {
fmt.Fprintf(
mgr.msg.Writer(),
"== h2d: name=%q\nentries=%d\nxmean=%+8.3f\nxRMS= %+8.3f\nymean=%+8.3f\nyRMS= %+8.3f\n",
h.Name(), h.Entries(),
h.XMean(), h.XRMS(),
h.YMean(), h.YRMS(),
)
p := hplot.New()
p.Title.Text = h.Name()
p.X.Label.Text = "x"
p.Y.Label.Text = "y"
hh := hplot.NewH2D(h, nil)
hh.Infos.Style = hplot.HInfoNone
p.Add(hh)
p.Add(hplot.NewGrid())
win := wmgr.newPlot(p)
return win, nil
}