ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/OutputMod.cc
Revision: 1.1
Committed: Mon Dec 1 17:42:23 2008 UTC (16 years, 5 months ago) by loizides
Content type: text/plain
Branch: MAIN
Log Message:
First sort of working version for OutputMod.

File Contents

# User Rev Content
1 loizides 1.1 // $Id: $
2    
3     #include "MitAna/TreeMod/interface/OutputMod.h"
4     #include "MitAna/DataUtil/interface/Debug.h"
5     #include "MitAna/DataTree/interface/Names.h"
6     #include "MitAna/DataUtil/interface/TreeWriter.h"
7     #include "MitAna/TreeMod/interface/TreeBranchLoader.h"
8    
9     using namespace mithep;
10     using namespace std;
11    
12     ClassImp(mithep::OutputMod)
13    
14     //--------------------------------------------------------------------------------------------------
15     OutputMod::OutputMod(const char *name, const char *title) :
16     BaseMod(name,title),
17     fTreeName(Names::gkEvtTreeName),
18     fPrefix("skimtest"),
19     fPathName("."),
20     fMaxSize(1024),
21     fCompLevel(9),
22     fSplitLevel(99),
23     fBranchSize(32*1024),
24     fDoReset(kFALSE),
25     fCheckDep(kTRUE),
26     fTreeWriter(0),
27     fNBranchesMax(1024)
28     {
29    
30     }
31    
32     //--------------------------------------------------------------------------------------------------
33     void OutputMod::BeginRun()
34     {
35     // Todo.
36    
37     }
38    
39     //--------------------------------------------------------------------------------------------------
40     void OutputMod::CheckAndAddBranch(const char *bname, const char *cname)
41     {
42     // Todo.
43    
44     if (IsAcceptedBranch(bname))
45     return;
46    
47     // populate regular expression list if this was not yet done
48     if (fCmdReList.size() != fCmdList.Entries()) {
49     for (UInt_t i=0; i<fCmdList.Entries(); ++i) {
50     const char *ptr = fCmdList.At(i)->c_str();
51     fCmdReList.push_back(TRegexp(ptr+5,kTRUE));
52     if (ptr[0]=='k')
53     fCmdDeList.push_back(kTRUE);
54     else
55     fCmdDeList.push_back(kFALSE);
56     }
57     }
58    
59     // decide whether given branch name should be kept or dropped
60     TString brname(bname);
61     Bool_t decision = kFALSE;
62     Bool_t decision_found = kFALSE;
63    
64     for (UInt_t i=0; i<fCmdList.Entries(); ++i) {
65     TRegexp &re(fCmdReList.at(i));
66     if (brname.Index(re) == kNPOS)
67     continue;
68     decision = fCmdDeList.at(i);
69     decision_found = kTRUE;
70     }
71    
72     if (!decision_found) { // no decision found: still drop branch
73     Warning("CheckAndAddBranch",
74     "No decision found for branch with name %s and class %s. Branch therefore dropped!",
75     bname, cname);
76     return;
77     }
78    
79     if (!decision) { // drop branch according to request
80     Info("CheckAndAddBranch", "Dropped branch with name %s and class %s.", bname, cname);
81     return;
82     }
83    
84     // add branch to accepted branch list
85     Info("CheckAndAddBranch", "Kept branch with name %s and class %s.", bname, cname);
86    
87     fBrNameList.AddCopy(string(bname));
88     fBrClassList.AddCopy(string(cname));
89    
90     // request branch
91     RequestBranch(bname);
92     }
93    
94     //--------------------------------------------------------------------------------------------------
95     void OutputMod::RequestBranch(const char *bname)
96     {
97     // Request given branch from TAM.
98    
99     if (GetNBranches()>=fNBranchesMax) {
100     Error("RequestBranch", "Can not request branch for %bname"
101     "since maximum number of branches [%d] is reached", bname, fNBranchesMax);
102     return;
103     }
104    
105     fBranches[GetNBranches()-1] = 0;
106     ReqBranch(bname, fBranches[GetNBranches()-1]);
107     }
108    
109     //--------------------------------------------------------------------------------------------------
110     void OutputMod::EndRun()
111     {
112     // Todo.
113    
114     }
115    
116     //--------------------------------------------------------------------------------------------------
117     Bool_t OutputMod::IsAcceptedBranch(const char *bname)
118     {
119     // Return true if given branch is already in branch list. Also return true if a special
120     // branch like the "EventHeader" branch is reqested.
121    
122     // search in branch list
123     for (UInt_t i=0; i<GetNBranches(); ++i) {
124     if (fBrNameList.At(i)->compare(bname) == 0)
125     return kTRUE;
126     }
127    
128     // check if special branch that we take care of ourselves
129     string name(bname);
130     if (name.compare("EventHeader") == 0) {
131     return kTRUE;
132     }
133    
134     return kFALSE;
135     }
136    
137     //--------------------------------------------------------------------------------------------------
138     Bool_t OutputMod::Notify()
139     {
140     // Todo
141    
142     TTree *tree=const_cast<TTree*>(GetSel()->GetTree());
143     if (!tree)
144     return kFALSE;
145    
146     TObjArray *arr = tree->GetTree()->GetListOfBranches();
147     if (!arr)
148     return kFALSE;
149    
150     for (Int_t i=0; i<arr->GetEntries(); ++i) {
151     TBranch *br = dynamic_cast<TBranch*>(arr->At(i));
152     if (!br && !br->GetMother())
153     continue;
154     br = br->GetMother();
155     TClass *cls = TClass::GetClass(br->GetClassName());
156     if (!cls)
157     continue;
158    
159     if (!cls->InheritsFrom("TObject")) {
160     Warning("Notify", "Found branch %s where class %s does not derive from TObject.",
161     br->GetName(), br->GetClassName());
162     continue;
163     }
164    
165     CheckAndAddBranch(br->GetName(), br->GetClassName());
166     }
167    
168     return kTRUE;
169     }
170    
171     //--------------------------------------------------------------------------------------------------
172     void OutputMod::LoadBranches()
173     {
174     // Loop over requested branches and load them.
175    
176     for (UInt_t i=0; i<GetNBranches(); ++i) {
177     LoadBranch(fBrNameList.At(i)->c_str());
178     }
179     }
180    
181     //--------------------------------------------------------------------------------------------------
182     void OutputMod::Process()
183     {
184     // Pre and post event processing at once?!
185    
186     fTreeWriter->BeginEvent(fDoReset);
187    
188     if (GetNEventsProcessed() == 0 && fCheckDep) {
189     ResolveDep(kTRUE);
190     }
191    
192     LoadBranches();
193     // load additional branches
194     //LoadBranch("EventHeader");
195    
196     if (GetNEventsProcessed() == 0) {
197     SetupBranches();
198     }
199    
200     IncNEventsProcessed();
201     fTreeWriter->EndEvent(fDoReset);
202     }
203    
204     //--------------------------------------------------------------------------------------------------
205     void OutputMod::SetupBranches()
206     {
207     // Setup branches in tree writer.
208    
209     for (UInt_t i=0; i<GetNBranches(); ++i) {
210     const char *bname = fBrNameList.At(i)->c_str();
211     const char *cname = fBrClassList.At(i)->c_str();
212     if (!fBranches[i]) {
213     Error("SetupBranches", "Pointer for branch with name %s and class %s is NULL.",
214     bname, cname);
215     continue;
216     }
217     fTreeWriter->AddBranch(bname,cname,&fBranches[i]);
218     }
219     }
220    
221     //--------------------------------------------------------------------------------------------------
222     void OutputMod::SlaveBegin()
223     {
224     // Todo
225    
226     // request here branches we want
227     //ReqBranch("EventHeader", fEventHeader);
228    
229     // setup tree writer
230     fTreeWriter = new TreeWriter(fTreeName, kFALSE);
231     fTreeWriter->SetBaseURL(fPathName);
232     fTreeWriter->SetPrefix(fPrefix);
233     fTreeWriter->SetMaxSize(fMaxSize*1024*1024);
234     fTreeWriter->SetCompressLevel(fCompLevel);
235     fTreeWriter->SetDefaultSL(fSplitLevel);
236     fTreeWriter->SetDefaultBrSize(fBranchSize);
237     fTreeWriter->AddTree(fTreeName);
238     fTreeWriter->DoBranchRef(fTreeName);
239    
240     // deal here with published objects
241    
242     // create TObject space for TAM
243     fBranches = new TObject*[fNBranchesMax];
244     }
245    
246     //--------------------------------------------------------------------------------------------------
247     void OutputMod::SlaveTerminate()
248     {
249     // Todo
250    
251     delete fTreeWriter;
252     fTreeWriter = 0;
253    
254     delete[] fBranches;
255     }
256    
257     //--------------------------------------------------------------------------------------------------
258    
259     void OutputMod::ResolveDep(Bool_t solve)
260     {
261     // Todo
262    
263     const THashTable &ht = GetSel()->GetBranchTable();
264    
265     TIterator *iter = ht.MakeIterator();
266     const TAMBranchInfo *next = dynamic_cast<const TAMBranchInfo*>(iter->Next());
267    
268     while (next) {
269     const TAMBranchInfo *cur = next;
270     next = dynamic_cast<const TAMBranchInfo*>(iter->Next());
271     Bool_t isloaded = cur->IsLoaded();
272     if (!isloaded)
273     continue;
274    
275     const char *bname = cur->GetName();
276     if (IsAcceptedBranch(bname))
277     continue;
278    
279     TreeBranchLoader *loader = dynamic_cast<TreeBranchLoader*>(cur->GetLoader());
280     if (!loader)
281     continue;
282    
283     TBranch *br = loader->GetBranch();
284     if (!br)
285     continue;
286    
287     const char *cname = br->GetClassName();
288    
289     if (solve) {
290     Info("ResolveDep", "Resolving dependency for auto-loaded branch %s and class %s",
291     bname,cname);
292     CheckAndAddBranch(bname, cname);
293     } else {
294     Warning("ResolveDep", "Unresolved dependency for auto-loaded branch %s and class %s",
295     bname,cname);
296     }
297     }
298     }