ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/OutputMod.cc
Revision: 1.2
Committed: Tue Dec 2 09:34:17 2008 UTC (16 years, 5 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.1: +100 -53 lines
Log Message:
2nd version of partially functional module

File Contents

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