Skip to content

Commit

Permalink
groot: add support for TLeafG
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastien Binet <binet@cern.ch>
  • Loading branch information
sbinet committed Sep 6, 2023
1 parent 7cb2fac commit 85fd5fd
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 1 deletion.
2 changes: 1 addition & 1 deletion groot/gen.rboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var (
"TChain",
"TLeaf", "TLeafElement", "TLeafObject",
"TLeafO",
"TLeafB", "TLeafS", "TLeafI", "TLeafL",
"TLeafB", "TLeafS", "TLeafI", "TLeafL", "TLeafG",
"TLeafF", "TLeafD",
"TLeafF16", "TLeafD32",
"TLeafC",
Expand Down
15 changes: 15 additions & 0 deletions groot/gen.rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ func genLeaves() {
WFuncArray: "w.WriteArrayI64",
Count: true,
},
{
Name: "LeafG",
Type: "int64",
Kind: "reflect.Int64",
UKind: "reflect.Uint64",
LenType: 8,
GoLenType: int(reflect.TypeOf(int64(0)).Size()),
DoUnsigned: true,
RFunc: "r.ReadI64()",
RFuncArray: "r.ReadArrayI64",
ResizeFunc: "rbytes.ResizeI64",
WFunc: "w.WriteI64",
WFuncArray: "w.WriteArrayI64",
Count: true,
},
{
Name: "LeafF",
Type: "float32",
Expand Down
41 changes: 41 additions & 0 deletions groot/rdict/cxx_root_streamers_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6702,6 +6702,47 @@ func init() {
Factor: 0.000000,
}.New()},
}))
StreamerInfos.Add(NewCxxStreamerInfo("TLeafG", 1, 0xeca71cb7, []rbytes.StreamerElement{
NewStreamerBase(Element{
Name: *rbase.NewNamed("TLeaf", "Leaf: description of a Branch data type"),
Type: rmeta.Base,
Size: 0,
ArrLen: 0,
ArrDim: 0,
MaxIdx: [5]int32{0, 1830715730, 0, 0, 0},
Offset: 0,
EName: "BASE",
XMin: 0.000000,
XMax: 0.000000,
Factor: 0.000000,
}.New(), 2),
&StreamerBasicType{StreamerElement: Element{
Name: *rbase.NewNamed("fMinimum", "Minimum value if leaf range is specified"),
Type: rmeta.Long,
Size: 8,
ArrLen: 0,
ArrDim: 0,
MaxIdx: [5]int32{0, 0, 0, 0, 0},
Offset: 0,
EName: "long",
XMin: 0.000000,
XMax: 0.000000,
Factor: 0.000000,
}.New()},
&StreamerBasicType{StreamerElement: Element{
Name: *rbase.NewNamed("fMaximum", "Maximum value if leaf range is specified"),
Type: rmeta.Long,
Size: 8,
ArrLen: 0,
ArrDim: 0,
MaxIdx: [5]int32{0, 0, 0, 0, 0},
Offset: 0,
EName: "long",
XMin: 0.000000,
XMax: 0.000000,
Factor: 0.000000,
}.New()},
}))
StreamerInfos.Add(NewCxxStreamerInfo("TLeafF", 1, 0x3add9d72, []rbytes.StreamerElement{
NewStreamerBase(Element{
Name: *rbase.NewNamed("TLeaf", "Leaf: description of a Branch data type"),
Expand Down
227 changes: 227 additions & 0 deletions groot/rtree/leaf_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,233 @@ var (
_ rbytes.Unmarshaler = (*LeafL)(nil)
)

// LeafG implements ROOT TLeafG
type LeafG struct {
rvers int16
tleaf
ptr *int64
sli *[]int64
min int64
max int64
}

func newLeafG(b Branch, name string, shape []int, unsigned bool, count Leaf) *LeafG {
const etype = 8
var lcnt leafCount
if count != nil {
lcnt = count.(leafCount)
}
return &LeafG{
rvers: rvers.LeafG,
tleaf: newLeaf(name, shape, etype, 0, false, unsigned, lcnt, b),
}
}

// Class returns the ROOT class name.
func (leaf *LeafG) Class() string {
return "TLeafG"
}

// Minimum returns the minimum value of the leaf.
func (leaf *LeafG) Minimum() int64 {
return leaf.min
}

// Maximum returns the maximum value of the leaf.
func (leaf *LeafG) Maximum() int64 {
return leaf.max
}

// Kind returns the leaf's kind.
func (leaf *LeafG) Kind() reflect.Kind {
if leaf.IsUnsigned() {
return reflect.Uint64
}
return reflect.Int64
}

// Type returns the leaf's type.
func (leaf *LeafG) Type() reflect.Type {
if leaf.IsUnsigned() {
var v uint64
return reflect.TypeOf(v)
}
var v int64
return reflect.TypeOf(v)
}

// ivalue returns the first leaf value as int
func (leaf *LeafG) ivalue() int {
return int(*leaf.ptr)
}

// imax returns the leaf maximum value as int
func (leaf *LeafG) imax() int {
return int(leaf.max)
}

func (leaf *LeafG) TypeName() string {
if leaf.IsUnsigned() {
return "uint64"
}
return "int64"
}

func (leaf *LeafG) MarshalROOT(w *rbytes.WBuffer) (int, error) {
if w.Err() != nil {
return 0, w.Err()
}

hdr := w.WriteHeader(leaf.Class(), leaf.rvers)
w.WriteObject(&leaf.tleaf)
w.WriteI64(leaf.min)
w.WriteI64(leaf.max)

return w.SetHeader(hdr)
}

func (leaf *LeafG) UnmarshalROOT(r *rbytes.RBuffer) error {
if r.Err() != nil {
return r.Err()
}

hdr := r.ReadHeader(leaf.Class())
if hdr.Vers > rvers.LeafG {
panic(fmt.Errorf("rtree: invalid TLeafG version=%d > %d", hdr.Vers, rvers.LeafG))
}

leaf.rvers = hdr.Vers

r.ReadObject(&leaf.tleaf)

leaf.min = r.ReadI64()
leaf.max = r.ReadI64()

r.CheckHeader(hdr)
return r.Err()
}

func (leaf *LeafG) readFromBuffer(r *rbytes.RBuffer) error {
if r.Err() != nil {
return r.Err()
}

if leaf.count == nil && leaf.ptr != nil {
*leaf.ptr = r.ReadI64()
} else {
if leaf.count != nil {
n := leaf.count.ivalue()
max := leaf.count.imax()
if n > max {
n = max
}
nn := leaf.tleaf.len * n
*leaf.sli = rbytes.ResizeI64(*leaf.sli, nn)
r.ReadArrayI64(*leaf.sli)
} else {
nn := leaf.tleaf.len
*leaf.sli = rbytes.ResizeI64(*leaf.sli, nn)
r.ReadArrayI64(*leaf.sli)
}
}
return r.Err()
}

func (leaf *LeafG) unsafeDecayArray(ptr interface{}) interface{} {
rv := reflect.ValueOf(ptr).Elem()
sz := rv.Type().Size() / 8
arr := (*[0]int64)(unsafe.Pointer(rv.UnsafeAddr()))
sli := (*arr)[:]
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&sli))
hdr.Len = int(sz)
hdr.Cap = int(sz)
return &sli
}

func (leaf *LeafG) setAddress(ptr interface{}) error {
if ptr == nil {
return leaf.setAddress(newValue(leaf))
}

if rv := reflect.Indirect(reflect.ValueOf(ptr)); rv.Kind() == reflect.Array {
sli := leaf.unsafeDecayArray(ptr)
switch sli := sli.(type) {
case *[]int64:
return leaf.setAddress(sli)
case *[]uint64:
return leaf.setAddress(sli)
default:
panic(fmt.Errorf("invalid ptr type %T (leaf=%s|%T)", ptr, leaf.Name(), leaf))
}
}

switch v := ptr.(type) {
case *int64:
leaf.ptr = v
case *[]int64:
leaf.sli = v
if *v == nil {
*leaf.sli = make([]int64, 0)
}
case *uint64:
leaf.ptr = (*int64)(unsafe.Pointer(v))
case *[]uint64:
leaf.sli = (*[]int64)(unsafe.Pointer(v))
if *v == nil {
*leaf.sli = make([]int64, 0)
}
default:
panic(fmt.Errorf("invalid ptr type %T (leaf=%s|%T)", v, leaf.Name(), leaf))
}
return nil
}

func (leaf *LeafG) writeToBuffer(w *rbytes.WBuffer) (int, error) {
if w.Err() != nil {
return 0, w.Err()
}

var nbytes int
switch {
case leaf.ptr != nil:
w.WriteI64(*leaf.ptr)
nbytes += leaf.tleaf.etype
if v := *leaf.ptr; v > leaf.max {
leaf.max = v
}
case leaf.count != nil:
n := leaf.count.ivalue()
max := leaf.count.imax()
if n > max {
n = max
}
end := leaf.tleaf.len * n
w.WriteArrayI64((*leaf.sli)[:end])
nbytes += leaf.tleaf.etype * end
default:
w.WriteArrayI64((*leaf.sli)[:leaf.tleaf.len])
nbytes += leaf.tleaf.etype * leaf.tleaf.len
}

return nbytes, w.Err()
}

func init() {
f := func() reflect.Value {
o := &LeafG{}
return reflect.ValueOf(o)
}
rtypes.Factory.Add("TLeafG", f)
}

var (
_ root.Object = (*LeafG)(nil)
_ root.Named = (*LeafG)(nil)
_ Leaf = (*LeafG)(nil)
_ rbytes.Marshaler = (*LeafG)(nil)
_ rbytes.Unmarshaler = (*LeafG)(nil)
)

// LeafF implements ROOT TLeafF
type LeafF struct {
rvers int16
Expand Down
7 changes: 7 additions & 0 deletions groot/rtree/rleaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package rtree
import (
"fmt"
"reflect"
"unsafe"

"go-hep.org/x/hep/groot/rbytes"
"go-hep.org/x/hep/groot/rdict"
Expand Down Expand Up @@ -147,6 +148,12 @@ func rleafFrom(leaf Leaf, rvar ReadVar, rctx rleafCtx) rleaf {
}
panic(fmt.Errorf("rvar mismatch for %T", leaf))
}
case *LeafG:
// FIXME(sbinet): should we bite the bullet and generate a whole
// set of types+funcs for LeafG instead of relying on the
// assumption that LeafG data has the same underlying layout and size
// than LeafL ? (ie: sizeof(Long_t) == sizeof(Long64_t))
return rleafFrom((*LeafL)(unsafe.Pointer(leaf)), rvar, rctx)
case *LeafF:
return newRLeafF32(leaf, rvar, rctx)
case *LeafD:
Expand Down
1 change: 1 addition & 0 deletions groot/rvers/versions_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const (
LeafS = 1 // ROOT version for TLeafS
LeafI = 1 // ROOT version for TLeafI
LeafL = 1 // ROOT version for TLeafL
LeafG = 1 // ROOT version for TLeafG
LeafF = 1 // ROOT version for TLeafF
LeafD = 1 // ROOT version for TLeafD
LeafF16 = 1 // ROOT version for TLeafF16
Expand Down

0 comments on commit 85fd5fd

Please sign in to comment.