go-hep.org/x/hep@v0.38.1/cmd/yoda2rio/main.go (about) 1 // Copyright ©2017 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 // yoda2rio converts YODA files containing hbook-like values (H1D, H2D, P1D, ...) 6 // into rio files. 7 // 8 // Example: 9 // 10 // $> yoda2rio rivet.yoda >| rivet.rio 11 // $> yoda2rio rivet.yoda.gz >| rivet.rio 12 package main // import "go-hep.org/x/hep/cmd/yoda2rio" 13 14 import ( 15 "compress/gzip" 16 "flag" 17 "fmt" 18 "io" 19 "log" 20 "os" 21 "path/filepath" 22 23 "go-hep.org/x/hep/hbook/yodacnv" 24 "go-hep.org/x/hep/rio" 25 ) 26 27 func main() { 28 log.SetFlags(0) 29 log.SetPrefix("yoda2rio: ") 30 log.SetOutput(os.Stderr) 31 32 flag.Usage = func() { 33 fmt.Fprintf( 34 os.Stderr, 35 `Usage: yoda2rio [options] <file1.yoda> [<file2.yoda> [...]] 36 37 ex: 38 $> yoda2rio rivet.yoda >| rivet.rio 39 $> yoda2rio rivet.yoda.gz >| rivet.rio 40 `) 41 } 42 43 flag.Parse() 44 45 if flag.NArg() < 1 { 46 log.Printf("missing input file name") 47 flag.Usage() 48 flag.PrintDefaults() 49 os.Exit(1) 50 } 51 52 o, err := rio.NewWriter(os.Stdout) 53 if err != nil { 54 log.Fatal(err) 55 } 56 defer o.Close() 57 58 for _, fname := range flag.Args() { 59 convert(o, fname) 60 } 61 } 62 63 func convert(w *rio.Writer, fname string) { 64 var r io.ReadCloser 65 r, err := os.Open(fname) 66 if err != nil { 67 log.Fatalf("error opening file [%s]: %v\n", fname, err) 68 } 69 defer r.Close() 70 71 if filepath.Ext(fname) == ".gz" { 72 rz, err := gzip.NewReader(r) 73 if err != nil { 74 log.Fatal(err) 75 } 76 defer rz.Close() 77 r = rz 78 } 79 80 vs, err := yodacnv.Read(r) 81 if err != nil { 82 log.Fatalf("error decoding YODA file [%s]: %v\n", fname, err) 83 } 84 85 for _, v := range vs { 86 err = w.WriteValue(v.Name(), v) 87 if err != nil { 88 log.Fatalf("error writing %q from YODA file [%s]: %v\n", v.Name(), fname, err) 89 } 90 } 91 }