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