go-hep.org/x/hep@v0.38.1/groot/rhist/p2d.go (about)

     1  // Copyright ©2022 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 rhist
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  
    11  	"go-hep.org/x/hep/groot/rbytes"
    12  	"go-hep.org/x/hep/groot/rcont"
    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  // Profile2D is a 2-dim profile histogram.
    19  type Profile2D struct {
    20  	h2d        H2D          // base class
    21  	binEntries rcont.ArrayD // number of entries per bin
    22  	errMode    int32        // Option to compute errors
    23  	zmin       float64      // Lower limit in Z (if set)
    24  	zmax       float64      // Upper limit in Z (if set)
    25  	sumwz      float64      // Total Sum of weight*Z
    26  	sumwz2     float64      // Total Sum of weight*Z*Z
    27  	binSumw2   rcont.ArrayD // Array of sum of squares of weights per bin
    28  }
    29  
    30  func newProfile2D() *Profile2D {
    31  	return &Profile2D{
    32  		h2d: *newH2D(),
    33  	}
    34  }
    35  
    36  func (*Profile2D) Class() string {
    37  	return "TProfile2D"
    38  }
    39  
    40  func (*Profile2D) RVersion() int16 {
    41  	return rvers.Profile2D
    42  }
    43  
    44  // MarshalROOT implements rbytes.Marshaler
    45  func (p2d *Profile2D) MarshalROOT(w *rbytes.WBuffer) (int, error) {
    46  	if w.Err() != nil {
    47  		return 0, w.Err()
    48  	}
    49  
    50  	hdr := w.WriteHeader(p2d.Class(), p2d.RVersion())
    51  
    52  	w.WriteObject(&p2d.h2d)
    53  	w.WriteObject(&p2d.binEntries)
    54  	w.WriteI32(p2d.errMode)
    55  	w.WriteF64(p2d.zmin)
    56  	w.WriteF64(p2d.zmax)
    57  	w.WriteF64(p2d.sumwz)
    58  	w.WriteF64(p2d.sumwz2)
    59  	w.WriteObject(&p2d.binSumw2)
    60  
    61  	return w.SetHeader(hdr)
    62  }
    63  
    64  // UnmarshalROOT implements rbytes.Unmarshaler
    65  func (p2d *Profile2D) UnmarshalROOT(r *rbytes.RBuffer) error {
    66  	if r.Err() != nil {
    67  		return r.Err()
    68  	}
    69  
    70  	hdr := r.ReadHeader(p2d.Class(), p2d.RVersion())
    71  	if hdr.Vers < 8 {
    72  		// tested with v8.
    73  		panic(fmt.Errorf("rhist: too old TProfile2D version=%d < 8", hdr.Vers))
    74  	}
    75  
    76  	r.ReadObject(&p2d.h2d)
    77  	r.ReadObject(&p2d.binEntries)
    78  	p2d.errMode = r.ReadI32()
    79  	p2d.zmin = r.ReadF64()
    80  	p2d.zmax = r.ReadF64()
    81  	p2d.sumwz = r.ReadF64()
    82  	p2d.sumwz2 = r.ReadF64()
    83  	r.ReadObject(&p2d.binSumw2)
    84  
    85  	r.CheckHeader(hdr)
    86  	return r.Err()
    87  }
    88  
    89  func init() {
    90  	f := func() reflect.Value {
    91  		p2d := newProfile2D()
    92  		return reflect.ValueOf(p2d)
    93  	}
    94  	rtypes.Factory.Add("TProfile2D", f)
    95  }
    96  
    97  var (
    98  	_ root.Object        = (*Profile2D)(nil)
    99  	_ rbytes.RVersioner  = (*Profile2D)(nil)
   100  	_ rbytes.Marshaler   = (*Profile2D)(nil)
   101  	_ rbytes.Unmarshaler = (*Profile2D)(nil)
   102  )