package rsql
import "go-hep.org/x/hep/groot/rsql"
Package rsql provides a convenient access to ROOT files/trees as a database.
Index ¶
- func Scan(tree rtree.Tree, query string, f interface{}) error
- func ScanH1D(tree rtree.Tree, query string, h *hbook.H1D) (*hbook.H1D, error)
- func ScanH2D(tree rtree.Tree, query string, h *hbook.H2D) (*hbook.H2D, error)
Examples ¶
Functions ¶
func Scan ¶
Scan executes a query against the given tree and runs the function f
within that context.
Code:play
Output: Code:play
Output:Example¶
package main
import (
"fmt"
"log"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/rsql"
"go-hep.org/x/hep/groot/rtree"
)
func main() {
f, err := groot.Open("../testdata/simple.root")
if err != nil {
log.Fatal(err)
}
defer f.Close()
o, err := f.Get("tree")
if err != nil {
log.Fatal(err)
}
tree := o.(rtree.Tree)
var data []float64
err = rsql.Scan(tree, "SELECT two FROM tree", func(x float64) error {
data = append(data, x)
return nil
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("tree[%q]: %v\n", "two", data)
}
tree["two"]: [1.1 2.2 3.3 4.4]
Example (NVars)¶
package main
import (
"fmt"
"log"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/rsql"
"go-hep.org/x/hep/groot/rtree"
)
func main() {
f, err := groot.Open("../testdata/simple.root")
if err != nil {
log.Fatal(err)
}
defer f.Close()
o, err := f.Get("tree")
if err != nil {
log.Fatal(err)
}
tree := o.(rtree.Tree)
var (
v1s []int32
v2s []float64
v3s []string
)
err = rsql.Scan(tree, "SELECT (one, two, three) FROM tree", func(x int32, y float64, z string) error {
v1s = append(v1s, x)
v2s = append(v2s, y)
v3s = append(v3s, z)
return nil
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("tree[%q]: %v\n", "one", v1s)
fmt.Printf("tree[%q]: %v\n", "two", v2s)
fmt.Printf("tree[%q]: %q\n", "three", v3s)
}
tree["one"]: [1 2 3 4]
tree["two"]: [1.1 2.2 3.3 4.4]
tree["three"]: ["uno" "dos" "tres" "quatro"]
func ScanH1D ¶
ScanH1D executes a query against the tree 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.
Code:play
Output: Code:play
Output:Example¶
package main
import (
"fmt"
"log"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/rsql"
"go-hep.org/x/hep/groot/rtree"
)
func main() {
f, err := groot.Open("../testdata/simple.root")
if err != nil {
log.Fatal(err)
}
defer f.Close()
o, err := f.Get("tree")
if err != nil {
log.Fatal(err)
}
tree := o.(rtree.Tree)
h, err := rsql.ScanH1D(tree, "SELECT two FROM tree", nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("entries: %v\n", h.Entries())
fmt.Printf("x-axis: (min=%v, max=%v)\n", h.XMin(), h.XMax())
fmt.Printf("x-mean: %v\n", h.XMean())
fmt.Printf("x-std-dev: %1.3f\nx-std-err: %1.3f\n", h.XStdDev(), h.XStdErr())
}
entries: 4
x-axis: (min=1.1, max=4.400000000000001)
x-mean: 2.75
x-std-dev: 1.420
x-std-err: 0.710
Example (WithH1D)¶
package main
import (
"fmt"
"log"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/rsql"
"go-hep.org/x/hep/groot/rtree"
"go-hep.org/x/hep/hbook"
)
func main() {
f, err := groot.Open("../testdata/simple.root")
if err != nil {
log.Fatal(err)
}
defer f.Close()
o, err := f.Get("tree")
if err != nil {
log.Fatal(err)
}
tree := o.(rtree.Tree)
h, err := rsql.ScanH1D(tree, "SELECT two FROM tree", hbook.NewH1D(100, 0, 10))
if err != nil {
log.Fatal(err)
}
fmt.Printf("entries: %v\n", h.Entries())
fmt.Printf("x-axis: (min=%v, max=%v)\n", h.XMin(), h.XMax())
fmt.Printf("x-mean: %v\n", h.XMean())
fmt.Printf("x-std-dev: %1.3f\nx-std-err: %1.3f\n", h.XStdDev(), h.XStdErr())
}
entries: 4
x-axis: (min=0, max=10)
x-mean: 2.75
x-std-dev: 1.420
x-std-err: 0.710
func ScanH2D ¶
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.
Code:play
Output: Code:play
Output:Example¶
package main
import (
"fmt"
"log"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/rsql"
"go-hep.org/x/hep/groot/rtree"
)
func main() {
f, err := groot.Open("../testdata/simple.root")
if err != nil {
log.Fatal(err)
}
defer f.Close()
o, err := f.Get("tree")
if err != nil {
log.Fatal(err)
}
tree := o.(rtree.Tree)
h, err := rsql.ScanH2D(tree, "SELECT (one, two) FROM tree", nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("entries: %v\n", h.Entries())
fmt.Printf("x-axis: (min=%v, max=%v)\n", h.XMin(), h.XMax())
fmt.Printf("x-mean: %v\n", h.XMean())
fmt.Printf("x-std-dev: %1.3f\nx-std-err: %1.3f\n", h.XStdDev(), h.XStdErr())
fmt.Printf("y-axis: (min=%v, max=%v)\n", h.YMin(), h.YMax())
fmt.Printf("y-mean: %v\n", h.YMean())
fmt.Printf("y-std-dev: %1.3f\ny-std-err: %1.3f\n", h.YStdDev(), h.YStdErr())
}
entries: 4
x-axis: (min=1, max=4.000000000000001)
x-mean: 2.5
x-std-dev: 1.291
x-std-err: 0.645
y-axis: (min=1.1, max=4.400000000000001)
y-mean: 2.75
y-std-dev: 1.420
y-std-err: 0.710
Example (WithH2D)¶
package main
import (
"fmt"
"log"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/rsql"
"go-hep.org/x/hep/groot/rtree"
"go-hep.org/x/hep/hbook"
)
func main() {
f, err := groot.Open("../testdata/simple.root")
if err != nil {
log.Fatal(err)
}
defer f.Close()
o, err := f.Get("tree")
if err != nil {
log.Fatal(err)
}
tree := o.(rtree.Tree)
h, err := rsql.ScanH2D(tree, "SELECT (one, two) FROM tree", hbook.NewH2D(100, 0, 10, 100, 0, 10))
if err != nil {
log.Fatal(err)
}
fmt.Printf("entries: %v\n", h.Entries())
fmt.Printf("x-axis: (min=%v, max=%v)\n", h.XMin(), h.XMax())
fmt.Printf("x-mean: %v\n", h.XMean())
fmt.Printf("x-std-dev: %1.3f\nx-std-err: %1.3f\n", h.XStdDev(), h.XStdErr())
fmt.Printf("y-axis: (min=%v, max=%v)\n", h.YMin(), h.YMax())
fmt.Printf("y-mean: %v\n", h.YMean())
fmt.Printf("y-std-dev: %1.3f\ny-std-err: %1.3f\n", h.YStdDev(), h.YStdErr())
}
entries: 4
x-axis: (min=0, max=10)
x-mean: 2.5
x-std-dev: 1.291
x-std-err: 0.645
y-axis: (min=0, max=10)
y-mean: 2.75
y-std-dev: 1.420
y-std-err: 0.710
Source Files ¶
scan.go
Directories ¶
Path | Synopsis |
---|---|
groot/rsql/rsqldrv | Package rsqldrv registers a database/sql/driver.Driver implementation for ROOT files. |
- Version
- v0.36.0 (latest)
- Published
- Nov 15, 2024
- Platform
- linux/amd64
- Imports
- 7 packages
- Last checked
- 2 days ago –
Tools for package owners.