go-hep.org/x/hep@v0.38.1/groot/riofs/walk_example_test.go (about) 1 // Copyright ©2018 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package riofs_test 6 7 import ( 8 "fmt" 9 "log" 10 stdpath "path" 11 "strings" 12 13 "go-hep.org/x/hep/groot/rhist" 14 "go-hep.org/x/hep/groot/riofs" 15 "go-hep.org/x/hep/groot/root" 16 ) 17 18 func ExampleWalk() { 19 f, err := riofs.Open("../testdata/dirs-6.14.00.root") 20 if err != nil { 21 log.Fatal(err) 22 } 23 defer f.Close() 24 25 fmt.Printf("visit all ROOT file tree:\n") 26 err = riofs.Walk(f, func(path string, obj root.Object, err error) error { 27 fmt.Printf("%s (%s)\n", path, obj.Class()) 28 return nil 29 }) 30 if err != nil { 31 log.Fatalf("could not walk through file: %v", err) 32 } 33 34 fmt.Printf("visit only dir1:\n") 35 err = riofs.Walk(f, func(path string, obj root.Object, err error) error { 36 if !(strings.HasPrefix(path, stdpath.Join(f.Name(), "dir1")) || path == f.Name()) { 37 return riofs.SkipDir 38 } 39 fmt.Printf("%s (%s)\n", path, obj.Class()) 40 return nil 41 }) 42 if err != nil { 43 log.Fatalf("could not walk through file: %v", err) 44 } 45 46 // Output: 47 // visit all ROOT file tree: 48 // dirs-6.14.00.root (TFile) 49 // dirs-6.14.00.root/dir1 (TDirectoryFile) 50 // dirs-6.14.00.root/dir1/dir11 (TDirectoryFile) 51 // dirs-6.14.00.root/dir1/dir11/h1 (TH1F) 52 // dirs-6.14.00.root/dir2 (TDirectoryFile) 53 // dirs-6.14.00.root/dir3 (TDirectoryFile) 54 // visit only dir1: 55 // dirs-6.14.00.root (TFile) 56 // dirs-6.14.00.root/dir1 (TDirectoryFile) 57 // dirs-6.14.00.root/dir1/dir11 (TDirectoryFile) 58 // dirs-6.14.00.root/dir1/dir11/h1 (TH1F) 59 } 60 61 func ExampleGet() { 62 f, err := riofs.Open("../testdata/dirs-6.14.00.root") 63 if err != nil { 64 log.Fatal(err) 65 } 66 defer f.Close() 67 68 h1, err := riofs.Get[rhist.H1](f, "dir1/dir11/h1") 69 if err != nil { 70 log.Fatal(err) 71 } 72 fmt.Printf("histo: %s (%s)", h1.Name(), h1.Class()) 73 74 // Output: 75 // histo: h1 (TH1F) 76 }