go-hep.org/x/hep@v0.38.1/sio/testdata/gen-runhdr.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  //go:build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"fmt"
    12  	"os"
    13  
    14  	"go-hep.org/x/hep/sio"
    15  )
    16  
    17  type RunHeader struct {
    18  	RunNbr   int32
    19  	Detector string
    20  	Descr    string
    21  	SubDets  []string
    22  	Ints     []int64
    23  	Floats   []float64
    24  }
    25  
    26  func main() {
    27  	fname := flag.String("fname", "runhdr.sio", "file to create")
    28  	compr := flag.Bool("compr", false, "enable records compression")
    29  	flag.Parse()
    30  
    31  	f, err := sio.Create(*fname)
    32  	if err != nil {
    33  		fmt.Printf("*** error: %v\n", err)
    34  		os.Exit(1)
    35  	}
    36  	defer func() {
    37  		err = f.Close()
    38  		if err != nil {
    39  			fmt.Printf("*** error closing file [%s]: %v\n", *fname, err)
    40  			os.Exit(1)
    41  		}
    42  	}()
    43  
    44  	var runhdr RunHeader
    45  	rec := f.Record("RioRunHeader")
    46  	if rec == nil {
    47  		fmt.Printf("*** error: could not fetch record [RioRunHeader]\n")
    48  		os.Exit(1)
    49  	}
    50  
    51  	rec.SetCompress(*compr)
    52  	err = rec.Connect("RunHeader", &runhdr)
    53  	if err != nil {
    54  		fmt.Printf("error connecting [RunHeader]: %v", err)
    55  		os.Exit(1)
    56  	}
    57  
    58  	for irec := 0; irec < 10; irec++ {
    59  		runhdr = RunHeader{
    60  			RunNbr:   int32(irec),
    61  			Detector: "MyDetector",
    62  			Descr:    "dummy run number",
    63  			SubDets:  []string{"subdet 0", "subdet 1"},
    64  			Floats: []float64{
    65  				float64(irec) + 100,
    66  				float64(irec) + 200,
    67  				float64(irec) + 300,
    68  			},
    69  			Ints: []int64{
    70  				int64(irec) + 100,
    71  				int64(irec) + 200,
    72  				int64(irec) + 300,
    73  			},
    74  		}
    75  		err = f.WriteRecord(rec)
    76  		if err != nil {
    77  			fmt.Printf("error writing record: %v (irec=%d)", err, irec)
    78  			os.Exit(1)
    79  		}
    80  
    81  		err = f.Sync()
    82  		if err != nil {
    83  			fmt.Printf("error flushing record: %v (irec=%d)", err, irec)
    84  			os.Exit(1)
    85  		}
    86  	}
    87  }