ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataUtil/src/TreeWriter.cc
Revision: 1.14
Committed: Tue Mar 3 08:37:12 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre1
Changes since 1.13: +3 -2 lines
Log Message:
Added additional static_cast.

File Contents

# User Rev Content
1 loizides 1.14 // $Id: TreeWriter.cc,v 1.13 2008/12/01 17:39:20 loizides Exp $
2 loizides 1.1
3     #include "MitAna/DataUtil/interface/TreeWriter.h"
4     #include <Riostream.h>
5     #include <TObject.h>
6     #include <TSystem.h>
7     #include <TProcessID.h>
8 bendavid 1.10 #include <TBranchRef.h>
9 loizides 1.3 #include "MitAna/DataUtil/interface/Debug.h"
10    
11 loizides 1.1 using namespace mithep;
12    
13     ClassImp(mithep::TreeWriter)
14    
15     //__________________________________________________________________________________________________
16 loizides 1.5 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 loizides 1.12 fDoBranchRef(0),
31 loizides 1.5 fFile(0),
32     fTrees(0)
33 loizides 1.1 {
34     // Constructor.
35    
36 loizides 1.2 fTrees.SetOwner();
37 loizides 1.1 }
38    
39     //__________________________________________________________________________________________________
40     TreeWriter::~TreeWriter()
41     {
42     // Destructor.
43    
44 loizides 1.13 Terminate();
45 loizides 1.2 }
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.
53    
54 loizides 1.7 MyTree *t = AddOrGetMyTree(GetName());
55     TBranch *b = t->Bronch(name, cname, obj, bsize, level);
56     b->SetCompressionLevel(GetCompressLevel());
57 loizides 1.1 }
58    
59 loizides 1.3
60 loizides 1.9 //--------------------------------------------------------------------------------------------------
61 loizides 1.3 void TreeWriter::AddBranch(const char *name, void *obj, Int_t bsize, Int_t level)
62     {
63     // Add branch with name "name" into tree with name "tname" and set its address
64     // to object pointer using a given buffer size and splitlevel.
65    
66     AddBranch(name, CName(obj), obj, bsize, level);
67     }
68    
69 loizides 1.9 //--------------------------------------------------------------------------------------------------
70 loizides 1.2 void TreeWriter::AddBranch(const char *name, const char *cname,
71     void *obj, Int_t bsize)
72 loizides 1.1 {
73 loizides 1.2 // Add branch with name "name" into tree with name "tname" and set its address
74     // to object pointer for class name "cname" using a given buffer size and default splitlevel.
75 loizides 1.1
76 loizides 1.7 MyTree *t = AddOrGetMyTree(GetName());
77     TBranch *b = t->Bronch(name, cname, obj, bsize, fDefSL);
78     b->SetCompressionLevel(GetCompressLevel());
79 loizides 1.1 }
80    
81 loizides 1.9 //--------------------------------------------------------------------------------------------------
82 loizides 1.3 void TreeWriter::AddBranch(const char *name, void *obj, Int_t bsize)
83     {
84     // Add branch with name "name" into tree with name "tname" and set its address
85     // to object pointer using a given buffer size and default splitlevel.
86    
87     AddBranch(name, CName(obj), obj, bsize);
88     }
89    
90 loizides 1.9 //--------------------------------------------------------------------------------------------------
91 loizides 1.2 void TreeWriter::AddBranch(const char *name, const char *cname,
92     void *obj)
93 loizides 1.1 {
94 loizides 1.2 // Add branch with name "name" into tree with name "tname" and set its address
95 loizides 1.3 // to object pointer for class name "cname" using a default buffer size and splitlevel.
96 loizides 1.1
97 loizides 1.7 MyTree *t = AddOrGetMyTree(GetName());
98     TBranch *b = t->Bronch(name, cname, obj, fDefBrSize, fDefSL);
99     b->SetCompressionLevel(GetCompressLevel());
100 loizides 1.1 }
101    
102 loizides 1.9 //--------------------------------------------------------------------------------------------------
103 loizides 1.3 void TreeWriter::AddBranch(const char *name, void *obj)
104     {
105     // Add branch with name "name" into tree with name "tname" and set its address
106     // to object pointer using a default buffer size and splitlevel.
107    
108     AddBranch(name, CName(obj), obj);
109     }
110    
111 loizides 1.9 //--------------------------------------------------------------------------------------------------
112 loizides 1.2 void TreeWriter::AddBranchToTree(const char *tname, const char *name, const char *cname,
113     void *obj, Int_t bsize, Int_t level)
114 loizides 1.1 {
115 loizides 1.2 // Add branch with name "name" into tree with name "tname" and set its address
116     // to object pointer for class name "cname" using a given buffer size and splitlevel.
117 loizides 1.1
118 loizides 1.7 MyTree *t = AddOrGetMyTree(tname);
119     TBranch *b = t->Bronch(name, cname, obj, bsize, level);
120     b->SetCompressionLevel(GetCompressLevel());
121 loizides 1.2 }
122    
123 loizides 1.9 //--------------------------------------------------------------------------------------------------
124 loizides 1.3 void TreeWriter::AddBranchToTree(const char *tname, const char *name, void *obj,
125     Int_t bsize, Int_t level)
126     {
127     // Add branch with name "name" into tree with name "tname" and set its address
128     // to object pointer using a given buffer size and splitlevel.
129    
130     AddBranchToTree(tname, name, CName(obj), obj, bsize, level);
131     }
132    
133 loizides 1.9 //--------------------------------------------------------------------------------------------------
134 loizides 1.2 void TreeWriter::AddBranchToTree(const char *tname, const char *name, const char *cname,
135 loizides 1.3 void *obj, Int_t bsize)
136 loizides 1.2 {
137     // Add branch with name "name" into tree with name "tname" and set its address
138     // to object pointer for class name "cname" using a given buffer size and default splitlevel.
139    
140 loizides 1.7 MyTree *t = AddOrGetMyTree(tname);
141     TBranch *b = t->Bronch(name, cname, obj, bsize, fDefSL);
142     b->SetCompressionLevel(GetCompressLevel());
143 loizides 1.2 }
144    
145 loizides 1.9 //--------------------------------------------------------------------------------------------------
146 loizides 1.3 void TreeWriter::AddBranchToTree(const char *tname, const char *name, void *obj,
147     Int_t bsize)
148     {
149     // Add branch with name "name" into tree with name "tname" and set its address
150     // to object pointer using a given buffer size and default splitlevel.
151    
152     AddBranchToTree(tname, name, CName(obj), obj, bsize);
153     }
154    
155 loizides 1.9 //--------------------------------------------------------------------------------------------------
156 loizides 1.2 void TreeWriter::AddBranchToTree(const char *tname, const char *name, const char *cname,
157 loizides 1.3 void *obj)
158 loizides 1.2 {
159     // Add branch with name "name" into tree with name "tname" and set its address
160 loizides 1.3 // to object pointer for class name "cname" using a default buffer size and splitlevel.
161 loizides 1.2
162 loizides 1.7 MyTree *t = AddOrGetMyTree(tname);
163     TBranch *b = t->Bronch(name, cname, obj, fDefBrSize, fDefSL);
164     b->SetCompressionLevel(GetCompressLevel());
165 loizides 1.2 }
166    
167 loizides 1.9 //--------------------------------------------------------------------------------------------------
168 loizides 1.3 void TreeWriter::AddBranchToTree(const char *tname, const char *name, void *obj)
169     {
170     // Add branch with name "name" into tree with name "tname" and set its address
171     // to object pointer for class name "cname" using a default buffer size and splitlevel.
172    
173     AddBranchToTree(tname, name, CName(obj), obj);
174     }
175    
176 loizides 1.9 //--------------------------------------------------------------------------------------------------
177 loizides 1.13 void TreeWriter::AddTree(const char *tname)
178     {
179     // Add tree with name "name" into tree with name "tname".
180    
181     AddOrGetMyTree(tname);
182     }
183    
184     //--------------------------------------------------------------------------------------------------
185 loizides 1.2 MyTree *TreeWriter::AddOrGetMyTree(const char *tn)
186     {
187     // Add new tree if not present in array of trees or return
188     // present tree.
189    
190 loizides 1.13 MyTree *tree = GetMyTree(tn);
191 paus 1.4 if (tree)
192     return tree;
193 loizides 1.2
194     TDirectory::TContext context(fFile);
195     tree = new MyTree(tn, tn);
196     tree->SetDirectory(fFile);
197 loizides 1.12 if (fDoBranchRef)
198 loizides 1.8 tree->BranchRef();
199 loizides 1.2 fTrees.AddLast(tree);
200     return tree;
201 loizides 1.1 }
202    
203 loizides 1.9 //--------------------------------------------------------------------------------------------------
204 loizides 1.1 Bool_t TreeWriter::BeginEvent(Bool_t doreset)
205     {
206     // Prepare for the next event. If doreset or fDoObjNumReset is kTRUE
207     // store the current object number to be reset in FillEvent().
208    
209     if (!fIsInit) {
210     OpenFile();
211     }
212    
213 loizides 1.2 if (doreset || fDoObjNumReset) {
214 loizides 1.1 fEvtObjNum = TProcessID::GetObjectCount();
215     }
216    
217     return kTRUE;
218     }
219    
220 loizides 1.9 //--------------------------------------------------------------------------------------------------
221 loizides 1.1 void TreeWriter::CloseFile()
222     {
223 loizides 1.2 // Write tree(s) and close file.
224 loizides 1.1
225     if (!fIsInit) {
226     Fatal("CloseFile", "File was not opened, call OpenFile() first!");
227     return;
228     }
229    
230     TDirectory::TContext context(fFile); // cd fFile &&
231     // automatically restore gDirectory
232    
233 loizides 1.2 for (Int_t i=0;i<fTrees.GetEntries();++i) {
234     MyTree *mt = static_cast<MyTree*>(fTrees.At(i));
235     mt->Write(mt->GetName(),TObject::kOverwrite);
236 loizides 1.12
237     // backup and restore list of branch pointers from TRefTable (needed for autoloading)
238 bendavid 1.10 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 loizides 1.2 mt->SetDirectory(0);
248     }
249 loizides 1.1
250     fFile->Close();
251     delete fFile;
252     fFile = 0;
253    
254     fIsInit = kFALSE;
255     fFileNumber++;
256     }
257    
258 loizides 1.9 //--------------------------------------------------------------------------------------------------
259 loizides 1.3 const char *TreeWriter::CName(void *obj) const
260     {
261     // Dereference void* pointer into TObject* pointer
262    
263 loizides 1.14 TObject **sobj = static_cast<TObject**>(obj);
264     TObject *tobj = dynamic_cast<TObject*>(*sobj);
265 paus 1.4 if (tobj==0) {
266 loizides 1.13 Fatal("CName", "Given void* ptr can not be dereferenced into TObject*");
267 loizides 1.3 }
268     return tobj->ClassName();
269     }
270    
271 loizides 1.9 //--------------------------------------------------------------------------------------------------
272 loizides 1.13 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 loizides 1.1 Bool_t TreeWriter::EndEvent(Bool_t doreset)
290     {
291     // Store the event in the tree. If doreset or fDoObjNumReset is kTRUE
292     // restore the stored object number at the time BeginEvent(kTRUE)
293     // was called.
294    
295     if (!fIsInit) {
296     Fatal("EndEvent", "File is not open, did you call BeginEvent?");
297     return kFALSE;
298     }
299    
300 loizides 1.2 Int_t r = 0;
301     for (Int_t i=0;i<fTrees.GetEntries();++i) {
302     MyTree *mt = static_cast<MyTree*>(fTrees.At(i));
303 paus 1.4 if (mt->GetAutoFill()==0)
304     continue;
305 loizides 1.2 r += mt->Fill();
306     }
307 loizides 1.1
308 loizides 1.2 if (IsFull())
309 loizides 1.1 CloseFile();
310    
311     if (doreset || fDoObjNumReset) {
312     if (fEvtObjNum<0) {
313     Error("EndEvent", "Object counter is zero. Did you call BeginEvent(kTRUE)?");
314     } else {
315 loizides 1.11 // Reset the TRef table. Keep it from growing with each event (see doc)
316 loizides 1.1 TProcessID::SetObjectCount(fEvtObjNum);
317     }
318     }
319    
320     return (r >= 0);
321     }
322    
323 loizides 1.9 //--------------------------------------------------------------------------------------------------
324 loizides 1.2 Long64_t TreeWriter::GetEntries(const char *tn) const
325     {
326 loizides 1.6 // Return entries of tree with given name. If no tree is given, return sum of entries
327     // of all trees.
328 loizides 1.2
329     if (fTrees.GetEntries()==0) return -1;
330    
331     if (tn) {
332     const TTree *mt=GetTree(tn);
333 paus 1.4 if (mt)
334     return mt->GetEntries();
335     else
336     return -1;
337 loizides 1.2 }
338    
339     Long64_t ret = 0;
340     for (Int_t i=0;i<fTrees.GetEntries();++i) {
341     const MyTree *mt = static_cast<const MyTree*>(fTrees.At(i));
342     ret += mt->GetEntries();
343     }
344     return ret;
345     }
346    
347 loizides 1.9 //--------------------------------------------------------------------------------------------------
348 loizides 1.2 MyTree *mithep::TreeWriter::GetMyTree(const char *tn)
349     {
350     // Return MyTree with given name from array.
351    
352     if (fTrees.GetEntries()==0)
353     return 0;
354    
355     TObject *obj = 0;
356     if (tn==0) {
357     obj = fTrees.At(0);
358     } else {
359     obj = fTrees.FindObject(tn);
360     }
361    
362     if (obj)
363     return static_cast<MyTree*>(obj);
364     return 0;
365     }
366    
367 loizides 1.9 //--------------------------------------------------------------------------------------------------
368 loizides 1.2 const TTree *mithep::TreeWriter::GetTree(const char *tn) const
369     {
370     // Return TTree with given name from array.
371    
372     if (fTrees.GetEntries()==0)
373     return 0;
374    
375     TObject *obj = 0;
376     if (tn==0) {
377     obj = fTrees.At(0);
378     } else {
379     obj = fTrees.FindObject(tn);
380     }
381    
382     if (obj)
383     return dynamic_cast<const TTree*>(obj);
384     return 0;
385     }
386    
387 loizides 1.9 //--------------------------------------------------------------------------------------------------
388 loizides 1.2 TTree *mithep::TreeWriter::GetTree(const char *tn)
389     {
390     // Return TTree with given name from array.
391    
392     if (fTrees.GetEntries()==0)
393     return 0;
394    
395     TObject *obj = 0;
396     if (tn==0) {
397     obj = fTrees.At(0);
398     } else {
399     obj = fTrees.FindObject(tn);
400     }
401    
402     if (obj)
403     return dynamic_cast<TTree*>(obj);
404     return 0;
405     }
406    
407 loizides 1.9 //--------------------------------------------------------------------------------------------------
408 loizides 1.1 Bool_t TreeWriter::IsFull() const
409     {
410     // Check if the maximum file size has been reached.
411    
412     Long64_t entries = GetEntries();
413     if (entries < 1) return kFALSE;
414    
415     Long64_t avgSize = GetFileSize() / entries;
416    
417     if (avgSize < fkMinAvgSize)
418     avgSize = fkMinAvgSize;
419    
420     return (GetFileSize() + avgSize + fkMinFreeSpace) > fMaxSize;
421     }
422    
423 loizides 1.9 //--------------------------------------------------------------------------------------------------
424 loizides 1.1 void TreeWriter::OpenFile()
425     {
426     // Open the file and attach the tree.
427    
428     if (fIsInit) {
429     Fatal("OpenFile", "File is already open, call CloseFile first!");
430     return;
431     }
432    
433     TDirectory::TContext context(0);
434    
435     TString pathname=GetFullName();
436     gSystem->ExpandPathName(pathname);
437    
438     fFile = TFile::Open(pathname, "RECREATE");
439     if (fFile == 0) {
440     Fatal("OpenFile", "Could not open file %s", pathname.Data());
441     return;
442     }
443    
444     fFile->SetCompressionLevel(fCompressLevel);
445 loizides 1.2
446     for (Int_t i=0;i<fTrees.GetEntries();++i) {
447     MyTree *mt = static_cast<MyTree*>(fTrees.At(i));
448     mt->SetDirectory(fFile);
449     }
450    
451 loizides 1.1 fIsInit = kTRUE;
452     }
453    
454 loizides 1.9 //--------------------------------------------------------------------------------------------------
455 loizides 1.1 void TreeWriter::Print(Option_t *option) const
456     {
457     // Print the contents of the tree writer.
458    
459 loizides 1.2 if (option) {
460 loizides 1.1 cout << ClassName() << " with members " << endl;
461     cout << " fBaseURL: " << fBaseURL << endl;
462     cout << " fPreFix: " << fPrefix << endl;
463     cout << " fFileNumber: " << fFileNumber << endl;
464     cout << " fCompressLevel: " << fCompressLevel << endl;
465     cout << " fDefBrSize: " << fDefBrSize << endl;
466     cout << " fDefSL: " << fDefSL << endl;
467     cout << " fMaxSize: " << fMaxSize << endl;
468     cout << " fDoObjNumReset: " << fDoObjNumReset << endl;
469     return;
470     }
471    
472     cout << ClassName() << ": " << GetEntries()
473     << (GetEntries() == 1 ? " event" : " events") << endl;
474     }
475    
476 loizides 1.9 //--------------------------------------------------------------------------------------------------
477 loizides 1.2 void TreeWriter::SetAutoFill(const char *tn, Bool_t b)
478     {
479     // Set auto-fill mode of tree with given name.
480    
481 paus 1.4 if (fTrees.GetEntries()==0)
482     return;
483 loizides 1.2
484     MyTree *mt = GetMyTree(tn);
485 paus 1.4 if (!mt)
486     return;
487 loizides 1.2
488     mt->SetAutoFill(b);
489     }
490    
491 loizides 1.9 //--------------------------------------------------------------------------------------------------
492 loizides 1.5 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 loizides 1.9 //--------------------------------------------------------------------------------------------------
508 loizides 1.1 void TreeWriter::StoreObject(const TObject *obj)
509     {
510     // Store object next to tree in file. Used to store the
511     // settings of how the tree was created.
512    
513     if (!fIsInit) {
514     Fatal("StoreObject", "Tree is not created, call create first!");
515     return;
516     }
517    
518     if (!obj) {
519     Fatal("StoreObject", "Ptr to TObject is null!");
520     return;
521     }
522    
523     fFile->WriteTObject(obj,obj->GetName(),"WriteDelete");
524     }
525 loizides 1.13
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     }