ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Selector.cc
Revision: 1.12
Committed: Thu Jun 11 08:59:32 2009 UTC (15 years, 10 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_018, Mit_017, Mit_017pre3, Mit_017pre2, Mit_017pre1, Mit_016, Mit_015b, Mit_015a, Mit_015, Mit_014e, Mit_014d, Mit_014c, Mit_014b, Mit_014a, Mit_014, Mit_014pre3, Mit_014pre2, Mit_014pre1, Mit_013d, Mit_013c, Mit_013b, Mit_013a, Mit_013, Mit_013pre1, Mit_012i, Mit_012h, Mit_012g, Mit_012f, Mit_012e, Mit_012d, Mit_012c, Mit_012b, Mit_012a, Mit_012, Mit_011a, Mit_011, Mit_010a, Mit_010, Mit_009c, Mit_009b, Mit_009a
Changes since 1.11: +14 -2 lines
Log Message:
Provide trash for event loop.

File Contents

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