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 |
< |
|
8 |
> |
#include <TBranchRef.h> |
9 |
|
#include "MitAna/DataUtil/interface/Debug.h" |
10 |
|
|
11 |
|
using namespace mithep; |
27 |
|
fEvtObjNum(-1), |
28 |
|
fIsInit(kFALSE), |
29 |
|
fDoObjNumReset(doreset), |
30 |
+ |
fDoBranchRef(0), |
31 |
|
fFile(0), |
32 |
|
fTrees(0) |
33 |
|
{ |
41 |
|
{ |
42 |
|
// Destructor. |
43 |
|
|
44 |
< |
if (fIsInit) { |
45 |
< |
CloseFile(); |
46 |
< |
} |
47 |
< |
|
48 |
< |
TDirectory::TContext context(0); |
49 |
< |
fTrees.Clear(); |
44 |
> |
Terminate(); |
45 |
|
} |
46 |
|
|
47 |
|
//__________________________________________________________________________________________________ |
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. |
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 |
< |
|
64 |
< |
//__________________________________________________________________________________________________ |
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 |
65 |
|
AddBranch(name, CName(obj), obj, bsize, level); |
66 |
|
} |
67 |
|
|
68 |
< |
//__________________________________________________________________________________________________ |
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 |
73 |
|
// to object pointer for class name "cname" using a given buffer size and default splitlevel. |
74 |
|
|
75 |
< |
MyTree *t = AddOrGetMyTree(GetName()); |
76 |
< |
t->Bronch(name, cname, obj, bsize, fDefSL); |
75 |
> |
MyTree *t = AddOrGetMyTree(GetName()); |
76 |
> |
TBranch *b = t->Bronch(name, cname, obj, bsize, fDefSL); |
77 |
> |
b->SetCompressionLevel(GetCompressLevel()); |
78 |
|
} |
79 |
|
|
80 |
< |
//__________________________________________________________________________________________________ |
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 |
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 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 |
107 |
|
AddBranch(name, CName(obj), obj); |
108 |
|
} |
109 |
|
|
110 |
< |
//__________________________________________________________________________________________________ |
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 |
|
{ |
129 |
|
AddBranchToTree(tname, name, CName(obj), obj, bsize, level); |
130 |
|
} |
131 |
|
|
132 |
< |
//__________________________________________________________________________________________________ |
132 |
> |
//-------------------------------------------------------------------------------------------------- |
133 |
|
void TreeWriter::AddBranchToTree(const char *tname, const char *name, const char *cname, |
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 |
|
{ |
151 |
|
AddBranchToTree(tname, name, CName(obj), obj, bsize); |
152 |
|
} |
153 |
|
|
154 |
< |
//__________________________________________________________________________________________________ |
154 |
> |
//-------------------------------------------------------------------------------------------------- |
155 |
|
void TreeWriter::AddBranchToTree(const char *tname, const char *name, const char *cname, |
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 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 |
172 |
|
AddBranchToTree(tname, name, CName(obj), obj); |
173 |
|
} |
174 |
|
|
175 |
< |
//__________________________________________________________________________________________________ |
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)); |
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 *tobj = dynamic_cast<TObject*>(*(TObject**)obj); |
263 |
> |
TObject **sobj = static_cast<TObject**>(obj); |
264 |
> |
TObject *tobj = dynamic_cast<TObject*>(*sobj); |
265 |
|
if (tobj==0) { |
266 |
< |
Fatal("ClassName", "Given void* ptr can not be dereferenced into TObject*"); |
266 |
> |
Fatal("CName", "Given void* ptr cannot be dereferenced into TObject*"); |
267 |
|
} |
268 |
|
return tobj->ClassName(); |
269 |
|
} |
270 |
|
|
271 |
< |
//__________________________________________________________________________________________________ |
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 |
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 |
|
// Return entries of tree with given name. If no tree is given, return sum of entries |
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. |
488 |
|
mt->SetAutoFill(b); |
489 |
|
} |
490 |
|
|
491 |
< |
|
453 |
< |
//------------------------------------------------------------------------------------------------- |
491 |
> |
//-------------------------------------------------------------------------------------------------- |
492 |
|
void TreeWriter::SetMaxSize(Long64_t s) |
493 |
|
{ |
494 |
|
// Set maximum file size. Check if this exceeds the ROOT file size and if, |
504 |
|
fMaxSize=s; |
505 |
|
} |
506 |
|
|
507 |
< |
//__________________________________________________________________________________________________ |
507 |
> |
//-------------------------------------------------------------------------------------------------- |
508 |
|
void TreeWriter::StoreObject(const TObject *obj) |
509 |
|
{ |
510 |
|
// Store object next to tree in file. Used to store the |
522 |
|
|
523 |
|
fFile->WriteTObject(obj,obj->GetName(),"WriteDelete"); |
524 |
|
} |
525 |
+ |
|
526 |
+ |
//__________________________________________________________________________________________________ |
527 |
+ |
void TreeWriter::Terminate() |
528 |
+ |
{ |
529 |
+ |
// Terminate tree file writing. |
530 |
+ |
|
531 |
+ |
if (fIsInit) { |
532 |
+ |
CloseFile(); |
533 |
+ |
} |
534 |
+ |
|
535 |
+ |
TDirectory::TContext context(0); |
536 |
+ |
fTrees.Clear(); |
537 |
+ |
} |