go-hep.org/x/hep@v0.38.1/groot/rphys/vector2.go (about)

     1  // Copyright ©2020 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  package rphys
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  
    11  	"go-hep.org/x/hep/groot/rbase"
    12  	"go-hep.org/x/hep/groot/rbytes"
    13  	"go-hep.org/x/hep/groot/root"
    14  	"go-hep.org/x/hep/groot/rtypes"
    15  	"go-hep.org/x/hep/groot/rvers"
    16  )
    17  
    18  type Vector2 struct {
    19  	obj rbase.Object
    20  	x   float64
    21  	y   float64
    22  }
    23  
    24  func NewVector2(x, y float64) *Vector2 {
    25  	return &Vector2{
    26  		obj: *rbase.NewObject(),
    27  		x:   x,
    28  		y:   y,
    29  	}
    30  }
    31  
    32  func (*Vector2) RVersion() int16 {
    33  	return rvers.Vector2
    34  }
    35  
    36  func (*Vector2) Class() string {
    37  	return "TVector2"
    38  }
    39  
    40  func (vec *Vector2) X() float64 { return vec.x }
    41  func (vec *Vector2) Y() float64 { return vec.y }
    42  
    43  func (vec *Vector2) SetX(x float64) { vec.x = x }
    44  func (vec *Vector2) SetY(y float64) { vec.y = y }
    45  
    46  func (vec *Vector2) MarshalROOT(w *rbytes.WBuffer) (int, error) {
    47  	if w.Err() != nil {
    48  		return 0, w.Err()
    49  	}
    50  
    51  	hdr := w.WriteHeader(vec.Class(), vec.RVersion())
    52  	w.WriteObject(&vec.obj)
    53  	w.WriteF64(vec.x)
    54  	w.WriteF64(vec.y)
    55  
    56  	return w.SetHeader(hdr)
    57  }
    58  
    59  func (vec *Vector2) UnmarshalROOT(r *rbytes.RBuffer) error {
    60  	if r.Err() != nil {
    61  		return r.Err()
    62  	}
    63  
    64  	hdr := r.ReadHeader(vec.Class(), vec.RVersion())
    65  
    66  	if hdr.Vers == 1 || hdr.Vers > 2 {
    67  		r.ReadObject(&vec.obj)
    68  	}
    69  
    70  	vec.x = r.ReadF64()
    71  	vec.y = r.ReadF64()
    72  
    73  	r.CheckHeader(hdr)
    74  	return r.Err()
    75  }
    76  
    77  func (vec *Vector2) String() string {
    78  	return fmt.Sprintf("TVector2{%v, %v}", vec.x, vec.y)
    79  }
    80  
    81  func init() {
    82  	{
    83  		f := func() reflect.Value {
    84  			o := &Vector2{}
    85  			return reflect.ValueOf(o)
    86  		}
    87  		rtypes.Factory.Add("TVector2", f)
    88  	}
    89  }
    90  
    91  var (
    92  	_ root.Object        = (*Vector2)(nil)
    93  	_ rbytes.Marshaler   = (*Vector2)(nil)
    94  	_ rbytes.Unmarshaler = (*Vector2)(nil)
    95  )