Skip to content

Commit

Permalink
groot: implement TFormula-v14
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastien Binet <[email protected]>
  • Loading branch information
sbinet committed Feb 14, 2025
1 parent f071454 commit 6e95c6d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
16 changes: 13 additions & 3 deletions groot/rhist/formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Formula struct {
params map[string]int32 // list of parameter names
formula string // string representing the formula expression
ndim int32 // Dimension - needed for lambda expressions
numID int32 // Number used to identify pre-defined functions (gaus, expo,..)
linearParts []root.Object // vector of linear functions
vectorized bool // whether we should use vectorized or regular variables
}
Expand Down Expand Up @@ -66,6 +67,7 @@ func (f *Formula) MarshalROOT(w *rbytes.WBuffer) (int, error) {
writeMapStringInt(w, f.params)
w.WriteString(f.formula)
w.WriteI32(f.ndim)
w.WriteI32(f.numID)
writeStdVectorObjP(w, f.linearParts)
w.WriteBool(f.vectorized)

Expand All @@ -79,9 +81,14 @@ func (f *Formula) UnmarshalROOT(r *rbytes.RBuffer) error {

hdr := r.ReadHeader(f.Class(), f.RVersion())

if hdr.Vers < 12 || hdr.Vers > 13 {
// tested with v12 and v13
panic(fmt.Errorf("rhist: too old TFormula version=%d < 12", hdr.Vers))
if hdr.Vers < 12 || hdr.Vers > 14 {
// tested with v12 and v14
if hdr.Vers < 12 {
panic(fmt.Errorf("rhist: too old TFormula version=%d < 12", hdr.Vers))
}
if hdr.Vers > 14 {
panic(fmt.Errorf("rhist: too new TFormula version=%d > 14", hdr.Vers))
}
}

r.ReadObject(&f.named)
Expand All @@ -90,6 +97,9 @@ func (f *Formula) UnmarshalROOT(r *rbytes.RBuffer) error {
f.params = readMapStringInt(r)
f.formula = r.ReadString()
f.ndim = r.ReadI32()
if hdr.Vers > 13 {
f.numID = r.ReadI32()
}

f.linearParts = readStdVectorObjP(r)
f.vectorized = r.ReadBool()
Expand Down
63 changes: 35 additions & 28 deletions groot/rhist/rw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,37 +330,44 @@ func TestWRBuffer(t *testing.T) {
}

func TestReadF1(t *testing.T) {
f, err := riofs.Open("../testdata/tformula.root")
if err != nil {
t.Fatal(err)
}
defer f.Close()

for _, key := range []string{
"func1", "func2", "func3", "func4",
"fconv",
"fnorm",
for _, fname := range []string{
"../testdata/tformula.root",
"../testdata/tformula-v14.root",
} {
t.Run(key, func(t *testing.T) {
obj, err := f.Get(key)
t.Run(fname, func(t *testing.T) {
f, err := riofs.Open(fname)
if err != nil {
t.Fatalf("could not read object %q: %+v", key, err)
t.Fatal(err)
}
switch v := obj.(type) {
case *F1:
if got, want := v.Name(), key; got != want {
t.Fatalf("invalid name: got=%q, want=%q", got, want)
}
if got, want := v.Class(), "TF1"; got != want {
t.Fatalf("invalid class: got=%q, want=%q", got, want)
}
if got, want := v.chi2, 0.2; got != want {
t.Fatalf("invalid chi2: got=%v, want=%v", got, want)
}
case F1Composition:
// ok.
default:
t.Fatalf("invalid object type for %q", key)
defer f.Close()

for _, key := range []string{
"func1", "func2", "func3", "func4",
"fconv",
"fnorm",
} {
t.Run(key, func(t *testing.T) {
obj, err := f.Get(key)
if err != nil {
t.Fatalf("could not read object %q: %+v", key, err)
}
switch v := obj.(type) {
case *F1:
if got, want := v.Name(), key; got != want {
t.Fatalf("invalid name: got=%q, want=%q", got, want)
}
if got, want := v.Class(), "TF1"; got != want {
t.Fatalf("invalid class: got=%q, want=%q", got, want)
}
if got, want := v.chi2, 0.2; got != want {
t.Fatalf("invalid chi2: got=%v, want=%v", got, want)
}
case F1Composition:
// ok.
default:
t.Fatalf("invalid object type for %q", key)
}
})
}
})
}
Expand Down
Binary file added groot/testdata/tformula-v14.root
Binary file not shown.

0 comments on commit 6e95c6d

Please sign in to comment.