I have multiple root files Mc_1.root, Mc_2.root etc. In each root file I have a histogram name counter. I want to find the number of entries in each file, secondly I want to add all the entries to get total number of entries in all root files. Kindly help me to do this.
package main
import (
"fmt"
"log"
"os"
"go-hep.org/x/hep/rootio"
)
func main() {
fnames := os.Args[1:]
entries := 0.0
for _, fname := range fnames {
n, err := process(fname, "rigidity")
if err != nil {
log.Fatalf("error processing %q: %v", fname, err)
}
fmt.Printf("file %q: %v\n", fname, n)
entries += n
}
fmt.Printf("\n==> total entries: %v\n", entries)
}
func process(fname, name string) (float64, error) {
f, err := rootio.Open(fname)
if err != nil {
return 0, err
}
defer f.Close()
obj, ok := f.Get(name)
if !ok {
return 0, fmt.Errorf("file %q has no object called %q", f.Name(), name)
}
h, ok := obj.(*rootio.H1F)
if !ok {
return 0, fmt.Errorf("object %q is not a TH1F", name)
}
return h.Entries(), nil
}
and then:
$> go get go-hep.org/x/hep/rootio
$> go run ./main.go /path/to/*.root
file "f1.root": 900000
file "f2.root": 900000
file "rigidity.root": 900000
==> total entries: 2.7e+06
(I just put 3 times the same file for that exercize)