ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataUtil/src/TreeWriter.cc
Revision: 1.9
Committed: Thu Jul 3 08:22:18 2008 UTC (16 years, 10 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: MITHEP_2_0_x
Changes since 1.8: +27 -30 lines
Log Message:
Coding conventions.

File Contents

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