func (w *CalibBase) FileTree(file, tree string) (croot.File, croot.Tree) { var err error var f croot.File var t croot.Tree key := w.workdir + file if c, ok := w.cache[key]; ok { f = c.file t = c.tree } else { fname := path.Join(w.workdir, file) f, err = croot.OpenFile(fname, "read", "TUCS ROOT file", 1, 0) if f != nil && err == nil { t = f.GetTree(tree) if t == nil { fmt.Printf("**error** tucs.FileTree failed to grab file=%s tree=%s\n", file, tree) } else { w.cache[key] = centry{ file: f, tree: t, } } } } return f, t }
// InitHistFile grabs the ROOT file 'fname' and makes it the current gDirectory func (b *Base) InitHistFile(fname string) error { var err error const compress = 1 const netopt = 0 hfile := croot.GRoot.GetFile(fname) if hfile == nil { hfile, err = croot.OpenFile(fname, "recreate", "TUCS histogram", compress, netopt) } if err != nil { return err } if hfile == nil { return fmt.Errorf("tucs.Base: could not open file [%s]", fname) } b.HistFile = hfile if !b.HistFile.Cd("") { return fmt.Errorf("tucs.Base: could not make [%s] the current directory", fname) } return err }
func main() { flag.Parse() if *fname == "" { if len(os.Args) > 1 { *fname = os.Args[1] } } if *oname == "" { if len(os.Args) > 2 { *oname = os.Args[2] } } if *oname == "" || *fname == "" { fmt.Fprintf( os.Stderr, "**error** you need to give an input file name and an output file name\n", ) flag.Usage() os.Exit(1) } var f io.Reader ff, err := os.Open(*fname) if err != nil { fmt.Fprintf( os.Stderr, "**error** %v\n", err, ) os.Exit(1) } defer ff.Close() f = ff if strings.HasSuffix(*fname, ".gz") { f, err = gzip.NewReader(ff) if err != nil { fmt.Fprintf( os.Stderr, "**error** %v\n", err, ) os.Exit(1) } } dec, err := arff.NewDecoder(f) if err != nil { fmt.Fprintf( os.Stderr, "**error** %v\n", err, ) os.Exit(1) } o, err := croot.OpenFile(*oname, "recreate", "ARFF event file", 1, 0) if err != nil { fmt.Fprintf( os.Stderr, "**error** %v\n", err, ) os.Exit(1) } defer o.Close("") fmt.Printf(":: arff file - relation: [%v]\n", dec.Header.Relation) tree := croot.NewTree(dec.Header.Relation, dec.Header.Relation, 32) if tree == nil { err = fmt.Errorf( "arff2root: could not create output tree [%s] in file [%s]", dec.Header.Relation, *oname, ) fmt.Fprintf( os.Stderr, "**error** %v\n", err, ) os.Exit(1) } type RootVar struct { Value interface{} } idata := make(map[string]interface{}) //odata := make(map[string]*RootVar) odata := make([]interface{}, len(dec.Header.Attrs)) const bufsize = 32000 // declare branches for i, attr := range dec.Header.Attrs { //fmt.Printf(">>> [%d]: %v\n", i, attr) switch attr.Type { case arff.Integer: vv := int64(0) br_name := attr.Name br_type := br_name + "/L" odata[i] = &vv _, err = tree.Branch2(br_name, odata[i], br_type, bufsize) case arff.Real, arff.Numeric: vv := float64(-999.0) br_name := attr.Name br_type := br_name + "/D" odata[i] = &vv _, err = tree.Branch2(br_name, odata[i], br_type, bufsize) case arff.Nominal: vv := "" br_name := attr.Name br_type := br_name + "/C" odata[i] = &vv _, err = tree.Branch2(br_name, odata[i], br_type, bufsize) default: fmt.Fprintf( os.Stderr, "**error** invalid type for attribute: %v\n", attr, ) os.Exit(1) } if err != nil { fmt.Fprintf( os.Stderr, "**error** setting up branch for attribute [%s]: %v\n", attr.Name, err, ) os.Exit(1) } } for { err = dec.Decode(idata) if err == io.EOF { break } if err != nil { fmt.Fprintf( os.Stderr, "**error** %v\n", err, ) os.Exit(1) } for i, attr := range dec.Header.Attrs { k := attr.Name v := idata[k] //fmt.Printf(">>> [%v]: %v\n", k, v) if v == nil { err = fmt.Errorf( "nil value for attribute [%v]", k, ) fmt.Fprintf( os.Stderr, "**error** %v\n", err, ) os.Exit(1) } switch attr.Type { case arff.Integer: vv := odata[i].(*int64) *vv = v.(int64) //fmt.Printf(">> [%v]: %v %v %T\n", k, v, odata[i], odata[i]) case arff.Numeric, arff.Real: vv := odata[i].(*float64) *vv = v.(float64) //fmt.Printf(">> [%v]: %v %v %T\n", k, v, odata[i], odata[i]) case arff.Nominal: vv := odata[i].(*string) *vv = v.(string) } } _, err = tree.Fill() if err != nil { fmt.Fprintf( os.Stderr, "**error** filling tree: %v\n", err, ) os.Exit(1) } } tree.Write("", 0, 0) }
func inspect_root_file(filename, treename string) []pb_field { f := croot.OpenFile(filename, "read", "ROOT file", 1, 0) if f == nil { fmt.Printf("**error** could not open ROOT file [%s]\n", filename) os.Exit(1) } defer f.Close("") tree := f.GetTree(treename) if tree == nil { fmt.Printf("**error** could not retrieve Tree [%s] from file [%s]\n", treename, filename) } //tree.Print("*") branches := tree.GetListOfBranches() fmt.Printf(" #-branches: %v\n", branches.GetSize()) imax := branches.GetSize() type stringset map[string]struct{} pb_fields := []pb_field{} for i := int64(0); i < imax; i++ { obj := branches.At(i) br := obj.(croot.Branch) typename := br.GetClassName() if typename == "" { leaf := tree.GetLeaf(br.GetName()) typename = leaf.GetTypeName() } if *verbose { fmt.Printf(" [%d] -> [%v] (%v) (type:%v)\n", i, obj.GetName(), br.ClassName(), typename) } name := br.GetName() pb_type, isrepeated := get_pb_type(typename) accept := true if *brsel != "" { accept = false for _, pattern := range strings.Split(*brsel, ",") { switch pattern[0] { case '-': matched, err := filepath.Match(pattern[1:], name) if err == nil && matched { accept = false break } case '+': matched, err := filepath.Match(pattern[1:], name) if err == nil && matched { accept = true break } default: matched, err := filepath.Match(pattern, name) if err == nil && matched { accept = true break } } } } if accept { pb_fields = append(pb_fields, pb_field{ Name: pb_gen.CamelCase(name), Type: pb_type, Id: <-gen_id, Branch: name, repeated: isrepeated, }) } } return pb_fields }
func main() { flag.Parse() if *fname == "" || *tname == "" { flag.Usage() os.Exit(1) } if *oname == "" { *oname = path.Base(*fname) *oname = strings.Replace(*oname, ".root", ".pbuf", -1) } fmt.Printf(":: cnv ROOT file into pbuf...\n") fmt.Printf(":: ROOT file: [%s]\n", *fname) fmt.Printf(":: ROOT tree: [%s]\n", *tname) fmt.Printf(":: PBuf file: [%s]\n", *oname) fmt.Printf(":: evtmax: [%v]\n", *evtmax) f := croot.OpenFile(*fname, "read", "ROOT file", 1, 0) if f == nil { fmt.Printf("**error** could not open ROOT file [%s]\n", *fname) os.Exit(1) } defer f.Close("") tree := f.GetTree(*tname) if tree == nil { fmt.Printf("**error** could not retrieve Tree [%s] from file [%s]\n", *tname, *fname) } if *evtmax < 0 { *evtmax = int64(tree.GetEntries()) } out, err := os.Create(*oname) if err != nil { fmt.Printf("**error** could not create output file [%s]\n%v\n", *oname, err) os.Exit(1) } defer func(){ err := out.Sync() if err != nil { fmt.Printf("**error** problem commiting data to disk: %v\n", err) } err = out.Close() if err != nil { fmt.Printf("**error** problem closing file: %v\n", err) } }() // proto-buf data-hdr { hdr := msgpkg.{{.DataHeader}}{} hdr.Nevts = proto.Uint64(uint64(*evtmax)) data, err := proto.Marshal(&hdr) if err != nil { fmt.Printf("**error** event-hdr: problem marshalling pbuf: %v\n", err) os.Exit(1) } _, err = out.Write(data) if err != nil { fmt.Printf("**error** event-hdr: problem writing header: %v\n", err) os.Exit(1) } }