go-hep.org/x/hep@v0.38.1/hbook/bin1d.go (about)

     1  // Copyright ©2015 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 hbook
     6  
     7  import "sort"
     8  
     9  // Bin1D models a bin in a 1-dim space.
    10  type Bin1D struct {
    11  	Range Range
    12  	Dist  Dist1D
    13  }
    14  
    15  func (b Bin1D) clone() Bin1D {
    16  	return Bin1D{
    17  		Range: b.Range.clone(),
    18  		Dist:  b.Dist.clone(),
    19  	}
    20  }
    21  
    22  // Rank returns the number of dimensions for this bin.
    23  func (Bin1D) Rank() int { return 1 }
    24  
    25  func (b *Bin1D) addScaled(a, a2 float64, o Bin1D) {
    26  	b.Dist.addScaled(a, a2, o.Dist)
    27  }
    28  
    29  func (b *Bin1D) scaleW(f float64) {
    30  	b.Dist.scaleW(f)
    31  }
    32  
    33  func (b *Bin1D) fill(x, w float64) {
    34  	b.Dist.fill(x, w)
    35  }
    36  
    37  // Entries returns the number of entries in this bin.
    38  func (b *Bin1D) Entries() int64 {
    39  	return b.Dist.Entries()
    40  }
    41  
    42  // EffEntries returns the effective number of entries \f$ = (\sum w)^2 / \sum w^2 \f$
    43  func (b *Bin1D) EffEntries() float64 {
    44  	return b.Dist.EffEntries()
    45  }
    46  
    47  // SumW returns the sum of weights in this bin.
    48  func (b *Bin1D) SumW() float64 {
    49  	return b.Dist.SumW()
    50  }
    51  
    52  // SumW2 returns the sum of squared weights in this bin.
    53  func (b *Bin1D) SumW2() float64 {
    54  	return b.Dist.SumW2()
    55  }
    56  
    57  // ErrW returns the absolute error on SumW()
    58  func (b *Bin1D) ErrW() float64 {
    59  	return b.Dist.errW()
    60  }
    61  
    62  // XEdges returns the [low,high] edges of this bin.
    63  func (b *Bin1D) XEdges() Range {
    64  	return b.Range
    65  }
    66  
    67  // XMin returns the lower limit of the bin (inclusive).
    68  func (b *Bin1D) XMin() float64 {
    69  	return b.Range.Min
    70  }
    71  
    72  // XMax returns the upper limit of the bin (exclusive).
    73  func (b *Bin1D) XMax() float64 {
    74  	return b.Range.Max
    75  }
    76  
    77  // XMid returns the geometric center of the bin.
    78  // i.e.: 0.5*(high+low)
    79  func (b *Bin1D) XMid() float64 {
    80  	return 0.5 * (b.Range.Min + b.Range.Max)
    81  }
    82  
    83  // XWidth returns the (signed) width of the bin
    84  func (b *Bin1D) XWidth() float64 {
    85  	return b.Range.Max - b.Range.Min
    86  }
    87  
    88  // XFocus returns the mean position in the bin, or the midpoint (if the
    89  // sum of weights for this bin is 0).
    90  func (b *Bin1D) XFocus() float64 {
    91  	if b.SumW() == 0 {
    92  		return b.XMid()
    93  	}
    94  	return b.XMean()
    95  }
    96  
    97  // XMean returns the mean X.
    98  func (b *Bin1D) XMean() float64 {
    99  	return b.Dist.mean()
   100  }
   101  
   102  // XVariance returns the variance in X.
   103  func (b *Bin1D) XVariance() float64 {
   104  	return b.Dist.variance()
   105  }
   106  
   107  // XStdDev returns the standard deviation in X.
   108  func (b *Bin1D) XStdDev() float64 {
   109  	return b.Dist.stdDev()
   110  }
   111  
   112  // XStdErr returns the standard error in X.
   113  func (b *Bin1D) XStdErr() float64 {
   114  	return b.Dist.stdErr()
   115  }
   116  
   117  // XRMS returns the RMS in X.
   118  func (b *Bin1D) XRMS() float64 {
   119  	return b.Dist.rms()
   120  }
   121  
   122  // Bin1Ds is a sorted slice of Bin1D implementing sort.Interface.
   123  type Bin1Ds []Bin1D
   124  
   125  func (p Bin1Ds) Len() int           { return len(p) }
   126  func (p Bin1Ds) Less(i, j int) bool { return p[i].Range.Min < p[j].Range.Min }
   127  func (p Bin1Ds) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
   128  
   129  // IndexOf returns the index of the Bin1D containing the value v.
   130  // It returns UndeflowBin if v is smaller than the smallest bin value.
   131  // It returns OverflowBin if v is greater than the greatest bin value.
   132  // It returns len(bins) if v falls within a bins gap.
   133  func (p Bin1Ds) IndexOf(v float64) int {
   134  	i := sort.Search(len(p), func(i int) bool { return v < p[i].Range.Max })
   135  	if i == len(p) {
   136  		return OverflowBin1D
   137  	}
   138  	rng := p[i].Range
   139  	if i == 0 && v < rng.Min {
   140  		return UnderflowBin1D
   141  	}
   142  	if rng.Min <= v && v < rng.Max {
   143  		return i
   144  	}
   145  	return len(p)
   146  }