/
chain.go
187 lines (164 loc) · 3.53 KB
/
chain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright ©2018 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rtree
import (
"fmt"
"go-hep.org/x/hep/groot/riofs"
"go-hep.org/x/hep/groot/root"
)
type chain struct {
trees []Tree
offs []int64 // number of entries before this tree
tots []int64 // total number of entries up to this tree
cur int // index of current tree
tree Tree // current tree
off int64 // current offset
tot int64 // current number of entries
}
// Chain returns a Tree that is the concatenation of all the input Trees.
func Chain(trees ...Tree) Tree {
if len(trees) == 0 {
return &chain{}
}
n := len(trees)
ch := &chain{
trees: make([]Tree, n),
offs: make([]int64, n),
tots: make([]int64, n),
cur: -1,
}
var (
sum int64
off int64
)
for i := range trees {
t := trees[i]
n := t.Entries()
sum += n
ch.trees[i] = t
ch.offs[i] = off
ch.tots[i] = sum
off += n
}
ch.loadTree(ch.cur + 1)
return ch
}
// ChainOf returns a Tree, a close function and an error if any.
// The tree is the logical concatenation of all the name trees
// located in the input named files.
// The close function allows to close all the open named files.
func ChainOf(name string, files ...string) (Tree, func() error, error) {
var (
trees = make([]Tree, len(files))
fs = make([]*riofs.File, len(files))
)
closef := func(fs []*riofs.File) {
for _, f := range fs {
if f == nil {
continue
}
f.Close()
}
}
for i, n := range files {
f, err := riofs.Open(n)
if err != nil {
closef(fs)
return nil, nil, err
}
fs[i] = f
obj, err := f.Get(name)
if err != nil {
closef(fs)
return nil, nil, err
}
t, ok := obj.(Tree)
if !ok {
closef(fs)
return nil, nil, fmt.Errorf("rtree: object %q in file %q is not a Tree", name, n)
}
trees[i] = t
}
ch := Chain(trees...)
close := func() error {
var err error
for _, f := range fs {
e := f.Close()
if e != nil && err == nil {
err = e
}
}
return err
}
return ch, close, nil
}
func (ch *chain) loadTree(i int) {
ch.cur = i
if ch.cur >= len(ch.trees) {
ch.tree = nil
return
}
ch.tree = ch.trees[ch.cur]
ch.off = ch.offs[ch.cur]
ch.tot = ch.tots[ch.cur]
}
// Class returns the ROOT class of the argument.
func (*chain) Class() string {
return "TChain"
}
// Name returns the name of the ROOT objet in the argument.
func (t *chain) Name() string {
if t.tree == nil {
return ""
}
return t.tree.Name()
}
// Title returns the title of the ROOT object in the argument.
func (t *chain) Title() string {
if t.tree == nil {
return ""
}
return t.tree.Title()
}
// Entries returns the total number of entries.
func (t *chain) Entries() int64 {
var v int64
for _, tree := range t.trees {
v += tree.Entries()
}
return v
}
// Branches returns the list of branches.
func (t *chain) Branches() []Branch {
if t.tree == nil {
return nil
}
return t.tree.Branches()
}
// Branch returns the branch whose name is the argument.
func (t *chain) Branch(name string) Branch {
if t.tree == nil {
return nil
}
return t.tree.Branch(name)
}
// Leaves returns direct pointers to individual branch leaves.
func (t *chain) Leaves() []Leaf {
if t.tree == nil {
return nil
}
return t.tree.Leaves()
}
// Leaf returns the leaf whose name is the argument.
func (t *chain) Leaf(name string) Leaf {
if t.tree == nil {
return nil
}
return t.tree.Leaf(name)
}
var (
_ root.Object = (*chain)(nil)
_ root.Named = (*chain)(nil)
_ Tree = (*chain)(nil)
)