go-hep.org/x/hep@v0.38.1/heppdt/pdt.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 heppdt
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  )
    11  
    12  var defaultTable Table
    13  
    14  // Table represents a particle data table.
    15  type Table struct {
    16  	name string
    17  	pdt  map[PID]*Particle
    18  	pid  map[string]PID
    19  }
    20  
    21  // New returns a new particle data table, initialized from r
    22  func New(r io.Reader, n string) (Table, error) {
    23  	t := Table{
    24  		name: n,
    25  		pdt:  make(map[PID]*Particle),
    26  		pid:  make(map[string]PID),
    27  	}
    28  	err := parse(r, &t)
    29  	return t, err
    30  }
    31  
    32  // Name returns the name of this particle data table
    33  func (t *Table) Name() string {
    34  	return t.name
    35  }
    36  
    37  // Len returns the size of the particle data table
    38  func (t *Table) Len() int {
    39  	return len(t.pdt)
    40  }
    41  
    42  // PDT returns the particle data table
    43  func (t *Table) PDT() map[PID]*Particle {
    44  	return t.pdt
    45  }
    46  
    47  // ParticleByID returns the particle information via particle ID
    48  func (t *Table) ParticleByID(pid PID) *Particle {
    49  	p, ok := t.pdt[pid]
    50  	if !ok {
    51  		return nil
    52  	}
    53  	return p
    54  }
    55  
    56  // ParticleByName returns the particle information via particle name
    57  func (t *Table) ParticleByName(n string) *Particle {
    58  	pid, ok := t.pid[n]
    59  	if !ok {
    60  		return nil
    61  	}
    62  
    63  	p, ok := t.pdt[pid]
    64  	if !ok {
    65  		return nil
    66  	}
    67  	return p
    68  }
    69  
    70  func init() {
    71  	var err error
    72  	defaultTable, err = New(bytes.NewBufferString(tabledata), "particle.tbl")
    73  	if err != nil {
    74  		panic(err)
    75  	}
    76  }