go-hep.org/x/hep@v0.38.1/hepmc/units.go (about)

     1  // Copyright ©2017 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 hepmc
     6  
     7  import (
     8  	"fmt"
     9  )
    10  
    11  // MomentumUnit describes the units of momentum quantities (MeV or GeV)
    12  type MomentumUnit int
    13  
    14  // LengthUnit describes the units of length quantities (mm or cm)
    15  type LengthUnit int
    16  
    17  const (
    18  	// MEV is a Momentum in MeV (default)
    19  	MEV MomentumUnit = iota
    20  	// GEV is a Momentum in GeV
    21  	GEV
    22  )
    23  
    24  const (
    25  	// MM is a Length in mm (default)
    26  	MM LengthUnit = iota
    27  	// CM is a Length in cm
    28  	CM
    29  )
    30  
    31  func (mu MomentumUnit) String() string {
    32  	switch mu {
    33  	case MEV:
    34  		return "MEV"
    35  	case GEV:
    36  		return "GEV"
    37  	}
    38  	panic(fmt.Errorf("hepmc.units: invalid MomentumUnit value (%d)", int(mu)))
    39  }
    40  
    41  // MomentumUnitFromString creates a MomentumUnit value from its string representation
    42  func MomentumUnitFromString(s string) (MomentumUnit, error) {
    43  	switch s {
    44  	case "MEV":
    45  		return MEV, nil
    46  	case "GEV":
    47  		return GEV, nil
    48  	}
    49  	return -1, fmt.Errorf("hepmc.units: invalid MomentumUnit string-value (%s)", s)
    50  }
    51  
    52  func (lu LengthUnit) String() string {
    53  	switch lu {
    54  	case MM:
    55  		return "MM"
    56  	case CM:
    57  		return "CM"
    58  	}
    59  	panic(fmt.Errorf("hepmc.units: invalid LengthUnit value (%d)", int(lu)))
    60  }
    61  
    62  // LengthUnitFromString creates a LengthUnit value from its string representation
    63  func LengthUnitFromString(s string) (LengthUnit, error) {
    64  	switch s {
    65  	case "MM":
    66  		return MM, nil
    67  	case "CM":
    68  		return CM, nil
    69  	}
    70  	return -1, fmt.Errorf("hepmc.units: invalid LengthUnit string-value (%s)", s)
    71  }