go-hep.org/x/hep@v0.38.1/rio/README.md (about)

     1  rio
     2  ===
     3  
     4  [![GoDoc](https://godoc.org/go-hep.org/x/hep/rio?status.svg)](https://godoc.org/go-hep.org/x/hep/rio)
     5  
     6  `rio` is a Record-oriented I/O library, modeled after SIO (Serial I/O).
     7  
     8  ## Installation
     9  
    10  ```sh
    11  $ go get go-hep.org/x/hep/rio
    12  ```
    13  
    14  ## Documentation
    15  
    16  The documentation is browsable at godoc.org:
    17   https://godoc.org/go-hep.org/x/hep/rio
    18  
    19  ## Example
    20  
    21  ```go
    22  package main
    23  
    24  import (
    25  	"fmt"
    26  	"io"
    27  
    28  	"go-hep.org/x/hep/rio"
    29  )
    30  
    31  type LCRunHeader struct {
    32  	RunNbr   int32
    33  	Detector string
    34  	Descr    string
    35  	SubDets  []string
    36  }
    37  
    38  func main() {
    39  	fname := "c_sim.slcio"
    40  	fmt.Printf(">>> opening [%s]\n", fname)
    41  
    42  	f, err := rio.Open(fname)
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  	defer f.Close()
    47  
    48  	var runhdr LCRunHeader
    49  	runhdr.RunNbr = 42
    50  	rec := f.Record("LCRunHeader")
    51  	rec.SetUnpack(true)
    52  	rec.Connect("RunHeader", &runhdr)
    53  
    54  	fmt.Printf("::: [%s]\n", f.Name())
    55  	fmt.Printf("::: [%s]\n", f.FileName())
    56  	for {
    57  		fmt.Printf("-----------------------------------------------\n")
    58  		var rec *rio.Record
    59  
    60  		rec, err = f.ReadRecord()
    61  		fmt.Printf("***\n")
    62  		if err != nil {
    63  			if err == io.EOF {
    64  				break
    65  			}
    66  			panic(err)
    67  		}
    68  		if rec == nil {
    69  			fmt.Printf("** no record!\n")
    70  			break
    71  		}
    72  
    73  		fmt.Printf(">> record: %s\n", rec.Name())
    74  		if rec.Name() == "LCRunHeader" {
    75  			fmt.Printf("runnbr: %d\n", runhdr.RunNbr)
    76  			fmt.Printf("det:    %q\n", runhdr.Detector)
    77  			dets := "["
    78  			for i, det := range runhdr.SubDets {
    79  				dets += fmt.Sprintf("%q", det)
    80  				if i+1 != len(runhdr.SubDets) {
    81  					dets += ", "
    82  				}
    83  			}
    84  			dets += "]"
    85  			fmt.Printf("dets:   %s\n", dets)
    86  		}
    87  	}
    88  }
    89  ```
    90  
    91  ## TODO
    92  
    93  - implement read/write pointers
    94  - implement buffered i/o
    95  - handle big files (ie: file offsets as `int64`)
    96  
    97  ## Bibliography
    98  
    99  - `SIO`: http://www-sldnt.slac.stanford.edu/nld/new/Docs/FileFormats/sio.pdf