ntup

package
v0.24.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2020 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

View Source
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")
)

Functions

This section is empty.

Types

type Descriptor

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

Descriptor describes a column

type Ntuple

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

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

Example (Open)
package main

import (
	"database/sql"
	"fmt"
	"log"

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

func main() {
	db, err := sql.Open("csv", "ntcsv/testdata/simple-with-header.csv")
	if err != nil {
		log.Fatalf("could not open csv-db file: %+v", err)
	}
	defer db.Close()

	nt, err := ntup.Open(db, "ntup")
	if err != nil {
		log.Fatalf("could not open ntup: %+v", err)
	}
	fmt.Printf("name=%q\n", nt.Name())

}
Output:

name="ntup"
Example (Scan)
package main

import (
	"fmt"
	"log"
	"math"

	"go-hep.org/x/hep/hbook/ntup/ntcsv"
)

func main() {
	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
Example (ScanH1D)
package main

import (
	"fmt"
	"log"

	"go-hep.org/x/hep/hbook/ntup/ntcsv"
)

func main() {
	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
Example (ScanH2D)
package main

import (
	"fmt"
	"log"

	"go-hep.org/x/hep/hbook/ntup/ntcsv"
)

func main() {
	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

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

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

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

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

func (*Ntuple) DB

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

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

func (*Ntuple) Name

func (nt *Ntuple) Name() string

Name returns the name of this n-tuple.

func (*Ntuple) Scan

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

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

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

Path Synopsis
Package ntcsv provides a convenient access to CSV files as n-tuple data.
Package ntcsv provides a convenient access to CSV files as n-tuple data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL