go-hep.org/x/hep@v0.38.1/groot/riofs/gendata/gen-small-evnt-tree.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  //go:build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"codeberg.org/go-hep/croot"
    13  )
    14  
    15  const ARRAYSZ = 10
    16  
    17  type Event struct {
    18  	I32 int32
    19  	I64 int64
    20  	U32 uint32
    21  	U64 uint64
    22  	F32 float32
    23  	F64 float64
    24  	Str string
    25  
    26  	ArrayI32 [ARRAYSZ]int32
    27  	ArrayI64 [ARRAYSZ]int64
    28  	ArrayU32 [ARRAYSZ]uint32
    29  	ArrayU64 [ARRAYSZ]uint64
    30  	ArrayF32 [ARRAYSZ]float32
    31  	ArrayF64 [ARRAYSZ]float64
    32  
    33  	N        int32
    34  	SliceI32 []int32
    35  	SliceI64 []int64
    36  	SliceU32 []uint32
    37  	SliceU64 []uint64
    38  	SliceF32 []float32
    39  	SliceF64 []float64
    40  }
    41  
    42  func main() {
    43  	const fname = "test-small.root"
    44  	const evtmax = 100
    45  	const splitlevel = 32
    46  	const bufsiz = 32000
    47  	const compress = 1
    48  	const netopt = 0
    49  
    50  	f, err := croot.OpenFile(fname, "recreate", "small event file", compress, netopt)
    51  	if err != nil {
    52  		panic(err.Error())
    53  	}
    54  
    55  	// create a tree
    56  	tree := croot.NewTree("tree", "my tree title", splitlevel)
    57  
    58  	e := Event{}
    59  
    60  	_, err = tree.Branch("evt", &e, bufsiz, 0)
    61  	if err != nil {
    62  		panic(err.Error())
    63  	}
    64  
    65  	// fill some events with random numbers
    66  	for iev := int64(0); iev != evtmax; iev++ {
    67  		if iev%1000 == 0 {
    68  			fmt.Printf(":: processing event %d...\n", iev)
    69  		}
    70  
    71  		e.I32 = int32(iev)
    72  		e.I64 = int64(iev)
    73  		e.U32 = uint32(iev)
    74  		e.U64 = uint64(iev)
    75  		e.F32 = float32(iev)
    76  		e.F64 = float64(iev)
    77  		e.Str = fmt.Sprintf("evt-%03d", iev)
    78  
    79  		for ii := 0; ii < ARRAYSZ; ii++ {
    80  			e.ArrayI32[ii] = int32(iev)
    81  			e.ArrayI64[ii] = int64(iev)
    82  			e.ArrayU32[ii] = uint32(iev)
    83  			e.ArrayU64[ii] = uint64(iev)
    84  			e.ArrayF32[ii] = float32(iev)
    85  			e.ArrayF64[ii] = float64(iev)
    86  		}
    87  
    88  		e.N = int32(iev) % 10
    89  		e.SliceI32 = make([]int32, int(e.N))
    90  		e.SliceI64 = make([]int64, int(e.N))
    91  		e.SliceU32 = make([]uint32, int(e.N))
    92  		e.SliceU64 = make([]uint64, int(e.N))
    93  		e.SliceF32 = make([]float32, int(e.N))
    94  		e.SliceF64 = make([]float64, int(e.N))
    95  		for ii := 0; ii < int(e.N); ii++ {
    96  			e.SliceI32[ii] = int32(iev)
    97  			e.SliceI64[ii] = int64(iev)
    98  			e.SliceU32[ii] = uint32(iev)
    99  			e.SliceU64[ii] = uint64(iev)
   100  			e.SliceF32[ii] = float32(iev)
   101  			e.SliceF64[ii] = float64(iev)
   102  		}
   103  
   104  		_, err = tree.Fill()
   105  		if err != nil {
   106  			panic(err.Error())
   107  		}
   108  	}
   109  	f.Write("", 0, 0)
   110  	f.Close("")
   111  
   112  }
   113  
   114  // EOF