ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataUtil/src/TreeWriter.cc
Revision: 1.11
Committed: Sat Sep 27 06:14:05 2008 UTC (16 years, 7 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_004
Changes since 1.10: +3 -3 lines
Log Message:
Cosmetics.

File Contents

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