Skip to content

Commit 5389a1f

Browse files
committed
groot/riofs: drop mmap-ing ROOT files by default, add file+mmap:// convenience protocol
Fixes #1019. Signed-off-by: Sebastien Binet <binet@cern.ch>
1 parent 4130003 commit 5389a1f

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

groot/example_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,54 @@ func ExampleOpen_graph() {
226226
// (x,y)[2] = (+3.000000e+00, +6.000000e+00)
227227
// (x,y)[3] = (+4.000000e+00, +8.000000e+00)
228228
}
229+
230+
// ExampleOpen_file shows how users can open a local ROOT file with the 'file://' protocol.
231+
func ExampleOpen_file() {
232+
f, err := groot.Open("file://./testdata/simple.root")
233+
if err != nil {
234+
log.Fatal(err)
235+
}
236+
defer f.Close()
237+
238+
for _, key := range f.Keys() {
239+
fmt.Printf("key: %q cycle=%d title=%q\n", key.Name(), key.Cycle(), key.Title())
240+
}
241+
242+
obj, err := f.Get("tree")
243+
if err != nil {
244+
log.Fatal(err)
245+
}
246+
247+
tree := obj.(rtree.Tree)
248+
fmt.Printf("tree: %q, entries=%d\n", tree.Name(), tree.Entries())
249+
250+
// Output:
251+
// key: "tree" cycle=1 title="fake data"
252+
// tree: "tree", entries=4
253+
}
254+
255+
// ExampleOpen_mmap shows how users can open and mmap a local ROOT file.
256+
// mmap-ing a file may be useful for performances reasons.
257+
func ExampleOpen_mmap() {
258+
f, err := groot.Open("file+mmap://./testdata/simple.root")
259+
if err != nil {
260+
log.Fatal(err)
261+
}
262+
defer f.Close()
263+
264+
for _, key := range f.Keys() {
265+
fmt.Printf("key: %q cycle=%d title=%q\n", key.Name(), key.Cycle(), key.Title())
266+
}
267+
268+
obj, err := f.Get("tree")
269+
if err != nil {
270+
log.Fatal(err)
271+
}
272+
273+
tree := obj.(rtree.Tree)
274+
fmt.Printf("tree: %q, entries=%d\n", tree.Name(), tree.Entries())
275+
276+
// Output:
277+
// key: "tree" cycle=1 title="fake data"
278+
// tree: "tree", entries=4
279+
}

groot/riofs/fileplugin.go

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package riofs
77
import (
88
"fmt"
99
"net/url"
10+
"os"
1011
"sort"
1112
"strings"
1213
"sync"
@@ -70,9 +71,15 @@ func openFile(path string) (Reader, error) {
7071

7172
func openLocalFile(path string) (Reader, error) {
7273
path = strings.TrimPrefix(path, "file://")
74+
return os.Open(path)
75+
}
76+
77+
func mmapLocalFile(path string) (Reader, error) {
78+
path = strings.TrimPrefix(path, "file+mmap://")
7379
return mmap.Open(path)
7480
}
7581

7682
func init() {
7783
Register("file", openLocalFile)
84+
Register("file+mmap", mmapLocalFile)
7885
}

0 commit comments

Comments
 (0)