hep: go-hep.org/x/hep/hbook/ntup Index | Examples | Files | Directories

package ntup

import "go-hep.org/x/hep/hbook/ntup"

Package ntup provides a way to create, open and iterate over n-tuple data.

Index

Examples

Package Files

ntuple.go

Variables

var (
    // ErrNotExist is returned when an n-tuple could not be located in a sql.DB
    ErrNotExist = errors.New("hbook/ntup: ntuple does not exist")

    // ErrMissingColDef is returned when some information is missing wrt
    // an n-tuple column definition
    ErrMissingColDef = errors.New("hbook/ntup: expected at least one column definition")
)

type Descriptor Uses

type Descriptor interface {
    Name() string       // the column name
    Type() reflect.Type // the column type
}

Descriptor describes a column

type Ntuple Uses

type Ntuple struct {
    // contains filtered or unexported fields
}

Ntuple provides read/write access to row-wise n-tuple data.

Code:

nt, err := ntcsv.Open(
    "ntcsv/testdata/simple-with-header.csv",
    ntcsv.Comma(';'),
    ntcsv.Header(),
    ntcsv.Columns("v1", "v2", "v3"),
)
if err != nil {
    log.Fatal(err)
}
defer nt.DB().Close()
var (
    v1min = +math.MaxFloat64
    v1max = -math.MaxFloat64
    v2min = +math.MaxFloat64
    v2max = -math.MaxFloat64
)
err = nt.Scan("v1, v2", func(v1, v2 float64) error {
    v1min = math.Min(v1min, v1)
    v1max = math.Max(v1max, v1)
    v2min = math.Min(v2min, v2)
    v2max = math.Max(v2max, v2)
    return nil
})
if err != nil {
    log.Fatal(err)
}

fmt.Printf("V1Min  %v\n", v1min)
fmt.Printf("V1Max  %v\n", v1max)
fmt.Printf("V2Min  %v\n", v2min)
fmt.Printf("V2Max  %v\n", v2max)

Output:

V1Min  0
V1Max  9
V2Min  0
V2Max  9

Code:

nt, err := ntcsv.Open(
    "ntcsv/testdata/simple-with-header.csv",
    ntcsv.Comma(';'),
    ntcsv.Header(),
    ntcsv.Columns("v1", "v2", "v3"),
)
if err != nil {
    log.Fatal(err)
}
defer nt.DB().Close()

h, err := nt.ScanH1D("v1", nil)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("V1Mean:      %f\n", h.XMean())
fmt.Printf("V1RMS:       %f\n", h.XRMS())
fmt.Printf("V1StdDev:    %f\n", h.XStdDev())
fmt.Printf("V1StdErr:    %f\n", h.XStdErr())

Output:

V1Mean:      4.500000
V1RMS:       5.338539
V1StdDev:    3.027650
V1StdErr:    0.957427

Code:

nt, err := ntcsv.Open(
    "ntcsv/testdata/simple-with-header.csv",
    ntcsv.Comma(';'),
    ntcsv.Header(),
    ntcsv.Columns("v1", "v2", "v3"),
)
if err != nil {
    log.Fatal(err)
}
defer nt.DB().Close()

h, err := nt.ScanH2D("v1, v2", nil)

if err != nil {
    log.Fatal(err)
}
fmt.Printf("XMean:      %f\n", h.XMean())
fmt.Printf("YMean:      %f\n", h.YMean())
fmt.Printf("XRMS:       %f\n", h.XRMS())
fmt.Printf("YRMS:       %f\n", h.YRMS())
fmt.Printf("XStdDev:    %f\n", h.XStdDev())
fmt.Printf("YStdDev:    %f\n", h.YStdDev())
fmt.Printf("XStdErr:    %f\n", h.XStdErr())
fmt.Printf("YStdErr:    %f\n", h.YStdErr())

Output:

XMean:      4.500000
YMean:      4.500000
XRMS:       5.338539
YRMS:       5.338539
XStdDev:    3.027650
YStdDev:    3.027650
XStdErr:    0.957427
YStdErr:    0.957427

func Create Uses

func Create(db *sql.DB, name string, cols ...interface{}) (*Ntuple, error)

Create creates a new ntuple with the given name inside the given database handle. The n-tuple schema is inferred from the cols argument. cols can be:

- a single struct value (columns are inferred from the names+types of the exported fields)
- a list of builtin values (the columns names are varX where X=[1-len(cols)])
- a list of ntup.Descriptors

e.g.:

nt, err := ntup.Create(db, "nt", struct{X float64 `hbook:"x"`}{})
nt, err := ntup.Create(db, "nt", int64(0), float64(0))

func Open Uses

func Open(db *sql.DB, name string) (*Ntuple, error)

Open inspects the given database handle and tries to return an Ntuple connected to a table with the given name. Open returns ErrNotExist if no such table exists. If name is "", Open will connect to the one-and-only table in the db.

e.g.:

db, err := sql.Open("csv", "file.csv")
nt, err := ntup.Open(db, "ntup")

func (*Ntuple) Cols Uses

func (nt *Ntuple) Cols() []Descriptor

Cols returns the columns' descriptors of this n-tuple. Modifying it directly leads to undefined behaviour.

func (*Ntuple) DB Uses

func (nt *Ntuple) DB() *sql.DB

DB returns the underlying db this n-tuple is connected to.

func (*Ntuple) Name Uses

func (nt *Ntuple) Name() string

Name returns the name of this n-tuple.

func (*Ntuple) Scan Uses

func (nt *Ntuple) Scan(query string, f interface{}) error

Scan executes a query against the ntuple and runs the function f against that context.

e.g.

err = nt.Scan("x,y where z>10", func(x,y float64) error {
  h1.Fill(x, 1)
  h2.Fill(y, 1)
  return nil
})

func (*Ntuple) ScanH1D Uses

func (nt *Ntuple) ScanH1D(query string, h *hbook.H1D) (*hbook.H1D, error)

ScanH1D executes a query against the ntuple and fills the histogram with the results of the query. If h is nil, a (100-bins, xmin, xmax+ULP) histogram is created, where xmin and xmax are inferred from the content of the underlying database.

func (*Ntuple) ScanH2D Uses

func (nt *Ntuple) ScanH2D(query string, h *hbook.H2D) (*hbook.H2D, error)

ScanH2D executes a query against the ntuple and fills the histogram with the results of the query. If h is nil, a (100-bins, xmin, xmax+ULP) (100-bins, ymin, ymax+ULP) 2d-histogram is created, where xmin, xmax and ymin,ymax are inferred from the content of the underlying database.

Directories

PathSynopsis
ntcsvPackage ntcsv provides a convenient access to CSV files as n-tuple data.

Package ntup imports 9 packages (graph) and is imported by 1 packages. Updated 2019-04-15. Refresh now. Tools for package owners.