ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataUtil/interface/TreeWriter.h
Revision: 1.14
Committed: Sun Mar 15 11:17:04 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre2
Changes since 1.13: +61 -21 lines
Log Message:
Added std::string setters

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.14 // $Id: TreeWriter.h,v 1.13 2008/12/10 11:30:32 loizides Exp $
3 loizides 1.1 //
4     // TreeWriter
5     //
6 paus 1.5 // Class implementing a tree writing infrastructure. The class creates and maintaines
7 loizides 1.3 // 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 loizides 1.1 // 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 loizides 1.3 // 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 loizides 1.1 //
15     // Authors: C.Loizides
16     //--------------------------------------------------------------------------------------------------
17    
18 loizides 1.10 #ifndef MITANA_DATAUTIL_TREEWRITER_H
19     #define MITANA_DATAUTIL_TREEWRITER_H
20 loizides 1.8
21 loizides 1.14 #include <string>
22 loizides 1.8 #include <TNamed.h>
23     #include <TString.h>
24     #include <TFile.h>
25     #include <TTree.h>
26     #include <TObjArray.h>
27    
28 loizides 1.2 namespace mithep
29     {
30 loizides 1.14 using std::string;
31    
32 loizides 1.3 class MyTree : public TTree
33     {
34     public:
35     MyTree(const char* name, const char* title, Int_t splitlevel = 99) :
36 loizides 1.12 TTree(name,title, splitlevel), fAutoFill(1) {}
37 paus 1.5 Bool_t GetAutoFill() const { return fAutoFill; }
38     void SetAutoFill(Bool_t b) { fAutoFill = b; }
39 loizides 1.11
40 loizides 1.3 protected:
41     Bool_t fAutoFill; //!=true then fill automatically in TreeWriter (def=1)
42     };
43 paus 1.5
44 loizides 1.1 class TreeWriter : public TNamed
45     {
46     public:
47     TreeWriter(const char *tname="MitTree", Bool_t doreset = kFALSE);
48     ~TreeWriter();
49    
50 loizides 1.3 void AddBranch(const char *name, const char *cname,
51     void *obj, Int_t bsize, Int_t level);
52 loizides 1.14 void AddBranch(const string &name, const string &cname,
53     void *obj, Int_t bsize, Int_t level)
54     { AddBranch(name.c_str(),cname.c_str(),obj,bsize,level); }
55 loizides 1.4 void AddBranch(const char *name, void *obj, Int_t bsize, Int_t level);
56 loizides 1.14 void AddBranch(const string &name, void *obj, Int_t bsize, Int_t level)
57     { AddBranch(name.c_str(),obj,bsize,level); }
58 loizides 1.3 void AddBranch(const char *name, const char *cname,
59     void *obj, Int_t bsize);
60 loizides 1.14 void AddBranch(const string &name, const string &cname,
61     void *obj, Int_t bsize)
62     { AddBranch(name.c_str(),cname.c_str(),obj,bsize); }
63 loizides 1.4 void AddBranch(const char *name, void *obj, Int_t bsize);
64 loizides 1.14 void AddBranch(const string &name, void *obj, Int_t bsize)
65     { AddBranch(name.c_str(),obj, bsize); }
66     void AddBranch(const char *name, const char *cname, void *obj);
67     void AddBranch(const string &name, const string &cname, void *obj)
68     { AddBranch(name.c_str(),cname.c_str(),obj); }
69 loizides 1.4 void AddBranch(const char *name, void *obj);
70 loizides 1.14 void AddBranch(const string &name, void *obj)
71     { AddBranch(name.c_str(),obj); }
72 loizides 1.3 void AddBranchToTree(const char *tname, const char *name, const char *cname,
73     void *obj, Int_t bsize, Int_t level);
74 loizides 1.14 void AddBranchToTree(const string &tn, const string &n, const string &cn,
75     void *obj, Int_t bsize, Int_t level)
76     { AddBranchToTree(tn.c_str(),n.c_str(),cn.c_str(),obj,bsize,level); }
77 loizides 1.4 void AddBranchToTree(const char *tname, const char *name, void *obj,
78     Int_t bsize, Int_t level);
79 loizides 1.14 void AddBranchToTree(const string &tn, const string &n, void *obj,
80     Int_t bsize, Int_t level)
81     { AddBranchToTree(tn.c_str(),n.c_str(),obj,bsize,level); }
82 loizides 1.3 void AddBranchToTree(const char *tname, const char *name, const char *cname,
83     void *obj, Int_t bsize);
84 loizides 1.14 void AddBranchToTree(const string &tn, const string &n, const string &cn,
85     void *obj, Int_t bsize)
86     { AddBranchToTree(tn.c_str(),n.c_str(),cn.c_str(),obj,bsize); }
87 loizides 1.4 void AddBranchToTree(const char *tname, const char *name, void *obj,
88     Int_t bsize);
89 loizides 1.14 void AddBranchToTree(const string &tn, const string &n, void *obj,
90     Int_t bsize)
91     { AddBranchToTree(tn.c_str(),n.c_str(),obj,bsize); }
92 loizides 1.3 void AddBranchToTree(const char *tname, const char *name, const char *cname,
93     void *obj);
94 loizides 1.14 void AddBranchToTree(const string &tn, const string &n, const string &cn,
95     void *obj)
96     { AddBranchToTree(tn.c_str(),n.c_str(),cn.c_str(),obj); }
97 loizides 1.4 void AddBranchToTree(const char *tname, const char *name, void *obj);
98 loizides 1.14 void AddBranchToTree(const string &tn, const string &n, void *obj)
99     { AddBranchToTree(tn.c_str(),n.c_str(),obj); }
100 loizides 1.12 void AddTree(const char *tname);
101 loizides 1.14 void AddTree(const string &tname) { AddTree(tname.c_str()); }
102 loizides 1.1 Bool_t BeginEvent(Bool_t doreset=kFALSE);
103 loizides 1.12 void DoBranchRef(const char *tn);
104 loizides 1.1 Bool_t EndEvent(Bool_t doreset=kFALSE);
105 loizides 1.11 const char *GetBaseURL() const
106 loizides 1.14 { return fBaseURL.IsNull()?".":fBaseURL; }
107     Int_t GetCompressLevel() const { return fCompressLevel; }
108     Bool_t GetDoObjNumReset() const { return fDoObjNumReset; }
109     Bool_t GetDoBranchRef() const { return fDoBranchRef; }
110 loizides 1.3 Long64_t GetEntries(const char *tn=0) const;
111 loizides 1.14 Long64_t GetFileSize() const
112     { return fFile!=0?fFile->GetEND():0; }
113     const TFile *GetFile() const { return fFile; }
114 paus 1.5 const char *GetFileName() const {
115 loizides 1.14 return Form("%s_%03d.root",GetPrefix(),GetFileNumber()); }
116     UShort_t GetFileNumber() const { return fFileNumber; }
117 paus 1.5 const char *GetFullName() const {
118 loizides 1.14 return Form("%s/%s",GetBaseURL(),GetFileName()); }
119     const char *GetPrefix() const { return fPrefix; }
120 loizides 1.11 const TTree *GetTree(const char *tn=0) const;
121     TTree *GetTree(const char *tn=0);
122 loizides 1.14 const TTree *GetTree(const string &tn) const { return GetTree(tn.c_str()); }
123     TTree *GetTree(const string &tn) { return GetTree(tn.c_str()); }
124 loizides 1.3 void Print(Option_t *option="") const;
125     void SetAutoFill(const char *tn, Bool_t b);
126 loizides 1.14 void SetAutoFill(const string &tn, Bool_t b)
127     { SetAutoFill(tn.c_str(), b); }
128     void SetBaseURL(const char *b) { fBaseURL = b; }
129     void SetBaseURL(const string &b) { fBaseURL = b.c_str(); }
130     void SetCompressLevel(Int_t l) { fCompressLevel = l; }
131     void SetDefaultBrSize(Int_t s) { fDefBrSize=s; }
132     void SetDefaultSL(Int_t s) { fDefSL=s; }
133     void SetDoObjNumReset(Bool_t b) { fDoObjNumReset = b; }
134     void SetDoBranchRef(Bool_t b) { fDoBranchRef = b; }
135 loizides 1.7 void SetMaxSize(Long64_t s);
136 loizides 1.14 void SetPrefix(const char *p) { fPrefix = p; }
137     void SetPrefix(const string &p) { fPrefix = p.c_str(); }
138 loizides 1.1 void StoreObject(const TObject *obj);
139 loizides 1.12 void Terminate();
140 loizides 1.1
141     protected:
142 loizides 1.11 TString fBaseURL; //base url for tree storage
143     TString fPrefix; //prefix of file name
144     UShort_t fFileNumber; //current sequence number
145     Int_t fCompressLevel; //compression level used for TFile
146     Int_t fDefBrSize; //default buffer size for branches
147     Int_t fDefSL; //default split level for branches
148     Long64_t fMaxSize; //maximum file size for a file [Bytes]
149     const Long64_t fkMinFreeSpace; //minimum free space required for closing file
150     const Long64_t fkMinAvgSize; //minimum average entry size
151     Long64_t fEvtObjNum; //event object number offset (for TRef)
152     Bool_t fIsInit; //true if OpenFile() was called
153     Bool_t fDoObjNumReset; //true if obj. number resets automatically (def=0)
154     Bool_t fDoBranchRef; //true if BranchRef is called automatically (def=0)
155     TFile *fFile; //file being written
156     TObjArray fTrees; //array of tree(s) being filled
157 loizides 1.3
158     mithep::MyTree *AddOrGetMyTree(const char *tn);
159     mithep::MyTree *GetMyTree(const char *tn);
160     Bool_t IsInit() const { return fIsInit; }
161     Bool_t IsFull() const;
162 loizides 1.1 void OpenFile();
163     void CloseFile();
164    
165 loizides 1.4 private:
166     const char *CName(void *obj) const;
167    
168 loizides 1.13 ClassDef(TreeWriter, 0) // Tree writer class
169 loizides 1.1 };
170 loizides 1.6 }
171     #endif