9
9
#include < TBranch.h>
10
10
#include < TLeaf.h>
11
11
#include < TTreeFormula.h>
12
+ #include < TTreeCache.h>
12
13
13
14
#include < iostream>
14
- #include < iomanip>
15
- #include < fstream>
16
- #include < cstdlib>
17
- #include < cstdio>
18
- #include < cassert>
19
15
#include < string>
20
16
#include < map>
21
17
#include < vector>
22
- #include < set >
18
+ #include < utility >
23
19
24
20
#include " Column.h"
25
21
#include " util.h"
@@ -47,20 +43,21 @@ class TreeChain
47
43
{
48
44
public:
49
45
50
- TreeChain (TTree* fChain ):
51
- fChain (fChain ),
52
- ientry (0 )
46
+ TreeChain (TTree* tree, long long cache_size):
47
+ tree (tree),
48
+ itree (-1 ),
49
+ ientry (0 ),
50
+ cache_size (cache_size)
53
51
{
54
- fCurrent = -1 ;
55
- notifier = new MiniNotify (fChain ->GetNotify ());
56
- fChain ->SetNotify (notifier);
52
+ notifier = new MiniNotify (tree->GetNotify ());
53
+ tree->SetNotify (notifier);
57
54
}
58
55
59
56
~TreeChain ()
60
57
{
61
- fChain ->SetNotify (notifier->oldnotify );
58
+ tree ->SetNotify (notifier->oldnotify );
62
59
63
- // Delete TTreeFormula
60
+ // Delete all TTreeFormula
64
61
std::vector<TTreeFormula*>::iterator fit;
65
62
for (fit = formulae.begin (); fit != formulae.end (); ++fit)
66
63
{
@@ -78,22 +75,21 @@ class TreeChain
78
75
// Enable all branches since we don't know yet which branches are
79
76
// required by the formulae. The branches must be activated when a
80
77
// TTreeFormula is initially created.
81
- fChain ->SetBranchStatus (" *" , true );
82
- // fChain->SetCacheSize(10000000);
78
+ tree->SetBranchStatus (" *" , true );
83
79
}
84
80
return (int )load;
85
81
}
86
82
87
83
long long LoadTree (long long entry)
88
84
{
89
- long long load = fChain ->LoadTree (entry);
85
+ long long load = tree ->LoadTree (entry);
90
86
if (load < 0 )
91
87
{
92
88
return load;
93
89
}
94
- if (fChain ->GetTreeNumber () != fCurrent )
90
+ if (tree ->GetTreeNumber () != itree )
95
91
{
96
- fCurrent = fChain ->GetTreeNumber ();
92
+ itree = tree ->GetTreeNumber ();
97
93
}
98
94
if (notifier->notified )
99
95
{
@@ -111,53 +107,81 @@ class TreeChain
111
107
112
108
void InitBranches ()
113
109
{
114
- // The branches must be activated when a TTreeFormula is initially created.
110
+ // Prepare() must be called before InitBranches()
111
+ TFile* file = NULL ;
112
+ TTreeCache* cache = NULL ;
115
113
TBranch* branch;
116
114
TLeaf* leaf;
117
115
std::string bname, lname;
118
116
LeafCache::iterator it;
119
117
120
- // Only the required branches will be added to the cache below
121
- fChain ->DropBranchFromCache (" *" , true );
118
+ // Set the cache size. This should create a new cache if one does not
119
+ // already exist.
120
+ tree->SetCacheSize (cache_size);
121
+
122
+ // Get and set up the cache
123
+ file = tree->GetCurrentFile ();
124
+ if (file)
125
+ {
126
+ cache = dynamic_cast <TTreeCache*>(file->GetCacheRead (tree));
127
+ }
128
+ if (cache)
129
+ {
130
+ cache->ResetCache ();
131
+ cache->StartLearningPhase ();
132
+ }
122
133
123
134
for (it = leafcache.begin (); it != leafcache.end (); ++it)
124
135
{
125
- bname = it->first .first ;
126
- lname = it->first .second ;
127
- branch = fChain ->GetBranch (bname.c_str ());
128
- leaf = branch->FindLeaf (lname.c_str ());
136
+ leaf = it->second ->leaf ;
137
+ branch = leaf->GetBranch ();
129
138
130
139
// Make the branch active and cache it
131
140
branch->SetStatus (true );
132
- fChain ->AddBranchToCache (branch, true );
133
- // and the length leaf as well
141
+ if (cache)
142
+ {
143
+ tree->AddBranchToCache (branch, true );
144
+ }
134
145
146
+ // ... and the length leaf as well
135
147
// TODO: Does this work if user doesn't want the length column
136
148
// in the output structure?
137
149
TLeaf* leafCount = leaf->GetLeafCount ();
138
150
if (leafCount != NULL )
139
151
{
140
152
branch = leafCount->GetBranch ();
141
153
branch->SetStatus (true );
142
- fChain ->AddBranchToCache (branch, true );
154
+ if (cache)
155
+ {
156
+ tree->AddBranchToCache (branch, true );
157
+ }
143
158
}
144
159
}
145
160
146
161
// Activate all branches used by the formulae
147
- int ncodes, n ;
162
+ int ncodes, icode ;
148
163
std::vector<TTreeFormula*>::iterator fit;
149
164
for (fit = formulae.begin (); fit != formulae.end (); ++fit)
150
165
{
151
166
ncodes = (*fit)->GetNcodes ();
152
- for (n = 0 ; n < ncodes; ++n )
167
+ for (icode = 0 ; icode < ncodes; ++icode )
153
168
{
154
- branch = (*fit)->GetLeaf (n )->GetBranch ();
169
+ branch = (*fit)->GetLeaf (icode )->GetBranch ();
155
170
// Branch may be a TObject split across multiple
156
171
// subbranches. These must be activated recursively.
157
172
activate_branch_recursive (branch);
158
- fChain ->AddBranchToCache (branch, true );
173
+ if (cache)
174
+ {
175
+ tree->AddBranchToCache (branch, true );
176
+ }
159
177
}
160
178
}
179
+ if (cache)
180
+ {
181
+ // Stop the cache learning phase since we have included a fixed set
182
+ // of branches
183
+ cache->StopLearningPhase ();
184
+ }
161
185
}
162
186
163
187
int GetEntry (long long entry)
@@ -234,18 +258,19 @@ class TreeChain
234
258
{
235
259
bname = it->first .first ;
236
260
lname = it->first .second ;
237
- branch = fChain ->FindBranch (bname.c_str ());
261
+ branch = tree ->FindBranch (bname.c_str ());
238
262
if (branch == NULL )
239
263
{
240
264
std::cerr << " WARNING: cannot find branch " << bname
241
- << std::endl;
265
+ << std::endl;
242
266
continue ;
243
267
}
244
268
leaf = branch->FindLeaf (lname.c_str ());
245
269
if (leaf == NULL )
246
270
{
247
271
std::cerr << " WARNING: cannot find leaf " << lname
248
- << " for branch " << bname << std::endl;
272
+ << " for branch " << bname
273
+ << std::endl;
249
274
continue ;
250
275
}
251
276
it->second ->SetLeaf (leaf, true );
@@ -273,7 +298,7 @@ class TreeChain
273
298
const std::string& leaf_name,
274
299
BranchColumn* column)
275
300
{
276
- BL bl = make_pair (branch_name, leaf_name);
301
+ BranchLeaf bl = make_pair (branch_name, leaf_name);
277
302
leafcache.insert (make_pair (bl, column));
278
303
}
279
304
@@ -299,15 +324,16 @@ class TreeChain
299
324
TObject* oldnotify;
300
325
};
301
326
302
- TTree* fChain ;
303
- int fCurrent ;
327
+ TTree* tree ;
328
+ int itree ;
304
329
long long ientry;
330
+ long long cache_size;
305
331
MiniNotify* notifier;
306
332
std::vector<TTreeFormula*> formulae;
307
333
308
334
// Branch name to leaf name association
309
- typedef std::pair<std::string, std::string> BL ;
310
- typedef std::map<BL , BranchColumn*> LeafCache;
335
+ typedef std::pair<std::string, std::string> BranchLeaf ;
336
+ typedef std::map<BranchLeaf , BranchColumn*> LeafCache;
311
337
312
338
// Column pointer cache to update leaves
313
339
// when new file is loaded in the chain
0 commit comments