ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Selector.cc
Revision: 1.9
Committed: Fri Dec 12 16:57:42 2008 UTC (16 years, 4 months ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_006b, Mit_006a
Changes since 1.8: +4 -1 lines
Log Message:
Added read caching for the events tree, enabled by default set to 128mb

File Contents

# User Rev Content
1 bendavid 1.9 // $Id: Selector.cc,v 1.8 2008/12/04 14:13:44 loizides Exp $
2 loizides 1.1
3     #include "MitAna/TreeMod/interface/Selector.h"
4 loizides 1.2 #include "MitAna/DataTree/interface/Names.h"
5 loizides 1.6 #include "MitAna/TreeMod/interface/OutputMod.h"
6 loizides 1.2 #include <TProcessID.h>
7     #include <TFile.h>
8     #include <TTree.h>
9    
10 loizides 1.1 using namespace mithep;
11    
12     ClassImp(mithep::Selector)
13    
14 loizides 1.4 //--------------------------------------------------------------------------------------------------
15 loizides 1.2 Selector::Selector() :
16     fDoRunInfo(kTRUE),
17 loizides 1.5 fEvtHdrName(Names::gkEvtHeaderBrn),
18     fRunTreeName(Names::gkRunTreeName),
19     fRunInfoName(Names::gkRunInfoBrn),
20 loizides 1.6 fAllEvtHdrBrn(Names::gkAllEvtHeaderBrn),
21 loizides 1.5 fLATreeName(Names::gkLATreeName),
22     fLAHdrName(Names::gkLAHeaderBrn),
23 loizides 1.6 fAllEvtTreeName(Names::gkAllEvtTreeName),
24 loizides 1.2 fRunTree(0),
25     fEventHeader(0),
26     fRunInfo(0),
27     fLATree(0),
28 loizides 1.3 fLAHeader(0),
29     fCurRunNum(UInt_t(-1))
30 loizides 1.1 {
31 loizides 1.5 // Constructor.
32 loizides 1.7
33     fOutputMods.SetOwner(kFALSE);
34 loizides 1.1 }
35    
36 loizides 1.4 //--------------------------------------------------------------------------------------------------
37 loizides 1.1 Selector::~Selector()
38     {
39 loizides 1.5 // Destructor.
40    
41     fRunTree = 0;
42     fEventHeader = 0;
43     fRunInfo = 0;
44     fLATree = 0;
45     fLAHeader = 0;
46     fCurRunNum = UInt_t(-1);
47 loizides 1.1 }
48 loizides 1.2
49 loizides 1.4 //--------------------------------------------------------------------------------------------------
50 loizides 1.2 Bool_t Selector::BeginRun()
51     {
52     // Determines whether we are at beginning of a new run.
53    
54     if (!fDoRunInfo)
55     return kFALSE;
56    
57 loizides 1.5 LoadBranch(fEvtHdrName);
58 loizides 1.2 if (!fEventHeader)
59     return kFALSE;
60    
61     if (ConsistentRunNum())
62     return kFALSE;
63    
64     UpdateRunInfo();
65     return ValidRunNum();
66     }
67    
68 loizides 1.4 //--------------------------------------------------------------------------------------------------
69 loizides 1.2 Bool_t Selector::EndRun()
70     {
71 loizides 1.6 // Determines whether we are at the end of a run. Also, do treat special case of output module
72 loizides 1.7 // here so that in any case it can process the event.
73    
74     if (IsAModAborted() || IsEventAborted()) { // deal with output module if needed: Do this here,
75     TIter it(fOutputMods.MakeIterator()); // avoids having to copy/rewrite large parts of
76     OutputMod *o = 0; // TAMSelector::Process and interaction with TAModule
77     while ((o=static_cast<OutputMod*>(it.Next())) != 0)
78     o->ProcessAll();
79     }
80 loizides 1.2
81     if (!fDoRunInfo)
82     return kFALSE;
83    
84     if (!ValidRunNum())
85     return kFALSE;
86    
87     // determine if run will end
88     if (fCurEvt+1==fTree->GetTree()->GetEntries())
89     return kTRUE; // we are at last entry in current file
90    
91     fLAHeader=0;
92     Int_t ret = fLATree->GetEvent(fCurEvt);
93     if (ret<0 || fLAHeader==0) {
94     Error("EndRun", "Could not get entry lookahead entry for next event,"
95     " assuming end of run %ld reached!", fCurRunNum);
96     return kTRUE;
97     }
98    
99     return (fLAHeader->RunNum()!=fCurRunNum);
100     }
101    
102 loizides 1.4 //--------------------------------------------------------------------------------------------------
103 loizides 1.2 Bool_t Selector::Notify()
104     {
105 loizides 1.5 // The Notify() function is called when a new file is opened.
106     // Here, we check for a new run info tree.
107 loizides 1.2
108     if (!GetCurrentFile())
109     return kTRUE;
110    
111     if (fDoRunInfo)
112     UpdateRunInfoTree();
113    
114 bendavid 1.9 //needed to force caching to occur for all files
115     fTree->GetTree()->SetCacheSize(fTree->GetCacheSize());
116    
117 loizides 1.2 return TAMSelector::Notify();
118     }
119    
120 loizides 1.4 //--------------------------------------------------------------------------------------------------
121 loizides 1.7 void Selector::SearchOutputMods(const TAModule *mod)
122     {
123     // Search for output module among list of modules.
124    
125     if (!mod)
126     return;
127    
128     const OutputMod *o = dynamic_cast<const OutputMod*>(mod);
129     if (o)
130     fOutputMods.Add(const_cast<OutputMod*>(o));
131    
132     const TList *tasks = mod->GetSubModules();
133     if (!tasks)
134     return;
135    
136     TIter it(tasks->MakeIterator());
137     TObject *ob = 0;
138     while ((ob=it.Next()) != 0) {
139     TAModule *nmod = dynamic_cast<TAModule*>(ob);
140     if (nmod)
141     SearchOutputMods(nmod);
142     }
143     }
144    
145     //--------------------------------------------------------------------------------------------------
146 loizides 1.6 void Selector::SlaveBegin(TTree *tree)
147 loizides 1.2 {
148     // The SlaveBegin() function is called after the Begin() function and can be used to setup
149     // analysis on the slaves. Here, we request the event header branch.
150    
151     if (fDoRunInfo) {
152 loizides 1.7 ReqBranch(fEvtHdrName, fEventHeader);
153 loizides 1.2 }
154    
155 loizides 1.7 SearchOutputMods(GetTopModule());
156    
157 loizides 1.2 TAMSelector::SlaveBegin(tree);
158     }
159    
160 loizides 1.4 //--------------------------------------------------------------------------------------------------
161 loizides 1.2 void Selector::UpdateRunInfo()
162     {
163     // Update the run info to be consistent with the information from the event header.
164    
165     fRunInfo = 0;
166    
167     if (!fRunTree)
168     return;
169    
170     Int_t ret = fRunTree->GetEvent(fEventHeader->RunEntry());
171     if (ret<0 || fRunInfo==0) {
172     Error("UpdateRunInfo", "Error updating run info for run %ld, entry %ld, return value %d",
173     fEventHeader->RunNum(), fEventHeader->RunEntry(), ret);
174     return;
175     }
176    
177     fCurRunNum = fEventHeader->RunNum();
178     if (!ValidRunInfo()) {
179     Error("UpdateRunInfo", "Error updating run info, run values do not match %d %d",
180     fCurRunNum, fRunInfo->RunNum());
181     return;
182     }
183     }
184    
185 loizides 1.4 //--------------------------------------------------------------------------------------------------
186 loizides 1.2 void Selector::UpdateRunInfoTree()
187     {
188     // Get the run info tree from current file and set our branches.
189    
190     // first erase previous entries
191     fRunTree = 0;
192     fRunInfo = 0;
193     fCurRunNum = UInt_t(-1);
194     fLATree = 0;
195     fLAHeader = 0;
196    
197     // get current file and retrieve trees
198     TFile *f = GetCurrentFile();
199     if (!f) {
200     Error("UpdateRunInfoTree", "Ptr to current file is null");
201     return;
202     }
203    
204     // run info tree
205 loizides 1.5 fRunTree = dynamic_cast<TTree*>(f->Get(fRunTreeName));
206 loizides 1.2 if (!fRunTree) {
207 loizides 1.5 Fatal("UpdateRunInfoTree", "Can not find run info tree with name %s", fRunTreeName.Data());
208 loizides 1.2 }
209    
210     // set branches
211 loizides 1.5 if (fRunTree->GetBranch(fRunInfoName)) {
212     fRunTree->SetBranchAddress(fRunInfoName, &fRunInfo);
213 loizides 1.2 } else {
214 loizides 1.5 Fatal("UpdateRunInfoTree", "Can not find run info branch with name %s", fRunInfoName.Data());
215 loizides 1.2 }
216    
217     // look-ahead tree
218 loizides 1.5 fLATree = dynamic_cast<TTree*>(f->Get(fLATreeName));
219 loizides 1.2 if (!fLATree) {
220 loizides 1.5 Fatal("UpdateRunInfoTree", "Can not find look-ahead tree with name %s", fLATreeName.Data());
221 loizides 1.2 }
222    
223     // set branches
224 loizides 1.5 if (fLATree->GetBranch(fLAHdrName)) {
225     fLATree->SetBranchAddress(fLAHdrName, &fLAHeader);
226 loizides 1.2 } else {
227 loizides 1.5 Fatal("UpdateRunInfoTree", "Can not find look-ahead branch with name %s", fLAHdrName.Data());
228 loizides 1.2 }
229     }