ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataUtil/interface/TreeWriter.h
Revision: 1.10
Committed: Wed Sep 10 03:33:27 2008 UTC (16 years, 8 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_004
Changes since 1.9: +3 -3 lines
Log Message:
Cleanup

File Contents

# Content
1 //--------------------------------------------------------------------------------------------------
2 // $Id: TreeWriter.h,v 1.9 2008/06/24 14:05:03 loizides Exp $
3 //
4 // TreeWriter
5 //
6 // Class implementing a tree writing infrastructure. The class creates and maintaines
7 // a standard tree with name "tname" as given in the constructor. The tree is stored in
8 // a file or in multiple files if needed, where the file path name and maximum size can be
9 // specified. Using TreeWriter::AddBranch allows to add various branches to the tree.
10 // The user has to make sure that (s)he calls TreeWriter::BeginEvent and
11 // TreeWriter::EndEvent for every event before and after filling of the branch
12 // structures. Note that in addition to the standard tree one can create new
13 // tree(s) in the same file using the TreeWriter::AddBranchToTree member functions.
14 //
15 // Authors: C.Loizides
16 //--------------------------------------------------------------------------------------------------
17
18 #ifndef MITANA_DATAUTIL_TREEWRITER_H
19 #define MITANA_DATAUTIL_TREEWRITER_H
20
21 #include <TNamed.h>
22 #include <TString.h>
23 #include <TFile.h>
24 #include <TTree.h>
25 #include <TObjArray.h>
26
27 namespace mithep
28 {
29 class MyTree : public TTree
30 {
31 public:
32 MyTree(const char* name, const char* title, Int_t splitlevel = 99) :
33 TTree(name,title, splitlevel), fAutoFill(1) {}
34 Bool_t GetAutoFill() const { return fAutoFill; }
35 void SetAutoFill(Bool_t b) { fAutoFill = b; }
36 protected:
37 Bool_t fAutoFill; //!=true then fill automatically in TreeWriter (def=1)
38 };
39
40 class TreeWriter : public TNamed
41 {
42 public:
43 TreeWriter(const char *tname="MitTree", Bool_t doreset = kFALSE);
44 ~TreeWriter();
45
46 void AddBranch(const char *name, const char *cname,
47 void *obj, Int_t bsize, Int_t level);
48 void AddBranch(const char *name, void *obj, Int_t bsize, Int_t level);
49 void AddBranch(const char *name, const char *cname,
50 void *obj, Int_t bsize);
51 void AddBranch(const char *name, void *obj, Int_t bsize);
52 void AddBranch(const char *name, const char *cname,
53 void *obj);
54 void AddBranch(const char *name, void *obj);
55 void AddBranchToTree(const char *tname, const char *name, const char *cname,
56 void *obj, Int_t bsize, Int_t level);
57 void AddBranchToTree(const char *tname, const char *name, void *obj,
58 Int_t bsize, Int_t level);
59 void AddBranchToTree(const char *tname, const char *name, const char *cname,
60 void *obj, Int_t bsize);
61 void AddBranchToTree(const char *tname, const char *name, void *obj,
62 Int_t bsize);
63 void AddBranchToTree(const char *tname, const char *name, const char *cname,
64 void *obj);
65 void AddBranchToTree(const char *tname, const char *name, void *obj);
66 Bool_t BeginEvent(Bool_t doreset=kFALSE);
67 Bool_t EndEvent(Bool_t doreset=kFALSE);
68 const char *GetBaseURL() const {
69 return fBaseURL.IsNull() ? "." : fBaseURL; }
70 Int_t GetCompressLevel() const { return fCompressLevel; }
71 Long64_t GetEntries(const char *tn=0) const;
72 Long64_t GetFileSize() const {
73 return fFile != 0 ? fFile->GetEND() : 0; }
74 const TFile *GetFile() const { return fFile; }
75 const char *GetFileName() const {
76 return Form("%s_%03d.root", GetPrefix(), GetFileNumber()); }
77 UShort_t GetFileNumber() const { return fFileNumber; }
78 const char *GetFullName() const {
79 return Form("%s/%s", GetBaseURL(), GetFileName()); }
80 const char *GetPrefix() const { return fPrefix; }
81 const TTree *GetTree(const char *tn) const;
82 TTree *GetTree(const char *tn);
83 void Print(Option_t *option="") const;
84 void SetAutoFill(const char *tn, Bool_t b);
85 void SetBaseURL(const char *b) { fBaseURL = b; }
86 void SetCompressLevel(Int_t l) { fCompressLevel = l; }
87 void SetDefaultBrSize(Int_t s) { fDefBrSize=s; }
88 void SetDefaultSL(Int_t s) { fDefSL=s;}
89 void SetMaxSize(Long64_t s);
90 void SetPrefix(const char *p) { fPrefix = p; }
91 void StoreObject(const TObject *obj);
92
93 protected:
94 TString fBaseURL; // base url for tree storage
95 TString fPrefix; // prefix of file name
96 UShort_t fFileNumber; // current sequence number
97 Int_t fCompressLevel; // compression level used for TFile
98 Int_t fDefBrSize; // default buffer size for branches
99 Int_t fDefSL; // default split level for branches
100 Long64_t fMaxSize; // maximum file size for a file [Bytes]
101 const Long64_t fkMinFreeSpace; // minimum free space required for closing file
102 const Long64_t fkMinAvgSize; // minimum average entry size
103 Long64_t fEvtObjNum; // event object number offset (for TRef)
104 Bool_t fIsInit; // true if OpenFile() was called
105 Bool_t fDoObjNumReset; // true if obj. number resets automatically (def=0)
106 TFile *fFile; // file being written
107 TObjArray fTrees; // array of tree(s) being filled
108
109 mithep::MyTree *AddOrGetMyTree(const char *tn);
110 mithep::MyTree *GetMyTree(const char *tn);
111 Bool_t IsInit() const { return fIsInit; }
112 Bool_t IsFull() const;
113 void OpenFile();
114 void CloseFile();
115
116 private:
117 const char *CName(void *obj) const;
118
119 ClassDef(TreeWriter,0) // Tree writer class
120 };
121 }
122 #endif