1 |
|
// $Id$ |
2 |
|
|
3 |
|
#include "MitAna/DataUtil/interface/TreeWriter.h" |
4 |
– |
|
4 |
|
#include <Riostream.h> |
5 |
|
#include <TObject.h> |
6 |
|
#include <TSystem.h> |
7 |
|
#include <TProcessID.h> |
8 |
+ |
#include <TBranchRef.h> |
9 |
+ |
#include "MitAna/DataUtil/interface/Debug.h" |
10 |
|
|
11 |
|
using namespace mithep; |
12 |
|
|
13 |
|
ClassImp(mithep::TreeWriter) |
14 |
|
|
15 |
|
//__________________________________________________________________________________________________ |
16 |
< |
TreeWriter::TreeWriter(const char *tname, Bool_t doreset) |
17 |
< |
: TNamed(tname,Form("%s written by mithep::TreeWriter", tname)), |
18 |
< |
fBaseURL("."), |
19 |
< |
fPrefix("mithep"), |
20 |
< |
fFileNumber(0), |
21 |
< |
fCompressLevel(9), |
22 |
< |
fDefBrSize(64*1024), |
23 |
< |
fDefSL(99), |
24 |
< |
fMaxSize((Long64_t)(0.99 * TTree::GetMaxTreeSize())), |
25 |
< |
fkMinFreeSpace(1024*1024), |
26 |
< |
fkMinAvgSize(10*1024), |
27 |
< |
fEvtObjNum(-1), |
28 |
< |
fIsInit(kFALSE), |
29 |
< |
fDoObjNumReset(doreset), |
30 |
< |
fFile(0), |
31 |
< |
fTrees(0) |
16 |
> |
TreeWriter::TreeWriter(const char *tname, Bool_t doreset) : |
17 |
> |
TNamed(tname,Form("%s written by mithep::TreeWriter", tname)), |
18 |
> |
fBaseURL("."), |
19 |
> |
fPrefix("mithep"), |
20 |
> |
fFileNumber(0), |
21 |
> |
fCompressLevel(9), |
22 |
> |
fDefBrSize(64*1024), |
23 |
> |
fDefSL(99), |
24 |
> |
fMaxSize((Long64_t)(0.99 * TTree::GetMaxTreeSize())), |
25 |
> |
fkMinFreeSpace(1024*1024), |
26 |
> |
fkMinAvgSize(10*1024), |
27 |
> |
fEvtObjNum(-1), |
28 |
> |
fIsInit(kFALSE), |
29 |
> |
fDoObjNumReset(doreset), |
30 |
> |
fDoBranchRef(0), |
31 |
> |
fFile(0), |
32 |
> |
fTrees(0) |
33 |
|
{ |
34 |
|
// Constructor. |
35 |
|
|
41 |
|
{ |
42 |
|
// Destructor. |
43 |
|
|
44 |
< |
if (fIsInit) { |
43 |
< |
CloseFile(); |
44 |
< |
} |
45 |
< |
|
46 |
< |
TDirectory::TContext context(0); |
47 |
< |
fTrees.Clear(); |
44 |
> |
Terminate(); |
45 |
|
} |
46 |
|
|
47 |
|
//__________________________________________________________________________________________________ |
48 |
|
void TreeWriter::AddBranch(const char *name, const char *cname, |
49 |
|
void *obj, Int_t bsize, Int_t level) |
50 |
|
{ |
51 |
< |
// Add branch with name "name" into tree with name "tname" and set its address |
52 |
< |
// to object pointer for class name "cname" using a given buffer size and splitlevel. |
51 |
> |
// Add branch with name "name" into tree with name "tname" and set its address to object pointer |
52 |
> |
// for class name "cname" using a given buffer size and splitlevel. |
53 |
|
|
54 |
< |
MyTree *t = AddOrGetMyTree(GetName()); |
55 |
< |
t->Bronch(name, cname, obj, bsize, level); |
54 |
> |
MyTree *t = AddOrGetMyTree(GetName()); |
55 |
> |
TBranch *b = t->Bronch(name, cname, obj, bsize, level); |
56 |
> |
b->SetCompressionLevel(GetCompressLevel()); |
57 |
|
} |
58 |
|
|
59 |
< |
//__________________________________________________________________________________________________ |
59 |
> |
//-------------------------------------------------------------------------------------------------- |
60 |
> |
void TreeWriter::AddBranch(const char *name, void *obj, Int_t bsize, Int_t level) |
61 |
> |
{ |
62 |
> |
// Add branch with name "name" into tree with name "tname" and set its address to object pointer |
63 |
> |
// using a given buffer size and splitlevel. |
64 |
> |
|
65 |
> |
AddBranch(name, CName(obj), obj, bsize, level); |
66 |
> |
} |
67 |
> |
|
68 |
> |
//-------------------------------------------------------------------------------------------------- |
69 |
|
void TreeWriter::AddBranch(const char *name, const char *cname, |
70 |
|
void *obj, Int_t bsize) |
71 |
|
{ |
72 |
+ |
// Add branch with name "name" into tree with name "tname" and set its address to object pointer |
73 |
+ |
// for class name "cname" using a given buffer size and default splitlevel. |
74 |
+ |
|
75 |
+ |
MyTree *t = AddOrGetMyTree(GetName()); |
76 |
+ |
TBranch *b = t->Bronch(name, cname, obj, bsize, fDefSL); |
77 |
+ |
b->SetCompressionLevel(GetCompressLevel()); |
78 |
+ |
} |
79 |
+ |
|
80 |
+ |
//-------------------------------------------------------------------------------------------------- |
81 |
+ |
void TreeWriter::AddBranch(const char *name, void *obj, Int_t bsize) |
82 |
+ |
{ |
83 |
|
// Add branch with name "name" into tree with name "tname" and set its address |
84 |
< |
// to object pointer for class name "cname" using a given buffer size and default splitlevel. |
84 |
> |
// to object pointer using a given buffer size and default splitlevel. |
85 |
|
|
86 |
< |
MyTree *t = AddOrGetMyTree(GetName()); |
69 |
< |
t->Bronch(name, cname, obj, bsize, fDefSL); |
86 |
> |
AddBranch(name, CName(obj), obj, bsize); |
87 |
|
} |
88 |
|
|
89 |
< |
//__________________________________________________________________________________________________ |
89 |
> |
//-------------------------------------------------------------------------------------------------- |
90 |
|
void TreeWriter::AddBranch(const char *name, const char *cname, |
91 |
|
void *obj) |
92 |
|
{ |
93 |
|
// Add branch with name "name" into tree with name "tname" and set its address |
94 |
< |
// to object pointer for class name "cname" using a given buffer size and default splitlevel. |
94 |
> |
// to object pointer for class name "cname" using a default buffer size and splitlevel. |
95 |
|
|
96 |
< |
MyTree *t = AddOrGetMyTree(GetName()); |
97 |
< |
t->Bronch(name, cname, obj, fDefBrSize, fDefSL); |
96 |
> |
MyTree *t = AddOrGetMyTree(GetName()); |
97 |
> |
TBranch *b = t->Bronch(name, cname, obj, fDefBrSize, fDefSL); |
98 |
> |
b->SetCompressionLevel(GetCompressLevel()); |
99 |
|
} |
100 |
|
|
101 |
< |
//__________________________________________________________________________________________________ |
101 |
> |
//-------------------------------------------------------------------------------------------------- |
102 |
> |
void TreeWriter::AddBranch(const char *name, void *obj) |
103 |
> |
{ |
104 |
> |
// Add branch with name "name" into tree with name "tname" and set its address |
105 |
> |
// to object pointer using a default buffer size and splitlevel. |
106 |
> |
|
107 |
> |
AddBranch(name, CName(obj), obj); |
108 |
> |
} |
109 |
> |
|
110 |
> |
//-------------------------------------------------------------------------------------------------- |
111 |
|
void TreeWriter::AddBranchToTree(const char *tname, const char *name, const char *cname, |
112 |
|
void *obj, Int_t bsize, Int_t level) |
113 |
|
{ |
114 |
|
// Add branch with name "name" into tree with name "tname" and set its address |
115 |
|
// to object pointer for class name "cname" using a given buffer size and splitlevel. |
116 |
|
|
117 |
< |
MyTree *t = AddOrGetMyTree(tname); |
118 |
< |
t->Bronch(name, cname, obj, bsize, level); |
117 |
> |
MyTree *t = AddOrGetMyTree(tname); |
118 |
> |
TBranch *b = t->Bronch(name, cname, obj, bsize, level); |
119 |
> |
b->SetCompressionLevel(GetCompressLevel()); |
120 |
|
} |
121 |
|
|
122 |
< |
//__________________________________________________________________________________________________ |
122 |
> |
//-------------------------------------------------------------------------------------------------- |
123 |
> |
void TreeWriter::AddBranchToTree(const char *tname, const char *name, void *obj, |
124 |
> |
Int_t bsize, Int_t level) |
125 |
> |
{ |
126 |
> |
// Add branch with name "name" into tree with name "tname" and set its address |
127 |
> |
// to object pointer using a given buffer size and splitlevel. |
128 |
> |
|
129 |
> |
AddBranchToTree(tname, name, CName(obj), obj, bsize, level); |
130 |
> |
} |
131 |
> |
|
132 |
> |
//-------------------------------------------------------------------------------------------------- |
133 |
|
void TreeWriter::AddBranchToTree(const char *tname, const char *name, const char *cname, |
134 |
< |
void *obj, Int_t bsize) |
134 |
> |
void *obj, Int_t bsize) |
135 |
|
{ |
136 |
|
// Add branch with name "name" into tree with name "tname" and set its address |
137 |
|
// to object pointer for class name "cname" using a given buffer size and default splitlevel. |
138 |
|
|
139 |
< |
MyTree *t = AddOrGetMyTree(tname); |
140 |
< |
t->Bronch(name, cname, obj, bsize, fDefSL); |
139 |
> |
MyTree *t = AddOrGetMyTree(tname); |
140 |
> |
TBranch *b = t->Bronch(name, cname, obj, bsize, fDefSL); |
141 |
> |
b->SetCompressionLevel(GetCompressLevel()); |
142 |
|
} |
143 |
|
|
144 |
< |
//__________________________________________________________________________________________________ |
144 |
> |
//-------------------------------------------------------------------------------------------------- |
145 |
> |
void TreeWriter::AddBranchToTree(const char *tname, const char *name, void *obj, |
146 |
> |
Int_t bsize) |
147 |
> |
{ |
148 |
> |
// Add branch with name "name" into tree with name "tname" and set its address |
149 |
> |
// to object pointer using a given buffer size and default splitlevel. |
150 |
> |
|
151 |
> |
AddBranchToTree(tname, name, CName(obj), obj, bsize); |
152 |
> |
} |
153 |
> |
|
154 |
> |
//-------------------------------------------------------------------------------------------------- |
155 |
|
void TreeWriter::AddBranchToTree(const char *tname, const char *name, const char *cname, |
156 |
< |
void *obj) |
156 |
> |
void *obj) |
157 |
|
{ |
158 |
|
// Add branch with name "name" into tree with name "tname" and set its address |
159 |
< |
// to object pointer for class name "cname" using a given buffer size and default splitlevel. |
159 |
> |
// to object pointer for class name "cname" using a default buffer size and splitlevel. |
160 |
|
|
161 |
< |
MyTree *t = AddOrGetMyTree(tname); |
162 |
< |
t->Bronch(name, cname, obj, fDefBrSize, fDefSL); |
161 |
> |
MyTree *t = AddOrGetMyTree(tname); |
162 |
> |
TBranch *b = t->Bronch(name, cname, obj, fDefBrSize, fDefSL); |
163 |
> |
b->SetCompressionLevel(GetCompressLevel()); |
164 |
|
} |
165 |
|
|
166 |
< |
//__________________________________________________________________________________________________ |
166 |
> |
//-------------------------------------------------------------------------------------------------- |
167 |
> |
void TreeWriter::AddBranchToTree(const char *tname, const char *name, void *obj) |
168 |
> |
{ |
169 |
> |
// Add branch with name "name" into tree with name "tname" and set its address |
170 |
> |
// to object pointer for class name "cname" using a default buffer size and splitlevel. |
171 |
> |
|
172 |
> |
AddBranchToTree(tname, name, CName(obj), obj); |
173 |
> |
} |
174 |
> |
|
175 |
> |
//-------------------------------------------------------------------------------------------------- |
176 |
> |
void TreeWriter::AddTree(const char *tname) |
177 |
> |
{ |
178 |
> |
// Add tree with name "name" into tree with name "tname". |
179 |
> |
|
180 |
> |
AddOrGetMyTree(tname); |
181 |
> |
} |
182 |
> |
|
183 |
> |
//-------------------------------------------------------------------------------------------------- |
184 |
|
MyTree *TreeWriter::AddOrGetMyTree(const char *tn) |
185 |
|
{ |
186 |
|
// Add new tree if not present in array of trees or return |
187 |
|
// present tree. |
188 |
|
|
189 |
< |
MyTree *tree = dynamic_cast<MyTree*>(fTrees.FindObject(tn)); |
190 |
< |
if (tree) return tree; |
189 |
> |
MyTree *tree = GetMyTree(tn); |
190 |
> |
if (tree) |
191 |
> |
return tree; |
192 |
|
|
193 |
|
TDirectory::TContext context(fFile); |
194 |
|
tree = new MyTree(tn, tn); |
195 |
|
tree->SetDirectory(fFile); |
196 |
+ |
if (fDoBranchRef) |
197 |
+ |
tree->BranchRef(); |
198 |
|
fTrees.AddLast(tree); |
199 |
|
return tree; |
200 |
|
} |
201 |
|
|
202 |
< |
//__________________________________________________________________________________________________ |
202 |
> |
//-------------------------------------------------------------------------------------------------- |
203 |
|
Bool_t TreeWriter::BeginEvent(Bool_t doreset) |
204 |
|
{ |
205 |
|
// Prepare for the next event. If doreset or fDoObjNumReset is kTRUE |
216 |
|
return kTRUE; |
217 |
|
} |
218 |
|
|
219 |
< |
//__________________________________________________________________________________________________ |
219 |
> |
//-------------------------------------------------------------------------------------------------- |
220 |
|
void TreeWriter::CloseFile() |
221 |
|
{ |
222 |
|
// Write tree(s) and close file. |
231 |
|
|
232 |
|
for (Int_t i=0;i<fTrees.GetEntries();++i) { |
233 |
|
MyTree *mt = static_cast<MyTree*>(fTrees.At(i)); |
234 |
+ |
mt->FlushBaskets(); |
235 |
|
mt->Write(mt->GetName(),TObject::kOverwrite); |
236 |
< |
mt->Reset(); |
236 |
> |
|
237 |
> |
// backup and restore list of branch pointers from TRefTable (needed for autoloading) |
238 |
> |
if (mt->GetBranchRef()) { |
239 |
> |
TObjArray *parents = mt->GetBranchRef()->GetRefTable()->GetParents(); |
240 |
> |
TObjArray parentsBak(*parents); |
241 |
> |
mt->Reset(); |
242 |
> |
for (Int_t j=0; j<parentsBak.GetEntries(); ++j) |
243 |
> |
parents->Add(parentsBak.At(j)); |
244 |
> |
} |
245 |
> |
else |
246 |
> |
mt->Reset(); |
247 |
|
mt->SetDirectory(0); |
248 |
|
} |
249 |
|
|
255 |
|
fFileNumber++; |
256 |
|
} |
257 |
|
|
258 |
< |
//__________________________________________________________________________________________________ |
258 |
> |
//-------------------------------------------------------------------------------------------------- |
259 |
> |
const char *TreeWriter::CName(void *obj) const |
260 |
> |
{ |
261 |
> |
// Dereference void* pointer into TObject* pointer |
262 |
> |
|
263 |
> |
TObject **sobj = static_cast<TObject**>(obj); |
264 |
> |
TObject *tobj = dynamic_cast<TObject*>(*sobj); |
265 |
> |
if (tobj==0) { |
266 |
> |
Fatal("CName", "Given void* ptr cannot be dereferenced into TObject*"); |
267 |
> |
} |
268 |
> |
return tobj->ClassName(); |
269 |
> |
} |
270 |
> |
|
271 |
> |
//-------------------------------------------------------------------------------------------------- |
272 |
> |
void TreeWriter::DoBranchRef(const char *tn) |
273 |
> |
{ |
274 |
> |
// Fill BranchRef for given tree. |
275 |
> |
|
276 |
> |
if (fTrees.GetEntries()==0) { |
277 |
> |
Error("DoBranchRef", "Tree with name %s not found!", tn); |
278 |
> |
return; |
279 |
> |
} |
280 |
> |
|
281 |
> |
MyTree *mt = GetMyTree(tn); |
282 |
> |
if (!mt) |
283 |
> |
return; |
284 |
> |
|
285 |
> |
mt->BranchRef(); |
286 |
> |
} |
287 |
> |
|
288 |
> |
//-------------------------------------------------------------------------------------------------- |
289 |
|
Bool_t TreeWriter::EndEvent(Bool_t doreset) |
290 |
|
{ |
291 |
|
// Store the event in the tree. If doreset or fDoObjNumReset is kTRUE |
300 |
|
Int_t r = 0; |
301 |
|
for (Int_t i=0;i<fTrees.GetEntries();++i) { |
302 |
|
MyTree *mt = static_cast<MyTree*>(fTrees.At(i)); |
303 |
< |
if (mt->GetAutoFill()==0) continue; |
303 |
> |
if (mt->GetAutoFill()==0) |
304 |
> |
continue; |
305 |
|
r += mt->Fill(); |
306 |
|
} |
307 |
|
|
312 |
|
if (fEvtObjNum<0) { |
313 |
|
Error("EndEvent", "Object counter is zero. Did you call BeginEvent(kTRUE)?"); |
314 |
|
} else { |
315 |
< |
// Reset the TRef table. keep it from growing with each event (see doc) |
315 |
> |
// Reset the TRef table. Keep it from growing with each event (see doc) |
316 |
|
TProcessID::SetObjectCount(fEvtObjNum); |
317 |
|
} |
318 |
|
} |
320 |
|
return (r >= 0); |
321 |
|
} |
322 |
|
|
323 |
< |
//------------------------------------------------------------------------------------------------- |
323 |
> |
//-------------------------------------------------------------------------------------------------- |
324 |
|
Long64_t TreeWriter::GetEntries(const char *tn) const |
325 |
|
{ |
326 |
< |
// |
326 |
> |
// Return entries of tree with given name. If no tree is given, return sum of entries |
327 |
> |
// of all trees. |
328 |
|
|
329 |
|
if (fTrees.GetEntries()==0) return -1; |
330 |
|
|
331 |
|
if (tn) { |
332 |
|
const TTree *mt=GetTree(tn); |
333 |
< |
if (mt) return mt->GetEntries(); |
334 |
< |
else return -1; |
333 |
> |
if (mt) |
334 |
> |
return mt->GetEntries(); |
335 |
> |
else |
336 |
> |
return -1; |
337 |
|
} |
338 |
|
|
339 |
|
Long64_t ret = 0; |
344 |
|
return ret; |
345 |
|
} |
346 |
|
|
347 |
< |
//------------------------------------------------------------------------------------------------- |
347 |
> |
//-------------------------------------------------------------------------------------------------- |
348 |
|
MyTree *mithep::TreeWriter::GetMyTree(const char *tn) |
349 |
|
{ |
350 |
|
// Return MyTree with given name from array. |
364 |
|
return 0; |
365 |
|
} |
366 |
|
|
367 |
< |
//------------------------------------------------------------------------------------------------- |
367 |
> |
//-------------------------------------------------------------------------------------------------- |
368 |
|
const TTree *mithep::TreeWriter::GetTree(const char *tn) const |
369 |
|
{ |
370 |
|
// Return TTree with given name from array. |
384 |
|
return 0; |
385 |
|
} |
386 |
|
|
387 |
< |
//------------------------------------------------------------------------------------------------- |
387 |
> |
//-------------------------------------------------------------------------------------------------- |
388 |
|
TTree *mithep::TreeWriter::GetTree(const char *tn) |
389 |
|
{ |
390 |
|
// Return TTree with given name from array. |
404 |
|
return 0; |
405 |
|
} |
406 |
|
|
407 |
< |
//__________________________________________________________________________________________________ |
407 |
> |
//-------------------------------------------------------------------------------------------------- |
408 |
|
Bool_t TreeWriter::IsFull() const |
409 |
|
{ |
410 |
|
// Check if the maximum file size has been reached. |
420 |
|
return (GetFileSize() + avgSize + fkMinFreeSpace) > fMaxSize; |
421 |
|
} |
422 |
|
|
423 |
< |
//__________________________________________________________________________________________________ |
423 |
> |
//-------------------------------------------------------------------------------------------------- |
424 |
|
void TreeWriter::OpenFile() |
425 |
|
{ |
426 |
|
// Open the file and attach the tree. |
451 |
|
fIsInit = kTRUE; |
452 |
|
} |
453 |
|
|
454 |
< |
//__________________________________________________________________________________________________ |
454 |
> |
//-------------------------------------------------------------------------------------------------- |
455 |
|
void TreeWriter::Print(Option_t *option) const |
456 |
|
{ |
457 |
|
// Print the contents of the tree writer. |
473 |
|
<< (GetEntries() == 1 ? " event" : " events") << endl; |
474 |
|
} |
475 |
|
|
476 |
< |
//------------------------------------------------------------------------------------------------- |
476 |
> |
//-------------------------------------------------------------------------------------------------- |
477 |
|
void TreeWriter::SetAutoFill(const char *tn, Bool_t b) |
478 |
|
{ |
479 |
|
// Set auto-fill mode of tree with given name. |
480 |
|
|
481 |
< |
if (fTrees.GetEntries()==0) return; |
481 |
> |
if (fTrees.GetEntries()==0) |
482 |
> |
return; |
483 |
|
|
484 |
|
MyTree *mt = GetMyTree(tn); |
485 |
< |
if (!mt) return; |
485 |
> |
if (!mt) |
486 |
> |
return; |
487 |
|
|
488 |
|
mt->SetAutoFill(b); |
489 |
|
} |
490 |
|
|
491 |
< |
//__________________________________________________________________________________________________ |
491 |
> |
//-------------------------------------------------------------------------------------------------- |
492 |
> |
void TreeWriter::SetMaxSize(Long64_t s) |
493 |
> |
{ |
494 |
> |
// Set maximum file size. Check if this exceeds the ROOT file size and if, |
495 |
> |
// print a warning and adjust it. |
496 |
> |
|
497 |
> |
if (s>=(Long64_t)(0.99 * TTree::GetMaxTreeSize())) { |
498 |
> |
Long64_t news = (Long64_t)(s/0.99); |
499 |
> |
Warning("SetMaxSize", "Maximum tree size increased from %lld to %lld", |
500 |
> |
TTree::GetMaxTreeSize(), news); |
501 |
> |
TTree::SetMaxTreeSize(news); |
502 |
> |
} |
503 |
> |
|
504 |
> |
fMaxSize=s; |
505 |
> |
} |
506 |
> |
|
507 |
> |
//-------------------------------------------------------------------------------------------------- |
508 |
|
void TreeWriter::StoreObject(const TObject *obj) |
509 |
|
{ |
510 |
< |
// Store object next to tree in file. Used to store the |
378 |
< |
// settings of how the tree was created. |
510 |
> |
// Store object next to tree in file. Used to store the settings of how the tree was created. |
511 |
|
|
512 |
|
if (!fIsInit) { |
513 |
|
Fatal("StoreObject", "Tree is not created, call create first!"); |
521 |
|
|
522 |
|
fFile->WriteTObject(obj,obj->GetName(),"WriteDelete"); |
523 |
|
} |
524 |
+ |
|
525 |
+ |
//__________________________________________________________________________________________________ |
526 |
+ |
void TreeWriter::Terminate() |
527 |
+ |
{ |
528 |
+ |
// Terminate tree file writing. |
529 |
+ |
|
530 |
+ |
if (fIsInit) { |
531 |
+ |
CloseFile(); |
532 |
+ |
} |
533 |
+ |
|
534 |
+ |
TDirectory::TContext context(0); |
535 |
+ |
fTrees.Clear(); |
536 |
+ |
} |